code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
"""simple docstring""" import math def lowercase ( lowerCAmelCase__ : int ) -> bool: 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(lowerCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowercase ( lowerCAmelCase__ : float = 0.1 ) -> int: __a = 3 __a = 3 while primes / (2 * j - 1) >= ratio: for i in range(j * j + j + 1 , (j + 2) * (j + 2) , j + 1 ): primes += is_prime(lowerCAmelCase__ ) j += 2 return j if __name__ == "__main__": import doctest doctest.testmod()
695
"""simple docstring""" import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler lowercase_ = 1_6 lowercase_ = 3_2 def lowercase ( lowerCAmelCase__ : Accelerator , lowerCAmelCase__ : int = 16 , lowerCAmelCase__ : str = "bert-base-cased" ) -> Optional[int]: __a = AutoTokenizer.from_pretrained(lowerCAmelCase__ ) __a = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(lowerCAmelCase__ : Optional[Any] ): # max_length=None => use the model max length (it's actually the default) __a = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=lowerCAmelCase__ , max_length=lowerCAmelCase__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset __a = datasets.map( lowerCAmelCase__ , batched=lowerCAmelCase__ , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , load_from_cache_file=lowerCAmelCase__ ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library __a = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(lowerCAmelCase__ : int ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(lowerCAmelCase__ , padding='''max_length''' , max_length=128 , return_tensors='''pt''' ) return tokenizer.pad(lowerCAmelCase__ , padding='''longest''' , return_tensors='''pt''' ) # Instantiate dataloaders. __a = DataLoader( tokenized_datasets['''train'''] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ ) __a = DataLoader( tokenized_datasets['''validation'''] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ ) return train_dataloader, eval_dataloader def lowercase ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any] ) -> Optional[int]: # Initialize accelerator __a = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __a = config['''lr'''] __a = int(config['''num_epochs'''] ) __a = int(config['''seed'''] ) __a = int(config['''batch_size'''] ) __a = args.model_name_or_path set_seed(lowerCAmelCase__ ) __a , __a = get_dataloaders(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __a = AutoModelForSequenceClassification.from_pretrained(lowerCAmelCase__ , return_dict=lowerCAmelCase__ ) # Instantiate optimizer __a = ( AdamW if accelerator.state.deepspeed_plugin is None or '''optimizer''' not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) __a = optimizer_cls(params=model.parameters() , lr=lowerCAmelCase__ ) if accelerator.state.deepspeed_plugin is not None: __a = accelerator.state.deepspeed_plugin.deepspeed_config[ '''gradient_accumulation_steps''' ] else: __a = 1 __a = (len(lowerCAmelCase__ ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): __a = get_linear_schedule_with_warmup( optimizer=lowerCAmelCase__ , num_warmup_steps=0 , num_training_steps=lowerCAmelCase__ , ) else: __a = DummyScheduler(lowerCAmelCase__ , total_num_steps=lowerCAmelCase__ , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. __a , __a , __a , __a , __a = accelerator.prepare( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # We need to keep track of how many total steps we have iterated over __a = 0 # We also need to keep track of the stating epoch so files are named properly __a = 0 # Now we train the model __a = evaluate.load('''glue''' , '''mrpc''' ) __a = 0 __a = {} for epoch in range(lowerCAmelCase__ , lowerCAmelCase__ ): model.train() for step, batch in enumerate(lowerCAmelCase__ ): __a = model(**lowerCAmelCase__ ) __a = outputs.loss __a = loss / gradient_accumulation_steps accelerator.backward(lowerCAmelCase__ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 model.eval() __a = 0 for step, batch in enumerate(lowerCAmelCase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): __a = model(**lowerCAmelCase__ ) __a = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times __a , __a = accelerator.gather( (predictions, batch['''labels''']) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(lowerCAmelCase__ ) - 1: __a = predictions[: len(eval_dataloader.dataset ) - samples_seen] __a = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=lowerCAmelCase__ , references=lowerCAmelCase__ , ) __a = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f'''epoch {epoch}:''' , lowerCAmelCase__ ) __a = eval_metric['''accuracy'''] if best_performance < eval_metric["accuracy"]: __a = eval_metric['''accuracy'''] if args.performance_lower_bound is not None: assert ( args.performance_lower_bound <= best_performance ), f'''Best performance metric {best_performance} is lower than the lower bound {args.performance_lower_bound}''' accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , '''all_results.json''' ) , '''w''' ) as f: json.dump(lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( ) -> List[str]: __a = argparse.ArgumentParser(description='''Simple example of training script tracking peak GPU memory usage.''' ) parser.add_argument( '''--model_name_or_path''' , type=lowerCAmelCase__ , default='''bert-base-cased''' , help='''Path to pretrained model or model identifier from huggingface.co/models.''' , required=lowerCAmelCase__ , ) parser.add_argument( '''--output_dir''' , type=lowerCAmelCase__ , default='''.''' , help='''Optional save directory where all checkpoint folders will be stored. Default is the current working directory.''' , ) parser.add_argument( '''--performance_lower_bound''' , type=lowerCAmelCase__ , default=lowerCAmelCase__ , help='''Optional lower bound for the performance metric. If set, the training will throw error when the performance metric drops below this value.''' , ) parser.add_argument( '''--num_epochs''' , type=lowerCAmelCase__ , default=3 , help='''Number of train epochs.''' , ) __a = parser.parse_args() __a = {'''lr''': 2e-5, '''num_epochs''': args.num_epochs, '''seed''': 42, '''batch_size''': 16} training_function(lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": main()
695
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> float: return price * (1 + tax_rate) if __name__ == "__main__": print(F'''{price_plus_tax(1_0_0, 0.25) = }''') print(F'''{price_plus_tax(125.50, 0.05) = }''')
695
"""simple docstring""" from typing import Any def lowercase ( lowerCAmelCase__ : list , lowerCAmelCase__ : list , lowerCAmelCase__ : dict , lowerCAmelCase__ : dict , lowerCAmelCase__ : dict , ) -> list: _validation( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ) # Creates data structures and fill initial step __a = {} __a = {} for state in states_space: __a = observations_space[0] __a = ( initial_probabilities[state] * emission_probabilities[state][observation] ) __a = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(lowerCAmelCase__ ) ): __a = observations_space[o] __a = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function __a = '''''' __a = -1 for k_state in states_space: __a = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: __a = probability __a = k_state # Update probabilities and pointers dicts __a = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) __a = arg_max # The final observation __a = observations_space[len(lowerCAmelCase__ ) - 1] # argmax for given final observation __a = '''''' __a = -1 for k_state in states_space: __a = probabilities[(k_state, final_observation)] if probability > max_probability: __a = probability __a = k_state __a = arg_max # Process pointers backwards __a = last_state __a = [] for o in range(len(lowerCAmelCase__ ) - 1 , -1 , -1 ): result.append(lowerCAmelCase__ ) __a = pointers[previous, observations_space[o]] result.reverse() return result def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: _validate_not_empty( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ) _validate_lists(lowerCAmelCase__ , lowerCAmelCase__ ) _validate_dicts( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError('''There\'s an empty parameter''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any ) -> None: _validate_list(lowerCAmelCase__ , '''observations_space''' ) _validate_list(lowerCAmelCase__ , '''states_space''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str ) -> None: if not isinstance(_object , lowerCAmelCase__ ): __a = f'''{var_name} must be a list''' raise ValueError(lowerCAmelCase__ ) else: for x in _object: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = f'''{var_name} must be a list of strings''' raise ValueError(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: _validate_dict(lowerCAmelCase__ , '''initial_probabilities''' , lowerCAmelCase__ ) _validate_nested_dict(lowerCAmelCase__ , '''transition_probabilities''' ) _validate_nested_dict(lowerCAmelCase__ , '''emission_probabilities''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str ) -> None: _validate_dict(_object , lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object.values(): _validate_dict(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : type , lowerCAmelCase__ : bool = False ) -> None: if not isinstance(_object , lowerCAmelCase__ ): __a = f'''{var_name} must be a dict''' raise ValueError(lowerCAmelCase__ ) if not all(isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object ): __a = f'''{var_name} all keys must be strings''' raise ValueError(lowerCAmelCase__ ) if not all(isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object.values() ): __a = '''nested dictionary ''' if nested else '''''' __a = f'''{var_name} {nested_text}all values must be {value_type.__name__}''' raise ValueError(lowerCAmelCase__ ) if __name__ == "__main__": from doctest import testmod testmod()
695
1
"""simple docstring""" import os import re import shutil import sys import tempfile import unittest import black lowercase_ = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If DDPMSchedulerOutput is changed in scheduling_ddpm.py, this code needs to be manually updated. lowercase_ = " \"\"\"\n Output class for the scheduler's step function output.\n\n Args:\n prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the\n denoising loop.\n pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n The predicted denoised sample (x_{0}) based on the model output from the current timestep.\n `pred_original_sample` can be used to preview progress or for guidance.\n \"\"\"\n\n prev_sample: torch.FloatTensor\n pred_original_sample: Optional[torch.FloatTensor] = None\n" class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = tempfile.mkdtemp() os.makedirs(os.path.join(self.diffusers_dir , '''schedulers/''' ) ) __a = self.diffusers_dir shutil.copy( os.path.join(_a , '''src/diffusers/schedulers/scheduling_ddpm.py''' ) , os.path.join(self.diffusers_dir , '''schedulers/scheduling_ddpm.py''' ) , ) def __UpperCAmelCase ( self ): __a = '''src/diffusers''' shutil.rmtree(self.diffusers_dir ) def __UpperCAmelCase ( self , _a , _a , _a , _a=None ): __a = comment + f'''\nclass {class_name}(nn.Module):\n''' + class_code if overwrite_result is not None: __a = comment + f'''\nclass {class_name}(nn.Module):\n''' + overwrite_result __a = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) __a = black.format_str(_a , mode=_a ) __a = os.path.join(self.diffusers_dir , '''new_code.py''' ) with open(_a , '''w''' , newline='''\n''' ) as f: f.write(_a ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(_a ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=_a ) with open(_a , '''r''' ) as f: self.assertTrue(f.read() , _a ) def __UpperCAmelCase ( self ): __a = check_copies.find_code_in_diffusers('''schedulers.scheduling_ddpm.DDPMSchedulerOutput''' ) self.assertEqual(_a , _a ) def __UpperCAmelCase ( self ): # Base copy consistency self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput''' , '''DDPMSchedulerOutput''' , REFERENCE_CODE + '''\n''' , ) # With no empty line at the end self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput''' , '''DDPMSchedulerOutput''' , _a , ) # Copy consistency with rename self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test''' , '''TestSchedulerOutput''' , re.sub('''DDPM''' , '''Test''' , _a ) , ) # Copy consistency with a really long name __a = '''TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason''' self.check_copy_consistency( f'''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}''' , f'''{long_class_name}SchedulerOutput''' , re.sub('''Bert''' , _a , _a ) , ) # Copy consistency with overwrite self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test''' , '''TestSchedulerOutput''' , _a , overwrite_result=re.sub('''DDPM''' , '''Test''' , _a ) , )
695
"""simple docstring""" import math def lowercase ( lowerCAmelCase__ : int ) -> bool: 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(lowerCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowercase ( lowerCAmelCase__ : float = 0.1 ) -> int: __a = 3 __a = 3 while primes / (2 * j - 1) >= ratio: for i in range(j * j + j + 1 , (j + 2) * (j + 2) , j + 1 ): primes += is_prime(lowerCAmelCase__ ) j += 2 return j if __name__ == "__main__": import doctest doctest.testmod()
695
1
"""simple docstring""" import os import string import sys lowercase_ = 1 << 8 lowercase_ = { "tab": ord("\t"), "newline": ord("\r"), "esc": 2_7, "up": 6_5 + ARROW_KEY_FLAG, "down": 6_6 + ARROW_KEY_FLAG, "right": 6_7 + ARROW_KEY_FLAG, "left": 6_8 + ARROW_KEY_FLAG, "mod_int": 9_1, "undefined": sys.maxsize, "interrupt": 3, "insert": 5_0, "delete": 5_1, "pg_up": 5_3, "pg_down": 5_4, } lowercase_ = KEYMAP["up"] lowercase_ = KEYMAP["left"] if sys.platform == "win32": lowercase_ = [] lowercase_ = { b"\xe0H": KEYMAP["up"] - ARROW_KEY_FLAG, b"\x00H": KEYMAP["up"] - ARROW_KEY_FLAG, b"\xe0P": KEYMAP["down"] - ARROW_KEY_FLAG, b"\x00P": KEYMAP["down"] - ARROW_KEY_FLAG, b"\xe0M": KEYMAP["right"] - ARROW_KEY_FLAG, b"\x00M": KEYMAP["right"] - ARROW_KEY_FLAG, b"\xe0K": KEYMAP["left"] - ARROW_KEY_FLAG, b"\x00K": KEYMAP["left"] - ARROW_KEY_FLAG, } for i in range(1_0): lowercase_ = ord(str(i)) def lowercase ( ) -> Dict: if os.name == "nt": import msvcrt __a = '''mbcs''' # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(lowerCAmelCase__ ) == 0: # Read the keystroke __a = msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): __a = ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: __a = chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP['''mod_int'''] ) ) WIN_CH_BUFFER.append(lowerCAmelCase__ ) if ord(lowerCAmelCase__ ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(126 ) ) __a = chr(KEYMAP['''esc'''] ) except KeyError: __a = cha[1] else: __a = ch.decode(lowerCAmelCase__ ) else: __a = WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty __a = sys.stdin.fileno() __a = termios.tcgetattr(lowerCAmelCase__ ) try: tty.setraw(lowerCAmelCase__ ) __a = sys.stdin.read(1 ) finally: termios.tcsetattr(lowerCAmelCase__ , termios.TCSADRAIN , lowerCAmelCase__ ) return ch def lowercase ( ) -> Union[str, Any]: __a = get_raw_chars() if ord(lowerCAmelCase__ ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(lowerCAmelCase__ ) == KEYMAP["esc"]: __a = get_raw_chars() if ord(lowerCAmelCase__ ) == KEYMAP["mod_int"]: __a = get_raw_chars() if ord(lowerCAmelCase__ ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(lowerCAmelCase__ ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(lowerCAmelCase__ ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
695
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = { "configuration_mctct": ["MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MCTCTConfig"], "feature_extraction_mctct": ["MCTCTFeatureExtractor"], "processing_mctct": ["MCTCTProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST", "MCTCTForCTC", "MCTCTModel", "MCTCTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig from .feature_extraction_mctct import MCTCTFeatureExtractor from .processing_mctct import MCTCTProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
695
1
"""simple docstring""" 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, ) lowercase_ = { "configuration_xlm_roberta": [ "XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLMRobertaConfig", "XLMRobertaOnnxConfig", ], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["XLMRobertaTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["XLMRobertaTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "XLMRobertaForCausalLM", "XLMRobertaForMaskedLM", "XLMRobertaForMultipleChoice", "XLMRobertaForQuestionAnswering", "XLMRobertaForSequenceClassification", "XLMRobertaForTokenClassification", "XLMRobertaModel", "XLMRobertaPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "TFXLMRobertaForCausalLM", "TFXLMRobertaForMaskedLM", "TFXLMRobertaForMultipleChoice", "TFXLMRobertaForQuestionAnswering", "TFXLMRobertaForSequenceClassification", "TFXLMRobertaForTokenClassification", "TFXLMRobertaModel", "TFXLMRobertaPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "FlaxXLMRobertaForMaskedLM", "FlaxXLMRobertaForCausalLM", "FlaxXLMRobertaForMultipleChoice", "FlaxXLMRobertaForQuestionAnswering", "FlaxXLMRobertaForSequenceClassification", "FlaxXLMRobertaForTokenClassification", "FlaxXLMRobertaModel", "FlaxXLMRobertaPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaConfig, XLMRobertaOnnxConfig, ) try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta import XLMRobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta_fast import XLMRobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaForCausalLM, XLMRobertaForMaskedLM, XLMRobertaForMultipleChoice, XLMRobertaForQuestionAnswering, XLMRobertaForSequenceClassification, XLMRobertaForTokenClassification, XLMRobertaModel, XLMRobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm_roberta import ( TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMRobertaForCausalLM, TFXLMRobertaForMaskedLM, TFXLMRobertaForMultipleChoice, TFXLMRobertaForQuestionAnswering, TFXLMRobertaForSequenceClassification, TFXLMRobertaForTokenClassification, TFXLMRobertaModel, TFXLMRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xlm_roberta import ( FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxXLMRobertaForCausalLM, FlaxXLMRobertaForMaskedLM, FlaxXLMRobertaForMultipleChoice, FlaxXLMRobertaForQuestionAnswering, FlaxXLMRobertaForSequenceClassification, FlaxXLMRobertaForTokenClassification, FlaxXLMRobertaModel, FlaxXLMRobertaPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
695
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json" ), } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[Any] = 'dpr' def __init__( self , _a=30_522 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=2 , _a=0.02 , _a=1E-12 , _a=0 , _a="absolute" , _a = 0 , **_a , ): super().__init__(pad_token_id=_a , **_a ) __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = hidden_act __a = intermediate_size __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = initializer_range __a = layer_norm_eps __a = projection_dim __a = position_embedding_type
695
1
"""simple docstring""" import torch from diffusers import StableDiffusionPipeline lowercase_ = "path-to-your-trained-model" lowercase_ = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.floataa).to("cuda") lowercase_ = "A photo of sks dog in a bucket" lowercase_ = pipe(prompt, num_inference_steps=5_0, guidance_scale=7.5).images[0] image.save("dog-bucket.png")
695
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = StableDiffusionInpaintPipeline __UpperCAmelCase : int = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS __UpperCAmelCase : str = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __UpperCAmelCase : int = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess __UpperCAmelCase : Tuple = frozenset([] ) def __UpperCAmelCase ( self ): torch.manual_seed(0 ) __a = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=9 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=_a , ) __a = PNDMScheduler(skip_prk_steps=_a ) torch.manual_seed(0 ) __a = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) __a = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , hidden_act='''gelu''' , projection_dim=512 , ) __a = CLIPTextModel(_a ) __a = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __a = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def __UpperCAmelCase ( self , _a , _a=0 ): # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched __a = floats_tensor((1, 3, 32, 32) , rng=random.Random(_a ) ).to(_a ) __a = image.cpu().permute(0 , 2 , 3 , 1 )[0] __a = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((64, 64) ) __a = Image.fromarray(np.uinta(image + 4 ) ).convert('''RGB''' ).resize((64, 64) ) if str(_a ).startswith('''mps''' ): __a = torch.manual_seed(_a ) else: __a = torch.Generator(device=_a ).manual_seed(_a ) __a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': init_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def __UpperCAmelCase ( self ): __a = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a = self.get_dummy_components() __a = StableDiffusionInpaintPipeline(**_a ) __a = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = sd_pipe(**_a ).images __a = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __a = np.array([0.4727, 0.5735, 0.3941, 0.5446, 0.5926, 0.4394, 0.5062, 0.4654, 0.4476] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __UpperCAmelCase ( self ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self ): __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench.npy''' ) __a = '''stabilityai/stable-diffusion-2-inpainting''' __a = StableDiffusionInpaintPipeline.from_pretrained(_a , safety_checker=_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = '''Face of a yellow cat, high resolution, sitting on a park bench''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , output_type='''np''' , ) __a = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 9E-3 def __UpperCAmelCase ( self ): __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench_fp16.npy''' ) __a = '''stabilityai/stable-diffusion-2-inpainting''' __a = StableDiffusionInpaintPipeline.from_pretrained( _a , torch_dtype=torch.floataa , safety_checker=_a , ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = '''Face of a yellow cat, high resolution, sitting on a park bench''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , output_type='''np''' , ) __a = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 5E-1 def __UpperCAmelCase ( self ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __a = '''stabilityai/stable-diffusion-2-inpainting''' __a = PNDMScheduler.from_pretrained(_a , subfolder='''scheduler''' ) __a = StableDiffusionInpaintPipeline.from_pretrained( _a , safety_checker=_a , scheduler=_a , torch_dtype=torch.floataa , ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() __a = '''Face of a yellow cat, high resolution, sitting on a park bench''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , num_inference_steps=2 , output_type='''np''' , ) __a = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 10**9
695
1
"""simple docstring""" import unittest from transformers import TrOCRConfig from transformers.testing_utils import is_torch_available, require_torch, 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.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM @require_torch class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a=99 , _a=13 , _a=16 , _a=7 , _a=True , _a=True , _a=True , _a=False , _a=True , _a=2 , _a=32 , _a=4 , _a=4 , _a=30 , _a=0 , _a=1 , _a=2 , _a=None , ): __a = parent __a = batch_size __a = decoder_seq_length # For common tests __a = self.decoder_seq_length __a = is_training __a = use_attention_mask __a = use_labels __a = vocab_size __a = d_model __a = d_model __a = decoder_layers __a = decoder_layers __a = decoder_ffn_dim __a = decoder_attention_heads __a = decoder_attention_heads __a = eos_token_id __a = bos_token_id __a = pad_token_id __a = decoder_start_token_id __a = use_cache __a = max_position_embeddings __a = None __a = decoder_seq_length __a = 2 __a = 1 def __UpperCAmelCase ( self ): __a = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) __a = None if self.use_attention_mask: __a = ids_tensor([self.batch_size, self.decoder_seq_length] , vocab_size=2 ) __a = None if self.use_labels: __a = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) __a = TrOCRConfig( vocab_size=self.vocab_size , d_model=self.d_model , decoder_layers=self.decoder_layers , decoder_ffn_dim=self.decoder_ffn_dim , decoder_attention_heads=self.decoder_attention_heads , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , use_cache=self.use_cache , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , max_position_embeddings=self.max_position_embeddings , ) return (config, input_ids, attention_mask, lm_labels) def __UpperCAmelCase ( self , _a , _a , _a , _a , ): __a = True __a = TrOCRDecoder(config=_a ).to(_a ).eval() __a = input_ids[:2] input_ids[input_ids == 0] += 1 # first forward pass __a = model(_a , use_cache=_a ) __a = model(_a ) __a = model(_a , use_cache=_a ) self.parent.assertTrue(len(_a ) == len(_a ) ) self.parent.assertTrue(len(_a ) == len(_a ) + 1 ) __a = outputs['''past_key_values'''] # create hypothetical next token and extent to next_input_ids __a = ids_tensor((2, 1) , config.vocab_size - 1 ) + 1 # append to next input_ids and __a = torch.cat([input_ids, next_tokens] , dim=-1 ) __a = model(_a )['''last_hidden_state'''] __a = model(_a , past_key_values=_a )['''last_hidden_state'''] # select random slice __a = ids_tensor((1,) , output_from_past.shape[-1] ).item() __a = output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach() __a = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice assert torch.allclose(_a , _a , atol=1E-3 ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() __a , __a , __a , __a = config_and_inputs __a = {'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_torch class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[int] = (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else () __UpperCAmelCase : Optional[int] = (TrOCRForCausalLM,) if is_torch_available() else () __UpperCAmelCase : Union[str, Any] = {'text-generation': TrOCRForCausalLM} if is_torch_available() else {} __UpperCAmelCase : Any = True __UpperCAmelCase : int = False def __UpperCAmelCase ( self ): __a = TrOCRStandaloneDecoderModelTester(self , is_training=_a ) __a = ConfigTester(self , config_class=_a ) def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past(*_a ) def __UpperCAmelCase ( self ): return @unittest.skip('''The model doesn\'t support left padding''' ) # and it's not used enough to be worth fixing :) def __UpperCAmelCase ( self ): pass
695
"""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 __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : int = 0 __UpperCAmelCase : bool = False __UpperCAmelCase : float = 3.0 class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): # 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=_a ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'''a''': 2, '''c''': 2.25} ) @require_cuda def __UpperCAmelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. __a = GradScalerKwargs(init_scale=1_024 , growth_factor=2 ) AcceleratorState._reset_state() __a = Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) __a = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2_000 ) self.assertEqual(scaler._enabled , _a ) @require_multi_gpu def __UpperCAmelCase ( self ): __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": lowercase_ = DistributedDataParallelKwargs(bucket_cap_mb=1_5, find_unused_parameters=True) lowercase_ = Accelerator(kwargs_handlers=[ddp_scaler]) lowercase_ = torch.nn.Linear(1_0_0, 2_0_0) lowercase_ = accelerator.prepare(model) # Check the values changed in kwargs lowercase_ = "" lowercase_ = model.bucket_bytes_cap // (1_0_2_4 * 1_0_2_4) if observed_bucket_cap_map != 1_5: 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)
695
1
"""simple docstring""" 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_float32 (there's also the fix_lavis branch) # also note: to convert Vicuna checkpoints, we had to include /home/niels/python_projects/checkpoints/FastChat/vicuna-7b in lavis/configs/models/blip2/blip2_instruct_vicuna7b.yaml # same for Vicuna-13b from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipImageProcessor, InstructBlipConfig, InstructBlipForConditionalGeneration, InstructBlipProcessor, InstructBlipQFormerConfig, InstructBlipVisionConfig, LlamaConfig, LlamaTokenizerFast, TaConfig, TaTokenizerFast, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def lowercase ( ) -> Any: __a = '''https://raw.githubusercontent.com/salesforce/LAVIS/main/docs/_static/Confusing-Pictures.jpg''' __a = Image.open(requests.get(lowerCAmelCase__ , stream=lowerCAmelCase__ ).raw ).convert('''RGB''' ) return image def lowercase ( lowerCAmelCase__ : Any ) -> Optional[Any]: __a = [] # 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.embeddings.layernorm.weight''') ) rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.bias''', '''qformer.embeddings.layernorm.bias''') ) # fmt: on return rename_keys def lowercase ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : int ) -> List[Any]: __a = dct.pop(lowerCAmelCase__ ) __a = val def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : Tuple ) -> Any: for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases __a = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.q_bias''' ) __a = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict __a = torch.cat((q_bias, torch.zeros_like(lowerCAmelCase__ , requires_grad=lowerCAmelCase__ ), v_bias) ) __a = qkv_bias def lowercase ( lowerCAmelCase__ : Optional[Any] ) -> Optional[Any]: __a = 364 if '''coco''' in model_name else 224 __a = InstructBlipVisionConfig(image_size=lowerCAmelCase__ ).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 "t5-xl" in model_name: __a = TaConfig.from_pretrained('''google/flan-t5-xl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: __a = TaConfig.from_pretrained('''google/flan-t5-xxl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() elif "vicuna-7b" in model_name: __a = LlamaConfig.from_pretrained('''decapoda-research/llama-7b-hf''' , vocab_size=32001 ).to_dict() elif "vicuna-13b" in model_name: __a = LlamaConfig.from_pretrained('''decapoda-research/llama-13b-hf''' , vocab_size=32001 ).to_dict() else: raise ValueError('''Model name not supported''' ) # the authors add one special "[DEC]" token to the vocab of Q-Former, hence vocab size = 30522 + 1 __a = InstructBlipQFormerConfig(vocab_size=30523 ).to_dict() __a = InstructBlipConfig(vision_config=lowerCAmelCase__ , text_config=lowerCAmelCase__ , qformer_config=lowerCAmelCase__ ) return config, image_size @torch.no_grad() def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : str=False ) -> List[str]: __a = AutoTokenizer.from_pretrained('''bert-base-uncased''' , truncation_side='''left''' ) qformer_tokenizer.add_special_tokens({'''bos_token''': '''[DEC]'''} ) if "t5" in model_name: __a = TaTokenizerFast.from_pretrained('''google/flan-t5-xl''' , truncation_side='''left''' ) elif "vicuna" in model_name: # the following was used in the original implementation: # tokenizer = LlamaTokenizer.from_pretrained("huggyllama/llama-7b", use_fast=False, truncation_side="left") # tokenizer.add_special_tokens({"pad_token": "[PAD]"}) # tokenizer.add_special_tokens({"bos_token": "</s>"}) # tokenizer.add_special_tokens({"eos_token": "</s>"}) # tokenizer.add_special_tokens({"unk_token": "</s>"}) __a = LlamaTokenizerFast.from_pretrained( '''huggyllama/llama-7b''' , truncation_side='''left''' , bos_token='''</s>''' , unk_token='''</s>''' ) tokenizer.add_special_tokens({'''pad_token''': '''[PAD]'''} ) __a , __a = get_blipa_config(lowerCAmelCase__ ) __a = InstructBlipForConditionalGeneration(lowerCAmelCase__ ).eval() __a = { '''instructblip-vicuna-7b''': ('''blip2_vicuna_instruct''', '''vicuna7b'''), '''instructblip-vicuna-13b''': ('''blip2_vicuna_instruct''', '''vicuna13b'''), '''instructblip-flan-t5-xl''': ('''blip2_t5_instruct''', '''flant5xl'''), '''instructblip-flan-t5-xxl''': ('''blip2_t5_instruct''', '''flant5xxl'''), } __a , __a = model_name_to_original[model_name] # load original model print('''Loading original model...''' ) __a = '''cuda:1''' if torch.cuda.is_available() else '''cpu''' __a = '''cuda:2''' if torch.cuda.is_available() else '''cpu''' __a , __a , __a = load_model_and_preprocess( name=lowerCAmelCase__ , model_type=lowerCAmelCase__ , is_eval=lowerCAmelCase__ , device=lowerCAmelCase__ ) original_model.eval() print('''Done!''' ) # update state dict keys __a = original_model.state_dict() __a = create_rename_keys(lowerCAmelCase__ ) for src, dest in rename_keys: rename_key(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): __a = state_dict.pop(lowerCAmelCase__ ) if key.startswith('''Qformer.bert''' ): __a = key.replace('''Qformer.bert''' , '''qformer''' ) if "attention.self" in key: __a = key.replace('''self''' , '''attention''' ) if "llm_proj" in key: __a = key.replace('''llm_proj''' , '''language_projection''' ) if "t5_proj" in key: __a = key.replace('''t5_proj''' , '''language_projection''' ) if key.startswith('''llm_model''' ): __a = key.replace('''llm_model''' , '''language_model''' ) if key.startswith('''t5''' ): __a = key.replace('''t5''' , '''language''' ) __a = val # read in qv biases read_in_q_v_bias(lowerCAmelCase__ , lowerCAmelCase__ ) # note: weights get loaded in torch.float32 by default hf_model.load_state_dict(lowerCAmelCase__ , strict=lowerCAmelCase__ ) __a = load_demo_image() __a = '''What is unusual about this image?''' # create processor __a = BlipImageProcessor( size={'''height''': image_size, '''width''': image_size} , image_mean=lowerCAmelCase__ , image_std=lowerCAmelCase__ ) __a = InstructBlipProcessor( image_processor=lowerCAmelCase__ , tokenizer=lowerCAmelCase__ , qformer_tokenizer=lowerCAmelCase__ , ) __a = processor(images=lowerCAmelCase__ , text=lowerCAmelCase__ , return_tensors='''pt''' ).to(lowerCAmelCase__ ) # make sure processor creates exact same pixel values __a = vis_processors['''eval'''](lowerCAmelCase__ ).unsqueeze(0 ).to(lowerCAmelCase__ ) __a = inputs.pixel_values assert torch.allclose(original_pixel_values.to(pixel_values.device ) , lowerCAmelCase__ ) original_model.to(lowerCAmelCase__ ) hf_model.to(lowerCAmelCase__ ) with torch.no_grad(): if "vicuna" in model_name: __a = original_model({'''image''': original_pixel_values, '''text_input''': [prompt]} ).logits __a = hf_model(**lowerCAmelCase__ ).logits else: __a = original_model( {'''image''': original_pixel_values, '''text_input''': [prompt], '''text_output''': ['''\n''']} ).logits __a = tokenizer('''\n''' , return_tensors='''pt''' ).input_ids.to(lowerCAmelCase__ ) __a = label_input_ids.masked_fill(label_input_ids == tokenizer.pad_token_id , -100 ) __a = hf_model(**lowerCAmelCase__ , labels=lowerCAmelCase__ ).logits print('''First values of original logits:''' , original_logits[0, :3, :3] ) print('''First values of HF logits:''' , logits[0, :3, :3] ) # assert values assert original_logits.shape == logits.shape __a = 1e-4 if '''vicuna''' in model_name else 1e-5 assert torch.allclose(original_logits.to(logits.device ) , lowerCAmelCase__ , atol=lowerCAmelCase__ ) print('''Looks ok!''' ) print('''Generating with original model...''' ) __a = original_model.generate({'''image''': original_pixel_values, '''prompt''': prompt} , num_beams=5 ) # important: we need to cast the weights of the HF model to the appropriate type print('''Generating with HF model...''' ) __a = hf_model.generate( **lowerCAmelCase__ , do_sample=lowerCAmelCase__ , num_beams=5 , max_length=256 , min_length=1 , top_p=0.9 , repetition_penalty=1.5 , length_penalty=1.0 , temperature=1 , ) if "vicuna" in model_name: # convert output id 0 to 2 (eos_token_id) # TODO add this in the generate method? __a = 2 print('''Original generation:''' , lowerCAmelCase__ ) __a = processor.batch_decode(lowerCAmelCase__ , skip_special_tokens=lowerCAmelCase__ ) __a = [text.strip() for text in output_text] print('''HF generation:''' , lowerCAmelCase__ ) if pytorch_dump_folder_path is not None: processor.save_pretrained(lowerCAmelCase__ ) hf_model.save_pretrained(lowerCAmelCase__ ) if push_to_hub: processor.push_to_hub(f'''Salesforce/{model_name}''' ) hf_model.push_to_hub(f'''Salesforce/{model_name}''' ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() lowercase_ = [ "instructblip-vicuna-7b", "instructblip-vicuna-13b", "instructblip-flan-t5-xl", "instructblip-flan-t5-xxl", ] parser.add_argument( "--model_name", default="instructblip-flan-t5-xl", 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", ) lowercase_ = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
695
"""simple docstring""" import inspect import os import sys import unittest import accelerate from accelerate.test_utils import execute_subprocess_async, require_tpu class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = inspect.getfile(accelerate.test_utils ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) __a = os.path.sep.join(inspect.getfile(self.__class__ ).split(os.path.sep )[:-1] ) @require_tpu def __UpperCAmelCase ( self ): __a = f''' {self.test_dir}/xla_spawn.py --num_cores 8 {self.test_file_path} '''.split() __a = [sys.executable] + distributed_args execute_subprocess_async(_a , env=os.environ.copy() )
695
1
"""simple docstring""" import argparse import torch from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert from transformers.utils import logging logging.set_verbosity_info() def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str ) -> List[Any]: # Initialise PyTorch model __a = RemBertConfig.from_json_file(lowerCAmelCase__ ) print('''Building PyTorch model from configuration: {}'''.format(str(lowerCAmelCase__ ) ) ) __a = RemBertModel(lowerCAmelCase__ ) # Load weights from tf checkpoint load_tf_weights_in_rembert(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Save pytorch-model print('''Save PyTorch model to {}'''.format(lowerCAmelCase__ ) ) torch.save(model.state_dict() , lowerCAmelCase__ ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--rembert_config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained RemBERT 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." ) lowercase_ = parser.parse_args() convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)
695
"""simple docstring""" import os import unittest from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, BertTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = BertTokenizer __UpperCAmelCase : Optional[Any] = BertTokenizerFast __UpperCAmelCase : str = True __UpperCAmelCase : Tuple = True __UpperCAmelCase : Any = filter_non_english def __UpperCAmelCase ( self ): super().setUp() __a = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] __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] ) ) def __UpperCAmelCase ( self , _a ): __a = '''UNwant\u00E9d,running''' __a = '''unwanted, running''' return input_text, output_text def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file ) __a = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(_a , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [9, 6, 7, 12, 10, 11] ) def __UpperCAmelCase ( self ): if not self.test_rust_tokenizer: return __a = self.get_tokenizer() __a = self.get_rust_tokenizer() __a = '''UNwant\u00E9d,running''' __a = tokenizer.tokenize(_a ) __a = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) __a = self.get_rust_tokenizer() __a = tokenizer.encode(_a ) __a = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a ) # With lower casing __a = self.get_tokenizer(do_lower_case=_a ) __a = self.get_rust_tokenizer(do_lower_case=_a ) __a = '''UNwant\u00E9d,running''' __a = tokenizer.tokenize(_a ) __a = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) __a = self.get_rust_tokenizer() __a = tokenizer.encode(_a ) __a = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('''ah\u535A\u63A8zz''' ) , ['''ah''', '''\u535A''', '''\u63A8''', '''zz'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''hello''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hällo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''h\u00E9llo'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HäLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HaLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , never_split=['''[UNK]'''] ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? [UNK]''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''', '''[UNK]'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer() __a = '''a\n\'ll !!to?\'d of, can\'t.''' __a = ['''a''', '''\'''', '''ll''', '''!''', '''!''', '''to''', '''?''', '''\'''', '''d''', '''of''', ''',''', '''can''', '''\'''', '''t''', '''.'''] self.assertListEqual(tokenizer.tokenize(_a ) , _a ) def __UpperCAmelCase ( self ): __a = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing'''] __a = {} for i, token in enumerate(_a ): __a = i __a = WordpieceTokenizer(vocab=_a , unk_token='''[UNK]''' ) self.assertListEqual(tokenizer.tokenize('''''' ) , [] ) self.assertListEqual(tokenizer.tokenize('''unwanted running''' ) , ['''un''', '''##want''', '''##ed''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.tokenize('''unwantedX running''' ) , ['''[UNK]''', '''runn''', '''##ing'''] ) def __UpperCAmelCase ( self ): self.assertTrue(_is_whitespace(''' ''' ) ) self.assertTrue(_is_whitespace('''\t''' ) ) self.assertTrue(_is_whitespace('''\r''' ) ) self.assertTrue(_is_whitespace('''\n''' ) ) self.assertTrue(_is_whitespace('''\u00A0''' ) ) self.assertFalse(_is_whitespace('''A''' ) ) self.assertFalse(_is_whitespace('''-''' ) ) def __UpperCAmelCase ( self ): self.assertTrue(_is_control('''\u0005''' ) ) self.assertFalse(_is_control('''A''' ) ) self.assertFalse(_is_control(''' ''' ) ) self.assertFalse(_is_control('''\t''' ) ) self.assertFalse(_is_control('''\r''' ) ) def __UpperCAmelCase ( self ): self.assertTrue(_is_punctuation('''-''' ) ) self.assertTrue(_is_punctuation('''$''' ) ) self.assertTrue(_is_punctuation('''`''' ) ) self.assertTrue(_is_punctuation('''.''' ) ) self.assertFalse(_is_punctuation('''A''' ) ) self.assertFalse(_is_punctuation(''' ''' ) ) def __UpperCAmelCase ( self ): __a = self.get_tokenizer() __a = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(_a ) for t in ['''Test''', '''\xad''', '''test''']] , [['''[UNK]'''], [], ['''[UNK]''']] ) self.assertListEqual( [rust_tokenizer.tokenize(_a ) for t in ['''Test''', '''\xad''', '''test''']] , [['''[UNK]'''], [], ['''[UNK]''']] ) @slow def __UpperCAmelCase ( self ): __a = self.tokenizer_class.from_pretrained('''bert-base-uncased''' ) __a = tokenizer.encode('''sequence builders''' , add_special_tokens=_a ) __a = tokenizer.encode('''multi-sequence build''' , add_special_tokens=_a ) __a = tokenizer.build_inputs_with_special_tokens(_a ) __a = tokenizer.build_inputs_with_special_tokens(_a , _a ) assert encoded_sentence == [101] + text + [102] assert encoded_pair == [101] + text + [102] + text_a + [102] def __UpperCAmelCase ( self ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = f'''A, naïve {tokenizer_r.mask_token} AllenNLP sentence.''' __a = tokenizer_r.encode_plus( _a , return_attention_mask=_a , return_token_type_ids=_a , return_offsets_mapping=_a , add_special_tokens=_a , ) __a = tokenizer_r.do_lower_case if hasattr(_a , '''do_lower_case''' ) else False __a = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), '''A'''), ((1, 2), ''','''), ((3, 5), '''na'''), ((5, 6), '''##ï'''), ((6, 8), '''##ve'''), ((9, 15), tokenizer_r.mask_token), ((16, 21), '''Allen'''), ((21, 23), '''##NL'''), ((23, 24), '''##P'''), ((25, 33), '''sentence'''), ((33, 34), '''.'''), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), '''a'''), ((1, 2), ''','''), ((3, 8), '''naive'''), ((9, 15), tokenizer_r.mask_token), ((16, 21), '''allen'''), ((21, 23), '''##nl'''), ((23, 24), '''##p'''), ((25, 33), '''sentence'''), ((33, 34), '''.'''), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens['''input_ids'''] ) ) self.assertEqual([e[0] for e in expected_results] , tokens['''offset_mapping'''] ) def __UpperCAmelCase ( self ): __a = ['''的''', '''人''', '''有'''] __a = ''''''.join(_a ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __a = True __a = self.tokenizer_class.from_pretrained(_a , **_a ) __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = tokenizer_p.encode(_a , add_special_tokens=_a ) __a = tokenizer_r.encode(_a , add_special_tokens=_a ) __a = tokenizer_r.convert_ids_to_tokens(_a ) __a = tokenizer_p.convert_ids_to_tokens(_a ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(_a , _a ) self.assertListEqual(_a , _a ) __a = False __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = self.tokenizer_class.from_pretrained(_a , **_a ) __a = tokenizer_r.encode(_a , add_special_tokens=_a ) __a = tokenizer_p.encode(_a , add_special_tokens=_a ) __a = tokenizer_r.convert_ids_to_tokens(_a ) __a = tokenizer_p.convert_ids_to_tokens(_a ) # it is expected that only the first Chinese character is not preceded by "##". __a = [ f'''##{token}''' if idx != 0 else token for idx, token in enumerate(_a ) ] self.assertListEqual(_a , _a ) self.assertListEqual(_a , _a )
695
1
"""simple docstring""" import json import sys import tempfile import unittest from pathlib import Path import transformers from transformers import ( CONFIG_MAPPING, FEATURE_EXTRACTOR_MAPPING, AutoConfig, AutoFeatureExtractor, WavaVecaConfig, WavaVecaFeatureExtractor, ) from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir sys.path.append(str(Path(__file__).parent.parent.parent.parent / "utils")) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 lowercase_ = get_tests_dir("fixtures") lowercase_ = get_tests_dir("fixtures/dummy_feature_extractor_config.json") lowercase_ = get_tests_dir("fixtures/dummy-config.json") class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = 0 def __UpperCAmelCase ( self ): __a = AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h''' ) self.assertIsInstance(_a , _a ) def __UpperCAmelCase ( self ): __a = AutoFeatureExtractor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __UpperCAmelCase ( self ): with tempfile.TemporaryDirectory() as tmpdirname: __a = WavaVecaConfig() # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally __a = AutoFeatureExtractor.from_pretrained(_a ).to_dict() config_dict.pop('''feature_extractor_type''' ) __a = WavaVecaFeatureExtractor(**_a ) # save in new folder model_config.save_pretrained(_a ) config.save_pretrained(_a ) __a = AutoFeatureExtractor.from_pretrained(_a ) # make sure private variable is not incorrectly saved __a = json.loads(config.to_json_string() ) self.assertTrue('''_processor_class''' not in dict_as_saved ) self.assertIsInstance(_a , _a ) def __UpperCAmelCase ( self ): __a = AutoFeatureExtractor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __UpperCAmelCase ( self ): with self.assertRaisesRegex( _a , '''bert-base is not a local folder and is not a valid model identifier''' ): __a = AutoFeatureExtractor.from_pretrained('''bert-base''' ) def __UpperCAmelCase ( self ): with self.assertRaisesRegex( _a , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): __a = AutoFeatureExtractor.from_pretrained(_a , revision='''aaaaaa''' ) def __UpperCAmelCase ( self ): with self.assertRaisesRegex( _a , '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.''' , ): __a = AutoFeatureExtractor.from_pretrained('''hf-internal-testing/config-no-model''' ) def __UpperCAmelCase ( self ): # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(_a ): __a = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(_a ): __a = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=_a ) __a = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=_a ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) # Test feature extractor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(_a ) __a = AutoFeatureExtractor.from_pretrained(_a , trust_remote_code=_a ) self.assertEqual(reloaded_feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) def __UpperCAmelCase ( self ): try: AutoConfig.register('''custom''' , _a ) AutoFeatureExtractor.register(_a , _a ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_a ): AutoFeatureExtractor.register(_a , _a ) # Now that the config is registered, it can be used as any other config with the auto-API __a = CustomFeatureExtractor.from_pretrained(_a ) with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(_a ) __a = AutoFeatureExtractor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] def __UpperCAmelCase ( self ): class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Optional[int] = True try: AutoConfig.register('''custom''' , _a ) AutoFeatureExtractor.register(_a , _a ) # If remote code is not set, the default is to use local __a = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) self.assertTrue(feature_extractor.is_local ) # If remote code is disabled, we load the local one. __a = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=_a ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) self.assertTrue(feature_extractor.is_local ) # If remote is enabled, we load from the Hub __a = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=_a ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) self.assertTrue(not hasattr(_a , '''is_local''' ) ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
695
"""simple docstring""" from __future__ import annotations def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> float: if days_between_payments <= 0: raise ValueError('''days_between_payments must be > 0''' ) if daily_interest_rate < 0: raise ValueError('''daily_interest_rate must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return principal * daily_interest_rate * days_between_payments def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float , ) -> float: if number_of_compounding_periods <= 0: raise ValueError('''number_of_compounding_periods must be > 0''' ) if nominal_annual_interest_rate_percentage < 0: raise ValueError('''nominal_annual_interest_rate_percentage must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return principal * ( (1 + nominal_annual_interest_rate_percentage) ** number_of_compounding_periods - 1 ) def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float , ) -> float: if number_of_years <= 0: raise ValueError('''number_of_years must be > 0''' ) if nominal_annual_percentage_rate < 0: raise ValueError('''nominal_annual_percentage_rate must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return compound_interest( lowerCAmelCase__ , nominal_annual_percentage_rate / 365 , number_of_years * 365 ) if __name__ == "__main__": import doctest doctest.testmod()
695
1
"""simple docstring""" # Note: if you intend to run this script make sure you look under scripts/fsmt/ # to locate the appropriate script to do the work correctly. There is a set of scripts to: # - download and prepare data and run the conversion script # - perform eval to get the best hparam into the config # - generate model_cards - useful if you have multiple models from the same paper import argparse import json import os import re from collections import OrderedDict from os.path import basename, dirname import fairseq import torch from fairseq import hub_utils from fairseq.data.dictionary import Dictionary from transformers import FSMTConfig, FSMTForConditionalGeneration from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES from transformers.tokenization_utils_base import TOKENIZER_CONFIG_FILE from transformers.utils import WEIGHTS_NAME, logging logging.set_verbosity_warning() lowercase_ = 2 # based on the results of a search on a range of `num_beams`, `length_penalty` and `early_stopping` # values against wmt19 test data to obtain the best BLEU scores, we will use the following defaults: # # * `num_beams`: 5 (higher scores better, but requires more memory/is slower, can be adjusted by users) # * `early_stopping`: `False` consistently scored better # * `length_penalty` varied, so will assign the best one depending on the model lowercase_ = { # fairseq: "wmt19-ru-en": {"length_penalty": 1.1}, "wmt19-en-ru": {"length_penalty": 1.15}, "wmt19-en-de": {"length_penalty": 1.0}, "wmt19-de-en": {"length_penalty": 1.1}, # allenai: "wmt16-en-de-dist-12-1": {"length_penalty": 0.6}, "wmt16-en-de-dist-6-1": {"length_penalty": 0.6}, "wmt16-en-de-12-1": {"length_penalty": 0.8}, "wmt19-de-en-6-6-base": {"length_penalty": 0.6}, "wmt19-de-en-6-6-big": {"length_penalty": 0.6}, } # this remaps the different models to their organization names lowercase_ = {} for m in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: lowercase_ = "facebook" for m in [ "wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1", "wmt19-de-en-6-6-base", "wmt19-de-en-6-6-big", ]: lowercase_ = "allenai" def lowercase ( lowerCAmelCase__ : Optional[int] ) -> Tuple: # (1) remove word breaking symbol, (2) add word ending symbol where the word is not broken up, # e.g.: d = {'le@@': 5, 'tt@@': 6, 'er': 7} => {'le': 5, 'tt': 6, 'er</w>': 7} __a = dict((re.sub(r'''@@$''' , '''''' , lowerCAmelCase__ ), v) if k.endswith('''@@''' ) else (re.sub(r'''$''' , '''</w>''' , lowerCAmelCase__ ), v) for k, v in d.items() ) __a = '''<s> <pad> </s> <unk>'''.split() # restore the special tokens for k in keep_keys: del da[f'''{k}</w>'''] __a = d[k] # restore return da def lowercase ( lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Any ) -> Tuple: # prep assert os.path.exists(lowerCAmelCase__ ) os.makedirs(lowerCAmelCase__ , exist_ok=lowerCAmelCase__ ) print(f'''Writing results to {pytorch_dump_folder_path}''' ) # handle various types of models __a = basename(lowerCAmelCase__ ) __a = dirname(lowerCAmelCase__ ) __a = fairseq.model_parallel.models.transformer.ModelParallelTransformerModel __a = cls.hub_models() __a = {'''bpe''': '''fastbpe''', '''tokenizer''': '''moses'''} __a = '''.''' # note: since the model dump is old, fairseq has upgraded its model some # time later, and it does a whole lot of rewrites and splits on the saved # weights, therefore we can't use torch.load() directly on the model file. # see: upgrade_state_dict(state_dict) in fairseq_model.py print(f'''using checkpoint {checkpoint_file}''' ) __a = hub_utils.from_pretrained( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , archive_map=lowerCAmelCase__ , **lowerCAmelCase__ ) __a = vars(chkpt['''args''']['''model'''] ) __a = args['''source_lang'''] __a = args['''target_lang'''] __a = dirname(lowerCAmelCase__ ) __a = basename(lowerCAmelCase__ ) # dicts __a = os.path.join(lowerCAmelCase__ , f'''dict.{src_lang}.txt''' ) __a = os.path.join(lowerCAmelCase__ , f'''dict.{tgt_lang}.txt''' ) __a = Dictionary.load(lowerCAmelCase__ ) __a = rewrite_dict_keys(src_dict.indices ) __a = len(lowerCAmelCase__ ) __a = os.path.join(lowerCAmelCase__ , '''vocab-src.json''' ) print(f'''Generating {src_vocab_file} of {src_vocab_size} of {src_lang} records''' ) with open(lowerCAmelCase__ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(lowerCAmelCase__ , ensure_ascii=lowerCAmelCase__ , indent=lowerCAmelCase__ ) ) # detect whether this is a do_lower_case situation, which can be derived by checking whether we # have at least one uppercase letter in the source vocab __a = True for k in src_vocab.keys(): if not k.islower(): __a = False break __a = Dictionary.load(lowerCAmelCase__ ) __a = rewrite_dict_keys(tgt_dict.indices ) __a = len(lowerCAmelCase__ ) __a = os.path.join(lowerCAmelCase__ , '''vocab-tgt.json''' ) print(f'''Generating {tgt_vocab_file} of {tgt_vocab_size} of {tgt_lang} records''' ) with open(lowerCAmelCase__ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(lowerCAmelCase__ , ensure_ascii=lowerCAmelCase__ , indent=lowerCAmelCase__ ) ) # merges_file (bpecodes) __a = os.path.join(lowerCAmelCase__ , VOCAB_FILES_NAMES['''merges_file'''] ) for fn in ["bpecodes", "code"]: # older fairseq called the merges file "code" __a = os.path.join(lowerCAmelCase__ , lowerCAmelCase__ ) if os.path.exists(lowerCAmelCase__ ): break with open(lowerCAmelCase__ , encoding='''utf-8''' ) as fin: __a = fin.read() __a = re.sub(r''' \d+$''' , '''''' , lowerCAmelCase__ , 0 , re.M ) # remove frequency number print(f'''Generating {merges_file}''' ) with open(lowerCAmelCase__ , '''w''' , encoding='''utf-8''' ) as fout: fout.write(lowerCAmelCase__ ) # model config __a = os.path.join(lowerCAmelCase__ , '''config.json''' ) # validate bpe/tokenizer config, as currently it's hardcoded to moses+fastbpe - # may have to modify the tokenizer if a different type is used by a future model assert args["bpe"] == "fastbpe", f'''need to extend tokenizer to support bpe={args['bpe']}''' assert args["tokenizer"] == "moses", f'''need to extend tokenizer to support bpe={args['tokenizer']}''' __a = { '''architectures''': ['''FSMTForConditionalGeneration'''], '''model_type''': '''fsmt''', '''activation_dropout''': args['''activation_dropout'''], '''activation_function''': '''relu''', '''attention_dropout''': args['''attention_dropout'''], '''d_model''': args['''decoder_embed_dim'''], '''dropout''': args['''dropout'''], '''init_std''': 0.02, '''max_position_embeddings''': args['''max_source_positions'''], '''num_hidden_layers''': args['''encoder_layers'''], '''src_vocab_size''': src_vocab_size, '''tgt_vocab_size''': tgt_vocab_size, '''langs''': [src_lang, tgt_lang], '''encoder_attention_heads''': args['''encoder_attention_heads'''], '''encoder_ffn_dim''': args['''encoder_ffn_embed_dim'''], '''encoder_layerdrop''': args['''encoder_layerdrop'''], '''encoder_layers''': args['''encoder_layers'''], '''decoder_attention_heads''': args['''decoder_attention_heads'''], '''decoder_ffn_dim''': args['''decoder_ffn_embed_dim'''], '''decoder_layerdrop''': args['''decoder_layerdrop'''], '''decoder_layers''': args['''decoder_layers'''], '''bos_token_id''': 0, '''pad_token_id''': 1, '''eos_token_id''': 2, '''is_encoder_decoder''': True, '''scale_embedding''': not args['''no_scale_embedding'''], '''tie_word_embeddings''': args['''share_all_embeddings'''], } # good hparam defaults to start with __a = 5 __a = False if model_dir in best_score_hparams and "length_penalty" in best_score_hparams[model_dir]: __a = best_score_hparams[model_dir]['''length_penalty'''] else: __a = 1.0 print(f'''Generating {fsmt_model_config_file}''' ) with open(lowerCAmelCase__ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(lowerCAmelCase__ , ensure_ascii=lowerCAmelCase__ , indent=lowerCAmelCase__ ) ) # tokenizer config __a = os.path.join(lowerCAmelCase__ , lowerCAmelCase__ ) __a = { '''langs''': [src_lang, tgt_lang], '''model_max_length''': 1024, '''do_lower_case''': do_lower_case, } print(f'''Generating {fsmt_tokenizer_config_file}''' ) with open(lowerCAmelCase__ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(lowerCAmelCase__ , ensure_ascii=lowerCAmelCase__ , indent=lowerCAmelCase__ ) ) # model __a = chkpt['''models'''][0] __a = model.state_dict() # rename keys to start with 'model.' __a = OrderedDict(('''model.''' + k, v) for k, v in model_state_dict.items() ) # remove unneeded keys __a = [ '''model.model''', '''model.encoder.version''', '''model.decoder.version''', '''model.encoder_embed_tokens.weight''', '''model.decoder_embed_tokens.weight''', '''model.encoder.embed_positions._float_tensor''', '''model.decoder.embed_positions._float_tensor''', ] for k in ignore_keys: model_state_dict.pop(lowerCAmelCase__ , lowerCAmelCase__ ) __a = FSMTConfig.from_pretrained(lowerCAmelCase__ ) __a = FSMTForConditionalGeneration(lowerCAmelCase__ ) # check that it loads ok model_new.load_state_dict(lowerCAmelCase__ , strict=lowerCAmelCase__ ) # save __a = os.path.join(lowerCAmelCase__ , lowerCAmelCase__ ) print(f'''Generating {pytorch_weights_dump_path}''' ) torch.save(lowerCAmelCase__ , lowerCAmelCase__ ) print('''Conversion is done!''' ) print('''\nLast step is to upload the files to s3''' ) print(f'''cd {data_root}''' ) print(f'''transformers-cli upload {model_dir}''' ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--fsmt_checkpoint_path", default=None, type=str, required=True, help=( "Path to the official PyTorch checkpoint file which is expected to reside in the dump dir with dicts," " bpecodes, etc." ), ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) lowercase_ = parser.parse_args() convert_fsmt_checkpoint_to_pytorch(args.fsmt_checkpoint_path, args.pytorch_dump_folder_path)
695
"""simple docstring""" def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any=False ) -> Any: if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = len(set_a.intersection(lowerCAmelCase__ ) ) if alternative_union: __a = len(lowerCAmelCase__ ) + len(lowerCAmelCase__ ) else: __a = len(set_a.union(lowerCAmelCase__ ) ) return intersection / union if isinstance(lowerCAmelCase__ , (list, tuple) ) and isinstance(lowerCAmelCase__ , (list, tuple) ): __a = [element for element in set_a if element in set_b] if alternative_union: __a = len(lowerCAmelCase__ ) + len(lowerCAmelCase__ ) return len(lowerCAmelCase__ ) / union else: __a = set_a + [element for element in set_b if element not in set_a] return len(lowerCAmelCase__ ) / len(lowerCAmelCase__ ) return len(lowerCAmelCase__ ) / len(lowerCAmelCase__ ) return None if __name__ == "__main__": lowercase_ = {"a", "b", "c", "d", "e"} lowercase_ = {"c", "d", "e", "f", "h", "i"} print(jaccard_similarity(set_a, set_b))
695
1
"""simple docstring""" from math import pi, sqrt def lowercase ( lowerCAmelCase__ : float ) -> float: if num <= 0: raise ValueError('''math domain error''' ) if num > 1_71.5: raise OverflowError('''math range error''' ) elif num - int(lowerCAmelCase__ ) not in (0, 0.5): raise NotImplementedError('''num must be an integer or a half-integer''' ) elif num == 0.5: return sqrt(lowerCAmelCase__ ) else: return 1.0 if num == 1 else (num - 1) * gamma(num - 1 ) def lowercase ( ) -> None: assert gamma(0.5 ) == sqrt(lowerCAmelCase__ ) assert gamma(1 ) == 1.0 assert gamma(2 ) == 1.0 if __name__ == "__main__": from doctest import testmod testmod() lowercase_ = 1.0 while num: lowercase_ = float(input("Gamma of: ")) print(F'''gamma({num}) = {gamma(num)}''') print("\nEnter 0 to exit...")
695
"""simple docstring""" from __future__ import annotations import requests def lowercase ( lowerCAmelCase__ : str ) -> dict: __a = f'''https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty''' return requests.get(lowerCAmelCase__ ).json() def lowercase ( lowerCAmelCase__ : int = 10 ) -> list[dict]: __a = '''https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty''' __a = requests.get(lowerCAmelCase__ ).json()[:max_stories] return [get_hackernews_story(lowerCAmelCase__ ) for story_id in story_ids] def lowercase ( lowerCAmelCase__ : int = 10 ) -> str: __a = hackernews_top_stories(lowerCAmelCase__ ) return "\n".join('''* [{title}]({url})'''.format(**lowerCAmelCase__ ) for story in stories ) if __name__ == "__main__": print(hackernews_top_stories_as_markdown())
695
1
"""simple docstring""" import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "microsoft/wavlm-base": "https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json", # See all WavLM models at https://huggingface.co/models?filter=wavlm } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = 'wavlm' def __init__( self , _a=32 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=0.1 , _a=0.0 , _a=0.1 , _a=0.1 , _a=0.02 , _a=1E-5 , _a="group" , _a="gelu" , _a=(512, 512, 512, 512, 512, 512, 512) , _a=(5, 2, 2, 2, 2, 2, 2) , _a=(10, 3, 3, 3, 3, 2, 2) , _a=False , _a=128 , _a=16 , _a=320 , _a=800 , _a=False , _a=True , _a=0.05 , _a=10 , _a=2 , _a=0.0 , _a=10 , _a=320 , _a=2 , _a=0.1 , _a=100 , _a=256 , _a=256 , _a=0.1 , _a="mean" , _a=False , _a=False , _a=256 , _a=(512, 512, 512, 512, 1_500) , _a=(5, 3, 3, 1, 1) , _a=(1, 2, 3, 1, 1) , _a=512 , _a=80 , _a=0 , _a=1 , _a=2 , _a=False , _a=3 , _a=2 , _a=3 , _a=None , **_a , ): super().__init__(**_a , pad_token_id=_a , bos_token_id=_a , eos_token_id=_a ) __a = hidden_size __a = feat_extract_norm __a = feat_extract_activation __a = list(_a ) __a = list(_a ) __a = list(_a ) __a = conv_bias __a = num_buckets __a = max_bucket_distance __a = num_conv_pos_embeddings __a = num_conv_pos_embedding_groups __a = len(self.conv_dim ) __a = num_hidden_layers __a = intermediate_size __a = hidden_act __a = num_attention_heads __a = hidden_dropout __a = attention_dropout __a = activation_dropout __a = feat_proj_dropout __a = final_dropout __a = layerdrop __a = layer_norm_eps __a = initializer_range __a = num_ctc_classes __a = vocab_size __a = do_stable_layer_norm __a = use_weighted_layer_sum __a = classifier_proj_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' f''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,''' f''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __a = apply_spec_augment __a = mask_time_prob __a = mask_time_length __a = mask_time_min_masks __a = mask_feature_prob __a = mask_feature_length # parameters for pretraining with codevector quantized representations __a = num_codevectors_per_group __a = num_codevector_groups __a = contrastive_logits_temperature __a = num_negatives __a = codevector_dim __a = proj_codevector_dim __a = diversity_loss_weight # ctc loss __a = ctc_loss_reduction __a = ctc_zero_infinity # adapter __a = add_adapter __a = adapter_kernel_size __a = adapter_stride __a = num_adapter_layers __a = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. __a = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. __a = list(_a ) __a = list(_a ) __a = list(_a ) __a = xvector_output_dim @property def __UpperCAmelCase ( self ): return functools.reduce(operator.mul , self.conv_stride , 1 )
695
"""simple docstring""" import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES from ...utils import logging from ..auto import CONFIG_MAPPING lowercase_ = logging.get_logger(__name__) lowercase_ = { "salesforce/blip2-opt-2.7b": "https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json", } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = 'blip_2_vision_model' def __init__( self , _a=1_408 , _a=6_144 , _a=39 , _a=16 , _a=224 , _a=14 , _a="gelu" , _a=0.0_0001 , _a=0.0 , _a=1E-10 , _a=True , **_a , ): super().__init__(**_a ) __a = hidden_size __a = intermediate_size __a = num_hidden_layers __a = num_attention_heads __a = patch_size __a = image_size __a = initializer_range __a = attention_dropout __a = layer_norm_eps __a = hidden_act __a = qkv_bias @classmethod def __UpperCAmelCase ( cls , _a , **_a ): cls._set_token_in_kwargs(_a ) __a , __a = cls.get_config_dict(_a , **_a ) # get the vision config dict if we are loading from Blip2Config if config_dict.get('''model_type''' ) == "blip-2": __a = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(_a , **_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : str = 'blip_2_qformer' def __init__( self , _a=30_522 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=0.02 , _a=1E-12 , _a=0 , _a="absolute" , _a=2 , _a=1_408 , **_a , ): super().__init__(pad_token_id=_a , **_a ) __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = hidden_act __a = intermediate_size __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = initializer_range __a = layer_norm_eps __a = position_embedding_type __a = cross_attention_frequency __a = encoder_hidden_size @classmethod def __UpperCAmelCase ( cls , _a , **_a ): cls._set_token_in_kwargs(_a ) __a , __a = cls.get_config_dict(_a , **_a ) # get the qformer config dict if we are loading from Blip2Config if config_dict.get('''model_type''' ) == "blip-2": __a = config_dict['''qformer_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(_a , **_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Any = 'blip-2' __UpperCAmelCase : List[str] = True def __init__( self , _a=None , _a=None , _a=None , _a=32 , **_a ): super().__init__(**_a ) if vision_config is None: __a = {} logger.info('''vision_config is None. initializing the Blip2VisionConfig with default values.''' ) if qformer_config is None: __a = {} logger.info('''qformer_config is None. Initializing the Blip2QFormerConfig with default values.''' ) if text_config is None: __a = {} logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).''' ) __a = BlipaVisionConfig(**_a ) __a = BlipaQFormerConfig(**_a ) __a = text_config['''model_type'''] if '''model_type''' in text_config else '''opt''' __a = CONFIG_MAPPING[text_model_type](**_a ) __a = self.text_config.tie_word_embeddings __a = self.text_config.is_encoder_decoder __a = num_query_tokens __a = self.vision_config.hidden_size __a = self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES __a = 1.0 __a = 0.02 @classmethod def __UpperCAmelCase ( cls , _a , _a , _a , **_a , ): return cls( vision_config=vision_config.to_dict() , qformer_config=qformer_config.to_dict() , text_config=text_config.to_dict() , **_a , ) def __UpperCAmelCase ( self ): __a = copy.deepcopy(self.__dict__ ) __a = self.vision_config.to_dict() __a = self.qformer_config.to_dict() __a = self.text_config.to_dict() __a = self.__class__.model_type return output
695
1
"""simple docstring""" import argparse import json import os import sys import tempfile import unittest from argparse import Namespace from dataclasses import dataclass, field from enum import Enum from pathlib import Path from typing import List, Literal, Optional import yaml from transformers import HfArgumentParser, TrainingArguments from transformers.hf_argparser import make_choice_type_function, string_to_bool # Since Python 3.10, we can use the builtin `|` operator for Union types # See PEP 604: https://peps.python.org/pep-0604 lowercase_ = sys.version_info >= (3, 1_0) def lowercase ( lowerCAmelCase__ : List[str]=None , lowerCAmelCase__ : Dict=None ) -> str: return field(default_factory=lambda: default , metadata=lowerCAmelCase__ ) @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : int __UpperCAmelCase : float __UpperCAmelCase : str __UpperCAmelCase : bool @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : int = 4_2 __UpperCAmelCase : str = field(default='toto' , metadata={'help': 'help message'} ) @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : bool = False __UpperCAmelCase : bool = True __UpperCAmelCase : Optional[bool] = None class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Tuple = 'titi' __UpperCAmelCase : Optional[int] = 'toto' class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Optional[int] = 'titi' __UpperCAmelCase : Union[str, Any] = 'toto' __UpperCAmelCase : Union[str, Any] = 4_2 @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : BasicEnum = "toto" def __UpperCAmelCase ( self ): __a = BasicEnum(self.foo ) @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : MixedTypeEnum = "toto" def __UpperCAmelCase ( self ): __a = MixedTypeEnum(self.foo ) @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : Optional[int] = None __UpperCAmelCase : Optional[float] = field(default=__SCREAMING_SNAKE_CASE , metadata={'help': 'help message'} ) __UpperCAmelCase : Optional[str] = None __UpperCAmelCase : Optional[List[str]] = list_field(default=[] ) __UpperCAmelCase : Optional[List[int]] = list_field(default=[] ) @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : List[int] = list_field(default=[] ) __UpperCAmelCase : List[int] = list_field(default=[1, 2, 3] ) __UpperCAmelCase : List[str] = list_field(default=['Hallo', 'Bonjour', 'Hello'] ) __UpperCAmelCase : List[float] = list_field(default=[0.1, 0.2, 0.3] ) @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : List[int] = field() __UpperCAmelCase : str = field() __UpperCAmelCase : BasicEnum = field() def __UpperCAmelCase ( self ): __a = BasicEnum(self.required_enum ) @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : int __UpperCAmelCase : "BasicEnum" = field() __UpperCAmelCase : "Optional[bool]" = None __UpperCAmelCase : "str" = field(default='toto' , metadata={'help': 'help message'} ) __UpperCAmelCase : "List[str]" = list_field(default=['Hallo', 'Bonjour', 'Hello'] ) if is_python_no_less_than_3_10: @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : bool = False __UpperCAmelCase : bool = True __UpperCAmelCase : bool | None = None @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : int | None = None __UpperCAmelCase : float | None = field(default=__SCREAMING_SNAKE_CASE , metadata={'help': 'help message'} ) __UpperCAmelCase : str | None = None __UpperCAmelCase : list[str] | None = list_field(default=[] ) __UpperCAmelCase : list[int] | None = list_field(default=[] ) class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self , _a , _a ): self.assertEqual(len(a._actions ) , len(b._actions ) ) for x, y in zip(a._actions , b._actions ): __a = {k: v for k, v in vars(_a ).items() if k != '''container'''} __a = {k: v for k, v in vars(_a ).items() if k != '''container'''} # Choices with mixed type have custom function as "type" # So we need to compare results directly for equality if xx.get('''choices''' , _a ) and yy.get('''choices''' , _a ): for expected_choice in yy["choices"] + xx["choices"]: self.assertEqual(xx['''type'''](_a ) , yy['''type'''](_a ) ) del xx["type"], yy["type"] self.assertEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = HfArgumentParser(_a ) __a = argparse.ArgumentParser() expected.add_argument('''--foo''' , type=_a , required=_a ) expected.add_argument('''--bar''' , type=_a , required=_a ) expected.add_argument('''--baz''' , type=_a , required=_a ) expected.add_argument('''--flag''' , type=_a , default=_a , const=_a , nargs='''?''' ) self.argparsersEqual(_a , _a ) __a = ['''--foo''', '''1''', '''--baz''', '''quux''', '''--bar''', '''0.5'''] ((__a) , ) = parser.parse_args_into_dataclasses(_a , look_for_args_file=_a ) self.assertFalse(example.flag ) def __UpperCAmelCase ( self ): __a = HfArgumentParser(_a ) __a = argparse.ArgumentParser() expected.add_argument('''--foo''' , default=42 , type=_a ) expected.add_argument('''--baz''' , default='''toto''' , type=_a , help='''help message''' ) self.argparsersEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = argparse.ArgumentParser() expected.add_argument('''--foo''' , type=_a , default=_a , const=_a , nargs='''?''' ) expected.add_argument('''--baz''' , type=_a , default=_a , const=_a , nargs='''?''' ) # A boolean no_* argument always has to come after its "default: True" regular counter-part # and its default must be set to False expected.add_argument('''--no_baz''' , action='''store_false''' , default=_a , dest='''baz''' ) expected.add_argument('''--opt''' , type=_a , default=_a ) __a = [WithDefaultBoolExample] if is_python_no_less_than_3_10: dataclass_types.append(_a ) for dataclass_type in dataclass_types: __a = HfArgumentParser(_a ) self.argparsersEqual(_a , _a ) __a = parser.parse_args([] ) self.assertEqual(_a , Namespace(foo=_a , baz=_a , opt=_a ) ) __a = parser.parse_args(['''--foo''', '''--no_baz'''] ) self.assertEqual(_a , Namespace(foo=_a , baz=_a , opt=_a ) ) __a = parser.parse_args(['''--foo''', '''--baz'''] ) self.assertEqual(_a , Namespace(foo=_a , baz=_a , opt=_a ) ) __a = parser.parse_args(['''--foo''', '''True''', '''--baz''', '''True''', '''--opt''', '''True'''] ) self.assertEqual(_a , Namespace(foo=_a , baz=_a , opt=_a ) ) __a = parser.parse_args(['''--foo''', '''False''', '''--baz''', '''False''', '''--opt''', '''False'''] ) self.assertEqual(_a , Namespace(foo=_a , baz=_a , opt=_a ) ) def __UpperCAmelCase ( self ): __a = HfArgumentParser(_a ) __a = argparse.ArgumentParser() expected.add_argument( '''--foo''' , default='''toto''' , choices=['''titi''', '''toto''', 42] , type=make_choice_type_function(['''titi''', '''toto''', 42] ) , ) self.argparsersEqual(_a , _a ) __a = parser.parse_args([] ) self.assertEqual(args.foo , '''toto''' ) __a = parser.parse_args_into_dataclasses([] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.toto ) __a = parser.parse_args(['''--foo''', '''titi'''] ) self.assertEqual(args.foo , '''titi''' ) __a = parser.parse_args_into_dataclasses(['''--foo''', '''titi'''] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.titi ) __a = parser.parse_args(['''--foo''', '''42'''] ) self.assertEqual(args.foo , 42 ) __a = parser.parse_args_into_dataclasses(['''--foo''', '''42'''] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.fourtytwo ) def __UpperCAmelCase ( self ): @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : Literal["titi", "toto", 4_2] = "toto" __a = HfArgumentParser(_a ) __a = argparse.ArgumentParser() expected.add_argument( '''--foo''' , default='''toto''' , choices=('''titi''', '''toto''', 42) , type=make_choice_type_function(['''titi''', '''toto''', 42] ) , ) self.argparsersEqual(_a , _a ) __a = parser.parse_args([] ) self.assertEqual(args.foo , '''toto''' ) __a = parser.parse_args(['''--foo''', '''titi'''] ) self.assertEqual(args.foo , '''titi''' ) __a = parser.parse_args(['''--foo''', '''42'''] ) self.assertEqual(args.foo , 42 ) def __UpperCAmelCase ( self ): __a = HfArgumentParser(_a ) __a = argparse.ArgumentParser() expected.add_argument('''--foo_int''' , nargs='''+''' , default=[] , type=_a ) expected.add_argument('''--bar_int''' , nargs='''+''' , default=[1, 2, 3] , type=_a ) expected.add_argument('''--foo_str''' , nargs='''+''' , default=['''Hallo''', '''Bonjour''', '''Hello'''] , type=_a ) expected.add_argument('''--foo_float''' , nargs='''+''' , default=[0.1, 0.2, 0.3] , type=_a ) self.argparsersEqual(_a , _a ) __a = parser.parse_args([] ) self.assertEqual( _a , Namespace(foo_int=[] , bar_int=[1, 2, 3] , foo_str=['''Hallo''', '''Bonjour''', '''Hello'''] , foo_float=[0.1, 0.2, 0.3] ) , ) __a = parser.parse_args('''--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7'''.split() ) self.assertEqual(_a , Namespace(foo_int=[1] , bar_int=[2, 3] , foo_str=['''a''', '''b''', '''c'''] , foo_float=[0.1, 0.7] ) ) def __UpperCAmelCase ( self ): __a = argparse.ArgumentParser() expected.add_argument('''--foo''' , default=_a , type=_a ) expected.add_argument('''--bar''' , default=_a , type=_a , help='''help message''' ) expected.add_argument('''--baz''' , default=_a , type=_a ) expected.add_argument('''--ces''' , nargs='''+''' , default=[] , type=_a ) expected.add_argument('''--des''' , nargs='''+''' , default=[] , type=_a ) __a = [OptionalExample] if is_python_no_less_than_3_10: dataclass_types.append(_a ) for dataclass_type in dataclass_types: __a = HfArgumentParser(_a ) self.argparsersEqual(_a , _a ) __a = parser.parse_args([] ) self.assertEqual(_a , Namespace(foo=_a , bar=_a , baz=_a , ces=[] , des=[] ) ) __a = parser.parse_args('''--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3'''.split() ) self.assertEqual(_a , Namespace(foo=12 , bar=3.14 , baz='''42''' , ces=['''a''', '''b''', '''c'''] , des=[1, 2, 3] ) ) def __UpperCAmelCase ( self ): __a = HfArgumentParser(_a ) __a = argparse.ArgumentParser() expected.add_argument('''--required_list''' , nargs='''+''' , type=_a , required=_a ) expected.add_argument('''--required_str''' , type=_a , required=_a ) expected.add_argument( '''--required_enum''' , type=make_choice_type_function(['''titi''', '''toto'''] ) , choices=['''titi''', '''toto'''] , required=_a , ) self.argparsersEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = HfArgumentParser(_a ) __a = argparse.ArgumentParser() expected.add_argument('''--foo''' , type=_a , required=_a ) expected.add_argument( '''--required_enum''' , type=make_choice_type_function(['''titi''', '''toto'''] ) , choices=['''titi''', '''toto'''] , required=_a , ) expected.add_argument('''--opt''' , type=_a , default=_a ) expected.add_argument('''--baz''' , default='''toto''' , type=_a , help='''help message''' ) expected.add_argument('''--foo_str''' , nargs='''+''' , default=['''Hallo''', '''Bonjour''', '''Hello'''] , type=_a ) self.argparsersEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = HfArgumentParser(_a ) __a = { '''foo''': 12, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, } __a = parser.parse_dict(_a )[0] __a = BasicExample(**_a ) self.assertEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = HfArgumentParser(_a ) __a = { '''foo''': 12, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, '''extra''': 42, } self.assertRaises(_a , parser.parse_dict , _a , allow_extra_keys=_a ) def __UpperCAmelCase ( self ): __a = HfArgumentParser(_a ) __a = { '''foo''': 12, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, } with tempfile.TemporaryDirectory() as tmp_dir: __a = os.path.join(_a , '''temp_json''' ) os.mkdir(_a ) with open(temp_local_path + '''.json''' , '''w+''' ) as f: json.dump(_a , _a ) __a = parser.parse_yaml_file(Path(temp_local_path + '''.json''' ) )[0] __a = BasicExample(**_a ) self.assertEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = HfArgumentParser(_a ) __a = { '''foo''': 12, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, } with tempfile.TemporaryDirectory() as tmp_dir: __a = os.path.join(_a , '''temp_yaml''' ) os.mkdir(_a ) with open(temp_local_path + '''.yaml''' , '''w+''' ) as f: yaml.dump(_a , _a ) __a = parser.parse_yaml_file(Path(temp_local_path + '''.yaml''' ) )[0] __a = BasicExample(**_a ) self.assertEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = HfArgumentParser(_a ) self.assertIsNotNone(_a )
695
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType lowercase_ = logging.get_logger(__name__) lowercase_ = { "microsoft/deberta-v2-xlarge": "https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json", "microsoft/deberta-v2-xxlarge": "https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json", "microsoft/deberta-v2-xlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json" ), "microsoft/deberta-v2-xxlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json" ), } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Dict = 'deberta-v2' def __init__( self , _a=128_100 , _a=1_536 , _a=24 , _a=24 , _a=6_144 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=0 , _a=0.02 , _a=1E-7 , _a=False , _a=-1 , _a=0 , _a=True , _a=None , _a=0 , _a="gelu" , **_a , ): super().__init__(**_a ) __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = initializer_range __a = relative_attention __a = max_relative_positions __a = pad_token_id __a = position_biased_input # Backwards compatibility if type(_a ) == str: __a = [x.strip() for x in pos_att_type.lower().split('''|''' )] __a = pos_att_type __a = vocab_size __a = layer_norm_eps __a = kwargs.get('''pooler_hidden_size''' , _a ) __a = pooler_dropout __a = pooler_hidden_act class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def __UpperCAmelCase ( self ): if self.task == "multiple-choice": __a = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: __a = {0: '''batch''', 1: '''sequence'''} if self._config.type_vocab_size > 0: return OrderedDict( [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)] ) else: return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)] ) @property def __UpperCAmelCase ( self ): return 12 def __UpperCAmelCase ( self , _a , _a = -1 , _a = -1 , _a = -1 , _a = False , _a = None , _a = 3 , _a = 40 , _a = 40 , _a = None , ): __a = super().generate_dummy_inputs(preprocessor=_a , framework=_a ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
695
1
"""simple docstring""" from scipy.stats import pearsonr import datasets lowercase_ = "\nPearson correlation coefficient and p-value for testing non-correlation.\nThe Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases.\nThe p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets.\n" lowercase_ = "\nArgs:\n predictions (`list` of `int`): Predicted class labels, as returned by a model.\n references (`list` of `int`): Ground truth labels.\n return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`.\n\nReturns:\n pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation.\n p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities.\n\nExamples:\n\n Example 1-A simple example using only predictions and references.\n >>> pearsonr_metric = datasets.load_metric(\"pearsonr\")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5])\n >>> print(round(results['pearsonr'], 2))\n -0.74\n\n Example 2-The same as Example 1, but that also returns the `p-value`.\n >>> pearsonr_metric = datasets.load_metric(\"pearsonr\")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True)\n >>> print(sorted(list(results.keys())))\n ['p-value', 'pearsonr']\n >>> print(round(results['pearsonr'], 2))\n -0.74\n >>> print(round(results['p-value'], 2))\n 0.15\n" lowercase_ = "\n@article{2020SciPy-NMeth,\nauthor = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and\n Haberland, Matt and Reddy, Tyler and Cournapeau, David and\n Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and\n Bright, Jonathan and {van der Walt}, St{\'e}fan J. and\n Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and\n Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and\n Kern, Robert and Larson, Eric and Carey, C J and\n Polat, Ilhan and Feng, Yu and Moore, Eric W. and\n {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and\n Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and\n Harris, Charles R. and Archibald, Anne M. and\n Ribeiro, Antonio H. and Pedregosa, Fabian and\n {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},\ntitle = {{{SciPy} 1.0: Fundamental Algorithms for Scientific\n Computing in Python}},\njournal = {Nature Methods},\nyear = {2020},\nvolume = {17},\npages = {261--272},\nadsurl = {https://rdcu.be/b08Wh},\ndoi = {10.1038/s41592-019-0686-2},\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __lowerCAmelCase ( datasets.Metric ): '''simple docstring''' def __UpperCAmelCase ( self ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''float''' ), '''references''': datasets.Value('''float''' ), } ) , reference_urls=['''https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html'''] , ) def __UpperCAmelCase ( self , _a , _a , _a=False ): if return_pvalue: __a = pearsonr(_a , _a ) return {"pearsonr": results[0], "p-value": results[1]} else: return {"pearsonr": float(pearsonr(_a , _a )[0] )}
695
"""simple docstring""" import importlib.metadata import operator import re import sys from typing import Optional from packaging import version lowercase_ = { "<": operator.lt, "<=": operator.le, "==": operator.eq, "!=": operator.ne, ">=": operator.ge, ">": operator.gt, } def lowercase ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any] ) -> Dict: 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(lowerCAmelCase__ ) , version.parse(lowerCAmelCase__ ) ): raise ImportError( f'''{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}''' ) def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None ) -> None: __a = f'''\n{hint}''' if hint is not None else '''''' # non-versioned check if re.match(r'''^[\w_\-\d]+$''' , lowerCAmelCase__ ): __a , __a , __a = requirement, None, None else: __a = re.findall(r'''^([^!=<>\s]+)([\s!=<>]{1,2}.+)''' , lowerCAmelCase__ ) if not match: raise ValueError( '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but''' f''' got {requirement}''' ) __a , __a = match[0] __a = want_full.split(''',''' ) # there could be multiple requirements __a = {} for w in want_range: __a = re.findall(r'''^([\s!=<>]{1,2})(.+)''' , lowerCAmelCase__ ) if not match: raise ValueError( '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,''' f''' but got {requirement}''' ) __a , __a = match[0] __a = want_ver if op not in ops: raise ValueError(f'''{requirement}: need one of {list(ops.keys() )}, but got {op}''' ) # special case if pkg == "python": __a = '''.'''.join([str(lowerCAmelCase__ ) for x in sys.version_info[:3]] ) for op, want_ver in wanted.items(): _compare_versions(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) return # check if any version is installed try: __a = importlib.metadata.version(lowerCAmelCase__ ) 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(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Tuple ) -> Optional[Any]: __a = '''Try: pip install transformers -U or pip install -e \'.[dev]\' if you\'re working with git main''' return require_version(lowerCAmelCase__ , lowerCAmelCase__ )
695
1
"""simple docstring""" from math import factorial class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a ): __a = real if isinstance(_a , _a ): __a = [1] * rank else: __a = rank def __repr__( self ): return ( f'''{self.real}+''' f'''{'+'.join(str(_a )+'E'+str(n+1 )for n,dual in enumerate(self.duals ) )}''' ) def __UpperCAmelCase ( self ): __a = self.duals.copy() while cur[-1] == 0: cur.pop(-1 ) return Dual(self.real , _a ) def __add__( self , _a ): if not isinstance(_a , _a ): return Dual(self.real + other , self.duals ) __a = self.duals.copy() __a = other.duals.copy() if len(_a ) > len(_a ): o_dual.extend([1] * (len(_a ) - len(_a )) ) elif len(_a ) < len(_a ): s_dual.extend([1] * (len(_a ) - len(_a )) ) __a = [] for i in range(len(_a ) ): new_duals.append(s_dual[i] + o_dual[i] ) return Dual(self.real + other.real , _a ) __UpperCAmelCase : Any = __add__ def __sub__( self , _a ): return self + other * -1 def __mul__( self , _a ): if not isinstance(_a , _a ): __a = [] for i in self.duals: new_duals.append(i * other ) return Dual(self.real * other , _a ) __a = [0] * (len(self.duals ) + len(other.duals ) + 1) for i, item in enumerate(self.duals ): for j, jtem in enumerate(other.duals ): new_duals[i + j + 1] += item * jtem for k in range(len(self.duals ) ): new_duals[k] += self.duals[k] * other.real for index in range(len(other.duals ) ): new_duals[index] += other.duals[index] * self.real return Dual(self.real * other.real , _a ) __UpperCAmelCase : Union[str, Any] = __mul__ def __truediv__( self , _a ): if not isinstance(_a , _a ): __a = [] for i in self.duals: new_duals.append(i / other ) return Dual(self.real / other , _a ) raise ValueError def __floordiv__( self , _a ): if not isinstance(_a , _a ): __a = [] for i in self.duals: new_duals.append(i // other ) return Dual(self.real // other , _a ) raise ValueError def __pow__( self , _a ): if n < 0 or isinstance(_a , _a ): raise ValueError('''power must be a positive integer''' ) if n == 0: return 1 if n == 1: return self __a = self for _ in range(n - 1 ): x *= self return x def lowercase ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Optional[int] ) -> Any: if not callable(lowerCAmelCase__ ): raise ValueError('''differentiate() requires a function as input for func''' ) if not isinstance(lowerCAmelCase__ , (float, int) ): raise ValueError('''differentiate() requires a float as input for position''' ) if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise ValueError('''differentiate() requires an int as input for order''' ) __a = Dual(lowerCAmelCase__ , 1 ) __a = func(lowerCAmelCase__ ) if order == 0: return result.real return result.duals[order - 1] * factorial(lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod() def lowercase ( lowerCAmelCase__ : int ) -> int: return y**2 * y**4 print(differentiate(f, 9, 2))
695
"""simple docstring""" from __future__ import annotations lowercase_ = list[tuple[int, int]] lowercase_ = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] lowercase_ = ([-1, 0], [0, -1], [1, 0], [0, 1]) # up, left, down, right class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a , _a , _a , _a , _a , ): __a = pos_x __a = pos_y __a = (pos_y, pos_x) __a = goal_x __a = goal_y __a = g_cost __a = parent __a = self.calculate_heuristic() def __UpperCAmelCase ( self ): __a = abs(self.pos_x - self.goal_x ) __a = abs(self.pos_y - self.goal_y ) return dx + dy def __lt__( self , _a ): return self.f_cost < other.f_cost class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a ): __a = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , _a ) __a = Node(goal[1] , goal[0] , goal[1] , goal[0] , 99_999 , _a ) __a = [self.start] __a = [] __a = False def __UpperCAmelCase ( self ): while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() __a = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: __a = True return self.retrace_path(_a ) self.closed_nodes.append(_a ) __a = self.get_successors(_a ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(_a ) else: # retrieve the best current path __a = self.open_nodes.pop(self.open_nodes.index(_a ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(_a ) else: self.open_nodes.append(_a ) if not self.reached: return [self.start.pos] return None def __UpperCAmelCase ( self , _a ): __a = [] for action in delta: __a = parent.pos_x + action[1] __a = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(_a ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( _a , _a , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , _a , ) ) return successors def __UpperCAmelCase ( self , _a ): __a = node __a = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) __a = current_node.parent path.reverse() return path if __name__ == "__main__": lowercase_ = (0, 0) lowercase_ = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) print("------") lowercase_ = GreedyBestFirst(init, goal) lowercase_ = greedy_bf.search() if path: for pos_x, pos_y in path: lowercase_ = 2 for elem in grid: print(elem)
695
1
"""simple docstring""" from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=__SCREAMING_SNAKE_CASE ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : str = field(default='language-modeling' , metadata={'include_in_asdict_even_if_is_default': True} ) __UpperCAmelCase : ClassVar[Features] = Features({'text': Value('string' )} ) __UpperCAmelCase : ClassVar[Features] = Features({} ) __UpperCAmelCase : str = "text" @property def __UpperCAmelCase ( self ): return {self.text_column: "text"}
695
"""simple docstring""" import argparse import torch from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert from transformers.utils import logging logging.set_verbosity_info() def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str ) -> List[Any]: # Initialise PyTorch model __a = RemBertConfig.from_json_file(lowerCAmelCase__ ) print('''Building PyTorch model from configuration: {}'''.format(str(lowerCAmelCase__ ) ) ) __a = RemBertModel(lowerCAmelCase__ ) # Load weights from tf checkpoint load_tf_weights_in_rembert(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Save pytorch-model print('''Save PyTorch model to {}'''.format(lowerCAmelCase__ ) ) torch.save(model.state_dict() , lowerCAmelCase__ ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--rembert_config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained RemBERT 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." ) lowercase_ = parser.parse_args() convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)
695
1
"""simple docstring""" from __future__ import annotations import math import numpy as np from numpy.linalg import norm def lowercase ( lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : np.ndarray ) -> float: return math.sqrt(sum(pow(a - b , 2 ) for a, b in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ) ) def lowercase ( lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : np.ndarray ) -> list[list[list[float] | float]]: if dataset.ndim != value_array.ndim: __a = ( '''Wrong input data\'s dimensions... ''' f'''dataset : {dataset.ndim}, value_array : {value_array.ndim}''' ) raise ValueError(lowerCAmelCase__ ) try: if dataset.shape[1] != value_array.shape[1]: __a = ( '''Wrong input data\'s shape... ''' f'''dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}''' ) raise ValueError(lowerCAmelCase__ ) except IndexError: if dataset.ndim != value_array.ndim: raise TypeError('''Wrong shape''' ) if dataset.dtype != value_array.dtype: __a = ( '''Input data have different datatype... ''' f'''dataset : {dataset.dtype}, value_array : {value_array.dtype}''' ) raise TypeError(lowerCAmelCase__ ) __a = [] for value in value_array: __a = euclidean(lowerCAmelCase__ , dataset[0] ) __a = dataset[0].tolist() for dataset_value in dataset[1:]: __a = euclidean(lowerCAmelCase__ , lowerCAmelCase__ ) if dist > temp_dist: __a = temp_dist __a = dataset_value.tolist() answer.append([vector, dist] ) return answer def lowercase ( lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : np.ndarray ) -> float: return np.dot(lowerCAmelCase__ , lowerCAmelCase__ ) / (norm(lowerCAmelCase__ ) * norm(lowerCAmelCase__ )) if __name__ == "__main__": import doctest doctest.testmod()
695
"""simple docstring""" import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel lowercase_ = "0.12" # assumed parallelism: 8 @require_flax @is_staging_test class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @classmethod def __UpperCAmelCase ( cls ): __a = TOKEN HfFolder.save_token(_a ) @classmethod def __UpperCAmelCase ( cls ): try: delete_repo(token=cls._token , repo_id='''test-model-flax''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-model-flax-org''' ) except HTTPError: pass def __UpperCAmelCase ( self ): __a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) __a = FlaxBertModel(_a ) model.push_to_hub('''test-model-flax''' , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''test-model-flax''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(_a , repo_id='''test-model-flax''' , push_to_hub=_a , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) def __UpperCAmelCase ( self ): __a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) __a = FlaxBertModel(_a ) model.push_to_hub('''valid_org/test-model-flax-org''' , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-model-flax-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( _a , repo_id='''valid_org/test-model-flax-org''' , push_to_hub=_a , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : Dict ) -> Optional[int]: __a = True __a = flatten_dict(modela.params ) __a = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1e-4: __a = False return models_are_equal @require_flax class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) __a = FlaxBertModel(_a ) __a = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_a , _a ) ) with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertTrue(check_models_equal(_a , _a ) ) def __UpperCAmelCase ( self ): __a = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) __a = FlaxBertModel(_a ) __a = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_a , _a ) , max_shard_size='''10KB''' ) with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertTrue(check_models_equal(_a , _a ) ) def __UpperCAmelCase ( self ): __a = '''bert''' __a = '''hf-internal-testing/tiny-random-bert-subfolder''' with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertIsNotNone(_a ) def __UpperCAmelCase ( self ): __a = '''bert''' __a = '''hf-internal-testing/tiny-random-bert-sharded-subfolder''' with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertIsNotNone(_a )
695
1
"""simple docstring""" import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , _a , _a , _a , _a , ): super().__init__() __a = value_function __a = unet __a = scheduler __a = env __a = env.get_dataset() __a = {} for key in self.data.keys(): try: __a = self.data[key].mean() except: # noqa: E722 pass __a = {} for key in self.data.keys(): try: __a = self.data[key].std() except: # noqa: E722 pass __a = env.observation_space.shape[0] __a = env.action_space.shape[0] def __UpperCAmelCase ( self , _a , _a ): return (x_in - self.means[key]) / self.stds[key] def __UpperCAmelCase ( self , _a , _a ): return x_in * self.stds[key] + self.means[key] def __UpperCAmelCase ( self , _a ): if type(_a ) is dict: return {k: self.to_torch(_a ) for k, v in x_in.items()} elif torch.is_tensor(_a ): return x_in.to(self.unet.device ) return torch.tensor(_a , device=self.unet.device ) def __UpperCAmelCase ( self , _a , _a , _a ): for key, val in cond.items(): __a = val.clone() return x_in def __UpperCAmelCase ( self , _a , _a , _a , _a ): __a = x.shape[0] __a = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model __a = torch.full((batch_size,) , _a , device=self.unet.device , dtype=torch.long ) for _ in range(_a ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models __a = self.value_function(x.permute(0 , 2 , 1 ) , _a ).sample __a = torch.autograd.grad([y.sum()] , [x] )[0] __a = self.scheduler._get_variance(_a ) __a = torch.exp(0.5 * posterior_variance ) __a = model_std * grad __a = 0 __a = x.detach() __a = x + scale * grad __a = self.reset_xa(_a , _a , self.action_dim ) __a = self.unet(x.permute(0 , 2 , 1 ) , _a ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg __a = self.scheduler.step(_a , _a , _a , predict_epsilon=_a )['''prev_sample'''] # apply conditions to the trajectory (set the initial state) __a = self.reset_xa(_a , _a , self.action_dim ) __a = self.to_torch(_a ) return x, y def __call__( self , _a , _a=64 , _a=32 , _a=2 , _a=0.1 ): # normalize the observations and create batch dimension __a = self.normalize(_a , '''observations''' ) __a = obs[None].repeat(_a , axis=0 ) __a = {0: self.to_torch(_a )} __a = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) __a = randn_tensor(_a , device=self.unet.device ) __a = self.reset_xa(_a , _a , self.action_dim ) __a = self.to_torch(_a ) # run the diffusion process __a , __a = self.run_diffusion(_a , _a , _a , _a ) # sort output trajectories by value __a = y.argsort(0 , descending=_a ).squeeze() __a = x[sorted_idx] __a = sorted_values[:, :, : self.action_dim] __a = actions.detach().cpu().numpy() __a = self.de_normalize(_a , key='''actions''' ) # select the action with the highest value if y is not None: __a = 0 else: # if we didn't run value guiding, select a random action __a = np.random.randint(0 , _a ) __a = denorm_actions[selected_index, 0] return denorm_actions
695
"""simple docstring""" import unittest from diffusers.models.unet_ad_blocks import * # noqa F403 from diffusers.utils import torch_device from .test_unet_blocks_common import UNetBlockTesterMixin class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = DownBlockaD # noqa F405 __UpperCAmelCase : Any = 'down' def __UpperCAmelCase ( self ): __a = [-0.0232, -0.9869, 0.8054, -0.0637, -0.1688, -1.4264, 0.4470, -1.3394, 0.0904] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = ResnetDownsampleBlockaD # noqa F405 __UpperCAmelCase : List[str] = 'down' def __UpperCAmelCase ( self ): __a = [0.0710, 0.2410, -0.7320, -1.0757, -1.1343, 0.3540, -0.0133, -0.2576, 0.0948] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[int] = AttnDownBlockaD # noqa F405 __UpperCAmelCase : Optional[Any] = 'down' def __UpperCAmelCase ( self ): __a = [0.0636, 0.8964, -0.6234, -1.0131, 0.0844, 0.4935, 0.3437, 0.0911, -0.2957] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : List[Any] = CrossAttnDownBlockaD # noqa F405 __UpperCAmelCase : Optional[Any] = 'down' def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.2238, -0.7396, -0.2255, -0.3829, 0.1925, 1.1665, 0.0603, -0.7295, 0.1983] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = SimpleCrossAttnDownBlockaD # noqa F405 __UpperCAmelCase : Any = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_encoder_hidden_states=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict @unittest.skipIf(torch_device == '''mps''' , '''MPS result is not consistent''' ) def __UpperCAmelCase ( self ): __a = [0.7921, -0.0992, -0.1962, -0.7695, -0.4242, 0.7804, 0.4737, 0.2765, 0.3338] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = SkipDownBlockaD # noqa F405 __UpperCAmelCase : Tuple = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_skip_sample=_a ) def __UpperCAmelCase ( self ): __a = [-0.0845, -0.2087, -0.2465, 0.0971, 0.1900, -0.0484, 0.2664, 0.4179, 0.5069] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : List[Any] = AttnSkipDownBlockaD # noqa F405 __UpperCAmelCase : Optional[int] = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_skip_sample=_a ) def __UpperCAmelCase ( self ): __a = [0.5539, 0.1609, 0.4924, 0.0537, -0.1995, 0.4050, 0.0979, -0.2721, -0.0642] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = DownEncoderBlockaD # noqa F405 __UpperCAmelCase : Optional[int] = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = { '''in_channels''': 32, '''out_channels''': 32, } __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [1.1102, 0.5302, 0.4872, -0.0023, -0.8042, 0.0483, -0.3489, -0.5632, 0.7626] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = AttnDownEncoderBlockaD # noqa F405 __UpperCAmelCase : Any = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = { '''in_channels''': 32, '''out_channels''': 32, } __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.8966, -0.1486, 0.8568, 0.8141, -0.9046, -0.1342, -0.0972, -0.7417, 0.1538] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = UNetMidBlockaD # noqa F405 __UpperCAmelCase : Any = 'mid' def __UpperCAmelCase ( self ): __a = { '''in_channels''': 32, '''temb_channels''': 128, } __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [-0.1062, 1.7248, 0.3494, 1.4569, -0.0910, -1.2421, -0.9984, 0.6736, 1.0028] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = UNetMidBlockaDCrossAttn # noqa F405 __UpperCAmelCase : str = 'mid' def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.0187, 2.4220, 0.4484, 1.1203, -0.6121, -1.5122, -0.8270, 0.7851, 1.8335] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Any = UNetMidBlockaDSimpleCrossAttn # noqa F405 __UpperCAmelCase : List[Any] = 'mid' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_encoder_hidden_states=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.7143, 1.9974, 0.5448, 1.3977, 0.1282, -1.1237, -1.4238, 0.5530, 0.8880] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = UpBlockaD # noqa F405 __UpperCAmelCase : Union[str, Any] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [-0.2041, -0.4165, -0.3022, 0.0041, -0.6628, -0.7053, 0.1928, -0.0325, 0.0523] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = ResnetUpsampleBlockaD # noqa F405 __UpperCAmelCase : int = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [0.2287, 0.3549, -0.1346, 0.4797, -0.1715, -0.9649, 0.7305, -0.5864, -0.6244] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Dict = CrossAttnUpBlockaD # noqa F405 __UpperCAmelCase : List[Any] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [-0.1403, -0.3515, -0.0420, -0.1425, 0.3167, 0.5094, -0.2181, 0.5931, 0.5582] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = SimpleCrossAttnUpBlockaD # noqa F405 __UpperCAmelCase : Optional[int] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a , include_encoder_hidden_states=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.2645, 0.1480, 0.0909, 0.8044, -0.9758, -0.9083, 0.0994, -1.1453, -0.7402] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Any = AttnUpBlockaD # noqa F405 __UpperCAmelCase : List[Any] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) @unittest.skipIf(torch_device == '''mps''' , '''MPS result is not consistent''' ) def __UpperCAmelCase ( self ): __a = [0.0979, 0.1326, 0.0021, 0.0659, 0.2249, 0.0059, 0.1132, 0.5952, 0.1033] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Any = SkipUpBlockaD # noqa F405 __UpperCAmelCase : str = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [-0.0893, -0.1234, -0.1506, -0.0332, 0.0123, -0.0211, 0.0566, 0.0143, 0.0362] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = AttnSkipUpBlockaD # noqa F405 __UpperCAmelCase : int = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [0.0361, 0.0617, 0.2787, -0.0350, 0.0342, 0.3421, -0.0843, 0.0913, 0.3015] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = UpDecoderBlockaD # noqa F405 __UpperCAmelCase : List[str] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = {'''in_channels''': 32, '''out_channels''': 32} __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.4404, 0.1998, -0.9886, -0.3320, -0.3128, -0.7034, -0.6955, -0.2338, -0.3137] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[int] = AttnUpDecoderBlockaD # noqa F405 __UpperCAmelCase : Any = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = {'''in_channels''': 32, '''out_channels''': 32} __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.6738, 0.4491, 0.1055, 1.0710, 0.7316, 0.3339, 0.3352, 0.1023, 0.3568] super().test_output(_a )
695
1
"""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 __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : int = 0 __UpperCAmelCase : bool = False __UpperCAmelCase : float = 3.0 class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): # 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=_a ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'''a''': 2, '''c''': 2.25} ) @require_cuda def __UpperCAmelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. __a = GradScalerKwargs(init_scale=1_024 , growth_factor=2 ) AcceleratorState._reset_state() __a = Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) __a = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2_000 ) self.assertEqual(scaler._enabled , _a ) @require_multi_gpu def __UpperCAmelCase ( self ): __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": lowercase_ = DistributedDataParallelKwargs(bucket_cap_mb=1_5, find_unused_parameters=True) lowercase_ = Accelerator(kwargs_handlers=[ddp_scaler]) lowercase_ = torch.nn.Linear(1_0_0, 2_0_0) lowercase_ = accelerator.prepare(model) # Check the values changed in kwargs lowercase_ = "" lowercase_ = model.bucket_bytes_cap // (1_0_2_4 * 1_0_2_4) if observed_bucket_cap_map != 1_5: 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)
695
"""simple docstring""" import copy from typing import Dict, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING from ..detr import DetrConfig from ..swin import SwinConfig lowercase_ = { "facebook/maskformer-swin-base-ade": ( "https://huggingface.co/facebook/maskformer-swin-base-ade/blob/main/config.json" ) # See all MaskFormer models at https://huggingface.co/models?filter=maskformer } lowercase_ = logging.get_logger(__name__) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : str = 'maskformer' __UpperCAmelCase : Optional[int] = {'hidden_size': 'mask_feature_size'} __UpperCAmelCase : Any = ['resnet', 'swin'] __UpperCAmelCase : Dict = ['detr'] def __init__( self , _a = 256 , _a = 256 , _a = 0.1 , _a = False , _a = None , _a = None , _a = 0.02 , _a = 1.0 , _a = 1.0 , _a = 1.0 , _a = 20.0 , _a = None , **_a , ): if backbone_config is None: # fall back to https://huggingface.co/microsoft/swin-base-patch4-window12-384-in22k __a = SwinConfig( image_size=384 , in_channels=3 , patch_size=4 , embed_dim=128 , depths=[2, 2, 18, 2] , num_heads=[4, 8, 16, 32] , window_size=12 , drop_path_rate=0.3 , out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] , ) if isinstance(_a , _a ): __a = backbone_config.pop('''model_type''' ) __a = CONFIG_MAPPING[backbone_model_type] __a = config_class.from_dict(_a ) # verify that the backbone is supported if backbone_config.model_type not in self.backbones_supported: logger.warning_once( f'''Backbone {backbone_config.model_type} is not a supported model and may not be compatible with MaskFormer. ''' f'''Supported model types: {','.join(self.backbones_supported )}''' ) if decoder_config is None: # fall back to https://huggingface.co/facebook/detr-resnet-50 __a = DetrConfig() else: # verify that the decoder is supported __a = ( decoder_config.pop('''model_type''' ) if isinstance(_a , _a ) else decoder_config.model_type ) if decoder_type not in self.decoders_supported: raise ValueError( f'''Transformer Decoder {decoder_type} not supported, please use one of''' f''' {','.join(self.decoders_supported )}''' ) if isinstance(_a , _a ): __a = CONFIG_MAPPING[decoder_type] __a = config_class.from_dict(_a ) __a = backbone_config __a = decoder_config # main feature dimension for the model __a = fpn_feature_size __a = mask_feature_size # initializer __a = init_std __a = init_xavier_std # Hungarian matcher && loss __a = cross_entropy_weight __a = dice_weight __a = mask_weight __a = use_auxiliary_loss __a = no_object_weight __a = output_auxiliary_logits __a = self.decoder_config.encoder_attention_heads __a = self.decoder_config.num_hidden_layers super().__init__(**_a ) @classmethod def __UpperCAmelCase ( cls , _a , _a , **_a ): return cls( backbone_config=_a , decoder_config=_a , **_a , ) def __UpperCAmelCase ( self ): __a = copy.deepcopy(self.__dict__ ) __a = self.backbone_config.to_dict() __a = self.decoder_config.to_dict() __a = self.__class__.model_type return output
695
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int ) -> int: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) or number < 0: raise ValueError('''Input must be a non-negative integer''' ) __a = 0 while number: # This way we arrive at next set bit (next 1) instead of looping # through each bit and checking for 1s hence the # loop won't run 32 times it will only run the number of `1` times number &= number - 1 count += 1 return count if __name__ == "__main__": import doctest doctest.testmod()
695
"""simple docstring""" from __future__ import annotations from collections.abc import Generator import requests from bsa import BeautifulSoup lowercase_ = "https://www.indeed.co.in/jobs?q=mobile+app+development&l=" def lowercase ( lowerCAmelCase__ : str = "mumbai" ) -> Generator[tuple[str, str], None, None]: __a = BeautifulSoup(requests.get(url + location ).content , '''html.parser''' ) # This attribute finds out all the specifics listed in a job for job in soup.find_all('''div''' , attrs={'''data-tn-component''': '''organicJob'''} ): __a = job.find('''a''' , attrs={'''data-tn-element''': '''jobTitle'''} ).text.strip() __a = job.find('''span''' , {'''class''': '''company'''} ).text.strip() yield job_title, company_name if __name__ == "__main__": for i, job in enumerate(fetch_jobs("Bangalore"), 1): print(F'''Job {i:>2} is {job[0]} at {job[1]}''')
695
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int = 10 , lowerCAmelCase__ : int = 1000 , lowerCAmelCase__ : bool = True ) -> int: assert ( isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) ), "Invalid type of value(s) specified to function!" if min_val > max_val: raise ValueError('''Invalid value for min_val or max_val (min_value < max_value)''' ) return min_val if option else max_val def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int ) -> int: return int((number_a + number_a) / 2 ) def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int ) -> None: assert ( isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) ), 'argument values must be type of "int"' if lower > higher: raise ValueError('''argument value for lower and higher must be(lower > higher)''' ) if not lower < to_guess < higher: raise ValueError( '''guess value must be within the range of lower and higher value''' ) def answer(lowerCAmelCase__ : int ) -> str: if number > to_guess: return "high" elif number < to_guess: return "low" else: return "same" print('''started...''' ) __a = lower __a = higher __a = [] while True: __a = get_avg(lowerCAmelCase__ , lowerCAmelCase__ ) last_numbers.append(lowerCAmelCase__ ) if answer(lowerCAmelCase__ ) == "low": __a = number elif answer(lowerCAmelCase__ ) == "high": __a = number else: break print(f'''guess the number : {last_numbers[-1]}''' ) print(f'''details : {last_numbers!s}''' ) def lowercase ( ) -> None: __a = int(input('''Enter lower value : ''' ).strip() ) __a = int(input('''Enter high value : ''' ).strip() ) __a = int(input('''Enter value to guess : ''' ).strip() ) guess_the_number(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": main()
695
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "bigcode/gpt_bigcode-santacoder": "https://huggingface.co/bigcode/gpt_bigcode-santacoder/resolve/main/config.json", } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = 'gpt_bigcode' __UpperCAmelCase : Tuple = ['past_key_values'] __UpperCAmelCase : Dict = { 'hidden_size': 'n_embd', 'max_position_embeddings': 'n_positions', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self , _a=50_257 , _a=1_024 , _a=768 , _a=12 , _a=12 , _a=None , _a="gelu_pytorch_tanh" , _a=0.1 , _a=0.1 , _a=0.1 , _a=1E-5 , _a=0.02 , _a=True , _a=True , _a=50_256 , _a=50_256 , _a=True , _a=True , _a=True , **_a , ): __a = vocab_size __a = n_positions __a = n_embd __a = n_layer __a = n_head __a = n_inner __a = activation_function __a = resid_pdrop __a = embd_pdrop __a = attn_pdrop __a = layer_norm_epsilon __a = initializer_range __a = scale_attn_weights __a = use_cache __a = attention_softmax_in_fpaa __a = scale_attention_softmax_in_fpaa __a = multi_query __a = bos_token_id __a = eos_token_id super().__init__(bos_token_id=_a , eos_token_id=_a , **_a )
695
1
"""simple docstring""" import os from glob import glob import imageio import torch import torchvision import wandb from img_processing import custom_to_pil, loop_post_process, preprocess, preprocess_vqgan from loaders import load_vqgan from PIL import Image from torch import nn from transformers import CLIPModel, CLIPTokenizerFast from utils import get_device, get_timestamp, show_pil class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a = "cpu" , _a = "openai/clip-vit-large-patch14" ): __a = device __a = CLIPTokenizerFast.from_pretrained(_a ) __a = [0.4814_5466, 0.457_8275, 0.4082_1073] __a = [0.2686_2954, 0.2613_0258, 0.2757_7711] __a = torchvision.transforms.Normalize(self.image_mean , self.image_std ) __a = torchvision.transforms.Resize(224 ) __a = torchvision.transforms.CenterCrop(224 ) def __UpperCAmelCase ( self , _a ): __a = self.resize(_a ) __a = self.center_crop(_a ) __a = self.normalize(_a ) return images def __call__( self , _a=None , _a=None , **_a ): __a = self.tokenizer(text=_a , **_a ) __a = self.preprocess_img(_a ) __a = {key: value.to(self.device ) for (key, value) in encoding.items()} return encoding class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a=10 , _a=0.01 , _a=None , _a=None , _a=None , _a=None , _a=None , _a=None , _a=False , _a=True , _a="image" , _a=True , _a=False , _a=False , _a=False , ): super().__init__() __a = None __a = device if device else get_device() if vqgan: __a = vqgan else: __a = load_vqgan(self.device , conf_path=_a , ckpt_path=_a ) self.vqgan.eval() if clip: __a = clip else: __a = CLIPModel.from_pretrained('''openai/clip-vit-base-patch32''' ) self.clip.to(self.device ) __a = ProcessorGradientFlow(device=self.device ) __a = iterations __a = lr __a = log __a = make_grid __a = return_val __a = quantize __a = self.vqgan.decoder.z_shape def __UpperCAmelCase ( self , _a=None , _a=None , _a=5 , _a=True ): __a = [] if output_path is None: __a = '''./animation.gif''' if input_path is None: __a = self.save_path __a = sorted(glob(input_path + '''/*''' ) ) if not len(_a ): raise ValueError( '''No images found in save path, aborting (did you pass save_intermediate=True to the generate''' ''' function?)''' ) if len(_a ) == 1: print('''Only one image found in save path, (did you pass save_intermediate=True to the generate function?)''' ) __a = total_duration / len(_a ) __a = [frame_duration] * len(_a ) if extend_frames: __a = 1.5 __a = 3 for file_name in paths: if file_name.endswith('''.png''' ): images.append(imageio.imread(_a ) ) imageio.mimsave(_a , _a , duration=_a ) print(f'''gif saved to {output_path}''' ) def __UpperCAmelCase ( self , _a=None , _a=None ): if not (path or img): raise ValueError('''Input either path or tensor''' ) if img is not None: raise NotImplementedError __a = preprocess(Image.open(_a ) , target_image_size=256 ).to(self.device ) __a = preprocess_vqgan(_a ) __a , *__a = self.vqgan.encode(_a ) return z def __UpperCAmelCase ( self , _a ): __a = self.latent.detach().requires_grad_() __a = base_latent + transform_vector if self.quantize: __a , *__a = self.vqgan.quantize(_a ) else: __a = trans_latent return self.vqgan.decode(_a ) def __UpperCAmelCase ( self , _a , _a , _a=None ): __a = self.clip_preprocessor(text=_a , images=_a , return_tensors='''pt''' , padding=_a ) __a = self.clip(**_a ) __a = clip_outputs.logits_per_image if weights is not None: __a = similarity_logits * weights return similarity_logits.sum() def __UpperCAmelCase ( self , _a , _a , _a ): __a = self._get_clip_similarity(pos_prompts['''prompts'''] , _a , weights=(1 / pos_prompts['''weights''']) ) if neg_prompts: __a = self._get_clip_similarity(neg_prompts['''prompts'''] , _a , weights=neg_prompts['''weights'''] ) else: __a = torch.tensor([1] , device=self.device ) __a = -torch.log(_a ) + torch.log(_a ) return loss def __UpperCAmelCase ( self , _a , _a , _a ): __a = torch.randn_like(self.latent , requires_grad=_a , device=self.device ) __a = torch.optim.Adam([vector] , lr=self.lr ) for i in range(self.iterations ): optim.zero_grad() __a = self._add_vector(_a ) __a = loop_post_process(_a ) __a = self._get_CLIP_loss(_a , _a , _a ) print('''CLIP loss''' , _a ) if self.log: wandb.log({'''CLIP Loss''': clip_loss} ) clip_loss.backward(retain_graph=_a ) optim.step() if self.return_val == "image": yield custom_to_pil(transformed_img[0] ) else: yield vector def __UpperCAmelCase ( self , _a , _a , _a ): wandb.init(reinit=_a , project='''face-editor''' ) wandb.config.update({'''Positive Prompts''': positive_prompts} ) wandb.config.update({'''Negative Prompts''': negative_prompts} ) wandb.config.update({'''lr''': self.lr, '''iterations''': self.iterations} ) if image_path: __a = Image.open(_a ) __a = image.resize((256, 256) ) wandb.log('''Original Image''' , wandb.Image(_a ) ) def __UpperCAmelCase ( self , _a ): if not prompts: return [] __a = [] __a = [] if isinstance(_a , _a ): __a = [prompt.strip() for prompt in prompts.split('''|''' )] for prompt in prompts: if isinstance(_a , (tuple, list) ): __a = prompt[0] __a = float(prompt[1] ) elif ":" in prompt: __a , __a = prompt.split(''':''' ) __a = float(_a ) else: __a = prompt __a = 1.0 processed_prompts.append(_a ) weights.append(_a ) return { "prompts": processed_prompts, "weights": torch.tensor(_a , device=self.device ), } def __UpperCAmelCase ( self , _a , _a=None , _a=None , _a=True , _a=False , _a=True , _a=True , _a=None , ): if image_path: __a = self._get_latent(_a ) else: __a = torch.randn(self.latent_dim , device=self.device ) if self.log: self._init_logging(_a , _a , _a ) assert pos_prompts, "You must provide at least one positive prompt." __a = self.process_prompts(_a ) __a = self.process_prompts(_a ) if save_final and save_path is None: __a = os.path.join('''./outputs/''' , '''_'''.join(pos_prompts['''prompts'''] ) ) if not os.path.exists(_a ): os.makedirs(_a ) else: __a = save_path + '''_''' + get_timestamp() os.makedirs(_a ) __a = save_path __a = self.vqgan.decode(self.latent )[0] if show_intermediate: print('''Original Image''' ) show_pil(custom_to_pil(_a ) ) __a = loop_post_process(_a ) for iter, transformed_img in enumerate(self._optimize_CLIP(_a , _a , _a ) ): if show_intermediate: show_pil(_a ) if save_intermediate: transformed_img.save(os.path.join(self.save_path , f'''iter_{iter:03d}.png''' ) ) if self.log: wandb.log({'''Image''': wandb.Image(_a )} ) if show_final: show_pil(_a ) if save_final: transformed_img.save(os.path.join(self.save_path , f'''iter_{iter:03d}_final.png''' ) )
695
"""simple docstring""" import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler lowercase_ = 1_6 lowercase_ = 3_2 def lowercase ( lowerCAmelCase__ : Accelerator , lowerCAmelCase__ : int = 16 , lowerCAmelCase__ : str = "bert-base-cased" ) -> Optional[int]: __a = AutoTokenizer.from_pretrained(lowerCAmelCase__ ) __a = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(lowerCAmelCase__ : Optional[Any] ): # max_length=None => use the model max length (it's actually the default) __a = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=lowerCAmelCase__ , max_length=lowerCAmelCase__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset __a = datasets.map( lowerCAmelCase__ , batched=lowerCAmelCase__ , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , load_from_cache_file=lowerCAmelCase__ ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library __a = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(lowerCAmelCase__ : int ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(lowerCAmelCase__ , padding='''max_length''' , max_length=128 , return_tensors='''pt''' ) return tokenizer.pad(lowerCAmelCase__ , padding='''longest''' , return_tensors='''pt''' ) # Instantiate dataloaders. __a = DataLoader( tokenized_datasets['''train'''] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ ) __a = DataLoader( tokenized_datasets['''validation'''] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ ) return train_dataloader, eval_dataloader def lowercase ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any] ) -> Optional[int]: # Initialize accelerator __a = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __a = config['''lr'''] __a = int(config['''num_epochs'''] ) __a = int(config['''seed'''] ) __a = int(config['''batch_size'''] ) __a = args.model_name_or_path set_seed(lowerCAmelCase__ ) __a , __a = get_dataloaders(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __a = AutoModelForSequenceClassification.from_pretrained(lowerCAmelCase__ , return_dict=lowerCAmelCase__ ) # Instantiate optimizer __a = ( AdamW if accelerator.state.deepspeed_plugin is None or '''optimizer''' not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) __a = optimizer_cls(params=model.parameters() , lr=lowerCAmelCase__ ) if accelerator.state.deepspeed_plugin is not None: __a = accelerator.state.deepspeed_plugin.deepspeed_config[ '''gradient_accumulation_steps''' ] else: __a = 1 __a = (len(lowerCAmelCase__ ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): __a = get_linear_schedule_with_warmup( optimizer=lowerCAmelCase__ , num_warmup_steps=0 , num_training_steps=lowerCAmelCase__ , ) else: __a = DummyScheduler(lowerCAmelCase__ , total_num_steps=lowerCAmelCase__ , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. __a , __a , __a , __a , __a = accelerator.prepare( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # We need to keep track of how many total steps we have iterated over __a = 0 # We also need to keep track of the stating epoch so files are named properly __a = 0 # Now we train the model __a = evaluate.load('''glue''' , '''mrpc''' ) __a = 0 __a = {} for epoch in range(lowerCAmelCase__ , lowerCAmelCase__ ): model.train() for step, batch in enumerate(lowerCAmelCase__ ): __a = model(**lowerCAmelCase__ ) __a = outputs.loss __a = loss / gradient_accumulation_steps accelerator.backward(lowerCAmelCase__ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 model.eval() __a = 0 for step, batch in enumerate(lowerCAmelCase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): __a = model(**lowerCAmelCase__ ) __a = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times __a , __a = accelerator.gather( (predictions, batch['''labels''']) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(lowerCAmelCase__ ) - 1: __a = predictions[: len(eval_dataloader.dataset ) - samples_seen] __a = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=lowerCAmelCase__ , references=lowerCAmelCase__ , ) __a = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f'''epoch {epoch}:''' , lowerCAmelCase__ ) __a = eval_metric['''accuracy'''] if best_performance < eval_metric["accuracy"]: __a = eval_metric['''accuracy'''] if args.performance_lower_bound is not None: assert ( args.performance_lower_bound <= best_performance ), f'''Best performance metric {best_performance} is lower than the lower bound {args.performance_lower_bound}''' accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , '''all_results.json''' ) , '''w''' ) as f: json.dump(lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( ) -> List[str]: __a = argparse.ArgumentParser(description='''Simple example of training script tracking peak GPU memory usage.''' ) parser.add_argument( '''--model_name_or_path''' , type=lowerCAmelCase__ , default='''bert-base-cased''' , help='''Path to pretrained model or model identifier from huggingface.co/models.''' , required=lowerCAmelCase__ , ) parser.add_argument( '''--output_dir''' , type=lowerCAmelCase__ , default='''.''' , help='''Optional save directory where all checkpoint folders will be stored. Default is the current working directory.''' , ) parser.add_argument( '''--performance_lower_bound''' , type=lowerCAmelCase__ , default=lowerCAmelCase__ , help='''Optional lower bound for the performance metric. If set, the training will throw error when the performance metric drops below this value.''' , ) parser.add_argument( '''--num_epochs''' , type=lowerCAmelCase__ , default=3 , help='''Number of train epochs.''' , ) __a = parser.parse_args() __a = {'''lr''': 2e-5, '''num_epochs''': args.num_epochs, '''seed''': 42, '''batch_size''': 16} training_function(lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": main()
695
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = {"ctrl": "https://huggingface.co/ctrl/resolve/main/config.json"} class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = 'ctrl' __UpperCAmelCase : List[str] = ['past_key_values'] __UpperCAmelCase : int = { 'max_position_embeddings': 'n_positions', 'hidden_size': 'n_embd', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self , _a=246_534 , _a=256 , _a=1_280 , _a=8_192 , _a=48 , _a=16 , _a=0.1 , _a=0.1 , _a=1E-6 , _a=0.02 , _a=True , **_a , ): __a = vocab_size __a = n_positions __a = n_embd __a = n_layer __a = n_head __a = dff __a = resid_pdrop __a = embd_pdrop __a = layer_norm_epsilon __a = initializer_range __a = use_cache super().__init__(**_a )
695
"""simple docstring""" from typing import Any def lowercase ( lowerCAmelCase__ : list , lowerCAmelCase__ : list , lowerCAmelCase__ : dict , lowerCAmelCase__ : dict , lowerCAmelCase__ : dict , ) -> list: _validation( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ) # Creates data structures and fill initial step __a = {} __a = {} for state in states_space: __a = observations_space[0] __a = ( initial_probabilities[state] * emission_probabilities[state][observation] ) __a = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(lowerCAmelCase__ ) ): __a = observations_space[o] __a = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function __a = '''''' __a = -1 for k_state in states_space: __a = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: __a = probability __a = k_state # Update probabilities and pointers dicts __a = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) __a = arg_max # The final observation __a = observations_space[len(lowerCAmelCase__ ) - 1] # argmax for given final observation __a = '''''' __a = -1 for k_state in states_space: __a = probabilities[(k_state, final_observation)] if probability > max_probability: __a = probability __a = k_state __a = arg_max # Process pointers backwards __a = last_state __a = [] for o in range(len(lowerCAmelCase__ ) - 1 , -1 , -1 ): result.append(lowerCAmelCase__ ) __a = pointers[previous, observations_space[o]] result.reverse() return result def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: _validate_not_empty( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ) _validate_lists(lowerCAmelCase__ , lowerCAmelCase__ ) _validate_dicts( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError('''There\'s an empty parameter''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any ) -> None: _validate_list(lowerCAmelCase__ , '''observations_space''' ) _validate_list(lowerCAmelCase__ , '''states_space''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str ) -> None: if not isinstance(_object , lowerCAmelCase__ ): __a = f'''{var_name} must be a list''' raise ValueError(lowerCAmelCase__ ) else: for x in _object: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = f'''{var_name} must be a list of strings''' raise ValueError(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: _validate_dict(lowerCAmelCase__ , '''initial_probabilities''' , lowerCAmelCase__ ) _validate_nested_dict(lowerCAmelCase__ , '''transition_probabilities''' ) _validate_nested_dict(lowerCAmelCase__ , '''emission_probabilities''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str ) -> None: _validate_dict(_object , lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object.values(): _validate_dict(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : type , lowerCAmelCase__ : bool = False ) -> None: if not isinstance(_object , lowerCAmelCase__ ): __a = f'''{var_name} must be a dict''' raise ValueError(lowerCAmelCase__ ) if not all(isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object ): __a = f'''{var_name} all keys must be strings''' raise ValueError(lowerCAmelCase__ ) if not all(isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object.values() ): __a = '''nested dictionary ''' if nested else '''''' __a = f'''{var_name} {nested_text}all values must be {value_type.__name__}''' raise ValueError(lowerCAmelCase__ ) if __name__ == "__main__": from doctest import testmod testmod()
695
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int = 1000 ) -> int: __a , __a = 1, 1 __a = [] for i in range(1 , n + 1 ): __a = prev_numerator + 2 * prev_denominator __a = prev_numerator + prev_denominator if len(str(lowerCAmelCase__ ) ) > len(str(lowerCAmelCase__ ) ): result.append(lowerCAmelCase__ ) __a = numerator __a = denominator return len(lowerCAmelCase__ ) if __name__ == "__main__": print(F'''{solution() = }''')
695
"""simple docstring""" import math def lowercase ( lowerCAmelCase__ : int ) -> bool: 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(lowerCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowercase ( lowerCAmelCase__ : float = 0.1 ) -> int: __a = 3 __a = 3 while primes / (2 * j - 1) >= ratio: for i in range(j * j + j + 1 , (j + 2) * (j + 2) , j + 1 ): primes += is_prime(lowerCAmelCase__ ) j += 2 return j if __name__ == "__main__": import doctest doctest.testmod()
695
1
"""simple docstring""" lowercase_ = "0.18.2" from .configuration_utils import ConfigMixin from .utils import ( OptionalDependencyNotAvailable, is_flax_available, is_inflect_available, is_invisible_watermark_available, is_k_diffusion_available, is_k_diffusion_version, is_librosa_available, is_note_seq_available, is_onnx_available, is_scipy_available, is_torch_available, is_torchsde_available, is_transformers_available, is_transformers_version, is_unidecode_available, logging, ) try: if not is_onnx_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_onnx_objects import * # noqa F403 else: from .pipelines import OnnxRuntimeModel try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_pt_objects import * # noqa F403 else: from .models import ( AutoencoderKL, ControlNetModel, ModelMixin, PriorTransformer, TaFilmDecoder, TransformeraDModel, UNetaDModel, UNetaDConditionModel, UNetaDModel, UNetaDConditionModel, VQModel, ) from .optimization import ( get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, get_scheduler, ) from .pipelines import ( AudioPipelineOutput, ConsistencyModelPipeline, DanceDiffusionPipeline, DDIMPipeline, DDPMPipeline, DiffusionPipeline, DiTPipeline, ImagePipelineOutput, KarrasVePipeline, LDMPipeline, LDMSuperResolutionPipeline, PNDMPipeline, RePaintPipeline, ScoreSdeVePipeline, ) from .schedulers import ( CMStochasticIterativeScheduler, DDIMInverseScheduler, DDIMParallelScheduler, DDIMScheduler, DDPMParallelScheduler, DDPMScheduler, DEISMultistepScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, HeunDiscreteScheduler, IPNDMScheduler, KarrasVeScheduler, KDPMaAncestralDiscreteScheduler, KDPMaDiscreteScheduler, PNDMScheduler, RePaintScheduler, SchedulerMixin, ScoreSdeVeScheduler, UnCLIPScheduler, UniPCMultistepScheduler, VQDiffusionScheduler, ) from .training_utils import EMAModel try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .schedulers import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .schedulers import DPMSolverSDEScheduler try: if not (is_torch_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipelines import ( AltDiffusionImgaImgPipeline, AltDiffusionPipeline, AudioLDMPipeline, CycleDiffusionPipeline, IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ImageTextPipelineOutput, KandinskyImgaImgPipeline, KandinskyInpaintPipeline, KandinskyPipeline, KandinskyPriorPipeline, KandinskyVaaControlnetImgaImgPipeline, KandinskyVaaControlnetPipeline, KandinskyVaaImgaImgPipeline, KandinskyVaaInpaintPipeline, KandinskyVaaPipeline, KandinskyVaaPriorEmbaEmbPipeline, KandinskyVaaPriorPipeline, LDMTextToImagePipeline, PaintByExamplePipeline, SemanticStableDiffusionPipeline, ShapEImgaImgPipeline, ShapEPipeline, StableDiffusionAttendAndExcitePipeline, StableDiffusionControlNetImgaImgPipeline, StableDiffusionControlNetInpaintPipeline, StableDiffusionControlNetPipeline, StableDiffusionDepthaImgPipeline, StableDiffusionDiffEditPipeline, StableDiffusionImageVariationPipeline, StableDiffusionImgaImgPipeline, StableDiffusionInpaintPipeline, StableDiffusionInpaintPipelineLegacy, StableDiffusionInstructPixaPixPipeline, StableDiffusionLatentUpscalePipeline, StableDiffusionLDMaDPipeline, StableDiffusionModelEditingPipeline, StableDiffusionPanoramaPipeline, StableDiffusionParadigmsPipeline, StableDiffusionPipeline, StableDiffusionPipelineSafe, StableDiffusionPixaPixZeroPipeline, StableDiffusionSAGPipeline, StableDiffusionUpscalePipeline, StableUnCLIPImgaImgPipeline, StableUnCLIPPipeline, TextToVideoSDPipeline, TextToVideoZeroPipeline, UnCLIPImageVariationPipeline, UnCLIPPipeline, UniDiffuserModel, UniDiffuserPipeline, UniDiffuserTextDecoder, VersatileDiffusionDualGuidedPipeline, VersatileDiffusionImageVariationPipeline, VersatileDiffusionPipeline, VersatileDiffusionTextToImagePipeline, VideoToVideoSDPipeline, VQDiffusionPipeline, ) try: if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import * # noqa F403 else: from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline try: if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import * # noqa F403 else: from .pipelines import StableDiffusionKDiffusionPipeline try: if not (is_torch_available() and is_transformers_available() and is_onnx_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_onnx_objects import * # noqa F403 else: from .pipelines import ( OnnxStableDiffusionImgaImgPipeline, OnnxStableDiffusionInpaintPipeline, OnnxStableDiffusionInpaintPipelineLegacy, OnnxStableDiffusionPipeline, OnnxStableDiffusionUpscalePipeline, StableDiffusionOnnxPipeline, ) try: if not (is_torch_available() and is_librosa_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_librosa_objects import * # noqa F403 else: from .pipelines import AudioDiffusionPipeline, Mel try: if not (is_transformers_available() and is_torch_available() and is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_transformers_and_torch_and_note_seq_objects import * # noqa F403 else: from .pipelines import SpectrogramDiffusionPipeline try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_objects import * # noqa F403 else: from .models.controlnet_flax import FlaxControlNetModel from .models.modeling_flax_utils import FlaxModelMixin from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel from .models.vae_flax import FlaxAutoencoderKL from .pipelines import FlaxDiffusionPipeline from .schedulers import ( FlaxDDIMScheduler, FlaxDDPMScheduler, FlaxDPMSolverMultistepScheduler, FlaxKarrasVeScheduler, FlaxLMSDiscreteScheduler, FlaxPNDMScheduler, FlaxSchedulerMixin, FlaxScoreSdeVeScheduler, ) try: if not (is_flax_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_and_transformers_objects import * # noqa F403 else: from .pipelines import ( FlaxStableDiffusionControlNetPipeline, FlaxStableDiffusionImgaImgPipeline, FlaxStableDiffusionInpaintPipeline, FlaxStableDiffusionPipeline, ) try: if not (is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_note_seq_objects import * # noqa F403 else: from .pipelines import MidiProcessor
695
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = { "configuration_mctct": ["MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MCTCTConfig"], "feature_extraction_mctct": ["MCTCTFeatureExtractor"], "processing_mctct": ["MCTCTProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST", "MCTCTForCTC", "MCTCTModel", "MCTCTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig from .feature_extraction_mctct import MCTCTFeatureExtractor from .processing_mctct import MCTCTProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
695
1
"""simple docstring""" import json import os import unittest from transformers.models.gptsan_japanese.tokenization_gptsan_japanese import ( VOCAB_FILES_NAMES, GPTSanJapaneseTokenizer, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : List[Any] = GPTSanJapaneseTokenizer __UpperCAmelCase : str = False __UpperCAmelCase : str = {'do_clean_text': False, 'add_prefix_space': False} def __UpperCAmelCase ( self ): super().setUp() # fmt: off __a = ['''こん''', '''こんに''', '''にちは''', '''ばんは''', '''世界,㔺界''', '''、''', '''。''', '''<BR>''', '''<SP>''', '''<TAB>''', '''<URL>''', '''<EMAIL>''', '''<TEL>''', '''<DATE>''', '''<PRICE>''', '''<BLOCK>''', '''<KIGOU>''', '''<U2000U2BFF>''', '''<|emoji1|>''', '''<unk>''', '''<|bagoftoken|>''', '''<|endoftext|>'''] # fmt: on __a = {'''emoji''': {'''\ud83d\ude00''': '''<|emoji1|>'''}, '''emoji_inv''': {'''<|emoji1|>''': '''\ud83d\ude00'''}} # 😀 __a = {'''unk_token''': '''<unk>'''} __a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) __a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''emoji_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) with open(self.emoji_file , '''w''' ) as emoji_writer: emoji_writer.write(json.dumps(_a ) ) def __UpperCAmelCase ( self , **_a ): kwargs.update(self.special_tokens_map ) return GPTSanJapaneseTokenizer.from_pretrained(self.tmpdirname , **_a ) def __UpperCAmelCase ( self , _a ): __a = '''こんにちは、世界。 \nこんばんは、㔺界。😀''' __a = '''こんにちは、世界。 \nこんばんは、世界。😀''' return input_text, output_text def __UpperCAmelCase ( self , _a ): __a , __a = self.get_input_output_texts(_a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = tokenizer.decode(_a , clean_up_tokenization_spaces=_a ) return text, ids def __UpperCAmelCase ( self ): pass # TODO add if relevant def __UpperCAmelCase ( self ): pass # TODO add if relevant def __UpperCAmelCase ( self ): pass # TODO add if relevant def __UpperCAmelCase ( self ): __a = self.get_tokenizer() # Testing tokenization __a = '''こんにちは、世界。 こんばんは、㔺界。''' __a = ['''こん''', '''にちは''', '''、''', '''世界''', '''。''', '''<SP>''', '''こん''', '''ばんは''', '''、''', '''㔺界''', '''。'''] __a = tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) # Testing conversion to ids without special tokens __a = [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6] __a = tokenizer.convert_tokens_to_ids(_a ) self.assertListEqual(_a , _a ) # Testing conversion to ids with special tokens __a = tokens + [tokenizer.unk_token] __a = [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 19] __a = tokenizer.convert_tokens_to_ids(_a ) self.assertListEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = self.get_tokenizer() # Testing tokenization __a = '''こんにちは、<|bagoftoken|>世界。こんばんは、<|bagoftoken|>㔺界。''' __a = '''こんにちは、、、、世界。こんばんは、、、、世界。''' __a = tokenizer.encode(_a ) __a = tokenizer.decode(_a ) self.assertEqual(_a , _a ) @slow def __UpperCAmelCase ( self ): __a = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' ) # Testing tokenization __a = '''こんにちは、世界。''' __a = '''こんばんは、㔺界。😀''' __a = '''こんにちは、世界。こんばんは、世界。😀''' __a = tokenizer.encode(prefix_text + input_text ) __a = tokenizer.encode('''''' , prefix_text=prefix_text + input_text ) __a = tokenizer.encode(_a , prefix_text=_a ) __a = tokenizer.decode(_a ) __a = tokenizer.decode(_a ) __a = tokenizer.decode(_a ) self.assertEqual(_a , _a ) self.assertEqual(_a , _a ) self.assertEqual(_a , _a ) @slow def __UpperCAmelCase ( self ): __a = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' ) # Testing tokenization __a = '''こんにちは、世界。''' __a = '''こんばんは、㔺界。😀''' __a = len(tokenizer.encode(_a ) ) - 2 __a = len(tokenizer.encode(_a ) ) - 2 __a = [1] + [0] * (len_prefix + len_text + 1) __a = [1] * (len_prefix + len_text + 1) + [0] __a = [1] + [1] * (len_prefix) + [0] * (len_text + 1) __a = tokenizer(prefix_text + input_text ).token_type_ids __a = tokenizer('''''' , prefix_text=prefix_text + input_text ).token_type_ids __a = tokenizer(_a , prefix_text=_a ).token_type_ids self.assertListEqual(_a , _a ) self.assertListEqual(_a , _a ) self.assertListEqual(_a , _a ) @slow def __UpperCAmelCase ( self ): __a = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' ) __a = tokenizer.encode('''あンいワ''' ) __a = tokenizer.encode('''''' , prefix_text='''あンいワ''' ) __a = tokenizer.encode('''いワ''' , prefix_text='''あン''' ) self.assertEqual(tokenizer.decode(_a ) , tokenizer.decode(_a ) ) self.assertEqual(tokenizer.decode(_a ) , tokenizer.decode(_a ) ) self.assertNotEqual(_a , _a ) self.assertNotEqual(_a , _a ) self.assertEqual(x_token_a[1] , x_token_a[-1] ) # SEG token self.assertEqual(x_token_a[1] , x_token_a[3] ) # SEG token @slow def __UpperCAmelCase ( self ): __a = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' ) __a = [['''武田信玄''', '''は、'''], ['''織田信長''', '''の配下の、''']] __a = tokenizer(_a , padding=_a ) __a = tokenizer.batch_encode_plus(_a , padding=_a ) # fmt: off __a = [[35_993, 8_640, 25_948, 35_998, 30_647, 35_675, 35_999, 35_999], [35_993, 10_382, 9_868, 35_998, 30_646, 9_459, 30_646, 35_675]] __a = [[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0]] __a = [[1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]] # fmt: on self.assertListEqual(x_token.input_ids , _a ) self.assertListEqual(x_token.token_type_ids , _a ) self.assertListEqual(x_token.attention_mask , _a ) self.assertListEqual(x_token_a.input_ids , _a ) self.assertListEqual(x_token_a.token_type_ids , _a ) self.assertListEqual(x_token_a.attention_mask , _a ) def __UpperCAmelCase ( self ): # Intentionally convert some words to accommodate character fluctuations unique to Japanese pass def __UpperCAmelCase ( self ): # tokenizer has no padding token pass
695
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json" ), } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[Any] = 'dpr' def __init__( self , _a=30_522 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=2 , _a=0.02 , _a=1E-12 , _a=0 , _a="absolute" , _a = 0 , **_a , ): super().__init__(pad_token_id=_a , **_a ) __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = hidden_act __a = intermediate_size __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = initializer_range __a = layer_norm_eps __a = projection_dim __a = position_embedding_type
695
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : list[list[int]] ) -> int: def update_area_of_max_square(lowerCAmelCase__ : int , lowerCAmelCase__ : int ) -> int: # BASE CASE if row >= rows or col >= cols: return 0 __a = update_area_of_max_square(lowerCAmelCase__ , col + 1 ) __a = update_area_of_max_square(row + 1 , col + 1 ) __a = update_area_of_max_square(row + 1 , lowerCAmelCase__ ) if mat[row][col]: __a = 1 + min([right, diagonal, down] ) __a = max(largest_square_area[0] , lowerCAmelCase__ ) return sub_problem_sol else: return 0 __a = [0] update_area_of_max_square(0 , 0 ) return largest_square_area[0] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : list[list[int]] ) -> int: def update_area_of_max_square_using_dp_array( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : list[list[int]] ) -> int: if row >= rows or col >= cols: return 0 if dp_array[row][col] != -1: return dp_array[row][col] __a = update_area_of_max_square_using_dp_array(lowerCAmelCase__ , col + 1 , lowerCAmelCase__ ) __a = update_area_of_max_square_using_dp_array(row + 1 , col + 1 , lowerCAmelCase__ ) __a = update_area_of_max_square_using_dp_array(row + 1 , lowerCAmelCase__ , lowerCAmelCase__ ) if mat[row][col]: __a = 1 + min([right, diagonal, down] ) __a = max(largest_square_area[0] , lowerCAmelCase__ ) __a = sub_problem_sol return sub_problem_sol else: return 0 __a = [0] __a = [[-1] * cols for _ in range(lowerCAmelCase__ )] update_area_of_max_square_using_dp_array(0 , 0 , lowerCAmelCase__ ) return largest_square_area[0] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : list[list[int]] ) -> int: __a = [[0] * (cols + 1) for _ in range(rows + 1 )] __a = 0 for row in range(rows - 1 , -1 , -1 ): for col in range(cols - 1 , -1 , -1 ): __a = dp_array[row][col + 1] __a = dp_array[row + 1][col + 1] __a = dp_array[row + 1][col] if mat[row][col] == 1: __a = 1 + min(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) __a = max(dp_array[row][col] , lowerCAmelCase__ ) else: __a = 0 return largest_square_area def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : list[list[int]] ) -> int: __a = [0] * (cols + 1) __a = [0] * (cols + 1) __a = 0 for row in range(rows - 1 , -1 , -1 ): for col in range(cols - 1 , -1 , -1 ): __a = current_row[col + 1] __a = next_row[col + 1] __a = next_row[col] if mat[row][col] == 1: __a = 1 + min(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) __a = max(current_row[col] , lowerCAmelCase__ ) else: __a = 0 __a = current_row return largest_square_area if __name__ == "__main__": import doctest doctest.testmod() print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))
695
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = StableDiffusionInpaintPipeline __UpperCAmelCase : int = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS __UpperCAmelCase : str = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __UpperCAmelCase : int = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess __UpperCAmelCase : Tuple = frozenset([] ) def __UpperCAmelCase ( self ): torch.manual_seed(0 ) __a = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=9 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=_a , ) __a = PNDMScheduler(skip_prk_steps=_a ) torch.manual_seed(0 ) __a = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) __a = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , hidden_act='''gelu''' , projection_dim=512 , ) __a = CLIPTextModel(_a ) __a = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __a = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def __UpperCAmelCase ( self , _a , _a=0 ): # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched __a = floats_tensor((1, 3, 32, 32) , rng=random.Random(_a ) ).to(_a ) __a = image.cpu().permute(0 , 2 , 3 , 1 )[0] __a = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((64, 64) ) __a = Image.fromarray(np.uinta(image + 4 ) ).convert('''RGB''' ).resize((64, 64) ) if str(_a ).startswith('''mps''' ): __a = torch.manual_seed(_a ) else: __a = torch.Generator(device=_a ).manual_seed(_a ) __a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': init_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def __UpperCAmelCase ( self ): __a = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a = self.get_dummy_components() __a = StableDiffusionInpaintPipeline(**_a ) __a = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = sd_pipe(**_a ).images __a = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __a = np.array([0.4727, 0.5735, 0.3941, 0.5446, 0.5926, 0.4394, 0.5062, 0.4654, 0.4476] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __UpperCAmelCase ( self ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self ): __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench.npy''' ) __a = '''stabilityai/stable-diffusion-2-inpainting''' __a = StableDiffusionInpaintPipeline.from_pretrained(_a , safety_checker=_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = '''Face of a yellow cat, high resolution, sitting on a park bench''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , output_type='''np''' , ) __a = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 9E-3 def __UpperCAmelCase ( self ): __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench_fp16.npy''' ) __a = '''stabilityai/stable-diffusion-2-inpainting''' __a = StableDiffusionInpaintPipeline.from_pretrained( _a , torch_dtype=torch.floataa , safety_checker=_a , ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = '''Face of a yellow cat, high resolution, sitting on a park bench''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , output_type='''np''' , ) __a = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 5E-1 def __UpperCAmelCase ( self ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __a = '''stabilityai/stable-diffusion-2-inpainting''' __a = PNDMScheduler.from_pretrained(_a , subfolder='''scheduler''' ) __a = StableDiffusionInpaintPipeline.from_pretrained( _a , safety_checker=_a , scheduler=_a , torch_dtype=torch.floataa , ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() __a = '''Face of a yellow cat, high resolution, sitting on a park bench''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , num_inference_steps=2 , output_type='''np''' , ) __a = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 10**9
695
1
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "facebook/data2vec-vision-base-ft": ( "https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json" ), } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = 'data2vec-vision' def __init__( self , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.0 , _a=0.0 , _a=0.02 , _a=1E-12 , _a=224 , _a=16 , _a=3 , _a=False , _a=False , _a=False , _a=False , _a=0.1 , _a=0.1 , _a=True , _a=[3, 5, 7, 11] , _a=[1, 2, 3, 6] , _a=True , _a=0.4 , _a=256 , _a=1 , _a=False , _a=255 , **_a , ): super().__init__(**_a ) __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = initializer_range __a = layer_norm_eps __a = image_size __a = patch_size __a = num_channels __a = use_mask_token __a = use_absolute_position_embeddings __a = use_relative_position_bias __a = use_shared_relative_position_bias __a = layer_scale_init_value __a = drop_path_rate __a = use_mean_pooling # decode head attributes (semantic segmentation) __a = out_indices __a = pool_scales # auxiliary head attributes (semantic segmentation) __a = use_auxiliary_head __a = auxiliary_loss_weight __a = auxiliary_channels __a = auxiliary_num_convs __a = auxiliary_concat_input __a = semantic_loss_ignore_index class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : int = version.parse('1.11' ) @property def __UpperCAmelCase ( self ): return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __UpperCAmelCase ( self ): return 1E-4
695
"""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 __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : int = 0 __UpperCAmelCase : bool = False __UpperCAmelCase : float = 3.0 class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): # 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=_a ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'''a''': 2, '''c''': 2.25} ) @require_cuda def __UpperCAmelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. __a = GradScalerKwargs(init_scale=1_024 , growth_factor=2 ) AcceleratorState._reset_state() __a = Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) __a = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2_000 ) self.assertEqual(scaler._enabled , _a ) @require_multi_gpu def __UpperCAmelCase ( self ): __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": lowercase_ = DistributedDataParallelKwargs(bucket_cap_mb=1_5, find_unused_parameters=True) lowercase_ = Accelerator(kwargs_handlers=[ddp_scaler]) lowercase_ = torch.nn.Linear(1_0_0, 2_0_0) lowercase_ = accelerator.prepare(model) # Check the values changed in kwargs lowercase_ = "" lowercase_ = model.bucket_bytes_cap // (1_0_2_4 * 1_0_2_4) if observed_bucket_cap_map != 1_5: 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)
695
1
"""simple docstring""" import json import os import unittest from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES, XLMTokenizer from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Dict = XLMTokenizer __UpperCAmelCase : Optional[int] = False def __UpperCAmelCase ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt __a = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''w</w>''', '''r</w>''', '''t</w>''', '''lo''', '''low''', '''er</w>''', '''low</w>''', '''lowest</w>''', '''newer</w>''', '''wider</w>''', '''<unk>''', ] __a = dict(zip(_a , range(len(_a ) ) ) ) __a = ['''l o 123''', '''lo w 1456''', '''e r</w> 1789''', ''''''] __a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) __a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' ) as fp: fp.write(json.dumps(_a ) ) with open(self.merges_file , '''w''' ) as fp: fp.write('''\n'''.join(_a ) ) def __UpperCAmelCase ( self , _a ): __a = '''lower newer''' __a = '''lower newer''' return input_text, output_text def __UpperCAmelCase ( self ): __a = XLMTokenizer(self.vocab_file , self.merges_file ) __a = '''lower''' __a = ['''low''', '''er</w>'''] __a = tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) __a = tokens + ['''<unk>'''] __a = [14, 15, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , _a ) @slow def __UpperCAmelCase ( self ): __a = XLMTokenizer.from_pretrained('''xlm-mlm-en-2048''' ) __a = tokenizer.encode('''sequence builders''' , add_special_tokens=_a ) __a = tokenizer.encode('''multi-sequence build''' , add_special_tokens=_a ) __a = tokenizer.build_inputs_with_special_tokens(_a ) __a = tokenizer.build_inputs_with_special_tokens(_a , _a ) assert encoded_sentence == [0] + text + [1] assert encoded_pair == [0] + text + [1] + text_a + [1]
695
"""simple docstring""" import inspect import os import sys import unittest import accelerate from accelerate.test_utils import execute_subprocess_async, require_tpu class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = inspect.getfile(accelerate.test_utils ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) __a = os.path.sep.join(inspect.getfile(self.__class__ ).split(os.path.sep )[:-1] ) @require_tpu def __UpperCAmelCase ( self ): __a = f''' {self.test_dir}/xla_spawn.py --num_cores 8 {self.test_file_path} '''.split() __a = [sys.executable] + distributed_args execute_subprocess_async(_a , env=os.environ.copy() )
695
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int ) -> int: __a = 0 while num > 0: digit_sum += num % 10 num //= 10 return digit_sum def lowercase ( lowerCAmelCase__ : int = 100 ) -> int: __a = 1 __a = 2 for i in range(2 , max_n + 1 ): __a = pre_numerator __a = 2 * i // 3 if i % 3 == 0 else 1 __a = cur_numerator __a = e_cont * pre_numerator + temp return sum_digits(lowerCAmelCase__ ) if __name__ == "__main__": print(F'''{solution() = }''')
695
"""simple docstring""" import os import unittest from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, BertTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = BertTokenizer __UpperCAmelCase : Optional[Any] = BertTokenizerFast __UpperCAmelCase : str = True __UpperCAmelCase : Tuple = True __UpperCAmelCase : Any = filter_non_english def __UpperCAmelCase ( self ): super().setUp() __a = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] __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] ) ) def __UpperCAmelCase ( self , _a ): __a = '''UNwant\u00E9d,running''' __a = '''unwanted, running''' return input_text, output_text def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file ) __a = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(_a , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [9, 6, 7, 12, 10, 11] ) def __UpperCAmelCase ( self ): if not self.test_rust_tokenizer: return __a = self.get_tokenizer() __a = self.get_rust_tokenizer() __a = '''UNwant\u00E9d,running''' __a = tokenizer.tokenize(_a ) __a = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) __a = self.get_rust_tokenizer() __a = tokenizer.encode(_a ) __a = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a ) # With lower casing __a = self.get_tokenizer(do_lower_case=_a ) __a = self.get_rust_tokenizer(do_lower_case=_a ) __a = '''UNwant\u00E9d,running''' __a = tokenizer.tokenize(_a ) __a = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) __a = self.get_rust_tokenizer() __a = tokenizer.encode(_a ) __a = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('''ah\u535A\u63A8zz''' ) , ['''ah''', '''\u535A''', '''\u63A8''', '''zz'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''hello''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hällo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''h\u00E9llo'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HäLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HaLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , never_split=['''[UNK]'''] ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? [UNK]''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''', '''[UNK]'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer() __a = '''a\n\'ll !!to?\'d of, can\'t.''' __a = ['''a''', '''\'''', '''ll''', '''!''', '''!''', '''to''', '''?''', '''\'''', '''d''', '''of''', ''',''', '''can''', '''\'''', '''t''', '''.'''] self.assertListEqual(tokenizer.tokenize(_a ) , _a ) def __UpperCAmelCase ( self ): __a = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing'''] __a = {} for i, token in enumerate(_a ): __a = i __a = WordpieceTokenizer(vocab=_a , unk_token='''[UNK]''' ) self.assertListEqual(tokenizer.tokenize('''''' ) , [] ) self.assertListEqual(tokenizer.tokenize('''unwanted running''' ) , ['''un''', '''##want''', '''##ed''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.tokenize('''unwantedX running''' ) , ['''[UNK]''', '''runn''', '''##ing'''] ) def __UpperCAmelCase ( self ): self.assertTrue(_is_whitespace(''' ''' ) ) self.assertTrue(_is_whitespace('''\t''' ) ) self.assertTrue(_is_whitespace('''\r''' ) ) self.assertTrue(_is_whitespace('''\n''' ) ) self.assertTrue(_is_whitespace('''\u00A0''' ) ) self.assertFalse(_is_whitespace('''A''' ) ) self.assertFalse(_is_whitespace('''-''' ) ) def __UpperCAmelCase ( self ): self.assertTrue(_is_control('''\u0005''' ) ) self.assertFalse(_is_control('''A''' ) ) self.assertFalse(_is_control(''' ''' ) ) self.assertFalse(_is_control('''\t''' ) ) self.assertFalse(_is_control('''\r''' ) ) def __UpperCAmelCase ( self ): self.assertTrue(_is_punctuation('''-''' ) ) self.assertTrue(_is_punctuation('''$''' ) ) self.assertTrue(_is_punctuation('''`''' ) ) self.assertTrue(_is_punctuation('''.''' ) ) self.assertFalse(_is_punctuation('''A''' ) ) self.assertFalse(_is_punctuation(''' ''' ) ) def __UpperCAmelCase ( self ): __a = self.get_tokenizer() __a = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(_a ) for t in ['''Test''', '''\xad''', '''test''']] , [['''[UNK]'''], [], ['''[UNK]''']] ) self.assertListEqual( [rust_tokenizer.tokenize(_a ) for t in ['''Test''', '''\xad''', '''test''']] , [['''[UNK]'''], [], ['''[UNK]''']] ) @slow def __UpperCAmelCase ( self ): __a = self.tokenizer_class.from_pretrained('''bert-base-uncased''' ) __a = tokenizer.encode('''sequence builders''' , add_special_tokens=_a ) __a = tokenizer.encode('''multi-sequence build''' , add_special_tokens=_a ) __a = tokenizer.build_inputs_with_special_tokens(_a ) __a = tokenizer.build_inputs_with_special_tokens(_a , _a ) assert encoded_sentence == [101] + text + [102] assert encoded_pair == [101] + text + [102] + text_a + [102] def __UpperCAmelCase ( self ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = f'''A, naïve {tokenizer_r.mask_token} AllenNLP sentence.''' __a = tokenizer_r.encode_plus( _a , return_attention_mask=_a , return_token_type_ids=_a , return_offsets_mapping=_a , add_special_tokens=_a , ) __a = tokenizer_r.do_lower_case if hasattr(_a , '''do_lower_case''' ) else False __a = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), '''A'''), ((1, 2), ''','''), ((3, 5), '''na'''), ((5, 6), '''##ï'''), ((6, 8), '''##ve'''), ((9, 15), tokenizer_r.mask_token), ((16, 21), '''Allen'''), ((21, 23), '''##NL'''), ((23, 24), '''##P'''), ((25, 33), '''sentence'''), ((33, 34), '''.'''), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), '''a'''), ((1, 2), ''','''), ((3, 8), '''naive'''), ((9, 15), tokenizer_r.mask_token), ((16, 21), '''allen'''), ((21, 23), '''##nl'''), ((23, 24), '''##p'''), ((25, 33), '''sentence'''), ((33, 34), '''.'''), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens['''input_ids'''] ) ) self.assertEqual([e[0] for e in expected_results] , tokens['''offset_mapping'''] ) def __UpperCAmelCase ( self ): __a = ['''的''', '''人''', '''有'''] __a = ''''''.join(_a ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __a = True __a = self.tokenizer_class.from_pretrained(_a , **_a ) __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = tokenizer_p.encode(_a , add_special_tokens=_a ) __a = tokenizer_r.encode(_a , add_special_tokens=_a ) __a = tokenizer_r.convert_ids_to_tokens(_a ) __a = tokenizer_p.convert_ids_to_tokens(_a ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(_a , _a ) self.assertListEqual(_a , _a ) __a = False __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = self.tokenizer_class.from_pretrained(_a , **_a ) __a = tokenizer_r.encode(_a , add_special_tokens=_a ) __a = tokenizer_p.encode(_a , add_special_tokens=_a ) __a = tokenizer_r.convert_ids_to_tokens(_a ) __a = tokenizer_p.convert_ids_to_tokens(_a ) # it is expected that only the first Chinese character is not preceded by "##". __a = [ f'''##{token}''' if idx != 0 else token for idx, token in enumerate(_a ) ] self.assertListEqual(_a , _a ) self.assertListEqual(_a , _a )
695
1
"""simple docstring""" from __future__ import annotations import collections import tempfile import unittest import numpy as np from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import is_tf_available, is_vision_available from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask from ..bert.test_modeling_tf_bert import TFBertModelTester from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester from ..deit.test_modeling_tf_deit import TFDeiTModelTester from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester from ..vit.test_modeling_tf_vit import TFViTModelTester if is_tf_available(): from transformers import ( TFBertModel, TFCLIPVisionModel, TFDeiTModel, TFRobertaModel, TFVisionTextDualEncoderModel, TFViTModel, VisionTextDualEncoderConfig, ) if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor def lowercase ( lowerCAmelCase__ : int ) -> Tuple: if isinstance(lowerCAmelCase__ , collections.abc.Iterable ): return x return (x, x) @require_tf class __lowerCAmelCase : '''simple docstring''' def __UpperCAmelCase ( self , _a , _a ): pass def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self , _a , _a , _a , _a , _a=None , **_a ): __a = VisionTextDualEncoderConfig.from_vision_text_configs(_a , _a ) __a = TFVisionTextDualEncoderModel(_a ) __a = model(input_ids=_a , pixel_values=_a , attention_mask=_a ) self.assertEqual(output['''text_embeds'''].shape , (input_ids.shape[0], config.projection_dim) ) self.assertEqual(output['''image_embeds'''].shape , (pixel_values.shape[0], config.projection_dim) ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a=None , **_a ): __a , __a = self.get_vision_text_model(_a , _a ) __a = TFVisionTextDualEncoderModel(vision_model=_a , text_model=_a ) __a = model(input_ids=_a , pixel_values=_a , attention_mask=_a ) self.assertEqual(output['''text_embeds'''].shape , (input_ids.shape[0], model.config.projection_dim) ) self.assertEqual(output['''image_embeds'''].shape , (pixel_values.shape[0], model.config.projection_dim) ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a=None , **_a ): __a , __a = self.get_vision_text_model(_a , _a ) __a = {'''vision_model''': vision_model, '''text_model''': text_model} __a = TFVisionTextDualEncoderModel.from_vision_text_pretrained(**_a ) __a = model(input_ids=_a , pixel_values=_a , attention_mask=_a ) self.assertEqual(output['''text_embeds'''].shape , (input_ids.shape[0], model.config.projection_dim) ) self.assertEqual(output['''image_embeds'''].shape , (pixel_values.shape[0], model.config.projection_dim) ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a=None , **_a ): __a , __a = self.get_vision_text_model(_a , _a ) __a = TFVisionTextDualEncoderModel(vision_model=_a , text_model=_a ) __a = model(input_ids=_a , pixel_values=_a , attention_mask=_a ) __a = output[0].numpy() with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_a ) __a = TFVisionTextDualEncoderModel.from_pretrained(_a ) __a = model(input_ids=_a , pixel_values=_a , attention_mask=_a ) __a = after_output[0].numpy() __a = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(_a , 1E-5 ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a=None , **_a ): __a , __a = self.get_vision_text_model(_a , _a ) __a = TFVisionTextDualEncoderModel(vision_model=_a , text_model=_a ) __a = model( input_ids=_a , pixel_values=_a , attention_mask=_a , output_attentions=_a ) __a = output.vision_model_output.attentions self.assertEqual(len(_a ) , vision_config.num_hidden_layers ) # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token) __a = to_atuple(vision_model.config.image_size ) __a = to_atuple(vision_model.config.patch_size ) __a = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) __a = num_patches + 1 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len) ) __a = output.text_model_output.attentions self.assertEqual(len(_a ) , text_config.num_hidden_layers ) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def __UpperCAmelCase ( self , _a , _a , _a ): __a = np.abs((a - b) ).max() self.assertLessEqual(_a , _a , f'''Difference between torch and flax is {diff} (>= {tol}).''' ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_model(**_a ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() self.check_model_from_pretrained_configs(**_a ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_from_pretrained(**_a ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() self.check_save_load(**_a ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() self.check_vision_text_output_attention(**_a ) @slow def __UpperCAmelCase ( self ): __a , __a = self.get_pretrained_model_and_inputs() __a = model_a(**_a ) __a = outputs[0].numpy() with tempfile.TemporaryDirectory() as tmp_dirname: model_a.save_pretrained(_a ) __a = TFVisionTextDualEncoderModel.from_pretrained(_a ) __a = model_a(**_a ) __a = after_outputs[0].numpy() __a = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(_a , 1E-5 ) @require_tf class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = TFVisionTextDualEncoderModel.from_vision_text_pretrained( '''hf-internal-testing/tiny-random-vit''' , '''hf-internal-testing/tiny-random-bert''' ) __a = 13 __a = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) __a = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) __a = random_attention_mask([batch_size, 4] ) __a = {'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask} return model, inputs def __UpperCAmelCase ( self , _a , _a ): __a = TFViTModel(_a , name='''vision_model''' ) __a = TFBertModel(_a , name='''text_model''' ) return vision_model, text_model def __UpperCAmelCase ( self ): __a = TFViTModelTester(self ) __a = TFBertModelTester(self ) __a = vit_model_tester.prepare_config_and_inputs() __a = bert_model_tester.prepare_config_and_inputs() __a , __a , __a = vision_config_and_inputs ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): # DeiT repo doesn't have TF weights, but we don't actually use the weights at all so let's # just reinitialize it. __a = TFVisionTextDualEncoderModel.from_vision_text_pretrained( '''Rocketknight1/tiny-random-deit-tf''' , '''hf-internal-testing/tiny-random-roberta''' ) __a = 13 __a = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) __a = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) __a = random_attention_mask([batch_size, 4] ) __a = {'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask} return model, inputs def __UpperCAmelCase ( self , _a , _a , _a , _a , _a=None , **_a ): __a , __a = self.get_vision_text_model(_a , _a ) __a = TFVisionTextDualEncoderModel(vision_model=_a , text_model=_a ) __a = model( input_ids=_a , pixel_values=_a , attention_mask=_a , output_attentions=_a ) __a = output.vision_model_output.attentions self.assertEqual(len(_a ) , vision_config.num_hidden_layers ) # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens) __a = to_atuple(vision_model.config.image_size ) __a = to_atuple(vision_model.config.patch_size ) __a = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) __a = num_patches + 2 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len) ) __a = output.text_model_output.attentions self.assertEqual(len(_a ) , text_config.num_hidden_layers ) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def __UpperCAmelCase ( self , _a , _a ): __a = TFDeiTModel(_a , name='''vision_model''' ) __a = TFRobertaModel(_a , name='''text_model''' ) return vision_model, text_model def __UpperCAmelCase ( self ): __a = TFDeiTModelTester(self ) __a = TFRobertaModelTester(self ) __a = vit_model_tester.prepare_config_and_inputs() __a = bert_model_tester.prepare_config_and_inputs() __a , __a , __a = vision_config_and_inputs ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = TFVisionTextDualEncoderModel.from_vision_text_pretrained( '''Rocketknight1/tiny-random-clip-tf''' , '''hf-internal-testing/tiny-random-bert''' ) __a = 13 __a = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) __a = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) __a = random_attention_mask([batch_size, 4] ) __a = {'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask} return model, inputs def __UpperCAmelCase ( self , _a , _a ): __a = TFCLIPVisionModel(_a , name='''vision_model''' ) __a = TFBertModel(_a , name='''text_model''' ) return vision_model, text_model def __UpperCAmelCase ( self ): __a = TFCLIPVisionModelTester(self ) __a = TFBertModelTester(self ) __a = clip_model_tester.prepare_config_and_inputs() __a = bert_model_tester.prepare_config_and_inputs() __a , __a = vision_config_and_inputs ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_vision @require_tf class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def __UpperCAmelCase ( self ): __a = TFVisionTextDualEncoderModel.from_pretrained( '''clip-italian/clip-italian''' , logit_scale_init_value=1.0 , from_pt=_a ) __a = VisionTextDualEncoderProcessor.from_pretrained('''clip-italian/clip-italian''' ) __a = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) __a = processor( text=['''una foto di un gatto''', '''una foto di un cane'''] , images=_a , padding=_a , return_tensors='''np''' ) __a = model(**_a ) # verify the logits self.assertEqual(outputs.logits_per_image.shape , (inputs.pixel_values.shape[0], inputs.input_ids.shape[0]) ) self.assertEqual( outputs.logits_per_text.shape , (inputs.input_ids.shape[0], inputs.pixel_values.shape[0]) , ) __a = np.array([[1.228_4727, 0.310_4122]] ) self.assertTrue(np.allclose(outputs.logits_per_image.numpy() , _a , atol=1E-3 ) )
695
"""simple docstring""" from __future__ import annotations def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> float: if days_between_payments <= 0: raise ValueError('''days_between_payments must be > 0''' ) if daily_interest_rate < 0: raise ValueError('''daily_interest_rate must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return principal * daily_interest_rate * days_between_payments def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float , ) -> float: if number_of_compounding_periods <= 0: raise ValueError('''number_of_compounding_periods must be > 0''' ) if nominal_annual_interest_rate_percentage < 0: raise ValueError('''nominal_annual_interest_rate_percentage must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return principal * ( (1 + nominal_annual_interest_rate_percentage) ** number_of_compounding_periods - 1 ) def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float , ) -> float: if number_of_years <= 0: raise ValueError('''number_of_years must be > 0''' ) if nominal_annual_percentage_rate < 0: raise ValueError('''nominal_annual_percentage_rate must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return compound_interest( lowerCAmelCase__ , nominal_annual_percentage_rate / 365 , number_of_years * 365 ) if __name__ == "__main__": import doctest doctest.testmod()
695
1
"""simple docstring""" import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "google/pix2struct-textcaps-base": ( "https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json" ), } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[Any] = 'pix2struct_text_model' __UpperCAmelCase : List[Any] = ['past_key_values'] __UpperCAmelCase : Optional[Any] = { 'hidden_size': 'hidden_size', 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self , _a=50_244 , _a=768 , _a=64 , _a=2_048 , _a=12 , _a=12 , _a=32 , _a=128 , _a=0.1 , _a=1E-6 , _a=1.0 , _a="gelu_new" , _a=0 , _a=False , _a=0 , _a=1 , _a=False , _a=True , **_a , ): __a = vocab_size __a = hidden_size __a = d_kv __a = d_ff __a = num_layers __a = num_heads __a = relative_attention_num_buckets __a = relative_attention_max_distance __a = dropout_rate __a = layer_norm_epsilon __a = initializer_factor __a = use_cache __a = eos_token_id __a = decoder_start_token_id # for backwards compatibility __a = dense_act_fn super().__init__( pad_token_id=_a , eos_token_id=_a , decoder_start_token_id=_a , tie_word_embeddings=_a , is_decoder=_a , **_a , ) @classmethod def __UpperCAmelCase ( cls , _a , **_a ): cls._set_token_in_kwargs(_a ) __a , __a = cls.get_config_dict(_a , **_a ) # get the text config dict if we are loading from Pix2StructConfig if config_dict.get('''model_type''' ) == "pix2struct": __a = config_dict['''text_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(_a , **_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : int = 'pix2struct_vision_model' def __init__( self , _a=768 , _a=768 , _a=2_048 , _a=64 , _a=12 , _a=12 , _a="gelu_new" , _a=1E-6 , _a=0.0 , _a=0.0 , _a=1E-10 , _a=1.0 , _a=4_096 , _a=32 , _a=128 , **_a , ): super().__init__(**_a ) __a = hidden_size __a = patch_embed_hidden_size __a = d_ff __a = dropout_rate __a = num_hidden_layers __a = num_attention_heads __a = initializer_range __a = initializer_factor __a = attention_dropout __a = layer_norm_eps __a = dense_act_fn __a = seq_len __a = relative_attention_num_buckets __a = relative_attention_max_distance __a = d_kv @classmethod def __UpperCAmelCase ( cls , _a , **_a ): cls._set_token_in_kwargs(_a ) __a , __a = cls.get_config_dict(_a , **_a ) # get the vision config dict if we are loading from Pix2StructConfig if config_dict.get('''model_type''' ) == "pix2struct": __a = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(_a , **_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Dict = 'pix2struct' __UpperCAmelCase : Union[str, Any] = True def __init__( self , _a=None , _a=None , _a=1.0 , _a=0.02 , _a=False , _a=False , _a=True , **_a , ): super().__init__(tie_word_embeddings=_a , is_encoder_decoder=_a , **_a ) if text_config is None: __a = {} logger.info('''text_config is None. Initializing the Pix2StructTextConfig with default values.''' ) if vision_config is None: __a = {} logger.info('''vision_config is None. Initializing the Pix2StructVisionConfig with default values.''' ) __a = PixaStructTextConfig(**_a ) __a = PixaStructVisionConfig(**_a ) __a = self.text_config.decoder_start_token_id __a = self.text_config.pad_token_id __a = self.text_config.eos_token_id __a = initializer_factor __a = initializer_range __a = self.initializer_range __a = self.initializer_range __a = is_vqa @classmethod def __UpperCAmelCase ( cls , _a , _a , **_a ): return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **_a ) def __UpperCAmelCase ( self ): __a = copy.deepcopy(self.__dict__ ) __a = self.text_config.to_dict() __a = self.vision_config.to_dict() __a = self.__class__.model_type return output
695
"""simple docstring""" def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any=False ) -> Any: if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = len(set_a.intersection(lowerCAmelCase__ ) ) if alternative_union: __a = len(lowerCAmelCase__ ) + len(lowerCAmelCase__ ) else: __a = len(set_a.union(lowerCAmelCase__ ) ) return intersection / union if isinstance(lowerCAmelCase__ , (list, tuple) ) and isinstance(lowerCAmelCase__ , (list, tuple) ): __a = [element for element in set_a if element in set_b] if alternative_union: __a = len(lowerCAmelCase__ ) + len(lowerCAmelCase__ ) return len(lowerCAmelCase__ ) / union else: __a = set_a + [element for element in set_b if element not in set_a] return len(lowerCAmelCase__ ) / len(lowerCAmelCase__ ) return len(lowerCAmelCase__ ) / len(lowerCAmelCase__ ) return None if __name__ == "__main__": lowercase_ = {"a", "b", "c", "d", "e"} lowercase_ = {"c", "d", "e", "f", "h", "i"} print(jaccard_similarity(set_a, set_b))
695
1
"""simple docstring""" import unittest import numpy as np from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING from transformers.pipelines import AudioClassificationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_torchaudio, slow, ) from .test_pipelines_common import ANY @is_pipeline_test class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING __UpperCAmelCase : List[str] = TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING def __UpperCAmelCase ( self , _a , _a , _a ): __a = AudioClassificationPipeline(model=_a , feature_extractor=_a ) # test with a raw waveform __a = np.zeros((34_000,) ) __a = np.zeros((14_000,) ) return audio_classifier, [audioa, audio] def __UpperCAmelCase ( self , _a , _a ): __a , __a = examples __a = audio_classifier(_a ) # by default a model is initialized with num_labels=2 self.assertEqual( _a , [ {'''score''': ANY(_a ), '''label''': ANY(_a )}, {'''score''': ANY(_a ), '''label''': ANY(_a )}, ] , ) __a = audio_classifier(_a , top_k=1 ) self.assertEqual( _a , [ {'''score''': ANY(_a ), '''label''': ANY(_a )}, ] , ) self.run_torchaudio(_a ) @require_torchaudio def __UpperCAmelCase ( self , _a ): import datasets # test with a local file __a = datasets.load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) __a = dataset[0]['''audio''']['''array'''] __a = audio_classifier(_a ) self.assertEqual( _a , [ {'''score''': ANY(_a ), '''label''': ANY(_a )}, {'''score''': ANY(_a ), '''label''': ANY(_a )}, ] , ) @require_torch def __UpperCAmelCase ( self ): __a = '''anton-l/wav2vec2-random-tiny-classifier''' __a = pipeline('''audio-classification''' , model=_a ) __a = np.ones((8_000,) ) __a = audio_classifier(_a , top_k=4 ) __a = [ {'''score''': 0.0842, '''label''': '''no'''}, {'''score''': 0.0838, '''label''': '''up'''}, {'''score''': 0.0837, '''label''': '''go'''}, {'''score''': 0.0834, '''label''': '''right'''}, ] __a = [ {'''score''': 0.0845, '''label''': '''stop'''}, {'''score''': 0.0844, '''label''': '''on'''}, {'''score''': 0.0841, '''label''': '''right'''}, {'''score''': 0.0834, '''label''': '''left'''}, ] self.assertIn(nested_simplify(_a , decimals=4 ) , [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) __a = {'''array''': np.ones((8_000,) ), '''sampling_rate''': audio_classifier.feature_extractor.sampling_rate} __a = audio_classifier(_a , top_k=4 ) self.assertIn(nested_simplify(_a , decimals=4 ) , [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) @require_torch @slow def __UpperCAmelCase ( self ): import datasets __a = '''superb/wav2vec2-base-superb-ks''' __a = pipeline('''audio-classification''' , model=_a ) __a = datasets.load_dataset('''anton-l/superb_dummy''' , '''ks''' , split='''test''' ) __a = np.array(dataset[3]['''speech'''] , dtype=np.floataa ) __a = audio_classifier(_a , top_k=4 ) self.assertEqual( nested_simplify(_a , decimals=3 ) , [ {'''score''': 0.981, '''label''': '''go'''}, {'''score''': 0.007, '''label''': '''up'''}, {'''score''': 0.006, '''label''': '''_unknown_'''}, {'''score''': 0.001, '''label''': '''down'''}, ] , ) @require_tf @unittest.skip('''Audio classification is not implemented for TF''' ) def __UpperCAmelCase ( self ): pass
695
"""simple docstring""" from __future__ import annotations import requests def lowercase ( lowerCAmelCase__ : str ) -> dict: __a = f'''https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty''' return requests.get(lowerCAmelCase__ ).json() def lowercase ( lowerCAmelCase__ : int = 10 ) -> list[dict]: __a = '''https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty''' __a = requests.get(lowerCAmelCase__ ).json()[:max_stories] return [get_hackernews_story(lowerCAmelCase__ ) for story_id in story_ids] def lowercase ( lowerCAmelCase__ : int = 10 ) -> str: __a = hackernews_top_stories(lowerCAmelCase__ ) return "\n".join('''* [{title}]({url})'''.format(**lowerCAmelCase__ ) for story in stories ) if __name__ == "__main__": print(hackernews_top_stories_as_markdown())
695
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : list ) -> list: if len(lowerCAmelCase__ ) <= 1: return lst __a = 1 while i < len(lowerCAmelCase__ ): if lst[i - 1] <= lst[i]: i += 1 else: __a , __a = lst[i], lst[i - 1] i -= 1 if i == 0: __a = 1 return lst if __name__ == "__main__": lowercase_ = input("Enter numbers separated by a comma:\n").strip() lowercase_ = [int(item) for item in user_input.split(",")] print(gnome_sort(unsorted))
695
"""simple docstring""" import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES from ...utils import logging from ..auto import CONFIG_MAPPING lowercase_ = logging.get_logger(__name__) lowercase_ = { "salesforce/blip2-opt-2.7b": "https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json", } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = 'blip_2_vision_model' def __init__( self , _a=1_408 , _a=6_144 , _a=39 , _a=16 , _a=224 , _a=14 , _a="gelu" , _a=0.0_0001 , _a=0.0 , _a=1E-10 , _a=True , **_a , ): super().__init__(**_a ) __a = hidden_size __a = intermediate_size __a = num_hidden_layers __a = num_attention_heads __a = patch_size __a = image_size __a = initializer_range __a = attention_dropout __a = layer_norm_eps __a = hidden_act __a = qkv_bias @classmethod def __UpperCAmelCase ( cls , _a , **_a ): cls._set_token_in_kwargs(_a ) __a , __a = cls.get_config_dict(_a , **_a ) # get the vision config dict if we are loading from Blip2Config if config_dict.get('''model_type''' ) == "blip-2": __a = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(_a , **_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : str = 'blip_2_qformer' def __init__( self , _a=30_522 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=0.02 , _a=1E-12 , _a=0 , _a="absolute" , _a=2 , _a=1_408 , **_a , ): super().__init__(pad_token_id=_a , **_a ) __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = hidden_act __a = intermediate_size __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = initializer_range __a = layer_norm_eps __a = position_embedding_type __a = cross_attention_frequency __a = encoder_hidden_size @classmethod def __UpperCAmelCase ( cls , _a , **_a ): cls._set_token_in_kwargs(_a ) __a , __a = cls.get_config_dict(_a , **_a ) # get the qformer config dict if we are loading from Blip2Config if config_dict.get('''model_type''' ) == "blip-2": __a = config_dict['''qformer_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(_a , **_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Any = 'blip-2' __UpperCAmelCase : List[str] = True def __init__( self , _a=None , _a=None , _a=None , _a=32 , **_a ): super().__init__(**_a ) if vision_config is None: __a = {} logger.info('''vision_config is None. initializing the Blip2VisionConfig with default values.''' ) if qformer_config is None: __a = {} logger.info('''qformer_config is None. Initializing the Blip2QFormerConfig with default values.''' ) if text_config is None: __a = {} logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).''' ) __a = BlipaVisionConfig(**_a ) __a = BlipaQFormerConfig(**_a ) __a = text_config['''model_type'''] if '''model_type''' in text_config else '''opt''' __a = CONFIG_MAPPING[text_model_type](**_a ) __a = self.text_config.tie_word_embeddings __a = self.text_config.is_encoder_decoder __a = num_query_tokens __a = self.vision_config.hidden_size __a = self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES __a = 1.0 __a = 0.02 @classmethod def __UpperCAmelCase ( cls , _a , _a , _a , **_a , ): return cls( vision_config=vision_config.to_dict() , qformer_config=qformer_config.to_dict() , text_config=text_config.to_dict() , **_a , ) def __UpperCAmelCase ( self ): __a = copy.deepcopy(self.__dict__ ) __a = self.vision_config.to_dict() __a = self.qformer_config.to_dict() __a = self.text_config.to_dict() __a = self.__class__.model_type return output
695
1
"""simple docstring""" from typing import List, Optional, Union import torch from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) lowercase_ = logging.get_logger(__name__) # pylint: disable=invalid-name lowercase_ = "\n Examples:\n ```py\n >>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline\n >>> import torch\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\"kandinsky-community/kandinsky-2-2-prior\")\n >>> pipe_prior.to(\"cuda\")\n >>> prompt = \"red cat, 4k photo\"\n >>> out = pipe_prior(prompt)\n >>> image_emb = out.image_embeds\n >>> zero_image_emb = out.negative_image_embeds\n >>> pipe = KandinskyV22Pipeline.from_pretrained(\"kandinsky-community/kandinsky-2-2-decoder\")\n >>> pipe.to(\"cuda\")\n >>> image = pipe(\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... height=768,\n ... width=768,\n ... num_inference_steps=50,\n ... ).images\n >>> image[0].save(\"cat.png\")\n ```\n" def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Any=8 ) -> Any: __a = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 __a = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , _a , _a , _a , ): super().__init__() self.register_modules( unet=_a , scheduler=_a , movq=_a , ) __a = 2 ** (len(self.movq.config.block_out_channels ) - 1) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , _a ): if latents is None: __a = randn_tensor(_a , generator=_a , device=_a , dtype=_a ) else: if latents.shape != shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {shape}''' ) __a = latents.to(_a ) __a = latents * scheduler.init_noise_sigma return latents def __UpperCAmelCase ( self , _a=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) __a = torch.device(f'''cuda:{gpu_id}''' ) __a = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_a , _a ) def __UpperCAmelCase ( self , _a=0 ): if is_accelerate_available() and is_accelerate_version('''>=''' , '''0.17.0.dev0''' ): from accelerate import cpu_offload_with_hook else: raise ImportError('''`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.''' ) __a = torch.device(f'''cuda:{gpu_id}''' ) if self.device.type != "cpu": self.to('''cpu''' , silence_dtype_warnings=_a ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) __a = None for cpu_offloaded_model in [self.unet, self.movq]: __a , __a = cpu_offload_with_hook(_a , _a , prev_module_hook=_a ) # We'll offload the last model manually. __a = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def __UpperCAmelCase ( self ): if not hasattr(self.unet , '''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(_a , '''_hf_hook''' ) and hasattr(module._hf_hook , '''execution_device''' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(_a ) def __call__( self , _a , _a , _a = 512 , _a = 512 , _a = 100 , _a = 4.0 , _a = 1 , _a = None , _a = None , _a = "pil" , _a = True , ): __a = self._execution_device __a = guidance_scale > 1.0 if isinstance(_a , _a ): __a = torch.cat(_a , dim=0 ) __a = image_embeds.shape[0] * num_images_per_prompt if isinstance(_a , _a ): __a = torch.cat(_a , dim=0 ) if do_classifier_free_guidance: __a = image_embeds.repeat_interleave(_a , dim=0 ) __a = negative_image_embeds.repeat_interleave(_a , dim=0 ) __a = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=_a ) self.scheduler.set_timesteps(_a , device=_a ) __a = self.scheduler.timesteps __a = self.unet.config.in_channels __a , __a = downscale_height_and_width(_a , _a , self.movq_scale_factor ) # create initial latent __a = self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , _a , _a , _a , self.scheduler , ) for i, t in enumerate(self.progress_bar(_a ) ): # expand the latents if we are doing classifier free guidance __a = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents __a = {'''image_embeds''': image_embeds} __a = self.unet( sample=_a , timestep=_a , encoder_hidden_states=_a , added_cond_kwargs=_a , return_dict=_a , )[0] if do_classifier_free_guidance: __a , __a = noise_pred.split(latents.shape[1] , dim=1 ) __a , __a = noise_pred.chunk(2 ) __a , __a = variance_pred.chunk(2 ) __a = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) __a = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , '''variance_type''' ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): __a , __a = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 __a = self.scheduler.step( _a , _a , _a , generator=_a , )[0] # post-processing __a = self.movq.decode(_a , force_not_quantize=_a )['''sample'''] if output_type not in ["pt", "np", "pil"]: raise ValueError(f'''Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}''' ) if output_type in ["np", "pil"]: __a = image * 0.5 + 0.5 __a = image.clamp(0 , 1 ) __a = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": __a = self.numpy_to_pil(_a ) if not return_dict: return (image,) return ImagePipelineOutput(images=_a )
695
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType lowercase_ = logging.get_logger(__name__) lowercase_ = { "microsoft/deberta-v2-xlarge": "https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json", "microsoft/deberta-v2-xxlarge": "https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json", "microsoft/deberta-v2-xlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json" ), "microsoft/deberta-v2-xxlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json" ), } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Dict = 'deberta-v2' def __init__( self , _a=128_100 , _a=1_536 , _a=24 , _a=24 , _a=6_144 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=0 , _a=0.02 , _a=1E-7 , _a=False , _a=-1 , _a=0 , _a=True , _a=None , _a=0 , _a="gelu" , **_a , ): super().__init__(**_a ) __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = initializer_range __a = relative_attention __a = max_relative_positions __a = pad_token_id __a = position_biased_input # Backwards compatibility if type(_a ) == str: __a = [x.strip() for x in pos_att_type.lower().split('''|''' )] __a = pos_att_type __a = vocab_size __a = layer_norm_eps __a = kwargs.get('''pooler_hidden_size''' , _a ) __a = pooler_dropout __a = pooler_hidden_act class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def __UpperCAmelCase ( self ): if self.task == "multiple-choice": __a = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: __a = {0: '''batch''', 1: '''sequence'''} if self._config.type_vocab_size > 0: return OrderedDict( [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)] ) else: return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)] ) @property def __UpperCAmelCase ( self ): return 12 def __UpperCAmelCase ( self , _a , _a = -1 , _a = -1 , _a = -1 , _a = False , _a = None , _a = 3 , _a = 40 , _a = 40 , _a = None , ): __a = super().generate_dummy_inputs(preprocessor=_a , framework=_a ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
695
1
"""simple docstring""" from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow if is_tf_available(): import tensorflow as tf from transformers import AutoTokenizer, TFAutoModelForSeqaSeqLM @require_tf @require_sentencepiece @require_tokenizers class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def __UpperCAmelCase ( self ): __a = TFAutoModelForSeqaSeqLM.from_pretrained('''google/mt5-small''' ) __a = AutoTokenizer.from_pretrained('''google/mt5-small''' ) __a = tokenizer('''Hello there''' , return_tensors='''tf''' ).input_ids __a = tokenizer('''Hi I am''' , return_tensors='''tf''' ).input_ids __a = model(_a , labels=_a ).loss __a = -tf.math.reduce_mean(_a ).numpy() __a = -21.22_8168 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 2E-4 )
695
"""simple docstring""" import importlib.metadata import operator import re import sys from typing import Optional from packaging import version lowercase_ = { "<": operator.lt, "<=": operator.le, "==": operator.eq, "!=": operator.ne, ">=": operator.ge, ">": operator.gt, } def lowercase ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any] ) -> Dict: 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(lowerCAmelCase__ ) , version.parse(lowerCAmelCase__ ) ): raise ImportError( f'''{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}''' ) def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None ) -> None: __a = f'''\n{hint}''' if hint is not None else '''''' # non-versioned check if re.match(r'''^[\w_\-\d]+$''' , lowerCAmelCase__ ): __a , __a , __a = requirement, None, None else: __a = re.findall(r'''^([^!=<>\s]+)([\s!=<>]{1,2}.+)''' , lowerCAmelCase__ ) if not match: raise ValueError( '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but''' f''' got {requirement}''' ) __a , __a = match[0] __a = want_full.split(''',''' ) # there could be multiple requirements __a = {} for w in want_range: __a = re.findall(r'''^([\s!=<>]{1,2})(.+)''' , lowerCAmelCase__ ) if not match: raise ValueError( '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,''' f''' but got {requirement}''' ) __a , __a = match[0] __a = want_ver if op not in ops: raise ValueError(f'''{requirement}: need one of {list(ops.keys() )}, but got {op}''' ) # special case if pkg == "python": __a = '''.'''.join([str(lowerCAmelCase__ ) for x in sys.version_info[:3]] ) for op, want_ver in wanted.items(): _compare_versions(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) return # check if any version is installed try: __a = importlib.metadata.version(lowerCAmelCase__ ) 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(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Tuple ) -> Optional[Any]: __a = '''Try: pip install transformers -U or pip install -e \'.[dev]\' if you\'re working with git main''' return require_version(lowerCAmelCase__ , lowerCAmelCase__ )
695
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int = 50 ) -> int: __a = [1] * (length + 1) for row_length in range(3 , length + 1 ): for block_length in range(3 , row_length + 1 ): for block_start in range(row_length - block_length ): ways_number[row_length] += ways_number[ row_length - block_start - block_length - 1 ] ways_number[row_length] += 1 return ways_number[length] if __name__ == "__main__": print(F'''{solution() = }''')
695
"""simple docstring""" from __future__ import annotations lowercase_ = list[tuple[int, int]] lowercase_ = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] lowercase_ = ([-1, 0], [0, -1], [1, 0], [0, 1]) # up, left, down, right class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a , _a , _a , _a , _a , ): __a = pos_x __a = pos_y __a = (pos_y, pos_x) __a = goal_x __a = goal_y __a = g_cost __a = parent __a = self.calculate_heuristic() def __UpperCAmelCase ( self ): __a = abs(self.pos_x - self.goal_x ) __a = abs(self.pos_y - self.goal_y ) return dx + dy def __lt__( self , _a ): return self.f_cost < other.f_cost class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a ): __a = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , _a ) __a = Node(goal[1] , goal[0] , goal[1] , goal[0] , 99_999 , _a ) __a = [self.start] __a = [] __a = False def __UpperCAmelCase ( self ): while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() __a = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: __a = True return self.retrace_path(_a ) self.closed_nodes.append(_a ) __a = self.get_successors(_a ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(_a ) else: # retrieve the best current path __a = self.open_nodes.pop(self.open_nodes.index(_a ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(_a ) else: self.open_nodes.append(_a ) if not self.reached: return [self.start.pos] return None def __UpperCAmelCase ( self , _a ): __a = [] for action in delta: __a = parent.pos_x + action[1] __a = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(_a ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( _a , _a , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , _a , ) ) return successors def __UpperCAmelCase ( self , _a ): __a = node __a = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) __a = current_node.parent path.reverse() return path if __name__ == "__main__": lowercase_ = (0, 0) lowercase_ = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) print("------") lowercase_ = GreedyBestFirst(init, goal) lowercase_ = greedy_bf.search() if path: for pos_x, pos_y in path: lowercase_ = 2 for elem in grid: print(elem)
695
1
"""simple docstring""" import re import string import numpy as np import datasets lowercase_ = "\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n" lowercase_ = "\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric(\"exact_match\")\n >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results[\"exact_match\"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric(\"exact_match\")\n >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results[\"exact_match\"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric(\"exact_match\")\n >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results[\"exact_match\"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric(\"exact_match\")\n >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results[\"exact_match\"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric(\"exact_match\")\n >>> refs = [\"The cat sat on the mat.\", \"Theaters are great.\", \"It's like comparing oranges and apples.\"]\n >>> preds = [\"The cat sat on the mat?\", \"Theaters are great.\", \"It's like comparing apples and oranges.\"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results[\"exact_match\"], 1))\n 33.3\n\n" lowercase_ = "\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __lowerCAmelCase ( datasets.Metric ): '''simple docstring''' def __UpperCAmelCase ( self ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , reference_urls=[] , ) def __UpperCAmelCase ( self , _a , _a , _a=None , _a=False , _a=False , _a=False , ): if regexes_to_ignore is not None: for s in regexes_to_ignore: __a = np.array([re.sub(_a , '''''' , _a ) for x in predictions] ) __a = np.array([re.sub(_a , '''''' , _a ) for x in references] ) else: __a = np.asarray(_a ) __a = np.asarray(_a ) if ignore_case: __a = np.char.lower(_a ) __a = np.char.lower(_a ) if ignore_punctuation: __a = string.punctuation.maketrans('''''' , '''''' , string.punctuation ) __a = np.char.translate(_a , table=_a ) __a = np.char.translate(_a , table=_a ) if ignore_numbers: __a = string.digits.maketrans('''''' , '''''' , string.digits ) __a = np.char.translate(_a , table=_a ) __a = np.char.translate(_a , table=_a ) __a = predictions == references return {"exact_match": np.mean(_a ) * 100}
695
"""simple docstring""" import argparse import torch from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert from transformers.utils import logging logging.set_verbosity_info() def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str ) -> List[Any]: # Initialise PyTorch model __a = RemBertConfig.from_json_file(lowerCAmelCase__ ) print('''Building PyTorch model from configuration: {}'''.format(str(lowerCAmelCase__ ) ) ) __a = RemBertModel(lowerCAmelCase__ ) # Load weights from tf checkpoint load_tf_weights_in_rembert(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Save pytorch-model print('''Save PyTorch model to {}'''.format(lowerCAmelCase__ ) ) torch.save(model.state_dict() , lowerCAmelCase__ ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--rembert_config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained RemBERT 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." ) lowercase_ = parser.parse_args() convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)
695
1
"""simple docstring""" import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast @require_vision class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = tempfile.mkdtemp() __a = BlipImageProcessor() __a = BertTokenizer.from_pretrained('''hf-internal-testing/tiny-random-BertModel''' ) __a = BlipProcessor(_a , _a ) processor.save_pretrained(self.tmpdirname ) def __UpperCAmelCase ( self , **_a ): return AutoProcessor.from_pretrained(self.tmpdirname , **_a ).tokenizer def __UpperCAmelCase ( self , **_a ): return AutoProcessor.from_pretrained(self.tmpdirname , **_a ).image_processor def __UpperCAmelCase ( self ): shutil.rmtree(self.tmpdirname ) def __UpperCAmelCase ( self ): __a = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] __a = [Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) for x in image_inputs] return image_inputs def __UpperCAmelCase ( self ): __a = BlipProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) __a = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) __a = self.get_image_processor(do_normalize=_a , padding_value=1.0 ) __a = BlipProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=_a , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , _a ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def __UpperCAmelCase ( self ): __a = self.get_image_processor() __a = self.get_tokenizer() __a = BlipProcessor(tokenizer=_a , image_processor=_a ) __a = self.prepare_image_inputs() __a = image_processor(_a , return_tensors='''np''' ) __a = processor(images=_a , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2 ) def __UpperCAmelCase ( self ): __a = self.get_image_processor() __a = self.get_tokenizer() __a = BlipProcessor(tokenizer=_a , image_processor=_a ) __a = '''lower newer''' __a = processor(text=_a ) __a = tokenizer(_a , return_token_type_ids=_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __UpperCAmelCase ( self ): __a = self.get_image_processor() __a = self.get_tokenizer() __a = BlipProcessor(tokenizer=_a , image_processor=_a ) __a = '''lower newer''' __a = self.prepare_image_inputs() __a = processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ['''pixel_values''', '''input_ids''', '''attention_mask'''] ) # test if it raises when no input is passed with pytest.raises(_a ): processor() def __UpperCAmelCase ( self ): __a = self.get_image_processor() __a = self.get_tokenizer() __a = BlipProcessor(tokenizer=_a , image_processor=_a ) __a = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] __a = processor.batch_decode(_a ) __a = tokenizer.batch_decode(_a ) self.assertListEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = self.get_image_processor() __a = self.get_tokenizer() __a = BlipProcessor(tokenizer=_a , image_processor=_a ) __a = '''lower newer''' __a = self.prepare_image_inputs() __a = processor(text=_a , images=_a ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) , ['''pixel_values''', '''input_ids''', '''attention_mask'''] )
695
"""simple docstring""" import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel lowercase_ = "0.12" # assumed parallelism: 8 @require_flax @is_staging_test class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @classmethod def __UpperCAmelCase ( cls ): __a = TOKEN HfFolder.save_token(_a ) @classmethod def __UpperCAmelCase ( cls ): try: delete_repo(token=cls._token , repo_id='''test-model-flax''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-model-flax-org''' ) except HTTPError: pass def __UpperCAmelCase ( self ): __a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) __a = FlaxBertModel(_a ) model.push_to_hub('''test-model-flax''' , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''test-model-flax''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(_a , repo_id='''test-model-flax''' , push_to_hub=_a , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) def __UpperCAmelCase ( self ): __a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) __a = FlaxBertModel(_a ) model.push_to_hub('''valid_org/test-model-flax-org''' , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-model-flax-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( _a , repo_id='''valid_org/test-model-flax-org''' , push_to_hub=_a , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : Dict ) -> Optional[int]: __a = True __a = flatten_dict(modela.params ) __a = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1e-4: __a = False return models_are_equal @require_flax class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) __a = FlaxBertModel(_a ) __a = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_a , _a ) ) with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertTrue(check_models_equal(_a , _a ) ) def __UpperCAmelCase ( self ): __a = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) __a = FlaxBertModel(_a ) __a = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_a , _a ) , max_shard_size='''10KB''' ) with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertTrue(check_models_equal(_a , _a ) ) def __UpperCAmelCase ( self ): __a = '''bert''' __a = '''hf-internal-testing/tiny-random-bert-subfolder''' with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertIsNotNone(_a ) def __UpperCAmelCase ( self ): __a = '''bert''' __a = '''hf-internal-testing/tiny-random-bert-sharded-subfolder''' with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertIsNotNone(_a )
695
1
"""simple docstring""" from math import asin, atan, cos, radians, sin, sqrt, tan lowercase_ = 6378137.0 lowercase_ = 6356752.314245 lowercase_ = 6_3_7_8_1_3_7 def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> float: __a = (AXIS_A - AXIS_B) / AXIS_A __a = atan((1 - flattening) * tan(radians(lowerCAmelCase__ ) ) ) __a = atan((1 - flattening) * tan(radians(lowerCAmelCase__ ) ) ) __a = radians(lowerCAmelCase__ ) __a = radians(lowerCAmelCase__ ) # Equation __a = sin((phi_a - phi_a) / 2 ) __a = sin((lambda_a - lambda_a) / 2 ) # Square both values sin_sq_phi *= sin_sq_phi sin_sq_lambda *= sin_sq_lambda __a = sqrt(sin_sq_phi + (cos(lowerCAmelCase__ ) * cos(lowerCAmelCase__ ) * sin_sq_lambda) ) return 2 * RADIUS * asin(lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod()
695
"""simple docstring""" import unittest from diffusers.models.unet_ad_blocks import * # noqa F403 from diffusers.utils import torch_device from .test_unet_blocks_common import UNetBlockTesterMixin class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = DownBlockaD # noqa F405 __UpperCAmelCase : Any = 'down' def __UpperCAmelCase ( self ): __a = [-0.0232, -0.9869, 0.8054, -0.0637, -0.1688, -1.4264, 0.4470, -1.3394, 0.0904] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = ResnetDownsampleBlockaD # noqa F405 __UpperCAmelCase : List[str] = 'down' def __UpperCAmelCase ( self ): __a = [0.0710, 0.2410, -0.7320, -1.0757, -1.1343, 0.3540, -0.0133, -0.2576, 0.0948] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[int] = AttnDownBlockaD # noqa F405 __UpperCAmelCase : Optional[Any] = 'down' def __UpperCAmelCase ( self ): __a = [0.0636, 0.8964, -0.6234, -1.0131, 0.0844, 0.4935, 0.3437, 0.0911, -0.2957] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : List[Any] = CrossAttnDownBlockaD # noqa F405 __UpperCAmelCase : Optional[Any] = 'down' def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.2238, -0.7396, -0.2255, -0.3829, 0.1925, 1.1665, 0.0603, -0.7295, 0.1983] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = SimpleCrossAttnDownBlockaD # noqa F405 __UpperCAmelCase : Any = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_encoder_hidden_states=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict @unittest.skipIf(torch_device == '''mps''' , '''MPS result is not consistent''' ) def __UpperCAmelCase ( self ): __a = [0.7921, -0.0992, -0.1962, -0.7695, -0.4242, 0.7804, 0.4737, 0.2765, 0.3338] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = SkipDownBlockaD # noqa F405 __UpperCAmelCase : Tuple = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_skip_sample=_a ) def __UpperCAmelCase ( self ): __a = [-0.0845, -0.2087, -0.2465, 0.0971, 0.1900, -0.0484, 0.2664, 0.4179, 0.5069] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : List[Any] = AttnSkipDownBlockaD # noqa F405 __UpperCAmelCase : Optional[int] = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_skip_sample=_a ) def __UpperCAmelCase ( self ): __a = [0.5539, 0.1609, 0.4924, 0.0537, -0.1995, 0.4050, 0.0979, -0.2721, -0.0642] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = DownEncoderBlockaD # noqa F405 __UpperCAmelCase : Optional[int] = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = { '''in_channels''': 32, '''out_channels''': 32, } __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [1.1102, 0.5302, 0.4872, -0.0023, -0.8042, 0.0483, -0.3489, -0.5632, 0.7626] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = AttnDownEncoderBlockaD # noqa F405 __UpperCAmelCase : Any = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = { '''in_channels''': 32, '''out_channels''': 32, } __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.8966, -0.1486, 0.8568, 0.8141, -0.9046, -0.1342, -0.0972, -0.7417, 0.1538] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = UNetMidBlockaD # noqa F405 __UpperCAmelCase : Any = 'mid' def __UpperCAmelCase ( self ): __a = { '''in_channels''': 32, '''temb_channels''': 128, } __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [-0.1062, 1.7248, 0.3494, 1.4569, -0.0910, -1.2421, -0.9984, 0.6736, 1.0028] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = UNetMidBlockaDCrossAttn # noqa F405 __UpperCAmelCase : str = 'mid' def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.0187, 2.4220, 0.4484, 1.1203, -0.6121, -1.5122, -0.8270, 0.7851, 1.8335] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Any = UNetMidBlockaDSimpleCrossAttn # noqa F405 __UpperCAmelCase : List[Any] = 'mid' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_encoder_hidden_states=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.7143, 1.9974, 0.5448, 1.3977, 0.1282, -1.1237, -1.4238, 0.5530, 0.8880] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = UpBlockaD # noqa F405 __UpperCAmelCase : Union[str, Any] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [-0.2041, -0.4165, -0.3022, 0.0041, -0.6628, -0.7053, 0.1928, -0.0325, 0.0523] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = ResnetUpsampleBlockaD # noqa F405 __UpperCAmelCase : int = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [0.2287, 0.3549, -0.1346, 0.4797, -0.1715, -0.9649, 0.7305, -0.5864, -0.6244] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Dict = CrossAttnUpBlockaD # noqa F405 __UpperCAmelCase : List[Any] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [-0.1403, -0.3515, -0.0420, -0.1425, 0.3167, 0.5094, -0.2181, 0.5931, 0.5582] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = SimpleCrossAttnUpBlockaD # noqa F405 __UpperCAmelCase : Optional[int] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a , include_encoder_hidden_states=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.2645, 0.1480, 0.0909, 0.8044, -0.9758, -0.9083, 0.0994, -1.1453, -0.7402] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Any = AttnUpBlockaD # noqa F405 __UpperCAmelCase : List[Any] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) @unittest.skipIf(torch_device == '''mps''' , '''MPS result is not consistent''' ) def __UpperCAmelCase ( self ): __a = [0.0979, 0.1326, 0.0021, 0.0659, 0.2249, 0.0059, 0.1132, 0.5952, 0.1033] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Any = SkipUpBlockaD # noqa F405 __UpperCAmelCase : str = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [-0.0893, -0.1234, -0.1506, -0.0332, 0.0123, -0.0211, 0.0566, 0.0143, 0.0362] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = AttnSkipUpBlockaD # noqa F405 __UpperCAmelCase : int = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [0.0361, 0.0617, 0.2787, -0.0350, 0.0342, 0.3421, -0.0843, 0.0913, 0.3015] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = UpDecoderBlockaD # noqa F405 __UpperCAmelCase : List[str] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = {'''in_channels''': 32, '''out_channels''': 32} __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.4404, 0.1998, -0.9886, -0.3320, -0.3128, -0.7034, -0.6955, -0.2338, -0.3137] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[int] = AttnUpDecoderBlockaD # noqa F405 __UpperCAmelCase : Any = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = {'''in_channels''': 32, '''out_channels''': 32} __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.6738, 0.4491, 0.1055, 1.0710, 0.7316, 0.3339, 0.3352, 0.1023, 0.3568] super().test_output(_a )
695
1
"""simple docstring""" import os import unicodedata 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 SPIECE_UNDERLINE, logging lowercase_ = logging.get_logger(__name__) lowercase_ = {"vocab_file": "spiece.model"} lowercase_ = { "vocab_file": { "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model", "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model", } } lowercase_ = { "xlnet-base-cased": None, "xlnet-large-cased": None, } # Segments (not really needed) lowercase_ = 0 lowercase_ = 1 lowercase_ = 2 lowercase_ = 3 lowercase_ = 4 class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : int = VOCAB_FILES_NAMES __UpperCAmelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __UpperCAmelCase : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCAmelCase : List[str] = 'left' def __init__( self , _a , _a=False , _a=True , _a=False , _a="<s>" , _a="</s>" , _a="<unk>" , _a="<sep>" , _a="<pad>" , _a="<cls>" , _a="<mask>" , _a=["<eop>", "<eod>"] , _a = None , **_a , ): # Mask token behave like a normal word, i.e. include the space before it __a = AddedToken(_a , lstrip=_a , rstrip=_a ) if isinstance(_a , _a ) else mask_token __a = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=_a , remove_space=_a , keep_accents=_a , bos_token=_a , eos_token=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , additional_special_tokens=_a , sp_model_kwargs=self.sp_model_kwargs , **_a , ) __a = 3 __a = do_lower_case __a = remove_space __a = keep_accents __a = vocab_file __a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_a ) @property def __UpperCAmelCase ( self ): return len(self.sp_model ) def __UpperCAmelCase ( self ): __a = {self.convert_ids_to_tokens(_a ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ): __a = self.__dict__.copy() __a = None return state def __setstate__( self , _a ): __a = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): __a = {} __a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __UpperCAmelCase ( self , _a ): if self.remove_space: __a = ''' '''.join(inputs.strip().split() ) else: __a = inputs __a = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: __a = unicodedata.normalize('''NFKD''' , _a ) __a = ''''''.join([c for c in outputs if not unicodedata.combining(_a )] ) if self.do_lower_case: __a = outputs.lower() return outputs def __UpperCAmelCase ( self , _a ): __a = self.preprocess_text(_a ) __a = self.sp_model.encode(_a , out_type=_a ) __a = [] for piece in pieces: if len(_a ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): __a = self.sp_model.EncodeAsPieces(piece[:-1].replace(_a , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: __a = cur_pieces[1:] else: __a = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(_a ) else: new_pieces.append(_a ) return new_pieces def __UpperCAmelCase ( self , _a ): return self.sp_model.PieceToId(_a ) def __UpperCAmelCase ( self , _a ): return self.sp_model.IdToPiece(_a ) def __UpperCAmelCase ( self , _a ): __a = ''''''.join(_a ).replace(_a , ''' ''' ).strip() return out_string def __UpperCAmelCase ( self , _a , _a = False , _a = None , _a = True , **_a , ): __a = kwargs.pop('''use_source_tokenizer''' , _a ) __a = self.convert_ids_to_tokens(_a , skip_special_tokens=_a ) # 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 __a = [] __a = [] 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(_a ) ) __a = [] sub_texts.append(_a ) else: current_sub_text.append(_a ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_a ) ) # Mimic the behavior of the Rust tokenizer: # By default, there are no spaces between special tokens __a = ''''''.join(_a ) __a = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: __a = self.clean_up_tokenization(_a ) return clean_text else: return text def __UpperCAmelCase ( self , _a , _a = None ): __a = [self.sep_token_id] __a = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def __UpperCAmelCase ( self , _a , _a = None , _a = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_a , token_ids_a=_a , already_has_special_tokens=_a ) if token_ids_a is not None: return ([0] * len(_a )) + [1] + ([0] * len(_a )) + [1, 1] return ([0] * len(_a )) + [1, 1] def __UpperCAmelCase ( self , _a , _a = None ): __a = [self.sep_token_id] __a = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def __UpperCAmelCase ( self , _a , _a = None ): if not os.path.isdir(_a ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __a = os.path.join( _a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _a ) elif not os.path.isfile(self.vocab_file ): with open(_a , '''wb''' ) as fi: __a = self.sp_model.serialized_model_proto() fi.write(_a ) return (out_vocab_file,)
695
"""simple docstring""" import copy from typing import Dict, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING from ..detr import DetrConfig from ..swin import SwinConfig lowercase_ = { "facebook/maskformer-swin-base-ade": ( "https://huggingface.co/facebook/maskformer-swin-base-ade/blob/main/config.json" ) # See all MaskFormer models at https://huggingface.co/models?filter=maskformer } lowercase_ = logging.get_logger(__name__) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : str = 'maskformer' __UpperCAmelCase : Optional[int] = {'hidden_size': 'mask_feature_size'} __UpperCAmelCase : Any = ['resnet', 'swin'] __UpperCAmelCase : Dict = ['detr'] def __init__( self , _a = 256 , _a = 256 , _a = 0.1 , _a = False , _a = None , _a = None , _a = 0.02 , _a = 1.0 , _a = 1.0 , _a = 1.0 , _a = 20.0 , _a = None , **_a , ): if backbone_config is None: # fall back to https://huggingface.co/microsoft/swin-base-patch4-window12-384-in22k __a = SwinConfig( image_size=384 , in_channels=3 , patch_size=4 , embed_dim=128 , depths=[2, 2, 18, 2] , num_heads=[4, 8, 16, 32] , window_size=12 , drop_path_rate=0.3 , out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] , ) if isinstance(_a , _a ): __a = backbone_config.pop('''model_type''' ) __a = CONFIG_MAPPING[backbone_model_type] __a = config_class.from_dict(_a ) # verify that the backbone is supported if backbone_config.model_type not in self.backbones_supported: logger.warning_once( f'''Backbone {backbone_config.model_type} is not a supported model and may not be compatible with MaskFormer. ''' f'''Supported model types: {','.join(self.backbones_supported )}''' ) if decoder_config is None: # fall back to https://huggingface.co/facebook/detr-resnet-50 __a = DetrConfig() else: # verify that the decoder is supported __a = ( decoder_config.pop('''model_type''' ) if isinstance(_a , _a ) else decoder_config.model_type ) if decoder_type not in self.decoders_supported: raise ValueError( f'''Transformer Decoder {decoder_type} not supported, please use one of''' f''' {','.join(self.decoders_supported )}''' ) if isinstance(_a , _a ): __a = CONFIG_MAPPING[decoder_type] __a = config_class.from_dict(_a ) __a = backbone_config __a = decoder_config # main feature dimension for the model __a = fpn_feature_size __a = mask_feature_size # initializer __a = init_std __a = init_xavier_std # Hungarian matcher && loss __a = cross_entropy_weight __a = dice_weight __a = mask_weight __a = use_auxiliary_loss __a = no_object_weight __a = output_auxiliary_logits __a = self.decoder_config.encoder_attention_heads __a = self.decoder_config.num_hidden_layers super().__init__(**_a ) @classmethod def __UpperCAmelCase ( cls , _a , _a , **_a ): return cls( backbone_config=_a , decoder_config=_a , **_a , ) def __UpperCAmelCase ( self ): __a = copy.deepcopy(self.__dict__ ) __a = self.backbone_config.to_dict() __a = self.decoder_config.to_dict() __a = self.__class__.model_type return output
695
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int ) -> "list[int]": if upper_limit < 0: raise ValueError('''Limit for the Catalan sequence must be ≥ 0''' ) __a = [0] * (upper_limit + 1) # Base case: C(0) = C(1) = 1 __a = 1 if upper_limit > 0: __a = 1 # Recurrence relation: C(i) = sum(C(j).C(i-j-1)), from j = 0 to i for i in range(2 , upper_limit + 1 ): for j in range(lowerCAmelCase__ ): catalan_list[i] += catalan_list[j] * catalan_list[i - j - 1] return catalan_list if __name__ == "__main__": print("\n********* Catalan Numbers Using Dynamic Programming ************\n") print("\n*** Enter -1 at any time to quit ***") print("\nEnter the upper limit (≥ 0) for the Catalan number sequence: ", end="") try: while True: lowercase_ = int(input().strip()) if N < 0: print("\n********* Goodbye!! ************") break else: print(F'''The Catalan numbers from 0 through {N} are:''') print(catalan_numbers(N)) print("Try another upper limit for the sequence: ", end="") except (NameError, ValueError): print("\n********* Invalid input, goodbye! ************\n") import doctest doctest.testmod()
695
"""simple docstring""" from __future__ import annotations from collections.abc import Generator import requests from bsa import BeautifulSoup lowercase_ = "https://www.indeed.co.in/jobs?q=mobile+app+development&l=" def lowercase ( lowerCAmelCase__ : str = "mumbai" ) -> Generator[tuple[str, str], None, None]: __a = BeautifulSoup(requests.get(url + location ).content , '''html.parser''' ) # This attribute finds out all the specifics listed in a job for job in soup.find_all('''div''' , attrs={'''data-tn-component''': '''organicJob'''} ): __a = job.find('''a''' , attrs={'''data-tn-element''': '''jobTitle'''} ).text.strip() __a = job.find('''span''' , {'''class''': '''company'''} ).text.strip() yield job_title, company_name if __name__ == "__main__": for i, job in enumerate(fetch_jobs("Bangalore"), 1): print(F'''Job {i:>2} is {job[0]} at {job[1]}''')
695
1
"""simple docstring""" import gc import math import unittest import torch from diffusers import UNetaDModel from diffusers.utils import floats_tensor, logging, slow, torch_all_close, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin lowercase_ = logging.get_logger(__name__) enable_full_determinism() class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Tuple = UNetaDModel __UpperCAmelCase : Tuple = 'sample' @property def __UpperCAmelCase ( self ): __a = 4 __a = 3 __a = (32, 32) __a = floats_tensor((batch_size, num_channels) + sizes ).to(_a ) __a = torch.tensor([10] ).to(_a ) return {"sample": noise, "timestep": time_step} @property def __UpperCAmelCase ( self ): return (3, 32, 32) @property def __UpperCAmelCase ( self ): return (3, 32, 32) def __UpperCAmelCase ( self ): __a = { '''block_out_channels''': (32, 64), '''down_block_types''': ('''DownBlock2D''', '''AttnDownBlock2D'''), '''up_block_types''': ('''AttnUpBlock2D''', '''UpBlock2D'''), '''attention_head_dim''': 3, '''out_channels''': 3, '''in_channels''': 3, '''layers_per_block''': 2, '''sample_size''': 32, } __a = self.dummy_input return init_dict, inputs_dict class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = UNetaDModel __UpperCAmelCase : Union[str, Any] = 'sample' @property def __UpperCAmelCase ( self ): __a = 4 __a = 4 __a = (32, 32) __a = floats_tensor((batch_size, num_channels) + sizes ).to(_a ) __a = torch.tensor([10] ).to(_a ) return {"sample": noise, "timestep": time_step} @property def __UpperCAmelCase ( self ): return (4, 32, 32) @property def __UpperCAmelCase ( self ): return (4, 32, 32) def __UpperCAmelCase ( self ): __a = { '''sample_size''': 32, '''in_channels''': 4, '''out_channels''': 4, '''layers_per_block''': 2, '''block_out_channels''': (32, 64), '''attention_head_dim''': 32, '''down_block_types''': ('''DownBlock2D''', '''DownBlock2D'''), '''up_block_types''': ('''UpBlock2D''', '''UpBlock2D'''), } __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a , __a = UNetaDModel.from_pretrained('''fusing/unet-ldm-dummy-update''' , output_loading_info=_a ) self.assertIsNotNone(_a ) self.assertEqual(len(loading_info['''missing_keys'''] ) , 0 ) model.to(_a ) __a = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != '''cuda''' , '''This test is supposed to run on GPU''' ) def __UpperCAmelCase ( self ): __a , __a = UNetaDModel.from_pretrained('''fusing/unet-ldm-dummy-update''' , output_loading_info=_a ) model.to(_a ) __a = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != '''cuda''' , '''This test is supposed to run on GPU''' ) def __UpperCAmelCase ( self ): # by defautl model loading will use accelerate as `low_cpu_mem_usage=True` __a , __a = UNetaDModel.from_pretrained('''fusing/unet-ldm-dummy-update''' , output_loading_info=_a ) model_accelerate.to(_a ) model_accelerate.eval() __a = torch.randn( 1 , model_accelerate.config.in_channels , model_accelerate.config.sample_size , model_accelerate.config.sample_size , generator=torch.manual_seed(0 ) , ) __a = noise.to(_a ) __a = torch.tensor([10] * noise.shape[0] ).to(_a ) __a = model_accelerate(_a , _a )['''sample'''] # two models don't need to stay in the device at the same time del model_accelerate torch.cuda.empty_cache() gc.collect() __a , __a = UNetaDModel.from_pretrained( '''fusing/unet-ldm-dummy-update''' , output_loading_info=_a , low_cpu_mem_usage=_a ) model_normal_load.to(_a ) model_normal_load.eval() __a = model_normal_load(_a , _a )['''sample'''] assert torch_all_close(_a , _a , rtol=1E-3 ) def __UpperCAmelCase ( self ): __a = UNetaDModel.from_pretrained('''fusing/unet-ldm-dummy-update''' ) model.eval() model.to(_a ) __a = torch.randn( 1 , model.config.in_channels , model.config.sample_size , model.config.sample_size , generator=torch.manual_seed(0 ) , ) __a = noise.to(_a ) __a = torch.tensor([10] * noise.shape[0] ).to(_a ) with torch.no_grad(): __a = model(_a , _a ).sample __a = output[0, -1, -3:, -3:].flatten().cpu() # fmt: off __a = torch.tensor([-13.3258, -20.1100, -15.9873, -17.6617, -23.0596, -17.9419, -13.3675, -16.1889, -12.3800] ) # fmt: on self.assertTrue(torch_all_close(_a , _a , rtol=1E-3 ) ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Tuple = UNetaDModel __UpperCAmelCase : Optional[Any] = 'sample' @property def __UpperCAmelCase ( self , _a=(32, 32) ): __a = 4 __a = 3 __a = floats_tensor((batch_size, num_channels) + sizes ).to(_a ) __a = torch.tensor(batch_size * [10] ).to(dtype=torch.intaa , device=_a ) return {"sample": noise, "timestep": time_step} @property def __UpperCAmelCase ( self ): return (3, 32, 32) @property def __UpperCAmelCase ( self ): return (3, 32, 32) def __UpperCAmelCase ( self ): __a = { '''block_out_channels''': [32, 64, 64, 64], '''in_channels''': 3, '''layers_per_block''': 1, '''out_channels''': 3, '''time_embedding_type''': '''fourier''', '''norm_eps''': 1E-6, '''mid_block_scale_factor''': math.sqrt(2.0 ), '''norm_num_groups''': None, '''down_block_types''': [ '''SkipDownBlock2D''', '''AttnSkipDownBlock2D''', '''SkipDownBlock2D''', '''SkipDownBlock2D''', ], '''up_block_types''': [ '''SkipUpBlock2D''', '''SkipUpBlock2D''', '''AttnSkipUpBlock2D''', '''SkipUpBlock2D''', ], } __a = self.dummy_input return init_dict, inputs_dict @slow def __UpperCAmelCase ( self ): __a , __a = UNetaDModel.from_pretrained('''google/ncsnpp-celebahq-256''' , output_loading_info=_a ) self.assertIsNotNone(_a ) self.assertEqual(len(loading_info['''missing_keys'''] ) , 0 ) model.to(_a ) __a = self.dummy_input __a = floats_tensor((4, 3) + (256, 256) ).to(_a ) __a = noise __a = model(**_a ) assert image is not None, "Make sure output is not None" @slow def __UpperCAmelCase ( self ): __a = UNetaDModel.from_pretrained('''google/ncsnpp-celebahq-256''' ) model.to(_a ) __a = 4 __a = 3 __a = (256, 256) __a = torch.ones((batch_size, num_channels) + sizes ).to(_a ) __a = torch.tensor(batch_size * [1E-4] ).to(_a ) with torch.no_grad(): __a = model(_a , _a ).sample __a = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off __a = torch.tensor([-4842.8691, -6499.6631, -3800.1953, -7978.2686, -1_0980.7129, -2_0028.8535, 8148.2822, 2342.2905, 567.7608] ) # fmt: on self.assertTrue(torch_all_close(_a , _a , rtol=1E-2 ) ) def __UpperCAmelCase ( self ): __a = UNetaDModel.from_pretrained('''fusing/ncsnpp-ffhq-ve-dummy-update''' ) model.to(_a ) __a = 4 __a = 3 __a = (32, 32) __a = torch.ones((batch_size, num_channels) + sizes ).to(_a ) __a = torch.tensor(batch_size * [1E-4] ).to(_a ) with torch.no_grad(): __a = model(_a , _a ).sample __a = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off __a = torch.tensor([-0.0325, -0.0900, -0.0869, -0.0332, -0.0725, -0.0270, -0.0101, 0.0227, 0.0256] ) # fmt: on self.assertTrue(torch_all_close(_a , _a , rtol=1E-2 ) ) def __UpperCAmelCase ( self ): # not required for this model pass
695
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "bigcode/gpt_bigcode-santacoder": "https://huggingface.co/bigcode/gpt_bigcode-santacoder/resolve/main/config.json", } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = 'gpt_bigcode' __UpperCAmelCase : Tuple = ['past_key_values'] __UpperCAmelCase : Dict = { 'hidden_size': 'n_embd', 'max_position_embeddings': 'n_positions', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self , _a=50_257 , _a=1_024 , _a=768 , _a=12 , _a=12 , _a=None , _a="gelu_pytorch_tanh" , _a=0.1 , _a=0.1 , _a=0.1 , _a=1E-5 , _a=0.02 , _a=True , _a=True , _a=50_256 , _a=50_256 , _a=True , _a=True , _a=True , **_a , ): __a = vocab_size __a = n_positions __a = n_embd __a = n_layer __a = n_head __a = n_inner __a = activation_function __a = resid_pdrop __a = embd_pdrop __a = attn_pdrop __a = layer_norm_epsilon __a = initializer_range __a = scale_attn_weights __a = use_cache __a = attention_softmax_in_fpaa __a = scale_attention_softmax_in_fpaa __a = multi_query __a = bos_token_id __a = eos_token_id super().__init__(bos_token_id=_a , eos_token_id=_a , **_a )
695
1
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPanoramaPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() @skip_mps class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Any = StableDiffusionPanoramaPipeline __UpperCAmelCase : str = TEXT_TO_IMAGE_PARAMS __UpperCAmelCase : str = TEXT_TO_IMAGE_BATCH_PARAMS __UpperCAmelCase : Tuple = TEXT_TO_IMAGE_IMAGE_PARAMS __UpperCAmelCase : int = TEXT_TO_IMAGE_IMAGE_PARAMS def __UpperCAmelCase ( self ): torch.manual_seed(0 ) __a = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=1 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) __a = DDIMScheduler() torch.manual_seed(0 ) __a = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) __a = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) __a = CLIPTextModel(_a ) __a = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __a = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def __UpperCAmelCase ( self , _a , _a=0 ): __a = torch.manual_seed(_a ) __a = { '''prompt''': '''a photo of the dolomites''', '''generator''': generator, # Setting height and width to None to prevent OOMs on CPU. '''height''': None, '''width''': None, '''num_inference_steps''': 1, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def __UpperCAmelCase ( self ): __a = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a = self.get_dummy_components() __a = StableDiffusionPanoramaPipeline(**_a ) __a = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = sd_pipe(**_a ).images __a = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __a = np.array([0.6186, 0.5374, 0.4915, 0.4135, 0.4114, 0.4563, 0.5128, 0.4977, 0.4757] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __UpperCAmelCase ( self ): super().test_inference_batch_consistent(batch_sizes=[1, 2] ) def __UpperCAmelCase ( self ): super().test_inference_batch_single_identical(batch_size=2 , expected_max_diff=3.25E-3 ) def __UpperCAmelCase ( self ): __a = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a = self.get_dummy_components() __a = StableDiffusionPanoramaPipeline(**_a ) __a = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = '''french fries''' __a = sd_pipe(**_a , negative_prompt=_a ) __a = output.images __a = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __a = np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __UpperCAmelCase ( self ): __a = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a = self.get_dummy_components() __a = StableDiffusionPanoramaPipeline(**_a ) __a = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = sd_pipe(**_a , view_batch_size=2 ) __a = output.images __a = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __a = np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __UpperCAmelCase ( self ): __a = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a = self.get_dummy_components() __a = EulerAncestralDiscreteScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule='''scaled_linear''' ) __a = StableDiffusionPanoramaPipeline(**_a ) __a = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = sd_pipe(**_a ).images __a = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __a = np.array([0.4024, 0.6510, 0.4901, 0.5378, 0.5813, 0.5622, 0.4795, 0.4467, 0.4952] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __UpperCAmelCase ( self ): __a = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a = self.get_dummy_components() __a = PNDMScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , skip_prk_steps=_a ) __a = StableDiffusionPanoramaPipeline(**_a ) __a = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = sd_pipe(**_a ).images __a = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __a = np.array([0.6391, 0.6291, 0.4861, 0.5134, 0.5552, 0.4578, 0.5032, 0.5023, 0.4539] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self , _a=0 ): __a = torch.manual_seed(_a ) __a = { '''prompt''': '''a photo of the dolomites''', '''generator''': generator, '''num_inference_steps''': 3, '''guidance_scale''': 7.5, '''output_type''': '''numpy''', } return inputs def __UpperCAmelCase ( self ): __a = '''stabilityai/stable-diffusion-2-base''' __a = DDIMScheduler.from_pretrained(_a , subfolder='''scheduler''' ) __a = StableDiffusionPanoramaPipeline.from_pretrained(_a , scheduler=_a , safety_checker=_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = self.get_inputs() __a = pipe(**_a ).images __a = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 2_048, 3) __a = np.array( [ 0.3696_8392, 0.2702_5372, 0.3244_6766, 0.2837_9387, 0.3636_3274, 0.3073_3347, 0.2710_0027, 0.2705_4125, 0.2553_6096, ] ) assert np.abs(expected_slice - image_slice ).max() < 1E-2 def __UpperCAmelCase ( self ): __a = StableDiffusionPanoramaPipeline.from_pretrained( '''stabilityai/stable-diffusion-2-base''' , safety_checker=_a ) __a = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = self.get_inputs() __a = pipe(**_a ).images __a = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 2_048, 3) __a = np.array( [ [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ] ] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __UpperCAmelCase ( self ): __a = 0 def callback_fn(_a , _a , _a ) -> None: __a = True nonlocal number_of_steps number_of_steps += 1 if step == 1: __a = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 256) __a = latents[0, -3:, -3:, -1] __a = np.array( [ 0.1868_1869, 0.3390_7816, 0.536_1276, 0.1443_2865, -0.0285_6611, -0.7394_1123, 0.2339_7987, 0.4732_2682, -0.3782_3164, ] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 elif step == 2: __a = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 256) __a = latents[0, -3:, -3:, -1] __a = np.array( [ 0.1853_9645, 0.3398_7248, 0.537_8559, 0.1443_7142, -0.0245_5261, -0.733_8317, 0.2399_0755, 0.4735_6272, -0.378_6505, ] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 __a = False __a = '''stabilityai/stable-diffusion-2-base''' __a = DDIMScheduler.from_pretrained(_a , subfolder='''scheduler''' ) __a = StableDiffusionPanoramaPipeline.from_pretrained(_a , scheduler=_a , safety_checker=_a ) __a = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = self.get_inputs() pipe(**_a , callback=_a , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def __UpperCAmelCase ( self ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __a = '''stabilityai/stable-diffusion-2-base''' __a = DDIMScheduler.from_pretrained(_a , subfolder='''scheduler''' ) __a = StableDiffusionPanoramaPipeline.from_pretrained(_a , scheduler=_a , safety_checker=_a ) __a = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() __a = self.get_inputs() __a = pipe(**_a ) __a = torch.cuda.max_memory_allocated() # make sure that less than 5.2 GB is allocated assert mem_bytes < 5.5 * 10**9
695
"""simple docstring""" import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler lowercase_ = 1_6 lowercase_ = 3_2 def lowercase ( lowerCAmelCase__ : Accelerator , lowerCAmelCase__ : int = 16 , lowerCAmelCase__ : str = "bert-base-cased" ) -> Optional[int]: __a = AutoTokenizer.from_pretrained(lowerCAmelCase__ ) __a = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(lowerCAmelCase__ : Optional[Any] ): # max_length=None => use the model max length (it's actually the default) __a = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=lowerCAmelCase__ , max_length=lowerCAmelCase__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset __a = datasets.map( lowerCAmelCase__ , batched=lowerCAmelCase__ , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , load_from_cache_file=lowerCAmelCase__ ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library __a = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(lowerCAmelCase__ : int ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(lowerCAmelCase__ , padding='''max_length''' , max_length=128 , return_tensors='''pt''' ) return tokenizer.pad(lowerCAmelCase__ , padding='''longest''' , return_tensors='''pt''' ) # Instantiate dataloaders. __a = DataLoader( tokenized_datasets['''train'''] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ ) __a = DataLoader( tokenized_datasets['''validation'''] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ ) return train_dataloader, eval_dataloader def lowercase ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any] ) -> Optional[int]: # Initialize accelerator __a = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __a = config['''lr'''] __a = int(config['''num_epochs'''] ) __a = int(config['''seed'''] ) __a = int(config['''batch_size'''] ) __a = args.model_name_or_path set_seed(lowerCAmelCase__ ) __a , __a = get_dataloaders(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __a = AutoModelForSequenceClassification.from_pretrained(lowerCAmelCase__ , return_dict=lowerCAmelCase__ ) # Instantiate optimizer __a = ( AdamW if accelerator.state.deepspeed_plugin is None or '''optimizer''' not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) __a = optimizer_cls(params=model.parameters() , lr=lowerCAmelCase__ ) if accelerator.state.deepspeed_plugin is not None: __a = accelerator.state.deepspeed_plugin.deepspeed_config[ '''gradient_accumulation_steps''' ] else: __a = 1 __a = (len(lowerCAmelCase__ ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): __a = get_linear_schedule_with_warmup( optimizer=lowerCAmelCase__ , num_warmup_steps=0 , num_training_steps=lowerCAmelCase__ , ) else: __a = DummyScheduler(lowerCAmelCase__ , total_num_steps=lowerCAmelCase__ , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. __a , __a , __a , __a , __a = accelerator.prepare( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # We need to keep track of how many total steps we have iterated over __a = 0 # We also need to keep track of the stating epoch so files are named properly __a = 0 # Now we train the model __a = evaluate.load('''glue''' , '''mrpc''' ) __a = 0 __a = {} for epoch in range(lowerCAmelCase__ , lowerCAmelCase__ ): model.train() for step, batch in enumerate(lowerCAmelCase__ ): __a = model(**lowerCAmelCase__ ) __a = outputs.loss __a = loss / gradient_accumulation_steps accelerator.backward(lowerCAmelCase__ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 model.eval() __a = 0 for step, batch in enumerate(lowerCAmelCase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): __a = model(**lowerCAmelCase__ ) __a = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times __a , __a = accelerator.gather( (predictions, batch['''labels''']) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(lowerCAmelCase__ ) - 1: __a = predictions[: len(eval_dataloader.dataset ) - samples_seen] __a = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=lowerCAmelCase__ , references=lowerCAmelCase__ , ) __a = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f'''epoch {epoch}:''' , lowerCAmelCase__ ) __a = eval_metric['''accuracy'''] if best_performance < eval_metric["accuracy"]: __a = eval_metric['''accuracy'''] if args.performance_lower_bound is not None: assert ( args.performance_lower_bound <= best_performance ), f'''Best performance metric {best_performance} is lower than the lower bound {args.performance_lower_bound}''' accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , '''all_results.json''' ) , '''w''' ) as f: json.dump(lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( ) -> List[str]: __a = argparse.ArgumentParser(description='''Simple example of training script tracking peak GPU memory usage.''' ) parser.add_argument( '''--model_name_or_path''' , type=lowerCAmelCase__ , default='''bert-base-cased''' , help='''Path to pretrained model or model identifier from huggingface.co/models.''' , required=lowerCAmelCase__ , ) parser.add_argument( '''--output_dir''' , type=lowerCAmelCase__ , default='''.''' , help='''Optional save directory where all checkpoint folders will be stored. Default is the current working directory.''' , ) parser.add_argument( '''--performance_lower_bound''' , type=lowerCAmelCase__ , default=lowerCAmelCase__ , help='''Optional lower bound for the performance metric. If set, the training will throw error when the performance metric drops below this value.''' , ) parser.add_argument( '''--num_epochs''' , type=lowerCAmelCase__ , default=3 , help='''Number of train epochs.''' , ) __a = parser.parse_args() __a = {'''lr''': 2e-5, '''num_epochs''': args.num_epochs, '''seed''': 42, '''batch_size''': 16} training_function(lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": main()
695
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowercase_ = { "configuration_m2m_100": ["M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP", "M2M100Config", "M2M100OnnxConfig"], "tokenization_m2m_100": ["M2M100Tokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST", "M2M100ForConditionalGeneration", "M2M100Model", "M2M100PreTrainedModel", ] if TYPE_CHECKING: from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig from .tokenization_mam_aaa import MaMaaaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mam_aaa import ( M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST, MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
695
"""simple docstring""" from typing import Any def lowercase ( lowerCAmelCase__ : list , lowerCAmelCase__ : list , lowerCAmelCase__ : dict , lowerCAmelCase__ : dict , lowerCAmelCase__ : dict , ) -> list: _validation( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ) # Creates data structures and fill initial step __a = {} __a = {} for state in states_space: __a = observations_space[0] __a = ( initial_probabilities[state] * emission_probabilities[state][observation] ) __a = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(lowerCAmelCase__ ) ): __a = observations_space[o] __a = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function __a = '''''' __a = -1 for k_state in states_space: __a = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: __a = probability __a = k_state # Update probabilities and pointers dicts __a = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) __a = arg_max # The final observation __a = observations_space[len(lowerCAmelCase__ ) - 1] # argmax for given final observation __a = '''''' __a = -1 for k_state in states_space: __a = probabilities[(k_state, final_observation)] if probability > max_probability: __a = probability __a = k_state __a = arg_max # Process pointers backwards __a = last_state __a = [] for o in range(len(lowerCAmelCase__ ) - 1 , -1 , -1 ): result.append(lowerCAmelCase__ ) __a = pointers[previous, observations_space[o]] result.reverse() return result def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: _validate_not_empty( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ) _validate_lists(lowerCAmelCase__ , lowerCAmelCase__ ) _validate_dicts( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError('''There\'s an empty parameter''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any ) -> None: _validate_list(lowerCAmelCase__ , '''observations_space''' ) _validate_list(lowerCAmelCase__ , '''states_space''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str ) -> None: if not isinstance(_object , lowerCAmelCase__ ): __a = f'''{var_name} must be a list''' raise ValueError(lowerCAmelCase__ ) else: for x in _object: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = f'''{var_name} must be a list of strings''' raise ValueError(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: _validate_dict(lowerCAmelCase__ , '''initial_probabilities''' , lowerCAmelCase__ ) _validate_nested_dict(lowerCAmelCase__ , '''transition_probabilities''' ) _validate_nested_dict(lowerCAmelCase__ , '''emission_probabilities''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str ) -> None: _validate_dict(_object , lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object.values(): _validate_dict(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : type , lowerCAmelCase__ : bool = False ) -> None: if not isinstance(_object , lowerCAmelCase__ ): __a = f'''{var_name} must be a dict''' raise ValueError(lowerCAmelCase__ ) if not all(isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object ): __a = f'''{var_name} all keys must be strings''' raise ValueError(lowerCAmelCase__ ) if not all(isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object.values() ): __a = '''nested dictionary ''' if nested else '''''' __a = f'''{var_name} {nested_text}all values must be {value_type.__name__}''' raise ValueError(lowerCAmelCase__ ) if __name__ == "__main__": from doctest import testmod testmod()
695
1
"""simple docstring""" 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 __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a , _a=0.0 , _a = None , _a = "geglu" , _a = None , _a = False , _a = False , _a = False , _a = False , _a = True , _a = "layer_norm" , _a = False , ): super().__init__() __a = only_cross_attention __a = (num_embeds_ada_norm is not None) and norm_type == '''ada_norm_zero''' __a = (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: __a = AdaLayerNorm(_a , _a ) elif self.use_ada_layer_norm_zero: __a = AdaLayerNormZero(_a , _a ) else: __a = nn.LayerNorm(_a , elementwise_affine=_a ) __a = Attention( query_dim=_a , heads=_a , dim_head=_a , dropout=_a , bias=_a , cross_attention_dim=cross_attention_dim if only_cross_attention else None , upcast_attention=_a , ) # 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. __a = ( AdaLayerNorm(_a , _a ) if self.use_ada_layer_norm else nn.LayerNorm(_a , elementwise_affine=_a ) ) __a = Attention( query_dim=_a , cross_attention_dim=cross_attention_dim if not double_self_attention else None , heads=_a , dim_head=_a , dropout=_a , bias=_a , upcast_attention=_a , ) # is self-attn if encoder_hidden_states is none else: __a = None __a = None # 3. Feed-forward __a = nn.LayerNorm(_a , elementwise_affine=_a ) __a = FeedForward(_a , dropout=_a , activation_fn=_a , final_dropout=_a ) # let chunk size default to None __a = None __a = 0 def __UpperCAmelCase ( self , _a , _a ): # Sets chunk feed-forward __a = chunk_size __a = dim def __UpperCAmelCase ( self , _a , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , ): # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: __a = self.norma(_a , _a ) elif self.use_ada_layer_norm_zero: __a , __a , __a , __a , __a = self.norma( _a , _a , _a , hidden_dtype=hidden_states.dtype ) else: __a = self.norma(_a ) __a = cross_attention_kwargs if cross_attention_kwargs is not None else {} __a = self.attna( _a , encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None , attention_mask=_a , **_a , ) if self.use_ada_layer_norm_zero: __a = gate_msa.unsqueeze(1 ) * attn_output __a = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: __a = ( self.norma(_a , _a ) if self.use_ada_layer_norm else self.norma(_a ) ) __a = self.attna( _a , encoder_hidden_states=_a , attention_mask=_a , **_a , ) __a = attn_output + hidden_states # 3. Feed-forward __a = self.norma(_a ) if self.use_ada_layer_norm_zero: __a = 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`.''' ) __a = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size __a = torch.cat( [self.ff(_a ) for hid_slice in norm_hidden_states.chunk(_a , dim=self._chunk_dim )] , dim=self._chunk_dim , ) else: __a = self.ff(_a ) if self.use_ada_layer_norm_zero: __a = gate_mlp.unsqueeze(1 ) * ff_output __a = ff_output + hidden_states return hidden_states class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a = None , _a = 4 , _a = 0.0 , _a = "geglu" , _a = False , ): super().__init__() __a = int(dim * mult ) __a = dim_out if dim_out is not None else dim if activation_fn == "gelu": __a = GELU(_a , _a ) if activation_fn == "gelu-approximate": __a = GELU(_a , _a , approximate='''tanh''' ) elif activation_fn == "geglu": __a = GEGLU(_a , _a ) elif activation_fn == "geglu-approximate": __a = ApproximateGELU(_a , _a ) __a = nn.ModuleList([] ) # project in self.net.append(_a ) # project dropout self.net.append(nn.Dropout(_a ) ) # project out self.net.append(nn.Linear(_a , _a ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(_a ) ) def __UpperCAmelCase ( self , _a ): for module in self.net: __a = module(_a ) return hidden_states class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a = "none" ): super().__init__() __a = nn.Linear(_a , _a ) __a = approximate def __UpperCAmelCase ( self , _a ): if gate.device.type != "mps": return F.gelu(_a , 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 __UpperCAmelCase ( self , _a ): __a = self.proj(_a ) __a = self.gelu(_a ) return hidden_states class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a ): super().__init__() __a = nn.Linear(_a , dim_out * 2 ) def __UpperCAmelCase ( self , _a ): if gate.device.type != "mps": return F.gelu(_a ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def __UpperCAmelCase ( self , _a ): __a , __a = self.proj(_a ).chunk(2 , dim=-1 ) return hidden_states * self.gelu(_a ) class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a ): super().__init__() __a = nn.Linear(_a , _a ) def __UpperCAmelCase ( self , _a ): __a = self.proj(_a ) return x * torch.sigmoid(1.702 * x ) class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a ): super().__init__() __a = nn.Embedding(_a , _a ) __a = nn.SiLU() __a = nn.Linear(_a , embedding_dim * 2 ) __a = nn.LayerNorm(_a , elementwise_affine=_a ) def __UpperCAmelCase ( self , _a , _a ): __a = self.linear(self.silu(self.emb(_a ) ) ) __a , __a = torch.chunk(_a , 2 ) __a = self.norm(_a ) * (1 + scale) + shift return x class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a ): super().__init__() __a = CombinedTimestepLabelEmbeddings(_a , _a ) __a = nn.SiLU() __a = nn.Linear(_a , 6 * embedding_dim , bias=_a ) __a = nn.LayerNorm(_a , elementwise_affine=_a , eps=1E-6 ) def __UpperCAmelCase ( self , _a , _a , _a , _a=None ): __a = self.linear(self.silu(self.emb(_a , _a , hidden_dtype=_a ) ) ) __a , __a , __a , __a , __a , __a = emb.chunk(6 , dim=1 ) __a = self.norm(_a ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a , _a = None , _a = 1E-5 ): super().__init__() __a = num_groups __a = eps if act_fn is None: __a = None else: __a = get_activation(_a ) __a = nn.Linear(_a , out_dim * 2 ) def __UpperCAmelCase ( self , _a , _a ): if self.act: __a = self.act(_a ) __a = self.linear(_a ) __a = emb[:, :, None, None] __a , __a = emb.chunk(2 , dim=1 ) __a = F.group_norm(_a , self.num_groups , eps=self.eps ) __a = x * (1 + scale) + shift return x
695
"""simple docstring""" import math def lowercase ( lowerCAmelCase__ : int ) -> bool: 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(lowerCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowercase ( lowerCAmelCase__ : float = 0.1 ) -> int: __a = 3 __a = 3 while primes / (2 * j - 1) >= ratio: for i in range(j * j + j + 1 , (j + 2) * (j + 2) , j + 1 ): primes += is_prime(lowerCAmelCase__ ) j += 2 return j if __name__ == "__main__": import doctest doctest.testmod()
695
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : int ) -> Union[str, Any]: # Return True if there is node that has not iterated. __a = [False] * len(lowerCAmelCase__ ) __a = [] queue.append(lowerCAmelCase__ ) __a = True while queue: __a = queue.pop(0 ) for ind in range(len(graph[u] ) ): if visited[ind] is False and graph[u][ind] > 0: queue.append(lowerCAmelCase__ ) __a = True __a = u return visited[t] def lowercase ( lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : List[Any] ) -> Dict: # This array is filled by BFS and to store path __a = [-1] * (len(lowerCAmelCase__ )) __a = 0 while bfs(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ): __a = float('''Inf''' ) __a = sink while s != source: # Find the minimum value in select path __a = min(lowerCAmelCase__ , graph[parent[s]][s] ) __a = parent[s] max_flow += path_flow __a = sink while v != source: __a = parent[v] graph[u][v] -= path_flow graph[v][u] += path_flow __a = parent[v] return max_flow lowercase_ = [ [0, 1_6, 1_3, 0, 0, 0], [0, 0, 1_0, 1_2, 0, 0], [0, 4, 0, 0, 1_4, 0], [0, 0, 9, 0, 0, 2_0], [0, 0, 0, 7, 0, 4], [0, 0, 0, 0, 0, 0], ] lowercase_ , lowercase_ = 0, 5 print(ford_fulkerson(graph, source, sink))
695
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = { "configuration_mctct": ["MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MCTCTConfig"], "feature_extraction_mctct": ["MCTCTFeatureExtractor"], "processing_mctct": ["MCTCTProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST", "MCTCTForCTC", "MCTCTModel", "MCTCTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig from .feature_extraction_mctct import MCTCTFeatureExtractor from .processing_mctct import MCTCTProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
695
1
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType lowercase_ = logging.get_logger(__name__) lowercase_ = { "microsoft/deberta-v2-xlarge": "https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json", "microsoft/deberta-v2-xxlarge": "https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json", "microsoft/deberta-v2-xlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json" ), "microsoft/deberta-v2-xxlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json" ), } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Dict = 'deberta-v2' def __init__( self , _a=128_100 , _a=1_536 , _a=24 , _a=24 , _a=6_144 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=0 , _a=0.02 , _a=1E-7 , _a=False , _a=-1 , _a=0 , _a=True , _a=None , _a=0 , _a="gelu" , **_a , ): super().__init__(**_a ) __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = initializer_range __a = relative_attention __a = max_relative_positions __a = pad_token_id __a = position_biased_input # Backwards compatibility if type(_a ) == str: __a = [x.strip() for x in pos_att_type.lower().split('''|''' )] __a = pos_att_type __a = vocab_size __a = layer_norm_eps __a = kwargs.get('''pooler_hidden_size''' , _a ) __a = pooler_dropout __a = pooler_hidden_act class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def __UpperCAmelCase ( self ): if self.task == "multiple-choice": __a = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: __a = {0: '''batch''', 1: '''sequence'''} if self._config.type_vocab_size > 0: return OrderedDict( [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)] ) else: return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)] ) @property def __UpperCAmelCase ( self ): return 12 def __UpperCAmelCase ( self , _a , _a = -1 , _a = -1 , _a = -1 , _a = False , _a = None , _a = 3 , _a = 40 , _a = 40 , _a = None , ): __a = super().generate_dummy_inputs(preprocessor=_a , framework=_a ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
695
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json" ), } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[Any] = 'dpr' def __init__( self , _a=30_522 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=2 , _a=0.02 , _a=1E-12 , _a=0 , _a="absolute" , _a = 0 , **_a , ): super().__init__(pad_token_id=_a , **_a ) __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = hidden_act __a = intermediate_size __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = initializer_range __a = layer_norm_eps __a = projection_dim __a = position_embedding_type
695
1
"""simple docstring""" import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : Union[str, Any] ) -> Optional[int]: assert isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) 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 lowercase ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Optional[Any] ) -> Tuple: __a = tmp_path / '''cache''' __a = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): __a = ParquetDatasetReader(lowerCAmelCase__ , cache_dir=lowerCAmelCase__ , keep_in_memory=lowerCAmelCase__ ).read() _check_parquet_dataset(lowerCAmelCase__ , lowerCAmelCase__ ) @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 lowercase ( lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Union[str, Any] ) -> Dict: __a = tmp_path / '''cache''' __a = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} __a = features.copy() if features else default_expected_features __a = ( Features({feature: Value(lowerCAmelCase__ ) for feature, dtype in features.items()} ) if features is not None else None ) __a = ParquetDatasetReader(lowerCAmelCase__ , features=lowerCAmelCase__ , cache_dir=lowerCAmelCase__ ).read() _check_parquet_dataset(lowerCAmelCase__ , lowerCAmelCase__ ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def lowercase ( lowerCAmelCase__ : Dict , lowerCAmelCase__ : str , lowerCAmelCase__ : List[Any] ) -> str: __a = tmp_path / '''cache''' __a = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} __a = ParquetDatasetReader(lowerCAmelCase__ , cache_dir=lowerCAmelCase__ , split=lowerCAmelCase__ ).read() _check_parquet_dataset(lowerCAmelCase__ , lowerCAmelCase__ ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('''path_type''' , [str, list] ) def lowercase ( lowerCAmelCase__ : Dict , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Optional[int] ) -> int: if issubclass(lowerCAmelCase__ , lowerCAmelCase__ ): __a = parquet_path elif issubclass(lowerCAmelCase__ , lowerCAmelCase__ ): __a = [parquet_path] __a = tmp_path / '''cache''' __a = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} __a = ParquetDatasetReader(lowerCAmelCase__ , cache_dir=lowerCAmelCase__ ).read() _check_parquet_dataset(lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[str]=("train",) ) -> Optional[Any]: assert isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) for split in splits: __a = 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 lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Dict ) -> Optional[Any]: __a = tmp_path / '''cache''' __a = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): __a = ParquetDatasetReader( {'''train''': parquet_path} , cache_dir=lowerCAmelCase__ , keep_in_memory=lowerCAmelCase__ ).read() _check_parquet_datasetdict(lowerCAmelCase__ , lowerCAmelCase__ ) @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 lowercase ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[int] ) -> str: __a = tmp_path / '''cache''' __a = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} __a = features.copy() if features else default_expected_features __a = ( Features({feature: Value(lowerCAmelCase__ ) for feature, dtype in features.items()} ) if features is not None else None ) __a = ParquetDatasetReader({'''train''': parquet_path} , features=lowerCAmelCase__ , cache_dir=lowerCAmelCase__ ).read() _check_parquet_datasetdict(lowerCAmelCase__ , lowerCAmelCase__ ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Optional[Any] ) -> int: if split: __a = {split: parquet_path} else: __a = '''train''' __a = {'''train''': parquet_path, '''test''': parquet_path} __a = tmp_path / '''cache''' __a = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} __a = ParquetDatasetReader(lowerCAmelCase__ , cache_dir=lowerCAmelCase__ ).read() _check_parquet_datasetdict(lowerCAmelCase__ , lowerCAmelCase__ , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : int ) -> str: __a = ParquetDatasetWriter(lowerCAmelCase__ , tmp_path / '''foo.parquet''' ) assert writer.write() > 0 __a = pq.ParquetFile(tmp_path / '''foo.parquet''' ) __a = pf.read() assert dataset.data.table == output_table def lowercase ( lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Optional[Any] ) -> Union[str, Any]: __a = str(shared_datadir / '''test_image_rgb.jpg''' ) __a = {'''image''': [image_path]} __a = Features({'''image''': Image()} ) __a = Dataset.from_dict(lowerCAmelCase__ , features=lowerCAmelCase__ ) __a = ParquetDatasetWriter(lowerCAmelCase__ , tmp_path / '''foo.parquet''' ) assert writer.write() > 0 __a = Dataset.from_parquet(str(tmp_path / '''foo.parquet''' ) ) assert dataset.features == reloaded_dataset.features __a = ParquetDatasetReader(str(tmp_path / '''foo.parquet''' ) , streaming=lowerCAmelCase__ ).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( '''feature, expected''' , [ (Features({'''foo''': Value('''int32''' )} ), None), (Features({'''image''': Image(), '''foo''': Value('''int32''' )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({'''nested''': Sequence(Audio() )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] , ) def lowercase ( lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int ) -> List[Any]: assert get_writer_batch_size(lowerCAmelCase__ ) == expected
695
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = StableDiffusionInpaintPipeline __UpperCAmelCase : int = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS __UpperCAmelCase : str = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __UpperCAmelCase : int = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess __UpperCAmelCase : Tuple = frozenset([] ) def __UpperCAmelCase ( self ): torch.manual_seed(0 ) __a = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=9 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=_a , ) __a = PNDMScheduler(skip_prk_steps=_a ) torch.manual_seed(0 ) __a = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) __a = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , hidden_act='''gelu''' , projection_dim=512 , ) __a = CLIPTextModel(_a ) __a = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __a = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def __UpperCAmelCase ( self , _a , _a=0 ): # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched __a = floats_tensor((1, 3, 32, 32) , rng=random.Random(_a ) ).to(_a ) __a = image.cpu().permute(0 , 2 , 3 , 1 )[0] __a = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((64, 64) ) __a = Image.fromarray(np.uinta(image + 4 ) ).convert('''RGB''' ).resize((64, 64) ) if str(_a ).startswith('''mps''' ): __a = torch.manual_seed(_a ) else: __a = torch.Generator(device=_a ).manual_seed(_a ) __a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': init_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def __UpperCAmelCase ( self ): __a = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a = self.get_dummy_components() __a = StableDiffusionInpaintPipeline(**_a ) __a = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = sd_pipe(**_a ).images __a = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __a = np.array([0.4727, 0.5735, 0.3941, 0.5446, 0.5926, 0.4394, 0.5062, 0.4654, 0.4476] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __UpperCAmelCase ( self ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self ): __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench.npy''' ) __a = '''stabilityai/stable-diffusion-2-inpainting''' __a = StableDiffusionInpaintPipeline.from_pretrained(_a , safety_checker=_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = '''Face of a yellow cat, high resolution, sitting on a park bench''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , output_type='''np''' , ) __a = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 9E-3 def __UpperCAmelCase ( self ): __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench_fp16.npy''' ) __a = '''stabilityai/stable-diffusion-2-inpainting''' __a = StableDiffusionInpaintPipeline.from_pretrained( _a , torch_dtype=torch.floataa , safety_checker=_a , ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = '''Face of a yellow cat, high resolution, sitting on a park bench''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , output_type='''np''' , ) __a = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 5E-1 def __UpperCAmelCase ( self ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __a = '''stabilityai/stable-diffusion-2-inpainting''' __a = PNDMScheduler.from_pretrained(_a , subfolder='''scheduler''' ) __a = StableDiffusionInpaintPipeline.from_pretrained( _a , safety_checker=_a , scheduler=_a , torch_dtype=torch.floataa , ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() __a = '''Face of a yellow cat, high resolution, sitting on a park bench''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , num_inference_steps=2 , output_type='''np''' , ) __a = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 10**9
695
1
"""simple docstring""" from __future__ import annotations import math from collections.abc import Callable def lowercase ( lowerCAmelCase__ : Callable[[int | float], int | float] , lowerCAmelCase__ : int | float , lowerCAmelCase__ : int | float , lowerCAmelCase__ : int = 100 , ) -> float: __a = x_start __a = fnc(lowerCAmelCase__ ) __a = 0.0 for _ in range(lowerCAmelCase__ ): # Approximates curve as a sequence of linear lines and sums their length __a = (x_end - x_start) / steps + xa __a = fnc(lowerCAmelCase__ ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step __a = xa __a = fxa return length if __name__ == "__main__": def lowercase ( lowerCAmelCase__ : Any ) -> Tuple: return math.sin(10 * x ) print("f(x) = sin(10 * x)") print("The length of the curve from x = -10 to x = 10 is:") lowercase_ = 1_0 while i <= 1_0_0_0_0_0: print(F'''With {i} steps: {line_length(f, -1_0, 1_0, i)}''') i *= 1_0
695
"""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 __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : int = 0 __UpperCAmelCase : bool = False __UpperCAmelCase : float = 3.0 class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): # 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=_a ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'''a''': 2, '''c''': 2.25} ) @require_cuda def __UpperCAmelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. __a = GradScalerKwargs(init_scale=1_024 , growth_factor=2 ) AcceleratorState._reset_state() __a = Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) __a = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2_000 ) self.assertEqual(scaler._enabled , _a ) @require_multi_gpu def __UpperCAmelCase ( self ): __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": lowercase_ = DistributedDataParallelKwargs(bucket_cap_mb=1_5, find_unused_parameters=True) lowercase_ = Accelerator(kwargs_handlers=[ddp_scaler]) lowercase_ = torch.nn.Linear(1_0_0, 2_0_0) lowercase_ = accelerator.prepare(model) # Check the values changed in kwargs lowercase_ = "" lowercase_ = model.bucket_bytes_cap // (1_0_2_4 * 1_0_2_4) if observed_bucket_cap_map != 1_5: 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)
695
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int = 1000 ) -> int: __a = 2**power __a = 0 while n: __a , __a = r + n % 10, n // 10 return r if __name__ == "__main__": print(solution(int(str(input()).strip())))
695
"""simple docstring""" import inspect import os import sys import unittest import accelerate from accelerate.test_utils import execute_subprocess_async, require_tpu class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = inspect.getfile(accelerate.test_utils ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) __a = os.path.sep.join(inspect.getfile(self.__class__ ).split(os.path.sep )[:-1] ) @require_tpu def __UpperCAmelCase ( self ): __a = f''' {self.test_dir}/xla_spawn.py --num_cores 8 {self.test_file_path} '''.split() __a = [sys.executable] + distributed_args execute_subprocess_async(_a , env=os.environ.copy() )
695
1
"""simple docstring""" lowercase_ = "0.21.0" from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
695
"""simple docstring""" import os import unittest from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, BertTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = BertTokenizer __UpperCAmelCase : Optional[Any] = BertTokenizerFast __UpperCAmelCase : str = True __UpperCAmelCase : Tuple = True __UpperCAmelCase : Any = filter_non_english def __UpperCAmelCase ( self ): super().setUp() __a = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] __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] ) ) def __UpperCAmelCase ( self , _a ): __a = '''UNwant\u00E9d,running''' __a = '''unwanted, running''' return input_text, output_text def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file ) __a = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(_a , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [9, 6, 7, 12, 10, 11] ) def __UpperCAmelCase ( self ): if not self.test_rust_tokenizer: return __a = self.get_tokenizer() __a = self.get_rust_tokenizer() __a = '''UNwant\u00E9d,running''' __a = tokenizer.tokenize(_a ) __a = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) __a = self.get_rust_tokenizer() __a = tokenizer.encode(_a ) __a = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a ) # With lower casing __a = self.get_tokenizer(do_lower_case=_a ) __a = self.get_rust_tokenizer(do_lower_case=_a ) __a = '''UNwant\u00E9d,running''' __a = tokenizer.tokenize(_a ) __a = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) __a = self.get_rust_tokenizer() __a = tokenizer.encode(_a ) __a = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('''ah\u535A\u63A8zz''' ) , ['''ah''', '''\u535A''', '''\u63A8''', '''zz'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''hello''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hällo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''h\u00E9llo'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HäLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HaLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , never_split=['''[UNK]'''] ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? [UNK]''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''', '''[UNK]'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer() __a = '''a\n\'ll !!to?\'d of, can\'t.''' __a = ['''a''', '''\'''', '''ll''', '''!''', '''!''', '''to''', '''?''', '''\'''', '''d''', '''of''', ''',''', '''can''', '''\'''', '''t''', '''.'''] self.assertListEqual(tokenizer.tokenize(_a ) , _a ) def __UpperCAmelCase ( self ): __a = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing'''] __a = {} for i, token in enumerate(_a ): __a = i __a = WordpieceTokenizer(vocab=_a , unk_token='''[UNK]''' ) self.assertListEqual(tokenizer.tokenize('''''' ) , [] ) self.assertListEqual(tokenizer.tokenize('''unwanted running''' ) , ['''un''', '''##want''', '''##ed''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.tokenize('''unwantedX running''' ) , ['''[UNK]''', '''runn''', '''##ing'''] ) def __UpperCAmelCase ( self ): self.assertTrue(_is_whitespace(''' ''' ) ) self.assertTrue(_is_whitespace('''\t''' ) ) self.assertTrue(_is_whitespace('''\r''' ) ) self.assertTrue(_is_whitespace('''\n''' ) ) self.assertTrue(_is_whitespace('''\u00A0''' ) ) self.assertFalse(_is_whitespace('''A''' ) ) self.assertFalse(_is_whitespace('''-''' ) ) def __UpperCAmelCase ( self ): self.assertTrue(_is_control('''\u0005''' ) ) self.assertFalse(_is_control('''A''' ) ) self.assertFalse(_is_control(''' ''' ) ) self.assertFalse(_is_control('''\t''' ) ) self.assertFalse(_is_control('''\r''' ) ) def __UpperCAmelCase ( self ): self.assertTrue(_is_punctuation('''-''' ) ) self.assertTrue(_is_punctuation('''$''' ) ) self.assertTrue(_is_punctuation('''`''' ) ) self.assertTrue(_is_punctuation('''.''' ) ) self.assertFalse(_is_punctuation('''A''' ) ) self.assertFalse(_is_punctuation(''' ''' ) ) def __UpperCAmelCase ( self ): __a = self.get_tokenizer() __a = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(_a ) for t in ['''Test''', '''\xad''', '''test''']] , [['''[UNK]'''], [], ['''[UNK]''']] ) self.assertListEqual( [rust_tokenizer.tokenize(_a ) for t in ['''Test''', '''\xad''', '''test''']] , [['''[UNK]'''], [], ['''[UNK]''']] ) @slow def __UpperCAmelCase ( self ): __a = self.tokenizer_class.from_pretrained('''bert-base-uncased''' ) __a = tokenizer.encode('''sequence builders''' , add_special_tokens=_a ) __a = tokenizer.encode('''multi-sequence build''' , add_special_tokens=_a ) __a = tokenizer.build_inputs_with_special_tokens(_a ) __a = tokenizer.build_inputs_with_special_tokens(_a , _a ) assert encoded_sentence == [101] + text + [102] assert encoded_pair == [101] + text + [102] + text_a + [102] def __UpperCAmelCase ( self ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = f'''A, naïve {tokenizer_r.mask_token} AllenNLP sentence.''' __a = tokenizer_r.encode_plus( _a , return_attention_mask=_a , return_token_type_ids=_a , return_offsets_mapping=_a , add_special_tokens=_a , ) __a = tokenizer_r.do_lower_case if hasattr(_a , '''do_lower_case''' ) else False __a = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), '''A'''), ((1, 2), ''','''), ((3, 5), '''na'''), ((5, 6), '''##ï'''), ((6, 8), '''##ve'''), ((9, 15), tokenizer_r.mask_token), ((16, 21), '''Allen'''), ((21, 23), '''##NL'''), ((23, 24), '''##P'''), ((25, 33), '''sentence'''), ((33, 34), '''.'''), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), '''a'''), ((1, 2), ''','''), ((3, 8), '''naive'''), ((9, 15), tokenizer_r.mask_token), ((16, 21), '''allen'''), ((21, 23), '''##nl'''), ((23, 24), '''##p'''), ((25, 33), '''sentence'''), ((33, 34), '''.'''), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens['''input_ids'''] ) ) self.assertEqual([e[0] for e in expected_results] , tokens['''offset_mapping'''] ) def __UpperCAmelCase ( self ): __a = ['''的''', '''人''', '''有'''] __a = ''''''.join(_a ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __a = True __a = self.tokenizer_class.from_pretrained(_a , **_a ) __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = tokenizer_p.encode(_a , add_special_tokens=_a ) __a = tokenizer_r.encode(_a , add_special_tokens=_a ) __a = tokenizer_r.convert_ids_to_tokens(_a ) __a = tokenizer_p.convert_ids_to_tokens(_a ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(_a , _a ) self.assertListEqual(_a , _a ) __a = False __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = self.tokenizer_class.from_pretrained(_a , **_a ) __a = tokenizer_r.encode(_a , add_special_tokens=_a ) __a = tokenizer_p.encode(_a , add_special_tokens=_a ) __a = tokenizer_r.convert_ids_to_tokens(_a ) __a = tokenizer_p.convert_ids_to_tokens(_a ) # it is expected that only the first Chinese character is not preceded by "##". __a = [ f'''##{token}''' if idx != 0 else token for idx, token in enumerate(_a ) ] self.assertListEqual(_a , _a ) self.assertListEqual(_a , _a )
695
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowercase_ = { "configuration_bloom": ["BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP", "BloomConfig", "BloomOnnxConfig"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["BloomTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST", "BloomForCausalLM", "BloomModel", "BloomPreTrainedModel", "BloomForSequenceClassification", "BloomForTokenClassification", "BloomForQuestionAnswering", ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
695
"""simple docstring""" from __future__ import annotations def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> float: if days_between_payments <= 0: raise ValueError('''days_between_payments must be > 0''' ) if daily_interest_rate < 0: raise ValueError('''daily_interest_rate must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return principal * daily_interest_rate * days_between_payments def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float , ) -> float: if number_of_compounding_periods <= 0: raise ValueError('''number_of_compounding_periods must be > 0''' ) if nominal_annual_interest_rate_percentage < 0: raise ValueError('''nominal_annual_interest_rate_percentage must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return principal * ( (1 + nominal_annual_interest_rate_percentage) ** number_of_compounding_periods - 1 ) def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float , ) -> float: if number_of_years <= 0: raise ValueError('''number_of_years must be > 0''' ) if nominal_annual_percentage_rate < 0: raise ValueError('''nominal_annual_percentage_rate must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return compound_interest( lowerCAmelCase__ , nominal_annual_percentage_rate / 365 , number_of_years * 365 ) if __name__ == "__main__": import doctest doctest.testmod()
695
1
"""simple docstring""" from __future__ import annotations lowercase_ = list[tuple[int, int]] lowercase_ = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] lowercase_ = ([-1, 0], [0, -1], [1, 0], [0, 1]) # up, left, down, right class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a , _a , _a , _a , _a , ): __a = pos_x __a = pos_y __a = (pos_y, pos_x) __a = goal_x __a = goal_y __a = g_cost __a = parent __a = self.calculate_heuristic() def __UpperCAmelCase ( self ): __a = abs(self.pos_x - self.goal_x ) __a = abs(self.pos_y - self.goal_y ) return dx + dy def __lt__( self , _a ): return self.f_cost < other.f_cost class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a ): __a = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , _a ) __a = Node(goal[1] , goal[0] , goal[1] , goal[0] , 99_999 , _a ) __a = [self.start] __a = [] __a = False def __UpperCAmelCase ( self ): while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() __a = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: __a = True return self.retrace_path(_a ) self.closed_nodes.append(_a ) __a = self.get_successors(_a ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(_a ) else: # retrieve the best current path __a = self.open_nodes.pop(self.open_nodes.index(_a ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(_a ) else: self.open_nodes.append(_a ) if not self.reached: return [self.start.pos] return None def __UpperCAmelCase ( self , _a ): __a = [] for action in delta: __a = parent.pos_x + action[1] __a = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(_a ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( _a , _a , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , _a , ) ) return successors def __UpperCAmelCase ( self , _a ): __a = node __a = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) __a = current_node.parent path.reverse() return path if __name__ == "__main__": lowercase_ = (0, 0) lowercase_ = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) print("------") lowercase_ = GreedyBestFirst(init, goal) lowercase_ = greedy_bf.search() if path: for pos_x, pos_y in path: lowercase_ = 2 for elem in grid: print(elem)
695
"""simple docstring""" def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any=False ) -> Any: if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = len(set_a.intersection(lowerCAmelCase__ ) ) if alternative_union: __a = len(lowerCAmelCase__ ) + len(lowerCAmelCase__ ) else: __a = len(set_a.union(lowerCAmelCase__ ) ) return intersection / union if isinstance(lowerCAmelCase__ , (list, tuple) ) and isinstance(lowerCAmelCase__ , (list, tuple) ): __a = [element for element in set_a if element in set_b] if alternative_union: __a = len(lowerCAmelCase__ ) + len(lowerCAmelCase__ ) return len(lowerCAmelCase__ ) / union else: __a = set_a + [element for element in set_b if element not in set_a] return len(lowerCAmelCase__ ) / len(lowerCAmelCase__ ) return len(lowerCAmelCase__ ) / len(lowerCAmelCase__ ) return None if __name__ == "__main__": lowercase_ = {"a", "b", "c", "d", "e"} lowercase_ = {"c", "d", "e", "f", "h", "i"} print(jaccard_similarity(set_a, set_b))
695
1
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL lowercase_ = logging.get_logger(__name__) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : str = ['pixel_values'] def __init__( self , _a = True , _a = None , _a = None , _a = PILImageResampling.BILINEAR , _a = True , _a = 1 / 255 , _a = True , _a = None , _a = None , **_a , ): super().__init__(**_a ) __a = size if size is not None else {'''shortest_edge''': 384} __a = get_size_dict(_a , default_to_square=_a ) __a = do_resize __a = size # Default value set here for backwards compatibility where the value in config is None __a = crop_pct if crop_pct is not None else 224 / 256 __a = resample __a = do_rescale __a = rescale_factor __a = do_normalize __a = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN __a = image_std if image_std is not None else IMAGENET_STANDARD_STD def __UpperCAmelCase ( self , _a , _a , _a , _a = PILImageResampling.BICUBIC , _a = None , **_a , ): __a = get_size_dict(_a , default_to_square=_a ) if "shortest_edge" not in size: raise ValueError(f'''Size dictionary must contain \'shortest_edge\' key. Got {size.keys()}''' ) __a = size['''shortest_edge'''] if shortest_edge < 384: # maintain same ratio, resizing shortest edge to shortest_edge/crop_pct __a = int(shortest_edge / crop_pct ) __a = get_resize_output_image_size(_a , size=_a , default_to_square=_a ) __a = resize(image=_a , size=_a , resample=_a , data_format=_a , **_a ) # then crop to (shortest_edge, shortest_edge) return center_crop(image=_a , size=(shortest_edge, shortest_edge) , data_format=_a , **_a ) else: # warping (no cropping) when evaluated at 384 or larger return resize( _a , size=(shortest_edge, shortest_edge) , resample=_a , data_format=_a , **_a ) def __UpperCAmelCase ( self , _a , _a , _a = None , **_a , ): return rescale(_a , scale=_a , data_format=_a , **_a ) def __UpperCAmelCase ( self , _a , _a , _a , _a = None , **_a , ): return normalize(_a , mean=_a , std=_a , data_format=_a , **_a ) def __UpperCAmelCase ( self , _a , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = ChannelDimension.FIRST , **_a , ): __a = do_resize if do_resize is not None else self.do_resize __a = crop_pct if crop_pct is not None else self.crop_pct __a = resample if resample is not None else self.resample __a = do_rescale if do_rescale is not None else self.do_rescale __a = rescale_factor if rescale_factor is not None else self.rescale_factor __a = do_normalize if do_normalize is not None else self.do_normalize __a = image_mean if image_mean is not None else self.image_mean __a = image_std if image_std is not None else self.image_std __a = size if size is not None else self.size __a = get_size_dict(_a , default_to_square=_a ) __a = make_list_of_images(_a ) if not valid_images(_a ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None or resample is None: raise ValueError('''Size and resample must be specified if do_resize is True.''' ) if do_resize and size["shortest_edge"] < 384 and crop_pct is None: raise ValueError('''crop_pct must be specified if size < 384.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # All transformations expect numpy arrays. __a = [to_numpy_array(_a ) for image in images] if do_resize: __a = [self.resize(image=_a , size=_a , crop_pct=_a , resample=_a ) for image in images] if do_rescale: __a = [self.rescale(image=_a , scale=_a ) for image in images] if do_normalize: __a = [self.normalize(image=_a , mean=_a , std=_a ) for image in images] __a = [to_channel_dimension_format(_a , _a ) for image in images] __a = {'''pixel_values''': images} return BatchFeature(data=_a , tensor_type=_a )
695
"""simple docstring""" from __future__ import annotations import requests def lowercase ( lowerCAmelCase__ : str ) -> dict: __a = f'''https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty''' return requests.get(lowerCAmelCase__ ).json() def lowercase ( lowerCAmelCase__ : int = 10 ) -> list[dict]: __a = '''https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty''' __a = requests.get(lowerCAmelCase__ ).json()[:max_stories] return [get_hackernews_story(lowerCAmelCase__ ) for story_id in story_ids] def lowercase ( lowerCAmelCase__ : int = 10 ) -> str: __a = hackernews_top_stories(lowerCAmelCase__ ) return "\n".join('''* [{title}]({url})'''.format(**lowerCAmelCase__ ) for story in stories ) if __name__ == "__main__": print(hackernews_top_stories_as_markdown())
695
1
"""simple docstring""" import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler lowercase_ = 1_6 lowercase_ = 3_2 def lowercase ( lowerCAmelCase__ : Accelerator , lowerCAmelCase__ : int = 16 , lowerCAmelCase__ : str = "bert-base-cased" ) -> Optional[int]: __a = AutoTokenizer.from_pretrained(lowerCAmelCase__ ) __a = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(lowerCAmelCase__ : Optional[Any] ): # max_length=None => use the model max length (it's actually the default) __a = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=lowerCAmelCase__ , max_length=lowerCAmelCase__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset __a = datasets.map( lowerCAmelCase__ , batched=lowerCAmelCase__ , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , load_from_cache_file=lowerCAmelCase__ ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library __a = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(lowerCAmelCase__ : int ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(lowerCAmelCase__ , padding='''max_length''' , max_length=128 , return_tensors='''pt''' ) return tokenizer.pad(lowerCAmelCase__ , padding='''longest''' , return_tensors='''pt''' ) # Instantiate dataloaders. __a = DataLoader( tokenized_datasets['''train'''] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ ) __a = DataLoader( tokenized_datasets['''validation'''] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ ) return train_dataloader, eval_dataloader def lowercase ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any] ) -> Optional[int]: # Initialize accelerator __a = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __a = config['''lr'''] __a = int(config['''num_epochs'''] ) __a = int(config['''seed'''] ) __a = int(config['''batch_size'''] ) __a = args.model_name_or_path set_seed(lowerCAmelCase__ ) __a , __a = get_dataloaders(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __a = AutoModelForSequenceClassification.from_pretrained(lowerCAmelCase__ , return_dict=lowerCAmelCase__ ) # Instantiate optimizer __a = ( AdamW if accelerator.state.deepspeed_plugin is None or '''optimizer''' not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) __a = optimizer_cls(params=model.parameters() , lr=lowerCAmelCase__ ) if accelerator.state.deepspeed_plugin is not None: __a = accelerator.state.deepspeed_plugin.deepspeed_config[ '''gradient_accumulation_steps''' ] else: __a = 1 __a = (len(lowerCAmelCase__ ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): __a = get_linear_schedule_with_warmup( optimizer=lowerCAmelCase__ , num_warmup_steps=0 , num_training_steps=lowerCAmelCase__ , ) else: __a = DummyScheduler(lowerCAmelCase__ , total_num_steps=lowerCAmelCase__ , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. __a , __a , __a , __a , __a = accelerator.prepare( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # We need to keep track of how many total steps we have iterated over __a = 0 # We also need to keep track of the stating epoch so files are named properly __a = 0 # Now we train the model __a = evaluate.load('''glue''' , '''mrpc''' ) __a = 0 __a = {} for epoch in range(lowerCAmelCase__ , lowerCAmelCase__ ): model.train() for step, batch in enumerate(lowerCAmelCase__ ): __a = model(**lowerCAmelCase__ ) __a = outputs.loss __a = loss / gradient_accumulation_steps accelerator.backward(lowerCAmelCase__ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 model.eval() __a = 0 for step, batch in enumerate(lowerCAmelCase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): __a = model(**lowerCAmelCase__ ) __a = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times __a , __a = accelerator.gather( (predictions, batch['''labels''']) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(lowerCAmelCase__ ) - 1: __a = predictions[: len(eval_dataloader.dataset ) - samples_seen] __a = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=lowerCAmelCase__ , references=lowerCAmelCase__ , ) __a = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f'''epoch {epoch}:''' , lowerCAmelCase__ ) __a = eval_metric['''accuracy'''] if best_performance < eval_metric["accuracy"]: __a = eval_metric['''accuracy'''] if args.performance_lower_bound is not None: assert ( args.performance_lower_bound <= best_performance ), f'''Best performance metric {best_performance} is lower than the lower bound {args.performance_lower_bound}''' accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , '''all_results.json''' ) , '''w''' ) as f: json.dump(lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( ) -> List[str]: __a = argparse.ArgumentParser(description='''Simple example of training script tracking peak GPU memory usage.''' ) parser.add_argument( '''--model_name_or_path''' , type=lowerCAmelCase__ , default='''bert-base-cased''' , help='''Path to pretrained model or model identifier from huggingface.co/models.''' , required=lowerCAmelCase__ , ) parser.add_argument( '''--output_dir''' , type=lowerCAmelCase__ , default='''.''' , help='''Optional save directory where all checkpoint folders will be stored. Default is the current working directory.''' , ) parser.add_argument( '''--performance_lower_bound''' , type=lowerCAmelCase__ , default=lowerCAmelCase__ , help='''Optional lower bound for the performance metric. If set, the training will throw error when the performance metric drops below this value.''' , ) parser.add_argument( '''--num_epochs''' , type=lowerCAmelCase__ , default=3 , help='''Number of train epochs.''' , ) __a = parser.parse_args() __a = {'''lr''': 2e-5, '''num_epochs''': args.num_epochs, '''seed''': 42, '''batch_size''': 16} training_function(lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": main()
695
"""simple docstring""" import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES from ...utils import logging from ..auto import CONFIG_MAPPING lowercase_ = logging.get_logger(__name__) lowercase_ = { "salesforce/blip2-opt-2.7b": "https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json", } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = 'blip_2_vision_model' def __init__( self , _a=1_408 , _a=6_144 , _a=39 , _a=16 , _a=224 , _a=14 , _a="gelu" , _a=0.0_0001 , _a=0.0 , _a=1E-10 , _a=True , **_a , ): super().__init__(**_a ) __a = hidden_size __a = intermediate_size __a = num_hidden_layers __a = num_attention_heads __a = patch_size __a = image_size __a = initializer_range __a = attention_dropout __a = layer_norm_eps __a = hidden_act __a = qkv_bias @classmethod def __UpperCAmelCase ( cls , _a , **_a ): cls._set_token_in_kwargs(_a ) __a , __a = cls.get_config_dict(_a , **_a ) # get the vision config dict if we are loading from Blip2Config if config_dict.get('''model_type''' ) == "blip-2": __a = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(_a , **_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : str = 'blip_2_qformer' def __init__( self , _a=30_522 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=0.02 , _a=1E-12 , _a=0 , _a="absolute" , _a=2 , _a=1_408 , **_a , ): super().__init__(pad_token_id=_a , **_a ) __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = hidden_act __a = intermediate_size __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = initializer_range __a = layer_norm_eps __a = position_embedding_type __a = cross_attention_frequency __a = encoder_hidden_size @classmethod def __UpperCAmelCase ( cls , _a , **_a ): cls._set_token_in_kwargs(_a ) __a , __a = cls.get_config_dict(_a , **_a ) # get the qformer config dict if we are loading from Blip2Config if config_dict.get('''model_type''' ) == "blip-2": __a = config_dict['''qformer_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(_a , **_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Any = 'blip-2' __UpperCAmelCase : List[str] = True def __init__( self , _a=None , _a=None , _a=None , _a=32 , **_a ): super().__init__(**_a ) if vision_config is None: __a = {} logger.info('''vision_config is None. initializing the Blip2VisionConfig with default values.''' ) if qformer_config is None: __a = {} logger.info('''qformer_config is None. Initializing the Blip2QFormerConfig with default values.''' ) if text_config is None: __a = {} logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).''' ) __a = BlipaVisionConfig(**_a ) __a = BlipaQFormerConfig(**_a ) __a = text_config['''model_type'''] if '''model_type''' in text_config else '''opt''' __a = CONFIG_MAPPING[text_model_type](**_a ) __a = self.text_config.tie_word_embeddings __a = self.text_config.is_encoder_decoder __a = num_query_tokens __a = self.vision_config.hidden_size __a = self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES __a = 1.0 __a = 0.02 @classmethod def __UpperCAmelCase ( cls , _a , _a , _a , **_a , ): return cls( vision_config=vision_config.to_dict() , qformer_config=qformer_config.to_dict() , text_config=text_config.to_dict() , **_a , ) def __UpperCAmelCase ( self ): __a = copy.deepcopy(self.__dict__ ) __a = self.vision_config.to_dict() __a = self.qformer_config.to_dict() __a = self.text_config.to_dict() __a = self.__class__.model_type return output
695
1
"""simple docstring""" import operator as op lowercase_ = "scaler.pt" lowercase_ = "pytorch_model" lowercase_ = "random_states" lowercase_ = "optimizer" lowercase_ = "scheduler" lowercase_ = "pytorch_model.bin" lowercase_ = "pytorch_model.bin.index.json" lowercase_ = "model.safetensors" lowercase_ = "model.safetensors.index.json" lowercase_ = "1.10.2" lowercase_ = "py38" lowercase_ = "4.17.0" lowercase_ = ["ml.p3.16xlarge", "ml.p3dn.24xlarge", "ml.p4dn.24xlarge"] lowercase_ = ["FULL_SHARD", "SHARD_GRAD_OP", "NO_SHARD", "HYBRID_SHARD", "HYBRID_SHARD_ZERO2"] lowercase_ = ["TRANSFORMER_BASED_WRAP", "SIZE_BASED_WRAP", "NO_WRAP"] lowercase_ = ["BACKWARD_PRE", "BACKWARD_POST", "NO_PREFETCH"] lowercase_ = ["FULL_STATE_DICT", "LOCAL_STATE_DICT", "SHARDED_STATE_DICT"] lowercase_ = "2.0.1" lowercase_ = ["pdsh", "standard", "openmpi", "mvapich"] lowercase_ = ["default", "reduce-overhead", "max-autotune"] lowercase_ = {">": op.gt, ">=": op.ge, "==": op.eq, "!=": op.ne, "<=": op.le, "<": op.lt} # These are the args for `torch.distributed.launch` for pytorch < 1.9 lowercase_ = [ "nnodes", "nproc_per_node", "rdzv_backend", "rdzv_endpoint", "rdzv_id", "rdzv_conf", "standalone", "max_restarts", "monitor_interval", "start_method", "role", "module", "m", "no_python", "run_path", "log_dir", "r", "redirects", "t", "tee", "node_rank", "master_addr", "master_port", ] lowercase_ = ["DEEPSPEED", "MULTI_GPU", "FSDP", "MEGATRON_LM"] lowercase_ = ["DEEPSPEED", "MULTI_XPU", "FSDP"]
695
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType lowercase_ = logging.get_logger(__name__) lowercase_ = { "microsoft/deberta-v2-xlarge": "https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json", "microsoft/deberta-v2-xxlarge": "https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json", "microsoft/deberta-v2-xlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json" ), "microsoft/deberta-v2-xxlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json" ), } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Dict = 'deberta-v2' def __init__( self , _a=128_100 , _a=1_536 , _a=24 , _a=24 , _a=6_144 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=0 , _a=0.02 , _a=1E-7 , _a=False , _a=-1 , _a=0 , _a=True , _a=None , _a=0 , _a="gelu" , **_a , ): super().__init__(**_a ) __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = initializer_range __a = relative_attention __a = max_relative_positions __a = pad_token_id __a = position_biased_input # Backwards compatibility if type(_a ) == str: __a = [x.strip() for x in pos_att_type.lower().split('''|''' )] __a = pos_att_type __a = vocab_size __a = layer_norm_eps __a = kwargs.get('''pooler_hidden_size''' , _a ) __a = pooler_dropout __a = pooler_hidden_act class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def __UpperCAmelCase ( self ): if self.task == "multiple-choice": __a = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: __a = {0: '''batch''', 1: '''sequence'''} if self._config.type_vocab_size > 0: return OrderedDict( [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)] ) else: return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)] ) @property def __UpperCAmelCase ( self ): return 12 def __UpperCAmelCase ( self , _a , _a = -1 , _a = -1 , _a = -1 , _a = False , _a = None , _a = 3 , _a = 40 , _a = 40 , _a = None , ): __a = super().generate_dummy_inputs(preprocessor=_a , framework=_a ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
695
1
"""simple docstring""" import warnings from ...utils import logging from .image_processing_deit import DeiTImageProcessor lowercase_ = logging.get_logger(__name__) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , *_a , **_a ): warnings.warn( '''The class DeiTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use DeiTImageProcessor instead.''' , _a , ) super().__init__(*_a , **_a )
695
"""simple docstring""" import importlib.metadata import operator import re import sys from typing import Optional from packaging import version lowercase_ = { "<": operator.lt, "<=": operator.le, "==": operator.eq, "!=": operator.ne, ">=": operator.ge, ">": operator.gt, } def lowercase ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any] ) -> Dict: 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(lowerCAmelCase__ ) , version.parse(lowerCAmelCase__ ) ): raise ImportError( f'''{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}''' ) def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None ) -> None: __a = f'''\n{hint}''' if hint is not None else '''''' # non-versioned check if re.match(r'''^[\w_\-\d]+$''' , lowerCAmelCase__ ): __a , __a , __a = requirement, None, None else: __a = re.findall(r'''^([^!=<>\s]+)([\s!=<>]{1,2}.+)''' , lowerCAmelCase__ ) if not match: raise ValueError( '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but''' f''' got {requirement}''' ) __a , __a = match[0] __a = want_full.split(''',''' ) # there could be multiple requirements __a = {} for w in want_range: __a = re.findall(r'''^([\s!=<>]{1,2})(.+)''' , lowerCAmelCase__ ) if not match: raise ValueError( '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,''' f''' but got {requirement}''' ) __a , __a = match[0] __a = want_ver if op not in ops: raise ValueError(f'''{requirement}: need one of {list(ops.keys() )}, but got {op}''' ) # special case if pkg == "python": __a = '''.'''.join([str(lowerCAmelCase__ ) for x in sys.version_info[:3]] ) for op, want_ver in wanted.items(): _compare_versions(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) return # check if any version is installed try: __a = importlib.metadata.version(lowerCAmelCase__ ) 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(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Tuple ) -> Optional[Any]: __a = '''Try: pip install transformers -U or pip install -e \'.[dev]\' if you\'re working with git main''' return require_version(lowerCAmelCase__ , lowerCAmelCase__ )
695
1
"""simple docstring""" from __future__ import annotations def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float , ) -> tuple[str, float]: if (stress, tangential_force, area).count(0 ) != 1: raise ValueError('''You cannot supply more or less than 2 values''' ) elif stress < 0: raise ValueError('''Stress cannot be negative''' ) elif tangential_force < 0: raise ValueError('''Tangential Force cannot be negative''' ) elif area < 0: raise ValueError('''Area cannot be negative''' ) elif stress == 0: return ( "stress", tangential_force / area, ) elif tangential_force == 0: return ( "tangential_force", stress * area, ) else: return ( "area", tangential_force / stress, ) if __name__ == "__main__": import doctest doctest.testmod()
695
"""simple docstring""" from __future__ import annotations lowercase_ = list[tuple[int, int]] lowercase_ = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] lowercase_ = ([-1, 0], [0, -1], [1, 0], [0, 1]) # up, left, down, right class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a , _a , _a , _a , _a , ): __a = pos_x __a = pos_y __a = (pos_y, pos_x) __a = goal_x __a = goal_y __a = g_cost __a = parent __a = self.calculate_heuristic() def __UpperCAmelCase ( self ): __a = abs(self.pos_x - self.goal_x ) __a = abs(self.pos_y - self.goal_y ) return dx + dy def __lt__( self , _a ): return self.f_cost < other.f_cost class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a ): __a = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , _a ) __a = Node(goal[1] , goal[0] , goal[1] , goal[0] , 99_999 , _a ) __a = [self.start] __a = [] __a = False def __UpperCAmelCase ( self ): while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() __a = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: __a = True return self.retrace_path(_a ) self.closed_nodes.append(_a ) __a = self.get_successors(_a ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(_a ) else: # retrieve the best current path __a = self.open_nodes.pop(self.open_nodes.index(_a ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(_a ) else: self.open_nodes.append(_a ) if not self.reached: return [self.start.pos] return None def __UpperCAmelCase ( self , _a ): __a = [] for action in delta: __a = parent.pos_x + action[1] __a = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(_a ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( _a , _a , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , _a , ) ) return successors def __UpperCAmelCase ( self , _a ): __a = node __a = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) __a = current_node.parent path.reverse() return path if __name__ == "__main__": lowercase_ = (0, 0) lowercase_ = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) print("------") lowercase_ = GreedyBestFirst(init, goal) lowercase_ = greedy_bf.search() if path: for pos_x, pos_y in path: lowercase_ = 2 for elem in grid: print(elem)
695
1
"""simple docstring""" import os import sys import unittest lowercase_ = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_dummies # noqa: E402 from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init # noqa: E402 # Align TRANSFORMERS_PATH in check_dummies with the current path lowercase_ = os.path.join(git_repo_path, "src", "transformers") lowercase_ = "\n{0} = None\n" lowercase_ = "\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n" lowercase_ = "\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n" class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = find_backend(''' _import_structure["models.albert"].append("AlbertTokenizerFast")''' ) self.assertIsNone(_a ) __a = find_backend(''' if not is_tokenizers_available():''' ) self.assertEqual(_a , '''tokenizers''' ) __a = find_backend(''' if not is_tensorflow_text_available():''' ) self.assertEqual(_a , '''tensorflow_text''' ) __a = find_backend(''' if not (is_sentencepiece_available() and is_tokenizers_available()):''' ) self.assertEqual(_a , '''sentencepiece_and_tokenizers''' ) __a = find_backend( ''' if not (is_sentencepiece_available() and is_tensorflow_text_available()):''' ) self.assertEqual(_a , '''sentencepiece_and_tensorflow_text''' ) __a = find_backend( ''' if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):''' ) self.assertEqual(_a , '''sentencepiece_and_tokenizers_and_vision''' ) def __UpperCAmelCase ( self ): __a = read_init() # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects self.assertIn('''torch''' , _a ) self.assertIn('''tensorflow_text''' , _a ) self.assertIn('''sentencepiece_and_tokenizers''' , _a ) # Likewise, we can't assert on the exact content of a key self.assertIn('''BertModel''' , objects['''torch'''] ) self.assertIn('''TFBertModel''' , objects['''tf'''] ) self.assertIn('''FlaxBertModel''' , objects['''flax'''] ) self.assertIn('''BertModel''' , objects['''torch'''] ) self.assertIn('''TFBertTokenizer''' , objects['''tensorflow_text'''] ) self.assertIn('''convert_slow_tokenizer''' , objects['''sentencepiece_and_tokenizers'''] ) def __UpperCAmelCase ( self ): __a = create_dummy_object('''CONSTANT''' , '''\'torch\'''' ) self.assertEqual(_a , '''\nCONSTANT = None\n''' ) __a = create_dummy_object('''function''' , '''\'torch\'''' ) self.assertEqual( _a , '''\ndef function(*args, **kwargs):\n requires_backends(function, \'torch\')\n''' ) __a = ''' class FakeClass(metaclass=DummyObject): _backends = \'torch\' def __init__(self, *args, **kwargs): requires_backends(self, \'torch\') ''' __a = create_dummy_object('''FakeClass''' , '''\'torch\'''' ) self.assertEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = '''# This file is autogenerated by the command `make fix-copies`, do not edit. from ..utils import DummyObject, requires_backends CONSTANT = None def function(*args, **kwargs): requires_backends(function, ["torch"]) class FakeClass(metaclass=DummyObject): _backends = ["torch"] def __init__(self, *args, **kwargs): requires_backends(self, ["torch"]) ''' __a = create_dummy_files({'''torch''': ['''CONSTANT''', '''function''', '''FakeClass''']} ) self.assertEqual(dummy_files['''torch'''] , _a )
695
"""simple docstring""" import argparse import torch from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert from transformers.utils import logging logging.set_verbosity_info() def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str ) -> List[Any]: # Initialise PyTorch model __a = RemBertConfig.from_json_file(lowerCAmelCase__ ) print('''Building PyTorch model from configuration: {}'''.format(str(lowerCAmelCase__ ) ) ) __a = RemBertModel(lowerCAmelCase__ ) # Load weights from tf checkpoint load_tf_weights_in_rembert(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Save pytorch-model print('''Save PyTorch model to {}'''.format(lowerCAmelCase__ ) ) torch.save(model.state_dict() , lowerCAmelCase__ ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--rembert_config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained RemBERT 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." ) lowercase_ = parser.parse_args() convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)
695
1
"""simple docstring""" import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING lowercase_ = logging.get_logger(__name__) lowercase_ = { "SenseTime/deformable-detr": "https://huggingface.co/sensetime/deformable-detr/resolve/main/config.json", # See all Deformable DETR models at https://huggingface.co/models?filter=deformable-detr } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = 'deformable_detr' __UpperCAmelCase : Optional[int] = { 'hidden_size': 'd_model', 'num_attention_heads': 'encoder_attention_heads', } def __init__( self , _a=True , _a=None , _a=3 , _a=300 , _a=1_024 , _a=6 , _a=1_024 , _a=8 , _a=6 , _a=1_024 , _a=8 , _a=0.0 , _a=True , _a="relu" , _a=256 , _a=0.1 , _a=0.0 , _a=0.0 , _a=0.02 , _a=1.0 , _a=True , _a=False , _a="sine" , _a="resnet50" , _a=True , _a=False , _a=4 , _a=4 , _a=4 , _a=False , _a=300 , _a=False , _a=1 , _a=5 , _a=2 , _a=1 , _a=1 , _a=5 , _a=2 , _a=0.1 , _a=0.25 , _a=False , **_a , ): if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) __a = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(_a , _a ): __a = backbone_config.get('''model_type''' ) __a = CONFIG_MAPPING[backbone_model_type] __a = config_class.from_dict(_a ) __a = use_timm_backbone __a = backbone_config __a = num_channels __a = num_queries __a = max_position_embeddings __a = d_model __a = encoder_ffn_dim __a = encoder_layers __a = encoder_attention_heads __a = decoder_ffn_dim __a = decoder_layers __a = decoder_attention_heads __a = dropout __a = attention_dropout __a = activation_dropout __a = activation_function __a = init_std __a = init_xavier_std __a = encoder_layerdrop __a = auxiliary_loss __a = position_embedding_type __a = backbone __a = use_pretrained_backbone __a = dilation # deformable attributes __a = num_feature_levels __a = encoder_n_points __a = decoder_n_points __a = two_stage __a = two_stage_num_proposals __a = with_box_refine if two_stage is True and with_box_refine is False: raise ValueError('''If two_stage is True, with_box_refine must be True.''' ) # Hungarian matcher __a = class_cost __a = bbox_cost __a = giou_cost # Loss coefficients __a = mask_loss_coefficient __a = dice_loss_coefficient __a = bbox_loss_coefficient __a = giou_loss_coefficient __a = eos_coefficient __a = focal_alpha __a = disable_custom_kernels super().__init__(is_encoder_decoder=_a , **_a ) @property def __UpperCAmelCase ( self ): return self.encoder_attention_heads @property def __UpperCAmelCase ( self ): return self.d_model def __UpperCAmelCase ( self ): __a = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: __a = self.backbone_config.to_dict() __a = self.__class__.model_type return output
695
"""simple docstring""" import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel lowercase_ = "0.12" # assumed parallelism: 8 @require_flax @is_staging_test class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @classmethod def __UpperCAmelCase ( cls ): __a = TOKEN HfFolder.save_token(_a ) @classmethod def __UpperCAmelCase ( cls ): try: delete_repo(token=cls._token , repo_id='''test-model-flax''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-model-flax-org''' ) except HTTPError: pass def __UpperCAmelCase ( self ): __a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) __a = FlaxBertModel(_a ) model.push_to_hub('''test-model-flax''' , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''test-model-flax''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(_a , repo_id='''test-model-flax''' , push_to_hub=_a , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) def __UpperCAmelCase ( self ): __a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) __a = FlaxBertModel(_a ) model.push_to_hub('''valid_org/test-model-flax-org''' , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-model-flax-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( _a , repo_id='''valid_org/test-model-flax-org''' , push_to_hub=_a , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : Dict ) -> Optional[int]: __a = True __a = flatten_dict(modela.params ) __a = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1e-4: __a = False return models_are_equal @require_flax class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) __a = FlaxBertModel(_a ) __a = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_a , _a ) ) with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertTrue(check_models_equal(_a , _a ) ) def __UpperCAmelCase ( self ): __a = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) __a = FlaxBertModel(_a ) __a = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_a , _a ) , max_shard_size='''10KB''' ) with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertTrue(check_models_equal(_a , _a ) ) def __UpperCAmelCase ( self ): __a = '''bert''' __a = '''hf-internal-testing/tiny-random-bert-subfolder''' with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertIsNotNone(_a ) def __UpperCAmelCase ( self ): __a = '''bert''' __a = '''hf-internal-testing/tiny-random-bert-sharded-subfolder''' with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertIsNotNone(_a )
695
1
"""simple docstring""" import math import unittest def lowercase ( lowerCAmelCase__ : int ) -> bool: assert isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and ( number >= 0 ), "'number' must been an int and positive" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or 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(lowerCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): self.assertTrue(is_prime(2 ) ) self.assertTrue(is_prime(3 ) ) self.assertTrue(is_prime(5 ) ) self.assertTrue(is_prime(7 ) ) self.assertTrue(is_prime(11 ) ) self.assertTrue(is_prime(13 ) ) self.assertTrue(is_prime(17 ) ) self.assertTrue(is_prime(19 ) ) self.assertTrue(is_prime(23 ) ) self.assertTrue(is_prime(29 ) ) def __UpperCAmelCase ( self ): with self.assertRaises(_a ): is_prime(-19 ) self.assertFalse( is_prime(0 ) , '''Zero doesn\'t have any positive factors, primes must have exactly two.''' , ) self.assertFalse( is_prime(1 ) , '''One only has 1 positive factor, primes must have exactly two.''' , ) self.assertFalse(is_prime(2 * 2 ) ) self.assertFalse(is_prime(2 * 3 ) ) self.assertFalse(is_prime(3 * 3 ) ) self.assertFalse(is_prime(3 * 5 ) ) self.assertFalse(is_prime(3 * 5 * 7 ) ) if __name__ == "__main__": unittest.main()
695
"""simple docstring""" import unittest from diffusers.models.unet_ad_blocks import * # noqa F403 from diffusers.utils import torch_device from .test_unet_blocks_common import UNetBlockTesterMixin class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = DownBlockaD # noqa F405 __UpperCAmelCase : Any = 'down' def __UpperCAmelCase ( self ): __a = [-0.0232, -0.9869, 0.8054, -0.0637, -0.1688, -1.4264, 0.4470, -1.3394, 0.0904] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = ResnetDownsampleBlockaD # noqa F405 __UpperCAmelCase : List[str] = 'down' def __UpperCAmelCase ( self ): __a = [0.0710, 0.2410, -0.7320, -1.0757, -1.1343, 0.3540, -0.0133, -0.2576, 0.0948] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[int] = AttnDownBlockaD # noqa F405 __UpperCAmelCase : Optional[Any] = 'down' def __UpperCAmelCase ( self ): __a = [0.0636, 0.8964, -0.6234, -1.0131, 0.0844, 0.4935, 0.3437, 0.0911, -0.2957] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : List[Any] = CrossAttnDownBlockaD # noqa F405 __UpperCAmelCase : Optional[Any] = 'down' def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.2238, -0.7396, -0.2255, -0.3829, 0.1925, 1.1665, 0.0603, -0.7295, 0.1983] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = SimpleCrossAttnDownBlockaD # noqa F405 __UpperCAmelCase : Any = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_encoder_hidden_states=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict @unittest.skipIf(torch_device == '''mps''' , '''MPS result is not consistent''' ) def __UpperCAmelCase ( self ): __a = [0.7921, -0.0992, -0.1962, -0.7695, -0.4242, 0.7804, 0.4737, 0.2765, 0.3338] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = SkipDownBlockaD # noqa F405 __UpperCAmelCase : Tuple = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_skip_sample=_a ) def __UpperCAmelCase ( self ): __a = [-0.0845, -0.2087, -0.2465, 0.0971, 0.1900, -0.0484, 0.2664, 0.4179, 0.5069] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : List[Any] = AttnSkipDownBlockaD # noqa F405 __UpperCAmelCase : Optional[int] = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_skip_sample=_a ) def __UpperCAmelCase ( self ): __a = [0.5539, 0.1609, 0.4924, 0.0537, -0.1995, 0.4050, 0.0979, -0.2721, -0.0642] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = DownEncoderBlockaD # noqa F405 __UpperCAmelCase : Optional[int] = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = { '''in_channels''': 32, '''out_channels''': 32, } __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [1.1102, 0.5302, 0.4872, -0.0023, -0.8042, 0.0483, -0.3489, -0.5632, 0.7626] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = AttnDownEncoderBlockaD # noqa F405 __UpperCAmelCase : Any = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = { '''in_channels''': 32, '''out_channels''': 32, } __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.8966, -0.1486, 0.8568, 0.8141, -0.9046, -0.1342, -0.0972, -0.7417, 0.1538] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = UNetMidBlockaD # noqa F405 __UpperCAmelCase : Any = 'mid' def __UpperCAmelCase ( self ): __a = { '''in_channels''': 32, '''temb_channels''': 128, } __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [-0.1062, 1.7248, 0.3494, 1.4569, -0.0910, -1.2421, -0.9984, 0.6736, 1.0028] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = UNetMidBlockaDCrossAttn # noqa F405 __UpperCAmelCase : str = 'mid' def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.0187, 2.4220, 0.4484, 1.1203, -0.6121, -1.5122, -0.8270, 0.7851, 1.8335] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Any = UNetMidBlockaDSimpleCrossAttn # noqa F405 __UpperCAmelCase : List[Any] = 'mid' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_encoder_hidden_states=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.7143, 1.9974, 0.5448, 1.3977, 0.1282, -1.1237, -1.4238, 0.5530, 0.8880] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = UpBlockaD # noqa F405 __UpperCAmelCase : Union[str, Any] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [-0.2041, -0.4165, -0.3022, 0.0041, -0.6628, -0.7053, 0.1928, -0.0325, 0.0523] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = ResnetUpsampleBlockaD # noqa F405 __UpperCAmelCase : int = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [0.2287, 0.3549, -0.1346, 0.4797, -0.1715, -0.9649, 0.7305, -0.5864, -0.6244] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Dict = CrossAttnUpBlockaD # noqa F405 __UpperCAmelCase : List[Any] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [-0.1403, -0.3515, -0.0420, -0.1425, 0.3167, 0.5094, -0.2181, 0.5931, 0.5582] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = SimpleCrossAttnUpBlockaD # noqa F405 __UpperCAmelCase : Optional[int] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a , include_encoder_hidden_states=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.2645, 0.1480, 0.0909, 0.8044, -0.9758, -0.9083, 0.0994, -1.1453, -0.7402] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Any = AttnUpBlockaD # noqa F405 __UpperCAmelCase : List[Any] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) @unittest.skipIf(torch_device == '''mps''' , '''MPS result is not consistent''' ) def __UpperCAmelCase ( self ): __a = [0.0979, 0.1326, 0.0021, 0.0659, 0.2249, 0.0059, 0.1132, 0.5952, 0.1033] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Any = SkipUpBlockaD # noqa F405 __UpperCAmelCase : str = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [-0.0893, -0.1234, -0.1506, -0.0332, 0.0123, -0.0211, 0.0566, 0.0143, 0.0362] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = AttnSkipUpBlockaD # noqa F405 __UpperCAmelCase : int = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [0.0361, 0.0617, 0.2787, -0.0350, 0.0342, 0.3421, -0.0843, 0.0913, 0.3015] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = UpDecoderBlockaD # noqa F405 __UpperCAmelCase : List[str] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = {'''in_channels''': 32, '''out_channels''': 32} __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.4404, 0.1998, -0.9886, -0.3320, -0.3128, -0.7034, -0.6955, -0.2338, -0.3137] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[int] = AttnUpDecoderBlockaD # noqa F405 __UpperCAmelCase : Any = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = {'''in_channels''': 32, '''out_channels''': 32} __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.6738, 0.4491, 0.1055, 1.0710, 0.7316, 0.3339, 0.3352, 0.1023, 0.3568] super().test_output(_a )
695
1
"""simple docstring""" from decimal import Decimal, getcontext from math import ceil, factorial def lowercase ( lowerCAmelCase__ : int ) -> str: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise TypeError('''Undefined for non-integers''' ) elif precision < 1: raise ValueError('''Undefined for non-natural numbers''' ) __a = precision __a = ceil(precision / 14 ) __a = 426880 * Decimal(10005 ).sqrt() __a = 1 __a = 13591409 __a = Decimal(lowerCAmelCase__ ) for k in range(1 , lowerCAmelCase__ ): __a = factorial(6 * k ) // (factorial(3 * k ) * factorial(lowerCAmelCase__ ) ** 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__": lowercase_ = 5_0 print(F'''The first {n} digits of pi is: {pi(n)}''')
695
"""simple docstring""" import copy from typing import Dict, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING from ..detr import DetrConfig from ..swin import SwinConfig lowercase_ = { "facebook/maskformer-swin-base-ade": ( "https://huggingface.co/facebook/maskformer-swin-base-ade/blob/main/config.json" ) # See all MaskFormer models at https://huggingface.co/models?filter=maskformer } lowercase_ = logging.get_logger(__name__) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : str = 'maskformer' __UpperCAmelCase : Optional[int] = {'hidden_size': 'mask_feature_size'} __UpperCAmelCase : Any = ['resnet', 'swin'] __UpperCAmelCase : Dict = ['detr'] def __init__( self , _a = 256 , _a = 256 , _a = 0.1 , _a = False , _a = None , _a = None , _a = 0.02 , _a = 1.0 , _a = 1.0 , _a = 1.0 , _a = 20.0 , _a = None , **_a , ): if backbone_config is None: # fall back to https://huggingface.co/microsoft/swin-base-patch4-window12-384-in22k __a = SwinConfig( image_size=384 , in_channels=3 , patch_size=4 , embed_dim=128 , depths=[2, 2, 18, 2] , num_heads=[4, 8, 16, 32] , window_size=12 , drop_path_rate=0.3 , out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] , ) if isinstance(_a , _a ): __a = backbone_config.pop('''model_type''' ) __a = CONFIG_MAPPING[backbone_model_type] __a = config_class.from_dict(_a ) # verify that the backbone is supported if backbone_config.model_type not in self.backbones_supported: logger.warning_once( f'''Backbone {backbone_config.model_type} is not a supported model and may not be compatible with MaskFormer. ''' f'''Supported model types: {','.join(self.backbones_supported )}''' ) if decoder_config is None: # fall back to https://huggingface.co/facebook/detr-resnet-50 __a = DetrConfig() else: # verify that the decoder is supported __a = ( decoder_config.pop('''model_type''' ) if isinstance(_a , _a ) else decoder_config.model_type ) if decoder_type not in self.decoders_supported: raise ValueError( f'''Transformer Decoder {decoder_type} not supported, please use one of''' f''' {','.join(self.decoders_supported )}''' ) if isinstance(_a , _a ): __a = CONFIG_MAPPING[decoder_type] __a = config_class.from_dict(_a ) __a = backbone_config __a = decoder_config # main feature dimension for the model __a = fpn_feature_size __a = mask_feature_size # initializer __a = init_std __a = init_xavier_std # Hungarian matcher && loss __a = cross_entropy_weight __a = dice_weight __a = mask_weight __a = use_auxiliary_loss __a = no_object_weight __a = output_auxiliary_logits __a = self.decoder_config.encoder_attention_heads __a = self.decoder_config.num_hidden_layers super().__init__(**_a ) @classmethod def __UpperCAmelCase ( cls , _a , _a , **_a ): return cls( backbone_config=_a , decoder_config=_a , **_a , ) def __UpperCAmelCase ( self ): __a = copy.deepcopy(self.__dict__ ) __a = self.backbone_config.to_dict() __a = self.decoder_config.to_dict() __a = self.__class__.model_type return output
695
1
"""simple docstring""" # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from pathlib import Path import torch from ...utils import is_npu_available, is_xpu_available from .config_args import ClusterConfig, default_json_config_file from .config_utils import SubcommandHelpFormatter lowercase_ = "Create a default config file for Accelerate with only a few flags set." def lowercase ( lowerCAmelCase__ : Optional[int]="no" , lowerCAmelCase__ : str = default_json_config_file , lowerCAmelCase__ : bool = False ) -> Dict: __a = Path(lowerCAmelCase__ ) path.parent.mkdir(parents=lowerCAmelCase__ , exist_ok=lowerCAmelCase__ ) if path.exists(): print( f'''Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.''' ) return False __a = mixed_precision.lower() if mixed_precision not in ["no", "fp16", "bf16", "fp8"]: raise ValueError( f'''`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}''' ) __a = { '''compute_environment''': '''LOCAL_MACHINE''', '''mixed_precision''': mixed_precision, } if torch.cuda.is_available(): __a = torch.cuda.device_count() __a = num_gpus __a = False if num_gpus > 1: __a = '''MULTI_GPU''' else: __a = '''NO''' elif is_xpu_available() and use_xpu: __a = torch.xpu.device_count() __a = num_xpus __a = False if num_xpus > 1: __a = '''MULTI_XPU''' else: __a = '''NO''' elif is_npu_available(): __a = torch.npu.device_count() __a = num_npus __a = False if num_npus > 1: __a = '''MULTI_NPU''' else: __a = '''NO''' else: __a = 0 __a = True __a = 1 __a = '''NO''' __a = ClusterConfig(**lowerCAmelCase__ ) config.to_json_file(lowerCAmelCase__ ) return path def lowercase ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Tuple ) -> int: __a = parser.add_parser('''default''' , parents=lowerCAmelCase__ , help=lowerCAmelCase__ , formatter_class=lowerCAmelCase__ ) parser.add_argument( '''--config_file''' , default=lowerCAmelCase__ , help=( '''The path to use to store the config file. Will default to a file named default_config.yaml in the cache ''' '''location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have ''' '''such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed ''' '''with \'huggingface\'.''' ) , dest='''save_location''' , ) parser.add_argument( '''--mixed_precision''' , choices=['''no''', '''fp16''', '''bf16'''] , type=lowerCAmelCase__ , help='''Whether or not to use mixed precision training. ''' '''Choose between FP16 and BF16 (bfloat16) training. ''' '''BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.''' , default='''no''' , ) parser.set_defaults(func=lowerCAmelCase__ ) return parser def lowercase ( lowerCAmelCase__ : Optional[Any] ) -> int: __a = write_basic_config(args.mixed_precision , args.save_location ) if config_file: print(f'''accelerate configuration saved at {config_file}''' )
695
"""simple docstring""" from __future__ import annotations from collections.abc import Generator import requests from bsa import BeautifulSoup lowercase_ = "https://www.indeed.co.in/jobs?q=mobile+app+development&l=" def lowercase ( lowerCAmelCase__ : str = "mumbai" ) -> Generator[tuple[str, str], None, None]: __a = BeautifulSoup(requests.get(url + location ).content , '''html.parser''' ) # This attribute finds out all the specifics listed in a job for job in soup.find_all('''div''' , attrs={'''data-tn-component''': '''organicJob'''} ): __a = job.find('''a''' , attrs={'''data-tn-element''': '''jobTitle'''} ).text.strip() __a = job.find('''span''' , {'''class''': '''company'''} ).text.strip() yield job_title, company_name if __name__ == "__main__": for i, job in enumerate(fetch_jobs("Bangalore"), 1): print(F'''Job {i:>2} is {job[0]} at {job[1]}''')
695
1
"""simple docstring""" import argparse import requests import torch from PIL import Image from transformers import SwinConfig, SwinForMaskedImageModeling, ViTImageProcessor def lowercase ( lowerCAmelCase__ : List[Any] ) -> Union[str, Any]: __a = SwinConfig(image_size=192 ) if "base" in model_name: __a = 6 __a = 128 __a = (2, 2, 18, 2) __a = (4, 8, 16, 32) elif "large" in model_name: __a = 12 __a = 192 __a = (2, 2, 18, 2) __a = (6, 12, 24, 48) else: raise ValueError('''Model not supported, only supports base and large variants''' ) __a = window_size __a = embed_dim __a = depths __a = num_heads return config def lowercase ( lowerCAmelCase__ : Optional[Any] ) -> Any: if "encoder.mask_token" in name: __a = name.replace('''encoder.mask_token''' , '''embeddings.mask_token''' ) if "encoder.patch_embed.proj" in name: __a = name.replace('''encoder.patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) if "encoder.patch_embed.norm" in name: __a = name.replace('''encoder.patch_embed.norm''' , '''embeddings.norm''' ) if "attn.proj" in name: __a = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name: __a = name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: __a = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: __a = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: __a = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: __a = name.replace('''mlp.fc2''' , '''output.dense''' ) if name == "encoder.norm.weight": __a = '''layernorm.weight''' if name == "encoder.norm.bias": __a = '''layernorm.bias''' if "decoder" in name: pass else: __a = '''swin.''' + name return name def lowercase ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Any ) -> Union[str, Any]: for key in orig_state_dict.copy().keys(): __a = orig_state_dict.pop(lowerCAmelCase__ ) if "attn_mask" in key: pass elif "qkv" in key: __a = key.split('''.''' ) __a = int(key_split[2] ) __a = int(key_split[4] ) __a = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: __a = val[:dim, :] __a = val[ dim : dim * 2, : ] __a = val[-dim:, :] else: __a = val[ :dim ] __a = val[ dim : dim * 2 ] __a = val[ -dim: ] else: __a = val return orig_state_dict def lowercase ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Tuple ) -> Dict: __a = torch.load(lowerCAmelCase__ , map_location='''cpu''' )['''model'''] __a = get_swin_config(lowerCAmelCase__ ) __a = SwinForMaskedImageModeling(lowerCAmelCase__ ) model.eval() __a = convert_state_dict(lowerCAmelCase__ , lowerCAmelCase__ ) model.load_state_dict(lowerCAmelCase__ ) __a = '''http://images.cocodataset.org/val2017/000000039769.jpg''' __a = ViTImageProcessor(size={'''height''': 192, '''width''': 192} ) __a = Image.open(requests.get(lowerCAmelCase__ , stream=lowerCAmelCase__ ).raw ) __a = image_processor(images=lowerCAmelCase__ , return_tensors='''pt''' ) with torch.no_grad(): __a = model(**lowerCAmelCase__ ).logits print(outputs.keys() ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(f'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(lowerCAmelCase__ ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(lowerCAmelCase__ ) if push_to_hub: print(f'''Pushing model and image processor for {model_name} to hub''' ) model.push_to_hub(f'''microsoft/{model_name}''' ) image_processor.push_to_hub(f'''microsoft/{model_name}''' ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="swin-base-simmim-window6-192", type=str, choices=["swin-base-simmim-window6-192", "swin-large-simmim-window12-192"], help="Name of the Swin SimMIM model you'd like to convert.", ) parser.add_argument( "--checkpoint_path", default="/Users/nielsrogge/Documents/SwinSimMIM/simmim_pretrain__swin_base__img192_window6__100ep.pth", type=str, help="Path to the original PyTorch checkpoint (.pth file).", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) lowercase_ = parser.parse_args() convert_swin_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
695
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "bigcode/gpt_bigcode-santacoder": "https://huggingface.co/bigcode/gpt_bigcode-santacoder/resolve/main/config.json", } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = 'gpt_bigcode' __UpperCAmelCase : Tuple = ['past_key_values'] __UpperCAmelCase : Dict = { 'hidden_size': 'n_embd', 'max_position_embeddings': 'n_positions', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self , _a=50_257 , _a=1_024 , _a=768 , _a=12 , _a=12 , _a=None , _a="gelu_pytorch_tanh" , _a=0.1 , _a=0.1 , _a=0.1 , _a=1E-5 , _a=0.02 , _a=True , _a=True , _a=50_256 , _a=50_256 , _a=True , _a=True , _a=True , **_a , ): __a = vocab_size __a = n_positions __a = n_embd __a = n_layer __a = n_head __a = n_inner __a = activation_function __a = resid_pdrop __a = embd_pdrop __a = attn_pdrop __a = layer_norm_epsilon __a = initializer_range __a = scale_attn_weights __a = use_cache __a = attention_softmax_in_fpaa __a = scale_attention_softmax_in_fpaa __a = multi_query __a = bos_token_id __a = eos_token_id super().__init__(bos_token_id=_a , eos_token_id=_a , **_a )
695
1
"""simple docstring""" import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=__SCREAMING_SNAKE_CASE ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : str = field(default='image-classification' , metadata={'include_in_asdict_even_if_is_default': True} ) __UpperCAmelCase : ClassVar[Features] = Features({'image': Image()} ) __UpperCAmelCase : ClassVar[Features] = Features({'labels': ClassLabel} ) __UpperCAmelCase : str = "image" __UpperCAmelCase : str = "labels" def __UpperCAmelCase ( self , _a ): if self.label_column not in features: raise ValueError(f'''Column {self.label_column} is not present in features.''' ) if not isinstance(features[self.label_column] , _a ): raise ValueError(f'''Column {self.label_column} is not a ClassLabel.''' ) __a = copy.deepcopy(self ) __a = self.label_schema.copy() __a = features[self.label_column] __a = label_schema return task_template @property def __UpperCAmelCase ( self ): return { self.image_column: "image", self.label_column: "labels", }
695
"""simple docstring""" import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler lowercase_ = 1_6 lowercase_ = 3_2 def lowercase ( lowerCAmelCase__ : Accelerator , lowerCAmelCase__ : int = 16 , lowerCAmelCase__ : str = "bert-base-cased" ) -> Optional[int]: __a = AutoTokenizer.from_pretrained(lowerCAmelCase__ ) __a = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(lowerCAmelCase__ : Optional[Any] ): # max_length=None => use the model max length (it's actually the default) __a = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=lowerCAmelCase__ , max_length=lowerCAmelCase__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset __a = datasets.map( lowerCAmelCase__ , batched=lowerCAmelCase__ , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , load_from_cache_file=lowerCAmelCase__ ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library __a = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(lowerCAmelCase__ : int ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(lowerCAmelCase__ , padding='''max_length''' , max_length=128 , return_tensors='''pt''' ) return tokenizer.pad(lowerCAmelCase__ , padding='''longest''' , return_tensors='''pt''' ) # Instantiate dataloaders. __a = DataLoader( tokenized_datasets['''train'''] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ ) __a = DataLoader( tokenized_datasets['''validation'''] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ ) return train_dataloader, eval_dataloader def lowercase ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any] ) -> Optional[int]: # Initialize accelerator __a = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __a = config['''lr'''] __a = int(config['''num_epochs'''] ) __a = int(config['''seed'''] ) __a = int(config['''batch_size'''] ) __a = args.model_name_or_path set_seed(lowerCAmelCase__ ) __a , __a = get_dataloaders(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __a = AutoModelForSequenceClassification.from_pretrained(lowerCAmelCase__ , return_dict=lowerCAmelCase__ ) # Instantiate optimizer __a = ( AdamW if accelerator.state.deepspeed_plugin is None or '''optimizer''' not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) __a = optimizer_cls(params=model.parameters() , lr=lowerCAmelCase__ ) if accelerator.state.deepspeed_plugin is not None: __a = accelerator.state.deepspeed_plugin.deepspeed_config[ '''gradient_accumulation_steps''' ] else: __a = 1 __a = (len(lowerCAmelCase__ ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): __a = get_linear_schedule_with_warmup( optimizer=lowerCAmelCase__ , num_warmup_steps=0 , num_training_steps=lowerCAmelCase__ , ) else: __a = DummyScheduler(lowerCAmelCase__ , total_num_steps=lowerCAmelCase__ , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. __a , __a , __a , __a , __a = accelerator.prepare( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # We need to keep track of how many total steps we have iterated over __a = 0 # We also need to keep track of the stating epoch so files are named properly __a = 0 # Now we train the model __a = evaluate.load('''glue''' , '''mrpc''' ) __a = 0 __a = {} for epoch in range(lowerCAmelCase__ , lowerCAmelCase__ ): model.train() for step, batch in enumerate(lowerCAmelCase__ ): __a = model(**lowerCAmelCase__ ) __a = outputs.loss __a = loss / gradient_accumulation_steps accelerator.backward(lowerCAmelCase__ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 model.eval() __a = 0 for step, batch in enumerate(lowerCAmelCase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): __a = model(**lowerCAmelCase__ ) __a = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times __a , __a = accelerator.gather( (predictions, batch['''labels''']) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(lowerCAmelCase__ ) - 1: __a = predictions[: len(eval_dataloader.dataset ) - samples_seen] __a = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=lowerCAmelCase__ , references=lowerCAmelCase__ , ) __a = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f'''epoch {epoch}:''' , lowerCAmelCase__ ) __a = eval_metric['''accuracy'''] if best_performance < eval_metric["accuracy"]: __a = eval_metric['''accuracy'''] if args.performance_lower_bound is not None: assert ( args.performance_lower_bound <= best_performance ), f'''Best performance metric {best_performance} is lower than the lower bound {args.performance_lower_bound}''' accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , '''all_results.json''' ) , '''w''' ) as f: json.dump(lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( ) -> List[str]: __a = argparse.ArgumentParser(description='''Simple example of training script tracking peak GPU memory usage.''' ) parser.add_argument( '''--model_name_or_path''' , type=lowerCAmelCase__ , default='''bert-base-cased''' , help='''Path to pretrained model or model identifier from huggingface.co/models.''' , required=lowerCAmelCase__ , ) parser.add_argument( '''--output_dir''' , type=lowerCAmelCase__ , default='''.''' , help='''Optional save directory where all checkpoint folders will be stored. Default is the current working directory.''' , ) parser.add_argument( '''--performance_lower_bound''' , type=lowerCAmelCase__ , default=lowerCAmelCase__ , help='''Optional lower bound for the performance metric. If set, the training will throw error when the performance metric drops below this value.''' , ) parser.add_argument( '''--num_epochs''' , type=lowerCAmelCase__ , default=3 , help='''Number of train epochs.''' , ) __a = parser.parse_args() __a = {'''lr''': 2e-5, '''num_epochs''': args.num_epochs, '''seed''': 42, '''batch_size''': 16} training_function(lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": main()
695
1
"""simple docstring""" from typing import Union import fire import torch from tqdm import tqdm def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : str = "cpu" , lowerCAmelCase__ : Union[str, None] = None ) -> None: __a = torch.load(lowerCAmelCase__ , map_location=lowerCAmelCase__ ) for k, v in tqdm(state_dict.items() ): if not isinstance(lowerCAmelCase__ , torch.Tensor ): raise TypeError('''FP16 conversion only works on paths that are saved state dicts, like pytorch_model.bin''' ) __a = v.half() if save_path is None: # overwrite src_path __a = src_path torch.save(lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": fire.Fire(convert)
695
"""simple docstring""" from typing import Any def lowercase ( lowerCAmelCase__ : list , lowerCAmelCase__ : list , lowerCAmelCase__ : dict , lowerCAmelCase__ : dict , lowerCAmelCase__ : dict , ) -> list: _validation( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ) # Creates data structures and fill initial step __a = {} __a = {} for state in states_space: __a = observations_space[0] __a = ( initial_probabilities[state] * emission_probabilities[state][observation] ) __a = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(lowerCAmelCase__ ) ): __a = observations_space[o] __a = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function __a = '''''' __a = -1 for k_state in states_space: __a = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: __a = probability __a = k_state # Update probabilities and pointers dicts __a = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) __a = arg_max # The final observation __a = observations_space[len(lowerCAmelCase__ ) - 1] # argmax for given final observation __a = '''''' __a = -1 for k_state in states_space: __a = probabilities[(k_state, final_observation)] if probability > max_probability: __a = probability __a = k_state __a = arg_max # Process pointers backwards __a = last_state __a = [] for o in range(len(lowerCAmelCase__ ) - 1 , -1 , -1 ): result.append(lowerCAmelCase__ ) __a = pointers[previous, observations_space[o]] result.reverse() return result def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: _validate_not_empty( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ) _validate_lists(lowerCAmelCase__ , lowerCAmelCase__ ) _validate_dicts( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError('''There\'s an empty parameter''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any ) -> None: _validate_list(lowerCAmelCase__ , '''observations_space''' ) _validate_list(lowerCAmelCase__ , '''states_space''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str ) -> None: if not isinstance(_object , lowerCAmelCase__ ): __a = f'''{var_name} must be a list''' raise ValueError(lowerCAmelCase__ ) else: for x in _object: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = f'''{var_name} must be a list of strings''' raise ValueError(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: _validate_dict(lowerCAmelCase__ , '''initial_probabilities''' , lowerCAmelCase__ ) _validate_nested_dict(lowerCAmelCase__ , '''transition_probabilities''' ) _validate_nested_dict(lowerCAmelCase__ , '''emission_probabilities''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str ) -> None: _validate_dict(_object , lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object.values(): _validate_dict(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : type , lowerCAmelCase__ : bool = False ) -> None: if not isinstance(_object , lowerCAmelCase__ ): __a = f'''{var_name} must be a dict''' raise ValueError(lowerCAmelCase__ ) if not all(isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object ): __a = f'''{var_name} all keys must be strings''' raise ValueError(lowerCAmelCase__ ) if not all(isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object.values() ): __a = '''nested dictionary ''' if nested else '''''' __a = f'''{var_name} {nested_text}all values must be {value_type.__name__}''' raise ValueError(lowerCAmelCase__ ) if __name__ == "__main__": from doctest import testmod testmod()
695
1
"""simple docstring""" lowercase_ = { "joule": 1.0, "kilojoule": 1_0_0_0, "megajoule": 1_0_0_0_0_0_0, "gigajoule": 1_0_0_0_0_0_0_0_0_0, "wattsecond": 1.0, "watthour": 3_6_0_0, "kilowatthour": 3_6_0_0_0_0_0, "newtonmeter": 1.0, "calorie_nutr": 4_1_8_6.8, "kilocalorie_nutr": 4_1_8_6_8_0_0.0_0, "electronvolt": 1.602_176_634e-19, "britishthermalunit_it": 1_0_5_5.0_5_5_8_5, "footpound": 1.355818, } def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : str , lowerCAmelCase__ : float ) -> float: if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: __a = ( f'''Incorrect \'from_type\' or \'to_type\' value: {from_type!r}, {to_type!r}\n''' f'''Valid values are: {', '.join(lowerCAmelCase__ )}''' ) raise ValueError(lowerCAmelCase__ ) return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type] if __name__ == "__main__": import doctest doctest.testmod()
695
"""simple docstring""" import math def lowercase ( lowerCAmelCase__ : int ) -> bool: 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(lowerCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowercase ( lowerCAmelCase__ : float = 0.1 ) -> int: __a = 3 __a = 3 while primes / (2 * j - 1) >= ratio: for i in range(j * j + j + 1 , (j + 2) * (j + 2) , j + 1 ): primes += is_prime(lowerCAmelCase__ ) j += 2 return j if __name__ == "__main__": import doctest doctest.testmod()
695
1
"""simple docstring""" import os import unittest from transformers import LxmertTokenizer, LxmertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : List[Any] = LxmertTokenizer __UpperCAmelCase : List[Any] = LxmertTokenizerFast __UpperCAmelCase : Optional[int] = True __UpperCAmelCase : Optional[int] = True def __UpperCAmelCase ( self ): super().setUp() __a = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] __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] ) ) def __UpperCAmelCase ( self , _a ): __a = '''UNwant\u00E9d,running''' __a = '''unwanted, running''' return input_text, output_text def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file ) __a = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(_a , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [7, 4, 5, 10, 8, 9] ) def __UpperCAmelCase ( self ): if not self.test_rust_tokenizer: return __a = self.get_tokenizer() __a = self.get_rust_tokenizer() __a = '''I was born in 92000, and this is falsé.''' __a = tokenizer.tokenize(_a ) __a = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) __a = self.get_rust_tokenizer() __a = tokenizer.encode(_a ) __a = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a )
695
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = { "configuration_mctct": ["MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MCTCTConfig"], "feature_extraction_mctct": ["MCTCTFeatureExtractor"], "processing_mctct": ["MCTCTProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST", "MCTCTForCTC", "MCTCTModel", "MCTCTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig from .feature_extraction_mctct import MCTCTFeatureExtractor from .processing_mctct import MCTCTProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
695
1
"""simple docstring""" from __future__ import annotations lowercase_ = 1.6_021e-19 # units = C def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float , ) -> tuple[str, float]: if (conductivity, electron_conc, mobility).count(0 ) != 1: raise ValueError('''You cannot supply more or less than 2 values''' ) elif conductivity < 0: raise ValueError('''Conductivity cannot be negative''' ) elif electron_conc < 0: raise ValueError('''Electron concentration cannot be negative''' ) elif mobility < 0: raise ValueError('''mobility cannot be negative''' ) elif conductivity == 0: return ( "conductivity", mobility * electron_conc * ELECTRON_CHARGE, ) elif electron_conc == 0: return ( "electron_conc", conductivity / (mobility * ELECTRON_CHARGE), ) else: return ( "mobility", conductivity / (electron_conc * ELECTRON_CHARGE), ) if __name__ == "__main__": import doctest doctest.testmod()
695
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json" ), } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[Any] = 'dpr' def __init__( self , _a=30_522 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=2 , _a=0.02 , _a=1E-12 , _a=0 , _a="absolute" , _a = 0 , **_a , ): super().__init__(pad_token_id=_a , **_a ) __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = hidden_act __a = intermediate_size __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = initializer_range __a = layer_norm_eps __a = projection_dim __a = position_embedding_type
695
1
"""simple docstring""" 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 lowercase_ = ["bart.large", "bart.large.mnli", "bart.large.cnn", "bart_xsum/model.pt"] lowercase_ = {"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() lowercase_ = logging.get_logger(__name__) lowercase_ = " Hello world! cécé herlolip" lowercase_ = [ ("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 lowercase ( lowerCAmelCase__ : int ) -> Tuple: __a = [ '''encoder.version''', '''decoder.version''', '''model.encoder.version''', '''model.decoder.version''', '''_float_tensor''', ] for k in ignore_keys: state_dict.pop(lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : int ) -> int: __a = dct.pop(lowerCAmelCase__ ) __a = val def lowercase ( lowerCAmelCase__ : Tuple ) -> int: __a = torch.load(lowerCAmelCase__ , map_location='''cpu''' ) __a = torch.hub.load('''pytorch/fairseq''' , '''bart.large.cnn''' ).eval() hub_interface.model.load_state_dict(sd['''model'''] ) return hub_interface def lowercase ( lowerCAmelCase__ : int ) -> int: __a , __a = emb.weight.shape __a = nn.Linear(lowerCAmelCase__ , lowerCAmelCase__ , bias=lowerCAmelCase__ ) __a = emb.weight.data return lin_layer @torch.no_grad() def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[Any]=None ) -> Union[str, Any]: if not os.path.exists(lowerCAmelCase__ ): __a = torch.hub.load('''pytorch/fairseq''' , lowerCAmelCase__ ).eval() else: __a = load_xsum_checkpoint(lowerCAmelCase__ ) bart.model.upgrade_state_dict(bart.model.state_dict() ) if hf_checkpoint_name is None: __a = checkpoint_path.replace('''.''' , '''-''' ) __a = BartConfig.from_pretrained(lowerCAmelCase__ ) __a = bart.encode(lowerCAmelCase__ ).unsqueeze(0 ) __a = BartTokenizer.from_pretrained(lowerCAmelCase__ ).encode(lowerCAmelCase__ , return_tensors='''pt''' ).unsqueeze(0 ) if not torch.eq(lowerCAmelCase__ , lowerCAmelCase__ ).all(): raise ValueError( f'''converted tokenizer and pretrained tokenizer returned different output: {tokens} != {tokensa}''' ) if checkpoint_path == "bart.large.mnli": __a = bart.state_dict() remove_ignore_keys_(lowerCAmelCase__ ) __a = state_dict['''model.decoder.embed_tokens.weight'''] for src, dest in mnli_rename_keys: rename_key(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) __a = BartForSequenceClassification(lowerCAmelCase__ ).eval() model.load_state_dict(lowerCAmelCase__ ) __a = bart.predict('''mnli''' , lowerCAmelCase__ , return_logits=lowerCAmelCase__ ) __a = model(lowerCAmelCase__ )[0] # logits else: # no classification heads to worry about __a = bart.model.state_dict() remove_ignore_keys_(lowerCAmelCase__ ) __a = state_dict['''decoder.embed_tokens.weight'''] __a = bart.extract_features(lowerCAmelCase__ ) if hf_checkpoint_name == "facebook/bart-large": __a = BartModel(lowerCAmelCase__ ).eval() model.load_state_dict(lowerCAmelCase__ ) __a = model(lowerCAmelCase__ ).model[0] else: __a = BartForConditionalGeneration(lowerCAmelCase__ ).eval() # an existing summarization ckpt model.model.load_state_dict(lowerCAmelCase__ ) if hasattr(lowerCAmelCase__ , '''lm_head''' ): __a = make_linear_from_emb(model.model.shared ) __a = model.model(lowerCAmelCase__ )[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(lowerCAmelCase__ ).mkdir(exist_ok=lowerCAmelCase__ ) model.save_pretrained(lowerCAmelCase__ ) if __name__ == "__main__": lowercase_ = 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" ) lowercase_ = parser.parse_args() convert_bart_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, hf_checkpoint_name=args.hf_config)
695
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = StableDiffusionInpaintPipeline __UpperCAmelCase : int = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS __UpperCAmelCase : str = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __UpperCAmelCase : int = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess __UpperCAmelCase : Tuple = frozenset([] ) def __UpperCAmelCase ( self ): torch.manual_seed(0 ) __a = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=9 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=_a , ) __a = PNDMScheduler(skip_prk_steps=_a ) torch.manual_seed(0 ) __a = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) __a = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , hidden_act='''gelu''' , projection_dim=512 , ) __a = CLIPTextModel(_a ) __a = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __a = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def __UpperCAmelCase ( self , _a , _a=0 ): # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched __a = floats_tensor((1, 3, 32, 32) , rng=random.Random(_a ) ).to(_a ) __a = image.cpu().permute(0 , 2 , 3 , 1 )[0] __a = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((64, 64) ) __a = Image.fromarray(np.uinta(image + 4 ) ).convert('''RGB''' ).resize((64, 64) ) if str(_a ).startswith('''mps''' ): __a = torch.manual_seed(_a ) else: __a = torch.Generator(device=_a ).manual_seed(_a ) __a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': init_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def __UpperCAmelCase ( self ): __a = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a = self.get_dummy_components() __a = StableDiffusionInpaintPipeline(**_a ) __a = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = sd_pipe(**_a ).images __a = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __a = np.array([0.4727, 0.5735, 0.3941, 0.5446, 0.5926, 0.4394, 0.5062, 0.4654, 0.4476] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __UpperCAmelCase ( self ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self ): __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench.npy''' ) __a = '''stabilityai/stable-diffusion-2-inpainting''' __a = StableDiffusionInpaintPipeline.from_pretrained(_a , safety_checker=_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = '''Face of a yellow cat, high resolution, sitting on a park bench''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , output_type='''np''' , ) __a = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 9E-3 def __UpperCAmelCase ( self ): __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench_fp16.npy''' ) __a = '''stabilityai/stable-diffusion-2-inpainting''' __a = StableDiffusionInpaintPipeline.from_pretrained( _a , torch_dtype=torch.floataa , safety_checker=_a , ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = '''Face of a yellow cat, high resolution, sitting on a park bench''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , output_type='''np''' , ) __a = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 5E-1 def __UpperCAmelCase ( self ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __a = '''stabilityai/stable-diffusion-2-inpainting''' __a = PNDMScheduler.from_pretrained(_a , subfolder='''scheduler''' ) __a = StableDiffusionInpaintPipeline.from_pretrained( _a , safety_checker=_a , scheduler=_a , torch_dtype=torch.floataa , ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() __a = '''Face of a yellow cat, high resolution, sitting on a park bench''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , num_inference_steps=2 , output_type='''np''' , ) __a = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 10**9
695
1
"""simple docstring""" from __future__ import annotations import sys from collections import deque from typing import Generic, TypeVar lowercase_ = TypeVar("T") class __lowerCAmelCase ( Generic[T] ): '''simple docstring''' __UpperCAmelCase : deque[T] # Cache store of keys __UpperCAmelCase : set[T] # References of the keys in cache __UpperCAmelCase : int = 1_0 # Maximum capacity of cache def __init__( self , _a ): __a = deque() __a = set() if not n: __a = sys.maxsize elif n < 0: raise ValueError('''n should be an integer greater than 0.''' ) else: __a = n def __UpperCAmelCase ( self , _a ): if x not in self.key_reference: if len(self.dq_store ) == LRUCache._MAX_CAPACITY: __a = self.dq_store.pop() self.key_reference.remove(_a ) else: self.dq_store.remove(_a ) self.dq_store.appendleft(_a ) self.key_reference.add(_a ) def __UpperCAmelCase ( self ): for k in self.dq_store: print(_a ) def __repr__( self ): return f'''LRUCache({self._MAX_CAPACITY}) => {list(self.dq_store )}''' if __name__ == "__main__": import doctest doctest.testmod() lowercase_ = LRUCache(4) lru_cache.refer("A") lru_cache.refer(2) lru_cache.refer(3) lru_cache.refer("A") lru_cache.refer(4) lru_cache.refer(5) lru_cache.display() print(lru_cache) assert str(lru_cache) == "LRUCache(4) => [5, 4, 'A', 3]"
695
"""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 __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : int = 0 __UpperCAmelCase : bool = False __UpperCAmelCase : float = 3.0 class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): # 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=_a ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'''a''': 2, '''c''': 2.25} ) @require_cuda def __UpperCAmelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. __a = GradScalerKwargs(init_scale=1_024 , growth_factor=2 ) AcceleratorState._reset_state() __a = Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) __a = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2_000 ) self.assertEqual(scaler._enabled , _a ) @require_multi_gpu def __UpperCAmelCase ( self ): __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": lowercase_ = DistributedDataParallelKwargs(bucket_cap_mb=1_5, find_unused_parameters=True) lowercase_ = Accelerator(kwargs_handlers=[ddp_scaler]) lowercase_ = torch.nn.Linear(1_0_0, 2_0_0) lowercase_ = accelerator.prepare(model) # Check the values changed in kwargs lowercase_ = "" lowercase_ = model.bucket_bytes_cap // (1_0_2_4 * 1_0_2_4) if observed_bucket_cap_map != 1_5: 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)
695
1
"""simple docstring""" import argparse import logging import os import sys import numpy as np import onnxruntime import torch from bart_onnx.generation_onnx import BARTBeamSearchGenerator from bart_onnx.reduce_onnx_size import remove_dup_initializers import transformers from transformers import BartForConditionalGeneration, BartTokenizer logging.basicConfig( format="%(asctime)s | %(levelname)s | %(name)s | [%(filename)s:%(lineno)d] %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=os.environ.get("LOGLEVEL", "INFO").upper(), stream=sys.stdout, ) lowercase_ = logging.getLogger(__name__) lowercase_ = {"facebook/bart-base": BartForConditionalGeneration} lowercase_ = {"facebook/bart-base": BartTokenizer} def lowercase ( ) -> Dict: __a = argparse.ArgumentParser(description='''Export Bart model + Beam Search to ONNX graph.''' ) parser.add_argument( '''--validation_file''' , type=lowerCAmelCase__ , default=lowerCAmelCase__ , help='''A csv or a json file containing the validation data.''' ) parser.add_argument( '''--max_length''' , type=lowerCAmelCase__ , default=5 , help='''The maximum total input sequence length after tokenization.''' , ) parser.add_argument( '''--num_beams''' , type=lowerCAmelCase__ , default=lowerCAmelCase__ , help=( '''Number of beams to use for evaluation. This argument will be ''' '''passed to ``model.generate``, which is used during ``evaluate`` and ``predict``.''' ) , ) parser.add_argument( '''--model_name_or_path''' , type=lowerCAmelCase__ , help='''Path to pretrained model or model identifier from huggingface.co/models.''' , required=lowerCAmelCase__ , ) parser.add_argument( '''--config_name''' , type=lowerCAmelCase__ , default=lowerCAmelCase__ , help='''Pretrained config name or path if not the same as model_name''' , ) parser.add_argument( '''--device''' , type=lowerCAmelCase__ , default='''cpu''' , help='''Device where the model will be run''' , ) parser.add_argument('''--output_file_path''' , type=lowerCAmelCase__ , default=lowerCAmelCase__ , help='''Where to store the final ONNX file.''' ) __a = parser.parse_args() return args def lowercase ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Tuple="cpu" ) -> Union[str, Any]: __a = model_dict[model_name].from_pretrained(lowerCAmelCase__ ).to(lowerCAmelCase__ ) __a = tokenizer_dict[model_name].from_pretrained(lowerCAmelCase__ ) if model_name in ["facebook/bart-base"]: __a = 0 __a = None __a = 0 return huggingface_model, tokenizer def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[Any] ) -> List[Any]: model.eval() __a = None __a = torch.jit.script(BARTBeamSearchGenerator(lowerCAmelCase__ ) ) with torch.no_grad(): __a = '''My friends are cool but they eat too many carbs.''' __a = tokenizer([ARTICLE_TO_SUMMARIZE] , max_length=1024 , return_tensors='''pt''' ).to(model.device ) __a = model.generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , num_beams=lowerCAmelCase__ , max_length=lowerCAmelCase__ , early_stopping=lowerCAmelCase__ , decoder_start_token_id=model.config.decoder_start_token_id , ) torch.onnx.export( lowerCAmelCase__ , ( inputs['''input_ids'''], inputs['''attention_mask'''], num_beams, max_length, model.config.decoder_start_token_id, ) , lowerCAmelCase__ , opset_version=14 , input_names=['''input_ids''', '''attention_mask''', '''num_beams''', '''max_length''', '''decoder_start_token_id'''] , output_names=['''output_ids'''] , dynamic_axes={ '''input_ids''': {0: '''batch''', 1: '''seq'''}, '''output_ids''': {0: '''batch''', 1: '''seq_out'''}, } , example_outputs=lowerCAmelCase__ , ) logger.info('''Model exported to {}'''.format(lowerCAmelCase__ ) ) __a = remove_dup_initializers(os.path.abspath(lowerCAmelCase__ ) ) logger.info('''Deduplicated and optimized model written to {}'''.format(lowerCAmelCase__ ) ) __a = onnxruntime.InferenceSession(lowerCAmelCase__ ) __a = ort_sess.run( lowerCAmelCase__ , { '''input_ids''': inputs['''input_ids'''].cpu().numpy(), '''attention_mask''': inputs['''attention_mask'''].cpu().numpy(), '''num_beams''': np.array(lowerCAmelCase__ ), '''max_length''': np.array(lowerCAmelCase__ ), '''decoder_start_token_id''': np.array(model.config.decoder_start_token_id ), } , ) np.testing.assert_allclose(summary_ids.cpu().numpy() , ort_out[0] , rtol=1e-3 , atol=1e-3 ) logger.info('''Model outputs from torch and ONNX Runtime are similar.''' ) logger.info('''Success.''' ) def lowercase ( ) -> Dict: __a = parse_args() __a = 5 __a = 4 # Make one log on every process with the configuration for debugging. logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , level=logging.INFO , ) logger.setLevel(logging.INFO ) transformers.utils.logging.set_verbosity_error() __a = torch.device(args.device ) __a , __a = load_model_tokenizer(args.model_name_or_path , lowerCAmelCase__ ) if model.config.decoder_start_token_id is None: raise ValueError('''Make sure that `config.decoder_start_token_id` is correctly defined''' ) model.to(lowerCAmelCase__ ) if args.max_length: __a = args.max_length if args.num_beams: __a = args.num_beams if args.output_file_path: __a = args.output_file_path else: __a = '''BART.onnx''' logger.info('''Exporting model to ONNX''' ) export_and_validate_model(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": main()
695
"""simple docstring""" import inspect import os import sys import unittest import accelerate from accelerate.test_utils import execute_subprocess_async, require_tpu class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = inspect.getfile(accelerate.test_utils ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) __a = os.path.sep.join(inspect.getfile(self.__class__ ).split(os.path.sep )[:-1] ) @require_tpu def __UpperCAmelCase ( self ): __a = f''' {self.test_dir}/xla_spawn.py --num_cores 8 {self.test_file_path} '''.split() __a = [sys.executable] + distributed_args execute_subprocess_async(_a , env=os.environ.copy() )
695
1
"""simple docstring""" from math import atan, cos, radians, sin, tan from .haversine_distance import haversine_distance lowercase_ = 6378137.0 lowercase_ = 6356752.314245 lowercase_ = 6_3_7_8_1_3_7 def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> float: __a = (AXIS_A - AXIS_B) / AXIS_A # Parametric latitudes # https://en.wikipedia.org/wiki/Latitude#Parametric_(or_reduced)_latitude __a = atan((1 - flattening) * tan(radians(lowerCAmelCase__ ) ) ) __a = atan((1 - flattening) * tan(radians(lowerCAmelCase__ ) ) ) # Compute central angle between two points # using haversine theta. sigma = haversine_distance / equatorial radius __a = haversine_distance(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) / EQUATORIAL_RADIUS # Intermediate P and Q values __a = (b_lata + b_lata) / 2 __a = (b_lata - b_lata) / 2 # Intermediate X value # X = (sigma - sin(sigma)) * sin^2Pcos^2Q / cos^2(sigma/2) __a = (sin(lowerCAmelCase__ ) ** 2) * (cos(lowerCAmelCase__ ) ** 2) __a = cos(sigma / 2 ) ** 2 __a = (sigma - sin(lowerCAmelCase__ )) * (x_numerator / x_demonimator) # Intermediate Y value # Y = (sigma + sin(sigma)) * cos^2Psin^2Q / sin^2(sigma/2) __a = (cos(lowerCAmelCase__ ) ** 2) * (sin(lowerCAmelCase__ ) ** 2) __a = sin(sigma / 2 ) ** 2 __a = (sigma + sin(lowerCAmelCase__ )) * (y_numerator / y_denominator) return EQUATORIAL_RADIUS * (sigma - ((flattening / 2) * (x_value + y_value))) if __name__ == "__main__": import doctest doctest.testmod()
695
"""simple docstring""" import os import unittest from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, BertTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = BertTokenizer __UpperCAmelCase : Optional[Any] = BertTokenizerFast __UpperCAmelCase : str = True __UpperCAmelCase : Tuple = True __UpperCAmelCase : Any = filter_non_english def __UpperCAmelCase ( self ): super().setUp() __a = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] __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] ) ) def __UpperCAmelCase ( self , _a ): __a = '''UNwant\u00E9d,running''' __a = '''unwanted, running''' return input_text, output_text def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file ) __a = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(_a , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [9, 6, 7, 12, 10, 11] ) def __UpperCAmelCase ( self ): if not self.test_rust_tokenizer: return __a = self.get_tokenizer() __a = self.get_rust_tokenizer() __a = '''UNwant\u00E9d,running''' __a = tokenizer.tokenize(_a ) __a = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) __a = self.get_rust_tokenizer() __a = tokenizer.encode(_a ) __a = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a ) # With lower casing __a = self.get_tokenizer(do_lower_case=_a ) __a = self.get_rust_tokenizer(do_lower_case=_a ) __a = '''UNwant\u00E9d,running''' __a = tokenizer.tokenize(_a ) __a = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) __a = self.get_rust_tokenizer() __a = tokenizer.encode(_a ) __a = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('''ah\u535A\u63A8zz''' ) , ['''ah''', '''\u535A''', '''\u63A8''', '''zz'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''hello''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hällo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''h\u00E9llo'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HäLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HaLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , never_split=['''[UNK]'''] ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? [UNK]''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''', '''[UNK]'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer() __a = '''a\n\'ll !!to?\'d of, can\'t.''' __a = ['''a''', '''\'''', '''ll''', '''!''', '''!''', '''to''', '''?''', '''\'''', '''d''', '''of''', ''',''', '''can''', '''\'''', '''t''', '''.'''] self.assertListEqual(tokenizer.tokenize(_a ) , _a ) def __UpperCAmelCase ( self ): __a = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing'''] __a = {} for i, token in enumerate(_a ): __a = i __a = WordpieceTokenizer(vocab=_a , unk_token='''[UNK]''' ) self.assertListEqual(tokenizer.tokenize('''''' ) , [] ) self.assertListEqual(tokenizer.tokenize('''unwanted running''' ) , ['''un''', '''##want''', '''##ed''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.tokenize('''unwantedX running''' ) , ['''[UNK]''', '''runn''', '''##ing'''] ) def __UpperCAmelCase ( self ): self.assertTrue(_is_whitespace(''' ''' ) ) self.assertTrue(_is_whitespace('''\t''' ) ) self.assertTrue(_is_whitespace('''\r''' ) ) self.assertTrue(_is_whitespace('''\n''' ) ) self.assertTrue(_is_whitespace('''\u00A0''' ) ) self.assertFalse(_is_whitespace('''A''' ) ) self.assertFalse(_is_whitespace('''-''' ) ) def __UpperCAmelCase ( self ): self.assertTrue(_is_control('''\u0005''' ) ) self.assertFalse(_is_control('''A''' ) ) self.assertFalse(_is_control(''' ''' ) ) self.assertFalse(_is_control('''\t''' ) ) self.assertFalse(_is_control('''\r''' ) ) def __UpperCAmelCase ( self ): self.assertTrue(_is_punctuation('''-''' ) ) self.assertTrue(_is_punctuation('''$''' ) ) self.assertTrue(_is_punctuation('''`''' ) ) self.assertTrue(_is_punctuation('''.''' ) ) self.assertFalse(_is_punctuation('''A''' ) ) self.assertFalse(_is_punctuation(''' ''' ) ) def __UpperCAmelCase ( self ): __a = self.get_tokenizer() __a = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(_a ) for t in ['''Test''', '''\xad''', '''test''']] , [['''[UNK]'''], [], ['''[UNK]''']] ) self.assertListEqual( [rust_tokenizer.tokenize(_a ) for t in ['''Test''', '''\xad''', '''test''']] , [['''[UNK]'''], [], ['''[UNK]''']] ) @slow def __UpperCAmelCase ( self ): __a = self.tokenizer_class.from_pretrained('''bert-base-uncased''' ) __a = tokenizer.encode('''sequence builders''' , add_special_tokens=_a ) __a = tokenizer.encode('''multi-sequence build''' , add_special_tokens=_a ) __a = tokenizer.build_inputs_with_special_tokens(_a ) __a = tokenizer.build_inputs_with_special_tokens(_a , _a ) assert encoded_sentence == [101] + text + [102] assert encoded_pair == [101] + text + [102] + text_a + [102] def __UpperCAmelCase ( self ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = f'''A, naïve {tokenizer_r.mask_token} AllenNLP sentence.''' __a = tokenizer_r.encode_plus( _a , return_attention_mask=_a , return_token_type_ids=_a , return_offsets_mapping=_a , add_special_tokens=_a , ) __a = tokenizer_r.do_lower_case if hasattr(_a , '''do_lower_case''' ) else False __a = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), '''A'''), ((1, 2), ''','''), ((3, 5), '''na'''), ((5, 6), '''##ï'''), ((6, 8), '''##ve'''), ((9, 15), tokenizer_r.mask_token), ((16, 21), '''Allen'''), ((21, 23), '''##NL'''), ((23, 24), '''##P'''), ((25, 33), '''sentence'''), ((33, 34), '''.'''), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), '''a'''), ((1, 2), ''','''), ((3, 8), '''naive'''), ((9, 15), tokenizer_r.mask_token), ((16, 21), '''allen'''), ((21, 23), '''##nl'''), ((23, 24), '''##p'''), ((25, 33), '''sentence'''), ((33, 34), '''.'''), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens['''input_ids'''] ) ) self.assertEqual([e[0] for e in expected_results] , tokens['''offset_mapping'''] ) def __UpperCAmelCase ( self ): __a = ['''的''', '''人''', '''有'''] __a = ''''''.join(_a ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __a = True __a = self.tokenizer_class.from_pretrained(_a , **_a ) __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = tokenizer_p.encode(_a , add_special_tokens=_a ) __a = tokenizer_r.encode(_a , add_special_tokens=_a ) __a = tokenizer_r.convert_ids_to_tokens(_a ) __a = tokenizer_p.convert_ids_to_tokens(_a ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(_a , _a ) self.assertListEqual(_a , _a ) __a = False __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = self.tokenizer_class.from_pretrained(_a , **_a ) __a = tokenizer_r.encode(_a , add_special_tokens=_a ) __a = tokenizer_p.encode(_a , add_special_tokens=_a ) __a = tokenizer_r.convert_ids_to_tokens(_a ) __a = tokenizer_p.convert_ids_to_tokens(_a ) # it is expected that only the first Chinese character is not preceded by "##". __a = [ f'''##{token}''' if idx != 0 else token for idx, token in enumerate(_a ) ] self.assertListEqual(_a , _a ) self.assertListEqual(_a , _a )
695
1
"""simple docstring""" import collections import json import math import os import re import time from fnmatch import fnmatch from typing import Dict import requests from slack_sdk import WebClient lowercase_ = WebClient(token=os.environ["CI_SLACK_BOT_TOKEN"]) def lowercase ( lowerCAmelCase__ : Optional[int] ) -> List[str]: __a = test_results.split(''' ''' ) __a = 0 __a = 0 # When the output is short enough, the output is surrounded by = signs: "== OUTPUT ==" # When it is too long, those signs are not present. __a = expressions[-2] if '''=''' in expressions[-1] else expressions[-1] for i, expression in enumerate(lowerCAmelCase__ ): if "failed" in expression: failed += int(expressions[i - 1] ) if "passed" in expression: success += int(expressions[i - 1] ) return failed, success, time_spent def lowercase ( lowerCAmelCase__ : int ) -> str: __a = {} __a = None __a = False for line in failures_short_lines.split('''\n''' ): if re.search(r'''_ \[doctest\]''' , lowerCAmelCase__ ): __a = True __a = line.split(''' ''' )[2] elif in_error and not line.split(''' ''' )[0].isdigit(): __a = line __a = False return failures class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a ): __a = title __a = doc_test_results['''time_spent'''].split(''',''' )[0] __a = doc_test_results['''success'''] __a = doc_test_results['''failures'''] __a = self.n_success + self.n_failures # Failures and success of the modeling tests __a = doc_test_results @property def __UpperCAmelCase ( self ): __a = [self._time_spent] __a = 0 for time in time_spent: __a = time.split(''':''' ) # Time can be formatted as xx:xx:xx, as .xx, or as x.xx if the time spent was less than a minute. if len(_a ) == 1: __a = [0, 0, time_parts[0]] __a , __a , __a = int(time_parts[0] ), int(time_parts[1] ), float(time_parts[2] ) total_secs += hours * 3_600 + minutes * 60 + seconds __a , __a , __a = total_secs // 3_600, (total_secs % 3_600) // 60, total_secs % 60 return f'''{int(_a )}h{int(_a )}m{int(_a )}s''' @property def __UpperCAmelCase ( self ): return {"type": "header", "text": {"type": "plain_text", "text": self.title}} @property def __UpperCAmelCase ( self ): return { "type": "section", "text": { "type": "plain_text", "text": f'''🌞 There were no failures: all {self.n_tests} tests passed. The suite ran in {self.time}.''', "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": f'''https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}''', }, } @property def __UpperCAmelCase ( self ): return { "type": "section", "text": { "type": "plain_text", "text": ( f'''There were {self.n_failures} failures, out of {self.n_tests} tests.\nThe suite ran in''' f''' {self.time}.''' ), "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": f'''https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}''', }, } @property def __UpperCAmelCase ( self ): __a = 40 __a = {k: v['''failed'''] for k, v in doc_test_results.items() if isinstance(_a , _a )} __a = '''''' for category, failures in category_failures.items(): if len(_a ) == 0: continue if report != "": report += "\n\n" report += f'''*{category} failures*:'''.ljust(line_length // 2 ).rjust(line_length // 2 ) + "\n" report += "`" report += "`\n`".join(_a ) report += "`" return { "type": "section", "text": { "type": "mrkdwn", "text": f'''The following examples had failures:\n\n\n{report}\n''', }, } @property def __UpperCAmelCase ( self ): __a = [self.header] if self.n_failures > 0: blocks.append(self.failures ) if self.n_failures > 0: blocks.extend([self.category_failures] ) if self.n_failures == 0: blocks.append(self.no_failures ) return json.dumps(_a ) @staticmethod def __UpperCAmelCase ( ): __a = [ { '''type''': '''section''', '''text''': { '''type''': '''plain_text''', '''text''': '''There was an issue running the tests.''', }, '''accessory''': { '''type''': '''button''', '''text''': {'''type''': '''plain_text''', '''text''': '''Check Action results''', '''emoji''': True}, '''url''': f'''https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}''', }, } ] print('''Sending the following payload''' ) print(json.dumps({'''blocks''': json.loads(_a )} ) ) client.chat_postMessage( channel=os.environ['''CI_SLACK_CHANNEL_ID_DAILY'''] , text='''There was an issue running the tests.''' , blocks=_a , ) def __UpperCAmelCase ( self ): print('''Sending the following payload''' ) print(json.dumps({'''blocks''': json.loads(self.payload )} ) ) __a = f'''{self.n_failures} failures out of {self.n_tests} tests,''' if self.n_failures else '''All tests passed.''' __a = client.chat_postMessage( channel=os.environ['''CI_SLACK_CHANNEL_ID_DAILY'''] , blocks=self.payload , text=_a , ) def __UpperCAmelCase ( self , _a , _a , _a , _a ): __a = '''''' for key, value in failures.items(): __a = value[:200] + ''' [Truncated]''' if len(_a ) > 250 else value failures_text += f'''*{key}*\n_{value}_\n\n''' __a = job_name __a = {'''type''': '''section''', '''text''': {'''type''': '''mrkdwn''', '''text''': text}} if job_link is not None: __a = { '''type''': '''button''', '''text''': {'''type''': '''plain_text''', '''text''': '''GitHub Action job''', '''emoji''': True}, '''url''': job_link, } return [ {"type": "header", "text": {"type": "plain_text", "text": title.upper(), "emoji": True}}, content, {"type": "section", "text": {"type": "mrkdwn", "text": failures_text}}, ] def __UpperCAmelCase ( self ): if self.thread_ts is None: raise ValueError('''Can only post reply if a post has been made.''' ) __a = self.doc_test_results.pop('''job_link''' ) self.doc_test_results.pop('''failures''' ) self.doc_test_results.pop('''success''' ) self.doc_test_results.pop('''time_spent''' ) __a = sorted(self.doc_test_results.items() , key=lambda _a : t[0] ) for job, job_result in sorted_dict: if len(job_result['''failures'''] ): __a = f'''*Num failures* :{len(job_result['failed'] )} \n''' __a = job_result['''failures'''] __a = self.get_reply_blocks(_a , _a , _a , text=_a ) print('''Sending the following reply''' ) print(json.dumps({'''blocks''': blocks} ) ) client.chat_postMessage( channel=os.environ['''CI_SLACK_CHANNEL_ID_DAILY'''] , text=f'''Results for {job}''' , blocks=_a , thread_ts=self.thread_ts['''ts'''] , ) time.sleep(1 ) def lowercase ( ) -> Any: __a = os.environ['''GITHUB_RUN_ID'''] __a = f'''https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100''' __a = requests.get(lowerCAmelCase__ ).json() __a = {} try: jobs.update({job['''name''']: job['''html_url'''] for job in result['''jobs''']} ) __a = math.ceil((result['''total_count'''] - 100) / 100 ) for i in range(lowerCAmelCase__ ): __a = requests.get(url + f'''&page={i + 2}''' ).json() jobs.update({job['''name''']: job['''html_url'''] for job in result['''jobs''']} ) return jobs except Exception as e: print('''Unknown error, could not fetch links.''' , lowerCAmelCase__ ) return {} def lowercase ( lowerCAmelCase__ : str ) -> Dict: __a = {} if os.path.exists(lowerCAmelCase__ ): __a = os.listdir(lowerCAmelCase__ ) for file in files: try: with open(os.path.join(lowerCAmelCase__ , lowerCAmelCase__ ) , encoding='''utf-8''' ) as f: __a = f.read() except UnicodeDecodeError as e: raise ValueError(f'''Could not open {os.path.join(lowerCAmelCase__ , lowerCAmelCase__ )}.''' ) from e return _artifact def lowercase ( ) -> Dict: class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a ): __a = name __a = [] def __str__( self ): return self.name def __UpperCAmelCase ( self , _a ): self.paths.append({'''name''': self.name, '''path''': path} ) __a = {} __a = filter(os.path.isdir , os.listdir() ) for directory in directories: __a = directory if artifact_name not in _available_artifacts: __a = Artifact(lowerCAmelCase__ ) _available_artifacts[artifact_name].add_path(lowerCAmelCase__ ) return _available_artifacts if __name__ == "__main__": lowercase_ = get_job_links() lowercase_ = retrieve_available_artifacts() lowercase_ = collections.OrderedDict( [ ("*.py", "API Examples"), ("*.md", "MD Examples"), ] ) # This dict will contain all the information relative to each doc test category: # - failed: list of failed tests # - failures: dict in the format 'test': 'error_message' lowercase_ = { v: { "failed": [], "failures": {}, } for v in docs.values() } # Link to the GitHub Action job lowercase_ = github_actions_job_links.get("run_doctests") lowercase_ = available_artifacts["doc_tests_gpu_test_reports"].paths[0] lowercase_ = retrieve_artifact(artifact_path["name"]) if "stats" in artifact: lowercase_ , lowercase_ , lowercase_ = handle_test_results(artifact["stats"]) lowercase_ = failed lowercase_ = success lowercase_ = time_spent[1:-1] + ", " lowercase_ = extract_first_line_failure(artifact["failures_short"]) for line in artifact["summary_short"].split("\n"): if re.search("FAILED", line): lowercase_ = line.replace("FAILED ", "") lowercase_ = line.split()[0].replace("\n", "") if "::" in line: lowercase_ , lowercase_ = line.split("::") else: lowercase_ , lowercase_ = line, line for file_regex in docs.keys(): if fnmatch(file_path, file_regex): lowercase_ = docs[file_regex] doc_test_results[category]["failed"].append(test) lowercase_ = all_failures[test] if test in all_failures else "N/A" lowercase_ = failure break lowercase_ = Message("🤗 Results of the doc tests.", doc_test_results) message.post() message.post_reply()
695
"""simple docstring""" from __future__ import annotations def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> float: if days_between_payments <= 0: raise ValueError('''days_between_payments must be > 0''' ) if daily_interest_rate < 0: raise ValueError('''daily_interest_rate must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return principal * daily_interest_rate * days_between_payments def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float , ) -> float: if number_of_compounding_periods <= 0: raise ValueError('''number_of_compounding_periods must be > 0''' ) if nominal_annual_interest_rate_percentage < 0: raise ValueError('''nominal_annual_interest_rate_percentage must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return principal * ( (1 + nominal_annual_interest_rate_percentage) ** number_of_compounding_periods - 1 ) def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float , ) -> float: if number_of_years <= 0: raise ValueError('''number_of_years must be > 0''' ) if nominal_annual_percentage_rate < 0: raise ValueError('''nominal_annual_percentage_rate must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return compound_interest( lowerCAmelCase__ , nominal_annual_percentage_rate / 365 , number_of_years * 365 ) if __name__ == "__main__": import doctest doctest.testmod()
695
1
"""simple docstring""" from typing import Optional, Tuple, Union import flax import flax.linen as nn import jax import jax.numpy as jnp from flax.core.frozen_dict import FrozenDict from ..configuration_utils import ConfigMixin, flax_register_to_config from ..utils import BaseOutput from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps from .modeling_flax_utils import FlaxModelMixin from .unet_ad_blocks_flax import ( FlaxCrossAttnDownBlockaD, FlaxDownBlockaD, FlaxUNetMidBlockaDCrossAttn, ) @flax.struct.dataclass class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : jnp.ndarray __UpperCAmelCase : jnp.ndarray class __lowerCAmelCase ( nn.Module ): '''simple docstring''' __UpperCAmelCase : int __UpperCAmelCase : Tuple[int] = (1_6, 3_2, 9_6, 2_5_6) __UpperCAmelCase : jnp.dtype = jnp.floataa def __UpperCAmelCase ( self ): __a = nn.Conv( self.block_out_channels[0] , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) __a = [] for i in range(len(self.block_out_channels ) - 1 ): __a = self.block_out_channels[i] __a = self.block_out_channels[i + 1] __a = nn.Conv( _a , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) blocks.append(_a ) __a = nn.Conv( _a , kernel_size=(3, 3) , strides=(2, 2) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) blocks.append(_a ) __a = blocks __a = nn.Conv( self.conditioning_embedding_channels , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) def __call__( self , _a ): __a = self.conv_in(_a ) __a = nn.silu(_a ) for block in self.blocks: __a = block(_a ) __a = nn.silu(_a ) __a = self.conv_out(_a ) return embedding @flax_register_to_config class __lowerCAmelCase ( nn.Module , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : int = 3_2 __UpperCAmelCase : int = 4 __UpperCAmelCase : Tuple[str] = ( "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "DownBlock2D", ) __UpperCAmelCase : Union[bool, Tuple[bool]] = False __UpperCAmelCase : Tuple[int] = (3_2_0, 6_4_0, 1_2_8_0, 1_2_8_0) __UpperCAmelCase : int = 2 __UpperCAmelCase : Union[int, Tuple[int]] = 8 __UpperCAmelCase : Optional[Union[int, Tuple[int]]] = None __UpperCAmelCase : int = 1_2_8_0 __UpperCAmelCase : float = 0.0 __UpperCAmelCase : bool = False __UpperCAmelCase : jnp.dtype = jnp.floataa __UpperCAmelCase : bool = True __UpperCAmelCase : int = 0 __UpperCAmelCase : str = "rgb" __UpperCAmelCase : Tuple[int] = (1_6, 3_2, 9_6, 2_5_6) def __UpperCAmelCase ( self , _a ): # init input tensors __a = (1, self.in_channels, self.sample_size, self.sample_size) __a = jnp.zeros(_a , dtype=jnp.floataa ) __a = jnp.ones((1,) , dtype=jnp.intaa ) __a = jnp.zeros((1, 1, self.cross_attention_dim) , dtype=jnp.floataa ) __a = (1, 3, self.sample_size * 8, self.sample_size * 8) __a = jnp.zeros(_a , dtype=jnp.floataa ) __a , __a = jax.random.split(_a ) __a = {'''params''': params_rng, '''dropout''': dropout_rng} return self.init(_a , _a , _a , _a , _a )["params"] def __UpperCAmelCase ( self ): __a = self.block_out_channels __a = block_out_channels[0] * 4 # If `num_attention_heads` is not defined (which is the case for most models) # it will default to `attention_head_dim`. This looks weird upon first reading it and it is. # The reason for this behavior is to correct for incorrectly named variables that were introduced # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131 # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking # which is why we correct for the naming here. __a = self.num_attention_heads or self.attention_head_dim # input __a = nn.Conv( block_out_channels[0] , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) # time __a = FlaxTimesteps( block_out_channels[0] , flip_sin_to_cos=self.flip_sin_to_cos , freq_shift=self.config.freq_shift ) __a = FlaxTimestepEmbedding(_a , dtype=self.dtype ) __a = FlaxControlNetConditioningEmbedding( conditioning_embedding_channels=block_out_channels[0] , block_out_channels=self.conditioning_embedding_out_channels , ) __a = self.only_cross_attention if isinstance(_a , _a ): __a = (only_cross_attention,) * len(self.down_block_types ) if isinstance(_a , _a ): __a = (num_attention_heads,) * len(self.down_block_types ) # down __a = [] __a = [] __a = block_out_channels[0] __a = nn.Conv( _a , kernel_size=(1, 1) , padding='''VALID''' , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) controlnet_down_blocks.append(_a ) for i, down_block_type in enumerate(self.down_block_types ): __a = output_channel __a = block_out_channels[i] __a = i == len(_a ) - 1 if down_block_type == "CrossAttnDownBlock2D": __a = FlaxCrossAttnDownBlockaD( in_channels=_a , out_channels=_a , dropout=self.dropout , num_layers=self.layers_per_block , num_attention_heads=num_attention_heads[i] , add_downsample=not is_final_block , use_linear_projection=self.use_linear_projection , only_cross_attention=only_cross_attention[i] , dtype=self.dtype , ) else: __a = FlaxDownBlockaD( in_channels=_a , out_channels=_a , dropout=self.dropout , num_layers=self.layers_per_block , add_downsample=not is_final_block , dtype=self.dtype , ) down_blocks.append(_a ) for _ in range(self.layers_per_block ): __a = nn.Conv( _a , kernel_size=(1, 1) , padding='''VALID''' , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) controlnet_down_blocks.append(_a ) if not is_final_block: __a = nn.Conv( _a , kernel_size=(1, 1) , padding='''VALID''' , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) controlnet_down_blocks.append(_a ) __a = down_blocks __a = controlnet_down_blocks # mid __a = block_out_channels[-1] __a = FlaxUNetMidBlockaDCrossAttn( in_channels=_a , dropout=self.dropout , num_attention_heads=num_attention_heads[-1] , use_linear_projection=self.use_linear_projection , dtype=self.dtype , ) __a = nn.Conv( _a , kernel_size=(1, 1) , padding='''VALID''' , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) def __call__( self , _a , _a , _a , _a , _a = 1.0 , _a = True , _a = False , ): __a = self.controlnet_conditioning_channel_order if channel_order == "bgr": __a = jnp.flip(_a , axis=1 ) # 1. time if not isinstance(_a , jnp.ndarray ): __a = jnp.array([timesteps] , dtype=jnp.intaa ) elif isinstance(_a , jnp.ndarray ) and len(timesteps.shape ) == 0: __a = timesteps.astype(dtype=jnp.floataa ) __a = jnp.expand_dims(_a , 0 ) __a = self.time_proj(_a ) __a = self.time_embedding(_a ) # 2. pre-process __a = jnp.transpose(_a , (0, 2, 3, 1) ) __a = self.conv_in(_a ) __a = jnp.transpose(_a , (0, 2, 3, 1) ) __a = self.controlnet_cond_embedding(_a ) sample += controlnet_cond # 3. down __a = (sample,) for down_block in self.down_blocks: if isinstance(_a , _a ): __a , __a = down_block(_a , _a , _a , deterministic=not train ) else: __a , __a = down_block(_a , _a , deterministic=not train ) down_block_res_samples += res_samples # 4. mid __a = self.mid_block(_a , _a , _a , deterministic=not train ) # 5. contronet blocks __a = () for down_block_res_sample, controlnet_block in zip(_a , self.controlnet_down_blocks ): __a = controlnet_block(_a ) controlnet_down_block_res_samples += (down_block_res_sample,) __a = controlnet_down_block_res_samples __a = self.controlnet_mid_block(_a ) # 6. scaling __a = [sample * conditioning_scale for sample in down_block_res_samples] mid_block_res_sample *= conditioning_scale if not return_dict: return (down_block_res_samples, mid_block_res_sample) return FlaxControlNetOutput( down_block_res_samples=_a , mid_block_res_sample=_a )
695
"""simple docstring""" def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any=False ) -> Any: if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = len(set_a.intersection(lowerCAmelCase__ ) ) if alternative_union: __a = len(lowerCAmelCase__ ) + len(lowerCAmelCase__ ) else: __a = len(set_a.union(lowerCAmelCase__ ) ) return intersection / union if isinstance(lowerCAmelCase__ , (list, tuple) ) and isinstance(lowerCAmelCase__ , (list, tuple) ): __a = [element for element in set_a if element in set_b] if alternative_union: __a = len(lowerCAmelCase__ ) + len(lowerCAmelCase__ ) return len(lowerCAmelCase__ ) / union else: __a = set_a + [element for element in set_b if element not in set_a] return len(lowerCAmelCase__ ) / len(lowerCAmelCase__ ) return len(lowerCAmelCase__ ) / len(lowerCAmelCase__ ) return None if __name__ == "__main__": lowercase_ = {"a", "b", "c", "d", "e"} lowercase_ = {"c", "d", "e", "f", "h", "i"} print(jaccard_similarity(set_a, set_b))
695
1
"""simple docstring""" import math import torch from torch import nn from ..configuration_utils import ConfigMixin, register_to_config from .attention_processor import Attention from .embeddings import get_timestep_embedding from .modeling_utils import ModelMixin class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' @register_to_config def __init__( self , _a = 128 , _a = 256 , _a = 2000.0 , _a = 768 , _a = 12 , _a = 12 , _a = 64 , _a = 2_048 , _a = 0.1 , ): super().__init__() __a = nn.Sequential( nn.Linear(_a , d_model * 4 , bias=_a ) , nn.SiLU() , nn.Linear(d_model * 4 , d_model * 4 , bias=_a ) , nn.SiLU() , ) __a = nn.Embedding(_a , _a ) __a = False __a = nn.Linear(_a , _a , bias=_a ) __a = nn.Dropout(p=_a ) __a = nn.ModuleList() for lyr_num in range(_a ): # FiLM conditional T5 decoder __a = DecoderLayer(d_model=_a , d_kv=_a , num_heads=_a , d_ff=_a , dropout_rate=_a ) self.decoders.append(_a ) __a = TaLayerNorm(_a ) __a = nn.Dropout(p=_a ) __a = nn.Linear(_a , _a , bias=_a ) def __UpperCAmelCase ( self , _a , _a ): __a = torch.mul(query_input.unsqueeze(-1 ) , key_input.unsqueeze(-2 ) ) return mask.unsqueeze(-3 ) def __UpperCAmelCase ( self , _a , _a , _a ): __a , __a , __a = decoder_input_tokens.shape assert decoder_noise_time.shape == (batch,) # decoder_noise_time is in [0, 1), so rescale to expected timing range. __a = get_timestep_embedding( decoder_noise_time * self.config.max_decoder_noise_time , embedding_dim=self.config.d_model , max_period=self.config.max_decoder_noise_time , ).to(dtype=self.dtype ) __a = self.conditioning_emb(_a ).unsqueeze(1 ) assert conditioning_emb.shape == (batch, 1, self.config.d_model * 4) __a = decoder_input_tokens.shape[1] # If we want to use relative positions for audio context, we can just offset # this sequence by the length of encodings_and_masks. __a = torch.broadcast_to( torch.arange(_a , device=decoder_input_tokens.device ) , (batch, seq_length) , ) __a = self.position_encoding(_a ) __a = self.continuous_inputs_projection(_a ) inputs += position_encodings __a = self.dropout(_a ) # decoder: No padding present. __a = torch.ones( decoder_input_tokens.shape[:2] , device=decoder_input_tokens.device , dtype=inputs.dtype ) # Translate encoding masks to encoder-decoder masks. __a = [(x, self.encoder_decoder_mask(_a , _a )) for x, y in encodings_and_masks] # cross attend style: concat encodings __a = torch.cat([x[0] for x in encodings_and_encdec_masks] , dim=1 ) __a = torch.cat([x[1] for x in encodings_and_encdec_masks] , dim=-1 ) for lyr in self.decoders: __a = lyr( _a , conditioning_emb=_a , encoder_hidden_states=_a , encoder_attention_mask=_a , )[0] __a = self.decoder_norm(_a ) __a = self.post_dropout(_a ) __a = self.spec_out(_a ) return spec_out class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a , _a , _a , _a=1E-6 ): super().__init__() __a = nn.ModuleList() # cond self attention: layer 0 self.layer.append( TaLayerSelfAttentionCond(d_model=_a , d_kv=_a , num_heads=_a , dropout_rate=_a ) ) # cross attention: layer 1 self.layer.append( TaLayerCrossAttention( d_model=_a , d_kv=_a , num_heads=_a , dropout_rate=_a , layer_norm_epsilon=_a , ) ) # Film Cond MLP + dropout: last layer self.layer.append( TaLayerFFCond(d_model=_a , d_ff=_a , dropout_rate=_a , layer_norm_epsilon=_a ) ) def __UpperCAmelCase ( self , _a , _a=None , _a=None , _a=None , _a=None , _a=None , ): __a = self.layer[0]( _a , conditioning_emb=_a , attention_mask=_a , ) if encoder_hidden_states is not None: __a = torch.where(encoder_attention_mask > 0 , 0 , -1E10 ).to( encoder_hidden_states.dtype ) __a = self.layer[1]( _a , key_value_states=_a , attention_mask=_a , ) # Apply Film Conditional Feed Forward layer __a = self.layer[-1](_a , _a ) return (hidden_states,) class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a , _a ): super().__init__() __a = TaLayerNorm(_a ) __a = TaFiLMLayer(in_features=d_model * 4 , out_features=_a ) __a = Attention(query_dim=_a , heads=_a , dim_head=_a , out_bias=_a , scale_qk=_a ) __a = nn.Dropout(_a ) def __UpperCAmelCase ( self , _a , _a=None , _a=None , ): # pre_self_attention_layer_norm __a = self.layer_norm(_a ) if conditioning_emb is not None: __a = self.FiLMLayer(_a , _a ) # Self-attention block __a = self.attention(_a ) __a = hidden_states + self.dropout(_a ) return hidden_states class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a , _a , _a ): super().__init__() __a = Attention(query_dim=_a , heads=_a , dim_head=_a , out_bias=_a , scale_qk=_a ) __a = TaLayerNorm(_a , eps=_a ) __a = nn.Dropout(_a ) def __UpperCAmelCase ( self , _a , _a=None , _a=None , ): __a = self.layer_norm(_a ) __a = self.attention( _a , encoder_hidden_states=_a , attention_mask=attention_mask.squeeze(1 ) , ) __a = hidden_states + self.dropout(_a ) return layer_output class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a , _a ): super().__init__() __a = TaDenseGatedActDense(d_model=_a , d_ff=_a , dropout_rate=_a ) __a = TaFiLMLayer(in_features=d_model * 4 , out_features=_a ) __a = TaLayerNorm(_a , eps=_a ) __a = nn.Dropout(_a ) def __UpperCAmelCase ( self , _a , _a=None ): __a = self.layer_norm(_a ) if conditioning_emb is not None: __a = self.film(_a , _a ) __a = self.DenseReluDense(_a ) __a = hidden_states + self.dropout(_a ) return hidden_states class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a ): super().__init__() __a = nn.Linear(_a , _a , bias=_a ) __a = nn.Linear(_a , _a , bias=_a ) __a = nn.Linear(_a , _a , bias=_a ) __a = nn.Dropout(_a ) __a = NewGELUActivation() def __UpperCAmelCase ( self , _a ): __a = self.act(self.wi_a(_a ) ) __a = self.wi_a(_a ) __a = hidden_gelu * hidden_linear __a = self.dropout(_a ) __a = self.wo(_a ) return hidden_states class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a=1E-6 ): super().__init__() __a = nn.Parameter(torch.ones(_a ) ) __a = eps def __UpperCAmelCase ( self , _a ): # T5 uses a layer_norm which only scales and doesn't shift, which is also known as Root Mean # Square Layer Normalization https://arxiv.org/abs/1910.07467 thus variance is calculated # w/o mean and there is no bias. Additionally we want to make sure that the accumulation for # half-precision inputs is done in fp32 __a = hidden_states.to(torch.floataa ).pow(2 ).mean(-1 , keepdim=_a ) __a = hidden_states * torch.rsqrt(variance + self.variance_epsilon ) # convert into half-precision if necessary if self.weight.dtype in [torch.floataa, torch.bfloataa]: __a = hidden_states.to(self.weight.dtype ) return self.weight * hidden_states class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __UpperCAmelCase ( self , _a ): return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi ) * (input + 0.04_4715 * torch.pow(_a , 3.0 )) )) class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a ): super().__init__() __a = nn.Linear(_a , out_features * 2 , bias=_a ) def __UpperCAmelCase ( self , _a , _a ): __a = self.scale_bias(_a ) __a , __a = torch.chunk(_a , 2 , -1 ) __a = x * (1 + scale) + shift return x
695
"""simple docstring""" from __future__ import annotations import requests def lowercase ( lowerCAmelCase__ : str ) -> dict: __a = f'''https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty''' return requests.get(lowerCAmelCase__ ).json() def lowercase ( lowerCAmelCase__ : int = 10 ) -> list[dict]: __a = '''https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty''' __a = requests.get(lowerCAmelCase__ ).json()[:max_stories] return [get_hackernews_story(lowerCAmelCase__ ) for story_id in story_ids] def lowercase ( lowerCAmelCase__ : int = 10 ) -> str: __a = hackernews_top_stories(lowerCAmelCase__ ) return "\n".join('''* [{title}]({url})'''.format(**lowerCAmelCase__ ) for story in stories ) if __name__ == "__main__": print(hackernews_top_stories_as_markdown())
695
1
"""simple docstring""" import argparse import torch from transformers import FunnelBaseModel, FunnelConfig, FunnelModel, load_tf_weights_in_funnel from transformers.utils import logging logging.set_verbosity_info() def lowercase ( lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Dict ) -> Tuple: # Initialise PyTorch model __a = FunnelConfig.from_json_file(lowerCAmelCase__ ) print(f'''Building PyTorch model from configuration: {config}''' ) __a = FunnelBaseModel(lowerCAmelCase__ ) if base_model else FunnelModel(lowerCAmelCase__ ) # Load weights from tf checkpoint load_tf_weights_in_funnel(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Save pytorch-model print(f'''Save PyTorch model to {pytorch_dump_path}''' ) torch.save(model.state_dict() , lowerCAmelCase__ ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--config_file", default=None, type=str, required=True, help="The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.", ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument( "--base_model", action="store_true", help="Whether you want just the base model (no decoder) or not." ) lowercase_ = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path, args.base_model )
695
"""simple docstring""" import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES from ...utils import logging from ..auto import CONFIG_MAPPING lowercase_ = logging.get_logger(__name__) lowercase_ = { "salesforce/blip2-opt-2.7b": "https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json", } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = 'blip_2_vision_model' def __init__( self , _a=1_408 , _a=6_144 , _a=39 , _a=16 , _a=224 , _a=14 , _a="gelu" , _a=0.0_0001 , _a=0.0 , _a=1E-10 , _a=True , **_a , ): super().__init__(**_a ) __a = hidden_size __a = intermediate_size __a = num_hidden_layers __a = num_attention_heads __a = patch_size __a = image_size __a = initializer_range __a = attention_dropout __a = layer_norm_eps __a = hidden_act __a = qkv_bias @classmethod def __UpperCAmelCase ( cls , _a , **_a ): cls._set_token_in_kwargs(_a ) __a , __a = cls.get_config_dict(_a , **_a ) # get the vision config dict if we are loading from Blip2Config if config_dict.get('''model_type''' ) == "blip-2": __a = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(_a , **_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : str = 'blip_2_qformer' def __init__( self , _a=30_522 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=0.02 , _a=1E-12 , _a=0 , _a="absolute" , _a=2 , _a=1_408 , **_a , ): super().__init__(pad_token_id=_a , **_a ) __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = hidden_act __a = intermediate_size __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = initializer_range __a = layer_norm_eps __a = position_embedding_type __a = cross_attention_frequency __a = encoder_hidden_size @classmethod def __UpperCAmelCase ( cls , _a , **_a ): cls._set_token_in_kwargs(_a ) __a , __a = cls.get_config_dict(_a , **_a ) # get the qformer config dict if we are loading from Blip2Config if config_dict.get('''model_type''' ) == "blip-2": __a = config_dict['''qformer_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(_a , **_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Any = 'blip-2' __UpperCAmelCase : List[str] = True def __init__( self , _a=None , _a=None , _a=None , _a=32 , **_a ): super().__init__(**_a ) if vision_config is None: __a = {} logger.info('''vision_config is None. initializing the Blip2VisionConfig with default values.''' ) if qformer_config is None: __a = {} logger.info('''qformer_config is None. Initializing the Blip2QFormerConfig with default values.''' ) if text_config is None: __a = {} logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).''' ) __a = BlipaVisionConfig(**_a ) __a = BlipaQFormerConfig(**_a ) __a = text_config['''model_type'''] if '''model_type''' in text_config else '''opt''' __a = CONFIG_MAPPING[text_model_type](**_a ) __a = self.text_config.tie_word_embeddings __a = self.text_config.is_encoder_decoder __a = num_query_tokens __a = self.vision_config.hidden_size __a = self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES __a = 1.0 __a = 0.02 @classmethod def __UpperCAmelCase ( cls , _a , _a , _a , **_a , ): return cls( vision_config=vision_config.to_dict() , qformer_config=qformer_config.to_dict() , text_config=text_config.to_dict() , **_a , ) def __UpperCAmelCase ( self ): __a = copy.deepcopy(self.__dict__ ) __a = self.vision_config.to_dict() __a = self.qformer_config.to_dict() __a = self.text_config.to_dict() __a = self.__class__.model_type return output
695
1
"""simple docstring""" import numpy as np import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModelWithProjection, PreTrainedModel from ...utils import logging lowercase_ = logging.get_logger(__name__) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = CLIPConfig __UpperCAmelCase : int = ['CLIPEncoderLayer'] def __init__( self , _a ): super().__init__(_a ) __a = CLIPVisionModelWithProjection(config.vision_config ) __a = nn.Linear(config.vision_config.projection_dim , 1 ) __a = nn.Linear(config.vision_config.projection_dim , 1 ) @torch.no_grad() def __UpperCAmelCase ( self , _a , _a , _a=0.5 , _a=0.5 ): __a = self.vision_model(_a )[0] __a = self.p_head(_a ) __a = nsfw_detected.flatten() __a = nsfw_detected > p_threshold __a = nsfw_detected.tolist() if any(_a ): logger.warning( '''Potential NSFW content was detected in one or more images. A black image will be returned instead.''' ''' Try again with a different prompt and/or seed.''' ) for idx, nsfw_detected_ in enumerate(_a ): if nsfw_detected_: __a = np.zeros(images[idx].shape ) __a = self.w_head(_a ) __a = watermark_detected.flatten() __a = watermark_detected > w_threshold __a = watermark_detected.tolist() if any(_a ): logger.warning( '''Potential watermarked content was detected in one or more images. A black image will be returned instead.''' ''' Try again with a different prompt and/or seed.''' ) for idx, watermark_detected_ in enumerate(_a ): if watermark_detected_: __a = np.zeros(images[idx].shape ) return images, nsfw_detected, watermark_detected
695
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType lowercase_ = logging.get_logger(__name__) lowercase_ = { "microsoft/deberta-v2-xlarge": "https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json", "microsoft/deberta-v2-xxlarge": "https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json", "microsoft/deberta-v2-xlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json" ), "microsoft/deberta-v2-xxlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json" ), } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Dict = 'deberta-v2' def __init__( self , _a=128_100 , _a=1_536 , _a=24 , _a=24 , _a=6_144 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=0 , _a=0.02 , _a=1E-7 , _a=False , _a=-1 , _a=0 , _a=True , _a=None , _a=0 , _a="gelu" , **_a , ): super().__init__(**_a ) __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = initializer_range __a = relative_attention __a = max_relative_positions __a = pad_token_id __a = position_biased_input # Backwards compatibility if type(_a ) == str: __a = [x.strip() for x in pos_att_type.lower().split('''|''' )] __a = pos_att_type __a = vocab_size __a = layer_norm_eps __a = kwargs.get('''pooler_hidden_size''' , _a ) __a = pooler_dropout __a = pooler_hidden_act class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def __UpperCAmelCase ( self ): if self.task == "multiple-choice": __a = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: __a = {0: '''batch''', 1: '''sequence'''} if self._config.type_vocab_size > 0: return OrderedDict( [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)] ) else: return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)] ) @property def __UpperCAmelCase ( self ): return 12 def __UpperCAmelCase ( self , _a , _a = -1 , _a = -1 , _a = -1 , _a = False , _a = None , _a = 3 , _a = 40 , _a = 40 , _a = None , ): __a = super().generate_dummy_inputs(preprocessor=_a , framework=_a ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
695
1
"""simple docstring""" import warnings from contextlib import contextmanager from ....processing_utils import ProcessorMixin class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = 'MCTCTFeatureExtractor' __UpperCAmelCase : Optional[Any] = 'AutoTokenizer' def __init__( self , _a , _a ): super().__init__(_a , _a ) __a = self.feature_extractor __a = False def __call__( self , *_a , **_a ): # For backward compatibility if self._in_target_context_manager: return self.current_processor(*_a , **_a ) if "raw_speech" in kwargs: warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.''' ) __a = kwargs.pop('''raw_speech''' ) else: __a = kwargs.pop('''audio''' , _a ) __a = kwargs.pop('''sampling_rate''' , _a ) __a = kwargs.pop('''text''' , _a ) if len(_a ) > 0: __a = args[0] __a = args[1:] if audio is None and text is None: raise ValueError('''You need to specify either an `audio` or `text` input to process.''' ) if audio is not None: __a = self.feature_extractor(_a , *_a , sampling_rate=_a , **_a ) if text is not None: __a = self.tokenizer(_a , **_a ) if text is None: return inputs elif audio is None: return encodings else: __a = encodings['''input_ids'''] return inputs def __UpperCAmelCase ( self , *_a , **_a ): return self.tokenizer.batch_decode(*_a , **_a ) def __UpperCAmelCase ( self , *_a , **_a ): # For backward compatibility if self._in_target_context_manager: return self.current_processor.pad(*_a , **_a ) __a = kwargs.pop('''input_features''' , _a ) __a = kwargs.pop('''labels''' , _a ) if len(_a ) > 0: __a = args[0] __a = args[1:] if input_features is not None: __a = self.feature_extractor.pad(_a , *_a , **_a ) if labels is not None: __a = self.tokenizer.pad(_a , **_a ) if labels is None: return input_features elif input_features is None: return labels else: __a = labels['''input_ids'''] return input_features def __UpperCAmelCase ( self , *_a , **_a ): return self.tokenizer.decode(*_a , **_a ) @contextmanager def __UpperCAmelCase ( self ): warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your audio inputs, or in a separate call.''' ) __a = True __a = self.tokenizer yield __a = self.feature_extractor __a = False
695
"""simple docstring""" import importlib.metadata import operator import re import sys from typing import Optional from packaging import version lowercase_ = { "<": operator.lt, "<=": operator.le, "==": operator.eq, "!=": operator.ne, ">=": operator.ge, ">": operator.gt, } def lowercase ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any] ) -> Dict: 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(lowerCAmelCase__ ) , version.parse(lowerCAmelCase__ ) ): raise ImportError( f'''{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}''' ) def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None ) -> None: __a = f'''\n{hint}''' if hint is not None else '''''' # non-versioned check if re.match(r'''^[\w_\-\d]+$''' , lowerCAmelCase__ ): __a , __a , __a = requirement, None, None else: __a = re.findall(r'''^([^!=<>\s]+)([\s!=<>]{1,2}.+)''' , lowerCAmelCase__ ) if not match: raise ValueError( '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but''' f''' got {requirement}''' ) __a , __a = match[0] __a = want_full.split(''',''' ) # there could be multiple requirements __a = {} for w in want_range: __a = re.findall(r'''^([\s!=<>]{1,2})(.+)''' , lowerCAmelCase__ ) if not match: raise ValueError( '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,''' f''' but got {requirement}''' ) __a , __a = match[0] __a = want_ver if op not in ops: raise ValueError(f'''{requirement}: need one of {list(ops.keys() )}, but got {op}''' ) # special case if pkg == "python": __a = '''.'''.join([str(lowerCAmelCase__ ) for x in sys.version_info[:3]] ) for op, want_ver in wanted.items(): _compare_versions(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) return # check if any version is installed try: __a = importlib.metadata.version(lowerCAmelCase__ ) 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(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Tuple ) -> Optional[Any]: __a = '''Try: pip install transformers -U or pip install -e \'.[dev]\' if you\'re working with git main''' return require_version(lowerCAmelCase__ , lowerCAmelCase__ )
695
1
"""simple docstring""" import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @property def __UpperCAmelCase ( self ): torch.manual_seed(0 ) __a = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def __UpperCAmelCase ( self ): __a = self.dummy_uncond_unet __a = KarrasVeScheduler() __a = KarrasVePipeline(unet=_a , scheduler=_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) __a = torch.manual_seed(0 ) __a = pipe(num_inference_steps=2 , generator=_a , output_type='''numpy''' ).images __a = torch.manual_seed(0 ) __a = pipe(num_inference_steps=2 , generator=_a , output_type='''numpy''' , return_dict=_a )[0] __a = image[0, -3:, -3:, -1] __a = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) __a = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = '''google/ncsnpp-celebahq-256''' __a = UNetaDModel.from_pretrained(_a ) __a = KarrasVeScheduler() __a = KarrasVePipeline(unet=_a , scheduler=_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) __a = torch.manual_seed(0 ) __a = pipe(num_inference_steps=20 , generator=_a , output_type='''numpy''' ).images __a = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) __a = np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
695
"""simple docstring""" from __future__ import annotations lowercase_ = list[tuple[int, int]] lowercase_ = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] lowercase_ = ([-1, 0], [0, -1], [1, 0], [0, 1]) # up, left, down, right class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a , _a , _a , _a , _a , ): __a = pos_x __a = pos_y __a = (pos_y, pos_x) __a = goal_x __a = goal_y __a = g_cost __a = parent __a = self.calculate_heuristic() def __UpperCAmelCase ( self ): __a = abs(self.pos_x - self.goal_x ) __a = abs(self.pos_y - self.goal_y ) return dx + dy def __lt__( self , _a ): return self.f_cost < other.f_cost class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a ): __a = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , _a ) __a = Node(goal[1] , goal[0] , goal[1] , goal[0] , 99_999 , _a ) __a = [self.start] __a = [] __a = False def __UpperCAmelCase ( self ): while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() __a = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: __a = True return self.retrace_path(_a ) self.closed_nodes.append(_a ) __a = self.get_successors(_a ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(_a ) else: # retrieve the best current path __a = self.open_nodes.pop(self.open_nodes.index(_a ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(_a ) else: self.open_nodes.append(_a ) if not self.reached: return [self.start.pos] return None def __UpperCAmelCase ( self , _a ): __a = [] for action in delta: __a = parent.pos_x + action[1] __a = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(_a ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( _a , _a , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , _a , ) ) return successors def __UpperCAmelCase ( self , _a ): __a = node __a = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) __a = current_node.parent path.reverse() return path if __name__ == "__main__": lowercase_ = (0, 0) lowercase_ = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) print("------") lowercase_ = GreedyBestFirst(init, goal) lowercase_ = greedy_bf.search() if path: for pos_x, pos_y in path: lowercase_ = 2 for elem in grid: print(elem)
695
1
"""simple docstring""" import argparse import torch from transformers import ( EncodecConfig, EncodecFeatureExtractor, EncodecModel, logging, ) # checkpoints downloaded from: # https://dl.fbaipublicfiles.com/encodec/v0/encodec_24khz-d7cc33bc.th # https://huggingface.co/facebook/musicgen-small/resolve/main/compression_state_dict.bin # https://dl.fbaipublicfiles.com/encodec/v0/encodec_48khz-7e698e3e.th logging.set_verbosity_info() lowercase_ = logging.get_logger("transformers.models.encodec") lowercase_ = { "quantizer.vq.layers.*._codebook.inited": "quantizer.layers.*.codebook.inited", "quantizer.vq.layers.*._codebook.cluster_size": "quantizer.layers.*.codebook.cluster_size", "quantizer.vq.layers.*._codebook.embed": "quantizer.layers.*.codebook.embed", "quantizer.vq.layers.*._codebook.embed_avg": "quantizer.layers.*.codebook.embed_avg", } lowercase_ = { "encoder.model.0.conv.conv": "encoder.layers.0.conv", "encoder.model.1.block.1.conv.conv": "encoder.layers.1.block.1.conv", "encoder.model.1.block.3.conv.conv": "encoder.layers.1.block.3.conv", "encoder.model.1.shortcut.conv.conv": "encoder.layers.1.shortcut.conv", "encoder.model.3.conv.conv": "encoder.layers.3.conv", "encoder.model.4.block.1.conv.conv": "encoder.layers.4.block.1.conv", "encoder.model.4.block.3.conv.conv": "encoder.layers.4.block.3.conv", "encoder.model.4.shortcut.conv.conv": "encoder.layers.4.shortcut.conv", "encoder.model.6.conv.conv": "encoder.layers.6.conv", "encoder.model.7.block.1.conv.conv": "encoder.layers.7.block.1.conv", "encoder.model.7.block.3.conv.conv": "encoder.layers.7.block.3.conv", "encoder.model.7.shortcut.conv.conv": "encoder.layers.7.shortcut.conv", "encoder.model.9.conv.conv": "encoder.layers.9.conv", "encoder.model.10.block.1.conv.conv": "encoder.layers.10.block.1.conv", "encoder.model.10.block.3.conv.conv": "encoder.layers.10.block.3.conv", "encoder.model.10.shortcut.conv.conv": "encoder.layers.10.shortcut.conv", "encoder.model.12.conv.conv": "encoder.layers.12.conv", "encoder.model.13.lstm": "encoder.layers.13.lstm", "encoder.model.15.conv.conv": "encoder.layers.15.conv", } lowercase_ = { "encoder.model.0.conv.norm": "encoder.layers.0.norm", "encoder.model.1.block.1.conv.norm": "encoder.layers.1.block.1.norm", "encoder.model.1.block.3.conv.norm": "encoder.layers.1.block.3.norm", "encoder.model.1.shortcut.conv.norm": "encoder.layers.1.shortcut.norm", "encoder.model.3.conv.norm": "encoder.layers.3.norm", "encoder.model.4.block.1.conv.norm": "encoder.layers.4.block.1.norm", "encoder.model.4.block.3.conv.norm": "encoder.layers.4.block.3.norm", "encoder.model.4.shortcut.conv.norm": "encoder.layers.4.shortcut.norm", "encoder.model.6.conv.norm": "encoder.layers.6.norm", "encoder.model.7.block.1.conv.norm": "encoder.layers.7.block.1.norm", "encoder.model.7.block.3.conv.norm": "encoder.layers.7.block.3.norm", "encoder.model.7.shortcut.conv.norm": "encoder.layers.7.shortcut.norm", "encoder.model.9.conv.norm": "encoder.layers.9.norm", "encoder.model.10.block.1.conv.norm": "encoder.layers.10.block.1.norm", "encoder.model.10.block.3.conv.norm": "encoder.layers.10.block.3.norm", "encoder.model.10.shortcut.conv.norm": "encoder.layers.10.shortcut.norm", "encoder.model.12.conv.norm": "encoder.layers.12.norm", "encoder.model.15.conv.norm": "encoder.layers.15.norm", } lowercase_ = { "decoder.model.0.conv.conv": "decoder.layers.0.conv", "decoder.model.1.lstm": "decoder.layers.1.lstm", "decoder.model.3.convtr.convtr": "decoder.layers.3.conv", "decoder.model.4.block.1.conv.conv": "decoder.layers.4.block.1.conv", "decoder.model.4.block.3.conv.conv": "decoder.layers.4.block.3.conv", "decoder.model.4.shortcut.conv.conv": "decoder.layers.4.shortcut.conv", "decoder.model.6.convtr.convtr": "decoder.layers.6.conv", "decoder.model.7.block.1.conv.conv": "decoder.layers.7.block.1.conv", "decoder.model.7.block.3.conv.conv": "decoder.layers.7.block.3.conv", "decoder.model.7.shortcut.conv.conv": "decoder.layers.7.shortcut.conv", "decoder.model.9.convtr.convtr": "decoder.layers.9.conv", "decoder.model.10.block.1.conv.conv": "decoder.layers.10.block.1.conv", "decoder.model.10.block.3.conv.conv": "decoder.layers.10.block.3.conv", "decoder.model.10.shortcut.conv.conv": "decoder.layers.10.shortcut.conv", "decoder.model.12.convtr.convtr": "decoder.layers.12.conv", "decoder.model.13.block.1.conv.conv": "decoder.layers.13.block.1.conv", "decoder.model.13.block.3.conv.conv": "decoder.layers.13.block.3.conv", "decoder.model.13.shortcut.conv.conv": "decoder.layers.13.shortcut.conv", "decoder.model.15.conv.conv": "decoder.layers.15.conv", } lowercase_ = { "decoder.model.0.conv.norm": "decoder.layers.0.norm", "decoder.model.3.convtr.norm": "decoder.layers.3.norm", "decoder.model.4.block.1.conv.norm": "decoder.layers.4.block.1.norm", "decoder.model.4.block.3.conv.norm": "decoder.layers.4.block.3.norm", "decoder.model.4.shortcut.conv.norm": "decoder.layers.4.shortcut.norm", "decoder.model.6.convtr.norm": "decoder.layers.6.norm", "decoder.model.7.block.1.conv.norm": "decoder.layers.7.block.1.norm", "decoder.model.7.block.3.conv.norm": "decoder.layers.7.block.3.norm", "decoder.model.7.shortcut.conv.norm": "decoder.layers.7.shortcut.norm", "decoder.model.9.convtr.norm": "decoder.layers.9.norm", "decoder.model.10.block.1.conv.norm": "decoder.layers.10.block.1.norm", "decoder.model.10.block.3.conv.norm": "decoder.layers.10.block.3.norm", "decoder.model.10.shortcut.conv.norm": "decoder.layers.10.shortcut.norm", "decoder.model.12.convtr.norm": "decoder.layers.12.norm", "decoder.model.13.block.1.conv.norm": "decoder.layers.13.block.1.norm", "decoder.model.13.block.3.conv.norm": "decoder.layers.13.block.3.norm", "decoder.model.13.shortcut.conv.norm": "decoder.layers.13.shortcut.norm", "decoder.model.15.conv.norm": "decoder.layers.15.norm", } lowercase_ = { **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_DECODER, } lowercase_ = { **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_ENCODER_48K, **MAPPING_DECODER, **MAPPING_DECODER_48K, } lowercase_ = [] lowercase_ = [] def lowercase ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : int , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any] ) -> Tuple: for attribute in key.split('''.''' ): __a = getattr(lowerCAmelCase__ , lowerCAmelCase__ ) if weight_type is not None: __a = getattr(lowerCAmelCase__ , lowerCAmelCase__ ).shape else: __a = hf_pointer.shape if hf_shape != value.shape: raise ValueError( f'''Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be''' f''' {value.shape} for {full_name}''' ) if weight_type == "weight": __a = value elif weight_type == "weight_g": __a = value elif weight_type == "weight_v": __a = value elif weight_type == "bias": __a = value elif weight_type == "running_mean": __a = value elif weight_type == "running_var": __a = value elif weight_type == "num_batches_tracked": __a = value elif weight_type == "weight_ih_l0": __a = value elif weight_type == "weight_hh_l0": __a = value elif weight_type == "bias_ih_l0": __a = value elif weight_type == "bias_hh_l0": __a = value elif weight_type == "weight_ih_l1": __a = value elif weight_type == "weight_hh_l1": __a = value elif weight_type == "bias_ih_l1": __a = value elif weight_type == "bias_hh_l1": __a = value else: __a = value logger.info(f'''{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.''' ) def lowercase ( lowerCAmelCase__ : Dict , lowerCAmelCase__ : Dict ) -> Tuple: for key in ignore_keys: if key.endswith('''.*''' ): if name.startswith(key[:-1] ): return True elif ".*." in key: __a , __a = key.split('''.*.''' ) if prefix in name and suffix in name: return True elif key in name: return True return False def lowercase ( lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[int] ) -> Dict: __a = [] if model_name == "encodec_24khz" or "encodec_32khz": __a = MAPPING_24K elif model_name == "encodec_48khz": __a = MAPPING_48K else: raise ValueError(f'''Unsupported model: {model_name}''' ) for name, value in orig_dict.items(): if should_ignore(lowerCAmelCase__ , lowerCAmelCase__ ): logger.info(f'''{name} was ignored''' ) continue __a = False for key, mapped_key in MAPPING.items(): if "*" in key: __a , __a = key.split('''.*.''' ) if prefix in name and suffix in name: __a = suffix if key in name: # HACK otherwise .embed gets initialized with .embed_avg too if key.endswith('''embed''' ) and name.endswith('''embed_avg''' ): continue __a = True if "*" in mapped_key: __a = name.split(lowerCAmelCase__ )[0].split('''.''' )[-2] __a = mapped_key.replace('''*''' , lowerCAmelCase__ ) if "weight_g" in name: __a = '''weight_g''' elif "weight_v" in name: __a = '''weight_v''' elif "weight_ih_l0" in name: __a = '''weight_ih_l0''' elif "weight_hh_l0" in name: __a = '''weight_hh_l0''' elif "bias_ih_l0" in name: __a = '''bias_ih_l0''' elif "bias_hh_l0" in name: __a = '''bias_hh_l0''' elif "weight_ih_l1" in name: __a = '''weight_ih_l1''' elif "weight_hh_l1" in name: __a = '''weight_hh_l1''' elif "bias_ih_l1" in name: __a = '''bias_ih_l1''' elif "bias_hh_l1" in name: __a = '''bias_hh_l1''' elif "bias" in name: __a = '''bias''' elif "weight" in name: __a = '''weight''' elif "running_mean" in name: __a = '''running_mean''' elif "running_var" in name: __a = '''running_var''' elif "num_batches_tracked" in name: __a = '''num_batches_tracked''' else: __a = None set_recursively(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) continue if not is_used: unused_weights.append(lowerCAmelCase__ ) logger.warning(f'''Unused weights: {unused_weights}''' ) @torch.no_grad() def lowercase ( lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : int=None , ) -> List[str]: if config_path is not None: __a = EncodecConfig.from_pretrained(lowerCAmelCase__ ) else: __a = EncodecConfig() if model_name == "encodec_24khz": pass # config is already correct elif model_name == "encodec_32khz": __a = [8, 5, 4, 4] __a = [2.2] __a = 64 __a = 32000 __a = 2048 __a = False __a = False __a = False elif model_name == "encodec_48khz": __a = [8, 5, 4, 2] __a = [3.0, 6.0, 12.0, 24.0] __a = 48000 __a = 2 __a = False __a = '''time_group_norm''' __a = True __a = 1.0 __a = 0.01 else: raise ValueError(f'''Unknown model name: {model_name}''' ) __a = EncodecModel(lowerCAmelCase__ ) __a = EncodecFeatureExtractor( feature_size=config.audio_channels , sampling_rate=config.sampling_rate , chunk_length_s=config.chunk_length_s , overlap=config.overlap , ) feature_extractor.save_pretrained(lowerCAmelCase__ ) __a = torch.load(lowerCAmelCase__ ) if "best_state" in original_checkpoint: # we might have a training state saved, in which case discard the yaml results and just retain the weights __a = original_checkpoint['''best_state'''] recursively_load_weights(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) model.save_pretrained(lowerCAmelCase__ ) if repo_id: print('''Pushing to the hub...''' ) feature_extractor.push_to_hub(lowerCAmelCase__ ) model.push_to_hub(lowerCAmelCase__ ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument( "--model", default="encodec_24khz", type=str, help="The model to convert. Should be one of 'encodec_24khz', 'encodec_32khz', 'encodec_48khz'.", ) parser.add_argument("--checkpoint_path", required=True, default=None, type=str, help="Path to original checkpoint") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") parser.add_argument( "--pytorch_dump_folder_path", required=True, default=None, type=str, help="Path to the output PyTorch model." ) parser.add_argument( "--push_to_hub", default=None, type=str, help="Where to upload the converted model on the 🤗 hub." ) lowercase_ = parser.parse_args() convert_checkpoint( args.model, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
695
"""simple docstring""" import argparse import torch from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert from transformers.utils import logging logging.set_verbosity_info() def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str ) -> List[Any]: # Initialise PyTorch model __a = RemBertConfig.from_json_file(lowerCAmelCase__ ) print('''Building PyTorch model from configuration: {}'''.format(str(lowerCAmelCase__ ) ) ) __a = RemBertModel(lowerCAmelCase__ ) # Load weights from tf checkpoint load_tf_weights_in_rembert(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Save pytorch-model print('''Save PyTorch model to {}'''.format(lowerCAmelCase__ ) ) torch.save(model.state_dict() , lowerCAmelCase__ ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--rembert_config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained RemBERT 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." ) lowercase_ = parser.parse_args() convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)
695
1
"""simple docstring""" import unittest from transformers import EsmConfig, is_torch_available from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import EsmForMaskedLM, EsmForSequenceClassification, EsmForTokenClassification, EsmModel from transformers.models.esm.modeling_esm import ( ESM_PRETRAINED_MODEL_ARCHIVE_LIST, EsmEmbeddings, create_position_ids_from_input_ids, ) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a=13 , _a=7 , _a=False , _a=True , _a=False , _a=True , _a=33 , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=3 , _a=4 , _a=None , ): __a = parent __a = batch_size __a = seq_length __a = is_training __a = use_input_mask __a = use_token_type_ids __a = use_labels __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = type_sequence_label_size __a = initializer_range __a = num_labels __a = num_choices __a = scope def __UpperCAmelCase ( self ): __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a = None if self.use_input_mask: __a = random_attention_mask([self.batch_size, self.seq_length] ) __a = None __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __a = ids_tensor([self.batch_size] , self.num_choices ) __a = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def __UpperCAmelCase ( self ): return EsmConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , pad_token_id=1 , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , _a ): __a = EsmModel(config=_a ) model.to(_a ) model.eval() __a = model(_a , attention_mask=_a ) __a = model(_a ) __a = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , _a ): __a = EsmForMaskedLM(config=_a ) model.to(_a ) model.eval() __a = model(_a , attention_mask=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , _a ): __a = self.num_labels __a = EsmForTokenClassification(config=_a ) model.to(_a ) model.eval() __a = model(_a , attention_mask=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = config_and_inputs __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = False __UpperCAmelCase : List[Any] = ( ( EsmForMaskedLM, EsmModel, EsmForSequenceClassification, EsmForTokenClassification, ) if is_torch_available() else () ) __UpperCAmelCase : List[str] = () __UpperCAmelCase : List[str] = ( { 'feature-extraction': EsmModel, 'fill-mask': EsmForMaskedLM, 'text-classification': EsmForSequenceClassification, 'token-classification': EsmForTokenClassification, 'zero-shot': EsmForSequenceClassification, } if is_torch_available() else {} ) __UpperCAmelCase : Optional[Any] = True def __UpperCAmelCase ( self ): __a = EsmModelTester(self ) __a = ConfigTester(self , config_class=_a , hidden_size=37 ) def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: __a = type self.model_tester.create_and_check_model(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_a ) @slow def __UpperCAmelCase ( self ): for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = EsmModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs()[0] __a = EsmEmbeddings(config=_a ) __a = torch.as_tensor([[12, 31, 13, model.padding_idx]] ) __a = torch.as_tensor( [ [ 0 + model.padding_idx + 1, 1 + model.padding_idx + 1, 2 + model.padding_idx + 1, model.padding_idx, ] ] ) __a = create_position_ids_from_input_ids(_a , model.padding_idx ) self.assertEqual(position_ids.shape , expected_positions.shape ) self.assertTrue(torch.all(torch.eq(_a , _a ) ) ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs()[0] __a = EsmEmbeddings(config=_a ) __a = torch.empty(2 , 4 , 30 ) __a = [ 0 + embeddings.padding_idx + 1, 1 + embeddings.padding_idx + 1, 2 + embeddings.padding_idx + 1, 3 + embeddings.padding_idx + 1, ] __a = torch.as_tensor([expected_single_positions, expected_single_positions] ) __a = embeddings.create_position_ids_from_inputs_embeds(_a ) self.assertEqual(position_ids.shape , expected_positions.shape ) self.assertTrue(torch.all(torch.eq(_a , _a ) ) ) @unittest.skip('''Esm does not support embedding resizing''' ) def __UpperCAmelCase ( self ): pass @unittest.skip('''Esm does not support embedding resizing''' ) def __UpperCAmelCase ( self ): pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __UpperCAmelCase ( self ): pass @require_torch class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' @slow def __UpperCAmelCase ( self ): with torch.no_grad(): __a = EsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) model.eval() __a = torch.tensor([[0, 1, 2, 3, 4, 5]] ) __a = model(_a )[0] __a = 33 __a = torch.Size((1, 6, vocab_size) ) self.assertEqual(output.shape , _a ) __a = torch.tensor( [[[8.9215, -10.5898, -6.4671], [-6.3967, -13.9114, -1.1212], [-7.7812, -13.9516, -3.7406]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _a , atol=1E-4 ) ) @slow def __UpperCAmelCase ( self ): with torch.no_grad(): __a = EsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) model.eval() __a = torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) __a = model(_a )[0] # compare the actual values for a slice. __a = torch.tensor( [[[0.1444, 0.5413, 0.3248], [0.3034, 0.0053, 0.3108], [0.3228, -0.2499, 0.3415]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _a , atol=1E-4 ) )
695
"""simple docstring""" import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel lowercase_ = "0.12" # assumed parallelism: 8 @require_flax @is_staging_test class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @classmethod def __UpperCAmelCase ( cls ): __a = TOKEN HfFolder.save_token(_a ) @classmethod def __UpperCAmelCase ( cls ): try: delete_repo(token=cls._token , repo_id='''test-model-flax''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-model-flax-org''' ) except HTTPError: pass def __UpperCAmelCase ( self ): __a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) __a = FlaxBertModel(_a ) model.push_to_hub('''test-model-flax''' , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''test-model-flax''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(_a , repo_id='''test-model-flax''' , push_to_hub=_a , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) def __UpperCAmelCase ( self ): __a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) __a = FlaxBertModel(_a ) model.push_to_hub('''valid_org/test-model-flax-org''' , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-model-flax-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( _a , repo_id='''valid_org/test-model-flax-org''' , push_to_hub=_a , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_a , 1E-3 , msg=f'''{key} not identical''' ) def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : Dict ) -> Optional[int]: __a = True __a = flatten_dict(modela.params ) __a = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1e-4: __a = False return models_are_equal @require_flax class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) __a = FlaxBertModel(_a ) __a = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_a , _a ) ) with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertTrue(check_models_equal(_a , _a ) ) def __UpperCAmelCase ( self ): __a = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) __a = FlaxBertModel(_a ) __a = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_a , _a ) , max_shard_size='''10KB''' ) with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertTrue(check_models_equal(_a , _a ) ) def __UpperCAmelCase ( self ): __a = '''bert''' __a = '''hf-internal-testing/tiny-random-bert-subfolder''' with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertIsNotNone(_a ) def __UpperCAmelCase ( self ): __a = '''bert''' __a = '''hf-internal-testing/tiny-random-bert-sharded-subfolder''' with self.assertRaises(_a ): __a = FlaxBertModel.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a , subfolder=_a ) self.assertIsNotNone(_a )
695
1
"""simple docstring""" import math import time from typing import Dict, List, Optional from torch.utils.data import Dataset from transformers import SeqaSeqTrainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , *_a , _a=None , _a=None , **_a ): super().__init__(*_a , **_a ) __a = eval_examples __a = post_process_function def __UpperCAmelCase ( self , _a = None , _a=None , _a = None , _a = "eval" , **_a , ): __a = gen_kwargs.copy() __a = ( gen_kwargs['''max_length'''] if gen_kwargs.get('''max_length''' ) is not None else self.args.generation_max_length ) __a = ( gen_kwargs['''num_beams'''] if gen_kwargs.get('''num_beams''' ) is not None else self.args.generation_num_beams ) __a = gen_kwargs __a = self.eval_dataset if eval_dataset is None else eval_dataset __a = self.get_eval_dataloader(_a ) __a = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. __a = self.compute_metrics __a = None __a = time.time() __a = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: __a = eval_loop( _a , description='''Evaluation''' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=_a , metric_key_prefix=_a , ) finally: __a = compute_metrics __a = self.args.eval_batch_size * self.args.world_size if f'''{metric_key_prefix}_jit_compilation_time''' in output.metrics: start_time += output.metrics[f'''{metric_key_prefix}_jit_compilation_time'''] output.metrics.update( speed_metrics( _a , _a , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default __a = self.post_process_function(_a , _a , _a ) __a = self.compute_metrics(_a ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f'''{metric_key_prefix}_''' ): __a = metrics.pop(_a ) metrics.update(output.metrics ) else: __a = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(_a ) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) __a = self.callback_handler.on_evaluate(self.args , self.state , self.control , _a ) return metrics def __UpperCAmelCase ( self , _a , _a , _a=None , _a = "test" , **_a ): __a = gen_kwargs.copy() __a = self.get_test_dataloader(_a ) # Temporarily disable metric computation, we will do it in the loop here. __a = self.compute_metrics __a = None __a = time.time() __a = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: __a = eval_loop( _a , description='''Prediction''' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=_a , metric_key_prefix=_a , ) finally: __a = compute_metrics __a = self.args.eval_batch_size * self.args.world_size if f'''{metric_key_prefix}_jit_compilation_time''' in output.metrics: start_time += output.metrics[f'''{metric_key_prefix}_jit_compilation_time'''] output.metrics.update( speed_metrics( _a , _a , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is None or self.compute_metrics is None: return output __a = self.post_process_function(_a , _a , _a , '''predict''' ) __a = self.compute_metrics(_a ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f'''{metric_key_prefix}_''' ): __a = metrics.pop(_a ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=_a )
695
"""simple docstring""" import unittest from diffusers.models.unet_ad_blocks import * # noqa F403 from diffusers.utils import torch_device from .test_unet_blocks_common import UNetBlockTesterMixin class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = DownBlockaD # noqa F405 __UpperCAmelCase : Any = 'down' def __UpperCAmelCase ( self ): __a = [-0.0232, -0.9869, 0.8054, -0.0637, -0.1688, -1.4264, 0.4470, -1.3394, 0.0904] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = ResnetDownsampleBlockaD # noqa F405 __UpperCAmelCase : List[str] = 'down' def __UpperCAmelCase ( self ): __a = [0.0710, 0.2410, -0.7320, -1.0757, -1.1343, 0.3540, -0.0133, -0.2576, 0.0948] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[int] = AttnDownBlockaD # noqa F405 __UpperCAmelCase : Optional[Any] = 'down' def __UpperCAmelCase ( self ): __a = [0.0636, 0.8964, -0.6234, -1.0131, 0.0844, 0.4935, 0.3437, 0.0911, -0.2957] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : List[Any] = CrossAttnDownBlockaD # noqa F405 __UpperCAmelCase : Optional[Any] = 'down' def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.2238, -0.7396, -0.2255, -0.3829, 0.1925, 1.1665, 0.0603, -0.7295, 0.1983] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = SimpleCrossAttnDownBlockaD # noqa F405 __UpperCAmelCase : Any = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_encoder_hidden_states=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict @unittest.skipIf(torch_device == '''mps''' , '''MPS result is not consistent''' ) def __UpperCAmelCase ( self ): __a = [0.7921, -0.0992, -0.1962, -0.7695, -0.4242, 0.7804, 0.4737, 0.2765, 0.3338] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = SkipDownBlockaD # noqa F405 __UpperCAmelCase : Tuple = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_skip_sample=_a ) def __UpperCAmelCase ( self ): __a = [-0.0845, -0.2087, -0.2465, 0.0971, 0.1900, -0.0484, 0.2664, 0.4179, 0.5069] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : List[Any] = AttnSkipDownBlockaD # noqa F405 __UpperCAmelCase : Optional[int] = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_skip_sample=_a ) def __UpperCAmelCase ( self ): __a = [0.5539, 0.1609, 0.4924, 0.0537, -0.1995, 0.4050, 0.0979, -0.2721, -0.0642] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = DownEncoderBlockaD # noqa F405 __UpperCAmelCase : Optional[int] = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = { '''in_channels''': 32, '''out_channels''': 32, } __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [1.1102, 0.5302, 0.4872, -0.0023, -0.8042, 0.0483, -0.3489, -0.5632, 0.7626] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = AttnDownEncoderBlockaD # noqa F405 __UpperCAmelCase : Any = 'down' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = { '''in_channels''': 32, '''out_channels''': 32, } __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.8966, -0.1486, 0.8568, 0.8141, -0.9046, -0.1342, -0.0972, -0.7417, 0.1538] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = UNetMidBlockaD # noqa F405 __UpperCAmelCase : Any = 'mid' def __UpperCAmelCase ( self ): __a = { '''in_channels''': 32, '''temb_channels''': 128, } __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [-0.1062, 1.7248, 0.3494, 1.4569, -0.0910, -1.2421, -0.9984, 0.6736, 1.0028] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = UNetMidBlockaDCrossAttn # noqa F405 __UpperCAmelCase : str = 'mid' def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.0187, 2.4220, 0.4484, 1.1203, -0.6121, -1.5122, -0.8270, 0.7851, 1.8335] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Any = UNetMidBlockaDSimpleCrossAttn # noqa F405 __UpperCAmelCase : List[Any] = 'mid' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_encoder_hidden_states=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.7143, 1.9974, 0.5448, 1.3977, 0.1282, -1.1237, -1.4238, 0.5530, 0.8880] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = UpBlockaD # noqa F405 __UpperCAmelCase : Union[str, Any] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [-0.2041, -0.4165, -0.3022, 0.0041, -0.6628, -0.7053, 0.1928, -0.0325, 0.0523] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = ResnetUpsampleBlockaD # noqa F405 __UpperCAmelCase : int = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [0.2287, 0.3549, -0.1346, 0.4797, -0.1715, -0.9649, 0.7305, -0.5864, -0.6244] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Dict = CrossAttnUpBlockaD # noqa F405 __UpperCAmelCase : List[Any] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [-0.1403, -0.3515, -0.0420, -0.1425, 0.3167, 0.5094, -0.2181, 0.5931, 0.5582] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = SimpleCrossAttnUpBlockaD # noqa F405 __UpperCAmelCase : Optional[int] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a , include_encoder_hidden_states=_a ) def __UpperCAmelCase ( self ): __a , __a = super().prepare_init_args_and_inputs_for_common() __a = 32 return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.2645, 0.1480, 0.0909, 0.8044, -0.9758, -0.9083, 0.0994, -1.1453, -0.7402] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Any = AttnUpBlockaD # noqa F405 __UpperCAmelCase : List[Any] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) @unittest.skipIf(torch_device == '''mps''' , '''MPS result is not consistent''' ) def __UpperCAmelCase ( self ): __a = [0.0979, 0.1326, 0.0021, 0.0659, 0.2249, 0.0059, 0.1132, 0.5952, 0.1033] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Any = SkipUpBlockaD # noqa F405 __UpperCAmelCase : str = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [-0.0893, -0.1234, -0.1506, -0.0332, 0.0123, -0.0211, 0.0566, 0.0143, 0.0362] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = AttnSkipUpBlockaD # noqa F405 __UpperCAmelCase : int = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_res_hidden_states_tuple=_a ) def __UpperCAmelCase ( self ): __a = [0.0361, 0.0617, 0.2787, -0.0350, 0.0342, 0.3421, -0.0843, 0.0913, 0.3015] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = UpDecoderBlockaD # noqa F405 __UpperCAmelCase : List[str] = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = {'''in_channels''': 32, '''out_channels''': 32} __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.4404, 0.1998, -0.9886, -0.3320, -0.3128, -0.7034, -0.6955, -0.2338, -0.3137] super().test_output(_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[int] = AttnUpDecoderBlockaD # noqa F405 __UpperCAmelCase : Any = 'up' @property def __UpperCAmelCase ( self ): return super().get_dummy_input(include_temb=_a ) def __UpperCAmelCase ( self ): __a = {'''in_channels''': 32, '''out_channels''': 32} __a = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self ): __a = [0.6738, 0.4491, 0.1055, 1.0710, 0.7316, 0.3339, 0.3352, 0.1023, 0.3568] super().test_output(_a )
695
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = {} class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[Any] = 'llama' __UpperCAmelCase : List[Any] = ['past_key_values'] def __init__( self , _a=32_000 , _a=4_096 , _a=11_008 , _a=32 , _a=32 , _a=None , _a="silu" , _a=2_048 , _a=0.02 , _a=1E-6 , _a=True , _a=0 , _a=1 , _a=2 , _a=1 , _a=False , _a=None , **_a , ): __a = vocab_size __a = max_position_embeddings __a = hidden_size __a = intermediate_size __a = num_hidden_layers __a = num_attention_heads # for backward compatibility if num_key_value_heads is None: __a = num_attention_heads __a = num_key_value_heads __a = hidden_act __a = initializer_range __a = rms_norm_eps __a = pretraining_tp __a = use_cache __a = rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , tie_word_embeddings=_a , **_a , ) def __UpperCAmelCase ( self ): if self.rope_scaling is None: return if not isinstance(self.rope_scaling , _a ) or len(self.rope_scaling ) != 2: raise ValueError( '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, ''' f'''got {self.rope_scaling}''' ) __a = self.rope_scaling.get('''type''' , _a ) __a = self.rope_scaling.get('''factor''' , _a ) if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: raise ValueError( f'''`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}''' ) if rope_scaling_factor is None or not isinstance(_a , _a ) or rope_scaling_factor <= 1.0: raise ValueError(f'''`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}''' )
695
"""simple docstring""" import copy from typing import Dict, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING from ..detr import DetrConfig from ..swin import SwinConfig lowercase_ = { "facebook/maskformer-swin-base-ade": ( "https://huggingface.co/facebook/maskformer-swin-base-ade/blob/main/config.json" ) # See all MaskFormer models at https://huggingface.co/models?filter=maskformer } lowercase_ = logging.get_logger(__name__) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : str = 'maskformer' __UpperCAmelCase : Optional[int] = {'hidden_size': 'mask_feature_size'} __UpperCAmelCase : Any = ['resnet', 'swin'] __UpperCAmelCase : Dict = ['detr'] def __init__( self , _a = 256 , _a = 256 , _a = 0.1 , _a = False , _a = None , _a = None , _a = 0.02 , _a = 1.0 , _a = 1.0 , _a = 1.0 , _a = 20.0 , _a = None , **_a , ): if backbone_config is None: # fall back to https://huggingface.co/microsoft/swin-base-patch4-window12-384-in22k __a = SwinConfig( image_size=384 , in_channels=3 , patch_size=4 , embed_dim=128 , depths=[2, 2, 18, 2] , num_heads=[4, 8, 16, 32] , window_size=12 , drop_path_rate=0.3 , out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] , ) if isinstance(_a , _a ): __a = backbone_config.pop('''model_type''' ) __a = CONFIG_MAPPING[backbone_model_type] __a = config_class.from_dict(_a ) # verify that the backbone is supported if backbone_config.model_type not in self.backbones_supported: logger.warning_once( f'''Backbone {backbone_config.model_type} is not a supported model and may not be compatible with MaskFormer. ''' f'''Supported model types: {','.join(self.backbones_supported )}''' ) if decoder_config is None: # fall back to https://huggingface.co/facebook/detr-resnet-50 __a = DetrConfig() else: # verify that the decoder is supported __a = ( decoder_config.pop('''model_type''' ) if isinstance(_a , _a ) else decoder_config.model_type ) if decoder_type not in self.decoders_supported: raise ValueError( f'''Transformer Decoder {decoder_type} not supported, please use one of''' f''' {','.join(self.decoders_supported )}''' ) if isinstance(_a , _a ): __a = CONFIG_MAPPING[decoder_type] __a = config_class.from_dict(_a ) __a = backbone_config __a = decoder_config # main feature dimension for the model __a = fpn_feature_size __a = mask_feature_size # initializer __a = init_std __a = init_xavier_std # Hungarian matcher && loss __a = cross_entropy_weight __a = dice_weight __a = mask_weight __a = use_auxiliary_loss __a = no_object_weight __a = output_auxiliary_logits __a = self.decoder_config.encoder_attention_heads __a = self.decoder_config.num_hidden_layers super().__init__(**_a ) @classmethod def __UpperCAmelCase ( cls , _a , _a , **_a ): return cls( backbone_config=_a , decoder_config=_a , **_a , ) def __UpperCAmelCase ( self ): __a = copy.deepcopy(self.__dict__ ) __a = self.backbone_config.to_dict() __a = self.decoder_config.to_dict() __a = self.__class__.model_type return output
695
1
"""simple docstring""" import json import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from transformers import OneFormerImageProcessor from transformers.models.oneformer.image_processing_oneformer import binary_mask_to_rle from transformers.models.oneformer.modeling_oneformer import OneFormerForUniversalSegmentationOutput if is_vision_available(): from PIL import Image def lowercase ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Any="shi-labs/oneformer_demo" ) -> Optional[Any]: with open(hf_hub_download(lowerCAmelCase__ , lowerCAmelCase__ , repo_type='''dataset''' ) , '''r''' ) as f: __a = json.load(lowerCAmelCase__ ) __a = {} __a = [] __a = [] for key, info in class_info.items(): __a = info['''name'''] class_names.append(info['''name'''] ) if info["isthing"]: thing_ids.append(int(lowerCAmelCase__ ) ) __a = thing_ids __a = class_names return metadata class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _a , _a=7 , _a=3 , _a=30 , _a=400 , _a=None , _a=True , _a=True , _a=[0.5, 0.5, 0.5] , _a=[0.5, 0.5, 0.5] , _a=10 , _a=False , _a=255 , _a="shi-labs/oneformer_demo" , _a="ade20k_panoptic.json" , _a=10 , ): __a = parent __a = batch_size __a = num_channels __a = min_resolution __a = max_resolution __a = do_resize __a = {'''shortest_edge''': 32, '''longest_edge''': 1_333} if size is None else size __a = do_normalize __a = image_mean __a = image_std __a = class_info_file __a = prepare_metadata(_a , _a ) __a = num_text __a = repo_path # for the post_process_functions __a = 2 __a = 10 __a = 10 __a = 3 __a = 4 __a = num_labels __a = do_reduce_labels __a = ignore_index def __UpperCAmelCase ( self ): return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "num_labels": self.num_labels, "do_reduce_labels": self.do_reduce_labels, "ignore_index": self.ignore_index, "class_info_file": self.class_info_file, "metadata": self.metadata, "num_text": self.num_text, } def __UpperCAmelCase ( self , _a , _a=False ): if not batched: __a = image_inputs[0] if isinstance(_a , Image.Image ): __a , __a = image.size else: __a , __a = image.shape[1], image.shape[2] if w < h: __a = int(self.size['''shortest_edge'''] * h / w ) __a = self.size['''shortest_edge'''] elif w > h: __a = self.size['''shortest_edge'''] __a = int(self.size['''shortest_edge'''] * w / h ) else: __a = self.size['''shortest_edge'''] __a = self.size['''shortest_edge'''] else: __a = [] for image in image_inputs: __a , __a = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) __a = max(_a , key=lambda _a : item[0] )[0] __a = max(_a , key=lambda _a : item[1] )[1] return expected_height, expected_width def __UpperCAmelCase ( self ): return OneFormerForUniversalSegmentationOutput( # +1 for null class class_queries_logits=torch.randn((self.batch_size, self.num_queries, self.num_classes + 1) ) , masks_queries_logits=torch.randn((self.batch_size, self.num_queries, self.height, self.width) ) , ) @require_torch @require_vision class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[int] = OneFormerImageProcessor if (is_vision_available() and is_torch_available()) else None # only for test_image_processing_common.test_image_proc_to_json_string __UpperCAmelCase : Union[str, Any] = image_processing_class def __UpperCAmelCase ( self ): __a = OneFormerImageProcessorTester(self ) @property def __UpperCAmelCase ( self ): return self.image_processing_tester.prepare_image_processor_dict() def __UpperCAmelCase ( self ): __a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , '''image_mean''' ) ) self.assertTrue(hasattr(_a , '''image_std''' ) ) self.assertTrue(hasattr(_a , '''do_normalize''' ) ) self.assertTrue(hasattr(_a , '''do_resize''' ) ) self.assertTrue(hasattr(_a , '''size''' ) ) self.assertTrue(hasattr(_a , '''ignore_index''' ) ) self.assertTrue(hasattr(_a , '''class_info_file''' ) ) self.assertTrue(hasattr(_a , '''num_text''' ) ) self.assertTrue(hasattr(_a , '''repo_path''' ) ) self.assertTrue(hasattr(_a , '''metadata''' ) ) self.assertTrue(hasattr(_a , '''do_reduce_labels''' ) ) def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): # Initialize image_processor __a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __a = prepare_image_inputs(self.image_processing_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input __a = image_processor(image_inputs[0] , ['''semantic'''] , return_tensors='''pt''' ).pixel_values __a , __a = self.image_processing_tester.get_expected_values(_a ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched __a , __a = self.image_processing_tester.get_expected_values(_a , batched=_a ) __a = image_processor( _a , ['''semantic'''] * len(_a ) , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def __UpperCAmelCase ( self ): # Initialize image_processor __a = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __a = prepare_image_inputs(self.image_processing_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input __a = image_processor(image_inputs[0] , ['''semantic'''] , return_tensors='''pt''' ).pixel_values __a , __a = self.image_processing_tester.get_expected_values(_a ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched __a , __a = self.image_processing_tester.get_expected_values(_a , batched=_a ) __a = image_processor( _a , ['''semantic'''] * len(_a ) , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def __UpperCAmelCase ( self ): # Initialize image_processor __a = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __a = prepare_image_inputs(self.image_processing_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input __a = image_processor(image_inputs[0] , ['''semantic'''] , return_tensors='''pt''' ).pixel_values __a , __a = self.image_processing_tester.get_expected_values(_a ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched __a , __a = self.image_processing_tester.get_expected_values(_a , batched=_a ) __a = image_processor( _a , ['''semantic'''] * len(_a ) , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def __UpperCAmelCase ( self , _a=False , _a=False , _a="np" ): __a = self.image_processing_class(**self.image_processor_dict ) # prepare image and target __a = self.image_processing_tester.num_labels __a = None __a = None __a = prepare_image_inputs(self.image_processing_tester , equal_resolution=_a ) if with_segmentation_maps: __a = num_labels if is_instance_map: __a = list(range(_a ) ) * 2 __a = dict(enumerate(_a ) ) __a = [ np.random.randint(0 , high * 2 , (img.size[1], img.size[0]) ).astype(np.uinta ) for img in image_inputs ] if segmentation_type == "pil": __a = [Image.fromarray(_a ) for annotation in annotations] __a = image_processor( _a , ['''semantic'''] * len(_a ) , _a , return_tensors='''pt''' , instance_id_to_semantic_id=_a , pad_and_return_pixel_mask=_a , ) return inputs def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): def common(_a=False , _a=None ): __a = self.comm_get_image_processor_inputs( with_segmentation_maps=_a , is_instance_map=_a , segmentation_type=_a ) __a = inputs['''mask_labels'''] __a = inputs['''class_labels'''] __a = inputs['''pixel_values'''] __a = inputs['''text_inputs'''] # check the batch_size for mask_label, class_label, text_input in zip(_a , _a , _a ): self.assertEqual(mask_label.shape[0] , class_label.shape[0] ) # this ensure padding has happened self.assertEqual(mask_label.shape[1:] , pixel_values.shape[2:] ) self.assertEqual(len(_a ) , self.image_processing_tester.num_text ) common() common(is_instance_map=_a ) common(is_instance_map=_a , segmentation_type='''pil''' ) common(is_instance_map=_a , segmentation_type='''pil''' ) def __UpperCAmelCase ( self ): __a = np.zeros((20, 50) ) __a = 1 __a = 1 __a = 1 __a = binary_mask_to_rle(_a ) self.assertEqual(len(_a ) , 4 ) self.assertEqual(rle[0] , 21 ) self.assertEqual(rle[1] , 45 ) def __UpperCAmelCase ( self ): __a = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file='''ade20k_panoptic.json''' , num_text=self.image_processing_tester.num_text , repo_path='''shi-labs/oneformer_demo''' , ) __a = self.image_processing_tester.get_fake_oneformer_outputs() __a = fature_extractor.post_process_semantic_segmentation(_a ) self.assertEqual(len(_a ) , self.image_processing_tester.batch_size ) self.assertEqual( segmentation[0].shape , ( self.image_processing_tester.height, self.image_processing_tester.width, ) , ) __a = [(1, 4) for i in range(self.image_processing_tester.batch_size )] __a = fature_extractor.post_process_semantic_segmentation(_a , target_sizes=_a ) self.assertEqual(segmentation[0].shape , target_sizes[0] ) def __UpperCAmelCase ( self ): __a = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file='''ade20k_panoptic.json''' , num_text=self.image_processing_tester.num_text , repo_path='''shi-labs/oneformer_demo''' , ) __a = self.image_processing_tester.get_fake_oneformer_outputs() __a = image_processor.post_process_instance_segmentation(_a , threshold=0 ) self.assertTrue(len(_a ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue('''segmentation''' in el ) self.assertTrue('''segments_info''' in el ) self.assertEqual(type(el['''segments_info'''] ) , _a ) self.assertEqual( el['''segmentation'''].shape , (self.image_processing_tester.height, self.image_processing_tester.width) ) def __UpperCAmelCase ( self ): __a = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file='''ade20k_panoptic.json''' , num_text=self.image_processing_tester.num_text , repo_path='''shi-labs/oneformer_demo''' , ) __a = self.image_processing_tester.get_fake_oneformer_outputs() __a = image_processor.post_process_panoptic_segmentation(_a , threshold=0 ) self.assertTrue(len(_a ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue('''segmentation''' in el ) self.assertTrue('''segments_info''' in el ) self.assertEqual(type(el['''segments_info'''] ) , _a ) self.assertEqual( el['''segmentation'''].shape , (self.image_processing_tester.height, self.image_processing_tester.width) )
695
"""simple docstring""" from __future__ import annotations from collections.abc import Generator import requests from bsa import BeautifulSoup lowercase_ = "https://www.indeed.co.in/jobs?q=mobile+app+development&l=" def lowercase ( lowerCAmelCase__ : str = "mumbai" ) -> Generator[tuple[str, str], None, None]: __a = BeautifulSoup(requests.get(url + location ).content , '''html.parser''' ) # This attribute finds out all the specifics listed in a job for job in soup.find_all('''div''' , attrs={'''data-tn-component''': '''organicJob'''} ): __a = job.find('''a''' , attrs={'''data-tn-element''': '''jobTitle'''} ).text.strip() __a = job.find('''span''' , {'''class''': '''company'''} ).text.strip() yield job_title, company_name if __name__ == "__main__": for i, job in enumerate(fetch_jobs("Bangalore"), 1): print(F'''Job {i:>2} is {job[0]} at {job[1]}''')
695
1
"""simple docstring""" import argparse from collections import defaultdict import yaml lowercase_ = "docs/source/en/_toctree.yml" def lowercase ( lowerCAmelCase__ : List[Any] ) -> Optional[Any]: __a = defaultdict(lowerCAmelCase__ ) __a = [] __a = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({'''local''': doc['''local'''], '''title''': doc['''title''']} ) else: new_doc_list.append(lowerCAmelCase__ ) __a = new_doc_list __a = [key for key, value in counts.items() if value > 1] __a = [] for duplicate_key in duplicates: __a = list({doc['''title'''] for doc in doc_list if doc['''local'''] == duplicate_key} ) if len(lowerCAmelCase__ ) > 1: raise ValueError( f'''{duplicate_key} is present several times in the documentation table of content at ''' '''`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the ''' '''others.''' ) # Only add this once new_doc.append({'''local''': duplicate_key, '''title''': titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in doc_list if '''local''' not in counts or counts[doc['''local''']] == 1] ) __a = sorted(lowerCAmelCase__ , key=lambda lowerCAmelCase__ : s["title"].lower() ) # "overview" gets special treatment and is always first if len(lowerCAmelCase__ ) > 1: raise ValueError('''{doc_list} has two \'overview\' docs which is not allowed.''' ) overview_doc.extend(lowerCAmelCase__ ) # Sort return overview_doc def lowercase ( lowerCAmelCase__ : Dict=False ) -> Dict: with open(lowerCAmelCase__ , encoding='''utf-8''' ) as f: __a = yaml.safe_load(f.read() ) # Get to the API doc __a = 0 while content[api_idx]["title"] != "API": api_idx += 1 __a = content[api_idx]['''sections'''] # Then to the model doc __a = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 __a = api_doc[scheduler_idx]['''sections'''] __a = clean_doc_toc(lowerCAmelCase__ ) __a = False if new_scheduler_doc != scheduler_doc: __a = True if overwrite: __a = new_scheduler_doc if diff: if overwrite: __a = api_doc with open(lowerCAmelCase__ , '''w''' , encoding='''utf-8''' ) as f: f.write(yaml.dump(lowerCAmelCase__ , allow_unicode=lowerCAmelCase__ ) ) else: raise ValueError( '''The model doc part of the table of content is not properly sorted, run `make style` to fix this.''' ) def lowercase ( lowerCAmelCase__ : Union[str, Any]=False ) -> str: with open(lowerCAmelCase__ , encoding='''utf-8''' ) as f: __a = yaml.safe_load(f.read() ) # Get to the API doc __a = 0 while content[api_idx]["title"] != "API": api_idx += 1 __a = content[api_idx]['''sections'''] # Then to the model doc __a = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 __a = False __a = api_doc[pipeline_idx]['''sections'''] __a = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: __a = pipeline_doc['''section'''] __a = clean_doc_toc(lowerCAmelCase__ ) if overwrite: __a = new_sub_pipeline_doc new_pipeline_docs.append(lowerCAmelCase__ ) # sort overall pipeline doc __a = clean_doc_toc(lowerCAmelCase__ ) if new_pipeline_docs != pipeline_docs: __a = True if overwrite: __a = new_pipeline_docs if diff: if overwrite: __a = api_doc with open(lowerCAmelCase__ , '''w''' , encoding='''utf-8''' ) as f: f.write(yaml.dump(lowerCAmelCase__ , allow_unicode=lowerCAmelCase__ ) ) else: raise ValueError( '''The model doc part of the table of content is not properly sorted, run `make style` to fix this.''' ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") lowercase_ = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
695
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "bigcode/gpt_bigcode-santacoder": "https://huggingface.co/bigcode/gpt_bigcode-santacoder/resolve/main/config.json", } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = 'gpt_bigcode' __UpperCAmelCase : Tuple = ['past_key_values'] __UpperCAmelCase : Dict = { 'hidden_size': 'n_embd', 'max_position_embeddings': 'n_positions', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self , _a=50_257 , _a=1_024 , _a=768 , _a=12 , _a=12 , _a=None , _a="gelu_pytorch_tanh" , _a=0.1 , _a=0.1 , _a=0.1 , _a=1E-5 , _a=0.02 , _a=True , _a=True , _a=50_256 , _a=50_256 , _a=True , _a=True , _a=True , **_a , ): __a = vocab_size __a = n_positions __a = n_embd __a = n_layer __a = n_head __a = n_inner __a = activation_function __a = resid_pdrop __a = embd_pdrop __a = attn_pdrop __a = layer_norm_epsilon __a = initializer_range __a = scale_attn_weights __a = use_cache __a = attention_softmax_in_fpaa __a = scale_attention_softmax_in_fpaa __a = multi_query __a = bos_token_id __a = eos_token_id super().__init__(bos_token_id=_a , eos_token_id=_a , **_a )
695
1
"""simple docstring""" import logging import re import pytorch_quantization import pytorch_quantization.nn as quant_nn import torch from pytorch_quantization import calib from pytorch_quantization.tensor_quant import QuantDescriptor lowercase_ = logging.getLogger(__name__) lowercase_ = 5_0 # max width of layer names lowercase_ = 7_0 # max width of quantizer names def lowercase ( lowerCAmelCase__ : int ) -> List[str]: __a = parser.add_argument_group('''quant_trainer arguments''' ) group.add_argument('''--wprec''' , type=lowerCAmelCase__ , default=8 , help='''weight precision''' ) group.add_argument('''--aprec''' , type=lowerCAmelCase__ , default=8 , help='''activation precision''' ) group.add_argument('''--quant-per-tensor''' , action='''store_true''' , help='''per tensor weight scaling''' ) group.add_argument('''--quant-disable''' , action='''store_true''' , help='''disable all quantizers''' ) group.add_argument('''--quant-disable-embeddings''' , action='''store_true''' , help='''disable all embeddings quantizers''' ) group.add_argument('''--quant-disable-keyword''' , type=lowerCAmelCase__ , nargs='''+''' , help='''disable quantizers by keyword''' ) group.add_argument('''--quant-disable-layer-module''' , type=lowerCAmelCase__ , help='''disable quantizers by keyword under layer.''' ) group.add_argument('''--quant-enable-layer-module''' , type=lowerCAmelCase__ , help='''enable quantizers by keyword under layer''' ) group.add_argument('''--calibrator''' , default='''max''' , help='''which quantization range calibrator to use''' ) group.add_argument('''--percentile''' , default=lowerCAmelCase__ , type=lowerCAmelCase__ , help='''percentile for PercentileCalibrator''' ) group.add_argument('''--fuse-qkv''' , action='''store_true''' , help='''use the same scale factor for qkv''' ) group.add_argument('''--clip-gelu''' , metavar='''N''' , type=lowerCAmelCase__ , help='''clip gelu output maximum value to N''' ) group.add_argument( '''--recalibrate-weights''' , action='''store_true''' , help=( '''recalibrate weight amaxes by taking the max of the weights.''' ''' amaxes will be computed with the current quantization granularity (axis).''' ) , ) def lowercase ( lowerCAmelCase__ : List[Any] ) -> List[str]: if args.calibrator == "max": __a = '''max''' elif args.calibrator == "percentile": if args.percentile is None: raise ValueError('''Specify --percentile when using percentile calibrator''' ) __a = '''histogram''' elif args.calibrator == "mse": __a = '''histogram''' else: raise ValueError(f'''Invalid calibrator {args.calibrator}''' ) __a = QuantDescriptor(num_bits=args.aprec , calib_method=lowerCAmelCase__ ) __a = QuantDescriptor(num_bits=args.wprec , axis=(None if args.quant_per_tensor else (0,)) ) quant_nn.QuantLinear.set_default_quant_desc_input(lowerCAmelCase__ ) quant_nn.QuantLinear.set_default_quant_desc_weight(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any]=False , lowerCAmelCase__ : Optional[Any]=False ) -> Optional[Any]: logger.info('''Configuring Model for Quantization''' ) logger.info(f'''using quantization package {pytorch_quantization.__file__}''' ) if not calib: if args.quant_disable_embeddings: set_quantizer_by_name(lowerCAmelCase__ , ['''embeddings'''] , which='''weight''' , _disabled=lowerCAmelCase__ ) if args.quant_disable: set_quantizer_by_name(lowerCAmelCase__ , [''''''] , _disabled=lowerCAmelCase__ ) if args.quant_disable_keyword: set_quantizer_by_name(lowerCAmelCase__ , args.quant_disable_keyword , _disabled=lowerCAmelCase__ ) if args.quant_disable_layer_module: set_quantizer_by_name(lowerCAmelCase__ , [r'''layer.\d+.''' + args.quant_disable_layer_module] , _disabled=lowerCAmelCase__ ) if args.quant_enable_layer_module: set_quantizer_by_name(lowerCAmelCase__ , [r'''layer.\d+.''' + args.quant_enable_layer_module] , _disabled=lowerCAmelCase__ ) if args.recalibrate_weights: recalibrate_weights(lowerCAmelCase__ ) if args.fuse_qkv: fuse_qkv(lowerCAmelCase__ , lowerCAmelCase__ ) if args.clip_gelu: clip_gelu(lowerCAmelCase__ , args.clip_gelu ) # if args.local_rank in [-1, 0] and not calib: print_quant_summary(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : str ) -> List[str]: logger.info('''Enabling Calibration''' ) for name, module in model.named_modules(): if name.endswith('''_quantizer''' ): if module._calibrator is not None: module.disable_quant() module.enable_calib() else: module.disable() logger.info(f'''{name:80}: {module}''' ) def lowercase ( lowerCAmelCase__ : Dict , lowerCAmelCase__ : int ) -> Any: logger.info('''Loading calibrated amax''' ) for name, module in model.named_modules(): if name.endswith('''_quantizer''' ): if module._calibrator is not None: if isinstance(module._calibrator , calib.MaxCalibrator ): module.load_calib_amax() else: module.load_calib_amax('''percentile''' , percentile=args.percentile ) module.enable_quant() module.disable_calib() else: module.enable() model.cuda() print_quant_summary(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str ) -> Any: def fusea(lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Optional[int] ): for mod in [qq, qk, qv]: if not hasattr(lowerCAmelCase__ , '''_amax''' ): print(''' WARNING: NO AMAX BUFFER''' ) return __a = qq._amax.detach().item() __a = qk._amax.detach().item() __a = qv._amax.detach().item() __a = max(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) qq._amax.fill_(lowerCAmelCase__ ) qk._amax.fill_(lowerCAmelCase__ ) qv._amax.fill_(lowerCAmelCase__ ) logger.info(f''' q={q:5.2f} k={k:5.2f} v={v:5.2f} -> {amax:5.2f}''' ) for name, mod in model.named_modules(): if name.endswith('''.attention.self''' ): logger.info(f'''FUSE_QKV: {name:{name_width}}''' ) fusea(mod.matmul_q_input_quantizer , mod.matmul_k_input_quantizer , mod.matmul_v_input_quantizer ) if args.quant_per_tensor: fusea(mod.query._weight_quantizer , mod.key._weight_quantizer , mod.value._weight_quantizer ) def lowercase ( lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Optional[int] ) -> Any: for name, mod in model.named_modules(): if name.endswith('''.output.dense''' ) and not name.endswith('''attention.output.dense''' ): __a = mod._input_quantizer._amax.data.detach().item() mod._input_quantizer._amax.data.detach().clamp_(max=lowerCAmelCase__ ) __a = mod._input_quantizer._amax.data.detach().item() logger.info(f'''CLIP_GELU: {name:{name_width}} amax: {amax_init:5.2f} -> {amax:5.2f}''' ) def lowercase ( lowerCAmelCase__ : str ) -> List[Any]: for name, mod in model.named_modules(): if hasattr(lowerCAmelCase__ , '''_weight_quantizer''' ) and mod._weight_quantizer.axis is not None: __a = mod.weight.shape[0] __a = mod._weight_quantizer._amax.detach() __a = torch.ones(lowerCAmelCase__ , dtype=amax.dtype , device=amax.device ) * amax print(f'''expanding {name} {amax} -> {mod._weight_quantizer._amax}''' ) def lowercase ( lowerCAmelCase__ : Optional[int] ) -> Optional[int]: for name, mod in model.named_modules(): if hasattr(lowerCAmelCase__ , '''_weight_quantizer''' ): if not hasattr(mod.weight_quantizer , '''_amax''' ): print('''RECALIB: {name:{name_width}} WARNING: NO AMAX BUFFER''' ) continue # determine which axes to reduce across # e.g. a 4D tensor quantized per axis 0 should reduce over (1,2,3) __a = set() if mod._weight_quantizer.axis is None else set(mod._weight_quantizer.axis ) __a = set(range(len(mod.weight.size() ) ) ) - axis_set __a = pytorch_quantization.utils.reduce_amax(mod.weight , axis=lowerCAmelCase__ , keepdims=lowerCAmelCase__ ).detach() logger.info(f'''RECALIB: {name:{name_width}} {mod._weight_quantizer._amax.flatten()} -> {amax.flatten()}''' ) __a = amax def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Dict=25 , lowerCAmelCase__ : Any=180 , lowerCAmelCase__ : Optional[Any]=None ) -> Tuple: if ignore is None: __a = [] elif not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = [ignore] __a = 0 for name, mod in model.named_modules(): if not hasattr(lowerCAmelCase__ , '''weight''' ): continue __a = max(lowerCAmelCase__ , len(lowerCAmelCase__ ) ) for name, mod in model.named_modules(): __a = getattr(lowerCAmelCase__ , '''_input_quantizer''' , lowerCAmelCase__ ) __a = getattr(lowerCAmelCase__ , '''_weight_quantizer''' , lowerCAmelCase__ ) if not hasattr(lowerCAmelCase__ , '''weight''' ): continue if type(lowerCAmelCase__ ) in ignore: continue if [True for s in ignore if type(lowerCAmelCase__ ) is str and s in name]: continue __a = f'''Act:{input_q.extra_repr()}''' __a = f'''Wgt:{weight_q.extra_repr()}''' __a = f'''{name:{name_width}} {act_str} {wgt_str}''' if len(lowerCAmelCase__ ) <= line_width: logger.info(lowerCAmelCase__ ) else: logger.info(f'''{name:{name_width}} {act_str}''' ) logger.info(f'''{' ':{name_width}} {wgt_str}''' ) def lowercase ( lowerCAmelCase__ : List[str] ) -> List[str]: __a = 0 for name, mod in model.named_modules(): if isinstance(lowerCAmelCase__ , pytorch_quantization.nn.TensorQuantizer ): print(f'''{name:80} {mod}''' ) count += 1 print(f'''{count} TensorQuantizers found in model''' ) def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : int , lowerCAmelCase__ : List[Any] ) -> Union[str, Any]: __a = getattr(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) if quantizer_mod is not None: assert hasattr(lowerCAmelCase__ , lowerCAmelCase__ ) setattr(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) else: logger.warning(f'''{name} has no {quantizer}''' ) def lowercase ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : int="both" , **lowerCAmelCase__ : Tuple ) -> Optional[int]: __a = f'''Warning: changing {which} quantizers of {name:{qname_width}}''' for k, v in kwargs.items(): s += f''' {k}={v}''' if which in ["input", "both"]: set_quantizer(lowerCAmelCase__ , lowerCAmelCase__ , '''_input_quantizer''' , lowerCAmelCase__ , lowerCAmelCase__ ) if which in ["weight", "both"]: set_quantizer(lowerCAmelCase__ , lowerCAmelCase__ , '''_weight_quantizer''' , lowerCAmelCase__ , lowerCAmelCase__ ) logger.info(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : str , **lowerCAmelCase__ : str ) -> Any: for name, mod in model.named_modules(): if hasattr(lowerCAmelCase__ , '''_input_quantizer''' ) or hasattr(lowerCAmelCase__ , '''_weight_quantizer''' ): for n in names: if re.search(lowerCAmelCase__ , lowerCAmelCase__ ): set_quantizers(lowerCAmelCase__ , lowerCAmelCase__ , **lowerCAmelCase__ ) elif name.endswith('''_quantizer''' ): for n in names: if re.search(lowerCAmelCase__ , lowerCAmelCase__ ): __a = f'''Warning: changing {name:{name_width}}''' for k, v in kwargs.items(): s += f''' {k}={v}''' setattr(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) logger.info(lowerCAmelCase__ )
695
"""simple docstring""" import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler lowercase_ = 1_6 lowercase_ = 3_2 def lowercase ( lowerCAmelCase__ : Accelerator , lowerCAmelCase__ : int = 16 , lowerCAmelCase__ : str = "bert-base-cased" ) -> Optional[int]: __a = AutoTokenizer.from_pretrained(lowerCAmelCase__ ) __a = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(lowerCAmelCase__ : Optional[Any] ): # max_length=None => use the model max length (it's actually the default) __a = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=lowerCAmelCase__ , max_length=lowerCAmelCase__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset __a = datasets.map( lowerCAmelCase__ , batched=lowerCAmelCase__ , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , load_from_cache_file=lowerCAmelCase__ ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library __a = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(lowerCAmelCase__ : int ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(lowerCAmelCase__ , padding='''max_length''' , max_length=128 , return_tensors='''pt''' ) return tokenizer.pad(lowerCAmelCase__ , padding='''longest''' , return_tensors='''pt''' ) # Instantiate dataloaders. __a = DataLoader( tokenized_datasets['''train'''] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ ) __a = DataLoader( tokenized_datasets['''validation'''] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ ) return train_dataloader, eval_dataloader def lowercase ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any] ) -> Optional[int]: # Initialize accelerator __a = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __a = config['''lr'''] __a = int(config['''num_epochs'''] ) __a = int(config['''seed'''] ) __a = int(config['''batch_size'''] ) __a = args.model_name_or_path set_seed(lowerCAmelCase__ ) __a , __a = get_dataloaders(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __a = AutoModelForSequenceClassification.from_pretrained(lowerCAmelCase__ , return_dict=lowerCAmelCase__ ) # Instantiate optimizer __a = ( AdamW if accelerator.state.deepspeed_plugin is None or '''optimizer''' not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) __a = optimizer_cls(params=model.parameters() , lr=lowerCAmelCase__ ) if accelerator.state.deepspeed_plugin is not None: __a = accelerator.state.deepspeed_plugin.deepspeed_config[ '''gradient_accumulation_steps''' ] else: __a = 1 __a = (len(lowerCAmelCase__ ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): __a = get_linear_schedule_with_warmup( optimizer=lowerCAmelCase__ , num_warmup_steps=0 , num_training_steps=lowerCAmelCase__ , ) else: __a = DummyScheduler(lowerCAmelCase__ , total_num_steps=lowerCAmelCase__ , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. __a , __a , __a , __a , __a = accelerator.prepare( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # We need to keep track of how many total steps we have iterated over __a = 0 # We also need to keep track of the stating epoch so files are named properly __a = 0 # Now we train the model __a = evaluate.load('''glue''' , '''mrpc''' ) __a = 0 __a = {} for epoch in range(lowerCAmelCase__ , lowerCAmelCase__ ): model.train() for step, batch in enumerate(lowerCAmelCase__ ): __a = model(**lowerCAmelCase__ ) __a = outputs.loss __a = loss / gradient_accumulation_steps accelerator.backward(lowerCAmelCase__ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 model.eval() __a = 0 for step, batch in enumerate(lowerCAmelCase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): __a = model(**lowerCAmelCase__ ) __a = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times __a , __a = accelerator.gather( (predictions, batch['''labels''']) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(lowerCAmelCase__ ) - 1: __a = predictions[: len(eval_dataloader.dataset ) - samples_seen] __a = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=lowerCAmelCase__ , references=lowerCAmelCase__ , ) __a = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f'''epoch {epoch}:''' , lowerCAmelCase__ ) __a = eval_metric['''accuracy'''] if best_performance < eval_metric["accuracy"]: __a = eval_metric['''accuracy'''] if args.performance_lower_bound is not None: assert ( args.performance_lower_bound <= best_performance ), f'''Best performance metric {best_performance} is lower than the lower bound {args.performance_lower_bound}''' accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , '''all_results.json''' ) , '''w''' ) as f: json.dump(lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( ) -> List[str]: __a = argparse.ArgumentParser(description='''Simple example of training script tracking peak GPU memory usage.''' ) parser.add_argument( '''--model_name_or_path''' , type=lowerCAmelCase__ , default='''bert-base-cased''' , help='''Path to pretrained model or model identifier from huggingface.co/models.''' , required=lowerCAmelCase__ , ) parser.add_argument( '''--output_dir''' , type=lowerCAmelCase__ , default='''.''' , help='''Optional save directory where all checkpoint folders will be stored. Default is the current working directory.''' , ) parser.add_argument( '''--performance_lower_bound''' , type=lowerCAmelCase__ , default=lowerCAmelCase__ , help='''Optional lower bound for the performance metric. If set, the training will throw error when the performance metric drops below this value.''' , ) parser.add_argument( '''--num_epochs''' , type=lowerCAmelCase__ , default=3 , help='''Number of train epochs.''' , ) __a = parser.parse_args() __a = {'''lr''': 2e-5, '''num_epochs''': args.num_epochs, '''seed''': 42, '''batch_size''': 16} training_function(lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": main()
695
1
"""simple docstring""" from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder lowercase_ = datasets.utils.logging.get_logger(__name__) class __lowerCAmelCase ( folder_based_builder.FolderBasedBuilderConfig ): '''simple docstring''' __UpperCAmelCase : bool = None __UpperCAmelCase : bool = None class __lowerCAmelCase ( folder_based_builder.FolderBasedBuilder ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = datasets.Audio() __UpperCAmelCase : Tuple = 'audio' __UpperCAmelCase : int = AudioFolderConfig __UpperCAmelCase : List[str] # definition at the bottom of the script __UpperCAmelCase : Optional[int] = AudioClassification(audio_column='audio' , label_column='label' ) lowercase_ = [ ".aiff", ".au", ".avr", ".caf", ".flac", ".htk", ".svx", ".mat4", ".mat5", ".mpc2k", ".ogg", ".paf", ".pvf", ".raw", ".rf64", ".sd2", ".sds", ".ircam", ".voc", ".w64", ".wav", ".nist", ".wavex", ".wve", ".xi", ".mp3", ".opus", ] lowercase_ = AUDIO_EXTENSIONS
695
"""simple docstring""" from typing import Any def lowercase ( lowerCAmelCase__ : list , lowerCAmelCase__ : list , lowerCAmelCase__ : dict , lowerCAmelCase__ : dict , lowerCAmelCase__ : dict , ) -> list: _validation( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ) # Creates data structures and fill initial step __a = {} __a = {} for state in states_space: __a = observations_space[0] __a = ( initial_probabilities[state] * emission_probabilities[state][observation] ) __a = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(lowerCAmelCase__ ) ): __a = observations_space[o] __a = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function __a = '''''' __a = -1 for k_state in states_space: __a = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: __a = probability __a = k_state # Update probabilities and pointers dicts __a = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) __a = arg_max # The final observation __a = observations_space[len(lowerCAmelCase__ ) - 1] # argmax for given final observation __a = '''''' __a = -1 for k_state in states_space: __a = probabilities[(k_state, final_observation)] if probability > max_probability: __a = probability __a = k_state __a = arg_max # Process pointers backwards __a = last_state __a = [] for o in range(len(lowerCAmelCase__ ) - 1 , -1 , -1 ): result.append(lowerCAmelCase__ ) __a = pointers[previous, observations_space[o]] result.reverse() return result def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: _validate_not_empty( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ) _validate_lists(lowerCAmelCase__ , lowerCAmelCase__ ) _validate_dicts( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError('''There\'s an empty parameter''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any ) -> None: _validate_list(lowerCAmelCase__ , '''observations_space''' ) _validate_list(lowerCAmelCase__ , '''states_space''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str ) -> None: if not isinstance(_object , lowerCAmelCase__ ): __a = f'''{var_name} must be a list''' raise ValueError(lowerCAmelCase__ ) else: for x in _object: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = f'''{var_name} must be a list of strings''' raise ValueError(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: _validate_dict(lowerCAmelCase__ , '''initial_probabilities''' , lowerCAmelCase__ ) _validate_nested_dict(lowerCAmelCase__ , '''transition_probabilities''' ) _validate_nested_dict(lowerCAmelCase__ , '''emission_probabilities''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str ) -> None: _validate_dict(_object , lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object.values(): _validate_dict(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : type , lowerCAmelCase__ : bool = False ) -> None: if not isinstance(_object , lowerCAmelCase__ ): __a = f'''{var_name} must be a dict''' raise ValueError(lowerCAmelCase__ ) if not all(isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object ): __a = f'''{var_name} all keys must be strings''' raise ValueError(lowerCAmelCase__ ) if not all(isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object.values() ): __a = '''nested dictionary ''' if nested else '''''' __a = f'''{var_name} {nested_text}all values must be {value_type.__name__}''' raise ValueError(lowerCAmelCase__ ) if __name__ == "__main__": from doctest import testmod testmod()
695
1
"""simple docstring""" import torch from diffusers import EulerDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : int = (EulerDiscreteScheduler,) __UpperCAmelCase : Union[str, Any] = 1_0 def __UpperCAmelCase ( self , **_a ): __a = { '''num_train_timesteps''': 1_100, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', } config.update(**_a ) return config def __UpperCAmelCase ( self ): for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=_a ) def __UpperCAmelCase ( self ): for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=_a , beta_end=_a ) def __UpperCAmelCase ( self ): for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_a ) def __UpperCAmelCase ( self ): for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_a ) def __UpperCAmelCase ( self ): __a = self.scheduler_classes[0] __a = self.get_scheduler_config() __a = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps ) __a = torch.manual_seed(0 ) __a = self.dummy_model() __a = self.dummy_sample_deter * scheduler.init_noise_sigma __a = sample.to(_a ) for i, t in enumerate(scheduler.timesteps ): __a = scheduler.scale_model_input(_a , _a ) __a = model(_a , _a ) __a = scheduler.step(_a , _a , _a , generator=_a ) __a = output.prev_sample __a = torch.sum(torch.abs(_a ) ) __a = torch.mean(torch.abs(_a ) ) assert abs(result_sum.item() - 10.0807 ) < 1E-2 assert abs(result_mean.item() - 0.0131 ) < 1E-3 def __UpperCAmelCase ( self ): __a = self.scheduler_classes[0] __a = self.get_scheduler_config(prediction_type='''v_prediction''' ) __a = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps ) __a = torch.manual_seed(0 ) __a = self.dummy_model() __a = self.dummy_sample_deter * scheduler.init_noise_sigma __a = sample.to(_a ) for i, t in enumerate(scheduler.timesteps ): __a = scheduler.scale_model_input(_a , _a ) __a = model(_a , _a ) __a = scheduler.step(_a , _a , _a , generator=_a ) __a = output.prev_sample __a = torch.sum(torch.abs(_a ) ) __a = torch.mean(torch.abs(_a ) ) assert abs(result_sum.item() - 0.0002 ) < 1E-2 assert abs(result_mean.item() - 2.2_676E-06 ) < 1E-3 def __UpperCAmelCase ( self ): __a = self.scheduler_classes[0] __a = self.get_scheduler_config() __a = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps , device=_a ) __a = torch.manual_seed(0 ) __a = self.dummy_model() __a = self.dummy_sample_deter * scheduler.init_noise_sigma.cpu() __a = sample.to(_a ) for t in scheduler.timesteps: __a = scheduler.scale_model_input(_a , _a ) __a = model(_a , _a ) __a = scheduler.step(_a , _a , _a , generator=_a ) __a = output.prev_sample __a = torch.sum(torch.abs(_a ) ) __a = torch.mean(torch.abs(_a ) ) assert abs(result_sum.item() - 10.0807 ) < 1E-2 assert abs(result_mean.item() - 0.0131 ) < 1E-3 def __UpperCAmelCase ( self ): __a = self.scheduler_classes[0] __a = self.get_scheduler_config() __a = scheduler_class(**_a , use_karras_sigmas=_a ) scheduler.set_timesteps(self.num_inference_steps , device=_a ) __a = torch.manual_seed(0 ) __a = self.dummy_model() __a = self.dummy_sample_deter * scheduler.init_noise_sigma.cpu() __a = sample.to(_a ) for t in scheduler.timesteps: __a = scheduler.scale_model_input(_a , _a ) __a = model(_a , _a ) __a = scheduler.step(_a , _a , _a , generator=_a ) __a = output.prev_sample __a = torch.sum(torch.abs(_a ) ) __a = torch.mean(torch.abs(_a ) ) assert abs(result_sum.item() - 124.52_2994_9951_1719 ) < 1E-2 assert abs(result_mean.item() - 0.1_6213_9326_3339_9963 ) < 1E-3
695
"""simple docstring""" import math def lowercase ( lowerCAmelCase__ : int ) -> bool: 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(lowerCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowercase ( lowerCAmelCase__ : float = 0.1 ) -> int: __a = 3 __a = 3 while primes / (2 * j - 1) >= ratio: for i in range(j * j + j + 1 , (j + 2) * (j + 2) , j + 1 ): primes += is_prime(lowerCAmelCase__ ) j += 2 return j if __name__ == "__main__": import doctest doctest.testmod()
695
1
"""simple docstring""" import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def lowercase ( lowerCAmelCase__ : str ) -> Union[str, Any]: __a = {} __a = tokenizer(example['''content'''] , truncation=lowerCAmelCase__ )['''input_ids'''] __a = len(example['''content'''] ) / len(output['''input_ids'''] ) return output lowercase_ = HfArgumentParser(PretokenizationArguments) lowercase_ = parser.parse_args() if args.num_workers is None: lowercase_ = multiprocessing.cpu_count() lowercase_ = AutoTokenizer.from_pretrained(args.tokenizer_dir) lowercase_ = time.time() lowercase_ = load_dataset(args.dataset_name, split="train") print(F'''Dataset loaded in {time.time()-t_start:.2f}s''') lowercase_ = time.time() lowercase_ = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ "repo_name", "path", "copies", "size", "content", "license", "hash", "line_mean", "line_max", "alpha_frac", "autogenerated", ], ) print(F'''Dataset tokenized in {time.time()-t_start:.2f}s''') lowercase_ = time.time() ds.push_to_hub(args.tokenized_data_repo) print(F'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
695
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = { "configuration_mctct": ["MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MCTCTConfig"], "feature_extraction_mctct": ["MCTCTFeatureExtractor"], "processing_mctct": ["MCTCTProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST", "MCTCTForCTC", "MCTCTModel", "MCTCTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig from .feature_extraction_mctct import MCTCTFeatureExtractor from .processing_mctct import MCTCTProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
695
1
"""simple docstring""" import json import os from typing import Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = {"vocab_file": "vocab.json"} lowercase_ = { "vocab_file": { "mgp-str": "https://huggingface.co/alibaba-damo/mgp-str-base/blob/main/vocab.json", } } lowercase_ = {"mgp-str": 2_7} class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : int = VOCAB_FILES_NAMES __UpperCAmelCase : Any = PRETRAINED_VOCAB_FILES_MAP __UpperCAmelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self , _a , _a="[GO]" , _a="[GO]" , _a="[s]" , _a="[GO]" , **_a ): super().__init__( unk_token=_a , bos_token=_a , eos_token=_a , pad_token=_a , **_a , ) with open(_a , encoding='''utf-8''' ) as vocab_handle: __a = json.load(_a ) __a = {v: k for k, v in self.vocab.items()} @property def __UpperCAmelCase ( self ): return len(self.vocab ) def __UpperCAmelCase ( self ): return dict(self.vocab , **self.added_tokens_encoder ) def __UpperCAmelCase ( self , _a ): __a = [] for s in text: char_tokens.extend(_a ) return char_tokens def __UpperCAmelCase ( self , _a ): return self.vocab.get(_a , self.vocab.get(self.unk_token ) ) def __UpperCAmelCase ( self , _a ): return self.decoder.get(_a ) def __UpperCAmelCase ( self , _a , _a = None ): if not os.path.isdir(_a ): logger.error('''Vocabulary path ({}) should be a directory'''.format(_a ) ) return __a = os.path.join( _a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) with open(_a , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.vocab , indent=2 , sort_keys=_a , ensure_ascii=_a ) + '''\n''' ) return (vocab_file,)
695
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json" ), } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[Any] = 'dpr' def __init__( self , _a=30_522 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=2 , _a=0.02 , _a=1E-12 , _a=0 , _a="absolute" , _a = 0 , **_a , ): super().__init__(pad_token_id=_a , **_a ) __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = hidden_act __a = intermediate_size __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = initializer_range __a = layer_norm_eps __a = projection_dim __a = position_embedding_type
695
1
"""simple docstring""" import json import os import shutil import tempfile import unittest from transformers import BatchEncoding, CanineTokenizer from transformers.testing_utils import require_tokenizers, require_torch from transformers.tokenization_utils import AddedToken from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[int] = CanineTokenizer __UpperCAmelCase : List[str] = False def __UpperCAmelCase ( self ): super().setUp() __a = CanineTokenizer() tokenizer.save_pretrained(self.tmpdirname ) @cached_property def __UpperCAmelCase ( self ): return CanineTokenizer.from_pretrained('''google/canine-s''' ) def __UpperCAmelCase ( self , **_a ): __a = self.tokenizer_class.from_pretrained(self.tmpdirname , **_a ) __a = 1_024 return tokenizer @require_torch def __UpperCAmelCase ( self ): __a = self.canine_tokenizer __a = ['''Life is like a box of chocolates.''', '''You never know what you\'re gonna get.'''] # fmt: off __a = [57_344, 76, 105, 102, 101, 32, 105, 115, 32, 108, 105, 107, 101, 32, 97, 32, 98, 111, 120, 32, 111, 102, 32, 99, 104, 111, 99, 111, 108, 97, 116, 101, 115, 46, 57_345, 0, 0, 0, 0] # fmt: on __a = tokenizer(_a , padding=_a , return_tensors='''pt''' ) self.assertIsInstance(_a , _a ) __a = list(batch.input_ids.numpy()[0] ) self.assertListEqual(_a , _a ) self.assertEqual((2, 39) , batch.input_ids.shape ) self.assertEqual((2, 39) , batch.attention_mask.shape ) @require_torch def __UpperCAmelCase ( self ): __a = self.canine_tokenizer __a = ['''Once there was a man.''', '''He wrote a test in HuggingFace Tranformers.'''] __a = tokenizer(_a , padding=_a , return_tensors='''pt''' ) # check if input_ids, attention_mask and token_type_ids are returned self.assertIn('''input_ids''' , _a ) self.assertIn('''attention_mask''' , _a ) self.assertIn('''token_type_ids''' , _a ) @require_torch def __UpperCAmelCase ( self ): __a = self.canine_tokenizer __a = [ '''What\'s the weater?''', '''It\'s about 25 degrees.''', ] __a = tokenizer( text_target=_a , max_length=32 , padding='''max_length''' , truncation=_a , return_tensors='''pt''' ) self.assertEqual(32 , targets['''input_ids'''].shape[1] ) def __UpperCAmelCase ( self ): # safety check on max_len default value so we are sure the test works __a = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): self.assertNotEqual(tokenizer.model_max_length , 42 ) # Now let's start the test __a = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): # Isolate this from the other tests because we save additional tokens/etc __a = tempfile.mkdtemp() __a = ''' He is very happy, UNwant\u00E9d,running''' __a = tokenizer.encode(_a , add_special_tokens=_a ) tokenizer.save_pretrained(_a ) __a = tokenizer.__class__.from_pretrained(_a ) __a = after_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) shutil.rmtree(_a ) __a = self.get_tokenizers(model_max_length=42 ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): # Isolate this from the other tests because we save additional tokens/etc __a = tempfile.mkdtemp() __a = ''' He is very happy, UNwant\u00E9d,running''' __a = tokenizer.additional_special_tokens # We can add a new special token for Canine as follows: __a = chr(0XE007 ) additional_special_tokens.append(_a ) tokenizer.add_special_tokens({'''additional_special_tokens''': additional_special_tokens} ) __a = tokenizer.encode(_a , add_special_tokens=_a ) tokenizer.save_pretrained(_a ) __a = tokenizer.__class__.from_pretrained(_a ) __a = after_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) self.assertIn(_a , after_tokenizer.additional_special_tokens ) self.assertEqual(after_tokenizer.model_max_length , 42 ) __a = tokenizer.__class__.from_pretrained(_a , model_max_length=43 ) self.assertEqual(tokenizer.model_max_length , 43 ) shutil.rmtree(_a ) def __UpperCAmelCase ( self ): __a = self.get_tokenizers(do_lower_case=_a ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): __a , __a = self.get_clean_sequence(_a ) # a special token for Canine can be defined as follows: __a = 0XE005 __a = chr(_a ) tokenizer.add_special_tokens({'''cls_token''': special_token} ) __a = tokenizer.encode(_a , add_special_tokens=_a ) self.assertEqual(len(_a ) , 1 ) __a = tokenizer.decode(ids + encoded_special_token , clean_up_tokenization_spaces=_a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) self.assertEqual(_a , input_encoded + special_token_id ) __a = tokenizer.decode(_a , skip_special_tokens=_a ) self.assertTrue(special_token not in decoded ) def __UpperCAmelCase ( self ): __a = self.get_tokenizers(do_lower_case=_a ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): __a = chr(0XE005 ) __a = chr(0XE006 ) # `add_tokens` method stores special tokens only in `tokenizer.unique_no_split_tokens`. (in tokenization_utils.py) tokenizer.add_tokens([SPECIAL_TOKEN_1] , special_tokens=_a ) # `add_special_tokens` method stores special tokens in `tokenizer.additional_special_tokens`, # which also occur in `tokenizer.all_special_tokens`. (in tokenization_utils_base.py) tokenizer.add_special_tokens({'''additional_special_tokens''': [SPECIAL_TOKEN_2]} ) __a = tokenizer.tokenize(_a ) __a = tokenizer.tokenize(_a ) self.assertEqual(len(_a ) , 1 ) self.assertEqual(len(_a ) , 1 ) self.assertEqual(token_a[0] , _a ) self.assertEqual(token_a[0] , _a ) @require_tokenizers def __UpperCAmelCase ( self ): __a = self.get_tokenizers(do_lower_case=_a ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): # a special token for Canine can be defined as follows: __a = 0XE006 __a = chr(_a ) __a = AddedToken(_a , lstrip=_a ) tokenizer.add_special_tokens({'''additional_special_tokens''': [new_token]} ) with tempfile.TemporaryDirectory() as tmp_dir_name: tokenizer.save_pretrained(_a ) tokenizer.from_pretrained(_a ) def __UpperCAmelCase ( self ): __a = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(_a ) with open(os.path.join(_a , '''special_tokens_map.json''' ) , encoding='''utf-8''' ) as json_file: __a = json.load(_a ) with open(os.path.join(_a , '''tokenizer_config.json''' ) , encoding='''utf-8''' ) as json_file: __a = json.load(_a ) # a special token for Canine can be defined as follows: __a = 0XE006 __a = chr(_a ) __a = [new_token_a] __a = [new_token_a] with open(os.path.join(_a , '''special_tokens_map.json''' ) , '''w''' , encoding='''utf-8''' ) as outfile: json.dump(_a , _a ) with open(os.path.join(_a , '''tokenizer_config.json''' ) , '''w''' , encoding='''utf-8''' ) as outfile: json.dump(_a , _a ) # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and # "special_tokens_map.json" files __a = tokenizer_class.from_pretrained(_a , extra_ids=0 ) self.assertIn(_a , tokenizer_without_change_in_init.additional_special_tokens ) # self.assertIn("an_additional_special_token",tokenizer_without_change_in_init.get_vocab()) # ByT5Tokenization no vocab self.assertEqual( [new_token_a] , tokenizer_without_change_in_init.convert_ids_to_tokens( tokenizer_without_change_in_init.convert_tokens_to_ids([new_token_a] ) ) , ) __a = 0XE007 __a = chr(_a ) # Now we test that we can change the value of additional_special_tokens in the from_pretrained __a = [AddedToken(_a , lstrip=_a )] __a = tokenizer_class.from_pretrained( _a , additional_special_tokens=_a , extra_ids=0 ) self.assertIn(_a , tokenizer.additional_special_tokens ) # self.assertIn(new_token_2,tokenizer.get_vocab()) # ByT5Tokenization no vocab self.assertEqual( [new_token_a] , tokenizer.convert_ids_to_tokens(tokenizer.convert_tokens_to_ids([new_token_a] ) ) ) @require_tokenizers def __UpperCAmelCase ( self ): __a = self.get_tokenizers(do_lower_case=_a ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): __a = '''hello world''' if self.space_between_special_tokens: __a = '''[CLS] hello world [SEP]''' else: __a = input __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = tokenizer.decode(_a , spaces_between_special_tokens=self.space_between_special_tokens ) self.assertIn(_a , [output, output.lower()] ) def __UpperCAmelCase ( self ): __a = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): __a = [ '''bos_token''', '''eos_token''', '''unk_token''', '''sep_token''', '''pad_token''', '''cls_token''', '''mask_token''', ] __a = '''a''' __a = ord(_a ) for attr in attributes_list: setattr(_a , attr + '''_id''' , _a ) self.assertEqual(getattr(_a , _a ) , _a ) self.assertEqual(getattr(_a , attr + '''_id''' ) , _a ) setattr(_a , attr + '''_id''' , _a ) self.assertEqual(getattr(_a , _a ) , _a ) self.assertEqual(getattr(_a , attr + '''_id''' ) , _a ) setattr(_a , '''additional_special_tokens_ids''' , [] ) self.assertListEqual(getattr(_a , '''additional_special_tokens''' ) , [] ) self.assertListEqual(getattr(_a , '''additional_special_tokens_ids''' ) , [] ) __a = 0XE006 __a = chr(_a ) setattr(_a , '''additional_special_tokens_ids''' , [additional_special_token_id] ) self.assertListEqual(getattr(_a , '''additional_special_tokens''' ) , [additional_special_token] ) self.assertListEqual(getattr(_a , '''additional_special_tokens_ids''' ) , [additional_special_token_id] ) def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): pass
695
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = StableDiffusionInpaintPipeline __UpperCAmelCase : int = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS __UpperCAmelCase : str = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __UpperCAmelCase : int = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess __UpperCAmelCase : Tuple = frozenset([] ) def __UpperCAmelCase ( self ): torch.manual_seed(0 ) __a = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=9 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=_a , ) __a = PNDMScheduler(skip_prk_steps=_a ) torch.manual_seed(0 ) __a = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) __a = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , hidden_act='''gelu''' , projection_dim=512 , ) __a = CLIPTextModel(_a ) __a = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __a = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def __UpperCAmelCase ( self , _a , _a=0 ): # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched __a = floats_tensor((1, 3, 32, 32) , rng=random.Random(_a ) ).to(_a ) __a = image.cpu().permute(0 , 2 , 3 , 1 )[0] __a = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((64, 64) ) __a = Image.fromarray(np.uinta(image + 4 ) ).convert('''RGB''' ).resize((64, 64) ) if str(_a ).startswith('''mps''' ): __a = torch.manual_seed(_a ) else: __a = torch.Generator(device=_a ).manual_seed(_a ) __a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': init_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def __UpperCAmelCase ( self ): __a = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a = self.get_dummy_components() __a = StableDiffusionInpaintPipeline(**_a ) __a = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = sd_pipe(**_a ).images __a = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __a = np.array([0.4727, 0.5735, 0.3941, 0.5446, 0.5926, 0.4394, 0.5062, 0.4654, 0.4476] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __UpperCAmelCase ( self ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self ): __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench.npy''' ) __a = '''stabilityai/stable-diffusion-2-inpainting''' __a = StableDiffusionInpaintPipeline.from_pretrained(_a , safety_checker=_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = '''Face of a yellow cat, high resolution, sitting on a park bench''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , output_type='''np''' , ) __a = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 9E-3 def __UpperCAmelCase ( self ): __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench_fp16.npy''' ) __a = '''stabilityai/stable-diffusion-2-inpainting''' __a = StableDiffusionInpaintPipeline.from_pretrained( _a , torch_dtype=torch.floataa , safety_checker=_a , ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = '''Face of a yellow cat, high resolution, sitting on a park bench''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , output_type='''np''' , ) __a = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 5E-1 def __UpperCAmelCase ( self ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __a = '''stabilityai/stable-diffusion-2-inpainting''' __a = PNDMScheduler.from_pretrained(_a , subfolder='''scheduler''' ) __a = StableDiffusionInpaintPipeline.from_pretrained( _a , safety_checker=_a , scheduler=_a , torch_dtype=torch.floataa , ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() __a = '''Face of a yellow cat, high resolution, sitting on a park bench''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , num_inference_steps=2 , output_type='''np''' , ) __a = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 10**9
695
1
"""simple docstring""" 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 __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = LongformerTokenizer __UpperCAmelCase : Union[str, Any] = True __UpperCAmelCase : Optional[Any] = LongformerTokenizerFast __UpperCAmelCase : Optional[int] = True def __UpperCAmelCase ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt __a = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] __a = dict(zip(_a , range(len(_a ) ) ) ) __a = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] __a = {'''unk_token''': '''<unk>'''} __a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) __a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(_a ) ) def __UpperCAmelCase ( self , **_a ): kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **_a ) def __UpperCAmelCase ( self , **_a ): kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **_a ) def __UpperCAmelCase ( self , _a ): __a = '''lower newer''' __a = '''lower newer''' return input_text, output_text def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) __a = '''lower newer''' __a = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] __a = tokenizer.tokenize(_a ) # , add_prefix_space=True) self.assertListEqual(_a , _a ) __a = tokens + [tokenizer.unk_token] __a = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , _a ) def __UpperCAmelCase ( self ): __a = self.get_tokenizer() self.assertListEqual(tokenizer.encode('''Hello world!''' , add_special_tokens=_a ) , [0, 31_414, 232, 328, 2] ) self.assertListEqual( tokenizer.encode('''Hello world! cécé herlolip 418''' , add_special_tokens=_a ) , [0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2] , ) @slow def __UpperCAmelCase ( self ): __a = self.tokenizer_class.from_pretrained('''allenai/longformer-base-4096''' ) __a = tokenizer.encode('''sequence builders''' , add_special_tokens=_a ) __a = tokenizer.encode('''multi-sequence build''' , add_special_tokens=_a ) __a = tokenizer.encode( '''sequence builders''' , add_special_tokens=_a , add_prefix_space=_a ) __a = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=_a , add_prefix_space=_a ) __a = tokenizer.build_inputs_with_special_tokens(_a ) __a = tokenizer.build_inputs_with_special_tokens(_a , _a ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def __UpperCAmelCase ( self ): __a = self.get_tokenizer() __a = '''Encode this sequence.''' __a = tokenizer.byte_encoder[''' '''.encode('''utf-8''' )[0]] # Testing encoder arguments __a = tokenizer.encode(_a , add_special_tokens=_a , add_prefix_space=_a ) __a = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(_a , _a ) __a = tokenizer.encode(_a , add_special_tokens=_a , add_prefix_space=_a ) __a = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(_a , _a ) tokenizer.add_special_tokens({'''bos_token''': '''<s>'''} ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(_a , _a ) # Testing spaces after special tokens __a = '''<mask>''' tokenizer.add_special_tokens( {'''mask_token''': AddedToken(_a , lstrip=_a , rstrip=_a )} ) # mask token has a left space __a = tokenizer.convert_tokens_to_ids(_a ) __a = '''Encode <mask> sequence''' __a = '''Encode <mask>sequence''' __a = tokenizer.encode(_a ) __a = encoded.index(_a ) __a = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(_a , _a ) __a = tokenizer.encode(_a ) __a = encoded.index(_a ) __a = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(_a , _a ) def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = self.tokenizer_class.from_pretrained(_a , **_a ) __a = '''A, <mask> AllenNLP sentence.''' __a = tokenizer_r.encode_plus(_a , add_special_tokens=_a , return_token_type_ids=_a ) __a = tokenizer_p.encode_plus(_a , add_special_tokens=_a , return_token_type_ids=_a ) # 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'''] ) , ) __a = tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids'''] ) __a = 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( _a , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) self.assertSequenceEqual( _a , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) def __UpperCAmelCase ( self ): for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): __a = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) __a = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) __a = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['''add_prefix_space'''] , _a ) self.assertEqual(post_processor_state['''add_prefix_space'''] , _a ) self.assertEqual(post_processor_state['''trim_offsets'''] , _a ) def __UpperCAmelCase ( self ): # 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})''' ): __a = '''hello''' # `hello` is a token in the vocabulary of `pretrained_name` __a = f'''{text_of_1_token} {text_of_1_token}''' __a = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) __a = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_a ) + 1, len(_a ) + 1 + len(_a )) , ) __a = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) __a = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_a ) + 1, len(_a ) + 1 + len(_a )) , ) __a = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) __a = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_a ), len(_a ) + 1 + len(_a )) , ) __a = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) __a = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_a ), len(_a ) + 1 + len(_a )) , ) __a = 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)), # ) __a = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) __a = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_a ) + 1, 1 + len(_a ) + 1 + len(_a )) , ) __a = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) __a = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_a ), 1 + len(_a ) + 1 + len(_a )) , ) __a = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) __a = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_a ), 1 + len(_a ) + 1 + len(_a )) , )
695
"""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 __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : int = 0 __UpperCAmelCase : bool = False __UpperCAmelCase : float = 3.0 class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): # 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=_a ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'''a''': 2, '''c''': 2.25} ) @require_cuda def __UpperCAmelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. __a = GradScalerKwargs(init_scale=1_024 , growth_factor=2 ) AcceleratorState._reset_state() __a = Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) __a = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2_000 ) self.assertEqual(scaler._enabled , _a ) @require_multi_gpu def __UpperCAmelCase ( self ): __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": lowercase_ = DistributedDataParallelKwargs(bucket_cap_mb=1_5, find_unused_parameters=True) lowercase_ = Accelerator(kwargs_handlers=[ddp_scaler]) lowercase_ = torch.nn.Linear(1_0_0, 2_0_0) lowercase_ = accelerator.prepare(model) # Check the values changed in kwargs lowercase_ = "" lowercase_ = model.bucket_bytes_cap // (1_0_2_4 * 1_0_2_4) if observed_bucket_cap_map != 1_5: 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)
695
1
"""simple docstring""" from typing import Any def lowercase ( lowerCAmelCase__ : list , lowerCAmelCase__ : list , lowerCAmelCase__ : dict , lowerCAmelCase__ : dict , lowerCAmelCase__ : dict , ) -> list: _validation( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ) # Creates data structures and fill initial step __a = {} __a = {} for state in states_space: __a = observations_space[0] __a = ( initial_probabilities[state] * emission_probabilities[state][observation] ) __a = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(lowerCAmelCase__ ) ): __a = observations_space[o] __a = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function __a = '''''' __a = -1 for k_state in states_space: __a = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: __a = probability __a = k_state # Update probabilities and pointers dicts __a = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) __a = arg_max # The final observation __a = observations_space[len(lowerCAmelCase__ ) - 1] # argmax for given final observation __a = '''''' __a = -1 for k_state in states_space: __a = probabilities[(k_state, final_observation)] if probability > max_probability: __a = probability __a = k_state __a = arg_max # Process pointers backwards __a = last_state __a = [] for o in range(len(lowerCAmelCase__ ) - 1 , -1 , -1 ): result.append(lowerCAmelCase__ ) __a = pointers[previous, observations_space[o]] result.reverse() return result def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: _validate_not_empty( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ) _validate_lists(lowerCAmelCase__ , lowerCAmelCase__ ) _validate_dicts( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError('''There\'s an empty parameter''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any ) -> None: _validate_list(lowerCAmelCase__ , '''observations_space''' ) _validate_list(lowerCAmelCase__ , '''states_space''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str ) -> None: if not isinstance(_object , lowerCAmelCase__ ): __a = f'''{var_name} must be a list''' raise ValueError(lowerCAmelCase__ ) else: for x in _object: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = f'''{var_name} must be a list of strings''' raise ValueError(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , ) -> None: _validate_dict(lowerCAmelCase__ , '''initial_probabilities''' , lowerCAmelCase__ ) _validate_nested_dict(lowerCAmelCase__ , '''transition_probabilities''' ) _validate_nested_dict(lowerCAmelCase__ , '''emission_probabilities''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str ) -> None: _validate_dict(_object , lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object.values(): _validate_dict(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : type , lowerCAmelCase__ : bool = False ) -> None: if not isinstance(_object , lowerCAmelCase__ ): __a = f'''{var_name} must be a dict''' raise ValueError(lowerCAmelCase__ ) if not all(isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object ): __a = f'''{var_name} all keys must be strings''' raise ValueError(lowerCAmelCase__ ) if not all(isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) for x in _object.values() ): __a = '''nested dictionary ''' if nested else '''''' __a = f'''{var_name} {nested_text}all values must be {value_type.__name__}''' raise ValueError(lowerCAmelCase__ ) if __name__ == "__main__": from doctest import testmod testmod()
695
"""simple docstring""" import inspect import os import sys import unittest import accelerate from accelerate.test_utils import execute_subprocess_async, require_tpu class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = inspect.getfile(accelerate.test_utils ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) __a = os.path.sep.join(inspect.getfile(self.__class__ ).split(os.path.sep )[:-1] ) @require_tpu def __UpperCAmelCase ( self ): __a = f''' {self.test_dir}/xla_spawn.py --num_cores 8 {self.test_file_path} '''.split() __a = [sys.executable] + distributed_args execute_subprocess_async(_a , env=os.environ.copy() )
695
1
"""simple docstring""" from maths.prime_factors import prime_factors def lowercase ( lowerCAmelCase__ : int ) -> int: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = f'''Input value of [number={number}] must be an integer''' raise TypeError(lowerCAmelCase__ ) if number < 1: raise ValueError('''Input must be a positive integer''' ) return -1 if len(prime_factors(lowerCAmelCase__ ) ) % 2 else 1 if __name__ == "__main__": import doctest doctest.testmod()
695
"""simple docstring""" import os import unittest from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, BertTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = BertTokenizer __UpperCAmelCase : Optional[Any] = BertTokenizerFast __UpperCAmelCase : str = True __UpperCAmelCase : Tuple = True __UpperCAmelCase : Any = filter_non_english def __UpperCAmelCase ( self ): super().setUp() __a = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] __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] ) ) def __UpperCAmelCase ( self , _a ): __a = '''UNwant\u00E9d,running''' __a = '''unwanted, running''' return input_text, output_text def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file ) __a = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(_a , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [9, 6, 7, 12, 10, 11] ) def __UpperCAmelCase ( self ): if not self.test_rust_tokenizer: return __a = self.get_tokenizer() __a = self.get_rust_tokenizer() __a = '''UNwant\u00E9d,running''' __a = tokenizer.tokenize(_a ) __a = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) __a = self.get_rust_tokenizer() __a = tokenizer.encode(_a ) __a = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a ) # With lower casing __a = self.get_tokenizer(do_lower_case=_a ) __a = self.get_rust_tokenizer(do_lower_case=_a ) __a = '''UNwant\u00E9d,running''' __a = tokenizer.tokenize(_a ) __a = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) __a = self.get_rust_tokenizer() __a = tokenizer.encode(_a ) __a = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('''ah\u535A\u63A8zz''' ) , ['''ah''', '''\u535A''', '''\u63A8''', '''zz'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''hello''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hällo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''h\u00E9llo'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HäLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HaLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer(do_lower_case=_a , never_split=['''[UNK]'''] ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? [UNK]''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''', '''[UNK]'''] ) def __UpperCAmelCase ( self ): __a = BasicTokenizer() __a = '''a\n\'ll !!to?\'d of, can\'t.''' __a = ['''a''', '''\'''', '''ll''', '''!''', '''!''', '''to''', '''?''', '''\'''', '''d''', '''of''', ''',''', '''can''', '''\'''', '''t''', '''.'''] self.assertListEqual(tokenizer.tokenize(_a ) , _a ) def __UpperCAmelCase ( self ): __a = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing'''] __a = {} for i, token in enumerate(_a ): __a = i __a = WordpieceTokenizer(vocab=_a , unk_token='''[UNK]''' ) self.assertListEqual(tokenizer.tokenize('''''' ) , [] ) self.assertListEqual(tokenizer.tokenize('''unwanted running''' ) , ['''un''', '''##want''', '''##ed''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.tokenize('''unwantedX running''' ) , ['''[UNK]''', '''runn''', '''##ing'''] ) def __UpperCAmelCase ( self ): self.assertTrue(_is_whitespace(''' ''' ) ) self.assertTrue(_is_whitespace('''\t''' ) ) self.assertTrue(_is_whitespace('''\r''' ) ) self.assertTrue(_is_whitespace('''\n''' ) ) self.assertTrue(_is_whitespace('''\u00A0''' ) ) self.assertFalse(_is_whitespace('''A''' ) ) self.assertFalse(_is_whitespace('''-''' ) ) def __UpperCAmelCase ( self ): self.assertTrue(_is_control('''\u0005''' ) ) self.assertFalse(_is_control('''A''' ) ) self.assertFalse(_is_control(''' ''' ) ) self.assertFalse(_is_control('''\t''' ) ) self.assertFalse(_is_control('''\r''' ) ) def __UpperCAmelCase ( self ): self.assertTrue(_is_punctuation('''-''' ) ) self.assertTrue(_is_punctuation('''$''' ) ) self.assertTrue(_is_punctuation('''`''' ) ) self.assertTrue(_is_punctuation('''.''' ) ) self.assertFalse(_is_punctuation('''A''' ) ) self.assertFalse(_is_punctuation(''' ''' ) ) def __UpperCAmelCase ( self ): __a = self.get_tokenizer() __a = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(_a ) for t in ['''Test''', '''\xad''', '''test''']] , [['''[UNK]'''], [], ['''[UNK]''']] ) self.assertListEqual( [rust_tokenizer.tokenize(_a ) for t in ['''Test''', '''\xad''', '''test''']] , [['''[UNK]'''], [], ['''[UNK]''']] ) @slow def __UpperCAmelCase ( self ): __a = self.tokenizer_class.from_pretrained('''bert-base-uncased''' ) __a = tokenizer.encode('''sequence builders''' , add_special_tokens=_a ) __a = tokenizer.encode('''multi-sequence build''' , add_special_tokens=_a ) __a = tokenizer.build_inputs_with_special_tokens(_a ) __a = tokenizer.build_inputs_with_special_tokens(_a , _a ) assert encoded_sentence == [101] + text + [102] assert encoded_pair == [101] + text + [102] + text_a + [102] def __UpperCAmelCase ( self ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = f'''A, naïve {tokenizer_r.mask_token} AllenNLP sentence.''' __a = tokenizer_r.encode_plus( _a , return_attention_mask=_a , return_token_type_ids=_a , return_offsets_mapping=_a , add_special_tokens=_a , ) __a = tokenizer_r.do_lower_case if hasattr(_a , '''do_lower_case''' ) else False __a = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), '''A'''), ((1, 2), ''','''), ((3, 5), '''na'''), ((5, 6), '''##ï'''), ((6, 8), '''##ve'''), ((9, 15), tokenizer_r.mask_token), ((16, 21), '''Allen'''), ((21, 23), '''##NL'''), ((23, 24), '''##P'''), ((25, 33), '''sentence'''), ((33, 34), '''.'''), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), '''a'''), ((1, 2), ''','''), ((3, 8), '''naive'''), ((9, 15), tokenizer_r.mask_token), ((16, 21), '''allen'''), ((21, 23), '''##nl'''), ((23, 24), '''##p'''), ((25, 33), '''sentence'''), ((33, 34), '''.'''), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens['''input_ids'''] ) ) self.assertEqual([e[0] for e in expected_results] , tokens['''offset_mapping'''] ) def __UpperCAmelCase ( self ): __a = ['''的''', '''人''', '''有'''] __a = ''''''.join(_a ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __a = True __a = self.tokenizer_class.from_pretrained(_a , **_a ) __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = tokenizer_p.encode(_a , add_special_tokens=_a ) __a = tokenizer_r.encode(_a , add_special_tokens=_a ) __a = tokenizer_r.convert_ids_to_tokens(_a ) __a = tokenizer_p.convert_ids_to_tokens(_a ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(_a , _a ) self.assertListEqual(_a , _a ) __a = False __a = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __a = self.tokenizer_class.from_pretrained(_a , **_a ) __a = tokenizer_r.encode(_a , add_special_tokens=_a ) __a = tokenizer_p.encode(_a , add_special_tokens=_a ) __a = tokenizer_r.convert_ids_to_tokens(_a ) __a = tokenizer_p.convert_ids_to_tokens(_a ) # it is expected that only the first Chinese character is not preceded by "##". __a = [ f'''##{token}''' if idx != 0 else token for idx, token in enumerate(_a ) ] self.assertListEqual(_a , _a ) self.assertListEqual(_a , _a )
695
1
"""simple docstring""" from __future__ import annotations lowercase_ = [True] * 1_0_0_0_0_0_1 lowercase_ = 2 while i * i <= 1_0_0_0_0_0_0: if seive[i]: for j in range(i * i, 1_0_0_0_0_0_1, i): lowercase_ = False i += 1 def lowercase ( lowerCAmelCase__ : int ) -> bool: return seive[n] def lowercase ( lowerCAmelCase__ : int ) -> bool: return any(digit in '''02468''' for digit in str(lowerCAmelCase__ ) ) def lowercase ( lowerCAmelCase__ : int = 1000000 ) -> list[int]: __a = [2] # result already includes the number 2. for num in range(3 , limit + 1 , 2 ): if is_prime(lowerCAmelCase__ ) and not contains_an_even_digit(lowerCAmelCase__ ): __a = str(lowerCAmelCase__ ) __a = [int(str_num[j:] + str_num[:j] ) for j in range(len(lowerCAmelCase__ ) )] if all(is_prime(lowerCAmelCase__ ) for i in list_nums ): result.append(lowerCAmelCase__ ) return result def lowercase ( ) -> int: return len(find_circular_primes() ) if __name__ == "__main__": print(F'''{len(find_circular_primes()) = }''')
695
"""simple docstring""" from __future__ import annotations def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> float: if days_between_payments <= 0: raise ValueError('''days_between_payments must be > 0''' ) if daily_interest_rate < 0: raise ValueError('''daily_interest_rate must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return principal * daily_interest_rate * days_between_payments def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float , ) -> float: if number_of_compounding_periods <= 0: raise ValueError('''number_of_compounding_periods must be > 0''' ) if nominal_annual_interest_rate_percentage < 0: raise ValueError('''nominal_annual_interest_rate_percentage must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return principal * ( (1 + nominal_annual_interest_rate_percentage) ** number_of_compounding_periods - 1 ) def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float , ) -> float: if number_of_years <= 0: raise ValueError('''number_of_years must be > 0''' ) if nominal_annual_percentage_rate < 0: raise ValueError('''nominal_annual_percentage_rate must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return compound_interest( lowerCAmelCase__ , nominal_annual_percentage_rate / 365 , number_of_years * 365 ) if __name__ == "__main__": import doctest doctest.testmod()
695
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "s-JoL/Open-Llama-V1": "https://huggingface.co/s-JoL/Open-Llama-V1/blob/main/config.json", } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Any = 'open-llama' def __init__( self , _a=100_000 , _a=4_096 , _a=11_008 , _a=32 , _a=32 , _a="silu" , _a=2_048 , _a=0.02 , _a=1E-6 , _a=True , _a=0 , _a=1 , _a=2 , _a=False , _a=True , _a=0.1 , _a=0.1 , _a=True , _a=True , _a=None , **_a , ): __a = vocab_size __a = max_position_embeddings __a = hidden_size __a = intermediate_size __a = num_hidden_layers __a = num_attention_heads __a = hidden_act __a = initializer_range __a = rms_norm_eps __a = use_cache __a = kwargs.pop( '''use_memorry_efficient_attention''' , _a ) __a = hidden_dropout_prob __a = attention_dropout_prob __a = use_stable_embedding __a = shared_input_output_embedding __a = rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , tie_word_embeddings=_a , **_a , ) def __UpperCAmelCase ( self ): if self.rope_scaling is None: return if not isinstance(self.rope_scaling , _a ) or len(self.rope_scaling ) != 2: raise ValueError( '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, ''' f'''got {self.rope_scaling}''' ) __a = self.rope_scaling.get('''type''' , _a ) __a = self.rope_scaling.get('''factor''' , _a ) if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: raise ValueError( f'''`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}''' ) if rope_scaling_factor is None or not isinstance(_a , _a ) or rope_scaling_factor <= 1.0: raise ValueError(f'''`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}''' )
695
"""simple docstring""" def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any=False ) -> Any: if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = len(set_a.intersection(lowerCAmelCase__ ) ) if alternative_union: __a = len(lowerCAmelCase__ ) + len(lowerCAmelCase__ ) else: __a = len(set_a.union(lowerCAmelCase__ ) ) return intersection / union if isinstance(lowerCAmelCase__ , (list, tuple) ) and isinstance(lowerCAmelCase__ , (list, tuple) ): __a = [element for element in set_a if element in set_b] if alternative_union: __a = len(lowerCAmelCase__ ) + len(lowerCAmelCase__ ) return len(lowerCAmelCase__ ) / union else: __a = set_a + [element for element in set_b if element not in set_a] return len(lowerCAmelCase__ ) / len(lowerCAmelCase__ ) return len(lowerCAmelCase__ ) / len(lowerCAmelCase__ ) return None if __name__ == "__main__": lowercase_ = {"a", "b", "c", "d", "e"} lowercase_ = {"c", "d", "e", "f", "h", "i"} print(jaccard_similarity(set_a, set_b))
695
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = {"configuration_ibert": ["IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "IBertConfig", "IBertOnnxConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "IBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "IBertForMaskedLM", "IBertForMultipleChoice", "IBertForQuestionAnswering", "IBertForSequenceClassification", "IBertForTokenClassification", "IBertModel", "IBertPreTrainedModel", ] if TYPE_CHECKING: from .configuration_ibert import IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, IBertConfig, IBertOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ibert import ( IBERT_PRETRAINED_MODEL_ARCHIVE_LIST, IBertForMaskedLM, IBertForMultipleChoice, IBertForQuestionAnswering, IBertForSequenceClassification, IBertForTokenClassification, IBertModel, IBertPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
695
"""simple docstring""" from __future__ import annotations import requests def lowercase ( lowerCAmelCase__ : str ) -> dict: __a = f'''https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty''' return requests.get(lowerCAmelCase__ ).json() def lowercase ( lowerCAmelCase__ : int = 10 ) -> list[dict]: __a = '''https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty''' __a = requests.get(lowerCAmelCase__ ).json()[:max_stories] return [get_hackernews_story(lowerCAmelCase__ ) for story_id in story_ids] def lowercase ( lowerCAmelCase__ : int = 10 ) -> str: __a = hackernews_top_stories(lowerCAmelCase__ ) return "\n".join('''* [{title}]({url})'''.format(**lowerCAmelCase__ ) for story in stories ) if __name__ == "__main__": print(hackernews_top_stories_as_markdown())
695
1