code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
import os import torch from ..logging import get_logger from .constants import FSDP_PYTORCH_VERSION, MODEL_NAME, OPTIMIZER_NAME from .versions import is_torch_version if is_torch_version('''>=''', FSDP_PYTORCH_VERSION): import torch.distributed.checkpoint as dist_cp from torch.distributed.checkpoint.default_planner import DefaultLoadPlanner, DefaultSavePlanner from torch.distributed.checkpoint.optimizer import load_sharded_optimizer_state_dict from torch.distributed.fsdp.fully_sharded_data_parallel import FullyShardedDataParallel as FSDP from torch.distributed.fsdp.fully_sharded_data_parallel import StateDictType _a = get_logger(__name__) def __A ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=0 )-> Dict: """simple docstring""" os.makedirs(SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) with FSDP.state_dict_type( SCREAMING_SNAKE_CASE__ , fsdp_plugin.state_dict_type , fsdp_plugin.state_dict_config , fsdp_plugin.optim_state_dict_config ): _UpperCAmelCase = model.state_dict() if fsdp_plugin.state_dict_type == StateDictType.FULL_STATE_DICT: _UpperCAmelCase = F"""{MODEL_NAME}.bin""" if model_index == 0 else F"""{MODEL_NAME}_{model_index}.bin""" _UpperCAmelCase = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if accelerator.process_index == 0: logger.info(F"""Saving model to {output_model_file}""" ) torch.save(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) logger.info(F"""Model saved to {output_model_file}""" ) elif fsdp_plugin.state_dict_type == StateDictType.LOCAL_STATE_DICT: _UpperCAmelCase = ( F"""{MODEL_NAME}_rank{accelerator.process_index}.bin""" if model_index == 0 else F"""{MODEL_NAME}_{model_index}_rank{accelerator.process_index}.bin""" ) _UpperCAmelCase = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) logger.info(F"""Saving model to {output_model_file}""" ) torch.save(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) logger.info(F"""Model saved to {output_model_file}""" ) elif fsdp_plugin.state_dict_type == StateDictType.SHARDED_STATE_DICT: _UpperCAmelCase = os.path.join(SCREAMING_SNAKE_CASE__ , F"""{MODEL_NAME}_{model_index}""" ) os.makedirs(SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) logger.info(F"""Saving model to {ckpt_dir}""" ) _UpperCAmelCase = {'model': state_dict} dist_cp.save_state_dict( state_dict=SCREAMING_SNAKE_CASE__ , storage_writer=dist_cp.FileSystemWriter(SCREAMING_SNAKE_CASE__ ) , planner=DefaultSavePlanner() , ) logger.info(F"""Model saved to {ckpt_dir}""" ) def __A ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=0 )-> Union[str, Any]: """simple docstring""" accelerator.wait_for_everyone() with FSDP.state_dict_type( SCREAMING_SNAKE_CASE__ , fsdp_plugin.state_dict_type , fsdp_plugin.state_dict_config , fsdp_plugin.optim_state_dict_config ): if fsdp_plugin.state_dict_type == StateDictType.FULL_STATE_DICT: if type(SCREAMING_SNAKE_CASE__ ) != FSDP and accelerator.process_index != 0: if not fsdp_plugin.sync_module_states: raise ValueError( 'Set the `sync_module_states` flag to `True` so that model states are synced across processes when ' 'initializing FSDP object' ) return _UpperCAmelCase = F"""{MODEL_NAME}.bin""" if model_index == 0 else F"""{MODEL_NAME}_{model_index}.bin""" _UpperCAmelCase = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) logger.info(F"""Loading model from {input_model_file}""" ) _UpperCAmelCase = torch.load(SCREAMING_SNAKE_CASE__ ) logger.info(F"""Model loaded from {input_model_file}""" ) elif fsdp_plugin.state_dict_type == StateDictType.LOCAL_STATE_DICT: _UpperCAmelCase = ( F"""{MODEL_NAME}_rank{accelerator.process_index}.bin""" if model_index == 0 else F"""{MODEL_NAME}_{model_index}_rank{accelerator.process_index}.bin""" ) _UpperCAmelCase = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) logger.info(F"""Loading model from {input_model_file}""" ) _UpperCAmelCase = torch.load(SCREAMING_SNAKE_CASE__ ) logger.info(F"""Model loaded from {input_model_file}""" ) elif fsdp_plugin.state_dict_type == StateDictType.SHARDED_STATE_DICT: _UpperCAmelCase = ( os.path.join(SCREAMING_SNAKE_CASE__ , F"""{MODEL_NAME}_{model_index}""" ) if F"""{MODEL_NAME}""" not in input_dir else input_dir ) logger.info(F"""Loading model from {ckpt_dir}""" ) _UpperCAmelCase = {'model': model.state_dict()} dist_cp.load_state_dict( state_dict=SCREAMING_SNAKE_CASE__ , storage_reader=dist_cp.FileSystemReader(SCREAMING_SNAKE_CASE__ ) , planner=DefaultLoadPlanner() , ) _UpperCAmelCase = state_dict['model'] logger.info(F"""Model loaded from {ckpt_dir}""" ) model.load_state_dict(SCREAMING_SNAKE_CASE__ ) def __A ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=0 )-> Tuple: """simple docstring""" os.makedirs(SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) with FSDP.state_dict_type( SCREAMING_SNAKE_CASE__ , fsdp_plugin.state_dict_type , fsdp_plugin.state_dict_config , fsdp_plugin.optim_state_dict_config ): _UpperCAmelCase = FSDP.optim_state_dict(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if fsdp_plugin.state_dict_type == StateDictType.FULL_STATE_DICT: if accelerator.process_index == 0: _UpperCAmelCase = ( F"""{OPTIMIZER_NAME}.bin""" if optimizer_index == 0 else F"""{OPTIMIZER_NAME}_{optimizer_index}.bin""" ) _UpperCAmelCase = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) logger.info(F"""Saving Optimizer state to {output_optimizer_file}""" ) torch.save(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) logger.info(F"""Optimizer state saved in {output_optimizer_file}""" ) else: _UpperCAmelCase = os.path.join(SCREAMING_SNAKE_CASE__ , F"""{OPTIMIZER_NAME}_{optimizer_index}""" ) os.makedirs(SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) logger.info(F"""Saving Optimizer state to {ckpt_dir}""" ) dist_cp.save_state_dict( state_dict={'optimizer': optim_state} , storage_writer=dist_cp.FileSystemWriter(SCREAMING_SNAKE_CASE__ ) , planner=DefaultSavePlanner() , ) logger.info(F"""Optimizer state saved in {ckpt_dir}""" ) def __A ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=0 )-> Optional[Any]: """simple docstring""" accelerator.wait_for_everyone() with FSDP.state_dict_type( SCREAMING_SNAKE_CASE__ , fsdp_plugin.state_dict_type , fsdp_plugin.state_dict_config , fsdp_plugin.optim_state_dict_config ): if fsdp_plugin.state_dict_type == StateDictType.FULL_STATE_DICT: _UpperCAmelCase = None # below check should work but currently it isn't working (mostly opytorch issue), # in the meantime disabling it at the cost of excess memory usage # if accelerator.process_index == 0 or not fsdp_plugin.optim_state_dict_config.rank0_only: _UpperCAmelCase = ( F"""{OPTIMIZER_NAME}.bin""" if optimizer_index == 0 else F"""{OPTIMIZER_NAME}_{optimizer_index}.bin""" ) _UpperCAmelCase = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) logger.info(F"""Loading Optimizer state from {input_optimizer_file}""" ) _UpperCAmelCase = torch.load(SCREAMING_SNAKE_CASE__ ) logger.info(F"""Optimizer state loaded from {input_optimizer_file}""" ) else: _UpperCAmelCase = ( os.path.join(SCREAMING_SNAKE_CASE__ , F"""{OPTIMIZER_NAME}_{optimizer_index}""" ) if F"""{OPTIMIZER_NAME}""" not in input_dir else input_dir ) logger.info(F"""Loading Optimizer from {ckpt_dir}""" ) _UpperCAmelCase = load_sharded_optimizer_state_dict( model_state_dict=model.state_dict() , optimizer_key='optimizer' , storage_reader=dist_cp.FileSystemReader(SCREAMING_SNAKE_CASE__ ) , ) _UpperCAmelCase = optim_state['optimizer'] logger.info(F"""Optimizer loaded from {ckpt_dir}""" ) _UpperCAmelCase = FSDP.optim_state_dict_to_load(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) optimizer.load_state_dict(SCREAMING_SNAKE_CASE__ )
39
def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = [] if len(SCREAMING_SNAKE_CASE__ ) == 1: return [nums.copy()] for _ in range(len(SCREAMING_SNAKE_CASE__ ) ): snake_case_ = nums.pop(0 ) snake_case_ = permute(SCREAMING_SNAKE_CASE__ ) for perm in permutations: perm.append(SCREAMING_SNAKE_CASE__ ) result.extend(SCREAMING_SNAKE_CASE__ ) nums.append(SCREAMING_SNAKE_CASE__ ) return result def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): def backtrack(SCREAMING_SNAKE_CASE__ ): if start == len(SCREAMING_SNAKE_CASE__ ) - 1: output.append(nums[:] ) else: for i in range(SCREAMING_SNAKE_CASE__ , len(SCREAMING_SNAKE_CASE__ ) ): snake_case_, snake_case_ = nums[i], nums[start] backtrack(start + 1 ) snake_case_, snake_case_ = nums[i], nums[start] # backtrack snake_case_ = [] backtrack(0 ) return output if __name__ == "__main__": import doctest # use res to print the data in permute2 function lowerCAmelCase_ = permutea([1, 2, 3]) print(res) doctest.testmod()
8
0
import argparse import glob import logging import os import sys import time from collections import defaultdict from pathlib import Path from typing import Dict, List, Tuple import numpy as np import pytorch_lightning as pl import torch from callbacks import SeqaSeqLoggingCallback, get_checkpoint_callback, get_early_stopping_callback from torch import nn from torch.utils.data import DataLoader from transformers import MBartTokenizer, TaForConditionalGeneration from transformers.models.bart.modeling_bart import shift_tokens_right from utils import ( ROUGE_KEYS, LegacySeqaSeqDataset, SeqaSeqDataset, assert_all_frozen, calculate_bleu, calculate_rouge, check_output_dir, flatten_list, freeze_embeds, freeze_params, get_git_info, label_smoothed_nll_loss, lmap, pickle_save, save_git_info, save_json, use_task_specific_params, ) # need the parent dir module sys.path.insert(2, str(Path(__file__).resolve().parents[1])) from lightning_base import BaseTransformer, add_generic_args, generic_train # noqa snake_case_ = logging.getLogger(__name__) class SCREAMING_SNAKE_CASE__ ( __A ): A_ : Optional[int] = "summarization" A_ : Union[str, Any] = ["loss"] A_ : List[Any] = ROUGE_KEYS A_ : List[Any] = "rouge2" def __init__(self : int , a__ : Any , **a__ : List[Any] ): """simple docstring""" if hparams.sortish_sampler and hparams.gpus > 1: __snake_case = False elif hparams.max_tokens_per_batch is not None: if hparams.gpus > 1: raise NotImplementedError('''Dynamic Batch size does not work for multi-gpu training''' ) if hparams.sortish_sampler: raise ValueError('''--sortish_sampler and --max_tokens_per_batch may not be used simultaneously''' ) super().__init__(_UpperCamelCase , num_labels=_UpperCamelCase , mode=self.mode , **_UpperCamelCase ) use_task_specific_params(self.model , '''summarization''' ) save_git_info(self.hparams.output_dir ) __snake_case = Path(self.output_dir ) / '''metrics.json''' __snake_case = Path(self.output_dir ) / '''hparams.pkl''' pickle_save(self.hparams , self.hparams_save_path ) __snake_case = 0 __snake_case = defaultdict(_UpperCamelCase ) __snake_case = self.config.model_type __snake_case = self.config.tgt_vocab_size if self.model_type == '''fsmt''' else self.config.vocab_size __snake_case = { '''data_dir''': self.hparams.data_dir, '''max_source_length''': self.hparams.max_source_length, '''prefix''': self.model.config.prefix or '''''', } __snake_case = { '''train''': self.hparams.n_train, '''val''': self.hparams.n_val, '''test''': self.hparams.n_test, } __snake_case = {k: v if v >= 0 else None for k, v in n_observations_per_split.items()} __snake_case = { '''train''': self.hparams.max_target_length, '''val''': self.hparams.val_max_target_length, '''test''': self.hparams.test_max_target_length, } assert self.target_lens["train"] <= self.target_lens["val"], f"""target_lens: {self.target_lens}""" assert self.target_lens["train"] <= self.target_lens["test"], f"""target_lens: {self.target_lens}""" if self.hparams.freeze_embeds: freeze_embeds(self.model ) if self.hparams.freeze_encoder: freeze_params(self.model.get_encoder() ) assert_all_frozen(self.model.get_encoder() ) __snake_case = get_git_info()['''repo_sha'''] __snake_case = hparams.num_workers __snake_case = None # default to config if self.model.config.decoder_start_token_id is None and isinstance(self.tokenizer , _UpperCamelCase ): __snake_case = self.tokenizer.lang_code_to_id[hparams.tgt_lang] __snake_case = self.decoder_start_token_id __snake_case = ( SeqaSeqDataset if hasattr(self.tokenizer , '''prepare_seq2seq_batch''' ) else LegacySeqaSeqDataset ) __snake_case = False __snake_case = self.model.config.num_beams if self.hparams.eval_beams is None else self.hparams.eval_beams if self.hparams.eval_max_gen_length is not None: __snake_case = self.hparams.eval_max_gen_length else: __snake_case = self.model.config.max_length __snake_case = self.default_val_metric if self.hparams.val_metric is None else self.hparams.val_metric def a (self : List[Any] , a__ : Dict[str, torch.Tensor] ): """simple docstring""" __snake_case = { k: self.tokenizer.batch_decode(v.tolist() ) if '''mask''' not in k else v.shape for k, v in batch.items() } save_json(_UpperCamelCase , Path(self.output_dir ) / '''text_batch.json''' ) save_json({k: v.tolist() for k, v in batch.items()} , Path(self.output_dir ) / '''tok_batch.json''' ) __snake_case = True return readable_batch def a (self : Tuple , a__ : Union[str, Any] , **a__ : List[str] ): """simple docstring""" return self.model(_UpperCamelCase , **_UpperCamelCase ) def a (self : int , a__ : List[int] ): """simple docstring""" __snake_case = self.tokenizer.batch_decode( _UpperCamelCase , skip_special_tokens=_UpperCamelCase , clean_up_tokenization_spaces=_UpperCamelCase ) return lmap(str.strip , _UpperCamelCase ) def a (self : List[Any] , a__ : dict ): """simple docstring""" __snake_case = self.tokenizer.pad_token_id __snake_case , __snake_case = batch['''input_ids'''], batch['''attention_mask'''] __snake_case = batch['''labels'''] if isinstance(self.model , _UpperCamelCase ): __snake_case = self.model._shift_right(_UpperCamelCase ) else: __snake_case = shift_tokens_right(_UpperCamelCase , _UpperCamelCase ) if not self.already_saved_batch: # This would be slightly better if it only happened on rank zero __snake_case = decoder_input_ids self.save_readable_batch(_UpperCamelCase ) __snake_case = self(_UpperCamelCase , attention_mask=_UpperCamelCase , decoder_input_ids=_UpperCamelCase , use_cache=_UpperCamelCase ) __snake_case = outputs['''logits'''] if self.hparams.label_smoothing == 0: # Same behavior as modeling_bart.py, besides ignoring pad_token_id __snake_case = nn.CrossEntropyLoss(ignore_index=_UpperCamelCase ) assert lm_logits.shape[-1] == self.vocab_size __snake_case = ce_loss_fct(lm_logits.view(-1 , lm_logits.shape[-1] ) , tgt_ids.view(-1 ) ) else: __snake_case = nn.functional.log_softmax(_UpperCamelCase , dim=-1 ) __snake_case , __snake_case = label_smoothed_nll_loss( _UpperCamelCase , _UpperCamelCase , self.hparams.label_smoothing , ignore_index=_UpperCamelCase ) return (loss,) @property def a (self : Tuple ): """simple docstring""" return self.tokenizer.pad_token_id def a (self : Dict , a__ : List[str] , a__ : str ): """simple docstring""" __snake_case = self._step(_UpperCamelCase ) __snake_case = dict(zip(self.loss_names , _UpperCamelCase ) ) # tokens per batch __snake_case = batch['''input_ids'''].ne(self.pad ).sum() + batch['''labels'''].ne(self.pad ).sum() __snake_case = batch['''input_ids'''].shape[0] __snake_case = batch['''input_ids'''].eq(self.pad ).sum() __snake_case = batch['''input_ids'''].eq(self.pad ).float().mean() # TODO(SS): make a wandb summary metric for this return {"loss": loss_tensors[0], "log": logs} def a (self : List[Any] , a__ : Optional[int] , a__ : Dict ): """simple docstring""" return self._generative_step(_UpperCamelCase ) def a (self : Optional[Any] , a__ : str , a__ : Optional[Any]="val" ): """simple docstring""" self.step_count += 1 __snake_case = {k: torch.stack([x[k] for x in outputs] ).mean() for k in self.loss_names} __snake_case = losses['''loss'''] __snake_case = { k: np.array([x[k] for x in outputs] ).mean() for k in self.metric_names + ['''gen_time''', '''gen_len'''] } __snake_case = ( generative_metrics[self.val_metric] if self.val_metric in generative_metrics else losses[self.val_metric] ) __snake_case = torch.tensor(_UpperCamelCase ).type_as(_UpperCamelCase ) generative_metrics.update({k: v.item() for k, v in losses.items()} ) losses.update(_UpperCamelCase ) __snake_case = {f"""{prefix}_avg_{k}""": x for k, x in losses.items()} __snake_case = self.step_count self.metrics[prefix].append(_UpperCamelCase ) # callback writes this to self.metrics_save_path __snake_case = flatten_list([x['''preds'''] for x in outputs] ) return { "log": all_metrics, "preds": preds, f"""{prefix}_loss""": loss, f"""{prefix}_{self.val_metric}""": metric_tensor, } def a (self : int , a__ : Optional[int] , a__ : List[str] ): """simple docstring""" return calculate_rouge(_UpperCamelCase , _UpperCamelCase ) def a (self : str , a__ : dict ): """simple docstring""" __snake_case = time.time() # parser.add_argument('--eval_max_gen_length', type=int, default=None, help='never generate more than n tokens') __snake_case = self.model.generate( batch['''input_ids'''] , attention_mask=batch['''attention_mask'''] , use_cache=_UpperCamelCase , decoder_start_token_id=self.decoder_start_token_id , num_beams=self.eval_beams , max_length=self.eval_max_length , ) __snake_case = (time.time() - ta) / batch['''input_ids'''].shape[0] __snake_case = self.ids_to_clean_text(_UpperCamelCase ) __snake_case = self.ids_to_clean_text(batch['''labels'''] ) __snake_case = self._step(_UpperCamelCase ) __snake_case = dict(zip(self.loss_names , _UpperCamelCase ) ) __snake_case = self.calc_generative_metrics(_UpperCamelCase , _UpperCamelCase ) __snake_case = np.mean(lmap(_UpperCamelCase , _UpperCamelCase ) ) base_metrics.update(gen_time=_UpperCamelCase , gen_len=_UpperCamelCase , preds=_UpperCamelCase , target=_UpperCamelCase , **_UpperCamelCase ) return base_metrics def a (self : Tuple , a__ : str , a__ : Dict ): """simple docstring""" return self._generative_step(_UpperCamelCase ) def a (self : Optional[int] , a__ : Optional[int] ): """simple docstring""" return self.validation_epoch_end(_UpperCamelCase , prefix='''test''' ) def a (self : Union[str, Any] , a__ : Any ): """simple docstring""" __snake_case = self.n_obs[type_path] __snake_case = self.target_lens[type_path] __snake_case = self.dataset_class( self.tokenizer , type_path=_UpperCamelCase , n_obs=_UpperCamelCase , max_target_length=_UpperCamelCase , **self.dataset_kwargs , ) return dataset def a (self : Optional[int] , a__ : str , a__ : int , a__ : bool = False ): """simple docstring""" __snake_case = self.get_dataset(_UpperCamelCase ) if self.hparams.sortish_sampler and type_path != "test" and type_path != "val": __snake_case = dataset.make_sortish_sampler(_UpperCamelCase , distributed=self.hparams.gpus > 1 ) return DataLoader( _UpperCamelCase , batch_size=_UpperCamelCase , collate_fn=dataset.collate_fn , shuffle=_UpperCamelCase , num_workers=self.num_workers , sampler=_UpperCamelCase , ) elif self.hparams.max_tokens_per_batch is not None and type_path != "test" and type_path != "val": __snake_case = dataset.make_dynamic_sampler( self.hparams.max_tokens_per_batch , distributed=self.hparams.gpus > 1 ) return DataLoader( _UpperCamelCase , batch_sampler=_UpperCamelCase , collate_fn=dataset.collate_fn , num_workers=self.num_workers , ) else: return DataLoader( _UpperCamelCase , batch_size=_UpperCamelCase , collate_fn=dataset.collate_fn , shuffle=_UpperCamelCase , num_workers=self.num_workers , sampler=_UpperCamelCase , ) def a (self : List[str] ): """simple docstring""" __snake_case = self.get_dataloader('''train''' , batch_size=self.hparams.train_batch_size , shuffle=_UpperCamelCase ) return dataloader def a (self : Union[str, Any] ): """simple docstring""" return self.get_dataloader('''val''' , batch_size=self.hparams.eval_batch_size ) def a (self : Tuple ): """simple docstring""" return self.get_dataloader('''test''' , batch_size=self.hparams.eval_batch_size ) @staticmethod def a (a__ : List[Any] , a__ : Optional[int] ): """simple docstring""" BaseTransformer.add_model_specific_args(_UpperCamelCase , _UpperCamelCase ) add_generic_args(_UpperCamelCase , _UpperCamelCase ) parser.add_argument( '''--max_source_length''' , default=1024 , type=_UpperCamelCase , help=( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) , ) parser.add_argument( '''--max_target_length''' , default=56 , type=_UpperCamelCase , help=( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) , ) parser.add_argument( '''--val_max_target_length''' , default=142 , type=_UpperCamelCase , help=( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) , ) parser.add_argument( '''--test_max_target_length''' , default=142 , type=_UpperCamelCase , help=( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) , ) parser.add_argument('''--freeze_encoder''' , action='''store_true''' ) parser.add_argument('''--freeze_embeds''' , action='''store_true''' ) parser.add_argument('''--sortish_sampler''' , action='''store_true''' , default=_UpperCamelCase ) parser.add_argument('''--overwrite_output_dir''' , action='''store_true''' , default=_UpperCamelCase ) parser.add_argument('''--max_tokens_per_batch''' , type=_UpperCamelCase , default=_UpperCamelCase ) parser.add_argument('''--logger_name''' , type=_UpperCamelCase , choices=['''default''', '''wandb''', '''wandb_shared'''] , default='''default''' ) parser.add_argument('''--n_train''' , type=_UpperCamelCase , default=-1 , required=_UpperCamelCase , help='''# examples. -1 means use all.''' ) parser.add_argument('''--n_val''' , type=_UpperCamelCase , default=500 , required=_UpperCamelCase , help='''# examples. -1 means use all.''' ) parser.add_argument('''--n_test''' , type=_UpperCamelCase , default=-1 , required=_UpperCamelCase , help='''# examples. -1 means use all.''' ) parser.add_argument( '''--task''' , type=_UpperCamelCase , default='''summarization''' , required=_UpperCamelCase , help='''# examples. -1 means use all.''' ) parser.add_argument('''--label_smoothing''' , type=_UpperCamelCase , default=0.0 , required=_UpperCamelCase ) parser.add_argument('''--src_lang''' , type=_UpperCamelCase , default='''''' , required=_UpperCamelCase ) parser.add_argument('''--tgt_lang''' , type=_UpperCamelCase , default='''''' , required=_UpperCamelCase ) parser.add_argument('''--eval_beams''' , type=_UpperCamelCase , default=_UpperCamelCase , required=_UpperCamelCase ) parser.add_argument( '''--val_metric''' , type=_UpperCamelCase , default=_UpperCamelCase , required=_UpperCamelCase , choices=['''bleu''', '''rouge2''', '''loss''', None] ) parser.add_argument('''--eval_max_gen_length''' , type=_UpperCamelCase , default=_UpperCamelCase , help='''never generate more than n tokens''' ) parser.add_argument('''--save_top_k''' , type=_UpperCamelCase , default=1 , required=_UpperCamelCase , help='''How many checkpoints to save''' ) parser.add_argument( '''--early_stopping_patience''' , type=_UpperCamelCase , default=-1 , required=_UpperCamelCase , help=( '''-1 means never early stop. early_stopping_patience is measured in validation checks, not epochs. So''' ''' val_check_interval will effect it.''' ) , ) return parser class SCREAMING_SNAKE_CASE__ ( __A ): A_ : Optional[int] = "translation" A_ : Dict = ["loss"] A_ : str = ["bleu"] A_ : List[str] = "bleu" def __init__(self : Optional[Any] , a__ : Any , **a__ : Any ): """simple docstring""" super().__init__(_UpperCamelCase , **_UpperCamelCase ) __snake_case = hparams.src_lang __snake_case = hparams.tgt_lang def a (self : List[Any] , a__ : Optional[Any] , a__ : Optional[Any] ): """simple docstring""" return calculate_bleu(_UpperCamelCase , _UpperCamelCase ) def lowerCamelCase__ ( snake_case_ : Any , snake_case_ : str=None ) -> List[Any]: Path(args.output_dir ).mkdir(exist_ok=SCREAMING_SNAKE_CASE__ ) check_output_dir(SCREAMING_SNAKE_CASE__ , expected_items=3 ) if model is None: if "summarization" in args.task: __snake_case = SummarizationModule(SCREAMING_SNAKE_CASE__ ) else: __snake_case = TranslationModule(SCREAMING_SNAKE_CASE__ ) __snake_case = Path(args.data_dir ).name if ( args.logger_name == "default" or args.fast_dev_run or str(args.output_dir ).startswith('''/tmp''' ) or str(args.output_dir ).startswith('''/var''' ) ): __snake_case = True # don't pollute wandb logs unnecessarily elif args.logger_name == "wandb": from pytorch_lightning.loggers import WandbLogger __snake_case = os.environ.get('''WANDB_PROJECT''' , SCREAMING_SNAKE_CASE__ ) __snake_case = WandbLogger(name=model.output_dir.name , project=SCREAMING_SNAKE_CASE__ ) elif args.logger_name == "wandb_shared": from pytorch_lightning.loggers import WandbLogger __snake_case = WandbLogger(name=model.output_dir.name , project=f"""hf_{dataset}""" ) if args.early_stopping_patience >= 0: __snake_case = get_early_stopping_callback(model.val_metric , args.early_stopping_patience ) else: __snake_case = False __snake_case = args.val_metric == '''loss''' __snake_case = generic_train( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , logging_callback=SeqaSeqLoggingCallback() , checkpoint_callback=get_checkpoint_callback( args.output_dir , model.val_metric , args.save_top_k , SCREAMING_SNAKE_CASE__ ) , early_stopping_callback=SCREAMING_SNAKE_CASE__ , logger=SCREAMING_SNAKE_CASE__ , ) pickle_save(model.hparams , model.output_dir / '''hparams.pkl''' ) if not args.do_predict: return model __snake_case = '''''' __snake_case = sorted(glob.glob(os.path.join(args.output_dir , '''*.ckpt''' ) , recursive=SCREAMING_SNAKE_CASE__ ) ) if checkpoints: __snake_case = checkpoints[-1] __snake_case = checkpoints[-1] trainer.logger.log_hyperparams(model.hparams ) # test() without a model tests using the best checkpoint automatically trainer.test() return model if __name__ == "__main__": snake_case_ = argparse.ArgumentParser() snake_case_ = pl.Trainer.add_argparse_args(parser) snake_case_ = SummarizationModule.add_model_specific_args(parser, os.getcwd()) snake_case_ = parser.parse_args() main(args)
24
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase_ = {'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['''XGLMTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['''XGLMTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XGLMForCausalLM''', '''XGLMModel''', '''XGLMPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''FlaxXGLMForCausalLM''', '''FlaxXGLMModel''', '''FlaxXGLMPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXGLMForCausalLM''', '''TFXGLMModel''', '''TFXGLMPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
8
0
from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> Optional[int]: __lowerCamelCase : Union[str, Any] = len(SCREAMING_SNAKE_CASE__ ) # We need to create solution object to save path. __lowerCamelCase : Tuple = [[0 for _ in range(SCREAMING_SNAKE_CASE__ )] for _ in range(SCREAMING_SNAKE_CASE__ )] __lowerCamelCase : Optional[Any] = run_maze(SCREAMING_SNAKE_CASE__ , 0 , 0 , SCREAMING_SNAKE_CASE__ ) if solved: print('\n'.join(str(SCREAMING_SNAKE_CASE__ ) for row in solutions ) ) else: print('No solution exists!' ) return solved def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Optional[Any]: __lowerCamelCase : Any = len(SCREAMING_SNAKE_CASE__ ) # Final check point. if i == j == (size - 1): __lowerCamelCase : Any = 1 return True __lowerCamelCase : int = (not i < 0) and (not j < 0) # Check lower bounds __lowerCamelCase : Any = (i < size) and (j < size) # Check upper bounds if lower_flag and upper_flag: # check for already visited and block points. __lowerCamelCase : List[str] = (not solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited __lowerCamelCase : List[str] = 1 # check for directions if ( run_maze(SCREAMING_SNAKE_CASE__ , i + 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or run_maze(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , j + 1 , SCREAMING_SNAKE_CASE__ ) or run_maze(SCREAMING_SNAKE_CASE__ , i - 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or run_maze(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , j - 1 , SCREAMING_SNAKE_CASE__ ) ): return True __lowerCamelCase : Any = 0 return False return False if __name__ == "__main__": import doctest doctest.testmod()
73
from ..utils import DummyObject, requires_backends class snake_case_ ( metaclass=__A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = ["note_seq"] def __init__( self : Optional[int] , *_UpperCamelCase : str , **_UpperCamelCase : Optional[int] ) ->Any: requires_backends(self , ['''note_seq'''] ) @classmethod def snake_case__( cls : int , *_UpperCamelCase : Any , **_UpperCamelCase : List[Any] ) ->int: requires_backends(cls , ['''note_seq'''] ) @classmethod def snake_case__( cls : Dict , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Union[str, Any] ) ->List[str]: requires_backends(cls , ['''note_seq'''] )
8
0
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING import torch from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class lowerCAmelCase_ ( __A ): UpperCAmelCase__ : Dict = "dandelin/vilt-b32-finetuned-vqa" UpperCAmelCase__ : str = ( "This is a tool that answers a question about an image. It takes an input named `image` which should be the " "image containing the information, as well as a `question` which should be the question in English. It " "returns a text that is the answer to the question." ) UpperCAmelCase__ : Any = "image_qa" UpperCAmelCase__ : str = AutoProcessor UpperCAmelCase__ : Optional[int] = AutoModelForVisualQuestionAnswering UpperCAmelCase__ : Optional[int] = ["image", "text"] UpperCAmelCase__ : List[Any] = ["text"] def __init__( self, *SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ) -> List[Any]: requires_backends(self, ['vision'] ) super().__init__(*_UpperCamelCase, **_UpperCamelCase ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: return self.pre_processor(_UpperCamelCase, _UpperCamelCase, return_tensors='pt' ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_ ) -> int: with torch.no_grad(): return self.model(**_UpperCamelCase ).logits def snake_case_ ( self, SCREAMING_SNAKE_CASE_ ) -> Tuple: UpperCamelCase : Tuple = outputs.argmax(-1 ).item() return self.model.config.idalabel[idx]
119
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = { '''sayakpaul/vit-msn-base''': '''https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json''', # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = "vit_msn" def __init__( self : Dict , _UpperCamelCase : Optional[int]=7_6_8 , _UpperCamelCase : Optional[Any]=1_2 , _UpperCamelCase : Union[str, Any]=1_2 , _UpperCamelCase : str=3_0_7_2 , _UpperCamelCase : Tuple="gelu" , _UpperCamelCase : Dict=0.0 , _UpperCamelCase : Dict=0.0 , _UpperCamelCase : List[str]=0.02 , _UpperCamelCase : List[Any]=1e-06 , _UpperCamelCase : Any=2_2_4 , _UpperCamelCase : Optional[Any]=1_6 , _UpperCamelCase : Any=3 , _UpperCamelCase : str=True , **_UpperCamelCase : Any , ) ->int: super().__init__(**_UpperCamelCase ) snake_case_ = hidden_size snake_case_ = num_hidden_layers snake_case_ = num_attention_heads snake_case_ = intermediate_size snake_case_ = hidden_act snake_case_ = hidden_dropout_prob snake_case_ = attention_probs_dropout_prob snake_case_ = initializer_range snake_case_ = layer_norm_eps snake_case_ = image_size snake_case_ = patch_size snake_case_ = num_channels snake_case_ = qkv_bias
8
0
from google.protobuf import descriptor as _descriptor from google.protobuf import descriptor_pool as _descriptor_pool from google.protobuf import symbol_database as _symbol_database from google.protobuf.internal import builder as _builder # @@protoc_insertion_point(imports) UpperCamelCase_ = _symbol_database.Default() UpperCamelCase_ = _descriptor_pool.Default().AddSerializedFile( B'''\n\x19sentencepiece_model.proto\x12\rsentencepiece"\x80\x0c\n\x0bTrainerSpec\x12\r\n\x05input\x18\x01 \x03(\t\x12\x14\n\x0cinput_format\x18\x07 \x01(\t\x12\x14\n\x0cmodel_prefix\x18\x02 \x01(\t\x12\x41\n\nmodel_type\x18\x03 \x01(\x0e\x32$.sentencepiece.TrainerSpec.ModelType:\x07UNIGRAM\x12\x18\n\nvocab_size\x18\x04 \x01(\x05:\x04\x38\x30\x30\x30\x12\x17\n\x0f\x61\x63\x63\x65pt_language\x18\x05 \x03(\t\x12 \n\x15self_test_sample_size\x18\x06 \x01(\x05:\x01\x30\x12*\n\x1b\x65nable_differential_privacy\x18\x32 \x01(\x08:\x05\x66\x61lse\x12+\n differential_privacy_noise_level\x18\x33 \x01(\x02:\x01\x30\x12\x32\n\'differential_privacy_clipping_threshold\x18\x34 \x01(\x04:\x01\x30\x12"\n\x12\x63haracter_coverage\x18\n \x01(\x02:\x06\x30.9995\x12\x1e\n\x13input_sentence_size\x18\x0b \x01(\x04:\x01\x30\x12$\n\x16shuffle_input_sentence\x18\x13 \x01(\x08:\x04true\x12 \n\x14mining_sentence_size\x18\x0c \x01(\x05\x42\x02\x18\x01\x12"\n\x16training_sentence_size\x18\r \x01(\x05\x42\x02\x18\x01\x12(\n\x17seed_sentencepiece_size\x18\x0e \x01(\x05:\x07\x31\x30\x30\x30\x30\x30\x30\x12\x1e\n\x10shrinking_factor\x18\x0f \x01(\x02:\x04\x30.75\x12!\n\x13max_sentence_length\x18\x12 \x01(\x05:\x04\x34\x31\x39\x32\x12\x17\n\x0bnum_threads\x18\x10 \x01(\x05:\x02\x31\x36\x12\x1d\n\x12num_sub_iterations\x18\x11 \x01(\x05:\x01\x32\x12$\n\x18max_sentencepiece_length\x18\x14 \x01(\x05:\x02\x31\x36\x12%\n\x17split_by_unicode_script\x18\x15 \x01(\x08:\x04true\x12\x1d\n\x0fsplit_by_number\x18\x17 \x01(\x08:\x04true\x12!\n\x13split_by_whitespace\x18\x16 \x01(\x08:\x04true\x12)\n\x1atreat_whitespace_as_suffix\x18\x18 \x01(\x08:\x05\x66\x61lse\x12+\n\x1c\x61llow_whitespace_only_pieces\x18\x1a \x01(\x08:\x05\x66\x61lse\x12\x1b\n\x0csplit_digits\x18\x19 \x01(\x08:\x05\x66\x61lse\x12#\n\x19pretokenization_delimiter\x18\x35 \x01(\t:\x00\x12\x17\n\x0f\x63ontrol_symbols\x18\x1e \x03(\t\x12\x1c\n\x14user_defined_symbols\x18\x1f \x03(\t\x12\x16\n\x0erequired_chars\x18$ \x01(\t\x12\x1c\n\rbyte_fallback\x18# \x01(\x08:\x05\x66\x61lse\x12+\n\x1dvocabulary_output_piece_score\x18 \x01(\x08:\x04true\x12\x1e\n\x10hard_vocab_limit\x18! \x01(\x08:\x04true\x12\x1c\n\ruse_all_vocab\x18" \x01(\x08:\x05\x66\x61lse\x12\x11\n\x06unk_id\x18( \x01(\x05:\x01\x30\x12\x11\n\x06\x62os_id\x18) \x01(\x05:\x01\x31\x12\x11\n\x06\x65os_id\x18* \x01(\x05:\x01\x32\x12\x12\n\x06pad_id\x18+ \x01(\x05:\x02-1\x12\x18\n\tunk_piece\x18- \x01(\t:\x05<unk>\x12\x16\n\tbos_piece\x18. \x01(\t:\x03<s>\x12\x17\n\teos_piece\x18/ \x01(\t:\x04</s>\x12\x18\n\tpad_piece\x18\x30 \x01(\t:\x05<pad>\x12\x1a\n\x0bunk_surface\x18, \x01(\t:\x05 \xe2\x81\x87 \x12+\n\x1ctrain_extremely_large_corpus\x18\x31 \x01(\x08:\x05\x66\x61lse"5\n\tModelType\x12\x0b\n\x07UNIGRAM\x10\x01\x12\x07\n\x03\x42PE\x10\x02\x12\x08\n\x04WORD\x10\x03\x12\x08\n\x04\x43HAR\x10\x04*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"\xd1\x01\n\x0eNormalizerSpec\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x1c\n\x14precompiled_charsmap\x18\x02 \x01(\x0c\x12\x1e\n\x10\x61\x64\x64_dummy_prefix\x18\x03 \x01(\x08:\x04true\x12&\n\x18remove_extra_whitespaces\x18\x04 \x01(\x08:\x04true\x12 \n\x12\x65scape_whitespaces\x18\x05 \x01(\x08:\x04true\x12\x1e\n\x16normalization_rule_tsv\x18\x06 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"y\n\x0cSelfTestData\x12\x33\n\x07samples\x18\x01 \x03(\x0b\x32".sentencepiece.SelfTestData.Sample\x1a)\n\x06Sample\x12\r\n\x05input\x18\x01 \x01(\t\x12\x10\n\x08\x65xpected\x18\x02 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"\xfe\x03\n\nModelProto\x12\x37\n\x06pieces\x18\x01 \x03(\x0b\x32\'.sentencepiece.ModelProto.SentencePiece\x12\x30\n\x0ctrainer_spec\x18\x02 \x01(\x0b\x32\x1a.sentencepiece.TrainerSpec\x12\x36\n\x0fnormalizer_spec\x18\x03 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x12\x33\n\x0eself_test_data\x18\x04 \x01(\x0b\x32\x1b.sentencepiece.SelfTestData\x12\x38\n\x11\x64\x65normalizer_spec\x18\x05 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x1a\xd2\x01\n\rSentencePiece\x12\r\n\x05piece\x18\x01 \x01(\t\x12\r\n\x05score\x18\x02 \x01(\x02\x12\x42\n\x04type\x18\x03 \x01(\x0e\x32,.sentencepiece.ModelProto.SentencePiece.Type:\x06NORMAL"T\n\x04Type\x12\n\n\x06NORMAL\x10\x01\x12\x0b\n\x07UNKNOWN\x10\x02\x12\x0b\n\x07\x43ONTROL\x10\x03\x12\x10\n\x0cUSER_DEFINED\x10\x04\x12\x08\n\x04\x42YTE\x10\x06\x12\n\n\x06UNUSED\x10\x05*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\x42\x02H\x03''' ) UpperCamelCase_ = globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, '''sentencepiece_model_pb2''', _globals) if _descriptor._USE_C_DESCRIPTORS is False: UpperCamelCase_ = None UpperCamelCase_ = B'''H\003''' # (generated by protobuf compiler, but `_TRAINERSPEC` is not defined) # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._serialized_options = b"\030\001" # _TRAINERSPEC.fields_by_name["training_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["training_sentence_size"]._serialized_options = b"\030\001" UpperCamelCase_ = 45 UpperCamelCase_ = 1581 UpperCamelCase_ = 1517 UpperCamelCase_ = 1570 UpperCamelCase_ = 1584 UpperCamelCase_ = 1793 UpperCamelCase_ = 1795 UpperCamelCase_ = 1916 UpperCamelCase_ = 1864 UpperCamelCase_ = 1905 UpperCamelCase_ = 1919 UpperCamelCase_ = 2429 UpperCamelCase_ = 2208 UpperCamelCase_ = 2418 UpperCamelCase_ = 2323 UpperCamelCase_ = 2407 # @@protoc_insertion_point(module_scope)
345
from __future__ import annotations from math import pi, sqrt def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): if inductance <= 0: raise ValueError('''Inductance cannot be 0 or negative''' ) elif capacitance <= 0: raise ValueError('''Capacitance cannot be 0 or negative''' ) else: return ( "Resonant frequency", float(1 / (2 * pi * (sqrt(inductance * capacitance ))) ), ) if __name__ == "__main__": import doctest doctest.testmod()
8
0
"""simple docstring""" import logging import sys from dataclasses import dataclass, field from typing import Any, Dict, List, Optional, Union import librosa import torch from datasets import DatasetDict, load_dataset from packaging import version from torch import nn from transformers import ( HfArgumentParser, Trainer, TrainingArguments, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaForPreTraining, is_apex_available, trainer_utils, ) from transformers.models.wavaveca.modeling_wavaveca import _compute_mask_indices if is_apex_available(): from apex import amp if version.parse(version.parse(torch.__version__).base_version) >= version.parse('1.6'): UpperCamelCase_ = True from torch.cuda.amp import autocast UpperCamelCase_ = logging.getLogger(__name__) @dataclass class snake_case : a_ : str = field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) a_ : Optional[str] = field( default=__A , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) a_ : Optional[bool] = field( default=__A , metadata={"""help""": """Whether to freeze the feature extractor layers of the model."""} ) a_ : Optional[bool] = field( default=__A , metadata={"""help""": """Whether to log verbose messages or not."""} , ) a_ : Optional[float] = field( default=2.0 , metadata={"""help""": """Maximum temperature for gumbel softmax."""} ) a_ : Optional[float] = field( default=0.5 , metadata={"""help""": """Minimum temperature for gumbel softmax."""} ) a_ : Optional[float] = field( default=0.999995 , metadata={"""help""": """Decay of gumbel temperature during training."""} ) def UpperCamelCase ( UpperCAmelCase , UpperCAmelCase ) ->Optional[int]: """simple docstring""" logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , handlers=[logging.StreamHandler(sys.stdout )] , ) a_ = logging.WARNING if model_args.verbose_logging: a_ = logging.DEBUG elif trainer_utils.is_main_process(training_args.local_rank ): a_ = logging.INFO logger.setLevel(SCREAMING_SNAKE_CASE__ ) @dataclass class snake_case : a_ : str = field( default=__A , metadata={"""help""": """The name of the dataset to use (via the datasets library)."""} ) a_ : Optional[str] = field( default=__A , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) a_ : Optional[str] = field( default="""train""" , metadata={ """help""": """The name of the training data set split to use (via the datasets library). Defaults to 'train'""" } , ) a_ : Optional[str] = field( default="""validation""" , metadata={ """help""": ( """The name of the validation data set split to use (via the datasets library). Defaults to 'validation'""" ) } , ) a_ : Optional[str] = field( default="""file""" , metadata={"""help""": """Column in the dataset that contains speech file path. Defaults to 'file'"""} , ) a_ : bool = field( default=__A , metadata={"""help""": """Overwrite the cached preprocessed datasets or not."""} ) a_ : Optional[int] = field( default=1 , metadata={ """help""": """The percentage of the train set used as validation set in case there's no validation split""" } , ) a_ : Optional[int] = field( default=__A , metadata={"""help""": """The number of processes to use for the preprocessing."""} , ) a_ : Optional[float] = field( default=20.0 , metadata={"""help""": """Filter audio files that are longer than `max_duration_in_seconds` seconds"""} ) @dataclass class snake_case : a_ : WavaVecaForPreTraining a_ : WavaVecaFeatureExtractor a_ : Union[bool, str] = "longest" a_ : Optional[int] = None a_ : Optional[int] = None def __call__( self , __UpperCAmelCase) ->Dict[str, torch.Tensor]: # reformat list to dict and set to pytorch format a_ = self.feature_extractor.pad( _UpperCamelCase , max_length=self.max_length , padding=self.padding , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors="pt" , ) a_ = self.model._get_feat_extract_output_lengths(batch["input_values"].shape[-1]) a_ = batch["input_values"].shape[0] # make sure that no loss is computed on padded inputs if batch["attention_mask"] is not None: # compute real output lengths according to convolution formula a_ = self.model._get_feat_extract_output_lengths(batch["attention_mask"].sum(-1)).to( torch.long) a_ = torch.zeros( (batch_size, mask_indices_seq_length) , dtype=torch.long , device=batch["input_values"].device) # these two operations makes sure that all values # before the output lengths indices are attended to a_ = 1 a_ = attention_mask.flip([-1]).cumsum(-1).flip([-1]).bool() # sample randomly masked indices a_ = _compute_mask_indices( (batch_size, mask_indices_seq_length) , self.model.config.mask_time_prob , self.model.config.mask_time_length , attention_mask=_UpperCamelCase , min_masks=2 , ) return batch class snake_case ( __A ): def __init__( self , *__UpperCAmelCase , __UpperCAmelCase=1 , __UpperCAmelCase=0 , __UpperCAmelCase=1.0 , **__UpperCAmelCase) ->str: super().__init__(*_UpperCamelCase , **_UpperCamelCase) a_ = 0 a_ = max_gumbel_temp a_ = min_gumbel_temp a_ = gumbel_temp_decay def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase) ->torch.Tensor: model.train() a_ = self._prepare_inputs(_UpperCamelCase) if self.use_amp: with autocast(): a_ = self.compute_loss(_UpperCamelCase , _UpperCamelCase) else: a_ = self.compute_loss(_UpperCamelCase , _UpperCamelCase) if self.args.n_gpu > 1 or self.deepspeed: if model.module.config.ctc_loss_reduction == "mean": a_ = loss.mean() elif model.module.config.ctc_loss_reduction == "sum": a_ = loss.sum() / (inputs["mask_time_indices"]).sum() else: raise ValueError(F'''{model.config.ctc_loss_reduction} is not valid. Choose one of [\'mean\', \'sum\']''') if self.args.gradient_accumulation_steps > 1: a_ = loss / self.args.gradient_accumulation_steps if self.use_amp: self.scaler.scale(_UpperCamelCase).backward() elif self.use_apex: with amp.scale_loss(_UpperCamelCase , self.optimizer) as scaled_loss: scaled_loss.backward() elif self.deepspeed: self.deepspeed.backward(_UpperCamelCase) else: loss.backward() self.num_update_step += 1 # make sure gumbel softmax temperature is decayed if self.args.n_gpu > 1 or self.deepspeed: model.module.set_gumbel_temperature( max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step , self.min_gumbel_temp)) else: model.set_gumbel_temperature( max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step , self.min_gumbel_temp)) return loss.detach() def UpperCamelCase ( ) ->str: """simple docstring""" a_ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) a_ , a_ , a_ = parser.parse_args_into_dataclasses() configure_logger(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Downloading and loading a dataset from the hub. a_ = load_dataset(data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) if "validation" not in datasets.keys(): # make sure only "validation" and "train" keys remain" a_ = DatasetDict() a_ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F'''{data_args.train_split_name}[:{data_args.validation_split_percentage}%]''' , cache_dir=model_args.cache_dir , ) a_ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F'''{data_args.train_split_name}[{data_args.validation_split_percentage}%:]''' , cache_dir=model_args.cache_dir , ) else: # make sure only "validation" and "train" keys remain" a_ = DatasetDict() a_ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split="validation" , cache_dir=model_args.cache_dir , ) a_ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F'''{data_args.train_split_name}''' , cache_dir=model_args.cache_dir , ) # only normalized-inputs-training is supported a_ = WavaVecaFeatureExtractor.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , do_normalize=SCREAMING_SNAKE_CASE__ ) def prepare_dataset(UpperCAmelCase ): # check that all files have the correct sampling rate a_ , a_ = librosa.load(batch[data_args.speech_file_column] , sr=feature_extractor.sampling_rate ) return batch # load audio files into numpy arrays a_ = datasets.map( SCREAMING_SNAKE_CASE__ , num_proc=data_args.preprocessing_num_workers , remove_columns=datasets["train"].column_names ) # filter audio files that are too long a_ = vectorized_datasets.filter( lambda UpperCAmelCase : len(data["speech"] ) < int(data_args.max_duration_in_seconds * feature_extractor.sampling_rate ) ) def normalize(UpperCAmelCase ): return feature_extractor(batch["speech"] , sampling_rate=feature_extractor.sampling_rate ) # normalize and transform to `BatchFeatures` a_ = vectorized_datasets.map( SCREAMING_SNAKE_CASE__ , batched=SCREAMING_SNAKE_CASE__ , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , remove_columns=vectorized_datasets["train"].column_names , ) # pretraining is only supported for "newer" stable layer norm architecture # apply_spec_augment has to be True, mask_feature_prob has to be 0.0 a_ = WavaVecaConfig.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , gradient_checkpointing=training_args.gradient_checkpointing , ) if not config.do_stable_layer_norm or config.feat_extract_norm != "layer": raise ValueError( "PreTraining is only supported for ``config.do_stable_layer_norm=True`` and" " ``config.feat_extract_norm=\'layer\'" ) a_ = WavaVecaForPreTraining(SCREAMING_SNAKE_CASE__ ) a_ = DataCollatorForWavaVecaPretraining(model=SCREAMING_SNAKE_CASE__ , feature_extractor=SCREAMING_SNAKE_CASE__ ) a_ = WavaVecaPreTrainer( model=SCREAMING_SNAKE_CASE__ , data_collator=SCREAMING_SNAKE_CASE__ , args=SCREAMING_SNAKE_CASE__ , train_dataset=vectorized_datasets["train"] , eval_dataset=vectorized_datasets["validation"] , tokenizer=SCREAMING_SNAKE_CASE__ , max_gumbel_temp=model_args.max_gumbel_temperature , min_gumbel_temp=model_args.min_gumbel_temperature , gumbel_temp_decay=model_args.gumbel_temperature_decay , ) trainer.train() if __name__ == "__main__": main()
243
import unittest from transformers.testing_utils import CaptureStdout from transformers.tools.python_interpreter import evaluate def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): return x + 2 class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : Optional[Any] ) ->int: snake_case_ = '''x = 3''' snake_case_ = {} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) assert result == 3 self.assertDictEqual(_UpperCamelCase , {'''x''': 3} ) snake_case_ = '''x = y''' snake_case_ = {'''y''': 5} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 5, '''y''': 5} ) def snake_case__( self : Dict ) ->Optional[int]: snake_case_ = '''y = add_two(x)''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {'''add_two''': add_two} , state=_UpperCamelCase ) assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''y''': 5} ) # Won't work without the tool with CaptureStdout() as out: snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) assert result is None assert "tried to execute add_two" in out.out def snake_case__( self : Union[str, Any] ) ->Dict: snake_case_ = '''x = 3''' snake_case_ = {} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) assert result == 3 self.assertDictEqual(_UpperCamelCase , {'''x''': 3} ) def snake_case__( self : Optional[int] ) ->Optional[int]: snake_case_ = '''test_dict = {\'x\': x, \'y\': add_two(x)}''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {'''add_two''': add_two} , state=_UpperCamelCase ) self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''y''': 5} ) self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''test_dict''': {'''x''': 3, '''y''': 5}} ) def snake_case__( self : Dict ) ->str: snake_case_ = '''x = 3\ny = 5''' snake_case_ = {} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''y''': 5} ) def snake_case__( self : str ) ->Tuple: snake_case_ = '''text = f\'This is x: {x}.\'''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) # evaluate returns the value of the last assignment. assert result == "This is x: 3." self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''text''': '''This is x: 3.'''} ) def snake_case__( self : Optional[Any] ) ->List[str]: snake_case_ = '''if x <= 3:\n y = 2\nelse:\n y = 5''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) # evaluate returns the value of the last assignment. assert result == 2 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''y''': 2} ) snake_case_ = {'''x''': 8} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 8, '''y''': 5} ) def snake_case__( self : str ) ->str: snake_case_ = '''test_list = [x, add_two(x)]''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {'''add_two''': add_two} , state=_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , [3, 5] ) self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''test_list''': [3, 5]} ) def snake_case__( self : Any ) ->List[Any]: snake_case_ = '''y = x''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) assert result == 3 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''y''': 3} ) def snake_case__( self : Optional[int] ) ->Dict: snake_case_ = '''test_list = [x, add_two(x)]\ntest_list[1]''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {'''add_two''': add_two} , state=_UpperCamelCase ) assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''test_list''': [3, 5]} ) snake_case_ = '''test_dict = {\'x\': x, \'y\': add_two(x)}\ntest_dict[\'y\']''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {'''add_two''': add_two} , state=_UpperCamelCase ) assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''test_dict''': {'''x''': 3, '''y''': 5}} ) def snake_case__( self : Optional[Any] ) ->int: snake_case_ = '''x = 0\nfor i in range(3):\n x = i''' snake_case_ = {} snake_case_ = evaluate(_UpperCamelCase , {'''range''': range} , state=_UpperCamelCase ) assert result == 2 self.assertDictEqual(_UpperCamelCase , {'''x''': 2, '''i''': 2} )
8
0
"""simple docstring""" import numpy as np # Importing the Keras libraries and packages import tensorflow as tf from tensorflow.keras import layers, models if __name__ == "__main__": # Initialising the CNN # (Sequential- Building the model layer by layer) SCREAMING_SNAKE_CASE : int = models.Sequential() # Step 1 - Convolution # Here 64,64 is the length & breadth of dataset images and 3 is for the RGB channel # (3,3) is the kernel size (filter matrix) classifier.add( layers.ConvaD(32, (3, 3), input_shape=(64, 64, 3), activation="""relu""") ) # Step 2 - Pooling classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Adding a second convolutional layer classifier.add(layers.ConvaD(32, (3, 3), activation="""relu""")) classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Step 3 - Flattening classifier.add(layers.Flatten()) # Step 4 - Full connection classifier.add(layers.Dense(units=128, activation="""relu""")) classifier.add(layers.Dense(units=1, activation="""sigmoid""")) # Compiling the CNN classifier.compile( optimizer="""adam""", loss="""binary_crossentropy""", metrics=["""accuracy"""] ) # Part 2 - Fitting the CNN to the images # Load Trained model weights # from keras.models import load_model # regressor=load_model('cnn.h5') SCREAMING_SNAKE_CASE : Tuple = tf.keras.preprocessing.image.ImageDataGenerator( rescale=1.0 / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True ) SCREAMING_SNAKE_CASE : List[Any] = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1.0 / 255) SCREAMING_SNAKE_CASE : List[str] = train_datagen.flow_from_directory( """dataset/training_set""", target_size=(64, 64), batch_size=32, class_mode="""binary""" ) SCREAMING_SNAKE_CASE : Union[str, Any] = test_datagen.flow_from_directory( """dataset/test_set""", target_size=(64, 64), batch_size=32, class_mode="""binary""" ) classifier.fit_generator( training_set, steps_per_epoch=5, epochs=30, validation_data=test_set ) classifier.save("""cnn.h5""") # Part 3 - Making new predictions SCREAMING_SNAKE_CASE : Union[str, Any] = tf.keras.preprocessing.image.load_img( """dataset/single_prediction/image.png""", target_size=(64, 64) ) SCREAMING_SNAKE_CASE : Optional[int] = tf.keras.preprocessing.image.img_to_array(test_image) SCREAMING_SNAKE_CASE : List[str] = np.expand_dims(test_image, axis=0) SCREAMING_SNAKE_CASE : str = classifier.predict(test_image) # training_set.class_indices if result[0][0] == 0: SCREAMING_SNAKE_CASE : int = """Normal""" if result[0][0] == 1: SCREAMING_SNAKE_CASE : Union[str, Any] = """Abnormality detected"""
102
import gc import unittest from parameterized import parameterized from diffusers import FlaxUNetaDConditionModel from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow if is_flax_available(): import jax import jax.numpy as jnp @slow @require_flax class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : Any , _UpperCamelCase : Any , _UpperCamelCase : Tuple ) ->List[Any]: return f'''gaussian_noise_s={seed}_shape={'_'.join([str(_UpperCamelCase ) for s in shape] )}.npy''' def snake_case__( self : Any ) ->List[str]: # clean up the VRAM after each test super().tearDown() gc.collect() def snake_case__( self : int , _UpperCamelCase : Union[str, Any]=0 , _UpperCamelCase : int=(4, 4, 6_4, 6_4) , _UpperCamelCase : Optional[int]=False ) ->Tuple: snake_case_ = jnp.bfloataa if fpaa else jnp.floataa snake_case_ = jnp.array(load_hf_numpy(self.get_file_format(_UpperCamelCase , _UpperCamelCase ) ) , dtype=_UpperCamelCase ) return image def snake_case__( self : List[Any] , _UpperCamelCase : Optional[Any]=False , _UpperCamelCase : Optional[int]="CompVis/stable-diffusion-v1-4" ) ->Optional[Any]: snake_case_ = jnp.bfloataa if fpaa else jnp.floataa snake_case_ = '''bf16''' if fpaa else None snake_case_, snake_case_ = FlaxUNetaDConditionModel.from_pretrained( _UpperCamelCase , subfolder='''unet''' , dtype=_UpperCamelCase , revision=_UpperCamelCase ) return model, params def snake_case__( self : Dict , _UpperCamelCase : List[Any]=0 , _UpperCamelCase : Tuple=(4, 7_7, 7_6_8) , _UpperCamelCase : List[Any]=False ) ->int: snake_case_ = jnp.bfloataa if fpaa else jnp.floataa snake_case_ = jnp.array(load_hf_numpy(self.get_file_format(_UpperCamelCase , _UpperCamelCase ) ) , dtype=_UpperCamelCase ) return hidden_states @parameterized.expand( [ # fmt: off [8_3, 4, [-0.2323, -0.1304, 0.0813, -0.3093, -0.0919, -0.1571, -0.1125, -0.5806]], [1_7, 0.55, [-0.0831, -0.2443, 0.0901, -0.0919, 0.3396, 0.0103, -0.3743, 0.0701]], [8, 0.89, [-0.4863, 0.0859, 0.0875, -0.1658, 0.9199, -0.0114, 0.4839, 0.4639]], [3, 1_0_0_0, [-0.5649, 0.2402, -0.5518, 0.1248, 1.1328, -0.2443, -0.0325, -1.0078]], # fmt: on ] ) def snake_case__( self : Optional[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : List[str] , _UpperCamelCase : Optional[int] ) ->Union[str, Any]: snake_case_, snake_case_ = self.get_unet_model(model_id='''CompVis/stable-diffusion-v1-4''' , fpaa=_UpperCamelCase ) snake_case_ = self.get_latents(_UpperCamelCase , fpaa=_UpperCamelCase ) snake_case_ = self.get_encoder_hidden_states(_UpperCamelCase , fpaa=_UpperCamelCase ) snake_case_ = model.apply( {'''params''': params} , _UpperCamelCase , jnp.array(_UpperCamelCase , dtype=jnp.intaa ) , encoder_hidden_states=_UpperCamelCase , ).sample assert sample.shape == latents.shape snake_case_ = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) snake_case_ = jnp.array(_UpperCamelCase , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware assert jnp.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-2 ) @parameterized.expand( [ # fmt: off [8_3, 4, [0.1514, 0.0807, 0.1624, 0.1016, -0.1896, 0.0263, 0.0677, 0.2310]], [1_7, 0.55, [0.1164, -0.0216, 0.0170, 0.1589, -0.3120, 0.1005, -0.0581, -0.1458]], [8, 0.89, [-0.1758, -0.0169, 0.1004, -0.1411, 0.1312, 0.1103, -0.1996, 0.2139]], [3, 1_0_0_0, [0.1214, 0.0352, -0.0731, -0.1562, -0.0994, -0.0906, -0.2340, -0.0539]], # fmt: on ] ) def snake_case__( self : Optional[int] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str ) ->Dict: snake_case_, snake_case_ = self.get_unet_model(model_id='''stabilityai/stable-diffusion-2''' , fpaa=_UpperCamelCase ) snake_case_ = self.get_latents(_UpperCamelCase , shape=(4, 4, 9_6, 9_6) , fpaa=_UpperCamelCase ) snake_case_ = self.get_encoder_hidden_states(_UpperCamelCase , shape=(4, 7_7, 1_0_2_4) , fpaa=_UpperCamelCase ) snake_case_ = model.apply( {'''params''': params} , _UpperCamelCase , jnp.array(_UpperCamelCase , dtype=jnp.intaa ) , encoder_hidden_states=_UpperCamelCase , ).sample assert sample.shape == latents.shape snake_case_ = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) snake_case_ = jnp.array(_UpperCamelCase , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware assert jnp.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-2 )
8
0
'''simple docstring''' import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class __A ( __A ): a__ : int = 0 a__ : bool = False a__ : float = 3.0 class __A ( unittest.TestCase ): def _lowercase (self : Optional[Any] ): # If no defaults are changed, `to_kwargs` returns an empty dict. self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {"a": 2} ) self.assertDictEqual(MockClass(a=2 , b=_UpperCamelCase ).to_kwargs() , {"a": 2, "b": True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {"a": 2, "c": 2.25} ) @require_cuda def _lowercase (self : Optional[int] ): # If no defaults are changed, `to_kwargs` returns an empty dict. UpperCAmelCase_ = GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() UpperCAmelCase_ = Accelerator(mixed_precision="fp16" , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) UpperCAmelCase_ = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 10_24.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , _UpperCamelCase ) @require_multi_gpu def _lowercase (self : Tuple ): UpperCAmelCase_ = ["torchrun", f"""--nproc_per_node={torch.cuda.device_count()}""", inspect.getfile(self.__class__ )] execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_: Optional[int] =DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) SCREAMING_SNAKE_CASE_: Optional[Any] =Accelerator(kwargs_handlers=[ddp_scaler]) SCREAMING_SNAKE_CASE_: Union[str, Any] =torch.nn.Linear(1_00, 2_00) SCREAMING_SNAKE_CASE_: List[str] =accelerator.prepare(model) # Check the values changed in kwargs SCREAMING_SNAKE_CASE_: Tuple ='' SCREAMING_SNAKE_CASE_: Optional[Any] =model.bucket_bytes_cap // (10_24 * 10_24) if observed_bucket_cap_map != 15: error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
1
import functools import gc import inspect import torch from .imports import is_npu_available, is_xpu_available def __SCREAMING_SNAKE_CASE (*SCREAMING_SNAKE_CASE__ ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = list(SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) ): snake_case_ = None gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() return objects def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = [ '''CUDA out of memory.''', # CUDA OOM '''cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.''', # CUDNN SNAFU '''DefaultCPUAllocator: can\'t allocate memory''', # CPU OOM ] if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and len(exception.args ) == 1: return any(err in exception.args[0] for err in _statements ) return False def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = 128 ): if function is None: return functools.partial(SCREAMING_SNAKE_CASE__ , starting_batch_size=SCREAMING_SNAKE_CASE__ ) snake_case_ = starting_batch_size def decorator(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): nonlocal batch_size gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() snake_case_ = list(inspect.signature(SCREAMING_SNAKE_CASE__ ).parameters.keys() ) # Guard against user error if len(SCREAMING_SNAKE_CASE__ ) < (len(SCREAMING_SNAKE_CASE__ ) + 1): snake_case_ = ''', '''.join([F'''{arg}={value}''' for arg, value in zip(params[1:] , args[1:] )] ) raise TypeError( F'''Batch size was passed into `{function.__name__}` as the first argument when called.''' F'''Remove this as the decorator already does so: `{function.__name__}({arg_str})`''' ) while True: if batch_size == 0: raise RuntimeError('''No executable batch size found, reached zero.''' ) try: return function(SCREAMING_SNAKE_CASE__ , *SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) except Exception as e: if should_reduce_batch_size(SCREAMING_SNAKE_CASE__ ): gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() batch_size //= 2 else: raise return decorator
8
0
'''simple docstring''' import os import zipfile import requests from get_ci_error_statistics import download_artifact, get_artifacts_links def snake_case_ ( lowerCAmelCase_ , lowerCAmelCase_=7 )-> Union[str, Any]: '''simple docstring''' _UpperCAmelCase : Tuple = None if token is not None: _UpperCAmelCase : int = {"""Accept""": """application/vnd.github+json""", """Authorization""": F'''Bearer {token}'''} # The id of a workflow (not of a workflow run) _UpperCAmelCase : str = """636036""" _UpperCAmelCase : List[str] = F'''https://api.github.com/repos/huggingface/transformers/actions/workflows/{workflow_id}/runs''' # On `main` branch + event being `schedule` + not returning PRs + only `num_runs` results url += F'''?branch=main&event=schedule&exclude_pull_requests=true&per_page={num_runs}''' _UpperCAmelCase : str = requests.get(SCREAMING_SNAKE_CASE__ , headers=SCREAMING_SNAKE_CASE__ ).json() return result["workflow_runs"] def snake_case_ ( lowerCAmelCase_ )-> Tuple: '''simple docstring''' _UpperCAmelCase : Any = get_daily_ci_runs(SCREAMING_SNAKE_CASE__ ) _UpperCAmelCase : Union[str, Any] = None for workflow_run in workflow_runs: if workflow_run["status"] == "completed": _UpperCAmelCase : Optional[Any] = workflow_run["""id"""] break return workflow_run_id def snake_case_ ( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ )-> List[Any]: '''simple docstring''' _UpperCAmelCase : int = get_last_daily_ci_runs(SCREAMING_SNAKE_CASE__ ) if workflow_run_id is not None: _UpperCAmelCase : Any = get_artifacts_links(worflow_run_id=SCREAMING_SNAKE_CASE__ , token=SCREAMING_SNAKE_CASE__ ) for artifact_name in artifact_names: if artifact_name in artifacts_links: _UpperCAmelCase : Dict = artifacts_links[artifact_name] download_artifact( artifact_name=SCREAMING_SNAKE_CASE__ , artifact_url=SCREAMING_SNAKE_CASE__ , output_dir=SCREAMING_SNAKE_CASE__ , token=SCREAMING_SNAKE_CASE__ ) def snake_case_ ( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ )-> Union[str, Any]: '''simple docstring''' get_last_daily_ci_artifacts(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) _UpperCAmelCase : Dict = {} for artifact_name in artifact_names: _UpperCAmelCase : int = os.path.join(SCREAMING_SNAKE_CASE__ , F'''{artifact_name}.zip''' ) if os.path.isfile(SCREAMING_SNAKE_CASE__ ): _UpperCAmelCase : Any = {} with zipfile.ZipFile(SCREAMING_SNAKE_CASE__ ) as z: for filename in z.namelist(): if not os.path.isdir(SCREAMING_SNAKE_CASE__ ): # read the file with z.open(SCREAMING_SNAKE_CASE__ ) as f: _UpperCAmelCase : Optional[int] = f.read().decode("""UTF-8""" ) return results
215
from __future__ import annotations def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): return [ord(SCREAMING_SNAKE_CASE__ ) - 96 for elem in plain] def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): return "".join(chr(elem + 96 ) for elem in encoded ) def __SCREAMING_SNAKE_CASE (): snake_case_ = encode(input('''-> ''' ).strip().lower() ) print('''Encoded: ''' , SCREAMING_SNAKE_CASE__ ) print('''Decoded:''' , decode(SCREAMING_SNAKE_CASE__ ) ) if __name__ == "__main__": main()
8
0
import gc import threading import time import psutil import torch class lowercase__ : def __init__( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = psutil.Process() SCREAMING_SNAKE_CASE : Dict = False def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = -1 while True: SCREAMING_SNAKE_CASE : Union[str, Any] = max(self.process.memory_info().rss , self.cpu_memory_peak ) # can't sleep or will not catch the peak right (this comment is here on purpose) if not self.peak_monitoring: break def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = True SCREAMING_SNAKE_CASE : Tuple = threading.Thread(target=self.peak_monitor ) SCREAMING_SNAKE_CASE : Optional[int] = True self.thread.start() def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = False self.thread.join() return self.cpu_memory_peak __UpperCamelCase : Any = PeakCPUMemory() def A ( ): # Time SCREAMING_SNAKE_CASE : Dict = {'''time''': time.time()} gc.collect() torch.cuda.empty_cache() # CPU mem SCREAMING_SNAKE_CASE : Optional[Any] = psutil.Process().memory_info().rss cpu_peak_tracker.start() # GPU mem for i in range(torch.cuda.device_count() ): SCREAMING_SNAKE_CASE : str = torch.cuda.memory_allocated(SCREAMING_SNAKE_CASE__ ) torch.cuda.reset_peak_memory_stats() return measures def A ( _lowercase ): # Time SCREAMING_SNAKE_CASE : Dict = {'''time''': time.time() - start_measures['''time''']} gc.collect() torch.cuda.empty_cache() # CPU mem SCREAMING_SNAKE_CASE : Tuple = (psutil.Process().memory_info().rss - start_measures['''cpu''']) / 2**20 SCREAMING_SNAKE_CASE : int = (cpu_peak_tracker.stop() - start_measures['''cpu''']) / 2**20 # GPU mem for i in range(torch.cuda.device_count() ): SCREAMING_SNAKE_CASE : Any = (torch.cuda.memory_allocated(SCREAMING_SNAKE_CASE__ ) - start_measures[str(SCREAMING_SNAKE_CASE__ )]) / 2**20 SCREAMING_SNAKE_CASE : List[str] = (torch.cuda.max_memory_allocated(SCREAMING_SNAKE_CASE__ ) - start_measures[str(SCREAMING_SNAKE_CASE__ )]) / 2**20 return measures def A ( _lowercase , _lowercase ): print(f"""{description}:""" ) print(f"""- Time: {measures['time']:.2f}s""" ) for i in range(torch.cuda.device_count() ): print(f"""- GPU {i} allocated: {measures[str(SCREAMING_SNAKE_CASE__ )]:.2f}MiB""" ) SCREAMING_SNAKE_CASE : List[Any] = measures[f"""{i}-peak"""] print(f"""- GPU {i} peak: {peak:.2f}MiB""" ) print(f"""- CPU RAM allocated: {measures['cpu']:.2f}MiB""" ) print(f"""- CPU RAM peak: {measures['cpu-peak']:.2f}MiB""" )
182
import math def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): 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''')
8
0
from typing import TYPE_CHECKING from ..utils import _LazyModule _lowerCamelCase : Any = { "config": [ "EXTERNAL_DATA_FORMAT_SIZE_LIMIT", "OnnxConfig", "OnnxConfigWithPast", "OnnxSeq2SeqConfigWithPast", "PatchingSpec", ], "convert": ["export", "validate_model_outputs"], "features": ["FeaturesManager"], "utils": ["ParameterFormat", "compute_serialized_parameters_size"], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys _lowerCamelCase : Any = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
336
import os import re from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = {'''vocab_file''': '''spiece.model'''} lowerCAmelCase_ = { '''vocab_file''': { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model''', '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model''' ), } } lowerCAmelCase_ = { '''google/bigbird-roberta-base''': 40_96, '''google/bigbird-roberta-large''': 40_96, '''google/bigbird-base-trivia-itc''': 40_96, } class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE : Optional[int] = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE : List[Any] = ["input_ids", "attention_mask"] SCREAMING_SNAKE_CASE : List[int] = [] def __init__( self : List[str] , _UpperCamelCase : List[str] , _UpperCamelCase : Dict="<unk>" , _UpperCamelCase : List[str]="<s>" , _UpperCamelCase : Tuple="</s>" , _UpperCamelCase : Any="<pad>" , _UpperCamelCase : Any="[SEP]" , _UpperCamelCase : Optional[Any]="[MASK]" , _UpperCamelCase : Any="[CLS]" , _UpperCamelCase : Optional[Dict[str, Any]] = None , **_UpperCamelCase : Dict , ) ->None: snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else bos_token snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else eos_token snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else unk_token snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else pad_token snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else cls_token snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else sep_token # Mask token behave like a normal word, i.e. include the space before it snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else mask_token snake_case_ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , sep_token=_UpperCamelCase , mask_token=_UpperCamelCase , cls_token=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCamelCase , ) snake_case_ = vocab_file snake_case_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_UpperCamelCase ) @property def snake_case__( self : str ) ->List[Any]: return self.sp_model.get_piece_size() def snake_case__( self : int ) ->Union[str, Any]: snake_case_ = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Tuple ) ->Any: snake_case_ = self.__dict__.copy() snake_case_ = None return state def __setstate__( self : str , _UpperCamelCase : List[Any] ) ->List[str]: snake_case_ = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): snake_case_ = {} snake_case_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def snake_case__( self : Optional[int] , _UpperCamelCase : str ) ->List[str]: return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def snake_case__( self : str , _UpperCamelCase : List[str] ) ->Tuple: return self.sp_model.piece_to_id(_UpperCamelCase ) def snake_case__( self : Union[str, Any] , _UpperCamelCase : str ) ->List[Any]: snake_case_ = self.sp_model.IdToPiece(_UpperCamelCase ) return token def snake_case__( self : Dict , _UpperCamelCase : Optional[int] ) ->List[str]: snake_case_ = [] snake_case_ = '''''' snake_case_ = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(_UpperCamelCase ) + token snake_case_ = True snake_case_ = [] else: current_sub_tokens.append(_UpperCamelCase ) snake_case_ = False out_string += self.sp_model.decode(_UpperCamelCase ) return out_string.strip() def snake_case__( self : int , _UpperCamelCase : List[int] , _UpperCamelCase : bool = False , _UpperCamelCase : bool = None , _UpperCamelCase : bool = True , **_UpperCamelCase : List[str] , ) ->str: snake_case_ = kwargs.pop('''use_source_tokenizer''' , _UpperCamelCase ) snake_case_ = self.convert_ids_to_tokens(_UpperCamelCase , skip_special_tokens=_UpperCamelCase ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 snake_case_ = [] snake_case_ = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_UpperCamelCase ) ) snake_case_ = [] sub_texts.append(_UpperCamelCase ) else: current_sub_text.append(_UpperCamelCase ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_UpperCamelCase ) ) # Mimic the behavior of the Rust tokenizer: # No space before [MASK] and [SEP] if spaces_between_special_tokens: snake_case_ = re.sub(R''' (\[(MASK|SEP)\])''' , R'''\1''' , ''' '''.join(_UpperCamelCase ) ) else: snake_case_ = ''''''.join(_UpperCamelCase ) snake_case_ = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: snake_case_ = self.clean_up_tokenization(_UpperCamelCase ) return clean_text else: return text def snake_case__( self : List[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) ->Tuple[str]: if not os.path.isdir(_UpperCamelCase ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return snake_case_ = os.path.join( _UpperCamelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase , '''wb''' ) as fi: snake_case_ = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,) def snake_case__( self : Tuple , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) ->List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] snake_case_ = [self.cls_token_id] snake_case_ = [self.sep_token_id] return cls + token_ids_a + sep + token_ids_a + sep def snake_case__( self : List[str] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : bool = False ) ->List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) if token_ids_a is None: return [1] + ([0] * len(_UpperCamelCase )) + [1] return [1] + ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] def snake_case__( self : List[Any] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) ->List[int]: snake_case_ = [self.sep_token_id] snake_case_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]
8
0
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_camembert import CamembertTokenizer else: _a = None _a = logging.get_logger(__name__) _a = {'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''} _a = { '''vocab_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/tokenizer.json''', }, } _a = { '''camembert-base''': 512, } _a = '''▁''' class __lowerCamelCase ( __A): """simple docstring""" UpperCamelCase__ = VOCAB_FILES_NAMES UpperCamelCase__ = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase__ = ["input_ids", "attention_mask"] UpperCamelCase__ = CamembertTokenizer def __init__( self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase="<s>" , UpperCAmelCase="</s>" , UpperCAmelCase="</s>" , UpperCAmelCase="<s>" , UpperCAmelCase="<unk>" , UpperCAmelCase="<pad>" , UpperCAmelCase="<mask>" , UpperCAmelCase=["<s>NOTUSED", "</s>NOTUSED"] , **UpperCAmelCase , ): """simple docstring""" _UpperCAmelCase = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else mask_token super().__init__( _UpperCamelCase , tokenizer_file=_UpperCamelCase , bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , sep_token=_UpperCamelCase , cls_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , mask_token=_UpperCamelCase , additional_special_tokens=_UpperCamelCase , **_UpperCamelCase , ) _UpperCAmelCase = vocab_file _UpperCAmelCase = False if not self.vocab_file else True def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase = None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] _UpperCAmelCase = [self.cls_token_id] _UpperCAmelCase = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase = None ): """simple docstring""" _UpperCAmelCase = [self.sep_token_id] _UpperCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase = 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(_UpperCamelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return _UpperCAmelCase = os.path.join( _UpperCamelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ): copyfile(self.vocab_file , _UpperCamelCase ) return (out_vocab_file,)
39
from __future__ import annotations from collections.abc import Generator def __SCREAMING_SNAKE_CASE (): snake_case_ = {} snake_case_ = 2 while True: snake_case_ = factor_map.pop(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if factor: snake_case_ = factor + prime while x in factor_map: x += factor snake_case_ = factor else: snake_case_ = prime yield prime prime += 1 def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = 1E10 ): snake_case_ = sieve() snake_case_ = 1 while True: snake_case_ = next(SCREAMING_SNAKE_CASE__ ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(SCREAMING_SNAKE_CASE__ ) n += 2 if __name__ == "__main__": print(solution())
8
0
from collections.abc import Sequence def lowerCamelCase__ ( snake_case_ : Union[str, Any] , snake_case_ : Optional[Any] = False ) -> Dict: if not arr: return 0 __snake_case = 0 if allow_empty_subarrays else float('''-inf''' ) __snake_case = 0.0 for num in arr: __snake_case = max(0 if allow_empty_subarrays else num , curr_sum + num ) __snake_case = max(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return max_sum if __name__ == "__main__": from doctest import testmod testmod() snake_case_ = [-2, 1, -3, 4, -1, 2, 1, -5, 4] print(F'{max_subarray_sum(nums) = }')
24
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase_ = {'''configuration_opt''': ['''OPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OPTConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''OPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OPTForCausalLM''', '''OPTModel''', '''OPTPreTrainedModel''', '''OPTForSequenceClassification''', '''OPTForQuestionAnswering''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['''TFOPTForCausalLM''', '''TFOPTModel''', '''TFOPTPreTrainedModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''FlaxOPTForCausalLM''', '''FlaxOPTModel''', '''FlaxOPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
8
0
from ...configuration_utils import PretrainedConfig from ...utils import logging a =logging.get_logger(__name__) a ={ """google/fnet-base""": """https://huggingface.co/google/fnet-base/resolve/main/config.json""", """google/fnet-large""": """https://huggingface.co/google/fnet-large/resolve/main/config.json""" # See all FNet models at https://huggingface.co/models?filter=fnet } class A_ ( __A ): _UpperCAmelCase : Dict = "fnet" def __init__( self : List[Any] ,SCREAMING_SNAKE_CASE__ : Tuple=3_2_0_0_0 ,SCREAMING_SNAKE_CASE__ : Optional[Any]=7_6_8 ,SCREAMING_SNAKE_CASE__ : Union[str, Any]=1_2 ,SCREAMING_SNAKE_CASE__ : str=3_0_7_2 ,SCREAMING_SNAKE_CASE__ : Optional[int]="gelu_new" ,SCREAMING_SNAKE_CASE__ : Optional[int]=0.1 ,SCREAMING_SNAKE_CASE__ : Union[str, Any]=5_1_2 ,SCREAMING_SNAKE_CASE__ : Tuple=4 ,SCREAMING_SNAKE_CASE__ : Tuple=0.02 ,SCREAMING_SNAKE_CASE__ : Tuple=1E-12 ,SCREAMING_SNAKE_CASE__ : List[str]=False ,SCREAMING_SNAKE_CASE__ : Union[str, Any]=5_1_2 ,SCREAMING_SNAKE_CASE__ : Any=3 ,SCREAMING_SNAKE_CASE__ : str=1 ,SCREAMING_SNAKE_CASE__ : List[Any]=2 ,**SCREAMING_SNAKE_CASE__ : List[str] ,): super().__init__(pad_token_id=_UpperCamelCase ,bos_token_id=_UpperCamelCase ,eos_token_id=_UpperCamelCase ,**_UpperCamelCase) __lowerCamelCase : Union[str, Any] = vocab_size __lowerCamelCase : str = max_position_embeddings __lowerCamelCase : List[str] = hidden_size __lowerCamelCase : Optional[int] = num_hidden_layers __lowerCamelCase : Any = intermediate_size __lowerCamelCase : str = hidden_act __lowerCamelCase : str = hidden_dropout_prob __lowerCamelCase : str = initializer_range __lowerCamelCase : List[str] = type_vocab_size __lowerCamelCase : List[Any] = layer_norm_eps __lowerCamelCase : Tuple = use_tpu_fourier_optimizations __lowerCamelCase : Any = tpu_short_seq_length
73
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer from .base import PipelineTool class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = "philschmid/bart-large-cnn-samsum" SCREAMING_SNAKE_CASE : Tuple = ( "This is a tool that summarizes an English text. It takes an input `text` containing the text to summarize, " "and returns a summary of the text." ) SCREAMING_SNAKE_CASE : str = "summarizer" SCREAMING_SNAKE_CASE : str = AutoTokenizer SCREAMING_SNAKE_CASE : str = AutoModelForSeqaSeqLM SCREAMING_SNAKE_CASE : Optional[int] = ["text"] SCREAMING_SNAKE_CASE : Optional[int] = ["text"] def snake_case__( self : str , _UpperCamelCase : int ) ->Optional[int]: return self.pre_processor(_UpperCamelCase , return_tensors='''pt''' , truncation=_UpperCamelCase ) def snake_case__( self : Tuple , _UpperCamelCase : Optional[int] ) ->Tuple: return self.model.generate(**_UpperCamelCase )[0] def snake_case__( self : Optional[Any] , _UpperCamelCase : Optional[int] ) ->Any: return self.pre_processor.decode(_UpperCamelCase , skip_special_tokens=_UpperCamelCase , clean_up_tokenization_spaces=_UpperCamelCase )
8
0
import argparse import gc import json import os import re import torch from huggingface_hub import hf_hub_download from transformers import AutoModelForCausalLM, AutoTokenizer, PreTrainedTokenizerFast, RwkvConfig from transformers.modeling_utils import WEIGHTS_INDEX_NAME, shard_checkpoint __UpperCAmelCase = { '''169M''': 12, '''430M''': 24, '''1B5''': 24, '''3B''': 32, '''7B''': 32, '''14B''': 40, } __UpperCAmelCase = { '''169M''': 768, '''430M''': 1_024, '''1B5''': 2_048, '''3B''': 2_560, '''7B''': 4_096, '''14B''': 5_120, } def UpperCamelCase ( snake_case__ : Optional[Any] ) -> str: UpperCamelCase : List[Any] = list(state_dict.keys() ) for name in state_dict_keys: UpperCamelCase : int = state_dict.pop(SCREAMING_SNAKE_CASE__ ) # emb -> embedding if name.startswith('emb.' ): UpperCamelCase : Union[str, Any] = name.replace('emb.' , 'embeddings.' ) # ln_0 -> pre_ln (only present at block 0) if name.startswith('blocks.0.ln0' ): UpperCamelCase : str = name.replace('blocks.0.ln0' , 'blocks.0.pre_ln' ) # att -> attention UpperCamelCase : List[str] = re.sub(R'blocks\.(\d+)\.att' , R'blocks.\1.attention' , SCREAMING_SNAKE_CASE__ ) # ffn -> feed_forward UpperCamelCase : Dict = re.sub(R'blocks\.(\d+)\.ffn' , R'blocks.\1.feed_forward' , SCREAMING_SNAKE_CASE__ ) # time_mix_k -> time_mix_key and reshape if name.endswith('.time_mix_k' ): UpperCamelCase : List[str] = name.replace('.time_mix_k' , '.time_mix_key' ) # time_mix_v -> time_mix_value and reshape if name.endswith('.time_mix_v' ): UpperCamelCase : Union[str, Any] = name.replace('.time_mix_v' , '.time_mix_value' ) # time_mix_r -> time_mix_key and reshape if name.endswith('.time_mix_r' ): UpperCamelCase : Dict = name.replace('.time_mix_r' , '.time_mix_receptance' ) if name != "head.weight": UpperCamelCase : Tuple = 'rwkv.' + name UpperCamelCase : List[str] = weight return state_dict def UpperCamelCase ( snake_case__ : Any , snake_case__ : Union[str, Any] , snake_case__ : Tuple , snake_case__ : Optional[int]=None , snake_case__ : Optional[int]=None , snake_case__ : List[str]=False , snake_case__ : Optional[int]=None ) -> Optional[int]: # 1. If possible, build the tokenizer. if tokenizer_file is None: print('No `--tokenizer_file` provided, we will use the default tokenizer.' ) UpperCamelCase : Union[str, Any] = 50277 UpperCamelCase : Optional[Any] = AutoTokenizer.from_pretrained('EleutherAI/gpt-neox-20b' ) else: UpperCamelCase : Optional[int] = PreTrainedTokenizerFast(tokenizer_file=SCREAMING_SNAKE_CASE__ ) UpperCamelCase : List[str] = len(SCREAMING_SNAKE_CASE__ ) tokenizer.save_pretrained(SCREAMING_SNAKE_CASE__ ) # 2. Build the config UpperCamelCase : Any = list(NUM_HIDDEN_LAYERS_MAPPING.keys() ) if size is None: # Try to infer size from the checkpoint name for candidate in possible_sizes: if candidate in checkpoint_file: UpperCamelCase : int = candidate break if size is None: raise ValueError('Could not infer the size, please provide it with the `--size` argument.' ) if size not in possible_sizes: raise ValueError(F"""`size` should be one of {possible_sizes}, got {size}.""" ) UpperCamelCase : int = RwkvConfig( vocab_size=SCREAMING_SNAKE_CASE__ , num_hidden_layers=NUM_HIDDEN_LAYERS_MAPPING[size] , hidden_size=HIDEN_SIZE_MAPPING[size] , ) config.save_pretrained(SCREAMING_SNAKE_CASE__ ) # 3. Download model file then convert state_dict UpperCamelCase : Optional[Any] = hf_hub_download(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) UpperCamelCase : str = torch.load(SCREAMING_SNAKE_CASE__ , map_location='cpu' ) UpperCamelCase : Tuple = convert_state_dict(SCREAMING_SNAKE_CASE__ ) # 4. Split in shards and save UpperCamelCase , UpperCamelCase : int = shard_checkpoint(SCREAMING_SNAKE_CASE__ ) for shard_file, shard in shards.items(): torch.save(SCREAMING_SNAKE_CASE__ , os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) if index is not None: UpperCamelCase : Union[str, Any] = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Save the index as well with open(SCREAMING_SNAKE_CASE__ , 'w' , encoding='utf-8' ) as f: UpperCamelCase : Dict = json.dumps(SCREAMING_SNAKE_CASE__ , indent=2 , sort_keys=SCREAMING_SNAKE_CASE__ ) + '\n' f.write(SCREAMING_SNAKE_CASE__ ) # 5. Clean up shards (for some reason the file PyTorch saves take the same space as the whole state_dict print( 'Cleaning up shards. This may error with an OOM error, it this is the case don\'t worry you still have converted the model.' ) UpperCamelCase : Optional[int] = list(shards.keys() ) del state_dict del shards gc.collect() for shard_file in shard_files: UpperCamelCase : Any = torch.load(os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) torch.save({k: v.cpu().clone() for k, v in state_dict.items()} , os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) del state_dict gc.collect() if push_to_hub: if model_name is None: raise ValueError('Please provide a `model_name` to push the model to the Hub.' ) UpperCamelCase : Any = AutoModelForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE__ ) model.push_to_hub(SCREAMING_SNAKE_CASE__ , max_shard_size='2GB' ) tokenizer.push_to_hub(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": __UpperCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--repo_id''', default=None, type=str, required=True, help='''Repo ID from which to pull the checkpoint.''' ) parser.add_argument( '''--checkpoint_file''', default=None, type=str, required=True, help='''Name of the checkpoint file in the repo.''' ) parser.add_argument( '''--output_dir''', default=None, type=str, required=True, help='''Where to save the converted model.''' ) parser.add_argument( '''--tokenizer_file''', default=None, type=str, help='''Path to the tokenizer file to use (if not provided, only the model is converted).''', ) parser.add_argument( '''--size''', default=None, type=str, help='''Size of the model. Will be inferred from the `checkpoint_file` if not passed.''', ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Push to the Hub the converted model.''', ) parser.add_argument( '''--model_name''', default=None, type=str, help='''Name of the pushed model on the Hub, including the username / organization.''', ) __UpperCAmelCase = parser.parse_args() convert_rmkv_checkpoint_to_hf_format( args.repo_id, args.checkpoint_file, args.output_dir, size=args.size, tokenizer_file=args.tokenizer_file, push_to_hub=args.push_to_hub, model_name=args.model_name, )
119
from collections import deque from .hash_table import HashTable class snake_case_ ( __A ): '''simple docstring''' def __init__( self : int , *_UpperCamelCase : int , **_UpperCamelCase : Tuple ) ->Tuple: super().__init__(*_UpperCamelCase , **_UpperCamelCase ) def snake_case__( self : Dict , _UpperCamelCase : List[str] , _UpperCamelCase : Dict ) ->Tuple: snake_case_ = deque([] ) if self.values[key] is None else self.values[key] self.values[key].appendleft(_UpperCamelCase ) snake_case_ = self.values[key] def snake_case__( self : List[Any] ) ->str: return ( sum(self.charge_factor - len(_UpperCamelCase ) for slot in self.values ) / self.size_table * self.charge_factor ) def snake_case__( self : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[int]=None ) ->str: if not ( len(self.values[key] ) == self.charge_factor and self.values.count(_UpperCamelCase ) == 0 ): return key return super()._collision_resolution(_UpperCamelCase , _UpperCamelCase )
8
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig UpperCamelCase_ = { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''', } class _snake_case ( __A ): '''simple docstring''' A__ : int = "albert" def __init__( self: Tuple ,lowerCamelCase_: List[str]=30000 ,lowerCamelCase_: Optional[Any]=128 ,lowerCamelCase_: List[Any]=4096 ,lowerCamelCase_: int=12 ,lowerCamelCase_: Union[str, Any]=1 ,lowerCamelCase_: Any=64 ,lowerCamelCase_: str=16384 ,lowerCamelCase_: str=1 ,lowerCamelCase_: Union[str, Any]="gelu_new" ,lowerCamelCase_: Union[str, Any]=0 ,lowerCamelCase_: Union[str, Any]=0 ,lowerCamelCase_: Optional[Any]=512 ,lowerCamelCase_: Any=2 ,lowerCamelCase_: List[str]=0.0_2 ,lowerCamelCase_: Dict=1e-12 ,lowerCamelCase_: Tuple=0.1 ,lowerCamelCase_: Tuple="absolute" ,lowerCamelCase_: int=0 ,lowerCamelCase_: int=2 ,lowerCamelCase_: Any=3 ,**lowerCamelCase_: Dict ,) -> Dict: super().__init__(pad_token_id=_UpperCamelCase ,bos_token_id=_UpperCamelCase ,eos_token_id=_UpperCamelCase ,**_UpperCamelCase ) UpperCAmelCase_ : str = vocab_size UpperCAmelCase_ : Any = embedding_size UpperCAmelCase_ : str = hidden_size UpperCAmelCase_ : Optional[Any] = num_hidden_layers UpperCAmelCase_ : Optional[int] = num_hidden_groups UpperCAmelCase_ : List[str] = num_attention_heads UpperCAmelCase_ : Dict = inner_group_num UpperCAmelCase_ : List[str] = hidden_act UpperCAmelCase_ : Any = intermediate_size UpperCAmelCase_ : Tuple = hidden_dropout_prob UpperCAmelCase_ : Any = attention_probs_dropout_prob UpperCAmelCase_ : int = max_position_embeddings UpperCAmelCase_ : str = type_vocab_size UpperCAmelCase_ : Any = initializer_range UpperCAmelCase_ : int = layer_norm_eps UpperCAmelCase_ : Optional[int] = classifier_dropout_prob UpperCAmelCase_ : Any = position_embedding_type class _snake_case ( __A ): '''simple docstring''' @property def A__ ( self: Optional[int] ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": UpperCAmelCase_ : List[Any] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: UpperCAmelCase_ : Union[str, Any] = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis), ] )
345
from __future__ import annotations def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = len(SCREAMING_SNAKE_CASE__ ) # We need to create solution object to save path. snake_case_ = [[0 for _ in range(SCREAMING_SNAKE_CASE__ )] for _ in range(SCREAMING_SNAKE_CASE__ )] snake_case_ = run_maze(SCREAMING_SNAKE_CASE__ , 0 , 0 , SCREAMING_SNAKE_CASE__ ) if solved: print('''\n'''.join(str(SCREAMING_SNAKE_CASE__ ) for row in solutions ) ) else: print('''No solution exists!''' ) return solved def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = len(SCREAMING_SNAKE_CASE__ ) # Final check point. if i == j == (size - 1): snake_case_ = 1 return True snake_case_ = (not i < 0) and (not j < 0) # Check lower bounds snake_case_ = (i < size) and (j < size) # Check upper bounds if lower_flag and upper_flag: # check for already visited and block points. snake_case_ = (not solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited snake_case_ = 1 # check for directions if ( run_maze(SCREAMING_SNAKE_CASE__ , i + 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or run_maze(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , j + 1 , SCREAMING_SNAKE_CASE__ ) or run_maze(SCREAMING_SNAKE_CASE__ , i - 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or run_maze(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , j - 1 , SCREAMING_SNAKE_CASE__ ) ): return True snake_case_ = 0 return False return False if __name__ == "__main__": import doctest doctest.testmod()
8
0
"""simple docstring""" import os import unittest from transformers.models.cpmant.tokenization_cpmant import VOCAB_FILES_NAMES, CpmAntTokenizer from transformers.testing_utils import require_jieba, tooslow from ...test_tokenization_common import TokenizerTesterMixin @require_jieba class snake_case ( __A , unittest.TestCase ): a_ : str = CpmAntTokenizer a_ : Union[str, Any] = False def UpperCAmelCase__ ( self) ->Optional[int]: super().setUp() a_ = [ "<d>", "</d>", "<s>", "</s>", "</_>", "<unk>", "<pad>", "</n>", "我", "是", "C", "P", "M", "A", "n", "t", ] a_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"]) with open(self.vocab_file , "w" , encoding="utf-8") as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens])) @tooslow def UpperCAmelCase__ ( self) ->List[str]: a_ = CpmAntTokenizer.from_pretrained("openbmb/cpm-ant-10b") a_ = "今天天气真好!" a_ = ["今天", "天气", "真", "好", "!"] a_ = tokenizer.tokenize(_UpperCamelCase) self.assertListEqual(_UpperCamelCase , _UpperCamelCase) a_ = "今天天气真好!" a_ = [tokenizer.bos_token] + tokens a_ = [6, 98_02, 1_49_62, 20_82, 8_31, 2_44] self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCamelCase) , _UpperCamelCase) a_ = tokenizer.decode(_UpperCamelCase) self.assertEqual(_UpperCamelCase , _UpperCamelCase)
243
from decimal import Decimal, getcontext from math import ceil, factorial def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): raise TypeError('''Undefined for non-integers''' ) elif precision < 1: raise ValueError('''Undefined for non-natural numbers''' ) snake_case_ = precision snake_case_ = ceil(precision / 14 ) snake_case_ = 426880 * Decimal(10005 ).sqrt() snake_case_ = 1 snake_case_ = 13591409 snake_case_ = Decimal(SCREAMING_SNAKE_CASE__ ) for k in range(1 , SCREAMING_SNAKE_CASE__ ): snake_case_ = factorial(6 * k ) // (factorial(3 * k ) * factorial(SCREAMING_SNAKE_CASE__ ) ** 3) linear_term += 545140134 exponential_term *= -262537412640768000 partial_sum += Decimal(multinomial_term * linear_term ) / exponential_term return str(constant_term / partial_sum )[:-1] if __name__ == "__main__": lowerCAmelCase_ = 50 print(f"""The first {n} digits of pi is: {pi(n)}""")
8
0
"""simple docstring""" import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_url from PIL import Image from transformers import DPTConfig, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTImageProcessor from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) def lowercase ( _snake_case : List[str] ) ->Tuple: """simple docstring""" __snake_case : List[Any] = DPTConfig() if "large" in checkpoint_url: __snake_case : Optional[Any] = 1_024 __snake_case : List[Any] = 4_096 __snake_case : Optional[int] = 24 __snake_case : int = 16 __snake_case : Union[str, Any] = [5, 11, 17, 23] __snake_case : List[str] = [256, 512, 1_024, 1_024] __snake_case : Optional[Any] = (1, 384, 384) if "ade" in checkpoint_url: __snake_case : Union[str, Any] = True __snake_case : Dict = 150 __snake_case : Union[str, Any] = '''huggingface/label-files''' __snake_case : Optional[int] = '''ade20k-id2label.json''' __snake_case : Optional[int] = json.load(open(cached_download(hf_hub_url(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , repo_type='''dataset''' ) ) , '''r''' ) ) __snake_case : Optional[int] = {int(SCREAMING_SNAKE_CASE__ ): v for k, v in idalabel.items()} __snake_case : Union[str, Any] = idalabel __snake_case : Tuple = {v: k for k, v in idalabel.items()} __snake_case : int = [1, 150, 480, 480] return config, expected_shape def lowercase ( _snake_case : Optional[Any] ) ->List[Any]: """simple docstring""" __snake_case : Any = ['''pretrained.model.head.weight''', '''pretrained.model.head.bias'''] for k in ignore_keys: state_dict.pop(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def lowercase ( _snake_case : int ) ->Union[str, Any]: """simple docstring""" if ( "pretrained.model" in name and "cls_token" not in name and "pos_embed" not in name and "patch_embed" not in name ): __snake_case : List[str] = name.replace('''pretrained.model''' , '''dpt.encoder''' ) if "pretrained.model" in name: __snake_case : Tuple = name.replace('''pretrained.model''' , '''dpt.embeddings''' ) if "patch_embed" in name: __snake_case : Any = name.replace('''patch_embed''' , '''patch_embeddings''' ) if "pos_embed" in name: __snake_case : Tuple = name.replace('''pos_embed''' , '''position_embeddings''' ) if "attn.proj" in name: __snake_case : List[str] = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "proj" in name and "project" not in name: __snake_case : Any = name.replace('''proj''' , '''projection''' ) if "blocks" in name: __snake_case : Tuple = name.replace('''blocks''' , '''layer''' ) if "mlp.fc1" in name: __snake_case : Optional[Any] = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: __snake_case : Tuple = name.replace('''mlp.fc2''' , '''output.dense''' ) if "norm1" in name: __snake_case : Any = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: __snake_case : Tuple = name.replace('''norm2''' , '''layernorm_after''' ) if "scratch.output_conv" in name: __snake_case : int = name.replace('''scratch.output_conv''' , '''head''' ) if "scratch" in name: __snake_case : int = name.replace('''scratch''' , '''neck''' ) if "layer1_rn" in name: __snake_case : Dict = name.replace('''layer1_rn''' , '''convs.0''' ) if "layer2_rn" in name: __snake_case : Dict = name.replace('''layer2_rn''' , '''convs.1''' ) if "layer3_rn" in name: __snake_case : Optional[Any] = name.replace('''layer3_rn''' , '''convs.2''' ) if "layer4_rn" in name: __snake_case : Dict = name.replace('''layer4_rn''' , '''convs.3''' ) if "refinenet" in name: __snake_case : Optional[Any] = int(name[len('''neck.refinenet''' ) : len('''neck.refinenet''' ) + 1] ) # tricky here: we need to map 4 to 0, 3 to 1, 2 to 2 and 1 to 3 __snake_case : Optional[int] = name.replace(f"""refinenet{layer_idx}""" , f"""fusion_stage.layers.{abs(layer_idx-4 )}""" ) if "out_conv" in name: __snake_case : List[str] = name.replace('''out_conv''' , '''projection''' ) if "resConfUnit1" in name: __snake_case : Any = name.replace('''resConfUnit1''' , '''residual_layer1''' ) if "resConfUnit2" in name: __snake_case : Optional[int] = name.replace('''resConfUnit2''' , '''residual_layer2''' ) if "conv1" in name: __snake_case : Any = name.replace('''conv1''' , '''convolution1''' ) if "conv2" in name: __snake_case : Optional[Any] = name.replace('''conv2''' , '''convolution2''' ) # readout blocks if "pretrained.act_postprocess1.0.project.0" in name: __snake_case : Dict = name.replace('''pretrained.act_postprocess1.0.project.0''' , '''neck.reassemble_stage.readout_projects.0.0''' ) if "pretrained.act_postprocess2.0.project.0" in name: __snake_case : Dict = name.replace('''pretrained.act_postprocess2.0.project.0''' , '''neck.reassemble_stage.readout_projects.1.0''' ) if "pretrained.act_postprocess3.0.project.0" in name: __snake_case : Dict = name.replace('''pretrained.act_postprocess3.0.project.0''' , '''neck.reassemble_stage.readout_projects.2.0''' ) if "pretrained.act_postprocess4.0.project.0" in name: __snake_case : Optional[int] = name.replace('''pretrained.act_postprocess4.0.project.0''' , '''neck.reassemble_stage.readout_projects.3.0''' ) # resize blocks if "pretrained.act_postprocess1.3" in name: __snake_case : List[str] = name.replace('''pretrained.act_postprocess1.3''' , '''neck.reassemble_stage.layers.0.projection''' ) if "pretrained.act_postprocess1.4" in name: __snake_case : List[str] = name.replace('''pretrained.act_postprocess1.4''' , '''neck.reassemble_stage.layers.0.resize''' ) if "pretrained.act_postprocess2.3" in name: __snake_case : List[str] = name.replace('''pretrained.act_postprocess2.3''' , '''neck.reassemble_stage.layers.1.projection''' ) if "pretrained.act_postprocess2.4" in name: __snake_case : List[Any] = name.replace('''pretrained.act_postprocess2.4''' , '''neck.reassemble_stage.layers.1.resize''' ) if "pretrained.act_postprocess3.3" in name: __snake_case : Any = name.replace('''pretrained.act_postprocess3.3''' , '''neck.reassemble_stage.layers.2.projection''' ) if "pretrained.act_postprocess4.3" in name: __snake_case : str = name.replace('''pretrained.act_postprocess4.3''' , '''neck.reassemble_stage.layers.3.projection''' ) if "pretrained.act_postprocess4.4" in name: __snake_case : Any = name.replace('''pretrained.act_postprocess4.4''' , '''neck.reassemble_stage.layers.3.resize''' ) if "pretrained" in name: __snake_case : str = name.replace('''pretrained''' , '''dpt''' ) if "bn" in name: __snake_case : Any = name.replace('''bn''' , '''batch_norm''' ) if "head" in name: __snake_case : Tuple = name.replace('''head''' , '''head.head''' ) if "encoder.norm" in name: __snake_case : List[str] = name.replace('''encoder.norm''' , '''layernorm''' ) if "auxlayer" in name: __snake_case : Optional[int] = name.replace('''auxlayer''' , '''auxiliary_head.head''' ) return name def lowercase ( _snake_case : str , _snake_case : Optional[int] ) ->Any: """simple docstring""" for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) __snake_case : int = state_dict.pop(f"""dpt.encoder.layer.{i}.attn.qkv.weight""" ) __snake_case : Tuple = state_dict.pop(f"""dpt.encoder.layer.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict __snake_case : Any = in_proj_weight[: config.hidden_size, :] __snake_case : List[Any] = in_proj_bias[: config.hidden_size] __snake_case : List[Any] = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] __snake_case : int = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] __snake_case : Optional[Any] = in_proj_weight[ -config.hidden_size :, : ] __snake_case : int = in_proj_bias[-config.hidden_size :] def lowercase ( ) ->Optional[int]: """simple docstring""" __snake_case : str = '''http://images.cocodataset.org/val2017/000000039769.jpg''' __snake_case : Union[str, Any] = Image.open(requests.get(SCREAMING_SNAKE_CASE__ , stream=SCREAMING_SNAKE_CASE__ ).raw ) return im @torch.no_grad() def lowercase ( _snake_case : Tuple , _snake_case : int , _snake_case : List[str] , _snake_case : Any ) ->List[Any]: """simple docstring""" __snake_case , __snake_case : Tuple = get_dpt_config(SCREAMING_SNAKE_CASE__ ) # load original state_dict from URL __snake_case : Union[str, Any] = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE__ , map_location='''cpu''' ) # remove certain keys remove_ignore_keys_(SCREAMING_SNAKE_CASE__ ) # rename keys for key in state_dict.copy().keys(): __snake_case : List[str] = state_dict.pop(SCREAMING_SNAKE_CASE__ ) __snake_case : Union[str, Any] = val # read in qkv matrices read_in_q_k_v(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # load HuggingFace model __snake_case : List[Any] = DPTForSemanticSegmentation(SCREAMING_SNAKE_CASE__ ) if '''ade''' in checkpoint_url else DPTForDepthEstimation(SCREAMING_SNAKE_CASE__ ) model.load_state_dict(SCREAMING_SNAKE_CASE__ ) model.eval() # Check outputs on an image __snake_case : Optional[int] = 480 if '''ade''' in checkpoint_url else 384 __snake_case : Any = DPTImageProcessor(size=SCREAMING_SNAKE_CASE__ ) __snake_case : List[str] = prepare_img() __snake_case : Dict = image_processor(SCREAMING_SNAKE_CASE__ , return_tensors='''pt''' ) # forward pass __snake_case : Any = model(**SCREAMING_SNAKE_CASE__ ).logits if '''ade''' in checkpoint_url else model(**SCREAMING_SNAKE_CASE__ ).predicted_depth # Assert logits __snake_case : Dict = torch.tensor([[6.3199, 6.3629, 6.4148], [6.3850, 6.3615, 6.4166], [6.3519, 6.3176, 6.3575]] ) if "ade" in checkpoint_url: __snake_case : int = torch.tensor([[4.0480, 4.2420, 4.4360], [4.3124, 4.5693, 4.8261], [4.5768, 4.8965, 5.2163]] ) assert outputs.shape == torch.Size(SCREAMING_SNAKE_CASE__ ) assert ( torch.allclose(outputs[0, 0, :3, :3] , SCREAMING_SNAKE_CASE__ , atol=1e-4 ) if "ade" in checkpoint_url else torch.allclose(outputs[0, :3, :3] , SCREAMING_SNAKE_CASE__ ) ) Path(SCREAMING_SNAKE_CASE__ ).mkdir(exist_ok=SCREAMING_SNAKE_CASE__ ) print(f"""Saving model to {pytorch_dump_folder_path}""" ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) print(f"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) if push_to_hub: print('''Pushing model to hub...''' ) model.push_to_hub( repo_path_or_name=Path(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) , organization='''nielsr''' , commit_message='''Add model''' , use_temp_dir=SCREAMING_SNAKE_CASE__ , ) image_processor.push_to_hub( repo_path_or_name=Path(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) , organization='''nielsr''' , commit_message='''Add image processor''' , use_temp_dir=SCREAMING_SNAKE_CASE__ , ) if __name__ == "__main__": SCREAMING_SNAKE_CASE : str = argparse.ArgumentParser() # Required parameters parser.add_argument( """--checkpoint_url""", default="""https://github.com/intel-isl/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt""", type=str, help="""URL of the original DPT checkpoint you\'d like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model directory.""", ) parser.add_argument( """--push_to_hub""", action="""store_true""", ) parser.add_argument( """--model_name""", default="""dpt-large""", type=str, help="""Name of the model, in case you\'re pushing to the hub.""", ) SCREAMING_SNAKE_CASE : Dict = parser.parse_args() convert_dpt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
102
from typing import Optional import pyspark from .. import Features, NamedSplit from ..download import DownloadMode from ..packaged_modules.spark.spark import Spark from .abc import AbstractDatasetReader class snake_case_ ( __A ): '''simple docstring''' def __init__( self : int , _UpperCamelCase : pyspark.sql.DataFrame , _UpperCamelCase : Optional[NamedSplit] = None , _UpperCamelCase : Optional[Features] = None , _UpperCamelCase : bool = True , _UpperCamelCase : str = None , _UpperCamelCase : bool = False , _UpperCamelCase : str = None , _UpperCamelCase : bool = True , _UpperCamelCase : str = "arrow" , **_UpperCamelCase : Tuple , ) ->str: super().__init__( split=_UpperCamelCase , features=_UpperCamelCase , cache_dir=_UpperCamelCase , keep_in_memory=_UpperCamelCase , streaming=_UpperCamelCase , **_UpperCamelCase , ) snake_case_ = load_from_cache_file snake_case_ = file_format snake_case_ = Spark( df=_UpperCamelCase , features=_UpperCamelCase , cache_dir=_UpperCamelCase , working_dir=_UpperCamelCase , **_UpperCamelCase , ) def snake_case__( self : int ) ->Tuple: if self.streaming: return self.builder.as_streaming_dataset(split=self.split ) snake_case_ = None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD self.builder.download_and_prepare( download_mode=_UpperCamelCase , file_format=self._file_format , ) return self.builder.as_dataset(split=self.split )
8
0
'''simple docstring''' SCREAMING_SNAKE_CASE_: List[str] ='\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n' SCREAMING_SNAKE_CASE_: Any =[{'type': 'code', 'content': INSTALL_CONTENT}] SCREAMING_SNAKE_CASE_: Optional[Any] ={ '{processor_class}': 'FakeProcessorClass', '{model_class}': 'FakeModelClass', '{object_class}': 'FakeObjectClass', }
1
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available from ...utils import OptionalDependencyNotAvailable lowerCAmelCase_ = {'''configuration_dpt''': ['''DPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DPTConfig''']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['''DPTFeatureExtractor'''] lowerCAmelCase_ = ['''DPTImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''DPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''DPTForDepthEstimation''', '''DPTForSemanticSegmentation''', '''DPTModel''', '''DPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_dpt import DPT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPTConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_dpt import DPTFeatureExtractor from .image_processing_dpt import DPTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dpt import ( DPT_PRETRAINED_MODEL_ARCHIVE_LIST, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel, DPTPreTrainedModel, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
8
0
'''simple docstring''' import numpy as np from transformers import Pipeline def snake_case_ ( lowerCAmelCase_ )-> List[Any]: '''simple docstring''' _UpperCAmelCase : List[Any] = np.max(SCREAMING_SNAKE_CASE__ , axis=-1 , keepdims=SCREAMING_SNAKE_CASE__ ) _UpperCAmelCase : Optional[Any] = np.exp(outputs - maxes ) return shifted_exp / shifted_exp.sum(axis=-1 , keepdims=SCREAMING_SNAKE_CASE__ ) class lowercase ( __A ): """simple docstring""" def _snake_case ( self ,**a_ ) -> Optional[int]: _UpperCAmelCase : str = {} if "second_text" in kwargs: _UpperCAmelCase : List[str] = kwargs["""second_text"""] return preprocess_kwargs, {}, {} def _snake_case ( self ,a_ ,a_=None ) -> Tuple: return self.tokenizer(_UpperCamelCase ,text_pair=_UpperCamelCase ,return_tensors=self.framework ) def _snake_case ( self ,a_ ) -> str: return self.model(**_UpperCamelCase ) def _snake_case ( self ,a_ ) -> Tuple: _UpperCAmelCase : Any = model_outputs.logits[0].numpy() _UpperCAmelCase : Union[str, Any] = softmax(_UpperCamelCase ) _UpperCAmelCase : str = np.argmax(_UpperCamelCase ) _UpperCAmelCase : int = self.model.config.idalabel[best_class] _UpperCAmelCase : List[str] = probabilities[best_class].item() _UpperCAmelCase : Optional[int] = logits.tolist() return {"label": label, "score": score, "logits": logits}
215
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from .tokenization_lxmert import LxmertTokenizer lowerCAmelCase_ = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} lowerCAmelCase_ = { '''vocab_file''': { '''unc-nlp/lxmert-base-uncased''': '''https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/vocab.txt''', }, '''tokenizer_file''': { '''unc-nlp/lxmert-base-uncased''': ( '''https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase_ = { '''unc-nlp/lxmert-base-uncased''': 5_12, } lowerCAmelCase_ = { '''unc-nlp/lxmert-base-uncased''': {'''do_lower_case''': True}, } class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE : Any = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE : List[Any] = PRETRAINED_INIT_CONFIGURATION SCREAMING_SNAKE_CASE : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE : Any = LxmertTokenizer def __init__( self : Union[str, Any] , _UpperCamelCase : int=None , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Dict=True , _UpperCamelCase : Any="[UNK]" , _UpperCamelCase : Tuple="[SEP]" , _UpperCamelCase : List[Any]="[PAD]" , _UpperCamelCase : Union[str, Any]="[CLS]" , _UpperCamelCase : str="[MASK]" , _UpperCamelCase : List[str]=True , _UpperCamelCase : List[str]=None , **_UpperCamelCase : List[str] , ) ->Any: super().__init__( _UpperCamelCase , tokenizer_file=_UpperCamelCase , do_lower_case=_UpperCamelCase , unk_token=_UpperCamelCase , sep_token=_UpperCamelCase , pad_token=_UpperCamelCase , cls_token=_UpperCamelCase , mask_token=_UpperCamelCase , tokenize_chinese_chars=_UpperCamelCase , strip_accents=_UpperCamelCase , **_UpperCamelCase , ) snake_case_ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , _UpperCamelCase ) != do_lower_case or normalizer_state.get('''strip_accents''' , _UpperCamelCase ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , _UpperCamelCase ) != tokenize_chinese_chars ): snake_case_ = getattr(_UpperCamelCase , normalizer_state.pop('''type''' ) ) snake_case_ = do_lower_case snake_case_ = strip_accents snake_case_ = tokenize_chinese_chars snake_case_ = normalizer_class(**_UpperCamelCase ) snake_case_ = do_lower_case def snake_case__( self : Optional[int] , _UpperCamelCase : List[Any] , _UpperCamelCase : List[str]=None ) ->List[Any]: snake_case_ = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def snake_case__( self : int , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) ->List[int]: snake_case_ = [self.sep_token_id] snake_case_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def snake_case__( self : Any , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) ->Tuple[str]: snake_case_ = self._tokenizer.model.save(_UpperCamelCase , name=_UpperCamelCase ) return tuple(_UpperCamelCase )
8
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __UpperCamelCase : Optional[int] = { 'configuration_llama': ['LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LlamaConfig'], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : List[Any] = ['LlamaTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : int = ['LlamaTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : List[Any] = [ 'LlamaForCausalLM', 'LlamaModel', 'LlamaPreTrainedModel', 'LlamaForSequenceClassification', ] if TYPE_CHECKING: from .configuration_llama import LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP, LlamaConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_llama import LlamaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_llama_fast import LlamaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_llama import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaPreTrainedModel else: import sys __UpperCamelCase : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
182
import math def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(SCREAMING_SNAKE_CASE__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = 10001 ): try: snake_case_ = int(SCREAMING_SNAKE_CASE__ ) except (TypeError, ValueError): raise TypeError('''Parameter nth must be int or castable to int.''' ) from None if nth <= 0: raise ValueError('''Parameter nth must be greater than or equal to one.''' ) snake_case_ = [] snake_case_ = 2 while len(SCREAMING_SNAKE_CASE__ ) < nth: if is_prime(SCREAMING_SNAKE_CASE__ ): primes.append(SCREAMING_SNAKE_CASE__ ) num += 1 else: num += 1 return primes[len(SCREAMING_SNAKE_CASE__ ) - 1] if __name__ == "__main__": print(f"""{solution() = }""")
8
0
import argparse import json from pathlib import Path import torch import torchaudio from datasets import load_dataset from huggingface_hub import hf_hub_download from transformers import ASTConfig, ASTFeatureExtractor, ASTForAudioClassification from transformers.utils import logging logging.set_verbosity_info() _lowerCamelCase : int = logging.get_logger(__name__) def a__ ( UpperCAmelCase : Union[str, Any] ) -> Optional[int]: UpperCAmelCase : str = ASTConfig() if "10-10" in model_name: pass elif "speech-commands" in model_name: UpperCAmelCase : Any = 128 elif "12-12" in model_name: UpperCAmelCase : Dict = 12 UpperCAmelCase : Tuple = 12 elif "14-14" in model_name: UpperCAmelCase : Tuple = 14 UpperCAmelCase : List[str] = 14 elif "16-16" in model_name: UpperCAmelCase : Union[str, Any] = 16 UpperCAmelCase : Optional[Any] = 16 else: raise ValueError('''Model not supported''' ) UpperCAmelCase : Union[str, Any] = '''huggingface/label-files''' if "speech-commands" in model_name: UpperCAmelCase : str = 35 UpperCAmelCase : Optional[Any] = '''speech-commands-v2-id2label.json''' else: UpperCAmelCase : List[Any] = 527 UpperCAmelCase : int = '''audioset-id2label.json''' UpperCAmelCase : str = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , repo_type='''dataset''' ) , '''r''' ) ) UpperCAmelCase : List[str] = {int(SCREAMING_SNAKE_CASE__ ): v for k, v in idalabel.items()} UpperCAmelCase : Tuple = idalabel UpperCAmelCase : List[Any] = {v: k for k, v in idalabel.items()} return config def a__ ( UpperCAmelCase : Union[str, Any] ) -> Union[str, Any]: if "module.v" in name: UpperCAmelCase : Dict = name.replace('''module.v''' , '''audio_spectrogram_transformer''' ) if "cls_token" in name: UpperCAmelCase : Dict = name.replace('''cls_token''' , '''embeddings.cls_token''' ) if "dist_token" in name: UpperCAmelCase : Optional[int] = name.replace('''dist_token''' , '''embeddings.distillation_token''' ) if "pos_embed" in name: UpperCAmelCase : int = name.replace('''pos_embed''' , '''embeddings.position_embeddings''' ) if "patch_embed.proj" in name: UpperCAmelCase : Optional[Any] = name.replace('''patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) # transformer blocks if "blocks" in name: UpperCAmelCase : Any = name.replace('''blocks''' , '''encoder.layer''' ) if "attn.proj" in name: UpperCAmelCase : Tuple = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name: UpperCAmelCase : List[Any] = name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: UpperCAmelCase : Tuple = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: UpperCAmelCase : Optional[int] = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: UpperCAmelCase : Tuple = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: UpperCAmelCase : str = name.replace('''mlp.fc2''' , '''output.dense''' ) # final layernorm if "audio_spectrogram_transformer.norm" in name: UpperCAmelCase : Optional[int] = name.replace('''audio_spectrogram_transformer.norm''' , '''audio_spectrogram_transformer.layernorm''' ) # classifier head if "module.mlp_head.0" in name: UpperCAmelCase : Dict = name.replace('''module.mlp_head.0''' , '''classifier.layernorm''' ) if "module.mlp_head.1" in name: UpperCAmelCase : Dict = name.replace('''module.mlp_head.1''' , '''classifier.dense''' ) return name def a__ ( UpperCAmelCase : Optional[int] , UpperCAmelCase : Union[str, Any] ) -> Tuple: for key in orig_state_dict.copy().keys(): UpperCAmelCase : List[Any] = orig_state_dict.pop(SCREAMING_SNAKE_CASE__ ) if "qkv" in key: UpperCAmelCase : List[Any] = key.split('''.''' ) UpperCAmelCase : Tuple = int(key_split[3] ) UpperCAmelCase : Any = config.hidden_size if "weight" in key: UpperCAmelCase : Any = val[:dim, :] UpperCAmelCase : List[Any] = val[dim : dim * 2, :] UpperCAmelCase : Optional[int] = val[-dim:, :] else: UpperCAmelCase : List[Any] = val[:dim] UpperCAmelCase : Optional[int] = val[dim : dim * 2] UpperCAmelCase : str = val[-dim:] else: UpperCAmelCase : Union[str, Any] = val return orig_state_dict def a__ ( UpperCAmelCase : Any ) -> Union[str, Any]: UpperCAmelCase : Union[str, Any] = [ '''module.v.head.weight''', '''module.v.head.bias''', '''module.v.head_dist.weight''', '''module.v.head_dist.bias''', ] for k in ignore_keys: state_dict.pop(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @torch.no_grad() def a__ ( UpperCAmelCase : List[Any] , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : List[Any]=False ) -> Union[str, Any]: UpperCAmelCase : Tuple = get_audio_spectrogram_transformer_config(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase : Union[str, Any] = { '''ast-finetuned-audioset-10-10-0.4593''': ( '''https://www.dropbox.com/s/ca0b1v2nlxzyeb4/audioset_10_10_0.4593.pth?dl=1''' ), '''ast-finetuned-audioset-10-10-0.450''': ( '''https://www.dropbox.com/s/1tv0hovue1bxupk/audioset_10_10_0.4495.pth?dl=1''' ), '''ast-finetuned-audioset-10-10-0.448''': ( '''https://www.dropbox.com/s/6u5sikl4b9wo4u5/audioset_10_10_0.4483.pth?dl=1''' ), '''ast-finetuned-audioset-10-10-0.448-v2''': ( '''https://www.dropbox.com/s/kt6i0v9fvfm1mbq/audioset_10_10_0.4475.pth?dl=1''' ), '''ast-finetuned-audioset-12-12-0.447''': ( '''https://www.dropbox.com/s/snfhx3tizr4nuc8/audioset_12_12_0.4467.pth?dl=1''' ), '''ast-finetuned-audioset-14-14-0.443''': ( '''https://www.dropbox.com/s/z18s6pemtnxm4k7/audioset_14_14_0.4431.pth?dl=1''' ), '''ast-finetuned-audioset-16-16-0.442''': ( '''https://www.dropbox.com/s/mdsa4t1xmcimia6/audioset_16_16_0.4422.pth?dl=1''' ), '''ast-finetuned-speech-commands-v2''': ( '''https://www.dropbox.com/s/q0tbqpwv44pquwy/speechcommands_10_10_0.9812.pth?dl=1''' ), } # load original state_dict UpperCAmelCase : Any = model_name_to_url[model_name] UpperCAmelCase : Union[str, Any] = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE__ , map_location='''cpu''' ) # remove some keys remove_keys(SCREAMING_SNAKE_CASE__ ) # rename some keys UpperCAmelCase : List[str] = convert_state_dict(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # load 🤗 model UpperCAmelCase : Union[str, Any] = ASTForAudioClassification(SCREAMING_SNAKE_CASE__ ) model.eval() model.load_state_dict(SCREAMING_SNAKE_CASE__ ) # verify outputs on dummy input # source: https://github.com/YuanGongND/ast/blob/79e873b8a54d0a3b330dd522584ff2b9926cd581/src/run.py#L62 UpperCAmelCase : Union[str, Any] = -4.2677393 if '''speech-commands''' not in model_name else -6.845978 UpperCAmelCase : Union[str, Any] = 4.5689974 if '''speech-commands''' not in model_name else 5.5654526 UpperCAmelCase : Optional[int] = 1_024 if '''speech-commands''' not in model_name else 128 UpperCAmelCase : Optional[int] = ASTFeatureExtractor(mean=SCREAMING_SNAKE_CASE__ , std=SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ ) if "speech-commands" in model_name: UpperCAmelCase : Any = load_dataset('''speech_commands''' , '''v0.02''' , split='''validation''' ) UpperCAmelCase : int = dataset[0]['''audio''']['''array'''] else: UpperCAmelCase : Optional[int] = hf_hub_download( repo_id='''nielsr/audio-spectogram-transformer-checkpoint''' , filename='''sample_audio.flac''' , repo_type='''dataset''' , ) UpperCAmelCase , UpperCAmelCase : int = torchaudio.load(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase : Dict = waveform.squeeze().numpy() UpperCAmelCase : List[str] = feature_extractor(SCREAMING_SNAKE_CASE__ , sampling_rate=16_000 , return_tensors='''pt''' ) # forward pass UpperCAmelCase : Optional[int] = model(**SCREAMING_SNAKE_CASE__ ) UpperCAmelCase : int = outputs.logits if model_name == "ast-finetuned-audioset-10-10-0.4593": UpperCAmelCase : List[str] = torch.tensor([-0.8760, -7.0042, -8.6602] ) elif model_name == "ast-finetuned-audioset-10-10-0.450": UpperCAmelCase : Dict = torch.tensor([-1.1986, -7.0903, -8.2718] ) elif model_name == "ast-finetuned-audioset-10-10-0.448": UpperCAmelCase : Tuple = torch.tensor([-2.6128, -8.0080, -9.4344] ) elif model_name == "ast-finetuned-audioset-10-10-0.448-v2": UpperCAmelCase : Optional[Any] = torch.tensor([-1.5080, -7.4534, -8.8917] ) elif model_name == "ast-finetuned-audioset-12-12-0.447": UpperCAmelCase : Optional[Any] = torch.tensor([-0.5050, -6.5833, -8.0843] ) elif model_name == "ast-finetuned-audioset-14-14-0.443": UpperCAmelCase : int = torch.tensor([-0.3826, -7.0336, -8.2413] ) elif model_name == "ast-finetuned-audioset-16-16-0.442": UpperCAmelCase : str = torch.tensor([-1.2113, -6.9101, -8.3470] ) elif model_name == "ast-finetuned-speech-commands-v2": UpperCAmelCase : str = torch.tensor([6.1589, -8.0566, -8.7984] ) else: raise ValueError('''Unknown model name''' ) if not torch.allclose(logits[0, :3] , SCREAMING_SNAKE_CASE__ , atol=1E-4 ): raise ValueError('''Logits don\'t match''' ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: Path(SCREAMING_SNAKE_CASE__ ).mkdir(exist_ok=SCREAMING_SNAKE_CASE__ ) print(f'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) print(f'''Saving feature extractor to {pytorch_dump_folder_path}''' ) feature_extractor.save_pretrained(SCREAMING_SNAKE_CASE__ ) if push_to_hub: print('''Pushing model and feature extractor to the hub...''' ) model.push_to_hub(f'''MIT/{model_name}''' ) feature_extractor.push_to_hub(f'''MIT/{model_name}''' ) if __name__ == "__main__": _lowerCamelCase : str = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="ast-finetuned-audioset-10-10-0.4593", type=str, help="Name of the Audio Spectrogram Transformer model you\'d like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) _lowerCamelCase : Tuple = parser.parse_args() convert_audio_spectrogram_transformer_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
336
from sklearn.metrics import mean_squared_error import datasets lowerCAmelCase_ = '''\ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' lowerCAmelCase_ = '''\ Mean Squared Error(MSE) is the average of the square of difference between the predicted and actual values. ''' lowerCAmelCase_ = ''' Args: predictions: array-like of shape (n_samples,) or (n_samples, n_outputs) Estimated target values. references: array-like of shape (n_samples,) or (n_samples, n_outputs) Ground truth (correct) target values. sample_weight: array-like of shape (n_samples,), default=None Sample weights. multioutput: {"raw_values", "uniform_average"} or array-like of shape (n_outputs,), default="uniform_average" Defines aggregating of multiple output values. Array-like value defines weights used to average errors. "raw_values" : Returns a full set of errors in case of multioutput input. "uniform_average" : Errors of all outputs are averaged with uniform weight. squared : bool, default=True If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value. Returns: mse : mean squared error. Examples: >>> mse_metric = datasets.load_metric("mse") >>> predictions = [2.5, 0.0, 2, 8] >>> references = [3, -0.5, 2, 7] >>> results = mse_metric.compute(predictions=predictions, references=references) >>> print(results) {\'mse\': 0.375} >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False) >>> print(rmse_result) {\'mse\': 0.6123724356957945} If you\'re using multi-dimensional lists, then set the config as follows : >>> mse_metric = datasets.load_metric("mse", "multilist") >>> predictions = [[0.5, 1], [-1, 1], [7, -6]] >>> references = [[0, 2], [-1, 2], [8, -5]] >>> results = mse_metric.compute(predictions=predictions, references=references) >>> print(results) {\'mse\': 0.7083333333333334} >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput=\'raw_values\') >>> print(results) # doctest: +NORMALIZE_WHITESPACE {\'mse\': array([0.41666667, 1. ])} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class snake_case_ ( datasets.Metric ): '''simple docstring''' def snake_case__( self : Optional[int] ) ->List[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , reference_urls=[ '''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html''' ] , ) def snake_case__( self : List[Any] ) ->Optional[int]: if self.config_name == "multilist": return { "predictions": datasets.Sequence(datasets.Value('''float''' ) ), "references": datasets.Sequence(datasets.Value('''float''' ) ), } else: return { "predictions": datasets.Value('''float''' ), "references": datasets.Value('''float''' ), } def snake_case__( self : int , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Any=None , _UpperCamelCase : Optional[int]="uniform_average" , _UpperCamelCase : Tuple=True ) ->Tuple: snake_case_ = mean_squared_error( _UpperCamelCase , _UpperCamelCase , sample_weight=_UpperCamelCase , multioutput=_UpperCamelCase , squared=_UpperCamelCase ) return {"mse": mse}
8
0
import os import re from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging _a = logging.get_logger(__name__) _a = {'''vocab_file''': '''spiece.model'''} _a = { '''vocab_file''': { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model''', '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model''' ), } } _a = { '''google/bigbird-roberta-base''': 4096, '''google/bigbird-roberta-large''': 4096, '''google/bigbird-base-trivia-itc''': 4096, } class __lowerCamelCase ( __A): """simple docstring""" UpperCamelCase__ = VOCAB_FILES_NAMES UpperCamelCase__ = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase__ = ["input_ids", "attention_mask"] UpperCamelCase__ = [] def __init__( self , UpperCAmelCase , UpperCAmelCase="<unk>" , UpperCAmelCase="<s>" , UpperCAmelCase="</s>" , UpperCAmelCase="<pad>" , UpperCAmelCase="[SEP]" , UpperCAmelCase="[MASK]" , UpperCAmelCase="[CLS]" , UpperCAmelCase = None , **UpperCAmelCase , ): """simple docstring""" _UpperCAmelCase = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else bos_token _UpperCAmelCase = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else eos_token _UpperCAmelCase = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else unk_token _UpperCAmelCase = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else pad_token _UpperCAmelCase = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else cls_token _UpperCAmelCase = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else sep_token # Mask token behave like a normal word, i.e. include the space before it _UpperCAmelCase = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else mask_token _UpperCAmelCase = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , sep_token=_UpperCamelCase , mask_token=_UpperCamelCase , cls_token=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCamelCase , ) _UpperCAmelCase = vocab_file _UpperCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_UpperCamelCase ) @property def UpperCamelCase ( self ): """simple docstring""" return self.sp_model.get_piece_size() def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ): """simple docstring""" _UpperCAmelCase = self.__dict__.copy() _UpperCAmelCase = None return state def __setstate__( self , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): _UpperCAmelCase = {} _UpperCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase ( self , UpperCAmelCase ): """simple docstring""" return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def UpperCamelCase ( self , UpperCAmelCase ): """simple docstring""" return self.sp_model.piece_to_id(_UpperCamelCase ) def UpperCamelCase ( self , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = self.sp_model.IdToPiece(_UpperCamelCase ) return token def UpperCamelCase ( self , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = [] _UpperCAmelCase = '' _UpperCAmelCase = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(_UpperCamelCase ) + token _UpperCAmelCase = True _UpperCAmelCase = [] else: current_sub_tokens.append(_UpperCamelCase ) _UpperCAmelCase = False out_string += self.sp_model.decode(_UpperCamelCase ) return out_string.strip() def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase = False , UpperCAmelCase = None , UpperCAmelCase = True , **UpperCAmelCase , ): """simple docstring""" _UpperCAmelCase = kwargs.pop('use_source_tokenizer' , _UpperCamelCase ) _UpperCAmelCase = self.convert_ids_to_tokens(_UpperCamelCase , skip_special_tokens=_UpperCamelCase ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 _UpperCAmelCase = [] _UpperCAmelCase = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_UpperCamelCase ) ) _UpperCAmelCase = [] sub_texts.append(_UpperCamelCase ) else: current_sub_text.append(_UpperCamelCase ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_UpperCamelCase ) ) # Mimic the behavior of the Rust tokenizer: # No space before [MASK] and [SEP] if spaces_between_special_tokens: _UpperCAmelCase = re.sub(R' (\[(MASK|SEP)\])' , R'\1' , ' '.join(_UpperCamelCase ) ) else: _UpperCAmelCase = ''.join(_UpperCamelCase ) _UpperCAmelCase = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: _UpperCAmelCase = self.clean_up_tokenization(_UpperCamelCase ) return clean_text else: return text def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase = None ): """simple docstring""" if not os.path.isdir(_UpperCamelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return _UpperCAmelCase = os.path.join( _UpperCamelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase , 'wb' ) as fi: _UpperCAmelCase = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,) def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase = None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] _UpperCAmelCase = [self.cls_token_id] _UpperCAmelCase = [self.sep_token_id] return cls + token_ids_a + sep + token_ids_a + sep def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase = None , UpperCAmelCase = False ): """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) if token_ids_a is None: return [1] + ([0] * len(_UpperCamelCase )) + [1] return [1] + ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase = None ): """simple docstring""" _UpperCAmelCase = [self.sep_token_id] _UpperCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]
39
def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = [] if len(SCREAMING_SNAKE_CASE__ ) == 1: return [nums.copy()] for _ in range(len(SCREAMING_SNAKE_CASE__ ) ): snake_case_ = nums.pop(0 ) snake_case_ = permute(SCREAMING_SNAKE_CASE__ ) for perm in permutations: perm.append(SCREAMING_SNAKE_CASE__ ) result.extend(SCREAMING_SNAKE_CASE__ ) nums.append(SCREAMING_SNAKE_CASE__ ) return result def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): def backtrack(SCREAMING_SNAKE_CASE__ ): if start == len(SCREAMING_SNAKE_CASE__ ) - 1: output.append(nums[:] ) else: for i in range(SCREAMING_SNAKE_CASE__ , len(SCREAMING_SNAKE_CASE__ ) ): snake_case_, snake_case_ = nums[i], nums[start] backtrack(start + 1 ) snake_case_, snake_case_ = nums[i], nums[start] # backtrack snake_case_ = [] backtrack(0 ) return output if __name__ == "__main__": import doctest # use res to print the data in permute2 function lowerCAmelCase_ = permutea([1, 2, 3]) print(res) doctest.testmod()
8
0
import json import os import tempfile import unittest import unittest.mock as mock from pathlib import Path from requests.exceptions import HTTPError from transformers.utils import ( CONFIG_NAME, FLAX_WEIGHTS_NAME, TF2_WEIGHTS_NAME, TRANSFORMERS_CACHE, WEIGHTS_NAME, cached_file, get_file_from_repo, has_file, ) snake_case_ = 'hf-internal-testing/tiny-random-bert' snake_case_ = os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert') snake_case_ = '9b8c223d42b2188cb49d29af482996f9d0f3e5a6' class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): def a (self : Any ): """simple docstring""" __snake_case = cached_file(_UpperCamelCase , _UpperCamelCase ) # Should have downloaded the file in here self.assertTrue(os.path.isdir(_UpperCamelCase ) ) # Cache should contain at least those three subfolders: for subfolder in ["blobs", "refs", "snapshots"]: self.assertTrue(os.path.isdir(os.path.join(_UpperCamelCase , _UpperCamelCase ) ) ) with open(os.path.join(_UpperCamelCase , '''refs''' , '''main''' ) ) as f: __snake_case = f.read() self.assertEqual(_UpperCamelCase , os.path.join(_UpperCamelCase , '''snapshots''' , _UpperCamelCase , _UpperCamelCase ) ) self.assertTrue(os.path.isfile(_UpperCamelCase ) ) # File is cached at the same place the second time. __snake_case = cached_file(_UpperCamelCase , _UpperCamelCase ) self.assertEqual(_UpperCamelCase , _UpperCamelCase ) # Using a specific revision to test the full commit hash. __snake_case = cached_file(_UpperCamelCase , _UpperCamelCase , revision='''9b8c223''' ) self.assertEqual(_UpperCamelCase , os.path.join(_UpperCamelCase , '''snapshots''' , _UpperCamelCase , _UpperCamelCase ) ) def a (self : Tuple ): """simple docstring""" with self.assertRaisesRegex(_UpperCamelCase , '''is not a valid model identifier''' ): __snake_case = cached_file('''tiny-random-bert''' , _UpperCamelCase ) with self.assertRaisesRegex(_UpperCamelCase , '''is not a valid git identifier''' ): __snake_case = cached_file(_UpperCamelCase , _UpperCamelCase , revision='''aaaa''' ) with self.assertRaisesRegex(_UpperCamelCase , '''does not appear to have a file named''' ): __snake_case = cached_file(_UpperCamelCase , '''conf''' ) def a (self : Optional[int] ): """simple docstring""" with self.assertRaisesRegex(_UpperCamelCase , '''does not appear to have a file named''' ): __snake_case = cached_file(_UpperCamelCase , '''conf''' ) with open(os.path.join(_UpperCamelCase , '''refs''' , '''main''' ) ) as f: __snake_case = f.read() self.assertTrue(os.path.isfile(os.path.join(_UpperCamelCase , '''.no_exist''' , _UpperCamelCase , '''conf''' ) ) ) __snake_case = cached_file(_UpperCamelCase , '''conf''' , _raise_exceptions_for_missing_entries=_UpperCamelCase ) self.assertIsNone(_UpperCamelCase ) __snake_case = cached_file(_UpperCamelCase , '''conf''' , local_files_only=_UpperCamelCase , _raise_exceptions_for_missing_entries=_UpperCamelCase ) self.assertIsNone(_UpperCamelCase ) __snake_case = mock.Mock() __snake_case = 500 __snake_case = {} __snake_case = HTTPError __snake_case = {} # Under the mock environment we get a 500 error when trying to reach the tokenizer. with mock.patch('''requests.Session.request''' , return_value=_UpperCamelCase ) as mock_head: __snake_case = cached_file(_UpperCamelCase , '''conf''' , _raise_exceptions_for_connection_errors=_UpperCamelCase ) self.assertIsNone(_UpperCamelCase ) # This check we did call the fake head request mock_head.assert_called() def a (self : Dict ): """simple docstring""" self.assertTrue(has_file('''hf-internal-testing/tiny-bert-pt-only''' , _UpperCamelCase ) ) self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' , _UpperCamelCase ) ) self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' , _UpperCamelCase ) ) def a (self : Optional[int] ): """simple docstring""" self.assertIsNone(get_file_from_repo('''bert-base-cased''' , '''ahah.txt''' ) ) # The function raises if the repository does not exist. with self.assertRaisesRegex(_UpperCamelCase , '''is not a valid model identifier''' ): get_file_from_repo('''bert-base-case''' , _UpperCamelCase ) # The function raises if the revision does not exist. with self.assertRaisesRegex(_UpperCamelCase , '''is not a valid git identifier''' ): get_file_from_repo('''bert-base-cased''' , _UpperCamelCase , revision='''ahaha''' ) __snake_case = get_file_from_repo('''bert-base-cased''' , _UpperCamelCase ) # The name is the cached name which is not very easy to test, so instead we load the content. __snake_case = json.loads(open(_UpperCamelCase , '''r''' ).read() ) self.assertEqual(config['''hidden_size'''] , 768 ) def a (self : Optional[Any] ): """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: __snake_case = Path(_UpperCamelCase ) / '''a.txt''' filename.touch() self.assertEqual(get_file_from_repo(_UpperCamelCase , '''a.txt''' ) , str(_UpperCamelCase ) ) self.assertIsNone(get_file_from_repo(_UpperCamelCase , '''b.txt''' ) )
24
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase_ = {'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['''XGLMTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['''XGLMTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XGLMForCausalLM''', '''XGLMModel''', '''XGLMPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''FlaxXGLMForCausalLM''', '''FlaxXGLMModel''', '''FlaxXGLMPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXGLMForCausalLM''', '''TFXGLMModel''', '''TFXGLMPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
8
0
def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> Optional[int]: if length <= 0 or not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): raise ValueError('Length must be a positive integer.' ) return [n * (2 * n - 1) for n in range(SCREAMING_SNAKE_CASE__ )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
73
from ..utils import DummyObject, requires_backends class snake_case_ ( metaclass=__A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = ["note_seq"] def __init__( self : Optional[int] , *_UpperCamelCase : str , **_UpperCamelCase : Optional[int] ) ->Any: requires_backends(self , ['''note_seq'''] ) @classmethod def snake_case__( cls : int , *_UpperCamelCase : Any , **_UpperCamelCase : List[Any] ) ->int: requires_backends(cls , ['''note_seq'''] ) @classmethod def snake_case__( cls : Dict , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Union[str, Any] ) ->List[str]: requires_backends(cls , ['''note_seq'''] )
8
0
def UpperCamelCase ( snake_case__ : Optional[int] ) -> Optional[int]: UpperCamelCase : Dict = n ** (1 / 3) return (val * val * val) == n if __name__ == "__main__": print(perfect_cube(27)) print(perfect_cube(4))
119
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = { '''sayakpaul/vit-msn-base''': '''https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json''', # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = "vit_msn" def __init__( self : Dict , _UpperCamelCase : Optional[int]=7_6_8 , _UpperCamelCase : Optional[Any]=1_2 , _UpperCamelCase : Union[str, Any]=1_2 , _UpperCamelCase : str=3_0_7_2 , _UpperCamelCase : Tuple="gelu" , _UpperCamelCase : Dict=0.0 , _UpperCamelCase : Dict=0.0 , _UpperCamelCase : List[str]=0.02 , _UpperCamelCase : List[Any]=1e-06 , _UpperCamelCase : Any=2_2_4 , _UpperCamelCase : Optional[Any]=1_6 , _UpperCamelCase : Any=3 , _UpperCamelCase : str=True , **_UpperCamelCase : Any , ) ->int: super().__init__(**_UpperCamelCase ) snake_case_ = hidden_size snake_case_ = num_hidden_layers snake_case_ = num_attention_heads snake_case_ = intermediate_size snake_case_ = hidden_act snake_case_ = hidden_dropout_prob snake_case_ = attention_probs_dropout_prob snake_case_ = initializer_range snake_case_ = layer_norm_eps snake_case_ = image_size snake_case_ = patch_size snake_case_ = num_channels snake_case_ = qkv_bias
8
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCamelCase_ = { '''configuration_whisper''': ['''WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''WhisperConfig''', '''WhisperOnnxConfig'''], '''feature_extraction_whisper''': ['''WhisperFeatureExtractor'''], '''processing_whisper''': ['''WhisperProcessor'''], '''tokenization_whisper''': ['''WhisperTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = ['''WhisperTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ '''WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''WhisperForConditionalGeneration''', '''WhisperModel''', '''WhisperPreTrainedModel''', '''WhisperForAudioClassification''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ '''TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFWhisperForConditionalGeneration''', '''TFWhisperModel''', '''TFWhisperPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ '''FlaxWhisperForConditionalGeneration''', '''FlaxWhisperModel''', '''FlaxWhisperPreTrainedModel''', '''FlaxWhisperForAudioClassification''', ] if TYPE_CHECKING: from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig from .feature_extraction_whisper import WhisperFeatureExtractor from .processing_whisper import WhisperProcessor from .tokenization_whisper import WhisperTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_whisper_fast import WhisperTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_whisper import ( WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, WhisperForAudioClassification, WhisperForConditionalGeneration, WhisperModel, WhisperPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_whisper import ( TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, TFWhisperForConditionalGeneration, TFWhisperModel, TFWhisperPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_whisper import ( FlaxWhisperForAudioClassification, FlaxWhisperForConditionalGeneration, FlaxWhisperModel, FlaxWhisperPreTrainedModel, ) else: import sys UpperCamelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
345
from __future__ import annotations from math import pi, sqrt def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): if inductance <= 0: raise ValueError('''Inductance cannot be 0 or negative''' ) elif capacitance <= 0: raise ValueError('''Capacitance cannot be 0 or negative''' ) else: return ( "Resonant frequency", float(1 / (2 * pi * (sqrt(inductance * capacitance ))) ), ) if __name__ == "__main__": import doctest doctest.testmod()
8
0
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_albert import AlbertTokenizer else: UpperCamelCase_ = None UpperCamelCase_ = logging.get_logger(__name__) UpperCamelCase_ = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} UpperCamelCase_ = { 'vocab_file': { 'albert-base-v1': 'https://huggingface.co/albert-base-v1/resolve/main/spiece.model', 'albert-large-v1': 'https://huggingface.co/albert-large-v1/resolve/main/spiece.model', 'albert-xlarge-v1': 'https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model', 'albert-xxlarge-v1': 'https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model', 'albert-base-v2': 'https://huggingface.co/albert-base-v2/resolve/main/spiece.model', 'albert-large-v2': 'https://huggingface.co/albert-large-v2/resolve/main/spiece.model', 'albert-xlarge-v2': 'https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model', 'albert-xxlarge-v2': 'https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model', }, 'tokenizer_file': { 'albert-base-v1': 'https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json', 'albert-large-v1': 'https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json', 'albert-xlarge-v1': 'https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json', 'albert-xxlarge-v1': 'https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json', 'albert-base-v2': 'https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json', 'albert-large-v2': 'https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json', 'albert-xlarge-v2': 'https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json', 'albert-xxlarge-v2': 'https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json', }, } UpperCamelCase_ = { 'albert-base-v1': 512, 'albert-large-v1': 512, 'albert-xlarge-v1': 512, 'albert-xxlarge-v1': 512, 'albert-base-v2': 512, 'albert-large-v2': 512, 'albert-xlarge-v2': 512, 'albert-xxlarge-v2': 512, } UpperCamelCase_ = '▁' class snake_case ( __A ): a_ : Union[str, Any] = VOCAB_FILES_NAMES a_ : Dict = PRETRAINED_VOCAB_FILES_MAP a_ : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : List[Any] = AlbertTokenizer def __init__( self , __UpperCAmelCase=None , __UpperCAmelCase=None , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=False , __UpperCAmelCase="[CLS]" , __UpperCAmelCase="[SEP]" , __UpperCAmelCase="<unk>" , __UpperCAmelCase="[SEP]" , __UpperCAmelCase="<pad>" , __UpperCAmelCase="[CLS]" , __UpperCAmelCase="[MASK]" , **__UpperCAmelCase , ) ->Tuple: # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. a_ = ( AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase , normalized=_UpperCamelCase) if isinstance(_UpperCamelCase , _UpperCamelCase) else mask_token ) super().__init__( _UpperCamelCase , tokenizer_file=_UpperCamelCase , do_lower_case=_UpperCamelCase , remove_space=_UpperCamelCase , keep_accents=_UpperCamelCase , bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , unk_token=_UpperCamelCase , sep_token=_UpperCamelCase , pad_token=_UpperCamelCase , cls_token=_UpperCamelCase , mask_token=_UpperCamelCase , **_UpperCamelCase , ) a_ = do_lower_case a_ = remove_space a_ = keep_accents a_ = vocab_file a_ = False if not self.vocab_file else True def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase = None) ->List[int]: a_ = [self.sep_token_id] a_ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase = None) ->List[int]: a_ = [self.sep_token_id] a_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1] def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase = None) ->Tuple[str]: 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(_UpperCamelCase): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''') return a_ = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]) if os.path.abspath(self.vocab_file) != os.path.abspath(_UpperCamelCase): copyfile(self.vocab_file , _UpperCamelCase) return (out_vocab_file,)
243
import unittest from transformers.testing_utils import CaptureStdout from transformers.tools.python_interpreter import evaluate def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): return x + 2 class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : Optional[Any] ) ->int: snake_case_ = '''x = 3''' snake_case_ = {} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) assert result == 3 self.assertDictEqual(_UpperCamelCase , {'''x''': 3} ) snake_case_ = '''x = y''' snake_case_ = {'''y''': 5} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 5, '''y''': 5} ) def snake_case__( self : Dict ) ->Optional[int]: snake_case_ = '''y = add_two(x)''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {'''add_two''': add_two} , state=_UpperCamelCase ) assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''y''': 5} ) # Won't work without the tool with CaptureStdout() as out: snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) assert result is None assert "tried to execute add_two" in out.out def snake_case__( self : Union[str, Any] ) ->Dict: snake_case_ = '''x = 3''' snake_case_ = {} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) assert result == 3 self.assertDictEqual(_UpperCamelCase , {'''x''': 3} ) def snake_case__( self : Optional[int] ) ->Optional[int]: snake_case_ = '''test_dict = {\'x\': x, \'y\': add_two(x)}''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {'''add_two''': add_two} , state=_UpperCamelCase ) self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''y''': 5} ) self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''test_dict''': {'''x''': 3, '''y''': 5}} ) def snake_case__( self : Dict ) ->str: snake_case_ = '''x = 3\ny = 5''' snake_case_ = {} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''y''': 5} ) def snake_case__( self : str ) ->Tuple: snake_case_ = '''text = f\'This is x: {x}.\'''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) # evaluate returns the value of the last assignment. assert result == "This is x: 3." self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''text''': '''This is x: 3.'''} ) def snake_case__( self : Optional[Any] ) ->List[str]: snake_case_ = '''if x <= 3:\n y = 2\nelse:\n y = 5''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) # evaluate returns the value of the last assignment. assert result == 2 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''y''': 2} ) snake_case_ = {'''x''': 8} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 8, '''y''': 5} ) def snake_case__( self : str ) ->str: snake_case_ = '''test_list = [x, add_two(x)]''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {'''add_two''': add_two} , state=_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , [3, 5] ) self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''test_list''': [3, 5]} ) def snake_case__( self : Any ) ->List[Any]: snake_case_ = '''y = x''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) assert result == 3 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''y''': 3} ) def snake_case__( self : Optional[int] ) ->Dict: snake_case_ = '''test_list = [x, add_two(x)]\ntest_list[1]''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {'''add_two''': add_two} , state=_UpperCamelCase ) assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''test_list''': [3, 5]} ) snake_case_ = '''test_dict = {\'x\': x, \'y\': add_two(x)}\ntest_dict[\'y\']''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {'''add_two''': add_two} , state=_UpperCamelCase ) assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''test_dict''': {'''x''': 3, '''y''': 5}} ) def snake_case__( self : Optional[Any] ) ->int: snake_case_ = '''x = 0\nfor i in range(3):\n x = i''' snake_case_ = {} snake_case_ = evaluate(_UpperCamelCase , {'''range''': range} , state=_UpperCamelCase ) assert result == 2 self.assertDictEqual(_UpperCamelCase , {'''x''': 2, '''i''': 2} )
8
0
"""simple docstring""" import datasets from .evaluate import evaluate SCREAMING_SNAKE_CASE : Dict = """\ @inproceedings{Rajpurkar2016SQuAD10, title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text}, author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang}, booktitle={EMNLP}, year={2016} } """ SCREAMING_SNAKE_CASE : Any = """ This metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD). Stanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by crowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span, from the corresponding reading passage, or the question might be unanswerable. """ SCREAMING_SNAKE_CASE : Any = """ Computes SQuAD scores (F1 and EM). Args: predictions: List of question-answers dictionaries with the following key-values: - \'id\': id of the question-answer pair as given in the references (see below) - \'prediction_text\': the text of the answer references: List of question-answers dictionaries with the following key-values: - \'id\': id of the question-answer pair (see above), - \'answers\': a Dict in the SQuAD dataset format { \'text\': list of possible texts for the answer, as a list of strings \'answer_start\': list of start positions for the answer, as a list of ints } Note that answer_start values are not taken into account to compute the metric. Returns: \'exact_match\': Exact match (the normalized answer exactly match the gold answer) \'f1\': The F-score of predicted tokens versus the gold answer Examples: >>> predictions = [{\'prediction_text\': \'1976\', \'id\': \'56e10a3be3433e1400422b22\'}] >>> references = [{\'answers\': {\'answer_start\': [97], \'text\': [\'1976\']}, \'id\': \'56e10a3be3433e1400422b22\'}] >>> squad_metric = datasets.load_metric(\"squad\") >>> results = squad_metric.compute(predictions=predictions, references=references) >>> print(results) {\'exact_match\': 100.0, \'f1\': 100.0} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class _UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': {'''id''': datasets.Value('''string''' ), '''prediction_text''': datasets.Value('''string''' )}, '''references''': { '''id''': datasets.Value('''string''' ), '''answers''': datasets.features.Sequence( { '''text''': datasets.Value('''string''' ), '''answer_start''': datasets.Value('''int32''' ), } ), }, } ) , codebase_urls=['''https://rajpurkar.github.io/SQuAD-explorer/'''] , reference_urls=['''https://rajpurkar.github.io/SQuAD-explorer/'''] , ) def SCREAMING_SNAKE_CASE (self , a_ , a_ ): '''simple docstring''' __snake_case : List[str] = {prediction['''id''']: prediction['''prediction_text'''] for prediction in predictions} __snake_case : int = [ { '''paragraphs''': [ { '''qas''': [ { '''answers''': [{'''text''': answer_text} for answer_text in ref['''answers''']['''text''']], '''id''': ref['''id'''], } for ref in references ] } ] } ] __snake_case : int = evaluate(dataset=_UpperCamelCase , predictions=_UpperCamelCase ) return score
102
import gc import unittest from parameterized import parameterized from diffusers import FlaxUNetaDConditionModel from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow if is_flax_available(): import jax import jax.numpy as jnp @slow @require_flax class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : Any , _UpperCamelCase : Any , _UpperCamelCase : Tuple ) ->List[Any]: return f'''gaussian_noise_s={seed}_shape={'_'.join([str(_UpperCamelCase ) for s in shape] )}.npy''' def snake_case__( self : Any ) ->List[str]: # clean up the VRAM after each test super().tearDown() gc.collect() def snake_case__( self : int , _UpperCamelCase : Union[str, Any]=0 , _UpperCamelCase : int=(4, 4, 6_4, 6_4) , _UpperCamelCase : Optional[int]=False ) ->Tuple: snake_case_ = jnp.bfloataa if fpaa else jnp.floataa snake_case_ = jnp.array(load_hf_numpy(self.get_file_format(_UpperCamelCase , _UpperCamelCase ) ) , dtype=_UpperCamelCase ) return image def snake_case__( self : List[Any] , _UpperCamelCase : Optional[Any]=False , _UpperCamelCase : Optional[int]="CompVis/stable-diffusion-v1-4" ) ->Optional[Any]: snake_case_ = jnp.bfloataa if fpaa else jnp.floataa snake_case_ = '''bf16''' if fpaa else None snake_case_, snake_case_ = FlaxUNetaDConditionModel.from_pretrained( _UpperCamelCase , subfolder='''unet''' , dtype=_UpperCamelCase , revision=_UpperCamelCase ) return model, params def snake_case__( self : Dict , _UpperCamelCase : List[Any]=0 , _UpperCamelCase : Tuple=(4, 7_7, 7_6_8) , _UpperCamelCase : List[Any]=False ) ->int: snake_case_ = jnp.bfloataa if fpaa else jnp.floataa snake_case_ = jnp.array(load_hf_numpy(self.get_file_format(_UpperCamelCase , _UpperCamelCase ) ) , dtype=_UpperCamelCase ) return hidden_states @parameterized.expand( [ # fmt: off [8_3, 4, [-0.2323, -0.1304, 0.0813, -0.3093, -0.0919, -0.1571, -0.1125, -0.5806]], [1_7, 0.55, [-0.0831, -0.2443, 0.0901, -0.0919, 0.3396, 0.0103, -0.3743, 0.0701]], [8, 0.89, [-0.4863, 0.0859, 0.0875, -0.1658, 0.9199, -0.0114, 0.4839, 0.4639]], [3, 1_0_0_0, [-0.5649, 0.2402, -0.5518, 0.1248, 1.1328, -0.2443, -0.0325, -1.0078]], # fmt: on ] ) def snake_case__( self : Optional[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : List[str] , _UpperCamelCase : Optional[int] ) ->Union[str, Any]: snake_case_, snake_case_ = self.get_unet_model(model_id='''CompVis/stable-diffusion-v1-4''' , fpaa=_UpperCamelCase ) snake_case_ = self.get_latents(_UpperCamelCase , fpaa=_UpperCamelCase ) snake_case_ = self.get_encoder_hidden_states(_UpperCamelCase , fpaa=_UpperCamelCase ) snake_case_ = model.apply( {'''params''': params} , _UpperCamelCase , jnp.array(_UpperCamelCase , dtype=jnp.intaa ) , encoder_hidden_states=_UpperCamelCase , ).sample assert sample.shape == latents.shape snake_case_ = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) snake_case_ = jnp.array(_UpperCamelCase , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware assert jnp.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-2 ) @parameterized.expand( [ # fmt: off [8_3, 4, [0.1514, 0.0807, 0.1624, 0.1016, -0.1896, 0.0263, 0.0677, 0.2310]], [1_7, 0.55, [0.1164, -0.0216, 0.0170, 0.1589, -0.3120, 0.1005, -0.0581, -0.1458]], [8, 0.89, [-0.1758, -0.0169, 0.1004, -0.1411, 0.1312, 0.1103, -0.1996, 0.2139]], [3, 1_0_0_0, [0.1214, 0.0352, -0.0731, -0.1562, -0.0994, -0.0906, -0.2340, -0.0539]], # fmt: on ] ) def snake_case__( self : Optional[int] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str ) ->Dict: snake_case_, snake_case_ = self.get_unet_model(model_id='''stabilityai/stable-diffusion-2''' , fpaa=_UpperCamelCase ) snake_case_ = self.get_latents(_UpperCamelCase , shape=(4, 4, 9_6, 9_6) , fpaa=_UpperCamelCase ) snake_case_ = self.get_encoder_hidden_states(_UpperCamelCase , shape=(4, 7_7, 1_0_2_4) , fpaa=_UpperCamelCase ) snake_case_ = model.apply( {'''params''': params} , _UpperCamelCase , jnp.array(_UpperCamelCase , dtype=jnp.intaa ) , encoder_hidden_states=_UpperCamelCase , ).sample assert sample.shape == latents.shape snake_case_ = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) snake_case_ = jnp.array(_UpperCamelCase , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware assert jnp.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-2 )
8
0
'''simple docstring''' from math import pi def lowerCAmelCase_ ( snake_case_ : Dict , snake_case_ : str ) -> Tuple: '''simple docstring''' return 2 * pi * radius * (angle / 3_60) if __name__ == "__main__": print(arc_length(90, 10))
1
import functools import gc import inspect import torch from .imports import is_npu_available, is_xpu_available def __SCREAMING_SNAKE_CASE (*SCREAMING_SNAKE_CASE__ ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = list(SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) ): snake_case_ = None gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() return objects def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = [ '''CUDA out of memory.''', # CUDA OOM '''cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.''', # CUDNN SNAFU '''DefaultCPUAllocator: can\'t allocate memory''', # CPU OOM ] if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and len(exception.args ) == 1: return any(err in exception.args[0] for err in _statements ) return False def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = 128 ): if function is None: return functools.partial(SCREAMING_SNAKE_CASE__ , starting_batch_size=SCREAMING_SNAKE_CASE__ ) snake_case_ = starting_batch_size def decorator(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): nonlocal batch_size gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() snake_case_ = list(inspect.signature(SCREAMING_SNAKE_CASE__ ).parameters.keys() ) # Guard against user error if len(SCREAMING_SNAKE_CASE__ ) < (len(SCREAMING_SNAKE_CASE__ ) + 1): snake_case_ = ''', '''.join([F'''{arg}={value}''' for arg, value in zip(params[1:] , args[1:] )] ) raise TypeError( F'''Batch size was passed into `{function.__name__}` as the first argument when called.''' F'''Remove this as the decorator already does so: `{function.__name__}({arg_str})`''' ) while True: if batch_size == 0: raise RuntimeError('''No executable batch size found, reached zero.''' ) try: return function(SCREAMING_SNAKE_CASE__ , *SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) except Exception as e: if should_reduce_batch_size(SCREAMING_SNAKE_CASE__ ): gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() batch_size //= 2 else: raise return decorator
8
0
'''simple docstring''' import inspect import unittest from huggingface_hub import hf_hub_download from transformers import ConvNextConfig, UperNetConfig from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import UperNetForSemanticSegmentation from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowercase : """simple docstring""" def __init__( self ,a_ ,a_=13 ,a_=32 ,a_=3 ,a_=4 ,a_=[10, 20, 30, 40] ,a_=[2, 2, 3, 2] ,a_=True ,a_=True ,a_=37 ,a_="gelu" ,a_=10 ,a_=0.02 ,a_=["stage2", "stage3", "stage4"] ,a_=3 ,a_=None ,) -> str: _UpperCAmelCase : str = parent _UpperCAmelCase : Dict = batch_size _UpperCAmelCase : Union[str, Any] = image_size _UpperCAmelCase : Optional[int] = num_channels _UpperCAmelCase : str = num_stages _UpperCAmelCase : Union[str, Any] = hidden_sizes _UpperCAmelCase : str = depths _UpperCAmelCase : int = is_training _UpperCAmelCase : Any = use_labels _UpperCAmelCase : Union[str, Any] = intermediate_size _UpperCAmelCase : Optional[int] = hidden_act _UpperCAmelCase : Optional[Any] = type_sequence_label_size _UpperCAmelCase : Dict = initializer_range _UpperCAmelCase : Tuple = out_features _UpperCAmelCase : Union[str, Any] = num_labels _UpperCAmelCase : Tuple = scope _UpperCAmelCase : Optional[Any] = num_stages def _snake_case ( self ) -> int: _UpperCAmelCase : Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _UpperCAmelCase : Dict = None if self.use_labels: _UpperCAmelCase : Optional[Any] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) _UpperCAmelCase : Tuple = self.get_config() return config, pixel_values, labels def _snake_case ( self ) -> List[str]: return ConvNextConfig( num_channels=self.num_channels ,num_stages=self.num_stages ,hidden_sizes=self.hidden_sizes ,depths=self.depths ,is_training=self.is_training ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,out_features=self.out_features ,) def _snake_case ( self ) -> Optional[int]: return UperNetConfig( backbone_config=self.get_backbone_config() ,hidden_size=512 ,pool_scales=[1, 2, 3, 6] ,use_auxiliary_head=_UpperCamelCase ,auxiliary_loss_weight=0.4 ,auxiliary_in_channels=40 ,auxiliary_channels=256 ,auxiliary_num_convs=1 ,auxiliary_concat_input=_UpperCamelCase ,loss_ignore_index=255 ,num_labels=self.num_labels ,) def _snake_case ( self ,a_ ,a_ ,a_ ) -> int: _UpperCAmelCase : Tuple = UperNetForSemanticSegmentation(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() _UpperCAmelCase : Dict = model(_UpperCamelCase ) self.parent.assertEqual( result.logits.shape ,(self.batch_size, self.num_labels, self.image_size, self.image_size) ) def _snake_case ( self ) -> int: _UpperCAmelCase : Tuple = self.prepare_config_and_inputs() ( ( _UpperCAmelCase ) ,( _UpperCAmelCase ) ,( _UpperCAmelCase ) , ) : Optional[int] = config_and_inputs _UpperCAmelCase : Optional[Any] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class lowercase ( __A , __A , unittest.TestCase ): """simple docstring""" UpperCAmelCase = (UperNetForSemanticSegmentation,) if is_torch_available() else () UpperCAmelCase = {"image-segmentation": UperNetForSemanticSegmentation} if is_torch_available() else {} UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False def _snake_case ( self ) -> int: _UpperCAmelCase : List[str] = UperNetModelTester(self ) _UpperCAmelCase : Union[str, Any] = ConfigTester(self ,config_class=_UpperCamelCase ,has_text_modality=_UpperCamelCase ,hidden_size=37 ) def _snake_case ( self ) -> List[str]: 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 _snake_case ( self ) -> Dict: return def _snake_case ( self ) -> str: _UpperCAmelCase ,_UpperCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCAmelCase : Optional[Any] = model_class(_UpperCamelCase ) _UpperCAmelCase : Union[str, Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _UpperCAmelCase : int = [*signature.parameters.keys()] _UpperCAmelCase : Dict = ["""pixel_values"""] self.assertListEqual(arg_names[:1] ,_UpperCamelCase ) def _snake_case ( self ) -> Optional[Any]: _UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*_UpperCamelCase ) @unittest.skip(reason="""UperNet does not use inputs_embeds""" ) def _snake_case ( self ) -> Tuple: pass @unittest.skip(reason="""UperNet does not support input and output embeddings""" ) def _snake_case ( self ) -> Tuple: pass @unittest.skip(reason="""UperNet does not have a base model""" ) def _snake_case ( self ) -> Optional[int]: pass @unittest.skip(reason="""UperNet does not have a base model""" ) def _snake_case ( self ) -> str: pass @require_torch_multi_gpu @unittest.skip(reason="""UperNet has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`""" ) def _snake_case ( self ) -> Tuple: pass @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def _snake_case ( self ) -> Dict: pass def _snake_case ( self ) -> Any: def check_hidden_states_output(a_ ,a_ ,a_ ): _UpperCAmelCase : Optional[int] = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() with torch.no_grad(): _UpperCAmelCase : List[str] = model(**self._prepare_for_class(_UpperCamelCase ,_UpperCamelCase ) ) _UpperCAmelCase : List[str] = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states _UpperCAmelCase : int = self.model_tester.num_stages self.assertEqual(len(_UpperCamelCase ) ,expected_num_stages + 1 ) # ConvNext's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) ,[self.model_tester.image_size // 4, self.model_tester.image_size // 4] ,) _UpperCAmelCase ,_UpperCAmelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCAmelCase : Optional[int] = True check_hidden_states_output(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _UpperCAmelCase : str = True check_hidden_states_output(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ) def _snake_case ( self ) -> Union[str, Any]: _UpperCAmelCase ,_UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase : Union[str, Any] = _config_zero_init(_UpperCamelCase ) _UpperCAmelCase : Tuple = _config_zero_init(configs_no_init.backbone_config ) for model_class in self.all_model_classes: _UpperCAmelCase : List[str] = model_class(config=_UpperCamelCase ) for name, param in model.named_parameters(): if param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() ,[0.0, 1.0] ,msg=f'''Parameter {name} of model {model_class} seems not properly initialized''' ,) @unittest.skip(reason="""UperNet does not have tied weights""" ) def _snake_case ( self ) -> Tuple: pass @slow def _snake_case ( self ) -> List[Any]: for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCAmelCase : List[Any] = UperNetForSemanticSegmentation.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def snake_case_ ( )-> List[str]: '''simple docstring''' _UpperCAmelCase : Union[str, Any] = hf_hub_download( repo_id="""hf-internal-testing/fixtures_ade20k""" , repo_type="""dataset""" , filename="""ADE_val_00000001.jpg""" ) _UpperCAmelCase : str = Image.open(SCREAMING_SNAKE_CASE__ ).convert("""RGB""" ) return image @require_torch @require_vision @slow class lowercase ( unittest.TestCase ): """simple docstring""" def _snake_case ( self ) -> Dict: _UpperCAmelCase : Tuple = AutoImageProcessor.from_pretrained("""openmmlab/upernet-swin-tiny""" ) _UpperCAmelCase : List[Any] = UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-swin-tiny""" ).to(_UpperCamelCase ) _UpperCAmelCase : Union[str, Any] = prepare_img() _UpperCAmelCase : List[str] = processor(images=_UpperCamelCase ,return_tensors="""pt""" ).to(_UpperCamelCase ) with torch.no_grad(): _UpperCAmelCase : Tuple = model(**_UpperCamelCase ) _UpperCAmelCase : Optional[int] = torch.Size((1, model.config.num_labels, 512, 512) ) self.assertEqual(outputs.logits.shape ,_UpperCamelCase ) _UpperCAmelCase : int = torch.tensor( [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] ,_UpperCamelCase ,atol=1E-4 ) ) def _snake_case ( self ) -> Optional[Any]: _UpperCAmelCase : int = AutoImageProcessor.from_pretrained("""openmmlab/upernet-convnext-tiny""" ) _UpperCAmelCase : int = UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-convnext-tiny""" ).to(_UpperCamelCase ) _UpperCAmelCase : Dict = prepare_img() _UpperCAmelCase : List[str] = processor(images=_UpperCamelCase ,return_tensors="""pt""" ).to(_UpperCamelCase ) with torch.no_grad(): _UpperCAmelCase : str = model(**_UpperCamelCase ) _UpperCAmelCase : int = torch.Size((1, model.config.num_labels, 512, 512) ) self.assertEqual(outputs.logits.shape ,_UpperCamelCase ) _UpperCAmelCase : Optional[int] = torch.tensor( [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] ,_UpperCamelCase ,atol=1E-4 ) )
215
from __future__ import annotations def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): return [ord(SCREAMING_SNAKE_CASE__ ) - 96 for elem in plain] def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): return "".join(chr(elem + 96 ) for elem in encoded ) def __SCREAMING_SNAKE_CASE (): snake_case_ = encode(input('''-> ''' ).strip().lower() ) print('''Encoded: ''' , SCREAMING_SNAKE_CASE__ ) print('''Decoded:''' , decode(SCREAMING_SNAKE_CASE__ ) ) if __name__ == "__main__": main()
8
0
import warnings from ...utils import logging from .image_processing_owlvit import OwlViTImageProcessor __UpperCamelCase : str = logging.get_logger(__name__) class lowercase__ ( __A): def __init__( self : Tuple , *UpperCamelCase__ : Any , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' warnings.warn( '''The class OwlViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use OwlViTImageProcessor instead.''' , _UpperCamelCase , ) super().__init__(*_UpperCamelCase , **_UpperCamelCase )
182
import math def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): 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''')
8
0
def a__ ( UpperCAmelCase : int ) -> str: if len(SCREAMING_SNAKE_CASE__ ) <= 1: return lst UpperCAmelCase : Optional[int] = 1 while i < len(SCREAMING_SNAKE_CASE__ ): if lst[i - 1] <= lst[i]: i += 1 else: UpperCAmelCase , UpperCAmelCase : Tuple = lst[i], lst[i - 1] i -= 1 if i == 0: UpperCAmelCase : List[str] = 1 return lst if __name__ == "__main__": _lowerCamelCase : List[Any] = input("Enter numbers separated by a comma:\n").strip() _lowerCamelCase : List[str] = [int(item) for item in user_input.split(",")] print(gnome_sort(unsorted))
336
import os import re from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = {'''vocab_file''': '''spiece.model'''} lowerCAmelCase_ = { '''vocab_file''': { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model''', '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model''' ), } } lowerCAmelCase_ = { '''google/bigbird-roberta-base''': 40_96, '''google/bigbird-roberta-large''': 40_96, '''google/bigbird-base-trivia-itc''': 40_96, } class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE : Optional[int] = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE : List[Any] = ["input_ids", "attention_mask"] SCREAMING_SNAKE_CASE : List[int] = [] def __init__( self : List[str] , _UpperCamelCase : List[str] , _UpperCamelCase : Dict="<unk>" , _UpperCamelCase : List[str]="<s>" , _UpperCamelCase : Tuple="</s>" , _UpperCamelCase : Any="<pad>" , _UpperCamelCase : Any="[SEP]" , _UpperCamelCase : Optional[Any]="[MASK]" , _UpperCamelCase : Any="[CLS]" , _UpperCamelCase : Optional[Dict[str, Any]] = None , **_UpperCamelCase : Dict , ) ->None: snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else bos_token snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else eos_token snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else unk_token snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else pad_token snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else cls_token snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else sep_token # Mask token behave like a normal word, i.e. include the space before it snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else mask_token snake_case_ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , sep_token=_UpperCamelCase , mask_token=_UpperCamelCase , cls_token=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCamelCase , ) snake_case_ = vocab_file snake_case_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_UpperCamelCase ) @property def snake_case__( self : str ) ->List[Any]: return self.sp_model.get_piece_size() def snake_case__( self : int ) ->Union[str, Any]: snake_case_ = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Tuple ) ->Any: snake_case_ = self.__dict__.copy() snake_case_ = None return state def __setstate__( self : str , _UpperCamelCase : List[Any] ) ->List[str]: snake_case_ = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): snake_case_ = {} snake_case_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def snake_case__( self : Optional[int] , _UpperCamelCase : str ) ->List[str]: return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def snake_case__( self : str , _UpperCamelCase : List[str] ) ->Tuple: return self.sp_model.piece_to_id(_UpperCamelCase ) def snake_case__( self : Union[str, Any] , _UpperCamelCase : str ) ->List[Any]: snake_case_ = self.sp_model.IdToPiece(_UpperCamelCase ) return token def snake_case__( self : Dict , _UpperCamelCase : Optional[int] ) ->List[str]: snake_case_ = [] snake_case_ = '''''' snake_case_ = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(_UpperCamelCase ) + token snake_case_ = True snake_case_ = [] else: current_sub_tokens.append(_UpperCamelCase ) snake_case_ = False out_string += self.sp_model.decode(_UpperCamelCase ) return out_string.strip() def snake_case__( self : int , _UpperCamelCase : List[int] , _UpperCamelCase : bool = False , _UpperCamelCase : bool = None , _UpperCamelCase : bool = True , **_UpperCamelCase : List[str] , ) ->str: snake_case_ = kwargs.pop('''use_source_tokenizer''' , _UpperCamelCase ) snake_case_ = self.convert_ids_to_tokens(_UpperCamelCase , skip_special_tokens=_UpperCamelCase ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 snake_case_ = [] snake_case_ = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_UpperCamelCase ) ) snake_case_ = [] sub_texts.append(_UpperCamelCase ) else: current_sub_text.append(_UpperCamelCase ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_UpperCamelCase ) ) # Mimic the behavior of the Rust tokenizer: # No space before [MASK] and [SEP] if spaces_between_special_tokens: snake_case_ = re.sub(R''' (\[(MASK|SEP)\])''' , R'''\1''' , ''' '''.join(_UpperCamelCase ) ) else: snake_case_ = ''''''.join(_UpperCamelCase ) snake_case_ = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: snake_case_ = self.clean_up_tokenization(_UpperCamelCase ) return clean_text else: return text def snake_case__( self : List[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) ->Tuple[str]: if not os.path.isdir(_UpperCamelCase ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return snake_case_ = os.path.join( _UpperCamelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase , '''wb''' ) as fi: snake_case_ = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,) def snake_case__( self : Tuple , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) ->List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] snake_case_ = [self.cls_token_id] snake_case_ = [self.sep_token_id] return cls + token_ids_a + sep + token_ids_a + sep def snake_case__( self : List[str] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : bool = False ) ->List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) if token_ids_a is None: return [1] + ([0] * len(_UpperCamelCase )) + [1] return [1] + ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] def snake_case__( self : List[Any] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) ->List[int]: snake_case_ = [self.sep_token_id] snake_case_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]
8
0
def __A ( __lowerCAmelCase = 1_000 )-> int: """simple docstring""" return sum(2 * a * ((a - 1) // 2) for a in range(3 , n + 1 ) ) if __name__ == "__main__": print(solution())
39
from __future__ import annotations from collections.abc import Generator def __SCREAMING_SNAKE_CASE (): snake_case_ = {} snake_case_ = 2 while True: snake_case_ = factor_map.pop(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if factor: snake_case_ = factor + prime while x in factor_map: x += factor snake_case_ = factor else: snake_case_ = prime yield prime prime += 1 def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = 1E10 ): snake_case_ = sieve() snake_case_ = 1 while True: snake_case_ = next(SCREAMING_SNAKE_CASE__ ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(SCREAMING_SNAKE_CASE__ ) n += 2 if __name__ == "__main__": print(solution())
8
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 lowerCamelCase__ ( snake_case_ : Any , snake_case_ : Dict , snake_case_ : Optional[int]=None ) -> List[str]: # set parameter of one layer assert torch_layer.weight.shape == weight.shape, f"""{torch_layer} layer.weight does not match""" __snake_case = 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""" __snake_case = nn.Parameter(SCREAMING_SNAKE_CASE__ ) def lowerCamelCase__ ( snake_case_ : Optional[Any] , snake_case_ : Tuple , snake_case_ : Any ) -> int: # set torch weights for 1-to-1 comparison __snake_case = np.asarray(weights[0] ) __snake_case = np.asarray(weights[1] ) __snake_case = 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 lowerCamelCase__ ( snake_case_ : Optional[int] , snake_case_ : int , snake_case_ : str ) -> Any: # set torch weights for 1-to-1 comparison __snake_case = np.asarray(weights[0] ) __snake_case = np.asarray(weights[1] ) __snake_case = np.asarray(weights[2] ) __snake_case = 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 lowerCamelCase__ ( snake_case_ : Any , snake_case_ : List[str] , snake_case_ : Tuple ) -> Any: # layernorm 1 __snake_case = weights[0][0][0] __snake_case = np.asarray(layer_norm_a[0] ) __snake_case = 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 __snake_case = 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 __snake_case = weights[2][0][1][2] # Chunked Feed Forward if len(SCREAMING_SNAKE_CASE__ ) == 4: __snake_case = intermediate_weights[2] # layernorm 2 __snake_case = np.asarray(intermediate_weights[0][0] ) __snake_case = 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 __snake_case = np.asarray(intermediate_weights[1][0] ) __snake_case = 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 __snake_case = np.asarray(intermediate_weights[4][0] ) __snake_case = 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 lowerCamelCase__ ( snake_case_ : str , snake_case_ : Optional[int] , snake_case_ : Any ) -> List[str]: # reformer model __snake_case = torch_model.reformer # word embeds __snake_case = np.asarray(weights[1] ) set_param( torch_model_reformer.embeddings.word_embeddings , torch.tensor(SCREAMING_SNAKE_CASE__ ) , ) if isinstance(weights[3] , SCREAMING_SNAKE_CASE__ ): __snake_case = torch_model_reformer.embeddings.position_embeddings for emb_idx in range(len(position_embeddings.weights ) ): __snake_case = 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""" __snake_case = nn.Parameter(torch.tensor(SCREAMING_SNAKE_CASE__ ) ) __snake_case = 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 ): __snake_case = 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 __snake_case = np.asarray(weights[7][0] ) __snake_case = 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 __snake_case = np.asarray(weights[9][0] ) __snake_case = 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 lowerCamelCase__ ( snake_case_ : Optional[int] , snake_case_ : int , snake_case_ : str ) -> List[Any]: # Initialise PyTorch model __snake_case = ReformerConfig.from_json_file(SCREAMING_SNAKE_CASE__ ) print(f"""Building PyTorch model from configuration: {config}""" ) __snake_case = ReformerModelWithLMHead(SCREAMING_SNAKE_CASE__ ) with open(SCREAMING_SNAKE_CASE__ , '''rb''' ) as f: __snake_case = 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__": snake_case_ = 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.' ) snake_case_ = parser.parse_args() convert_trax_checkpoint_to_pytorch(args.trax_model_pkl_path, args.config_file, args.pytorch_dump_path)
24
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase_ = {'''configuration_opt''': ['''OPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OPTConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''OPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OPTForCausalLM''', '''OPTModel''', '''OPTPreTrainedModel''', '''OPTForSequenceClassification''', '''OPTForQuestionAnswering''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['''TFOPTForCausalLM''', '''TFOPTModel''', '''TFOPTPreTrainedModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''FlaxOPTForCausalLM''', '''FlaxOPTModel''', '''FlaxOPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
8
0
def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> int: if num < 0: return False __lowerCamelCase : List[str] = num __lowerCamelCase : Union[str, Any] = 0 while num > 0: __lowerCamelCase : Union[str, Any] = rev_num * 1_0 + (num % 1_0) num //= 1_0 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
73
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer from .base import PipelineTool class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = "philschmid/bart-large-cnn-samsum" SCREAMING_SNAKE_CASE : Tuple = ( "This is a tool that summarizes an English text. It takes an input `text` containing the text to summarize, " "and returns a summary of the text." ) SCREAMING_SNAKE_CASE : str = "summarizer" SCREAMING_SNAKE_CASE : str = AutoTokenizer SCREAMING_SNAKE_CASE : str = AutoModelForSeqaSeqLM SCREAMING_SNAKE_CASE : Optional[int] = ["text"] SCREAMING_SNAKE_CASE : Optional[int] = ["text"] def snake_case__( self : str , _UpperCamelCase : int ) ->Optional[int]: return self.pre_processor(_UpperCamelCase , return_tensors='''pt''' , truncation=_UpperCamelCase ) def snake_case__( self : Tuple , _UpperCamelCase : Optional[int] ) ->Tuple: return self.model.generate(**_UpperCamelCase )[0] def snake_case__( self : Optional[Any] , _UpperCamelCase : Optional[int] ) ->Any: return self.pre_processor.decode(_UpperCamelCase , skip_special_tokens=_UpperCamelCase , clean_up_tokenization_spaces=_UpperCamelCase )
8
0
from __future__ import annotations import copy import inspect import json import math import os import tempfile import unittest from importlib import import_module import numpy as np from transformers import ViTMAEConfig from transformers.file_utils import cached_property, is_tf_available, is_vision_available from transformers.testing_utils import require_tf, require_vision, slow 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 TFViTMAEForPreTraining, TFViTMAEModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class lowerCAmelCase_ : def __init__( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_=13, SCREAMING_SNAKE_CASE_=30, SCREAMING_SNAKE_CASE_=2, SCREAMING_SNAKE_CASE_=3, SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_=32, SCREAMING_SNAKE_CASE_=2, SCREAMING_SNAKE_CASE_=4, SCREAMING_SNAKE_CASE_=37, SCREAMING_SNAKE_CASE_="gelu", SCREAMING_SNAKE_CASE_=0.1, SCREAMING_SNAKE_CASE_=0.1, SCREAMING_SNAKE_CASE_=10, SCREAMING_SNAKE_CASE_=0.02, SCREAMING_SNAKE_CASE_=3, SCREAMING_SNAKE_CASE_=0.6, SCREAMING_SNAKE_CASE_=None, ) -> Any: UpperCamelCase : Tuple = parent UpperCamelCase : Optional[Any] = batch_size UpperCamelCase : Union[str, Any] = image_size UpperCamelCase : str = patch_size UpperCamelCase : Optional[Any] = num_channels UpperCamelCase : Any = is_training UpperCamelCase : Dict = use_labels UpperCamelCase : int = hidden_size UpperCamelCase : Dict = num_hidden_layers UpperCamelCase : int = num_attention_heads UpperCamelCase : Any = intermediate_size UpperCamelCase : List[Any] = hidden_act UpperCamelCase : str = hidden_dropout_prob UpperCamelCase : int = attention_probs_dropout_prob UpperCamelCase : List[Any] = type_sequence_label_size UpperCamelCase : Optional[int] = initializer_range UpperCamelCase : Any = mask_ratio UpperCamelCase : Optional[int] = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) UpperCamelCase : int = (image_size // patch_size) ** 2 UpperCamelCase : Optional[Any] = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def snake_case_ ( self ) -> Optional[Any]: UpperCamelCase : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase : Union[str, Any] = None if self.use_labels: UpperCamelCase : Any = ids_tensor([self.batch_size], self.type_sequence_label_size ) UpperCamelCase : List[Any] = self.get_config() return config, pixel_values, labels def snake_case_ ( self ) -> str: return ViTMAEConfig( 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, decoder_hidden_size=self.hidden_size, decoder_num_hidden_layers=self.num_hidden_layers, decoder_num_attention_heads=self.num_attention_heads, decoder_intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, is_decoder=_UpperCamelCase, initializer_range=self.initializer_range, mask_ratio=self.mask_ratio, ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: UpperCamelCase : List[Any] = TFViTMAEModel(config=_UpperCamelCase ) UpperCamelCase : List[Any] = model(_UpperCamelCase, training=_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Optional[int]: UpperCamelCase : Optional[int] = TFViTMAEForPreTraining(_UpperCamelCase ) UpperCamelCase : Tuple = model(_UpperCamelCase, training=_UpperCamelCase ) # expected sequence length = num_patches UpperCamelCase : Any = (self.image_size // self.patch_size) ** 2 UpperCamelCase : int = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape, (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images UpperCamelCase : Tuple = 1 UpperCamelCase : str = TFViTMAEForPreTraining(_UpperCamelCase ) UpperCamelCase : int = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCamelCase : Optional[int] = model(_UpperCamelCase, training=_UpperCamelCase ) UpperCamelCase : Optional[int] = self.patch_size**2 self.parent.assertEqual(result.logits.shape, (self.batch_size, num_patches, expected_num_channels) ) def snake_case_ ( self ) -> Dict: UpperCamelCase : List[Any] = self.prepare_config_and_inputs() ((UpperCamelCase) , (UpperCamelCase) , (UpperCamelCase)) : int = config_and_inputs UpperCamelCase : Union[str, Any] = {'pixel_values': pixel_values} return config, inputs_dict @require_tf class lowerCAmelCase_ ( __A , __A , unittest.TestCase ): UpperCAmelCase__ : str = (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else () UpperCAmelCase__ : Tuple = {"feature-extraction": TFViTMAEModel} if is_tf_available() else {} UpperCAmelCase__ : Union[str, Any] = False UpperCAmelCase__ : Optional[Any] = False UpperCAmelCase__ : Union[str, Any] = False UpperCAmelCase__ : Union[str, Any] = False def snake_case_ ( self ) -> List[Any]: UpperCamelCase : Dict = TFViTMAEModelTester(self ) UpperCamelCase : str = ConfigTester(self, config_class=_UpperCamelCase, has_text_modality=_UpperCamelCase, hidden_size=37 ) def snake_case_ ( self ) -> int: self.config_tester.run_common_tests() @unittest.skip(reason='ViTMAE does not use inputs_embeds' ) def snake_case_ ( self ) -> int: pass def snake_case_ ( self ) -> int: UpperCamelCase , UpperCamelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase : List[str] = model_class(_UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings(), (tf.keras.layers.Layer) ) UpperCamelCase : Optional[Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_UpperCamelCase, tf.keras.layers.Layer ) ) def snake_case_ ( self ) -> Union[str, Any]: UpperCamelCase , UpperCamelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase : Any = model_class(_UpperCamelCase ) UpperCamelCase : Optional[int] = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase : str = [*signature.parameters.keys()] UpperCamelCase : Tuple = ['pixel_values'] self.assertListEqual(arg_names[:1], _UpperCamelCase ) def snake_case_ ( self ) -> int: UpperCamelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def snake_case_ ( self ) -> Dict: UpperCamelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_UpperCamelCase ) def snake_case_ ( self ) -> Optional[Any]: # make the mask reproducible np.random.seed(2 ) UpperCamelCase , UpperCamelCase : str = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase : Tuple = int((config.image_size // config.patch_size) ** 2 ) UpperCamelCase : str = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: UpperCamelCase : str = model_class(_UpperCamelCase ) UpperCamelCase : Optional[Any] = self._prepare_for_class(_UpperCamelCase, _UpperCamelCase ) UpperCamelCase : List[Any] = model(_UpperCamelCase, noise=_UpperCamelCase ) UpperCamelCase : Any = copy.deepcopy(self._prepare_for_class(_UpperCamelCase, _UpperCamelCase ) ) UpperCamelCase : int = model(**_UpperCamelCase, noise=_UpperCamelCase ) UpperCamelCase : Union[str, Any] = outputs_dict[0].numpy() UpperCamelCase : Optional[int] = outputs_keywords[0].numpy() self.assertLess(np.sum(np.abs(output_dict - output_keywords ) ), 1e-6 ) def snake_case_ ( self ) -> Optional[Any]: # make the mask reproducible np.random.seed(2 ) UpperCamelCase , UpperCamelCase : List[str] = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase : int = int((config.image_size // config.patch_size) ** 2 ) UpperCamelCase : Union[str, Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) def prepare_numpy_arrays(SCREAMING_SNAKE_CASE_ ): UpperCamelCase : str = {} for k, v in inputs_dict.items(): if tf.is_tensor(_UpperCamelCase ): UpperCamelCase : Tuple = v.numpy() else: UpperCamelCase : List[str] = np.array(_UpperCamelCase ) return inputs_np_dict for model_class in self.all_model_classes: UpperCamelCase : Optional[int] = model_class(_UpperCamelCase ) UpperCamelCase : List[str] = self._prepare_for_class(_UpperCamelCase, _UpperCamelCase ) UpperCamelCase : List[Any] = prepare_numpy_arrays(_UpperCamelCase ) UpperCamelCase : Any = model(_UpperCamelCase, noise=_UpperCamelCase ) UpperCamelCase : Dict = model(**_UpperCamelCase, noise=_UpperCamelCase ) self.assert_outputs_same(_UpperCamelCase, _UpperCamelCase ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Dict: # make masks reproducible np.random.seed(2 ) UpperCamelCase : Optional[int] = int((tf_model.config.image_size // tf_model.config.patch_size) ** 2 ) UpperCamelCase : Tuple = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) UpperCamelCase : Union[str, Any] = tf.constant(_UpperCamelCase ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument UpperCamelCase : Optional[int] = tf_noise super().check_pt_tf_models(_UpperCamelCase, _UpperCamelCase, _UpperCamelCase ) def snake_case_ ( self ) -> str: # make mask reproducible np.random.seed(2 ) UpperCamelCase , UpperCamelCase : str = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase : Tuple = { module_member for model_class in self.all_model_classes for module in (import_module(model_class.__module__ ),) for module_member_name in dir(_UpperCamelCase ) if module_member_name.endswith('MainLayer' ) # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`. and module_member_name[: -len('MainLayer' )] == model_class.__name__[: -len('Model' )] for module_member in (getattr(_UpperCamelCase, _UpperCamelCase ),) if isinstance(_UpperCamelCase, _UpperCamelCase ) and tf.keras.layers.Layer in module_member.__bases__ and getattr(_UpperCamelCase, '_keras_serializable', _UpperCamelCase ) } UpperCamelCase : Tuple = int((config.image_size // config.patch_size) ** 2 ) UpperCamelCase : Dict = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) UpperCamelCase : Dict = tf.convert_to_tensor(_UpperCamelCase ) inputs_dict.update({'noise': noise} ) for main_layer_class in tf_main_layer_classes: UpperCamelCase : int = main_layer_class(_UpperCamelCase ) UpperCamelCase : str = { name: tf.keras.Input(tensor.shape[1:], dtype=tensor.dtype ) for name, tensor in inputs_dict.items() } UpperCamelCase : Any = tf.keras.Model(_UpperCamelCase, outputs=main_layer(_UpperCamelCase ) ) UpperCamelCase : int = model(_UpperCamelCase ) with tempfile.TemporaryDirectory() as tmpdirname: UpperCamelCase : Optional[int] = os.path.join(_UpperCamelCase, 'keras_model.h5' ) model.save(_UpperCamelCase ) UpperCamelCase : Tuple = tf.keras.models.load_model( _UpperCamelCase, custom_objects={main_layer_class.__name__: main_layer_class} ) assert isinstance(_UpperCamelCase, tf.keras.Model ) UpperCamelCase : Optional[Any] = model(_UpperCamelCase ) self.assert_outputs_same(_UpperCamelCase, _UpperCamelCase ) @slow def snake_case_ ( self ) -> str: # make mask reproducible np.random.seed(2 ) UpperCamelCase , UpperCamelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase : Dict = int((config.image_size // config.patch_size) ** 2 ) UpperCamelCase : List[Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: UpperCamelCase : List[str] = model_class(_UpperCamelCase ) UpperCamelCase : Optional[int] = self._prepare_for_class(_UpperCamelCase, _UpperCamelCase ) UpperCamelCase : List[str] = model(_UpperCamelCase, noise=_UpperCamelCase ) if model_class.__name__ == "TFViTMAEModel": UpperCamelCase : Optional[Any] = outputs.last_hidden_state.numpy() UpperCamelCase : int = 0 else: UpperCamelCase : List[Any] = outputs.logits.numpy() UpperCamelCase : Optional[Any] = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_UpperCamelCase, saved_model=_UpperCamelCase ) UpperCamelCase : List[str] = model_class.from_pretrained(_UpperCamelCase ) UpperCamelCase : Optional[Any] = model(_UpperCamelCase, noise=_UpperCamelCase ) if model_class.__name__ == "TFViTMAEModel": UpperCamelCase : int = after_outputs['last_hidden_state'].numpy() UpperCamelCase : Tuple = 0 else: UpperCamelCase : int = after_outputs['logits'].numpy() UpperCamelCase : Any = 0 UpperCamelCase : Union[str, Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(_UpperCamelCase, 1e-5 ) def snake_case_ ( self ) -> List[str]: # make mask reproducible np.random.seed(2 ) UpperCamelCase , UpperCamelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase : Optional[int] = int((config.image_size // config.patch_size) ** 2 ) UpperCamelCase : Tuple = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: UpperCamelCase : List[Any] = model_class(_UpperCamelCase ) UpperCamelCase : str = self._prepare_for_class(_UpperCamelCase, _UpperCamelCase ) UpperCamelCase : Union[str, Any] = model(_UpperCamelCase, noise=_UpperCamelCase ) UpperCamelCase : Optional[Any] = model.get_config() # make sure that returned config is jsonifiable, which is required by keras json.dumps(_UpperCamelCase ) UpperCamelCase : Union[str, Any] = model_class.from_config(model.get_config() ) # make sure it also accepts a normal config UpperCamelCase : Optional[int] = model_class.from_config(model.config ) UpperCamelCase : Optional[Any] = new_model(_UpperCamelCase ) # Build model new_model.set_weights(model.get_weights() ) UpperCamelCase : List[str] = new_model(_UpperCamelCase, noise=_UpperCamelCase ) self.assert_outputs_same(_UpperCamelCase, _UpperCamelCase ) @unittest.skip( reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n to get deterministic results.' ) def snake_case_ ( self ) -> List[Any]: pass @unittest.skip(reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load' ) def snake_case_ ( self ) -> Optional[int]: pass @slow def snake_case_ ( self ) -> List[str]: UpperCamelCase : Any = TFViTMAEModel.from_pretrained('google/vit-base-patch16-224' ) self.assertIsNotNone(_UpperCamelCase ) def UpperCamelCase ( ) -> List[str]: UpperCamelCase : Optional[int] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class lowerCAmelCase_ ( unittest.TestCase ): @cached_property def snake_case_ ( self ) -> Any: return ViTImageProcessor.from_pretrained('facebook/vit-mae-base' ) if is_vision_available() else None @slow def snake_case_ ( self ) -> str: # make random mask reproducible across the PT and TF model np.random.seed(2 ) UpperCamelCase : List[Any] = TFViTMAEForPreTraining.from_pretrained('facebook/vit-mae-base' ) UpperCamelCase : int = self.default_image_processor UpperCamelCase : int = prepare_img() UpperCamelCase : List[Any] = image_processor(images=_UpperCamelCase, return_tensors='tf' ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) UpperCamelCase : Union[str, Any] = ViTMAEConfig() UpperCamelCase : Any = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) UpperCamelCase : Optional[int] = np.random.uniform(size=(1, num_patches) ) # forward pass UpperCamelCase : Union[str, Any] = model(**_UpperCamelCase, noise=_UpperCamelCase ) # verify the logits UpperCamelCase : Tuple = tf.convert_to_tensor([1, 196, 768] ) self.assertEqual(outputs.logits.shape, _UpperCamelCase ) UpperCamelCase : List[Any] = tf.convert_to_tensor( [[-0.05_48, -1.70_23, -0.93_25], [0.37_21, -0.56_70, -0.22_33], [0.82_35, -1.38_78, -0.35_24]] ) tf.debugging.assert_near(outputs.logits[0, :3, :3], _UpperCamelCase, atol=1e-4 )
119
from collections import deque from .hash_table import HashTable class snake_case_ ( __A ): '''simple docstring''' def __init__( self : int , *_UpperCamelCase : int , **_UpperCamelCase : Tuple ) ->Tuple: super().__init__(*_UpperCamelCase , **_UpperCamelCase ) def snake_case__( self : Dict , _UpperCamelCase : List[str] , _UpperCamelCase : Dict ) ->Tuple: snake_case_ = deque([] ) if self.values[key] is None else self.values[key] self.values[key].appendleft(_UpperCamelCase ) snake_case_ = self.values[key] def snake_case__( self : List[Any] ) ->str: return ( sum(self.charge_factor - len(_UpperCamelCase ) for slot in self.values ) / self.size_table * self.charge_factor ) def snake_case__( self : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[int]=None ) ->str: if not ( len(self.values[key] ) == self.charge_factor and self.values.count(_UpperCamelCase ) == 0 ): return key return super()._collision_resolution(_UpperCamelCase , _UpperCamelCase )
8
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCamelCase_ = logging.get_logger(__name__) UpperCamelCase_ = { '''facebook/xlm-roberta-xl''': '''https://huggingface.co/facebook/xlm-roberta-xl/resolve/main/config.json''', '''facebook/xlm-roberta-xxl''': '''https://huggingface.co/facebook/xlm-roberta-xxl/resolve/main/config.json''', # See all XLM-RoBERTa-XL models at https://huggingface.co/models?filter=xlm-roberta-xl } class _snake_case ( __A ): '''simple docstring''' A__ : int = "xlm-roberta-xl" def __init__( self: str ,lowerCamelCase_: Union[str, Any]=250880 ,lowerCamelCase_: List[Any]=2560 ,lowerCamelCase_: Any=36 ,lowerCamelCase_: Dict=32 ,lowerCamelCase_: Optional[int]=10240 ,lowerCamelCase_: Dict="gelu" ,lowerCamelCase_: Union[str, Any]=0.1 ,lowerCamelCase_: Dict=0.1 ,lowerCamelCase_: Union[str, Any]=514 ,lowerCamelCase_: Dict=1 ,lowerCamelCase_: int=0.0_2 ,lowerCamelCase_: List[str]=1e-05 ,lowerCamelCase_: Dict=1 ,lowerCamelCase_: List[str]=0 ,lowerCamelCase_: str=2 ,lowerCamelCase_: Dict="absolute" ,lowerCamelCase_: Optional[Any]=True ,lowerCamelCase_: Dict=None ,**lowerCamelCase_: List[Any] ,) -> Union[str, Any]: super().__init__(pad_token_id=_UpperCamelCase ,bos_token_id=_UpperCamelCase ,eos_token_id=_UpperCamelCase ,**_UpperCamelCase ) UpperCAmelCase_ : Dict = vocab_size UpperCAmelCase_ : Tuple = hidden_size UpperCAmelCase_ : int = num_hidden_layers UpperCAmelCase_ : Any = num_attention_heads UpperCAmelCase_ : int = hidden_act UpperCAmelCase_ : List[Any] = intermediate_size UpperCAmelCase_ : Any = hidden_dropout_prob UpperCAmelCase_ : Any = attention_probs_dropout_prob UpperCAmelCase_ : Optional[int] = max_position_embeddings UpperCAmelCase_ : int = type_vocab_size UpperCAmelCase_ : Optional[int] = initializer_range UpperCAmelCase_ : Optional[int] = layer_norm_eps UpperCAmelCase_ : List[str] = position_embedding_type UpperCAmelCase_ : Tuple = use_cache UpperCAmelCase_ : Any = classifier_dropout class _snake_case ( __A ): '''simple docstring''' @property def A__ ( self: List[str] ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": UpperCAmelCase_ : List[str] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: UpperCAmelCase_ : int = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
345
from __future__ import annotations def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = len(SCREAMING_SNAKE_CASE__ ) # We need to create solution object to save path. snake_case_ = [[0 for _ in range(SCREAMING_SNAKE_CASE__ )] for _ in range(SCREAMING_SNAKE_CASE__ )] snake_case_ = run_maze(SCREAMING_SNAKE_CASE__ , 0 , 0 , SCREAMING_SNAKE_CASE__ ) if solved: print('''\n'''.join(str(SCREAMING_SNAKE_CASE__ ) for row in solutions ) ) else: print('''No solution exists!''' ) return solved def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = len(SCREAMING_SNAKE_CASE__ ) # Final check point. if i == j == (size - 1): snake_case_ = 1 return True snake_case_ = (not i < 0) and (not j < 0) # Check lower bounds snake_case_ = (i < size) and (j < size) # Check upper bounds if lower_flag and upper_flag: # check for already visited and block points. snake_case_ = (not solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited snake_case_ = 1 # check for directions if ( run_maze(SCREAMING_SNAKE_CASE__ , i + 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or run_maze(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , j + 1 , SCREAMING_SNAKE_CASE__ ) or run_maze(SCREAMING_SNAKE_CASE__ , i - 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or run_maze(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , j - 1 , SCREAMING_SNAKE_CASE__ ) ): return True snake_case_ = 0 return False return False if __name__ == "__main__": import doctest doctest.testmod()
8
0
"""simple docstring""" import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( "kwargs, expected" , [ ({"num_shards": 0, "max_num_jobs": 1}, []), ({"num_shards": 10, "max_num_jobs": 1}, [range(10 )]), ({"num_shards": 10, "max_num_jobs": 10}, [range(SCREAMING_SNAKE_CASE__ , i + 1 ) for i in range(10 )]), ({"num_shards": 1, "max_num_jobs": 10}, [range(1 )]), ({"num_shards": 10, "max_num_jobs": 3}, [range(0 , 4 ), range(4 , 7 ), range(7 , 10 )]), ({"num_shards": 3, "max_num_jobs": 10}, [range(0 , 1 ), range(1 , 2 ), range(2 , 3 )]), ] , ) def UpperCamelCase ( UpperCAmelCase , UpperCAmelCase ) ->Optional[Any]: """simple docstring""" a_ = _distribute_shards(**SCREAMING_SNAKE_CASE__ ) assert out == expected @pytest.mark.parametrize( "gen_kwargs, max_num_jobs, expected" , [ ({"foo": 0}, 10, [{"foo": 0}]), ({"shards": [0, 1, 2, 3]}, 1, [{"shards": [0, 1, 2, 3]}]), ({"shards": [0, 1, 2, 3]}, 4, [{"shards": [0]}, {"shards": [1]}, {"shards": [2]}, {"shards": [3]}]), ({"shards": [0, 1]}, 4, [{"shards": [0]}, {"shards": [1]}]), ({"shards": [0, 1, 2, 3]}, 2, [{"shards": [0, 1]}, {"shards": [2, 3]}]), ] , ) def UpperCamelCase ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) ->Optional[Any]: """simple docstring""" a_ = _split_gen_kwargs(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) assert out == expected @pytest.mark.parametrize( "gen_kwargs, expected" , [ ({"foo": 0}, 1), ({"shards": [0]}, 1), ({"shards": [0, 1, 2, 3]}, 4), ({"shards": [0, 1, 2, 3], "foo": 0}, 4), ({"shards": [0, 1, 2, 3], "other": (0, 1)}, 4), ({"shards": [0, 1, 2, 3], "shards2": [0, 1]}, RuntimeError), ] , ) def UpperCamelCase ( UpperCAmelCase , UpperCAmelCase ) ->int: """simple docstring""" if expected is RuntimeError: with pytest.raises(SCREAMING_SNAKE_CASE__ ): _number_of_shards_in_gen_kwargs(SCREAMING_SNAKE_CASE__ ) else: a_ = _number_of_shards_in_gen_kwargs(SCREAMING_SNAKE_CASE__ ) assert out == expected
243
from decimal import Decimal, getcontext from math import ceil, factorial def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): raise TypeError('''Undefined for non-integers''' ) elif precision < 1: raise ValueError('''Undefined for non-natural numbers''' ) snake_case_ = precision snake_case_ = ceil(precision / 14 ) snake_case_ = 426880 * Decimal(10005 ).sqrt() snake_case_ = 1 snake_case_ = 13591409 snake_case_ = Decimal(SCREAMING_SNAKE_CASE__ ) for k in range(1 , SCREAMING_SNAKE_CASE__ ): snake_case_ = factorial(6 * k ) // (factorial(3 * k ) * factorial(SCREAMING_SNAKE_CASE__ ) ** 3) linear_term += 545140134 exponential_term *= -262537412640768000 partial_sum += Decimal(multinomial_term * linear_term ) / exponential_term return str(constant_term / partial_sum )[:-1] if __name__ == "__main__": lowerCAmelCase_ = 50 print(f"""The first {n} digits of pi is: {pi(n)}""")
8
0
"""simple docstring""" import gc import unittest import numpy as np import torch import torch.nn.functional as F from transformers import ( ClapTextConfig, ClapTextModelWithProjection, RobertaTokenizer, SpeechTaHifiGan, SpeechTaHifiGanConfig, ) from diffusers import ( AudioLDMPipeline, AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.utils import is_xformers_available, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism from ..pipeline_params import TEXT_TO_AUDIO_BATCH_PARAMS, TEXT_TO_AUDIO_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class _UpperCAmelCase ( __A, unittest.TestCase ): '''simple docstring''' lowerCamelCase__ =AudioLDMPipeline lowerCamelCase__ =TEXT_TO_AUDIO_PARAMS lowerCamelCase__ =TEXT_TO_AUDIO_BATCH_PARAMS lowerCamelCase__ =frozenset( [ 'num_inference_steps', 'num_waveforms_per_prompt', 'generator', 'latents', 'output_type', 'return_dict', 'callback', 'callback_steps', ] ) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' torch.manual_seed(0 ) __snake_case : List[Any] = 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, 64) , class_embed_type='''simple_projection''' , projection_class_embeddings_input_dim=32 , class_embeddings_concat=_UpperCamelCase , ) __snake_case : Tuple = DDIMScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=_UpperCamelCase , set_alpha_to_one=_UpperCamelCase , ) torch.manual_seed(0 ) __snake_case : Optional[int] = AutoencoderKL( block_out_channels=[32, 64] , in_channels=1 , out_channels=1 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) __snake_case : Dict = ClapTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , projection_dim=32 , ) __snake_case : str = ClapTextModelWithProjection(_UpperCamelCase ) __snake_case : str = RobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-random-roberta''' , model_max_length=77 ) __snake_case : List[Any] = SpeechTaHifiGanConfig( model_in_dim=8 , sampling_rate=1_60_00 , upsample_initial_channel=16 , upsample_rates=[2, 2] , upsample_kernel_sizes=[4, 4] , resblock_kernel_sizes=[3, 7] , resblock_dilation_sizes=[[1, 3, 5], [1, 3, 5]] , normalize_before=_UpperCamelCase , ) __snake_case : Tuple = SpeechTaHifiGan(_UpperCamelCase ) __snake_case : List[str] = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''vocoder''': vocoder, } return components def SCREAMING_SNAKE_CASE (self , a_ , a_=0 ): '''simple docstring''' if str(_UpperCamelCase ).startswith('''mps''' ): __snake_case : int = torch.manual_seed(_UpperCamelCase ) else: __snake_case : Tuple = torch.Generator(device=_UpperCamelCase ).manual_seed(_UpperCamelCase ) __snake_case : Dict = { '''prompt''': '''A hammer hitting a wooden surface''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, } return inputs def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : str = '''cpu''' # ensure determinism for the device-dependent torch.Generator __snake_case : List[Any] = self.get_dummy_components() __snake_case : int = AudioLDMPipeline(**_UpperCamelCase ) __snake_case : Optional[Any] = audioldm_pipe.to(_UpperCamelCase ) audioldm_pipe.set_progress_bar_config(disable=_UpperCamelCase ) __snake_case : Optional[int] = self.get_dummy_inputs(_UpperCamelCase ) __snake_case : int = audioldm_pipe(**_UpperCamelCase ) __snake_case : Union[str, Any] = output.audios[0] assert audio.ndim == 1 assert len(_UpperCamelCase ) == 2_56 __snake_case : str = audio[:10] __snake_case : int = np.array( [-0.0050, 0.0050, -0.0060, 0.0033, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0033] ) assert np.abs(audio_slice - expected_slice ).max() < 1E-2 def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : Optional[Any] = self.get_dummy_components() __snake_case : Dict = AudioLDMPipeline(**_UpperCamelCase ) __snake_case : List[Any] = audioldm_pipe.to(_UpperCamelCase ) __snake_case : Tuple = audioldm_pipe.to(_UpperCamelCase ) audioldm_pipe.set_progress_bar_config(disable=_UpperCamelCase ) __snake_case : List[Any] = self.get_dummy_inputs(_UpperCamelCase ) __snake_case : Tuple = 3 * [inputs['''prompt''']] # forward __snake_case : str = audioldm_pipe(**_UpperCamelCase ) __snake_case : Optional[Any] = output.audios[0] __snake_case : List[str] = self.get_dummy_inputs(_UpperCamelCase ) __snake_case : List[str] = 3 * [inputs.pop('''prompt''' )] __snake_case : List[Any] = audioldm_pipe.tokenizer( _UpperCamelCase , padding='''max_length''' , max_length=audioldm_pipe.tokenizer.model_max_length , truncation=_UpperCamelCase , return_tensors='''pt''' , ) __snake_case : int = text_inputs['''input_ids'''].to(_UpperCamelCase ) __snake_case : Optional[Any] = audioldm_pipe.text_encoder( _UpperCamelCase , ) __snake_case : int = prompt_embeds.text_embeds # additional L_2 normalization over each hidden-state __snake_case : List[str] = F.normalize(_UpperCamelCase , dim=-1 ) __snake_case : Tuple = prompt_embeds # forward __snake_case : int = audioldm_pipe(**_UpperCamelCase ) __snake_case : int = output.audios[0] assert np.abs(audio_a - audio_a ).max() < 1E-2 def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : List[str] = self.get_dummy_components() __snake_case : Optional[Any] = AudioLDMPipeline(**_UpperCamelCase ) __snake_case : int = audioldm_pipe.to(_UpperCamelCase ) __snake_case : Optional[int] = audioldm_pipe.to(_UpperCamelCase ) audioldm_pipe.set_progress_bar_config(disable=_UpperCamelCase ) __snake_case : str = self.get_dummy_inputs(_UpperCamelCase ) __snake_case : Union[str, Any] = 3 * ['''this is a negative prompt'''] __snake_case : str = negative_prompt __snake_case : Optional[int] = 3 * [inputs['''prompt''']] # forward __snake_case : Optional[int] = audioldm_pipe(**_UpperCamelCase ) __snake_case : int = output.audios[0] __snake_case : List[str] = self.get_dummy_inputs(_UpperCamelCase ) __snake_case : Tuple = 3 * [inputs.pop('''prompt''' )] __snake_case : List[str] = [] for p in [prompt, negative_prompt]: __snake_case : Optional[int] = audioldm_pipe.tokenizer( _UpperCamelCase , padding='''max_length''' , max_length=audioldm_pipe.tokenizer.model_max_length , truncation=_UpperCamelCase , return_tensors='''pt''' , ) __snake_case : List[str] = text_inputs['''input_ids'''].to(_UpperCamelCase ) __snake_case : Optional[int] = audioldm_pipe.text_encoder( _UpperCamelCase , ) __snake_case : str = text_embeds.text_embeds # additional L_2 normalization over each hidden-state __snake_case : Any = F.normalize(_UpperCamelCase , dim=-1 ) embeds.append(_UpperCamelCase ) __snake_case , __snake_case : List[Any] = embeds # forward __snake_case : Dict = audioldm_pipe(**_UpperCamelCase ) __snake_case : Dict = output.audios[0] assert np.abs(audio_a - audio_a ).max() < 1E-2 def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : int = '''cpu''' # ensure determinism for the device-dependent torch.Generator __snake_case : Union[str, Any] = self.get_dummy_components() __snake_case : str = PNDMScheduler(skip_prk_steps=_UpperCamelCase ) __snake_case : Union[str, Any] = AudioLDMPipeline(**_UpperCamelCase ) __snake_case : Any = audioldm_pipe.to(_UpperCamelCase ) audioldm_pipe.set_progress_bar_config(disable=_UpperCamelCase ) __snake_case : Optional[int] = self.get_dummy_inputs(_UpperCamelCase ) __snake_case : List[Any] = '''egg cracking''' __snake_case : List[Any] = audioldm_pipe(**_UpperCamelCase , negative_prompt=_UpperCamelCase ) __snake_case : Any = output.audios[0] assert audio.ndim == 1 assert len(_UpperCamelCase ) == 2_56 __snake_case : List[str] = audio[:10] __snake_case : List[str] = np.array( [-0.0051, 0.0050, -0.0060, 0.0034, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0032] ) assert np.abs(audio_slice - expected_slice ).max() < 1E-2 def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : Optional[int] = '''cpu''' # ensure determinism for the device-dependent torch.Generator __snake_case : Optional[Any] = self.get_dummy_components() __snake_case : int = PNDMScheduler(skip_prk_steps=_UpperCamelCase ) __snake_case : List[Any] = AudioLDMPipeline(**_UpperCamelCase ) __snake_case : List[Any] = audioldm_pipe.to(_UpperCamelCase ) audioldm_pipe.set_progress_bar_config(disable=_UpperCamelCase ) __snake_case : Dict = '''A hammer hitting a wooden surface''' # test num_waveforms_per_prompt=1 (default) __snake_case : List[Any] = audioldm_pipe(_UpperCamelCase , num_inference_steps=2 ).audios assert audios.shape == (1, 2_56) # test num_waveforms_per_prompt=1 (default) for batch of prompts __snake_case : Optional[int] = 2 __snake_case : Union[str, Any] = audioldm_pipe([prompt] * batch_size , num_inference_steps=2 ).audios assert audios.shape == (batch_size, 2_56) # test num_waveforms_per_prompt for single prompt __snake_case : Tuple = 2 __snake_case : Optional[Any] = audioldm_pipe(_UpperCamelCase , num_inference_steps=2 , num_waveforms_per_prompt=_UpperCamelCase ).audios assert audios.shape == (num_waveforms_per_prompt, 2_56) # test num_waveforms_per_prompt for batch of prompts __snake_case : Any = 2 __snake_case : Optional[int] = audioldm_pipe( [prompt] * batch_size , num_inference_steps=2 , num_waveforms_per_prompt=_UpperCamelCase ).audios assert audios.shape == (batch_size * num_waveforms_per_prompt, 2_56) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : str = '''cpu''' # ensure determinism for the device-dependent torch.Generator __snake_case : Optional[Any] = self.get_dummy_components() __snake_case : Optional[Any] = AudioLDMPipeline(**_UpperCamelCase ) __snake_case : Dict = audioldm_pipe.to(_UpperCamelCase ) audioldm_pipe.set_progress_bar_config(disable=_UpperCamelCase ) __snake_case : Any = audioldm_pipe.vocoder.config.sampling_rate __snake_case : Optional[Any] = self.get_dummy_inputs(_UpperCamelCase ) __snake_case : str = audioldm_pipe(audio_length_in_s=0.016 , **_UpperCamelCase ) __snake_case : Optional[Any] = output.audios[0] assert audio.ndim == 1 assert len(_UpperCamelCase ) / vocoder_sampling_rate == 0.016 __snake_case : Optional[Any] = audioldm_pipe(audio_length_in_s=0.032 , **_UpperCamelCase ) __snake_case : Any = output.audios[0] assert audio.ndim == 1 assert len(_UpperCamelCase ) / vocoder_sampling_rate == 0.032 def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : List[str] = self.get_dummy_components() __snake_case : str = AudioLDMPipeline(**_UpperCamelCase ) __snake_case : Any = audioldm_pipe.to(_UpperCamelCase ) audioldm_pipe.set_progress_bar_config(disable=_UpperCamelCase ) __snake_case : Optional[int] = ['''hey'''] __snake_case : Optional[Any] = audioldm_pipe(_UpperCamelCase , num_inference_steps=1 ) __snake_case : Optional[Any] = output.audios.shape assert audio_shape == (1, 2_56) __snake_case : Optional[int] = audioldm_pipe.vocoder.config config.model_in_dim *= 2 __snake_case : Optional[Any] = SpeechTaHifiGan(_UpperCamelCase ).to(_UpperCamelCase ) __snake_case : str = audioldm_pipe(_UpperCamelCase , num_inference_steps=1 ) __snake_case : List[str] = output.audios.shape # waveform shape is unchanged, we just have 2x the number of mel channels in the spectrogram assert audio_shape == (1, 2_56) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' self._test_attention_slicing_forward_pass(test_mean_pixel_difference=_UpperCamelCase ) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' self._test_inference_batch_single_identical(test_mean_pixel_difference=_UpperCamelCase ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_UpperCamelCase ) @slow class _UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE (self , a_ , a_="cpu" , a_=torch.floataa , a_=0 ): '''simple docstring''' __snake_case : List[str] = torch.Generator(device=_UpperCamelCase ).manual_seed(_UpperCamelCase ) __snake_case : Optional[int] = np.random.RandomState(_UpperCamelCase ).standard_normal((1, 8, 1_28, 16) ) __snake_case : int = torch.from_numpy(_UpperCamelCase ).to(device=_UpperCamelCase , dtype=_UpperCamelCase ) __snake_case : Tuple = { '''prompt''': '''A hammer hitting a wooden surface''', '''latents''': latents, '''generator''': generator, '''num_inference_steps''': 3, '''guidance_scale''': 2.5, } return inputs def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : int = AudioLDMPipeline.from_pretrained('''cvssp/audioldm''' ) __snake_case : Optional[Any] = audioldm_pipe.to(_UpperCamelCase ) audioldm_pipe.set_progress_bar_config(disable=_UpperCamelCase ) __snake_case : Dict = self.get_inputs(_UpperCamelCase ) __snake_case : Dict = 25 __snake_case : str = audioldm_pipe(**_UpperCamelCase ).audios[0] assert audio.ndim == 1 assert len(_UpperCamelCase ) == 8_19_20 __snake_case : List[str] = audio[7_72_30:7_72_40] __snake_case : Any = np.array( [-0.4884, -0.4607, 0.0023, 0.5007, 0.5896, 0.5151, 0.3813, -0.0208, -0.3687, -0.4315] ) __snake_case : Optional[int] = np.abs(expected_slice - audio_slice ).max() assert max_diff < 1E-2 def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : Optional[Any] = AudioLDMPipeline.from_pretrained('''cvssp/audioldm''' ) __snake_case : List[str] = LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config ) __snake_case : Dict = audioldm_pipe.to(_UpperCamelCase ) audioldm_pipe.set_progress_bar_config(disable=_UpperCamelCase ) __snake_case : int = self.get_inputs(_UpperCamelCase ) __snake_case : Tuple = audioldm_pipe(**_UpperCamelCase ).audios[0] assert audio.ndim == 1 assert len(_UpperCamelCase ) == 8_19_20 __snake_case : Tuple = audio[2_77_80:2_77_90] __snake_case : Union[str, Any] = np.array([-0.2131, -0.0873, -0.0124, -0.0189, 0.0569, 0.1373, 0.1883, 0.2886, 0.3297, 0.2212] ) __snake_case : List[str] = np.abs(expected_slice - audio_slice ).max() assert max_diff < 3E-2
102
from typing import Optional import pyspark from .. import Features, NamedSplit from ..download import DownloadMode from ..packaged_modules.spark.spark import Spark from .abc import AbstractDatasetReader class snake_case_ ( __A ): '''simple docstring''' def __init__( self : int , _UpperCamelCase : pyspark.sql.DataFrame , _UpperCamelCase : Optional[NamedSplit] = None , _UpperCamelCase : Optional[Features] = None , _UpperCamelCase : bool = True , _UpperCamelCase : str = None , _UpperCamelCase : bool = False , _UpperCamelCase : str = None , _UpperCamelCase : bool = True , _UpperCamelCase : str = "arrow" , **_UpperCamelCase : Tuple , ) ->str: super().__init__( split=_UpperCamelCase , features=_UpperCamelCase , cache_dir=_UpperCamelCase , keep_in_memory=_UpperCamelCase , streaming=_UpperCamelCase , **_UpperCamelCase , ) snake_case_ = load_from_cache_file snake_case_ = file_format snake_case_ = Spark( df=_UpperCamelCase , features=_UpperCamelCase , cache_dir=_UpperCamelCase , working_dir=_UpperCamelCase , **_UpperCamelCase , ) def snake_case__( self : int ) ->Tuple: if self.streaming: return self.builder.as_streaming_dataset(split=self.split ) snake_case_ = None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD self.builder.download_and_prepare( download_mode=_UpperCamelCase , file_format=self._file_format , ) return self.builder.as_dataset(split=self.split )
8
0
'''simple docstring''' import fire from utils import calculate_rouge, save_json def lowerCAmelCase_ ( snake_case_ : Union[str, Any] , snake_case_ : int , snake_case_ : List[Any]=None , **snake_case_ : List[Any] ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase_ = [x.strip() for x in open(SCREAMING_SNAKE_CASE__ ).readlines()] UpperCAmelCase_ = [x.strip() for x in open(SCREAMING_SNAKE_CASE__ ).readlines()][: len(SCREAMING_SNAKE_CASE__ )] UpperCAmelCase_ = calculate_rouge(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) if save_path is not None: save_json(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , indent=SCREAMING_SNAKE_CASE__ ) return metrics # these print nicely if __name__ == "__main__": fire.Fire(calculate_rouge_path)
1
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available from ...utils import OptionalDependencyNotAvailable lowerCAmelCase_ = {'''configuration_dpt''': ['''DPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DPTConfig''']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['''DPTFeatureExtractor'''] lowerCAmelCase_ = ['''DPTImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''DPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''DPTForDepthEstimation''', '''DPTForSemanticSegmentation''', '''DPTModel''', '''DPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_dpt import DPT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPTConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_dpt import DPTFeatureExtractor from .image_processing_dpt import DPTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dpt import ( DPT_PRETRAINED_MODEL_ARCHIVE_LIST, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel, DPTPreTrainedModel, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
8
0
'''simple docstring''' from decimal import Decimal, getcontext from math import ceil, factorial def snake_case_ ( lowerCAmelCase_ )-> List[str]: '''simple docstring''' if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): raise TypeError("""Undefined for non-integers""" ) elif precision < 1: raise ValueError("""Undefined for non-natural numbers""" ) _UpperCAmelCase : Optional[int] = precision _UpperCAmelCase : Union[str, Any] = ceil(precision / 14 ) _UpperCAmelCase : Dict = 426880 * Decimal(10005 ).sqrt() _UpperCAmelCase : Tuple = 1 _UpperCAmelCase : Union[str, Any] = 13591409 _UpperCAmelCase : int = Decimal(SCREAMING_SNAKE_CASE__ ) for k in range(1 , SCREAMING_SNAKE_CASE__ ): _UpperCAmelCase : List[Any] = factorial(6 * k ) // (factorial(3 * k ) * factorial(SCREAMING_SNAKE_CASE__ ) ** 3) linear_term += 545140134 exponential_term *= -262537412640768000 partial_sum += Decimal(multinomial_term * linear_term ) / exponential_term return str(constant_term / partial_sum )[:-1] if __name__ == "__main__": A_ : Any = 5_0 print(f"""The first {n} digits of pi is: {pi(n)}""")
215
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from .tokenization_lxmert import LxmertTokenizer lowerCAmelCase_ = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} lowerCAmelCase_ = { '''vocab_file''': { '''unc-nlp/lxmert-base-uncased''': '''https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/vocab.txt''', }, '''tokenizer_file''': { '''unc-nlp/lxmert-base-uncased''': ( '''https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase_ = { '''unc-nlp/lxmert-base-uncased''': 5_12, } lowerCAmelCase_ = { '''unc-nlp/lxmert-base-uncased''': {'''do_lower_case''': True}, } class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE : Any = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE : List[Any] = PRETRAINED_INIT_CONFIGURATION SCREAMING_SNAKE_CASE : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE : Any = LxmertTokenizer def __init__( self : Union[str, Any] , _UpperCamelCase : int=None , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Dict=True , _UpperCamelCase : Any="[UNK]" , _UpperCamelCase : Tuple="[SEP]" , _UpperCamelCase : List[Any]="[PAD]" , _UpperCamelCase : Union[str, Any]="[CLS]" , _UpperCamelCase : str="[MASK]" , _UpperCamelCase : List[str]=True , _UpperCamelCase : List[str]=None , **_UpperCamelCase : List[str] , ) ->Any: super().__init__( _UpperCamelCase , tokenizer_file=_UpperCamelCase , do_lower_case=_UpperCamelCase , unk_token=_UpperCamelCase , sep_token=_UpperCamelCase , pad_token=_UpperCamelCase , cls_token=_UpperCamelCase , mask_token=_UpperCamelCase , tokenize_chinese_chars=_UpperCamelCase , strip_accents=_UpperCamelCase , **_UpperCamelCase , ) snake_case_ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , _UpperCamelCase ) != do_lower_case or normalizer_state.get('''strip_accents''' , _UpperCamelCase ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , _UpperCamelCase ) != tokenize_chinese_chars ): snake_case_ = getattr(_UpperCamelCase , normalizer_state.pop('''type''' ) ) snake_case_ = do_lower_case snake_case_ = strip_accents snake_case_ = tokenize_chinese_chars snake_case_ = normalizer_class(**_UpperCamelCase ) snake_case_ = do_lower_case def snake_case__( self : Optional[int] , _UpperCamelCase : List[Any] , _UpperCamelCase : List[str]=None ) ->List[Any]: snake_case_ = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def snake_case__( self : int , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) ->List[int]: snake_case_ = [self.sep_token_id] snake_case_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def snake_case__( self : Any , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) ->Tuple[str]: snake_case_ = self._tokenizer.model.save(_UpperCamelCase , name=_UpperCamelCase ) return tuple(_UpperCamelCase )
8
0
import pytest __UpperCamelCase : Any = '__dummy_dataset1__' __UpperCamelCase : str = '\nimport json\nimport os\n\nimport datasets\n\n\nREPO_URL = "https://huggingface.co/datasets/albertvillanova/tests-raw-jsonl/resolve/main/"\nURLS = {"train": REPO_URL + "wikiann-bn-train.jsonl", "validation": REPO_URL + "wikiann-bn-validation.jsonl"}\n\n\nclass __DummyDataset1__(datasets.GeneratorBasedBuilder):\n\n def _info(self):\n features = datasets.Features(\n {\n "tokens": datasets.Sequence(datasets.Value("string")),\n "ner_tags": datasets.Sequence(\n datasets.features.ClassLabel(\n names=[\n "O",\n "B-PER",\n "I-PER",\n "B-ORG",\n "I-ORG",\n "B-LOC",\n "I-LOC",\n ]\n )\n ),\n "langs": datasets.Sequence(datasets.Value("string")),\n "spans": datasets.Sequence(datasets.Value("string")),\n }\n )\n return datasets.DatasetInfo(features=features)\n\n def _split_generators(self, dl_manager):\n dl_path = dl_manager.download(URLS)\n return [\n datasets.SplitGenerator(datasets.Split.TRAIN, gen_kwargs={"filepath": dl_path["train"]}),\n datasets.SplitGenerator(datasets.Split.VALIDATION, gen_kwargs={"filepath": dl_path["validation"]}),\n ]\n\n def _generate_examples(self, filepath):\n with open(filepath, "r", encoding="utf-8") as f:\n for i, line in enumerate(f):\n yield i, json.loads(line)\n' @pytest.fixture def A ( ): return DATASET_LOADING_SCRIPT_NAME @pytest.fixture def A ( ): return DATASET_LOADING_SCRIPT_CODE @pytest.fixture def A ( _lowercase , _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : List[str] = dataset_loading_script_name SCREAMING_SNAKE_CASE : Any = tmp_path / '''datasets''' / script_name script_dir.mkdir(parents=SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE : List[str] = script_dir / f"""{script_name}.py""" with open(SCREAMING_SNAKE_CASE__ , '''w''' ) as f: f.write(SCREAMING_SNAKE_CASE__ ) return str(SCREAMING_SNAKE_CASE__ )
182
import math def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(SCREAMING_SNAKE_CASE__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = 10001 ): try: snake_case_ = int(SCREAMING_SNAKE_CASE__ ) except (TypeError, ValueError): raise TypeError('''Parameter nth must be int or castable to int.''' ) from None if nth <= 0: raise ValueError('''Parameter nth must be greater than or equal to one.''' ) snake_case_ = [] snake_case_ = 2 while len(SCREAMING_SNAKE_CASE__ ) < nth: if is_prime(SCREAMING_SNAKE_CASE__ ): primes.append(SCREAMING_SNAKE_CASE__ ) num += 1 else: num += 1 return primes[len(SCREAMING_SNAKE_CASE__ ) - 1] if __name__ == "__main__": print(f"""{solution() = }""")
8
0
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast from ...utils import logging _lowerCamelCase : Optional[int] = logging.get_logger(__name__) _lowerCamelCase : List[str] = { "EleutherAI/gpt-neo-1.3B": "https://huggingface.co/EleutherAI/gpt-neo-1.3B/resolve/main/config.json", # See all GPTNeo models at https://huggingface.co/models?filter=gpt_neo } class __UpperCAmelCase ( __A ): UpperCamelCase = "gpt_neo" UpperCamelCase = ["past_key_values"] UpperCamelCase = {"num_attention_heads": "num_heads", "num_hidden_layers": "num_layers"} def __init__( self : Optional[int], __A : Dict=5_0_2_5_7, __A : Any=2_0_4_8, __A : Optional[Any]=2_0_4_8, __A : Optional[Any]=2_4, __A : Optional[Any]=[[["global", "local"], 1_2]], __A : Optional[Any]=1_6, __A : List[Any]=None, __A : Union[str, Any]=2_5_6, __A : Optional[int]="gelu_new", __A : Optional[int]=0.0, __A : Tuple=0.0, __A : List[str]=0.0, __A : Any=0.1, __A : List[Any]=1E-5, __A : Dict=0.0_2, __A : Union[str, Any]=True, __A : Dict=5_0_2_5_6, __A : int=5_0_2_5_6, **__A : Optional[int], ): UpperCAmelCase : Dict = vocab_size UpperCAmelCase : Any = max_position_embeddings UpperCAmelCase : Optional[Any] = hidden_size UpperCAmelCase : Tuple = num_layers UpperCAmelCase : Dict = num_heads UpperCAmelCase : Tuple = intermediate_size UpperCAmelCase : Union[str, Any] = window_size UpperCAmelCase : List[str] = activation_function UpperCAmelCase : Tuple = resid_dropout UpperCAmelCase : List[str] = embed_dropout UpperCAmelCase : Union[str, Any] = attention_dropout UpperCAmelCase : Union[str, Any] = classifier_dropout UpperCAmelCase : Tuple = layer_norm_epsilon UpperCAmelCase : Dict = initializer_range UpperCAmelCase : Union[str, Any] = use_cache UpperCAmelCase : Optional[int] = bos_token_id UpperCAmelCase : Optional[Any] = eos_token_id UpperCAmelCase : Tuple = attention_types UpperCAmelCase : Union[str, Any] = self.expand_attention_types_params(_UpperCamelCase ) if len(self.attention_layers ) != self.num_layers: raise ValueError( '''Configuration for convolutional module is incorrect. ''' '''It is required that `len(config.attention_layers)` == `config.num_layers` ''' F'''but is `len(config.attention_layers) = {len(self.attention_layers )}`, ''' F'''`config.num_layers = {self.num_layers}`. ''' '''`config.attention_layers` is prepared using `config.attention_types`. ''' '''Please verify the value of `config.attention_types` argument.''' ) super().__init__(bos_token_id=_UpperCamelCase, eos_token_id=_UpperCamelCase, **_UpperCamelCase ) @staticmethod def __magic_name__ ( __A : Optional[int] ): UpperCAmelCase : List[str] = [] for item in attention_types: for _ in range(item[1] ): attentions.extend(item[0] ) return attentions def a__ ( UpperCAmelCase : Optional[int] , UpperCAmelCase : str , UpperCAmelCase : Optional[Any] , UpperCAmelCase : List[str] ) -> Dict: import torch UpperCAmelCase : Any = input.size() UpperCAmelCase : Optional[int] = len(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase : Optional[int] = shape[dimension] UpperCAmelCase : str = torch.arange(0 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) UpperCAmelCase : Any = torch.div(sizedim - size , SCREAMING_SNAKE_CASE__ , rounding_mode='''floor''' ) + 1 UpperCAmelCase : List[Any] = torch.arange(SCREAMING_SNAKE_CASE__ ) + low_indices[:min_length][:, None] UpperCAmelCase : str = [slice(SCREAMING_SNAKE_CASE__ )] * rank UpperCAmelCase : List[Any] = indices UpperCAmelCase : List[str] = input[s] UpperCAmelCase : Union[str, Any] = list(range(0 , rank + 1 ) ) perm.append(perm.pop(dimension + 1 ) ) return sliced.permute(SCREAMING_SNAKE_CASE__ ) def a__ ( UpperCAmelCase : Dict , UpperCAmelCase : Optional[Any] ) -> Dict: import torch UpperCAmelCase : List[Any] = torch.arange(1 , SCREAMING_SNAKE_CASE__ ) UpperCAmelCase : Union[str, Any] = torch.remainder(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) UpperCAmelCase : List[Any] = remainders == 0 UpperCAmelCase : Optional[Any] = candidates[divisor_indices] UpperCAmelCase : int = torch.max(SCREAMING_SNAKE_CASE__ ) return largest_divisor, torch.div(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , rounding_mode='''floor''' ) class __UpperCAmelCase ( __A ): @property def __magic_name__ ( self : List[Any] ): UpperCAmelCase : str = OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}} ) if self.use_past: self.fill_with_past_key_values_(_UpperCamelCase, direction='''inputs''' ) UpperCAmelCase : str = {0: '''batch''', 1: '''past_sequence + sequence'''} else: UpperCAmelCase : Any = {0: '''batch''', 1: '''sequence'''} return common_inputs @property def __magic_name__ ( self : str ): return self._config.num_heads def __magic_name__ ( self : Optional[int], __A : PreTrainedTokenizer, __A : int = -1, __A : int = -1, __A : bool = False, __A : Optional[TensorType] = None, ): UpperCAmelCase : Tuple = super(_UpperCamelCase, self ).generate_dummy_inputs( _UpperCamelCase, batch_size=_UpperCamelCase, seq_length=_UpperCamelCase, is_pair=_UpperCamelCase, framework=_UpperCamelCase ) # We need to order the input in the way they appears in the forward() UpperCAmelCase : str = OrderedDict({'''input_ids''': common_inputs['''input_ids''']} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch UpperCAmelCase , UpperCAmelCase : Optional[Any] = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values UpperCAmelCase : Any = seqlen + 2 UpperCAmelCase : Dict = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) UpperCAmelCase : Any = [ (torch.zeros(_UpperCamelCase ), torch.zeros(_UpperCamelCase )) for _ in range(self.num_layers ) ] UpperCAmelCase : List[str] = common_inputs['''attention_mask'''] if self.use_past: UpperCAmelCase : List[str] = ordered_inputs['''attention_mask'''].dtype UpperCAmelCase : str = torch.cat( [ordered_inputs['''attention_mask'''], torch.ones(_UpperCamelCase, _UpperCamelCase, dtype=_UpperCamelCase )], dim=1 ) return ordered_inputs @property def __magic_name__ ( self : int ): return 1_3
336
from sklearn.metrics import mean_squared_error import datasets lowerCAmelCase_ = '''\ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' lowerCAmelCase_ = '''\ Mean Squared Error(MSE) is the average of the square of difference between the predicted and actual values. ''' lowerCAmelCase_ = ''' Args: predictions: array-like of shape (n_samples,) or (n_samples, n_outputs) Estimated target values. references: array-like of shape (n_samples,) or (n_samples, n_outputs) Ground truth (correct) target values. sample_weight: array-like of shape (n_samples,), default=None Sample weights. multioutput: {"raw_values", "uniform_average"} or array-like of shape (n_outputs,), default="uniform_average" Defines aggregating of multiple output values. Array-like value defines weights used to average errors. "raw_values" : Returns a full set of errors in case of multioutput input. "uniform_average" : Errors of all outputs are averaged with uniform weight. squared : bool, default=True If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value. Returns: mse : mean squared error. Examples: >>> mse_metric = datasets.load_metric("mse") >>> predictions = [2.5, 0.0, 2, 8] >>> references = [3, -0.5, 2, 7] >>> results = mse_metric.compute(predictions=predictions, references=references) >>> print(results) {\'mse\': 0.375} >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False) >>> print(rmse_result) {\'mse\': 0.6123724356957945} If you\'re using multi-dimensional lists, then set the config as follows : >>> mse_metric = datasets.load_metric("mse", "multilist") >>> predictions = [[0.5, 1], [-1, 1], [7, -6]] >>> references = [[0, 2], [-1, 2], [8, -5]] >>> results = mse_metric.compute(predictions=predictions, references=references) >>> print(results) {\'mse\': 0.7083333333333334} >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput=\'raw_values\') >>> print(results) # doctest: +NORMALIZE_WHITESPACE {\'mse\': array([0.41666667, 1. ])} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class snake_case_ ( datasets.Metric ): '''simple docstring''' def snake_case__( self : Optional[int] ) ->List[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , reference_urls=[ '''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html''' ] , ) def snake_case__( self : List[Any] ) ->Optional[int]: if self.config_name == "multilist": return { "predictions": datasets.Sequence(datasets.Value('''float''' ) ), "references": datasets.Sequence(datasets.Value('''float''' ) ), } else: return { "predictions": datasets.Value('''float''' ), "references": datasets.Value('''float''' ), } def snake_case__( self : int , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Any=None , _UpperCamelCase : Optional[int]="uniform_average" , _UpperCamelCase : Tuple=True ) ->Tuple: snake_case_ = mean_squared_error( _UpperCamelCase , _UpperCamelCase , sample_weight=_UpperCamelCase , multioutput=_UpperCamelCase , squared=_UpperCamelCase ) return {"mse": mse}
8
0
import re def __A ( __lowerCAmelCase )-> Optional[int]: """simple docstring""" _UpperCAmelCase = re.compile( R'^(?:0|94|\+94|0{2}94)' R'7(0|1|2|4|5|6|7|8)' R'(-| |)' R'\d{7}$' ) return bool(re.search(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) if __name__ == "__main__": _a = '''0094702343221''' print(is_sri_lankan_phone_number(phone))
39
def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = [] if len(SCREAMING_SNAKE_CASE__ ) == 1: return [nums.copy()] for _ in range(len(SCREAMING_SNAKE_CASE__ ) ): snake_case_ = nums.pop(0 ) snake_case_ = permute(SCREAMING_SNAKE_CASE__ ) for perm in permutations: perm.append(SCREAMING_SNAKE_CASE__ ) result.extend(SCREAMING_SNAKE_CASE__ ) nums.append(SCREAMING_SNAKE_CASE__ ) return result def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): def backtrack(SCREAMING_SNAKE_CASE__ ): if start == len(SCREAMING_SNAKE_CASE__ ) - 1: output.append(nums[:] ) else: for i in range(SCREAMING_SNAKE_CASE__ , len(SCREAMING_SNAKE_CASE__ ) ): snake_case_, snake_case_ = nums[i], nums[start] backtrack(start + 1 ) snake_case_, snake_case_ = nums[i], nums[start] # backtrack snake_case_ = [] backtrack(0 ) return output if __name__ == "__main__": import doctest # use res to print the data in permute2 function lowerCAmelCase_ = permutea([1, 2, 3]) print(res) doctest.testmod()
8
0
import argparse import logging import pickle import random import time import numpy as np from transformers import BertTokenizer, GPTaTokenizer, RobertaTokenizer logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO ) snake_case_ = logging.getLogger(__name__) def lowerCamelCase__ ( ) -> List[str]: __snake_case = argparse.ArgumentParser( description='''Preprocess the data to avoid re-doing it several times by (tokenization + token_to_ids).''' ) parser.add_argument('''--file_path''' , type=SCREAMING_SNAKE_CASE__ , default='''data/dump.txt''' , help='''The path to the data.''' ) parser.add_argument('''--tokenizer_type''' , type=SCREAMING_SNAKE_CASE__ , default='''bert''' , choices=['''bert''', '''roberta''', '''gpt2'''] ) parser.add_argument('''--tokenizer_name''' , type=SCREAMING_SNAKE_CASE__ , default='''bert-base-uncased''' , help='''The tokenizer to use.''' ) parser.add_argument('''--dump_file''' , type=SCREAMING_SNAKE_CASE__ , default='''data/dump''' , help='''The dump file prefix.''' ) __snake_case = parser.parse_args() logger.info(f"""Loading Tokenizer ({args.tokenizer_name})""" ) if args.tokenizer_type == "bert": __snake_case = BertTokenizer.from_pretrained(args.tokenizer_name ) __snake_case = tokenizer.special_tokens_map['''cls_token'''] # `[CLS]` __snake_case = tokenizer.special_tokens_map['''sep_token'''] # `[SEP]` elif args.tokenizer_type == "roberta": __snake_case = RobertaTokenizer.from_pretrained(args.tokenizer_name ) __snake_case = tokenizer.special_tokens_map['''cls_token'''] # `<s>` __snake_case = tokenizer.special_tokens_map['''sep_token'''] # `</s>` elif args.tokenizer_type == "gpt2": __snake_case = GPTaTokenizer.from_pretrained(args.tokenizer_name ) __snake_case = tokenizer.special_tokens_map['''bos_token'''] # `<|endoftext|>` __snake_case = tokenizer.special_tokens_map['''eos_token'''] # `<|endoftext|>` logger.info(f"""Loading text from {args.file_path}""" ) with open(args.file_path , '''r''' , encoding='''utf8''' ) as fp: __snake_case = fp.readlines() logger.info('''Start encoding''' ) logger.info(f"""{len(SCREAMING_SNAKE_CASE__ )} examples to process.""" ) __snake_case = [] __snake_case = 0 __snake_case = 1_0000 __snake_case = time.time() for text in data: __snake_case = f"""{bos} {text.strip()} {sep}""" __snake_case = tokenizer.encode(SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ ) rslt.append(SCREAMING_SNAKE_CASE__ ) iter += 1 if iter % interval == 0: __snake_case = time.time() logger.info(f"""{iter} examples processed. - {(end-start):.2f}s/{interval}expl""" ) __snake_case = time.time() logger.info('''Finished binarization''' ) logger.info(f"""{len(SCREAMING_SNAKE_CASE__ )} examples processed.""" ) __snake_case = f"""{args.dump_file}.{args.tokenizer_name}.pickle""" __snake_case = tokenizer.vocab_size if vocab_size < (1 << 16): __snake_case = [np.uintaa(SCREAMING_SNAKE_CASE__ ) for d in rslt] else: __snake_case = [np.intaa(SCREAMING_SNAKE_CASE__ ) for d in rslt] random.shuffle(rslt_ ) logger.info(f"""Dump to {dp_file}""" ) with open(SCREAMING_SNAKE_CASE__ , '''wb''' ) as handle: pickle.dump(rslt_ , SCREAMING_SNAKE_CASE__ , protocol=pickle.HIGHEST_PROTOCOL ) if __name__ == "__main__": main()
24
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase_ = {'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['''XGLMTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['''XGLMTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XGLMForCausalLM''', '''XGLMModel''', '''XGLMPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''FlaxXGLMForCausalLM''', '''FlaxXGLMModel''', '''FlaxXGLMPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXGLMForCausalLM''', '''TFXGLMModel''', '''TFXGLMPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
8
0
import argparse import os from pathlib import Path import fairseq import torch from packaging import version from torch import nn from transformers import ( BartConfig, BartForConditionalGeneration, BartForSequenceClassification, BartModel, BartTokenizer, ) from transformers.utils import logging a =["""bart.large""", """bart.large.mnli""", """bart.large.cnn""", """bart_xsum/model.pt"""] a ={"""bart.large""": BartModel, """bart.large.mnli""": BartForSequenceClassification} if version.parse(fairseq.__version__) < version.parse("""0.9.0"""): raise Exception("""requires fairseq >= 0.9.0""") logging.set_verbosity_info() a =logging.get_logger(__name__) a =""" Hello world! cécé herlolip""" a =[ ("""model.classification_heads.mnli.dense.weight""", """classification_head.dense.weight"""), ("""model.classification_heads.mnli.dense.bias""", """classification_head.dense.bias"""), ("""model.classification_heads.mnli.out_proj.weight""", """classification_head.out_proj.weight"""), ("""model.classification_heads.mnli.out_proj.bias""", """classification_head.out_proj.bias"""), ] def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> Optional[int]: __lowerCamelCase : Union[str, Any] = [ 'encoder.version', 'decoder.version', 'model.encoder.version', 'model.decoder.version', '_float_tensor', ] for k in ignore_keys: state_dict.pop(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Optional[int]: __lowerCamelCase : Tuple = dct.pop(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : Tuple = val def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> Union[str, Any]: __lowerCamelCase : Optional[int] = torch.load(SCREAMING_SNAKE_CASE__ , map_location='cpu' ) __lowerCamelCase : Tuple = torch.hub.load('pytorch/fairseq' , 'bart.large.cnn' ).eval() hub_interface.model.load_state_dict(sd['model'] ) return hub_interface def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> int: __lowerCamelCase , __lowerCamelCase : List[Any] = emb.weight.shape __lowerCamelCase : Any = nn.Linear(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , bias=SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : List[Any] = emb.weight.data return lin_layer @torch.no_grad() def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__=None ) -> Union[str, Any]: if not os.path.exists(SCREAMING_SNAKE_CASE__ ): __lowerCamelCase : str = torch.hub.load('pytorch/fairseq' , SCREAMING_SNAKE_CASE__ ).eval() else: __lowerCamelCase : Optional[Any] = load_xsum_checkpoint(SCREAMING_SNAKE_CASE__ ) bart.model.upgrade_state_dict(bart.model.state_dict() ) if hf_checkpoint_name is None: __lowerCamelCase : Any = checkpoint_path.replace('.' , '-' ) __lowerCamelCase : str = BartConfig.from_pretrained(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : List[str] = bart.encode(SCREAMING_SNAKE_CASE__ ).unsqueeze(0 ) __lowerCamelCase : List[Any] = BartTokenizer.from_pretrained(SCREAMING_SNAKE_CASE__ ).encode(SCREAMING_SNAKE_CASE__ , return_tensors='pt' ).unsqueeze(0 ) if not torch.eq(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ).all(): raise ValueError( F"converted tokenizer and pretrained tokenizer returned different output: {tokens} != {tokensa}" ) if checkpoint_path == "bart.large.mnli": __lowerCamelCase : Dict = bart.state_dict() remove_ignore_keys_(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : Any = state_dict['model.decoder.embed_tokens.weight'] for src, dest in mnli_rename_keys: rename_key(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : Any = BartForSequenceClassification(SCREAMING_SNAKE_CASE__ ).eval() model.load_state_dict(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : Tuple = bart.predict('mnli' , SCREAMING_SNAKE_CASE__ , return_logits=SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : str = model(SCREAMING_SNAKE_CASE__ )[0] # logits else: # no classification heads to worry about __lowerCamelCase : Optional[Any] = bart.model.state_dict() remove_ignore_keys_(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : List[Any] = state_dict['decoder.embed_tokens.weight'] __lowerCamelCase : List[Any] = bart.extract_features(SCREAMING_SNAKE_CASE__ ) if hf_checkpoint_name == "facebook/bart-large": __lowerCamelCase : int = BartModel(SCREAMING_SNAKE_CASE__ ).eval() model.load_state_dict(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : Optional[int] = model(SCREAMING_SNAKE_CASE__ ).model[0] else: __lowerCamelCase : int = BartForConditionalGeneration(SCREAMING_SNAKE_CASE__ ).eval() # an existing summarization ckpt model.model.load_state_dict(SCREAMING_SNAKE_CASE__ ) if hasattr(SCREAMING_SNAKE_CASE__ , 'lm_head' ): __lowerCamelCase : Any = make_linear_from_emb(model.model.shared ) __lowerCamelCase : Optional[Any] = model.model(SCREAMING_SNAKE_CASE__ )[0] # Check results if fairseq_output.shape != new_model_outputs.shape: raise ValueError( F"`fairseq_output` shape and `new_model_output` shape are different: {fairseq_output.shape=}, {new_model_outputs.shape}" ) if (fairseq_output != new_model_outputs).any().item(): raise ValueError('Some values in `fairseq_output` are different from `new_model_outputs`' ) Path(SCREAMING_SNAKE_CASE__ ).mkdir(exist_ok=SCREAMING_SNAKE_CASE__ ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": a =argparse.ArgumentParser() # Required parameters parser.add_argument( """fairseq_path""", type=str, help="""bart.large, bart.large.cnn or a path to a model.pt on local filesystem.""" ) parser.add_argument("""pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument( """--hf_config""", default=None, type=str, help="""Which huggingface architecture to use: bart-large-xsum""" ) a =parser.parse_args() convert_bart_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, hf_checkpoint_name=args.hf_config)
73
from ..utils import DummyObject, requires_backends class snake_case_ ( metaclass=__A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = ["note_seq"] def __init__( self : Optional[int] , *_UpperCamelCase : str , **_UpperCamelCase : Optional[int] ) ->Any: requires_backends(self , ['''note_seq'''] ) @classmethod def snake_case__( cls : int , *_UpperCamelCase : Any , **_UpperCamelCase : List[Any] ) ->int: requires_backends(cls , ['''note_seq'''] ) @classmethod def snake_case__( cls : Dict , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Union[str, Any] ) ->List[str]: requires_backends(cls , ['''note_seq'''] )
8
0
import random class lowerCAmelCase_ : @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ) -> tuple[list[int], list[int]]: UpperCamelCase : List[str] = [ord(_UpperCamelCase ) for i in text] UpperCamelCase : List[Any] = [] UpperCamelCase : Optional[Any] = [] for i in plain: UpperCamelCase : List[str] = random.randint(1, 300 ) UpperCamelCase : List[str] = (i + k) * k cipher.append(_UpperCamelCase ) key.append(_UpperCamelCase ) return cipher, key @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> str: UpperCamelCase : List[str] = [] for i in range(len(_UpperCamelCase ) ): UpperCamelCase : Dict = int((cipher[i] - (key[i]) ** 2) / key[i] ) plain.append(chr(_UpperCamelCase ) ) return "".join(_UpperCamelCase ) if __name__ == "__main__": __UpperCAmelCase , __UpperCAmelCase = Onepad().encrypt('''Hello''') print(c, k) print(Onepad().decrypt(c, k))
119
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = { '''sayakpaul/vit-msn-base''': '''https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json''', # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = "vit_msn" def __init__( self : Dict , _UpperCamelCase : Optional[int]=7_6_8 , _UpperCamelCase : Optional[Any]=1_2 , _UpperCamelCase : Union[str, Any]=1_2 , _UpperCamelCase : str=3_0_7_2 , _UpperCamelCase : Tuple="gelu" , _UpperCamelCase : Dict=0.0 , _UpperCamelCase : Dict=0.0 , _UpperCamelCase : List[str]=0.02 , _UpperCamelCase : List[Any]=1e-06 , _UpperCamelCase : Any=2_2_4 , _UpperCamelCase : Optional[Any]=1_6 , _UpperCamelCase : Any=3 , _UpperCamelCase : str=True , **_UpperCamelCase : Any , ) ->int: super().__init__(**_UpperCamelCase ) snake_case_ = hidden_size snake_case_ = num_hidden_layers snake_case_ = num_attention_heads snake_case_ = intermediate_size snake_case_ = hidden_act snake_case_ = hidden_dropout_prob snake_case_ = attention_probs_dropout_prob snake_case_ = initializer_range snake_case_ = layer_norm_eps snake_case_ = image_size snake_case_ = patch_size snake_case_ = num_channels snake_case_ = qkv_bias
8
0
from __future__ import annotations def lowerCamelCase_ ( _a : Optional[Any] ): # This function is recursive '''simple docstring''' UpperCAmelCase_ : Union[str, Any] = len(SCREAMING_SNAKE_CASE__ ) # If the array contains only one element, we return it (it's the stop condition of # recursion) if array_length <= 1: return array # Else UpperCAmelCase_ : Tuple = array[0] UpperCAmelCase_ : Dict = False UpperCAmelCase_ : Union[str, Any] = 1 UpperCAmelCase_ : List[Any] = [] while not is_found and i < array_length: if array[i] < pivot: UpperCAmelCase_ : Optional[int] = True UpperCAmelCase_ : List[str] = [element for element in array[i:] if element >= array[i]] UpperCAmelCase_ : int = longest_subsequence(SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) > len(SCREAMING_SNAKE_CASE__ ): UpperCAmelCase_ : Dict = temp_array else: i += 1 UpperCAmelCase_ : Any = [element for element in array[1:] if element >= pivot] UpperCAmelCase_ : Any = [pivot, *longest_subsequence(SCREAMING_SNAKE_CASE__ )] if len(SCREAMING_SNAKE_CASE__ ) > len(SCREAMING_SNAKE_CASE__ ): return temp_array else: return longest_subseq if __name__ == "__main__": import doctest doctest.testmod()
345
from __future__ import annotations from math import pi, sqrt def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): if inductance <= 0: raise ValueError('''Inductance cannot be 0 or negative''' ) elif capacitance <= 0: raise ValueError('''Capacitance cannot be 0 or negative''' ) else: return ( "Resonant frequency", float(1 / (2 * pi * (sqrt(inductance * capacitance ))) ), ) if __name__ == "__main__": import doctest doctest.testmod()
8
0
"""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 UpperCamelCase_ = logging.get_logger(__name__) def UpperCamelCase ( UpperCAmelCase , UpperCAmelCase ) ->List[str]: """simple docstring""" a_ = set() a_ = [] def parse_line(UpperCAmelCase ): for line in fp: if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): a_ = 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: a_ = "\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: a_ = line.strip() buffer.append(SCREAMING_SNAKE_CASE__ ) if from_gh: for filename in os.listdir(SCREAMING_SNAKE_CASE__ ): a_ = 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 UpperCamelCase ( UpperCAmelCase , UpperCAmelCase ) ->str: """simple docstring""" a_ = set() a_ = [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 UpperCamelCase ( UpperCAmelCase ) ->str: """simple docstring""" return values.split("," ) UpperCamelCase_ = 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.', ) UpperCamelCase_ = parser.parse_args() UpperCamelCase_ = 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 UpperCamelCase_ = 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 UpperCamelCase_ = extract_warnings(args.output_dir, args.targets) UpperCamelCase_ = 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)
243
import unittest from transformers.testing_utils import CaptureStdout from transformers.tools.python_interpreter import evaluate def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): return x + 2 class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : Optional[Any] ) ->int: snake_case_ = '''x = 3''' snake_case_ = {} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) assert result == 3 self.assertDictEqual(_UpperCamelCase , {'''x''': 3} ) snake_case_ = '''x = y''' snake_case_ = {'''y''': 5} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 5, '''y''': 5} ) def snake_case__( self : Dict ) ->Optional[int]: snake_case_ = '''y = add_two(x)''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {'''add_two''': add_two} , state=_UpperCamelCase ) assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''y''': 5} ) # Won't work without the tool with CaptureStdout() as out: snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) assert result is None assert "tried to execute add_two" in out.out def snake_case__( self : Union[str, Any] ) ->Dict: snake_case_ = '''x = 3''' snake_case_ = {} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) assert result == 3 self.assertDictEqual(_UpperCamelCase , {'''x''': 3} ) def snake_case__( self : Optional[int] ) ->Optional[int]: snake_case_ = '''test_dict = {\'x\': x, \'y\': add_two(x)}''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {'''add_two''': add_two} , state=_UpperCamelCase ) self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''y''': 5} ) self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''test_dict''': {'''x''': 3, '''y''': 5}} ) def snake_case__( self : Dict ) ->str: snake_case_ = '''x = 3\ny = 5''' snake_case_ = {} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''y''': 5} ) def snake_case__( self : str ) ->Tuple: snake_case_ = '''text = f\'This is x: {x}.\'''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) # evaluate returns the value of the last assignment. assert result == "This is x: 3." self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''text''': '''This is x: 3.'''} ) def snake_case__( self : Optional[Any] ) ->List[str]: snake_case_ = '''if x <= 3:\n y = 2\nelse:\n y = 5''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) # evaluate returns the value of the last assignment. assert result == 2 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''y''': 2} ) snake_case_ = {'''x''': 8} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 8, '''y''': 5} ) def snake_case__( self : str ) ->str: snake_case_ = '''test_list = [x, add_two(x)]''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {'''add_two''': add_two} , state=_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , [3, 5] ) self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''test_list''': [3, 5]} ) def snake_case__( self : Any ) ->List[Any]: snake_case_ = '''y = x''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {} , state=_UpperCamelCase ) assert result == 3 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''y''': 3} ) def snake_case__( self : Optional[int] ) ->Dict: snake_case_ = '''test_list = [x, add_two(x)]\ntest_list[1]''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {'''add_two''': add_two} , state=_UpperCamelCase ) assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''test_list''': [3, 5]} ) snake_case_ = '''test_dict = {\'x\': x, \'y\': add_two(x)}\ntest_dict[\'y\']''' snake_case_ = {'''x''': 3} snake_case_ = evaluate(_UpperCamelCase , {'''add_two''': add_two} , state=_UpperCamelCase ) assert result == 5 self.assertDictEqual(_UpperCamelCase , {'''x''': 3, '''test_dict''': {'''x''': 3, '''y''': 5}} ) def snake_case__( self : Optional[Any] ) ->int: snake_case_ = '''x = 0\nfor i in range(3):\n x = i''' snake_case_ = {} snake_case_ = evaluate(_UpperCamelCase , {'''range''': range} , state=_UpperCamelCase ) assert result == 2 self.assertDictEqual(_UpperCamelCase , {'''x''': 2, '''i''': 2} )
8
0
"""simple docstring""" from __future__ import annotations SCREAMING_SNAKE_CASE : Any = list[list[int]] # assigning initial values to the grid SCREAMING_SNAKE_CASE : List[Any] = [ [3, 0, 6, 5, 0, 8, 4, 0, 0], [5, 2, 0, 0, 0, 0, 0, 0, 0], [0, 8, 7, 0, 0, 0, 0, 3, 1], [0, 0, 3, 0, 1, 0, 0, 8, 0], [9, 0, 0, 8, 6, 3, 0, 0, 5], [0, 5, 0, 0, 9, 0, 6, 0, 0], [1, 3, 0, 0, 0, 0, 2, 5, 0], [0, 0, 0, 0, 0, 0, 0, 7, 4], [0, 0, 5, 2, 0, 6, 3, 0, 0], ] # a grid with no solution SCREAMING_SNAKE_CASE : Tuple = [ [5, 0, 6, 5, 0, 8, 4, 0, 3], [5, 2, 0, 0, 0, 0, 0, 0, 2], [1, 8, 7, 0, 0, 0, 0, 3, 1], [0, 0, 3, 0, 1, 0, 0, 8, 0], [9, 0, 0, 8, 6, 3, 0, 0, 5], [0, 5, 0, 0, 9, 0, 6, 0, 0], [1, 3, 0, 0, 0, 0, 2, 5, 0], [0, 0, 0, 0, 0, 0, 0, 7, 4], [0, 0, 5, 2, 0, 6, 3, 0, 0], ] def lowercase ( _snake_case : Optional[Any] , _snake_case : Optional[Any] , _snake_case : Tuple , _snake_case : List[Any] ) ->Any: """simple docstring""" for i in range(9 ): if grid[row][i] == n or grid[i][column] == n: return False for i in range(3 ): for j in range(3 ): if grid[(row - row % 3) + i][(column - column % 3) + j] == n: return False return True def lowercase ( _snake_case : Union[str, Any] ) ->Optional[int]: """simple docstring""" for i in range(9 ): for j in range(9 ): if grid[i][j] == 0: return i, j return None def lowercase ( _snake_case : str ) ->Optional[Any]: """simple docstring""" if location := find_empty_location(SCREAMING_SNAKE_CASE__ ): __snake_case , __snake_case : str = location else: # If the location is ``None``, then the grid is solved. return grid for digit in range(1 , 10 ): if is_safe(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): __snake_case : List[Any] = digit if sudoku(SCREAMING_SNAKE_CASE__ ) is not None: return grid __snake_case : List[Any] = 0 return None def lowercase ( _snake_case : Dict ) ->List[Any]: """simple docstring""" for row in grid: for cell in row: print(SCREAMING_SNAKE_CASE__ , end=''' ''' ) print() if __name__ == "__main__": # make a copy of grid so that you can compare with the unmodified grid for example_grid in (initial_grid, no_solution): print("""\nExample grid:\n""" + """=""" * 20) print_solution(example_grid) print("""\nExample grid solution:""") SCREAMING_SNAKE_CASE : Dict = sudoku(example_grid) if solution is not None: print_solution(solution) else: print("""Cannot find a solution.""")
102
import gc import unittest from parameterized import parameterized from diffusers import FlaxUNetaDConditionModel from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow if is_flax_available(): import jax import jax.numpy as jnp @slow @require_flax class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : Any , _UpperCamelCase : Any , _UpperCamelCase : Tuple ) ->List[Any]: return f'''gaussian_noise_s={seed}_shape={'_'.join([str(_UpperCamelCase ) for s in shape] )}.npy''' def snake_case__( self : Any ) ->List[str]: # clean up the VRAM after each test super().tearDown() gc.collect() def snake_case__( self : int , _UpperCamelCase : Union[str, Any]=0 , _UpperCamelCase : int=(4, 4, 6_4, 6_4) , _UpperCamelCase : Optional[int]=False ) ->Tuple: snake_case_ = jnp.bfloataa if fpaa else jnp.floataa snake_case_ = jnp.array(load_hf_numpy(self.get_file_format(_UpperCamelCase , _UpperCamelCase ) ) , dtype=_UpperCamelCase ) return image def snake_case__( self : List[Any] , _UpperCamelCase : Optional[Any]=False , _UpperCamelCase : Optional[int]="CompVis/stable-diffusion-v1-4" ) ->Optional[Any]: snake_case_ = jnp.bfloataa if fpaa else jnp.floataa snake_case_ = '''bf16''' if fpaa else None snake_case_, snake_case_ = FlaxUNetaDConditionModel.from_pretrained( _UpperCamelCase , subfolder='''unet''' , dtype=_UpperCamelCase , revision=_UpperCamelCase ) return model, params def snake_case__( self : Dict , _UpperCamelCase : List[Any]=0 , _UpperCamelCase : Tuple=(4, 7_7, 7_6_8) , _UpperCamelCase : List[Any]=False ) ->int: snake_case_ = jnp.bfloataa if fpaa else jnp.floataa snake_case_ = jnp.array(load_hf_numpy(self.get_file_format(_UpperCamelCase , _UpperCamelCase ) ) , dtype=_UpperCamelCase ) return hidden_states @parameterized.expand( [ # fmt: off [8_3, 4, [-0.2323, -0.1304, 0.0813, -0.3093, -0.0919, -0.1571, -0.1125, -0.5806]], [1_7, 0.55, [-0.0831, -0.2443, 0.0901, -0.0919, 0.3396, 0.0103, -0.3743, 0.0701]], [8, 0.89, [-0.4863, 0.0859, 0.0875, -0.1658, 0.9199, -0.0114, 0.4839, 0.4639]], [3, 1_0_0_0, [-0.5649, 0.2402, -0.5518, 0.1248, 1.1328, -0.2443, -0.0325, -1.0078]], # fmt: on ] ) def snake_case__( self : Optional[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : List[str] , _UpperCamelCase : Optional[int] ) ->Union[str, Any]: snake_case_, snake_case_ = self.get_unet_model(model_id='''CompVis/stable-diffusion-v1-4''' , fpaa=_UpperCamelCase ) snake_case_ = self.get_latents(_UpperCamelCase , fpaa=_UpperCamelCase ) snake_case_ = self.get_encoder_hidden_states(_UpperCamelCase , fpaa=_UpperCamelCase ) snake_case_ = model.apply( {'''params''': params} , _UpperCamelCase , jnp.array(_UpperCamelCase , dtype=jnp.intaa ) , encoder_hidden_states=_UpperCamelCase , ).sample assert sample.shape == latents.shape snake_case_ = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) snake_case_ = jnp.array(_UpperCamelCase , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware assert jnp.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-2 ) @parameterized.expand( [ # fmt: off [8_3, 4, [0.1514, 0.0807, 0.1624, 0.1016, -0.1896, 0.0263, 0.0677, 0.2310]], [1_7, 0.55, [0.1164, -0.0216, 0.0170, 0.1589, -0.3120, 0.1005, -0.0581, -0.1458]], [8, 0.89, [-0.1758, -0.0169, 0.1004, -0.1411, 0.1312, 0.1103, -0.1996, 0.2139]], [3, 1_0_0_0, [0.1214, 0.0352, -0.0731, -0.1562, -0.0994, -0.0906, -0.2340, -0.0539]], # fmt: on ] ) def snake_case__( self : Optional[int] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str ) ->Dict: snake_case_, snake_case_ = self.get_unet_model(model_id='''stabilityai/stable-diffusion-2''' , fpaa=_UpperCamelCase ) snake_case_ = self.get_latents(_UpperCamelCase , shape=(4, 4, 9_6, 9_6) , fpaa=_UpperCamelCase ) snake_case_ = self.get_encoder_hidden_states(_UpperCamelCase , shape=(4, 7_7, 1_0_2_4) , fpaa=_UpperCamelCase ) snake_case_ = model.apply( {'''params''': params} , _UpperCamelCase , jnp.array(_UpperCamelCase , dtype=jnp.intaa ) , encoder_hidden_states=_UpperCamelCase , ).sample assert sample.shape == latents.shape snake_case_ = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) snake_case_ = jnp.array(_UpperCamelCase , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware assert jnp.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-2 )
8
0
'''simple docstring''' import warnings from typing import List, Optional, Tuple, Union import numpy as np import PIL import torch from ...models import UNetaDModel from ...schedulers import RePaintScheduler from ...utils import PIL_INTERPOLATION, logging, randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput SCREAMING_SNAKE_CASE_: str =logging.get_logger(__name__) # pylint: disable=invalid-name def lowerCAmelCase_ ( snake_case_ : Union[str, Any] ) -> List[str]: '''simple docstring''' warnings.warn( "The preprocess method is deprecated and will be removed in a future version. Please" " use VaeImageProcessor.preprocess instead" , SCREAMING_SNAKE_CASE__ , ) if isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): return image elif isinstance(SCREAMING_SNAKE_CASE__ , PIL.Image.Image ): UpperCAmelCase_ = [image] if isinstance(image[0] , PIL.Image.Image ): UpperCAmelCase_ , UpperCAmelCase_ = image[0].size UpperCAmelCase_ , UpperCAmelCase_ = (x - x % 8 for x in (w, h)) # resize to integer multiple of 8 UpperCAmelCase_ = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION["lanczos"] ) )[None, :] for i in image] UpperCAmelCase_ = np.concatenate(SCREAMING_SNAKE_CASE__ , axis=0 ) UpperCAmelCase_ = np.array(SCREAMING_SNAKE_CASE__ ).astype(np.floataa ) / 255.0 UpperCAmelCase_ = image.transpose(0 , 3 , 1 , 2 ) UpperCAmelCase_ = 2.0 * image - 1.0 UpperCAmelCase_ = torch.from_numpy(SCREAMING_SNAKE_CASE__ ) elif isinstance(image[0] , torch.Tensor ): UpperCAmelCase_ = torch.cat(SCREAMING_SNAKE_CASE__ , dim=0 ) return image def lowerCAmelCase_ ( snake_case_ : List[str] ) -> Optional[Any]: '''simple docstring''' if isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): return mask elif isinstance(SCREAMING_SNAKE_CASE__ , PIL.Image.Image ): UpperCAmelCase_ = [mask] if isinstance(mask[0] , PIL.Image.Image ): UpperCAmelCase_ , UpperCAmelCase_ = mask[0].size UpperCAmelCase_ , UpperCAmelCase_ = (x - x % 32 for x in (w, h)) # resize to integer multiple of 32 UpperCAmelCase_ = [np.array(m.convert("L" ).resize((w, h) , resample=PIL_INTERPOLATION["nearest"] ) )[None, :] for m in mask] UpperCAmelCase_ = np.concatenate(SCREAMING_SNAKE_CASE__ , axis=0 ) UpperCAmelCase_ = mask.astype(np.floataa ) / 255.0 UpperCAmelCase_ = 0 UpperCAmelCase_ = 1 UpperCAmelCase_ = torch.from_numpy(SCREAMING_SNAKE_CASE__ ) elif isinstance(mask[0] , torch.Tensor ): UpperCAmelCase_ = torch.cat(SCREAMING_SNAKE_CASE__ , dim=0 ) return mask class __A ( __A ): a__ : UNetaDModel a__ : RePaintScheduler def __init__(self : List[str] , __a : int , __a : Optional[int] ): super().__init__() self.register_modules(unet=_UpperCamelCase , scheduler=_UpperCamelCase ) @torch.no_grad() def __call__(self : List[Any] , __a : Union[torch.Tensor, PIL.Image.Image] , __a : Union[torch.Tensor, PIL.Image.Image] , __a : int = 250 , __a : float = 0.0 , __a : int = 10 , __a : int = 10 , __a : Optional[Union[torch.Generator, List[torch.Generator]]] = None , __a : Optional[str] = "pil" , __a : bool = True , ): UpperCAmelCase_ = image UpperCAmelCase_ = _preprocess_image(_UpperCamelCase ) UpperCAmelCase_ = original_image.to(device=self.device , dtype=self.unet.dtype ) UpperCAmelCase_ = _preprocess_mask(_UpperCamelCase ) UpperCAmelCase_ = mask_image.to(device=self.device , dtype=self.unet.dtype ) UpperCAmelCase_ = original_image.shape[0] # sample gaussian noise to begin the loop if isinstance(_UpperCamelCase , _UpperCamelCase ) and len(_UpperCamelCase ) != batch_size: raise ValueError( f"""You have passed a list of generators of length {len(_UpperCamelCase )}, but requested an effective batch""" f""" size of {batch_size}. Make sure the batch size matches the length of the generators.""" ) UpperCAmelCase_ = original_image.shape UpperCAmelCase_ = randn_tensor(_UpperCamelCase , generator=_UpperCamelCase , device=self.device , dtype=self.unet.dtype ) # set step values self.scheduler.set_timesteps(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , self.device ) UpperCAmelCase_ = eta UpperCAmelCase_ = self.scheduler.timesteps[0] + 1 UpperCAmelCase_ = generator[0] if isinstance(_UpperCamelCase , _UpperCamelCase ) else generator for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): if t < t_last: # predict the noise residual UpperCAmelCase_ = self.unet(_UpperCamelCase , _UpperCamelCase ).sample # compute previous image: x_t -> x_t-1 UpperCAmelCase_ = self.scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ).prev_sample else: # compute the reverse: x_t-1 -> x_t UpperCAmelCase_ = self.scheduler.undo_step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) UpperCAmelCase_ = t UpperCAmelCase_ = (image / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase_ = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase_ = self.numpy_to_pil(_UpperCamelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=_UpperCamelCase )
1
import functools import gc import inspect import torch from .imports import is_npu_available, is_xpu_available def __SCREAMING_SNAKE_CASE (*SCREAMING_SNAKE_CASE__ ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = list(SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) ): snake_case_ = None gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() return objects def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = [ '''CUDA out of memory.''', # CUDA OOM '''cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.''', # CUDNN SNAFU '''DefaultCPUAllocator: can\'t allocate memory''', # CPU OOM ] if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and len(exception.args ) == 1: return any(err in exception.args[0] for err in _statements ) return False def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = 128 ): if function is None: return functools.partial(SCREAMING_SNAKE_CASE__ , starting_batch_size=SCREAMING_SNAKE_CASE__ ) snake_case_ = starting_batch_size def decorator(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): nonlocal batch_size gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() snake_case_ = list(inspect.signature(SCREAMING_SNAKE_CASE__ ).parameters.keys() ) # Guard against user error if len(SCREAMING_SNAKE_CASE__ ) < (len(SCREAMING_SNAKE_CASE__ ) + 1): snake_case_ = ''', '''.join([F'''{arg}={value}''' for arg, value in zip(params[1:] , args[1:] )] ) raise TypeError( F'''Batch size was passed into `{function.__name__}` as the first argument when called.''' F'''Remove this as the decorator already does so: `{function.__name__}({arg_str})`''' ) while True: if batch_size == 0: raise RuntimeError('''No executable batch size found, reached zero.''' ) try: return function(SCREAMING_SNAKE_CASE__ , *SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) except Exception as e: if should_reduce_batch_size(SCREAMING_SNAKE_CASE__ ): gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() batch_size //= 2 else: raise return decorator
8
0
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import logging A_ : Dict = logging.get_logger(__name__) class lowercase ( __A ): """simple docstring""" UpperCAmelCase = "encoder-decoder" UpperCAmelCase = True def __init__( self ,**a_ ) -> Dict: super().__init__(**_UpperCamelCase ) assert ( "encoder" in kwargs and "decoder" in kwargs ), "Config has to be initialized with encoder and decoder config" _UpperCAmelCase : Union[str, Any] = kwargs.pop("""encoder""" ) _UpperCAmelCase : Optional[Any] = encoder_config.pop("""model_type""" ) _UpperCAmelCase : Tuple = kwargs.pop("""decoder""" ) _UpperCAmelCase : List[Any] = decoder_config.pop("""model_type""" ) from ..auto.configuration_auto import AutoConfig _UpperCAmelCase : Optional[int] = AutoConfig.for_model(_UpperCamelCase ,**_UpperCamelCase ) _UpperCAmelCase : str = AutoConfig.for_model(_UpperCamelCase ,**_UpperCamelCase ) _UpperCAmelCase : Dict = True @classmethod def _snake_case ( cls ,a_ ,a_ ,**a_ ) -> PretrainedConfig: logger.info("""Set `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config""" ) _UpperCAmelCase : Tuple = True _UpperCAmelCase : str = True return cls(encoder=encoder_config.to_dict() ,decoder=decoder_config.to_dict() ,**_UpperCamelCase ) def _snake_case ( self ) -> Union[str, Any]: _UpperCAmelCase : int = copy.deepcopy(self.__dict__ ) _UpperCAmelCase : int = self.encoder.to_dict() _UpperCAmelCase : str = self.decoder.to_dict() _UpperCAmelCase : List[Any] = self.__class__.model_type return output
215
from __future__ import annotations def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): return [ord(SCREAMING_SNAKE_CASE__ ) - 96 for elem in plain] def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): return "".join(chr(elem + 96 ) for elem in encoded ) def __SCREAMING_SNAKE_CASE (): snake_case_ = encode(input('''-> ''' ).strip().lower() ) print('''Encoded: ''' , SCREAMING_SNAKE_CASE__ ) print('''Decoded:''' , decode(SCREAMING_SNAKE_CASE__ ) ) if __name__ == "__main__": main()
8
0
import warnings from typing import Dict, List, Optional, Tuple from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __UpperCamelCase : List[Any] = logging.get_logger(__name__) class lowercase__ ( __A): UpperCamelCase_ = ["input_ids", "attention_mask"] def __init__( self : List[Any] , UpperCamelCase__ : str="</s>" , UpperCamelCase__ : List[Any]="<unk>" , UpperCamelCase__ : Optional[int]="<pad>" , UpperCamelCase__ : Optional[int]=125 , UpperCamelCase__ : Dict=None , **UpperCamelCase__ : Tuple , ): '''simple docstring''' if extra_ids > 0 and additional_special_tokens is None: SCREAMING_SNAKE_CASE : Any = [f"""<extra_id_{i}>""" for i in range(_UpperCamelCase )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra_id special tokens SCREAMING_SNAKE_CASE : str = len(set(filter(lambda UpperCamelCase__ : bool('''extra_id''' in str(_UpperCamelCase ) ) , _UpperCamelCase ) ) ) if extra_tokens != extra_ids: raise ValueError( f"""Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are""" ''' provided to ByT5Tokenizer. In this case the additional_special_tokens must include the''' ''' extra_ids tokens''' ) SCREAMING_SNAKE_CASE : int = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else pad_token SCREAMING_SNAKE_CASE : Optional[int] = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else eos_token SCREAMING_SNAKE_CASE : Tuple = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else unk_token super().__init__( eos_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , extra_ids=_UpperCamelCase , additional_special_tokens=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE : List[str] = extra_ids SCREAMING_SNAKE_CASE : Tuple = 2**8 # utf is 8 bits # define special tokens dict SCREAMING_SNAKE_CASE : Dict = { self.pad_token: 0, self.eos_token: 1, self.unk_token: 2, } SCREAMING_SNAKE_CASE : str = len(self.special_tokens_encoder ) SCREAMING_SNAKE_CASE : Optional[int] = len(_UpperCamelCase ) for i, token in enumerate(_UpperCamelCase ): SCREAMING_SNAKE_CASE : List[str] = self.vocab_size + i - n SCREAMING_SNAKE_CASE : Union[str, Any] = {v: k for k, v in self.special_tokens_encoder.items()} @property def __A ( self : Optional[int] ): '''simple docstring''' return self._utf_vocab_size + self._num_special_tokens + self._extra_ids def __A ( self : Tuple , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None , UpperCamelCase__ : bool = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) # normal case: some special tokens if token_ids_a is None: return ([0] * len(_UpperCamelCase )) + [1] return ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] def __A ( self : Union[str, Any] , UpperCamelCase__ : List[int] ): '''simple docstring''' if len(_UpperCamelCase ) > 0 and token_ids[-1] == self.eos_token_id: warnings.warn( f"""This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated""" ''' eos tokens being added.''' ) return token_ids else: return token_ids + [self.eos_token_id] def __A ( self : Optional[Any] , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos ) * [0] return len(token_ids_a + eos + token_ids_a + eos ) * [0] def __A ( self : Optional[Any] , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self._add_eos_if_not_present(_UpperCamelCase ) if token_ids_a is None: return token_ids_a else: SCREAMING_SNAKE_CASE : Any = self._add_eos_if_not_present(_UpperCamelCase ) return token_ids_a + token_ids_a def __A ( self : Optional[int] , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = [chr(_UpperCamelCase ) for i in text.encode('''utf-8''' )] return tokens def __A ( self : List[str] , UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' if token in self.special_tokens_encoder: SCREAMING_SNAKE_CASE : Tuple = self.special_tokens_encoder[token] elif token in self.added_tokens_encoder: SCREAMING_SNAKE_CASE : Any = self.added_tokens_encoder[token] elif len(_UpperCamelCase ) != 1: SCREAMING_SNAKE_CASE : List[str] = self.unk_token_id else: SCREAMING_SNAKE_CASE : List[str] = ord(_UpperCamelCase ) + self._num_special_tokens return token_id def __A ( self : int , UpperCamelCase__ : Tuple ): '''simple docstring''' if index in self.special_tokens_decoder: SCREAMING_SNAKE_CASE : Any = self.special_tokens_decoder[index] else: SCREAMING_SNAKE_CASE : str = chr(index - self._num_special_tokens ) return token def __A ( self : int , UpperCamelCase__ : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = B'''''' for token in tokens: if token in self.special_tokens_decoder: SCREAMING_SNAKE_CASE : Any = self.special_tokens_decoder[token].encode('''utf-8''' ) elif token in self.added_tokens_decoder: SCREAMING_SNAKE_CASE : List[Any] = self.special_tokens_decoder[token].encode('''utf-8''' ) elif token in self.special_tokens_encoder: SCREAMING_SNAKE_CASE : int = token.encode('''utf-8''' ) elif token in self.added_tokens_encoder: SCREAMING_SNAKE_CASE : List[str] = token.encode('''utf-8''' ) else: SCREAMING_SNAKE_CASE : Optional[Any] = bytes([ord(_UpperCamelCase )] ) bstring += tok_string SCREAMING_SNAKE_CASE : List[Any] = bstring.decode('''utf-8''' , errors='''ignore''' ) return string def __A ( self : Dict , UpperCamelCase__ : str , UpperCamelCase__ : Optional[str] = None ): '''simple docstring''' return ()
182
import math def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): 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''')
8
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _lowerCamelCase : List[Any] = { "configuration_git": ["GIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "GitConfig", "GitVisionConfig"], "processing_git": ["GitProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[str] = [ "GIT_PRETRAINED_MODEL_ARCHIVE_LIST", "GitForCausalLM", "GitModel", "GitPreTrainedModel", "GitVisionModel", ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys _lowerCamelCase : List[str] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
336
import os import re from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = {'''vocab_file''': '''spiece.model'''} lowerCAmelCase_ = { '''vocab_file''': { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model''', '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model''' ), } } lowerCAmelCase_ = { '''google/bigbird-roberta-base''': 40_96, '''google/bigbird-roberta-large''': 40_96, '''google/bigbird-base-trivia-itc''': 40_96, } class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE : Optional[int] = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE : List[Any] = ["input_ids", "attention_mask"] SCREAMING_SNAKE_CASE : List[int] = [] def __init__( self : List[str] , _UpperCamelCase : List[str] , _UpperCamelCase : Dict="<unk>" , _UpperCamelCase : List[str]="<s>" , _UpperCamelCase : Tuple="</s>" , _UpperCamelCase : Any="<pad>" , _UpperCamelCase : Any="[SEP]" , _UpperCamelCase : Optional[Any]="[MASK]" , _UpperCamelCase : Any="[CLS]" , _UpperCamelCase : Optional[Dict[str, Any]] = None , **_UpperCamelCase : Dict , ) ->None: snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else bos_token snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else eos_token snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else unk_token snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else pad_token snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else cls_token snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else sep_token # Mask token behave like a normal word, i.e. include the space before it snake_case_ = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else mask_token snake_case_ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , sep_token=_UpperCamelCase , mask_token=_UpperCamelCase , cls_token=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCamelCase , ) snake_case_ = vocab_file snake_case_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_UpperCamelCase ) @property def snake_case__( self : str ) ->List[Any]: return self.sp_model.get_piece_size() def snake_case__( self : int ) ->Union[str, Any]: snake_case_ = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Tuple ) ->Any: snake_case_ = self.__dict__.copy() snake_case_ = None return state def __setstate__( self : str , _UpperCamelCase : List[Any] ) ->List[str]: snake_case_ = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): snake_case_ = {} snake_case_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def snake_case__( self : Optional[int] , _UpperCamelCase : str ) ->List[str]: return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def snake_case__( self : str , _UpperCamelCase : List[str] ) ->Tuple: return self.sp_model.piece_to_id(_UpperCamelCase ) def snake_case__( self : Union[str, Any] , _UpperCamelCase : str ) ->List[Any]: snake_case_ = self.sp_model.IdToPiece(_UpperCamelCase ) return token def snake_case__( self : Dict , _UpperCamelCase : Optional[int] ) ->List[str]: snake_case_ = [] snake_case_ = '''''' snake_case_ = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(_UpperCamelCase ) + token snake_case_ = True snake_case_ = [] else: current_sub_tokens.append(_UpperCamelCase ) snake_case_ = False out_string += self.sp_model.decode(_UpperCamelCase ) return out_string.strip() def snake_case__( self : int , _UpperCamelCase : List[int] , _UpperCamelCase : bool = False , _UpperCamelCase : bool = None , _UpperCamelCase : bool = True , **_UpperCamelCase : List[str] , ) ->str: snake_case_ = kwargs.pop('''use_source_tokenizer''' , _UpperCamelCase ) snake_case_ = self.convert_ids_to_tokens(_UpperCamelCase , skip_special_tokens=_UpperCamelCase ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 snake_case_ = [] snake_case_ = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_UpperCamelCase ) ) snake_case_ = [] sub_texts.append(_UpperCamelCase ) else: current_sub_text.append(_UpperCamelCase ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_UpperCamelCase ) ) # Mimic the behavior of the Rust tokenizer: # No space before [MASK] and [SEP] if spaces_between_special_tokens: snake_case_ = re.sub(R''' (\[(MASK|SEP)\])''' , R'''\1''' , ''' '''.join(_UpperCamelCase ) ) else: snake_case_ = ''''''.join(_UpperCamelCase ) snake_case_ = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: snake_case_ = self.clean_up_tokenization(_UpperCamelCase ) return clean_text else: return text def snake_case__( self : List[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) ->Tuple[str]: if not os.path.isdir(_UpperCamelCase ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return snake_case_ = os.path.join( _UpperCamelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase , '''wb''' ) as fi: snake_case_ = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,) def snake_case__( self : Tuple , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) ->List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] snake_case_ = [self.cls_token_id] snake_case_ = [self.sep_token_id] return cls + token_ids_a + sep + token_ids_a + sep def snake_case__( self : List[str] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : bool = False ) ->List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) if token_ids_a is None: return [1] + ([0] * len(_UpperCamelCase )) + [1] return [1] + ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] def snake_case__( self : List[Any] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) ->List[int]: snake_case_ = [self.sep_token_id] snake_case_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]
8
0
import os import pickle import unittest from transformers import AutoTokenizer from transformers.models.bert.tokenization_bert import BertTokenizer from transformers.models.bert_japanese.tokenization_bert_japanese import ( VOCAB_FILES_NAMES, BertJapaneseTokenizer, CharacterTokenizer, JumanppTokenizer, MecabTokenizer, SudachiTokenizer, WordpieceTokenizer, ) from transformers.testing_utils import custom_tokenizers, require_jumanpp, require_sudachi from ...test_tokenization_common import TokenizerTesterMixin @custom_tokenizers class __lowerCamelCase ( __A , unittest.TestCase): """simple docstring""" UpperCamelCase__ = BertJapaneseTokenizer UpperCamelCase__ = False UpperCamelCase__ = True def UpperCamelCase ( self ): """simple docstring""" super().setUp() _UpperCAmelCase = [ '[UNK]', '[CLS]', '[SEP]', 'こんにちは', 'こん', 'にちは', 'ばんは', '##こん', '##にちは', '##ばんは', '世界', '##世界', '、', '##、', '。', '##。', ] _UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def UpperCamelCase ( self , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = 'こんにちは、世界。 \nこんばんは、世界。' _UpperCAmelCase = 'こんにちは 、 世界 。 こんばんは 、 世界 。' return input_text, output_text def UpperCamelCase ( self , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = self.get_input_output_texts(_UpperCamelCase ) _UpperCAmelCase = tokenizer.encode(_UpperCamelCase , add_special_tokens=_UpperCamelCase ) _UpperCAmelCase = tokenizer.decode(_UpperCamelCase , clean_up_tokenization_spaces=_UpperCamelCase ) return text, ids def UpperCamelCase ( self ): """simple docstring""" pass # TODO add if relevant def UpperCamelCase ( self ): """simple docstring""" pass # TODO add if relevant def UpperCamelCase ( self ): """simple docstring""" pass # TODO add if relevant def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.tokenizer_class(self.vocab_file ) _UpperCAmelCase = tokenizer.tokenize('こんにちは、世界。\nこんばんは、世界。' ) self.assertListEqual(_UpperCamelCase , ['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCamelCase ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.tokenizer_class(self.vocab_file , word_tokenizer_type='mecab' ) self.assertIsNotNone(_UpperCamelCase ) _UpperCAmelCase = 'こんにちは、世界。\nこんばんは、世界。' _UpperCAmelCase = tokenizer.tokenize(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , ['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCamelCase ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) _UpperCAmelCase = os.path.join(self.tmpdirname , 'tokenizer.bin' ) with open(_UpperCamelCase , 'wb' ) as handle: pickle.dump(_UpperCamelCase , _UpperCamelCase ) with open(_UpperCamelCase , 'rb' ) as handle: _UpperCAmelCase = pickle.load(_UpperCamelCase ) _UpperCAmelCase = tokenizer_new.tokenize(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = MecabTokenizer(mecab_dic='ipadic' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップルストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'] , ) def UpperCamelCase ( self ): """simple docstring""" try: _UpperCAmelCase = MecabTokenizer(mecab_dic='unidic_lite' ) except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'] , ) def UpperCamelCase ( self ): """simple docstring""" try: _UpperCAmelCase = MecabTokenizer(mecab_dic='unidic' ) except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'] , ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = MecabTokenizer(do_lower_case=_UpperCamelCase , mecab_dic='ipadic' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップルストア', 'で', 'iphone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'] , ) def UpperCamelCase ( self ): """simple docstring""" try: _UpperCAmelCase = MecabTokenizer( do_lower_case=_UpperCamelCase , normalize_text=_UpperCamelCase , mecab_option='-d /usr/local/lib/mecab/dic/jumandic' ) except RuntimeError: # if dict doesn't exist in the system, previous code raises this error. return self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップルストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れた', '\u3000', '。'] , ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = MecabTokenizer(normalize_text=_UpperCamelCase , mecab_dic='ipadic' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップルストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', ' ', '。'] , ) @require_sudachi def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.tokenizer_class(self.vocab_file , word_tokenizer_type='sudachi' ) self.assertIsNotNone(_UpperCamelCase ) _UpperCAmelCase = 'こんにちは、世界。\nこんばんは、世界。' _UpperCAmelCase = tokenizer.tokenize(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , ['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCamelCase ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) _UpperCAmelCase = os.path.join(self.tmpdirname , 'tokenizer.bin' ) with open(_UpperCamelCase , 'wb' ) as handle: pickle.dump(_UpperCamelCase , _UpperCamelCase ) with open(_UpperCamelCase , 'rb' ) as handle: _UpperCAmelCase = pickle.load(_UpperCamelCase ) _UpperCAmelCase = tokenizer_new.tokenize(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) @require_sudachi def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = SudachiTokenizer(sudachi_dict_type='core' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , [' ', '\t', 'アップル', 'ストア', 'で', 'iPhone', '8', ' ', 'が', ' ', ' ', '\n ', '発売', 'さ', 'れ', 'た', ' ', '。', ' ', ' '] , ) @require_sudachi def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = SudachiTokenizer(sudachi_dict_type='core' , sudachi_split_mode='A' ) self.assertListEqual(tokenizer.tokenize('外国人参政権' ) , ['外国', '人', '参政', '権'] ) @require_sudachi def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = SudachiTokenizer(sudachi_dict_type='core' , sudachi_split_mode='B' ) self.assertListEqual(tokenizer.tokenize('外国人参政権' ) , ['外国人', '参政権'] ) @require_sudachi def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = SudachiTokenizer(sudachi_dict_type='core' , sudachi_split_mode='C' ) self.assertListEqual(tokenizer.tokenize('外国人参政権' ) , ['外国人参政権'] ) @require_sudachi def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = SudachiTokenizer(do_lower_case=_UpperCamelCase , sudachi_dict_type='core' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , [' ', '\t', 'アップル', 'ストア', 'で', 'iphone', '8', ' ', 'が', ' ', ' ', '\n ', '発売', 'さ', 'れ', 'た', ' ', '。', ' ', ' '] , ) @require_sudachi def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = SudachiTokenizer(normalize_text=_UpperCamelCase , sudachi_dict_type='core' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , [' ', '\t', 'アップル', 'ストア', 'で', 'iPhone', '8', ' ', 'が', ' ', ' ', '\n ', '発売', 'さ', 'れ', 'た', '\u3000', '。', ' ', ' '] , ) @require_sudachi def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = SudachiTokenizer(trim_whitespace=_UpperCamelCase , sudachi_dict_type='core' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'] , ) @require_jumanpp def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.tokenizer_class(self.vocab_file , word_tokenizer_type='jumanpp' ) self.assertIsNotNone(_UpperCamelCase ) _UpperCAmelCase = 'こんにちは、世界。\nこんばんは、世界。' _UpperCAmelCase = tokenizer.tokenize(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , ['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCamelCase ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) _UpperCAmelCase = os.path.join(self.tmpdirname , 'tokenizer.bin' ) with open(_UpperCamelCase , 'wb' ) as handle: pickle.dump(_UpperCamelCase , _UpperCamelCase ) with open(_UpperCamelCase , 'rb' ) as handle: _UpperCAmelCase = pickle.load(_UpperCamelCase ) _UpperCAmelCase = tokenizer_new.tokenize(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) @require_jumanpp def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iPhone', '8', '\u3000', 'が', '\u3000', '\u3000', '\u3000', '発売', 'さ', 'れた', '\u3000', '。'] , ) @require_jumanpp def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = JumanppTokenizer(do_lower_case=_UpperCamelCase ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iphone', '8', '\u3000', 'が', '\u3000', '\u3000', '\u3000', '発売', 'さ', 'れた', '\u3000', '。'] , ) @require_jumanpp def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = JumanppTokenizer(normalize_text=_UpperCamelCase ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['ア', 'ッ', 'フ', '゚', 'ル', 'ストア', 'で', 'iPhone', '8', '\u3000', 'が', '\u3000', '\u3000', '\u3000', '発売', 'さ', 'れた', '\u3000', '。'] , ) @require_jumanpp def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = JumanppTokenizer(trim_whitespace=_UpperCamelCase ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れた', '。'] , ) @require_jumanpp def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize('ありがとうございますm(_ _)m見つけるのが大変です。' ) , ['ありがとう', 'ございます', 'm(_ _)m', '見つける', 'の', 'が', '大変です', '。'] , ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = ['[UNK]', '[CLS]', '[SEP]', 'こんにちは', 'こん', 'にちは', 'ばんは', '##こん', '##にちは', '##ばんは'] _UpperCAmelCase = {} for i, token in enumerate(_UpperCamelCase ): _UpperCAmelCase = i _UpperCAmelCase = WordpieceTokenizer(vocab=_UpperCamelCase , unk_token='[UNK]' ) self.assertListEqual(tokenizer.tokenize('' ) , [] ) self.assertListEqual(tokenizer.tokenize('こんにちは' ) , ['こんにちは'] ) self.assertListEqual(tokenizer.tokenize('こんばんは' ) , ['こん', '##ばんは'] ) self.assertListEqual(tokenizer.tokenize('こんばんは こんばんにちは こんにちは' ) , ['こん', '##ばんは', '[UNK]', 'こんにちは'] ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = BertJapaneseTokenizer.from_pretrained('nlp-waseda/roberta-base-japanese-with-auto-jumanpp' ) _UpperCAmelCase = tokenizer.subword_tokenizer _UpperCAmelCase = subword_tokenizer.tokenize('国境 の 長い トンネル を 抜ける と 雪国 であった 。' ) self.assertListEqual(_UpperCamelCase , ['▁国境', '▁の', '▁長い', '▁トンネル', '▁を', '▁抜ける', '▁と', '▁雪', '国', '▁であった', '▁。'] ) _UpperCAmelCase = subword_tokenizer.tokenize('こんばんは こんばん にち は こんにちは' ) self.assertListEqual(_UpperCamelCase , ['▁こん', 'ばん', 'は', '▁こん', 'ばん', '▁に', 'ち', '▁は', '▁こんにちは'] ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.tokenizer_class.from_pretrained('cl-tohoku/bert-base-japanese' ) _UpperCAmelCase = tokenizer.encode('ありがとう。' , add_special_tokens=_UpperCamelCase ) _UpperCAmelCase = tokenizer.encode('どういたしまして。' , add_special_tokens=_UpperCamelCase ) _UpperCAmelCase = tokenizer.build_inputs_with_special_tokens(_UpperCamelCase ) _UpperCAmelCase = tokenizer.build_inputs_with_special_tokens(_UpperCamelCase , _UpperCamelCase ) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class __lowerCamelCase ( __A , unittest.TestCase): """simple docstring""" UpperCamelCase__ = BertJapaneseTokenizer UpperCamelCase__ = False def UpperCamelCase ( self ): """simple docstring""" super().setUp() _UpperCAmelCase = ['[UNK]', '[CLS]', '[SEP]', 'こ', 'ん', 'に', 'ち', 'は', 'ば', '世', '界', '、', '。'] _UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def UpperCamelCase ( self , **UpperCAmelCase ): """simple docstring""" return BertJapaneseTokenizer.from_pretrained(self.tmpdirname , subword_tokenizer_type='character' , **_UpperCamelCase ) def UpperCamelCase ( self , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = 'こんにちは、世界。 \nこんばんは、世界。' _UpperCAmelCase = 'こ ん に ち は 、 世 界 。 こ ん ば ん は 、 世 界 。' return input_text, output_text def UpperCamelCase ( self ): """simple docstring""" pass # TODO add if relevant def UpperCamelCase ( self ): """simple docstring""" pass # TODO add if relevant def UpperCamelCase ( self ): """simple docstring""" pass # TODO add if relevant def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.tokenizer_class(self.vocab_file , subword_tokenizer_type='character' ) _UpperCAmelCase = tokenizer.tokenize('こんにちは、世界。 \nこんばんは、世界。' ) self.assertListEqual( _UpperCamelCase , ['こ', 'ん', 'に', 'ち', 'は', '、', '世', '界', '。', 'こ', 'ん', 'ば', 'ん', 'は', '、', '世', '界', '。'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_UpperCamelCase ) , [3, 4, 5, 6, 7, 11, 9, 10, 12, 3, 4, 8, 4, 7, 11, 9, 10, 12] ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = ['[UNK]', '[CLS]', '[SEP]', 'こ', 'ん', 'に', 'ち', 'は', 'ば', '世', '界', '、', '。'] _UpperCAmelCase = {} for i, token in enumerate(_UpperCamelCase ): _UpperCAmelCase = i _UpperCAmelCase = CharacterTokenizer(vocab=_UpperCamelCase , unk_token='[UNK]' ) self.assertListEqual(tokenizer.tokenize('' ) , [] ) self.assertListEqual(tokenizer.tokenize('こんにちは' ) , ['こ', 'ん', 'に', 'ち', 'は'] ) self.assertListEqual(tokenizer.tokenize('こんにちほ' ) , ['こ', 'ん', 'に', 'ち', '[UNK]'] ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.tokenizer_class.from_pretrained('cl-tohoku/bert-base-japanese-char' ) _UpperCAmelCase = tokenizer.encode('ありがとう。' , add_special_tokens=_UpperCamelCase ) _UpperCAmelCase = tokenizer.encode('どういたしまして。' , add_special_tokens=_UpperCamelCase ) _UpperCAmelCase = tokenizer.build_inputs_with_special_tokens(_UpperCamelCase ) _UpperCAmelCase = tokenizer.build_inputs_with_special_tokens(_UpperCamelCase , _UpperCamelCase ) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class __lowerCamelCase ( unittest.TestCase): """simple docstring""" def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = 'cl-tohoku/bert-base-japanese' _UpperCAmelCase = AutoTokenizer.from_pretrained(_UpperCamelCase ) self.assertIsInstance(_UpperCamelCase , _UpperCamelCase ) class __lowerCamelCase ( unittest.TestCase): """simple docstring""" def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = 'cl-tohoku/bert-base-japanese' with self.assertLogs('transformers' , level='WARNING' ) as cm: BertTokenizer.from_pretrained(_UpperCamelCase ) self.assertTrue( cm.records[0].message.startswith( 'The tokenizer class you load from this checkpoint is not the same type as the class this function' ' is called from.' ) ) _UpperCAmelCase = 'bert-base-cased' with self.assertLogs('transformers' , level='WARNING' ) as cm: BertJapaneseTokenizer.from_pretrained(_UpperCamelCase ) self.assertTrue( cm.records[0].message.startswith( 'The tokenizer class you load from this checkpoint is not the same type as the class this function' ' is called from.' ) )
39
from __future__ import annotations from collections.abc import Generator def __SCREAMING_SNAKE_CASE (): snake_case_ = {} snake_case_ = 2 while True: snake_case_ = factor_map.pop(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if factor: snake_case_ = factor + prime while x in factor_map: x += factor snake_case_ = factor else: snake_case_ = prime yield prime prime += 1 def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = 1E10 ): snake_case_ = sieve() snake_case_ = 1 while True: snake_case_ = next(SCREAMING_SNAKE_CASE__ ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(SCREAMING_SNAKE_CASE__ ) n += 2 if __name__ == "__main__": print(solution())
8
0
from numpy import exp, pi, sqrt def lowerCamelCase__ ( snake_case_ : int , snake_case_ : List[str] = 0.0 , snake_case_ : Any = 1.0 ) -> Tuple: return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
24
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase_ = {'''configuration_opt''': ['''OPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OPTConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''OPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OPTForCausalLM''', '''OPTModel''', '''OPTPreTrainedModel''', '''OPTForSequenceClassification''', '''OPTForQuestionAnswering''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['''TFOPTForCausalLM''', '''TFOPTModel''', '''TFOPTPreTrainedModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''FlaxOPTForCausalLM''', '''FlaxOPTModel''', '''FlaxOPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
8
0
from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class A_ ( nn.Module ): def __init__( self : str ,SCREAMING_SNAKE_CASE__ : int ,SCREAMING_SNAKE_CASE__ : int ,SCREAMING_SNAKE_CASE__ : int ,SCREAMING_SNAKE_CASE__ : str=0.0 ,SCREAMING_SNAKE_CASE__ : Optional[int] = None ,SCREAMING_SNAKE_CASE__ : str = "geglu" ,SCREAMING_SNAKE_CASE__ : Optional[int] = None ,SCREAMING_SNAKE_CASE__ : bool = False ,SCREAMING_SNAKE_CASE__ : bool = False ,SCREAMING_SNAKE_CASE__ : bool = False ,SCREAMING_SNAKE_CASE__ : bool = False ,SCREAMING_SNAKE_CASE__ : bool = True ,SCREAMING_SNAKE_CASE__ : str = "layer_norm" ,SCREAMING_SNAKE_CASE__ : bool = False ,): super().__init__() __lowerCamelCase : List[str] = only_cross_attention __lowerCamelCase : Union[str, Any] = (num_embeds_ada_norm is not None) and norm_type == 'ada_norm_zero' __lowerCamelCase : str = (num_embeds_ada_norm is not None) and norm_type == 'ada_norm' if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( F"`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to" F" define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.") # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: __lowerCamelCase : List[str] = AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase) elif self.use_ada_layer_norm_zero: __lowerCamelCase : str = AdaLayerNormZero(_UpperCamelCase ,_UpperCamelCase) else: __lowerCamelCase : Optional[int] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase) __lowerCamelCase : List[str] = Attention( query_dim=_UpperCamelCase ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if only_cross_attention else None ,upcast_attention=_UpperCamelCase ,) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. __lowerCamelCase : Any = ( AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase) if self.use_ada_layer_norm else nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase) ) __lowerCamelCase : Optional[Any] = Attention( query_dim=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if not double_self_attention else None ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,upcast_attention=_UpperCamelCase ,) # is self-attn if encoder_hidden_states is none else: __lowerCamelCase : Union[str, Any] = None __lowerCamelCase : str = None # 3. Feed-forward __lowerCamelCase : Tuple = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase) __lowerCamelCase : Optional[Any] = FeedForward(_UpperCamelCase ,dropout=_UpperCamelCase ,activation_fn=_UpperCamelCase ,final_dropout=_UpperCamelCase) # let chunk size default to None __lowerCamelCase : Any = None __lowerCamelCase : Optional[Any] = 0 def lowerCAmelCase ( self : str ,SCREAMING_SNAKE_CASE__ : Optional[int] ,SCREAMING_SNAKE_CASE__ : int): # Sets chunk feed-forward __lowerCamelCase : Tuple = chunk_size __lowerCamelCase : int = dim def lowerCAmelCase ( self : Optional[Any] ,SCREAMING_SNAKE_CASE__ : torch.FloatTensor ,SCREAMING_SNAKE_CASE__ : Optional[torch.FloatTensor] = None ,SCREAMING_SNAKE_CASE__ : Optional[torch.FloatTensor] = None ,SCREAMING_SNAKE_CASE__ : Optional[torch.FloatTensor] = None ,SCREAMING_SNAKE_CASE__ : Optional[torch.LongTensor] = None ,SCREAMING_SNAKE_CASE__ : Dict[str, Any] = None ,SCREAMING_SNAKE_CASE__ : Optional[torch.LongTensor] = None ,): # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: __lowerCamelCase : str = self.norma(_UpperCamelCase ,_UpperCamelCase) elif self.use_ada_layer_norm_zero: __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase : str = self.norma( _UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=hidden_states.dtype) else: __lowerCamelCase : Optional[Any] = self.norma(_UpperCamelCase) __lowerCamelCase : List[Any] = cross_attention_kwargs if cross_attention_kwargs is not None else {} __lowerCamelCase : Tuple = self.attna( _UpperCamelCase ,encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) if self.use_ada_layer_norm_zero: __lowerCamelCase : List[Any] = gate_msa.unsqueeze(1) * attn_output __lowerCamelCase : Dict = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: __lowerCamelCase : Dict = ( self.norma(_UpperCamelCase ,_UpperCamelCase) if self.use_ada_layer_norm else self.norma(_UpperCamelCase) ) __lowerCamelCase : str = self.attna( _UpperCamelCase ,encoder_hidden_states=_UpperCamelCase ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) __lowerCamelCase : Optional[Any] = attn_output + hidden_states # 3. Feed-forward __lowerCamelCase : List[str] = self.norma(_UpperCamelCase) if self.use_ada_layer_norm_zero: __lowerCamelCase : str = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( F"`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.") __lowerCamelCase : Tuple = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size __lowerCamelCase : Optional[Any] = torch.cat( [self.ff(_UpperCamelCase) for hid_slice in norm_hidden_states.chunk(_UpperCamelCase ,dim=self._chunk_dim)] ,dim=self._chunk_dim ,) else: __lowerCamelCase : Optional[int] = self.ff(_UpperCamelCase) if self.use_ada_layer_norm_zero: __lowerCamelCase : List[str] = gate_mlp.unsqueeze(1) * ff_output __lowerCamelCase : Optional[int] = ff_output + hidden_states return hidden_states class A_ ( nn.Module ): def __init__( self : Optional[Any] ,SCREAMING_SNAKE_CASE__ : int ,SCREAMING_SNAKE_CASE__ : Optional[int] = None ,SCREAMING_SNAKE_CASE__ : int = 4 ,SCREAMING_SNAKE_CASE__ : float = 0.0 ,SCREAMING_SNAKE_CASE__ : str = "geglu" ,SCREAMING_SNAKE_CASE__ : bool = False ,): super().__init__() __lowerCamelCase : str = int(dim * mult) __lowerCamelCase : Dict = dim_out if dim_out is not None else dim if activation_fn == "gelu": __lowerCamelCase : Tuple = GELU(_UpperCamelCase ,_UpperCamelCase) if activation_fn == "gelu-approximate": __lowerCamelCase : Dict = GELU(_UpperCamelCase ,_UpperCamelCase ,approximate='tanh') elif activation_fn == "geglu": __lowerCamelCase : int = GEGLU(_UpperCamelCase ,_UpperCamelCase) elif activation_fn == "geglu-approximate": __lowerCamelCase : Optional[int] = ApproximateGELU(_UpperCamelCase ,_UpperCamelCase) __lowerCamelCase : Tuple = nn.ModuleList([]) # project in self.net.append(_UpperCamelCase) # project dropout self.net.append(nn.Dropout(_UpperCamelCase)) # project out self.net.append(nn.Linear(_UpperCamelCase ,_UpperCamelCase)) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(_UpperCamelCase)) def lowerCAmelCase ( self : Optional[Any] ,SCREAMING_SNAKE_CASE__ : Union[str, Any]): for module in self.net: __lowerCamelCase : int = module(_UpperCamelCase) return hidden_states class A_ ( nn.Module ): def __init__( self : List[str] ,SCREAMING_SNAKE_CASE__ : int ,SCREAMING_SNAKE_CASE__ : int ,SCREAMING_SNAKE_CASE__ : str = "none"): super().__init__() __lowerCamelCase : Dict = nn.Linear(_UpperCamelCase ,_UpperCamelCase) __lowerCamelCase : int = approximate def lowerCAmelCase ( self : Tuple ,SCREAMING_SNAKE_CASE__ : int): if gate.device.type != "mps": return F.gelu(_UpperCamelCase ,approximate=self.approximate) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa) ,approximate=self.approximate).to(dtype=gate.dtype) def lowerCAmelCase ( self : Any ,SCREAMING_SNAKE_CASE__ : List[str]): __lowerCamelCase : Dict = self.proj(_UpperCamelCase) __lowerCamelCase : List[str] = self.gelu(_UpperCamelCase) return hidden_states class A_ ( nn.Module ): def __init__( self : Tuple ,SCREAMING_SNAKE_CASE__ : int ,SCREAMING_SNAKE_CASE__ : int): super().__init__() __lowerCamelCase : Any = nn.Linear(_UpperCamelCase ,dim_out * 2) def lowerCAmelCase ( self : Union[str, Any] ,SCREAMING_SNAKE_CASE__ : Dict): if gate.device.type != "mps": return F.gelu(_UpperCamelCase) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa)).to(dtype=gate.dtype) def lowerCAmelCase ( self : Optional[Any] ,SCREAMING_SNAKE_CASE__ : Dict): __lowerCamelCase , __lowerCamelCase : Tuple = self.proj(_UpperCamelCase).chunk(2 ,dim=-1) return hidden_states * self.gelu(_UpperCamelCase) class A_ ( nn.Module ): def __init__( self : int ,SCREAMING_SNAKE_CASE__ : int ,SCREAMING_SNAKE_CASE__ : int): super().__init__() __lowerCamelCase : Union[str, Any] = nn.Linear(_UpperCamelCase ,_UpperCamelCase) def lowerCAmelCase ( self : str ,SCREAMING_SNAKE_CASE__ : Optional[int]): __lowerCamelCase : Tuple = self.proj(_UpperCamelCase) return x * torch.sigmoid(1.702 * x) class A_ ( nn.Module ): def __init__( self : str ,SCREAMING_SNAKE_CASE__ : Optional[int] ,SCREAMING_SNAKE_CASE__ : Tuple): super().__init__() __lowerCamelCase : int = nn.Embedding(_UpperCamelCase ,_UpperCamelCase) __lowerCamelCase : int = nn.SiLU() __lowerCamelCase : Union[str, Any] = nn.Linear(_UpperCamelCase ,embedding_dim * 2) __lowerCamelCase : Optional[Any] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase) def lowerCAmelCase ( self : Optional[Any] ,SCREAMING_SNAKE_CASE__ : Any ,SCREAMING_SNAKE_CASE__ : List[str]): __lowerCamelCase : Optional[Any] = self.linear(self.silu(self.emb(_UpperCamelCase))) __lowerCamelCase , __lowerCamelCase : Optional[Any] = torch.chunk(_UpperCamelCase ,2) __lowerCamelCase : Any = self.norm(_UpperCamelCase) * (1 + scale) + shift return x class A_ ( nn.Module ): def __init__( self : int ,SCREAMING_SNAKE_CASE__ : int ,SCREAMING_SNAKE_CASE__ : Any): super().__init__() __lowerCamelCase : Union[str, Any] = CombinedTimestepLabelEmbeddings(_UpperCamelCase ,_UpperCamelCase) __lowerCamelCase : List[Any] = nn.SiLU() __lowerCamelCase : Union[str, Any] = nn.Linear(_UpperCamelCase ,6 * embedding_dim ,bias=_UpperCamelCase) __lowerCamelCase : Union[str, Any] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ,eps=1E-6) def lowerCAmelCase ( self : Tuple ,SCREAMING_SNAKE_CASE__ : Optional[Any] ,SCREAMING_SNAKE_CASE__ : Union[str, Any] ,SCREAMING_SNAKE_CASE__ : Optional[int] ,SCREAMING_SNAKE_CASE__ : Optional[Any]=None): __lowerCamelCase : Union[str, Any] = self.linear(self.silu(self.emb(_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=_UpperCamelCase))) __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase : List[Any] = emb.chunk(6 ,dim=1) __lowerCamelCase : str = self.norm(_UpperCamelCase) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class A_ ( nn.Module ): def __init__( self : List[str] ,SCREAMING_SNAKE_CASE__ : int ,SCREAMING_SNAKE_CASE__ : int ,SCREAMING_SNAKE_CASE__ : int ,SCREAMING_SNAKE_CASE__ : Optional[str] = None ,SCREAMING_SNAKE_CASE__ : float = 1E-5): super().__init__() __lowerCamelCase : Optional[Any] = num_groups __lowerCamelCase : Union[str, Any] = eps if act_fn is None: __lowerCamelCase : Any = None else: __lowerCamelCase : List[Any] = get_activation(_UpperCamelCase) __lowerCamelCase : str = nn.Linear(_UpperCamelCase ,out_dim * 2) def lowerCAmelCase ( self : List[Any] ,SCREAMING_SNAKE_CASE__ : Dict ,SCREAMING_SNAKE_CASE__ : Any): if self.act: __lowerCamelCase : Union[str, Any] = self.act(_UpperCamelCase) __lowerCamelCase : Optional[int] = self.linear(_UpperCamelCase) __lowerCamelCase : Union[str, Any] = emb[:, :, None, None] __lowerCamelCase , __lowerCamelCase : Optional[int] = emb.chunk(2 ,dim=1) __lowerCamelCase : List[str] = F.group_norm(_UpperCamelCase ,self.num_groups ,eps=self.eps) __lowerCamelCase : Dict = x * (1 + scale) + shift return x
73
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer from .base import PipelineTool class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = "philschmid/bart-large-cnn-samsum" SCREAMING_SNAKE_CASE : Tuple = ( "This is a tool that summarizes an English text. It takes an input `text` containing the text to summarize, " "and returns a summary of the text." ) SCREAMING_SNAKE_CASE : str = "summarizer" SCREAMING_SNAKE_CASE : str = AutoTokenizer SCREAMING_SNAKE_CASE : str = AutoModelForSeqaSeqLM SCREAMING_SNAKE_CASE : Optional[int] = ["text"] SCREAMING_SNAKE_CASE : Optional[int] = ["text"] def snake_case__( self : str , _UpperCamelCase : int ) ->Optional[int]: return self.pre_processor(_UpperCamelCase , return_tensors='''pt''' , truncation=_UpperCamelCase ) def snake_case__( self : Tuple , _UpperCamelCase : Optional[int] ) ->Tuple: return self.model.generate(**_UpperCamelCase )[0] def snake_case__( self : Optional[Any] , _UpperCamelCase : Optional[int] ) ->Any: return self.pre_processor.decode(_UpperCamelCase , skip_special_tokens=_UpperCamelCase , clean_up_tokenization_spaces=_UpperCamelCase )
8
0
import warnings from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase = logging.get_logger(__name__) __UpperCAmelCase = { '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/config.json''', '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/config.json''', } class lowerCAmelCase_ ( __A ): UpperCAmelCase__ : List[str] = "xlnet" UpperCAmelCase__ : Union[str, Any] = ["mems"] UpperCAmelCase__ : Tuple = { "n_token": "vocab_size", # Backward compatibility "hidden_size": "d_model", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self, SCREAMING_SNAKE_CASE_=3_2000, SCREAMING_SNAKE_CASE_=1024, SCREAMING_SNAKE_CASE_=24, SCREAMING_SNAKE_CASE_=16, SCREAMING_SNAKE_CASE_=4096, SCREAMING_SNAKE_CASE_="gelu", SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_="bi", SCREAMING_SNAKE_CASE_=0.02, SCREAMING_SNAKE_CASE_=1e-12, SCREAMING_SNAKE_CASE_=0.1, SCREAMING_SNAKE_CASE_=512, SCREAMING_SNAKE_CASE_=None, SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_=False, SCREAMING_SNAKE_CASE_=False, SCREAMING_SNAKE_CASE_=-1, SCREAMING_SNAKE_CASE_=False, SCREAMING_SNAKE_CASE_="last", SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_="tanh", SCREAMING_SNAKE_CASE_=0.1, SCREAMING_SNAKE_CASE_=5, SCREAMING_SNAKE_CASE_=5, SCREAMING_SNAKE_CASE_=5, SCREAMING_SNAKE_CASE_=1, SCREAMING_SNAKE_CASE_=2, **SCREAMING_SNAKE_CASE_, ) -> List[str]: UpperCamelCase : Union[str, Any] = vocab_size UpperCamelCase : List[str] = d_model UpperCamelCase : List[Any] = n_layer UpperCamelCase : Optional[int] = n_head if d_model % n_head != 0: raise ValueError(F"""\'d_model % n_head\' ({d_model % n_head}) should be equal to 0""" ) if "d_head" in kwargs: if kwargs["d_head"] != d_model // n_head: raise ValueError( F"""`d_head` ({kwargs["d_head"]}) should be equal to `d_model // n_head` ({d_model // n_head})""" ) UpperCamelCase : Optional[Any] = d_model // n_head UpperCamelCase : List[str] = ff_activation UpperCamelCase : Union[str, Any] = d_inner UpperCamelCase : str = untie_r UpperCamelCase : List[Any] = attn_type UpperCamelCase : Optional[Any] = initializer_range UpperCamelCase : Optional[Any] = layer_norm_eps UpperCamelCase : Any = dropout UpperCamelCase : str = mem_len UpperCamelCase : List[str] = reuse_len UpperCamelCase : Dict = bi_data UpperCamelCase : Tuple = clamp_len UpperCamelCase : Optional[int] = same_length UpperCamelCase : Union[str, Any] = summary_type UpperCamelCase : Union[str, Any] = summary_use_proj UpperCamelCase : Union[str, Any] = summary_activation UpperCamelCase : str = summary_last_dropout UpperCamelCase : List[Any] = start_n_top UpperCamelCase : Union[str, Any] = end_n_top UpperCamelCase : Union[str, Any] = bos_token_id UpperCamelCase : List[str] = pad_token_id UpperCamelCase : Optional[int] = eos_token_id if "use_cache" in kwargs: warnings.warn( 'The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`' ' instead.', _UpperCamelCase, ) UpperCamelCase : List[Any] = kwargs['use_cache'] UpperCamelCase : Any = use_mems_eval UpperCamelCase : Union[str, Any] = use_mems_train super().__init__(pad_token_id=_UpperCamelCase, bos_token_id=_UpperCamelCase, eos_token_id=_UpperCamelCase, **_UpperCamelCase ) @property def snake_case_ ( self ) -> Union[str, Any]: logger.info(F"""The model {self.model_type} is one of the few models that has no sequence length limit.""" ) return -1 @max_position_embeddings.setter def snake_case_ ( self, SCREAMING_SNAKE_CASE_ ) -> int: # Message copied from Transformer-XL documentation raise NotImplementedError( F"""The model {self.model_type} is one of the few models that has no sequence length limit.""" )
119
from collections import deque from .hash_table import HashTable class snake_case_ ( __A ): '''simple docstring''' def __init__( self : int , *_UpperCamelCase : int , **_UpperCamelCase : Tuple ) ->Tuple: super().__init__(*_UpperCamelCase , **_UpperCamelCase ) def snake_case__( self : Dict , _UpperCamelCase : List[str] , _UpperCamelCase : Dict ) ->Tuple: snake_case_ = deque([] ) if self.values[key] is None else self.values[key] self.values[key].appendleft(_UpperCamelCase ) snake_case_ = self.values[key] def snake_case__( self : List[Any] ) ->str: return ( sum(self.charge_factor - len(_UpperCamelCase ) for slot in self.values ) / self.size_table * self.charge_factor ) def snake_case__( self : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[int]=None ) ->str: if not ( len(self.values[key] ) == self.charge_factor and self.values.count(_UpperCamelCase ) == 0 ): return key return super()._collision_resolution(_UpperCamelCase , _UpperCamelCase )
8
0
def lowerCamelCase_ ( _a : Any ): '''simple docstring''' if not numbers: return 0 if not isinstance(SCREAMING_SNAKE_CASE__ , (list, tuple) ) or not all( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for number in numbers ): raise ValueError("""numbers must be an iterable of integers""" ) UpperCAmelCase_ : Dict = numbers[0] for i in range(1 , len(SCREAMING_SNAKE_CASE__ ) ): # update the maximum and minimum subarray products UpperCAmelCase_ : str = numbers[i] if number < 0: UpperCAmelCase_ , UpperCAmelCase_ : Tuple = min_till_now, max_till_now UpperCAmelCase_ : str = max(SCREAMING_SNAKE_CASE__ , max_till_now * number ) UpperCAmelCase_ : Dict = min(SCREAMING_SNAKE_CASE__ , min_till_now * number ) # update the maximum product found till now UpperCAmelCase_ : Any = max(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return max_prod
345
from __future__ import annotations def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = len(SCREAMING_SNAKE_CASE__ ) # We need to create solution object to save path. snake_case_ = [[0 for _ in range(SCREAMING_SNAKE_CASE__ )] for _ in range(SCREAMING_SNAKE_CASE__ )] snake_case_ = run_maze(SCREAMING_SNAKE_CASE__ , 0 , 0 , SCREAMING_SNAKE_CASE__ ) if solved: print('''\n'''.join(str(SCREAMING_SNAKE_CASE__ ) for row in solutions ) ) else: print('''No solution exists!''' ) return solved def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = len(SCREAMING_SNAKE_CASE__ ) # Final check point. if i == j == (size - 1): snake_case_ = 1 return True snake_case_ = (not i < 0) and (not j < 0) # Check lower bounds snake_case_ = (i < size) and (j < size) # Check upper bounds if lower_flag and upper_flag: # check for already visited and block points. snake_case_ = (not solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited snake_case_ = 1 # check for directions if ( run_maze(SCREAMING_SNAKE_CASE__ , i + 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or run_maze(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , j + 1 , SCREAMING_SNAKE_CASE__ ) or run_maze(SCREAMING_SNAKE_CASE__ , i - 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or run_maze(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , j - 1 , SCREAMING_SNAKE_CASE__ ) ): return True snake_case_ = 0 return False return False if __name__ == "__main__": import doctest doctest.testmod()
8
0
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __lowerCAmelCase : Optional[Any] =logging.get_logger(__name__) __lowerCAmelCase : List[str] ={ 'facebook/wav2vec2-base-960h': 'https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json', # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2 } class _lowercase ( A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Any = '''wav2vec2''' def __init__( self :Any , lowerCAmelCase__ :Optional[Any]=32 , lowerCAmelCase__ :Optional[int]=768 , lowerCAmelCase__ :Tuple=12 , lowerCAmelCase__ :List[str]=12 , lowerCAmelCase__ :Any=3_072 , lowerCAmelCase__ :Dict="gelu" , lowerCAmelCase__ :Dict=0.1 , lowerCAmelCase__ :List[str]=0.1 , lowerCAmelCase__ :str=0.1 , lowerCAmelCase__ :Any=0.0 , lowerCAmelCase__ :Union[str, Any]=0.0 , lowerCAmelCase__ :Optional[Any]=0.1 , lowerCAmelCase__ :Optional[Any]=0.1 , lowerCAmelCase__ :List[Any]=0.02 , lowerCAmelCase__ :Optional[int]=1E-5 , lowerCAmelCase__ :Tuple="group" , lowerCAmelCase__ :Optional[int]="gelu" , lowerCAmelCase__ :Dict=(512, 512, 512, 512, 512, 512, 512) , lowerCAmelCase__ :Any=(5, 2, 2, 2, 2, 2, 2) , lowerCAmelCase__ :Optional[int]=(10, 3, 3, 3, 3, 2, 2) , lowerCAmelCase__ :List[Any]=False , lowerCAmelCase__ :Dict=128 , lowerCAmelCase__ :List[Any]=16 , lowerCAmelCase__ :int=False , lowerCAmelCase__ :List[Any]=True , lowerCAmelCase__ :str=0.05 , lowerCAmelCase__ :Any=10 , lowerCAmelCase__ :Dict=2 , lowerCAmelCase__ :Any=0.0 , lowerCAmelCase__ :Optional[Any]=10 , lowerCAmelCase__ :List[Any]=0 , lowerCAmelCase__ :List[str]=320 , lowerCAmelCase__ :Union[str, Any]=2 , lowerCAmelCase__ :str=0.1 , lowerCAmelCase__ :Union[str, Any]=100 , lowerCAmelCase__ :str=256 , lowerCAmelCase__ :List[str]=256 , lowerCAmelCase__ :Any=0.1 , lowerCAmelCase__ :Tuple="sum" , lowerCAmelCase__ :Dict=False , lowerCAmelCase__ :Dict=False , lowerCAmelCase__ :Union[str, Any]=256 , lowerCAmelCase__ :Optional[Any]=(512, 512, 512, 512, 1_500) , lowerCAmelCase__ :Optional[int]=(5, 3, 3, 1, 1) , lowerCAmelCase__ :Any=(1, 2, 3, 1, 1) , lowerCAmelCase__ :Union[str, Any]=512 , lowerCAmelCase__ :Any=0 , lowerCAmelCase__ :Optional[Any]=1 , lowerCAmelCase__ :Any=2 , lowerCAmelCase__ :int=False , lowerCAmelCase__ :str=3 , lowerCAmelCase__ :Tuple=2 , lowerCAmelCase__ :Dict=3 , lowerCAmelCase__ :int=None , lowerCAmelCase__ :Optional[int]=None , **lowerCAmelCase__ :Any , ) -> List[str]: super().__init__(**lowerCAmelCase__ , pad_token_id=lowerCAmelCase__ , bos_token_id=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : str = hidden_size __SCREAMING_SNAKE_CASE : List[Any] = feat_extract_norm __SCREAMING_SNAKE_CASE : List[str] = feat_extract_activation __SCREAMING_SNAKE_CASE : Optional[int] = list(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[str] = list(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : str = list(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = conv_bias __SCREAMING_SNAKE_CASE : Any = num_conv_pos_embeddings __SCREAMING_SNAKE_CASE : Tuple = num_conv_pos_embedding_groups __SCREAMING_SNAKE_CASE : Optional[int] = len(self.conv_dim ) __SCREAMING_SNAKE_CASE : Tuple = num_hidden_layers __SCREAMING_SNAKE_CASE : Optional[Any] = intermediate_size __SCREAMING_SNAKE_CASE : Any = hidden_act __SCREAMING_SNAKE_CASE : Tuple = num_attention_heads __SCREAMING_SNAKE_CASE : int = hidden_dropout __SCREAMING_SNAKE_CASE : str = attention_dropout __SCREAMING_SNAKE_CASE : Dict = activation_dropout __SCREAMING_SNAKE_CASE : str = feat_proj_dropout __SCREAMING_SNAKE_CASE : Dict = final_dropout __SCREAMING_SNAKE_CASE : Optional[int] = layerdrop __SCREAMING_SNAKE_CASE : Optional[Any] = layer_norm_eps __SCREAMING_SNAKE_CASE : str = initializer_range __SCREAMING_SNAKE_CASE : Tuple = vocab_size __SCREAMING_SNAKE_CASE : int = do_stable_layer_norm __SCREAMING_SNAKE_CASE : Tuple = use_weighted_layer_sum if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' f''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,''' f''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __SCREAMING_SNAKE_CASE : Optional[Any] = apply_spec_augment __SCREAMING_SNAKE_CASE : List[Any] = mask_time_prob __SCREAMING_SNAKE_CASE : str = mask_time_length __SCREAMING_SNAKE_CASE : Any = mask_time_min_masks __SCREAMING_SNAKE_CASE : Optional[int] = mask_feature_prob __SCREAMING_SNAKE_CASE : int = mask_feature_length __SCREAMING_SNAKE_CASE : int = mask_feature_min_masks # parameters for pretraining with codevector quantized representations __SCREAMING_SNAKE_CASE : Dict = num_codevectors_per_group __SCREAMING_SNAKE_CASE : Optional[Any] = num_codevector_groups __SCREAMING_SNAKE_CASE : Optional[int] = contrastive_logits_temperature __SCREAMING_SNAKE_CASE : Any = feat_quantizer_dropout __SCREAMING_SNAKE_CASE : List[Any] = num_negatives __SCREAMING_SNAKE_CASE : Tuple = codevector_dim __SCREAMING_SNAKE_CASE : List[str] = proj_codevector_dim __SCREAMING_SNAKE_CASE : Union[str, Any] = diversity_loss_weight # ctc loss __SCREAMING_SNAKE_CASE : Tuple = ctc_loss_reduction __SCREAMING_SNAKE_CASE : Optional[int] = ctc_zero_infinity # adapter __SCREAMING_SNAKE_CASE : str = add_adapter __SCREAMING_SNAKE_CASE : Union[str, Any] = adapter_kernel_size __SCREAMING_SNAKE_CASE : Any = adapter_stride __SCREAMING_SNAKE_CASE : List[str] = num_adapter_layers __SCREAMING_SNAKE_CASE : List[str] = output_hidden_size or hidden_size __SCREAMING_SNAKE_CASE : int = adapter_attn_dim # SequenceClassification-specific parameter. Feel free to ignore for other classes. __SCREAMING_SNAKE_CASE : Union[str, Any] = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. __SCREAMING_SNAKE_CASE : Optional[int] = list(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Tuple = list(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : str = list(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[str] = xvector_output_dim @property def __magic_name__( self :List[Any] ) -> Any: return functools.reduce(operator.mul , self.conv_stride , 1 )
9
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import VivitImageProcessor class _lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self :Any , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :Tuple=7 , lowerCAmelCase__ :List[Any]=3 , lowerCAmelCase__ :Any=10 , lowerCAmelCase__ :Optional[int]=18 , lowerCAmelCase__ :Dict=30 , lowerCAmelCase__ :Tuple=400 , lowerCAmelCase__ :List[Any]=True , lowerCAmelCase__ :Tuple=None , lowerCAmelCase__ :str=True , lowerCAmelCase__ :List[str]=[0.5, 0.5, 0.5] , lowerCAmelCase__ :List[str]=[0.5, 0.5, 0.5] , lowerCAmelCase__ :Optional[Any]=None , ) -> Optional[Any]: __SCREAMING_SNAKE_CASE : Dict = size if size is not None else {'''shortest_edge''': 18} __SCREAMING_SNAKE_CASE : Optional[int] = crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} __SCREAMING_SNAKE_CASE : Tuple = parent __SCREAMING_SNAKE_CASE : List[Any] = batch_size __SCREAMING_SNAKE_CASE : List[str] = num_channels __SCREAMING_SNAKE_CASE : Union[str, Any] = num_frames __SCREAMING_SNAKE_CASE : Tuple = image_size __SCREAMING_SNAKE_CASE : Optional[Any] = min_resolution __SCREAMING_SNAKE_CASE : Any = max_resolution __SCREAMING_SNAKE_CASE : List[Any] = do_resize __SCREAMING_SNAKE_CASE : Optional[Any] = size __SCREAMING_SNAKE_CASE : Optional[int] = do_normalize __SCREAMING_SNAKE_CASE : List[Any] = image_mean __SCREAMING_SNAKE_CASE : List[str] = image_std __SCREAMING_SNAKE_CASE : str = crop_size def __magic_name__( self :Tuple ) -> Any: return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class _lowercase ( A__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[str] = VivitImageProcessor if is_vision_available() else None def __magic_name__( self :List[str] ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : str = VivitImageProcessingTester(self ) @property def __magic_name__( self :int ) -> Union[str, Any]: return self.image_processor_tester.prepare_image_processor_dict() def __magic_name__( self :List[str] ) -> Optional[int]: __SCREAMING_SNAKE_CASE : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCAmelCase__ , '''image_mean''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''image_std''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_normalize''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_resize''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_center_crop''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''size''' ) ) def __magic_name__( self :Optional[Any] ) -> Optional[int]: __SCREAMING_SNAKE_CASE : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 18} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) __SCREAMING_SNAKE_CASE : Tuple = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42} ) self.assertEqual(image_processor.crop_size , {'''height''': 84, '''width''': 84} ) def __magic_name__( self :List[Any] ) -> Union[str, Any]: # Initialize image_processing __SCREAMING_SNAKE_CASE : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PIL videos __SCREAMING_SNAKE_CASE : List[Any] = prepare_video_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ ) for video in video_inputs: self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertIsInstance(video[0] , Image.Image ) # Test not batched input __SCREAMING_SNAKE_CASE : List[str] = image_processing(video_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched __SCREAMING_SNAKE_CASE : Optional[Any] = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __magic_name__( self :str ) -> int: # Initialize image_processing __SCREAMING_SNAKE_CASE : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __SCREAMING_SNAKE_CASE : List[str] = prepare_video_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , numpify=lowerCAmelCase__ ) for video in video_inputs: self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertIsInstance(video[0] , np.ndarray ) # Test not batched input __SCREAMING_SNAKE_CASE : Optional[int] = image_processing(video_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched __SCREAMING_SNAKE_CASE : Any = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __magic_name__( self :Any ) -> List[str]: # Initialize image_processing __SCREAMING_SNAKE_CASE : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __SCREAMING_SNAKE_CASE : Optional[int] = prepare_video_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , torchify=lowerCAmelCase__ ) for video in video_inputs: self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertIsInstance(video[0] , torch.Tensor ) # Test not batched input __SCREAMING_SNAKE_CASE : Tuple = image_processing(video_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched __SCREAMING_SNAKE_CASE : Optional[int] = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , )
9
1
import numpy as np from scipy.spatial.distance import cdist from sklearn.metrics import fa_score import datasets __lowerCAmelCase : Optional[int] ='\\n @inproceedings{kakwani2020indicnlpsuite,\n title={{IndicNLPSuite: Monolingual Corpora, Evaluation Benchmarks and Pre-trained Multilingual Language Models for Indian Languages}},\n author={Divyanshu Kakwani and Anoop Kunchukuttan and Satish Golla and Gokul N.C. and Avik Bhattacharyya and Mitesh M. Khapra and Pratyush Kumar},\n year={2020},\n booktitle={Findings of EMNLP},\n}\n' __lowerCAmelCase : Optional[Any] ='\\n IndicGLUE is a natural language understanding benchmark for Indian languages. It contains a wide\n variety of tasks and covers 11 major Indian languages - as, bn, gu, hi, kn, ml, mr, or, pa, ta, te.\n' __lowerCAmelCase : Dict ='\nCompute IndicGLUE evaluation metric associated to each IndicGLUE dataset.\nArgs:\n predictions: list of predictions to score (as int64),\n except for \'cvit-mkb-clsr\' where each prediction is a vector (of float32).\n references: list of ground truth labels corresponding to the predictions (as int64),\n except for \'cvit-mkb-clsr\' where each reference is a vector (of float32).\nReturns: depending on the IndicGLUE subset, one or several of:\n "accuracy": Accuracy\n "f1": F1 score\n "precision": Precision@10\nExamples:\n\n >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'wnli\') # \'wnli\' or any of ["copa", "sna", "csqa", "wstp", "inltkh", "bbca", "iitp-mr", "iitp-pr", "actsa-sc", "md"]\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0}\n\n >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'wiki-ner\')\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0, \'f1\': 1.0}\n\n >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'cvit-mkb-clsr\')\n >>> references = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n >>> predictions = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'precision@10\': 1.0}\n\n' def _UpperCamelCase ( lowercase__ , lowercase__ ): return float((preds == labels).mean() ) def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Optional[Any] = simple_accuracy(lowercase__ , lowercase__ ) __SCREAMING_SNAKE_CASE : List[str] = float(fa_score(y_true=lowercase__ , y_pred=lowercase__ ) ) return { "accuracy": acc, "f1": fa, } def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Optional[int] = np.array(lowercase__ ) __SCREAMING_SNAKE_CASE : str = np.array(lowercase__ ) __SCREAMING_SNAKE_CASE : str = en_sentvecs.shape[0] # mean centering __SCREAMING_SNAKE_CASE : Tuple = en_sentvecs - np.mean(lowercase__ , axis=0 ) __SCREAMING_SNAKE_CASE : Optional[int] = in_sentvecs - np.mean(lowercase__ , axis=0 ) __SCREAMING_SNAKE_CASE : str = cdist(lowercase__ , lowercase__ , '''cosine''' ) __SCREAMING_SNAKE_CASE : int = np.array(range(lowercase__ ) ) __SCREAMING_SNAKE_CASE : Optional[Any] = sim.argsort(axis=1 )[:, :10] __SCREAMING_SNAKE_CASE : str = np.any(preds == actual[:, None] , axis=1 ) return float(matches.mean() ) @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _lowercase ( datasets.Metric ): '''simple docstring''' def __magic_name__( self :Tuple ) -> Tuple: if self.config_name not in [ "wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", "cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", "wiki-ner", ]: raise KeyError( '''You should supply a configuration name selected in ''' '''["wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", ''' '''"cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", ''' '''"wiki-ner"]''' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''int64''' ) if self.config_name != '''cvit-mkb-clsr''' else datasets.Sequence(datasets.Value('''float32''' ) ), '''references''': datasets.Value('''int64''' ) if self.config_name != '''cvit-mkb-clsr''' else datasets.Sequence(datasets.Value('''float32''' ) ), } ) , codebase_urls=[] , reference_urls=[] , format='''numpy''' if self.config_name != '''cvit-mkb-clsr''' else None , ) def __magic_name__( self :List[str] , lowerCAmelCase__ :Optional[Any] , lowerCAmelCase__ :Tuple ) -> str: if self.config_name == "cvit-mkb-clsr": return {"precision@10": precision_at_aa(lowerCAmelCase__ , lowerCAmelCase__ )} elif self.config_name in ["wiki-ner"]: return acc_and_fa(lowerCAmelCase__ , lowerCAmelCase__ ) elif self.config_name in [ "wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", "iitp-mr", "iitp-pr", "actsa-sc", "md", ]: return {"accuracy": simple_accuracy(lowerCAmelCase__ , lowerCAmelCase__ )} else: raise KeyError( '''You should supply a configuration name selected in ''' '''["wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", ''' '''"cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", ''' '''"wiki-ner"]''' )
9
import unittest from transformers.testing_utils import require_bsa from transformers.utils import is_bsa_available from ...test_feature_extraction_common import FeatureExtractionSavingTestMixin if is_bsa_available(): from transformers import MarkupLMFeatureExtractor class _lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self :Optional[Any] , lowerCAmelCase__ :Optional[Any] ) -> str: __SCREAMING_SNAKE_CASE : Optional[Any] = parent def __magic_name__( self :List[Any] ) -> Tuple: return {} def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : Optional[Any] = '''<HTML> <HEAD> <TITLE>sample document</TITLE> </HEAD> <BODY BGCOLOR="FFFFFF"> <HR> <a href="http://google.com">Goog</a> <H1>This is one header</H1> <H2>This is a another Header</H2> <P>Travel from <P> <B>SFO to JFK</B> <BR> <B><I>on May 2, 2015 at 2:00 pm. For details go to confirm.com </I></B> <HR> <div style="color:#0000FF"> <h3>Traveler <b> name </b> is <p> John Doe </p> </div>''' __SCREAMING_SNAKE_CASE : str = ''' <!DOCTYPE html> <html> <body> <h1>My First Heading</h1> <p>My first paragraph.</p> </body> </html> ''' return [html_string_a, html_string_a] @require_bsa class _lowercase ( A__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = MarkupLMFeatureExtractor if is_bsa_available() else None def __magic_name__( self :int ) -> Optional[Any]: __SCREAMING_SNAKE_CASE : Optional[Any] = MarkupLMFeatureExtractionTester(self ) @property def __magic_name__( self :Any ) -> Optional[Any]: return self.feature_extract_tester.prepare_feat_extract_dict() def __magic_name__( self :Optional[int] ) -> Any: # Initialize feature_extractor __SCREAMING_SNAKE_CASE : int = self.feature_extraction_class() # Test not batched input __SCREAMING_SNAKE_CASE : Tuple = get_html_strings()[0] __SCREAMING_SNAKE_CASE : Dict = feature_extractor(lowerCAmelCase__ ) # fmt: off __SCREAMING_SNAKE_CASE : str = [['''sample document''', '''Goog''', '''This is one header''', '''This is a another Header''', '''Travel from''', '''SFO to JFK''', '''on May 2, 2015 at 2:00 pm. For details go to confirm.com''', '''Traveler''', '''name''', '''is''', '''John Doe''']] __SCREAMING_SNAKE_CASE : List[str] = [['''/html/head/title''', '''/html/body/a''', '''/html/body/h1''', '''/html/body/h2''', '''/html/body/p''', '''/html/body/p/p/b[1]''', '''/html/body/p/p/b[2]/i''', '''/html/body/p/p/div/h3''', '''/html/body/p/p/div/h3/b''', '''/html/body/p/p/div/h3''', '''/html/body/p/p/div/h3/p''']] # fmt: on self.assertEqual(encoding.nodes , lowerCAmelCase__ ) self.assertEqual(encoding.xpaths , lowerCAmelCase__ ) # Test batched __SCREAMING_SNAKE_CASE : Tuple = get_html_strings() __SCREAMING_SNAKE_CASE : Dict = feature_extractor(lowerCAmelCase__ ) # fmt: off __SCREAMING_SNAKE_CASE : int = expected_nodes + [['''My First Heading''', '''My first paragraph.''']] __SCREAMING_SNAKE_CASE : str = expected_xpaths + [['''/html/body/h1''', '''/html/body/p''']] self.assertEqual(len(encoding.nodes ) , 2 ) self.assertEqual(len(encoding.xpaths ) , 2 ) self.assertEqual(encoding.nodes , lowerCAmelCase__ ) self.assertEqual(encoding.xpaths , lowerCAmelCase__ )
9
1
import argparse import datetime def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : Union[str, Any] = { '''0''': '''Sunday''', '''1''': '''Monday''', '''2''': '''Tuesday''', '''3''': '''Wednesday''', '''4''': '''Thursday''', '''5''': '''Friday''', '''6''': '''Saturday''', } __SCREAMING_SNAKE_CASE : Dict = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 0} # Validate if not 0 < len(lowercase__ ) < 11: raise ValueError('''Must be 10 characters long''' ) # Get month __SCREAMING_SNAKE_CASE : int = int(date_input[0] + date_input[1] ) # Validate if not 0 < m < 13: raise ValueError('''Month must be between 1 - 12''' ) __SCREAMING_SNAKE_CASE : str = date_input[2] # Validate if sep_a not in ["-", "/"]: raise ValueError('''Date separator must be \'-\' or \'/\'''' ) # Get day __SCREAMING_SNAKE_CASE : int = int(date_input[3] + date_input[4] ) # Validate if not 0 < d < 32: raise ValueError('''Date must be between 1 - 31''' ) # Get second separator __SCREAMING_SNAKE_CASE : str = date_input[5] # Validate if sep_a not in ["-", "/"]: raise ValueError('''Date separator must be \'-\' or \'/\'''' ) # Get year __SCREAMING_SNAKE_CASE : int = int(date_input[6] + date_input[7] + date_input[8] + date_input[9] ) # Arbitrary year range if not 45 < y < 8500: raise ValueError( '''Year out of range. There has to be some sort of limit...right?''' ) # Get datetime obj for validation __SCREAMING_SNAKE_CASE : List[str] = datetime.date(int(lowercase__ ) , int(lowercase__ ) , int(lowercase__ ) ) # Start math if m <= 2: __SCREAMING_SNAKE_CASE : Dict = y - 1 __SCREAMING_SNAKE_CASE : List[str] = m + 12 # maths var __SCREAMING_SNAKE_CASE : int = int(str(lowercase__ )[:2] ) __SCREAMING_SNAKE_CASE : int = int(str(lowercase__ )[2:] ) __SCREAMING_SNAKE_CASE : int = int(2.6 * m - 5.39 ) __SCREAMING_SNAKE_CASE : int = int(c / 4 ) __SCREAMING_SNAKE_CASE : int = int(k / 4 ) __SCREAMING_SNAKE_CASE : int = int(d + k ) __SCREAMING_SNAKE_CASE : int = int(t + u + v + x ) __SCREAMING_SNAKE_CASE : int = int(z - (2 * c) ) __SCREAMING_SNAKE_CASE : int = round(w % 7 ) # End math # Validate math if f != convert_datetime_days[dt_ck.weekday()]: raise AssertionError('''The date was evaluated incorrectly. Contact developer.''' ) # Response __SCREAMING_SNAKE_CASE : str = F'''Your date {date_input}, is a {days[str(lowercase__ )]}!''' return response if __name__ == "__main__": import doctest doctest.testmod() __lowerCAmelCase : int =argparse.ArgumentParser( description=( 'Find out what day of the week nearly any date is or was. Enter ' 'date as a string in the mm-dd-yyyy or mm/dd/yyyy format' ) ) parser.add_argument( 'date_input', type=str, help='Date as a string (mm-dd-yyyy or mm/dd/yyyy)' ) __lowerCAmelCase : int =parser.parse_args() zeller(args.date_input)
9
import os import unittest from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class _lowercase ( A__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = TransfoXLTokenizer SCREAMING_SNAKE_CASE__ : int = False SCREAMING_SNAKE_CASE__ : Optional[Any] = False def __magic_name__( self :str ) -> Dict: super().setUp() __SCREAMING_SNAKE_CASE : List[str] = [ '''<unk>''', '''[CLS]''', '''[SEP]''', '''want''', '''unwanted''', '''wa''', '''un''', '''running''', ''',''', '''low''', '''l''', ] __SCREAMING_SNAKE_CASE : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) def __magic_name__( self :Any , **lowerCAmelCase__ :int ) -> str: __SCREAMING_SNAKE_CASE : Optional[Any] = True return TransfoXLTokenizer.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def __magic_name__( self :Union[str, Any] , lowerCAmelCase__ :List[Any] ) -> Tuple: __SCREAMING_SNAKE_CASE : Dict = '''<unk> UNwanted , running''' __SCREAMING_SNAKE_CASE : List[str] = '''<unk> unwanted, running''' return input_text, output_text def __magic_name__( self :Any ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : int = TransfoXLTokenizer(vocab_file=self.vocab_file , lower_case=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : int = tokenizer.tokenize('''<unk> UNwanted , running''' ) self.assertListEqual(lowerCAmelCase__ , ['''<unk>''', '''unwanted''', ''',''', '''running'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase__ ) , [0, 4, 8, 7] ) def __magic_name__( self :Tuple ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : Optional[int] = TransfoXLTokenizer(lower_case=lowerCAmelCase__ ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo ! how \n Are yoU ? ''' ) , ['''hello''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) def __magic_name__( self :Tuple ) -> List[Any]: __SCREAMING_SNAKE_CASE : Union[str, Any] = TransfoXLTokenizer(lower_case=lowerCAmelCase__ ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo ! how \n Are yoU ? ''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __magic_name__( self :Dict ) -> List[Any]: __SCREAMING_SNAKE_CASE : List[str] = TransfoXLTokenizer(lower_case=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = '''Hello (bracket) and side-scrolled [and] Henry\'s $5,000 with 3.34 m. What\'s up!?''' __SCREAMING_SNAKE_CASE : Optional[int] = [ '''Hello''', '''(''', '''bracket''', ''')''', '''and''', '''side''', '''@-@''', '''scrolled''', '''[''', '''and''', ''']''', '''Henry''', '''\'s''', '''$''', '''5''', '''@,@''', '''000''', '''with''', '''3''', '''@.@''', '''34''', '''m''', '''.''', '''What''', '''\'s''', '''up''', '''!''', '''?''', ] self.assertListEqual(tokenizer.tokenize(lowerCAmelCase__ ) , lowerCAmelCase__ ) self.assertEqual(tokenizer.convert_tokens_to_string(lowerCAmelCase__ ) , lowerCAmelCase__ ) def __magic_name__( self :str ) -> int: __SCREAMING_SNAKE_CASE : Union[str, Any] = self.get_tokenizer() __SCREAMING_SNAKE_CASE : Any = len(lowerCAmelCase__ ) tokenizer.add_tokens(['''new1''', '''new2'''] ) tokenizer.move_added_token('''new1''' , 1 ) # Check that moved token is not copied (duplicate) self.assertEqual(len(lowerCAmelCase__ ) , original_len + 2 ) # Check that token is moved to specified id self.assertEqual(tokenizer.encode('''new1''' ) , [1] ) self.assertEqual(tokenizer.decode([1] ) , '''new1''' )
9
1
import numpy as np from cva import COLOR_BGR2GRAY, cvtColor, imread from numpy import array, uinta from PIL import Image from digital_image_processing import change_contrast as cc from digital_image_processing import convert_to_negative as cn from digital_image_processing import sepia as sp from digital_image_processing.dithering import burkes as bs from digital_image_processing.edge_detection import canny from digital_image_processing.filters import convolve as conv from digital_image_processing.filters import gaussian_filter as gg from digital_image_processing.filters import local_binary_pattern as lbp from digital_image_processing.filters import median_filter as med from digital_image_processing.filters import sobel_filter as sob from digital_image_processing.resize import resize as rs __lowerCAmelCase : Union[str, Any] =imread(r'digital_image_processing/image_data/lena_small.jpg') __lowerCAmelCase : List[Any] =cvtColor(img, COLOR_BGR2GRAY) def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : Optional[Any] = cn.convert_to_negative(lowercase__ ) # assert negative_img array for at least one True assert negative_img.any() def _UpperCamelCase ( ): with Image.open('''digital_image_processing/image_data/lena_small.jpg''' ) as img: # Work around assertion for response assert str(cc.change_contrast(lowercase__ , 110 ) ).startswith( '''<PIL.Image.Image image mode=RGB size=100x100 at''' ) def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : List[Any] = canny.gen_gaussian_kernel(9 , sigma=1.4 ) # Assert ambiguous array assert resp.all() def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : str = imread('''digital_image_processing/image_data/lena_small.jpg''' , 0 ) # assert ambiguous array for all == True assert canny_img.all() __SCREAMING_SNAKE_CASE : Dict = canny.canny(lowercase__ ) # assert canny array for at least one True assert canny_array.any() def _UpperCamelCase ( ): assert gg.gaussian_filter(lowercase__ , 5 , sigma=0.9 ).all() def _UpperCamelCase ( ): # laplace diagonals __SCREAMING_SNAKE_CASE : List[Any] = array([[0.25, 0.5, 0.25], [0.5, -3, 0.5], [0.25, 0.5, 0.25]] ) __SCREAMING_SNAKE_CASE : Tuple = conv.img_convolve(lowercase__ , lowercase__ ).astype(lowercase__ ) assert res.any() def _UpperCamelCase ( ): assert med.median_filter(lowercase__ , 3 ).any() def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : str = sob.sobel_filter(lowercase__ ) assert grad.any() and theta.any() def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : List[str] = sp.make_sepia(lowercase__ , 20 ) assert sepia.all() def _UpperCamelCase ( lowercase__ = "digital_image_processing/image_data/lena_small.jpg" ): __SCREAMING_SNAKE_CASE : List[str] = bs.Burkes(imread(lowercase__ , 1 ) , 120 ) burkes.process() assert burkes.output_img.any() def _UpperCamelCase ( lowercase__ = "digital_image_processing/image_data/lena_small.jpg" , ): __SCREAMING_SNAKE_CASE : Union[str, Any] = rs.NearestNeighbour(imread(lowercase__ , 1 ) , 400 , 200 ) nn.process() assert nn.output.any() def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : Union[str, Any] = '''digital_image_processing/image_data/lena.jpg''' # Reading the image and converting it to grayscale. __SCREAMING_SNAKE_CASE : Dict = imread(lowercase__ , 0 ) # Test for get_neighbors_pixel function() return not None __SCREAMING_SNAKE_CASE : List[str] = 0 __SCREAMING_SNAKE_CASE : Optional[int] = 0 __SCREAMING_SNAKE_CASE : str = image[x_coordinate][y_coordinate] __SCREAMING_SNAKE_CASE : Any = lbp.get_neighbors_pixel( lowercase__ , lowercase__ , lowercase__ , lowercase__ ) assert neighbors_pixels is not None # Test for local_binary_pattern function() # Create a numpy array as the same height and width of read image __SCREAMING_SNAKE_CASE : Dict = np.zeros((image.shape[0], image.shape[1]) ) # Iterating through the image and calculating the local binary pattern value # for each pixel. for i in range(0 , image.shape[0] ): for j in range(0 , image.shape[1] ): __SCREAMING_SNAKE_CASE : Optional[Any] = lbp.local_binary_value(lowercase__ , lowercase__ , lowercase__ ) assert lbp_image.any()
9
def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__=False ): if isinstance(lowercase__ , lowercase__ ) and isinstance(lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : List[str] = len(set_a.intersection(lowercase__ ) ) if alternative_union: __SCREAMING_SNAKE_CASE : int = len(lowercase__ ) + len(lowercase__ ) else: __SCREAMING_SNAKE_CASE : int = len(set_a.union(lowercase__ ) ) return intersection / union if isinstance(lowercase__ , (list, tuple) ) and isinstance(lowercase__ , (list, tuple) ): __SCREAMING_SNAKE_CASE : Dict = [element for element in set_a if element in set_b] if alternative_union: __SCREAMING_SNAKE_CASE : Optional[int] = len(lowercase__ ) + len(lowercase__ ) return len(lowercase__ ) / union else: __SCREAMING_SNAKE_CASE : Tuple = set_a + [element for element in set_b if element not in set_a] return len(lowercase__ ) / len(lowercase__ ) return len(lowercase__ ) / len(lowercase__ ) return None if __name__ == "__main__": __lowerCAmelCase : List[Any] ={'a', 'b', 'c', 'd', 'e'} __lowerCAmelCase : Optional[Any] ={'c', 'd', 'e', 'f', 'h', 'i'} print(jaccard_similarity(set_a, set_b))
9
1
def _UpperCamelCase ( ): return [ a * b * (1000 - a - b) for a in range(1 , 999 ) for b in range(lowercase__ , 999 ) if (a * a + b * b == (1000 - a - b) ** 2) ][0] if __name__ == "__main__": print(f"""{solution() = }""")
9
import numpy as np from scipy.spatial.distance import cdist from sklearn.metrics import fa_score import datasets __lowerCAmelCase : Optional[int] ='\\n @inproceedings{kakwani2020indicnlpsuite,\n title={{IndicNLPSuite: Monolingual Corpora, Evaluation Benchmarks and Pre-trained Multilingual Language Models for Indian Languages}},\n author={Divyanshu Kakwani and Anoop Kunchukuttan and Satish Golla and Gokul N.C. and Avik Bhattacharyya and Mitesh M. Khapra and Pratyush Kumar},\n year={2020},\n booktitle={Findings of EMNLP},\n}\n' __lowerCAmelCase : Optional[Any] ='\\n IndicGLUE is a natural language understanding benchmark for Indian languages. It contains a wide\n variety of tasks and covers 11 major Indian languages - as, bn, gu, hi, kn, ml, mr, or, pa, ta, te.\n' __lowerCAmelCase : Dict ='\nCompute IndicGLUE evaluation metric associated to each IndicGLUE dataset.\nArgs:\n predictions: list of predictions to score (as int64),\n except for \'cvit-mkb-clsr\' where each prediction is a vector (of float32).\n references: list of ground truth labels corresponding to the predictions (as int64),\n except for \'cvit-mkb-clsr\' where each reference is a vector (of float32).\nReturns: depending on the IndicGLUE subset, one or several of:\n "accuracy": Accuracy\n "f1": F1 score\n "precision": Precision@10\nExamples:\n\n >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'wnli\') # \'wnli\' or any of ["copa", "sna", "csqa", "wstp", "inltkh", "bbca", "iitp-mr", "iitp-pr", "actsa-sc", "md"]\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0}\n\n >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'wiki-ner\')\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0, \'f1\': 1.0}\n\n >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'cvit-mkb-clsr\')\n >>> references = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n >>> predictions = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'precision@10\': 1.0}\n\n' def _UpperCamelCase ( lowercase__ , lowercase__ ): return float((preds == labels).mean() ) def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Optional[Any] = simple_accuracy(lowercase__ , lowercase__ ) __SCREAMING_SNAKE_CASE : List[str] = float(fa_score(y_true=lowercase__ , y_pred=lowercase__ ) ) return { "accuracy": acc, "f1": fa, } def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Optional[int] = np.array(lowercase__ ) __SCREAMING_SNAKE_CASE : str = np.array(lowercase__ ) __SCREAMING_SNAKE_CASE : str = en_sentvecs.shape[0] # mean centering __SCREAMING_SNAKE_CASE : Tuple = en_sentvecs - np.mean(lowercase__ , axis=0 ) __SCREAMING_SNAKE_CASE : Optional[int] = in_sentvecs - np.mean(lowercase__ , axis=0 ) __SCREAMING_SNAKE_CASE : str = cdist(lowercase__ , lowercase__ , '''cosine''' ) __SCREAMING_SNAKE_CASE : int = np.array(range(lowercase__ ) ) __SCREAMING_SNAKE_CASE : Optional[Any] = sim.argsort(axis=1 )[:, :10] __SCREAMING_SNAKE_CASE : str = np.any(preds == actual[:, None] , axis=1 ) return float(matches.mean() ) @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _lowercase ( datasets.Metric ): '''simple docstring''' def __magic_name__( self :Tuple ) -> Tuple: if self.config_name not in [ "wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", "cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", "wiki-ner", ]: raise KeyError( '''You should supply a configuration name selected in ''' '''["wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", ''' '''"cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", ''' '''"wiki-ner"]''' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''int64''' ) if self.config_name != '''cvit-mkb-clsr''' else datasets.Sequence(datasets.Value('''float32''' ) ), '''references''': datasets.Value('''int64''' ) if self.config_name != '''cvit-mkb-clsr''' else datasets.Sequence(datasets.Value('''float32''' ) ), } ) , codebase_urls=[] , reference_urls=[] , format='''numpy''' if self.config_name != '''cvit-mkb-clsr''' else None , ) def __magic_name__( self :List[str] , lowerCAmelCase__ :Optional[Any] , lowerCAmelCase__ :Tuple ) -> str: if self.config_name == "cvit-mkb-clsr": return {"precision@10": precision_at_aa(lowerCAmelCase__ , lowerCAmelCase__ )} elif self.config_name in ["wiki-ner"]: return acc_and_fa(lowerCAmelCase__ , lowerCAmelCase__ ) elif self.config_name in [ "wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", "iitp-mr", "iitp-pr", "actsa-sc", "md", ]: return {"accuracy": simple_accuracy(lowerCAmelCase__ , lowerCAmelCase__ )} else: raise KeyError( '''You should supply a configuration name selected in ''' '''["wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", ''' '''"cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", ''' '''"wiki-ner"]''' )
9
1
def _UpperCamelCase ( lowercase__ ): if not isinstance(lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : List[str] = F'''Input value of [number={number}] must be an integer''' raise TypeError(lowercase__ ) if number < 0: return False __SCREAMING_SNAKE_CASE : Any = number * number while number > 0: if number % 10 != number_square % 10: return False number //= 10 number_square //= 10 return True if __name__ == "__main__": import doctest doctest.testmod()
9
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging __lowerCAmelCase : Dict =logging.get_logger(__name__) def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : List[Any] = nn.functional.normalize(lowercase__ ) __SCREAMING_SNAKE_CASE : Tuple = nn.functional.normalize(lowercase__ ) return torch.mm(lowercase__ , normalized_text_embeds.t() ) class _lowercase ( A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[Any] = CLIPConfig SCREAMING_SNAKE_CASE__ : List[str] = ['''CLIPEncoderLayer'''] def __init__( self :str , lowerCAmelCase__ :CLIPConfig ) -> Tuple: super().__init__(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = CLIPVisionModel(config.vision_config ) __SCREAMING_SNAKE_CASE : Union[str, Any] = nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Any = nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = nn.Parameter(torch.ones(17 ) , requires_grad=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : str = nn.Parameter(torch.ones(3 ) , requires_grad=lowerCAmelCase__ ) @torch.no_grad() def __magic_name__( self :Optional[int] , lowerCAmelCase__ :int , lowerCAmelCase__ :Optional[int] ) -> Dict: __SCREAMING_SNAKE_CASE : int = self.vision_model(lowerCAmelCase__ )[1] # pooled_output __SCREAMING_SNAKE_CASE : Optional[Any] = self.visual_projection(lowerCAmelCase__ ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 __SCREAMING_SNAKE_CASE : Optional[Any] = cosine_distance(lowerCAmelCase__ , self.special_care_embeds ).cpu().float().numpy() __SCREAMING_SNAKE_CASE : Optional[Any] = cosine_distance(lowerCAmelCase__ , self.concept_embeds ).cpu().float().numpy() __SCREAMING_SNAKE_CASE : List[Any] = [] __SCREAMING_SNAKE_CASE : List[Any] = image_embeds.shape[0] for i in range(lowerCAmelCase__ ): __SCREAMING_SNAKE_CASE : Optional[int] = {'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images __SCREAMING_SNAKE_CASE : List[Any] = 0.0 for concept_idx in range(len(special_cos_dist[0] ) ): __SCREAMING_SNAKE_CASE : List[str] = special_cos_dist[i][concept_idx] __SCREAMING_SNAKE_CASE : Any = self.special_care_embeds_weights[concept_idx].item() __SCREAMING_SNAKE_CASE : Union[str, Any] = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} ) __SCREAMING_SNAKE_CASE : Union[str, Any] = 0.01 for concept_idx in range(len(cos_dist[0] ) ): __SCREAMING_SNAKE_CASE : int = cos_dist[i][concept_idx] __SCREAMING_SNAKE_CASE : Union[str, Any] = self.concept_embeds_weights[concept_idx].item() __SCREAMING_SNAKE_CASE : Tuple = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(lowerCAmelCase__ ) result.append(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[str] = [len(res['''bad_concepts'''] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def __magic_name__( self :Optional[int] , lowerCAmelCase__ :torch.FloatTensor , lowerCAmelCase__ :torch.FloatTensor ) -> Any: __SCREAMING_SNAKE_CASE : Optional[Any] = self.vision_model(lowerCAmelCase__ )[1] # pooled_output __SCREAMING_SNAKE_CASE : List[str] = self.visual_projection(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = cosine_distance(lowerCAmelCase__ , self.special_care_embeds ) __SCREAMING_SNAKE_CASE : Optional[int] = cosine_distance(lowerCAmelCase__ , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images __SCREAMING_SNAKE_CASE : List[Any] = 0.0 __SCREAMING_SNAKE_CASE : Union[str, Any] = special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) __SCREAMING_SNAKE_CASE : List[str] = torch.any(special_scores > 0 , dim=1 ) __SCREAMING_SNAKE_CASE : List[str] = special_care * 0.01 __SCREAMING_SNAKE_CASE : int = special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) __SCREAMING_SNAKE_CASE : Optional[int] = (cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) __SCREAMING_SNAKE_CASE : Any = torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
9
1
from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , lowercase__ , ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : List[str] = coefficient_matrix.shape __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : List[Any] = constant_matrix.shape if rowsa != colsa: __SCREAMING_SNAKE_CASE : int = F'''Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}''' raise ValueError(lowercase__ ) if colsa != 1: __SCREAMING_SNAKE_CASE : str = F'''Constant matrix must be nx1 but received {rowsa}x{colsa}''' raise ValueError(lowercase__ ) if rowsa != rowsa: __SCREAMING_SNAKE_CASE : List[str] = ( '''Coefficient and constant matrices dimensions must be nxn and nx1 but ''' F'''received {rowsa}x{colsa} and {rowsa}x{colsa}''' ) raise ValueError(lowercase__ ) if len(lowercase__ ) != rowsa: __SCREAMING_SNAKE_CASE : Tuple = ( '''Number of initial values must be equal to number of rows in coefficient ''' F'''matrix but received {len(lowercase__ )} and {rowsa}''' ) raise ValueError(lowercase__ ) if iterations <= 0: raise ValueError('''Iterations must be at least 1''' ) __SCREAMING_SNAKE_CASE : NDArray[floataa] = np.concatenate( (coefficient_matrix, constant_matrix) , axis=1 ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : List[Any] = table.shape strictly_diagonally_dominant(lowercase__ ) # Iterates the whole matrix for given number of times for _ in range(lowercase__ ): __SCREAMING_SNAKE_CASE : Union[str, Any] = [] for row in range(lowercase__ ): __SCREAMING_SNAKE_CASE : int = 0 for col in range(lowercase__ ): if col == row: __SCREAMING_SNAKE_CASE : List[Any] = table[row][col] elif col == cols - 1: __SCREAMING_SNAKE_CASE : int = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] __SCREAMING_SNAKE_CASE : str = (temp + val) / denom new_val.append(lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = new_val return [float(lowercase__ ) for i in new_val] def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Optional[int] = table.shape __SCREAMING_SNAKE_CASE : Dict = True for i in range(0 , lowercase__ ): __SCREAMING_SNAKE_CASE : Optional[Any] = 0 for j in range(0 , cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError('''Coefficient matrix is not strictly diagonally dominant''' ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
9
from collections import Counter import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split __lowerCAmelCase : List[Any] =datasets.load_iris() __lowerCAmelCase : Tuple =np.array(data['data']) __lowerCAmelCase : Dict =np.array(data['target']) __lowerCAmelCase : List[str] =data['target_names'] __lowerCAmelCase ,__lowerCAmelCase ,__lowerCAmelCase ,__lowerCAmelCase : str =train_test_split(X, y) def _UpperCamelCase ( lowercase__ , lowercase__ ): return np.linalg.norm(np.array(lowercase__ ) - np.array(lowercase__ ) ) def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__=5 ): __SCREAMING_SNAKE_CASE : Optional[int] = zip(lowercase__ , lowercase__ ) # List of distances of all points from the point to be classified __SCREAMING_SNAKE_CASE : Dict = [] for data_point in data: __SCREAMING_SNAKE_CASE : Tuple = euclidean_distance(data_point[0] , lowercase__ ) distances.append((distance, data_point[1]) ) # Choosing 'k' points with the least distances. __SCREAMING_SNAKE_CASE : int = [i[1] for i in sorted(lowercase__ )[:k]] # Most commonly occurring class among them # is the class into which the point is classified __SCREAMING_SNAKE_CASE : Any = Counter(lowercase__ ).most_common(1 )[0][0] return classes[result] if __name__ == "__main__": print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))
9
1
from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class _lowercase ( nn.Module ): '''simple docstring''' def __init__( self :Optional[int] , lowerCAmelCase__ :int , lowerCAmelCase__ :int , lowerCAmelCase__ :int , lowerCAmelCase__ :List[str]=0.0 , lowerCAmelCase__ :Optional[int] = None , lowerCAmelCase__ :str = "geglu" , lowerCAmelCase__ :Optional[int] = None , lowerCAmelCase__ :bool = False , lowerCAmelCase__ :bool = False , lowerCAmelCase__ :bool = False , lowerCAmelCase__ :bool = False , lowerCAmelCase__ :bool = True , lowerCAmelCase__ :str = "layer_norm" , lowerCAmelCase__ :bool = False , ) -> Tuple: super().__init__() __SCREAMING_SNAKE_CASE : Optional[Any] = only_cross_attention __SCREAMING_SNAKE_CASE : int = (num_embeds_ada_norm is not None) and norm_type == '''ada_norm_zero''' __SCREAMING_SNAKE_CASE : List[str] = (num_embeds_ada_norm is not None) and norm_type == '''ada_norm''' if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( f'''`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to''' f''' define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.''' ) # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: __SCREAMING_SNAKE_CASE : Dict = AdaLayerNorm(lowerCAmelCase__ , lowerCAmelCase__ ) elif self.use_ada_layer_norm_zero: __SCREAMING_SNAKE_CASE : Union[str, Any] = AdaLayerNormZero(lowerCAmelCase__ , lowerCAmelCase__ ) else: __SCREAMING_SNAKE_CASE : Dict = nn.LayerNorm(lowerCAmelCase__ , elementwise_affine=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : str = Attention( query_dim=lowerCAmelCase__ , heads=lowerCAmelCase__ , dim_head=lowerCAmelCase__ , dropout=lowerCAmelCase__ , bias=lowerCAmelCase__ , cross_attention_dim=cross_attention_dim if only_cross_attention else None , upcast_attention=lowerCAmelCase__ , ) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. __SCREAMING_SNAKE_CASE : Optional[int] = ( AdaLayerNorm(lowerCAmelCase__ , lowerCAmelCase__ ) if self.use_ada_layer_norm else nn.LayerNorm(lowerCAmelCase__ , elementwise_affine=lowerCAmelCase__ ) ) __SCREAMING_SNAKE_CASE : Dict = Attention( query_dim=lowerCAmelCase__ , cross_attention_dim=cross_attention_dim if not double_self_attention else None , heads=lowerCAmelCase__ , dim_head=lowerCAmelCase__ , dropout=lowerCAmelCase__ , bias=lowerCAmelCase__ , upcast_attention=lowerCAmelCase__ , ) # is self-attn if encoder_hidden_states is none else: __SCREAMING_SNAKE_CASE : Dict = None __SCREAMING_SNAKE_CASE : Dict = None # 3. Feed-forward __SCREAMING_SNAKE_CASE : Tuple = nn.LayerNorm(lowerCAmelCase__ , elementwise_affine=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = FeedForward(lowerCAmelCase__ , dropout=lowerCAmelCase__ , activation_fn=lowerCAmelCase__ , final_dropout=lowerCAmelCase__ ) # let chunk size default to None __SCREAMING_SNAKE_CASE : int = None __SCREAMING_SNAKE_CASE : int = 0 def __magic_name__( self :List[str] , lowerCAmelCase__ :Optional[int] , lowerCAmelCase__ :int ) -> Union[str, Any]: # Sets chunk feed-forward __SCREAMING_SNAKE_CASE : Tuple = chunk_size __SCREAMING_SNAKE_CASE : str = dim def __magic_name__( self :Tuple , lowerCAmelCase__ :torch.FloatTensor , lowerCAmelCase__ :Optional[torch.FloatTensor] = None , lowerCAmelCase__ :Optional[torch.FloatTensor] = None , lowerCAmelCase__ :Optional[torch.FloatTensor] = None , lowerCAmelCase__ :Optional[torch.LongTensor] = None , lowerCAmelCase__ :Dict[str, Any] = None , lowerCAmelCase__ :Optional[torch.LongTensor] = None , ) -> List[Any]: # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: __SCREAMING_SNAKE_CASE : Optional[Any] = self.norma(lowerCAmelCase__ , lowerCAmelCase__ ) elif self.use_ada_layer_norm_zero: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : List[str] = self.norma( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , hidden_dtype=hidden_states.dtype ) else: __SCREAMING_SNAKE_CASE : List[Any] = self.norma(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : int = cross_attention_kwargs if cross_attention_kwargs is not None else {} __SCREAMING_SNAKE_CASE : Any = self.attna( lowerCAmelCase__ , encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None , attention_mask=lowerCAmelCase__ , **lowerCAmelCase__ , ) if self.use_ada_layer_norm_zero: __SCREAMING_SNAKE_CASE : List[str] = gate_msa.unsqueeze(1 ) * attn_output __SCREAMING_SNAKE_CASE : Union[str, Any] = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: __SCREAMING_SNAKE_CASE : Optional[Any] = ( self.norma(lowerCAmelCase__ , lowerCAmelCase__ ) if self.use_ada_layer_norm else self.norma(lowerCAmelCase__ ) ) __SCREAMING_SNAKE_CASE : Union[str, Any] = self.attna( lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , **lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE : str = attn_output + hidden_states # 3. Feed-forward __SCREAMING_SNAKE_CASE : int = self.norma(lowerCAmelCase__ ) if self.use_ada_layer_norm_zero: __SCREAMING_SNAKE_CASE : Optional[Any] = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( f'''`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.''' ) __SCREAMING_SNAKE_CASE : List[str] = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.cat( [self.ff(lowerCAmelCase__ ) for hid_slice in norm_hidden_states.chunk(lowerCAmelCase__ , dim=self._chunk_dim )] , dim=self._chunk_dim , ) else: __SCREAMING_SNAKE_CASE : int = self.ff(lowerCAmelCase__ ) if self.use_ada_layer_norm_zero: __SCREAMING_SNAKE_CASE : Union[str, Any] = gate_mlp.unsqueeze(1 ) * ff_output __SCREAMING_SNAKE_CASE : Tuple = ff_output + hidden_states return hidden_states class _lowercase ( nn.Module ): '''simple docstring''' def __init__( self :int , lowerCAmelCase__ :int , lowerCAmelCase__ :Optional[int] = None , lowerCAmelCase__ :int = 4 , lowerCAmelCase__ :float = 0.0 , lowerCAmelCase__ :str = "geglu" , lowerCAmelCase__ :bool = False , ) -> Optional[Any]: super().__init__() __SCREAMING_SNAKE_CASE : List[Any] = int(dim * mult ) __SCREAMING_SNAKE_CASE : Optional[Any] = dim_out if dim_out is not None else dim if activation_fn == "gelu": __SCREAMING_SNAKE_CASE : Tuple = GELU(lowerCAmelCase__ , lowerCAmelCase__ ) if activation_fn == "gelu-approximate": __SCREAMING_SNAKE_CASE : int = GELU(lowerCAmelCase__ , lowerCAmelCase__ , approximate='''tanh''' ) elif activation_fn == "geglu": __SCREAMING_SNAKE_CASE : List[str] = GEGLU(lowerCAmelCase__ , lowerCAmelCase__ ) elif activation_fn == "geglu-approximate": __SCREAMING_SNAKE_CASE : int = ApproximateGELU(lowerCAmelCase__ , lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = nn.ModuleList([] ) # project in self.net.append(lowerCAmelCase__ ) # project dropout self.net.append(nn.Dropout(lowerCAmelCase__ ) ) # project out self.net.append(nn.Linear(lowerCAmelCase__ , lowerCAmelCase__ ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(lowerCAmelCase__ ) ) def __magic_name__( self :Dict , lowerCAmelCase__ :int ) -> Any: for module in self.net: __SCREAMING_SNAKE_CASE : str = module(lowerCAmelCase__ ) return hidden_states class _lowercase ( nn.Module ): '''simple docstring''' def __init__( self :List[Any] , lowerCAmelCase__ :int , lowerCAmelCase__ :int , lowerCAmelCase__ :str = "none" ) -> Optional[int]: super().__init__() __SCREAMING_SNAKE_CASE : Tuple = nn.Linear(lowerCAmelCase__ , lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Union[str, Any] = approximate def __magic_name__( self :int , lowerCAmelCase__ :Optional[int] ) -> Tuple: if gate.device.type != "mps": return F.gelu(lowerCAmelCase__ , approximate=self.approximate ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) , approximate=self.approximate ).to(dtype=gate.dtype ) def __magic_name__( self :int , lowerCAmelCase__ :Union[str, Any] ) -> int: __SCREAMING_SNAKE_CASE : Tuple = self.proj(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : str = self.gelu(lowerCAmelCase__ ) return hidden_states class _lowercase ( nn.Module ): '''simple docstring''' def __init__( self :Dict , lowerCAmelCase__ :int , lowerCAmelCase__ :int ) -> Optional[Any]: super().__init__() __SCREAMING_SNAKE_CASE : Dict = nn.Linear(lowerCAmelCase__ , dim_out * 2 ) def __magic_name__( self :List[Any] , lowerCAmelCase__ :List[Any] ) -> Optional[Any]: if gate.device.type != "mps": return F.gelu(lowerCAmelCase__ ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def __magic_name__( self :Tuple , lowerCAmelCase__ :List[Any] ) -> Dict: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : int = self.proj(lowerCAmelCase__ ).chunk(2 , dim=-1 ) return hidden_states * self.gelu(lowerCAmelCase__ ) class _lowercase ( nn.Module ): '''simple docstring''' def __init__( self :List[str] , lowerCAmelCase__ :int , lowerCAmelCase__ :int ) -> Tuple: super().__init__() __SCREAMING_SNAKE_CASE : Dict = nn.Linear(lowerCAmelCase__ , lowerCAmelCase__ ) def __magic_name__( self :Tuple , lowerCAmelCase__ :List[Any] ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : Dict = self.proj(lowerCAmelCase__ ) return x * torch.sigmoid(1.702 * x ) class _lowercase ( nn.Module ): '''simple docstring''' def __init__( self :Any , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :Dict ) -> Any: super().__init__() __SCREAMING_SNAKE_CASE : Dict = nn.Embedding(lowerCAmelCase__ , lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : str = nn.SiLU() __SCREAMING_SNAKE_CASE : Any = nn.Linear(lowerCAmelCase__ , embedding_dim * 2 ) __SCREAMING_SNAKE_CASE : Optional[Any] = nn.LayerNorm(lowerCAmelCase__ , elementwise_affine=lowerCAmelCase__ ) def __magic_name__( self :Any , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :Dict ) -> Any: __SCREAMING_SNAKE_CASE : Any = self.linear(self.silu(self.emb(lowerCAmelCase__ ) ) ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : str = torch.chunk(lowerCAmelCase__ , 2 ) __SCREAMING_SNAKE_CASE : str = self.norm(lowerCAmelCase__ ) * (1 + scale) + shift return x class _lowercase ( nn.Module ): '''simple docstring''' def __init__( self :Any , lowerCAmelCase__ :Any , lowerCAmelCase__ :str ) -> Dict: super().__init__() __SCREAMING_SNAKE_CASE : List[Any] = CombinedTimestepLabelEmbeddings(lowerCAmelCase__ , lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Any = nn.SiLU() __SCREAMING_SNAKE_CASE : int = nn.Linear(lowerCAmelCase__ , 6 * embedding_dim , bias=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : int = nn.LayerNorm(lowerCAmelCase__ , elementwise_affine=lowerCAmelCase__ , eps=1E-6 ) def __magic_name__( self :Dict , lowerCAmelCase__ :Any , lowerCAmelCase__ :Optional[Any] , lowerCAmelCase__ :str , lowerCAmelCase__ :Optional[Any]=None ) -> List[Any]: __SCREAMING_SNAKE_CASE : Optional[int] = self.linear(self.silu(self.emb(lowerCAmelCase__ , lowerCAmelCase__ , hidden_dtype=lowerCAmelCase__ ) ) ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Optional[Any] = emb.chunk(6 , dim=1 ) __SCREAMING_SNAKE_CASE : Optional[int] = self.norm(lowerCAmelCase__ ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class _lowercase ( nn.Module ): '''simple docstring''' def __init__( self :int , lowerCAmelCase__ :int , lowerCAmelCase__ :int , lowerCAmelCase__ :int , lowerCAmelCase__ :Optional[str] = None , lowerCAmelCase__ :float = 1E-5 ) -> Tuple: super().__init__() __SCREAMING_SNAKE_CASE : Dict = num_groups __SCREAMING_SNAKE_CASE : Optional[Any] = eps if act_fn is None: __SCREAMING_SNAKE_CASE : Optional[int] = None else: __SCREAMING_SNAKE_CASE : str = get_activation(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = nn.Linear(lowerCAmelCase__ , out_dim * 2 ) def __magic_name__( self :int , lowerCAmelCase__ :str , lowerCAmelCase__ :Optional[Any] ) -> Optional[int]: if self.act: __SCREAMING_SNAKE_CASE : Dict = self.act(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : str = self.linear(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = emb[:, :, None, None] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Union[str, Any] = emb.chunk(2 , dim=1 ) __SCREAMING_SNAKE_CASE : Tuple = F.group_norm(lowerCAmelCase__ , self.num_groups , eps=self.eps ) __SCREAMING_SNAKE_CASE : List[Any] = x * (1 + scale) + shift return x
9
import unittest from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available from transformers.pipelines import pipeline from transformers.pipelines.document_question_answering import apply_tesseract from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_detectrona, require_pytesseract, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image from transformers.image_utils import load_image else: class _lowercase : '''simple docstring''' @staticmethod def __magic_name__( *lowerCAmelCase__ :Union[str, Any] , **lowerCAmelCase__ :str ) -> Union[str, Any]: pass def _UpperCamelCase ( lowercase__ ): return None # This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace, # so we can expect it to be available. __lowerCAmelCase : str =( 'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png' ) @is_pipeline_test @require_torch @require_vision class _lowercase ( unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Tuple = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING @require_pytesseract @require_vision def __magic_name__( self :Any , lowerCAmelCase__ :List[str] , lowerCAmelCase__ :Optional[Any] , lowerCAmelCase__ :Any ) -> Any: __SCREAMING_SNAKE_CASE : Optional[int] = pipeline( '''document-question-answering''' , model=lowerCAmelCase__ , tokenizer=lowerCAmelCase__ , image_processor=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = INVOICE_URL __SCREAMING_SNAKE_CASE : Optional[Any] = list(zip(*apply_tesseract(load_image(lowerCAmelCase__ ) , lowerCAmelCase__ , '''''' ) ) ) __SCREAMING_SNAKE_CASE : str = '''What is the placebo?''' __SCREAMING_SNAKE_CASE : str = [ { '''image''': load_image(lowerCAmelCase__ ), '''question''': question, }, { '''image''': image, '''question''': question, }, { '''image''': image, '''question''': question, '''word_boxes''': word_boxes, }, ] return dqa_pipeline, examples def __magic_name__( self :Optional[Any] , lowerCAmelCase__ :Tuple , lowerCAmelCase__ :Tuple ) -> str: __SCREAMING_SNAKE_CASE : Any = dqa_pipeline(lowerCAmelCase__ , top_k=2 ) self.assertEqual( lowerCAmelCase__ , [ [ {'''score''': ANY(lowerCAmelCase__ ), '''answer''': ANY(lowerCAmelCase__ ), '''start''': ANY(lowerCAmelCase__ ), '''end''': ANY(lowerCAmelCase__ )}, {'''score''': ANY(lowerCAmelCase__ ), '''answer''': ANY(lowerCAmelCase__ ), '''start''': ANY(lowerCAmelCase__ ), '''end''': ANY(lowerCAmelCase__ )}, ] ] * 3 , ) @require_torch @require_detectrona @require_pytesseract def __magic_name__( self :Dict ) -> List[str]: __SCREAMING_SNAKE_CASE : Tuple = pipeline('''document-question-answering''' , model='''hf-internal-testing/tiny-random-layoutlmv2''' ) __SCREAMING_SNAKE_CASE : Dict = INVOICE_URL __SCREAMING_SNAKE_CASE : int = '''How many cats are there?''' __SCREAMING_SNAKE_CASE : Optional[int] = [ {'''score''': 0.0001, '''answer''': '''oy 2312/2019''', '''start''': 38, '''end''': 39}, {'''score''': 0.0001, '''answer''': '''oy 2312/2019 DUE''', '''start''': 38, '''end''': 40}, ] __SCREAMING_SNAKE_CASE : Tuple = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , top_k=2 ) self.assertEqual(nested_simplify(lowerCAmelCase__ , decimals=4 ) , lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual(nested_simplify(lowerCAmelCase__ , decimals=4 ) , lowerCAmelCase__ ) # This image does not detect ANY text in it, meaning layoutlmv2 should fail. # Empty answer probably __SCREAMING_SNAKE_CASE : Any = '''./tests/fixtures/tests_samples/COCO/000000039769.png''' __SCREAMING_SNAKE_CASE : List[Any] = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , top_k=2 ) self.assertEqual(lowerCAmelCase__ , [] ) # We can optionnally pass directly the words and bounding boxes __SCREAMING_SNAKE_CASE : Union[str, Any] = '''./tests/fixtures/tests_samples/COCO/000000039769.png''' __SCREAMING_SNAKE_CASE : Union[str, Any] = [] __SCREAMING_SNAKE_CASE : Union[str, Any] = [] __SCREAMING_SNAKE_CASE : Union[str, Any] = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , words=lowerCAmelCase__ , boxes=lowerCAmelCase__ , top_k=2 ) self.assertEqual(lowerCAmelCase__ , [] ) @slow @require_torch @require_detectrona @require_pytesseract def __magic_name__( self :int ) -> Optional[Any]: __SCREAMING_SNAKE_CASE : List[Any] = pipeline( '''document-question-answering''' , model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa''' , revision='''9977165''' , ) __SCREAMING_SNAKE_CASE : Dict = INVOICE_URL __SCREAMING_SNAKE_CASE : Any = '''What is the invoice number?''' __SCREAMING_SNAKE_CASE : Any = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) __SCREAMING_SNAKE_CASE : Tuple = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) __SCREAMING_SNAKE_CASE : List[Any] = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ [ {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ], ] * 2 , ) @slow @require_torch @require_detectrona @require_pytesseract def __magic_name__( self :Optional[Any] ) -> Any: __SCREAMING_SNAKE_CASE : int = pipeline( '''document-question-answering''' , model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa''' , revision='''9977165''' , max_seq_len=50 , ) __SCREAMING_SNAKE_CASE : Union[str, Any] = INVOICE_URL __SCREAMING_SNAKE_CASE : Tuple = '''What is the invoice number?''' __SCREAMING_SNAKE_CASE : List[str] = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) __SCREAMING_SNAKE_CASE : int = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) __SCREAMING_SNAKE_CASE : str = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ [ {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] ] * 2 , ) @slow @require_torch @require_pytesseract @require_vision def __magic_name__( self :int ) -> List[Any]: __SCREAMING_SNAKE_CASE : List[Any] = AutoTokenizer.from_pretrained( '''impira/layoutlm-document-qa''' , revision='''3dc6de3''' , add_prefix_space=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = pipeline( '''document-question-answering''' , model='''impira/layoutlm-document-qa''' , tokenizer=lowerCAmelCase__ , revision='''3dc6de3''' , ) __SCREAMING_SNAKE_CASE : Union[str, Any] = INVOICE_URL __SCREAMING_SNAKE_CASE : str = '''What is the invoice number?''' __SCREAMING_SNAKE_CASE : Dict = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) __SCREAMING_SNAKE_CASE : Union[str, Any] = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) __SCREAMING_SNAKE_CASE : Union[str, Any] = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ [ {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] ] * 2 , ) __SCREAMING_SNAKE_CASE : Optional[int] = list(zip(*apply_tesseract(load_image(lowerCAmelCase__ ) , lowerCAmelCase__ , '''''' ) ) ) # This model should also work if `image` is set to None __SCREAMING_SNAKE_CASE : str = dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) @slow @require_torch @require_pytesseract @require_vision def __magic_name__( self :str ) -> Dict: __SCREAMING_SNAKE_CASE : Optional[int] = AutoTokenizer.from_pretrained( '''impira/layoutlm-document-qa''' , revision='''3dc6de3''' , add_prefix_space=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Tuple = pipeline( '''document-question-answering''' , model='''impira/layoutlm-document-qa''' , tokenizer=lowerCAmelCase__ , revision='''3dc6de3''' , max_seq_len=50 , ) __SCREAMING_SNAKE_CASE : List[str] = INVOICE_URL __SCREAMING_SNAKE_CASE : Dict = '''What is the invoice number?''' __SCREAMING_SNAKE_CASE : List[Any] = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) __SCREAMING_SNAKE_CASE : Optional[int] = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ [ {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] ] * 2 , ) __SCREAMING_SNAKE_CASE : List[str] = list(zip(*apply_tesseract(load_image(lowerCAmelCase__ ) , lowerCAmelCase__ , '''''' ) ) ) # This model should also work if `image` is set to None __SCREAMING_SNAKE_CASE : List[Any] = dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) @slow @require_torch def __magic_name__( self :Union[str, Any] ) -> Tuple: __SCREAMING_SNAKE_CASE : str = pipeline( '''document-question-answering''' , model='''naver-clova-ix/donut-base-finetuned-docvqa''' , tokenizer=AutoTokenizer.from_pretrained('''naver-clova-ix/donut-base-finetuned-docvqa''' ) , feature_extractor='''naver-clova-ix/donut-base-finetuned-docvqa''' , ) __SCREAMING_SNAKE_CASE : Union[str, Any] = INVOICE_URL __SCREAMING_SNAKE_CASE : Optional[int] = '''What is the invoice number?''' __SCREAMING_SNAKE_CASE : Tuple = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , top_k=2 ) self.assertEqual(nested_simplify(lowerCAmelCase__ , decimals=4 ) , [{'''answer''': '''us-001'''}] ) @require_tf @unittest.skip('''Document question answering not implemented in TF''' ) def __magic_name__( self :Union[str, Any] ) -> Tuple: pass
9
1
import argparse import math import traceback import dateutil.parser as date_parser import requests def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : List[str] = {} __SCREAMING_SNAKE_CASE : Optional[Any] = job['''started_at'''] __SCREAMING_SNAKE_CASE : List[str] = job['''completed_at'''] __SCREAMING_SNAKE_CASE : List[str] = date_parser.parse(lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = date_parser.parse(lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = round((end_datetime - start_datetime).total_seconds() / 60.0 ) __SCREAMING_SNAKE_CASE : Any = start __SCREAMING_SNAKE_CASE : Optional[int] = end __SCREAMING_SNAKE_CASE : Dict = duration_in_min return job_info def _UpperCamelCase ( lowercase__ , lowercase__=None ): __SCREAMING_SNAKE_CASE : Optional[Any] = None if token is not None: __SCREAMING_SNAKE_CASE : Optional[int] = {'''Accept''': '''application/vnd.github+json''', '''Authorization''': F'''Bearer {token}'''} __SCREAMING_SNAKE_CASE : int = F'''https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100''' __SCREAMING_SNAKE_CASE : int = requests.get(lowercase__ , headers=lowercase__ ).json() __SCREAMING_SNAKE_CASE : Optional[Any] = {} try: job_time.update({job['''name''']: extract_time_from_single_job(lowercase__ ) for job in result['''jobs''']} ) __SCREAMING_SNAKE_CASE : Optional[int] = math.ceil((result['''total_count'''] - 100) / 100 ) for i in range(lowercase__ ): __SCREAMING_SNAKE_CASE : Optional[int] = requests.get(url + F'''&page={i + 2}''' , headers=lowercase__ ).json() job_time.update({job['''name''']: extract_time_from_single_job(lowercase__ ) for job in result['''jobs''']} ) return job_time except Exception: print(F'''Unknown error, could not fetch links:\n{traceback.format_exc()}''' ) return {} if __name__ == "__main__": __lowerCAmelCase : int =argparse.ArgumentParser() # Required parameters parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.') __lowerCAmelCase : Tuple =parser.parse_args() __lowerCAmelCase : Any =get_job_time(args.workflow_run_id) __lowerCAmelCase : int =dict(sorted(job_time.items(), key=lambda item: item[1]["duration"], reverse=True)) for k, v in job_time.items(): print(f"""{k}: {v["duration"]}""")
9
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase : Any ={'configuration_vit_msn': ['VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ViTMSNConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : int =[ 'VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST', 'ViTMSNModel', 'ViTMSNForImageClassification', 'ViTMSNPreTrainedModel', ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys __lowerCAmelCase : Union[str, Any] =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
9
1
from collections import defaultdict from math import ceil, sqrt def _UpperCamelCase ( lowercase__ = 1000000 , lowercase__ = 10 ): __SCREAMING_SNAKE_CASE : defaultdict = defaultdict(lowercase__ ) for outer_width in range(3 , (t_limit // 4) + 2 ): if outer_width * outer_width > t_limit: __SCREAMING_SNAKE_CASE : Tuple = max( ceil(sqrt(outer_width * outer_width - t_limit ) ) , 1 ) else: __SCREAMING_SNAKE_CASE : Union[str, Any] = 1 hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 for hole_width in range(lowercase__ , outer_width - 1 , 2 ): count[outer_width * outer_width - hole_width * hole_width] += 1 return sum(1 for n in count.values() if 1 <= n <= 10 ) if __name__ == "__main__": print(f"""{solution() = }""")
9
from typing import Optional from .. import Features, NamedSplit from ..packaged_modules.text.text import Text from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class _lowercase ( A__ ): '''simple docstring''' def __init__( self :int , lowerCAmelCase__ :NestedDataStructureLike[PathLike] , lowerCAmelCase__ :Optional[NamedSplit] = None , lowerCAmelCase__ :Optional[Features] = None , lowerCAmelCase__ :str = None , lowerCAmelCase__ :bool = False , lowerCAmelCase__ :bool = False , lowerCAmelCase__ :Optional[int] = None , **lowerCAmelCase__ :Optional[int] , ) -> Tuple: super().__init__( lowerCAmelCase__ , split=lowerCAmelCase__ , features=lowerCAmelCase__ , cache_dir=lowerCAmelCase__ , keep_in_memory=lowerCAmelCase__ , streaming=lowerCAmelCase__ , num_proc=lowerCAmelCase__ , **lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE : List[str] = path_or_paths if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else {self.split: path_or_paths} __SCREAMING_SNAKE_CASE : int = Text( cache_dir=lowerCAmelCase__ , data_files=lowerCAmelCase__ , features=lowerCAmelCase__ , **lowerCAmelCase__ , ) def __magic_name__( self :Dict ) -> Tuple: # Build iterable dataset if self.streaming: __SCREAMING_SNAKE_CASE : int = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: __SCREAMING_SNAKE_CASE : List[str] = None __SCREAMING_SNAKE_CASE : str = None __SCREAMING_SNAKE_CASE : Dict = None __SCREAMING_SNAKE_CASE : Tuple = None self.builder.download_and_prepare( download_config=lowerCAmelCase__ , download_mode=lowerCAmelCase__ , verification_mode=lowerCAmelCase__ , base_path=lowerCAmelCase__ , num_proc=self.num_proc , ) __SCREAMING_SNAKE_CASE : Optional[int] = self.builder.as_dataset( split=self.split , verification_mode=lowerCAmelCase__ , in_memory=self.keep_in_memory ) return dataset
9
1
from ..utils import DummyObject, requires_backends class _lowercase ( metaclass=A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : str = ['''flax''', '''transformers'''] def __init__( self :Tuple , *lowerCAmelCase__ :Optional[Any] , **lowerCAmelCase__ :Optional[Any] ) -> Dict: requires_backends(self , ['''flax''', '''transformers'''] ) @classmethod def __magic_name__( cls :List[Any] , *lowerCAmelCase__ :Any , **lowerCAmelCase__ :Union[str, Any] ) -> Any: requires_backends(cls , ['''flax''', '''transformers'''] ) @classmethod def __magic_name__( cls :Union[str, Any] , *lowerCAmelCase__ :List[Any] , **lowerCAmelCase__ :str ) -> Any: requires_backends(cls , ['''flax''', '''transformers'''] ) class _lowercase ( metaclass=A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = ['''flax''', '''transformers'''] def __init__( self :List[str] , *lowerCAmelCase__ :Optional[Any] , **lowerCAmelCase__ :List[Any] ) -> Optional[int]: requires_backends(self , ['''flax''', '''transformers'''] ) @classmethod def __magic_name__( cls :Any , *lowerCAmelCase__ :str , **lowerCAmelCase__ :str ) -> Dict: requires_backends(cls , ['''flax''', '''transformers'''] ) @classmethod def __magic_name__( cls :str , *lowerCAmelCase__ :Optional[Any] , **lowerCAmelCase__ :List[str] ) -> Tuple: requires_backends(cls , ['''flax''', '''transformers'''] ) class _lowercase ( metaclass=A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Dict = ['''flax''', '''transformers'''] def __init__( self :Any , *lowerCAmelCase__ :List[str] , **lowerCAmelCase__ :List[str] ) -> int: requires_backends(self , ['''flax''', '''transformers'''] ) @classmethod def __magic_name__( cls :Optional[Any] , *lowerCAmelCase__ :str , **lowerCAmelCase__ :int ) -> Optional[Any]: requires_backends(cls , ['''flax''', '''transformers'''] ) @classmethod def __magic_name__( cls :Dict , *lowerCAmelCase__ :List[Any] , **lowerCAmelCase__ :Tuple ) -> Dict: requires_backends(cls , ['''flax''', '''transformers'''] ) class _lowercase ( metaclass=A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Union[str, Any] = ['''flax''', '''transformers'''] def __init__( self :Dict , *lowerCAmelCase__ :Dict , **lowerCAmelCase__ :List[str] ) -> Tuple: requires_backends(self , ['''flax''', '''transformers'''] ) @classmethod def __magic_name__( cls :Tuple , *lowerCAmelCase__ :int , **lowerCAmelCase__ :int ) -> List[Any]: requires_backends(cls , ['''flax''', '''transformers'''] ) @classmethod def __magic_name__( cls :Tuple , *lowerCAmelCase__ :Optional[Any] , **lowerCAmelCase__ :List[str] ) -> str: requires_backends(cls , ['''flax''', '''transformers'''] )
9
import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipaConfig, BlipaForConditionalGeneration, BlipaProcessor, BlipaVisionConfig, BlipImageProcessor, OPTConfig, TaConfig, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : Dict = '''https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png''' __SCREAMING_SNAKE_CASE : List[str] = Image.open(requests.get(lowercase__ , stream=lowercase__ ).raw ).convert('''RGB''' ) return image def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : List[Any] = [] # fmt: off # vision encoder rename_keys.append(('''visual_encoder.cls_token''', '''vision_model.embeddings.class_embedding''') ) rename_keys.append(('''visual_encoder.pos_embed''', '''vision_model.embeddings.position_embedding''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.weight''', '''vision_model.embeddings.patch_embedding.weight''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.bias''', '''vision_model.embeddings.patch_embedding.bias''') ) rename_keys.append(('''ln_vision.weight''', '''vision_model.post_layernorm.weight''') ) rename_keys.append(('''ln_vision.bias''', '''vision_model.post_layernorm.bias''') ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((F'''visual_encoder.blocks.{i}.norm1.weight''', F'''vision_model.encoder.layers.{i}.layer_norm1.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.norm1.bias''', F'''vision_model.encoder.layers.{i}.layer_norm1.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.norm2.weight''', F'''vision_model.encoder.layers.{i}.layer_norm2.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.norm2.bias''', F'''vision_model.encoder.layers.{i}.layer_norm2.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.qkv.weight''', F'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.proj.weight''', F'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',) ) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.proj.bias''', F'''vision_model.encoder.layers.{i}.self_attn.projection.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc1.weight''', F'''vision_model.encoder.layers.{i}.mlp.fc1.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc1.bias''', F'''vision_model.encoder.layers.{i}.mlp.fc1.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc2.weight''', F'''vision_model.encoder.layers.{i}.mlp.fc2.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc2.bias''', F'''vision_model.encoder.layers.{i}.mlp.fc2.bias''') ) # QFormer rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.weight''', '''qformer.layernorm.weight''') ) rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.bias''', '''qformer.layernorm.bias''') ) # fmt: on return rename_keys def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : List[Any] = dct.pop(lowercase__ ) __SCREAMING_SNAKE_CASE : List[Any] = val def _UpperCamelCase ( lowercase__ , lowercase__ ): for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases __SCREAMING_SNAKE_CASE : Optional[int] = state_dict.pop(F'''visual_encoder.blocks.{i}.attn.q_bias''' ) __SCREAMING_SNAKE_CASE : int = state_dict.pop(F'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict __SCREAMING_SNAKE_CASE : Optional[int] = torch.cat((q_bias, torch.zeros_like(lowercase__ , requires_grad=lowercase__ ), v_bias) ) __SCREAMING_SNAKE_CASE : Optional[Any] = qkv_bias def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Any = 364 if '''coco''' in model_name else 224 __SCREAMING_SNAKE_CASE : List[str] = BlipaVisionConfig(image_size=lowercase__ ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: __SCREAMING_SNAKE_CASE : Union[str, Any] = OPTConfig.from_pretrained('''facebook/opt-2.7b''' , eos_token_id=lowercase__ ).to_dict() elif "opt-6.7b" in model_name: __SCREAMING_SNAKE_CASE : List[Any] = OPTConfig.from_pretrained('''facebook/opt-6.7b''' , eos_token_id=lowercase__ ).to_dict() elif "t5-xl" in model_name: __SCREAMING_SNAKE_CASE : Optional[Any] = TaConfig.from_pretrained('''google/flan-t5-xl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: __SCREAMING_SNAKE_CASE : Union[str, Any] = TaConfig.from_pretrained('''google/flan-t5-xxl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() __SCREAMING_SNAKE_CASE : Optional[int] = BlipaConfig(vision_config=lowercase__ , text_config=lowercase__ ) return config, image_size @torch.no_grad() def _UpperCamelCase ( lowercase__ , lowercase__=None , lowercase__=False ): __SCREAMING_SNAKE_CASE : Any = ( AutoTokenizer.from_pretrained('''facebook/opt-2.7b''' ) if '''opt''' in model_name else AutoTokenizer.from_pretrained('''google/flan-t5-xl''' ) ) __SCREAMING_SNAKE_CASE : str = tokenizer('''\n''' , add_special_tokens=lowercase__ ).input_ids[0] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Union[str, Any] = get_blipa_config(lowercase__ , eos_token_id=lowercase__ ) __SCREAMING_SNAKE_CASE : int = BlipaForConditionalGeneration(lowercase__ ).eval() __SCREAMING_SNAKE_CASE : int = { '''blip2-opt-2.7b''': ('''blip2_opt''', '''pretrain_opt2.7b'''), '''blip2-opt-6.7b''': ('''blip2_opt''', '''pretrain_opt6.7b'''), '''blip2-opt-2.7b-coco''': ('''blip2_opt''', '''caption_coco_opt2.7b'''), '''blip2-opt-6.7b-coco''': ('''blip2_opt''', '''caption_coco_opt6.7b'''), '''blip2-flan-t5-xl''': ('''blip2_t5''', '''pretrain_flant5xl'''), '''blip2-flan-t5-xl-coco''': ('''blip2_t5''', '''caption_coco_flant5xl'''), '''blip2-flan-t5-xxl''': ('''blip2_t5''', '''pretrain_flant5xxl'''), } __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Any = model_name_to_original[model_name] # load original model print('''Loading original model...''' ) __SCREAMING_SNAKE_CASE : List[str] = '''cuda''' if torch.cuda.is_available() else '''cpu''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Dict = load_model_and_preprocess( name=lowercase__ , model_type=lowercase__ , is_eval=lowercase__ , device=lowercase__ ) original_model.eval() print('''Done!''' ) # update state dict keys __SCREAMING_SNAKE_CASE : List[str] = original_model.state_dict() __SCREAMING_SNAKE_CASE : Optional[int] = create_rename_keys(lowercase__ ) for src, dest in rename_keys: rename_key(lowercase__ , lowercase__ , lowercase__ ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): __SCREAMING_SNAKE_CASE : Tuple = state_dict.pop(lowercase__ ) if key.startswith('''Qformer.bert''' ): __SCREAMING_SNAKE_CASE : List[str] = key.replace('''Qformer.bert''' , '''qformer''' ) if "attention.self" in key: __SCREAMING_SNAKE_CASE : Union[str, Any] = key.replace('''self''' , '''attention''' ) if "opt_proj" in key: __SCREAMING_SNAKE_CASE : Dict = key.replace('''opt_proj''' , '''language_projection''' ) if "t5_proj" in key: __SCREAMING_SNAKE_CASE : Tuple = key.replace('''t5_proj''' , '''language_projection''' ) if key.startswith('''opt''' ): __SCREAMING_SNAKE_CASE : List[str] = key.replace('''opt''' , '''language''' ) if key.startswith('''t5''' ): __SCREAMING_SNAKE_CASE : Tuple = key.replace('''t5''' , '''language''' ) __SCREAMING_SNAKE_CASE : Tuple = val # read in qv biases read_in_q_v_bias(lowercase__ , lowercase__ ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : List[str] = hf_model.load_state_dict(lowercase__ , strict=lowercase__ ) assert len(lowercase__ ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] __SCREAMING_SNAKE_CASE : List[str] = load_demo_image() __SCREAMING_SNAKE_CASE : Any = vis_processors['''eval'''](lowercase__ ).unsqueeze(0 ).to(lowercase__ ) __SCREAMING_SNAKE_CASE : str = tokenizer(['''\n'''] , return_tensors='''pt''' ).input_ids.to(lowercase__ ) # create processor __SCREAMING_SNAKE_CASE : List[Any] = BlipImageProcessor( size={'''height''': image_size, '''width''': image_size} , image_mean=lowercase__ , image_std=lowercase__ ) __SCREAMING_SNAKE_CASE : int = BlipaProcessor(image_processor=lowercase__ , tokenizer=lowercase__ ) __SCREAMING_SNAKE_CASE : Any = processor(images=lowercase__ , return_tensors='''pt''' ).pixel_values.to(lowercase__ ) # make sure processor creates exact same pixel values assert torch.allclose(lowercase__ , lowercase__ ) original_model.to(lowercase__ ) hf_model.to(lowercase__ ) with torch.no_grad(): if "opt" in model_name: __SCREAMING_SNAKE_CASE : Dict = original_model({'''image''': original_pixel_values, '''text_input''': ['''''']} ).logits __SCREAMING_SNAKE_CASE : Dict = hf_model(lowercase__ , lowercase__ ).logits else: __SCREAMING_SNAKE_CASE : int = original_model( {'''image''': original_pixel_values, '''text_input''': ['''\n'''], '''text_output''': ['''\n''']} ).logits __SCREAMING_SNAKE_CASE : List[Any] = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -100 ) __SCREAMING_SNAKE_CASE : Optional[int] = hf_model(lowercase__ , lowercase__ , labels=lowercase__ ).logits assert original_logits.shape == logits.shape print('''First values of original logits:''' , original_logits[0, :3, :3] ) print('''First values of HF logits:''' , logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": __SCREAMING_SNAKE_CASE : Dict = torch.tensor( [[-41.5850, -4.4440, -8.9922], [-47.4322, -5.9143, -1.7340]] , device=lowercase__ ) assert torch.allclose(logits[0, :3, :3] , lowercase__ , atol=1e-4 ) elif model_name == "blip2-flan-t5-xl-coco": __SCREAMING_SNAKE_CASE : Any = torch.tensor( [[-57.0109, -9.8967, -12.6280], [-68.6578, -12.7191, -10.5065]] , device=lowercase__ ) else: # cast to same type __SCREAMING_SNAKE_CASE : Optional[Any] = logits.dtype assert torch.allclose(original_logits.to(lowercase__ ) , lowercase__ , atol=1e-2 ) print('''Looks ok!''' ) print('''Generating a caption...''' ) __SCREAMING_SNAKE_CASE : Any = '''''' __SCREAMING_SNAKE_CASE : Optional[int] = tokenizer(lowercase__ , return_tensors='''pt''' ).input_ids.to(lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = original_model.generate({'''image''': original_pixel_values} ) __SCREAMING_SNAKE_CASE : Union[str, Any] = hf_model.generate( lowercase__ , lowercase__ , do_sample=lowercase__ , num_beams=5 , max_length=30 , min_length=1 , top_p=0.9 , repetition_penalty=1.0 , length_penalty=1.0 , temperature=1 , ) print('''Original generation:''' , lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = input_ids.shape[1] __SCREAMING_SNAKE_CASE : Any = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = [text.strip() for text in output_text] print('''HF generation:''' , lowercase__ ) if pytorch_dump_folder_path is not None: processor.save_pretrained(lowercase__ ) hf_model.save_pretrained(lowercase__ ) if push_to_hub: processor.push_to_hub(F'''nielsr/{model_name}''' ) hf_model.push_to_hub(F'''nielsr/{model_name}''' ) if __name__ == "__main__": __lowerCAmelCase : List[str] =argparse.ArgumentParser() __lowerCAmelCase : Tuple =[ 'blip2-opt-2.7b', 'blip2-opt-6.7b', 'blip2-opt-2.7b-coco', 'blip2-opt-6.7b-coco', 'blip2-flan-t5-xl', 'blip2-flan-t5-xl-coco', 'blip2-flan-t5-xxl', ] parser.add_argument( '--model_name', default='blip2-opt-2.7b', choices=choices, type=str, help='Path to hf config.json of model to convert', ) parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument( '--push_to_hub', action='store_true', help='Whether to push the model and processor to the hub after converting', ) __lowerCAmelCase : List[Any] =parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
9
1
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __lowerCAmelCase : Union[str, Any] =logging.get_logger(__name__) __lowerCAmelCase : List[Any] ={ 'roberta-base': 'https://huggingface.co/roberta-base/resolve/main/config.json', 'roberta-large': 'https://huggingface.co/roberta-large/resolve/main/config.json', 'roberta-large-mnli': 'https://huggingface.co/roberta-large-mnli/resolve/main/config.json', 'distilroberta-base': 'https://huggingface.co/distilroberta-base/resolve/main/config.json', 'roberta-base-openai-detector': 'https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json', 'roberta-large-openai-detector': 'https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json', } class _lowercase ( A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = '''roberta''' def __init__( self :str , lowerCAmelCase__ :Any=50_265 , lowerCAmelCase__ :List[str]=768 , lowerCAmelCase__ :Dict=12 , lowerCAmelCase__ :Union[str, Any]=12 , lowerCAmelCase__ :Optional[Any]=3_072 , lowerCAmelCase__ :List[Any]="gelu" , lowerCAmelCase__ :List[Any]=0.1 , lowerCAmelCase__ :Optional[int]=0.1 , lowerCAmelCase__ :int=512 , lowerCAmelCase__ :List[str]=2 , lowerCAmelCase__ :str=0.02 , lowerCAmelCase__ :Union[str, Any]=1E-1_2 , lowerCAmelCase__ :Optional[int]=1 , lowerCAmelCase__ :Tuple=0 , lowerCAmelCase__ :int=2 , lowerCAmelCase__ :int="absolute" , lowerCAmelCase__ :Any=True , lowerCAmelCase__ :str=None , **lowerCAmelCase__ :int , ) -> List[Any]: super().__init__(pad_token_id=lowerCAmelCase__ , bos_token_id=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__ , **lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = vocab_size __SCREAMING_SNAKE_CASE : Optional[int] = hidden_size __SCREAMING_SNAKE_CASE : Dict = num_hidden_layers __SCREAMING_SNAKE_CASE : Dict = num_attention_heads __SCREAMING_SNAKE_CASE : List[Any] = hidden_act __SCREAMING_SNAKE_CASE : Optional[int] = intermediate_size __SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob __SCREAMING_SNAKE_CASE : Tuple = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : List[Any] = max_position_embeddings __SCREAMING_SNAKE_CASE : Optional[int] = type_vocab_size __SCREAMING_SNAKE_CASE : str = initializer_range __SCREAMING_SNAKE_CASE : str = layer_norm_eps __SCREAMING_SNAKE_CASE : Any = position_embedding_type __SCREAMING_SNAKE_CASE : str = use_cache __SCREAMING_SNAKE_CASE : Tuple = classifier_dropout class _lowercase ( A__ ): '''simple docstring''' @property def __magic_name__( self :str ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": __SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: __SCREAMING_SNAKE_CASE : int = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
9
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __lowerCAmelCase : Optional[int] =logging.get_logger(__name__) __lowerCAmelCase : Optional[Any] ={'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} __lowerCAmelCase : List[str] ={ 'tokenizer_file': { 'EleutherAI/gpt-neox-20b': 'https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/tokenizer.json', }, } __lowerCAmelCase : Optional[int] ={ 'gpt-neox-20b': 2_0_4_8, } class _lowercase ( A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : str = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE__ : Dict = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE__ : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE__ : Optional[Any] = ['''input_ids''', '''attention_mask'''] def __init__( self :int , lowerCAmelCase__ :Any=None , lowerCAmelCase__ :Optional[Any]=None , lowerCAmelCase__ :List[Any]=None , lowerCAmelCase__ :str="<|endoftext|>" , lowerCAmelCase__ :str="<|endoftext|>" , lowerCAmelCase__ :Dict="<|endoftext|>" , lowerCAmelCase__ :Union[str, Any]=False , **lowerCAmelCase__ :List[str] , ) -> Any: super().__init__( lowerCAmelCase__ , lowerCAmelCase__ , tokenizer_file=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , bos_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , **lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE : List[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , lowerCAmelCase__ ) != add_prefix_space: __SCREAMING_SNAKE_CASE : List[str] = getattr(lowerCAmelCase__ , pre_tok_state.pop('''type''' ) ) __SCREAMING_SNAKE_CASE : str = add_prefix_space __SCREAMING_SNAKE_CASE : Any = pre_tok_class(**lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = add_prefix_space def __magic_name__( self :Union[str, Any] , lowerCAmelCase__ :str , lowerCAmelCase__ :Optional[str] = None ) -> Tuple[str]: __SCREAMING_SNAKE_CASE : List[str] = self._tokenizer.model.save(lowerCAmelCase__ , name=lowerCAmelCase__ ) return tuple(lowerCAmelCase__ ) def __magic_name__( self :Optional[Any] , lowerCAmelCase__ :"Conversation" ) -> List[int]: __SCREAMING_SNAKE_CASE : Optional[Any] = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) + [self.eos_token_id] ) if len(lowerCAmelCase__ ) > self.model_max_length: __SCREAMING_SNAKE_CASE : List[str] = input_ids[-self.model_max_length :] return input_ids
9
1
import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to properly calculate the metrics on the # validation dataset when in a distributed system, and builds off the # `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## __lowerCAmelCase : Dict =1_6 __lowerCAmelCase : List[str] =3_2 def _UpperCamelCase ( lowercase__ , lowercase__ = 16 ): __SCREAMING_SNAKE_CASE : Dict = AutoTokenizer.from_pretrained('''bert-base-cased''' ) __SCREAMING_SNAKE_CASE : Optional[int] = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(lowercase__ ): # max_length=None => use the model max length (it's actually the default) __SCREAMING_SNAKE_CASE : Optional[int] = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=lowercase__ , max_length=lowercase__ ) 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(): __SCREAMING_SNAKE_CASE : Dict = datasets.map( lowercase__ , batched=lowercase__ , 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 __SCREAMING_SNAKE_CASE : List[Any] = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(lowercase__ ): # On TPU it's best to pad everything to the same length or training will be very slow. __SCREAMING_SNAKE_CASE : List[str] = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": __SCREAMING_SNAKE_CASE : str = 16 elif accelerator.mixed_precision != "no": __SCREAMING_SNAKE_CASE : Tuple = 8 else: __SCREAMING_SNAKE_CASE : Dict = None return tokenizer.pad( lowercase__ , padding='''longest''' , max_length=lowercase__ , pad_to_multiple_of=lowercase__ , return_tensors='''pt''' , ) # Instantiate dataloaders. __SCREAMING_SNAKE_CASE : Tuple = DataLoader( tokenized_datasets['''train'''] , shuffle=lowercase__ , collate_fn=lowercase__ , batch_size=lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = DataLoader( tokenized_datasets['''validation'''] , shuffle=lowercase__ , collate_fn=lowercase__ , batch_size=lowercase__ ) 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 : Dict =mocked_dataloaders # noqa: F811 def _UpperCamelCase ( lowercase__ , lowercase__ ): # For testing only if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , lowercase__ ) == "1": __SCREAMING_SNAKE_CASE : Dict = 2 # Initialize accelerator __SCREAMING_SNAKE_CASE : List[str] = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __SCREAMING_SNAKE_CASE : str = config['''lr'''] __SCREAMING_SNAKE_CASE : Tuple = int(config['''num_epochs'''] ) __SCREAMING_SNAKE_CASE : Optional[Any] = int(config['''seed'''] ) __SCREAMING_SNAKE_CASE : Optional[int] = int(config['''batch_size'''] ) __SCREAMING_SNAKE_CASE : Dict = evaluate.load('''glue''' , '''mrpc''' ) # If the batch size is too big we use gradient accumulation __SCREAMING_SNAKE_CASE : List[Any] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: __SCREAMING_SNAKE_CASE : List[Any] = batch_size // MAX_GPU_BATCH_SIZE __SCREAMING_SNAKE_CASE : List[str] = MAX_GPU_BATCH_SIZE set_seed(lowercase__ ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Tuple = get_dataloaders(lowercase__ , lowercase__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __SCREAMING_SNAKE_CASE : List[Any] = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=lowercase__ ) # 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). __SCREAMING_SNAKE_CASE : Union[str, Any] = model.to(accelerator.device ) # Instantiate optimizer __SCREAMING_SNAKE_CASE : List[Any] = AdamW(params=model.parameters() , lr=lowercase__ ) # Instantiate scheduler __SCREAMING_SNAKE_CASE : str = get_linear_schedule_with_warmup( optimizer=lowercase__ , num_warmup_steps=100 , num_training_steps=(len(lowercase__ ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Any = accelerator.prepare( lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ ) # Now we train the model for epoch in range(lowercase__ ): model.train() for step, batch in enumerate(lowercase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) __SCREAMING_SNAKE_CASE : Union[str, Any] = model(**lowercase__ ) __SCREAMING_SNAKE_CASE : Any = outputs.loss __SCREAMING_SNAKE_CASE : Tuple = loss / gradient_accumulation_steps accelerator.backward(lowercase__ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() __SCREAMING_SNAKE_CASE : Dict = 0 for step, batch in enumerate(lowercase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): __SCREAMING_SNAKE_CASE : Union[str, Any] = model(**lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = outputs.logits.argmax(dim=-1 ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Optional[int] = accelerator.gather((predictions, batch['''labels''']) ) # New Code # # First we check if it's a distributed system if accelerator.use_distributed: # Then see if we're on the last batch of our eval dataloader if step == len(lowercase__ ) - 1: # Last batch needs to be truncated on distributed systems as it contains additional samples __SCREAMING_SNAKE_CASE : int = predictions[: len(eval_dataloader.dataset ) - samples_seen] __SCREAMING_SNAKE_CASE : Any = references[: len(eval_dataloader.dataset ) - samples_seen] else: # Otherwise we add the number of samples seen samples_seen += references.shape[0] # All of this can be avoided if you use `Accelerator.gather_for_metrics` instead of `Accelerator.gather`: # accelerator.gather_for_metrics((predictions, batch["labels"])) metric.add_batch( predictions=lowercase__ , references=lowercase__ , ) __SCREAMING_SNAKE_CASE : Dict = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , lowercase__ ) def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : int = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=lowercase__ , default=lowercase__ , 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.''' ) __SCREAMING_SNAKE_CASE : str = parser.parse_args() __SCREAMING_SNAKE_CASE : Dict = {'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(lowercase__ , lowercase__ ) if __name__ == "__main__": main()
9
import math_equivalence # From: git+https://github.com/hendrycks/math.git import datasets __lowerCAmelCase : Optional[Any] ='\\n@article{hendrycksmath2021,\n title={Measuring Mathematical Problem Solving With the MATH Dataset},\n author={Dan Hendrycks\n and Collin Burns\n and Saurav Kadavath\n and Akul Arora\n and Steven Basart\n and Eric Tang\n and Dawn Song\n and Jacob Steinhardt},\n journal={arXiv preprint arXiv:2103.03874},\n year={2021}\n}\n' __lowerCAmelCase : Any ='\\nThis metric is used to assess performance on the Mathematics Aptitude Test of Heuristics (MATH) dataset.\nIt first canonicalizes the inputs (e.g., converting "1/2" to "\\frac{1}{2}") and then computes accuracy.\n' __lowerCAmelCase : Optional[Any] =r'\nCalculates accuracy after canonicalizing inputs.\n\nArgs:\n predictions: list of predictions to score. Each prediction\n is a string that contains natural language and LaTex.\n references: list of reference for each prediction. Each\n reference is a string that contains natural language\n and LaTex.\nReturns:\n accuracy: accuracy after canonicalizing inputs\n (e.g., converting "1/2" to "\\frac{1}{2}")\n\nExamples:\n >>> metric = datasets.load_metric("competition_math")\n >>> results = metric.compute(references=["\\frac{1}{2}"], predictions=["1/2"])\n >>> print(results)\n {\'accuracy\': 1.0}\n' @datasets.utils.file_utils.add_end_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _lowercase ( datasets.Metric ): '''simple docstring''' def __magic_name__( self :Optional[Any] ) -> List[str]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' ), '''references''': datasets.Value('''string''' ), } ) , homepage='''https://github.com/hendrycks/math''' , codebase_urls=['''https://github.com/hendrycks/math'''] , ) def __magic_name__( self :Any , lowerCAmelCase__ :Optional[int] , lowerCAmelCase__ :Any ) -> Optional[int]: __SCREAMING_SNAKE_CASE : Tuple = 0.0 for i, j in zip(lowerCAmelCase__ , lowerCAmelCase__ ): n_correct += 1.0 if math_equivalence.is_equiv(lowerCAmelCase__ , lowerCAmelCase__ ) else 0.0 __SCREAMING_SNAKE_CASE : str = n_correct / len(lowerCAmelCase__ ) return { "accuracy": accuracy, }
9
1
def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : Union[str, Any] = len(lowercase__ ) for i in range(1 , lowercase__ ): __SCREAMING_SNAKE_CASE : Tuple = collection[i] __SCREAMING_SNAKE_CASE : Any = 0 __SCREAMING_SNAKE_CASE : List[str] = i - 1 while low <= high: __SCREAMING_SNAKE_CASE : str = (low + high) // 2 if val < collection[mid]: __SCREAMING_SNAKE_CASE : List[str] = mid - 1 else: __SCREAMING_SNAKE_CASE : str = mid + 1 for j in range(lowercase__ , lowercase__ , -1 ): __SCREAMING_SNAKE_CASE : Any = collection[j - 1] __SCREAMING_SNAKE_CASE : Any = val return collection if __name__ == "__main__": __lowerCAmelCase : Any =input('Enter numbers separated by a comma:\n').strip() __lowerCAmelCase : Any =[int(item) for item in user_input.split(',')] print(binary_insertion_sort(unsorted))
9
import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST, OpenAIGPTConfig, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification, OpenAIGPTLMHeadModel, OpenAIGPTModel, ) class _lowercase : '''simple docstring''' def __init__( self :Optional[int] , lowerCAmelCase__ :Optional[Any] , lowerCAmelCase__ :int=13 , lowerCAmelCase__ :List[str]=7 , lowerCAmelCase__ :Dict=True , lowerCAmelCase__ :List[str]=True , lowerCAmelCase__ :str=True , lowerCAmelCase__ :List[Any]=99 , lowerCAmelCase__ :List[str]=32 , lowerCAmelCase__ :Any=5 , lowerCAmelCase__ :List[str]=4 , lowerCAmelCase__ :int=37 , lowerCAmelCase__ :Optional[int]="gelu" , lowerCAmelCase__ :str=0.1 , lowerCAmelCase__ :str=0.1 , lowerCAmelCase__ :Optional[Any]=512 , lowerCAmelCase__ :Union[str, Any]=16 , lowerCAmelCase__ :Dict=2 , lowerCAmelCase__ :Tuple=0.02 , lowerCAmelCase__ :List[Any]=3 , lowerCAmelCase__ :Tuple=4 , lowerCAmelCase__ :int=None , ) -> int: __SCREAMING_SNAKE_CASE : Dict = parent __SCREAMING_SNAKE_CASE : Any = batch_size __SCREAMING_SNAKE_CASE : Union[str, Any] = seq_length __SCREAMING_SNAKE_CASE : Optional[Any] = is_training __SCREAMING_SNAKE_CASE : int = use_token_type_ids __SCREAMING_SNAKE_CASE : Any = use_labels __SCREAMING_SNAKE_CASE : Any = vocab_size __SCREAMING_SNAKE_CASE : List[Any] = hidden_size __SCREAMING_SNAKE_CASE : int = num_hidden_layers __SCREAMING_SNAKE_CASE : List[Any] = num_attention_heads __SCREAMING_SNAKE_CASE : str = intermediate_size __SCREAMING_SNAKE_CASE : Tuple = hidden_act __SCREAMING_SNAKE_CASE : Dict = hidden_dropout_prob __SCREAMING_SNAKE_CASE : int = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : Optional[Any] = max_position_embeddings __SCREAMING_SNAKE_CASE : List[Any] = type_vocab_size __SCREAMING_SNAKE_CASE : List[str] = type_sequence_label_size __SCREAMING_SNAKE_CASE : List[str] = initializer_range __SCREAMING_SNAKE_CASE : Tuple = num_labels __SCREAMING_SNAKE_CASE : Union[str, Any] = num_choices __SCREAMING_SNAKE_CASE : Union[str, Any] = scope __SCREAMING_SNAKE_CASE : Union[str, Any] = self.vocab_size - 1 def __magic_name__( self :Optional[Any] ) -> List[Any]: __SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __SCREAMING_SNAKE_CASE : Optional[Any] = None if self.use_token_type_ids: __SCREAMING_SNAKE_CASE : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __SCREAMING_SNAKE_CASE : Dict = None __SCREAMING_SNAKE_CASE : Optional[int] = None __SCREAMING_SNAKE_CASE : Union[str, Any] = None if self.use_labels: __SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __SCREAMING_SNAKE_CASE : Optional[Any] = ids_tensor([self.batch_size] , self.num_choices ) __SCREAMING_SNAKE_CASE : Optional[int] = OpenAIGPTConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) __SCREAMING_SNAKE_CASE : Any = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, head_mask, token_type_ids, sequence_labels, token_labels, choice_labels, ) def __magic_name__( self :Tuple , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :List[str] , lowerCAmelCase__ :Dict , lowerCAmelCase__ :Any , *lowerCAmelCase__ :Union[str, Any] ) -> Any: __SCREAMING_SNAKE_CASE : Any = OpenAIGPTModel(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() __SCREAMING_SNAKE_CASE : Dict = model(lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , head_mask=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Tuple = model(lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : str = model(lowerCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __magic_name__( self :Optional[Any] , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :Any , lowerCAmelCase__ :Dict , *lowerCAmelCase__ :List[Any] ) -> Dict: __SCREAMING_SNAKE_CASE : Optional[Any] = OpenAIGPTLMHeadModel(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() __SCREAMING_SNAKE_CASE : Tuple = model(lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __magic_name__( self :Tuple , lowerCAmelCase__ :Dict , lowerCAmelCase__ :str , lowerCAmelCase__ :Optional[Any] , lowerCAmelCase__ :List[str] , *lowerCAmelCase__ :Optional[Any] ) -> Any: __SCREAMING_SNAKE_CASE : Any = OpenAIGPTDoubleHeadsModel(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() __SCREAMING_SNAKE_CASE : Any = model(lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __magic_name__( self :Dict , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :str , *lowerCAmelCase__ :Optional[int] ) -> Dict: __SCREAMING_SNAKE_CASE : Optional[Any] = self.num_labels __SCREAMING_SNAKE_CASE : List[Any] = OpenAIGPTForSequenceClassification(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() __SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __SCREAMING_SNAKE_CASE : Optional[Any] = model(lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __magic_name__( self :Optional[Any] ) -> str: __SCREAMING_SNAKE_CASE : str = self.prepare_config_and_inputs() ( ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ) : List[str] = config_and_inputs __SCREAMING_SNAKE_CASE : List[str] = { '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask, } return config, inputs_dict @require_torch class _lowercase ( A__ , A__ , A__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : int = ( (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification) if is_torch_available() else () ) SCREAMING_SNAKE_CASE__ : str = ( (OpenAIGPTLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly SCREAMING_SNAKE_CASE__ : str = ( { '''feature-extraction''': OpenAIGPTModel, '''text-classification''': OpenAIGPTForSequenceClassification, '''text-generation''': OpenAIGPTLMHeadModel, '''zero-shot''': OpenAIGPTForSequenceClassification, } if is_torch_available() else {} ) def __magic_name__( self :Optional[int] , lowerCAmelCase__ :str , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :Union[str, Any] ) -> Tuple: if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a # tiny config could not be created. return True return False def __magic_name__( self :List[str] , lowerCAmelCase__ :Dict , lowerCAmelCase__ :int , lowerCAmelCase__ :int=False ) -> Dict: __SCREAMING_SNAKE_CASE : Tuple = super()._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ , return_labels=lowerCAmelCase__ ) if return_labels: if model_class.__name__ == "OpenAIGPTDoubleHeadsModel": __SCREAMING_SNAKE_CASE : Any = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length) , dtype=torch.long , device=lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE : Tuple = inputs_dict['''labels'''] __SCREAMING_SNAKE_CASE : Dict = inputs_dict['''labels'''] __SCREAMING_SNAKE_CASE : List[Any] = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices) , dtype=torch.long , device=lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE : Optional[int] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCAmelCase__ ) return inputs_dict def __magic_name__( self :Optional[int] ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : int = OpenAIGPTModelTester(self ) __SCREAMING_SNAKE_CASE : Optional[Any] = ConfigTester(self , config_class=lowerCAmelCase__ , n_embd=37 ) def __magic_name__( self :Any ) -> Optional[Any]: self.config_tester.run_common_tests() def __magic_name__( self :List[str] ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__ ) def __magic_name__( self :int ) -> int: __SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase__ ) def __magic_name__( self :List[str] ) -> Dict: __SCREAMING_SNAKE_CASE : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase__ ) def __magic_name__( self :List[str] ) -> str: __SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase__ ) @slow def __magic_name__( self :Any ) -> List[Any]: for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __SCREAMING_SNAKE_CASE : Dict = OpenAIGPTModel.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) @require_torch class _lowercase ( unittest.TestCase ): '''simple docstring''' @slow def __magic_name__( self :Union[str, Any] ) -> Optional[int]: __SCREAMING_SNAKE_CASE : List[str] = OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt''' ) model.to(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = torch.tensor([[481, 4_735, 544]] , dtype=torch.long , device=lowerCAmelCase__ ) # the president is __SCREAMING_SNAKE_CASE : Dict = [ 481, 4_735, 544, 246, 963, 870, 762, 239, 244, 40_477, 244, 249, 719, 881, 487, 544, 240, 244, 603, 481, ] # the president is a very good man. " \n " i\'m sure he is, " said the __SCREAMING_SNAKE_CASE : Dict = model.generate(lowerCAmelCase__ , do_sample=lowerCAmelCase__ ) self.assertListEqual(output_ids[0].tolist() , lowerCAmelCase__ )
9
1
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 from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __lowerCAmelCase : Optional[Any] =logging.get_logger(__name__) __lowerCAmelCase : Optional[int] ={ 'microsoft/resnet-50': 'https://huggingface.co/microsoft/resnet-50/blob/main/config.json', } class _lowercase ( A__ , A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = '''resnet''' SCREAMING_SNAKE_CASE__ : Optional[Any] = ['''basic''', '''bottleneck'''] def __init__( self :List[Any] , lowerCAmelCase__ :Dict=3 , lowerCAmelCase__ :int=64 , lowerCAmelCase__ :List[str]=[256, 512, 1_024, 2_048] , lowerCAmelCase__ :Any=[3, 4, 6, 3] , lowerCAmelCase__ :str="bottleneck" , lowerCAmelCase__ :List[Any]="relu" , lowerCAmelCase__ :List[Any]=False , lowerCAmelCase__ :int=None , lowerCAmelCase__ :Dict=None , **lowerCAmelCase__ :Tuple , ) -> Optional[int]: super().__init__(**lowerCAmelCase__ ) if layer_type not in self.layer_types: raise ValueError(f'''layer_type={layer_type} is not one of {','.join(self.layer_types )}''' ) __SCREAMING_SNAKE_CASE : List[Any] = num_channels __SCREAMING_SNAKE_CASE : Optional[Any] = embedding_size __SCREAMING_SNAKE_CASE : Tuple = hidden_sizes __SCREAMING_SNAKE_CASE : List[str] = depths __SCREAMING_SNAKE_CASE : Tuple = layer_type __SCREAMING_SNAKE_CASE : Tuple = hidden_act __SCREAMING_SNAKE_CASE : int = downsample_in_first_stage __SCREAMING_SNAKE_CASE : List[Any] = ['''stem'''] + [f'''stage{idx}''' for idx in range(1 , len(lowerCAmelCase__ ) + 1 )] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Optional[Any] = get_aligned_output_features_output_indices( out_features=lowerCAmelCase__ , out_indices=lowerCAmelCase__ , stage_names=self.stage_names ) class _lowercase ( A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : int = version.parse('''1.11''' ) @property def __magic_name__( self :List[str] ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __magic_name__( self :Optional[Any] ) -> float: return 1E-3
9
import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _UpperCamelCase ( *lowercase__ , lowercase__ = None , lowercase__=True , lowercase__=2 ): from .. import __version__ __SCREAMING_SNAKE_CASE : Optional[Any] = take_from __SCREAMING_SNAKE_CASE : List[str] = () if not isinstance(args[0] , lowercase__ ): __SCREAMING_SNAKE_CASE : List[Any] = (args,) for attribute, version_name, message in args: if version.parse(version.parse(lowercase__ ).base_version ) >= version.parse(lowercase__ ): raise ValueError( F'''The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\'''' F''' version {__version__} is >= {version_name}''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = None if isinstance(lowercase__ , lowercase__ ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(lowercase__ ),) __SCREAMING_SNAKE_CASE : List[Any] = F'''The `{attribute}` argument is deprecated and will be removed in version {version_name}.''' elif hasattr(lowercase__ , lowercase__ ): values += (getattr(lowercase__ , lowercase__ ),) __SCREAMING_SNAKE_CASE : List[str] = F'''The `{attribute}` attribute is deprecated and will be removed in version {version_name}.''' elif deprecated_kwargs is None: __SCREAMING_SNAKE_CASE : str = F'''`{attribute}` is deprecated and will be removed in version {version_name}.''' if warning is not None: __SCREAMING_SNAKE_CASE : Any = warning + ''' ''' if standard_warn else '''''' warnings.warn(warning + message , lowercase__ , stacklevel=lowercase__ ) if isinstance(lowercase__ , lowercase__ ) and len(lowercase__ ) > 0: __SCREAMING_SNAKE_CASE : Union[str, Any] = inspect.getouterframes(inspect.currentframe() )[1] __SCREAMING_SNAKE_CASE : Dict = call_frame.filename __SCREAMING_SNAKE_CASE : Optional[Any] = call_frame.lineno __SCREAMING_SNAKE_CASE : int = call_frame.function __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : int = next(iter(deprecated_kwargs.items() ) ) raise TypeError(F'''{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`''' ) if len(lowercase__ ) == 0: return elif len(lowercase__ ) == 1: return values[0] return values
9
1
from __future__ import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass __lowerCAmelCase : Dict =(3, 9, -1_1, 0, 7, 5, 1, -1) __lowerCAmelCase : int =(4, 6, 2, 0, 8, 1_0, 3, -2) @dataclass class _lowercase : '''simple docstring''' SCREAMING_SNAKE_CASE__ : int SCREAMING_SNAKE_CASE__ : Node | None class _lowercase : '''simple docstring''' def __init__( self :str , lowerCAmelCase__ :Iterable[int] ) -> None: __SCREAMING_SNAKE_CASE : Node | None = None for i in sorted(lowerCAmelCase__ , reverse=lowerCAmelCase__ ): __SCREAMING_SNAKE_CASE : int = Node(lowerCAmelCase__ , self.head ) def __iter__( self :Optional[Any] ) -> Iterator[int]: __SCREAMING_SNAKE_CASE : Tuple = self.head while node: yield node.data __SCREAMING_SNAKE_CASE : int = node.next_node def __len__( self :Optional[int] ) -> int: return sum(1 for _ in self ) def __str__( self :List[Any] ) -> str: return " -> ".join([str(lowerCAmelCase__ ) for node in self] ) def _UpperCamelCase ( lowercase__ , lowercase__ ): return SortedLinkedList(list(lowercase__ ) + list(lowercase__ ) ) if __name__ == "__main__": import doctest doctest.testmod() __lowerCAmelCase : Union[str, Any] =SortedLinkedList print(merge_lists(SSL(test_data_odd), SSL(test_data_even)))
9
from __future__ import annotations import bisect def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ = 0 , lowercase__ = -1 ): if hi < 0: __SCREAMING_SNAKE_CASE : Union[str, Any] = len(lowercase__ ) while lo < hi: __SCREAMING_SNAKE_CASE : Any = lo + (hi - lo) // 2 if sorted_collection[mid] < item: __SCREAMING_SNAKE_CASE : Union[str, Any] = mid + 1 else: __SCREAMING_SNAKE_CASE : Optional[Any] = mid return lo def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ = 0 , lowercase__ = -1 ): if hi < 0: __SCREAMING_SNAKE_CASE : List[Any] = len(lowercase__ ) while lo < hi: __SCREAMING_SNAKE_CASE : Optional[int] = lo + (hi - lo) // 2 if sorted_collection[mid] <= item: __SCREAMING_SNAKE_CASE : Any = mid + 1 else: __SCREAMING_SNAKE_CASE : Optional[int] = mid return lo def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ = 0 , lowercase__ = -1 ): sorted_collection.insert(bisect_left(lowercase__ , lowercase__ , lowercase__ , lowercase__ ) , lowercase__ ) def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ = 0 , lowercase__ = -1 ): sorted_collection.insert(bisect_right(lowercase__ , lowercase__ , lowercase__ , lowercase__ ) , lowercase__ ) def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Any = 0 __SCREAMING_SNAKE_CASE : List[Any] = len(lowercase__ ) - 1 while left <= right: __SCREAMING_SNAKE_CASE : str = left + (right - left) // 2 __SCREAMING_SNAKE_CASE : List[str] = sorted_collection[midpoint] if current_item == item: return midpoint elif item < current_item: __SCREAMING_SNAKE_CASE : int = midpoint - 1 else: __SCREAMING_SNAKE_CASE : Dict = midpoint + 1 return None def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Union[str, Any] = bisect.bisect_left(lowercase__ , lowercase__ ) if index != len(lowercase__ ) and sorted_collection[index] == item: return index return None def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , lowercase__ ): if right < left: return None __SCREAMING_SNAKE_CASE : int = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: return binary_search_by_recursion(lowercase__ , lowercase__ , lowercase__ , midpoint - 1 ) else: return binary_search_by_recursion(lowercase__ , lowercase__ , midpoint + 1 , lowercase__ ) if __name__ == "__main__": __lowerCAmelCase : Dict =input('Enter numbers separated by comma:\n').strip() __lowerCAmelCase : str =sorted(int(item) for item in user_input.split(',')) __lowerCAmelCase : Tuple =int(input('Enter a single number to be found in the list:\n')) __lowerCAmelCase : Tuple =binary_search(collection, target) if result is None: print(f"""{target} was not found in {collection}.""") else: print(f"""{target} was found at position {result} in {collection}.""")
9
1
import unittest import numpy as np import torch from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad class _lowercase ( unittest.TestCase ): '''simple docstring''' def __magic_name__( self :Dict ) -> Dict: __SCREAMING_SNAKE_CASE : Any = 10 def __magic_name__( self :Optional[Any] ) -> Optional[int]: __SCREAMING_SNAKE_CASE : Tuple = [1, 2, 3, 4] __SCREAMING_SNAKE_CASE : str = [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] self.assertEqual(truncate_or_pad(lowerCAmelCase__ , self.block_size , 0 ) , lowerCAmelCase__ ) def __magic_name__( self :int ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : Union[str, Any] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] __SCREAMING_SNAKE_CASE : int = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(lowerCAmelCase__ , self.block_size , 0 ) , lowerCAmelCase__ ) def __magic_name__( self :List[Any] ) -> Optional[Any]: __SCREAMING_SNAKE_CASE : Tuple = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] __SCREAMING_SNAKE_CASE : int = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(lowerCAmelCase__ , self.block_size , 0 ) , lowerCAmelCase__ ) def __magic_name__( self :Optional[Any] ) -> Tuple: __SCREAMING_SNAKE_CASE : Any = '''It was the year of Our Lord one thousand seven hundred and seventy-five.\n\nSpiritual revelations were conceded to England at that favoured period, as at this.''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Optional[int] = process_story(lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , [] ) def __magic_name__( self :List[str] ) -> Optional[int]: __SCREAMING_SNAKE_CASE : List[str] = '''''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : str = process_story(lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , [] ) self.assertEqual(lowerCAmelCase__ , [] ) def __magic_name__( self :Dict ) -> List[Any]: __SCREAMING_SNAKE_CASE : List[str] = ( '''It was the year of Our Lord one thousand seven hundred and ''' '''seventy-five\n\nSpiritual revelations were conceded to England ''' '''at that favoured period, as at this.\n@highlight\n\nIt was the best of times''' ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Union[str, Any] = process_story(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : int = [ '''It was the year of Our Lord one thousand seven hundred and seventy-five.''', '''Spiritual revelations were conceded to England at that favoured period, as at this.''', ] self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Union[str, Any] = ['''It was the best of times.'''] self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) def __magic_name__( self :Tuple ) -> str: __SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor([1, 2, 3, 4] ) __SCREAMING_SNAKE_CASE : List[str] = torch.tensor([1, 1, 1, 1] ) np.testing.assert_array_equal(build_mask(lowerCAmelCase__ , 0 ).numpy() , expected.numpy() ) def __magic_name__( self :Any ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : Tuple = torch.tensor([1, 2, 3, 4, 23, 23, 23] ) __SCREAMING_SNAKE_CASE : int = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(lowerCAmelCase__ , 23 ).numpy() , expected.numpy() ) def __magic_name__( self :str ) -> Optional[Any]: __SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor([8, 2, 3, 4, 1, 1, 1] ) __SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(lowerCAmelCase__ , 1 ).numpy() , expected.numpy() ) def __magic_name__( self :str ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : int = 101 __SCREAMING_SNAKE_CASE : Optional[int] = torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 101, 5, 6], [1, 101, 3, 4, 101, 6]] ) __SCREAMING_SNAKE_CASE : str = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]] ) __SCREAMING_SNAKE_CASE : Tuple = compute_token_type_ids(lowerCAmelCase__ , lowerCAmelCase__ ) np.testing.assert_array_equal(lowerCAmelCase__ , lowerCAmelCase__ )
9
import os import shutil import tempfile import unittest import numpy as np from transformers import AutoTokenizer, BarkProcessor from transformers.testing_utils import require_torch, slow @require_torch class _lowercase ( unittest.TestCase ): '''simple docstring''' def __magic_name__( self :Union[str, Any] ) -> Tuple: __SCREAMING_SNAKE_CASE : str = '''ylacombe/bark-small''' __SCREAMING_SNAKE_CASE : Optional[int] = tempfile.mkdtemp() __SCREAMING_SNAKE_CASE : str = '''en_speaker_1''' __SCREAMING_SNAKE_CASE : Any = '''This is a test string''' __SCREAMING_SNAKE_CASE : int = '''speaker_embeddings_path.json''' __SCREAMING_SNAKE_CASE : int = '''speaker_embeddings''' def __magic_name__( self :List[str] , **lowerCAmelCase__ :Union[str, Any] ) -> Any: return AutoTokenizer.from_pretrained(self.checkpoint , **lowerCAmelCase__ ) def __magic_name__( self :List[str] ) -> int: shutil.rmtree(self.tmpdirname ) def __magic_name__( self :Dict ) -> str: __SCREAMING_SNAKE_CASE : Dict = self.get_tokenizer() __SCREAMING_SNAKE_CASE : Tuple = BarkProcessor(tokenizer=lowerCAmelCase__ ) processor.save_pretrained(self.tmpdirname ) __SCREAMING_SNAKE_CASE : Optional[Any] = BarkProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) @slow def __magic_name__( self :Tuple ) -> List[Any]: __SCREAMING_SNAKE_CASE : Dict = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) processor.save_pretrained( self.tmpdirname , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , speaker_embeddings_directory=self.speaker_embeddings_directory , ) __SCREAMING_SNAKE_CASE : Dict = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) __SCREAMING_SNAKE_CASE : Union[str, Any] = BarkProcessor.from_pretrained( self.tmpdirname , self.speaker_embeddings_dict_path , bos_token='''(BOS)''' , eos_token='''(EOS)''' , ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) def __magic_name__( self :List[str] ) -> Tuple: __SCREAMING_SNAKE_CASE : List[Any] = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) __SCREAMING_SNAKE_CASE : str = 35 __SCREAMING_SNAKE_CASE : str = 2 __SCREAMING_SNAKE_CASE : List[Any] = 8 __SCREAMING_SNAKE_CASE : Union[str, Any] = { '''semantic_prompt''': np.ones(lowerCAmelCase__ ), '''coarse_prompt''': np.ones((nb_codebooks_coarse, seq_len) ), '''fine_prompt''': np.ones((nb_codebooks_total, seq_len) ), } # test providing already loaded voice_preset __SCREAMING_SNAKE_CASE : Union[str, Any] = processor(text=self.input_string , voice_preset=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[str] = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(lowerCAmelCase__ , np.array([] ) ).tolist() ) # test loading voice preset from npz file __SCREAMING_SNAKE_CASE : str = os.path.join(self.tmpdirname , '''file.npz''' ) np.savez(lowerCAmelCase__ , **lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[Any] = processor(text=self.input_string , voice_preset=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Union[str, Any] = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(lowerCAmelCase__ , np.array([] ) ).tolist() ) # test loading voice preset from the hub __SCREAMING_SNAKE_CASE : Union[str, Any] = processor(text=self.input_string , voice_preset=self.voice_preset ) def __magic_name__( self :Tuple ) -> Optional[Any]: __SCREAMING_SNAKE_CASE : Tuple = self.get_tokenizer() __SCREAMING_SNAKE_CASE : Any = BarkProcessor(tokenizer=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[Any] = processor(text=self.input_string ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer( self.input_string , padding='''max_length''' , max_length=256 , add_special_tokens=lowerCAmelCase__ , return_attention_mask=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ , ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key].squeeze().tolist() )
9
1
import io import json import fsspec import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.json import JsonDatasetReader, JsonDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def _UpperCamelCase ( lowercase__ , lowercase__ ): assert isinstance(lowercase__ , lowercase__ ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Dict = tmp_path / '''cache''' __SCREAMING_SNAKE_CASE : Optional[Any] = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): __SCREAMING_SNAKE_CASE : Union[str, Any] = JsonDatasetReader(lowercase__ , cache_dir=lowercase__ , keep_in_memory=lowercase__ ).read() _check_json_dataset(lowercase__ , lowercase__ ) @pytest.mark.parametrize( '''features''' , [ None, {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}, {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''}, {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''}, {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''}, ] , ) def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : List[Any] = tmp_path / '''cache''' __SCREAMING_SNAKE_CASE : str = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} __SCREAMING_SNAKE_CASE : Union[str, Any] = features.copy() if features else default_expected_features __SCREAMING_SNAKE_CASE : Any = ( Features({feature: Value(lowercase__ ) for feature, dtype in features.items()} ) if features is not None else None ) __SCREAMING_SNAKE_CASE : str = JsonDatasetReader(lowercase__ , features=lowercase__ , cache_dir=lowercase__ ).read() _check_json_dataset(lowercase__ , lowercase__ ) @pytest.mark.parametrize( '''features''' , [ None, {'''col_3''': '''float64''', '''col_1''': '''string''', '''col_2''': '''int64'''}, ] , ) def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : List[Any] = tmp_path / '''cache''' __SCREAMING_SNAKE_CASE : Dict = {'''col_3''': '''float64''', '''col_1''': '''string''', '''col_2''': '''int64'''} __SCREAMING_SNAKE_CASE : Tuple = features.copy() if features else default_expected_features __SCREAMING_SNAKE_CASE : Dict = ( Features({feature: Value(lowercase__ ) for feature, dtype in features.items()} ) if features is not None else None ) __SCREAMING_SNAKE_CASE : Optional[Any] = JsonDatasetReader(lowercase__ , features=lowercase__ , cache_dir=lowercase__ ).read() assert isinstance(lowercase__ , lowercase__ ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_3", "col_1", "col_2"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype def _UpperCamelCase ( lowercase__ , lowercase__ ): # jsonl_312_path features are {"col_3": "float64", "col_1": "string", "col_2": "int64"} __SCREAMING_SNAKE_CASE : Optional[Any] = {'''col_2''': '''int64''', '''col_3''': '''float64''', '''col_1''': '''string'''} __SCREAMING_SNAKE_CASE : List[Any] = features.copy() __SCREAMING_SNAKE_CASE : List[Any] = ( Features({feature: Value(lowercase__ ) for feature, dtype in features.items()} ) if features is not None else None ) __SCREAMING_SNAKE_CASE : Dict = tmp_path / '''cache''' __SCREAMING_SNAKE_CASE : Dict = JsonDatasetReader(lowercase__ , features=lowercase__ , cache_dir=lowercase__ ).read() assert isinstance(lowercase__ , lowercase__ ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_2", "col_3", "col_1"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Dict = tmp_path / '''cache''' __SCREAMING_SNAKE_CASE : Dict = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} __SCREAMING_SNAKE_CASE : List[str] = JsonDatasetReader(lowercase__ , cache_dir=lowercase__ , split=lowercase__ ).read() _check_json_dataset(lowercase__ , lowercase__ ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('''path_type''' , [str, list] ) def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): if issubclass(lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Dict = jsonl_path elif issubclass(lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : int = [jsonl_path] __SCREAMING_SNAKE_CASE : int = tmp_path / '''cache''' __SCREAMING_SNAKE_CASE : Any = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} __SCREAMING_SNAKE_CASE : List[Any] = JsonDatasetReader(lowercase__ , cache_dir=lowercase__ ).read() _check_json_dataset(lowercase__ , lowercase__ ) def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__=("train",) ): assert isinstance(lowercase__ , lowercase__ ) for split in splits: __SCREAMING_SNAKE_CASE : Optional[int] = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Dict = tmp_path / '''cache''' __SCREAMING_SNAKE_CASE : Union[str, Any] = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): __SCREAMING_SNAKE_CASE : Dict = JsonDatasetReader({'''train''': jsonl_path} , cache_dir=lowercase__ , keep_in_memory=lowercase__ ).read() _check_json_datasetdict(lowercase__ , lowercase__ ) @pytest.mark.parametrize( '''features''' , [ None, {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}, {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''}, {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''}, {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''}, ] , ) def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Union[str, Any] = tmp_path / '''cache''' __SCREAMING_SNAKE_CASE : List[Any] = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} __SCREAMING_SNAKE_CASE : Union[str, Any] = features.copy() if features else default_expected_features __SCREAMING_SNAKE_CASE : Optional[Any] = ( Features({feature: Value(lowercase__ ) for feature, dtype in features.items()} ) if features is not None else None ) __SCREAMING_SNAKE_CASE : List[Any] = JsonDatasetReader({'''train''': jsonl_path} , features=lowercase__ , cache_dir=lowercase__ ).read() _check_json_datasetdict(lowercase__ , lowercase__ ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): if split: __SCREAMING_SNAKE_CASE : Union[str, Any] = {split: jsonl_path} else: __SCREAMING_SNAKE_CASE : str = '''train''' __SCREAMING_SNAKE_CASE : List[str] = {'''train''': jsonl_path, '''test''': jsonl_path} __SCREAMING_SNAKE_CASE : Union[str, Any] = tmp_path / '''cache''' __SCREAMING_SNAKE_CASE : Union[str, Any] = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} __SCREAMING_SNAKE_CASE : Dict = JsonDatasetReader(lowercase__ , cache_dir=lowercase__ ).read() _check_json_datasetdict(lowercase__ , lowercase__ , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def _UpperCamelCase ( lowercase__ ): return json.load(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return [json.loads(lowercase__ ) for line in buffer] class _lowercase : '''simple docstring''' @pytest.mark.parametrize('''lines, load_json_function''' , [(True, load_json_lines), (False, load_json)] ) def __magic_name__( self :str , lowerCAmelCase__ :Dict , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :Any ) -> Optional[Any]: with io.BytesIO() as buffer: JsonDatasetWriter(lowerCAmelCase__ , lowerCAmelCase__ , lines=lowerCAmelCase__ ).write() buffer.seek(0 ) __SCREAMING_SNAKE_CASE : Tuple = load_json_function(lowerCAmelCase__ ) assert isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) assert isinstance(exported_content[0] , lowerCAmelCase__ ) assert len(lowerCAmelCase__ ) == 10 @pytest.mark.parametrize( '''orient, container, keys, len_at''' , [ ('''records''', list, {'''tokens''', '''labels''', '''answers''', '''id'''}, None), ('''split''', dict, {'''columns''', '''data'''}, '''data'''), ('''index''', dict, set('''0123456789''' ), None), ('''columns''', dict, {'''tokens''', '''labels''', '''answers''', '''id'''}, '''tokens'''), ('''values''', list, None, None), ('''table''', dict, {'''schema''', '''data'''}, '''data'''), ] , ) def __magic_name__( self :Optional[int] , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :Tuple , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :List[Any] ) -> Optional[Any]: with io.BytesIO() as buffer: JsonDatasetWriter(lowerCAmelCase__ , lowerCAmelCase__ , lines=lowerCAmelCase__ , orient=lowerCAmelCase__ ).write() buffer.seek(0 ) __SCREAMING_SNAKE_CASE : List[Any] = load_json(lowerCAmelCase__ ) assert isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(lowerCAmelCase__ , '''keys''' ) and not hasattr(exported_content[0] , '''keys''' ) if len_at: assert len(exported_content[len_at] ) == 10 else: assert len(lowerCAmelCase__ ) == 10 @pytest.mark.parametrize('''lines, load_json_function''' , [(True, load_json_lines), (False, load_json)] ) def __magic_name__( self :Optional[Any] , lowerCAmelCase__ :str , lowerCAmelCase__ :Any , lowerCAmelCase__ :Union[str, Any] ) -> Dict: with io.BytesIO() as buffer: JsonDatasetWriter(lowerCAmelCase__ , lowerCAmelCase__ , lines=lowerCAmelCase__ , num_proc=2 ).write() buffer.seek(0 ) __SCREAMING_SNAKE_CASE : Tuple = load_json_function(lowerCAmelCase__ ) assert isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) assert isinstance(exported_content[0] , lowerCAmelCase__ ) assert len(lowerCAmelCase__ ) == 10 @pytest.mark.parametrize( '''orient, container, keys, len_at''' , [ ('''records''', list, {'''tokens''', '''labels''', '''answers''', '''id'''}, None), ('''split''', dict, {'''columns''', '''data'''}, '''data'''), ('''index''', dict, set('''0123456789''' ), None), ('''columns''', dict, {'''tokens''', '''labels''', '''answers''', '''id'''}, '''tokens'''), ('''values''', list, None, None), ('''table''', dict, {'''schema''', '''data'''}, '''data'''), ] , ) def __magic_name__( self :Any , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :Tuple , lowerCAmelCase__ :Optional[Any] , lowerCAmelCase__ :Optional[Any] ) -> Optional[Any]: with io.BytesIO() as buffer: JsonDatasetWriter(lowerCAmelCase__ , lowerCAmelCase__ , lines=lowerCAmelCase__ , orient=lowerCAmelCase__ , num_proc=2 ).write() buffer.seek(0 ) __SCREAMING_SNAKE_CASE : Tuple = load_json(lowerCAmelCase__ ) assert isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(lowerCAmelCase__ , '''keys''' ) and not hasattr(exported_content[0] , '''keys''' ) if len_at: assert len(exported_content[len_at] ) == 10 else: assert len(lowerCAmelCase__ ) == 10 def __magic_name__( self :Dict , lowerCAmelCase__ :Optional[Any] ) -> int: with pytest.raises(lowerCAmelCase__ ): with io.BytesIO() as buffer: JsonDatasetWriter(lowerCAmelCase__ , lowerCAmelCase__ , num_proc=0 ) @pytest.mark.parametrize('''compression, extension''' , [('''gzip''', '''gz'''), ('''bz2''', '''bz2'''), ('''xz''', '''xz''')] ) def __magic_name__( self :Any , lowerCAmelCase__ :Dict , lowerCAmelCase__ :int , lowerCAmelCase__ :List[str] , lowerCAmelCase__ :int , lowerCAmelCase__ :Optional[Any] ) -> int: __SCREAMING_SNAKE_CASE : Tuple = tmp_path_factory.mktemp('''data''' ) / f'''test.json.{extension}''' __SCREAMING_SNAKE_CASE : Dict = str(shared_datadir / f'''test_file.json.{extension}''' ) JsonDatasetWriter(lowerCAmelCase__ , lowerCAmelCase__ , compression=lowerCAmelCase__ ).write() with fsspec.open(lowerCAmelCase__ , '''rb''' , compression='''infer''' ) as f: __SCREAMING_SNAKE_CASE : Dict = f.read() with fsspec.open(lowerCAmelCase__ , '''rb''' , compression='''infer''' ) as f: __SCREAMING_SNAKE_CASE : str = f.read() assert exported_content == original_content
9
from importlib import import_module from .logging import get_logger __lowerCAmelCase : str =get_logger(__name__) class _lowercase : '''simple docstring''' def __init__( self :List[Any] , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :str=None ) -> int: __SCREAMING_SNAKE_CASE : List[str] = attrs or [] if module is not None: for key in module.__dict__: if key in attrs or not key.startswith('''__''' ): setattr(self , lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) __SCREAMING_SNAKE_CASE : Optional[Any] = module._original_module if isinstance(lowerCAmelCase__ , _PatchedModuleObj ) else module class _lowercase : '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = [] def __init__( self :Tuple , lowerCAmelCase__ :Optional[Any] , lowerCAmelCase__ :str , lowerCAmelCase__ :Any , lowerCAmelCase__ :Dict=None ) -> List[Any]: __SCREAMING_SNAKE_CASE : Optional[int] = obj __SCREAMING_SNAKE_CASE : str = target __SCREAMING_SNAKE_CASE : Dict = new __SCREAMING_SNAKE_CASE : Union[str, Any] = target.split('''.''' )[0] __SCREAMING_SNAKE_CASE : List[str] = {} __SCREAMING_SNAKE_CASE : Tuple = attrs or [] def __enter__( self :int ) -> Dict: *__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Optional[Any] = self.target.split('''.''' ) # Patch modules: # it's used to patch attributes of submodules like "os.path.join"; # in this case we need to patch "os" and "os.path" for i in range(len(lowerCAmelCase__ ) ): try: __SCREAMING_SNAKE_CASE : Any = import_module('''.'''.join(submodules[: i + 1] ) ) except ModuleNotFoundError: continue # We iterate over all the globals in self.obj in case we find "os" or "os.path" for attr in self.obj.__dir__(): __SCREAMING_SNAKE_CASE : Union[str, Any] = getattr(self.obj , lowerCAmelCase__ ) # We don't check for the name of the global, but rather if its value *is* "os" or "os.path". # This allows to patch renamed modules like "from os import path as ospath". if obj_attr is submodule or ( (isinstance(lowerCAmelCase__ , _PatchedModuleObj ) and obj_attr._original_module is submodule) ): __SCREAMING_SNAKE_CASE : int = obj_attr # patch at top level setattr(self.obj , lowerCAmelCase__ , _PatchedModuleObj(lowerCAmelCase__ , attrs=self.attrs ) ) __SCREAMING_SNAKE_CASE : List[str] = getattr(self.obj , lowerCAmelCase__ ) # construct lower levels patches for key in submodules[i + 1 :]: setattr(lowerCAmelCase__ , lowerCAmelCase__ , _PatchedModuleObj(getattr(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) , attrs=self.attrs ) ) __SCREAMING_SNAKE_CASE : Tuple = getattr(lowerCAmelCase__ , lowerCAmelCase__ ) # finally set the target attribute setattr(lowerCAmelCase__ , lowerCAmelCase__ , self.new ) # Patch attribute itself: # it's used for builtins like "open", # and also to patch "os.path.join" we may also need to patch "join" # itself if it was imported as "from os.path import join". if submodules: # if it's an attribute of a submodule like "os.path.join" try: __SCREAMING_SNAKE_CASE : Union[str, Any] = getattr(import_module('''.'''.join(lowerCAmelCase__ ) ) , lowerCAmelCase__ ) except (AttributeError, ModuleNotFoundError): return # We iterate over all the globals in self.obj in case we find "os.path.join" for attr in self.obj.__dir__(): # We don't check for the name of the global, but rather if its value *is* "os.path.join". # This allows to patch renamed attributes like "from os.path import join as pjoin". if getattr(self.obj , lowerCAmelCase__ ) is attr_value: __SCREAMING_SNAKE_CASE : Any = getattr(self.obj , lowerCAmelCase__ ) setattr(self.obj , lowerCAmelCase__ , self.new ) elif target_attr in globals()["__builtins__"]: # if it'a s builtin like "open" __SCREAMING_SNAKE_CASE : Union[str, Any] = globals()['''__builtins__'''][target_attr] setattr(self.obj , lowerCAmelCase__ , self.new ) else: raise RuntimeError(f'''Tried to patch attribute {target_attr} instead of a submodule.''' ) def __exit__( self :str , *lowerCAmelCase__ :Union[str, Any] ) -> Optional[int]: for attr in list(self.original ): setattr(self.obj , lowerCAmelCase__ , self.original.pop(lowerCAmelCase__ ) ) def __magic_name__( self :List[Any] ) -> List[Any]: self.__enter__() self._active_patches.append(self ) def __magic_name__( self :Optional[int] ) -> int: try: self._active_patches.remove(self ) except ValueError: # If the patch hasn't been started this will fail return None return self.__exit__()
9
1
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __lowerCAmelCase : str ={'configuration_mra': ['MRA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MraConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Union[str, Any] =[ 'MRA_PRETRAINED_MODEL_ARCHIVE_LIST', 'MraForMaskedLM', 'MraForMultipleChoice', 'MraForQuestionAnswering', 'MraForSequenceClassification', 'MraForTokenClassification', 'MraLayer', 'MraModel', 'MraPreTrainedModel', ] if TYPE_CHECKING: from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mra import ( MRA_PRETRAINED_MODEL_ARCHIVE_LIST, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraLayer, MraModel, MraPreTrainedModel, ) else: import sys __lowerCAmelCase : Any =_LazyModule(__name__, globals()['__file__'], _import_structure)
9
import math import os from copy import deepcopy import datasets import evaluate import torch import transformers from datasets import load_dataset from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer from accelerate import Accelerator from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import is_tpu_available, set_seed __lowerCAmelCase : List[str] ='true' def _UpperCamelCase ( lowercase__ , lowercase__=82 , lowercase__=16 ): set_seed(42 ) __SCREAMING_SNAKE_CASE : Optional[int] = RegressionModel() __SCREAMING_SNAKE_CASE : Optional[int] = deepcopy(lowercase__ ) __SCREAMING_SNAKE_CASE : Any = RegressionDataset(length=lowercase__ ) __SCREAMING_SNAKE_CASE : Union[str, Any] = DataLoader(lowercase__ , batch_size=lowercase__ ) model.to(accelerator.device ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Optional[Any] = accelerator.prepare(lowercase__ , lowercase__ ) return model, ddp_model, dataloader def _UpperCamelCase ( lowercase__ , lowercase__=False ): __SCREAMING_SNAKE_CASE : Optional[Any] = AutoTokenizer.from_pretrained('''hf-internal-testing/mrpc-bert-base-cased''' ) __SCREAMING_SNAKE_CASE : str = load_dataset('''glue''' , '''mrpc''' , split='''validation''' ) def tokenize_function(lowercase__ ): __SCREAMING_SNAKE_CASE : Dict = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=lowercase__ , max_length=lowercase__ ) return outputs with accelerator.main_process_first(): __SCREAMING_SNAKE_CASE : Tuple = dataset.map( lowercase__ , batched=lowercase__ , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) __SCREAMING_SNAKE_CASE : List[Any] = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(lowercase__ ): if use_longest: return tokenizer.pad(lowercase__ , padding='''longest''' , return_tensors='''pt''' ) return tokenizer.pad(lowercase__ , padding='''max_length''' , max_length=128 , return_tensors='''pt''' ) return DataLoader(lowercase__ , shuffle=lowercase__ , collate_fn=lowercase__ , batch_size=16 ) def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : str = Accelerator(dispatch_batches=lowercase__ , split_batches=lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = get_dataloader(lowercase__ , not dispatch_batches ) __SCREAMING_SNAKE_CASE : List[str] = AutoModelForSequenceClassification.from_pretrained( '''hf-internal-testing/mrpc-bert-base-cased''' , return_dict=lowercase__ ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : List[str] = accelerator.prepare(lowercase__ , lowercase__ ) return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : List[str] = [] for batch in dataloader: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Optional[Any] = batch.values() with torch.no_grad(): __SCREAMING_SNAKE_CASE : Dict = model(lowercase__ ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : str = accelerator.gather_for_metrics((logit, target) ) logits_and_targets.append((logit, target) ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : str = [], [] for logit, targ in logits_and_targets: logits.append(lowercase__ ) targs.append(lowercase__ ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Any = torch.cat(lowercase__ ), torch.cat(lowercase__ ) return logits, targs def _UpperCamelCase ( lowercase__ , lowercase__=82 , lowercase__=False , lowercase__=False , lowercase__=16 ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Tuple = get_basic_setup(lowercase__ , lowercase__ , lowercase__ ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : List[str] = generate_predictions(lowercase__ , lowercase__ , lowercase__ ) assert ( len(lowercase__ ) == num_samples ), F'''Unexpected number of inputs:\n Expected: {num_samples}\n Actual: {len(lowercase__ )}''' def _UpperCamelCase ( lowercase__ = False , lowercase__ = False ): __SCREAMING_SNAKE_CASE : Optional[Any] = evaluate.load('''glue''' , '''mrpc''' ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : List[Any] = get_mrpc_setup(lowercase__ , lowercase__ ) # First do baseline __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Optional[Any] = setup['''no'''] model.to(lowercase__ ) model.eval() for batch in dataloader: batch.to(lowercase__ ) with torch.inference_mode(): __SCREAMING_SNAKE_CASE : Dict = model(**lowercase__ ) __SCREAMING_SNAKE_CASE : Dict = outputs.logits.argmax(dim=-1 ) metric.add_batch(predictions=lowercase__ , references=batch['''labels'''] ) __SCREAMING_SNAKE_CASE : int = metric.compute() # Then do distributed __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Dict = setup['''ddp'''] model.eval() for batch in dataloader: with torch.inference_mode(): __SCREAMING_SNAKE_CASE : int = model(**lowercase__ ) __SCREAMING_SNAKE_CASE : str = outputs.logits.argmax(dim=-1 ) __SCREAMING_SNAKE_CASE : Any = batch['''labels'''] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Dict = accelerator.gather_for_metrics((preds, references) ) metric.add_batch(predictions=lowercase__ , references=lowercase__ ) __SCREAMING_SNAKE_CASE : List[Any] = metric.compute() for key in "accuracy f1".split(): assert math.isclose( baseline[key] , distributed[key] ), F'''Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n''' def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : Dict = Accelerator(split_batches=lowercase__ , dispatch_batches=lowercase__ ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_warning() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # These are a bit slower so they should only be ran on the GPU or TPU if torch.cuda.is_available() or is_tpu_available(): if accelerator.is_local_main_process: print('''**Testing gather_for_metrics**''' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: if accelerator.is_local_main_process: print(F'''With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`''' ) test_mrpc(lowercase__ , lowercase__ ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('''**Test torch metrics**''' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: __SCREAMING_SNAKE_CASE : List[Any] = Accelerator(split_batches=lowercase__ , dispatch_batches=lowercase__ ) if accelerator.is_local_main_process: print(F'''With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99''' ) test_torch_metrics(lowercase__ , 99 ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('''**Test last batch is not dropped when perfectly divisible**''' ) __SCREAMING_SNAKE_CASE : Tuple = Accelerator() test_torch_metrics(lowercase__ , 512 ) accelerator.state._reset_state() def _UpperCamelCase ( lowercase__ ): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
9
1
from operator import delitem, getitem, setitem import pytest from data_structures.hashing.hash_map import HashMap def _UpperCamelCase ( lowercase__ ): return getitem, k def _UpperCamelCase ( lowercase__ , lowercase__ ): return setitem, k, v def _UpperCamelCase ( lowercase__ ): return delitem, k def _UpperCamelCase ( lowercase__ , lowercase__ , *lowercase__ ): try: return fun(lowercase__ , *lowercase__ ), None except Exception as e: return None, e __lowerCAmelCase : Any =( _set('key_a', 'val_a'), _set('key_b', 'val_b'), ) __lowerCAmelCase : Optional[Any] =[ _set('key_a', 'val_a'), _set('key_a', 'val_b'), ] __lowerCAmelCase : Union[str, Any] =[ _set('key_a', 'val_a'), _set('key_b', 'val_b'), _del('key_a'), _del('key_b'), _set('key_a', 'val_a'), _del('key_a'), ] __lowerCAmelCase : int =[ _get('key_a'), _del('key_a'), _set('key_a', 'val_a'), _del('key_a'), _del('key_a'), _get('key_a'), ] __lowerCAmelCase : Optional[Any] =[ *[_set(x, x) for x in range(5)], # guaranteed upsize ] __lowerCAmelCase : Optional[Any] =[ *[_set(x, x) for x in range(5)], # guaranteed upsize *[_del(x) for x in range(5)], _set('key_a', 'val_b'), ] @pytest.mark.parametrize( '''operations''' , ( pytest.param(_add_items , id='''add items''' ), pytest.param(_overwrite_items , id='''overwrite items''' ), pytest.param(_delete_items , id='''delete items''' ), pytest.param(_access_absent_items , id='''access absent items''' ), pytest.param(_add_with_resize_up , id='''add with resize up''' ), pytest.param(_add_with_resize_down , id='''add with resize down''' ), ) , ) def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : Optional[int] = HashMap(initial_block_size=4 ) __SCREAMING_SNAKE_CASE : int = {} for _, (fun, *args) in enumerate(lowercase__ ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Optional[int] = _run_operation(lowercase__ , lowercase__ , *lowercase__ ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Union[str, Any] = _run_operation(lowercase__ , lowercase__ , *lowercase__ ) assert my_res == py_res assert str(lowercase__ ) == str(lowercase__ ) assert set(lowercase__ ) == set(lowercase__ ) assert len(lowercase__ ) == len(lowercase__ ) assert set(my.items() ) == set(py.items() ) def _UpperCamelCase ( ): def is_public(lowercase__ ) -> bool: return not name.startswith('''_''' ) __SCREAMING_SNAKE_CASE : List[str] = {name for name in dir({} ) if is_public(lowercase__ )} __SCREAMING_SNAKE_CASE : List[Any] = {name for name in dir(HashMap() ) if is_public(lowercase__ )} assert dict_public_names > hash_public_names
9
import importlib.metadata import operator import re import sys from typing import Optional from packaging import version __lowerCAmelCase : Union[str, Any] ={ '<': operator.lt, '<=': operator.le, '==': operator.eq, '!=': operator.ne, '>=': operator.ge, '>': operator.gt, } def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ ): if got_ver is None or want_ver is None: raise ValueError( F'''Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider''' F''' reinstalling {pkg}.''' ) if not ops[op](version.parse(lowercase__ ) , version.parse(lowercase__ ) ): raise ImportError( F'''{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}''' ) def _UpperCamelCase ( lowercase__ , lowercase__ = None ): __SCREAMING_SNAKE_CASE : Union[str, Any] = F'''\n{hint}''' if hint is not None else '''''' # non-versioned check if re.match(R'''^[\w_\-\d]+$''' , lowercase__ ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : str = requirement, None, None else: __SCREAMING_SNAKE_CASE : List[Any] = re.findall(R'''^([^!=<>\s]+)([\s!=<>]{1,2}.+)''' , lowercase__ ) if not match: raise ValueError( '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but''' F''' got {requirement}''' ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Tuple = match[0] __SCREAMING_SNAKE_CASE : Optional[int] = want_full.split(''',''' ) # there could be multiple requirements __SCREAMING_SNAKE_CASE : Optional[Any] = {} for w in want_range: __SCREAMING_SNAKE_CASE : Any = re.findall(R'''^([\s!=<>]{1,2})(.+)''' , lowercase__ ) if not match: raise ValueError( '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,''' F''' but got {requirement}''' ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Tuple = match[0] __SCREAMING_SNAKE_CASE : List[Any] = want_ver if op not in ops: raise ValueError(F'''{requirement}: need one of {list(ops.keys() )}, but got {op}''' ) # special case if pkg == "python": __SCREAMING_SNAKE_CASE : Optional[Any] = '''.'''.join([str(lowercase__ ) for x in sys.version_info[:3]] ) for op, want_ver in wanted.items(): _compare_versions(lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ ) return # check if any version is installed try: __SCREAMING_SNAKE_CASE : Optional[int] = importlib.metadata.version(lowercase__ ) except importlib.metadata.PackageNotFoundError: raise importlib.metadata.PackageNotFoundError( F'''The \'{requirement}\' distribution was not found and is required by this application. {hint}''' ) # check that the right version is installed if version number or a range was provided if want_ver is not None: for op, want_ver in wanted.items(): _compare_versions(lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ ) def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : Union[str, Any] = '''Try: pip install transformers -U or pip install -e \'.[dev]\' if you\'re working with git main''' return require_version(lowercase__ , lowercase__ )
9
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase : Any ={'configuration_vit_msn': ['VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ViTMSNConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : int =[ 'VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST', 'ViTMSNModel', 'ViTMSNForImageClassification', 'ViTMSNPreTrainedModel', ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys __lowerCAmelCase : Union[str, Any] =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
9
from __future__ import annotations def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : Dict = 0.00 __SCREAMING_SNAKE_CASE : List[str] = 0 for resistor in resistors: if resistor <= 0: __SCREAMING_SNAKE_CASE : Any = F'''Resistor at index {index} has a negative or zero value!''' raise ValueError(lowercase__ ) first_sum += 1 / float(lowercase__ ) index += 1 return 1 / first_sum def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : Tuple = 0.00 __SCREAMING_SNAKE_CASE : int = 0 for resistor in resistors: sum_r += resistor if resistor < 0: __SCREAMING_SNAKE_CASE : Tuple = F'''Resistor at index {index} has a negative value!''' raise ValueError(lowercase__ ) index += 1 return sum_r if __name__ == "__main__": import doctest doctest.testmod()
9
1
import os import unittest from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class _lowercase ( A__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = TransfoXLTokenizer SCREAMING_SNAKE_CASE__ : int = False SCREAMING_SNAKE_CASE__ : Optional[Any] = False def __magic_name__( self :str ) -> Dict: super().setUp() __SCREAMING_SNAKE_CASE : List[str] = [ '''<unk>''', '''[CLS]''', '''[SEP]''', '''want''', '''unwanted''', '''wa''', '''un''', '''running''', ''',''', '''low''', '''l''', ] __SCREAMING_SNAKE_CASE : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) def __magic_name__( self :Any , **lowerCAmelCase__ :int ) -> str: __SCREAMING_SNAKE_CASE : Optional[Any] = True return TransfoXLTokenizer.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def __magic_name__( self :Union[str, Any] , lowerCAmelCase__ :List[Any] ) -> Tuple: __SCREAMING_SNAKE_CASE : Dict = '''<unk> UNwanted , running''' __SCREAMING_SNAKE_CASE : List[str] = '''<unk> unwanted, running''' return input_text, output_text def __magic_name__( self :Any ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : int = TransfoXLTokenizer(vocab_file=self.vocab_file , lower_case=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : int = tokenizer.tokenize('''<unk> UNwanted , running''' ) self.assertListEqual(lowerCAmelCase__ , ['''<unk>''', '''unwanted''', ''',''', '''running'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase__ ) , [0, 4, 8, 7] ) def __magic_name__( self :Tuple ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : Optional[int] = TransfoXLTokenizer(lower_case=lowerCAmelCase__ ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo ! how \n Are yoU ? ''' ) , ['''hello''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) def __magic_name__( self :Tuple ) -> List[Any]: __SCREAMING_SNAKE_CASE : Union[str, Any] = TransfoXLTokenizer(lower_case=lowerCAmelCase__ ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo ! how \n Are yoU ? ''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __magic_name__( self :Dict ) -> List[Any]: __SCREAMING_SNAKE_CASE : List[str] = TransfoXLTokenizer(lower_case=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = '''Hello (bracket) and side-scrolled [and] Henry\'s $5,000 with 3.34 m. What\'s up!?''' __SCREAMING_SNAKE_CASE : Optional[int] = [ '''Hello''', '''(''', '''bracket''', ''')''', '''and''', '''side''', '''@-@''', '''scrolled''', '''[''', '''and''', ''']''', '''Henry''', '''\'s''', '''$''', '''5''', '''@,@''', '''000''', '''with''', '''3''', '''@.@''', '''34''', '''m''', '''.''', '''What''', '''\'s''', '''up''', '''!''', '''?''', ] self.assertListEqual(tokenizer.tokenize(lowerCAmelCase__ ) , lowerCAmelCase__ ) self.assertEqual(tokenizer.convert_tokens_to_string(lowerCAmelCase__ ) , lowerCAmelCase__ ) def __magic_name__( self :str ) -> int: __SCREAMING_SNAKE_CASE : Union[str, Any] = self.get_tokenizer() __SCREAMING_SNAKE_CASE : Any = len(lowerCAmelCase__ ) tokenizer.add_tokens(['''new1''', '''new2'''] ) tokenizer.move_added_token('''new1''' , 1 ) # Check that moved token is not copied (duplicate) self.assertEqual(len(lowerCAmelCase__ ) , original_len + 2 ) # Check that token is moved to specified id self.assertEqual(tokenizer.encode('''new1''' ) , [1] ) self.assertEqual(tokenizer.decode([1] ) , '''new1''' )
9
from ..utils import DummyObject, requires_backends class _lowercase ( metaclass=A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = ['''keras_nlp'''] def __init__( self :Tuple , *lowerCAmelCase__ :Optional[Any] , **lowerCAmelCase__ :Dict ) -> Dict: requires_backends(self , ['''keras_nlp'''] )
9
1
from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 __lowerCAmelCase : Tuple ={ # 1536-bit 5: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF', base=1_6, ), 'generator': 2, }, # 2048-bit 1_4: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AACAA68FFFFFFFFFFFFFFFF', base=1_6, ), 'generator': 2, }, # 3072-bit 1_5: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF', base=1_6, ), 'generator': 2, }, # 4096-bit 1_6: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7' + '88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA' + '2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6' + '287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED' + '1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9' + '93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199' + 'FFFFFFFFFFFFFFFF', base=1_6, ), 'generator': 2, }, # 6144-bit 1_7: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08' + '8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B' + '302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9' + 'A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6' + '49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8' + 'FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C' + '180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718' + '3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D' + '04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D' + 'B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226' + '1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC' + 'E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26' + '99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB' + '04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2' + '233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127' + 'D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492' + '36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406' + 'AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918' + 'DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151' + '2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03' + 'F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F' + 'BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA' + 'CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B' + 'B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632' + '387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E' + '6DCC4024FFFFFFFFFFFFFFFF', base=1_6, ), 'generator': 2, }, # 8192-bit 1_8: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7' + '88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA' + '2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6' + '287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED' + '1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9' + '93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492' + '36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD' + 'F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831' + '179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B' + 'DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF' + '5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6' + 'D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3' + '23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA' + 'CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328' + '06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C' + 'DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE' + '12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4' + '38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300' + '741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568' + '3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9' + '22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B' + '4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A' + '062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36' + '4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1' + 'B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92' + '4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47' + '9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71' + '60C980DD98EDD3DFFFFFFFFFFFFFFFFF', base=1_6, ), 'generator': 2, }, } class _lowercase : '''simple docstring''' def __init__( self :List[Any] , lowerCAmelCase__ :int = 14 ) -> None: if group not in primes: raise ValueError('''Unsupported Group''' ) __SCREAMING_SNAKE_CASE : int = primes[group]['''prime'''] __SCREAMING_SNAKE_CASE : Tuple = primes[group]['''generator'''] __SCREAMING_SNAKE_CASE : Tuple = int(hexlify(urandom(32 ) ) , base=16 ) def __magic_name__( self :Union[str, Any] ) -> str: return hex(self.__private_key )[2:] def __magic_name__( self :Union[str, Any] ) -> str: __SCREAMING_SNAKE_CASE : Dict = pow(self.generator , self.__private_key , self.prime ) return hex(lowerCAmelCase__ )[2:] def __magic_name__( self :List[str] , lowerCAmelCase__ :int ) -> bool: # check if the other public key is valid based on NIST SP800-56 return ( 2 <= key <= self.prime - 2 and pow(lowerCAmelCase__ , (self.prime - 1) // 2 , self.prime ) == 1 ) def __magic_name__( self :List[str] , lowerCAmelCase__ :str ) -> str: __SCREAMING_SNAKE_CASE : Dict = int(lowerCAmelCase__ , base=16 ) if not self.is_valid_public_key(lowerCAmelCase__ ): raise ValueError('''Invalid public key''' ) __SCREAMING_SNAKE_CASE : Union[str, Any] = pow(lowerCAmelCase__ , self.__private_key , self.prime ) return shaaaa(str(lowerCAmelCase__ ).encode() ).hexdigest() @staticmethod def __magic_name__( lowerCAmelCase__ :int , lowerCAmelCase__ :int ) -> bool: # check if the other public key is valid based on NIST SP800-56 return ( 2 <= remote_public_key_str <= prime - 2 and pow(lowerCAmelCase__ , (prime - 1) // 2 , lowerCAmelCase__ ) == 1 ) @staticmethod def __magic_name__( lowerCAmelCase__ :str , lowerCAmelCase__ :str , lowerCAmelCase__ :int = 14 ) -> str: __SCREAMING_SNAKE_CASE : List[Any] = int(lowerCAmelCase__ , base=16 ) __SCREAMING_SNAKE_CASE : Optional[int] = int(lowerCAmelCase__ , base=16 ) __SCREAMING_SNAKE_CASE : str = primes[group]['''prime'''] if not DiffieHellman.is_valid_public_key_static(lowerCAmelCase__ , lowerCAmelCase__ ): raise ValueError('''Invalid public key''' ) __SCREAMING_SNAKE_CASE : Tuple = pow(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) return shaaaa(str(lowerCAmelCase__ ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
9
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import VivitImageProcessor class _lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self :Any , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :Tuple=7 , lowerCAmelCase__ :List[Any]=3 , lowerCAmelCase__ :Any=10 , lowerCAmelCase__ :Optional[int]=18 , lowerCAmelCase__ :Dict=30 , lowerCAmelCase__ :Tuple=400 , lowerCAmelCase__ :List[Any]=True , lowerCAmelCase__ :Tuple=None , lowerCAmelCase__ :str=True , lowerCAmelCase__ :List[str]=[0.5, 0.5, 0.5] , lowerCAmelCase__ :List[str]=[0.5, 0.5, 0.5] , lowerCAmelCase__ :Optional[Any]=None , ) -> Optional[Any]: __SCREAMING_SNAKE_CASE : Dict = size if size is not None else {'''shortest_edge''': 18} __SCREAMING_SNAKE_CASE : Optional[int] = crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} __SCREAMING_SNAKE_CASE : Tuple = parent __SCREAMING_SNAKE_CASE : List[Any] = batch_size __SCREAMING_SNAKE_CASE : List[str] = num_channels __SCREAMING_SNAKE_CASE : Union[str, Any] = num_frames __SCREAMING_SNAKE_CASE : Tuple = image_size __SCREAMING_SNAKE_CASE : Optional[Any] = min_resolution __SCREAMING_SNAKE_CASE : Any = max_resolution __SCREAMING_SNAKE_CASE : List[Any] = do_resize __SCREAMING_SNAKE_CASE : Optional[Any] = size __SCREAMING_SNAKE_CASE : Optional[int] = do_normalize __SCREAMING_SNAKE_CASE : List[Any] = image_mean __SCREAMING_SNAKE_CASE : List[str] = image_std __SCREAMING_SNAKE_CASE : str = crop_size def __magic_name__( self :Tuple ) -> Any: return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class _lowercase ( A__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[str] = VivitImageProcessor if is_vision_available() else None def __magic_name__( self :List[str] ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : str = VivitImageProcessingTester(self ) @property def __magic_name__( self :int ) -> Union[str, Any]: return self.image_processor_tester.prepare_image_processor_dict() def __magic_name__( self :List[str] ) -> Optional[int]: __SCREAMING_SNAKE_CASE : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCAmelCase__ , '''image_mean''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''image_std''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_normalize''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_resize''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_center_crop''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''size''' ) ) def __magic_name__( self :Optional[Any] ) -> Optional[int]: __SCREAMING_SNAKE_CASE : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 18} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) __SCREAMING_SNAKE_CASE : Tuple = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42} ) self.assertEqual(image_processor.crop_size , {'''height''': 84, '''width''': 84} ) def __magic_name__( self :List[Any] ) -> Union[str, Any]: # Initialize image_processing __SCREAMING_SNAKE_CASE : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PIL videos __SCREAMING_SNAKE_CASE : List[Any] = prepare_video_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ ) for video in video_inputs: self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertIsInstance(video[0] , Image.Image ) # Test not batched input __SCREAMING_SNAKE_CASE : List[str] = image_processing(video_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched __SCREAMING_SNAKE_CASE : Optional[Any] = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __magic_name__( self :str ) -> int: # Initialize image_processing __SCREAMING_SNAKE_CASE : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __SCREAMING_SNAKE_CASE : List[str] = prepare_video_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , numpify=lowerCAmelCase__ ) for video in video_inputs: self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertIsInstance(video[0] , np.ndarray ) # Test not batched input __SCREAMING_SNAKE_CASE : Optional[int] = image_processing(video_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched __SCREAMING_SNAKE_CASE : Any = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __magic_name__( self :Any ) -> List[str]: # Initialize image_processing __SCREAMING_SNAKE_CASE : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __SCREAMING_SNAKE_CASE : Optional[int] = prepare_video_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , torchify=lowerCAmelCase__ ) for video in video_inputs: self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertIsInstance(video[0] , torch.Tensor ) # Test not batched input __SCREAMING_SNAKE_CASE : Tuple = image_processing(video_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched __SCREAMING_SNAKE_CASE : Optional[int] = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , )
9
1
def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : Tuple = 1 __SCREAMING_SNAKE_CASE : Optional[int] = 2 while i * i <= n: __SCREAMING_SNAKE_CASE : Optional[Any] = 0 while n % i == 0: n //= i multiplicity += 1 n_divisors *= multiplicity + 1 i += 1 if n > 1: n_divisors *= 2 return n_divisors def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : Tuple = 1 __SCREAMING_SNAKE_CASE : List[str] = 1 while True: i += 1 t_num += i if count_divisors(lowercase__ ) > 500: break return t_num if __name__ == "__main__": print(solution())
9
import unittest from transformers.testing_utils import require_bsa from transformers.utils import is_bsa_available from ...test_feature_extraction_common import FeatureExtractionSavingTestMixin if is_bsa_available(): from transformers import MarkupLMFeatureExtractor class _lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self :Optional[Any] , lowerCAmelCase__ :Optional[Any] ) -> str: __SCREAMING_SNAKE_CASE : Optional[Any] = parent def __magic_name__( self :List[Any] ) -> Tuple: return {} def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : Optional[Any] = '''<HTML> <HEAD> <TITLE>sample document</TITLE> </HEAD> <BODY BGCOLOR="FFFFFF"> <HR> <a href="http://google.com">Goog</a> <H1>This is one header</H1> <H2>This is a another Header</H2> <P>Travel from <P> <B>SFO to JFK</B> <BR> <B><I>on May 2, 2015 at 2:00 pm. For details go to confirm.com </I></B> <HR> <div style="color:#0000FF"> <h3>Traveler <b> name </b> is <p> John Doe </p> </div>''' __SCREAMING_SNAKE_CASE : str = ''' <!DOCTYPE html> <html> <body> <h1>My First Heading</h1> <p>My first paragraph.</p> </body> </html> ''' return [html_string_a, html_string_a] @require_bsa class _lowercase ( A__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = MarkupLMFeatureExtractor if is_bsa_available() else None def __magic_name__( self :int ) -> Optional[Any]: __SCREAMING_SNAKE_CASE : Optional[Any] = MarkupLMFeatureExtractionTester(self ) @property def __magic_name__( self :Any ) -> Optional[Any]: return self.feature_extract_tester.prepare_feat_extract_dict() def __magic_name__( self :Optional[int] ) -> Any: # Initialize feature_extractor __SCREAMING_SNAKE_CASE : int = self.feature_extraction_class() # Test not batched input __SCREAMING_SNAKE_CASE : Tuple = get_html_strings()[0] __SCREAMING_SNAKE_CASE : Dict = feature_extractor(lowerCAmelCase__ ) # fmt: off __SCREAMING_SNAKE_CASE : str = [['''sample document''', '''Goog''', '''This is one header''', '''This is a another Header''', '''Travel from''', '''SFO to JFK''', '''on May 2, 2015 at 2:00 pm. For details go to confirm.com''', '''Traveler''', '''name''', '''is''', '''John Doe''']] __SCREAMING_SNAKE_CASE : List[str] = [['''/html/head/title''', '''/html/body/a''', '''/html/body/h1''', '''/html/body/h2''', '''/html/body/p''', '''/html/body/p/p/b[1]''', '''/html/body/p/p/b[2]/i''', '''/html/body/p/p/div/h3''', '''/html/body/p/p/div/h3/b''', '''/html/body/p/p/div/h3''', '''/html/body/p/p/div/h3/p''']] # fmt: on self.assertEqual(encoding.nodes , lowerCAmelCase__ ) self.assertEqual(encoding.xpaths , lowerCAmelCase__ ) # Test batched __SCREAMING_SNAKE_CASE : Tuple = get_html_strings() __SCREAMING_SNAKE_CASE : Dict = feature_extractor(lowerCAmelCase__ ) # fmt: off __SCREAMING_SNAKE_CASE : int = expected_nodes + [['''My First Heading''', '''My first paragraph.''']] __SCREAMING_SNAKE_CASE : str = expected_xpaths + [['''/html/body/h1''', '''/html/body/p''']] self.assertEqual(len(encoding.nodes ) , 2 ) self.assertEqual(len(encoding.xpaths ) , 2 ) self.assertEqual(encoding.nodes , lowerCAmelCase__ ) self.assertEqual(encoding.xpaths , lowerCAmelCase__ )
9
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __lowerCAmelCase : Dict ={ 'configuration_albert': ['ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'AlbertConfig', 'AlbertOnnxConfig'], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : List[str] =['AlbertTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Any =['AlbertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Optional[Any] =[ 'ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'AlbertForMaskedLM', 'AlbertForMultipleChoice', 'AlbertForPreTraining', 'AlbertForQuestionAnswering', 'AlbertForSequenceClassification', 'AlbertForTokenClassification', 'AlbertModel', 'AlbertPreTrainedModel', 'load_tf_weights_in_albert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Any =[ 'TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFAlbertForMaskedLM', 'TFAlbertForMultipleChoice', 'TFAlbertForPreTraining', 'TFAlbertForQuestionAnswering', 'TFAlbertForSequenceClassification', 'TFAlbertForTokenClassification', 'TFAlbertMainLayer', 'TFAlbertModel', 'TFAlbertPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Optional[int] =[ 'FlaxAlbertForMaskedLM', 'FlaxAlbertForMultipleChoice', 'FlaxAlbertForPreTraining', 'FlaxAlbertForQuestionAnswering', 'FlaxAlbertForSequenceClassification', 'FlaxAlbertForTokenClassification', 'FlaxAlbertModel', 'FlaxAlbertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_albert import ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, AlbertConfig, AlbertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_albert import AlbertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_albert_fast import AlbertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_albert import ( ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForPreTraining, AlbertForQuestionAnswering, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertModel, AlbertPreTrainedModel, load_tf_weights_in_albert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_albert import ( TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFAlbertForMaskedLM, TFAlbertForMultipleChoice, TFAlbertForPreTraining, TFAlbertForQuestionAnswering, TFAlbertForSequenceClassification, TFAlbertForTokenClassification, TFAlbertMainLayer, TFAlbertModel, TFAlbertPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, FlaxAlbertPreTrainedModel, ) else: import sys __lowerCAmelCase : Tuple =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
9
import os import unittest from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class _lowercase ( A__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = TransfoXLTokenizer SCREAMING_SNAKE_CASE__ : int = False SCREAMING_SNAKE_CASE__ : Optional[Any] = False def __magic_name__( self :str ) -> Dict: super().setUp() __SCREAMING_SNAKE_CASE : List[str] = [ '''<unk>''', '''[CLS]''', '''[SEP]''', '''want''', '''unwanted''', '''wa''', '''un''', '''running''', ''',''', '''low''', '''l''', ] __SCREAMING_SNAKE_CASE : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) def __magic_name__( self :Any , **lowerCAmelCase__ :int ) -> str: __SCREAMING_SNAKE_CASE : Optional[Any] = True return TransfoXLTokenizer.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def __magic_name__( self :Union[str, Any] , lowerCAmelCase__ :List[Any] ) -> Tuple: __SCREAMING_SNAKE_CASE : Dict = '''<unk> UNwanted , running''' __SCREAMING_SNAKE_CASE : List[str] = '''<unk> unwanted, running''' return input_text, output_text def __magic_name__( self :Any ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : int = TransfoXLTokenizer(vocab_file=self.vocab_file , lower_case=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : int = tokenizer.tokenize('''<unk> UNwanted , running''' ) self.assertListEqual(lowerCAmelCase__ , ['''<unk>''', '''unwanted''', ''',''', '''running'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase__ ) , [0, 4, 8, 7] ) def __magic_name__( self :Tuple ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : Optional[int] = TransfoXLTokenizer(lower_case=lowerCAmelCase__ ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo ! how \n Are yoU ? ''' ) , ['''hello''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) def __magic_name__( self :Tuple ) -> List[Any]: __SCREAMING_SNAKE_CASE : Union[str, Any] = TransfoXLTokenizer(lower_case=lowerCAmelCase__ ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo ! how \n Are yoU ? ''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __magic_name__( self :Dict ) -> List[Any]: __SCREAMING_SNAKE_CASE : List[str] = TransfoXLTokenizer(lower_case=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = '''Hello (bracket) and side-scrolled [and] Henry\'s $5,000 with 3.34 m. What\'s up!?''' __SCREAMING_SNAKE_CASE : Optional[int] = [ '''Hello''', '''(''', '''bracket''', ''')''', '''and''', '''side''', '''@-@''', '''scrolled''', '''[''', '''and''', ''']''', '''Henry''', '''\'s''', '''$''', '''5''', '''@,@''', '''000''', '''with''', '''3''', '''@.@''', '''34''', '''m''', '''.''', '''What''', '''\'s''', '''up''', '''!''', '''?''', ] self.assertListEqual(tokenizer.tokenize(lowerCAmelCase__ ) , lowerCAmelCase__ ) self.assertEqual(tokenizer.convert_tokens_to_string(lowerCAmelCase__ ) , lowerCAmelCase__ ) def __magic_name__( self :str ) -> int: __SCREAMING_SNAKE_CASE : Union[str, Any] = self.get_tokenizer() __SCREAMING_SNAKE_CASE : Any = len(lowerCAmelCase__ ) tokenizer.add_tokens(['''new1''', '''new2'''] ) tokenizer.move_added_token('''new1''' , 1 ) # Check that moved token is not copied (duplicate) self.assertEqual(len(lowerCAmelCase__ ) , original_len + 2 ) # Check that token is moved to specified id self.assertEqual(tokenizer.encode('''new1''' ) , [1] ) self.assertEqual(tokenizer.decode([1] ) , '''new1''' )
9
1
import json import os from functools import lru_cache from typing import TYPE_CHECKING, List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __lowerCAmelCase : Tuple =logging.get_logger(__name__) __lowerCAmelCase : List[Any] ={ 'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_config_file': 'tokenizer_config.json', } __lowerCAmelCase : Dict ={ 'vocab_file': {'facebook/blenderbot-3B': 'https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json'}, 'merges_file': {'facebook/blenderbot-3B': 'https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt'}, 'tokenizer_config_file': { 'facebook/blenderbot-3B': 'https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json' }, } __lowerCAmelCase : Optional[int] ={'facebook/blenderbot-3B': 1_2_8} @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : int = ( list(range(ord('''!''' ) , ord('''~''' ) + 1 ) ) + list(range(ord('''¡''' ) , ord('''¬''' ) + 1 ) ) + list(range(ord('''®''' ) , ord('''ÿ''' ) + 1 ) ) ) __SCREAMING_SNAKE_CASE : List[Any] = bs[:] __SCREAMING_SNAKE_CASE : str = 0 for b in range(2**8 ): if b not in bs: bs.append(lowercase__ ) cs.append(2**8 + n ) n += 1 __SCREAMING_SNAKE_CASE : str = [chr(lowercase__ ) for n in cs] return dict(zip(lowercase__ , lowercase__ ) ) def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : int = set() __SCREAMING_SNAKE_CASE : int = word[0] for char in word[1:]: pairs.add((prev_char, char) ) __SCREAMING_SNAKE_CASE : str = char return pairs class _lowercase ( A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[Any] = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE__ : Any = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE__ : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE__ : List[str] = ['''input_ids''', '''attention_mask'''] def __init__( self :List[Any] , lowerCAmelCase__ :str , lowerCAmelCase__ :str , lowerCAmelCase__ :Optional[Any]="replace" , lowerCAmelCase__ :List[str]="<s>" , lowerCAmelCase__ :Union[str, Any]="</s>" , lowerCAmelCase__ :Optional[Any]="</s>" , lowerCAmelCase__ :Optional[Any]="<s>" , lowerCAmelCase__ :str="<unk>" , lowerCAmelCase__ :int="<pad>" , lowerCAmelCase__ :Tuple="<mask>" , lowerCAmelCase__ :int=False , **lowerCAmelCase__ :int , ) -> Dict: __SCREAMING_SNAKE_CASE : Optional[Any] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else bos_token __SCREAMING_SNAKE_CASE : List[str] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else eos_token __SCREAMING_SNAKE_CASE : Tuple = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else sep_token __SCREAMING_SNAKE_CASE : Dict = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else cls_token __SCREAMING_SNAKE_CASE : Any = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else unk_token __SCREAMING_SNAKE_CASE : Union[str, Any] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it __SCREAMING_SNAKE_CASE : Tuple = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else mask_token super().__init__( errors=lowerCAmelCase__ , bos_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , sep_token=lowerCAmelCase__ , cls_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , **lowerCAmelCase__ , ) with open(lowerCAmelCase__ , encoding='''utf-8''' ) as vocab_handle: __SCREAMING_SNAKE_CASE : str = json.load(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Any = {v: k for k, v in self.encoder.items()} __SCREAMING_SNAKE_CASE : Dict = errors # how to handle errors in decoding __SCREAMING_SNAKE_CASE : int = bytes_to_unicode() __SCREAMING_SNAKE_CASE : int = {v: k for k, v in self.byte_encoder.items()} with open(lowerCAmelCase__ , encoding='''utf-8''' ) as merges_handle: __SCREAMING_SNAKE_CASE : Tuple = merges_handle.read().split('''\n''' )[1:-1] __SCREAMING_SNAKE_CASE : List[str] = [tuple(merge.split() ) for merge in bpe_merges] __SCREAMING_SNAKE_CASE : Tuple = dict(zip(lowerCAmelCase__ , range(len(lowerCAmelCase__ ) ) ) ) __SCREAMING_SNAKE_CASE : Any = {} __SCREAMING_SNAKE_CASE : Tuple = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions __SCREAMING_SNAKE_CASE : Any = re.compile(r'''\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+''' ) @property # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.vocab_size with Roberta->Blenderbot, RoBERTa->Blenderbot def __magic_name__( self :Optional[int] ) -> Tuple: return len(self.encoder ) def __magic_name__( self :Optional[Any] ) -> Tuple: return dict(self.encoder , **self.added_tokens_encoder ) def __magic_name__( self :Dict , lowerCAmelCase__ :Optional[Any] ) -> Dict: if token in self.cache: return self.cache[token] __SCREAMING_SNAKE_CASE : Union[str, Any] = tuple(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = get_pairs(lowerCAmelCase__ ) if not pairs: return token while True: __SCREAMING_SNAKE_CASE : List[Any] = min(lowerCAmelCase__ , key=lambda lowerCAmelCase__ : self.bpe_ranks.get(lowerCAmelCase__ , float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Tuple = bigram __SCREAMING_SNAKE_CASE : Optional[Any] = [] __SCREAMING_SNAKE_CASE : Any = 0 while i < len(lowerCAmelCase__ ): try: __SCREAMING_SNAKE_CASE : Tuple = word.index(lowerCAmelCase__ , lowerCAmelCase__ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) __SCREAMING_SNAKE_CASE : Optional[int] = j if word[i] == first and i < len(lowerCAmelCase__ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 __SCREAMING_SNAKE_CASE : Any = tuple(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[str] = new_word if len(lowerCAmelCase__ ) == 1: break else: __SCREAMING_SNAKE_CASE : Tuple = get_pairs(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Tuple = ''' '''.join(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = word return word def __magic_name__( self :Any , lowerCAmelCase__ :Union[str, Any] ) -> List[Any]: __SCREAMING_SNAKE_CASE : List[str] = [] for token in re.findall(self.pat , lowerCAmelCase__ ): __SCREAMING_SNAKE_CASE : int = ''''''.join( self.byte_encoder[b] for b in token.encode('''utf-8''' ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(lowerCAmelCase__ ).split(''' ''' ) ) return bpe_tokens def __magic_name__( self :str , lowerCAmelCase__ :List[str] ) -> int: return self.encoder.get(lowerCAmelCase__ , self.encoder.get(self.unk_token ) ) def __magic_name__( self :Any , lowerCAmelCase__ :Optional[int] ) -> List[Any]: return self.decoder.get(lowerCAmelCase__ ) def __magic_name__( self :Union[str, Any] , lowerCAmelCase__ :List[Any] ) -> Dict: __SCREAMING_SNAKE_CASE : Optional[int] = ''''''.join(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Tuple = bytearray([self.byte_decoder[c] for c in text] ).decode('''utf-8''' , errors=self.errors ) return text def __magic_name__( self :Union[str, Any] , lowerCAmelCase__ :str , lowerCAmelCase__ :Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(lowerCAmelCase__ ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __SCREAMING_SNAKE_CASE : List[str] = os.path.join( lowerCAmelCase__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) __SCREAMING_SNAKE_CASE : List[Any] = os.path.join( lowerCAmelCase__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file'''] ) with open(lowerCAmelCase__ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=lowerCAmelCase__ , ensure_ascii=lowerCAmelCase__ ) + '''\n''' ) __SCREAMING_SNAKE_CASE : Dict = 0 with open(lowerCAmelCase__ , '''w''' , encoding='''utf-8''' ) as writer: writer.write('''#version: 0.2\n''' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda lowerCAmelCase__ : kv[1] ): if index != token_index: logger.warning( f'''Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.''' ''' Please check that the tokenizer is not corrupted!''' ) __SCREAMING_SNAKE_CASE : Any = token_index writer.write(''' '''.join(lowerCAmelCase__ ) + '''\n''' ) index += 1 return vocab_file, merge_file def __magic_name__( self :Optional[int] , lowerCAmelCase__ :List[int] , lowerCAmelCase__ :Optional[List[int]] = None , lowerCAmelCase__ :bool = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCAmelCase__ , token_ids_a=lowerCAmelCase__ , already_has_special_tokens=lowerCAmelCase__ ) if token_ids_a is None: return [1] + ([0] * len(lowerCAmelCase__ )) + [1] return [1] + ([0] * len(lowerCAmelCase__ )) + [1, 1] + ([0] * len(lowerCAmelCase__ )) + [1] def __magic_name__( self :Any , lowerCAmelCase__ :List[int] , lowerCAmelCase__ :Optional[List[int]] = None ) -> List[int]: __SCREAMING_SNAKE_CASE : Tuple = [self.sep_token_id] __SCREAMING_SNAKE_CASE : Optional[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __magic_name__( self :Optional[Any] , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :Tuple=False , **lowerCAmelCase__ :int ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : List[str] = kwargs.pop('''add_prefix_space''' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(lowerCAmelCase__ ) > 0 and not text[0].isspace()): __SCREAMING_SNAKE_CASE : List[Any] = ''' ''' + text return (text, kwargs) def __magic_name__( self :List[str] , lowerCAmelCase__ :List[int] , lowerCAmelCase__ :Optional[List[int]] = None ) -> Tuple: return token_ids_a + [self.eos_token_id] def __magic_name__( self :Any , lowerCAmelCase__ :"Conversation" ) -> List[int]: __SCREAMING_SNAKE_CASE : Optional[int] = [] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(''' ''' + text ) else: # Generated responses should contain them already. inputs.append(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Any = ''' '''.join(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = self.encode(lowerCAmelCase__ ) if len(lowerCAmelCase__ ) > self.model_max_length: __SCREAMING_SNAKE_CASE : Optional[int] = input_ids[-self.model_max_length :] logger.warning(f'''Trimmed input from conversation as it was longer than {self.model_max_length} tokens.''' ) return input_ids
9
def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__=False ): if isinstance(lowercase__ , lowercase__ ) and isinstance(lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : List[str] = len(set_a.intersection(lowercase__ ) ) if alternative_union: __SCREAMING_SNAKE_CASE : int = len(lowercase__ ) + len(lowercase__ ) else: __SCREAMING_SNAKE_CASE : int = len(set_a.union(lowercase__ ) ) return intersection / union if isinstance(lowercase__ , (list, tuple) ) and isinstance(lowercase__ , (list, tuple) ): __SCREAMING_SNAKE_CASE : Dict = [element for element in set_a if element in set_b] if alternative_union: __SCREAMING_SNAKE_CASE : Optional[int] = len(lowercase__ ) + len(lowercase__ ) return len(lowercase__ ) / union else: __SCREAMING_SNAKE_CASE : Tuple = set_a + [element for element in set_b if element not in set_a] return len(lowercase__ ) / len(lowercase__ ) return len(lowercase__ ) / len(lowercase__ ) return None if __name__ == "__main__": __lowerCAmelCase : List[Any] ={'a', 'b', 'c', 'd', 'e'} __lowerCAmelCase : Optional[Any] ={'c', 'd', 'e', 'f', 'h', 'i'} print(jaccard_similarity(set_a, set_b))
9
1
import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class _lowercase ( A__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[Any] = LongformerTokenizer SCREAMING_SNAKE_CASE__ : Optional[Any] = True SCREAMING_SNAKE_CASE__ : List[Any] = LongformerTokenizerFast SCREAMING_SNAKE_CASE__ : List[str] = True def __magic_name__( self :List[str] ) -> Union[str, Any]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt __SCREAMING_SNAKE_CASE : Any = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] __SCREAMING_SNAKE_CASE : List[str] = dict(zip(lowerCAmelCase__ , range(len(lowerCAmelCase__ ) ) ) ) __SCREAMING_SNAKE_CASE : Optional[int] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] __SCREAMING_SNAKE_CASE : List[Any] = {'''unk_token''': '''<unk>'''} __SCREAMING_SNAKE_CASE : Tuple = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) __SCREAMING_SNAKE_CASE : Tuple = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(lowerCAmelCase__ ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(lowerCAmelCase__ ) ) def __magic_name__( self :Dict , **lowerCAmelCase__ :Any ) -> Optional[int]: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def __magic_name__( self :int , **lowerCAmelCase__ :List[Any] ) -> str: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def __magic_name__( self :Tuple , lowerCAmelCase__ :int ) -> int: __SCREAMING_SNAKE_CASE : Optional[Any] = '''lower newer''' __SCREAMING_SNAKE_CASE : Union[str, Any] = '''lower newer''' return input_text, output_text def __magic_name__( self :List[Any] ) -> List[str]: __SCREAMING_SNAKE_CASE : List[str] = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) __SCREAMING_SNAKE_CASE : str = '''lower newer''' __SCREAMING_SNAKE_CASE : int = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] __SCREAMING_SNAKE_CASE : Optional[int] = tokenizer.tokenize(lowerCAmelCase__ ) # , add_prefix_space=True) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = tokens + [tokenizer.unk_token] __SCREAMING_SNAKE_CASE : int = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase__ ) , lowerCAmelCase__ ) def __magic_name__( self :Optional[int] ) -> List[Any]: __SCREAMING_SNAKE_CASE : Optional[Any] = self.get_tokenizer() self.assertListEqual(tokenizer.encode('''Hello world!''' , add_special_tokens=lowerCAmelCase__ ) , [0, 31_414, 232, 328, 2] ) self.assertListEqual( tokenizer.encode('''Hello world! cécé herlolip 418''' , add_special_tokens=lowerCAmelCase__ ) , [0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2] , ) @slow def __magic_name__( self :Union[str, Any] ) -> List[Any]: __SCREAMING_SNAKE_CASE : Any = self.tokenizer_class.from_pretrained('''allenai/longformer-base-4096''' ) __SCREAMING_SNAKE_CASE : int = tokenizer.encode('''sequence builders''' , add_special_tokens=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Union[str, Any] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : int = tokenizer.encode( '''sequence builders''' , add_special_tokens=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : int = tokenizer.build_inputs_with_special_tokens(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : str = tokenizer.build_inputs_with_special_tokens(lowerCAmelCase__ , lowerCAmelCase__ ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def __magic_name__( self :Tuple ) -> Tuple: __SCREAMING_SNAKE_CASE : List[str] = self.get_tokenizer() __SCREAMING_SNAKE_CASE : Optional[int] = '''Encode this sequence.''' __SCREAMING_SNAKE_CASE : List[str] = tokenizer.byte_encoder[''' '''.encode('''utf-8''' )[0]] # Testing encoder arguments __SCREAMING_SNAKE_CASE : int = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Tuple = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(lowerCAmelCase__ , lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Tuple = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : int = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) tokenizer.add_special_tokens({'''bos_token''': '''<s>'''} ) __SCREAMING_SNAKE_CASE : List[str] = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : str = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(lowerCAmelCase__ , lowerCAmelCase__ ) # Testing spaces after special tokens __SCREAMING_SNAKE_CASE : Optional[Any] = '''<mask>''' tokenizer.add_special_tokens( {'''mask_token''': AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ )} ) # mask token has a left space __SCREAMING_SNAKE_CASE : str = tokenizer.convert_tokens_to_ids(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = '''Encode <mask> sequence''' __SCREAMING_SNAKE_CASE : Any = '''Encode <mask>sequence''' __SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer.encode(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Tuple = encoded.index(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.encode(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = encoded.index(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(lowerCAmelCase__ , lowerCAmelCase__ ) def __magic_name__( self :Optional[Any] ) -> List[Any]: pass def __magic_name__( self :int ) -> Any: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __SCREAMING_SNAKE_CASE : Tuple = self.rust_tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Union[str, Any] = self.tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[Any] = '''A, <mask> AllenNLP sentence.''' __SCREAMING_SNAKE_CASE : str = tokenizer_r.encode_plus(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Tuple = tokenizer_p.encode_plus(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['''token_type_ids'''] ) , sum(tokens_p['''token_type_ids'''] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['''attention_mask'''] ) / len(tokens_r['''attention_mask'''] ) , sum(tokens_p['''attention_mask'''] ) / len(tokens_p['''attention_mask'''] ) , ) __SCREAMING_SNAKE_CASE : str = tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids'''] ) __SCREAMING_SNAKE_CASE : Optional[int] = tokenizer_p.convert_ids_to_tokens(tokens_p['''input_ids'''] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['''input_ids'''] , [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2] ) self.assertSequenceEqual(tokens_r['''input_ids'''] , [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2] ) self.assertSequenceEqual( lowerCAmelCase__ , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) self.assertSequenceEqual( lowerCAmelCase__ , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) def __magic_name__( self :Optional[int] ) -> Any: for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): __SCREAMING_SNAKE_CASE : Any = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , trim_offsets=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) __SCREAMING_SNAKE_CASE : List[Any] = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['''add_prefix_space'''] , lowerCAmelCase__ ) self.assertEqual(post_processor_state['''add_prefix_space'''] , lowerCAmelCase__ ) self.assertEqual(post_processor_state['''trim_offsets'''] , lowerCAmelCase__ ) def __magic_name__( self :Any ) -> List[Any]: # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and # `trim_offsets` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __SCREAMING_SNAKE_CASE : Optional[Any] = '''hello''' # `hello` is a token in the vocabulary of `pretrained_name` __SCREAMING_SNAKE_CASE : Optional[int] = f'''{text_of_1_token} {text_of_1_token}''' __SCREAMING_SNAKE_CASE : List[Any] = self.rust_tokenizer_class.from_pretrained( lowerCAmelCase__ , use_fast=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , trim_offsets=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : str = tokenizer_r(lowerCAmelCase__ , return_offsets_mapping=lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (0, len(lowerCAmelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (len(lowerCAmelCase__ ) + 1, len(lowerCAmelCase__ ) + 1 + len(lowerCAmelCase__ )) , ) __SCREAMING_SNAKE_CASE : str = self.rust_tokenizer_class.from_pretrained( lowerCAmelCase__ , use_fast=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , trim_offsets=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer_r(lowerCAmelCase__ , return_offsets_mapping=lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (0, len(lowerCAmelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (len(lowerCAmelCase__ ) + 1, len(lowerCAmelCase__ ) + 1 + len(lowerCAmelCase__ )) , ) __SCREAMING_SNAKE_CASE : Dict = self.rust_tokenizer_class.from_pretrained( lowerCAmelCase__ , use_fast=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , trim_offsets=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = tokenizer_r(lowerCAmelCase__ , return_offsets_mapping=lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (0, len(lowerCAmelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (len(lowerCAmelCase__ ), len(lowerCAmelCase__ ) + 1 + len(lowerCAmelCase__ )) , ) __SCREAMING_SNAKE_CASE : List[Any] = self.rust_tokenizer_class.from_pretrained( lowerCAmelCase__ , use_fast=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , trim_offsets=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Union[str, Any] = tokenizer_r(lowerCAmelCase__ , return_offsets_mapping=lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (0, len(lowerCAmelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (len(lowerCAmelCase__ ), len(lowerCAmelCase__ ) + 1 + len(lowerCAmelCase__ )) , ) __SCREAMING_SNAKE_CASE : List[Any] = f''' {text}''' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) __SCREAMING_SNAKE_CASE : Optional[Any] = self.rust_tokenizer_class.from_pretrained( lowerCAmelCase__ , use_fast=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , trim_offsets=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer_r(lowerCAmelCase__ , return_offsets_mapping=lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(lowerCAmelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(lowerCAmelCase__ ) + 1, 1 + len(lowerCAmelCase__ ) + 1 + len(lowerCAmelCase__ )) , ) __SCREAMING_SNAKE_CASE : Tuple = self.rust_tokenizer_class.from_pretrained( lowerCAmelCase__ , use_fast=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , trim_offsets=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer_r(lowerCAmelCase__ , return_offsets_mapping=lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(lowerCAmelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(lowerCAmelCase__ ), 1 + len(lowerCAmelCase__ ) + 1 + len(lowerCAmelCase__ )) , ) __SCREAMING_SNAKE_CASE : Dict = self.rust_tokenizer_class.from_pretrained( lowerCAmelCase__ , use_fast=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , trim_offsets=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Any = tokenizer_r(lowerCAmelCase__ , return_offsets_mapping=lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(lowerCAmelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(lowerCAmelCase__ ), 1 + len(lowerCAmelCase__ ) + 1 + len(lowerCAmelCase__ )) , )
9
import numpy as np from scipy.spatial.distance import cdist from sklearn.metrics import fa_score import datasets __lowerCAmelCase : Optional[int] ='\\n @inproceedings{kakwani2020indicnlpsuite,\n title={{IndicNLPSuite: Monolingual Corpora, Evaluation Benchmarks and Pre-trained Multilingual Language Models for Indian Languages}},\n author={Divyanshu Kakwani and Anoop Kunchukuttan and Satish Golla and Gokul N.C. and Avik Bhattacharyya and Mitesh M. Khapra and Pratyush Kumar},\n year={2020},\n booktitle={Findings of EMNLP},\n}\n' __lowerCAmelCase : Optional[Any] ='\\n IndicGLUE is a natural language understanding benchmark for Indian languages. It contains a wide\n variety of tasks and covers 11 major Indian languages - as, bn, gu, hi, kn, ml, mr, or, pa, ta, te.\n' __lowerCAmelCase : Dict ='\nCompute IndicGLUE evaluation metric associated to each IndicGLUE dataset.\nArgs:\n predictions: list of predictions to score (as int64),\n except for \'cvit-mkb-clsr\' where each prediction is a vector (of float32).\n references: list of ground truth labels corresponding to the predictions (as int64),\n except for \'cvit-mkb-clsr\' where each reference is a vector (of float32).\nReturns: depending on the IndicGLUE subset, one or several of:\n "accuracy": Accuracy\n "f1": F1 score\n "precision": Precision@10\nExamples:\n\n >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'wnli\') # \'wnli\' or any of ["copa", "sna", "csqa", "wstp", "inltkh", "bbca", "iitp-mr", "iitp-pr", "actsa-sc", "md"]\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0}\n\n >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'wiki-ner\')\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0, \'f1\': 1.0}\n\n >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'cvit-mkb-clsr\')\n >>> references = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n >>> predictions = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'precision@10\': 1.0}\n\n' def _UpperCamelCase ( lowercase__ , lowercase__ ): return float((preds == labels).mean() ) def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Optional[Any] = simple_accuracy(lowercase__ , lowercase__ ) __SCREAMING_SNAKE_CASE : List[str] = float(fa_score(y_true=lowercase__ , y_pred=lowercase__ ) ) return { "accuracy": acc, "f1": fa, } def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Optional[int] = np.array(lowercase__ ) __SCREAMING_SNAKE_CASE : str = np.array(lowercase__ ) __SCREAMING_SNAKE_CASE : str = en_sentvecs.shape[0] # mean centering __SCREAMING_SNAKE_CASE : Tuple = en_sentvecs - np.mean(lowercase__ , axis=0 ) __SCREAMING_SNAKE_CASE : Optional[int] = in_sentvecs - np.mean(lowercase__ , axis=0 ) __SCREAMING_SNAKE_CASE : str = cdist(lowercase__ , lowercase__ , '''cosine''' ) __SCREAMING_SNAKE_CASE : int = np.array(range(lowercase__ ) ) __SCREAMING_SNAKE_CASE : Optional[Any] = sim.argsort(axis=1 )[:, :10] __SCREAMING_SNAKE_CASE : str = np.any(preds == actual[:, None] , axis=1 ) return float(matches.mean() ) @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _lowercase ( datasets.Metric ): '''simple docstring''' def __magic_name__( self :Tuple ) -> Tuple: if self.config_name not in [ "wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", "cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", "wiki-ner", ]: raise KeyError( '''You should supply a configuration name selected in ''' '''["wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", ''' '''"cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", ''' '''"wiki-ner"]''' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''int64''' ) if self.config_name != '''cvit-mkb-clsr''' else datasets.Sequence(datasets.Value('''float32''' ) ), '''references''': datasets.Value('''int64''' ) if self.config_name != '''cvit-mkb-clsr''' else datasets.Sequence(datasets.Value('''float32''' ) ), } ) , codebase_urls=[] , reference_urls=[] , format='''numpy''' if self.config_name != '''cvit-mkb-clsr''' else None , ) def __magic_name__( self :List[str] , lowerCAmelCase__ :Optional[Any] , lowerCAmelCase__ :Tuple ) -> str: if self.config_name == "cvit-mkb-clsr": return {"precision@10": precision_at_aa(lowerCAmelCase__ , lowerCAmelCase__ )} elif self.config_name in ["wiki-ner"]: return acc_and_fa(lowerCAmelCase__ , lowerCAmelCase__ ) elif self.config_name in [ "wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", "iitp-mr", "iitp-pr", "actsa-sc", "md", ]: return {"accuracy": simple_accuracy(lowerCAmelCase__ , lowerCAmelCase__ )} else: raise KeyError( '''You should supply a configuration name selected in ''' '''["wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", ''' '''"cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", ''' '''"wiki-ner"]''' )
9
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __lowerCAmelCase : Optional[Any] ={ 'configuration_graphormer': ['GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GraphormerConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Dict =[ 'GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'GraphormerForGraphClassification', 'GraphormerModel', 'GraphormerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_graphormer import GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, GraphormerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_graphormer import ( GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST, GraphormerForGraphClassification, GraphormerModel, GraphormerPreTrainedModel, ) else: import sys __lowerCAmelCase : Tuple =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
9
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging __lowerCAmelCase : Dict =logging.get_logger(__name__) def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : List[Any] = nn.functional.normalize(lowercase__ ) __SCREAMING_SNAKE_CASE : Tuple = nn.functional.normalize(lowercase__ ) return torch.mm(lowercase__ , normalized_text_embeds.t() ) class _lowercase ( A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[Any] = CLIPConfig SCREAMING_SNAKE_CASE__ : List[str] = ['''CLIPEncoderLayer'''] def __init__( self :str , lowerCAmelCase__ :CLIPConfig ) -> Tuple: super().__init__(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = CLIPVisionModel(config.vision_config ) __SCREAMING_SNAKE_CASE : Union[str, Any] = nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Any = nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = nn.Parameter(torch.ones(17 ) , requires_grad=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : str = nn.Parameter(torch.ones(3 ) , requires_grad=lowerCAmelCase__ ) @torch.no_grad() def __magic_name__( self :Optional[int] , lowerCAmelCase__ :int , lowerCAmelCase__ :Optional[int] ) -> Dict: __SCREAMING_SNAKE_CASE : int = self.vision_model(lowerCAmelCase__ )[1] # pooled_output __SCREAMING_SNAKE_CASE : Optional[Any] = self.visual_projection(lowerCAmelCase__ ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 __SCREAMING_SNAKE_CASE : Optional[Any] = cosine_distance(lowerCAmelCase__ , self.special_care_embeds ).cpu().float().numpy() __SCREAMING_SNAKE_CASE : Optional[Any] = cosine_distance(lowerCAmelCase__ , self.concept_embeds ).cpu().float().numpy() __SCREAMING_SNAKE_CASE : List[Any] = [] __SCREAMING_SNAKE_CASE : List[Any] = image_embeds.shape[0] for i in range(lowerCAmelCase__ ): __SCREAMING_SNAKE_CASE : Optional[int] = {'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images __SCREAMING_SNAKE_CASE : List[Any] = 0.0 for concept_idx in range(len(special_cos_dist[0] ) ): __SCREAMING_SNAKE_CASE : List[str] = special_cos_dist[i][concept_idx] __SCREAMING_SNAKE_CASE : Any = self.special_care_embeds_weights[concept_idx].item() __SCREAMING_SNAKE_CASE : Union[str, Any] = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} ) __SCREAMING_SNAKE_CASE : Union[str, Any] = 0.01 for concept_idx in range(len(cos_dist[0] ) ): __SCREAMING_SNAKE_CASE : int = cos_dist[i][concept_idx] __SCREAMING_SNAKE_CASE : Union[str, Any] = self.concept_embeds_weights[concept_idx].item() __SCREAMING_SNAKE_CASE : Tuple = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(lowerCAmelCase__ ) result.append(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[str] = [len(res['''bad_concepts'''] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def __magic_name__( self :Optional[int] , lowerCAmelCase__ :torch.FloatTensor , lowerCAmelCase__ :torch.FloatTensor ) -> Any: __SCREAMING_SNAKE_CASE : Optional[Any] = self.vision_model(lowerCAmelCase__ )[1] # pooled_output __SCREAMING_SNAKE_CASE : List[str] = self.visual_projection(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = cosine_distance(lowerCAmelCase__ , self.special_care_embeds ) __SCREAMING_SNAKE_CASE : Optional[int] = cosine_distance(lowerCAmelCase__ , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images __SCREAMING_SNAKE_CASE : List[Any] = 0.0 __SCREAMING_SNAKE_CASE : Union[str, Any] = special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) __SCREAMING_SNAKE_CASE : List[str] = torch.any(special_scores > 0 , dim=1 ) __SCREAMING_SNAKE_CASE : List[str] = special_care * 0.01 __SCREAMING_SNAKE_CASE : int = special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) __SCREAMING_SNAKE_CASE : Optional[int] = (cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) __SCREAMING_SNAKE_CASE : Any = torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
9
1
import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate # and perform gradient accumulation # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## __lowerCAmelCase : Optional[Any] =1_6 __lowerCAmelCase : Union[str, Any] =3_2 def _UpperCamelCase ( lowercase__ , lowercase__ = 16 ): __SCREAMING_SNAKE_CASE : Union[str, Any] = AutoTokenizer.from_pretrained('''bert-base-cased''' ) __SCREAMING_SNAKE_CASE : Union[str, Any] = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(lowercase__ ): # max_length=None => use the model max length (it's actually the default) __SCREAMING_SNAKE_CASE : Tuple = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=lowercase__ , max_length=lowercase__ ) 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(): __SCREAMING_SNAKE_CASE : List[Any] = datasets.map( lowercase__ , batched=lowercase__ , 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 __SCREAMING_SNAKE_CASE : int = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(lowercase__ ): # On TPU it's best to pad everything to the same length or training will be very slow. __SCREAMING_SNAKE_CASE : Dict = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": __SCREAMING_SNAKE_CASE : Union[str, Any] = 16 elif accelerator.mixed_precision != "no": __SCREAMING_SNAKE_CASE : str = 8 else: __SCREAMING_SNAKE_CASE : Union[str, Any] = None return tokenizer.pad( lowercase__ , padding='''longest''' , max_length=lowercase__ , pad_to_multiple_of=lowercase__ , return_tensors='''pt''' , ) # Instantiate dataloaders. __SCREAMING_SNAKE_CASE : Tuple = DataLoader( tokenized_datasets['''train'''] , shuffle=lowercase__ , collate_fn=lowercase__ , batch_size=lowercase__ ) __SCREAMING_SNAKE_CASE : Tuple = DataLoader( tokenized_datasets['''validation'''] , shuffle=lowercase__ , collate_fn=lowercase__ , batch_size=lowercase__ ) 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 : Optional[int] =mocked_dataloaders # noqa: F811 def _UpperCamelCase ( lowercase__ , lowercase__ ): # For testing only if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , lowercase__ ) == "1": __SCREAMING_SNAKE_CASE : Optional[Any] = 2 # New Code # __SCREAMING_SNAKE_CASE : Optional[int] = int(args.gradient_accumulation_steps ) # Initialize accelerator __SCREAMING_SNAKE_CASE : Dict = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , gradient_accumulation_steps=lowercase__ ) if accelerator.distributed_type == DistributedType.TPU and gradient_accumulation_steps > 1: raise NotImplementedError( '''Gradient accumulation on TPUs is currently not supported. Pass `gradient_accumulation_steps=1`''' ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __SCREAMING_SNAKE_CASE : Tuple = config['''lr'''] __SCREAMING_SNAKE_CASE : Optional[Any] = int(config['''num_epochs'''] ) __SCREAMING_SNAKE_CASE : Union[str, Any] = int(config['''seed'''] ) __SCREAMING_SNAKE_CASE : Optional[Any] = int(config['''batch_size'''] ) __SCREAMING_SNAKE_CASE : Dict = evaluate.load('''glue''' , '''mrpc''' ) set_seed(lowercase__ ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Optional[int] = get_dataloaders(lowercase__ , lowercase__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __SCREAMING_SNAKE_CASE : Optional[int] = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=lowercase__ ) # 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). __SCREAMING_SNAKE_CASE : int = model.to(accelerator.device ) # Instantiate optimizer __SCREAMING_SNAKE_CASE : Union[str, Any] = AdamW(params=model.parameters() , lr=lowercase__ ) # Instantiate scheduler __SCREAMING_SNAKE_CASE : Tuple = get_linear_schedule_with_warmup( optimizer=lowercase__ , num_warmup_steps=100 , num_training_steps=(len(lowercase__ ) * 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. __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : str = accelerator.prepare( lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ ) # Now we train the model for epoch in range(lowercase__ ): model.train() for step, batch in enumerate(lowercase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) # New code # # We use the new `accumulate` context manager to perform gradient accumulation # We also currently do not support TPUs nor advise it as bugs were found on the XLA side when running our tests. with accelerator.accumulate(lowercase__ ): __SCREAMING_SNAKE_CASE : Tuple = model(**lowercase__ ) __SCREAMING_SNAKE_CASE : str = output.loss accelerator.backward(lowercase__ ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(lowercase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): __SCREAMING_SNAKE_CASE : Dict = model(**lowercase__ ) __SCREAMING_SNAKE_CASE : List[str] = outputs.logits.argmax(dim=-1 ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Optional[Any] = accelerator.gather_for_metrics((predictions, batch['''labels''']) ) metric.add_batch( predictions=lowercase__ , references=lowercase__ , ) __SCREAMING_SNAKE_CASE : Union[str, Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , lowercase__ ) def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : str = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=lowercase__ , default=lowercase__ , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) # New Code # parser.add_argument( '''--gradient_accumulation_steps''' , type=lowercase__ , default=1 , help='''The number of minibatches to be ran before gradients are accumulated.''' , ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) __SCREAMING_SNAKE_CASE : Any = parser.parse_args() __SCREAMING_SNAKE_CASE : Union[str, Any] = {'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(lowercase__ , lowercase__ ) if __name__ == "__main__": main()
9
from collections import Counter import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split __lowerCAmelCase : List[Any] =datasets.load_iris() __lowerCAmelCase : Tuple =np.array(data['data']) __lowerCAmelCase : Dict =np.array(data['target']) __lowerCAmelCase : List[str] =data['target_names'] __lowerCAmelCase ,__lowerCAmelCase ,__lowerCAmelCase ,__lowerCAmelCase : str =train_test_split(X, y) def _UpperCamelCase ( lowercase__ , lowercase__ ): return np.linalg.norm(np.array(lowercase__ ) - np.array(lowercase__ ) ) def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__=5 ): __SCREAMING_SNAKE_CASE : Optional[int] = zip(lowercase__ , lowercase__ ) # List of distances of all points from the point to be classified __SCREAMING_SNAKE_CASE : Dict = [] for data_point in data: __SCREAMING_SNAKE_CASE : Tuple = euclidean_distance(data_point[0] , lowercase__ ) distances.append((distance, data_point[1]) ) # Choosing 'k' points with the least distances. __SCREAMING_SNAKE_CASE : int = [i[1] for i in sorted(lowercase__ )[:k]] # Most commonly occurring class among them # is the class into which the point is classified __SCREAMING_SNAKE_CASE : Any = Counter(lowercase__ ).most_common(1 )[0][0] return classes[result] if __name__ == "__main__": print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))
9
1
from queue import PriorityQueue from typing import Any import numpy as np def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , ): for nxt, d in graph[v]: if nxt in visited_forward: continue __SCREAMING_SNAKE_CASE : Union[str, Any] = cst_fwd.get(lowercase__ , np.inf ) __SCREAMING_SNAKE_CASE : int = cst_fwd[v] + d if new_cost_f < old_cost_f: queue.put((new_cost_f, nxt) ) __SCREAMING_SNAKE_CASE : int = new_cost_f __SCREAMING_SNAKE_CASE : Optional[Any] = v if nxt in visited_backward: if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance: __SCREAMING_SNAKE_CASE : Tuple = cst_fwd[v] + d + cst_bwd[nxt] return shortest_distance def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : List[Any] = -1 __SCREAMING_SNAKE_CASE : str = set() __SCREAMING_SNAKE_CASE : Dict = set() __SCREAMING_SNAKE_CASE : List[Any] = {source: 0} __SCREAMING_SNAKE_CASE : str = {destination: 0} __SCREAMING_SNAKE_CASE : Union[str, Any] = {source: None} __SCREAMING_SNAKE_CASE : int = {destination: None} __SCREAMING_SNAKE_CASE : PriorityQueue[Any] = PriorityQueue() __SCREAMING_SNAKE_CASE : PriorityQueue[Any] = PriorityQueue() __SCREAMING_SNAKE_CASE : Dict = np.inf queue_forward.put((0, source) ) queue_backward.put((0, destination) ) if source == destination: return 0 while not queue_forward.empty() and not queue_backward.empty(): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Any = queue_forward.get() visited_forward.add(lowercase__ ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Tuple = queue_backward.get() visited_backward.add(lowercase__ ) __SCREAMING_SNAKE_CASE : Union[str, Any] = pass_and_relaxation( lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , ) __SCREAMING_SNAKE_CASE : str = pass_and_relaxation( lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , ) if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance: break if shortest_distance != np.inf: __SCREAMING_SNAKE_CASE : Optional[int] = shortest_distance return shortest_path_distance __lowerCAmelCase : int ={ 'B': [['C', 1]], 'C': [['D', 1]], 'D': [['F', 1]], 'E': [['B', 1], ['G', 2]], 'F': [], 'G': [['F', 1]], } __lowerCAmelCase : Tuple ={ 'B': [['E', 1]], 'C': [['B', 1]], 'D': [['C', 1]], 'F': [['D', 1], ['G', 1]], 'E': [[None, np.inf]], 'G': [['E', 2]], } if __name__ == "__main__": import doctest doctest.testmod()
9
import unittest from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available from transformers.pipelines import pipeline from transformers.pipelines.document_question_answering import apply_tesseract from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_detectrona, require_pytesseract, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image from transformers.image_utils import load_image else: class _lowercase : '''simple docstring''' @staticmethod def __magic_name__( *lowerCAmelCase__ :Union[str, Any] , **lowerCAmelCase__ :str ) -> Union[str, Any]: pass def _UpperCamelCase ( lowercase__ ): return None # This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace, # so we can expect it to be available. __lowerCAmelCase : str =( 'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png' ) @is_pipeline_test @require_torch @require_vision class _lowercase ( unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Tuple = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING @require_pytesseract @require_vision def __magic_name__( self :Any , lowerCAmelCase__ :List[str] , lowerCAmelCase__ :Optional[Any] , lowerCAmelCase__ :Any ) -> Any: __SCREAMING_SNAKE_CASE : Optional[int] = pipeline( '''document-question-answering''' , model=lowerCAmelCase__ , tokenizer=lowerCAmelCase__ , image_processor=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = INVOICE_URL __SCREAMING_SNAKE_CASE : Optional[Any] = list(zip(*apply_tesseract(load_image(lowerCAmelCase__ ) , lowerCAmelCase__ , '''''' ) ) ) __SCREAMING_SNAKE_CASE : str = '''What is the placebo?''' __SCREAMING_SNAKE_CASE : str = [ { '''image''': load_image(lowerCAmelCase__ ), '''question''': question, }, { '''image''': image, '''question''': question, }, { '''image''': image, '''question''': question, '''word_boxes''': word_boxes, }, ] return dqa_pipeline, examples def __magic_name__( self :Optional[Any] , lowerCAmelCase__ :Tuple , lowerCAmelCase__ :Tuple ) -> str: __SCREAMING_SNAKE_CASE : Any = dqa_pipeline(lowerCAmelCase__ , top_k=2 ) self.assertEqual( lowerCAmelCase__ , [ [ {'''score''': ANY(lowerCAmelCase__ ), '''answer''': ANY(lowerCAmelCase__ ), '''start''': ANY(lowerCAmelCase__ ), '''end''': ANY(lowerCAmelCase__ )}, {'''score''': ANY(lowerCAmelCase__ ), '''answer''': ANY(lowerCAmelCase__ ), '''start''': ANY(lowerCAmelCase__ ), '''end''': ANY(lowerCAmelCase__ )}, ] ] * 3 , ) @require_torch @require_detectrona @require_pytesseract def __magic_name__( self :Dict ) -> List[str]: __SCREAMING_SNAKE_CASE : Tuple = pipeline('''document-question-answering''' , model='''hf-internal-testing/tiny-random-layoutlmv2''' ) __SCREAMING_SNAKE_CASE : Dict = INVOICE_URL __SCREAMING_SNAKE_CASE : int = '''How many cats are there?''' __SCREAMING_SNAKE_CASE : Optional[int] = [ {'''score''': 0.0001, '''answer''': '''oy 2312/2019''', '''start''': 38, '''end''': 39}, {'''score''': 0.0001, '''answer''': '''oy 2312/2019 DUE''', '''start''': 38, '''end''': 40}, ] __SCREAMING_SNAKE_CASE : Tuple = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , top_k=2 ) self.assertEqual(nested_simplify(lowerCAmelCase__ , decimals=4 ) , lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual(nested_simplify(lowerCAmelCase__ , decimals=4 ) , lowerCAmelCase__ ) # This image does not detect ANY text in it, meaning layoutlmv2 should fail. # Empty answer probably __SCREAMING_SNAKE_CASE : Any = '''./tests/fixtures/tests_samples/COCO/000000039769.png''' __SCREAMING_SNAKE_CASE : List[Any] = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , top_k=2 ) self.assertEqual(lowerCAmelCase__ , [] ) # We can optionnally pass directly the words and bounding boxes __SCREAMING_SNAKE_CASE : Union[str, Any] = '''./tests/fixtures/tests_samples/COCO/000000039769.png''' __SCREAMING_SNAKE_CASE : Union[str, Any] = [] __SCREAMING_SNAKE_CASE : Union[str, Any] = [] __SCREAMING_SNAKE_CASE : Union[str, Any] = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , words=lowerCAmelCase__ , boxes=lowerCAmelCase__ , top_k=2 ) self.assertEqual(lowerCAmelCase__ , [] ) @slow @require_torch @require_detectrona @require_pytesseract def __magic_name__( self :int ) -> Optional[Any]: __SCREAMING_SNAKE_CASE : List[Any] = pipeline( '''document-question-answering''' , model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa''' , revision='''9977165''' , ) __SCREAMING_SNAKE_CASE : Dict = INVOICE_URL __SCREAMING_SNAKE_CASE : Any = '''What is the invoice number?''' __SCREAMING_SNAKE_CASE : Any = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) __SCREAMING_SNAKE_CASE : Tuple = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) __SCREAMING_SNAKE_CASE : List[Any] = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ [ {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ], ] * 2 , ) @slow @require_torch @require_detectrona @require_pytesseract def __magic_name__( self :Optional[Any] ) -> Any: __SCREAMING_SNAKE_CASE : int = pipeline( '''document-question-answering''' , model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa''' , revision='''9977165''' , max_seq_len=50 , ) __SCREAMING_SNAKE_CASE : Union[str, Any] = INVOICE_URL __SCREAMING_SNAKE_CASE : Tuple = '''What is the invoice number?''' __SCREAMING_SNAKE_CASE : List[str] = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) __SCREAMING_SNAKE_CASE : int = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) __SCREAMING_SNAKE_CASE : str = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ [ {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] ] * 2 , ) @slow @require_torch @require_pytesseract @require_vision def __magic_name__( self :int ) -> List[Any]: __SCREAMING_SNAKE_CASE : List[Any] = AutoTokenizer.from_pretrained( '''impira/layoutlm-document-qa''' , revision='''3dc6de3''' , add_prefix_space=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = pipeline( '''document-question-answering''' , model='''impira/layoutlm-document-qa''' , tokenizer=lowerCAmelCase__ , revision='''3dc6de3''' , ) __SCREAMING_SNAKE_CASE : Union[str, Any] = INVOICE_URL __SCREAMING_SNAKE_CASE : str = '''What is the invoice number?''' __SCREAMING_SNAKE_CASE : Dict = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) __SCREAMING_SNAKE_CASE : Union[str, Any] = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) __SCREAMING_SNAKE_CASE : Union[str, Any] = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ [ {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] ] * 2 , ) __SCREAMING_SNAKE_CASE : Optional[int] = list(zip(*apply_tesseract(load_image(lowerCAmelCase__ ) , lowerCAmelCase__ , '''''' ) ) ) # This model should also work if `image` is set to None __SCREAMING_SNAKE_CASE : str = dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) @slow @require_torch @require_pytesseract @require_vision def __magic_name__( self :str ) -> Dict: __SCREAMING_SNAKE_CASE : Optional[int] = AutoTokenizer.from_pretrained( '''impira/layoutlm-document-qa''' , revision='''3dc6de3''' , add_prefix_space=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Tuple = pipeline( '''document-question-answering''' , model='''impira/layoutlm-document-qa''' , tokenizer=lowerCAmelCase__ , revision='''3dc6de3''' , max_seq_len=50 , ) __SCREAMING_SNAKE_CASE : List[str] = INVOICE_URL __SCREAMING_SNAKE_CASE : Dict = '''What is the invoice number?''' __SCREAMING_SNAKE_CASE : List[Any] = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) __SCREAMING_SNAKE_CASE : Optional[int] = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ [ {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] ] * 2 , ) __SCREAMING_SNAKE_CASE : List[str] = list(zip(*apply_tesseract(load_image(lowerCAmelCase__ ) , lowerCAmelCase__ , '''''' ) ) ) # This model should also work if `image` is set to None __SCREAMING_SNAKE_CASE : List[Any] = dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(lowerCAmelCase__ , decimals=4 ) , [ {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) @slow @require_torch def __magic_name__( self :Union[str, Any] ) -> Tuple: __SCREAMING_SNAKE_CASE : str = pipeline( '''document-question-answering''' , model='''naver-clova-ix/donut-base-finetuned-docvqa''' , tokenizer=AutoTokenizer.from_pretrained('''naver-clova-ix/donut-base-finetuned-docvqa''' ) , feature_extractor='''naver-clova-ix/donut-base-finetuned-docvqa''' , ) __SCREAMING_SNAKE_CASE : Union[str, Any] = INVOICE_URL __SCREAMING_SNAKE_CASE : Optional[int] = '''What is the invoice number?''' __SCREAMING_SNAKE_CASE : Tuple = dqa_pipeline(image=lowerCAmelCase__ , question=lowerCAmelCase__ , top_k=2 ) self.assertEqual(nested_simplify(lowerCAmelCase__ , decimals=4 ) , [{'''answer''': '''us-001'''}] ) @require_tf @unittest.skip('''Document question answering not implemented in TF''' ) def __magic_name__( self :Union[str, Any] ) -> Tuple: pass
9
1
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_download, hf_hub_url from PIL import Image from transformers import DetaConfig, DetaForObjectDetection, DetaImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() __lowerCAmelCase : List[str] =logging.get_logger(__name__) def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : Any = SwinConfig( embed_dim=192 , depths=(2, 2, 18, 2) , num_heads=(6, 12, 24, 48) , window_size=12 , out_features=['''stage2''', '''stage3''', '''stage4'''] , ) __SCREAMING_SNAKE_CASE : Tuple = DetaConfig( backbone_config=lowercase__ , num_queries=900 , encoder_ffn_dim=2048 , decoder_ffn_dim=2048 , num_feature_levels=5 , assign_first_stage=lowercase__ , with_box_refine=lowercase__ , two_stage=lowercase__ , ) # set labels __SCREAMING_SNAKE_CASE : Tuple = '''huggingface/label-files''' if "o365" in model_name: __SCREAMING_SNAKE_CASE : Dict = 366 __SCREAMING_SNAKE_CASE : Any = '''object365-id2label.json''' else: __SCREAMING_SNAKE_CASE : List[str] = 91 __SCREAMING_SNAKE_CASE : List[Any] = '''coco-detection-id2label.json''' __SCREAMING_SNAKE_CASE : List[str] = num_labels __SCREAMING_SNAKE_CASE : Union[str, Any] = json.load(open(cached_download(hf_hub_url(lowercase__ , lowercase__ , repo_type='''dataset''' ) ) , '''r''' ) ) __SCREAMING_SNAKE_CASE : Dict = {int(lowercase__ ): v for k, v in idalabel.items()} __SCREAMING_SNAKE_CASE : Optional[Any] = idalabel __SCREAMING_SNAKE_CASE : Optional[int] = {v: k for k, v in idalabel.items()} return config def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : int = [] # stem # fmt: off rename_keys.append(('''backbone.0.body.patch_embed.proj.weight''', '''model.backbone.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.proj.bias''', '''model.backbone.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.weight''', '''model.backbone.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.bias''', '''model.backbone.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.0.body.layers.{i}.blocks.{j}.norm1.weight''', F'''model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight''') ) rename_keys.append((F'''backbone.0.body.layers.{i}.blocks.{j}.norm1.bias''', F'''model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias''') ) rename_keys.append((F'''backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_bias_table''', F'''model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table''') ) rename_keys.append((F'''backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_index''', F'''model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index''') ) rename_keys.append((F'''backbone.0.body.layers.{i}.blocks.{j}.attn.proj.weight''', F'''model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight''') ) rename_keys.append((F'''backbone.0.body.layers.{i}.blocks.{j}.attn.proj.bias''', F'''model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias''') ) rename_keys.append((F'''backbone.0.body.layers.{i}.blocks.{j}.norm2.weight''', F'''model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight''') ) rename_keys.append((F'''backbone.0.body.layers.{i}.blocks.{j}.norm2.bias''', F'''model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias''') ) rename_keys.append((F'''backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.weight''', F'''model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight''') ) rename_keys.append((F'''backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.bias''', F'''model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias''') ) rename_keys.append((F'''backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.weight''', F'''model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.weight''') ) rename_keys.append((F'''backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.bias''', F'''model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.bias''') ) if i < 3: rename_keys.append((F'''backbone.0.body.layers.{i}.downsample.reduction.weight''', F'''model.backbone.model.encoder.layers.{i}.downsample.reduction.weight''') ) rename_keys.append((F'''backbone.0.body.layers.{i}.downsample.norm.weight''', F'''model.backbone.model.encoder.layers.{i}.downsample.norm.weight''') ) rename_keys.append((F'''backbone.0.body.layers.{i}.downsample.norm.bias''', F'''model.backbone.model.encoder.layers.{i}.downsample.norm.bias''') ) rename_keys.append(('''backbone.0.body.norm1.weight''', '''model.backbone.model.hidden_states_norms.stage2.weight''') ) rename_keys.append(('''backbone.0.body.norm1.bias''', '''model.backbone.model.hidden_states_norms.stage2.bias''') ) rename_keys.append(('''backbone.0.body.norm2.weight''', '''model.backbone.model.hidden_states_norms.stage3.weight''') ) rename_keys.append(('''backbone.0.body.norm2.bias''', '''model.backbone.model.hidden_states_norms.stage3.bias''') ) rename_keys.append(('''backbone.0.body.norm3.weight''', '''model.backbone.model.hidden_states_norms.stage4.weight''') ) rename_keys.append(('''backbone.0.body.norm3.bias''', '''model.backbone.model.hidden_states_norms.stage4.bias''') ) # transformer encoder for i in range(config.encoder_layers ): rename_keys.append((F'''transformer.encoder.layers.{i}.self_attn.sampling_offsets.weight''', F'''model.encoder.layers.{i}.self_attn.sampling_offsets.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.self_attn.sampling_offsets.bias''', F'''model.encoder.layers.{i}.self_attn.sampling_offsets.bias''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.self_attn.attention_weights.weight''', F'''model.encoder.layers.{i}.self_attn.attention_weights.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.self_attn.attention_weights.bias''', F'''model.encoder.layers.{i}.self_attn.attention_weights.bias''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.self_attn.value_proj.weight''', F'''model.encoder.layers.{i}.self_attn.value_proj.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.self_attn.value_proj.bias''', F'''model.encoder.layers.{i}.self_attn.value_proj.bias''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.self_attn.output_proj.weight''', F'''model.encoder.layers.{i}.self_attn.output_proj.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.self_attn.output_proj.bias''', F'''model.encoder.layers.{i}.self_attn.output_proj.bias''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.norm1.weight''', F'''model.encoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.norm1.bias''', F'''model.encoder.layers.{i}.self_attn_layer_norm.bias''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.weight''', F'''model.encoder.layers.{i}.fc1.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.bias''', F'''model.encoder.layers.{i}.fc1.bias''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.weight''', F'''model.encoder.layers.{i}.fc2.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.bias''', F'''model.encoder.layers.{i}.fc2.bias''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.weight''', F'''model.encoder.layers.{i}.final_layer_norm.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.bias''', F'''model.encoder.layers.{i}.final_layer_norm.bias''') ) # transformer decoder for i in range(config.decoder_layers ): rename_keys.append((F'''transformer.decoder.layers.{i}.cross_attn.sampling_offsets.weight''', F'''model.decoder.layers.{i}.encoder_attn.sampling_offsets.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.cross_attn.sampling_offsets.bias''', F'''model.decoder.layers.{i}.encoder_attn.sampling_offsets.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.cross_attn.attention_weights.weight''', F'''model.decoder.layers.{i}.encoder_attn.attention_weights.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.cross_attn.attention_weights.bias''', F'''model.decoder.layers.{i}.encoder_attn.attention_weights.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.cross_attn.value_proj.weight''', F'''model.decoder.layers.{i}.encoder_attn.value_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.cross_attn.value_proj.bias''', F'''model.decoder.layers.{i}.encoder_attn.value_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.cross_attn.output_proj.weight''', F'''model.decoder.layers.{i}.encoder_attn.output_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.cross_attn.output_proj.bias''', F'''model.decoder.layers.{i}.encoder_attn.output_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm1.weight''', F'''model.decoder.layers.{i}.encoder_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm1.bias''', F'''model.decoder.layers.{i}.encoder_attn_layer_norm.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.self_attn.out_proj.weight''', F'''model.decoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.self_attn.out_proj.bias''', F'''model.decoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm2.weight''', F'''model.decoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm2.bias''', F'''model.decoder.layers.{i}.self_attn_layer_norm.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.weight''', F'''model.decoder.layers.{i}.fc1.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.bias''', F'''model.decoder.layers.{i}.fc1.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.weight''', F'''model.decoder.layers.{i}.fc2.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.bias''', F'''model.decoder.layers.{i}.fc2.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.weight''', F'''model.decoder.layers.{i}.final_layer_norm.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.bias''', F'''model.decoder.layers.{i}.final_layer_norm.bias''') ) # fmt: on return rename_keys def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Tuple = dct.pop(lowercase__ ) __SCREAMING_SNAKE_CASE : Any = val def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : str = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): __SCREAMING_SNAKE_CASE : Optional[Any] = 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) __SCREAMING_SNAKE_CASE : Optional[int] = state_dict.pop(F'''backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.weight''' ) __SCREAMING_SNAKE_CASE : Tuple = state_dict.pop(F'''backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict __SCREAMING_SNAKE_CASE : Any = in_proj_weight[:dim, :] __SCREAMING_SNAKE_CASE : Optional[Any] = in_proj_bias[: dim] __SCREAMING_SNAKE_CASE : Optional[Any] = in_proj_weight[ dim : dim * 2, : ] __SCREAMING_SNAKE_CASE : str = in_proj_bias[ dim : dim * 2 ] __SCREAMING_SNAKE_CASE : Tuple = in_proj_weight[ -dim :, : ] __SCREAMING_SNAKE_CASE : Tuple = in_proj_bias[-dim :] # fmt: on def _UpperCamelCase ( lowercase__ , lowercase__ ): # transformer decoder self-attention layers __SCREAMING_SNAKE_CASE : Any = config.d_model for i in range(config.decoder_layers ): # read in weights + bias of input projection layer of self-attention __SCREAMING_SNAKE_CASE : List[str] = state_dict.pop(F'''transformer.decoder.layers.{i}.self_attn.in_proj_weight''' ) __SCREAMING_SNAKE_CASE : Any = state_dict.pop(F'''transformer.decoder.layers.{i}.self_attn.in_proj_bias''' ) # next, add query, keys and values (in that order) to the state dict __SCREAMING_SNAKE_CASE : Any = in_proj_weight[:hidden_size, :] __SCREAMING_SNAKE_CASE : Tuple = in_proj_bias[:hidden_size] __SCREAMING_SNAKE_CASE : str = in_proj_weight[ hidden_size : hidden_size * 2, : ] __SCREAMING_SNAKE_CASE : Optional[int] = in_proj_bias[hidden_size : hidden_size * 2] __SCREAMING_SNAKE_CASE : int = in_proj_weight[-hidden_size:, :] __SCREAMING_SNAKE_CASE : Optional[Any] = in_proj_bias[-hidden_size:] def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : int = '''http://images.cocodataset.org/val2017/000000039769.jpg''' __SCREAMING_SNAKE_CASE : Optional[int] = Image.open(requests.get(lowercase__ , stream=lowercase__ ).raw ) return im @torch.no_grad() def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Tuple = get_deta_config(lowercase__ ) # load original state dict if model_name == "deta-swin-large": __SCREAMING_SNAKE_CASE : Union[str, Any] = hf_hub_download(repo_id='''nielsr/deta-checkpoints''' , filename='''adet_swin_ft.pth''' ) elif model_name == "deta-swin-large-o365": __SCREAMING_SNAKE_CASE : Optional[Any] = hf_hub_download(repo_id='''jozhang97/deta-swin-l-o365''' , filename='''deta_swin_pt_o365.pth''' ) else: raise ValueError(F'''Model name {model_name} not supported''' ) __SCREAMING_SNAKE_CASE : Dict = torch.load(lowercase__ , map_location='''cpu''' )['''model'''] # original state dict for name, param in state_dict.items(): print(lowercase__ , param.shape ) # rename keys __SCREAMING_SNAKE_CASE : Optional[int] = create_rename_keys(lowercase__ ) for src, dest in rename_keys: rename_key(lowercase__ , lowercase__ , lowercase__ ) read_in_swin_q_k_v(lowercase__ , config.backbone_config ) read_in_decoder_q_k_v(lowercase__ , lowercase__ ) # fix some prefixes for key in state_dict.copy().keys(): if "transformer.decoder.class_embed" in key or "transformer.decoder.bbox_embed" in key: __SCREAMING_SNAKE_CASE : Tuple = state_dict.pop(lowercase__ ) __SCREAMING_SNAKE_CASE : List[str] = val if "input_proj" in key: __SCREAMING_SNAKE_CASE : List[str] = state_dict.pop(lowercase__ ) __SCREAMING_SNAKE_CASE : str = val if "level_embed" in key or "pos_trans" in key or "pix_trans" in key or "enc_output" in key: __SCREAMING_SNAKE_CASE : Optional[int] = state_dict.pop(lowercase__ ) __SCREAMING_SNAKE_CASE : Union[str, Any] = val # finally, create HuggingFace model and load state dict __SCREAMING_SNAKE_CASE : List[str] = DetaForObjectDetection(lowercase__ ) model.load_state_dict(lowercase__ ) model.eval() __SCREAMING_SNAKE_CASE : List[Any] = '''cuda''' if torch.cuda.is_available() else '''cpu''' model.to(lowercase__ ) # load image processor __SCREAMING_SNAKE_CASE : Union[str, Any] = DetaImageProcessor(format='''coco_detection''' ) # verify our conversion on image __SCREAMING_SNAKE_CASE : Tuple = prepare_img() __SCREAMING_SNAKE_CASE : Optional[Any] = processor(images=lowercase__ , return_tensors='''pt''' ) __SCREAMING_SNAKE_CASE : Optional[int] = encoding['''pixel_values'''] __SCREAMING_SNAKE_CASE : Union[str, Any] = model(pixel_values.to(lowercase__ ) ) # verify logits print('''Logits:''' , outputs.logits[0, :3, :3] ) print('''Boxes:''' , outputs.pred_boxes[0, :3, :3] ) if model_name == "deta-swin-large": __SCREAMING_SNAKE_CASE : str = torch.tensor( [[-7.6308, -2.8485, -5.3737], [-7.2037, -4.5505, -4.8027], [-7.2943, -4.2611, -4.6617]] ) __SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor([[0.4987, 0.4969, 0.9999], [0.2549, 0.5498, 0.4805], [0.5498, 0.2757, 0.0569]] ) elif model_name == "deta-swin-large-o365": __SCREAMING_SNAKE_CASE : List[Any] = torch.tensor( [[-8.0122, -3.5720, -4.9717], [-8.1547, -3.6886, -4.6389], [-7.6610, -3.6194, -5.0134]] ) __SCREAMING_SNAKE_CASE : List[Any] = torch.tensor([[0.2523, 0.5549, 0.4881], [0.7715, 0.4149, 0.4601], [0.5503, 0.2753, 0.0575]] ) assert torch.allclose(outputs.logits[0, :3, :3] , expected_logits.to(lowercase__ ) , atol=1e-4 ) assert torch.allclose(outputs.pred_boxes[0, :3, :3] , expected_boxes.to(lowercase__ ) , atol=1e-4 ) print('''Everything ok!''' ) if pytorch_dump_folder_path: # Save model and processor logger.info(F'''Saving PyTorch model and processor to {pytorch_dump_folder_path}...''' ) Path(lowercase__ ).mkdir(exist_ok=lowercase__ ) model.save_pretrained(lowercase__ ) processor.save_pretrained(lowercase__ ) # Push to hub if push_to_hub: print('''Pushing model and processor to hub...''' ) model.push_to_hub(F'''jozhang97/{model_name}''' ) processor.push_to_hub(F'''jozhang97/{model_name}''' ) if __name__ == "__main__": __lowerCAmelCase : str =argparse.ArgumentParser() parser.add_argument( '--model_name', type=str, default='deta-swin-large', choices=['deta-swin-large', 'deta-swin-large-o365'], help='Name of the model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.', ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) __lowerCAmelCase : List[str] =parser.parse_args() convert_deta_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
9
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase : Any ={'configuration_vit_msn': ['VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ViTMSNConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : int =[ 'VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST', 'ViTMSNModel', 'ViTMSNForImageClassification', 'ViTMSNPreTrainedModel', ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys __lowerCAmelCase : Union[str, Any] =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
9
1
import unittest from typing import Dict, List, Optional, Union import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import BridgeTowerImageProcessor class _lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self :Any , lowerCAmelCase__ :Tuple , lowerCAmelCase__ :bool = True , lowerCAmelCase__ :Dict[str, int] = None , lowerCAmelCase__ :int = 32 , lowerCAmelCase__ :bool = True , lowerCAmelCase__ :Union[int, float] = 1 / 255 , lowerCAmelCase__ :bool = True , lowerCAmelCase__ :bool = True , lowerCAmelCase__ :Optional[Union[float, List[float]]] = [0.4814_5466, 0.457_8275, 0.4082_1073] , lowerCAmelCase__ :Optional[Union[float, List[float]]] = [0.2686_2954, 0.2613_0258, 0.2757_7711] , lowerCAmelCase__ :bool = True , lowerCAmelCase__ :Tuple=7 , lowerCAmelCase__ :Any=30 , lowerCAmelCase__ :Optional[Any]=400 , lowerCAmelCase__ :List[str]=3 , ) -> List[Any]: __SCREAMING_SNAKE_CASE : Tuple = parent __SCREAMING_SNAKE_CASE : List[str] = do_resize __SCREAMING_SNAKE_CASE : List[str] = size if size is not None else {'''shortest_edge''': 288} __SCREAMING_SNAKE_CASE : str = size_divisor __SCREAMING_SNAKE_CASE : Optional[int] = do_rescale __SCREAMING_SNAKE_CASE : Dict = rescale_factor __SCREAMING_SNAKE_CASE : int = do_normalize __SCREAMING_SNAKE_CASE : Union[str, Any] = do_center_crop __SCREAMING_SNAKE_CASE : int = image_mean __SCREAMING_SNAKE_CASE : List[Any] = image_std __SCREAMING_SNAKE_CASE : Any = do_pad __SCREAMING_SNAKE_CASE : str = batch_size __SCREAMING_SNAKE_CASE : Dict = num_channels __SCREAMING_SNAKE_CASE : List[Any] = min_resolution __SCREAMING_SNAKE_CASE : Tuple = max_resolution def __magic_name__( self :str ) -> Any: return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "size_divisor": self.size_divisor, } def __magic_name__( self :Optional[Any] , lowerCAmelCase__ :List[str] , lowerCAmelCase__ :Tuple=False ) -> List[Any]: if not batched: __SCREAMING_SNAKE_CASE : Dict = self.size['''shortest_edge'''] __SCREAMING_SNAKE_CASE : int = image_inputs[0] if isinstance(lowerCAmelCase__ , Image.Image ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : List[str] = image.size else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : int = image.shape[1], image.shape[2] __SCREAMING_SNAKE_CASE : Optional[int] = size / min(lowerCAmelCase__ , lowerCAmelCase__ ) if h < w: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : int = size, scale * w else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : int = scale * h, size __SCREAMING_SNAKE_CASE : Tuple = int((1_333 / 800) * size ) if max(lowerCAmelCase__ , lowerCAmelCase__ ) > max_size: __SCREAMING_SNAKE_CASE : Tuple = max_size / max(lowerCAmelCase__ , lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Any = newh * scale __SCREAMING_SNAKE_CASE : List[Any] = neww * scale __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Any = int(newh + 0.5 ), int(neww + 0.5 ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : int = ( newh // self.size_divisor * self.size_divisor, neww // self.size_divisor * self.size_divisor, ) else: __SCREAMING_SNAKE_CASE : Optional[Any] = [] for image in image_inputs: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : str = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) __SCREAMING_SNAKE_CASE : int = max(lowerCAmelCase__ , key=lambda lowerCAmelCase__ : item[0] )[0] __SCREAMING_SNAKE_CASE : str = max(lowerCAmelCase__ , key=lambda lowerCAmelCase__ : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class _lowercase ( A__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[Any] = BridgeTowerImageProcessor if is_vision_available() else None def __magic_name__( self :Optional[int] ) -> Any: __SCREAMING_SNAKE_CASE : Union[str, Any] = BridgeTowerImageProcessingTester(self ) @property def __magic_name__( self :Dict ) -> Optional[int]: return self.image_processor_tester.prepare_image_processor_dict() def __magic_name__( self :List[str] ) -> List[str]: __SCREAMING_SNAKE_CASE : str = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCAmelCase__ , '''image_mean''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''image_std''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_normalize''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_resize''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''size''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''size_divisor''' ) ) def __magic_name__( self :int ) -> List[str]: pass def __magic_name__( self :Optional[int] ) -> int: # Initialize image processor __SCREAMING_SNAKE_CASE : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __SCREAMING_SNAKE_CASE : Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , Image.Image ) # Test not batched input __SCREAMING_SNAKE_CASE : Dict = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Dict = self.image_processor_tester.get_expected_values(lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched __SCREAMING_SNAKE_CASE : Tuple = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : str = self.image_processor_tester.get_expected_values(lowerCAmelCase__ , batched=lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __magic_name__( self :str ) -> Any: # Initialize image processor __SCREAMING_SNAKE_CASE : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __SCREAMING_SNAKE_CASE : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , numpify=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , np.ndarray ) # Test not batched input __SCREAMING_SNAKE_CASE : Any = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : List[str] = self.image_processor_tester.get_expected_values(lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched __SCREAMING_SNAKE_CASE : int = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Union[str, Any] = self.image_processor_tester.get_expected_values(lowerCAmelCase__ , batched=lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __magic_name__( self :List[str] ) -> Any: # Initialize image processor __SCREAMING_SNAKE_CASE : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __SCREAMING_SNAKE_CASE : Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , torchify=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , torch.Tensor ) # Test not batched input __SCREAMING_SNAKE_CASE : Tuple = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Union[str, Any] = self.image_processor_tester.get_expected_values(lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched __SCREAMING_SNAKE_CASE : Dict = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : int = self.image_processor_tester.get_expected_values(lowerCAmelCase__ , batched=lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , )
9
from typing import Optional from .. import Features, NamedSplit from ..packaged_modules.text.text import Text from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class _lowercase ( A__ ): '''simple docstring''' def __init__( self :int , lowerCAmelCase__ :NestedDataStructureLike[PathLike] , lowerCAmelCase__ :Optional[NamedSplit] = None , lowerCAmelCase__ :Optional[Features] = None , lowerCAmelCase__ :str = None , lowerCAmelCase__ :bool = False , lowerCAmelCase__ :bool = False , lowerCAmelCase__ :Optional[int] = None , **lowerCAmelCase__ :Optional[int] , ) -> Tuple: super().__init__( lowerCAmelCase__ , split=lowerCAmelCase__ , features=lowerCAmelCase__ , cache_dir=lowerCAmelCase__ , keep_in_memory=lowerCAmelCase__ , streaming=lowerCAmelCase__ , num_proc=lowerCAmelCase__ , **lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE : List[str] = path_or_paths if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else {self.split: path_or_paths} __SCREAMING_SNAKE_CASE : int = Text( cache_dir=lowerCAmelCase__ , data_files=lowerCAmelCase__ , features=lowerCAmelCase__ , **lowerCAmelCase__ , ) def __magic_name__( self :Dict ) -> Tuple: # Build iterable dataset if self.streaming: __SCREAMING_SNAKE_CASE : int = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: __SCREAMING_SNAKE_CASE : List[str] = None __SCREAMING_SNAKE_CASE : str = None __SCREAMING_SNAKE_CASE : Dict = None __SCREAMING_SNAKE_CASE : Tuple = None self.builder.download_and_prepare( download_config=lowerCAmelCase__ , download_mode=lowerCAmelCase__ , verification_mode=lowerCAmelCase__ , base_path=lowerCAmelCase__ , num_proc=self.num_proc , ) __SCREAMING_SNAKE_CASE : Optional[int] = self.builder.as_dataset( split=self.split , verification_mode=lowerCAmelCase__ , in_memory=self.keep_in_memory ) return dataset
9
1
# Imports import numpy as np class _lowercase : '''simple docstring''' def __init__( self :List[Any] , lowerCAmelCase__ :Optional[Any]=None , lowerCAmelCase__ :str=None , lowerCAmelCase__ :Tuple=None , lowerCAmelCase__ :Any=None , lowerCAmelCase__ :Any=None ) -> int: self.set_matricies(red=lowerCAmelCase__ , green=lowerCAmelCase__ , blue=lowerCAmelCase__ , red_edge=lowerCAmelCase__ , nir=lowerCAmelCase__ ) def __magic_name__( self :Tuple , lowerCAmelCase__ :Optional[Any]=None , lowerCAmelCase__ :List[Any]=None , lowerCAmelCase__ :Union[str, Any]=None , lowerCAmelCase__ :List[Any]=None , lowerCAmelCase__ :str=None ) -> Tuple: if red is not None: __SCREAMING_SNAKE_CASE : Tuple = red if green is not None: __SCREAMING_SNAKE_CASE : Dict = green if blue is not None: __SCREAMING_SNAKE_CASE : Tuple = blue if red_edge is not None: __SCREAMING_SNAKE_CASE : List[Any] = red_edge if nir is not None: __SCREAMING_SNAKE_CASE : List[Any] = nir return True def __magic_name__( self :Optional[Any] , lowerCAmelCase__ :List[Any]="" , lowerCAmelCase__ :List[Any]=None , lowerCAmelCase__ :Tuple=None , lowerCAmelCase__ :str=None , lowerCAmelCase__ :List[Any]=None , lowerCAmelCase__ :Tuple=None ) -> Union[str, Any]: self.set_matricies(red=lowerCAmelCase__ , green=lowerCAmelCase__ , blue=lowerCAmelCase__ , red_edge=lowerCAmelCase__ , nir=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[Any] = { '''ARVI2''': self.arvaa, '''CCCI''': self.ccci, '''CVI''': self.cvi, '''GLI''': self.gli, '''NDVI''': self.ndvi, '''BNDVI''': self.bndvi, '''redEdgeNDVI''': self.red_edge_ndvi, '''GNDVI''': self.gndvi, '''GBNDVI''': self.gbndvi, '''GRNDVI''': self.grndvi, '''RBNDVI''': self.rbndvi, '''PNDVI''': self.pndvi, '''ATSAVI''': self.atsavi, '''BWDRVI''': self.bwdrvi, '''CIgreen''': self.ci_green, '''CIrededge''': self.ci_rededge, '''CI''': self.ci, '''CTVI''': self.ctvi, '''GDVI''': self.gdvi, '''EVI''': self.evi, '''GEMI''': self.gemi, '''GOSAVI''': self.gosavi, '''GSAVI''': self.gsavi, '''Hue''': self.hue, '''IVI''': self.ivi, '''IPVI''': self.ipvi, '''I''': self.i, '''RVI''': self.rvi, '''MRVI''': self.mrvi, '''MSAVI''': self.m_savi, '''NormG''': self.norm_g, '''NormNIR''': self.norm_nir, '''NormR''': self.norm_r, '''NGRDI''': self.ngrdi, '''RI''': self.ri, '''S''': self.s, '''IF''': self._if, '''DVI''': self.dvi, '''TVI''': self.tvi, '''NDRE''': self.ndre, } try: return funcs[index]() except KeyError: print('''Index not in the list!''' ) return False def __magic_name__( self :Any ) -> Tuple: return -0.18 + (1.17 * ((self.nir - self.red) / (self.nir + self.red))) def __magic_name__( self :Optional[int] ) -> Union[str, Any]: return ((self.nir - self.redEdge) / (self.nir + self.redEdge)) / ( (self.nir - self.red) / (self.nir + self.red) ) def __magic_name__( self :Union[str, Any] ) -> int: return self.nir * (self.red / (self.green**2)) def __magic_name__( self :str ) -> Union[str, Any]: return (2 * self.green - self.red - self.blue) / ( 2 * self.green + self.red + self.blue ) def __magic_name__( self :int ) -> str: return (self.nir - self.red) / (self.nir + self.red) def __magic_name__( self :int ) -> Optional[Any]: return (self.nir - self.blue) / (self.nir + self.blue) def __magic_name__( self :int ) -> int: return (self.redEdge - self.red) / (self.redEdge + self.red) def __magic_name__( self :Any ) -> List[str]: return (self.nir - self.green) / (self.nir + self.green) def __magic_name__( self :Optional[Any] ) -> Tuple: return (self.nir - (self.green + self.blue)) / ( self.nir + (self.green + self.blue) ) def __magic_name__( self :Any ) -> Optional[int]: return (self.nir - (self.green + self.red)) / ( self.nir + (self.green + self.red) ) def __magic_name__( self :str ) -> Optional[int]: return (self.nir - (self.blue + self.red)) / (self.nir + (self.blue + self.red)) def __magic_name__( self :int ) -> Optional[int]: return (self.nir - (self.green + self.red + self.blue)) / ( self.nir + (self.green + self.red + self.blue) ) def __magic_name__( self :List[Any] , lowerCAmelCase__ :List[Any]=0.08 , lowerCAmelCase__ :Optional[int]=1.22 , lowerCAmelCase__ :Any=0.03 ) -> Optional[Any]: return a * ( (self.nir - a * self.red - b) / (a * self.nir + self.red - a * b + x * (1 + a**2)) ) def __magic_name__( self :Tuple ) -> Optional[int]: return (0.1 * self.nir - self.blue) / (0.1 * self.nir + self.blue) def __magic_name__( self :str ) -> Tuple: return (self.nir / self.green) - 1 def __magic_name__( self :Tuple ) -> Tuple: return (self.nir / self.redEdge) - 1 def __magic_name__( self :Optional[Any] ) -> Optional[int]: return (self.red - self.blue) / self.red def __magic_name__( self :Dict ) -> str: __SCREAMING_SNAKE_CASE : Optional[int] = self.ndvi() return ((ndvi + 0.5) / (abs(ndvi + 0.5 ))) * (abs(ndvi + 0.5 ) ** (1 / 2)) def __magic_name__( self :Dict ) -> Optional[Any]: return self.nir - self.green def __magic_name__( self :Any ) -> int: return 2.5 * ( (self.nir - self.red) / (self.nir + 6 * self.red - 7.5 * self.blue + 1) ) def __magic_name__( self :Tuple ) -> Dict: __SCREAMING_SNAKE_CASE : Dict = (2 * (self.nir**2 - self.red**2) + 1.5 * self.nir + 0.5 * self.red) / ( self.nir + self.red + 0.5 ) return n * (1 - 0.25 * n) - (self.red - 0.125) / (1 - self.red) def __magic_name__( self :Any , lowerCAmelCase__ :List[Any]=0.16 ) -> Tuple: return (self.nir - self.green) / (self.nir + self.green + y) def __magic_name__( self :Any , lowerCAmelCase__ :Optional[Any]=0.5 ) -> List[Any]: return ((self.nir - self.green) / (self.nir + self.green + n)) * (1 + n) def __magic_name__( self :Optional[int] ) -> Optional[Any]: return np.arctan( ((2 * self.red - self.green - self.blue) / 30.5) * (self.green - self.blue) ) def __magic_name__( self :int , lowerCAmelCase__ :Optional[Any]=None , lowerCAmelCase__ :Optional[int]=None ) -> Optional[Any]: return (self.nir - b) / (a * self.red) def __magic_name__( self :Any ) -> List[Any]: return (self.nir / ((self.nir + self.red) / 2)) * (self.ndvi() + 1) def __magic_name__( self :List[Any] ) -> List[str]: return (self.red + self.green + self.blue) / 30.5 def __magic_name__( self :List[Any] ) -> int: return self.nir / self.red def __magic_name__( self :Any ) -> str: return (self.rvi() - 1) / (self.rvi() + 1) def __magic_name__( self :List[Any] ) -> Union[str, Any]: return ( (2 * self.nir + 1) - ((2 * self.nir + 1) ** 2 - 8 * (self.nir - self.red)) ** (1 / 2) ) / 2 def __magic_name__( self :List[Any] ) -> Any: return self.green / (self.nir + self.red + self.green) def __magic_name__( self :int ) -> Union[str, Any]: return self.nir / (self.nir + self.red + self.green) def __magic_name__( self :Any ) -> List[Any]: return self.red / (self.nir + self.red + self.green) def __magic_name__( self :Any ) -> List[Any]: return (self.green - self.red) / (self.green + self.red) def __magic_name__( self :str ) -> str: return (self.red - self.green) / (self.red + self.green) def __magic_name__( self :Any ) -> Any: __SCREAMING_SNAKE_CASE : Dict = np.max([np.max(self.red ), np.max(self.green ), np.max(self.blue )] ) __SCREAMING_SNAKE_CASE : Any = np.min([np.min(self.red ), np.min(self.green ), np.min(self.blue )] ) return (max_value - min_value) / max_value def __magic_name__( self :Union[str, Any] ) -> List[str]: return (2 * self.red - self.green - self.blue) / (self.green - self.blue) def __magic_name__( self :int ) -> Dict: return self.nir / self.red def __magic_name__( self :List[str] ) -> int: return (self.ndvi() + 0.5) ** (1 / 2) def __magic_name__( self :List[str] ) -> Optional[int]: return (self.nir - self.redEdge) / (self.nir + self.redEdge)
9
import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipaConfig, BlipaForConditionalGeneration, BlipaProcessor, BlipaVisionConfig, BlipImageProcessor, OPTConfig, TaConfig, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : Dict = '''https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png''' __SCREAMING_SNAKE_CASE : List[str] = Image.open(requests.get(lowercase__ , stream=lowercase__ ).raw ).convert('''RGB''' ) return image def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : List[Any] = [] # fmt: off # vision encoder rename_keys.append(('''visual_encoder.cls_token''', '''vision_model.embeddings.class_embedding''') ) rename_keys.append(('''visual_encoder.pos_embed''', '''vision_model.embeddings.position_embedding''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.weight''', '''vision_model.embeddings.patch_embedding.weight''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.bias''', '''vision_model.embeddings.patch_embedding.bias''') ) rename_keys.append(('''ln_vision.weight''', '''vision_model.post_layernorm.weight''') ) rename_keys.append(('''ln_vision.bias''', '''vision_model.post_layernorm.bias''') ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((F'''visual_encoder.blocks.{i}.norm1.weight''', F'''vision_model.encoder.layers.{i}.layer_norm1.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.norm1.bias''', F'''vision_model.encoder.layers.{i}.layer_norm1.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.norm2.weight''', F'''vision_model.encoder.layers.{i}.layer_norm2.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.norm2.bias''', F'''vision_model.encoder.layers.{i}.layer_norm2.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.qkv.weight''', F'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.proj.weight''', F'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',) ) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.proj.bias''', F'''vision_model.encoder.layers.{i}.self_attn.projection.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc1.weight''', F'''vision_model.encoder.layers.{i}.mlp.fc1.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc1.bias''', F'''vision_model.encoder.layers.{i}.mlp.fc1.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc2.weight''', F'''vision_model.encoder.layers.{i}.mlp.fc2.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc2.bias''', F'''vision_model.encoder.layers.{i}.mlp.fc2.bias''') ) # QFormer rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.weight''', '''qformer.layernorm.weight''') ) rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.bias''', '''qformer.layernorm.bias''') ) # fmt: on return rename_keys def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : List[Any] = dct.pop(lowercase__ ) __SCREAMING_SNAKE_CASE : List[Any] = val def _UpperCamelCase ( lowercase__ , lowercase__ ): for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases __SCREAMING_SNAKE_CASE : Optional[int] = state_dict.pop(F'''visual_encoder.blocks.{i}.attn.q_bias''' ) __SCREAMING_SNAKE_CASE : int = state_dict.pop(F'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict __SCREAMING_SNAKE_CASE : Optional[int] = torch.cat((q_bias, torch.zeros_like(lowercase__ , requires_grad=lowercase__ ), v_bias) ) __SCREAMING_SNAKE_CASE : Optional[Any] = qkv_bias def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Any = 364 if '''coco''' in model_name else 224 __SCREAMING_SNAKE_CASE : List[str] = BlipaVisionConfig(image_size=lowercase__ ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: __SCREAMING_SNAKE_CASE : Union[str, Any] = OPTConfig.from_pretrained('''facebook/opt-2.7b''' , eos_token_id=lowercase__ ).to_dict() elif "opt-6.7b" in model_name: __SCREAMING_SNAKE_CASE : List[Any] = OPTConfig.from_pretrained('''facebook/opt-6.7b''' , eos_token_id=lowercase__ ).to_dict() elif "t5-xl" in model_name: __SCREAMING_SNAKE_CASE : Optional[Any] = TaConfig.from_pretrained('''google/flan-t5-xl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: __SCREAMING_SNAKE_CASE : Union[str, Any] = TaConfig.from_pretrained('''google/flan-t5-xxl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() __SCREAMING_SNAKE_CASE : Optional[int] = BlipaConfig(vision_config=lowercase__ , text_config=lowercase__ ) return config, image_size @torch.no_grad() def _UpperCamelCase ( lowercase__ , lowercase__=None , lowercase__=False ): __SCREAMING_SNAKE_CASE : Any = ( AutoTokenizer.from_pretrained('''facebook/opt-2.7b''' ) if '''opt''' in model_name else AutoTokenizer.from_pretrained('''google/flan-t5-xl''' ) ) __SCREAMING_SNAKE_CASE : str = tokenizer('''\n''' , add_special_tokens=lowercase__ ).input_ids[0] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Union[str, Any] = get_blipa_config(lowercase__ , eos_token_id=lowercase__ ) __SCREAMING_SNAKE_CASE : int = BlipaForConditionalGeneration(lowercase__ ).eval() __SCREAMING_SNAKE_CASE : int = { '''blip2-opt-2.7b''': ('''blip2_opt''', '''pretrain_opt2.7b'''), '''blip2-opt-6.7b''': ('''blip2_opt''', '''pretrain_opt6.7b'''), '''blip2-opt-2.7b-coco''': ('''blip2_opt''', '''caption_coco_opt2.7b'''), '''blip2-opt-6.7b-coco''': ('''blip2_opt''', '''caption_coco_opt6.7b'''), '''blip2-flan-t5-xl''': ('''blip2_t5''', '''pretrain_flant5xl'''), '''blip2-flan-t5-xl-coco''': ('''blip2_t5''', '''caption_coco_flant5xl'''), '''blip2-flan-t5-xxl''': ('''blip2_t5''', '''pretrain_flant5xxl'''), } __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Any = model_name_to_original[model_name] # load original model print('''Loading original model...''' ) __SCREAMING_SNAKE_CASE : List[str] = '''cuda''' if torch.cuda.is_available() else '''cpu''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Dict = load_model_and_preprocess( name=lowercase__ , model_type=lowercase__ , is_eval=lowercase__ , device=lowercase__ ) original_model.eval() print('''Done!''' ) # update state dict keys __SCREAMING_SNAKE_CASE : List[str] = original_model.state_dict() __SCREAMING_SNAKE_CASE : Optional[int] = create_rename_keys(lowercase__ ) for src, dest in rename_keys: rename_key(lowercase__ , lowercase__ , lowercase__ ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): __SCREAMING_SNAKE_CASE : Tuple = state_dict.pop(lowercase__ ) if key.startswith('''Qformer.bert''' ): __SCREAMING_SNAKE_CASE : List[str] = key.replace('''Qformer.bert''' , '''qformer''' ) if "attention.self" in key: __SCREAMING_SNAKE_CASE : Union[str, Any] = key.replace('''self''' , '''attention''' ) if "opt_proj" in key: __SCREAMING_SNAKE_CASE : Dict = key.replace('''opt_proj''' , '''language_projection''' ) if "t5_proj" in key: __SCREAMING_SNAKE_CASE : Tuple = key.replace('''t5_proj''' , '''language_projection''' ) if key.startswith('''opt''' ): __SCREAMING_SNAKE_CASE : List[str] = key.replace('''opt''' , '''language''' ) if key.startswith('''t5''' ): __SCREAMING_SNAKE_CASE : Tuple = key.replace('''t5''' , '''language''' ) __SCREAMING_SNAKE_CASE : Tuple = val # read in qv biases read_in_q_v_bias(lowercase__ , lowercase__ ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : List[str] = hf_model.load_state_dict(lowercase__ , strict=lowercase__ ) assert len(lowercase__ ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] __SCREAMING_SNAKE_CASE : List[str] = load_demo_image() __SCREAMING_SNAKE_CASE : Any = vis_processors['''eval'''](lowercase__ ).unsqueeze(0 ).to(lowercase__ ) __SCREAMING_SNAKE_CASE : str = tokenizer(['''\n'''] , return_tensors='''pt''' ).input_ids.to(lowercase__ ) # create processor __SCREAMING_SNAKE_CASE : List[Any] = BlipImageProcessor( size={'''height''': image_size, '''width''': image_size} , image_mean=lowercase__ , image_std=lowercase__ ) __SCREAMING_SNAKE_CASE : int = BlipaProcessor(image_processor=lowercase__ , tokenizer=lowercase__ ) __SCREAMING_SNAKE_CASE : Any = processor(images=lowercase__ , return_tensors='''pt''' ).pixel_values.to(lowercase__ ) # make sure processor creates exact same pixel values assert torch.allclose(lowercase__ , lowercase__ ) original_model.to(lowercase__ ) hf_model.to(lowercase__ ) with torch.no_grad(): if "opt" in model_name: __SCREAMING_SNAKE_CASE : Dict = original_model({'''image''': original_pixel_values, '''text_input''': ['''''']} ).logits __SCREAMING_SNAKE_CASE : Dict = hf_model(lowercase__ , lowercase__ ).logits else: __SCREAMING_SNAKE_CASE : int = original_model( {'''image''': original_pixel_values, '''text_input''': ['''\n'''], '''text_output''': ['''\n''']} ).logits __SCREAMING_SNAKE_CASE : List[Any] = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -100 ) __SCREAMING_SNAKE_CASE : Optional[int] = hf_model(lowercase__ , lowercase__ , labels=lowercase__ ).logits assert original_logits.shape == logits.shape print('''First values of original logits:''' , original_logits[0, :3, :3] ) print('''First values of HF logits:''' , logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": __SCREAMING_SNAKE_CASE : Dict = torch.tensor( [[-41.5850, -4.4440, -8.9922], [-47.4322, -5.9143, -1.7340]] , device=lowercase__ ) assert torch.allclose(logits[0, :3, :3] , lowercase__ , atol=1e-4 ) elif model_name == "blip2-flan-t5-xl-coco": __SCREAMING_SNAKE_CASE : Any = torch.tensor( [[-57.0109, -9.8967, -12.6280], [-68.6578, -12.7191, -10.5065]] , device=lowercase__ ) else: # cast to same type __SCREAMING_SNAKE_CASE : Optional[Any] = logits.dtype assert torch.allclose(original_logits.to(lowercase__ ) , lowercase__ , atol=1e-2 ) print('''Looks ok!''' ) print('''Generating a caption...''' ) __SCREAMING_SNAKE_CASE : Any = '''''' __SCREAMING_SNAKE_CASE : Optional[int] = tokenizer(lowercase__ , return_tensors='''pt''' ).input_ids.to(lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = original_model.generate({'''image''': original_pixel_values} ) __SCREAMING_SNAKE_CASE : Union[str, Any] = hf_model.generate( lowercase__ , lowercase__ , do_sample=lowercase__ , num_beams=5 , max_length=30 , min_length=1 , top_p=0.9 , repetition_penalty=1.0 , length_penalty=1.0 , temperature=1 , ) print('''Original generation:''' , lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = input_ids.shape[1] __SCREAMING_SNAKE_CASE : Any = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = [text.strip() for text in output_text] print('''HF generation:''' , lowercase__ ) if pytorch_dump_folder_path is not None: processor.save_pretrained(lowercase__ ) hf_model.save_pretrained(lowercase__ ) if push_to_hub: processor.push_to_hub(F'''nielsr/{model_name}''' ) hf_model.push_to_hub(F'''nielsr/{model_name}''' ) if __name__ == "__main__": __lowerCAmelCase : List[str] =argparse.ArgumentParser() __lowerCAmelCase : Tuple =[ 'blip2-opt-2.7b', 'blip2-opt-6.7b', 'blip2-opt-2.7b-coco', 'blip2-opt-6.7b-coco', 'blip2-flan-t5-xl', 'blip2-flan-t5-xl-coco', 'blip2-flan-t5-xxl', ] parser.add_argument( '--model_name', default='blip2-opt-2.7b', choices=choices, type=str, help='Path to hf config.json of model to convert', ) parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument( '--push_to_hub', action='store_true', help='Whether to push the model and processor to the hub after converting', ) __lowerCAmelCase : List[Any] =parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
9
1
import argparse import os import numpy as np import tensorflow as tf import torch from transformers import BertModel def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Union[str, Any] = ('''dense.weight''', '''attention.self.query''', '''attention.self.key''', '''attention.self.value''') __SCREAMING_SNAKE_CASE : int = ( ('''layer.''', '''layer_'''), ('''word_embeddings.weight''', '''word_embeddings'''), ('''position_embeddings.weight''', '''position_embeddings'''), ('''token_type_embeddings.weight''', '''token_type_embeddings'''), ('''.''', '''/'''), ('''LayerNorm/weight''', '''LayerNorm/gamma'''), ('''LayerNorm/bias''', '''LayerNorm/beta'''), ('''weight''', '''kernel'''), ) if not os.path.isdir(lowercase__ ): os.makedirs(lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = model.state_dict() def to_tf_var_name(lowercase__ ): for patt, repl in iter(lowercase__ ): __SCREAMING_SNAKE_CASE : Any = name.replace(lowercase__ , lowercase__ ) return F'''bert/{name}''' def create_tf_var(lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Union[str, Any] = tf.dtypes.as_dtype(tensor.dtype ) __SCREAMING_SNAKE_CASE : int = tf.get_variable(dtype=lowercase__ , shape=tensor.shape , name=lowercase__ , initializer=tf.zeros_initializer() ) session.run(tf.variables_initializer([tf_var] ) ) session.run(lowercase__ ) return tf_var tf.reset_default_graph() with tf.Session() as session: for var_name in state_dict: __SCREAMING_SNAKE_CASE : Union[str, Any] = to_tf_var_name(lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = state_dict[var_name].numpy() if any(x in var_name for x in tensors_to_transpose ): __SCREAMING_SNAKE_CASE : Dict = torch_tensor.T __SCREAMING_SNAKE_CASE : Optional[Any] = create_tf_var(tensor=lowercase__ , name=lowercase__ , session=lowercase__ ) tf.keras.backend.set_value(lowercase__ , lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = session.run(lowercase__ ) print(F'''Successfully created {tf_name}: {np.allclose(lowercase__ , lowercase__ )}''' ) __SCREAMING_SNAKE_CASE : Dict = tf.train.Saver(tf.trainable_variables() ) saver.save(lowercase__ , os.path.join(lowercase__ , model_name.replace('''-''' , '''_''' ) + '''.ckpt''' ) ) def _UpperCamelCase ( lowercase__=None ): __SCREAMING_SNAKE_CASE : Dict = argparse.ArgumentParser() parser.add_argument('''--model_name''' , type=lowercase__ , required=lowercase__ , help='''model name e.g. bert-base-uncased''' ) parser.add_argument( '''--cache_dir''' , type=lowercase__ , default=lowercase__ , required=lowercase__ , help='''Directory containing pytorch model''' ) parser.add_argument('''--pytorch_model_path''' , type=lowercase__ , required=lowercase__ , help='''/path/to/<pytorch-model-name>.bin''' ) parser.add_argument('''--tf_cache_dir''' , type=lowercase__ , required=lowercase__ , help='''Directory in which to save tensorflow model''' ) __SCREAMING_SNAKE_CASE : List[Any] = parser.parse_args(lowercase__ ) __SCREAMING_SNAKE_CASE : int = BertModel.from_pretrained( pretrained_model_name_or_path=args.model_name , state_dict=torch.load(args.pytorch_model_path ) , cache_dir=args.cache_dir , ) convert_pytorch_checkpoint_to_tf(model=lowercase__ , ckpt_dir=args.tf_cache_dir , model_name=args.model_name ) if __name__ == "__main__": main()
9
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __lowerCAmelCase : Optional[int] =logging.get_logger(__name__) __lowerCAmelCase : Optional[Any] ={'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} __lowerCAmelCase : List[str] ={ 'tokenizer_file': { 'EleutherAI/gpt-neox-20b': 'https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/tokenizer.json', }, } __lowerCAmelCase : Optional[int] ={ 'gpt-neox-20b': 2_0_4_8, } class _lowercase ( A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : str = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE__ : Dict = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE__ : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE__ : Optional[Any] = ['''input_ids''', '''attention_mask'''] def __init__( self :int , lowerCAmelCase__ :Any=None , lowerCAmelCase__ :Optional[Any]=None , lowerCAmelCase__ :List[Any]=None , lowerCAmelCase__ :str="<|endoftext|>" , lowerCAmelCase__ :str="<|endoftext|>" , lowerCAmelCase__ :Dict="<|endoftext|>" , lowerCAmelCase__ :Union[str, Any]=False , **lowerCAmelCase__ :List[str] , ) -> Any: super().__init__( lowerCAmelCase__ , lowerCAmelCase__ , tokenizer_file=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , bos_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , **lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE : List[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , lowerCAmelCase__ ) != add_prefix_space: __SCREAMING_SNAKE_CASE : List[str] = getattr(lowerCAmelCase__ , pre_tok_state.pop('''type''' ) ) __SCREAMING_SNAKE_CASE : str = add_prefix_space __SCREAMING_SNAKE_CASE : Any = pre_tok_class(**lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = add_prefix_space def __magic_name__( self :Union[str, Any] , lowerCAmelCase__ :str , lowerCAmelCase__ :Optional[str] = None ) -> Tuple[str]: __SCREAMING_SNAKE_CASE : List[str] = self._tokenizer.model.save(lowerCAmelCase__ , name=lowerCAmelCase__ ) return tuple(lowerCAmelCase__ ) def __magic_name__( self :Optional[Any] , lowerCAmelCase__ :"Conversation" ) -> List[int]: __SCREAMING_SNAKE_CASE : Optional[Any] = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) + [self.eos_token_id] ) if len(lowerCAmelCase__ ) > self.model_max_length: __SCREAMING_SNAKE_CASE : List[str] = input_ids[-self.model_max_length :] return input_ids
9
1
import os from typing import List, Optional, Union from ...tokenization_utils import PreTrainedTokenizer from ...tokenization_utils_base import AddedToken from ...utils import logging __lowerCAmelCase : Any =logging.get_logger(__name__) __lowerCAmelCase : List[str] ={'vocab_file': 'vocab.txt'} __lowerCAmelCase : Dict ={ 'vocab_file': { 'facebook/esm2_t6_8M_UR50D': 'https://huggingface.co/facebook/esm2_t6_8M_UR50D/resolve/main/vocab.txt', 'facebook/esm2_t12_35M_UR50D': 'https://huggingface.co/facebook/esm2_t12_35M_UR50D/resolve/main/vocab.txt', }, } __lowerCAmelCase : int ={ 'facebook/esm2_t6_8M_UR50D': 1_0_2_4, 'facebook/esm2_t12_35M_UR50D': 1_0_2_4, } def _UpperCamelCase ( lowercase__ ): with open(lowercase__ , '''r''' ) as f: __SCREAMING_SNAKE_CASE : Tuple = f.read().splitlines() return [l.strip() for l in lines] class _lowercase ( A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : int = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE__ : str = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE__ : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE__ : Dict = ['''input_ids''', '''attention_mask'''] def __init__( self :Dict , lowerCAmelCase__ :int , lowerCAmelCase__ :int="<unk>" , lowerCAmelCase__ :List[Any]="<cls>" , lowerCAmelCase__ :Optional[Any]="<pad>" , lowerCAmelCase__ :List[str]="<mask>" , lowerCAmelCase__ :List[Any]="<eos>" , **lowerCAmelCase__ :Optional[int] , ) -> Optional[int]: super().__init__(**lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[str] = load_vocab_file(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Any = dict(enumerate(self.all_tokens ) ) __SCREAMING_SNAKE_CASE : Union[str, Any] = {tok: ind for ind, tok in enumerate(self.all_tokens )} __SCREAMING_SNAKE_CASE : Tuple = unk_token __SCREAMING_SNAKE_CASE : Tuple = cls_token __SCREAMING_SNAKE_CASE : Dict = pad_token __SCREAMING_SNAKE_CASE : Dict = mask_token __SCREAMING_SNAKE_CASE : Union[str, Any] = eos_token __SCREAMING_SNAKE_CASE : Dict = self.all_tokens self._create_trie(self.unique_no_split_tokens ) def __magic_name__( self :str , lowerCAmelCase__ :int ) -> str: return self._id_to_token.get(lowerCAmelCase__ , self.unk_token ) def __magic_name__( self :List[str] , lowerCAmelCase__ :str ) -> int: return self._token_to_id.get(lowerCAmelCase__ , self._token_to_id.get(self.unk_token ) ) def __magic_name__( self :Tuple , lowerCAmelCase__ :List[str] , **lowerCAmelCase__ :Tuple ) -> Tuple: return text.split() def __magic_name__( self :Union[str, Any] , lowerCAmelCase__ :Optional[int]=False ) -> Optional[int]: return len(self._id_to_token ) def __magic_name__( self :Tuple ) -> Any: return {token: i for i, token in enumerate(self.all_tokens )} def __magic_name__( self :str , lowerCAmelCase__ :str ) -> int: return self._token_to_id.get(lowerCAmelCase__ , self._token_to_id.get(self.unk_token ) ) def __magic_name__( self :Optional[int] , lowerCAmelCase__ :int ) -> str: return self._id_to_token.get(lowerCAmelCase__ , self.unk_token ) def __magic_name__( self :Tuple , lowerCAmelCase__ :List[int] , lowerCAmelCase__ :Optional[List[int]] = None ) -> List[int]: __SCREAMING_SNAKE_CASE : str = [self.cls_token_id] __SCREAMING_SNAKE_CASE : Optional[Any] = [self.eos_token_id] # No sep token in ESM vocabulary if token_ids_a is None: if self.eos_token_id is None: return cls + token_ids_a else: return cls + token_ids_a + sep elif self.eos_token_id is None: raise ValueError('''Cannot tokenize multiple sequences when EOS token is not set!''' ) return cls + token_ids_a + sep + token_ids_a + sep # Multiple inputs always have an EOS token def __magic_name__( self :Union[str, Any] , lowerCAmelCase__ :List , lowerCAmelCase__ :Optional[List] = None , lowerCAmelCase__ :bool = False ) -> List[int]: if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if token in self.all_special_ids else 0 for token in token_ids_a] __SCREAMING_SNAKE_CASE : Any = [1] + ([0] * len(lowerCAmelCase__ )) + [1] if token_ids_a is not None: mask += [0] * len(lowerCAmelCase__ ) + [1] return mask def __magic_name__( self :Optional[Any] , lowerCAmelCase__ :List[str] , lowerCAmelCase__ :Union[str, Any] ) -> Optional[int]: __SCREAMING_SNAKE_CASE : str = os.path.join(lowerCAmelCase__ , (filename_prefix + '''-''' if filename_prefix else '''''') + '''vocab.txt''' ) with open(lowerCAmelCase__ , '''w''' ) as f: f.write('''\n'''.join(self.all_tokens ) ) return (vocab_file,) @property def __magic_name__( self :int ) -> int: return self.get_vocab_size(with_added_tokens=lowerCAmelCase__ ) def __magic_name__( self :Optional[int] , lowerCAmelCase__ :Union[List[str], List[AddedToken]] , lowerCAmelCase__ :bool = False ) -> int: return super()._add_tokens(lowerCAmelCase__ , special_tokens=lowerCAmelCase__ )
9
import math_equivalence # From: git+https://github.com/hendrycks/math.git import datasets __lowerCAmelCase : Optional[Any] ='\\n@article{hendrycksmath2021,\n title={Measuring Mathematical Problem Solving With the MATH Dataset},\n author={Dan Hendrycks\n and Collin Burns\n and Saurav Kadavath\n and Akul Arora\n and Steven Basart\n and Eric Tang\n and Dawn Song\n and Jacob Steinhardt},\n journal={arXiv preprint arXiv:2103.03874},\n year={2021}\n}\n' __lowerCAmelCase : Any ='\\nThis metric is used to assess performance on the Mathematics Aptitude Test of Heuristics (MATH) dataset.\nIt first canonicalizes the inputs (e.g., converting "1/2" to "\\frac{1}{2}") and then computes accuracy.\n' __lowerCAmelCase : Optional[Any] =r'\nCalculates accuracy after canonicalizing inputs.\n\nArgs:\n predictions: list of predictions to score. Each prediction\n is a string that contains natural language and LaTex.\n references: list of reference for each prediction. Each\n reference is a string that contains natural language\n and LaTex.\nReturns:\n accuracy: accuracy after canonicalizing inputs\n (e.g., converting "1/2" to "\\frac{1}{2}")\n\nExamples:\n >>> metric = datasets.load_metric("competition_math")\n >>> results = metric.compute(references=["\\frac{1}{2}"], predictions=["1/2"])\n >>> print(results)\n {\'accuracy\': 1.0}\n' @datasets.utils.file_utils.add_end_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _lowercase ( datasets.Metric ): '''simple docstring''' def __magic_name__( self :Optional[Any] ) -> List[str]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' ), '''references''': datasets.Value('''string''' ), } ) , homepage='''https://github.com/hendrycks/math''' , codebase_urls=['''https://github.com/hendrycks/math'''] , ) def __magic_name__( self :Any , lowerCAmelCase__ :Optional[int] , lowerCAmelCase__ :Any ) -> Optional[int]: __SCREAMING_SNAKE_CASE : Tuple = 0.0 for i, j in zip(lowerCAmelCase__ , lowerCAmelCase__ ): n_correct += 1.0 if math_equivalence.is_equiv(lowerCAmelCase__ , lowerCAmelCase__ ) else 0.0 __SCREAMING_SNAKE_CASE : str = n_correct / len(lowerCAmelCase__ ) return { "accuracy": accuracy, }
9
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __lowerCAmelCase : Optional[int] ={ 'configuration_roberta': ['ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RobertaConfig', 'RobertaOnnxConfig'], 'tokenization_roberta': ['RobertaTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : int =['RobertaTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : List[Any] =[ 'ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'RobertaForCausalLM', 'RobertaForMaskedLM', 'RobertaForMultipleChoice', 'RobertaForQuestionAnswering', 'RobertaForSequenceClassification', 'RobertaForTokenClassification', 'RobertaModel', 'RobertaPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Optional[int] =[ 'TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFRobertaForCausalLM', 'TFRobertaForMaskedLM', 'TFRobertaForMultipleChoice', 'TFRobertaForQuestionAnswering', 'TFRobertaForSequenceClassification', 'TFRobertaForTokenClassification', 'TFRobertaMainLayer', 'TFRobertaModel', 'TFRobertaPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : List[str] =[ 'FlaxRobertaForCausalLM', 'FlaxRobertaForMaskedLM', 'FlaxRobertaForMultipleChoice', 'FlaxRobertaForQuestionAnswering', 'FlaxRobertaForSequenceClassification', 'FlaxRobertaForTokenClassification', 'FlaxRobertaModel', 'FlaxRobertaPreTrainedModel', ] if TYPE_CHECKING: from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig, RobertaOnnxConfig from .tokenization_roberta import RobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roberta_fast import RobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roberta import ( ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, RobertaForCausalLM, RobertaForMaskedLM, RobertaForMultipleChoice, RobertaForQuestionAnswering, RobertaForSequenceClassification, RobertaForTokenClassification, RobertaModel, RobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roberta import ( TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForMultipleChoice, TFRobertaForQuestionAnswering, TFRobertaForSequenceClassification, TFRobertaForTokenClassification, TFRobertaMainLayer, TFRobertaModel, TFRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, FlaxRobertaPreTrainedModel, ) else: import sys __lowerCAmelCase : Optional[int] =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
9
import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST, OpenAIGPTConfig, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification, OpenAIGPTLMHeadModel, OpenAIGPTModel, ) class _lowercase : '''simple docstring''' def __init__( self :Optional[int] , lowerCAmelCase__ :Optional[Any] , lowerCAmelCase__ :int=13 , lowerCAmelCase__ :List[str]=7 , lowerCAmelCase__ :Dict=True , lowerCAmelCase__ :List[str]=True , lowerCAmelCase__ :str=True , lowerCAmelCase__ :List[Any]=99 , lowerCAmelCase__ :List[str]=32 , lowerCAmelCase__ :Any=5 , lowerCAmelCase__ :List[str]=4 , lowerCAmelCase__ :int=37 , lowerCAmelCase__ :Optional[int]="gelu" , lowerCAmelCase__ :str=0.1 , lowerCAmelCase__ :str=0.1 , lowerCAmelCase__ :Optional[Any]=512 , lowerCAmelCase__ :Union[str, Any]=16 , lowerCAmelCase__ :Dict=2 , lowerCAmelCase__ :Tuple=0.02 , lowerCAmelCase__ :List[Any]=3 , lowerCAmelCase__ :Tuple=4 , lowerCAmelCase__ :int=None , ) -> int: __SCREAMING_SNAKE_CASE : Dict = parent __SCREAMING_SNAKE_CASE : Any = batch_size __SCREAMING_SNAKE_CASE : Union[str, Any] = seq_length __SCREAMING_SNAKE_CASE : Optional[Any] = is_training __SCREAMING_SNAKE_CASE : int = use_token_type_ids __SCREAMING_SNAKE_CASE : Any = use_labels __SCREAMING_SNAKE_CASE : Any = vocab_size __SCREAMING_SNAKE_CASE : List[Any] = hidden_size __SCREAMING_SNAKE_CASE : int = num_hidden_layers __SCREAMING_SNAKE_CASE : List[Any] = num_attention_heads __SCREAMING_SNAKE_CASE : str = intermediate_size __SCREAMING_SNAKE_CASE : Tuple = hidden_act __SCREAMING_SNAKE_CASE : Dict = hidden_dropout_prob __SCREAMING_SNAKE_CASE : int = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : Optional[Any] = max_position_embeddings __SCREAMING_SNAKE_CASE : List[Any] = type_vocab_size __SCREAMING_SNAKE_CASE : List[str] = type_sequence_label_size __SCREAMING_SNAKE_CASE : List[str] = initializer_range __SCREAMING_SNAKE_CASE : Tuple = num_labels __SCREAMING_SNAKE_CASE : Union[str, Any] = num_choices __SCREAMING_SNAKE_CASE : Union[str, Any] = scope __SCREAMING_SNAKE_CASE : Union[str, Any] = self.vocab_size - 1 def __magic_name__( self :Optional[Any] ) -> List[Any]: __SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __SCREAMING_SNAKE_CASE : Optional[Any] = None if self.use_token_type_ids: __SCREAMING_SNAKE_CASE : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __SCREAMING_SNAKE_CASE : Dict = None __SCREAMING_SNAKE_CASE : Optional[int] = None __SCREAMING_SNAKE_CASE : Union[str, Any] = None if self.use_labels: __SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __SCREAMING_SNAKE_CASE : Optional[Any] = ids_tensor([self.batch_size] , self.num_choices ) __SCREAMING_SNAKE_CASE : Optional[int] = OpenAIGPTConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) __SCREAMING_SNAKE_CASE : Any = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, head_mask, token_type_ids, sequence_labels, token_labels, choice_labels, ) def __magic_name__( self :Tuple , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :List[str] , lowerCAmelCase__ :Dict , lowerCAmelCase__ :Any , *lowerCAmelCase__ :Union[str, Any] ) -> Any: __SCREAMING_SNAKE_CASE : Any = OpenAIGPTModel(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() __SCREAMING_SNAKE_CASE : Dict = model(lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , head_mask=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Tuple = model(lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : str = model(lowerCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __magic_name__( self :Optional[Any] , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :Any , lowerCAmelCase__ :Dict , *lowerCAmelCase__ :List[Any] ) -> Dict: __SCREAMING_SNAKE_CASE : Optional[Any] = OpenAIGPTLMHeadModel(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() __SCREAMING_SNAKE_CASE : Tuple = model(lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __magic_name__( self :Tuple , lowerCAmelCase__ :Dict , lowerCAmelCase__ :str , lowerCAmelCase__ :Optional[Any] , lowerCAmelCase__ :List[str] , *lowerCAmelCase__ :Optional[Any] ) -> Any: __SCREAMING_SNAKE_CASE : Any = OpenAIGPTDoubleHeadsModel(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() __SCREAMING_SNAKE_CASE : Any = model(lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __magic_name__( self :Dict , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :str , *lowerCAmelCase__ :Optional[int] ) -> Dict: __SCREAMING_SNAKE_CASE : Optional[Any] = self.num_labels __SCREAMING_SNAKE_CASE : List[Any] = OpenAIGPTForSequenceClassification(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() __SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __SCREAMING_SNAKE_CASE : Optional[Any] = model(lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __magic_name__( self :Optional[Any] ) -> str: __SCREAMING_SNAKE_CASE : str = self.prepare_config_and_inputs() ( ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ) : List[str] = config_and_inputs __SCREAMING_SNAKE_CASE : List[str] = { '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask, } return config, inputs_dict @require_torch class _lowercase ( A__ , A__ , A__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : int = ( (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification) if is_torch_available() else () ) SCREAMING_SNAKE_CASE__ : str = ( (OpenAIGPTLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly SCREAMING_SNAKE_CASE__ : str = ( { '''feature-extraction''': OpenAIGPTModel, '''text-classification''': OpenAIGPTForSequenceClassification, '''text-generation''': OpenAIGPTLMHeadModel, '''zero-shot''': OpenAIGPTForSequenceClassification, } if is_torch_available() else {} ) def __magic_name__( self :Optional[int] , lowerCAmelCase__ :str , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :Union[str, Any] ) -> Tuple: if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a # tiny config could not be created. return True return False def __magic_name__( self :List[str] , lowerCAmelCase__ :Dict , lowerCAmelCase__ :int , lowerCAmelCase__ :int=False ) -> Dict: __SCREAMING_SNAKE_CASE : Tuple = super()._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ , return_labels=lowerCAmelCase__ ) if return_labels: if model_class.__name__ == "OpenAIGPTDoubleHeadsModel": __SCREAMING_SNAKE_CASE : Any = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length) , dtype=torch.long , device=lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE : Tuple = inputs_dict['''labels'''] __SCREAMING_SNAKE_CASE : Dict = inputs_dict['''labels'''] __SCREAMING_SNAKE_CASE : List[Any] = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices) , dtype=torch.long , device=lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE : Optional[int] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCAmelCase__ ) return inputs_dict def __magic_name__( self :Optional[int] ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : int = OpenAIGPTModelTester(self ) __SCREAMING_SNAKE_CASE : Optional[Any] = ConfigTester(self , config_class=lowerCAmelCase__ , n_embd=37 ) def __magic_name__( self :Any ) -> Optional[Any]: self.config_tester.run_common_tests() def __magic_name__( self :List[str] ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__ ) def __magic_name__( self :int ) -> int: __SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase__ ) def __magic_name__( self :List[str] ) -> Dict: __SCREAMING_SNAKE_CASE : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase__ ) def __magic_name__( self :List[str] ) -> str: __SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase__ ) @slow def __magic_name__( self :Any ) -> List[Any]: for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __SCREAMING_SNAKE_CASE : Dict = OpenAIGPTModel.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) @require_torch class _lowercase ( unittest.TestCase ): '''simple docstring''' @slow def __magic_name__( self :Union[str, Any] ) -> Optional[int]: __SCREAMING_SNAKE_CASE : List[str] = OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt''' ) model.to(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = torch.tensor([[481, 4_735, 544]] , dtype=torch.long , device=lowerCAmelCase__ ) # the president is __SCREAMING_SNAKE_CASE : Dict = [ 481, 4_735, 544, 246, 963, 870, 762, 239, 244, 40_477, 244, 249, 719, 881, 487, 544, 240, 244, 603, 481, ] # the president is a very good man. " \n " i\'m sure he is, " said the __SCREAMING_SNAKE_CASE : Dict = model.generate(lowerCAmelCase__ , do_sample=lowerCAmelCase__ ) self.assertListEqual(output_ids[0].tolist() , lowerCAmelCase__ )
9
1
import warnings from ...utils import logging from .image_processing_deformable_detr import DeformableDetrImageProcessor __lowerCAmelCase : Tuple =logging.get_logger(__name__) class _lowercase ( A__ ): '''simple docstring''' def __init__( self :List[str] , *lowerCAmelCase__ :List[Any] , **lowerCAmelCase__ :str ) -> None: warnings.warn( '''The class DeformableDetrFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use DeformableDetrImageProcessor instead.''' , lowerCAmelCase__ , ) super().__init__(*lowerCAmelCase__ , **lowerCAmelCase__ )
9
import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _UpperCamelCase ( *lowercase__ , lowercase__ = None , lowercase__=True , lowercase__=2 ): from .. import __version__ __SCREAMING_SNAKE_CASE : Optional[Any] = take_from __SCREAMING_SNAKE_CASE : List[str] = () if not isinstance(args[0] , lowercase__ ): __SCREAMING_SNAKE_CASE : List[Any] = (args,) for attribute, version_name, message in args: if version.parse(version.parse(lowercase__ ).base_version ) >= version.parse(lowercase__ ): raise ValueError( F'''The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\'''' F''' version {__version__} is >= {version_name}''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = None if isinstance(lowercase__ , lowercase__ ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(lowercase__ ),) __SCREAMING_SNAKE_CASE : List[Any] = F'''The `{attribute}` argument is deprecated and will be removed in version {version_name}.''' elif hasattr(lowercase__ , lowercase__ ): values += (getattr(lowercase__ , lowercase__ ),) __SCREAMING_SNAKE_CASE : List[str] = F'''The `{attribute}` attribute is deprecated and will be removed in version {version_name}.''' elif deprecated_kwargs is None: __SCREAMING_SNAKE_CASE : str = F'''`{attribute}` is deprecated and will be removed in version {version_name}.''' if warning is not None: __SCREAMING_SNAKE_CASE : Any = warning + ''' ''' if standard_warn else '''''' warnings.warn(warning + message , lowercase__ , stacklevel=lowercase__ ) if isinstance(lowercase__ , lowercase__ ) and len(lowercase__ ) > 0: __SCREAMING_SNAKE_CASE : Union[str, Any] = inspect.getouterframes(inspect.currentframe() )[1] __SCREAMING_SNAKE_CASE : Dict = call_frame.filename __SCREAMING_SNAKE_CASE : Optional[Any] = call_frame.lineno __SCREAMING_SNAKE_CASE : int = call_frame.function __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : int = next(iter(deprecated_kwargs.items() ) ) raise TypeError(F'''{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`''' ) if len(lowercase__ ) == 0: return elif len(lowercase__ ) == 1: return values[0] return values
9
1
from math import ceil from typing import List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor from ...utils import TensorType, logging __lowerCAmelCase : Dict =logging.get_logger(__name__) class _lowercase ( A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Union[str, Any] = ['''audio_values''', '''audio_mask'''] def __init__( self :Optional[Any] , lowerCAmelCase__ :str=2_048 , lowerCAmelCase__ :str=1 , lowerCAmelCase__ :List[Any]=[16, 16] , lowerCAmelCase__ :List[str]=128 , lowerCAmelCase__ :Dict=44_100 , lowerCAmelCase__ :Tuple=86 , lowerCAmelCase__ :List[str]=2_048 , lowerCAmelCase__ :Union[str, Any]=0.0 , **lowerCAmelCase__ :int , ) -> str: super().__init__( feature_size=lowerCAmelCase__ , sampling_rate=lowerCAmelCase__ , padding_value=lowerCAmelCase__ , **lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE : Union[str, Any] = spectrogram_length __SCREAMING_SNAKE_CASE : Dict = num_channels __SCREAMING_SNAKE_CASE : List[Any] = patch_size __SCREAMING_SNAKE_CASE : Optional[int] = feature_size // self.patch_size[1] __SCREAMING_SNAKE_CASE : Optional[Any] = n_fft __SCREAMING_SNAKE_CASE : int = sampling_rate // hop_length_to_sampling_rate __SCREAMING_SNAKE_CASE : Optional[int] = sampling_rate __SCREAMING_SNAKE_CASE : Tuple = padding_value __SCREAMING_SNAKE_CASE : Dict = mel_filter_bank( num_frequency_bins=1 + n_fft // 2 , num_mel_filters=lowerCAmelCase__ , min_frequency=0.0 , max_frequency=2_2050.0 , sampling_rate=lowerCAmelCase__ , norm='''slaney''' , mel_scale='''slaney''' , ).T def __magic_name__( self :Union[str, Any] , lowerCAmelCase__ :np.array ) -> np.ndarray: __SCREAMING_SNAKE_CASE : Union[str, Any] = spectrogram( lowerCAmelCase__ , window_function(self.n_fft , '''hann''' ) , frame_length=self.n_fft , hop_length=self.hop_length , power=2.0 , mel_filters=self.mel_filters.T , log_mel='''dB''' , db_range=80.0 , ) __SCREAMING_SNAKE_CASE : Tuple = log_spec[:, :-1] __SCREAMING_SNAKE_CASE : Tuple = log_spec - 20.0 __SCREAMING_SNAKE_CASE : List[str] = np.clip(log_spec / 40.0 , -2.0 , 0.0 ) + 1.0 return log_spec def __call__( self :Dict , lowerCAmelCase__ :Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , lowerCAmelCase__ :Optional[Union[str, TensorType]] = None , lowerCAmelCase__ :Optional[bool] = True , lowerCAmelCase__ :Optional[int] = None , lowerCAmelCase__ :bool = False , lowerCAmelCase__ :bool = False , **lowerCAmelCase__ :Union[str, Any] , ) -> BatchFeature: if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( '''This feature extractor is set to support sampling rate''' f''' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled''' f''' with {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) __SCREAMING_SNAKE_CASE : Any = isinstance(lowerCAmelCase__ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f'''Only mono-channel audio is supported for input to {self}''' ) __SCREAMING_SNAKE_CASE : str = is_batched_numpy or ( isinstance(lowerCAmelCase__ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: __SCREAMING_SNAKE_CASE : Union[str, Any] = [np.asarray([speech] , dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(lowerCAmelCase__ , np.ndarray ): __SCREAMING_SNAKE_CASE : Union[str, Any] = np.asarray(lowerCAmelCase__ , dtype=np.floataa ) elif isinstance(lowerCAmelCase__ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): __SCREAMING_SNAKE_CASE : Optional[Any] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: __SCREAMING_SNAKE_CASE : str = [np.asarray([raw_speech] ).T] # Convert audio signals to log mel spectrograms, truncate by time axis __SCREAMING_SNAKE_CASE : Optional[Any] = [ self._np_extract_fbank_features(waveform.squeeze() ).T[: self.spectrogram_length] for waveform in raw_speech ] if isinstance(audio_features[0] , lowerCAmelCase__ ): __SCREAMING_SNAKE_CASE : Optional[Any] = [np.asarray(lowerCAmelCase__ , dtype=np.floataa ) for feature in audio_features] # Create audio attention mask __SCREAMING_SNAKE_CASE : Tuple = max( [ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len for feature in audio_features] ) # The maximum number of audio patches in a batch if return_attention_mask: __SCREAMING_SNAKE_CASE : Dict = [ (ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [1] + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [0] for feature in audio_features ] __SCREAMING_SNAKE_CASE : Optional[int] = np.array(lowerCAmelCase__ ).astype(np.floataa ) # convert into correct format for padding __SCREAMING_SNAKE_CASE : Optional[int] = max_patch_len // self.freq_len * self.patch_size[0] # The maximum audio size in a batch __SCREAMING_SNAKE_CASE : List[str] = np.ones([len(lowerCAmelCase__ ), 1, max_time_len, self.feature_size] ).astype(np.floataa ) __SCREAMING_SNAKE_CASE : Union[str, Any] = padded_audio_features * self.padding_value for i in range(len(lowerCAmelCase__ ) ): __SCREAMING_SNAKE_CASE : Dict = audio_features[i] __SCREAMING_SNAKE_CASE : str = feature # return as BatchFeature if return_attention_mask: __SCREAMING_SNAKE_CASE : Dict = {'''audio_values''': padded_audio_features, '''audio_mask''': audio_mask} else: __SCREAMING_SNAKE_CASE : List[str] = {'''audio_values''': padded_audio_features} __SCREAMING_SNAKE_CASE : Any = BatchFeature(data=lowerCAmelCase__ , tensor_type=lowerCAmelCase__ ) return encoded_inputs
9
from __future__ import annotations import bisect def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ = 0 , lowercase__ = -1 ): if hi < 0: __SCREAMING_SNAKE_CASE : Union[str, Any] = len(lowercase__ ) while lo < hi: __SCREAMING_SNAKE_CASE : Any = lo + (hi - lo) // 2 if sorted_collection[mid] < item: __SCREAMING_SNAKE_CASE : Union[str, Any] = mid + 1 else: __SCREAMING_SNAKE_CASE : Optional[Any] = mid return lo def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ = 0 , lowercase__ = -1 ): if hi < 0: __SCREAMING_SNAKE_CASE : List[Any] = len(lowercase__ ) while lo < hi: __SCREAMING_SNAKE_CASE : Optional[int] = lo + (hi - lo) // 2 if sorted_collection[mid] <= item: __SCREAMING_SNAKE_CASE : Any = mid + 1 else: __SCREAMING_SNAKE_CASE : Optional[int] = mid return lo def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ = 0 , lowercase__ = -1 ): sorted_collection.insert(bisect_left(lowercase__ , lowercase__ , lowercase__ , lowercase__ ) , lowercase__ ) def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ = 0 , lowercase__ = -1 ): sorted_collection.insert(bisect_right(lowercase__ , lowercase__ , lowercase__ , lowercase__ ) , lowercase__ ) def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Any = 0 __SCREAMING_SNAKE_CASE : List[Any] = len(lowercase__ ) - 1 while left <= right: __SCREAMING_SNAKE_CASE : str = left + (right - left) // 2 __SCREAMING_SNAKE_CASE : List[str] = sorted_collection[midpoint] if current_item == item: return midpoint elif item < current_item: __SCREAMING_SNAKE_CASE : int = midpoint - 1 else: __SCREAMING_SNAKE_CASE : Dict = midpoint + 1 return None def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Union[str, Any] = bisect.bisect_left(lowercase__ , lowercase__ ) if index != len(lowercase__ ) and sorted_collection[index] == item: return index return None def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , lowercase__ ): if right < left: return None __SCREAMING_SNAKE_CASE : int = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: return binary_search_by_recursion(lowercase__ , lowercase__ , lowercase__ , midpoint - 1 ) else: return binary_search_by_recursion(lowercase__ , lowercase__ , midpoint + 1 , lowercase__ ) if __name__ == "__main__": __lowerCAmelCase : Dict =input('Enter numbers separated by comma:\n').strip() __lowerCAmelCase : str =sorted(int(item) for item in user_input.split(',')) __lowerCAmelCase : Tuple =int(input('Enter a single number to be found in the list:\n')) __lowerCAmelCase : Tuple =binary_search(collection, target) if result is None: print(f"""{target} was not found in {collection}.""") else: print(f"""{target} was found at position {result} in {collection}.""")
9
1
import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class _lowercase ( A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : str = ['''image_processor''', '''tokenizer'''] SCREAMING_SNAKE_CASE__ : Dict = '''LayoutLMv2ImageProcessor''' SCREAMING_SNAKE_CASE__ : Dict = ('''LayoutXLMTokenizer''', '''LayoutXLMTokenizerFast''') def __init__( self :Union[str, Any] , lowerCAmelCase__ :int=None , lowerCAmelCase__ :List[str]=None , **lowerCAmelCase__ :Optional[int] ) -> Union[str, Any]: if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE : Union[str, Any] = kwargs.pop('''feature_extractor''' ) __SCREAMING_SNAKE_CASE : Optional[int] = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(lowerCAmelCase__ , lowerCAmelCase__ ) def __call__( self :Optional[int] , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , lowerCAmelCase__ :Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None , lowerCAmelCase__ :Union[List[List[int]], List[List[List[int]]]] = None , lowerCAmelCase__ :Optional[Union[List[int], List[List[int]]]] = None , lowerCAmelCase__ :bool = True , lowerCAmelCase__ :Union[bool, str, PaddingStrategy] = False , lowerCAmelCase__ :Union[bool, str, TruncationStrategy] = None , lowerCAmelCase__ :Optional[int] = None , lowerCAmelCase__ :int = 0 , lowerCAmelCase__ :Optional[int] = None , lowerCAmelCase__ :Optional[bool] = None , lowerCAmelCase__ :Optional[bool] = None , lowerCAmelCase__ :bool = False , lowerCAmelCase__ :bool = False , lowerCAmelCase__ :bool = False , lowerCAmelCase__ :bool = False , lowerCAmelCase__ :bool = True , lowerCAmelCase__ :Optional[Union[str, TensorType]] = None , **lowerCAmelCase__ :List[Any] , ) -> BatchEncoding: # verify input if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( '''You cannot provide bounding boxes ''' '''if you initialized the image processor with apply_ocr set to True.''' ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.''' ) if return_overflowing_tokens is True and return_offsets_mapping is False: raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.''' ) # first, apply the image processor __SCREAMING_SNAKE_CASE : str = self.image_processor(images=lowerCAmelCase__ , return_tensors=lowerCAmelCase__ ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __SCREAMING_SNAKE_CASE : Any = [text] # add batch dimension (as the image processor always adds a batch dimension) __SCREAMING_SNAKE_CASE : List[str] = features['''words'''] __SCREAMING_SNAKE_CASE : str = self.tokenizer( text=text if text is not None else features['''words'''] , text_pair=text_pair if text_pair is not None else None , boxes=boxes if boxes is not None else features['''boxes'''] , word_labels=lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , padding=lowerCAmelCase__ , truncation=lowerCAmelCase__ , max_length=lowerCAmelCase__ , stride=lowerCAmelCase__ , pad_to_multiple_of=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ , return_attention_mask=lowerCAmelCase__ , return_overflowing_tokens=lowerCAmelCase__ , return_special_tokens_mask=lowerCAmelCase__ , return_offsets_mapping=lowerCAmelCase__ , return_length=lowerCAmelCase__ , verbose=lowerCAmelCase__ , return_tensors=lowerCAmelCase__ , **lowerCAmelCase__ , ) # add pixel values __SCREAMING_SNAKE_CASE : Optional[Any] = features.pop('''pixel_values''' ) if return_overflowing_tokens is True: __SCREAMING_SNAKE_CASE : List[str] = self.get_overflowing_images(lowerCAmelCase__ , encoded_inputs['''overflow_to_sample_mapping'''] ) __SCREAMING_SNAKE_CASE : List[str] = images return encoded_inputs def __magic_name__( self :int , lowerCAmelCase__ :List[str] , lowerCAmelCase__ :Optional[int] ) -> Optional[int]: # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image __SCREAMING_SNAKE_CASE : Dict = [] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(lowerCAmelCase__ ) != len(lowerCAmelCase__ ): raise ValueError( '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got''' f''' {len(lowerCAmelCase__ )} and {len(lowerCAmelCase__ )}''' ) return images_with_overflow def __magic_name__( self :Any , *lowerCAmelCase__ :Optional[int] , **lowerCAmelCase__ :Optional[Any] ) -> Union[str, Any]: return self.tokenizer.batch_decode(*lowerCAmelCase__ , **lowerCAmelCase__ ) def __magic_name__( self :Union[str, Any] , *lowerCAmelCase__ :Tuple , **lowerCAmelCase__ :Union[str, Any] ) -> str: return self.tokenizer.decode(*lowerCAmelCase__ , **lowerCAmelCase__ ) @property def __magic_name__( self :Union[str, Any] ) -> List[Any]: return ["input_ids", "bbox", "attention_mask", "image"] @property def __magic_name__( self :str ) -> Dict: warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , lowerCAmelCase__ , ) return self.image_processor_class @property def __magic_name__( self :Tuple ) -> List[Any]: warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , lowerCAmelCase__ , ) return self.image_processor
9
import os import shutil import tempfile import unittest import numpy as np from transformers import AutoTokenizer, BarkProcessor from transformers.testing_utils import require_torch, slow @require_torch class _lowercase ( unittest.TestCase ): '''simple docstring''' def __magic_name__( self :Union[str, Any] ) -> Tuple: __SCREAMING_SNAKE_CASE : str = '''ylacombe/bark-small''' __SCREAMING_SNAKE_CASE : Optional[int] = tempfile.mkdtemp() __SCREAMING_SNAKE_CASE : str = '''en_speaker_1''' __SCREAMING_SNAKE_CASE : Any = '''This is a test string''' __SCREAMING_SNAKE_CASE : int = '''speaker_embeddings_path.json''' __SCREAMING_SNAKE_CASE : int = '''speaker_embeddings''' def __magic_name__( self :List[str] , **lowerCAmelCase__ :Union[str, Any] ) -> Any: return AutoTokenizer.from_pretrained(self.checkpoint , **lowerCAmelCase__ ) def __magic_name__( self :List[str] ) -> int: shutil.rmtree(self.tmpdirname ) def __magic_name__( self :Dict ) -> str: __SCREAMING_SNAKE_CASE : Dict = self.get_tokenizer() __SCREAMING_SNAKE_CASE : Tuple = BarkProcessor(tokenizer=lowerCAmelCase__ ) processor.save_pretrained(self.tmpdirname ) __SCREAMING_SNAKE_CASE : Optional[Any] = BarkProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) @slow def __magic_name__( self :Tuple ) -> List[Any]: __SCREAMING_SNAKE_CASE : Dict = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) processor.save_pretrained( self.tmpdirname , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , speaker_embeddings_directory=self.speaker_embeddings_directory , ) __SCREAMING_SNAKE_CASE : Dict = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) __SCREAMING_SNAKE_CASE : Union[str, Any] = BarkProcessor.from_pretrained( self.tmpdirname , self.speaker_embeddings_dict_path , bos_token='''(BOS)''' , eos_token='''(EOS)''' , ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) def __magic_name__( self :List[str] ) -> Tuple: __SCREAMING_SNAKE_CASE : List[Any] = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) __SCREAMING_SNAKE_CASE : str = 35 __SCREAMING_SNAKE_CASE : str = 2 __SCREAMING_SNAKE_CASE : List[Any] = 8 __SCREAMING_SNAKE_CASE : Union[str, Any] = { '''semantic_prompt''': np.ones(lowerCAmelCase__ ), '''coarse_prompt''': np.ones((nb_codebooks_coarse, seq_len) ), '''fine_prompt''': np.ones((nb_codebooks_total, seq_len) ), } # test providing already loaded voice_preset __SCREAMING_SNAKE_CASE : Union[str, Any] = processor(text=self.input_string , voice_preset=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[str] = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(lowerCAmelCase__ , np.array([] ) ).tolist() ) # test loading voice preset from npz file __SCREAMING_SNAKE_CASE : str = os.path.join(self.tmpdirname , '''file.npz''' ) np.savez(lowerCAmelCase__ , **lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[Any] = processor(text=self.input_string , voice_preset=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Union[str, Any] = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(lowerCAmelCase__ , np.array([] ) ).tolist() ) # test loading voice preset from the hub __SCREAMING_SNAKE_CASE : Union[str, Any] = processor(text=self.input_string , voice_preset=self.voice_preset ) def __magic_name__( self :Tuple ) -> Optional[Any]: __SCREAMING_SNAKE_CASE : Tuple = self.get_tokenizer() __SCREAMING_SNAKE_CASE : Any = BarkProcessor(tokenizer=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[Any] = processor(text=self.input_string ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer( self.input_string , padding='''max_length''' , max_length=256 , add_special_tokens=lowerCAmelCase__ , return_attention_mask=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ , ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key].squeeze().tolist() )
9
1
import importlib import torch import yaml from omegaconf import OmegaConf from taming.models.vqgan import VQModel def _UpperCamelCase ( lowercase__ , lowercase__=False ): __SCREAMING_SNAKE_CASE : Tuple = OmegaConf.load(lowercase__ ) if display: print(yaml.dump(OmegaConf.to_container(lowercase__ ) ) ) return config def _UpperCamelCase ( lowercase__ , lowercase__=None , lowercase__=None ): if conf_path is None: __SCREAMING_SNAKE_CASE : int = '''./model_checkpoints/vqgan_only.yaml''' __SCREAMING_SNAKE_CASE : List[str] = load_config(lowercase__ , display=lowercase__ ) __SCREAMING_SNAKE_CASE : Union[str, Any] = VQModel(**config.model.params ) if ckpt_path is None: __SCREAMING_SNAKE_CASE : Optional[int] = '''./model_checkpoints/vqgan_only.pt''' __SCREAMING_SNAKE_CASE : str = torch.load(lowercase__ , map_location=lowercase__ ) if ".ckpt" in ckpt_path: __SCREAMING_SNAKE_CASE : int = sd['''state_dict'''] model.load_state_dict(lowercase__ , strict=lowercase__ ) model.to(lowercase__ ) del sd return model def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Any = model.encode(lowercase__ ) print(F'''VQGAN --- {model.__class__.__name__}: latent shape: {z.shape[2:]}''' ) __SCREAMING_SNAKE_CASE : Dict = model.decode(lowercase__ ) return xrec def _UpperCamelCase ( lowercase__ , lowercase__=False ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : int = string.rsplit('''.''' , 1 ) if reload: __SCREAMING_SNAKE_CASE : int = importlib.import_module(lowercase__ ) importlib.reload(lowercase__ ) return getattr(importlib.import_module(lowercase__ , package=lowercase__ ) , cls ) def _UpperCamelCase ( lowercase__ ): if "target" not in config: raise KeyError('''Expected key `target` to instantiate.''' ) return get_obj_from_str(config['''target'''] )(**config.get('''params''' , {} ) ) def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__=True , lowercase__=True ): __SCREAMING_SNAKE_CASE : Any = instantiate_from_config(lowercase__ ) if sd is not None: model.load_state_dict(lowercase__ ) if gpu: model.cuda() if eval_mode: model.eval() return {"model": model} def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , lowercase__ ): # load the specified checkpoint if ckpt: __SCREAMING_SNAKE_CASE : Optional[Any] = torch.load(lowercase__ , map_location='''cpu''' ) __SCREAMING_SNAKE_CASE : List[Any] = pl_sd['''global_step'''] print(F'''loaded model from global step {global_step}.''' ) else: __SCREAMING_SNAKE_CASE : Tuple = {'''state_dict''': None} __SCREAMING_SNAKE_CASE : Optional[int] = None __SCREAMING_SNAKE_CASE : str = load_model_from_config(config.model , pl_sd['''state_dict'''] , gpu=lowercase__ , eval_mode=lowercase__ )['''model'''] return model, global_step
9
from importlib import import_module from .logging import get_logger __lowerCAmelCase : str =get_logger(__name__) class _lowercase : '''simple docstring''' def __init__( self :List[Any] , lowerCAmelCase__ :List[Any] , lowerCAmelCase__ :str=None ) -> int: __SCREAMING_SNAKE_CASE : List[str] = attrs or [] if module is not None: for key in module.__dict__: if key in attrs or not key.startswith('''__''' ): setattr(self , lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) __SCREAMING_SNAKE_CASE : Optional[Any] = module._original_module if isinstance(lowerCAmelCase__ , _PatchedModuleObj ) else module class _lowercase : '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = [] def __init__( self :Tuple , lowerCAmelCase__ :Optional[Any] , lowerCAmelCase__ :str , lowerCAmelCase__ :Any , lowerCAmelCase__ :Dict=None ) -> List[Any]: __SCREAMING_SNAKE_CASE : Optional[int] = obj __SCREAMING_SNAKE_CASE : str = target __SCREAMING_SNAKE_CASE : Dict = new __SCREAMING_SNAKE_CASE : Union[str, Any] = target.split('''.''' )[0] __SCREAMING_SNAKE_CASE : List[str] = {} __SCREAMING_SNAKE_CASE : Tuple = attrs or [] def __enter__( self :int ) -> Dict: *__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Optional[Any] = self.target.split('''.''' ) # Patch modules: # it's used to patch attributes of submodules like "os.path.join"; # in this case we need to patch "os" and "os.path" for i in range(len(lowerCAmelCase__ ) ): try: __SCREAMING_SNAKE_CASE : Any = import_module('''.'''.join(submodules[: i + 1] ) ) except ModuleNotFoundError: continue # We iterate over all the globals in self.obj in case we find "os" or "os.path" for attr in self.obj.__dir__(): __SCREAMING_SNAKE_CASE : Union[str, Any] = getattr(self.obj , lowerCAmelCase__ ) # We don't check for the name of the global, but rather if its value *is* "os" or "os.path". # This allows to patch renamed modules like "from os import path as ospath". if obj_attr is submodule or ( (isinstance(lowerCAmelCase__ , _PatchedModuleObj ) and obj_attr._original_module is submodule) ): __SCREAMING_SNAKE_CASE : int = obj_attr # patch at top level setattr(self.obj , lowerCAmelCase__ , _PatchedModuleObj(lowerCAmelCase__ , attrs=self.attrs ) ) __SCREAMING_SNAKE_CASE : List[str] = getattr(self.obj , lowerCAmelCase__ ) # construct lower levels patches for key in submodules[i + 1 :]: setattr(lowerCAmelCase__ , lowerCAmelCase__ , _PatchedModuleObj(getattr(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) , attrs=self.attrs ) ) __SCREAMING_SNAKE_CASE : Tuple = getattr(lowerCAmelCase__ , lowerCAmelCase__ ) # finally set the target attribute setattr(lowerCAmelCase__ , lowerCAmelCase__ , self.new ) # Patch attribute itself: # it's used for builtins like "open", # and also to patch "os.path.join" we may also need to patch "join" # itself if it was imported as "from os.path import join". if submodules: # if it's an attribute of a submodule like "os.path.join" try: __SCREAMING_SNAKE_CASE : Union[str, Any] = getattr(import_module('''.'''.join(lowerCAmelCase__ ) ) , lowerCAmelCase__ ) except (AttributeError, ModuleNotFoundError): return # We iterate over all the globals in self.obj in case we find "os.path.join" for attr in self.obj.__dir__(): # We don't check for the name of the global, but rather if its value *is* "os.path.join". # This allows to patch renamed attributes like "from os.path import join as pjoin". if getattr(self.obj , lowerCAmelCase__ ) is attr_value: __SCREAMING_SNAKE_CASE : Any = getattr(self.obj , lowerCAmelCase__ ) setattr(self.obj , lowerCAmelCase__ , self.new ) elif target_attr in globals()["__builtins__"]: # if it'a s builtin like "open" __SCREAMING_SNAKE_CASE : Union[str, Any] = globals()['''__builtins__'''][target_attr] setattr(self.obj , lowerCAmelCase__ , self.new ) else: raise RuntimeError(f'''Tried to patch attribute {target_attr} instead of a submodule.''' ) def __exit__( self :str , *lowerCAmelCase__ :Union[str, Any] ) -> Optional[int]: for attr in list(self.original ): setattr(self.obj , lowerCAmelCase__ , self.original.pop(lowerCAmelCase__ ) ) def __magic_name__( self :List[Any] ) -> List[Any]: self.__enter__() self._active_patches.append(self ) def __magic_name__( self :Optional[int] ) -> int: try: self._active_patches.remove(self ) except ValueError: # If the patch hasn't been started this will fail return None return self.__exit__()
9
1