code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
'''simple docstring''' import unittest from datasets import load_dataset from transformers.pipelines import pipeline from transformers.testing_utils import is_pipeline_test, nested_simplify, require_torch, slow @is_pipeline_test @require_torch class UpperCAmelCase_ ( unittest.TestCase ): @require_torch def __UpperCAmelCase ( self : Any ) -> Optional[Any]: lowerCAmelCase = pipeline( task='zero-shot-audio-classification' , model='hf-internal-testing/tiny-clap-htsat-unfused' ) lowerCAmelCase = load_dataset('ashraq/esc50' ) lowerCAmelCase = dataset['train']['audio'][-1]['array'] lowerCAmelCase = audio_classifier(UpperCAmelCase__ , candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner'] ) self.assertEqual( nested_simplify(UpperCAmelCase__ ) , [{'score': 0.501, 'label': 'Sound of a dog'}, {'score': 0.499, 'label': 'Sound of vaccum cleaner'}] , ) @unittest.skip('No models are available in TF' ) def __UpperCAmelCase ( self : Any ) -> Optional[Any]: pass @slow @require_torch def __UpperCAmelCase ( self : Tuple ) -> List[Any]: lowerCAmelCase = pipeline( task='zero-shot-audio-classification' , model='laion/clap-htsat-unfused' , ) # This is an audio of a dog lowerCAmelCase = load_dataset('ashraq/esc50' ) lowerCAmelCase = dataset['train']['audio'][-1]['array'] lowerCAmelCase = audio_classifier(UpperCAmelCase__ , candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner'] ) self.assertEqual( nested_simplify(UpperCAmelCase__ ) , [ {'score': 0.999, 'label': 'Sound of a dog'}, {'score': 0.001, 'label': 'Sound of vaccum cleaner'}, ] , ) lowerCAmelCase = audio_classifier([audio] * 5 , candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner'] ) self.assertEqual( nested_simplify(UpperCAmelCase__ ) , [ [ {'score': 0.999, 'label': 'Sound of a dog'}, {'score': 0.001, 'label': 'Sound of vaccum cleaner'}, ], ] * 5 , ) lowerCAmelCase = audio_classifier( [audio] * 5 , candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner'] , batch_size=5 ) self.assertEqual( nested_simplify(UpperCAmelCase__ ) , [ [ {'score': 0.999, 'label': 'Sound of a dog'}, {'score': 0.001, 'label': 'Sound of vaccum cleaner'}, ], ] * 5 , ) @unittest.skip('No models are available in TF' ) def __UpperCAmelCase ( self : Any ) -> Union[str, Any]: pass
4
'''simple docstring''' import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __snake_case =logging.get_logger(__name__) __snake_case ={ """vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_config_file""": """tokenizer_config.json""", } __snake_case ={ """vocab_file""": {"""facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json"""}, """merges_file""": {"""facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt"""}, """tokenizer_config_file""": { """facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json""" }, } __snake_case ={"""facebook/blenderbot-3B""": 128} class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : List[Any] = VOCAB_FILES_NAMES lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[Any] = ['''input_ids''', '''attention_mask'''] lowerCamelCase : List[Any] = BlenderbotTokenizer def __init__( self : Union[str, Any] , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : List[Any]=None , UpperCAmelCase__ : str="replace" , UpperCAmelCase__ : Dict="<s>" , UpperCAmelCase__ : Tuple="</s>" , UpperCAmelCase__ : Optional[Any]="</s>" , UpperCAmelCase__ : Any="<s>" , UpperCAmelCase__ : List[str]="<unk>" , UpperCAmelCase__ : int="<pad>" , UpperCAmelCase__ : Union[str, Any]="<mask>" , UpperCAmelCase__ : str=False , UpperCAmelCase__ : Union[str, Any]=True , **UpperCAmelCase__ : Optional[int] , ) -> int: super().__init__( UpperCAmelCase__ , UpperCAmelCase__ , tokenizer_file=UpperCAmelCase__ , errors=UpperCAmelCase__ , bos_token=UpperCAmelCase__ , eos_token=UpperCAmelCase__ , sep_token=UpperCAmelCase__ , cls_token=UpperCAmelCase__ , unk_token=UpperCAmelCase__ , pad_token=UpperCAmelCase__ , mask_token=UpperCAmelCase__ , add_prefix_space=UpperCAmelCase__ , trim_offsets=UpperCAmelCase__ , **UpperCAmelCase__ , ) lowerCAmelCase = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('add_prefix_space' , UpperCAmelCase__ ) != add_prefix_space: lowerCAmelCase = getattr(UpperCAmelCase__ , pre_tok_state.pop('type' ) ) lowerCAmelCase = add_prefix_space lowerCAmelCase = pre_tok_class(**UpperCAmelCase__ ) lowerCAmelCase = add_prefix_space lowerCAmelCase = 'post_processor' lowerCAmelCase = getattr(self.backend_tokenizer , UpperCAmelCase__ , UpperCAmelCase__ ) if tokenizer_component_instance: lowerCAmelCase = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: lowerCAmelCase = tuple(state['sep'] ) if "cls" in state: lowerCAmelCase = tuple(state['cls'] ) lowerCAmelCase = False if state.get('add_prefix_space' , UpperCAmelCase__ ) != add_prefix_space: lowerCAmelCase = add_prefix_space lowerCAmelCase = True if state.get('trim_offsets' , UpperCAmelCase__ ) != trim_offsets: lowerCAmelCase = trim_offsets lowerCAmelCase = True if changes_to_apply: lowerCAmelCase = getattr(UpperCAmelCase__ , state.pop('type' ) ) lowerCAmelCase = component_class(**UpperCAmelCase__ ) setattr(self.backend_tokenizer , UpperCAmelCase__ , UpperCAmelCase__ ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def __UpperCAmelCase ( self : Union[str, Any] ) -> str: if self._mask_token is None: if self.verbose: logger.error('Using mask_token, but it is not set yet.' ) return None return str(self._mask_token ) @mask_token.setter def __UpperCAmelCase ( self : int , UpperCAmelCase__ : Optional[Any] ) -> Tuple: lowerCAmelCase = AddedToken(UpperCAmelCase__ , lstrip=UpperCAmelCase__ , rstrip=UpperCAmelCase__ ) if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) else value lowerCAmelCase = value def __UpperCAmelCase ( self : Optional[Any] , *UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : List[str] ) -> BatchEncoding: lowerCAmelCase = kwargs.get('is_split_into_words' , UpperCAmelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*UpperCAmelCase__ , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : List[str] , *UpperCAmelCase__ : str , **UpperCAmelCase__ : List[str] ) -> BatchEncoding: lowerCAmelCase = kwargs.get('is_split_into_words' , UpperCAmelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*UpperCAmelCase__ , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : str , UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[str] = None ) -> Tuple[str]: lowerCAmelCase = self._tokenizer.model.save(UpperCAmelCase__ , name=UpperCAmelCase__ ) return tuple(UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> Any: return token_ids_a + [self.eos_token_id] def __UpperCAmelCase ( self : Union[str, Any] , UpperCAmelCase__ : "Conversation" ) -> List[int]: lowerCAmelCase = [] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(' ' + text ) else: # Generated responses should contain them already. inputs.append(UpperCAmelCase__ ) lowerCAmelCase = ' '.join(UpperCAmelCase__ ) lowerCAmelCase = self.encode(UpperCAmelCase__ ) if len(UpperCAmelCase__ ) > self.model_max_length: lowerCAmelCase = input_ids[-self.model_max_length :] logger.warning(F'''Trimmed input from conversation as it was longer than {self.model_max_length} tokens.''' ) return input_ids
4
1
'''simple docstring''' import random import unittest import numpy as np import transformers from transformers import is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax if is_flax_available(): import os import jax.numpy as jnp from jax import jit from transformers import AutoTokenizer, FlaxAutoModelForCausalLM from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model __snake_case ="""0.12""" # assumed parallelism: 8 if is_torch_available(): import torch def a_ ( lowerCamelCase : Dict , lowerCamelCase : Dict , lowerCamelCase : Optional[Any]=None ): if rng is None: lowerCAmelCase = random.Random() lowerCAmelCase = 1 for dim in shape: total_dims *= dim lowerCAmelCase = [] for _ in range(lowerCamelCase ): values.append(rng.randint(0 , vocab_size - 1 ) ) lowerCAmelCase = np.array(lowerCamelCase , dtype=jnp.intaa ).reshape(lowerCamelCase ) return output def a_ ( lowerCamelCase : Optional[Any] , lowerCamelCase : Tuple=None ): lowerCAmelCase = ids_tensor(lowerCamelCase , vocab_size=2 , rng=lowerCamelCase ) # make sure that at least one token is attended to for each batch lowerCAmelCase = 1 return attn_mask @require_flax class UpperCAmelCase_ : lowerCamelCase : Dict = None lowerCamelCase : Dict = () def __UpperCAmelCase ( self : str ) -> Optional[int]: lowerCAmelCase , lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() # cut to half length & take max batch_size 3 lowerCAmelCase = 2 lowerCAmelCase = inputs['input_ids'].shape[-1] // 2 lowerCAmelCase = inputs['input_ids'][:max_batch_size, :sequence_length] lowerCAmelCase = jnp.ones_like(UpperCAmelCase__ ) lowerCAmelCase = attention_mask[:max_batch_size, :sequence_length] # generate max 5 tokens lowerCAmelCase = input_ids.shape[-1] + 5 if config.eos_token_id is not None and config.pad_token_id is None: # hack to allow generate for models such as GPT2 as is done in `generate()` lowerCAmelCase = config.eos_token_id return config, input_ids, attention_mask, max_length @is_pt_flax_cross_test def __UpperCAmelCase ( self : List[str] ) -> List[str]: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = self._get_input_ids_and_config() lowerCAmelCase = False lowerCAmelCase = max_length lowerCAmelCase = 0 for model_class in self.all_generative_model_classes: lowerCAmelCase = model_class(UpperCAmelCase__ ) lowerCAmelCase = model_class.__name__[4:] # Skip the "Flax" at the beginning lowerCAmelCase = getattr(UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = pt_model_class(UpperCAmelCase__ ).eval() lowerCAmelCase = load_flax_weights_in_pytorch_model(UpperCAmelCase__ , flax_model.params ) lowerCAmelCase = flax_model.generate(UpperCAmelCase__ ).sequences lowerCAmelCase = pt_model.generate(torch.tensor(UpperCAmelCase__ , dtype=torch.long ) ) if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]: lowerCAmelCase = flax_generation_outputs[:, : pt_generation_outputs.shape[-1]] self.assertListEqual(pt_generation_outputs.numpy().tolist() , flax_generation_outputs.tolist() ) def __UpperCAmelCase ( self : Dict ) -> Optional[Any]: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = self._get_input_ids_and_config() lowerCAmelCase = False lowerCAmelCase = max_length for model_class in self.all_generative_model_classes: lowerCAmelCase = model_class(UpperCAmelCase__ ) lowerCAmelCase = model.generate(UpperCAmelCase__ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase__ ) lowerCAmelCase = jit(model.generate ) lowerCAmelCase = jit_generate(UpperCAmelCase__ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = self._get_input_ids_and_config() lowerCAmelCase = True lowerCAmelCase = max_length for model_class in self.all_generative_model_classes: lowerCAmelCase = model_class(UpperCAmelCase__ ) lowerCAmelCase = model.generate(UpperCAmelCase__ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase__ ) lowerCAmelCase = jit(model.generate ) lowerCAmelCase = jit_generate(UpperCAmelCase__ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __UpperCAmelCase ( self : Any ) -> Dict: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = self._get_input_ids_and_config() lowerCAmelCase = False lowerCAmelCase = max_length lowerCAmelCase = 2 for model_class in self.all_generative_model_classes: lowerCAmelCase = model_class(UpperCAmelCase__ ) lowerCAmelCase = model.generate(UpperCAmelCase__ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase__ ) lowerCAmelCase = jit(model.generate ) lowerCAmelCase = jit_generate(UpperCAmelCase__ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __UpperCAmelCase ( self : List[Any] ) -> Any: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = self._get_input_ids_and_config() lowerCAmelCase = False lowerCAmelCase = max_length lowerCAmelCase = 2 lowerCAmelCase = 2 for model_class in self.all_generative_model_classes: lowerCAmelCase = model_class(UpperCAmelCase__ ) lowerCAmelCase = model.generate(UpperCAmelCase__ ).sequences self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences ) def __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = self._get_input_ids_and_config() lowerCAmelCase = True lowerCAmelCase = max_length lowerCAmelCase = 0.8 lowerCAmelCase = 1_0 lowerCAmelCase = 0.3 lowerCAmelCase = 1 lowerCAmelCase = 8 lowerCAmelCase = 9 for model_class in self.all_generative_model_classes: lowerCAmelCase = model_class(UpperCAmelCase__ ) lowerCAmelCase = model.generate(UpperCAmelCase__ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase__ ) lowerCAmelCase = jit(model.generate ) lowerCAmelCase = jit_generate(UpperCAmelCase__ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __UpperCAmelCase ( self : Union[str, Any] ) -> Dict: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = self._get_input_ids_and_config() lowerCAmelCase = max_length lowerCAmelCase = 1 lowerCAmelCase = 8 lowerCAmelCase = 9 for model_class in self.all_generative_model_classes: lowerCAmelCase = model_class(UpperCAmelCase__ ) lowerCAmelCase = model.generate(UpperCAmelCase__ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase__ ) lowerCAmelCase = jit(model.generate ) lowerCAmelCase = jit_generate(UpperCAmelCase__ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __UpperCAmelCase ( self : List[Any] ) -> int: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = self._get_input_ids_and_config() lowerCAmelCase = max_length lowerCAmelCase = 2 lowerCAmelCase = 1 lowerCAmelCase = 8 lowerCAmelCase = 9 for model_class in self.all_generative_model_classes: lowerCAmelCase = model_class(UpperCAmelCase__ ) lowerCAmelCase = model.generate(UpperCAmelCase__ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase__ ) lowerCAmelCase = jit(model.generate ) lowerCAmelCase = jit_generate(UpperCAmelCase__ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __UpperCAmelCase ( self : List[Any] ) -> Dict: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = self._get_input_ids_and_config() # pad attention mask on the left lowerCAmelCase = attention_mask.at[(0, 0)].set(0 ) lowerCAmelCase = False lowerCAmelCase = max_length for model_class in self.all_generative_model_classes: lowerCAmelCase = model_class(UpperCAmelCase__ ) lowerCAmelCase = model.generate(UpperCAmelCase__ , attention_mask=UpperCAmelCase__ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase__ ) lowerCAmelCase = jit(model.generate ) lowerCAmelCase = jit_generate(UpperCAmelCase__ , attention_mask=UpperCAmelCase__ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __UpperCAmelCase ( self : int ) -> int: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = self._get_input_ids_and_config() # pad attention mask on the left lowerCAmelCase = attention_mask.at[(0, 0)].set(0 ) lowerCAmelCase = True lowerCAmelCase = max_length for model_class in self.all_generative_model_classes: lowerCAmelCase = model_class(UpperCAmelCase__ ) lowerCAmelCase = model.generate(UpperCAmelCase__ , attention_mask=UpperCAmelCase__ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase__ ) lowerCAmelCase = jit(model.generate ) lowerCAmelCase = jit_generate(UpperCAmelCase__ , attention_mask=UpperCAmelCase__ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = self._get_input_ids_and_config() # pad attention mask on the left lowerCAmelCase = attention_mask.at[(0, 0)].set(0 ) lowerCAmelCase = 2 lowerCAmelCase = max_length for model_class in self.all_generative_model_classes: lowerCAmelCase = model_class(UpperCAmelCase__ ) lowerCAmelCase = model.generate(UpperCAmelCase__ , attention_mask=UpperCAmelCase__ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase__ ) lowerCAmelCase = jit(model.generate ) lowerCAmelCase = jit_generate(UpperCAmelCase__ , attention_mask=UpperCAmelCase__ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) @require_flax class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : List[Any] ) -> Any: lowerCAmelCase = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-bert' ) lowerCAmelCase = FlaxAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-bert-flax-only' ) lowerCAmelCase = 'Hello world' lowerCAmelCase = tokenizer(UpperCAmelCase__ , return_tensors='np' ).input_ids # typos are quickly detected (the correct argument is `do_sample`) with self.assertRaisesRegex(UpperCAmelCase__ , 'do_samples' ): model.generate(UpperCAmelCase__ , do_samples=UpperCAmelCase__ ) # arbitrary arguments that will not be used anywhere are also not accepted with self.assertRaisesRegex(UpperCAmelCase__ , 'foo' ): lowerCAmelCase = {'foo': 'bar'} model.generate(UpperCAmelCase__ , **UpperCAmelCase__ )
4
'''simple docstring''' from __future__ import annotations from statistics import mean def a_ ( lowerCamelCase : list[int] , lowerCamelCase : list[int] , lowerCamelCase : int ): lowerCAmelCase = [0] * no_of_processes lowerCAmelCase = [0] * no_of_processes # Initialize remaining_time to waiting_time. for i in range(lowerCamelCase ): lowerCAmelCase = burst_time[i] lowerCAmelCase = [] lowerCAmelCase = 0 lowerCAmelCase = 0 # When processes are not completed, # A process whose arrival time has passed \ # and has remaining execution time is put into the ready_process. # The shortest process in the ready_process, target_process is executed. while completed != no_of_processes: lowerCAmelCase = [] lowerCAmelCase = -1 for i in range(lowerCamelCase ): if (arrival_time[i] <= total_time) and (remaining_time[i] > 0): ready_process.append(lowerCamelCase ) if len(lowerCamelCase ) > 0: lowerCAmelCase = ready_process[0] for i in ready_process: if remaining_time[i] < remaining_time[target_process]: lowerCAmelCase = i total_time += burst_time[target_process] completed += 1 lowerCAmelCase = 0 lowerCAmelCase = ( total_time - arrival_time[target_process] - burst_time[target_process] ) else: total_time += 1 return waiting_time def a_ ( lowerCamelCase : list[int] , lowerCamelCase : int , lowerCamelCase : list[int] ): lowerCAmelCase = [0] * no_of_processes for i in range(lowerCamelCase ): lowerCAmelCase = burst_time[i] + waiting_time[i] return turn_around_time if __name__ == "__main__": print("""[TEST CASE 01]""") __snake_case =4 __snake_case =[2, 5, 3, 7] __snake_case =[0, 0, 0, 0] __snake_case =calculate_waitingtime(arrival_time, burst_time, no_of_processes) __snake_case =calculate_turnaroundtime( burst_time, no_of_processes, waiting_time ) # Printing the Result print("""PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time""") for i, process_id in enumerate(list(range(1, 5))): print( F'''{process_id}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t''' F'''{waiting_time[i]}\t\t\t\t{turn_around_time[i]}''' ) print(F'''\nAverage waiting time = {mean(waiting_time):.5f}''') print(F'''Average turnaround time = {mean(turn_around_time):.5f}''')
4
1
'''simple docstring''' from argparse import ArgumentParser, Namespace from ..utils import logging from . import BaseTransformersCLICommand def a_ ( lowerCamelCase : Namespace ): return ConvertCommand( args.model_type , args.tf_checkpoint , args.pytorch_dump_output , args.config , args.finetuning_task_name ) __snake_case =""" transformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires TensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions. """ class UpperCAmelCase_ ( __lowercase ): @staticmethod def __UpperCAmelCase ( UpperCAmelCase__ : ArgumentParser ) -> Optional[int]: lowerCAmelCase = parser.add_parser( 'convert' , help='CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.' , ) train_parser.add_argument('--model_type' , type=UpperCAmelCase__ , required=UpperCAmelCase__ , help='Model\'s type.' ) train_parser.add_argument( '--tf_checkpoint' , type=UpperCAmelCase__ , required=UpperCAmelCase__ , help='TensorFlow checkpoint path or folder.' ) train_parser.add_argument( '--pytorch_dump_output' , type=UpperCAmelCase__ , required=UpperCAmelCase__ , help='Path to the PyTorch saved model output.' ) train_parser.add_argument('--config' , type=UpperCAmelCase__ , default='' , help='Configuration file path or folder.' ) train_parser.add_argument( '--finetuning_task_name' , type=UpperCAmelCase__ , default=UpperCAmelCase__ , help='Optional fine-tuning task name if the TF model was a finetuned model.' , ) train_parser.set_defaults(func=UpperCAmelCase__ ) def __init__( self : Optional[int] , UpperCAmelCase__ : str , UpperCAmelCase__ : str , UpperCAmelCase__ : str , UpperCAmelCase__ : str , UpperCAmelCase__ : str , *UpperCAmelCase__ : List[str] , ) -> Optional[Any]: lowerCAmelCase = logging.get_logger('transformers-cli/converting' ) self._logger.info(F'''Loading model {model_type}''' ) lowerCAmelCase = model_type lowerCAmelCase = tf_checkpoint lowerCAmelCase = pytorch_dump_output lowerCAmelCase = config lowerCAmelCase = finetuning_task_name def __UpperCAmelCase ( self : List[str] ) -> Dict: if self._model_type == "albert": try: from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(UpperCAmelCase__ ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "bert": try: from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(UpperCAmelCase__ ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "funnel": try: from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(UpperCAmelCase__ ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "t5": try: from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch except ImportError: raise ImportError(UpperCAmelCase__ ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "gpt": from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import ( convert_openai_checkpoint_to_pytorch, ) convert_openai_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "transfo_xl": try: from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import ( convert_transfo_xl_checkpoint_to_pytorch, ) except ImportError: raise ImportError(UpperCAmelCase__ ) if "ckpt" in self._tf_checkpoint.lower(): lowerCAmelCase = self._tf_checkpoint lowerCAmelCase = '' else: lowerCAmelCase = self._tf_checkpoint lowerCAmelCase = '' convert_transfo_xl_checkpoint_to_pytorch( UpperCAmelCase__ , self._config , self._pytorch_dump_output , UpperCAmelCase__ ) elif self._model_type == "gpt2": try: from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import ( convert_gpta_checkpoint_to_pytorch, ) except ImportError: raise ImportError(UpperCAmelCase__ ) convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "xlnet": try: from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import ( convert_xlnet_checkpoint_to_pytorch, ) except ImportError: raise ImportError(UpperCAmelCase__ ) convert_xlnet_checkpoint_to_pytorch( self._tf_checkpoint , self._config , self._pytorch_dump_output , self._finetuning_task_name ) elif self._model_type == "xlm": from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import ( convert_xlm_checkpoint_to_pytorch, ) convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "lxmert": from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import ( convert_lxmert_checkpoint_to_pytorch, ) convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "rembert": from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import ( convert_rembert_tf_checkpoint_to_pytorch, ) convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) else: raise ValueError( '--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]' )
4
'''simple docstring''' import gc import unittest import numpy as np import torch from diffusers import StableDiffusionKDiffusionPipeline from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : Optional[int] ) -> int: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self : Tuple ) -> Any: lowerCAmelCase = StableDiffusionKDiffusionPipeline.from_pretrained('CompVis/stable-diffusion-v1-4' ) lowerCAmelCase = sd_pipe.to(UpperCAmelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) sd_pipe.set_scheduler('sample_euler' ) lowerCAmelCase = 'A painting of a squirrel eating a burger' lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = sd_pipe([prompt] , generator=UpperCAmelCase__ , guidance_scale=9.0 , num_inference_steps=2_0 , output_type='np' ) lowerCAmelCase = output.images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase = np.array([0.0_447, 0.0_492, 0.0_468, 0.0_408, 0.0_383, 0.0_408, 0.0_354, 0.0_380, 0.0_339] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __UpperCAmelCase ( self : List[str] ) -> Dict: lowerCAmelCase = StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowerCAmelCase = sd_pipe.to(UpperCAmelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) sd_pipe.set_scheduler('sample_euler' ) lowerCAmelCase = 'A painting of a squirrel eating a burger' lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = sd_pipe([prompt] , generator=UpperCAmelCase__ , guidance_scale=9.0 , num_inference_steps=2_0 , output_type='np' ) lowerCAmelCase = output.images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase = np.array([0.1_237, 0.1_320, 0.1_438, 0.1_359, 0.1_390, 0.1_132, 0.1_277, 0.1_175, 0.1_112] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-1 def __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: lowerCAmelCase = StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowerCAmelCase = sd_pipe.to(UpperCAmelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) sd_pipe.set_scheduler('sample_dpmpp_2m' ) lowerCAmelCase = 'A painting of a squirrel eating a burger' lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = sd_pipe( [prompt] , generator=UpperCAmelCase__ , guidance_scale=7.5 , num_inference_steps=1_5 , output_type='np' , use_karras_sigmas=UpperCAmelCase__ , ) lowerCAmelCase = output.images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase = np.array( [0.11_381_689, 0.12_112_921, 0.1_389_457, 0.12_549_606, 0.1_244_964, 0.10_831_517, 0.11_562_866, 0.10_867_816, 0.10_499_048] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
4
1
'''simple docstring''' # Lint as: python3 import sys from collections.abc import Mapping from typing import TYPE_CHECKING, Dict, Optional import numpy as np import pyarrow as pa from .. import config from ..utils.logging import get_logger from ..utils.py_utils import map_nested from .formatting import TensorFormatter if TYPE_CHECKING: import jax import jaxlib __snake_case =get_logger() __snake_case =None class UpperCAmelCase_ ( TensorFormatter[Mapping, '''jax.Array''', Mapping] ): def __init__( self : Optional[int] , UpperCAmelCase__ : Union[str, Any]=None , UpperCAmelCase__ : List[Any]=None , **UpperCAmelCase__ : Dict ) -> int: super().__init__(features=UpperCAmelCase__ ) import jax from jaxlib.xla_client import Device if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise ValueError( F'''Expected {device} to be a `str` not {type(UpperCAmelCase__ )}, as `jaxlib.xla_extension.Device` ''' 'is not serializable neither with `pickle` nor with `dill`. Instead you can surround ' 'the device with `str()` to get its string identifier that will be internally mapped ' 'to the actual `jaxlib.xla_extension.Device`.' ) lowerCAmelCase = device if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) else str(jax.devices()[0] ) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: lowerCAmelCase = self._map_devices_to_str() if self.device not in list(DEVICE_MAPPING.keys() ): logger.warning( F'''Device with string identifier {self.device} not listed among the available ''' F'''devices: {list(DEVICE_MAPPING.keys() )}, so falling back to the default ''' F'''device: {str(jax.devices()[0] )}.''' ) lowerCAmelCase = str(jax.devices()[0] ) lowerCAmelCase = jnp_array_kwargs @staticmethod def __UpperCAmelCase ( ) -> Dict[str, "jaxlib.xla_extension.Device"]: import jax return {str(UpperCAmelCase__ ): device for device in jax.devices()} def __UpperCAmelCase ( self : Tuple , UpperCAmelCase__ : Optional[Any] ) -> List[Any]: import jax import jax.numpy as jnp if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) and column: if all( isinstance(UpperCAmelCase__ , jax.Array ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column ): return jnp.stack(UpperCAmelCase__ , axis=0 ) return column def __UpperCAmelCase ( self : Any , UpperCAmelCase__ : Union[str, Any] ) -> List[Any]: import jax import jax.numpy as jnp if isinstance(UpperCAmelCase__ , (str, bytes, type(UpperCAmelCase__ )) ): return value elif isinstance(UpperCAmelCase__ , (np.character, np.ndarray) ) and np.issubdtype(value.dtype , np.character ): return value.tolist() lowerCAmelCase = {} if isinstance(UpperCAmelCase__ , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.integer ): # the default int precision depends on the jax config # see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision if jax.config.jax_enable_xaa: lowerCAmelCase = {'dtype': jnp.intaa} else: lowerCAmelCase = {'dtype': jnp.intaa} elif isinstance(UpperCAmelCase__ , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.floating ): lowerCAmelCase = {'dtype': jnp.floataa} elif config.PIL_AVAILABLE and "PIL" in sys.modules: import PIL.Image if isinstance(UpperCAmelCase__ , PIL.Image.Image ): lowerCAmelCase = np.asarray(UpperCAmelCase__ ) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: lowerCAmelCase = self._map_devices_to_str() with jax.default_device(DEVICE_MAPPING[self.device] ): # calling jnp.array on a np.ndarray does copy the data # see https://github.com/google/jax/issues/4486 return jnp.array(UpperCAmelCase__ , **{**default_dtype, **self.jnp_array_kwargs} ) def __UpperCAmelCase ( self : Union[str, Any] , UpperCAmelCase__ : Optional[int] ) -> str: import jax # support for torch, tf, jax etc. if config.TORCH_AVAILABLE and "torch" in sys.modules: import torch if isinstance(UpperCAmelCase__ , torch.Tensor ): return self._tensorize(data_struct.detach().cpu().numpy()[()] ) if hasattr(UpperCAmelCase__ , '__array__' ) and not isinstance(UpperCAmelCase__ , jax.Array ): lowerCAmelCase = data_struct.__array__() # support for nested types like struct of list of struct if isinstance(UpperCAmelCase__ , np.ndarray ): if data_struct.dtype == object: # jax arrays cannot be instantied from an array of objects return self._consolidate([self.recursive_tensorize(UpperCAmelCase__ ) for substruct in data_struct] ) elif isinstance(UpperCAmelCase__ , (list, tuple) ): return self._consolidate([self.recursive_tensorize(UpperCAmelCase__ ) for substruct in data_struct] ) return self._tensorize(UpperCAmelCase__ ) def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : dict ) -> str: return map_nested(self._recursive_tensorize , UpperCAmelCase__ , map_list=UpperCAmelCase__ ) def __UpperCAmelCase ( self : Any , UpperCAmelCase__ : pa.Table ) -> Mapping: lowerCAmelCase = self.numpy_arrow_extractor().extract_row(UpperCAmelCase__ ) lowerCAmelCase = self.python_features_decoder.decode_row(UpperCAmelCase__ ) return self.recursive_tensorize(UpperCAmelCase__ ) def __UpperCAmelCase ( self : str , UpperCAmelCase__ : pa.Table ) -> "jax.Array": lowerCAmelCase = self.numpy_arrow_extractor().extract_column(UpperCAmelCase__ ) lowerCAmelCase = self.python_features_decoder.decode_column(UpperCAmelCase__ , pa_table.column_names[0] ) lowerCAmelCase = self.recursive_tensorize(UpperCAmelCase__ ) lowerCAmelCase = self._consolidate(UpperCAmelCase__ ) return column def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : pa.Table ) -> Mapping: lowerCAmelCase = self.numpy_arrow_extractor().extract_batch(UpperCAmelCase__ ) lowerCAmelCase = self.python_features_decoder.decode_batch(UpperCAmelCase__ ) lowerCAmelCase = self.recursive_tensorize(UpperCAmelCase__ ) for column_name in batch: lowerCAmelCase = self._consolidate(batch[column_name] ) return batch
4
'''simple docstring''' # Copyright (c) 2021-, NVIDIA CORPORATION. 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. #################################################################################################### # # Note: If when running this conversion script you're getting an exception: # ModuleNotFoundError: No module named 'megatron.model.enums' # you need to tell python where to find the clone of Megatron-LM, e.g.: # # cd /tmp # git clone https://github.com/NVIDIA/Megatron-LM # PYTHONPATH=/tmp/Megatron-LM python src/transformers/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py ... # # if you already have it cloned elsewhere, simply adjust the path to the existing path # # If the training was done using a Megatron-LM fork, e.g., # https://github.com/microsoft/Megatron-DeepSpeed/ then chances are that you need to have that one # in your path, i.e., /path/to/Megatron-DeepSpeed/ # import argparse import os import re import zipfile import torch from transformers import AutoTokenizer, GPTaConfig def a_ ( lowerCamelCase : int , lowerCamelCase : Optional[Any] , lowerCamelCase : Union[str, Any]=0 ): # Format the message. if name is None: lowerCAmelCase = None else: lowerCAmelCase = '.' * max(0 , spaces - 2 ) + '# {:' + str(50 - spaces ) + 's}' lowerCAmelCase = fmt.format(lowerCamelCase ) # Print and recurse (if needed). if isinstance(lowerCamelCase , lowerCamelCase ): if msg is not None: print(lowerCamelCase ) for k in val.keys(): recursive_print(lowerCamelCase , val[k] , spaces + 2 ) elif isinstance(lowerCamelCase , torch.Tensor ): print(lowerCamelCase , ':' , val.size() ) else: print(lowerCamelCase , ':' , lowerCamelCase ) def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : List[str] , lowerCamelCase : List[Any] , lowerCamelCase : Dict , lowerCamelCase : Tuple ): # Permutes layout of param tensor to [num_splits * num_heads * hidden_size, :] # for compatibility with later versions of NVIDIA Megatron-LM. # The inverse operation is performed inside Megatron-LM to read checkpoints: # https://github.com/NVIDIA/Megatron-LM/blob/v2.4/megatron/checkpointing.py#L209 # If param is the weight tensor of the self-attention block, the returned tensor # will have to be transposed one more time to be read by HuggingFace GPT2. lowerCAmelCase = param.size() if checkpoint_version == 1.0: # version 1.0 stores [num_heads * hidden_size * num_splits, :] lowerCAmelCase = (num_heads, hidden_size, num_splits) + input_shape[1:] lowerCAmelCase = param.view(*lowerCamelCase ) lowerCAmelCase = param.transpose(0 , 2 ) lowerCAmelCase = param.transpose(1 , 2 ).contiguous() elif checkpoint_version >= 2.0: # other versions store [num_heads * num_splits * hidden_size, :] lowerCAmelCase = (num_heads, num_splits, hidden_size) + input_shape[1:] lowerCAmelCase = param.view(*lowerCamelCase ) lowerCAmelCase = param.transpose(0 , 1 ).contiguous() lowerCAmelCase = param.view(*lowerCamelCase ) return param def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Optional[int] , lowerCamelCase : str ): # The converted output model. lowerCAmelCase = {} # old versions did not store training args lowerCAmelCase = input_state_dict.get('args' , lowerCamelCase ) if ds_args is not None: # do not make the user write a config file when the exact dimensions/sizes are already in the checkpoint # from pprint import pprint # pprint(vars(ds_args)) lowerCAmelCase = ds_args.padded_vocab_size lowerCAmelCase = ds_args.max_position_embeddings lowerCAmelCase = ds_args.hidden_size lowerCAmelCase = ds_args.num_layers lowerCAmelCase = ds_args.num_attention_heads lowerCAmelCase = ds_args.ffn_hidden_size # pprint(config) # The number of heads. lowerCAmelCase = config.n_head # The hidden_size per head. lowerCAmelCase = config.n_embd // config.n_head # Megatron-LM checkpoint version if "checkpoint_version" in input_state_dict.keys(): lowerCAmelCase = input_state_dict['checkpoint_version'] else: lowerCAmelCase = 0.0 # The model. lowerCAmelCase = input_state_dict['model'] # The language model. lowerCAmelCase = model['language_model'] # The embeddings. lowerCAmelCase = lm['embedding'] # The word embeddings. lowerCAmelCase = embeddings['word_embeddings']['weight'] # Truncate the embedding table to vocab_size rows. lowerCAmelCase = word_embeddings[: config.vocab_size, :] lowerCAmelCase = word_embeddings # The position embeddings. lowerCAmelCase = embeddings['position_embeddings']['weight'] # Read the causal mask dimension (seqlen). [max_sequence_length, hidden_size] lowerCAmelCase = pos_embeddings.size(0 ) if n_positions != config.n_positions: raise ValueError( f'''pos_embeddings.max_sequence_length={n_positions} and config.n_positions={config.n_positions} don\'t match''' ) # Store the position embeddings. lowerCAmelCase = pos_embeddings # The transformer. lowerCAmelCase = lm['transformer'] if 'transformer' in lm.keys() else lm['encoder'] # The regex to extract layer names. lowerCAmelCase = re.compile(R'layers\.(\d+)\.([a-z0-9_.]+)\.([a-z]+)' ) # The simple map of names for "automated" rules. lowerCAmelCase = { 'attention.dense': '.attn.c_proj.', 'self_attention.dense': '.attn.c_proj.', 'mlp.dense_h_to_4h': '.mlp.c_fc.', 'mlp.dense_4h_to_h': '.mlp.c_proj.', } # Extract the layers. for key, val in transformer.items(): # Match the name. lowerCAmelCase = layer_re.match(lowerCamelCase ) # Stop if that's not a layer if m is None: break # The index of the layer. lowerCAmelCase = int(m.group(1 ) ) # The name of the operation. lowerCAmelCase = m.group(2 ) # Is it a weight or a bias? lowerCAmelCase = m.group(3 ) # The name of the layer. lowerCAmelCase = f'''transformer.h.{layer_idx}''' # For layernorm(s), simply store the layer norm. if op_name.endswith('layernorm' ): lowerCAmelCase = 'ln_1' if op_name.startswith('input' ) else 'ln_2' lowerCAmelCase = val # Transpose the QKV matrix. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "weight": # Insert a tensor of 1x1xDxD bias. lowerCAmelCase = torch.tril(torch.ones((n_positions, n_positions) , dtype=torch.floataa ) ).view( 1 , 1 , lowerCamelCase , lowerCamelCase ) lowerCAmelCase = causal_mask # Insert a "dummy" tensor for masked_bias. lowerCAmelCase = torch.tensor(-1e4 , dtype=torch.floataa ) lowerCAmelCase = masked_bias lowerCAmelCase = fix_query_key_value_ordering(lowerCamelCase , lowerCamelCase , 3 , lowerCamelCase , lowerCamelCase ) # Megatron stores (3*D) x D but transformers-GPT2 expects D x 3*D. lowerCAmelCase = out_val.transpose(0 , 1 ).contiguous() # Store. lowerCAmelCase = out_val # Transpose the bias. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "bias": lowerCAmelCase = fix_query_key_value_ordering(lowerCamelCase , lowerCamelCase , 3 , lowerCamelCase , lowerCamelCase ) # Store. No change of shape. lowerCAmelCase = out_val # Transpose the weights. elif weight_or_bias == "weight": lowerCAmelCase = megatron_to_transformers[op_name] lowerCAmelCase = val.transpose(0 , 1 ) # Copy the bias. elif weight_or_bias == "bias": lowerCAmelCase = megatron_to_transformers[op_name] lowerCAmelCase = val # DEBUG. assert config.n_layer == layer_idx + 1 # The final layernorm. lowerCAmelCase = transformer['final_layernorm.weight'] lowerCAmelCase = transformer['final_layernorm.bias'] # For LM head, transformers' wants the matrix to weight embeddings. lowerCAmelCase = word_embeddings # It should be done! return output_state_dict def a_ ( ): # Create the argument parser. lowerCAmelCase = argparse.ArgumentParser() parser.add_argument('--print-checkpoint-structure' , action='store_true' ) parser.add_argument( 'path_to_checkpoint' , type=lowerCamelCase , help='Path to the checkpoint file (.zip archive or direct .pt file)' , ) parser.add_argument( '--config_file' , default='' , type=lowerCamelCase , help='An optional config json file describing the pre-trained model.' , ) lowerCAmelCase = parser.parse_args() # Extract the basename. lowerCAmelCase = os.path.dirname(args.path_to_checkpoint ) # Load the model. # the .zip is very optional, let's keep it for backward compatibility print(f'''Extracting PyTorch state dictionary from {args.path_to_checkpoint}''' ) if args.path_to_checkpoint.endswith('.zip' ): with zipfile.ZipFile(args.path_to_checkpoint , 'r' ) as checkpoint: with checkpoint.open('release/mp_rank_00/model_optim_rng.pt' ) as pytorch_dict: lowerCAmelCase = torch.load(lowerCamelCase , map_location='cpu' ) else: lowerCAmelCase = torch.load(args.path_to_checkpoint , map_location='cpu' ) lowerCAmelCase = input_state_dict.get('args' , lowerCamelCase ) # Read the config, or default to the model released by NVIDIA. if args.config_file == "": if ds_args is not None: if ds_args.bias_gelu_fusion: lowerCAmelCase = 'gelu_fast' elif ds_args.openai_gelu: lowerCAmelCase = 'gelu_new' else: lowerCAmelCase = 'gelu' else: # in the very early days this used to be "gelu_new" lowerCAmelCase = 'gelu_new' # Spell out all parameters in case the defaults change. lowerCAmelCase = GPTaConfig( vocab_size=50257 , n_positions=1024 , n_embd=1024 , n_layer=24 , n_head=16 , n_inner=4096 , activation_function=lowerCamelCase , resid_pdrop=0.1 , embd_pdrop=0.1 , attn_pdrop=0.1 , layer_norm_epsilon=1e-5 , initializer_range=0.02 , summary_type='cls_index' , summary_use_proj=lowerCamelCase , summary_activation=lowerCamelCase , summary_proj_to_labels=lowerCamelCase , summary_first_dropout=0.1 , scale_attn_weights=lowerCamelCase , use_cache=lowerCamelCase , bos_token_id=50256 , eos_token_id=50256 , ) else: lowerCAmelCase = GPTaConfig.from_json_file(args.config_file ) lowerCAmelCase = ['GPT2LMHeadModel'] # Convert. print('Converting' ) lowerCAmelCase = convert_megatron_checkpoint(lowerCamelCase , lowerCamelCase , lowerCamelCase ) # Print the structure of converted state dict. if args.print_checkpoint_structure: recursive_print(lowerCamelCase , lowerCamelCase ) # Add tokenizer class info to config # see https://github.com/huggingface/transformers/issues/13906) if ds_args is not None: lowerCAmelCase = ds_args.tokenizer_type if tokenizer_type == "GPT2BPETokenizer": lowerCAmelCase = 'gpt2' elif tokenizer_type == "PretrainedFromHF": lowerCAmelCase = ds_args.tokenizer_name_or_path else: raise ValueError(f'''Unrecognized tokenizer_type {tokenizer_type}''' ) else: lowerCAmelCase = 'gpt2' lowerCAmelCase = AutoTokenizer.from_pretrained(lowerCamelCase ) lowerCAmelCase = type(lowerCamelCase ).__name__ lowerCAmelCase = tokenizer_class # Store the config to file. print('Saving config' ) config.save_pretrained(lowerCamelCase ) # Save tokenizer based on args print(f'''Adding {tokenizer_class} tokenizer files''' ) tokenizer.save_pretrained(lowerCamelCase ) # Store the state_dict to file. lowerCAmelCase = os.path.join(lowerCamelCase , 'pytorch_model.bin' ) print(f'''Saving checkpoint to "{output_checkpoint_file}"''' ) torch.save(lowerCamelCase , lowerCamelCase ) #################################################################################################### if __name__ == "__main__": main() ####################################################################################################
4
1
'''simple docstring''' from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. __snake_case =200 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. __snake_case =50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. __snake_case =0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 1_000)) def a_ ( lowerCamelCase : str , lowerCamelCase : str ): lowerCAmelCase = len([g for position, g in enumerate(lowerCamelCase ) if g == main_target[position]] ) return (item, float(lowerCamelCase )) def a_ ( lowerCamelCase : str , lowerCamelCase : str ): lowerCAmelCase = random.randint(0 , len(lowerCamelCase ) - 1 ) lowerCAmelCase = parent_a[:random_slice] + parent_a[random_slice:] lowerCAmelCase = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def a_ ( lowerCamelCase : str , lowerCamelCase : list[str] ): lowerCAmelCase = list(lowerCamelCase ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: lowerCAmelCase = random.choice(lowerCamelCase ) return "".join(lowerCamelCase ) def a_ ( lowerCamelCase : tuple[str, float] , lowerCamelCase : list[tuple[str, float]] , lowerCamelCase : list[str] , ): lowerCAmelCase = [] # Generate more children proportionally to the fitness score. lowerCAmelCase = int(parent_a[1] * 100 ) + 1 lowerCAmelCase = 10 if child_n >= 10 else child_n for _ in range(lowerCamelCase ): lowerCAmelCase = population_score[random.randint(0 , lowerCamelCase )][0] lowerCAmelCase , lowerCAmelCase = crossover(parent_a[0] , lowerCamelCase ) # Append new string to the population list. pop.append(mutate(lowerCamelCase , lowerCamelCase ) ) pop.append(mutate(lowerCamelCase , lowerCamelCase ) ) return pop def a_ ( lowerCamelCase : str , lowerCamelCase : list[str] , lowerCamelCase : bool = True ): # Verify if N_POPULATION is bigger than N_SELECTED if N_POPULATION < N_SELECTED: lowerCAmelCase = f'''{N_POPULATION} must be bigger than {N_SELECTED}''' raise ValueError(lowerCamelCase ) # Verify that the target contains no genes besides the ones inside genes variable. lowerCAmelCase = sorted({c for c in target if c not in genes} ) if not_in_genes_list: lowerCAmelCase = f'''{not_in_genes_list} is not in genes list, evolution cannot converge''' raise ValueError(lowerCamelCase ) # Generate random starting population. lowerCAmelCase = [] for _ in range(lowerCamelCase ): population.append(''.join([random.choice(lowerCamelCase ) for i in range(len(lowerCamelCase ) )] ) ) # Just some logs to know what the algorithms is doing. lowerCAmelCase , lowerCAmelCase = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(lowerCamelCase ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. lowerCAmelCase = [evaluate(lowerCamelCase , lowerCamelCase ) for item in population] # Check if there is a matching evolution. lowerCAmelCase = sorted(lowerCamelCase , key=lambda lowerCamelCase : x[1] , reverse=lowerCamelCase ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( f'''\nGeneration: {generation}''' f'''\nTotal Population:{total_population}''' f'''\nBest score: {population_score[0][1]}''' f'''\nBest string: {population_score[0][0]}''' ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. lowerCAmelCase = population[: int(N_POPULATION / 3 )] population.clear() population.extend(lowerCamelCase ) # Normalize population score to be between 0 and 1. lowerCAmelCase = [ (item, score / len(lowerCamelCase )) for item, score in population_score ] # This is selection for i in range(lowerCamelCase ): population.extend(select(population_score[int(lowerCamelCase )] , lowerCamelCase , lowerCamelCase ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(lowerCamelCase ) > N_POPULATION: break if __name__ == "__main__": __snake_case =( """This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!""" ) __snake_case =list( """ ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm""" """nopqrstuvwxyz.,;!?+-*#@^'èéòà€ù=)(&%$£/\\""" ) __snake_case , __snake_case , __snake_case =basic(target_str, genes_list) print( F'''\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}''' )
4
'''simple docstring''' from __future__ import annotations from typing import Any class UpperCAmelCase_ : def __init__( self : Optional[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : float = 0 ) -> None: lowerCAmelCase , lowerCAmelCase = row, column lowerCAmelCase = [[default_value for c in range(UpperCAmelCase__ )] for r in range(UpperCAmelCase__ )] def __str__( self : List[str] ) -> str: lowerCAmelCase = F'''Matrix consist of {self.row} rows and {self.column} columns\n''' # Make string identifier lowerCAmelCase = 0 for row_vector in self.array: for obj in row_vector: lowerCAmelCase = max(UpperCAmelCase__ , len(str(UpperCAmelCase__ ) ) ) lowerCAmelCase = F'''%{max_element_length}s''' # Make string and return def single_line(UpperCAmelCase__ : list[float] ) -> str: nonlocal string_format_identifier lowerCAmelCase = '[' line += ", ".join(string_format_identifier % (obj,) for obj in row_vector ) line += "]" return line s += "\n".join(single_line(UpperCAmelCase__ ) for row_vector in self.array ) return s def __repr__( self : List[str] ) -> str: return str(self ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : tuple[int, int] ) -> bool: if not (isinstance(UpperCAmelCase__ , (list, tuple) ) and len(UpperCAmelCase__ ) == 2): return False elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False else: return True def __getitem__( self : Any , UpperCAmelCase__ : tuple[int, int] ) -> Any: assert self.validate_indicies(UpperCAmelCase__ ) return self.array[loc[0]][loc[1]] def __setitem__( self : Dict , UpperCAmelCase__ : tuple[int, int] , UpperCAmelCase__ : float ) -> None: assert self.validate_indicies(UpperCAmelCase__ ) lowerCAmelCase = value def __add__( self : Any , UpperCAmelCase__ : Matrix ) -> Matrix: assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) assert self.row == another.row and self.column == another.column # Add lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] + another[r, c] return result def __neg__( self : int ) -> Matrix: lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = -self[r, c] return result def __sub__( self : str , UpperCAmelCase__ : Matrix ) -> Matrix: return self + (-another) def __mul__( self : str , UpperCAmelCase__ : int | float | Matrix ) -> Matrix: if isinstance(UpperCAmelCase__ , (int, float) ): # Scalar multiplication lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] * another return result elif isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): # Matrix multiplication assert self.column == another.row lowerCAmelCase = Matrix(self.row , another.column ) for r in range(self.row ): for c in range(another.column ): for i in range(self.column ): result[r, c] += self[r, i] * another[i, c] return result else: lowerCAmelCase = F'''Unsupported type given for another ({type(UpperCAmelCase__ )})''' raise TypeError(UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[Any] ) -> Matrix: lowerCAmelCase = Matrix(self.column , self.row ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] return result def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : Matrix , UpperCAmelCase__ : Matrix ) -> Any: assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) and isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) assert self.row == self.column == u.row == v.row # u, v should be column vector assert u.column == v.column == 1 # u, v should be column vector # Calculate lowerCAmelCase = v.transpose() lowerCAmelCase = (v_t * self * u)[0, 0] + 1 if numerator_factor == 0: return None # It's not invertable return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor)) # Testing if __name__ == "__main__": def a_ ( ): # a^(-1) lowerCAmelCase = Matrix(3 , 3 , 0 ) for i in range(3 ): lowerCAmelCase = 1 print(f'''a^(-1) is {ainv}''' ) # u, v lowerCAmelCase = Matrix(3 , 1 , 0 ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = 1, 2, -3 lowerCAmelCase = Matrix(3 , 1 , 0 ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = 4, -2, 5 print(f'''u is {u}''' ) print(f'''v is {v}''' ) print(f'''uv^T is {u * v.transpose()}''' ) # Sherman Morrison print(f'''(a + uv^T)^(-1) is {ainv.sherman_morrison(lowerCamelCase , lowerCamelCase )}''' ) def a_ ( ): import doctest doctest.testmod() testa()
4
1
'''simple docstring''' import warnings from ...utils import logging from .image_processing_deformable_detr import DeformableDetrImageProcessor __snake_case =logging.get_logger(__name__) class UpperCAmelCase_ ( __lowercase ): def __init__( self : str , *UpperCAmelCase__ : List[str] , **UpperCAmelCase__ : Any ) -> None: warnings.warn( 'The class DeformableDetrFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use DeformableDetrImageProcessor instead.' , UpperCAmelCase__ , ) super().__init__(*UpperCAmelCase__ , **UpperCAmelCase__ )
4
'''simple docstring''' class UpperCAmelCase_ : def __init__( self : List[str] , UpperCAmelCase__ : list[int] ) -> None: lowerCAmelCase = len(UpperCAmelCase__ ) lowerCAmelCase = [0] * len_array if len_array > 0: lowerCAmelCase = array[0] for i in range(1 , UpperCAmelCase__ ): lowerCAmelCase = self.prefix_sum[i - 1] + array[i] def __UpperCAmelCase ( self : Optional[Any] , UpperCAmelCase__ : int , UpperCAmelCase__ : int ) -> int: if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def __UpperCAmelCase ( self : int , UpperCAmelCase__ : int ) -> bool: lowerCAmelCase = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(UpperCAmelCase__ ) return False if __name__ == "__main__": import doctest doctest.testmod()
4
1
'''simple docstring''' from __future__ import annotations def a_ ( lowerCamelCase : list[int] , lowerCamelCase : int ): lowerCAmelCase = 0 lowerCAmelCase = len(lowerCamelCase ) - 1 while i < j: if nums[i] + nums[j] == target: return [i, j] elif nums[i] + nums[j] < target: lowerCAmelCase = i + 1 else: lowerCAmelCase = j - 1 return [] if __name__ == "__main__": import doctest doctest.testmod() print(F'''{two_pointer([2, 7, 11, 15], 9) = }''')
4
'''simple docstring''' def a_ ( lowerCamelCase : Optional[Any] ): return [ { 0: [1, 2], 1: [0, 2], 2: [0, 1, 3, 5], 3: [2, 4], 4: [3], 5: [2, 6, 8], 6: [5, 7], 7: [6, 8], 8: [5, 7], }, { 0: [6], 1: [9], 2: [4, 5], 3: [4], 4: [2, 3], 5: [2], 6: [0, 7], 7: [6], 8: [], 9: [1], }, { 0: [4], 1: [6], 2: [], 3: [5, 6, 7], 4: [0, 6], 5: [3, 8, 9], 6: [1, 3, 4, 7], 7: [3, 6, 8, 9], 8: [5, 7], 9: [5, 7], }, { 0: [1, 3], 1: [0, 2, 4], 2: [1, 3, 4], 3: [0, 2, 4], 4: [1, 2, 3], }, ][index] def a_ ( lowerCamelCase : dict[int, list[int]] ): lowerCAmelCase = 0 lowerCAmelCase = len(lowerCamelCase ) # No of vertices in graph lowerCAmelCase = [0] * n lowerCAmelCase = [False] * n def dfs(lowerCamelCase : Tuple , lowerCamelCase : str , lowerCamelCase : Dict , lowerCamelCase : str ): lowerCAmelCase = True lowerCAmelCase = id_ id_ += 1 for to in graph[at]: if to == parent: pass elif not visited[to]: dfs(lowerCamelCase , lowerCamelCase , lowerCamelCase , id_ ) lowerCAmelCase = min(low[at] , low[to] ) if id_ <= low[to]: bridges.append((at, to) if at < to else (to, at) ) else: # This edge is a back edge and cannot be a bridge lowerCAmelCase = min(low[at] , low[to] ) lowerCAmelCase = [] for i in range(lowerCamelCase ): if not visited[i]: dfs(lowerCamelCase , -1 , lowerCamelCase , id_ ) return bridges if __name__ == "__main__": import doctest doctest.testmod()
4
1
'''simple docstring''' import argparse import os import torch from diffusers import ( CMStochasticIterativeScheduler, ConsistencyModelPipeline, UNetaDModel, ) __snake_case ={ """sample_size""": 32, """in_channels""": 3, """out_channels""": 3, """layers_per_block""": 2, """num_class_embeds""": 1_000, """block_out_channels""": [32, 64], """attention_head_dim""": 8, """down_block_types""": [ """ResnetDownsampleBlock2D""", """AttnDownBlock2D""", ], """up_block_types""": [ """AttnUpBlock2D""", """ResnetUpsampleBlock2D""", ], """resnet_time_scale_shift""": """scale_shift""", """upsample_type""": """resnet""", """downsample_type""": """resnet""", } __snake_case ={ """sample_size""": 64, """in_channels""": 3, """out_channels""": 3, """layers_per_block""": 3, """num_class_embeds""": 1_000, """block_out_channels""": [192, 192 * 2, 192 * 3, 192 * 4], """attention_head_dim""": 64, """down_block_types""": [ """ResnetDownsampleBlock2D""", """AttnDownBlock2D""", """AttnDownBlock2D""", """AttnDownBlock2D""", ], """up_block_types""": [ """AttnUpBlock2D""", """AttnUpBlock2D""", """AttnUpBlock2D""", """ResnetUpsampleBlock2D""", ], """resnet_time_scale_shift""": """scale_shift""", """upsample_type""": """resnet""", """downsample_type""": """resnet""", } __snake_case ={ """sample_size""": 256, """in_channels""": 3, """out_channels""": 3, """layers_per_block""": 2, """num_class_embeds""": None, """block_out_channels""": [256, 256, 256 * 2, 256 * 2, 256 * 4, 256 * 4], """attention_head_dim""": 64, """down_block_types""": [ """ResnetDownsampleBlock2D""", """ResnetDownsampleBlock2D""", """ResnetDownsampleBlock2D""", """AttnDownBlock2D""", """AttnDownBlock2D""", """AttnDownBlock2D""", ], """up_block_types""": [ """AttnUpBlock2D""", """AttnUpBlock2D""", """AttnUpBlock2D""", """ResnetUpsampleBlock2D""", """ResnetUpsampleBlock2D""", """ResnetUpsampleBlock2D""", ], """resnet_time_scale_shift""": """default""", """upsample_type""": """resnet""", """downsample_type""": """resnet""", } __snake_case ={ """num_train_timesteps""": 40, """sigma_min""": 0.0_0_2, """sigma_max""": 8_0.0, } __snake_case ={ """num_train_timesteps""": 201, """sigma_min""": 0.0_0_2, """sigma_max""": 8_0.0, } __snake_case ={ """num_train_timesteps""": 151, """sigma_min""": 0.0_0_2, """sigma_max""": 8_0.0, } def a_ ( lowerCamelCase : Tuple ): if isinstance(lowerCamelCase , lowerCamelCase ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise argparse.ArgumentTypeError('boolean value expected' ) def a_ ( lowerCamelCase : Tuple , lowerCamelCase : List[Any] , lowerCamelCase : List[Any] , lowerCamelCase : str , lowerCamelCase : Union[str, Any]=False ): lowerCAmelCase = checkpoint[f'''{old_prefix}.in_layers.0.weight'''] lowerCAmelCase = checkpoint[f'''{old_prefix}.in_layers.0.bias'''] lowerCAmelCase = checkpoint[f'''{old_prefix}.in_layers.2.weight'''] lowerCAmelCase = checkpoint[f'''{old_prefix}.in_layers.2.bias'''] lowerCAmelCase = checkpoint[f'''{old_prefix}.emb_layers.1.weight'''] lowerCAmelCase = checkpoint[f'''{old_prefix}.emb_layers.1.bias'''] lowerCAmelCase = checkpoint[f'''{old_prefix}.out_layers.0.weight'''] lowerCAmelCase = checkpoint[f'''{old_prefix}.out_layers.0.bias'''] lowerCAmelCase = checkpoint[f'''{old_prefix}.out_layers.3.weight'''] lowerCAmelCase = checkpoint[f'''{old_prefix}.out_layers.3.bias'''] if has_skip: lowerCAmelCase = checkpoint[f'''{old_prefix}.skip_connection.weight'''] lowerCAmelCase = checkpoint[f'''{old_prefix}.skip_connection.bias'''] return new_checkpoint def a_ ( lowerCamelCase : Any , lowerCamelCase : List[Any] , lowerCamelCase : List[Any] , lowerCamelCase : Optional[int] , lowerCamelCase : Optional[int]=None ): lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = checkpoint[f'''{old_prefix}.qkv.weight'''].chunk(3 , dim=0 ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = checkpoint[f'''{old_prefix}.qkv.bias'''].chunk(3 , dim=0 ) lowerCAmelCase = checkpoint[f'''{old_prefix}.norm.weight'''] lowerCAmelCase = checkpoint[f'''{old_prefix}.norm.bias'''] lowerCAmelCase = weight_q.squeeze(-1 ).squeeze(-1 ) lowerCAmelCase = bias_q.squeeze(-1 ).squeeze(-1 ) lowerCAmelCase = weight_k.squeeze(-1 ).squeeze(-1 ) lowerCAmelCase = bias_k.squeeze(-1 ).squeeze(-1 ) lowerCAmelCase = weight_v.squeeze(-1 ).squeeze(-1 ) lowerCAmelCase = bias_v.squeeze(-1 ).squeeze(-1 ) lowerCAmelCase = ( checkpoint[f'''{old_prefix}.proj_out.weight'''].squeeze(-1 ).squeeze(-1 ) ) lowerCAmelCase = checkpoint[f'''{old_prefix}.proj_out.bias'''].squeeze(-1 ).squeeze(-1 ) return new_checkpoint def a_ ( lowerCamelCase : str , lowerCamelCase : List[str] ): lowerCAmelCase = torch.load(lowerCamelCase , map_location='cpu' ) lowerCAmelCase = {} lowerCAmelCase = checkpoint['time_embed.0.weight'] lowerCAmelCase = checkpoint['time_embed.0.bias'] lowerCAmelCase = checkpoint['time_embed.2.weight'] lowerCAmelCase = checkpoint['time_embed.2.bias'] if unet_config["num_class_embeds"] is not None: lowerCAmelCase = checkpoint['label_emb.weight'] lowerCAmelCase = checkpoint['input_blocks.0.0.weight'] lowerCAmelCase = checkpoint['input_blocks.0.0.bias'] lowerCAmelCase = unet_config['down_block_types'] lowerCAmelCase = unet_config['layers_per_block'] lowerCAmelCase = unet_config['attention_head_dim'] lowerCAmelCase = unet_config['block_out_channels'] lowerCAmelCase = 1 lowerCAmelCase = channels_list[0] for i, layer_type in enumerate(lowerCamelCase ): lowerCAmelCase = channels_list[i] lowerCAmelCase = current_channels != prev_channels if layer_type == "ResnetDownsampleBlock2D": for j in range(lowerCamelCase ): lowerCAmelCase = f'''down_blocks.{i}.resnets.{j}''' lowerCAmelCase = f'''input_blocks.{current_layer}.0''' lowerCAmelCase = True if j == 0 and downsample_block_has_skip else False lowerCAmelCase = convert_resnet(lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , has_skip=lowerCamelCase ) current_layer += 1 elif layer_type == "AttnDownBlock2D": for j in range(lowerCamelCase ): lowerCAmelCase = f'''down_blocks.{i}.resnets.{j}''' lowerCAmelCase = f'''input_blocks.{current_layer}.0''' lowerCAmelCase = True if j == 0 and downsample_block_has_skip else False lowerCAmelCase = convert_resnet(lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , has_skip=lowerCamelCase ) lowerCAmelCase = f'''down_blocks.{i}.attentions.{j}''' lowerCAmelCase = f'''input_blocks.{current_layer}.1''' lowerCAmelCase = convert_attention( lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase ) current_layer += 1 if i != len(lowerCamelCase ) - 1: lowerCAmelCase = f'''down_blocks.{i}.downsamplers.0''' lowerCAmelCase = f'''input_blocks.{current_layer}.0''' lowerCAmelCase = convert_resnet(lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase ) current_layer += 1 lowerCAmelCase = current_channels # hardcoded the mid-block for now lowerCAmelCase = 'mid_block.resnets.0' lowerCAmelCase = 'middle_block.0' lowerCAmelCase = convert_resnet(lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase ) lowerCAmelCase = 'mid_block.attentions.0' lowerCAmelCase = 'middle_block.1' lowerCAmelCase = convert_attention(lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase ) lowerCAmelCase = 'mid_block.resnets.1' lowerCAmelCase = 'middle_block.2' lowerCAmelCase = convert_resnet(lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase ) lowerCAmelCase = 0 lowerCAmelCase = unet_config['up_block_types'] for i, layer_type in enumerate(lowerCamelCase ): if layer_type == "ResnetUpsampleBlock2D": for j in range(layers_per_block + 1 ): lowerCAmelCase = f'''up_blocks.{i}.resnets.{j}''' lowerCAmelCase = f'''output_blocks.{current_layer}.0''' lowerCAmelCase = convert_resnet(lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , has_skip=lowerCamelCase ) current_layer += 1 if i != len(lowerCamelCase ) - 1: lowerCAmelCase = f'''up_blocks.{i}.upsamplers.0''' lowerCAmelCase = f'''output_blocks.{current_layer-1}.1''' lowerCAmelCase = convert_resnet(lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase ) elif layer_type == "AttnUpBlock2D": for j in range(layers_per_block + 1 ): lowerCAmelCase = f'''up_blocks.{i}.resnets.{j}''' lowerCAmelCase = f'''output_blocks.{current_layer}.0''' lowerCAmelCase = convert_resnet(lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , has_skip=lowerCamelCase ) lowerCAmelCase = f'''up_blocks.{i}.attentions.{j}''' lowerCAmelCase = f'''output_blocks.{current_layer}.1''' lowerCAmelCase = convert_attention( lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase ) current_layer += 1 if i != len(lowerCamelCase ) - 1: lowerCAmelCase = f'''up_blocks.{i}.upsamplers.0''' lowerCAmelCase = f'''output_blocks.{current_layer-1}.2''' lowerCAmelCase = convert_resnet(lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase ) lowerCAmelCase = checkpoint['out.0.weight'] lowerCAmelCase = checkpoint['out.0.bias'] lowerCAmelCase = checkpoint['out.2.weight'] lowerCAmelCase = checkpoint['out.2.bias'] return new_checkpoint if __name__ == "__main__": __snake_case =argparse.ArgumentParser() parser.add_argument("""--unet_path""", default=None, type=str, required=True, help="""Path to the unet.pt to convert.""") parser.add_argument( """--dump_path""", default=None, type=str, required=True, help="""Path to output the converted UNet model.""" ) parser.add_argument("""--class_cond""", default=True, type=str, help="""Whether the model is class-conditional.""") __snake_case =parser.parse_args() __snake_case =strabool(args.class_cond) __snake_case =os.path.basename(args.unet_path) print(F'''Checkpoint: {ckpt_name}''') # Get U-Net config if "imagenet64" in ckpt_name: __snake_case =IMAGENET_64_UNET_CONFIG elif "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)): __snake_case =LSUN_256_UNET_CONFIG elif "test" in ckpt_name: __snake_case =TEST_UNET_CONFIG else: raise ValueError(F'''Checkpoint type {ckpt_name} is not currently supported.''') if not args.class_cond: __snake_case =None __snake_case =con_pt_to_diffuser(args.unet_path, unet_config) __snake_case =UNetaDModel(**unet_config) image_unet.load_state_dict(converted_unet_ckpt) # Get scheduler config if "cd" in ckpt_name or "test" in ckpt_name: __snake_case =CD_SCHEDULER_CONFIG elif "ct" in ckpt_name and "imagenet64" in ckpt_name: __snake_case =CT_IMAGENET_64_SCHEDULER_CONFIG elif "ct" in ckpt_name and "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)): __snake_case =CT_LSUN_256_SCHEDULER_CONFIG else: raise ValueError(F'''Checkpoint type {ckpt_name} is not currently supported.''') __snake_case =CMStochasticIterativeScheduler(**scheduler_config) __snake_case =ConsistencyModelPipeline(unet=image_unet, scheduler=cm_scheduler) consistency_model.save_pretrained(args.dump_path)
4
'''simple docstring''' import argparse from collections import OrderedDict from pathlib import Path import requests import torch from PIL import Image from transformers import GLPNConfig, GLPNForDepthEstimation, GLPNImageProcessor from transformers.utils import logging logging.set_verbosity_info() __snake_case =logging.get_logger(__name__) def a_ ( lowerCamelCase : Any ): lowerCAmelCase = OrderedDict() for key, value in state_dict.items(): if key.startswith('module.encoder' ): lowerCAmelCase = key.replace('module.encoder' , 'glpn.encoder' ) if key.startswith('module.decoder' ): lowerCAmelCase = key.replace('module.decoder' , 'decoder.stages' ) if "patch_embed" in key: # replace for example patch_embed1 by patch_embeddings.0 lowerCAmelCase = key[key.find('patch_embed' ) + len('patch_embed' )] lowerCAmelCase = key.replace(f'''patch_embed{idx}''' , f'''patch_embeddings.{int(lowerCamelCase )-1}''' ) if "norm" in key: lowerCAmelCase = key.replace('norm' , 'layer_norm' ) if "glpn.encoder.layer_norm" in key: # replace for example layer_norm1 by layer_norm.0 lowerCAmelCase = key[key.find('glpn.encoder.layer_norm' ) + len('glpn.encoder.layer_norm' )] lowerCAmelCase = key.replace(f'''layer_norm{idx}''' , f'''layer_norm.{int(lowerCamelCase )-1}''' ) if "layer_norm1" in key: lowerCAmelCase = key.replace('layer_norm1' , 'layer_norm_1' ) if "layer_norm2" in key: lowerCAmelCase = key.replace('layer_norm2' , 'layer_norm_2' ) if "block" in key: # replace for example block1 by block.0 lowerCAmelCase = key[key.find('block' ) + len('block' )] lowerCAmelCase = key.replace(f'''block{idx}''' , f'''block.{int(lowerCamelCase )-1}''' ) if "attn.q" in key: lowerCAmelCase = key.replace('attn.q' , 'attention.self.query' ) if "attn.proj" in key: lowerCAmelCase = key.replace('attn.proj' , 'attention.output.dense' ) if "attn" in key: lowerCAmelCase = key.replace('attn' , 'attention.self' ) if "fc1" in key: lowerCAmelCase = key.replace('fc1' , 'dense1' ) if "fc2" in key: lowerCAmelCase = key.replace('fc2' , 'dense2' ) if "linear_pred" in key: lowerCAmelCase = key.replace('linear_pred' , 'classifier' ) if "linear_fuse" in key: lowerCAmelCase = key.replace('linear_fuse.conv' , 'linear_fuse' ) lowerCAmelCase = key.replace('linear_fuse.bn' , 'batch_norm' ) if "linear_c" in key: # replace for example linear_c4 by linear_c.3 lowerCAmelCase = key[key.find('linear_c' ) + len('linear_c' )] lowerCAmelCase = key.replace(f'''linear_c{idx}''' , f'''linear_c.{int(lowerCamelCase )-1}''' ) if "bot_conv" in key: lowerCAmelCase = key.replace('bot_conv' , '0.convolution' ) if "skip_conv1" in key: lowerCAmelCase = key.replace('skip_conv1' , '1.convolution' ) if "skip_conv2" in key: lowerCAmelCase = key.replace('skip_conv2' , '2.convolution' ) if "fusion1" in key: lowerCAmelCase = key.replace('fusion1' , '1.fusion' ) if "fusion2" in key: lowerCAmelCase = key.replace('fusion2' , '2.fusion' ) if "fusion3" in key: lowerCAmelCase = key.replace('fusion3' , '3.fusion' ) if "fusion" in key and "conv" in key: lowerCAmelCase = key.replace('conv' , 'convolutional_layer' ) if key.startswith('module.last_layer_depth' ): lowerCAmelCase = key.replace('module.last_layer_depth' , 'head.head' ) lowerCAmelCase = value return new_state_dict def a_ ( lowerCamelCase : List[str] , lowerCamelCase : str ): # for each of the encoder blocks: for i in range(config.num_encoder_blocks ): for j in range(config.depths[i] ): # read in weights + bias of keys and values (which is a single matrix in the original implementation) lowerCAmelCase = state_dict.pop(f'''glpn.encoder.block.{i}.{j}.attention.self.kv.weight''' ) lowerCAmelCase = state_dict.pop(f'''glpn.encoder.block.{i}.{j}.attention.self.kv.bias''' ) # next, add keys and values (in that order) to the state dict lowerCAmelCase = kv_weight[ : config.hidden_sizes[i], : ] lowerCAmelCase = kv_bias[: config.hidden_sizes[i]] lowerCAmelCase = kv_weight[ config.hidden_sizes[i] :, : ] lowerCAmelCase = kv_bias[config.hidden_sizes[i] :] def a_ ( ): lowerCAmelCase = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowerCAmelCase = Image.open(requests.get(lowerCamelCase , stream=lowerCamelCase ).raw ) return image @torch.no_grad() def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Tuple , lowerCamelCase : Optional[Any]=False , lowerCamelCase : List[str]=None ): lowerCAmelCase = GLPNConfig(hidden_sizes=[64, 128, 320, 512] , decoder_hidden_size=64 , depths=[3, 8, 27, 3] ) # load image processor (only resize + rescale) lowerCAmelCase = GLPNImageProcessor() # prepare image lowerCAmelCase = prepare_img() lowerCAmelCase = image_processor(images=lowerCamelCase , return_tensors='pt' ).pixel_values logger.info('Converting model...' ) # load original state dict lowerCAmelCase = torch.load(lowerCamelCase , map_location=torch.device('cpu' ) ) # rename keys lowerCAmelCase = rename_keys(lowerCamelCase ) # key and value matrices need special treatment read_in_k_v(lowerCamelCase , lowerCamelCase ) # create HuggingFace model and load state dict lowerCAmelCase = GLPNForDepthEstimation(lowerCamelCase ) model.load_state_dict(lowerCamelCase ) model.eval() # forward pass lowerCAmelCase = model(lowerCamelCase ) lowerCAmelCase = outputs.predicted_depth # verify output if model_name is not None: if "nyu" in model_name: lowerCAmelCase = torch.tensor( [[4.4_147, 4.0_873, 4.0_673], [3.7_890, 3.2_881, 3.1_525], [3.7_674, 3.5_423, 3.4_913]] ) elif "kitti" in model_name: lowerCAmelCase = torch.tensor( [[3.4_291, 2.7_865, 2.5_151], [3.2_841, 2.7_021, 2.3_502], [3.1_147, 2.4_625, 2.2_481]] ) else: raise ValueError(f'''Unknown model name: {model_name}''' ) lowerCAmelCase = torch.Size([1, 480, 640] ) assert predicted_depth.shape == expected_shape assert torch.allclose(predicted_depth[0, :3, :3] , lowerCamelCase , atol=1e-4 ) print('Looks ok!' ) # finally, push to hub if required if push_to_hub: logger.info('Pushing model and image processor to the hub...' ) model.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=lowerCamelCase , ) image_processor.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=lowerCamelCase , ) if __name__ == "__main__": __snake_case =argparse.ArgumentParser() parser.add_argument( """--checkpoint_path""", default=None, 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 folder to output PyTorch model.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to upload the model to the HuggingFace hub.""" ) parser.add_argument( """--model_name""", default="""glpn-kitti""", type=str, help="""Name of the model in case you're pushing to the hub.""", ) __snake_case =parser.parse_args() convert_glpn_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
4
1
'''simple docstring''' import argparse import os from pathlib import Path import torch from bark.generation import _load_model as _bark_load_model from huggingface_hub import hf_hub_download from transformers import EncodecConfig, EncodecModel, set_seed from transformers.models.bark.configuration_bark import ( BarkCoarseConfig, BarkConfig, BarkFineConfig, BarkSemanticConfig, ) from transformers.models.bark.generation_configuration_bark import ( BarkCoarseGenerationConfig, BarkFineGenerationConfig, BarkGenerationConfig, BarkSemanticGenerationConfig, ) from transformers.models.bark.modeling_bark import BarkCoarseModel, BarkFineModel, BarkModel, BarkSemanticModel from transformers.utils import logging logging.set_verbosity_info() __snake_case =logging.get_logger(__name__) set_seed(770) __snake_case ={ """c_attn""": """att_proj""", """c_proj""": """out_proj""", """c_fc""": """in_proj""", """transformer.""": """""", """h.""": """layers.""", """ln_1""": """layernorm_1""", """ln_2""": """layernorm_2""", """ln_f""": """layernorm_final""", """wpe""": """position_embeds_layer""", """wte""": """input_embeds_layer""", } __snake_case ={ """text_small""": { """repo_id""": """suno/bark""", """file_name""": """text.pt""", }, """coarse_small""": { """repo_id""": """suno/bark""", """file_name""": """coarse.pt""", }, """fine_small""": { """repo_id""": """suno/bark""", """file_name""": """fine.pt""", }, """text""": { """repo_id""": """suno/bark""", """file_name""": """text_2.pt""", }, """coarse""": { """repo_id""": """suno/bark""", """file_name""": """coarse_2.pt""", }, """fine""": { """repo_id""": """suno/bark""", """file_name""": """fine_2.pt""", }, } __snake_case =os.path.dirname(os.path.abspath(__file__)) __snake_case =os.path.join(os.path.expanduser("""~"""), """.cache""") __snake_case =os.path.join(os.getenv("""XDG_CACHE_HOME""", default_cache_dir), """suno""", """bark_v0""") def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Optional[Any]=False ): lowerCAmelCase = model_type if use_small: key += "_small" return os.path.join(lowerCamelCase , REMOTE_MODEL_PATHS[key]['file_name'] ) def a_ ( lowerCamelCase : Any , lowerCamelCase : Union[str, Any] ): os.makedirs(lowerCamelCase , exist_ok=lowerCamelCase ) hf_hub_download(repo_id=lowerCamelCase , filename=lowerCamelCase , local_dir=lowerCamelCase ) def a_ ( lowerCamelCase : Tuple , lowerCamelCase : Any , lowerCamelCase : Dict=False , lowerCamelCase : Optional[int]="text" ): if model_type == "text": lowerCAmelCase = BarkSemanticModel lowerCAmelCase = BarkSemanticConfig lowerCAmelCase = BarkSemanticGenerationConfig elif model_type == "coarse": lowerCAmelCase = BarkCoarseModel lowerCAmelCase = BarkCoarseConfig lowerCAmelCase = BarkCoarseGenerationConfig elif model_type == "fine": lowerCAmelCase = BarkFineModel lowerCAmelCase = BarkFineConfig lowerCAmelCase = BarkFineGenerationConfig else: raise NotImplementedError() lowerCAmelCase = f'''{model_type}_small''' if use_small else model_type lowerCAmelCase = REMOTE_MODEL_PATHS[model_key] if not os.path.exists(lowerCamelCase ): logger.info(f'''{model_type} model not found, downloading into `{CACHE_DIR}`.''' ) _download(model_info['repo_id'] , model_info['file_name'] ) lowerCAmelCase = torch.load(lowerCamelCase , map_location=lowerCamelCase ) # this is a hack lowerCAmelCase = checkpoint['model_args'] if "input_vocab_size" not in model_args: lowerCAmelCase = model_args['vocab_size'] lowerCAmelCase = model_args['vocab_size'] del model_args["vocab_size"] # convert Bark model arguments to HF Bark model arguments lowerCAmelCase = model_args.pop('n_head' ) lowerCAmelCase = model_args.pop('n_embd' ) lowerCAmelCase = model_args.pop('n_layer' ) lowerCAmelCase = ConfigClass(**checkpoint['model_args'] ) lowerCAmelCase = ModelClass(config=lowerCamelCase ) lowerCAmelCase = GenerationConfigClass() lowerCAmelCase = model_generation_config lowerCAmelCase = checkpoint['model'] # fixup checkpoint lowerCAmelCase = '_orig_mod.' for k, v in list(state_dict.items() ): if k.startswith(lowerCamelCase ): # replace part of the key with corresponding layer name in HF implementation lowerCAmelCase = k[len(lowerCamelCase ) :] for old_layer_name in new_layer_name_dict: lowerCAmelCase = new_k.replace(lowerCamelCase , new_layer_name_dict[old_layer_name] ) lowerCAmelCase = state_dict.pop(lowerCamelCase ) lowerCAmelCase = set(state_dict.keys() ) - set(model.state_dict().keys() ) lowerCAmelCase = {k for k in extra_keys if not k.endswith('.attn.bias' )} lowerCAmelCase = set(model.state_dict().keys() ) - set(state_dict.keys() ) lowerCAmelCase = {k for k in missing_keys if not k.endswith('.attn.bias' )} if len(lowerCamelCase ) != 0: raise ValueError(f'''extra keys found: {extra_keys}''' ) if len(lowerCamelCase ) != 0: raise ValueError(f'''missing keys: {missing_keys}''' ) model.load_state_dict(lowerCamelCase , strict=lowerCamelCase ) lowerCAmelCase = model.num_parameters(exclude_embeddings=lowerCamelCase ) lowerCAmelCase = checkpoint['best_val_loss'].item() logger.info(f'''model loaded: {round(n_params/1e6 , 1 )}M params, {round(lowerCamelCase , 3 )} loss''' ) model.eval() model.to(lowerCamelCase ) del checkpoint, state_dict return model def a_ ( lowerCamelCase : Tuple , lowerCamelCase : List[str]=False , lowerCamelCase : Union[str, Any]="text" ): if model_type not in ("text", "coarse", "fine"): raise NotImplementedError() lowerCAmelCase = 'cpu' # do conversion on cpu lowerCAmelCase = _get_ckpt_path(lowerCamelCase , use_small=lowerCamelCase ) lowerCAmelCase = _load_model(lowerCamelCase , lowerCamelCase , model_type=lowerCamelCase , use_small=lowerCamelCase ) # load bark initial model lowerCAmelCase = _bark_load_model(lowerCamelCase , 'cpu' , model_type=lowerCamelCase , use_small=lowerCamelCase ) if model_type == "text": lowerCAmelCase = bark_model['model'] if model.num_parameters(exclude_embeddings=lowerCamelCase ) != bark_model.get_num_params(): raise ValueError('initial and new models don\'t have the same number of parameters' ) # check if same output as the bark model lowerCAmelCase = 5 lowerCAmelCase = 10 if model_type in ["text", "coarse"]: lowerCAmelCase = torch.randint(256 , (batch_size, sequence_length) , dtype=torch.int ) lowerCAmelCase = bark_model(lowerCamelCase )[0] lowerCAmelCase = model(lowerCamelCase ) # take last logits lowerCAmelCase = output_new_model_total.logits[:, [-1], :] else: lowerCAmelCase = 3 lowerCAmelCase = 8 lowerCAmelCase = torch.randint(256 , (batch_size, sequence_length, n_codes_total) , dtype=torch.int ) lowerCAmelCase = model(lowerCamelCase , lowerCamelCase ) lowerCAmelCase = bark_model(lowerCamelCase , lowerCamelCase ) lowerCAmelCase = output_new_model_total.logits # output difference should come from the difference of self-attention implementation design if output_new_model.shape != output_old_model.shape: raise ValueError('initial and new outputs don\'t have the same shape' ) if (output_new_model - output_old_model).abs().max().item() > 1e-3: raise ValueError('initial and new outputs are not equal' ) Path(lowerCamelCase ).mkdir(exist_ok=lowerCamelCase ) model.save_pretrained(lowerCamelCase ) def a_ ( lowerCamelCase : Dict , lowerCamelCase : Tuple , lowerCamelCase : Tuple , lowerCamelCase : Optional[Any] , lowerCamelCase : int , lowerCamelCase : str , ): lowerCAmelCase = os.path.join(lowerCamelCase , lowerCamelCase ) lowerCAmelCase = BarkSemanticConfig.from_pretrained(os.path.join(lowerCamelCase , 'config.json' ) ) lowerCAmelCase = BarkCoarseConfig.from_pretrained(os.path.join(lowerCamelCase , 'config.json' ) ) lowerCAmelCase = BarkFineConfig.from_pretrained(os.path.join(lowerCamelCase , 'config.json' ) ) lowerCAmelCase = EncodecConfig.from_pretrained('facebook/encodec_24khz' ) lowerCAmelCase = BarkSemanticModel.from_pretrained(lowerCamelCase ) lowerCAmelCase = BarkCoarseModel.from_pretrained(lowerCamelCase ) lowerCAmelCase = BarkFineModel.from_pretrained(lowerCamelCase ) lowerCAmelCase = EncodecModel.from_pretrained('facebook/encodec_24khz' ) lowerCAmelCase = BarkConfig.from_sub_model_configs( lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase ) lowerCAmelCase = BarkGenerationConfig.from_sub_model_configs( semantic.generation_config , coarseAcoustic.generation_config , fineAcoustic.generation_config ) lowerCAmelCase = BarkModel(lowerCamelCase ) lowerCAmelCase = semantic lowerCAmelCase = coarseAcoustic lowerCAmelCase = fineAcoustic lowerCAmelCase = codec lowerCAmelCase = bark_generation_config Path(lowerCamelCase ).mkdir(exist_ok=lowerCamelCase ) bark.save_pretrained(lowerCamelCase , repo_id=lowerCamelCase , push_to_hub=lowerCamelCase ) if __name__ == "__main__": __snake_case =argparse.ArgumentParser() # Required parameters parser.add_argument("""model_type""", type=str, help="""text, coarse or fine.""") parser.add_argument("""pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--is_small""", action="""store_true""", help="""convert the small version instead of the large.""") __snake_case =parser.parse_args() load_model(args.pytorch_dump_folder_path, model_type=args.model_type, use_small=args.is_small)
4
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class UpperCAmelCase_ ( unittest.TestCase ): @slow def __UpperCAmelCase ( self : str ) -> List[str]: lowerCAmelCase = XLMRobertaModel.from_pretrained('xlm-roberta-base' ) lowerCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house lowerCAmelCase = torch.Size((1, 1_2, 7_6_8) ) # batch_size, sequence_length, embedding_vector_dim lowerCAmelCase = torch.tensor( [[-0.0_101, 0.1_218, -0.0_803, 0.0_801, 0.1_327, 0.0_776, -0.1_215, 0.2_383, 0.3_338, 0.3_106, 0.0_300, 0.0_252]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): lowerCAmelCase = model(UpperCAmelCase__ )['last_hidden_state'].detach() self.assertEqual(output.shape , UpperCAmelCase__ ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , UpperCAmelCase__ , atol=1E-3 ) ) @slow def __UpperCAmelCase ( self : List[Any] ) -> Tuple: lowerCAmelCase = XLMRobertaModel.from_pretrained('xlm-roberta-large' ) lowerCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house lowerCAmelCase = torch.Size((1, 1_2, 1_0_2_4) ) # batch_size, sequence_length, embedding_vector_dim lowerCAmelCase = torch.tensor( [[-0.0_699, -0.0_318, 0.0_705, -0.1_241, 0.0_999, -0.0_520, 0.1_004, -0.1_838, -0.4_704, 0.1_437, 0.0_821, 0.0_126]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): lowerCAmelCase = model(UpperCAmelCase__ )['last_hidden_state'].detach() self.assertEqual(output.shape , UpperCAmelCase__ ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , UpperCAmelCase__ , atol=1E-3 ) )
4
1
'''simple docstring''' def a_ ( lowerCamelCase : list ): lowerCAmelCase = len(lowerCamelCase ) for i in range(1 , lowerCamelCase ): lowerCAmelCase = collection[i] lowerCAmelCase = 0 lowerCAmelCase = i - 1 while low <= high: lowerCAmelCase = (low + high) // 2 if val < collection[mid]: lowerCAmelCase = mid - 1 else: lowerCAmelCase = mid + 1 for j in range(lowerCamelCase , lowerCamelCase , -1 ): lowerCAmelCase = collection[j - 1] lowerCAmelCase = val return collection if __name__ == "__main__": __snake_case =input("""Enter numbers separated by a comma:\n""").strip() __snake_case =[int(item) for item in user_input.split(""",""")] print(binary_insertion_sort(unsorted))
4
'''simple docstring''' import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def a_ ( lowerCamelCase : Dict ): lowerCAmelCase = {} lowerCAmelCase = tokenizer(example['content'] , truncation=lowerCamelCase )['input_ids'] lowerCAmelCase = len(example['content'] ) / len(output['input_ids'] ) return output __snake_case =HfArgumentParser(PretokenizationArguments) __snake_case =parser.parse_args() if args.num_workers is None: __snake_case =multiprocessing.cpu_count() __snake_case =AutoTokenizer.from_pretrained(args.tokenizer_dir) __snake_case =time.time() __snake_case =load_dataset(args.dataset_name, split="""train""") print(F'''Dataset loaded in {time.time()-t_start:.2f}s''') __snake_case =time.time() __snake_case =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''') __snake_case =time.time() ds.push_to_hub(args.tokenized_data_repo) print(F'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
4
1
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_nllb import NllbTokenizer else: __snake_case =None __snake_case =logging.get_logger(__name__) __snake_case ={"""vocab_file""": """sentencepiece.bpe.model""", """tokenizer_file""": """tokenizer.json"""} __snake_case ={ """vocab_file""": { """facebook/nllb-200-distilled-600M""": ( """https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/sentencepiece.bpe.model""" ), }, """tokenizer_file""": { """facebook/nllb-200-distilled-600M""": ( """https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/tokenizer.json""" ), }, } __snake_case ={ """facebook/nllb-large-en-ro""": 1_024, """facebook/nllb-200-distilled-600M""": 1_024, } # fmt: off __snake_case =["""ace_Arab""", """ace_Latn""", """acm_Arab""", """acq_Arab""", """aeb_Arab""", """afr_Latn""", """ajp_Arab""", """aka_Latn""", """amh_Ethi""", """apc_Arab""", """arb_Arab""", """ars_Arab""", """ary_Arab""", """arz_Arab""", """asm_Beng""", """ast_Latn""", """awa_Deva""", """ayr_Latn""", """azb_Arab""", """azj_Latn""", """bak_Cyrl""", """bam_Latn""", """ban_Latn""", """bel_Cyrl""", """bem_Latn""", """ben_Beng""", """bho_Deva""", """bjn_Arab""", """bjn_Latn""", """bod_Tibt""", """bos_Latn""", """bug_Latn""", """bul_Cyrl""", """cat_Latn""", """ceb_Latn""", """ces_Latn""", """cjk_Latn""", """ckb_Arab""", """crh_Latn""", """cym_Latn""", """dan_Latn""", """deu_Latn""", """dik_Latn""", """dyu_Latn""", """dzo_Tibt""", """ell_Grek""", """eng_Latn""", """epo_Latn""", """est_Latn""", """eus_Latn""", """ewe_Latn""", """fao_Latn""", """pes_Arab""", """fij_Latn""", """fin_Latn""", """fon_Latn""", """fra_Latn""", """fur_Latn""", """fuv_Latn""", """gla_Latn""", """gle_Latn""", """glg_Latn""", """grn_Latn""", """guj_Gujr""", """hat_Latn""", """hau_Latn""", """heb_Hebr""", """hin_Deva""", """hne_Deva""", """hrv_Latn""", """hun_Latn""", """hye_Armn""", """ibo_Latn""", """ilo_Latn""", """ind_Latn""", """isl_Latn""", """ita_Latn""", """jav_Latn""", """jpn_Jpan""", """kab_Latn""", """kac_Latn""", """kam_Latn""", """kan_Knda""", """kas_Arab""", """kas_Deva""", """kat_Geor""", """knc_Arab""", """knc_Latn""", """kaz_Cyrl""", """kbp_Latn""", """kea_Latn""", """khm_Khmr""", """kik_Latn""", """kin_Latn""", """kir_Cyrl""", """kmb_Latn""", """kon_Latn""", """kor_Hang""", """kmr_Latn""", """lao_Laoo""", """lvs_Latn""", """lij_Latn""", """lim_Latn""", """lin_Latn""", """lit_Latn""", """lmo_Latn""", """ltg_Latn""", """ltz_Latn""", """lua_Latn""", """lug_Latn""", """luo_Latn""", """lus_Latn""", """mag_Deva""", """mai_Deva""", """mal_Mlym""", """mar_Deva""", """min_Latn""", """mkd_Cyrl""", """plt_Latn""", """mlt_Latn""", """mni_Beng""", """khk_Cyrl""", """mos_Latn""", """mri_Latn""", """zsm_Latn""", """mya_Mymr""", """nld_Latn""", """nno_Latn""", """nob_Latn""", """npi_Deva""", """nso_Latn""", """nus_Latn""", """nya_Latn""", """oci_Latn""", """gaz_Latn""", """ory_Orya""", """pag_Latn""", """pan_Guru""", """pap_Latn""", """pol_Latn""", """por_Latn""", """prs_Arab""", """pbt_Arab""", """quy_Latn""", """ron_Latn""", """run_Latn""", """rus_Cyrl""", """sag_Latn""", """san_Deva""", """sat_Beng""", """scn_Latn""", """shn_Mymr""", """sin_Sinh""", """slk_Latn""", """slv_Latn""", """smo_Latn""", """sna_Latn""", """snd_Arab""", """som_Latn""", """sot_Latn""", """spa_Latn""", """als_Latn""", """srd_Latn""", """srp_Cyrl""", """ssw_Latn""", """sun_Latn""", """swe_Latn""", """swh_Latn""", """szl_Latn""", """tam_Taml""", """tat_Cyrl""", """tel_Telu""", """tgk_Cyrl""", """tgl_Latn""", """tha_Thai""", """tir_Ethi""", """taq_Latn""", """taq_Tfng""", """tpi_Latn""", """tsn_Latn""", """tso_Latn""", """tuk_Latn""", """tum_Latn""", """tur_Latn""", """twi_Latn""", """tzm_Tfng""", """uig_Arab""", """ukr_Cyrl""", """umb_Latn""", """urd_Arab""", """uzn_Latn""", """vec_Latn""", """vie_Latn""", """war_Latn""", """wol_Latn""", """xho_Latn""", """ydd_Hebr""", """yor_Latn""", """yue_Hant""", """zho_Hans""", """zho_Hant""", """zul_Latn"""] class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Tuple = VOCAB_FILES_NAMES lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Any = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : Optional[Any] = ['''input_ids''', '''attention_mask'''] lowerCamelCase : str = NllbTokenizer lowerCamelCase : List[int] = [] lowerCamelCase : List[int] = [] def __init__( self : List[str] , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : List[str]=None , UpperCAmelCase__ : Optional[int]="<s>" , UpperCAmelCase__ : str="</s>" , UpperCAmelCase__ : int="</s>" , UpperCAmelCase__ : List[Any]="<s>" , UpperCAmelCase__ : Dict="<unk>" , UpperCAmelCase__ : Union[str, Any]="<pad>" , UpperCAmelCase__ : Union[str, Any]="<mask>" , UpperCAmelCase__ : str=None , UpperCAmelCase__ : List[str]=None , UpperCAmelCase__ : Any=None , UpperCAmelCase__ : int=False , **UpperCAmelCase__ : Any , ) -> Dict: # Mask token behave like a normal word, i.e. include the space before it lowerCAmelCase = AddedToken(UpperCAmelCase__ , lstrip=UpperCAmelCase__ , rstrip=UpperCAmelCase__ ) if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) else mask_token lowerCAmelCase = legacy_behaviour super().__init__( vocab_file=UpperCAmelCase__ , tokenizer_file=UpperCAmelCase__ , bos_token=UpperCAmelCase__ , eos_token=UpperCAmelCase__ , sep_token=UpperCAmelCase__ , cls_token=UpperCAmelCase__ , unk_token=UpperCAmelCase__ , pad_token=UpperCAmelCase__ , mask_token=UpperCAmelCase__ , src_lang=UpperCAmelCase__ , tgt_lang=UpperCAmelCase__ , additional_special_tokens=UpperCAmelCase__ , legacy_behaviour=UpperCAmelCase__ , **UpperCAmelCase__ , ) lowerCAmelCase = vocab_file lowerCAmelCase = False if not self.vocab_file else True lowerCAmelCase = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({'additional_special_tokens': _additional_special_tokens} ) lowerCAmelCase = { lang_code: self.convert_tokens_to_ids(UpperCAmelCase__ ) for lang_code in FAIRSEQ_LANGUAGE_CODES } lowerCAmelCase = src_lang if src_lang is not None else 'eng_Latn' lowerCAmelCase = self.convert_tokens_to_ids(self._src_lang ) lowerCAmelCase = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def __UpperCAmelCase ( self : List[Any] ) -> str: return self._src_lang @src_lang.setter def __UpperCAmelCase ( self : List[Any] , UpperCAmelCase__ : str ) -> None: lowerCAmelCase = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def __UpperCAmelCase ( self : Union[str, Any] , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> List[int]: if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __UpperCAmelCase ( self : int , UpperCAmelCase__ : Dict , UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[str] , UpperCAmelCase__ : Optional[str] , **UpperCAmelCase__ : int ) -> Any: if src_lang is None or tgt_lang is None: raise ValueError('Translation requires a `src_lang` and a `tgt_lang` for this model' ) lowerCAmelCase = src_lang lowerCAmelCase = self(UpperCAmelCase__ , add_special_tokens=UpperCAmelCase__ , return_tensors=UpperCAmelCase__ , **UpperCAmelCase__ ) lowerCAmelCase = self.convert_tokens_to_ids(UpperCAmelCase__ ) lowerCAmelCase = tgt_lang_id return inputs def __UpperCAmelCase ( self : Tuple , UpperCAmelCase__ : List[str] , UpperCAmelCase__ : str = "eng_Latn" , UpperCAmelCase__ : Optional[List[str]] = None , UpperCAmelCase__ : str = "fra_Latn" , **UpperCAmelCase__ : List[Any] , ) -> BatchEncoding: lowerCAmelCase = src_lang lowerCAmelCase = tgt_lang return super().prepare_seqaseq_batch(UpperCAmelCase__ , UpperCAmelCase__ , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : int ) -> List[str]: return self.set_src_lang_special_tokens(self.src_lang ) def __UpperCAmelCase ( self : Tuple ) -> List[str]: return self.set_tgt_lang_special_tokens(self.tgt_lang ) def __UpperCAmelCase ( self : List[Any] , UpperCAmelCase__ : Any ) -> None: lowerCAmelCase = self.convert_tokens_to_ids(UpperCAmelCase__ ) if self.legacy_behaviour: lowerCAmelCase = [] lowerCAmelCase = [self.eos_token_id, self.cur_lang_code] else: lowerCAmelCase = [self.cur_lang_code] lowerCAmelCase = [self.eos_token_id] lowerCAmelCase = self.convert_ids_to_tokens(self.prefix_tokens ) lowerCAmelCase = self.convert_ids_to_tokens(self.suffix_tokens ) lowerCAmelCase = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def __UpperCAmelCase ( self : Dict , UpperCAmelCase__ : str ) -> None: lowerCAmelCase = self.convert_tokens_to_ids(UpperCAmelCase__ ) if self.legacy_behaviour: lowerCAmelCase = [] lowerCAmelCase = [self.eos_token_id, self.cur_lang_code] else: lowerCAmelCase = [self.cur_lang_code] lowerCAmelCase = [self.eos_token_id] lowerCAmelCase = self.convert_ids_to_tokens(self.prefix_tokens ) lowerCAmelCase = self.convert_ids_to_tokens(self.suffix_tokens ) lowerCAmelCase = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def __UpperCAmelCase ( self : Tuple , UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[str] = None ) -> Tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(UpperCAmelCase__ ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory.''' ) return lowerCAmelCase = os.path.join( UpperCAmelCase__ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCAmelCase__ ): copyfile(self.vocab_file , UpperCAmelCase__ ) return (out_vocab_file,)
4
'''simple docstring''' import logging from dataclasses import dataclass, field from pathlib import Path from typing import Optional, Union from .generation.configuration_utils import GenerationConfig from .training_args import TrainingArguments from .utils import add_start_docstrings __snake_case =logging.getLogger(__name__) @dataclass @add_start_docstrings(TrainingArguments.__doc__ ) class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : bool = field(default=__lowercase , metadata={'''help''': '''Whether to use SortishSampler or not.'''} ) lowerCamelCase : bool = field( default=__lowercase , metadata={'''help''': '''Whether to use generate to calculate generative metrics (ROUGE, BLEU).'''} ) lowerCamelCase : Optional[int] = field( default=__lowercase , metadata={ '''help''': ( '''The `max_length` to use on each evaluation loop when `predict_with_generate=True`. Will default ''' '''to the `max_length` value of the model configuration.''' ) } , ) lowerCamelCase : Optional[int] = field( default=__lowercase , metadata={ '''help''': ( '''The `num_beams` to use on each evaluation loop when `predict_with_generate=True`. Will default ''' '''to the `num_beams` value of the model configuration.''' ) } , ) lowerCamelCase : Optional[Union[str, Path, GenerationConfig]] = field( default=__lowercase , metadata={ '''help''': '''Model id, file path or url pointing to a GenerationConfig json file, to use during prediction.''' } , ) def __UpperCAmelCase ( self : Dict ) -> List[str]: lowerCAmelCase = super().to_dict() for k, v in d.items(): if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): lowerCAmelCase = v.to_dict() return d
4
1
'''simple docstring''' import contextlib import csv import json import os import sqlitea import tarfile import textwrap import zipfile import pyarrow as pa import pyarrow.parquet as pq import pytest import datasets import datasets.config @pytest.fixture(scope='session' ) def a_ ( ): lowerCAmelCase = 10 lowerCAmelCase = datasets.Features( { 'tokens': datasets.Sequence(datasets.Value('string' ) ), 'labels': datasets.Sequence(datasets.ClassLabel(names=['negative', 'positive'] ) ), 'answers': datasets.Sequence( { 'text': datasets.Value('string' ), 'answer_start': datasets.Value('int32' ), } ), 'id': datasets.Value('int64' ), } ) lowerCAmelCase = datasets.Dataset.from_dict( { 'tokens': [['foo'] * 5] * n, 'labels': [[1] * 5] * n, 'answers': [{'answer_start': [97], 'text': ['1976']}] * 10, 'id': list(range(lowerCamelCase ) ), } , features=lowerCamelCase , ) return dataset @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : int , lowerCamelCase : Tuple ): lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'file.arrow' ) dataset.map(cache_file_name=lowerCamelCase ) return filename # FILE_CONTENT + files __snake_case ="""\ Text data. Second line of data.""" @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : List[str] ): lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'file.txt' lowerCAmelCase = FILE_CONTENT with open(lowerCamelCase , 'w' ) as f: f.write(lowerCamelCase ) return filename @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Union[str, Any] ): import bza lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'file.txt.bz2' lowerCAmelCase = bytes(lowerCamelCase , 'utf-8' ) with bza.open(lowerCamelCase , 'wb' ) as f: f.write(lowerCamelCase ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Union[str, Any] ): import gzip lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'file.txt.gz' ) lowerCAmelCase = bytes(lowerCamelCase , 'utf-8' ) with gzip.open(lowerCamelCase , 'wb' ) as f: f.write(lowerCamelCase ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Any ): if datasets.config.LZ4_AVAILABLE: import lza.frame lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'file.txt.lz4' lowerCAmelCase = bytes(lowerCamelCase , 'utf-8' ) with lza.frame.open(lowerCamelCase , 'wb' ) as f: f.write(lowerCamelCase ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Dict , lowerCamelCase : List[Any] ): if datasets.config.PY7ZR_AVAILABLE: import pyazr lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'file.txt.7z' with pyazr.SevenZipFile(lowerCamelCase , 'w' ) as archive: archive.write(lowerCamelCase , arcname=os.path.basename(lowerCamelCase ) ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Dict , lowerCamelCase : List[str] ): import tarfile lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'file.txt.tar' with tarfile.TarFile(lowerCamelCase , 'w' ) as f: f.add(lowerCamelCase , arcname=os.path.basename(lowerCamelCase ) ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Any ): import lzma lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'file.txt.xz' lowerCAmelCase = bytes(lowerCamelCase , 'utf-8' ) with lzma.open(lowerCamelCase , 'wb' ) as f: f.write(lowerCamelCase ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Tuple , lowerCamelCase : int ): import zipfile lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'file.txt.zip' with zipfile.ZipFile(lowerCamelCase , 'w' ) as f: f.write(lowerCamelCase , arcname=os.path.basename(lowerCamelCase ) ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : List[Any] ): if datasets.config.ZSTANDARD_AVAILABLE: import zstandard as zstd lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'file.txt.zst' lowerCAmelCase = bytes(lowerCamelCase , 'utf-8' ) with zstd.open(lowerCamelCase , 'wb' ) as f: f.write(lowerCamelCase ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Optional[Any] ): lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'file.xml' lowerCAmelCase = textwrap.dedent( '\\n <?xml version="1.0" encoding="UTF-8" ?>\n <tmx version="1.4">\n <header segtype="sentence" srclang="ca" />\n <body>\n <tu>\n <tuv xml:lang="ca"><seg>Contingut 1</seg></tuv>\n <tuv xml:lang="en"><seg>Content 1</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang="ca"><seg>Contingut 2</seg></tuv>\n <tuv xml:lang="en"><seg>Content 2</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang="ca"><seg>Contingut 3</seg></tuv>\n <tuv xml:lang="en"><seg>Content 3</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang="ca"><seg>Contingut 4</seg></tuv>\n <tuv xml:lang="en"><seg>Content 4</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang="ca"><seg>Contingut 5</seg></tuv>\n <tuv xml:lang="en"><seg>Content 5</seg></tuv>\n </tu>\n </body>\n </tmx>' ) with open(lowerCamelCase , 'w' ) as f: f.write(lowerCamelCase ) return filename __snake_case =[ {"""col_1""": """0""", """col_2""": 0, """col_3""": 0.0}, {"""col_1""": """1""", """col_2""": 1, """col_3""": 1.0}, {"""col_1""": """2""", """col_2""": 2, """col_3""": 2.0}, {"""col_1""": """3""", """col_2""": 3, """col_3""": 3.0}, ] __snake_case =[ {"""col_1""": """4""", """col_2""": 4, """col_3""": 4.0}, {"""col_1""": """5""", """col_2""": 5, """col_3""": 5.0}, ] __snake_case ={ """col_1""": ["""0""", """1""", """2""", """3"""], """col_2""": [0, 1, 2, 3], """col_3""": [0.0, 1.0, 2.0, 3.0], } __snake_case =[ {"""col_3""": 0.0, """col_1""": """0""", """col_2""": 0}, {"""col_3""": 1.0, """col_1""": """1""", """col_2""": 1}, ] __snake_case =[ {"""col_1""": """s0""", """col_2""": 0, """col_3""": 0.0}, {"""col_1""": """s1""", """col_2""": 1, """col_3""": 1.0}, {"""col_1""": """s2""", """col_2""": 2, """col_3""": 2.0}, {"""col_1""": """s3""", """col_2""": 3, """col_3""": 3.0}, ] @pytest.fixture(scope='session' ) def a_ ( ): return DATA_DICT_OF_LISTS @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : List[Any] ): lowerCAmelCase = datasets.Dataset.from_dict(lowerCamelCase ) lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset.arrow' ) dataset.map(cache_file_name=lowerCamelCase ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Optional[int] ): lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset.sqlite' ) with contextlib.closing(sqlitea.connect(lowerCamelCase ) ) as con: lowerCAmelCase = con.cursor() cur.execute('CREATE TABLE dataset(col_1 text, col_2 int, col_3 real)' ) for item in DATA: cur.execute('INSERT INTO dataset(col_1, col_2, col_3) VALUES (?, ?, ?)' , tuple(item.values() ) ) con.commit() return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Any ): lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset.csv' ) with open(lowerCamelCase , 'w' , newline='' ) as f: lowerCAmelCase = csv.DictWriter(lowerCamelCase , fieldnames=['col_1', 'col_2', 'col_3'] ) writer.writeheader() for item in DATA: writer.writerow(lowerCamelCase ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Optional[Any] ): lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset2.csv' ) with open(lowerCamelCase , 'w' , newline='' ) as f: lowerCAmelCase = csv.DictWriter(lowerCamelCase , fieldnames=['col_1', 'col_2', 'col_3'] ) writer.writeheader() for item in DATA: writer.writerow(lowerCamelCase ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : List[Any] ): import bza lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'dataset.csv.bz2' with open(lowerCamelCase , 'rb' ) as f: lowerCAmelCase = f.read() # data = bytes(FILE_CONTENT, "utf-8") with bza.open(lowerCamelCase , 'wb' ) as f: f.write(lowerCamelCase ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : str , lowerCamelCase : Dict , lowerCamelCase : Optional[int] ): lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'dataset.csv.zip' with zipfile.ZipFile(lowerCamelCase , 'w' ) as f: f.write(lowerCamelCase , arcname=os.path.basename(lowerCamelCase ) ) f.write(lowerCamelCase , arcname=os.path.basename(lowerCamelCase ) ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Optional[Any] , lowerCamelCase : Any ): lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'dataset.csv.zip' with zipfile.ZipFile(lowerCamelCase , 'w' ) as f: f.write(lowerCamelCase , arcname=os.path.basename(csv_path.replace('.csv' , '.CSV' ) ) ) f.write(lowerCamelCase , arcname=os.path.basename(csva_path.replace('.csv' , '.CSV' ) ) ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : int , lowerCamelCase : int , lowerCamelCase : Union[str, Any] ): lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'dataset_with_dir.csv.zip' with zipfile.ZipFile(lowerCamelCase , 'w' ) as f: f.write(lowerCamelCase , arcname=os.path.join('main_dir' , os.path.basename(lowerCamelCase ) ) ) f.write(lowerCamelCase , arcname=os.path.join('main_dir' , os.path.basename(lowerCamelCase ) ) ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Dict ): lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset.parquet' ) lowerCAmelCase = pa.schema( { 'col_1': pa.string(), 'col_2': pa.intaa(), 'col_3': pa.floataa(), } ) with open(lowerCamelCase , 'wb' ) as f: lowerCAmelCase = pq.ParquetWriter(lowerCamelCase , schema=lowerCamelCase ) lowerCAmelCase = pa.Table.from_pydict({k: [DATA[i][k] for i in range(len(lowerCamelCase ) )] for k in DATA[0]} , schema=lowerCamelCase ) writer.write_table(lowerCamelCase ) writer.close() return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Union[str, Any] ): lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset.json' ) lowerCAmelCase = {'data': DATA} with open(lowerCamelCase , 'w' ) as f: json.dump(lowerCamelCase , lowerCamelCase ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Optional[int] ): lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset.json' ) lowerCAmelCase = {'data': DATA_DICT_OF_LISTS} with open(lowerCamelCase , 'w' ) as f: json.dump(lowerCamelCase , lowerCamelCase ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : str ): lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset.jsonl' ) with open(lowerCamelCase , 'w' ) as f: for item in DATA: f.write(json.dumps(lowerCamelCase ) + '\n' ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : List[Any] ): lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset2.jsonl' ) with open(lowerCamelCase , 'w' ) as f: for item in DATA: f.write(json.dumps(lowerCamelCase ) + '\n' ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Dict ): lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset_312.jsonl' ) with open(lowerCamelCase , 'w' ) as f: for item in DATA_312: f.write(json.dumps(lowerCamelCase ) + '\n' ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : List[str] ): lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset-str.jsonl' ) with open(lowerCamelCase , 'w' ) as f: for item in DATA_STR: f.write(json.dumps(lowerCamelCase ) + '\n' ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Tuple , lowerCamelCase : Tuple ): import gzip lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset.txt.gz' ) with open(lowerCamelCase , 'rb' ) as orig_file: with gzip.open(lowerCamelCase , 'wb' ) as zipped_file: zipped_file.writelines(lowerCamelCase ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Dict , lowerCamelCase : int ): import gzip lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset.jsonl.gz' ) with open(lowerCamelCase , 'rb' ) as orig_file: with gzip.open(lowerCamelCase , 'wb' ) as zipped_file: zipped_file.writelines(lowerCamelCase ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : List[str] , lowerCamelCase : str , lowerCamelCase : int ): lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'dataset.jsonl.zip' with zipfile.ZipFile(lowerCamelCase , 'w' ) as f: f.write(lowerCamelCase , arcname=os.path.basename(lowerCamelCase ) ) f.write(lowerCamelCase , arcname=os.path.basename(lowerCamelCase ) ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Optional[Any] , lowerCamelCase : Union[str, Any] , lowerCamelCase : Optional[Any] , lowerCamelCase : Any ): lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'dataset_nested.jsonl.zip' with zipfile.ZipFile(lowerCamelCase , 'w' ) as f: f.write(lowerCamelCase , arcname=os.path.join('nested' , os.path.basename(lowerCamelCase ) ) ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : List[str] , lowerCamelCase : Any , lowerCamelCase : Union[str, Any] ): lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'dataset_with_dir.jsonl.zip' with zipfile.ZipFile(lowerCamelCase , 'w' ) as f: f.write(lowerCamelCase , arcname=os.path.join('main_dir' , os.path.basename(lowerCamelCase ) ) ) f.write(lowerCamelCase , arcname=os.path.join('main_dir' , os.path.basename(lowerCamelCase ) ) ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : int , lowerCamelCase : str , lowerCamelCase : Optional[Any] ): lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'dataset.jsonl.tar' with tarfile.TarFile(lowerCamelCase , 'w' ) as f: f.add(lowerCamelCase , arcname=os.path.basename(lowerCamelCase ) ) f.add(lowerCamelCase , arcname=os.path.basename(lowerCamelCase ) ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : int , lowerCamelCase : List[Any] , lowerCamelCase : Any , lowerCamelCase : str ): lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'dataset_nested.jsonl.tar' with tarfile.TarFile(lowerCamelCase , 'w' ) as f: f.add(lowerCamelCase , arcname=os.path.join('nested' , os.path.basename(lowerCamelCase ) ) ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : int ): lowerCAmelCase = ['0', '1', '2', '3'] lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset.txt' ) with open(lowerCamelCase , 'w' ) as f: for item in data: f.write(item + '\n' ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : List[str] ): lowerCAmelCase = ['0', '1', '2', '3'] lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset2.txt' ) with open(lowerCamelCase , 'w' ) as f: for item in data: f.write(item + '\n' ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : int ): lowerCAmelCase = ['0', '1', '2', '3'] lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'dataset.abc' with open(lowerCamelCase , 'w' ) as f: for item in data: f.write(item + '\n' ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : int , lowerCamelCase : Optional[int] , lowerCamelCase : Tuple ): lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'dataset.text.zip' with zipfile.ZipFile(lowerCamelCase , 'w' ) as f: f.write(lowerCamelCase , arcname=os.path.basename(lowerCamelCase ) ) f.write(lowerCamelCase , arcname=os.path.basename(lowerCamelCase ) ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : int , lowerCamelCase : Any , lowerCamelCase : int ): lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'dataset_with_dir.text.zip' with zipfile.ZipFile(lowerCamelCase , 'w' ) as f: f.write(lowerCamelCase , arcname=os.path.join('main_dir' , os.path.basename(lowerCamelCase ) ) ) f.write(lowerCamelCase , arcname=os.path.join('main_dir' , os.path.basename(lowerCamelCase ) ) ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Tuple , lowerCamelCase : Optional[int] , lowerCamelCase : Tuple ): lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'dataset.ext.zip' with zipfile.ZipFile(lowerCamelCase , 'w' ) as f: f.write(lowerCamelCase , arcname=os.path.basename('unsupported.ext' ) ) f.write(lowerCamelCase , arcname=os.path.basename('unsupported_2.ext' ) ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Dict ): lowerCAmelCase = '\n'.join(['First', 'Second\u2029with Unicode new line', 'Third'] ) lowerCAmelCase = str(tmp_path_factory.mktemp('data' ) / 'dataset_with_unicode_new_lines.txt' ) with open(lowerCamelCase , 'w' , encoding='utf-8' ) as f: f.write(lowerCamelCase ) return path @pytest.fixture(scope='session' ) def a_ ( ): return os.path.join('tests' , 'features' , 'data' , 'test_image_rgb.jpg' ) @pytest.fixture(scope='session' ) def a_ ( ): return os.path.join('tests' , 'features' , 'data' , 'test_audio_44100.wav' ) @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Union[str, Any] ): lowerCAmelCase = tmp_path_factory.mktemp('data' ) / 'dataset.img.zip' with zipfile.ZipFile(lowerCamelCase , 'w' ) as f: f.write(lowerCamelCase , arcname=os.path.basename(lowerCamelCase ) ) f.write(lowerCamelCase , arcname=os.path.basename(lowerCamelCase ).replace('.jpg' , '2.jpg' ) ) return path @pytest.fixture(scope='session' ) def a_ ( lowerCamelCase : Union[str, Any] ): lowerCAmelCase = tmp_path_factory.mktemp('data_dir' ) (data_dir / "subdir").mkdir() with open(data_dir / 'subdir' / 'train.txt' , 'w' ) as f: f.write('foo\n' * 10 ) with open(data_dir / 'subdir' / 'test.txt' , 'w' ) as f: f.write('bar\n' * 10 ) # hidden file with open(data_dir / 'subdir' / '.test.txt' , 'w' ) as f: f.write('bar\n' * 10 ) # hidden directory (data_dir / ".subdir").mkdir() with open(data_dir / '.subdir' / 'train.txt' , 'w' ) as f: f.write('foo\n' * 10 ) with open(data_dir / '.subdir' / 'test.txt' , 'w' ) as f: f.write('bar\n' * 10 ) return data_dir
4
'''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() __snake_case =logging.get_logger("""transformers.models.encodec""") __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_DECODER, } __snake_case ={ **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_ENCODER_48K, **MAPPING_DECODER, **MAPPING_DECODER_48K, } __snake_case =[] __snake_case =[] def a_ ( lowerCamelCase : Optional[Any] , lowerCamelCase : Union[str, Any] , lowerCamelCase : int , lowerCamelCase : Any , lowerCamelCase : List[str] ): for attribute in key.split('.' ): lowerCAmelCase = getattr(lowerCamelCase , lowerCamelCase ) if weight_type is not None: lowerCAmelCase = getattr(lowerCamelCase , lowerCamelCase ).shape else: lowerCAmelCase = 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": lowerCAmelCase = value elif weight_type == "weight_g": lowerCAmelCase = value elif weight_type == "weight_v": lowerCAmelCase = value elif weight_type == "bias": lowerCAmelCase = value elif weight_type == "running_mean": lowerCAmelCase = value elif weight_type == "running_var": lowerCAmelCase = value elif weight_type == "num_batches_tracked": lowerCAmelCase = value elif weight_type == "weight_ih_l0": lowerCAmelCase = value elif weight_type == "weight_hh_l0": lowerCAmelCase = value elif weight_type == "bias_ih_l0": lowerCAmelCase = value elif weight_type == "bias_hh_l0": lowerCAmelCase = value elif weight_type == "weight_ih_l1": lowerCAmelCase = value elif weight_type == "weight_hh_l1": lowerCAmelCase = value elif weight_type == "bias_ih_l1": lowerCAmelCase = value elif weight_type == "bias_hh_l1": lowerCAmelCase = value else: lowerCAmelCase = value logger.info(f'''{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.''' ) def a_ ( lowerCamelCase : Optional[Any] , lowerCamelCase : Optional[Any] ): for key in ignore_keys: if key.endswith('.*' ): if name.startswith(key[:-1] ): return True elif ".*." in key: lowerCAmelCase , lowerCAmelCase = key.split('.*.' ) if prefix in name and suffix in name: return True elif key in name: return True return False def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Any , lowerCamelCase : str ): lowerCAmelCase = [] if model_name == "encodec_24khz" or "encodec_32khz": lowerCAmelCase = MAPPING_24K elif model_name == "encodec_48khz": lowerCAmelCase = 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 lowerCAmelCase = False for key, mapped_key in MAPPING.items(): if "*" in key: lowerCAmelCase , lowerCAmelCase = key.split('.*.' ) if prefix in name and suffix in name: lowerCAmelCase = suffix if key in name: # HACK otherwise .embed gets initialized with .embed_avg too if key.endswith('embed' ) and name.endswith('embed_avg' ): continue lowerCAmelCase = True if "*" in mapped_key: lowerCAmelCase = name.split(lowerCamelCase )[0].split('.' )[-2] lowerCAmelCase = mapped_key.replace('*' , lowerCamelCase ) if "weight_g" in name: lowerCAmelCase = 'weight_g' elif "weight_v" in name: lowerCAmelCase = 'weight_v' elif "weight_ih_l0" in name: lowerCAmelCase = 'weight_ih_l0' elif "weight_hh_l0" in name: lowerCAmelCase = 'weight_hh_l0' elif "bias_ih_l0" in name: lowerCAmelCase = 'bias_ih_l0' elif "bias_hh_l0" in name: lowerCAmelCase = 'bias_hh_l0' elif "weight_ih_l1" in name: lowerCAmelCase = 'weight_ih_l1' elif "weight_hh_l1" in name: lowerCAmelCase = 'weight_hh_l1' elif "bias_ih_l1" in name: lowerCAmelCase = 'bias_ih_l1' elif "bias_hh_l1" in name: lowerCAmelCase = 'bias_hh_l1' elif "bias" in name: lowerCAmelCase = 'bias' elif "weight" in name: lowerCAmelCase = 'weight' elif "running_mean" in name: lowerCAmelCase = 'running_mean' elif "running_var" in name: lowerCAmelCase = 'running_var' elif "num_batches_tracked" in name: lowerCAmelCase = 'num_batches_tracked' else: lowerCAmelCase = 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 a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Union[str, Any] , lowerCamelCase : str , lowerCamelCase : Dict=None , lowerCamelCase : Union[str, Any]=None , ): if config_path is not None: lowerCAmelCase = EncodecConfig.from_pretrained(lowerCamelCase ) else: lowerCAmelCase = EncodecConfig() if model_name == "encodec_24khz": pass # config is already correct elif model_name == "encodec_32khz": lowerCAmelCase = [8, 5, 4, 4] lowerCAmelCase = [2.2] lowerCAmelCase = 64 lowerCAmelCase = 32000 lowerCAmelCase = 2048 lowerCAmelCase = False lowerCAmelCase = False lowerCAmelCase = False elif model_name == "encodec_48khz": lowerCAmelCase = [8, 5, 4, 2] lowerCAmelCase = [3.0, 6.0, 12.0, 24.0] lowerCAmelCase = 48000 lowerCAmelCase = 2 lowerCAmelCase = False lowerCAmelCase = 'time_group_norm' lowerCAmelCase = True lowerCAmelCase = 1.0 lowerCAmelCase = 0.01 else: raise ValueError(f'''Unknown model name: {model_name}''' ) lowerCAmelCase = EncodecModel(lowerCamelCase ) lowerCAmelCase = 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 ) lowerCAmelCase = 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 lowerCAmelCase = 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__": __snake_case =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.""" ) __snake_case =parser.parse_args() convert_checkpoint( args.model, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
4
1
'''simple docstring''' import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XLMRobertaTokenizer, XLMRobertaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin __snake_case =get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece @require_tokenizers class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Union[str, Any] = XLMRobertaTokenizer lowerCamelCase : Dict = XLMRobertaTokenizerFast lowerCamelCase : str = True lowerCamelCase : Optional[int] = True def __UpperCAmelCase ( self : Dict ) -> str: super().setUp() # We have a SentencePiece fixture for testing lowerCAmelCase = XLMRobertaTokenizer(UpperCAmelCase__ , keep_accents=UpperCAmelCase__ ) tokenizer.save_pretrained(self.tmpdirname ) def __UpperCAmelCase ( self : List[Any] ) -> Union[str, Any]: lowerCAmelCase = '<pad>' lowerCAmelCase = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCAmelCase__ ) , UpperCAmelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCAmelCase__ ) , UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[Any] ) -> Union[str, Any]: lowerCAmelCase = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<s>' ) self.assertEqual(vocab_keys[1] , '<pad>' ) self.assertEqual(vocab_keys[-1] , '<mask>' ) self.assertEqual(len(UpperCAmelCase__ ) , 1_0_0_2 ) def __UpperCAmelCase ( self : Union[str, Any] ) -> Tuple: self.assertEqual(self.get_tokenizer().vocab_size , 1_0_0_2 ) def __UpperCAmelCase ( self : Optional[int] ) -> List[str]: lowerCAmelCase = XLMRobertaTokenizer(UpperCAmelCase__ , keep_accents=UpperCAmelCase__ ) lowerCAmelCase = tokenizer.tokenize('This is a test' ) self.assertListEqual(UpperCAmelCase__ , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(UpperCAmelCase__ ) , [value + tokenizer.fairseq_offset for value in [2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]] , ) lowerCAmelCase = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( UpperCAmelCase__ , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) lowerCAmelCase = tokenizer.convert_tokens_to_ids(UpperCAmelCase__ ) self.assertListEqual( UpperCAmelCase__ , [ value + tokenizer.fairseq_offset for value in [8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, 2, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] , ) lowerCAmelCase = tokenizer.convert_ids_to_tokens(UpperCAmelCase__ ) self.assertListEqual( UpperCAmelCase__ , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] , ) def __UpperCAmelCase ( self : List[Any] ) -> str: if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return lowerCAmelCase = (self.rust_tokenizer_class, 'hf-internal-testing/tiny-xlm-roberta', {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): lowerCAmelCase = self.rust_tokenizer_class.from_pretrained(UpperCAmelCase__ , **UpperCAmelCase__ ) lowerCAmelCase = self.tokenizer_class.from_pretrained(UpperCAmelCase__ , **UpperCAmelCase__ ) lowerCAmelCase = tempfile.mkdtemp() lowerCAmelCase = tokenizer_r.save_pretrained(UpperCAmelCase__ ) lowerCAmelCase = tokenizer_p.save_pretrained(UpperCAmelCase__ ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files ) ) lowerCAmelCase = tuple(f for f in tokenizer_r_files if 'tokenizer.json' not in f ) self.assertSequenceEqual(UpperCAmelCase__ , UpperCAmelCase__ ) # Checks everything loads correctly in the same way lowerCAmelCase = tokenizer_r.from_pretrained(UpperCAmelCase__ ) lowerCAmelCase = tokenizer_p.from_pretrained(UpperCAmelCase__ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(UpperCAmelCase__ , UpperCAmelCase__ ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(UpperCAmelCase__ ) # Save tokenizer rust, legacy_format=True lowerCAmelCase = tempfile.mkdtemp() lowerCAmelCase = tokenizer_r.save_pretrained(UpperCAmelCase__ , legacy_format=UpperCAmelCase__ ) lowerCAmelCase = tokenizer_p.save_pretrained(UpperCAmelCase__ ) # Checks it save with the same files self.assertSequenceEqual(UpperCAmelCase__ , UpperCAmelCase__ ) # Checks everything loads correctly in the same way lowerCAmelCase = tokenizer_r.from_pretrained(UpperCAmelCase__ ) lowerCAmelCase = tokenizer_p.from_pretrained(UpperCAmelCase__ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(UpperCAmelCase__ , UpperCAmelCase__ ) ) shutil.rmtree(UpperCAmelCase__ ) # Save tokenizer rust, legacy_format=False lowerCAmelCase = tempfile.mkdtemp() lowerCAmelCase = tokenizer_r.save_pretrained(UpperCAmelCase__ , legacy_format=UpperCAmelCase__ ) lowerCAmelCase = tokenizer_p.save_pretrained(UpperCAmelCase__ ) # Checks it saved the tokenizer.json file self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way lowerCAmelCase = tokenizer_r.from_pretrained(UpperCAmelCase__ ) lowerCAmelCase = tokenizer_p.from_pretrained(UpperCAmelCase__ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(UpperCAmelCase__ , UpperCAmelCase__ ) ) shutil.rmtree(UpperCAmelCase__ ) @cached_property def __UpperCAmelCase ( self : Union[str, Any] ) -> Dict: return XLMRobertaTokenizer.from_pretrained('xlm-roberta-base' ) def __UpperCAmelCase ( self : int ) -> Union[str, Any]: with tempfile.NamedTemporaryFile() as f: shutil.copyfile(UpperCAmelCase__ , f.name ) lowerCAmelCase = XLMRobertaTokenizer(f.name , keep_accents=UpperCAmelCase__ ) lowerCAmelCase = pickle.dumps(UpperCAmelCase__ ) pickle.loads(UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[int] ) -> Dict: if not self.test_rust_tokenizer: return lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = self.get_rust_tokenizer() lowerCAmelCase = 'I was born in 92000, and this is falsé.' lowerCAmelCase = tokenizer.tokenize(UpperCAmelCase__ ) lowerCAmelCase = rust_tokenizer.tokenize(UpperCAmelCase__ ) self.assertListEqual(UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = tokenizer.encode(UpperCAmelCase__ , add_special_tokens=UpperCAmelCase__ ) lowerCAmelCase = rust_tokenizer.encode(UpperCAmelCase__ , add_special_tokens=UpperCAmelCase__ ) self.assertListEqual(UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = self.get_rust_tokenizer() lowerCAmelCase = tokenizer.encode(UpperCAmelCase__ ) lowerCAmelCase = rust_tokenizer.encode(UpperCAmelCase__ ) self.assertListEqual(UpperCAmelCase__ , UpperCAmelCase__ ) @slow def __UpperCAmelCase ( self : Optional[Any] ) -> Any: lowerCAmelCase = 'Hello World!' lowerCAmelCase = [0, 3_5_3_7_8, 6_6_6_1, 3_8, 2] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(UpperCAmelCase__ , self.big_tokenizer.encode(UpperCAmelCase__ ) ) @slow def __UpperCAmelCase ( self : Tuple ) -> Optional[int]: lowerCAmelCase = ( 'This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will' ' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth' ) lowerCAmelCase = [ 0, 3_2_9_3, 8_3, 1_0, 4_5_5_2, 4_9_8_9, 7_9_8_6, 6_7_8, 1_0, 5_9_1_5, 1_1_1, 1_7_9_4_5_9, 1_2_4_8_5_0, 4, 6_0_4_4, 2_3_7, 1_2, 6, 5, 6, 4, 6_7_8_0, 7_0_5, 1_5, 1_3_8_8, 4_4, 3_7_8, 1_0_1_1_4, 7_1_1, 1_5_2, 2_0, 6, 5, 2_2_3_7_6, 6_4_2, 1_2_2_1, 1_5_1_9_0, 3_4_1_5_3, 4_5_0, 5_6_0_8, 9_5_9, 1_1_1_9, 5_7_7_0_2, 1_3_6, 1_8_6, 4_7, 1_0_9_8, 2_9_3_6_7, 4_7, # 4426, # What fairseq tokenizes from "<unk>": "_<" # 3678, # What fairseq tokenizes from "<unk>": "unk" # 2740, # What fairseq tokenizes from "<unk>": ">" 3, # What we tokenize from "<unk>": "<unk>" 6, # Residue from the tokenization: an extra sentencepiece underline 4, 6_0_4_4, 2_3_7, 6_2_8_4, 5_0_9_0_1, 5_2_8, 3_1, 9_0, 3_4, 9_2_7, 2, ] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(UpperCAmelCase__ , self.big_tokenizer.encode(UpperCAmelCase__ ) ) @slow def __UpperCAmelCase ( self : str ) -> Tuple: # fmt: off lowerCAmelCase = {'input_ids': [[0, 1_1_0_6_2, 8_2_7_7_2, 7, 1_5, 8_2_7_7_2, 5_3_8, 5_1_5_2_9, 2_3_7, 1_7_1_9_8, 1_2_9_0, 2_0_6, 9, 2_1_5_1_7_5, 1_3_1_4, 1_3_6, 1_7_1_9_8, 1_2_9_0, 2_0_6, 9, 5_6_3_5_9, 4_2, 1_2_2_0_0_9, 9, 1_6_4_6_6, 1_6, 8_7_3_4_4, 4_5_3_7, 9, 4_7_1_7, 7_8_3_8_1, 6, 1_5_9_9_5_8, 7, 1_5, 2_4_4_8_0, 6_1_8, 4, 5_2_7, 2_2_6_9_3, 5_4_2_8, 4, 2_7_7_7, 2_4_4_8_0, 9_8_7_4, 4, 4_3_5_2_3, 5_9_4, 4, 8_0_3, 1_8_3_9_2, 3_3_1_8_9, 1_8, 4, 4_3_5_2_3, 2_4_4_4_7, 1_2_3_9_9, 1_0_0, 2_4_9_5_5, 8_3_6_5_8, 9_6_2_6, 1_4_4_0_5_7, 1_5, 8_3_9, 2_2_3_3_5, 1_6, 1_3_6, 2_4_9_5_5, 8_3_6_5_8, 8_3_4_7_9, 1_5, 3_9_1_0_2, 7_2_4, 1_6, 6_7_8, 6_4_5, 2_7_8_9, 1_3_2_8, 4_5_8_9, 4_2, 1_2_2_0_0_9, 1_1_5_7_7_4, 2_3, 8_0_5, 1_3_2_8, 4_6_8_7_6, 7, 1_3_6, 5_3_8_9_4, 1_9_4_0, 4_2_2_2_7, 4_1_1_5_9, 1_7_7_2_1, 8_2_3, 4_2_5, 4, 2_7_5_1_2, 9_8_7_2_2, 2_0_6, 1_3_6, 5_5_3_1, 4_9_7_0, 9_1_9, 1_7_3_3_6, 5, 2], [0, 2_0_0_8_0, 6_1_8, 8_3, 8_2_7_7_5, 4_7, 4_7_9, 9, 1_5_1_7, 7_3, 5_3_8_9_4, 3_3_3, 8_0_5_8_1, 1_1_0_1_1_7, 1_8_8_1_1, 5_2_5_6, 1_2_9_5, 5_1, 1_5_2_5_2_6, 2_9_7, 7_9_8_6, 3_9_0, 1_2_4_4_1_6, 5_3_8, 3_5_4_3_1, 2_1_4, 9_8, 1_5_0_4_4, 2_5_7_3_7, 1_3_6, 7_1_0_8, 4_3_7_0_1, 2_3, 7_5_6, 1_3_5_3_5_5, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 5_8_1, 6_3_7_7_3, 1_1_9_4_5_5, 6, 1_4_7_7_9_7, 8_8_2_0_3, 7, 6_4_5, 7_0, 2_1, 3_2_8_5, 1_0_2_6_9, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=UpperCAmelCase__ , model_name='xlm-roberta-base' , revision='d9d8a8ea5eb94b1c6654ae9249df7793cd2933d3' , )
4
'''simple docstring''' import warnings from ...utils import logging from .image_processing_flava import FlavaImageProcessor __snake_case =logging.get_logger(__name__) class UpperCAmelCase_ ( __lowercase ): def __init__( self : Dict , *UpperCAmelCase__ : Dict , **UpperCAmelCase__ : List[str] ) -> None: warnings.warn( 'The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use FlavaImageProcessor instead.' , UpperCAmelCase__ , ) super().__init__(*UpperCAmelCase__ , **UpperCAmelCase__ )
4
1
'''simple docstring''' # Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __snake_case ={ """configuration_efficientnet""": [ """EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP""", """EfficientNetConfig""", """EfficientNetOnnxConfig""", ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case =["""EfficientNetImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case =[ """EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST""", """EfficientNetForImageClassification""", """EfficientNetModel""", """EfficientNetPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_efficientnet import ( EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientNetConfig, EfficientNetOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientnet import EfficientNetImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientnet import ( EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientNetForImageClassification, EfficientNetModel, EfficientNetPreTrainedModel, ) else: import sys __snake_case =_LazyModule(__name__, globals()["""__file__"""], _import_structure)
4
'''simple docstring''' from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer __snake_case =logging.get_logger(__name__) __snake_case ={ """vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_config_file""": """tokenizer_config.json""", } __snake_case ={ """vocab_file""": { """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json""" }, """merges_file""": { """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt""" }, """tokenizer_config_file""": { """facebook/blenderbot_small-90M""": ( """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json""" ) }, } __snake_case ={ """facebook/blenderbot_small-90M""": 512, } class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Tuple = VOCAB_FILES_NAMES lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[Any] = BlenderbotSmallTokenizer def __init__( self : Any , UpperCAmelCase__ : Union[str, Any]=None , UpperCAmelCase__ : Dict=None , UpperCAmelCase__ : int="<|endoftext|>" , UpperCAmelCase__ : Dict="<|endoftext|>" , UpperCAmelCase__ : str="<|endoftext|>" , UpperCAmelCase__ : str=False , UpperCAmelCase__ : Tuple=True , **UpperCAmelCase__ : Optional[Any] , ) -> Any: super().__init__( ByteLevelBPETokenizer( vocab=UpperCAmelCase__ , merges=UpperCAmelCase__ , add_prefix_space=UpperCAmelCase__ , trim_offsets=UpperCAmelCase__ , ) , bos_token=UpperCAmelCase__ , eos_token=UpperCAmelCase__ , unk_token=UpperCAmelCase__ , **UpperCAmelCase__ , ) lowerCAmelCase = add_prefix_space def __UpperCAmelCase ( self : List[Any] , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Dict=None ) -> Any: lowerCAmelCase = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def __UpperCAmelCase ( self : Any , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
4
1
'''simple docstring''' import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCAmelCase_ : def __init__( self : Union[str, Any] , UpperCAmelCase__ : List[str] , UpperCAmelCase__ : Optional[int]=1_3 , UpperCAmelCase__ : Optional[int]=3_0 , UpperCAmelCase__ : Dict=2 , UpperCAmelCase__ : Dict=3 , UpperCAmelCase__ : Any=True , UpperCAmelCase__ : List[str]=True , UpperCAmelCase__ : str=3_2 , UpperCAmelCase__ : List[str]=5 , UpperCAmelCase__ : Optional[int]=4 , UpperCAmelCase__ : List[str]=3_7 , UpperCAmelCase__ : Dict="gelu" , UpperCAmelCase__ : str=0.1 , UpperCAmelCase__ : List[Any]=0.1 , UpperCAmelCase__ : Tuple=1_0 , UpperCAmelCase__ : int=0.02 , UpperCAmelCase__ : Union[str, Any]=None , UpperCAmelCase__ : Tuple=2 , ) -> Tuple: lowerCAmelCase = parent lowerCAmelCase = batch_size lowerCAmelCase = image_size lowerCAmelCase = patch_size lowerCAmelCase = num_channels lowerCAmelCase = is_training lowerCAmelCase = use_labels lowerCAmelCase = hidden_size lowerCAmelCase = num_hidden_layers lowerCAmelCase = num_attention_heads lowerCAmelCase = intermediate_size lowerCAmelCase = hidden_act lowerCAmelCase = hidden_dropout_prob lowerCAmelCase = attention_probs_dropout_prob lowerCAmelCase = type_sequence_label_size lowerCAmelCase = initializer_range lowerCAmelCase = scope lowerCAmelCase = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) lowerCAmelCase = (image_size // patch_size) ** 2 lowerCAmelCase = num_patches + 1 def __UpperCAmelCase ( self : Optional[int] ) -> Union[str, Any]: lowerCAmelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase = None if self.use_labels: lowerCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCAmelCase = self.get_config() return config, pixel_values, labels def __UpperCAmelCase ( self : Tuple ) -> Optional[int]: return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=UpperCAmelCase__ , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Dict , UpperCAmelCase__ : int ) -> Optional[Any]: lowerCAmelCase = ViTModel(config=UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() lowerCAmelCase = model(UpperCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __UpperCAmelCase ( self : Any , UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : List[str] , UpperCAmelCase__ : List[Any] ) -> Union[str, Any]: lowerCAmelCase = ViTForMaskedImageModeling(config=UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() lowerCAmelCase = model(UpperCAmelCase__ ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images lowerCAmelCase = 1 lowerCAmelCase = ViTForMaskedImageModeling(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() lowerCAmelCase = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCAmelCase = model(UpperCAmelCase__ ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def __UpperCAmelCase ( self : Optional[Any] , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : List[str] , UpperCAmelCase__ : str ) -> Tuple: lowerCAmelCase = self.type_sequence_label_size lowerCAmelCase = ViTForImageClassification(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() lowerCAmelCase = model(UpperCAmelCase__ , labels=UpperCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowerCAmelCase = 1 lowerCAmelCase = ViTForImageClassification(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() lowerCAmelCase = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCAmelCase = model(UpperCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __UpperCAmelCase ( self : List[str] ) -> Optional[Any]: lowerCAmelCase = self.prepare_config_and_inputs() ( ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ) = config_and_inputs lowerCAmelCase = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase_ ( __lowercase , __lowercase , unittest.TestCase ): lowerCamelCase : Optional[Any] = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) lowerCamelCase : Union[str, Any] = ( {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification} if is_torch_available() else {} ) lowerCamelCase : int = True lowerCamelCase : str = False lowerCamelCase : List[str] = False lowerCamelCase : Optional[int] = False def __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: lowerCAmelCase = ViTModelTester(self ) lowerCAmelCase = ConfigTester(self , config_class=UpperCAmelCase__ , has_text_modality=UpperCAmelCase__ , hidden_size=3_7 ) def __UpperCAmelCase ( self : str ) -> str: self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def __UpperCAmelCase ( self : List[str] ) -> List[Any]: pass def __UpperCAmelCase ( self : Union[str, Any] ) -> List[Any]: lowerCAmelCase , lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase = model_class(UpperCAmelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowerCAmelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCAmelCase__ , nn.Linear ) ) def __UpperCAmelCase ( self : List[str] ) -> List[Any]: lowerCAmelCase , lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase = model_class(UpperCAmelCase__ ) lowerCAmelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase = [*signature.parameters.keys()] lowerCAmelCase = ['pixel_values'] self.assertListEqual(arg_names[:1] , UpperCAmelCase__ ) def __UpperCAmelCase ( self : int ) -> Any: lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase__ ) def __UpperCAmelCase ( self : Any ) -> Any: lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[Any] ) -> Tuple: lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase__ ) @slow def __UpperCAmelCase ( self : List[str] ) -> Optional[int]: for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase = ViTModel.from_pretrained(UpperCAmelCase__ ) self.assertIsNotNone(UpperCAmelCase__ ) def a_ ( ): lowerCAmelCase = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class UpperCAmelCase_ ( unittest.TestCase ): @cached_property def __UpperCAmelCase ( self : Optional[Any] ) -> List[Any]: return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224' ) if is_vision_available() else None @slow def __UpperCAmelCase ( self : Dict ) -> Union[str, Any]: lowerCAmelCase = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224' ).to(UpperCAmelCase__ ) lowerCAmelCase = self.default_image_processor lowerCAmelCase = prepare_img() lowerCAmelCase = image_processor(images=UpperCAmelCase__ , return_tensors='pt' ).to(UpperCAmelCase__ ) # forward pass with torch.no_grad(): lowerCAmelCase = model(**UpperCAmelCase__ ) # verify the logits lowerCAmelCase = torch.Size((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , UpperCAmelCase__ ) lowerCAmelCase = torch.tensor([-0.2_744, 0.8_215, -0.0_836] ).to(UpperCAmelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , UpperCAmelCase__ , atol=1E-4 ) ) @slow def __UpperCAmelCase ( self : int ) -> List[Any]: # ViT models have an `interpolate_pos_encoding` argument in their forward method, # allowing to interpolate the pre-trained position embeddings in order to use # the model on higher resolutions. The DINO model by Facebook AI leverages this # to visualize self-attention on higher resolution images. lowerCAmelCase = ViTModel.from_pretrained('facebook/dino-vits8' ).to(UpperCAmelCase__ ) lowerCAmelCase = ViTImageProcessor.from_pretrained('facebook/dino-vits8' , size=4_8_0 ) lowerCAmelCase = prepare_img() lowerCAmelCase = image_processor(images=UpperCAmelCase__ , return_tensors='pt' ) lowerCAmelCase = inputs.pixel_values.to(UpperCAmelCase__ ) # forward pass with torch.no_grad(): lowerCAmelCase = model(UpperCAmelCase__ , interpolate_pos_encoding=UpperCAmelCase__ ) # verify the logits lowerCAmelCase = torch.Size((1, 3_6_0_1, 3_8_4) ) self.assertEqual(outputs.last_hidden_state.shape , UpperCAmelCase__ ) lowerCAmelCase = torch.tensor( [[4.2_340, 4.3_906, -6.6_692], [4.5_463, 1.8_928, -6.7_257], [4.4_429, 0.8_496, -5.8_585]] ).to(UpperCAmelCase__ ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , UpperCAmelCase__ , atol=1E-4 ) ) @slow @require_accelerate @require_torch_gpu def __UpperCAmelCase ( self : Any ) -> Optional[int]: lowerCAmelCase = ViTModel.from_pretrained('facebook/dino-vits8' , torch_dtype=torch.floataa , device_map='auto' ) lowerCAmelCase = self.default_image_processor lowerCAmelCase = prepare_img() lowerCAmelCase = image_processor(images=UpperCAmelCase__ , return_tensors='pt' ) lowerCAmelCase = inputs.pixel_values.to(UpperCAmelCase__ ) # forward pass to make sure inference works in fp16 with torch.no_grad(): lowerCAmelCase = model(UpperCAmelCase__ )
4
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case =logging.get_logger(__name__) __snake_case ={ """facebook/s2t-wav2vec2-large-en-de""": ( """https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json""" ), # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2 } class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Union[str, Any] = '''speech_to_text_2''' lowerCamelCase : Any = ['''past_key_values'''] lowerCamelCase : Optional[Any] = {'''num_attention_heads''': '''decoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self : Optional[int] , UpperCAmelCase__ : Optional[Any]=1_0_0_0_0 , UpperCAmelCase__ : int=6 , UpperCAmelCase__ : Optional[Any]=2_0_4_8 , UpperCAmelCase__ : Union[str, Any]=4 , UpperCAmelCase__ : List[Any]=0.0 , UpperCAmelCase__ : str=True , UpperCAmelCase__ : str="relu" , UpperCAmelCase__ : Any=2_5_6 , UpperCAmelCase__ : Optional[Any]=0.1 , UpperCAmelCase__ : Any=0.0 , UpperCAmelCase__ : Tuple=0.0 , UpperCAmelCase__ : List[Any]=0.02 , UpperCAmelCase__ : List[Any]=2 , UpperCAmelCase__ : str=True , UpperCAmelCase__ : List[str]=1 , UpperCAmelCase__ : Any=0 , UpperCAmelCase__ : Dict=2 , UpperCAmelCase__ : int=1_0_2_4 , **UpperCAmelCase__ : Optional[Any] , ) -> Dict: lowerCAmelCase = vocab_size lowerCAmelCase = d_model lowerCAmelCase = decoder_ffn_dim lowerCAmelCase = decoder_layers lowerCAmelCase = decoder_attention_heads lowerCAmelCase = dropout lowerCAmelCase = attention_dropout lowerCAmelCase = activation_dropout lowerCAmelCase = activation_function lowerCAmelCase = init_std lowerCAmelCase = decoder_layerdrop lowerCAmelCase = use_cache lowerCAmelCase = decoder_layers lowerCAmelCase = scale_embedding # scale factor will be sqrt(d_model) if True lowerCAmelCase = max_target_positions super().__init__( pad_token_id=UpperCAmelCase__ , bos_token_id=UpperCAmelCase__ , eos_token_id=UpperCAmelCase__ , decoder_start_token_id=UpperCAmelCase__ , **UpperCAmelCase__ , )
4
1
'''simple docstring''' import argparse import torch from huggingface_hub import hf_hub_download from transformers import AutoTokenizer, RobertaPreLayerNormConfig, RobertaPreLayerNormForMaskedLM from transformers.utils import logging logging.set_verbosity_info() __snake_case =logging.get_logger(__name__) def a_ ( lowerCamelCase : str , lowerCamelCase : str ): lowerCAmelCase = RobertaPreLayerNormConfig.from_pretrained( lowerCamelCase , architectures=['RobertaPreLayerNormForMaskedLM'] ) # convert state_dict lowerCAmelCase = torch.load(hf_hub_download(repo_id=lowerCamelCase , filename='pytorch_model.bin' ) ) lowerCAmelCase = {} for tensor_key, tensor_value in original_state_dict.items(): # The transformer implementation gives the model a unique name, rather than overwiriting 'roberta' if tensor_key.startswith('roberta.' ): lowerCAmelCase = 'roberta_prelayernorm.' + tensor_key[len('roberta.' ) :] # The original implementation contains weights which are not used, remove them from the state_dict if tensor_key.endswith('.self.LayerNorm.weight' ) or tensor_key.endswith('.self.LayerNorm.bias' ): continue lowerCAmelCase = tensor_value lowerCAmelCase = RobertaPreLayerNormForMaskedLM.from_pretrained( pretrained_model_name_or_path=lowerCamelCase , config=lowerCamelCase , state_dict=lowerCamelCase ) model.save_pretrained(lowerCamelCase ) # convert tokenizer lowerCAmelCase = AutoTokenizer.from_pretrained(lowerCamelCase ) tokenizer.save_pretrained(lowerCamelCase ) if __name__ == "__main__": __snake_case =argparse.ArgumentParser() # Required parameters parser.add_argument( """--checkpoint-repo""", default=None, type=str, required=True, help="""Path the official PyTorch dump, e.g. 'andreasmadsen/efficient_mlm_m0.40'.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) __snake_case =parser.parse_args() convert_roberta_prelayernorm_checkpoint_to_pytorch(args.checkpoint_repo, args.pytorch_dump_folder_path)
4
'''simple docstring''' from __future__ import annotations from bisect import bisect_left from functools import total_ordering from heapq import merge @total_ordering class UpperCAmelCase_ ( __lowercase ): def __lt__( self : Optional[int] , UpperCAmelCase__ : List[str] ) -> List[Any]: return self[-1] < other[-1] def __eq__( self : str , UpperCAmelCase__ : List[str] ) -> Tuple: return self[-1] == other[-1] def a_ ( lowerCamelCase : list ): lowerCAmelCase = [] # sort into stacks for element in collection: lowerCAmelCase = Stack([element] ) lowerCAmelCase = bisect_left(lowerCamelCase , lowerCamelCase ) if i != len(lowerCamelCase ): stacks[i].append(lowerCamelCase ) else: stacks.append(lowerCamelCase ) # use a heap-based merge to merge stack efficiently lowerCAmelCase = merge(*(reversed(lowerCamelCase ) for stack in stacks) ) return collection if __name__ == "__main__": __snake_case =input("""Enter numbers separated by a comma:\n""").strip() __snake_case =[int(item) for item in user_input.split(""",""")] print(patience_sort(unsorted))
4
1
'''simple docstring''' import os import random import sys from . import cryptomath_module as cryptomath from . import rabin_miller __snake_case =3 def a_ ( lowerCamelCase : int ): print('Generating primitive root of p' ) while True: lowerCAmelCase = random.randrange(3 , lowerCamelCase ) if pow(lowerCamelCase , 2 , lowerCamelCase ) == 1: continue if pow(lowerCamelCase , lowerCamelCase , lowerCamelCase ) == 1: continue return g def a_ ( lowerCamelCase : int ): print('Generating prime p...' ) lowerCAmelCase = rabin_miller.generate_large_prime(lowerCamelCase ) # select large prime number. lowerCAmelCase = primitive_root(lowerCamelCase ) # one primitive root on modulo p. lowerCAmelCase = random.randrange(3 , lowerCamelCase ) # private_key -> have to be greater than 2 for safety. lowerCAmelCase = cryptomath.find_mod_inverse(pow(lowerCamelCase , lowerCamelCase , lowerCamelCase ) , lowerCamelCase ) lowerCAmelCase = (key_size, e_a, e_a, p) lowerCAmelCase = (key_size, d) return public_key, private_key def a_ ( lowerCamelCase : str , lowerCamelCase : int ): if os.path.exists(f'''{name}_pubkey.txt''' ) or os.path.exists(f'''{name}_privkey.txt''' ): print('\nWARNING:' ) print( f'''"{name}_pubkey.txt" or "{name}_privkey.txt" already exists. \n''' 'Use a different name or delete these files and re-run this program.' ) sys.exit() lowerCAmelCase , lowerCAmelCase = generate_key(lowerCamelCase ) print(f'''\nWriting public key to file {name}_pubkey.txt...''' ) with open(f'''{name}_pubkey.txt''' , 'w' ) as fo: fo.write(f'''{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}''' ) print(f'''Writing private key to file {name}_privkey.txt...''' ) with open(f'''{name}_privkey.txt''' , 'w' ) as fo: fo.write(f'''{private_key[0]},{private_key[1]}''' ) def a_ ( ): print('Making key files...' ) make_key_files('elgamal' , 2048 ) print('Key files generation successful' ) if __name__ == "__main__": main()
4
'''simple docstring''' import datasets from .nmt_bleu import compute_bleu # From: https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py __snake_case ="""\ @INPROCEEDINGS{Papineni02bleu:a, author = {Kishore Papineni and Salim Roukos and Todd Ward and Wei-jing Zhu}, title = {BLEU: a Method for Automatic Evaluation of Machine Translation}, booktitle = {}, year = {2002}, pages = {311--318} } @inproceedings{lin-och-2004-orange, title = \"{ORANGE}: a Method for Evaluating Automatic Evaluation Metrics for Machine Translation\", author = \"Lin, Chin-Yew and Och, Franz Josef\", booktitle = \"{COLING} 2004: Proceedings of the 20th International Conference on Computational Linguistics\", month = \"aug 23{--}aug 27\", year = \"2004\", address = \"Geneva, Switzerland\", publisher = \"COLING\", url = \"https://www.aclweb.org/anthology/C04-1072\", pages = \"501--507\", } """ __snake_case ="""\ BLEU (bilingual evaluation understudy) is an algorithm for evaluating the quality of text which has been machine-translated from one natural language to another. Quality is considered to be the correspondence between a machine's output and that of a human: \"the closer a machine translation is to a professional human translation, the better it is\" – this is the central idea behind BLEU. BLEU was one of the first metrics to claim a high correlation with human judgements of quality, and remains one of the most popular automated and inexpensive metrics. Scores are calculated for individual translated segments—generally sentences—by comparing them with a set of good quality reference translations. Those scores are then averaged over the whole corpus to reach an estimate of the translation's overall quality. Intelligibility or grammatical correctness are not taken into account[citation needed]. BLEU's output is always a number between 0 and 1. This value indicates how similar the candidate text is to the reference texts, with values closer to 1 representing more similar texts. Few human translations will attain a score of 1, since this would indicate that the candidate is identical to one of the reference translations. For this reason, it is not necessary to attain a score of 1. Because there are more opportunities to match, adding additional reference translations will increase the BLEU score. """ __snake_case =""" Computes BLEU score of translated segments against one or more references. Args: predictions: list of translations to score. Each translation should be tokenized into a list of tokens. references: list of lists of references for each translation. Each reference should be tokenized into a list of tokens. max_order: Maximum n-gram order to use when computing BLEU score. smooth: Whether or not to apply Lin et al. 2004 smoothing. Returns: 'bleu': bleu score, 'precisions': geometric mean of n-gram precisions, 'brevity_penalty': brevity penalty, 'length_ratio': ratio of lengths, 'translation_length': translation_length, 'reference_length': reference_length Examples: >>> predictions = [ ... [\"hello\", \"there\", \"general\", \"kenobi\"], # tokenized prediction of the first sample ... [\"foo\", \"bar\", \"foobar\"] # tokenized prediction of the second sample ... ] >>> references = [ ... [[\"hello\", \"there\", \"general\", \"kenobi\"], [\"hello\", \"there\", \"!\"]], # tokenized references for the first sample (2 references) ... [[\"foo\", \"bar\", \"foobar\"]] # tokenized references for the second sample (1 reference) ... ] >>> bleu = datasets.load_metric(\"bleu\") >>> results = bleu.compute(predictions=predictions, references=references) >>> print(results[\"bleu\"]) 1.0 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): def __UpperCAmelCase ( self : Tuple ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string' , id='token' ) , id='sequence' ), 'references': datasets.Sequence( datasets.Sequence(datasets.Value('string' , id='token' ) , id='sequence' ) , id='references' ), } ) , codebase_urls=['https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py'] , reference_urls=[ 'https://en.wikipedia.org/wiki/BLEU', 'https://towardsdatascience.com/evaluating-text-output-in-nlp-bleu-at-your-own-risk-e8609665a213', ] , ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Any , UpperCAmelCase__ : Union[str, Any]=4 , UpperCAmelCase__ : Optional[int]=False ) -> int: lowerCAmelCase = compute_bleu( reference_corpus=UpperCAmelCase__ , translation_corpus=UpperCAmelCase__ , max_order=UpperCAmelCase__ , smooth=UpperCAmelCase__ ) ((lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase)) = score return { "bleu": bleu, "precisions": precisions, "brevity_penalty": bp, "length_ratio": ratio, "translation_length": translation_length, "reference_length": reference_length, }
4
1
'''simple docstring''' import unicodedata from dataclasses import dataclass from typing import Optional, Union import numpy as np from transformers.data.data_collator import DataCollatorMixin from transformers.file_utils import PaddingStrategy from transformers.tokenization_utils_base import PreTrainedTokenizerBase def a_ ( lowerCamelCase : Tuple , lowerCamelCase : str , lowerCamelCase : Tuple , lowerCamelCase : int ): if isinstance(lowerCamelCase , lowerCamelCase ): lowerCAmelCase = np.full((len(lowerCamelCase ), sequence_length, 2) , lowerCamelCase ) else: lowerCAmelCase = np.full((len(lowerCamelCase ), sequence_length) , lowerCamelCase ) for i, tensor in enumerate(lowerCamelCase ): if padding_side == "right": if isinstance(lowerCamelCase , lowerCamelCase ): lowerCAmelCase = tensor[:sequence_length] else: lowerCAmelCase = tensor[:sequence_length] else: if isinstance(lowerCamelCase , lowerCamelCase ): lowerCAmelCase = tensor[:sequence_length] else: lowerCAmelCase = tensor[:sequence_length] return out_tensor.tolist() def a_ ( lowerCamelCase : Optional[int] ): lowerCAmelCase = ord(lowerCamelCase ) if (cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or (cp >= 91 and cp <= 96) or (cp >= 123 and cp <= 126): return True lowerCAmelCase = unicodedata.category(lowerCamelCase ) if cat.startswith('P' ): return True return False @dataclass class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : PreTrainedTokenizerBase lowerCamelCase : Union[bool, str, PaddingStrategy] = True lowerCamelCase : Optional[int] = None lowerCamelCase : Optional[int] = None lowerCamelCase : int = -100 lowerCamelCase : str = "pt" def __UpperCAmelCase ( self : int , UpperCAmelCase__ : Any ) -> List[Any]: import torch lowerCAmelCase = 'label' if 'label' in features[0].keys() else 'labels' lowerCAmelCase = [feature[label_name] for feature in features] if label_name in features[0].keys() else None lowerCAmelCase = self.tokenizer.pad( UpperCAmelCase__ , padding=self.padding , max_length=self.max_length , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors='pt' if labels is None else None , ) if labels is None: return batch lowerCAmelCase = torch.tensor(batch['entity_ids'] ).shape[1] lowerCAmelCase = self.tokenizer.padding_side if padding_side == "right": lowerCAmelCase = [ list(UpperCAmelCase__ ) + [self.label_pad_token_id] * (sequence_length - len(UpperCAmelCase__ )) for label in labels ] else: lowerCAmelCase = [ [self.label_pad_token_id] * (sequence_length - len(UpperCAmelCase__ )) + list(UpperCAmelCase__ ) for label in labels ] lowerCAmelCase = [feature['ner_tags'] for feature in features] lowerCAmelCase = padding_tensor(UpperCAmelCase__ , -1 , UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = [feature['original_entity_spans'] for feature in features] lowerCAmelCase = padding_tensor(UpperCAmelCase__ , (-1, -1) , UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = {k: torch.tensor(UpperCAmelCase__ , dtype=torch.intaa ) for k, v in batch.items()} return batch
4
'''simple docstring''' from sklearn.metrics import fa_score, matthews_corrcoef import datasets from .record_evaluation import evaluate as evaluate_record __snake_case ="""\ @article{wang2019superglue, title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems}, author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R}, journal={arXiv preprint arXiv:1905.00537}, year={2019} } """ __snake_case ="""\ SuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after GLUE with a new set of more difficult language understanding tasks, improved resources, and a new public leaderboard. """ __snake_case =""" Compute SuperGLUE evaluation metric associated to each SuperGLUE dataset. Args: predictions: list of predictions to score. Depending on the SuperGlUE subset: - for 'record': list of question-answer dictionaries with the following keys: - 'idx': index of the question as specified by the dataset - 'prediction_text': the predicted answer text - for 'multirc': list of question-answer dictionaries with the following keys: - 'idx': index of the question-answer pair as specified by the dataset - 'prediction': the predicted answer label - otherwise: list of predicted labels references: list of reference labels. Depending on the SuperGLUE subset: - for 'record': list of question-answers dictionaries with the following keys: - 'idx': index of the question as specified by the dataset - 'answers': list of possible answers - otherwise: list of reference labels Returns: depending on the SuperGLUE subset: - for 'record': - 'exact_match': Exact match between answer and gold answer - 'f1': F1 score - for 'multirc': - 'exact_match': Exact match between answer and gold answer - 'f1_m': Per-question macro-F1 score - 'f1_a': Average F1 score over all answers - for 'axb': 'matthews_correlation': Matthew Correlation - for 'cb': - 'accuracy': Accuracy - 'f1': F1 score - for all others: - 'accuracy': Accuracy Examples: >>> super_glue_metric = datasets.load_metric('super_glue', 'copa') # any of [\"copa\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"boolq\", \"axg\"] >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'cb') >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0, 'f1': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'record') >>> predictions = [{'idx': {'passage': 0, 'query': 0}, 'prediction_text': 'answer'}] >>> references = [{'idx': {'passage': 0, 'query': 0}, 'answers': ['answer', 'another_answer']}] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 1.0, 'f1': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'multirc') >>> predictions = [{'idx': {'answer': 0, 'paragraph': 0, 'question': 0}, 'prediction': 0}, {'idx': {'answer': 1, 'paragraph': 2, 'question': 3}, 'prediction': 1}] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 1.0, 'f1_m': 1.0, 'f1_a': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'axb') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'matthews_correlation': 1.0} """ def a_ ( lowerCamelCase : str , lowerCamelCase : Union[str, Any] ): return float((preds == labels).mean() ) def a_ ( lowerCamelCase : Union[str, Any] , lowerCamelCase : Dict , lowerCamelCase : str="binary" ): lowerCAmelCase = simple_accuracy(lowerCamelCase , lowerCamelCase ) lowerCAmelCase = float(fa_score(y_true=lowerCamelCase , y_pred=lowerCamelCase , average=lowerCamelCase ) ) return { "accuracy": acc, "f1": fa, } def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : List[Any] ): lowerCAmelCase = {} for id_pred, label in zip(lowerCamelCase , lowerCamelCase ): lowerCAmelCase = f'''{id_pred['idx']['paragraph']}-{id_pred['idx']['question']}''' lowerCAmelCase = id_pred['prediction'] if question_id in question_map: question_map[question_id].append((pred, label) ) else: lowerCAmelCase = [(pred, label)] lowerCAmelCase , lowerCAmelCase = [], [] for question, preds_labels in question_map.items(): lowerCAmelCase , lowerCAmelCase = zip(*lowerCamelCase ) lowerCAmelCase = fa_score(y_true=lowerCamelCase , y_pred=lowerCamelCase , average='macro' ) fas.append(lowerCamelCase ) lowerCAmelCase = int(sum(pred == label for pred, label in preds_labels ) == len(lowerCamelCase ) ) ems.append(lowerCamelCase ) lowerCAmelCase = float(sum(lowerCamelCase ) / len(lowerCamelCase ) ) lowerCAmelCase = sum(lowerCamelCase ) / len(lowerCamelCase ) lowerCAmelCase = float(fa_score(y_true=lowerCamelCase , y_pred=[id_pred['prediction'] for id_pred in ids_preds] ) ) return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a} @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): def __UpperCAmelCase ( self : List[str] ) -> List[Any]: if self.config_name not in [ "boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg", ]: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , codebase_urls=[] , reference_urls=[] , format='numpy' if not self.config_name == 'record' and not self.config_name == 'multirc' else None , ) def __UpperCAmelCase ( self : Union[str, Any] ) -> str: if self.config_name == "record": return { "predictions": { "idx": { "passage": datasets.Value('int64' ), "query": datasets.Value('int64' ), }, "prediction_text": datasets.Value('string' ), }, "references": { "idx": { "passage": datasets.Value('int64' ), "query": datasets.Value('int64' ), }, "answers": datasets.Sequence(datasets.Value('string' ) ), }, } elif self.config_name == "multirc": return { "predictions": { "idx": { "answer": datasets.Value('int64' ), "paragraph": datasets.Value('int64' ), "question": datasets.Value('int64' ), }, "prediction": datasets.Value('int64' ), }, "references": datasets.Value('int64' ), } else: return { "predictions": datasets.Value('int64' ), "references": datasets.Value('int64' ), } def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : Any , UpperCAmelCase__ : Optional[Any] ) -> Any: if self.config_name == "axb": return {"matthews_correlation": matthews_corrcoef(UpperCAmelCase__ , UpperCAmelCase__ )} elif self.config_name == "cb": return acc_and_fa(UpperCAmelCase__ , UpperCAmelCase__ , fa_avg='macro' ) elif self.config_name == "record": lowerCAmelCase = [ { 'qas': [ {'id': ref['idx']['query'], 'answers': [{'text': ans} for ans in ref['answers']]} for ref in references ] } ] lowerCAmelCase = {pred['idx']['query']: pred['prediction_text'] for pred in predictions} return evaluate_record(UpperCAmelCase__ , UpperCAmelCase__ )[0] elif self.config_name == "multirc": return evaluate_multirc(UpperCAmelCase__ , UpperCAmelCase__ ) elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]: return {"accuracy": simple_accuracy(UpperCAmelCase__ , UpperCAmelCase__ )} else: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]' )
4
1
'''simple docstring''' def a_ ( lowerCamelCase : str ): return " ".join(input_str.split()[::-1] ) if __name__ == "__main__": import doctest doctest.testmod()
4
'''simple docstring''' print((lambda quine: quine % quine)("""print((lambda quine: quine %% quine)(%r))"""))
4
1
'''simple docstring''' import itertools from dataclasses import dataclass from typing import List, Optional import pyarrow as pa import pyarrow.parquet as pq import datasets from datasets.table import table_cast __snake_case =datasets.utils.logging.get_logger(__name__) @dataclass class UpperCAmelCase_ ( datasets.BuilderConfig ): lowerCamelCase : int = 10_000 lowerCamelCase : Optional[List[str]] = None lowerCamelCase : Optional[datasets.Features] = None class UpperCAmelCase_ ( datasets.ArrowBasedBuilder ): lowerCamelCase : List[str] = ParquetConfig def __UpperCAmelCase ( self : List[Any] ) -> Optional[int]: return datasets.DatasetInfo(features=self.config.features ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : Union[str, Any] ) -> Tuple: if not self.config.data_files: raise ValueError(F'''At least one data file must be specified, but got data_files={self.config.data_files}''' ) lowerCAmelCase = dl_manager.download_and_extract(self.config.data_files ) if isinstance(UpperCAmelCase__ , (str, list, tuple) ): lowerCAmelCase = data_files if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): lowerCAmelCase = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive lowerCAmelCase = [dl_manager.iter_files(UpperCAmelCase__ ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'files': files} )] lowerCAmelCase = [] for split_name, files in data_files.items(): if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): lowerCAmelCase = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive lowerCAmelCase = [dl_manager.iter_files(UpperCAmelCase__ ) for file in files] # Infer features is they are stoed in the arrow schema if self.info.features is None: for file in itertools.chain.from_iterable(UpperCAmelCase__ ): with open(UpperCAmelCase__ , 'rb' ) as f: lowerCAmelCase = datasets.Features.from_arrow_schema(pq.read_schema(UpperCAmelCase__ ) ) break splits.append(datasets.SplitGenerator(name=UpperCAmelCase__ , gen_kwargs={'files': files} ) ) return splits def __UpperCAmelCase ( self : str , UpperCAmelCase__ : pa.Table ) -> pa.Table: if self.info.features is not None: # more expensive cast to support nested features with keys in a different order # allows str <-> int/float or str to Audio for example lowerCAmelCase = table_cast(UpperCAmelCase__ , self.info.features.arrow_schema ) return pa_table def __UpperCAmelCase ( self : Dict , UpperCAmelCase__ : int ) -> Dict: lowerCAmelCase = self.info.features.arrow_schema if self.info.features is not None else None if self.info.features is not None and self.config.columns is not None: if sorted(field.name for field in schema ) != sorted(self.config.columns ): raise ValueError( F'''Tried to load parquet data with columns \'{self.config.columns}\' with mismatching features \'{self.info.features}\'''' ) for file_idx, file in enumerate(itertools.chain.from_iterable(UpperCAmelCase__ ) ): with open(UpperCAmelCase__ , 'rb' ) as f: lowerCAmelCase = pq.ParquetFile(UpperCAmelCase__ ) try: for batch_idx, record_batch in enumerate( parquet_file.iter_batches(batch_size=self.config.batch_size , columns=self.config.columns ) ): lowerCAmelCase = pa.Table.from_batches([record_batch] ) # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield F'''{file_idx}_{batch_idx}''', self._cast_table(UpperCAmelCase__ ) except ValueError as e: logger.error(F'''Failed to read file \'{file}\' with error {type(UpperCAmelCase__ )}: {e}''' ) raise
4
'''simple docstring''' import os __snake_case ={"""I""": 1, """V""": 5, """X""": 10, """L""": 50, """C""": 100, """D""": 500, """M""": 1_000} def a_ ( lowerCamelCase : str ): lowerCAmelCase = 0 lowerCAmelCase = 0 while index < len(lowerCamelCase ) - 1: lowerCAmelCase = SYMBOLS[numerals[index]] lowerCAmelCase = SYMBOLS[numerals[index + 1]] if current_value < next_value: total_value -= current_value else: total_value += current_value index += 1 total_value += SYMBOLS[numerals[index]] return total_value def a_ ( lowerCamelCase : int ): lowerCAmelCase = '' lowerCAmelCase = num // 1000 numerals += m_count * "M" num %= 1000 lowerCAmelCase = num // 100 if c_count == 9: numerals += "CM" c_count -= 9 elif c_count == 4: numerals += "CD" c_count -= 4 if c_count >= 5: numerals += "D" c_count -= 5 numerals += c_count * "C" num %= 100 lowerCAmelCase = num // 10 if x_count == 9: numerals += "XC" x_count -= 9 elif x_count == 4: numerals += "XL" x_count -= 4 if x_count >= 5: numerals += "L" x_count -= 5 numerals += x_count * "X" num %= 10 if num == 9: numerals += "IX" num -= 9 elif num == 4: numerals += "IV" num -= 4 if num >= 5: numerals += "V" num -= 5 numerals += num * "I" return numerals def a_ ( lowerCamelCase : str = "/p089_roman.txt" ): lowerCAmelCase = 0 with open(os.path.dirname(lowerCamelCase ) + roman_numerals_filename ) as filea: lowerCAmelCase = filea.readlines() for line in lines: lowerCAmelCase = line.strip() lowerCAmelCase = parse_roman_numerals(lowerCamelCase ) lowerCAmelCase = generate_roman_numerals(lowerCamelCase ) savings += len(lowerCamelCase ) - len(lowerCamelCase ) return savings if __name__ == "__main__": print(F'''{solution() = }''')
4
1
'''simple docstring''' class UpperCAmelCase_ : def __init__( self : List[str] , UpperCAmelCase__ : Optional[int] ) -> str: # we need a list not a string, so do something to change the type lowerCAmelCase = arr.split(',' ) def __UpperCAmelCase ( self : Union[str, Any] ) -> int: lowerCAmelCase = [int(self.array[0] )] * len(self.array ) lowerCAmelCase = [int(self.array[0] )] * len(self.array ) for i in range(1 , len(self.array ) ): lowerCAmelCase = max( int(self.array[i] ) + sum_value[i - 1] , int(self.array[i] ) ) lowerCAmelCase = max(sum_value[i] , rear[i - 1] ) return rear[len(self.array ) - 1] if __name__ == "__main__": __snake_case =input("""please input some numbers:""") __snake_case =SubArray(whole_array) __snake_case =array.solve_sub_array() print(("""the results is:""", re))
4
'''simple docstring''' import itertools import random import unittest import numpy as np from transformers import ASTFeatureExtractor from transformers.testing_utils import require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin __snake_case =random.Random() if is_torch_available(): import torch def a_ ( lowerCamelCase : Dict , lowerCamelCase : Dict=1.0 , lowerCamelCase : List[Any]=None , lowerCamelCase : Union[str, Any]=None ): if rng is None: lowerCAmelCase = global_rng lowerCAmelCase = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class UpperCAmelCase_ ( unittest.TestCase ): def __init__( self : List[Any] , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : List[str]=7 , UpperCAmelCase__ : int=4_0_0 , UpperCAmelCase__ : int=2_0_0_0 , UpperCAmelCase__ : List[str]=1 , UpperCAmelCase__ : Tuple=0.0 , UpperCAmelCase__ : Tuple=1_6_0_0_0 , UpperCAmelCase__ : Tuple=True , UpperCAmelCase__ : Union[str, Any]=True , ) -> Any: lowerCAmelCase = parent lowerCAmelCase = batch_size lowerCAmelCase = min_seq_length lowerCAmelCase = max_seq_length lowerCAmelCase = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) lowerCAmelCase = feature_size lowerCAmelCase = padding_value lowerCAmelCase = sampling_rate lowerCAmelCase = return_attention_mask lowerCAmelCase = do_normalize def __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def __UpperCAmelCase ( self : str , UpperCAmelCase__ : Union[str, Any]=False , UpperCAmelCase__ : Union[str, Any]=False ) -> Optional[Any]: def _flatten(UpperCAmelCase__ : int ): return list(itertools.chain(*UpperCAmelCase__ ) ) if equal_length: lowerCAmelCase = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size lowerCAmelCase = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: lowerCAmelCase = [np.asarray(UpperCAmelCase__ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Dict = ASTFeatureExtractor def __UpperCAmelCase ( self : str ) -> Optional[int]: lowerCAmelCase = ASTFeatureExtractionTester(self ) def __UpperCAmelCase ( self : Optional[int] ) -> Optional[int]: # Tests that all call wrap to encode_plus and batch_encode_plus lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 lowerCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] lowerCAmelCase = [np.asarray(UpperCAmelCase__ ) for speech_input in speech_inputs] # Test not batched input lowerCAmelCase = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values lowerCAmelCase = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1E-3 ) ) # Test batched lowerCAmelCase = feat_extract(UpperCAmelCase__ , padding=UpperCAmelCase__ , return_tensors='np' ).input_values lowerCAmelCase = feat_extract(UpperCAmelCase__ , padding=UpperCAmelCase__ , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__ , UpperCAmelCase__ ): self.assertTrue(np.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. lowerCAmelCase = [floats_list((1, x) )[0] for x in (8_0_0, 8_0_0, 8_0_0)] lowerCAmelCase = np.asarray(UpperCAmelCase__ ) lowerCAmelCase = feat_extract(UpperCAmelCase__ , return_tensors='np' ).input_values lowerCAmelCase = feat_extract(UpperCAmelCase__ , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__ , UpperCAmelCase__ ): self.assertTrue(np.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1E-3 ) ) @require_torch def __UpperCAmelCase ( self : Union[str, Any] ) -> Optional[int]: import torch lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCAmelCase = np.random.rand(1_0_0 ).astype(np.floataa ) lowerCAmelCase = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: lowerCAmelCase = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) lowerCAmelCase = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def __UpperCAmelCase ( self : int , UpperCAmelCase__ : str ) -> Tuple: from datasets import load_dataset lowerCAmelCase = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech lowerCAmelCase = ds.sort('id' ).select(range(UpperCAmelCase__ ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] @require_torch def __UpperCAmelCase ( self : str ) -> Optional[Any]: # fmt: off lowerCAmelCase = torch.tensor( [-0.9_894, -1.2_776, -0.9_066, -1.2_776, -0.9_349, -1.2_609, -1.0_386, -1.2_776, -1.1_561, -1.2_776, -1.2_052, -1.2_723, -1.2_190, -1.2_132, -1.2_776, -1.1_133, -1.1_953, -1.1_343, -1.1_584, -1.2_203, -1.1_770, -1.2_474, -1.2_381, -1.1_936, -0.9_270, -0.8_317, -0.8_049, -0.7_706, -0.7_565, -0.7_869] ) # fmt: on lowerCAmelCase = self._load_datasamples(1 ) lowerCAmelCase = ASTFeatureExtractor() lowerCAmelCase = feature_extractor(UpperCAmelCase__ , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 1_0_2_4, 1_2_8) ) self.assertTrue(torch.allclose(input_values[0, 0, :3_0] , UpperCAmelCase__ , atol=1E-4 ) )
4
1
'''simple docstring''' import os __snake_case ={"""I""": 1, """V""": 5, """X""": 10, """L""": 50, """C""": 100, """D""": 500, """M""": 1_000} def a_ ( lowerCamelCase : str ): lowerCAmelCase = 0 lowerCAmelCase = 0 while index < len(lowerCamelCase ) - 1: lowerCAmelCase = SYMBOLS[numerals[index]] lowerCAmelCase = SYMBOLS[numerals[index + 1]] if current_value < next_value: total_value -= current_value else: total_value += current_value index += 1 total_value += SYMBOLS[numerals[index]] return total_value def a_ ( lowerCamelCase : int ): lowerCAmelCase = '' lowerCAmelCase = num // 1000 numerals += m_count * "M" num %= 1000 lowerCAmelCase = num // 100 if c_count == 9: numerals += "CM" c_count -= 9 elif c_count == 4: numerals += "CD" c_count -= 4 if c_count >= 5: numerals += "D" c_count -= 5 numerals += c_count * "C" num %= 100 lowerCAmelCase = num // 10 if x_count == 9: numerals += "XC" x_count -= 9 elif x_count == 4: numerals += "XL" x_count -= 4 if x_count >= 5: numerals += "L" x_count -= 5 numerals += x_count * "X" num %= 10 if num == 9: numerals += "IX" num -= 9 elif num == 4: numerals += "IV" num -= 4 if num >= 5: numerals += "V" num -= 5 numerals += num * "I" return numerals def a_ ( lowerCamelCase : str = "/p089_roman.txt" ): lowerCAmelCase = 0 with open(os.path.dirname(lowerCamelCase ) + roman_numerals_filename ) as filea: lowerCAmelCase = filea.readlines() for line in lines: lowerCAmelCase = line.strip() lowerCAmelCase = parse_roman_numerals(lowerCamelCase ) lowerCAmelCase = generate_roman_numerals(lowerCamelCase ) savings += len(lowerCamelCase ) - len(lowerCamelCase ) return savings if __name__ == "__main__": print(F'''{solution() = }''')
4
'''simple docstring''' import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : str ) -> List[Any]: lowerCAmelCase = torch.nn.Linear(1_0 , 1_0 ) lowerCAmelCase = torch.optim.SGD(model.parameters() , 0.1 ) lowerCAmelCase = Accelerator() lowerCAmelCase = accelerator.prepare(UpperCAmelCase__ ) try: pickle.loads(pickle.dumps(UpperCAmelCase__ ) ) except Exception as e: self.fail(F'''Accelerated optimizer pickling failed with {e}''' ) AcceleratorState._reset_state()
4
1
'''simple docstring''' # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import torch from ..models.clipseg import CLIPSegForImageSegmentation from ..utils import is_vision_available, requires_backends from .base import PipelineTool if is_vision_available(): from PIL import Image class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : List[Any] = ( '''This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image.''' '''It takes two arguments named `image` which should be the original image, and `label` which should be a text ''' '''describing the elements what should be identified in the segmentation mask. The tool returns the mask.''' ) lowerCamelCase : Any = '''CIDAS/clipseg-rd64-refined''' lowerCamelCase : Dict = '''image_segmenter''' lowerCamelCase : Tuple = CLIPSegForImageSegmentation lowerCamelCase : Union[str, Any] = ['''image''', '''text'''] lowerCamelCase : List[Any] = ['''image'''] def __init__( self : Any , *UpperCAmelCase__ : int , **UpperCAmelCase__ : Optional[Any] ) -> Optional[int]: requires_backends(self , ['vision'] ) super().__init__(*UpperCAmelCase__ , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[Any] , UpperCAmelCase__ : "Image" , UpperCAmelCase__ : str ) -> Optional[Any]: return self.pre_processor(text=[label] , images=[image] , padding=UpperCAmelCase__ , return_tensors='pt' ) def __UpperCAmelCase ( self : List[Any] , UpperCAmelCase__ : Dict ) -> Tuple: with torch.no_grad(): lowerCAmelCase = self.model(**UpperCAmelCase__ ).logits return logits def __UpperCAmelCase ( self : List[Any] , UpperCAmelCase__ : Tuple ) -> Any: lowerCAmelCase = outputs.cpu().detach().numpy() lowerCAmelCase = 0 lowerCAmelCase = 1 return Image.fromarray((array * 2_5_5).astype(np.uinta ) )
4
'''simple docstring''' import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __snake_case =logging.get_logger(__name__) __snake_case ={ """vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_config_file""": """tokenizer_config.json""", } __snake_case ={ """vocab_file""": {"""facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json"""}, """merges_file""": {"""facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt"""}, """tokenizer_config_file""": { """facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json""" }, } __snake_case ={"""facebook/blenderbot-3B""": 128} class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : List[Any] = VOCAB_FILES_NAMES lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[Any] = ['''input_ids''', '''attention_mask'''] lowerCamelCase : List[Any] = BlenderbotTokenizer def __init__( self : Union[str, Any] , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : List[Any]=None , UpperCAmelCase__ : str="replace" , UpperCAmelCase__ : Dict="<s>" , UpperCAmelCase__ : Tuple="</s>" , UpperCAmelCase__ : Optional[Any]="</s>" , UpperCAmelCase__ : Any="<s>" , UpperCAmelCase__ : List[str]="<unk>" , UpperCAmelCase__ : int="<pad>" , UpperCAmelCase__ : Union[str, Any]="<mask>" , UpperCAmelCase__ : str=False , UpperCAmelCase__ : Union[str, Any]=True , **UpperCAmelCase__ : Optional[int] , ) -> int: super().__init__( UpperCAmelCase__ , UpperCAmelCase__ , tokenizer_file=UpperCAmelCase__ , errors=UpperCAmelCase__ , bos_token=UpperCAmelCase__ , eos_token=UpperCAmelCase__ , sep_token=UpperCAmelCase__ , cls_token=UpperCAmelCase__ , unk_token=UpperCAmelCase__ , pad_token=UpperCAmelCase__ , mask_token=UpperCAmelCase__ , add_prefix_space=UpperCAmelCase__ , trim_offsets=UpperCAmelCase__ , **UpperCAmelCase__ , ) lowerCAmelCase = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('add_prefix_space' , UpperCAmelCase__ ) != add_prefix_space: lowerCAmelCase = getattr(UpperCAmelCase__ , pre_tok_state.pop('type' ) ) lowerCAmelCase = add_prefix_space lowerCAmelCase = pre_tok_class(**UpperCAmelCase__ ) lowerCAmelCase = add_prefix_space lowerCAmelCase = 'post_processor' lowerCAmelCase = getattr(self.backend_tokenizer , UpperCAmelCase__ , UpperCAmelCase__ ) if tokenizer_component_instance: lowerCAmelCase = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: lowerCAmelCase = tuple(state['sep'] ) if "cls" in state: lowerCAmelCase = tuple(state['cls'] ) lowerCAmelCase = False if state.get('add_prefix_space' , UpperCAmelCase__ ) != add_prefix_space: lowerCAmelCase = add_prefix_space lowerCAmelCase = True if state.get('trim_offsets' , UpperCAmelCase__ ) != trim_offsets: lowerCAmelCase = trim_offsets lowerCAmelCase = True if changes_to_apply: lowerCAmelCase = getattr(UpperCAmelCase__ , state.pop('type' ) ) lowerCAmelCase = component_class(**UpperCAmelCase__ ) setattr(self.backend_tokenizer , UpperCAmelCase__ , UpperCAmelCase__ ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def __UpperCAmelCase ( self : Union[str, Any] ) -> str: if self._mask_token is None: if self.verbose: logger.error('Using mask_token, but it is not set yet.' ) return None return str(self._mask_token ) @mask_token.setter def __UpperCAmelCase ( self : int , UpperCAmelCase__ : Optional[Any] ) -> Tuple: lowerCAmelCase = AddedToken(UpperCAmelCase__ , lstrip=UpperCAmelCase__ , rstrip=UpperCAmelCase__ ) if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) else value lowerCAmelCase = value def __UpperCAmelCase ( self : Optional[Any] , *UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : List[str] ) -> BatchEncoding: lowerCAmelCase = kwargs.get('is_split_into_words' , UpperCAmelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*UpperCAmelCase__ , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : List[str] , *UpperCAmelCase__ : str , **UpperCAmelCase__ : List[str] ) -> BatchEncoding: lowerCAmelCase = kwargs.get('is_split_into_words' , UpperCAmelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*UpperCAmelCase__ , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : str , UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[str] = None ) -> Tuple[str]: lowerCAmelCase = self._tokenizer.model.save(UpperCAmelCase__ , name=UpperCAmelCase__ ) return tuple(UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> Any: return token_ids_a + [self.eos_token_id] def __UpperCAmelCase ( self : Union[str, Any] , UpperCAmelCase__ : "Conversation" ) -> List[int]: lowerCAmelCase = [] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(' ' + text ) else: # Generated responses should contain them already. inputs.append(UpperCAmelCase__ ) lowerCAmelCase = ' '.join(UpperCAmelCase__ ) lowerCAmelCase = self.encode(UpperCAmelCase__ ) if len(UpperCAmelCase__ ) > self.model_max_length: lowerCAmelCase = input_ids[-self.model_max_length :] logger.warning(F'''Trimmed input from conversation as it was longer than {self.model_max_length} tokens.''' ) return input_ids
4
1
'''simple docstring''' print((lambda quine: quine % quine)("""print((lambda quine: quine %% quine)(%r))"""))
4
'''simple docstring''' from __future__ import annotations from statistics import mean def a_ ( lowerCamelCase : list[int] , lowerCamelCase : list[int] , lowerCamelCase : int ): lowerCAmelCase = [0] * no_of_processes lowerCAmelCase = [0] * no_of_processes # Initialize remaining_time to waiting_time. for i in range(lowerCamelCase ): lowerCAmelCase = burst_time[i] lowerCAmelCase = [] lowerCAmelCase = 0 lowerCAmelCase = 0 # When processes are not completed, # A process whose arrival time has passed \ # and has remaining execution time is put into the ready_process. # The shortest process in the ready_process, target_process is executed. while completed != no_of_processes: lowerCAmelCase = [] lowerCAmelCase = -1 for i in range(lowerCamelCase ): if (arrival_time[i] <= total_time) and (remaining_time[i] > 0): ready_process.append(lowerCamelCase ) if len(lowerCamelCase ) > 0: lowerCAmelCase = ready_process[0] for i in ready_process: if remaining_time[i] < remaining_time[target_process]: lowerCAmelCase = i total_time += burst_time[target_process] completed += 1 lowerCAmelCase = 0 lowerCAmelCase = ( total_time - arrival_time[target_process] - burst_time[target_process] ) else: total_time += 1 return waiting_time def a_ ( lowerCamelCase : list[int] , lowerCamelCase : int , lowerCamelCase : list[int] ): lowerCAmelCase = [0] * no_of_processes for i in range(lowerCamelCase ): lowerCAmelCase = burst_time[i] + waiting_time[i] return turn_around_time if __name__ == "__main__": print("""[TEST CASE 01]""") __snake_case =4 __snake_case =[2, 5, 3, 7] __snake_case =[0, 0, 0, 0] __snake_case =calculate_waitingtime(arrival_time, burst_time, no_of_processes) __snake_case =calculate_turnaroundtime( burst_time, no_of_processes, waiting_time ) # Printing the Result print("""PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time""") for i, process_id in enumerate(list(range(1, 5))): print( F'''{process_id}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t''' F'''{waiting_time[i]}\t\t\t\t{turn_around_time[i]}''' ) print(F'''\nAverage waiting time = {mean(waiting_time):.5f}''') print(F'''Average turnaround time = {mean(turn_around_time):.5f}''')
4
1
'''simple docstring''' import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import DeiTConfig, DeiTForImageClassificationWithTeacher, DeiTImageProcessor from transformers.utils import logging logging.set_verbosity_info() __snake_case =logging.get_logger(__name__) def a_ ( lowerCamelCase : Union[str, Any] , lowerCamelCase : List[str]=False ): lowerCAmelCase = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((f'''blocks.{i}.norm1.weight''', f'''deit.encoder.layer.{i}.layernorm_before.weight''') ) rename_keys.append((f'''blocks.{i}.norm1.bias''', f'''deit.encoder.layer.{i}.layernorm_before.bias''') ) rename_keys.append((f'''blocks.{i}.attn.proj.weight''', f'''deit.encoder.layer.{i}.attention.output.dense.weight''') ) rename_keys.append((f'''blocks.{i}.attn.proj.bias''', f'''deit.encoder.layer.{i}.attention.output.dense.bias''') ) rename_keys.append((f'''blocks.{i}.norm2.weight''', f'''deit.encoder.layer.{i}.layernorm_after.weight''') ) rename_keys.append((f'''blocks.{i}.norm2.bias''', f'''deit.encoder.layer.{i}.layernorm_after.bias''') ) rename_keys.append((f'''blocks.{i}.mlp.fc1.weight''', f'''deit.encoder.layer.{i}.intermediate.dense.weight''') ) rename_keys.append((f'''blocks.{i}.mlp.fc1.bias''', f'''deit.encoder.layer.{i}.intermediate.dense.bias''') ) rename_keys.append((f'''blocks.{i}.mlp.fc2.weight''', f'''deit.encoder.layer.{i}.output.dense.weight''') ) rename_keys.append((f'''blocks.{i}.mlp.fc2.bias''', f'''deit.encoder.layer.{i}.output.dense.bias''') ) # projection layer + position embeddings rename_keys.extend( [ ('cls_token', 'deit.embeddings.cls_token'), ('dist_token', 'deit.embeddings.distillation_token'), ('patch_embed.proj.weight', 'deit.embeddings.patch_embeddings.projection.weight'), ('patch_embed.proj.bias', 'deit.embeddings.patch_embeddings.projection.bias'), ('pos_embed', 'deit.embeddings.position_embeddings'), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('norm.weight', 'layernorm.weight'), ('norm.bias', 'layernorm.bias'), ('pre_logits.fc.weight', 'pooler.dense.weight'), ('pre_logits.fc.bias', 'pooler.dense.bias'), ] ) # if just the base model, we should remove "deit" from all keys that start with "deit" lowerCAmelCase = [(pair[0], pair[1][4:]) if pair[1].startswith('deit' ) else pair for pair in rename_keys] else: # layernorm + classification heads rename_keys.extend( [ ('norm.weight', 'deit.layernorm.weight'), ('norm.bias', 'deit.layernorm.bias'), ('head.weight', 'cls_classifier.weight'), ('head.bias', 'cls_classifier.bias'), ('head_dist.weight', 'distillation_classifier.weight'), ('head_dist.bias', 'distillation_classifier.bias'), ] ) return rename_keys def a_ ( lowerCamelCase : Tuple , lowerCamelCase : Union[str, Any] , lowerCamelCase : Union[str, Any]=False ): for i in range(config.num_hidden_layers ): if base_model: lowerCAmelCase = '' else: lowerCAmelCase = 'deit.' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) lowerCAmelCase = state_dict.pop(f'''blocks.{i}.attn.qkv.weight''' ) lowerCAmelCase = state_dict.pop(f'''blocks.{i}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict lowerCAmelCase = in_proj_weight[ : config.hidden_size, : ] lowerCAmelCase = in_proj_bias[: config.hidden_size] lowerCAmelCase = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] lowerCAmelCase = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] lowerCAmelCase = in_proj_weight[ -config.hidden_size :, : ] lowerCAmelCase = in_proj_bias[-config.hidden_size :] def a_ ( lowerCamelCase : Dict , lowerCamelCase : List[Any] , lowerCamelCase : str ): lowerCAmelCase = dct.pop(lowerCamelCase ) lowerCAmelCase = val def a_ ( ): lowerCAmelCase = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowerCAmelCase = Image.open(requests.get(lowerCamelCase , stream=lowerCamelCase ).raw ) return im @torch.no_grad() def a_ ( lowerCamelCase : List[str] , lowerCamelCase : Dict ): lowerCAmelCase = DeiTConfig() # all deit models have fine-tuned heads lowerCAmelCase = False # dataset (fine-tuned on ImageNet 2012), patch_size and image_size lowerCAmelCase = 1000 lowerCAmelCase = 'huggingface/label-files' lowerCAmelCase = 'imagenet-1k-id2label.json' lowerCAmelCase = json.load(open(hf_hub_download(lowerCamelCase , lowerCamelCase , repo_type='dataset' ) , 'r' ) ) lowerCAmelCase = {int(lowerCamelCase ): v for k, v in idalabel.items()} lowerCAmelCase = idalabel lowerCAmelCase = {v: k for k, v in idalabel.items()} lowerCAmelCase = int(deit_name[-6:-4] ) lowerCAmelCase = int(deit_name[-3:] ) # size of the architecture if deit_name[9:].startswith('tiny' ): lowerCAmelCase = 192 lowerCAmelCase = 768 lowerCAmelCase = 12 lowerCAmelCase = 3 elif deit_name[9:].startswith('small' ): lowerCAmelCase = 384 lowerCAmelCase = 1536 lowerCAmelCase = 12 lowerCAmelCase = 6 if deit_name[9:].startswith('base' ): pass elif deit_name[4:].startswith('large' ): lowerCAmelCase = 1024 lowerCAmelCase = 4096 lowerCAmelCase = 24 lowerCAmelCase = 16 # load original model from timm lowerCAmelCase = timm.create_model(lowerCamelCase , pretrained=lowerCamelCase ) timm_model.eval() # load state_dict of original model, remove and rename some keys lowerCAmelCase = timm_model.state_dict() lowerCAmelCase = create_rename_keys(lowerCamelCase , lowerCamelCase ) for src, dest in rename_keys: rename_key(lowerCamelCase , lowerCamelCase , lowerCamelCase ) read_in_q_k_v(lowerCamelCase , lowerCamelCase , lowerCamelCase ) # load HuggingFace model lowerCAmelCase = DeiTForImageClassificationWithTeacher(lowerCamelCase ).eval() model.load_state_dict(lowerCamelCase ) # Check outputs on an image, prepared by DeiTImageProcessor lowerCAmelCase = int( (256 / 224) * config.image_size ) # to maintain same ratio w.r.t. 224 images, see https://github.com/facebookresearch/deit/blob/ab5715372db8c6cad5740714b2216d55aeae052e/datasets.py#L103 lowerCAmelCase = DeiTImageProcessor(size=lowerCamelCase , crop_size=config.image_size ) lowerCAmelCase = image_processor(images=prepare_img() , return_tensors='pt' ) lowerCAmelCase = encoding['pixel_values'] lowerCAmelCase = model(lowerCamelCase ) lowerCAmelCase = timm_model(lowerCamelCase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(lowerCamelCase , outputs.logits , atol=1e-3 ) Path(lowerCamelCase ).mkdir(exist_ok=lowerCamelCase ) print(f'''Saving model {deit_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 __name__ == "__main__": __snake_case =argparse.ArgumentParser() # Required parameters parser.add_argument( """--deit_name""", default="""vit_deit_base_distilled_patch16_224""", type=str, help="""Name of the DeiT timm model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) __snake_case =parser.parse_args() convert_deit_checkpoint(args.deit_name, args.pytorch_dump_folder_path)
4
'''simple docstring''' import gc import unittest import numpy as np import torch from diffusers import StableDiffusionKDiffusionPipeline from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : Optional[int] ) -> int: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self : Tuple ) -> Any: lowerCAmelCase = StableDiffusionKDiffusionPipeline.from_pretrained('CompVis/stable-diffusion-v1-4' ) lowerCAmelCase = sd_pipe.to(UpperCAmelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) sd_pipe.set_scheduler('sample_euler' ) lowerCAmelCase = 'A painting of a squirrel eating a burger' lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = sd_pipe([prompt] , generator=UpperCAmelCase__ , guidance_scale=9.0 , num_inference_steps=2_0 , output_type='np' ) lowerCAmelCase = output.images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase = np.array([0.0_447, 0.0_492, 0.0_468, 0.0_408, 0.0_383, 0.0_408, 0.0_354, 0.0_380, 0.0_339] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __UpperCAmelCase ( self : List[str] ) -> Dict: lowerCAmelCase = StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowerCAmelCase = sd_pipe.to(UpperCAmelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) sd_pipe.set_scheduler('sample_euler' ) lowerCAmelCase = 'A painting of a squirrel eating a burger' lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = sd_pipe([prompt] , generator=UpperCAmelCase__ , guidance_scale=9.0 , num_inference_steps=2_0 , output_type='np' ) lowerCAmelCase = output.images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase = np.array([0.1_237, 0.1_320, 0.1_438, 0.1_359, 0.1_390, 0.1_132, 0.1_277, 0.1_175, 0.1_112] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-1 def __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: lowerCAmelCase = StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowerCAmelCase = sd_pipe.to(UpperCAmelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) sd_pipe.set_scheduler('sample_dpmpp_2m' ) lowerCAmelCase = 'A painting of a squirrel eating a burger' lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = sd_pipe( [prompt] , generator=UpperCAmelCase__ , guidance_scale=7.5 , num_inference_steps=1_5 , output_type='np' , use_karras_sigmas=UpperCAmelCase__ , ) lowerCAmelCase = output.images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase = np.array( [0.11_381_689, 0.12_112_921, 0.1_389_457, 0.12_549_606, 0.1_244_964, 0.10_831_517, 0.11_562_866, 0.10_867_816, 0.10_499_048] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
4
1
'''simple docstring''' import unittest from queue import Empty from threading import Thread from transformers import AutoTokenizer, TextIteratorStreamer, TextStreamer, is_torch_available from transformers.testing_utils import CaptureStdout, require_torch, torch_device from ..test_modeling_common import ids_tensor if is_torch_available(): import torch from transformers import AutoModelForCausalLM @require_torch class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : Tuple ) -> Optional[int]: lowerCAmelCase = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2' ) lowerCAmelCase = AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2' ).to(UpperCAmelCase__ ) lowerCAmelCase = -1 lowerCAmelCase = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(UpperCAmelCase__ ) lowerCAmelCase = model.generate(UpperCAmelCase__ , max_new_tokens=1_0 , do_sample=UpperCAmelCase__ ) lowerCAmelCase = tokenizer.decode(greedy_ids[0] ) with CaptureStdout() as cs: lowerCAmelCase = TextStreamer(UpperCAmelCase__ ) model.generate(UpperCAmelCase__ , max_new_tokens=1_0 , do_sample=UpperCAmelCase__ , streamer=UpperCAmelCase__ ) # The greedy text should be printed to stdout, except for the final "\n" in the streamer lowerCAmelCase = cs.out[:-1] self.assertEqual(UpperCAmelCase__ , UpperCAmelCase__ ) def __UpperCAmelCase ( self : Any ) -> Any: lowerCAmelCase = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2' ) lowerCAmelCase = AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2' ).to(UpperCAmelCase__ ) lowerCAmelCase = -1 lowerCAmelCase = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(UpperCAmelCase__ ) lowerCAmelCase = model.generate(UpperCAmelCase__ , max_new_tokens=1_0 , do_sample=UpperCAmelCase__ ) lowerCAmelCase = tokenizer.decode(greedy_ids[0] ) lowerCAmelCase = TextIteratorStreamer(UpperCAmelCase__ ) lowerCAmelCase = {'input_ids': input_ids, 'max_new_tokens': 1_0, 'do_sample': False, 'streamer': streamer} lowerCAmelCase = Thread(target=model.generate , kwargs=UpperCAmelCase__ ) thread.start() lowerCAmelCase = '' for new_text in streamer: streamer_text += new_text self.assertEqual(UpperCAmelCase__ , UpperCAmelCase__ ) def __UpperCAmelCase ( self : Dict ) -> str: lowerCAmelCase = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2' ) lowerCAmelCase = AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2' ).to(UpperCAmelCase__ ) lowerCAmelCase = -1 lowerCAmelCase = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(UpperCAmelCase__ ) lowerCAmelCase = model.generate(UpperCAmelCase__ , max_new_tokens=1_0 , do_sample=UpperCAmelCase__ ) lowerCAmelCase = greedy_ids[:, input_ids.shape[1] :] lowerCAmelCase = tokenizer.decode(new_greedy_ids[0] ) with CaptureStdout() as cs: lowerCAmelCase = TextStreamer(UpperCAmelCase__ , skip_prompt=UpperCAmelCase__ ) model.generate(UpperCAmelCase__ , max_new_tokens=1_0 , do_sample=UpperCAmelCase__ , streamer=UpperCAmelCase__ ) # The greedy text should be printed to stdout, except for the final "\n" in the streamer lowerCAmelCase = cs.out[:-1] self.assertEqual(UpperCAmelCase__ , UpperCAmelCase__ ) def __UpperCAmelCase ( self : List[Any] ) -> Optional[int]: # Tests that we can pass `decode_kwargs` to the streamer to control how the tokens are decoded. Must be tested # with actual models -- the dummy models' tokenizers are not aligned with their models, and # `skip_special_tokens=True` has no effect on them lowerCAmelCase = AutoTokenizer.from_pretrained('distilgpt2' ) lowerCAmelCase = AutoModelForCausalLM.from_pretrained('distilgpt2' ).to(UpperCAmelCase__ ) lowerCAmelCase = -1 lowerCAmelCase = torch.ones((1, 5) , device=UpperCAmelCase__ ).long() * model.config.bos_token_id with CaptureStdout() as cs: lowerCAmelCase = TextStreamer(UpperCAmelCase__ , skip_special_tokens=UpperCAmelCase__ ) model.generate(UpperCAmelCase__ , max_new_tokens=1 , do_sample=UpperCAmelCase__ , streamer=UpperCAmelCase__ ) # The prompt contains a special token, so the streamer should not print it. As such, the output text, when # re-tokenized, must only contain one token lowerCAmelCase = cs.out[:-1] # Remove the final "\n" lowerCAmelCase = tokenizer(UpperCAmelCase__ , return_tensors='pt' ) self.assertEqual(streamer_text_tokenized.input_ids.shape , (1, 1) ) def __UpperCAmelCase ( self : Optional[Any] ) -> List[Any]: lowerCAmelCase = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2' ) lowerCAmelCase = AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2' ).to(UpperCAmelCase__ ) lowerCAmelCase = -1 lowerCAmelCase = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(UpperCAmelCase__ ) lowerCAmelCase = TextIteratorStreamer(UpperCAmelCase__ , timeout=0.001 ) lowerCAmelCase = {'input_ids': input_ids, 'max_new_tokens': 1_0, 'do_sample': False, 'streamer': streamer} lowerCAmelCase = Thread(target=model.generate , kwargs=UpperCAmelCase__ ) thread.start() # The streamer will timeout after 0.001 seconds, so an exception will be raised with self.assertRaises(UpperCAmelCase__ ): lowerCAmelCase = '' for new_text in streamer: streamer_text += new_text
4
'''simple docstring''' # Copyright (c) 2021-, NVIDIA CORPORATION. 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. #################################################################################################### # # Note: If when running this conversion script you're getting an exception: # ModuleNotFoundError: No module named 'megatron.model.enums' # you need to tell python where to find the clone of Megatron-LM, e.g.: # # cd /tmp # git clone https://github.com/NVIDIA/Megatron-LM # PYTHONPATH=/tmp/Megatron-LM python src/transformers/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py ... # # if you already have it cloned elsewhere, simply adjust the path to the existing path # # If the training was done using a Megatron-LM fork, e.g., # https://github.com/microsoft/Megatron-DeepSpeed/ then chances are that you need to have that one # in your path, i.e., /path/to/Megatron-DeepSpeed/ # import argparse import os import re import zipfile import torch from transformers import AutoTokenizer, GPTaConfig def a_ ( lowerCamelCase : int , lowerCamelCase : Optional[Any] , lowerCamelCase : Union[str, Any]=0 ): # Format the message. if name is None: lowerCAmelCase = None else: lowerCAmelCase = '.' * max(0 , spaces - 2 ) + '# {:' + str(50 - spaces ) + 's}' lowerCAmelCase = fmt.format(lowerCamelCase ) # Print and recurse (if needed). if isinstance(lowerCamelCase , lowerCamelCase ): if msg is not None: print(lowerCamelCase ) for k in val.keys(): recursive_print(lowerCamelCase , val[k] , spaces + 2 ) elif isinstance(lowerCamelCase , torch.Tensor ): print(lowerCamelCase , ':' , val.size() ) else: print(lowerCamelCase , ':' , lowerCamelCase ) def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : List[str] , lowerCamelCase : List[Any] , lowerCamelCase : Dict , lowerCamelCase : Tuple ): # Permutes layout of param tensor to [num_splits * num_heads * hidden_size, :] # for compatibility with later versions of NVIDIA Megatron-LM. # The inverse operation is performed inside Megatron-LM to read checkpoints: # https://github.com/NVIDIA/Megatron-LM/blob/v2.4/megatron/checkpointing.py#L209 # If param is the weight tensor of the self-attention block, the returned tensor # will have to be transposed one more time to be read by HuggingFace GPT2. lowerCAmelCase = param.size() if checkpoint_version == 1.0: # version 1.0 stores [num_heads * hidden_size * num_splits, :] lowerCAmelCase = (num_heads, hidden_size, num_splits) + input_shape[1:] lowerCAmelCase = param.view(*lowerCamelCase ) lowerCAmelCase = param.transpose(0 , 2 ) lowerCAmelCase = param.transpose(1 , 2 ).contiguous() elif checkpoint_version >= 2.0: # other versions store [num_heads * num_splits * hidden_size, :] lowerCAmelCase = (num_heads, num_splits, hidden_size) + input_shape[1:] lowerCAmelCase = param.view(*lowerCamelCase ) lowerCAmelCase = param.transpose(0 , 1 ).contiguous() lowerCAmelCase = param.view(*lowerCamelCase ) return param def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Optional[int] , lowerCamelCase : str ): # The converted output model. lowerCAmelCase = {} # old versions did not store training args lowerCAmelCase = input_state_dict.get('args' , lowerCamelCase ) if ds_args is not None: # do not make the user write a config file when the exact dimensions/sizes are already in the checkpoint # from pprint import pprint # pprint(vars(ds_args)) lowerCAmelCase = ds_args.padded_vocab_size lowerCAmelCase = ds_args.max_position_embeddings lowerCAmelCase = ds_args.hidden_size lowerCAmelCase = ds_args.num_layers lowerCAmelCase = ds_args.num_attention_heads lowerCAmelCase = ds_args.ffn_hidden_size # pprint(config) # The number of heads. lowerCAmelCase = config.n_head # The hidden_size per head. lowerCAmelCase = config.n_embd // config.n_head # Megatron-LM checkpoint version if "checkpoint_version" in input_state_dict.keys(): lowerCAmelCase = input_state_dict['checkpoint_version'] else: lowerCAmelCase = 0.0 # The model. lowerCAmelCase = input_state_dict['model'] # The language model. lowerCAmelCase = model['language_model'] # The embeddings. lowerCAmelCase = lm['embedding'] # The word embeddings. lowerCAmelCase = embeddings['word_embeddings']['weight'] # Truncate the embedding table to vocab_size rows. lowerCAmelCase = word_embeddings[: config.vocab_size, :] lowerCAmelCase = word_embeddings # The position embeddings. lowerCAmelCase = embeddings['position_embeddings']['weight'] # Read the causal mask dimension (seqlen). [max_sequence_length, hidden_size] lowerCAmelCase = pos_embeddings.size(0 ) if n_positions != config.n_positions: raise ValueError( f'''pos_embeddings.max_sequence_length={n_positions} and config.n_positions={config.n_positions} don\'t match''' ) # Store the position embeddings. lowerCAmelCase = pos_embeddings # The transformer. lowerCAmelCase = lm['transformer'] if 'transformer' in lm.keys() else lm['encoder'] # The regex to extract layer names. lowerCAmelCase = re.compile(R'layers\.(\d+)\.([a-z0-9_.]+)\.([a-z]+)' ) # The simple map of names for "automated" rules. lowerCAmelCase = { 'attention.dense': '.attn.c_proj.', 'self_attention.dense': '.attn.c_proj.', 'mlp.dense_h_to_4h': '.mlp.c_fc.', 'mlp.dense_4h_to_h': '.mlp.c_proj.', } # Extract the layers. for key, val in transformer.items(): # Match the name. lowerCAmelCase = layer_re.match(lowerCamelCase ) # Stop if that's not a layer if m is None: break # The index of the layer. lowerCAmelCase = int(m.group(1 ) ) # The name of the operation. lowerCAmelCase = m.group(2 ) # Is it a weight or a bias? lowerCAmelCase = m.group(3 ) # The name of the layer. lowerCAmelCase = f'''transformer.h.{layer_idx}''' # For layernorm(s), simply store the layer norm. if op_name.endswith('layernorm' ): lowerCAmelCase = 'ln_1' if op_name.startswith('input' ) else 'ln_2' lowerCAmelCase = val # Transpose the QKV matrix. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "weight": # Insert a tensor of 1x1xDxD bias. lowerCAmelCase = torch.tril(torch.ones((n_positions, n_positions) , dtype=torch.floataa ) ).view( 1 , 1 , lowerCamelCase , lowerCamelCase ) lowerCAmelCase = causal_mask # Insert a "dummy" tensor for masked_bias. lowerCAmelCase = torch.tensor(-1e4 , dtype=torch.floataa ) lowerCAmelCase = masked_bias lowerCAmelCase = fix_query_key_value_ordering(lowerCamelCase , lowerCamelCase , 3 , lowerCamelCase , lowerCamelCase ) # Megatron stores (3*D) x D but transformers-GPT2 expects D x 3*D. lowerCAmelCase = out_val.transpose(0 , 1 ).contiguous() # Store. lowerCAmelCase = out_val # Transpose the bias. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "bias": lowerCAmelCase = fix_query_key_value_ordering(lowerCamelCase , lowerCamelCase , 3 , lowerCamelCase , lowerCamelCase ) # Store. No change of shape. lowerCAmelCase = out_val # Transpose the weights. elif weight_or_bias == "weight": lowerCAmelCase = megatron_to_transformers[op_name] lowerCAmelCase = val.transpose(0 , 1 ) # Copy the bias. elif weight_or_bias == "bias": lowerCAmelCase = megatron_to_transformers[op_name] lowerCAmelCase = val # DEBUG. assert config.n_layer == layer_idx + 1 # The final layernorm. lowerCAmelCase = transformer['final_layernorm.weight'] lowerCAmelCase = transformer['final_layernorm.bias'] # For LM head, transformers' wants the matrix to weight embeddings. lowerCAmelCase = word_embeddings # It should be done! return output_state_dict def a_ ( ): # Create the argument parser. lowerCAmelCase = argparse.ArgumentParser() parser.add_argument('--print-checkpoint-structure' , action='store_true' ) parser.add_argument( 'path_to_checkpoint' , type=lowerCamelCase , help='Path to the checkpoint file (.zip archive or direct .pt file)' , ) parser.add_argument( '--config_file' , default='' , type=lowerCamelCase , help='An optional config json file describing the pre-trained model.' , ) lowerCAmelCase = parser.parse_args() # Extract the basename. lowerCAmelCase = os.path.dirname(args.path_to_checkpoint ) # Load the model. # the .zip is very optional, let's keep it for backward compatibility print(f'''Extracting PyTorch state dictionary from {args.path_to_checkpoint}''' ) if args.path_to_checkpoint.endswith('.zip' ): with zipfile.ZipFile(args.path_to_checkpoint , 'r' ) as checkpoint: with checkpoint.open('release/mp_rank_00/model_optim_rng.pt' ) as pytorch_dict: lowerCAmelCase = torch.load(lowerCamelCase , map_location='cpu' ) else: lowerCAmelCase = torch.load(args.path_to_checkpoint , map_location='cpu' ) lowerCAmelCase = input_state_dict.get('args' , lowerCamelCase ) # Read the config, or default to the model released by NVIDIA. if args.config_file == "": if ds_args is not None: if ds_args.bias_gelu_fusion: lowerCAmelCase = 'gelu_fast' elif ds_args.openai_gelu: lowerCAmelCase = 'gelu_new' else: lowerCAmelCase = 'gelu' else: # in the very early days this used to be "gelu_new" lowerCAmelCase = 'gelu_new' # Spell out all parameters in case the defaults change. lowerCAmelCase = GPTaConfig( vocab_size=50257 , n_positions=1024 , n_embd=1024 , n_layer=24 , n_head=16 , n_inner=4096 , activation_function=lowerCamelCase , resid_pdrop=0.1 , embd_pdrop=0.1 , attn_pdrop=0.1 , layer_norm_epsilon=1e-5 , initializer_range=0.02 , summary_type='cls_index' , summary_use_proj=lowerCamelCase , summary_activation=lowerCamelCase , summary_proj_to_labels=lowerCamelCase , summary_first_dropout=0.1 , scale_attn_weights=lowerCamelCase , use_cache=lowerCamelCase , bos_token_id=50256 , eos_token_id=50256 , ) else: lowerCAmelCase = GPTaConfig.from_json_file(args.config_file ) lowerCAmelCase = ['GPT2LMHeadModel'] # Convert. print('Converting' ) lowerCAmelCase = convert_megatron_checkpoint(lowerCamelCase , lowerCamelCase , lowerCamelCase ) # Print the structure of converted state dict. if args.print_checkpoint_structure: recursive_print(lowerCamelCase , lowerCamelCase ) # Add tokenizer class info to config # see https://github.com/huggingface/transformers/issues/13906) if ds_args is not None: lowerCAmelCase = ds_args.tokenizer_type if tokenizer_type == "GPT2BPETokenizer": lowerCAmelCase = 'gpt2' elif tokenizer_type == "PretrainedFromHF": lowerCAmelCase = ds_args.tokenizer_name_or_path else: raise ValueError(f'''Unrecognized tokenizer_type {tokenizer_type}''' ) else: lowerCAmelCase = 'gpt2' lowerCAmelCase = AutoTokenizer.from_pretrained(lowerCamelCase ) lowerCAmelCase = type(lowerCamelCase ).__name__ lowerCAmelCase = tokenizer_class # Store the config to file. print('Saving config' ) config.save_pretrained(lowerCamelCase ) # Save tokenizer based on args print(f'''Adding {tokenizer_class} tokenizer files''' ) tokenizer.save_pretrained(lowerCamelCase ) # Store the state_dict to file. lowerCAmelCase = os.path.join(lowerCamelCase , 'pytorch_model.bin' ) print(f'''Saving checkpoint to "{output_checkpoint_file}"''' ) torch.save(lowerCamelCase , lowerCamelCase ) #################################################################################################### if __name__ == "__main__": main() ####################################################################################################
4
1
'''simple docstring''' import inspect import re from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py __snake_case ="""src/transformers""" # This is to make sure the transformers module imported is the one in the repo. __snake_case =direct_transformers_import(PATH_TO_TRANSFORMERS) __snake_case =transformers.models.auto.configuration_auto.CONFIG_MAPPING # Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`. # For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)` __snake_case =re.compile(R"""\[(.+?)\]\((https://huggingface\.co/.+?)\)""") __snake_case ={ """DecisionTransformerConfig""", """EncoderDecoderConfig""", """MusicgenConfig""", """RagConfig""", """SpeechEncoderDecoderConfig""", """TimmBackboneConfig""", """VisionEncoderDecoderConfig""", """VisionTextDualEncoderConfig""", """LlamaConfig""", } def a_ ( lowerCamelCase : int ): lowerCAmelCase = None # source code of `config_class` lowerCAmelCase = inspect.getsource(lowerCamelCase ) lowerCAmelCase = _re_checkpoint.findall(lowerCamelCase ) # Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link. # For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')` for ckpt_name, ckpt_link in checkpoints: # allow the link to end with `/` if ckpt_link.endswith('/' ): lowerCAmelCase = ckpt_link[:-1] # verify the checkpoint name corresponds to the checkpoint link lowerCAmelCase = f'''https://huggingface.co/{ckpt_name}''' if ckpt_link == ckpt_link_from_name: lowerCAmelCase = ckpt_name break return checkpoint def a_ ( ): lowerCAmelCase = [] for config_class in list(CONFIG_MAPPING.values() ): # Skip deprecated models if "models.deprecated" in config_class.__module__: continue lowerCAmelCase = get_checkpoint_from_config_class(lowerCamelCase ) lowerCAmelCase = config_class.__name__ if checkpoint is None and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK: configs_without_checkpoint.append(lowerCamelCase ) if len(lowerCamelCase ) > 0: lowerCAmelCase = '\n'.join(sorted(lowerCamelCase ) ) raise ValueError(f'''The following configurations don\'t contain any valid checkpoint:\n{message}''' ) if __name__ == "__main__": check_config_docstrings_have_checkpoints()
4
'''simple docstring''' from __future__ import annotations from typing import Any class UpperCAmelCase_ : def __init__( self : Optional[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : float = 0 ) -> None: lowerCAmelCase , lowerCAmelCase = row, column lowerCAmelCase = [[default_value for c in range(UpperCAmelCase__ )] for r in range(UpperCAmelCase__ )] def __str__( self : List[str] ) -> str: lowerCAmelCase = F'''Matrix consist of {self.row} rows and {self.column} columns\n''' # Make string identifier lowerCAmelCase = 0 for row_vector in self.array: for obj in row_vector: lowerCAmelCase = max(UpperCAmelCase__ , len(str(UpperCAmelCase__ ) ) ) lowerCAmelCase = F'''%{max_element_length}s''' # Make string and return def single_line(UpperCAmelCase__ : list[float] ) -> str: nonlocal string_format_identifier lowerCAmelCase = '[' line += ", ".join(string_format_identifier % (obj,) for obj in row_vector ) line += "]" return line s += "\n".join(single_line(UpperCAmelCase__ ) for row_vector in self.array ) return s def __repr__( self : List[str] ) -> str: return str(self ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : tuple[int, int] ) -> bool: if not (isinstance(UpperCAmelCase__ , (list, tuple) ) and len(UpperCAmelCase__ ) == 2): return False elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False else: return True def __getitem__( self : Any , UpperCAmelCase__ : tuple[int, int] ) -> Any: assert self.validate_indicies(UpperCAmelCase__ ) return self.array[loc[0]][loc[1]] def __setitem__( self : Dict , UpperCAmelCase__ : tuple[int, int] , UpperCAmelCase__ : float ) -> None: assert self.validate_indicies(UpperCAmelCase__ ) lowerCAmelCase = value def __add__( self : Any , UpperCAmelCase__ : Matrix ) -> Matrix: assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) assert self.row == another.row and self.column == another.column # Add lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] + another[r, c] return result def __neg__( self : int ) -> Matrix: lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = -self[r, c] return result def __sub__( self : str , UpperCAmelCase__ : Matrix ) -> Matrix: return self + (-another) def __mul__( self : str , UpperCAmelCase__ : int | float | Matrix ) -> Matrix: if isinstance(UpperCAmelCase__ , (int, float) ): # Scalar multiplication lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] * another return result elif isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): # Matrix multiplication assert self.column == another.row lowerCAmelCase = Matrix(self.row , another.column ) for r in range(self.row ): for c in range(another.column ): for i in range(self.column ): result[r, c] += self[r, i] * another[i, c] return result else: lowerCAmelCase = F'''Unsupported type given for another ({type(UpperCAmelCase__ )})''' raise TypeError(UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[Any] ) -> Matrix: lowerCAmelCase = Matrix(self.column , self.row ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] return result def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : Matrix , UpperCAmelCase__ : Matrix ) -> Any: assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) and isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) assert self.row == self.column == u.row == v.row # u, v should be column vector assert u.column == v.column == 1 # u, v should be column vector # Calculate lowerCAmelCase = v.transpose() lowerCAmelCase = (v_t * self * u)[0, 0] + 1 if numerator_factor == 0: return None # It's not invertable return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor)) # Testing if __name__ == "__main__": def a_ ( ): # a^(-1) lowerCAmelCase = Matrix(3 , 3 , 0 ) for i in range(3 ): lowerCAmelCase = 1 print(f'''a^(-1) is {ainv}''' ) # u, v lowerCAmelCase = Matrix(3 , 1 , 0 ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = 1, 2, -3 lowerCAmelCase = Matrix(3 , 1 , 0 ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = 4, -2, 5 print(f'''u is {u}''' ) print(f'''v is {v}''' ) print(f'''uv^T is {u * v.transpose()}''' ) # Sherman Morrison print(f'''(a + uv^T)^(-1) is {ainv.sherman_morrison(lowerCamelCase , lowerCamelCase )}''' ) def a_ ( ): import doctest doctest.testmod() testa()
4
1
'''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 UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Dict = DownBlockaD # noqa F405 lowerCamelCase : List[str] = '''down''' def __UpperCAmelCase ( self : Optional[int] ) -> Any: lowerCAmelCase = [-0.0_232, -0.9_869, 0.8_054, -0.0_637, -0.1_688, -1.4_264, 0.4_470, -1.3_394, 0.0_904] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : int = ResnetDownsampleBlockaD # noqa F405 lowerCamelCase : int = '''down''' def __UpperCAmelCase ( self : Tuple ) -> Dict: lowerCAmelCase = [0.0_710, 0.2_410, -0.7_320, -1.0_757, -1.1_343, 0.3_540, -0.0_133, -0.2_576, 0.0_948] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Optional[int] = AttnDownBlockaD # noqa F405 lowerCamelCase : Union[str, Any] = '''down''' def __UpperCAmelCase ( self : List[Any] ) -> List[str]: lowerCAmelCase = [0.0_636, 0.8_964, -0.6_234, -1.0_131, 0.0_844, 0.4_935, 0.3_437, 0.0_911, -0.2_957] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : str = CrossAttnDownBlockaD # noqa F405 lowerCamelCase : List[str] = '''down''' def __UpperCAmelCase ( self : Dict ) -> Dict: lowerCAmelCase , lowerCAmelCase = super().prepare_init_args_and_inputs_for_common() lowerCAmelCase = 3_2 return init_dict, inputs_dict def __UpperCAmelCase ( self : List[Any] ) -> Union[str, Any]: lowerCAmelCase = [0.2_238, -0.7_396, -0.2_255, -0.3_829, 0.1_925, 1.1_665, 0.0_603, -0.7_295, 0.1_983] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Optional[int] = SimpleCrossAttnDownBlockaD # noqa F405 lowerCamelCase : str = '''down''' @property def __UpperCAmelCase ( self : Dict ) -> str: return super().get_dummy_input(include_encoder_hidden_states=UpperCAmelCase__ ) def __UpperCAmelCase ( self : int ) -> str: lowerCAmelCase , lowerCAmelCase = super().prepare_init_args_and_inputs_for_common() lowerCAmelCase = 3_2 return init_dict, inputs_dict @unittest.skipIf(torch_device == 'mps' , 'MPS result is not consistent' ) def __UpperCAmelCase ( self : Tuple ) -> Union[str, Any]: lowerCAmelCase = [0.7_921, -0.0_992, -0.1_962, -0.7_695, -0.4_242, 0.7_804, 0.4_737, 0.2_765, 0.3_338] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : List[str] = SkipDownBlockaD # noqa F405 lowerCamelCase : int = '''down''' @property def __UpperCAmelCase ( self : str ) -> str: return super().get_dummy_input(include_skip_sample=UpperCAmelCase__ ) def __UpperCAmelCase ( self : int ) -> int: lowerCAmelCase = [-0.0_845, -0.2_087, -0.2_465, 0.0_971, 0.1_900, -0.0_484, 0.2_664, 0.4_179, 0.5_069] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Union[str, Any] = AttnSkipDownBlockaD # noqa F405 lowerCamelCase : str = '''down''' @property def __UpperCAmelCase ( self : Tuple ) -> str: return super().get_dummy_input(include_skip_sample=UpperCAmelCase__ ) def __UpperCAmelCase ( self : int ) -> str: lowerCAmelCase = [0.5_539, 0.1_609, 0.4_924, 0.0_537, -0.1_995, 0.4_050, 0.0_979, -0.2_721, -0.0_642] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Optional[Any] = DownEncoderBlockaD # noqa F405 lowerCamelCase : Optional[Any] = '''down''' @property def __UpperCAmelCase ( self : Tuple ) -> Dict: return super().get_dummy_input(include_temb=UpperCAmelCase__ ) def __UpperCAmelCase ( self : Tuple ) -> Union[str, Any]: lowerCAmelCase = { 'in_channels': 3_2, 'out_channels': 3_2, } lowerCAmelCase = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self : Union[str, Any] ) -> Optional[int]: lowerCAmelCase = [1.1_102, 0.5_302, 0.4_872, -0.0_023, -0.8_042, 0.0_483, -0.3_489, -0.5_632, 0.7_626] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : List[Any] = AttnDownEncoderBlockaD # noqa F405 lowerCamelCase : Dict = '''down''' @property def __UpperCAmelCase ( self : Union[str, Any] ) -> List[str]: return super().get_dummy_input(include_temb=UpperCAmelCase__ ) def __UpperCAmelCase ( self : List[Any] ) -> Optional[int]: lowerCAmelCase = { 'in_channels': 3_2, 'out_channels': 3_2, } lowerCAmelCase = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self : int ) -> List[Any]: lowerCAmelCase = [0.8_966, -0.1_486, 0.8_568, 0.8_141, -0.9_046, -0.1_342, -0.0_972, -0.7_417, 0.1_538] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Union[str, Any] = UNetMidBlockaD # noqa F405 lowerCamelCase : Union[str, Any] = '''mid''' def __UpperCAmelCase ( self : Dict ) -> Optional[int]: lowerCAmelCase = { 'in_channels': 3_2, 'temb_channels': 1_2_8, } lowerCAmelCase = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self : int ) -> str: lowerCAmelCase = [-0.1_062, 1.7_248, 0.3_494, 1.4_569, -0.0_910, -1.2_421, -0.9_984, 0.6_736, 1.0_028] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : List[Any] = UNetMidBlockaDCrossAttn # noqa F405 lowerCamelCase : List[Any] = '''mid''' def __UpperCAmelCase ( self : Union[str, Any] ) -> List[str]: lowerCAmelCase , lowerCAmelCase = super().prepare_init_args_and_inputs_for_common() lowerCAmelCase = 3_2 return init_dict, inputs_dict def __UpperCAmelCase ( self : int ) -> Optional[int]: lowerCAmelCase = [0.0_187, 2.4_220, 0.4_484, 1.1_203, -0.6_121, -1.5_122, -0.8_270, 0.7_851, 1.8_335] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : int = UNetMidBlockaDSimpleCrossAttn # noqa F405 lowerCamelCase : List[str] = '''mid''' @property def __UpperCAmelCase ( self : List[Any] ) -> Tuple: return super().get_dummy_input(include_encoder_hidden_states=UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[int] ) -> Union[str, Any]: lowerCAmelCase , lowerCAmelCase = super().prepare_init_args_and_inputs_for_common() lowerCAmelCase = 3_2 return init_dict, inputs_dict def __UpperCAmelCase ( self : Tuple ) -> List[str]: lowerCAmelCase = [0.7_143, 1.9_974, 0.5_448, 1.3_977, 0.1_282, -1.1_237, -1.4_238, 0.5_530, 0.8_880] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Optional[int] = UpBlockaD # noqa F405 lowerCamelCase : int = '''up''' @property def __UpperCAmelCase ( self : Optional[Any] ) -> Optional[int]: return super().get_dummy_input(include_res_hidden_states_tuple=UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[int] ) -> int: lowerCAmelCase = [-0.2_041, -0.4_165, -0.3_022, 0.0_041, -0.6_628, -0.7_053, 0.1_928, -0.0_325, 0.0_523] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Any = ResnetUpsampleBlockaD # noqa F405 lowerCamelCase : List[str] = '''up''' @property def __UpperCAmelCase ( self : List[str] ) -> Dict: return super().get_dummy_input(include_res_hidden_states_tuple=UpperCAmelCase__ ) def __UpperCAmelCase ( self : List[Any] ) -> Union[str, Any]: lowerCAmelCase = [0.2_287, 0.3_549, -0.1_346, 0.4_797, -0.1_715, -0.9_649, 0.7_305, -0.5_864, -0.6_244] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : List[str] = CrossAttnUpBlockaD # noqa F405 lowerCamelCase : Tuple = '''up''' @property def __UpperCAmelCase ( self : Dict ) -> Optional[Any]: return super().get_dummy_input(include_res_hidden_states_tuple=UpperCAmelCase__ ) def __UpperCAmelCase ( self : List[str] ) -> Union[str, Any]: lowerCAmelCase , lowerCAmelCase = super().prepare_init_args_and_inputs_for_common() lowerCAmelCase = 3_2 return init_dict, inputs_dict def __UpperCAmelCase ( self : List[Any] ) -> Dict: lowerCAmelCase = [-0.1_403, -0.3_515, -0.0_420, -0.1_425, 0.3_167, 0.5_094, -0.2_181, 0.5_931, 0.5_582] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Tuple = SimpleCrossAttnUpBlockaD # noqa F405 lowerCamelCase : Dict = '''up''' @property def __UpperCAmelCase ( self : List[Any] ) -> Union[str, Any]: return super().get_dummy_input(include_res_hidden_states_tuple=UpperCAmelCase__ , include_encoder_hidden_states=UpperCAmelCase__ ) def __UpperCAmelCase ( self : List[Any] ) -> Tuple: lowerCAmelCase , lowerCAmelCase = super().prepare_init_args_and_inputs_for_common() lowerCAmelCase = 3_2 return init_dict, inputs_dict def __UpperCAmelCase ( self : Optional[int] ) -> List[Any]: lowerCAmelCase = [0.2_645, 0.1_480, 0.0_909, 0.8_044, -0.9_758, -0.9_083, 0.0_994, -1.1_453, -0.7_402] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : int = AttnUpBlockaD # noqa F405 lowerCamelCase : List[str] = '''up''' @property def __UpperCAmelCase ( self : Tuple ) -> Union[str, Any]: return super().get_dummy_input(include_res_hidden_states_tuple=UpperCAmelCase__ ) @unittest.skipIf(torch_device == 'mps' , 'MPS result is not consistent' ) def __UpperCAmelCase ( self : Optional[Any] ) -> str: lowerCAmelCase = [0.0_979, 0.1_326, 0.0_021, 0.0_659, 0.2_249, 0.0_059, 0.1_132, 0.5_952, 0.1_033] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Tuple = SkipUpBlockaD # noqa F405 lowerCamelCase : str = '''up''' @property def __UpperCAmelCase ( self : Tuple ) -> Union[str, Any]: return super().get_dummy_input(include_res_hidden_states_tuple=UpperCAmelCase__ ) def __UpperCAmelCase ( self : Union[str, Any] ) -> int: lowerCAmelCase = [-0.0_893, -0.1_234, -0.1_506, -0.0_332, 0.0_123, -0.0_211, 0.0_566, 0.0_143, 0.0_362] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Optional[Any] = AttnSkipUpBlockaD # noqa F405 lowerCamelCase : List[Any] = '''up''' @property def __UpperCAmelCase ( self : int ) -> Tuple: return super().get_dummy_input(include_res_hidden_states_tuple=UpperCAmelCase__ ) def __UpperCAmelCase ( self : Any ) -> List[Any]: lowerCAmelCase = [0.0_361, 0.0_617, 0.2_787, -0.0_350, 0.0_342, 0.3_421, -0.0_843, 0.0_913, 0.3_015] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : str = UpDecoderBlockaD # noqa F405 lowerCamelCase : List[str] = '''up''' @property def __UpperCAmelCase ( self : List[str] ) -> List[str]: return super().get_dummy_input(include_temb=UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[int] ) -> Dict: lowerCAmelCase = {'in_channels': 3_2, 'out_channels': 3_2} lowerCAmelCase = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self : Union[str, Any] ) -> str: lowerCAmelCase = [0.4_404, 0.1_998, -0.9_886, -0.3_320, -0.3_128, -0.7_034, -0.6_955, -0.2_338, -0.3_137] super().test_output(UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Dict = AttnUpDecoderBlockaD # noqa F405 lowerCamelCase : Tuple = '''up''' @property def __UpperCAmelCase ( self : Tuple ) -> int: return super().get_dummy_input(include_temb=UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[int] ) -> List[str]: lowerCAmelCase = {'in_channels': 3_2, 'out_channels': 3_2} lowerCAmelCase = self.dummy_input return init_dict, inputs_dict def __UpperCAmelCase ( self : List[str] ) -> Optional[int]: lowerCAmelCase = [0.6_738, 0.4_491, 0.1_055, 1.0_710, 0.7_316, 0.3_339, 0.3_352, 0.1_023, 0.3_568] super().test_output(UpperCAmelCase__ )
4
'''simple docstring''' class UpperCAmelCase_ : def __init__( self : List[str] , UpperCAmelCase__ : list[int] ) -> None: lowerCAmelCase = len(UpperCAmelCase__ ) lowerCAmelCase = [0] * len_array if len_array > 0: lowerCAmelCase = array[0] for i in range(1 , UpperCAmelCase__ ): lowerCAmelCase = self.prefix_sum[i - 1] + array[i] def __UpperCAmelCase ( self : Optional[Any] , UpperCAmelCase__ : int , UpperCAmelCase__ : int ) -> int: if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def __UpperCAmelCase ( self : int , UpperCAmelCase__ : int ) -> bool: lowerCAmelCase = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(UpperCAmelCase__ ) return False if __name__ == "__main__": import doctest doctest.testmod()
4
1
'''simple docstring''' from __future__ import annotations from PIL import Image # Define glider example __snake_case =[ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], ] # Define blinker example __snake_case =[[0, 1, 0], [0, 1, 0], [0, 1, 0]] def a_ ( lowerCamelCase : list[list[int]] ): lowerCAmelCase = [] for i in range(len(lowerCamelCase ) ): lowerCAmelCase = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours lowerCAmelCase = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(lowerCamelCase ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(lowerCamelCase ) - 1: neighbour_count += cells[i + 1][j] if i < len(lowerCamelCase ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. lowerCAmelCase = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(lowerCamelCase ) return next_generation def a_ ( lowerCamelCase : list[list[int]] , lowerCamelCase : int ): lowerCAmelCase = [] for _ in range(lowerCamelCase ): # Create output image lowerCAmelCase = Image.new('RGB' , (len(cells[0] ), len(lowerCamelCase )) ) lowerCAmelCase = img.load() # Save cells to image for x in range(len(lowerCamelCase ) ): for y in range(len(cells[0] ) ): lowerCAmelCase = 255 - cells[y][x] * 255 lowerCAmelCase = (colour, colour, colour) # Save image images.append(lowerCamelCase ) lowerCAmelCase = new_generation(lowerCamelCase ) return images if __name__ == "__main__": __snake_case =generate_images(GLIDER, 16) images[0].save("""out.gif""", save_all=True, append_images=images[1:])
4
'''simple docstring''' def a_ ( lowerCamelCase : Optional[Any] ): return [ { 0: [1, 2], 1: [0, 2], 2: [0, 1, 3, 5], 3: [2, 4], 4: [3], 5: [2, 6, 8], 6: [5, 7], 7: [6, 8], 8: [5, 7], }, { 0: [6], 1: [9], 2: [4, 5], 3: [4], 4: [2, 3], 5: [2], 6: [0, 7], 7: [6], 8: [], 9: [1], }, { 0: [4], 1: [6], 2: [], 3: [5, 6, 7], 4: [0, 6], 5: [3, 8, 9], 6: [1, 3, 4, 7], 7: [3, 6, 8, 9], 8: [5, 7], 9: [5, 7], }, { 0: [1, 3], 1: [0, 2, 4], 2: [1, 3, 4], 3: [0, 2, 4], 4: [1, 2, 3], }, ][index] def a_ ( lowerCamelCase : dict[int, list[int]] ): lowerCAmelCase = 0 lowerCAmelCase = len(lowerCamelCase ) # No of vertices in graph lowerCAmelCase = [0] * n lowerCAmelCase = [False] * n def dfs(lowerCamelCase : Tuple , lowerCamelCase : str , lowerCamelCase : Dict , lowerCamelCase : str ): lowerCAmelCase = True lowerCAmelCase = id_ id_ += 1 for to in graph[at]: if to == parent: pass elif not visited[to]: dfs(lowerCamelCase , lowerCamelCase , lowerCamelCase , id_ ) lowerCAmelCase = min(low[at] , low[to] ) if id_ <= low[to]: bridges.append((at, to) if at < to else (to, at) ) else: # This edge is a back edge and cannot be a bridge lowerCAmelCase = min(low[at] , low[to] ) lowerCAmelCase = [] for i in range(lowerCamelCase ): if not visited[i]: dfs(lowerCamelCase , -1 , lowerCamelCase , id_ ) return bridges if __name__ == "__main__": import doctest doctest.testmod()
4
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case =logging.get_logger(__name__) __snake_case ={ """facebook/s2t-wav2vec2-large-en-de""": ( """https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json""" ), # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2 } class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Union[str, Any] = '''speech_to_text_2''' lowerCamelCase : Any = ['''past_key_values'''] lowerCamelCase : Optional[Any] = {'''num_attention_heads''': '''decoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self : Optional[int] , UpperCAmelCase__ : Optional[Any]=1_0_0_0_0 , UpperCAmelCase__ : int=6 , UpperCAmelCase__ : Optional[Any]=2_0_4_8 , UpperCAmelCase__ : Union[str, Any]=4 , UpperCAmelCase__ : List[Any]=0.0 , UpperCAmelCase__ : str=True , UpperCAmelCase__ : str="relu" , UpperCAmelCase__ : Any=2_5_6 , UpperCAmelCase__ : Optional[Any]=0.1 , UpperCAmelCase__ : Any=0.0 , UpperCAmelCase__ : Tuple=0.0 , UpperCAmelCase__ : List[Any]=0.02 , UpperCAmelCase__ : List[Any]=2 , UpperCAmelCase__ : str=True , UpperCAmelCase__ : List[str]=1 , UpperCAmelCase__ : Any=0 , UpperCAmelCase__ : Dict=2 , UpperCAmelCase__ : int=1_0_2_4 , **UpperCAmelCase__ : Optional[Any] , ) -> Dict: lowerCAmelCase = vocab_size lowerCAmelCase = d_model lowerCAmelCase = decoder_ffn_dim lowerCAmelCase = decoder_layers lowerCAmelCase = decoder_attention_heads lowerCAmelCase = dropout lowerCAmelCase = attention_dropout lowerCAmelCase = activation_dropout lowerCAmelCase = activation_function lowerCAmelCase = init_std lowerCAmelCase = decoder_layerdrop lowerCAmelCase = use_cache lowerCAmelCase = decoder_layers lowerCAmelCase = scale_embedding # scale factor will be sqrt(d_model) if True lowerCAmelCase = max_target_positions super().__init__( pad_token_id=UpperCAmelCase__ , bos_token_id=UpperCAmelCase__ , eos_token_id=UpperCAmelCase__ , decoder_start_token_id=UpperCAmelCase__ , **UpperCAmelCase__ , )
4
'''simple docstring''' import argparse from collections import OrderedDict from pathlib import Path import requests import torch from PIL import Image from transformers import GLPNConfig, GLPNForDepthEstimation, GLPNImageProcessor from transformers.utils import logging logging.set_verbosity_info() __snake_case =logging.get_logger(__name__) def a_ ( lowerCamelCase : Any ): lowerCAmelCase = OrderedDict() for key, value in state_dict.items(): if key.startswith('module.encoder' ): lowerCAmelCase = key.replace('module.encoder' , 'glpn.encoder' ) if key.startswith('module.decoder' ): lowerCAmelCase = key.replace('module.decoder' , 'decoder.stages' ) if "patch_embed" in key: # replace for example patch_embed1 by patch_embeddings.0 lowerCAmelCase = key[key.find('patch_embed' ) + len('patch_embed' )] lowerCAmelCase = key.replace(f'''patch_embed{idx}''' , f'''patch_embeddings.{int(lowerCamelCase )-1}''' ) if "norm" in key: lowerCAmelCase = key.replace('norm' , 'layer_norm' ) if "glpn.encoder.layer_norm" in key: # replace for example layer_norm1 by layer_norm.0 lowerCAmelCase = key[key.find('glpn.encoder.layer_norm' ) + len('glpn.encoder.layer_norm' )] lowerCAmelCase = key.replace(f'''layer_norm{idx}''' , f'''layer_norm.{int(lowerCamelCase )-1}''' ) if "layer_norm1" in key: lowerCAmelCase = key.replace('layer_norm1' , 'layer_norm_1' ) if "layer_norm2" in key: lowerCAmelCase = key.replace('layer_norm2' , 'layer_norm_2' ) if "block" in key: # replace for example block1 by block.0 lowerCAmelCase = key[key.find('block' ) + len('block' )] lowerCAmelCase = key.replace(f'''block{idx}''' , f'''block.{int(lowerCamelCase )-1}''' ) if "attn.q" in key: lowerCAmelCase = key.replace('attn.q' , 'attention.self.query' ) if "attn.proj" in key: lowerCAmelCase = key.replace('attn.proj' , 'attention.output.dense' ) if "attn" in key: lowerCAmelCase = key.replace('attn' , 'attention.self' ) if "fc1" in key: lowerCAmelCase = key.replace('fc1' , 'dense1' ) if "fc2" in key: lowerCAmelCase = key.replace('fc2' , 'dense2' ) if "linear_pred" in key: lowerCAmelCase = key.replace('linear_pred' , 'classifier' ) if "linear_fuse" in key: lowerCAmelCase = key.replace('linear_fuse.conv' , 'linear_fuse' ) lowerCAmelCase = key.replace('linear_fuse.bn' , 'batch_norm' ) if "linear_c" in key: # replace for example linear_c4 by linear_c.3 lowerCAmelCase = key[key.find('linear_c' ) + len('linear_c' )] lowerCAmelCase = key.replace(f'''linear_c{idx}''' , f'''linear_c.{int(lowerCamelCase )-1}''' ) if "bot_conv" in key: lowerCAmelCase = key.replace('bot_conv' , '0.convolution' ) if "skip_conv1" in key: lowerCAmelCase = key.replace('skip_conv1' , '1.convolution' ) if "skip_conv2" in key: lowerCAmelCase = key.replace('skip_conv2' , '2.convolution' ) if "fusion1" in key: lowerCAmelCase = key.replace('fusion1' , '1.fusion' ) if "fusion2" in key: lowerCAmelCase = key.replace('fusion2' , '2.fusion' ) if "fusion3" in key: lowerCAmelCase = key.replace('fusion3' , '3.fusion' ) if "fusion" in key and "conv" in key: lowerCAmelCase = key.replace('conv' , 'convolutional_layer' ) if key.startswith('module.last_layer_depth' ): lowerCAmelCase = key.replace('module.last_layer_depth' , 'head.head' ) lowerCAmelCase = value return new_state_dict def a_ ( lowerCamelCase : List[str] , lowerCamelCase : str ): # for each of the encoder blocks: for i in range(config.num_encoder_blocks ): for j in range(config.depths[i] ): # read in weights + bias of keys and values (which is a single matrix in the original implementation) lowerCAmelCase = state_dict.pop(f'''glpn.encoder.block.{i}.{j}.attention.self.kv.weight''' ) lowerCAmelCase = state_dict.pop(f'''glpn.encoder.block.{i}.{j}.attention.self.kv.bias''' ) # next, add keys and values (in that order) to the state dict lowerCAmelCase = kv_weight[ : config.hidden_sizes[i], : ] lowerCAmelCase = kv_bias[: config.hidden_sizes[i]] lowerCAmelCase = kv_weight[ config.hidden_sizes[i] :, : ] lowerCAmelCase = kv_bias[config.hidden_sizes[i] :] def a_ ( ): lowerCAmelCase = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowerCAmelCase = Image.open(requests.get(lowerCamelCase , stream=lowerCamelCase ).raw ) return image @torch.no_grad() def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Tuple , lowerCamelCase : Optional[Any]=False , lowerCamelCase : List[str]=None ): lowerCAmelCase = GLPNConfig(hidden_sizes=[64, 128, 320, 512] , decoder_hidden_size=64 , depths=[3, 8, 27, 3] ) # load image processor (only resize + rescale) lowerCAmelCase = GLPNImageProcessor() # prepare image lowerCAmelCase = prepare_img() lowerCAmelCase = image_processor(images=lowerCamelCase , return_tensors='pt' ).pixel_values logger.info('Converting model...' ) # load original state dict lowerCAmelCase = torch.load(lowerCamelCase , map_location=torch.device('cpu' ) ) # rename keys lowerCAmelCase = rename_keys(lowerCamelCase ) # key and value matrices need special treatment read_in_k_v(lowerCamelCase , lowerCamelCase ) # create HuggingFace model and load state dict lowerCAmelCase = GLPNForDepthEstimation(lowerCamelCase ) model.load_state_dict(lowerCamelCase ) model.eval() # forward pass lowerCAmelCase = model(lowerCamelCase ) lowerCAmelCase = outputs.predicted_depth # verify output if model_name is not None: if "nyu" in model_name: lowerCAmelCase = torch.tensor( [[4.4_147, 4.0_873, 4.0_673], [3.7_890, 3.2_881, 3.1_525], [3.7_674, 3.5_423, 3.4_913]] ) elif "kitti" in model_name: lowerCAmelCase = torch.tensor( [[3.4_291, 2.7_865, 2.5_151], [3.2_841, 2.7_021, 2.3_502], [3.1_147, 2.4_625, 2.2_481]] ) else: raise ValueError(f'''Unknown model name: {model_name}''' ) lowerCAmelCase = torch.Size([1, 480, 640] ) assert predicted_depth.shape == expected_shape assert torch.allclose(predicted_depth[0, :3, :3] , lowerCamelCase , atol=1e-4 ) print('Looks ok!' ) # finally, push to hub if required if push_to_hub: logger.info('Pushing model and image processor to the hub...' ) model.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=lowerCamelCase , ) image_processor.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=lowerCamelCase , ) if __name__ == "__main__": __snake_case =argparse.ArgumentParser() parser.add_argument( """--checkpoint_path""", default=None, 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 folder to output PyTorch model.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to upload the model to the HuggingFace hub.""" ) parser.add_argument( """--model_name""", default="""glpn-kitti""", type=str, help="""Name of the model in case you're pushing to the hub.""", ) __snake_case =parser.parse_args() convert_glpn_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
4
1
'''simple docstring''' import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Optional[Any] = KandinskyImgaImgPipeline lowerCamelCase : Tuple = ['''prompt''', '''image_embeds''', '''negative_image_embeds''', '''image'''] lowerCamelCase : Tuple = [ '''prompt''', '''negative_prompt''', '''image_embeds''', '''negative_image_embeds''', '''image''', ] lowerCamelCase : int = [ '''generator''', '''height''', '''width''', '''strength''', '''guidance_scale''', '''negative_prompt''', '''num_inference_steps''', '''return_dict''', '''guidance_scale''', '''num_images_per_prompt''', '''output_type''', '''return_dict''', ] lowerCamelCase : Dict = False @property def __UpperCAmelCase ( self : Tuple ) -> str: return 3_2 @property def __UpperCAmelCase ( self : List[str] ) -> Optional[Any]: return 3_2 @property def __UpperCAmelCase ( self : Any ) -> Dict: return self.time_input_dim @property def __UpperCAmelCase ( self : Union[str, Any] ) -> List[Any]: return self.time_input_dim * 4 @property def __UpperCAmelCase ( self : Dict ) -> List[str]: return 1_0_0 @property def __UpperCAmelCase ( self : int ) -> Tuple: lowerCAmelCase = XLMRobertaTokenizerFast.from_pretrained('YiYiXu/tiny-random-mclip-base' ) return tokenizer @property def __UpperCAmelCase ( self : List[str] ) -> Optional[Any]: torch.manual_seed(0 ) lowerCAmelCase = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=3_7 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1_0_0_5 , ) lowerCAmelCase = MultilingualCLIP(UpperCAmelCase__ ) lowerCAmelCase = text_encoder.eval() return text_encoder @property def __UpperCAmelCase ( self : Optional[Any] ) -> Optional[Any]: torch.manual_seed(0 ) lowerCAmelCase = { 'in_channels': 4, # Out channels is double in channels because predicts mean and variance 'out_channels': 8, 'addition_embed_type': 'text_image', 'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'), 'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'), 'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn', 'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2), 'layers_per_block': 1, 'encoder_hid_dim': self.text_embedder_hidden_size, 'encoder_hid_dim_type': 'text_image_proj', 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': None, } lowerCAmelCase = UNetaDConditionModel(**UpperCAmelCase__ ) return model @property def __UpperCAmelCase ( self : Any ) -> str: return { "block_out_channels": [3_2, 6_4], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __UpperCAmelCase ( self : int ) -> Tuple: torch.manual_seed(0 ) lowerCAmelCase = VQModel(**self.dummy_movq_kwargs ) return model def __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: lowerCAmelCase = self.dummy_text_encoder lowerCAmelCase = self.dummy_tokenizer lowerCAmelCase = self.dummy_unet lowerCAmelCase = self.dummy_movq lowerCAmelCase = { 'num_train_timesteps': 1_0_0_0, 'beta_schedule': 'linear', 'beta_start': 0.00_085, 'beta_end': 0.012, 'clip_sample': False, 'set_alpha_to_one': False, 'steps_offset': 0, 'prediction_type': 'epsilon', 'thresholding': False, } lowerCAmelCase = DDIMScheduler(**UpperCAmelCase__ ) lowerCAmelCase = { 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'unet': unet, 'scheduler': scheduler, 'movq': movq, } return components def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : List[Any]=0 ) -> Tuple: lowerCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(UpperCAmelCase__ ) ).to(UpperCAmelCase__ ) lowerCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(UpperCAmelCase__ ) # create init_image lowerCAmelCase = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(UpperCAmelCase__ ) ).to(UpperCAmelCase__ ) lowerCAmelCase = image.cpu().permute(0 , 2 , 3 , 1 )[0] lowerCAmelCase = Image.fromarray(np.uinta(UpperCAmelCase__ ) ).convert('RGB' ).resize((2_5_6, 2_5_6) ) if str(UpperCAmelCase__ ).startswith('mps' ): lowerCAmelCase = torch.manual_seed(UpperCAmelCase__ ) else: lowerCAmelCase = torch.Generator(device=UpperCAmelCase__ ).manual_seed(UpperCAmelCase__ ) lowerCAmelCase = { 'prompt': 'horse', 'image': init_image, 'image_embeds': image_embeds, 'negative_image_embeds': negative_image_embeds, 'generator': generator, 'height': 6_4, 'width': 6_4, 'num_inference_steps': 1_0, 'guidance_scale': 7.0, 'strength': 0.2, 'output_type': 'np', } return inputs def __UpperCAmelCase ( self : Dict ) -> List[str]: lowerCAmelCase = 'cpu' lowerCAmelCase = self.get_dummy_components() lowerCAmelCase = self.pipeline_class(**UpperCAmelCase__ ) lowerCAmelCase = pipe.to(UpperCAmelCase__ ) pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) lowerCAmelCase = pipe(**self.get_dummy_inputs(UpperCAmelCase__ ) ) lowerCAmelCase = output.images lowerCAmelCase = pipe( **self.get_dummy_inputs(UpperCAmelCase__ ) , return_dict=UpperCAmelCase__ , )[0] lowerCAmelCase = image[0, -3:, -3:, -1] lowerCAmelCase = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) lowerCAmelCase = np.array( [0.61_474_943, 0.6_073_539, 0.43_308_544, 0.5_928_269, 0.47_493_595, 0.46_755_973, 0.4_613_838, 0.45_368_797, 0.50_119_233] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), F''' expected_slice {expected_slice}, but got {image_slice.flatten()}''' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), F''' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}''' @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : List[str] ) -> Tuple: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self : List[str] ) -> List[str]: lowerCAmelCase = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/kandinsky_img2img_frog.npy' ) lowerCAmelCase = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/cat.png' ) lowerCAmelCase = 'A red cartoon frog, 4k' lowerCAmelCase = KandinskyPriorPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-1-prior' , torch_dtype=torch.floataa ) pipe_prior.to(UpperCAmelCase__ ) lowerCAmelCase = KandinskyImgaImgPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-1' , torch_dtype=torch.floataa ) lowerCAmelCase = pipeline.to(UpperCAmelCase__ ) pipeline.set_progress_bar_config(disable=UpperCAmelCase__ ) lowerCAmelCase = torch.Generator(device='cpu' ).manual_seed(0 ) lowerCAmelCase , lowerCAmelCase = pipe_prior( UpperCAmelCase__ , generator=UpperCAmelCase__ , num_inference_steps=5 , negative_prompt='' , ).to_tuple() lowerCAmelCase = pipeline( UpperCAmelCase__ , image=UpperCAmelCase__ , image_embeds=UpperCAmelCase__ , negative_image_embeds=UpperCAmelCase__ , generator=UpperCAmelCase__ , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , strength=0.2 , output_type='np' , ) lowerCAmelCase = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(UpperCAmelCase__ , UpperCAmelCase__ )
4
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class UpperCAmelCase_ ( unittest.TestCase ): @slow def __UpperCAmelCase ( self : str ) -> List[str]: lowerCAmelCase = XLMRobertaModel.from_pretrained('xlm-roberta-base' ) lowerCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house lowerCAmelCase = torch.Size((1, 1_2, 7_6_8) ) # batch_size, sequence_length, embedding_vector_dim lowerCAmelCase = torch.tensor( [[-0.0_101, 0.1_218, -0.0_803, 0.0_801, 0.1_327, 0.0_776, -0.1_215, 0.2_383, 0.3_338, 0.3_106, 0.0_300, 0.0_252]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): lowerCAmelCase = model(UpperCAmelCase__ )['last_hidden_state'].detach() self.assertEqual(output.shape , UpperCAmelCase__ ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , UpperCAmelCase__ , atol=1E-3 ) ) @slow def __UpperCAmelCase ( self : List[Any] ) -> Tuple: lowerCAmelCase = XLMRobertaModel.from_pretrained('xlm-roberta-large' ) lowerCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house lowerCAmelCase = torch.Size((1, 1_2, 1_0_2_4) ) # batch_size, sequence_length, embedding_vector_dim lowerCAmelCase = torch.tensor( [[-0.0_699, -0.0_318, 0.0_705, -0.1_241, 0.0_999, -0.0_520, 0.1_004, -0.1_838, -0.4_704, 0.1_437, 0.0_821, 0.0_126]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): lowerCAmelCase = model(UpperCAmelCase__ )['last_hidden_state'].detach() self.assertEqual(output.shape , UpperCAmelCase__ ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , UpperCAmelCase__ , atol=1E-3 ) )
4
1
'''simple docstring''' import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : int ) -> List[str]: lowerCAmelCase = inspect.getfile(accelerate.test_utils ) lowerCAmelCase = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['scripts', 'test_script.py'] ) lowerCAmelCase = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['scripts', 'test_distributed_data_loop.py'] ) lowerCAmelCase = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['scripts', 'test_ops.py'] ) @require_multi_gpu def __UpperCAmelCase ( self : Any ) -> List[Any]: print(F'''Found {torch.cuda.device_count()} devices.''' ) lowerCAmelCase = ['torchrun', F'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(UpperCAmelCase__ , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self : List[str] ) -> Optional[int]: print(F'''Found {torch.cuda.device_count()} devices.''' ) lowerCAmelCase = ['torchrun', F'''--nproc_per_node={torch.cuda.device_count()}''', self.operation_file_path] print(F'''Command: {cmd}''' ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(UpperCAmelCase__ , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self : Optional[int] ) -> Union[str, Any]: lowerCAmelCase = ['torchrun', F'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(UpperCAmelCase__ , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self : str ) -> Optional[int]: print(F'''Found {torch.cuda.device_count()} devices, using 2 devices only''' ) lowerCAmelCase = ['torchrun', F'''--nproc_per_node={torch.cuda.device_count()}''', self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices='0,1' ): execute_subprocess_async(UpperCAmelCase__ , env=os.environ.copy() ) if __name__ == "__main__": __snake_case =Accelerator() __snake_case =(accelerator.state.process_index + 2, 10) __snake_case =torch.randint(0, 10, shape).to(accelerator.device) __snake_case ="""""" __snake_case =accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." __snake_case =accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." __snake_case =accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # 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)
4
'''simple docstring''' import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def a_ ( lowerCamelCase : Dict ): lowerCAmelCase = {} lowerCAmelCase = tokenizer(example['content'] , truncation=lowerCamelCase )['input_ids'] lowerCAmelCase = len(example['content'] ) / len(output['input_ids'] ) return output __snake_case =HfArgumentParser(PretokenizationArguments) __snake_case =parser.parse_args() if args.num_workers is None: __snake_case =multiprocessing.cpu_count() __snake_case =AutoTokenizer.from_pretrained(args.tokenizer_dir) __snake_case =time.time() __snake_case =load_dataset(args.dataset_name, split="""train""") print(F'''Dataset loaded in {time.time()-t_start:.2f}s''') __snake_case =time.time() __snake_case =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''') __snake_case =time.time() ds.push_to_hub(args.tokenized_data_repo) print(F'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
4
1
'''simple docstring''' from __future__ import annotations from collections.abc import MutableSequence class UpperCAmelCase_ : def __init__( self : Dict , UpperCAmelCase__ : int , UpperCAmelCase__ : MutableSequence[float] ) -> None: if len(UpperCAmelCase__ ) != degree + 1: raise ValueError( 'The number of coefficients should be equal to the degree + 1.' ) lowerCAmelCase = list(UpperCAmelCase__ ) lowerCAmelCase = degree def __add__( self : Any , UpperCAmelCase__ : Polynomial ) -> Polynomial: if self.degree > polynomial_a.degree: lowerCAmelCase = self.coefficients[:] for i in range(polynomial_a.degree + 1 ): coefficients[i] += polynomial_a.coefficients[i] return Polynomial(self.degree , UpperCAmelCase__ ) else: lowerCAmelCase = polynomial_a.coefficients[:] for i in range(self.degree + 1 ): coefficients[i] += self.coefficients[i] return Polynomial(polynomial_a.degree , UpperCAmelCase__ ) def __sub__( self : str , UpperCAmelCase__ : Polynomial ) -> Polynomial: return self + polynomial_a * Polynomial(0 , [-1] ) def __neg__( self : int ) -> Polynomial: return Polynomial(self.degree , [-c for c in self.coefficients] ) def __mul__( self : int , UpperCAmelCase__ : Polynomial ) -> Polynomial: lowerCAmelCase = [0] * (self.degree + polynomial_a.degree + 1) for i in range(self.degree + 1 ): for j in range(polynomial_a.degree + 1 ): coefficients[i + j] += ( self.coefficients[i] * polynomial_a.coefficients[j] ) return Polynomial(self.degree + polynomial_a.degree , UpperCAmelCase__ ) def __UpperCAmelCase ( self : Union[str, Any] , UpperCAmelCase__ : int | float ) -> int | float: lowerCAmelCase = 0 for i in range(self.degree + 1 ): result += self.coefficients[i] * (substitution**i) return result def __str__( self : List[Any] ) -> str: lowerCAmelCase = '' for i in range(self.degree , -1 , -1 ): if self.coefficients[i] == 0: continue elif self.coefficients[i] > 0: if polynomial: polynomial += " + " else: polynomial += " - " if i == 0: polynomial += str(abs(self.coefficients[i] ) ) elif i == 1: polynomial += str(abs(self.coefficients[i] ) ) + "x" else: polynomial += str(abs(self.coefficients[i] ) ) + "x^" + str(UpperCAmelCase__ ) return polynomial def __repr__( self : str ) -> str: return self.__str__() def __UpperCAmelCase ( self : Optional[Any] ) -> Polynomial: lowerCAmelCase = [0] * self.degree for i in range(self.degree ): lowerCAmelCase = self.coefficients[i + 1] * (i + 1) return Polynomial(self.degree - 1 , UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : int | float = 0 ) -> Polynomial: lowerCAmelCase = [0] * (self.degree + 2) lowerCAmelCase = constant for i in range(self.degree + 1 ): lowerCAmelCase = self.coefficients[i] / (i + 1) return Polynomial(self.degree + 1 , UpperCAmelCase__ ) def __eq__( self : List[str] , UpperCAmelCase__ : object ) -> bool: if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): return False if self.degree != polynomial_a.degree: return False for i in range(self.degree + 1 ): if self.coefficients[i] != polynomial_a.coefficients[i]: return False return True def __ne__( self : int , UpperCAmelCase__ : object ) -> bool: return not self.__eq__(UpperCAmelCase__ )
4
'''simple docstring''' import logging from dataclasses import dataclass, field from pathlib import Path from typing import Optional, Union from .generation.configuration_utils import GenerationConfig from .training_args import TrainingArguments from .utils import add_start_docstrings __snake_case =logging.getLogger(__name__) @dataclass @add_start_docstrings(TrainingArguments.__doc__ ) class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : bool = field(default=__lowercase , metadata={'''help''': '''Whether to use SortishSampler or not.'''} ) lowerCamelCase : bool = field( default=__lowercase , metadata={'''help''': '''Whether to use generate to calculate generative metrics (ROUGE, BLEU).'''} ) lowerCamelCase : Optional[int] = field( default=__lowercase , metadata={ '''help''': ( '''The `max_length` to use on each evaluation loop when `predict_with_generate=True`. Will default ''' '''to the `max_length` value of the model configuration.''' ) } , ) lowerCamelCase : Optional[int] = field( default=__lowercase , metadata={ '''help''': ( '''The `num_beams` to use on each evaluation loop when `predict_with_generate=True`. Will default ''' '''to the `num_beams` value of the model configuration.''' ) } , ) lowerCamelCase : Optional[Union[str, Path, GenerationConfig]] = field( default=__lowercase , metadata={ '''help''': '''Model id, file path or url pointing to a GenerationConfig json file, to use during prediction.''' } , ) def __UpperCAmelCase ( self : Dict ) -> List[str]: lowerCAmelCase = super().to_dict() for k, v in d.items(): if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): lowerCAmelCase = v.to_dict() return d
4
1
'''simple docstring''' from __future__ import annotations from bisect import bisect_left from functools import total_ordering from heapq import merge @total_ordering class UpperCAmelCase_ ( __lowercase ): def __lt__( self : Optional[int] , UpperCAmelCase__ : List[str] ) -> List[Any]: return self[-1] < other[-1] def __eq__( self : str , UpperCAmelCase__ : List[str] ) -> Tuple: return self[-1] == other[-1] def a_ ( lowerCamelCase : list ): lowerCAmelCase = [] # sort into stacks for element in collection: lowerCAmelCase = Stack([element] ) lowerCAmelCase = bisect_left(lowerCamelCase , lowerCamelCase ) if i != len(lowerCamelCase ): stacks[i].append(lowerCamelCase ) else: stacks.append(lowerCamelCase ) # use a heap-based merge to merge stack efficiently lowerCAmelCase = merge(*(reversed(lowerCamelCase ) for stack in stacks) ) return collection if __name__ == "__main__": __snake_case =input("""Enter numbers separated by a comma:\n""").strip() __snake_case =[int(item) for item in user_input.split(""",""")] print(patience_sort(unsorted))
4
'''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() __snake_case =logging.get_logger("""transformers.models.encodec""") __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_DECODER, } __snake_case ={ **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_ENCODER_48K, **MAPPING_DECODER, **MAPPING_DECODER_48K, } __snake_case =[] __snake_case =[] def a_ ( lowerCamelCase : Optional[Any] , lowerCamelCase : Union[str, Any] , lowerCamelCase : int , lowerCamelCase : Any , lowerCamelCase : List[str] ): for attribute in key.split('.' ): lowerCAmelCase = getattr(lowerCamelCase , lowerCamelCase ) if weight_type is not None: lowerCAmelCase = getattr(lowerCamelCase , lowerCamelCase ).shape else: lowerCAmelCase = 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": lowerCAmelCase = value elif weight_type == "weight_g": lowerCAmelCase = value elif weight_type == "weight_v": lowerCAmelCase = value elif weight_type == "bias": lowerCAmelCase = value elif weight_type == "running_mean": lowerCAmelCase = value elif weight_type == "running_var": lowerCAmelCase = value elif weight_type == "num_batches_tracked": lowerCAmelCase = value elif weight_type == "weight_ih_l0": lowerCAmelCase = value elif weight_type == "weight_hh_l0": lowerCAmelCase = value elif weight_type == "bias_ih_l0": lowerCAmelCase = value elif weight_type == "bias_hh_l0": lowerCAmelCase = value elif weight_type == "weight_ih_l1": lowerCAmelCase = value elif weight_type == "weight_hh_l1": lowerCAmelCase = value elif weight_type == "bias_ih_l1": lowerCAmelCase = value elif weight_type == "bias_hh_l1": lowerCAmelCase = value else: lowerCAmelCase = value logger.info(f'''{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.''' ) def a_ ( lowerCamelCase : Optional[Any] , lowerCamelCase : Optional[Any] ): for key in ignore_keys: if key.endswith('.*' ): if name.startswith(key[:-1] ): return True elif ".*." in key: lowerCAmelCase , lowerCAmelCase = key.split('.*.' ) if prefix in name and suffix in name: return True elif key in name: return True return False def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Any , lowerCamelCase : str ): lowerCAmelCase = [] if model_name == "encodec_24khz" or "encodec_32khz": lowerCAmelCase = MAPPING_24K elif model_name == "encodec_48khz": lowerCAmelCase = 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 lowerCAmelCase = False for key, mapped_key in MAPPING.items(): if "*" in key: lowerCAmelCase , lowerCAmelCase = key.split('.*.' ) if prefix in name and suffix in name: lowerCAmelCase = suffix if key in name: # HACK otherwise .embed gets initialized with .embed_avg too if key.endswith('embed' ) and name.endswith('embed_avg' ): continue lowerCAmelCase = True if "*" in mapped_key: lowerCAmelCase = name.split(lowerCamelCase )[0].split('.' )[-2] lowerCAmelCase = mapped_key.replace('*' , lowerCamelCase ) if "weight_g" in name: lowerCAmelCase = 'weight_g' elif "weight_v" in name: lowerCAmelCase = 'weight_v' elif "weight_ih_l0" in name: lowerCAmelCase = 'weight_ih_l0' elif "weight_hh_l0" in name: lowerCAmelCase = 'weight_hh_l0' elif "bias_ih_l0" in name: lowerCAmelCase = 'bias_ih_l0' elif "bias_hh_l0" in name: lowerCAmelCase = 'bias_hh_l0' elif "weight_ih_l1" in name: lowerCAmelCase = 'weight_ih_l1' elif "weight_hh_l1" in name: lowerCAmelCase = 'weight_hh_l1' elif "bias_ih_l1" in name: lowerCAmelCase = 'bias_ih_l1' elif "bias_hh_l1" in name: lowerCAmelCase = 'bias_hh_l1' elif "bias" in name: lowerCAmelCase = 'bias' elif "weight" in name: lowerCAmelCase = 'weight' elif "running_mean" in name: lowerCAmelCase = 'running_mean' elif "running_var" in name: lowerCAmelCase = 'running_var' elif "num_batches_tracked" in name: lowerCAmelCase = 'num_batches_tracked' else: lowerCAmelCase = 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 a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Union[str, Any] , lowerCamelCase : str , lowerCamelCase : Dict=None , lowerCamelCase : Union[str, Any]=None , ): if config_path is not None: lowerCAmelCase = EncodecConfig.from_pretrained(lowerCamelCase ) else: lowerCAmelCase = EncodecConfig() if model_name == "encodec_24khz": pass # config is already correct elif model_name == "encodec_32khz": lowerCAmelCase = [8, 5, 4, 4] lowerCAmelCase = [2.2] lowerCAmelCase = 64 lowerCAmelCase = 32000 lowerCAmelCase = 2048 lowerCAmelCase = False lowerCAmelCase = False lowerCAmelCase = False elif model_name == "encodec_48khz": lowerCAmelCase = [8, 5, 4, 2] lowerCAmelCase = [3.0, 6.0, 12.0, 24.0] lowerCAmelCase = 48000 lowerCAmelCase = 2 lowerCAmelCase = False lowerCAmelCase = 'time_group_norm' lowerCAmelCase = True lowerCAmelCase = 1.0 lowerCAmelCase = 0.01 else: raise ValueError(f'''Unknown model name: {model_name}''' ) lowerCAmelCase = EncodecModel(lowerCamelCase ) lowerCAmelCase = 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 ) lowerCAmelCase = 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 lowerCAmelCase = 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__": __snake_case =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.""" ) __snake_case =parser.parse_args() convert_checkpoint( args.model, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
4
1
'''simple docstring''' def a_ ( lowerCamelCase : Any , lowerCamelCase : str , lowerCamelCase : Any , lowerCamelCase : Union[str, Any] ): if height >= 1: move_tower(height - 1 , lowerCamelCase , lowerCamelCase , lowerCamelCase ) move_disk(lowerCamelCase , lowerCamelCase ) move_tower(height - 1 , lowerCamelCase , lowerCamelCase , lowerCamelCase ) def a_ ( lowerCamelCase : Any , lowerCamelCase : Optional[int] ): print('moving disk from' , lowerCamelCase , 'to' , lowerCamelCase ) def a_ ( ): lowerCAmelCase = int(input('Height of hanoi: ' ).strip() ) move_tower(lowerCamelCase , 'A' , 'B' , 'C' ) if __name__ == "__main__": main()
4
'''simple docstring''' import warnings from ...utils import logging from .image_processing_flava import FlavaImageProcessor __snake_case =logging.get_logger(__name__) class UpperCAmelCase_ ( __lowercase ): def __init__( self : Dict , *UpperCAmelCase__ : Dict , **UpperCAmelCase__ : List[str] ) -> None: warnings.warn( 'The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use FlavaImageProcessor instead.' , UpperCAmelCase__ , ) super().__init__(*UpperCAmelCase__ , **UpperCAmelCase__ )
4
1
'''simple docstring''' from collections.abc import Sequence def a_ ( lowerCamelCase : Sequence[float] , lowerCamelCase : bool = False ): if not arr: return 0 lowerCAmelCase = 0 if allow_empty_subarrays else float('-inf' ) lowerCAmelCase = 0.0 for num in arr: lowerCAmelCase = max(0 if allow_empty_subarrays else num , curr_sum + num ) lowerCAmelCase = max(lowerCamelCase , lowerCamelCase ) return max_sum if __name__ == "__main__": from doctest import testmod testmod() __snake_case =[-2, 1, -3, 4, -1, 2, 1, -5, 4] print(F'''{max_subarray_sum(nums) = }''')
4
'''simple docstring''' from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer __snake_case =logging.get_logger(__name__) __snake_case ={ """vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_config_file""": """tokenizer_config.json""", } __snake_case ={ """vocab_file""": { """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json""" }, """merges_file""": { """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt""" }, """tokenizer_config_file""": { """facebook/blenderbot_small-90M""": ( """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json""" ) }, } __snake_case ={ """facebook/blenderbot_small-90M""": 512, } class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Tuple = VOCAB_FILES_NAMES lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[Any] = BlenderbotSmallTokenizer def __init__( self : Any , UpperCAmelCase__ : Union[str, Any]=None , UpperCAmelCase__ : Dict=None , UpperCAmelCase__ : int="<|endoftext|>" , UpperCAmelCase__ : Dict="<|endoftext|>" , UpperCAmelCase__ : str="<|endoftext|>" , UpperCAmelCase__ : str=False , UpperCAmelCase__ : Tuple=True , **UpperCAmelCase__ : Optional[Any] , ) -> Any: super().__init__( ByteLevelBPETokenizer( vocab=UpperCAmelCase__ , merges=UpperCAmelCase__ , add_prefix_space=UpperCAmelCase__ , trim_offsets=UpperCAmelCase__ , ) , bos_token=UpperCAmelCase__ , eos_token=UpperCAmelCase__ , unk_token=UpperCAmelCase__ , **UpperCAmelCase__ , ) lowerCAmelCase = add_prefix_space def __UpperCAmelCase ( self : List[Any] , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Dict=None ) -> Any: lowerCAmelCase = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def __UpperCAmelCase ( self : Any , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
4
1
'''simple docstring''' from __future__ import annotations from typing import Any class UpperCAmelCase_ : def __init__( self : Optional[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : float = 0 ) -> None: lowerCAmelCase , lowerCAmelCase = row, column lowerCAmelCase = [[default_value for c in range(UpperCAmelCase__ )] for r in range(UpperCAmelCase__ )] def __str__( self : List[str] ) -> str: lowerCAmelCase = F'''Matrix consist of {self.row} rows and {self.column} columns\n''' # Make string identifier lowerCAmelCase = 0 for row_vector in self.array: for obj in row_vector: lowerCAmelCase = max(UpperCAmelCase__ , len(str(UpperCAmelCase__ ) ) ) lowerCAmelCase = F'''%{max_element_length}s''' # Make string and return def single_line(UpperCAmelCase__ : list[float] ) -> str: nonlocal string_format_identifier lowerCAmelCase = '[' line += ", ".join(string_format_identifier % (obj,) for obj in row_vector ) line += "]" return line s += "\n".join(single_line(UpperCAmelCase__ ) for row_vector in self.array ) return s def __repr__( self : List[str] ) -> str: return str(self ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : tuple[int, int] ) -> bool: if not (isinstance(UpperCAmelCase__ , (list, tuple) ) and len(UpperCAmelCase__ ) == 2): return False elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False else: return True def __getitem__( self : Any , UpperCAmelCase__ : tuple[int, int] ) -> Any: assert self.validate_indicies(UpperCAmelCase__ ) return self.array[loc[0]][loc[1]] def __setitem__( self : Dict , UpperCAmelCase__ : tuple[int, int] , UpperCAmelCase__ : float ) -> None: assert self.validate_indicies(UpperCAmelCase__ ) lowerCAmelCase = value def __add__( self : Any , UpperCAmelCase__ : Matrix ) -> Matrix: assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) assert self.row == another.row and self.column == another.column # Add lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] + another[r, c] return result def __neg__( self : int ) -> Matrix: lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = -self[r, c] return result def __sub__( self : str , UpperCAmelCase__ : Matrix ) -> Matrix: return self + (-another) def __mul__( self : str , UpperCAmelCase__ : int | float | Matrix ) -> Matrix: if isinstance(UpperCAmelCase__ , (int, float) ): # Scalar multiplication lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] * another return result elif isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): # Matrix multiplication assert self.column == another.row lowerCAmelCase = Matrix(self.row , another.column ) for r in range(self.row ): for c in range(another.column ): for i in range(self.column ): result[r, c] += self[r, i] * another[i, c] return result else: lowerCAmelCase = F'''Unsupported type given for another ({type(UpperCAmelCase__ )})''' raise TypeError(UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[Any] ) -> Matrix: lowerCAmelCase = Matrix(self.column , self.row ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] return result def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : Matrix , UpperCAmelCase__ : Matrix ) -> Any: assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) and isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) assert self.row == self.column == u.row == v.row # u, v should be column vector assert u.column == v.column == 1 # u, v should be column vector # Calculate lowerCAmelCase = v.transpose() lowerCAmelCase = (v_t * self * u)[0, 0] + 1 if numerator_factor == 0: return None # It's not invertable return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor)) # Testing if __name__ == "__main__": def a_ ( ): # a^(-1) lowerCAmelCase = Matrix(3 , 3 , 0 ) for i in range(3 ): lowerCAmelCase = 1 print(f'''a^(-1) is {ainv}''' ) # u, v lowerCAmelCase = Matrix(3 , 1 , 0 ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = 1, 2, -3 lowerCAmelCase = Matrix(3 , 1 , 0 ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = 4, -2, 5 print(f'''u is {u}''' ) print(f'''v is {v}''' ) print(f'''uv^T is {u * v.transpose()}''' ) # Sherman Morrison print(f'''(a + uv^T)^(-1) is {ainv.sherman_morrison(lowerCamelCase , lowerCamelCase )}''' ) def a_ ( ): import doctest doctest.testmod() testa()
4
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case =logging.get_logger(__name__) __snake_case ={ """facebook/s2t-wav2vec2-large-en-de""": ( """https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json""" ), # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2 } class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Union[str, Any] = '''speech_to_text_2''' lowerCamelCase : Any = ['''past_key_values'''] lowerCamelCase : Optional[Any] = {'''num_attention_heads''': '''decoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self : Optional[int] , UpperCAmelCase__ : Optional[Any]=1_0_0_0_0 , UpperCAmelCase__ : int=6 , UpperCAmelCase__ : Optional[Any]=2_0_4_8 , UpperCAmelCase__ : Union[str, Any]=4 , UpperCAmelCase__ : List[Any]=0.0 , UpperCAmelCase__ : str=True , UpperCAmelCase__ : str="relu" , UpperCAmelCase__ : Any=2_5_6 , UpperCAmelCase__ : Optional[Any]=0.1 , UpperCAmelCase__ : Any=0.0 , UpperCAmelCase__ : Tuple=0.0 , UpperCAmelCase__ : List[Any]=0.02 , UpperCAmelCase__ : List[Any]=2 , UpperCAmelCase__ : str=True , UpperCAmelCase__ : List[str]=1 , UpperCAmelCase__ : Any=0 , UpperCAmelCase__ : Dict=2 , UpperCAmelCase__ : int=1_0_2_4 , **UpperCAmelCase__ : Optional[Any] , ) -> Dict: lowerCAmelCase = vocab_size lowerCAmelCase = d_model lowerCAmelCase = decoder_ffn_dim lowerCAmelCase = decoder_layers lowerCAmelCase = decoder_attention_heads lowerCAmelCase = dropout lowerCAmelCase = attention_dropout lowerCAmelCase = activation_dropout lowerCAmelCase = activation_function lowerCAmelCase = init_std lowerCAmelCase = decoder_layerdrop lowerCAmelCase = use_cache lowerCAmelCase = decoder_layers lowerCAmelCase = scale_embedding # scale factor will be sqrt(d_model) if True lowerCAmelCase = max_target_positions super().__init__( pad_token_id=UpperCAmelCase__ , bos_token_id=UpperCAmelCase__ , eos_token_id=UpperCAmelCase__ , decoder_start_token_id=UpperCAmelCase__ , **UpperCAmelCase__ , )
4
1
'''simple docstring''' def a_ ( lowerCamelCase : str , lowerCamelCase : str ): lowerCAmelCase = len(lowerCamelCase ) lowerCAmelCase = [] for i in range(len(lowerCamelCase ) - pat_len + 1 ): lowerCAmelCase = True for j in range(lowerCamelCase ): if s[i + j] != pattern[j]: lowerCAmelCase = False break if match_found: position.append(lowerCamelCase ) return position if __name__ == "__main__": assert naive_pattern_search("""ABCDEFG""", """DE""") == [3] print(naive_pattern_search("""ABAAABCDBBABCDDEBCABC""", """ABC"""))
4
'''simple docstring''' from __future__ import annotations from bisect import bisect_left from functools import total_ordering from heapq import merge @total_ordering class UpperCAmelCase_ ( __lowercase ): def __lt__( self : Optional[int] , UpperCAmelCase__ : List[str] ) -> List[Any]: return self[-1] < other[-1] def __eq__( self : str , UpperCAmelCase__ : List[str] ) -> Tuple: return self[-1] == other[-1] def a_ ( lowerCamelCase : list ): lowerCAmelCase = [] # sort into stacks for element in collection: lowerCAmelCase = Stack([element] ) lowerCAmelCase = bisect_left(lowerCamelCase , lowerCamelCase ) if i != len(lowerCamelCase ): stacks[i].append(lowerCamelCase ) else: stacks.append(lowerCamelCase ) # use a heap-based merge to merge stack efficiently lowerCAmelCase = merge(*(reversed(lowerCamelCase ) for stack in stacks) ) return collection if __name__ == "__main__": __snake_case =input("""Enter numbers separated by a comma:\n""").strip() __snake_case =[int(item) for item in user_input.split(""",""")] print(patience_sort(unsorted))
4
1
'''simple docstring''' import tempfile import torch from diffusers import PNDMScheduler from .test_schedulers import SchedulerCommonTest class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Tuple = (PNDMScheduler,) lowerCamelCase : Optional[int] = (('''num_inference_steps''', 50),) def __UpperCAmelCase ( self : int , **UpperCAmelCase__ : List[str] ) -> Dict: lowerCAmelCase = { 'num_train_timesteps': 1_0_0_0, 'beta_start': 0.0_001, 'beta_end': 0.02, 'beta_schedule': 'linear', } config.update(**UpperCAmelCase__ ) return config def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : Any=0 , **UpperCAmelCase__ : List[Any] ) -> Tuple: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('num_inference_steps' , UpperCAmelCase__ ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample lowerCAmelCase = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config(**UpperCAmelCase__ ) lowerCAmelCase = scheduler_class(**UpperCAmelCase__ ) scheduler.set_timesteps(UpperCAmelCase__ ) # copy over dummy past residuals lowerCAmelCase = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(UpperCAmelCase__ ) lowerCAmelCase = scheduler_class.from_pretrained(UpperCAmelCase__ ) new_scheduler.set_timesteps(UpperCAmelCase__ ) # copy over dummy past residuals lowerCAmelCase = dummy_past_residuals[:] lowerCAmelCase = scheduler.step_prk(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , **UpperCAmelCase__ ).prev_sample lowerCAmelCase = new_scheduler.step_prk(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , **UpperCAmelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" lowerCAmelCase = scheduler.step_plms(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , **UpperCAmelCase__ ).prev_sample lowerCAmelCase = new_scheduler.step_plms(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , **UpperCAmelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __UpperCAmelCase ( self : List[str] ) -> Tuple: pass def __UpperCAmelCase ( self : Tuple , UpperCAmelCase__ : Optional[int]=0 , **UpperCAmelCase__ : Tuple ) -> List[str]: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('num_inference_steps' , UpperCAmelCase__ ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample lowerCAmelCase = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**UpperCAmelCase__ ) scheduler.set_timesteps(UpperCAmelCase__ ) # copy over dummy past residuals (must be after setting timesteps) lowerCAmelCase = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(UpperCAmelCase__ ) lowerCAmelCase = scheduler_class.from_pretrained(UpperCAmelCase__ ) # copy over dummy past residuals new_scheduler.set_timesteps(UpperCAmelCase__ ) # copy over dummy past residual (must be after setting timesteps) lowerCAmelCase = dummy_past_residuals[:] lowerCAmelCase = scheduler.step_prk(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , **UpperCAmelCase__ ).prev_sample lowerCAmelCase = new_scheduler.step_prk(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , **UpperCAmelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" lowerCAmelCase = scheduler.step_plms(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , **UpperCAmelCase__ ).prev_sample lowerCAmelCase = new_scheduler.step_plms(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , **UpperCAmelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __UpperCAmelCase ( self : Dict , **UpperCAmelCase__ : Tuple ) -> str: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(**UpperCAmelCase__ ) lowerCAmelCase = scheduler_class(**UpperCAmelCase__ ) lowerCAmelCase = 1_0 lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter scheduler.set_timesteps(UpperCAmelCase__ ) for i, t in enumerate(scheduler.prk_timesteps ): lowerCAmelCase = model(UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = scheduler.step_prk(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ).prev_sample for i, t in enumerate(scheduler.plms_timesteps ): lowerCAmelCase = model(UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = scheduler.step_plms(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ).prev_sample return sample def __UpperCAmelCase ( self : Tuple ) -> List[Any]: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('num_inference_steps' , UpperCAmelCase__ ) for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**UpperCAmelCase__ ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample if num_inference_steps is not None and hasattr(UpperCAmelCase__ , 'set_timesteps' ): scheduler.set_timesteps(UpperCAmelCase__ ) elif num_inference_steps is not None and not hasattr(UpperCAmelCase__ , 'set_timesteps' ): lowerCAmelCase = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) lowerCAmelCase = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] lowerCAmelCase = dummy_past_residuals[:] lowerCAmelCase = scheduler.step_prk(UpperCAmelCase__ , 0 , UpperCAmelCase__ , **UpperCAmelCase__ ).prev_sample lowerCAmelCase = scheduler.step_prk(UpperCAmelCase__ , 1 , UpperCAmelCase__ , **UpperCAmelCase__ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) lowerCAmelCase = scheduler.step_plms(UpperCAmelCase__ , 0 , UpperCAmelCase__ , **UpperCAmelCase__ ).prev_sample lowerCAmelCase = scheduler.step_plms(UpperCAmelCase__ , 1 , UpperCAmelCase__ , **UpperCAmelCase__ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def __UpperCAmelCase ( self : int ) -> Tuple: for timesteps in [1_0_0, 1_0_0_0]: self.check_over_configs(num_train_timesteps=UpperCAmelCase__ ) def __UpperCAmelCase ( self : Dict ) -> Optional[int]: for steps_offset in [0, 1]: self.check_over_configs(steps_offset=UpperCAmelCase__ ) lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(steps_offset=1 ) lowerCAmelCase = scheduler_class(**UpperCAmelCase__ ) scheduler.set_timesteps(1_0 ) assert torch.equal( scheduler.timesteps , torch.LongTensor( [9_0_1, 8_5_1, 8_5_1, 8_0_1, 8_0_1, 7_5_1, 7_5_1, 7_0_1, 7_0_1, 6_5_1, 6_5_1, 6_0_1, 6_0_1, 5_0_1, 4_0_1, 3_0_1, 2_0_1, 1_0_1, 1] ) , ) def __UpperCAmelCase ( self : Optional[int] ) -> Any: for beta_start, beta_end in zip([0.0_001, 0.001] , [0.002, 0.02] ): self.check_over_configs(beta_start=UpperCAmelCase__ , beta_end=UpperCAmelCase__ ) def __UpperCAmelCase ( self : Dict ) -> Tuple: for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=UpperCAmelCase__ ) def __UpperCAmelCase ( self : List[Any] ) -> int: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=UpperCAmelCase__ ) def __UpperCAmelCase ( self : Dict ) -> Union[str, Any]: for t in [1, 5, 1_0]: self.check_over_forward(time_step=UpperCAmelCase__ ) def __UpperCAmelCase ( self : str ) -> str: for t, num_inference_steps in zip([1, 5, 1_0] , [1_0, 5_0, 1_0_0] ): self.check_over_forward(num_inference_steps=UpperCAmelCase__ ) def __UpperCAmelCase ( self : Tuple ) -> str: # earlier version of set_timesteps() caused an error indexing alpha's with inference steps as power of 3 lowerCAmelCase = 2_7 for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**UpperCAmelCase__ ) scheduler.set_timesteps(UpperCAmelCase__ ) # before power of 3 fix, would error on first step, so we only need to do two for i, t in enumerate(scheduler.prk_timesteps[:2] ): lowerCAmelCase = scheduler.step_prk(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ).prev_sample def __UpperCAmelCase ( self : Union[str, Any] ) -> int: with self.assertRaises(UpperCAmelCase__ ): lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**UpperCAmelCase__ ) scheduler.step_plms(self.dummy_sample , 1 , self.dummy_sample ).prev_sample def __UpperCAmelCase ( self : Optional[Any] ) -> List[Any]: lowerCAmelCase = self.full_loop() lowerCAmelCase = torch.sum(torch.abs(UpperCAmelCase__ ) ) lowerCAmelCase = torch.mean(torch.abs(UpperCAmelCase__ ) ) assert abs(result_sum.item() - 198.1_318 ) < 1E-2 assert abs(result_mean.item() - 0.2_580 ) < 1E-3 def __UpperCAmelCase ( self : List[Any] ) -> List[Any]: lowerCAmelCase = self.full_loop(prediction_type='v_prediction' ) lowerCAmelCase = torch.sum(torch.abs(UpperCAmelCase__ ) ) lowerCAmelCase = torch.mean(torch.abs(UpperCAmelCase__ ) ) assert abs(result_sum.item() - 67.3_986 ) < 1E-2 assert abs(result_mean.item() - 0.0_878 ) < 1E-3 def __UpperCAmelCase ( self : Dict ) -> Optional[Any]: # We specify different beta, so that the first alpha is 0.99 lowerCAmelCase = self.full_loop(set_alpha_to_one=UpperCAmelCase__ , beta_start=0.01 ) lowerCAmelCase = torch.sum(torch.abs(UpperCAmelCase__ ) ) lowerCAmelCase = torch.mean(torch.abs(UpperCAmelCase__ ) ) assert abs(result_sum.item() - 230.0_399 ) < 1E-2 assert abs(result_mean.item() - 0.2_995 ) < 1E-3 def __UpperCAmelCase ( self : Any ) -> Optional[Any]: # We specify different beta, so that the first alpha is 0.99 lowerCAmelCase = self.full_loop(set_alpha_to_one=UpperCAmelCase__ , beta_start=0.01 ) lowerCAmelCase = torch.sum(torch.abs(UpperCAmelCase__ ) ) lowerCAmelCase = torch.mean(torch.abs(UpperCAmelCase__ ) ) assert abs(result_sum.item() - 186.9_482 ) < 1E-2 assert abs(result_mean.item() - 0.2_434 ) < 1E-3
4
'''simple docstring''' import datasets from .nmt_bleu import compute_bleu # From: https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py __snake_case ="""\ @INPROCEEDINGS{Papineni02bleu:a, author = {Kishore Papineni and Salim Roukos and Todd Ward and Wei-jing Zhu}, title = {BLEU: a Method for Automatic Evaluation of Machine Translation}, booktitle = {}, year = {2002}, pages = {311--318} } @inproceedings{lin-och-2004-orange, title = \"{ORANGE}: a Method for Evaluating Automatic Evaluation Metrics for Machine Translation\", author = \"Lin, Chin-Yew and Och, Franz Josef\", booktitle = \"{COLING} 2004: Proceedings of the 20th International Conference on Computational Linguistics\", month = \"aug 23{--}aug 27\", year = \"2004\", address = \"Geneva, Switzerland\", publisher = \"COLING\", url = \"https://www.aclweb.org/anthology/C04-1072\", pages = \"501--507\", } """ __snake_case ="""\ BLEU (bilingual evaluation understudy) is an algorithm for evaluating the quality of text which has been machine-translated from one natural language to another. Quality is considered to be the correspondence between a machine's output and that of a human: \"the closer a machine translation is to a professional human translation, the better it is\" – this is the central idea behind BLEU. BLEU was one of the first metrics to claim a high correlation with human judgements of quality, and remains one of the most popular automated and inexpensive metrics. Scores are calculated for individual translated segments—generally sentences—by comparing them with a set of good quality reference translations. Those scores are then averaged over the whole corpus to reach an estimate of the translation's overall quality. Intelligibility or grammatical correctness are not taken into account[citation needed]. BLEU's output is always a number between 0 and 1. This value indicates how similar the candidate text is to the reference texts, with values closer to 1 representing more similar texts. Few human translations will attain a score of 1, since this would indicate that the candidate is identical to one of the reference translations. For this reason, it is not necessary to attain a score of 1. Because there are more opportunities to match, adding additional reference translations will increase the BLEU score. """ __snake_case =""" Computes BLEU score of translated segments against one or more references. Args: predictions: list of translations to score. Each translation should be tokenized into a list of tokens. references: list of lists of references for each translation. Each reference should be tokenized into a list of tokens. max_order: Maximum n-gram order to use when computing BLEU score. smooth: Whether or not to apply Lin et al. 2004 smoothing. Returns: 'bleu': bleu score, 'precisions': geometric mean of n-gram precisions, 'brevity_penalty': brevity penalty, 'length_ratio': ratio of lengths, 'translation_length': translation_length, 'reference_length': reference_length Examples: >>> predictions = [ ... [\"hello\", \"there\", \"general\", \"kenobi\"], # tokenized prediction of the first sample ... [\"foo\", \"bar\", \"foobar\"] # tokenized prediction of the second sample ... ] >>> references = [ ... [[\"hello\", \"there\", \"general\", \"kenobi\"], [\"hello\", \"there\", \"!\"]], # tokenized references for the first sample (2 references) ... [[\"foo\", \"bar\", \"foobar\"]] # tokenized references for the second sample (1 reference) ... ] >>> bleu = datasets.load_metric(\"bleu\") >>> results = bleu.compute(predictions=predictions, references=references) >>> print(results[\"bleu\"]) 1.0 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): def __UpperCAmelCase ( self : Tuple ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string' , id='token' ) , id='sequence' ), 'references': datasets.Sequence( datasets.Sequence(datasets.Value('string' , id='token' ) , id='sequence' ) , id='references' ), } ) , codebase_urls=['https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py'] , reference_urls=[ 'https://en.wikipedia.org/wiki/BLEU', 'https://towardsdatascience.com/evaluating-text-output-in-nlp-bleu-at-your-own-risk-e8609665a213', ] , ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Any , UpperCAmelCase__ : Union[str, Any]=4 , UpperCAmelCase__ : Optional[int]=False ) -> int: lowerCAmelCase = compute_bleu( reference_corpus=UpperCAmelCase__ , translation_corpus=UpperCAmelCase__ , max_order=UpperCAmelCase__ , smooth=UpperCAmelCase__ ) ((lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase)) = score return { "bleu": bleu, "precisions": precisions, "brevity_penalty": bp, "length_ratio": ratio, "translation_length": translation_length, "reference_length": reference_length, }
4
1
'''simple docstring''' import pprint import requests __snake_case ="""https://zenquotes.io/api""" def a_ ( ): return requests.get(API_ENDPOINT_URL + '/today' ).json() def a_ ( ): return requests.get(API_ENDPOINT_URL + '/random' ).json() if __name__ == "__main__": __snake_case =random_quotes() pprint.pprint(response)
4
'''simple docstring''' from sklearn.metrics import fa_score, matthews_corrcoef import datasets from .record_evaluation import evaluate as evaluate_record __snake_case ="""\ @article{wang2019superglue, title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems}, author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R}, journal={arXiv preprint arXiv:1905.00537}, year={2019} } """ __snake_case ="""\ SuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after GLUE with a new set of more difficult language understanding tasks, improved resources, and a new public leaderboard. """ __snake_case =""" Compute SuperGLUE evaluation metric associated to each SuperGLUE dataset. Args: predictions: list of predictions to score. Depending on the SuperGlUE subset: - for 'record': list of question-answer dictionaries with the following keys: - 'idx': index of the question as specified by the dataset - 'prediction_text': the predicted answer text - for 'multirc': list of question-answer dictionaries with the following keys: - 'idx': index of the question-answer pair as specified by the dataset - 'prediction': the predicted answer label - otherwise: list of predicted labels references: list of reference labels. Depending on the SuperGLUE subset: - for 'record': list of question-answers dictionaries with the following keys: - 'idx': index of the question as specified by the dataset - 'answers': list of possible answers - otherwise: list of reference labels Returns: depending on the SuperGLUE subset: - for 'record': - 'exact_match': Exact match between answer and gold answer - 'f1': F1 score - for 'multirc': - 'exact_match': Exact match between answer and gold answer - 'f1_m': Per-question macro-F1 score - 'f1_a': Average F1 score over all answers - for 'axb': 'matthews_correlation': Matthew Correlation - for 'cb': - 'accuracy': Accuracy - 'f1': F1 score - for all others: - 'accuracy': Accuracy Examples: >>> super_glue_metric = datasets.load_metric('super_glue', 'copa') # any of [\"copa\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"boolq\", \"axg\"] >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'cb') >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0, 'f1': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'record') >>> predictions = [{'idx': {'passage': 0, 'query': 0}, 'prediction_text': 'answer'}] >>> references = [{'idx': {'passage': 0, 'query': 0}, 'answers': ['answer', 'another_answer']}] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 1.0, 'f1': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'multirc') >>> predictions = [{'idx': {'answer': 0, 'paragraph': 0, 'question': 0}, 'prediction': 0}, {'idx': {'answer': 1, 'paragraph': 2, 'question': 3}, 'prediction': 1}] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 1.0, 'f1_m': 1.0, 'f1_a': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'axb') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'matthews_correlation': 1.0} """ def a_ ( lowerCamelCase : str , lowerCamelCase : Union[str, Any] ): return float((preds == labels).mean() ) def a_ ( lowerCamelCase : Union[str, Any] , lowerCamelCase : Dict , lowerCamelCase : str="binary" ): lowerCAmelCase = simple_accuracy(lowerCamelCase , lowerCamelCase ) lowerCAmelCase = float(fa_score(y_true=lowerCamelCase , y_pred=lowerCamelCase , average=lowerCamelCase ) ) return { "accuracy": acc, "f1": fa, } def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : List[Any] ): lowerCAmelCase = {} for id_pred, label in zip(lowerCamelCase , lowerCamelCase ): lowerCAmelCase = f'''{id_pred['idx']['paragraph']}-{id_pred['idx']['question']}''' lowerCAmelCase = id_pred['prediction'] if question_id in question_map: question_map[question_id].append((pred, label) ) else: lowerCAmelCase = [(pred, label)] lowerCAmelCase , lowerCAmelCase = [], [] for question, preds_labels in question_map.items(): lowerCAmelCase , lowerCAmelCase = zip(*lowerCamelCase ) lowerCAmelCase = fa_score(y_true=lowerCamelCase , y_pred=lowerCamelCase , average='macro' ) fas.append(lowerCamelCase ) lowerCAmelCase = int(sum(pred == label for pred, label in preds_labels ) == len(lowerCamelCase ) ) ems.append(lowerCamelCase ) lowerCAmelCase = float(sum(lowerCamelCase ) / len(lowerCamelCase ) ) lowerCAmelCase = sum(lowerCamelCase ) / len(lowerCamelCase ) lowerCAmelCase = float(fa_score(y_true=lowerCamelCase , y_pred=[id_pred['prediction'] for id_pred in ids_preds] ) ) return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a} @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): def __UpperCAmelCase ( self : List[str] ) -> List[Any]: if self.config_name not in [ "boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg", ]: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , codebase_urls=[] , reference_urls=[] , format='numpy' if not self.config_name == 'record' and not self.config_name == 'multirc' else None , ) def __UpperCAmelCase ( self : Union[str, Any] ) -> str: if self.config_name == "record": return { "predictions": { "idx": { "passage": datasets.Value('int64' ), "query": datasets.Value('int64' ), }, "prediction_text": datasets.Value('string' ), }, "references": { "idx": { "passage": datasets.Value('int64' ), "query": datasets.Value('int64' ), }, "answers": datasets.Sequence(datasets.Value('string' ) ), }, } elif self.config_name == "multirc": return { "predictions": { "idx": { "answer": datasets.Value('int64' ), "paragraph": datasets.Value('int64' ), "question": datasets.Value('int64' ), }, "prediction": datasets.Value('int64' ), }, "references": datasets.Value('int64' ), } else: return { "predictions": datasets.Value('int64' ), "references": datasets.Value('int64' ), } def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : Any , UpperCAmelCase__ : Optional[Any] ) -> Any: if self.config_name == "axb": return {"matthews_correlation": matthews_corrcoef(UpperCAmelCase__ , UpperCAmelCase__ )} elif self.config_name == "cb": return acc_and_fa(UpperCAmelCase__ , UpperCAmelCase__ , fa_avg='macro' ) elif self.config_name == "record": lowerCAmelCase = [ { 'qas': [ {'id': ref['idx']['query'], 'answers': [{'text': ans} for ans in ref['answers']]} for ref in references ] } ] lowerCAmelCase = {pred['idx']['query']: pred['prediction_text'] for pred in predictions} return evaluate_record(UpperCAmelCase__ , UpperCAmelCase__ )[0] elif self.config_name == "multirc": return evaluate_multirc(UpperCAmelCase__ , UpperCAmelCase__ ) elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]: return {"accuracy": simple_accuracy(UpperCAmelCase__ , UpperCAmelCase__ )} else: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]' )
4
1
'''simple docstring''' def a_ ( lowerCamelCase : int ): lowerCAmelCase = n ** (1 / 3) return (val * val * val) == n if __name__ == "__main__": print(perfect_cube(27)) print(perfect_cube(4))
4
'''simple docstring''' print((lambda quine: quine % quine)("""print((lambda quine: quine %% quine)(%r))"""))
4
1
'''simple docstring''' import numpy as np import torch from imwatermark import WatermarkEncoder # Copied from https://github.com/Stability-AI/generative-models/blob/613af104c6b85184091d42d374fef420eddb356d/scripts/demo/streamlit_helpers.py#L66 __snake_case =0B1011_0011_1110_1100_1001_0000_0111_1011_1011_0001_1001_1110 # bin(x)[2:] gives bits of x as str, use int to convert them to 0/1 __snake_case =[int(bit) for bit in bin(WATERMARK_MESSAGE)[2:]] class UpperCAmelCase_ : def __init__( self : List[str] ) -> str: lowerCAmelCase = WATERMARK_BITS lowerCAmelCase = WatermarkEncoder() self.encoder.set_watermark('bits' , self.watermark ) def __UpperCAmelCase ( self : Dict , UpperCAmelCase__ : torch.FloatTensor ) -> str: # can't encode images that are smaller than 256 if images.shape[-1] < 2_5_6: return images lowerCAmelCase = (2_5_5 * (images / 2 + 0.5)).cpu().permute(0 , 2 , 3 , 1 ).float().numpy() lowerCAmelCase = [self.encoder.encode(UpperCAmelCase__ , 'dwtDct' ) for image in images] lowerCAmelCase = torch.from_numpy(np.array(UpperCAmelCase__ ) ).permute(0 , 3 , 1 , 2 ) lowerCAmelCase = torch.clamp(2 * (images / 2_5_5 - 0.5) , min=-1.0 , max=1.0 ) return images
4
'''simple docstring''' import os __snake_case ={"""I""": 1, """V""": 5, """X""": 10, """L""": 50, """C""": 100, """D""": 500, """M""": 1_000} def a_ ( lowerCamelCase : str ): lowerCAmelCase = 0 lowerCAmelCase = 0 while index < len(lowerCamelCase ) - 1: lowerCAmelCase = SYMBOLS[numerals[index]] lowerCAmelCase = SYMBOLS[numerals[index + 1]] if current_value < next_value: total_value -= current_value else: total_value += current_value index += 1 total_value += SYMBOLS[numerals[index]] return total_value def a_ ( lowerCamelCase : int ): lowerCAmelCase = '' lowerCAmelCase = num // 1000 numerals += m_count * "M" num %= 1000 lowerCAmelCase = num // 100 if c_count == 9: numerals += "CM" c_count -= 9 elif c_count == 4: numerals += "CD" c_count -= 4 if c_count >= 5: numerals += "D" c_count -= 5 numerals += c_count * "C" num %= 100 lowerCAmelCase = num // 10 if x_count == 9: numerals += "XC" x_count -= 9 elif x_count == 4: numerals += "XL" x_count -= 4 if x_count >= 5: numerals += "L" x_count -= 5 numerals += x_count * "X" num %= 10 if num == 9: numerals += "IX" num -= 9 elif num == 4: numerals += "IV" num -= 4 if num >= 5: numerals += "V" num -= 5 numerals += num * "I" return numerals def a_ ( lowerCamelCase : str = "/p089_roman.txt" ): lowerCAmelCase = 0 with open(os.path.dirname(lowerCamelCase ) + roman_numerals_filename ) as filea: lowerCAmelCase = filea.readlines() for line in lines: lowerCAmelCase = line.strip() lowerCAmelCase = parse_roman_numerals(lowerCamelCase ) lowerCAmelCase = generate_roman_numerals(lowerCamelCase ) savings += len(lowerCamelCase ) - len(lowerCamelCase ) return savings if __name__ == "__main__": print(F'''{solution() = }''')
4
1
'''simple docstring''' class UpperCAmelCase_ : def __init__( self : Optional[int] , UpperCAmelCase__ : int ) -> Union[str, Any]: lowerCAmelCase = val lowerCAmelCase = None lowerCAmelCase = None def __UpperCAmelCase ( self : str , UpperCAmelCase__ : List[str] ) -> Tuple: if self.val: if val < self.val: if self.left is None: lowerCAmelCase = Node(UpperCAmelCase__ ) else: self.left.insert(UpperCAmelCase__ ) elif val > self.val: if self.right is None: lowerCAmelCase = Node(UpperCAmelCase__ ) else: self.right.insert(UpperCAmelCase__ ) else: lowerCAmelCase = val def a_ ( lowerCamelCase : Any , lowerCamelCase : int ): # Recursive traversal if root: inorder(root.left , lowerCamelCase ) res.append(root.val ) inorder(root.right , lowerCamelCase ) def a_ ( lowerCamelCase : Dict ): # Build BST if len(lowerCamelCase ) == 0: return arr lowerCAmelCase = Node(arr[0] ) for i in range(1 , len(lowerCamelCase ) ): root.insert(arr[i] ) # Traverse BST in order. lowerCAmelCase = [] inorder(lowerCamelCase , lowerCamelCase ) return res if __name__ == "__main__": print(tree_sort([10, 1, 3, 2, 9, 14, 13]))
4
'''simple docstring''' import itertools import random import unittest import numpy as np from transformers import ASTFeatureExtractor from transformers.testing_utils import require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin __snake_case =random.Random() if is_torch_available(): import torch def a_ ( lowerCamelCase : Dict , lowerCamelCase : Dict=1.0 , lowerCamelCase : List[Any]=None , lowerCamelCase : Union[str, Any]=None ): if rng is None: lowerCAmelCase = global_rng lowerCAmelCase = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class UpperCAmelCase_ ( unittest.TestCase ): def __init__( self : List[Any] , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : List[str]=7 , UpperCAmelCase__ : int=4_0_0 , UpperCAmelCase__ : int=2_0_0_0 , UpperCAmelCase__ : List[str]=1 , UpperCAmelCase__ : Tuple=0.0 , UpperCAmelCase__ : Tuple=1_6_0_0_0 , UpperCAmelCase__ : Tuple=True , UpperCAmelCase__ : Union[str, Any]=True , ) -> Any: lowerCAmelCase = parent lowerCAmelCase = batch_size lowerCAmelCase = min_seq_length lowerCAmelCase = max_seq_length lowerCAmelCase = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) lowerCAmelCase = feature_size lowerCAmelCase = padding_value lowerCAmelCase = sampling_rate lowerCAmelCase = return_attention_mask lowerCAmelCase = do_normalize def __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def __UpperCAmelCase ( self : str , UpperCAmelCase__ : Union[str, Any]=False , UpperCAmelCase__ : Union[str, Any]=False ) -> Optional[Any]: def _flatten(UpperCAmelCase__ : int ): return list(itertools.chain(*UpperCAmelCase__ ) ) if equal_length: lowerCAmelCase = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size lowerCAmelCase = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: lowerCAmelCase = [np.asarray(UpperCAmelCase__ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Dict = ASTFeatureExtractor def __UpperCAmelCase ( self : str ) -> Optional[int]: lowerCAmelCase = ASTFeatureExtractionTester(self ) def __UpperCAmelCase ( self : Optional[int] ) -> Optional[int]: # Tests that all call wrap to encode_plus and batch_encode_plus lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 lowerCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] lowerCAmelCase = [np.asarray(UpperCAmelCase__ ) for speech_input in speech_inputs] # Test not batched input lowerCAmelCase = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values lowerCAmelCase = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1E-3 ) ) # Test batched lowerCAmelCase = feat_extract(UpperCAmelCase__ , padding=UpperCAmelCase__ , return_tensors='np' ).input_values lowerCAmelCase = feat_extract(UpperCAmelCase__ , padding=UpperCAmelCase__ , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__ , UpperCAmelCase__ ): self.assertTrue(np.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. lowerCAmelCase = [floats_list((1, x) )[0] for x in (8_0_0, 8_0_0, 8_0_0)] lowerCAmelCase = np.asarray(UpperCAmelCase__ ) lowerCAmelCase = feat_extract(UpperCAmelCase__ , return_tensors='np' ).input_values lowerCAmelCase = feat_extract(UpperCAmelCase__ , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__ , UpperCAmelCase__ ): self.assertTrue(np.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1E-3 ) ) @require_torch def __UpperCAmelCase ( self : Union[str, Any] ) -> Optional[int]: import torch lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCAmelCase = np.random.rand(1_0_0 ).astype(np.floataa ) lowerCAmelCase = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: lowerCAmelCase = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) lowerCAmelCase = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def __UpperCAmelCase ( self : int , UpperCAmelCase__ : str ) -> Tuple: from datasets import load_dataset lowerCAmelCase = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech lowerCAmelCase = ds.sort('id' ).select(range(UpperCAmelCase__ ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] @require_torch def __UpperCAmelCase ( self : str ) -> Optional[Any]: # fmt: off lowerCAmelCase = torch.tensor( [-0.9_894, -1.2_776, -0.9_066, -1.2_776, -0.9_349, -1.2_609, -1.0_386, -1.2_776, -1.1_561, -1.2_776, -1.2_052, -1.2_723, -1.2_190, -1.2_132, -1.2_776, -1.1_133, -1.1_953, -1.1_343, -1.1_584, -1.2_203, -1.1_770, -1.2_474, -1.2_381, -1.1_936, -0.9_270, -0.8_317, -0.8_049, -0.7_706, -0.7_565, -0.7_869] ) # fmt: on lowerCAmelCase = self._load_datasamples(1 ) lowerCAmelCase = ASTFeatureExtractor() lowerCAmelCase = feature_extractor(UpperCAmelCase__ , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 1_0_2_4, 1_2_8) ) self.assertTrue(torch.allclose(input_values[0, 0, :3_0] , UpperCAmelCase__ , atol=1E-4 ) )
4
1
'''simple docstring''' import enum import warnings from ..tokenization_utils import TruncationStrategy from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING __snake_case =logging.get_logger(__name__) class UpperCAmelCase_ ( enum.Enum ): lowerCamelCase : Optional[int] = 0 lowerCamelCase : str = 1 @add_end_docstrings(__lowercase ) class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Tuple = '''generated''' def __init__( self : Optional[int] , *UpperCAmelCase__ : Optional[Any] , **UpperCAmelCase__ : Optional[int] ) -> Tuple: super().__init__(*UpperCAmelCase__ , **UpperCAmelCase__ ) self.check_model_type( TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING if self.framework == 'tf' else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING ) def __UpperCAmelCase ( self : Dict , UpperCAmelCase__ : List[str]=None , UpperCAmelCase__ : Dict=None , UpperCAmelCase__ : List[str]=None , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : Any=None , UpperCAmelCase__ : Dict=None , **UpperCAmelCase__ : List[str] , ) -> Union[str, Any]: lowerCAmelCase = {} if truncation is not None: lowerCAmelCase = truncation lowerCAmelCase = generate_kwargs lowerCAmelCase = {} if return_tensors is not None and return_type is None: lowerCAmelCase = ReturnType.TENSORS if return_tensors else ReturnType.TEXT if return_type is not None: lowerCAmelCase = return_type if clean_up_tokenization_spaces is not None: lowerCAmelCase = clean_up_tokenization_spaces if stop_sequence is not None: lowerCAmelCase = self.tokenizer.encode(UpperCAmelCase__ , add_special_tokens=UpperCAmelCase__ ) if len(UpperCAmelCase__ ) > 1: warnings.warn( 'Stopping on a multiple token sequence is not yet supported on transformers. The first token of' ' the stop sequence will be used as the stop sequence string in the interim.' ) lowerCAmelCase = stop_sequence_ids[0] return preprocess_params, forward_params, postprocess_params def __UpperCAmelCase ( self : List[Any] , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : int ) -> List[Any]: return True def __UpperCAmelCase ( self : Optional[int] , *UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : List[Any] ) -> Optional[int]: lowerCAmelCase = self.model.config.prefix if self.model.config.prefix is not None else '' if isinstance(args[0] , UpperCAmelCase__ ): if self.tokenizer.pad_token_id is None: raise ValueError('Please make sure that the tokenizer has a pad_token_id when using a batch input' ) lowerCAmelCase = ([prefix + arg for arg in args[0]],) lowerCAmelCase = True elif isinstance(args[0] , UpperCAmelCase__ ): lowerCAmelCase = (prefix + args[0],) lowerCAmelCase = False else: raise ValueError( F''' `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`''' ) lowerCAmelCase = self.tokenizer(*UpperCAmelCase__ , padding=UpperCAmelCase__ , truncation=UpperCAmelCase__ , return_tensors=self.framework ) # This is produced by tokenizers but is an invalid generate kwargs if "token_type_ids" in inputs: del inputs["token_type_ids"] return inputs def __call__( self : Optional[Any] , *UpperCAmelCase__ : List[Any] , **UpperCAmelCase__ : str ) -> List[Any]: lowerCAmelCase = super().__call__(*UpperCAmelCase__ , **UpperCAmelCase__ ) if ( isinstance(args[0] , UpperCAmelCase__ ) and all(isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for el in args[0] ) and all(len(UpperCAmelCase__ ) == 1 for res in result ) ): return [res[0] for res in result] return result def __UpperCAmelCase ( self : Optional[Any] , UpperCAmelCase__ : Any , UpperCAmelCase__ : Optional[Any]=TruncationStrategy.DO_NOT_TRUNCATE , **UpperCAmelCase__ : int ) -> int: lowerCAmelCase = self._parse_and_tokenize(UpperCAmelCase__ , truncation=UpperCAmelCase__ , **UpperCAmelCase__ ) return inputs def __UpperCAmelCase ( self : int , UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : str ) -> Tuple: if self.framework == "pt": lowerCAmelCase , lowerCAmelCase = model_inputs['input_ids'].shape elif self.framework == "tf": lowerCAmelCase , lowerCAmelCase = tf.shape(model_inputs['input_ids'] ).numpy() lowerCAmelCase = generate_kwargs.get('min_length' , self.model.config.min_length ) lowerCAmelCase = generate_kwargs.get('max_length' , self.model.config.max_length ) self.check_inputs(UpperCAmelCase__ , generate_kwargs['min_length'] , generate_kwargs['max_length'] ) lowerCAmelCase = self.model.generate(**UpperCAmelCase__ , **UpperCAmelCase__ ) lowerCAmelCase = output_ids.shape[0] if self.framework == "pt": lowerCAmelCase = output_ids.reshape(UpperCAmelCase__ , out_b // in_b , *output_ids.shape[1:] ) elif self.framework == "tf": lowerCAmelCase = tf.reshape(UpperCAmelCase__ , (in_b, out_b // in_b, *output_ids.shape[1:]) ) return {"output_ids": output_ids} def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : int , UpperCAmelCase__ : List[Any]=ReturnType.TEXT , UpperCAmelCase__ : str=False ) -> int: lowerCAmelCase = [] for output_ids in model_outputs["output_ids"][0]: if return_type == ReturnType.TENSORS: lowerCAmelCase = {F'''{self.return_name}_token_ids''': output_ids} elif return_type == ReturnType.TEXT: lowerCAmelCase = { F'''{self.return_name}_text''': self.tokenizer.decode( UpperCAmelCase__ , skip_special_tokens=UpperCAmelCase__ , clean_up_tokenization_spaces=UpperCAmelCase__ , ) } records.append(UpperCAmelCase__ ) return records @add_end_docstrings(__lowercase ) class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Union[str, Any] = '''summary''' def __call__( self : Any , *UpperCAmelCase__ : int , **UpperCAmelCase__ : str ) -> Union[str, Any]: return super().__call__(*UpperCAmelCase__ , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : int ) -> bool: if max_length < min_length: logger.warning(F'''Your min_length={min_length} must be inferior than your max_length={max_length}.''' ) if input_length < max_length: logger.warning( F'''Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is ''' 'a summarization task, where outputs shorter than the input are typically wanted, you might ' F'''consider decreasing max_length manually, e.g. summarizer(\'...\', max_length={input_length//2})''' ) @add_end_docstrings(__lowercase ) class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Union[str, Any] = '''translation''' def __UpperCAmelCase ( self : str , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : int ) -> Union[str, Any]: if input_length > 0.9 * max_length: logger.warning( F'''Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider ''' 'increasing your max_length manually, e.g. translator(\'...\', max_length=400)' ) return True def __UpperCAmelCase ( self : Union[str, Any] , *UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Optional[int]=TruncationStrategy.DO_NOT_TRUNCATE , UpperCAmelCase__ : str=None , UpperCAmelCase__ : Optional[int]=None ) -> str: if getattr(self.tokenizer , '_build_translation_inputs' , UpperCAmelCase__ ): return self.tokenizer._build_translation_inputs( *UpperCAmelCase__ , return_tensors=self.framework , truncation=UpperCAmelCase__ , src_lang=UpperCAmelCase__ , tgt_lang=UpperCAmelCase__ ) else: return super()._parse_and_tokenize(*UpperCAmelCase__ , truncation=UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : List[Any]=None , UpperCAmelCase__ : Union[str, Any]=None , **UpperCAmelCase__ : int ) -> Tuple: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = super()._sanitize_parameters(**UpperCAmelCase__ ) if src_lang is not None: lowerCAmelCase = src_lang if tgt_lang is not None: lowerCAmelCase = tgt_lang if src_lang is None and tgt_lang is None: # Backward compatibility, direct arguments use is preferred. lowerCAmelCase = kwargs.get('task' , self.task ) lowerCAmelCase = task.split('_' ) if task and len(UpperCAmelCase__ ) == 4: # translation, XX, to YY lowerCAmelCase = items[1] lowerCAmelCase = items[3] return preprocess_params, forward_params, postprocess_params def __call__( self : Optional[int] , *UpperCAmelCase__ : int , **UpperCAmelCase__ : Any ) -> List[Any]: return super().__call__(*UpperCAmelCase__ , **UpperCAmelCase__ )
4
'''simple docstring''' import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : str ) -> List[Any]: lowerCAmelCase = torch.nn.Linear(1_0 , 1_0 ) lowerCAmelCase = torch.optim.SGD(model.parameters() , 0.1 ) lowerCAmelCase = Accelerator() lowerCAmelCase = accelerator.prepare(UpperCAmelCase__ ) try: pickle.loads(pickle.dumps(UpperCAmelCase__ ) ) except Exception as e: self.fail(F'''Accelerated optimizer pickling failed with {e}''' ) AcceleratorState._reset_state()
4
1
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class UpperCAmelCase_ ( unittest.TestCase ): def __init__( self : Dict , UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Union[str, Any]=7 , UpperCAmelCase__ : int=3 , UpperCAmelCase__ : Tuple=1_8 , UpperCAmelCase__ : Optional[int]=3_0 , UpperCAmelCase__ : Union[str, Any]=4_0_0 , UpperCAmelCase__ : int=True , UpperCAmelCase__ : Any=None , UpperCAmelCase__ : Optional[Any]=True , ) -> int: lowerCAmelCase = size if size is not None else {'height': 1_8, 'width': 1_8} lowerCAmelCase = parent lowerCAmelCase = batch_size lowerCAmelCase = num_channels lowerCAmelCase = image_size lowerCAmelCase = min_resolution lowerCAmelCase = max_resolution lowerCAmelCase = do_resize lowerCAmelCase = size lowerCAmelCase = apply_ocr def __UpperCAmelCase ( self : List[Any] ) -> Union[str, Any]: return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Optional[int] = LayoutLMvaImageProcessor if is_pytesseract_available() else None def __UpperCAmelCase ( self : Tuple ) -> int: lowerCAmelCase = LayoutLMvaImageProcessingTester(self ) @property def __UpperCAmelCase ( self : Any ) -> List[Any]: return self.image_processor_tester.prepare_image_processor_dict() def __UpperCAmelCase ( self : int ) -> Union[str, Any]: lowerCAmelCase = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCAmelCase__ , 'do_resize' ) ) self.assertTrue(hasattr(UpperCAmelCase__ , 'size' ) ) self.assertTrue(hasattr(UpperCAmelCase__ , 'apply_ocr' ) ) def __UpperCAmelCase ( self : List[Any] ) -> Union[str, Any]: lowerCAmelCase = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 1_8, 'width': 1_8} ) lowerCAmelCase = self.image_processing_class.from_dict(self.image_processor_dict , size=4_2 ) self.assertEqual(image_processor.size , {'height': 4_2, 'width': 4_2} ) def __UpperCAmelCase ( self : Dict ) -> str: pass def __UpperCAmelCase ( self : Optional[Any] ) -> Union[str, Any]: # Initialize image_processing lowerCAmelCase = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowerCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase__ , Image.Image ) # Test not batched input lowerCAmelCase = image_processing(image_inputs[0] , return_tensors='pt' ) self.assertEqual( encoding.pixel_values.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) self.assertIsInstance(encoding.words , UpperCAmelCase__ ) self.assertIsInstance(encoding.boxes , UpperCAmelCase__ ) # Test batched lowerCAmelCase = image_processing(UpperCAmelCase__ , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) def __UpperCAmelCase ( self : Optional[int] ) -> Dict: # Initialize image_processing lowerCAmelCase = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowerCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase__ , numpify=UpperCAmelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase__ , np.ndarray ) # Test not batched input lowerCAmelCase = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowerCAmelCase = image_processing(UpperCAmelCase__ , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) def __UpperCAmelCase ( self : List[Any] ) -> Any: # Initialize image_processing lowerCAmelCase = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowerCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase__ , torchify=UpperCAmelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase__ , torch.Tensor ) # Test not batched input lowerCAmelCase = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowerCAmelCase = image_processing(UpperCAmelCase__ , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) def __UpperCAmelCase ( self : str ) -> Optional[int]: # with apply_OCR = True lowerCAmelCase = LayoutLMvaImageProcessor() from datasets import load_dataset lowerCAmelCase = load_dataset('hf-internal-testing/fixtures_docvqa' , split='test' ) lowerCAmelCase = Image.open(ds[0]['file'] ).convert('RGB' ) lowerCAmelCase = image_processing(UpperCAmelCase__ , return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 2_2_4, 2_2_4) ) self.assertEqual(len(encoding.words ) , len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 lowerCAmelCase = [['11:14', 'to', '11:39', 'a.m', '11:39', 'to', '11:44', 'a.m.', '11:44', 'a.m.', 'to', '12:25', 'p.m.', '12:25', 'to', '12:58', 'p.m.', '12:58', 'to', '4:00', 'p.m.', '2:00', 'to', '5:00', 'p.m.', 'Coffee', 'Break', 'Coffee', 'will', 'be', 'served', 'for', 'men', 'and', 'women', 'in', 'the', 'lobby', 'adjacent', 'to', 'exhibit', 'area.', 'Please', 'move', 'into', 'exhibit', 'area.', '(Exhibits', 'Open)', 'TRRF', 'GENERAL', 'SESSION', '(PART', '|)', 'Presiding:', 'Lee', 'A.', 'Waller', 'TRRF', 'Vice', 'President', '“Introductory', 'Remarks”', 'Lee', 'A.', 'Waller,', 'TRRF', 'Vice', 'Presi-', 'dent', 'Individual', 'Interviews', 'with', 'TRRF', 'Public', 'Board', 'Members', 'and', 'Sci-', 'entific', 'Advisory', 'Council', 'Mem-', 'bers', 'Conducted', 'by', 'TRRF', 'Treasurer', 'Philip', 'G.', 'Kuehn', 'to', 'get', 'answers', 'which', 'the', 'public', 'refrigerated', 'warehousing', 'industry', 'is', 'looking', 'for.', 'Plus', 'questions', 'from', 'the', 'floor.', 'Dr.', 'Emil', 'M.', 'Mrak,', 'University', 'of', 'Cal-', 'ifornia,', 'Chairman,', 'TRRF', 'Board;', 'Sam', 'R.', 'Cecil,', 'University', 'of', 'Georgia', 'College', 'of', 'Agriculture;', 'Dr.', 'Stanley', 'Charm,', 'Tufts', 'University', 'School', 'of', 'Medicine;', 'Dr.', 'Robert', 'H.', 'Cotton,', 'ITT', 'Continental', 'Baking', 'Company;', 'Dr.', 'Owen', 'Fennema,', 'University', 'of', 'Wis-', 'consin;', 'Dr.', 'Robert', 'E.', 'Hardenburg,', 'USDA.', 'Questions', 'and', 'Answers', 'Exhibits', 'Open', 'Capt.', 'Jack', 'Stoney', 'Room', 'TRRF', 'Scientific', 'Advisory', 'Council', 'Meeting', 'Ballroom', 'Foyer']] # noqa: E231 lowerCAmelCase = [[[1_4_1, 5_7, 2_1_4, 6_9], [2_2_8, 5_8, 2_5_2, 6_9], [1_4_1, 7_5, 2_1_6, 8_8], [2_3_0, 7_9, 2_8_0, 8_8], [1_4_2, 2_6_0, 2_1_8, 2_7_3], [2_3_0, 2_6_1, 2_5_5, 2_7_3], [1_4_3, 2_7_9, 2_1_8, 2_9_0], [2_3_1, 2_8_2, 2_9_0, 2_9_1], [1_4_3, 3_4_2, 2_1_8, 3_5_4], [2_3_1, 3_4_5, 2_8_9, 3_5_5], [2_0_2, 3_6_2, 2_2_7, 3_7_3], [1_4_3, 3_7_9, 2_2_0, 3_9_2], [2_3_1, 3_8_2, 2_9_1, 3_9_4], [1_4_4, 7_1_4, 2_2_0, 7_2_6], [2_3_1, 7_1_5, 2_5_6, 7_2_6], [1_4_4, 7_3_2, 2_2_0, 7_4_5], [2_3_2, 7_3_6, 2_9_1, 7_4_7], [1_4_4, 7_6_9, 2_1_8, 7_8_2], [2_3_1, 7_7_0, 2_5_6, 7_8_2], [1_4_1, 7_8_8, 2_0_2, 8_0_1], [2_1_5, 7_9_1, 2_7_4, 8_0_4], [1_4_3, 8_2_6, 2_0_4, 8_3_8], [2_1_5, 8_2_6, 2_4_0, 8_3_8], [1_4_2, 8_4_4, 2_0_2, 8_5_7], [2_1_5, 8_4_7, 2_7_4, 8_5_9], [3_3_4, 5_7, 4_2_7, 6_9], [4_4_0, 5_7, 5_2_2, 6_9], [3_6_9, 7_5, 4_6_1, 8_8], [4_6_9, 7_5, 5_1_6, 8_8], [5_2_8, 7_6, 5_6_2, 8_8], [5_7_0, 7_6, 6_6_7, 8_8], [6_7_5, 7_5, 7_1_1, 8_7], [7_2_1, 7_9, 7_7_8, 8_8], [7_8_9, 7_5, 8_4_0, 8_8], [3_6_9, 9_7, 4_7_0, 1_0_7], [4_8_4, 9_4, 5_0_7, 1_0_6], [5_1_8, 9_4, 5_6_2, 1_0_7], [5_7_6, 9_4, 6_5_5, 1_1_0], [6_6_8, 9_4, 7_9_2, 1_0_9], [8_0_4, 9_5, 8_2_9, 1_0_7], [3_6_9, 1_1_3, 4_6_5, 1_2_5], [4_7_7, 1_1_6, 5_4_7, 1_2_5], [5_6_2, 1_1_3, 6_5_8, 1_2_5], [6_7_1, 1_1_6, 7_4_8, 1_2_5], [7_6_1, 1_1_3, 8_1_1, 1_2_5], [3_6_9, 1_3_1, 4_6_5, 1_4_3], [4_7_7, 1_3_3, 5_4_8, 1_4_3], [5_6_3, 1_3_0, 6_9_8, 1_4_5], [7_1_0, 1_3_0, 8_0_2, 1_4_6], [3_3_6, 1_7_1, 4_1_2, 1_8_3], [4_2_3, 1_7_1, 5_7_2, 1_8_3], [5_8_2, 1_7_0, 7_1_6, 1_8_4], [7_2_8, 1_7_1, 8_1_7, 1_8_7], [8_2_9, 1_7_1, 8_4_4, 1_8_6], [3_3_8, 1_9_7, 4_8_2, 2_1_2], [5_0_7, 1_9_6, 5_5_7, 2_0_9], [5_6_9, 1_9_6, 5_9_5, 2_0_8], [6_1_0, 1_9_6, 7_0_2, 2_0_9], [5_0_5, 2_1_4, 5_8_3, 2_2_6], [5_9_5, 2_1_4, 6_5_6, 2_2_7], [6_7_0, 2_1_5, 8_0_7, 2_2_7], [3_3_5, 2_5_9, 5_4_3, 2_7_4], [5_5_6, 2_5_9, 7_0_8, 2_7_2], [3_7_2, 2_7_9, 4_2_2, 2_9_1], [4_3_5, 2_7_9, 4_6_0, 2_9_1], [4_7_4, 2_7_9, 5_7_4, 2_9_2], [5_8_7, 2_7_8, 6_6_4, 2_9_1], [6_7_6, 2_7_8, 7_3_8, 2_9_1], [7_5_1, 2_7_9, 8_3_4, 2_9_1], [3_7_2, 2_9_8, 4_3_4, 3_1_0], [3_3_5, 3_4_1, 4_8_3, 3_5_4], [4_9_7, 3_4_1, 6_5_5, 3_5_4], [6_6_7, 3_4_1, 7_2_8, 3_5_4], [7_4_0, 3_4_1, 8_2_5, 3_5_4], [3_3_5, 3_6_0, 4_3_0, 3_7_2], [4_4_2, 3_6_0, 5_3_4, 3_7_2], [5_4_5, 3_5_9, 6_8_7, 3_7_2], [6_9_7, 3_6_0, 7_5_4, 3_7_2], [7_6_5, 3_6_0, 8_2_3, 3_7_3], [3_3_4, 3_7_8, 4_2_8, 3_9_1], [4_4_0, 3_7_8, 5_7_7, 3_9_4], [5_9_0, 3_7_8, 7_0_5, 3_9_1], [7_2_0, 3_7_8, 8_0_1, 3_9_1], [3_3_4, 3_9_7, 4_0_0, 4_0_9], [3_7_0, 4_1_6, 5_2_9, 4_2_9], [5_4_4, 4_1_6, 5_7_6, 4_3_2], [5_8_7, 4_1_6, 6_6_5, 4_2_8], [6_7_7, 4_1_6, 8_1_4, 4_2_9], [3_7_2, 4_3_5, 4_5_2, 4_5_0], [4_6_5, 4_3_4, 4_9_5, 4_4_7], [5_1_1, 4_3_4, 6_0_0, 4_4_7], [6_1_1, 4_3_6, 6_3_7, 4_4_7], [6_4_9, 4_3_6, 6_9_4, 4_5_1], [7_0_5, 4_3_8, 8_2_4, 4_4_7], [3_6_9, 4_5_3, 4_5_2, 4_6_6], [4_6_4, 4_5_4, 5_0_9, 4_6_6], [5_2_2, 4_5_3, 6_1_1, 4_6_9], [6_2_5, 4_5_3, 7_9_2, 4_6_9], [3_7_0, 4_7_2, 5_5_6, 4_8_8], [5_7_0, 4_7_2, 6_8_4, 4_8_7], [6_9_7, 4_7_2, 7_1_8, 4_8_5], [7_3_2, 4_7_2, 8_3_5, 4_8_8], [3_6_9, 4_9_0, 4_1_1, 5_0_3], [4_2_5, 4_9_0, 4_8_4, 5_0_3], [4_9_6, 4_9_0, 6_3_5, 5_0_6], [6_4_5, 4_9_0, 7_0_7, 5_0_3], [7_1_8, 4_9_1, 7_6_1, 5_0_3], [7_7_1, 4_9_0, 8_4_0, 5_0_3], [3_3_6, 5_1_0, 3_7_4, 5_2_1], [3_8_8, 5_1_0, 4_4_7, 5_2_2], [4_6_0, 5_1_0, 4_8_9, 5_2_1], [5_0_3, 5_1_0, 5_8_0, 5_2_2], [5_9_2, 5_0_9, 7_3_6, 5_2_5], [7_4_5, 5_0_9, 7_7_0, 5_2_2], [7_8_1, 5_0_9, 8_4_0, 5_2_2], [3_3_8, 5_2_8, 4_3_4, 5_4_1], [4_4_8, 5_2_8, 5_9_6, 5_4_1], [6_0_9, 5_2_7, 6_8_7, 5_4_0], [7_0_0, 5_2_8, 7_9_2, 5_4_1], [3_3_6, 5_4_6, 3_9_7, 5_5_9], [4_0_7, 5_4_6, 4_3_1, 5_5_9], [4_4_3, 5_4_6, 5_2_5, 5_6_0], [5_3_7, 5_4_6, 6_8_0, 5_6_2], [6_8_8, 5_4_6, 7_1_4, 5_5_9], [7_2_2, 5_4_6, 8_3_7, 5_6_2], [3_3_6, 5_6_5, 4_4_9, 5_8_1], [4_6_1, 5_6_5, 4_8_5, 5_7_7], [4_9_7, 5_6_5, 6_6_5, 5_8_1], [6_8_1, 5_6_5, 7_1_8, 5_7_7], [7_3_2, 5_6_5, 8_3_7, 5_8_0], [3_3_7, 5_8_4, 4_3_8, 5_9_7], [4_5_2, 5_8_3, 5_2_1, 5_9_6], [5_3_5, 5_8_4, 6_7_7, 5_9_9], [6_9_0, 5_8_3, 7_8_7, 5_9_6], [8_0_1, 5_8_3, 8_2_5, 5_9_6], [3_3_8, 6_0_2, 4_7_8, 6_1_5], [4_9_2, 6_0_2, 5_3_0, 6_1_4], [5_4_3, 6_0_2, 6_3_8, 6_1_5], [6_5_0, 6_0_2, 6_7_6, 6_1_4], [6_8_8, 6_0_2, 7_8_8, 6_1_5], [8_0_2, 6_0_2, 8_4_3, 6_1_4], [3_3_7, 6_2_1, 5_0_2, 6_3_3], [5_1_6, 6_2_1, 6_1_5, 6_3_7], [6_2_9, 6_2_1, 7_7_4, 6_3_6], [7_8_9, 6_2_1, 8_2_7, 6_3_3], [3_3_7, 6_3_9, 4_1_8, 6_5_2], [4_3_2, 6_4_0, 5_7_1, 6_5_3], [5_8_7, 6_3_9, 7_3_1, 6_5_5], [7_4_3, 6_3_9, 7_6_9, 6_5_2], [7_8_0, 6_3_9, 8_4_1, 6_5_2], [3_3_8, 6_5_8, 4_4_0, 6_7_3], [4_5_5, 6_5_8, 4_9_1, 6_7_0], [5_0_8, 6_5_8, 6_0_2, 6_7_1], [6_1_6, 6_5_8, 6_3_8, 6_7_0], [6_5_4, 6_5_8, 8_3_5, 6_7_4], [3_3_7, 6_7_7, 4_2_9, 6_8_9], [3_3_7, 7_1_4, 4_8_2, 7_2_6], [4_9_5, 7_1_4, 5_4_8, 7_2_6], [5_6_1, 7_1_4, 6_8_3, 7_2_6], [3_3_8, 7_7_0, 4_6_1, 7_8_2], [4_7_4, 7_6_9, 5_5_4, 7_8_5], [4_8_9, 7_8_8, 5_6_2, 8_0_3], [5_7_6, 7_8_8, 6_4_3, 8_0_1], [6_5_6, 7_8_7, 7_5_1, 8_0_4], [7_6_4, 7_8_8, 8_4_4, 8_0_1], [3_3_4, 8_2_5, 4_2_1, 8_3_8], [4_3_0, 8_2_4, 5_7_4, 8_3_8], [5_8_4, 8_2_4, 7_2_3, 8_4_1], [3_3_5, 8_4_4, 4_5_0, 8_5_7], [4_6_4, 8_4_3, 5_8_3, 8_6_0], [6_2_8, 8_6_2, 7_5_5, 8_7_5], [7_6_9, 8_6_1, 8_4_8, 8_7_8]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words , UpperCAmelCase__ ) self.assertListEqual(encoding.boxes , UpperCAmelCase__ ) # with apply_OCR = False lowerCAmelCase = LayoutLMvaImageProcessor(apply_ocr=UpperCAmelCase__ ) lowerCAmelCase = image_processing(UpperCAmelCase__ , return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 2_2_4, 2_2_4) )
4
'''simple docstring''' import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __snake_case =logging.get_logger(__name__) __snake_case ={ """vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_config_file""": """tokenizer_config.json""", } __snake_case ={ """vocab_file""": {"""facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json"""}, """merges_file""": {"""facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt"""}, """tokenizer_config_file""": { """facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json""" }, } __snake_case ={"""facebook/blenderbot-3B""": 128} class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : List[Any] = VOCAB_FILES_NAMES lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[Any] = ['''input_ids''', '''attention_mask'''] lowerCamelCase : List[Any] = BlenderbotTokenizer def __init__( self : Union[str, Any] , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : List[Any]=None , UpperCAmelCase__ : str="replace" , UpperCAmelCase__ : Dict="<s>" , UpperCAmelCase__ : Tuple="</s>" , UpperCAmelCase__ : Optional[Any]="</s>" , UpperCAmelCase__ : Any="<s>" , UpperCAmelCase__ : List[str]="<unk>" , UpperCAmelCase__ : int="<pad>" , UpperCAmelCase__ : Union[str, Any]="<mask>" , UpperCAmelCase__ : str=False , UpperCAmelCase__ : Union[str, Any]=True , **UpperCAmelCase__ : Optional[int] , ) -> int: super().__init__( UpperCAmelCase__ , UpperCAmelCase__ , tokenizer_file=UpperCAmelCase__ , errors=UpperCAmelCase__ , bos_token=UpperCAmelCase__ , eos_token=UpperCAmelCase__ , sep_token=UpperCAmelCase__ , cls_token=UpperCAmelCase__ , unk_token=UpperCAmelCase__ , pad_token=UpperCAmelCase__ , mask_token=UpperCAmelCase__ , add_prefix_space=UpperCAmelCase__ , trim_offsets=UpperCAmelCase__ , **UpperCAmelCase__ , ) lowerCAmelCase = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('add_prefix_space' , UpperCAmelCase__ ) != add_prefix_space: lowerCAmelCase = getattr(UpperCAmelCase__ , pre_tok_state.pop('type' ) ) lowerCAmelCase = add_prefix_space lowerCAmelCase = pre_tok_class(**UpperCAmelCase__ ) lowerCAmelCase = add_prefix_space lowerCAmelCase = 'post_processor' lowerCAmelCase = getattr(self.backend_tokenizer , UpperCAmelCase__ , UpperCAmelCase__ ) if tokenizer_component_instance: lowerCAmelCase = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: lowerCAmelCase = tuple(state['sep'] ) if "cls" in state: lowerCAmelCase = tuple(state['cls'] ) lowerCAmelCase = False if state.get('add_prefix_space' , UpperCAmelCase__ ) != add_prefix_space: lowerCAmelCase = add_prefix_space lowerCAmelCase = True if state.get('trim_offsets' , UpperCAmelCase__ ) != trim_offsets: lowerCAmelCase = trim_offsets lowerCAmelCase = True if changes_to_apply: lowerCAmelCase = getattr(UpperCAmelCase__ , state.pop('type' ) ) lowerCAmelCase = component_class(**UpperCAmelCase__ ) setattr(self.backend_tokenizer , UpperCAmelCase__ , UpperCAmelCase__ ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def __UpperCAmelCase ( self : Union[str, Any] ) -> str: if self._mask_token is None: if self.verbose: logger.error('Using mask_token, but it is not set yet.' ) return None return str(self._mask_token ) @mask_token.setter def __UpperCAmelCase ( self : int , UpperCAmelCase__ : Optional[Any] ) -> Tuple: lowerCAmelCase = AddedToken(UpperCAmelCase__ , lstrip=UpperCAmelCase__ , rstrip=UpperCAmelCase__ ) if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) else value lowerCAmelCase = value def __UpperCAmelCase ( self : Optional[Any] , *UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : List[str] ) -> BatchEncoding: lowerCAmelCase = kwargs.get('is_split_into_words' , UpperCAmelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*UpperCAmelCase__ , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : List[str] , *UpperCAmelCase__ : str , **UpperCAmelCase__ : List[str] ) -> BatchEncoding: lowerCAmelCase = kwargs.get('is_split_into_words' , UpperCAmelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*UpperCAmelCase__ , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : str , UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[str] = None ) -> Tuple[str]: lowerCAmelCase = self._tokenizer.model.save(UpperCAmelCase__ , name=UpperCAmelCase__ ) return tuple(UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> Any: return token_ids_a + [self.eos_token_id] def __UpperCAmelCase ( self : Union[str, Any] , UpperCAmelCase__ : "Conversation" ) -> List[int]: lowerCAmelCase = [] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(' ' + text ) else: # Generated responses should contain them already. inputs.append(UpperCAmelCase__ ) lowerCAmelCase = ' '.join(UpperCAmelCase__ ) lowerCAmelCase = self.encode(UpperCAmelCase__ ) if len(UpperCAmelCase__ ) > self.model_max_length: lowerCAmelCase = input_ids[-self.model_max_length :] logger.warning(F'''Trimmed input from conversation as it was longer than {self.model_max_length} tokens.''' ) return input_ids
4
1
'''simple docstring''' from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxSeqaSeqConfigWithPast from ...utils import logging __snake_case =logging.get_logger(__name__) __snake_case ={ """t5-small""": """https://huggingface.co/t5-small/resolve/main/config.json""", """t5-base""": """https://huggingface.co/t5-base/resolve/main/config.json""", """t5-large""": """https://huggingface.co/t5-large/resolve/main/config.json""", """t5-3b""": """https://huggingface.co/t5-3b/resolve/main/config.json""", """t5-11b""": """https://huggingface.co/t5-11b/resolve/main/config.json""", } class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Optional[Any] = '''t5''' lowerCamelCase : Optional[Any] = ['''past_key_values'''] lowerCamelCase : List[str] = {'''hidden_size''': '''d_model''', '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers'''} def __init__( self : str , UpperCAmelCase__ : int=3_2_1_2_8 , UpperCAmelCase__ : Optional[int]=5_1_2 , UpperCAmelCase__ : Tuple=6_4 , UpperCAmelCase__ : Union[str, Any]=2_0_4_8 , UpperCAmelCase__ : str=6 , UpperCAmelCase__ : Union[str, Any]=None , UpperCAmelCase__ : int=8 , UpperCAmelCase__ : Tuple=3_2 , UpperCAmelCase__ : Dict=1_2_8 , UpperCAmelCase__ : List[Any]=0.1 , UpperCAmelCase__ : Optional[Any]=1E-6 , UpperCAmelCase__ : str=1.0 , UpperCAmelCase__ : str="relu" , UpperCAmelCase__ : int=True , UpperCAmelCase__ : Optional[Any]=True , UpperCAmelCase__ : Dict=0 , UpperCAmelCase__ : Tuple=1 , **UpperCAmelCase__ : str , ) -> Optional[int]: lowerCAmelCase = vocab_size lowerCAmelCase = d_model lowerCAmelCase = d_kv lowerCAmelCase = d_ff lowerCAmelCase = num_layers lowerCAmelCase = ( num_decoder_layers if num_decoder_layers is not None else self.num_layers ) # default = symmetry lowerCAmelCase = num_heads lowerCAmelCase = relative_attention_num_buckets lowerCAmelCase = relative_attention_max_distance lowerCAmelCase = dropout_rate lowerCAmelCase = layer_norm_epsilon lowerCAmelCase = initializer_factor lowerCAmelCase = feed_forward_proj lowerCAmelCase = use_cache lowerCAmelCase = self.feed_forward_proj.split('-' ) lowerCAmelCase = act_info[-1] lowerCAmelCase = act_info[0] == 'gated' if len(UpperCAmelCase__ ) > 1 and act_info[0] != "gated" or len(UpperCAmelCase__ ) > 2: raise ValueError( F'''`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.''' 'Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. ' '\'gated-gelu\' or \'relu\'' ) # for backwards compatibility if feed_forward_proj == "gated-gelu": lowerCAmelCase = 'gelu_new' super().__init__( pad_token_id=UpperCAmelCase__ , eos_token_id=UpperCAmelCase__ , is_encoder_decoder=UpperCAmelCase__ , **UpperCAmelCase__ , ) class UpperCAmelCase_ ( __lowercase ): @property def __UpperCAmelCase ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: lowerCAmelCase = { 'input_ids': {0: 'batch', 1: 'encoder_sequence'}, 'attention_mask': {0: 'batch', 1: 'encoder_sequence'}, } if self.use_past: lowerCAmelCase = 'past_encoder_sequence + sequence' lowerCAmelCase = {0: 'batch'} lowerCAmelCase = {0: 'batch', 1: 'past_decoder_sequence + sequence'} else: lowerCAmelCase = {0: 'batch', 1: 'decoder_sequence'} lowerCAmelCase = {0: 'batch', 1: 'decoder_sequence'} if self.use_past: self.fill_with_past_key_values_(UpperCAmelCase__ , direction='inputs' ) return common_inputs @property def __UpperCAmelCase ( self : Optional[Any] ) -> int: return 1_3
4
'''simple docstring''' from __future__ import annotations from statistics import mean def a_ ( lowerCamelCase : list[int] , lowerCamelCase : list[int] , lowerCamelCase : int ): lowerCAmelCase = [0] * no_of_processes lowerCAmelCase = [0] * no_of_processes # Initialize remaining_time to waiting_time. for i in range(lowerCamelCase ): lowerCAmelCase = burst_time[i] lowerCAmelCase = [] lowerCAmelCase = 0 lowerCAmelCase = 0 # When processes are not completed, # A process whose arrival time has passed \ # and has remaining execution time is put into the ready_process. # The shortest process in the ready_process, target_process is executed. while completed != no_of_processes: lowerCAmelCase = [] lowerCAmelCase = -1 for i in range(lowerCamelCase ): if (arrival_time[i] <= total_time) and (remaining_time[i] > 0): ready_process.append(lowerCamelCase ) if len(lowerCamelCase ) > 0: lowerCAmelCase = ready_process[0] for i in ready_process: if remaining_time[i] < remaining_time[target_process]: lowerCAmelCase = i total_time += burst_time[target_process] completed += 1 lowerCAmelCase = 0 lowerCAmelCase = ( total_time - arrival_time[target_process] - burst_time[target_process] ) else: total_time += 1 return waiting_time def a_ ( lowerCamelCase : list[int] , lowerCamelCase : int , lowerCamelCase : list[int] ): lowerCAmelCase = [0] * no_of_processes for i in range(lowerCamelCase ): lowerCAmelCase = burst_time[i] + waiting_time[i] return turn_around_time if __name__ == "__main__": print("""[TEST CASE 01]""") __snake_case =4 __snake_case =[2, 5, 3, 7] __snake_case =[0, 0, 0, 0] __snake_case =calculate_waitingtime(arrival_time, burst_time, no_of_processes) __snake_case =calculate_turnaroundtime( burst_time, no_of_processes, waiting_time ) # Printing the Result print("""PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time""") for i, process_id in enumerate(list(range(1, 5))): print( F'''{process_id}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t''' F'''{waiting_time[i]}\t\t\t\t{turn_around_time[i]}''' ) print(F'''\nAverage waiting time = {mean(waiting_time):.5f}''') print(F'''Average turnaround time = {mean(turn_around_time):.5f}''')
4
1
'''simple docstring''' import copy import tempfile import unittest from huggingface_hub import HfFolder, delete_repo from parameterized import parameterized from requests.exceptions import HTTPError from transformers import AutoConfig, GenerationConfig from transformers.testing_utils import TOKEN, USER, is_staging_test class UpperCAmelCase_ ( unittest.TestCase ): @parameterized.expand([(None,), ('foo.json',)] ) def __UpperCAmelCase ( self : Dict , UpperCAmelCase__ : Optional[Any] ) -> Tuple: lowerCAmelCase = GenerationConfig( do_sample=UpperCAmelCase__ , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(UpperCAmelCase__ , config_name=UpperCAmelCase__ ) lowerCAmelCase = GenerationConfig.from_pretrained(UpperCAmelCase__ , config_name=UpperCAmelCase__ ) # Checks parameters that were specified self.assertEqual(loaded_config.do_sample , UpperCAmelCase__ ) self.assertEqual(loaded_config.temperature , 0.7 ) self.assertEqual(loaded_config.length_penalty , 1.0 ) self.assertEqual(loaded_config.bad_words_ids , [[1, 2, 3], [4, 5]] ) # Checks parameters that were not specified (defaults) self.assertEqual(loaded_config.top_k , 5_0 ) self.assertEqual(loaded_config.max_length , 2_0 ) self.assertEqual(loaded_config.max_time , UpperCAmelCase__ ) def __UpperCAmelCase ( self : Tuple ) -> Union[str, Any]: lowerCAmelCase = AutoConfig.from_pretrained('gpt2' ) lowerCAmelCase = GenerationConfig.from_model_config(UpperCAmelCase__ ) lowerCAmelCase = GenerationConfig() # The generation config has loaded a few non-default parameters from the model config self.assertNotEqual(UpperCAmelCase__ , UpperCAmelCase__ ) # One of those parameters is eos_token_id -- check if it matches self.assertNotEqual(generation_config_from_model.eos_token_id , default_generation_config.eos_token_id ) self.assertEqual(generation_config_from_model.eos_token_id , model_config.eos_token_id ) def __UpperCAmelCase ( self : str ) -> Dict: lowerCAmelCase = GenerationConfig() lowerCAmelCase = { 'max_new_tokens': 1_0_2_4, 'foo': 'bar', } lowerCAmelCase = copy.deepcopy(UpperCAmelCase__ ) lowerCAmelCase = generation_config.update(**UpperCAmelCase__ ) # update_kwargs was not modified (no side effects) self.assertEqual(UpperCAmelCase__ , UpperCAmelCase__ ) # update_kwargs was used to update the config on valid attributes self.assertEqual(generation_config.max_new_tokens , 1_0_2_4 ) # `.update()` returns a dictionary of unused kwargs self.assertEqual(UpperCAmelCase__ , {'foo': 'bar'} ) def __UpperCAmelCase ( self : int ) -> Any: lowerCAmelCase = GenerationConfig() lowerCAmelCase = 'bar' with tempfile.TemporaryDirectory('test-generation-config' ) as tmp_dir: generation_config.save_pretrained(UpperCAmelCase__ ) lowerCAmelCase = GenerationConfig.from_pretrained(UpperCAmelCase__ ) # update_kwargs was used to update the config on valid attributes self.assertEqual(new_config.foo , 'bar' ) lowerCAmelCase = GenerationConfig.from_model_config(UpperCAmelCase__ ) assert not hasattr(UpperCAmelCase__ , 'foo' ) # no new kwargs should be initialized if from config def __UpperCAmelCase ( self : int ) -> int: lowerCAmelCase = GenerationConfig() self.assertEqual(default_config.temperature , 1.0 ) self.assertEqual(default_config.do_sample , UpperCAmelCase__ ) self.assertEqual(default_config.num_beams , 1 ) lowerCAmelCase = GenerationConfig( do_sample=UpperCAmelCase__ , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) self.assertEqual(config.temperature , 0.7 ) self.assertEqual(config.do_sample , UpperCAmelCase__ ) self.assertEqual(config.num_beams , 1 ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(UpperCAmelCase__ ) lowerCAmelCase = GenerationConfig.from_pretrained(UpperCAmelCase__ , temperature=1.0 ) self.assertEqual(loaded_config.temperature , 1.0 ) self.assertEqual(loaded_config.do_sample , UpperCAmelCase__ ) self.assertEqual(loaded_config.num_beams , 1 ) # default value @is_staging_test class UpperCAmelCase_ ( unittest.TestCase ): @classmethod def __UpperCAmelCase ( cls : Dict ) -> List[str]: lowerCAmelCase = TOKEN HfFolder.save_token(UpperCAmelCase__ ) @classmethod def __UpperCAmelCase ( cls : str ) -> str: try: delete_repo(token=cls._token , repo_id='test-generation-config' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='valid_org/test-generation-config-org' ) except HTTPError: pass def __UpperCAmelCase ( self : int ) -> List[Any]: lowerCAmelCase = GenerationConfig( do_sample=UpperCAmelCase__ , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('test-generation-config' , use_auth_token=self._token ) lowerCAmelCase = GenerationConfig.from_pretrained(F'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(UpperCAmelCase__ , getattr(UpperCAmelCase__ , UpperCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id='test-generation-config' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( UpperCAmelCase__ , repo_id='test-generation-config' , push_to_hub=UpperCAmelCase__ , use_auth_token=self._token ) lowerCAmelCase = GenerationConfig.from_pretrained(F'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(UpperCAmelCase__ , getattr(UpperCAmelCase__ , UpperCAmelCase__ ) ) def __UpperCAmelCase ( self : Dict ) -> Optional[int]: lowerCAmelCase = GenerationConfig( do_sample=UpperCAmelCase__ , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('valid_org/test-generation-config-org' , use_auth_token=self._token ) lowerCAmelCase = GenerationConfig.from_pretrained('valid_org/test-generation-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(UpperCAmelCase__ , getattr(UpperCAmelCase__ , UpperCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id='valid_org/test-generation-config-org' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( UpperCAmelCase__ , repo_id='valid_org/test-generation-config-org' , push_to_hub=UpperCAmelCase__ , use_auth_token=self._token ) lowerCAmelCase = GenerationConfig.from_pretrained('valid_org/test-generation-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(UpperCAmelCase__ , getattr(UpperCAmelCase__ , UpperCAmelCase__ ) )
4
'''simple docstring''' import gc import unittest import numpy as np import torch from diffusers import StableDiffusionKDiffusionPipeline from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : Optional[int] ) -> int: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self : Tuple ) -> Any: lowerCAmelCase = StableDiffusionKDiffusionPipeline.from_pretrained('CompVis/stable-diffusion-v1-4' ) lowerCAmelCase = sd_pipe.to(UpperCAmelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) sd_pipe.set_scheduler('sample_euler' ) lowerCAmelCase = 'A painting of a squirrel eating a burger' lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = sd_pipe([prompt] , generator=UpperCAmelCase__ , guidance_scale=9.0 , num_inference_steps=2_0 , output_type='np' ) lowerCAmelCase = output.images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase = np.array([0.0_447, 0.0_492, 0.0_468, 0.0_408, 0.0_383, 0.0_408, 0.0_354, 0.0_380, 0.0_339] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __UpperCAmelCase ( self : List[str] ) -> Dict: lowerCAmelCase = StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowerCAmelCase = sd_pipe.to(UpperCAmelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) sd_pipe.set_scheduler('sample_euler' ) lowerCAmelCase = 'A painting of a squirrel eating a burger' lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = sd_pipe([prompt] , generator=UpperCAmelCase__ , guidance_scale=9.0 , num_inference_steps=2_0 , output_type='np' ) lowerCAmelCase = output.images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase = np.array([0.1_237, 0.1_320, 0.1_438, 0.1_359, 0.1_390, 0.1_132, 0.1_277, 0.1_175, 0.1_112] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-1 def __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: lowerCAmelCase = StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowerCAmelCase = sd_pipe.to(UpperCAmelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) sd_pipe.set_scheduler('sample_dpmpp_2m' ) lowerCAmelCase = 'A painting of a squirrel eating a burger' lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = sd_pipe( [prompt] , generator=UpperCAmelCase__ , guidance_scale=7.5 , num_inference_steps=1_5 , output_type='np' , use_karras_sigmas=UpperCAmelCase__ , ) lowerCAmelCase = output.images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase = np.array( [0.11_381_689, 0.12_112_921, 0.1_389_457, 0.12_549_606, 0.1_244_964, 0.10_831_517, 0.11_562_866, 0.10_867_816, 0.10_499_048] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
4
1
'''simple docstring''' from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_VISION_2_SEQ_MAPPING if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_VISION_2_SEQ_MAPPING __snake_case =logging.get_logger(__name__) @add_end_docstrings(__lowercase ) class UpperCAmelCase_ ( __lowercase ): def __init__( self : Tuple , *UpperCAmelCase__ : int , **UpperCAmelCase__ : Optional[int] ) -> int: super().__init__(*UpperCAmelCase__ , **UpperCAmelCase__ ) requires_backends(self , 'vision' ) self.check_model_type( TF_MODEL_FOR_VISION_2_SEQ_MAPPING if self.framework == 'tf' else MODEL_FOR_VISION_2_SEQ_MAPPING ) def __UpperCAmelCase ( self : Dict , UpperCAmelCase__ : List[str]=None , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : Dict=None ) -> Tuple: lowerCAmelCase = {} lowerCAmelCase = {} if prompt is not None: lowerCAmelCase = prompt if generate_kwargs is not None: lowerCAmelCase = generate_kwargs if max_new_tokens is not None: if "generate_kwargs" not in forward_kwargs: lowerCAmelCase = {} if "max_new_tokens" in forward_kwargs["generate_kwargs"]: raise ValueError( '\'max_new_tokens\' is defined twice, once in \'generate_kwargs\' and once as a direct parameter,' ' please use only one' ) lowerCAmelCase = max_new_tokens return preprocess_params, forward_kwargs, {} def __call__( self : Optional[Any] , UpperCAmelCase__ : Union[str, List[str], "Image.Image", List["Image.Image"]] , **UpperCAmelCase__ : Tuple ) -> Any: return super().__call__(UpperCAmelCase__ , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : int , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Optional[Any]=None ) -> Dict: lowerCAmelCase = load_image(UpperCAmelCase__ ) if prompt is not None: if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise ValueError( F'''Received an invalid text input, got - {type(UpperCAmelCase__ )} - but expected a single string. ''' 'Note also that one single text can be provided for conditional image to text generation.' ) lowerCAmelCase = self.model.config.model_type if model_type == "git": lowerCAmelCase = self.image_processor(images=UpperCAmelCase__ , return_tensors=self.framework ) lowerCAmelCase = self.tokenizer(text=UpperCAmelCase__ , add_special_tokens=UpperCAmelCase__ ).input_ids lowerCAmelCase = [self.tokenizer.cls_token_id] + input_ids lowerCAmelCase = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) model_inputs.update({'input_ids': input_ids} ) elif model_type == "pix2struct": lowerCAmelCase = self.image_processor(images=UpperCAmelCase__ , header_text=UpperCAmelCase__ , return_tensors=self.framework ) elif model_type != "vision-encoder-decoder": # vision-encoder-decoder does not support conditional generation lowerCAmelCase = self.image_processor(images=UpperCAmelCase__ , return_tensors=self.framework ) lowerCAmelCase = self.tokenizer(UpperCAmelCase__ , return_tensors=self.framework ) model_inputs.update(UpperCAmelCase__ ) else: raise ValueError(F'''Model type {model_type} does not support conditional text generation''' ) else: lowerCAmelCase = self.image_processor(images=UpperCAmelCase__ , return_tensors=self.framework ) if self.model.config.model_type == "git" and prompt is None: lowerCAmelCase = None return model_inputs def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : List[Any]=None ) -> str: # Git model sets `model_inputs["input_ids"] = None` in `preprocess` (when `prompt=None`). In batch model, the # pipeline will group them into a list of `None`, which fail `_forward`. Avoid this by checking it first. if ( "input_ids" in model_inputs and isinstance(model_inputs['input_ids'] , UpperCAmelCase__ ) and all(x is None for x in model_inputs['input_ids'] ) ): lowerCAmelCase = None if generate_kwargs is None: lowerCAmelCase = {} # FIXME: We need to pop here due to a difference in how `generation.py` and `generation.tf_utils.py` # parse inputs. In the Tensorflow version, `generate` raises an error if we don't use `input_ids` whereas # the PyTorch version matches it with `self.model.main_input_name` or `self.model.encoder.main_input_name` # in the `_prepare_model_inputs` method. lowerCAmelCase = model_inputs.pop(self.model.main_input_name ) lowerCAmelCase = self.model.generate(UpperCAmelCase__ , **UpperCAmelCase__ , **UpperCAmelCase__ ) return model_outputs def __UpperCAmelCase ( self : Union[str, Any] , UpperCAmelCase__ : Tuple ) -> Optional[int]: lowerCAmelCase = [] for output_ids in model_outputs: lowerCAmelCase = { 'generated_text': self.tokenizer.decode( UpperCAmelCase__ , skip_special_tokens=UpperCAmelCase__ , ) } records.append(UpperCAmelCase__ ) return records
4
'''simple docstring''' # Copyright (c) 2021-, NVIDIA CORPORATION. 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. #################################################################################################### # # Note: If when running this conversion script you're getting an exception: # ModuleNotFoundError: No module named 'megatron.model.enums' # you need to tell python where to find the clone of Megatron-LM, e.g.: # # cd /tmp # git clone https://github.com/NVIDIA/Megatron-LM # PYTHONPATH=/tmp/Megatron-LM python src/transformers/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py ... # # if you already have it cloned elsewhere, simply adjust the path to the existing path # # If the training was done using a Megatron-LM fork, e.g., # https://github.com/microsoft/Megatron-DeepSpeed/ then chances are that you need to have that one # in your path, i.e., /path/to/Megatron-DeepSpeed/ # import argparse import os import re import zipfile import torch from transformers import AutoTokenizer, GPTaConfig def a_ ( lowerCamelCase : int , lowerCamelCase : Optional[Any] , lowerCamelCase : Union[str, Any]=0 ): # Format the message. if name is None: lowerCAmelCase = None else: lowerCAmelCase = '.' * max(0 , spaces - 2 ) + '# {:' + str(50 - spaces ) + 's}' lowerCAmelCase = fmt.format(lowerCamelCase ) # Print and recurse (if needed). if isinstance(lowerCamelCase , lowerCamelCase ): if msg is not None: print(lowerCamelCase ) for k in val.keys(): recursive_print(lowerCamelCase , val[k] , spaces + 2 ) elif isinstance(lowerCamelCase , torch.Tensor ): print(lowerCamelCase , ':' , val.size() ) else: print(lowerCamelCase , ':' , lowerCamelCase ) def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : List[str] , lowerCamelCase : List[Any] , lowerCamelCase : Dict , lowerCamelCase : Tuple ): # Permutes layout of param tensor to [num_splits * num_heads * hidden_size, :] # for compatibility with later versions of NVIDIA Megatron-LM. # The inverse operation is performed inside Megatron-LM to read checkpoints: # https://github.com/NVIDIA/Megatron-LM/blob/v2.4/megatron/checkpointing.py#L209 # If param is the weight tensor of the self-attention block, the returned tensor # will have to be transposed one more time to be read by HuggingFace GPT2. lowerCAmelCase = param.size() if checkpoint_version == 1.0: # version 1.0 stores [num_heads * hidden_size * num_splits, :] lowerCAmelCase = (num_heads, hidden_size, num_splits) + input_shape[1:] lowerCAmelCase = param.view(*lowerCamelCase ) lowerCAmelCase = param.transpose(0 , 2 ) lowerCAmelCase = param.transpose(1 , 2 ).contiguous() elif checkpoint_version >= 2.0: # other versions store [num_heads * num_splits * hidden_size, :] lowerCAmelCase = (num_heads, num_splits, hidden_size) + input_shape[1:] lowerCAmelCase = param.view(*lowerCamelCase ) lowerCAmelCase = param.transpose(0 , 1 ).contiguous() lowerCAmelCase = param.view(*lowerCamelCase ) return param def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Optional[int] , lowerCamelCase : str ): # The converted output model. lowerCAmelCase = {} # old versions did not store training args lowerCAmelCase = input_state_dict.get('args' , lowerCamelCase ) if ds_args is not None: # do not make the user write a config file when the exact dimensions/sizes are already in the checkpoint # from pprint import pprint # pprint(vars(ds_args)) lowerCAmelCase = ds_args.padded_vocab_size lowerCAmelCase = ds_args.max_position_embeddings lowerCAmelCase = ds_args.hidden_size lowerCAmelCase = ds_args.num_layers lowerCAmelCase = ds_args.num_attention_heads lowerCAmelCase = ds_args.ffn_hidden_size # pprint(config) # The number of heads. lowerCAmelCase = config.n_head # The hidden_size per head. lowerCAmelCase = config.n_embd // config.n_head # Megatron-LM checkpoint version if "checkpoint_version" in input_state_dict.keys(): lowerCAmelCase = input_state_dict['checkpoint_version'] else: lowerCAmelCase = 0.0 # The model. lowerCAmelCase = input_state_dict['model'] # The language model. lowerCAmelCase = model['language_model'] # The embeddings. lowerCAmelCase = lm['embedding'] # The word embeddings. lowerCAmelCase = embeddings['word_embeddings']['weight'] # Truncate the embedding table to vocab_size rows. lowerCAmelCase = word_embeddings[: config.vocab_size, :] lowerCAmelCase = word_embeddings # The position embeddings. lowerCAmelCase = embeddings['position_embeddings']['weight'] # Read the causal mask dimension (seqlen). [max_sequence_length, hidden_size] lowerCAmelCase = pos_embeddings.size(0 ) if n_positions != config.n_positions: raise ValueError( f'''pos_embeddings.max_sequence_length={n_positions} and config.n_positions={config.n_positions} don\'t match''' ) # Store the position embeddings. lowerCAmelCase = pos_embeddings # The transformer. lowerCAmelCase = lm['transformer'] if 'transformer' in lm.keys() else lm['encoder'] # The regex to extract layer names. lowerCAmelCase = re.compile(R'layers\.(\d+)\.([a-z0-9_.]+)\.([a-z]+)' ) # The simple map of names for "automated" rules. lowerCAmelCase = { 'attention.dense': '.attn.c_proj.', 'self_attention.dense': '.attn.c_proj.', 'mlp.dense_h_to_4h': '.mlp.c_fc.', 'mlp.dense_4h_to_h': '.mlp.c_proj.', } # Extract the layers. for key, val in transformer.items(): # Match the name. lowerCAmelCase = layer_re.match(lowerCamelCase ) # Stop if that's not a layer if m is None: break # The index of the layer. lowerCAmelCase = int(m.group(1 ) ) # The name of the operation. lowerCAmelCase = m.group(2 ) # Is it a weight or a bias? lowerCAmelCase = m.group(3 ) # The name of the layer. lowerCAmelCase = f'''transformer.h.{layer_idx}''' # For layernorm(s), simply store the layer norm. if op_name.endswith('layernorm' ): lowerCAmelCase = 'ln_1' if op_name.startswith('input' ) else 'ln_2' lowerCAmelCase = val # Transpose the QKV matrix. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "weight": # Insert a tensor of 1x1xDxD bias. lowerCAmelCase = torch.tril(torch.ones((n_positions, n_positions) , dtype=torch.floataa ) ).view( 1 , 1 , lowerCamelCase , lowerCamelCase ) lowerCAmelCase = causal_mask # Insert a "dummy" tensor for masked_bias. lowerCAmelCase = torch.tensor(-1e4 , dtype=torch.floataa ) lowerCAmelCase = masked_bias lowerCAmelCase = fix_query_key_value_ordering(lowerCamelCase , lowerCamelCase , 3 , lowerCamelCase , lowerCamelCase ) # Megatron stores (3*D) x D but transformers-GPT2 expects D x 3*D. lowerCAmelCase = out_val.transpose(0 , 1 ).contiguous() # Store. lowerCAmelCase = out_val # Transpose the bias. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "bias": lowerCAmelCase = fix_query_key_value_ordering(lowerCamelCase , lowerCamelCase , 3 , lowerCamelCase , lowerCamelCase ) # Store. No change of shape. lowerCAmelCase = out_val # Transpose the weights. elif weight_or_bias == "weight": lowerCAmelCase = megatron_to_transformers[op_name] lowerCAmelCase = val.transpose(0 , 1 ) # Copy the bias. elif weight_or_bias == "bias": lowerCAmelCase = megatron_to_transformers[op_name] lowerCAmelCase = val # DEBUG. assert config.n_layer == layer_idx + 1 # The final layernorm. lowerCAmelCase = transformer['final_layernorm.weight'] lowerCAmelCase = transformer['final_layernorm.bias'] # For LM head, transformers' wants the matrix to weight embeddings. lowerCAmelCase = word_embeddings # It should be done! return output_state_dict def a_ ( ): # Create the argument parser. lowerCAmelCase = argparse.ArgumentParser() parser.add_argument('--print-checkpoint-structure' , action='store_true' ) parser.add_argument( 'path_to_checkpoint' , type=lowerCamelCase , help='Path to the checkpoint file (.zip archive or direct .pt file)' , ) parser.add_argument( '--config_file' , default='' , type=lowerCamelCase , help='An optional config json file describing the pre-trained model.' , ) lowerCAmelCase = parser.parse_args() # Extract the basename. lowerCAmelCase = os.path.dirname(args.path_to_checkpoint ) # Load the model. # the .zip is very optional, let's keep it for backward compatibility print(f'''Extracting PyTorch state dictionary from {args.path_to_checkpoint}''' ) if args.path_to_checkpoint.endswith('.zip' ): with zipfile.ZipFile(args.path_to_checkpoint , 'r' ) as checkpoint: with checkpoint.open('release/mp_rank_00/model_optim_rng.pt' ) as pytorch_dict: lowerCAmelCase = torch.load(lowerCamelCase , map_location='cpu' ) else: lowerCAmelCase = torch.load(args.path_to_checkpoint , map_location='cpu' ) lowerCAmelCase = input_state_dict.get('args' , lowerCamelCase ) # Read the config, or default to the model released by NVIDIA. if args.config_file == "": if ds_args is not None: if ds_args.bias_gelu_fusion: lowerCAmelCase = 'gelu_fast' elif ds_args.openai_gelu: lowerCAmelCase = 'gelu_new' else: lowerCAmelCase = 'gelu' else: # in the very early days this used to be "gelu_new" lowerCAmelCase = 'gelu_new' # Spell out all parameters in case the defaults change. lowerCAmelCase = GPTaConfig( vocab_size=50257 , n_positions=1024 , n_embd=1024 , n_layer=24 , n_head=16 , n_inner=4096 , activation_function=lowerCamelCase , resid_pdrop=0.1 , embd_pdrop=0.1 , attn_pdrop=0.1 , layer_norm_epsilon=1e-5 , initializer_range=0.02 , summary_type='cls_index' , summary_use_proj=lowerCamelCase , summary_activation=lowerCamelCase , summary_proj_to_labels=lowerCamelCase , summary_first_dropout=0.1 , scale_attn_weights=lowerCamelCase , use_cache=lowerCamelCase , bos_token_id=50256 , eos_token_id=50256 , ) else: lowerCAmelCase = GPTaConfig.from_json_file(args.config_file ) lowerCAmelCase = ['GPT2LMHeadModel'] # Convert. print('Converting' ) lowerCAmelCase = convert_megatron_checkpoint(lowerCamelCase , lowerCamelCase , lowerCamelCase ) # Print the structure of converted state dict. if args.print_checkpoint_structure: recursive_print(lowerCamelCase , lowerCamelCase ) # Add tokenizer class info to config # see https://github.com/huggingface/transformers/issues/13906) if ds_args is not None: lowerCAmelCase = ds_args.tokenizer_type if tokenizer_type == "GPT2BPETokenizer": lowerCAmelCase = 'gpt2' elif tokenizer_type == "PretrainedFromHF": lowerCAmelCase = ds_args.tokenizer_name_or_path else: raise ValueError(f'''Unrecognized tokenizer_type {tokenizer_type}''' ) else: lowerCAmelCase = 'gpt2' lowerCAmelCase = AutoTokenizer.from_pretrained(lowerCamelCase ) lowerCAmelCase = type(lowerCamelCase ).__name__ lowerCAmelCase = tokenizer_class # Store the config to file. print('Saving config' ) config.save_pretrained(lowerCamelCase ) # Save tokenizer based on args print(f'''Adding {tokenizer_class} tokenizer files''' ) tokenizer.save_pretrained(lowerCamelCase ) # Store the state_dict to file. lowerCAmelCase = os.path.join(lowerCamelCase , 'pytorch_model.bin' ) print(f'''Saving checkpoint to "{output_checkpoint_file}"''' ) torch.save(lowerCamelCase , lowerCamelCase ) #################################################################################################### if __name__ == "__main__": main() ####################################################################################################
4
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() __snake_case =logging.get_logger("""transformers.models.encodec""") __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_DECODER, } __snake_case ={ **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_ENCODER_48K, **MAPPING_DECODER, **MAPPING_DECODER_48K, } __snake_case =[] __snake_case =[] def a_ ( lowerCamelCase : Optional[Any] , lowerCamelCase : Union[str, Any] , lowerCamelCase : int , lowerCamelCase : Any , lowerCamelCase : List[str] ): for attribute in key.split('.' ): lowerCAmelCase = getattr(lowerCamelCase , lowerCamelCase ) if weight_type is not None: lowerCAmelCase = getattr(lowerCamelCase , lowerCamelCase ).shape else: lowerCAmelCase = 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": lowerCAmelCase = value elif weight_type == "weight_g": lowerCAmelCase = value elif weight_type == "weight_v": lowerCAmelCase = value elif weight_type == "bias": lowerCAmelCase = value elif weight_type == "running_mean": lowerCAmelCase = value elif weight_type == "running_var": lowerCAmelCase = value elif weight_type == "num_batches_tracked": lowerCAmelCase = value elif weight_type == "weight_ih_l0": lowerCAmelCase = value elif weight_type == "weight_hh_l0": lowerCAmelCase = value elif weight_type == "bias_ih_l0": lowerCAmelCase = value elif weight_type == "bias_hh_l0": lowerCAmelCase = value elif weight_type == "weight_ih_l1": lowerCAmelCase = value elif weight_type == "weight_hh_l1": lowerCAmelCase = value elif weight_type == "bias_ih_l1": lowerCAmelCase = value elif weight_type == "bias_hh_l1": lowerCAmelCase = value else: lowerCAmelCase = value logger.info(f'''{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.''' ) def a_ ( lowerCamelCase : Optional[Any] , lowerCamelCase : Optional[Any] ): for key in ignore_keys: if key.endswith('.*' ): if name.startswith(key[:-1] ): return True elif ".*." in key: lowerCAmelCase , lowerCAmelCase = key.split('.*.' ) if prefix in name and suffix in name: return True elif key in name: return True return False def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Any , lowerCamelCase : str ): lowerCAmelCase = [] if model_name == "encodec_24khz" or "encodec_32khz": lowerCAmelCase = MAPPING_24K elif model_name == "encodec_48khz": lowerCAmelCase = 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 lowerCAmelCase = False for key, mapped_key in MAPPING.items(): if "*" in key: lowerCAmelCase , lowerCAmelCase = key.split('.*.' ) if prefix in name and suffix in name: lowerCAmelCase = suffix if key in name: # HACK otherwise .embed gets initialized with .embed_avg too if key.endswith('embed' ) and name.endswith('embed_avg' ): continue lowerCAmelCase = True if "*" in mapped_key: lowerCAmelCase = name.split(lowerCamelCase )[0].split('.' )[-2] lowerCAmelCase = mapped_key.replace('*' , lowerCamelCase ) if "weight_g" in name: lowerCAmelCase = 'weight_g' elif "weight_v" in name: lowerCAmelCase = 'weight_v' elif "weight_ih_l0" in name: lowerCAmelCase = 'weight_ih_l0' elif "weight_hh_l0" in name: lowerCAmelCase = 'weight_hh_l0' elif "bias_ih_l0" in name: lowerCAmelCase = 'bias_ih_l0' elif "bias_hh_l0" in name: lowerCAmelCase = 'bias_hh_l0' elif "weight_ih_l1" in name: lowerCAmelCase = 'weight_ih_l1' elif "weight_hh_l1" in name: lowerCAmelCase = 'weight_hh_l1' elif "bias_ih_l1" in name: lowerCAmelCase = 'bias_ih_l1' elif "bias_hh_l1" in name: lowerCAmelCase = 'bias_hh_l1' elif "bias" in name: lowerCAmelCase = 'bias' elif "weight" in name: lowerCAmelCase = 'weight' elif "running_mean" in name: lowerCAmelCase = 'running_mean' elif "running_var" in name: lowerCAmelCase = 'running_var' elif "num_batches_tracked" in name: lowerCAmelCase = 'num_batches_tracked' else: lowerCAmelCase = 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 a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Union[str, Any] , lowerCamelCase : str , lowerCamelCase : Dict=None , lowerCamelCase : Union[str, Any]=None , ): if config_path is not None: lowerCAmelCase = EncodecConfig.from_pretrained(lowerCamelCase ) else: lowerCAmelCase = EncodecConfig() if model_name == "encodec_24khz": pass # config is already correct elif model_name == "encodec_32khz": lowerCAmelCase = [8, 5, 4, 4] lowerCAmelCase = [2.2] lowerCAmelCase = 64 lowerCAmelCase = 32000 lowerCAmelCase = 2048 lowerCAmelCase = False lowerCAmelCase = False lowerCAmelCase = False elif model_name == "encodec_48khz": lowerCAmelCase = [8, 5, 4, 2] lowerCAmelCase = [3.0, 6.0, 12.0, 24.0] lowerCAmelCase = 48000 lowerCAmelCase = 2 lowerCAmelCase = False lowerCAmelCase = 'time_group_norm' lowerCAmelCase = True lowerCAmelCase = 1.0 lowerCAmelCase = 0.01 else: raise ValueError(f'''Unknown model name: {model_name}''' ) lowerCAmelCase = EncodecModel(lowerCamelCase ) lowerCAmelCase = 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 ) lowerCAmelCase = 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 lowerCAmelCase = 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__": __snake_case =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.""" ) __snake_case =parser.parse_args() convert_checkpoint( args.model, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
4
'''simple docstring''' from __future__ import annotations from typing import Any class UpperCAmelCase_ : def __init__( self : Optional[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : float = 0 ) -> None: lowerCAmelCase , lowerCAmelCase = row, column lowerCAmelCase = [[default_value for c in range(UpperCAmelCase__ )] for r in range(UpperCAmelCase__ )] def __str__( self : List[str] ) -> str: lowerCAmelCase = F'''Matrix consist of {self.row} rows and {self.column} columns\n''' # Make string identifier lowerCAmelCase = 0 for row_vector in self.array: for obj in row_vector: lowerCAmelCase = max(UpperCAmelCase__ , len(str(UpperCAmelCase__ ) ) ) lowerCAmelCase = F'''%{max_element_length}s''' # Make string and return def single_line(UpperCAmelCase__ : list[float] ) -> str: nonlocal string_format_identifier lowerCAmelCase = '[' line += ", ".join(string_format_identifier % (obj,) for obj in row_vector ) line += "]" return line s += "\n".join(single_line(UpperCAmelCase__ ) for row_vector in self.array ) return s def __repr__( self : List[str] ) -> str: return str(self ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : tuple[int, int] ) -> bool: if not (isinstance(UpperCAmelCase__ , (list, tuple) ) and len(UpperCAmelCase__ ) == 2): return False elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False else: return True def __getitem__( self : Any , UpperCAmelCase__ : tuple[int, int] ) -> Any: assert self.validate_indicies(UpperCAmelCase__ ) return self.array[loc[0]][loc[1]] def __setitem__( self : Dict , UpperCAmelCase__ : tuple[int, int] , UpperCAmelCase__ : float ) -> None: assert self.validate_indicies(UpperCAmelCase__ ) lowerCAmelCase = value def __add__( self : Any , UpperCAmelCase__ : Matrix ) -> Matrix: assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) assert self.row == another.row and self.column == another.column # Add lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] + another[r, c] return result def __neg__( self : int ) -> Matrix: lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = -self[r, c] return result def __sub__( self : str , UpperCAmelCase__ : Matrix ) -> Matrix: return self + (-another) def __mul__( self : str , UpperCAmelCase__ : int | float | Matrix ) -> Matrix: if isinstance(UpperCAmelCase__ , (int, float) ): # Scalar multiplication lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] * another return result elif isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): # Matrix multiplication assert self.column == another.row lowerCAmelCase = Matrix(self.row , another.column ) for r in range(self.row ): for c in range(another.column ): for i in range(self.column ): result[r, c] += self[r, i] * another[i, c] return result else: lowerCAmelCase = F'''Unsupported type given for another ({type(UpperCAmelCase__ )})''' raise TypeError(UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[Any] ) -> Matrix: lowerCAmelCase = Matrix(self.column , self.row ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] return result def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : Matrix , UpperCAmelCase__ : Matrix ) -> Any: assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) and isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) assert self.row == self.column == u.row == v.row # u, v should be column vector assert u.column == v.column == 1 # u, v should be column vector # Calculate lowerCAmelCase = v.transpose() lowerCAmelCase = (v_t * self * u)[0, 0] + 1 if numerator_factor == 0: return None # It's not invertable return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor)) # Testing if __name__ == "__main__": def a_ ( ): # a^(-1) lowerCAmelCase = Matrix(3 , 3 , 0 ) for i in range(3 ): lowerCAmelCase = 1 print(f'''a^(-1) is {ainv}''' ) # u, v lowerCAmelCase = Matrix(3 , 1 , 0 ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = 1, 2, -3 lowerCAmelCase = Matrix(3 , 1 , 0 ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = 4, -2, 5 print(f'''u is {u}''' ) print(f'''v is {v}''' ) print(f'''uv^T is {u * v.transpose()}''' ) # Sherman Morrison print(f'''(a + uv^T)^(-1) is {ainv.sherman_morrison(lowerCamelCase , lowerCamelCase )}''' ) def a_ ( ): import doctest doctest.testmod() testa()
4
1
'''simple docstring''' # Copyright (c) 2021-, NVIDIA CORPORATION. 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. #################################################################################################### # # Note: If when running this conversion script you're getting an exception: # ModuleNotFoundError: No module named 'megatron.model.enums' # you need to tell python where to find the clone of Megatron-LM, e.g.: # # cd /tmp # git clone https://github.com/NVIDIA/Megatron-LM # PYTHONPATH=/tmp/Megatron-LM python src/transformers/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py ... # # if you already have it cloned elsewhere, simply adjust the path to the existing path # # If the training was done using a Megatron-LM fork, e.g., # https://github.com/microsoft/Megatron-DeepSpeed/ then chances are that you need to have that one # in your path, i.e., /path/to/Megatron-DeepSpeed/ # import argparse import os import re import zipfile import torch from transformers import AutoTokenizer, GPTaConfig def a_ ( lowerCamelCase : int , lowerCamelCase : Optional[Any] , lowerCamelCase : Union[str, Any]=0 ): # Format the message. if name is None: lowerCAmelCase = None else: lowerCAmelCase = '.' * max(0 , spaces - 2 ) + '# {:' + str(50 - spaces ) + 's}' lowerCAmelCase = fmt.format(lowerCamelCase ) # Print and recurse (if needed). if isinstance(lowerCamelCase , lowerCamelCase ): if msg is not None: print(lowerCamelCase ) for k in val.keys(): recursive_print(lowerCamelCase , val[k] , spaces + 2 ) elif isinstance(lowerCamelCase , torch.Tensor ): print(lowerCamelCase , ':' , val.size() ) else: print(lowerCamelCase , ':' , lowerCamelCase ) def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : List[str] , lowerCamelCase : List[Any] , lowerCamelCase : Dict , lowerCamelCase : Tuple ): # Permutes layout of param tensor to [num_splits * num_heads * hidden_size, :] # for compatibility with later versions of NVIDIA Megatron-LM. # The inverse operation is performed inside Megatron-LM to read checkpoints: # https://github.com/NVIDIA/Megatron-LM/blob/v2.4/megatron/checkpointing.py#L209 # If param is the weight tensor of the self-attention block, the returned tensor # will have to be transposed one more time to be read by HuggingFace GPT2. lowerCAmelCase = param.size() if checkpoint_version == 1.0: # version 1.0 stores [num_heads * hidden_size * num_splits, :] lowerCAmelCase = (num_heads, hidden_size, num_splits) + input_shape[1:] lowerCAmelCase = param.view(*lowerCamelCase ) lowerCAmelCase = param.transpose(0 , 2 ) lowerCAmelCase = param.transpose(1 , 2 ).contiguous() elif checkpoint_version >= 2.0: # other versions store [num_heads * num_splits * hidden_size, :] lowerCAmelCase = (num_heads, num_splits, hidden_size) + input_shape[1:] lowerCAmelCase = param.view(*lowerCamelCase ) lowerCAmelCase = param.transpose(0 , 1 ).contiguous() lowerCAmelCase = param.view(*lowerCamelCase ) return param def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Optional[int] , lowerCamelCase : str ): # The converted output model. lowerCAmelCase = {} # old versions did not store training args lowerCAmelCase = input_state_dict.get('args' , lowerCamelCase ) if ds_args is not None: # do not make the user write a config file when the exact dimensions/sizes are already in the checkpoint # from pprint import pprint # pprint(vars(ds_args)) lowerCAmelCase = ds_args.padded_vocab_size lowerCAmelCase = ds_args.max_position_embeddings lowerCAmelCase = ds_args.hidden_size lowerCAmelCase = ds_args.num_layers lowerCAmelCase = ds_args.num_attention_heads lowerCAmelCase = ds_args.ffn_hidden_size # pprint(config) # The number of heads. lowerCAmelCase = config.n_head # The hidden_size per head. lowerCAmelCase = config.n_embd // config.n_head # Megatron-LM checkpoint version if "checkpoint_version" in input_state_dict.keys(): lowerCAmelCase = input_state_dict['checkpoint_version'] else: lowerCAmelCase = 0.0 # The model. lowerCAmelCase = input_state_dict['model'] # The language model. lowerCAmelCase = model['language_model'] # The embeddings. lowerCAmelCase = lm['embedding'] # The word embeddings. lowerCAmelCase = embeddings['word_embeddings']['weight'] # Truncate the embedding table to vocab_size rows. lowerCAmelCase = word_embeddings[: config.vocab_size, :] lowerCAmelCase = word_embeddings # The position embeddings. lowerCAmelCase = embeddings['position_embeddings']['weight'] # Read the causal mask dimension (seqlen). [max_sequence_length, hidden_size] lowerCAmelCase = pos_embeddings.size(0 ) if n_positions != config.n_positions: raise ValueError( f'''pos_embeddings.max_sequence_length={n_positions} and config.n_positions={config.n_positions} don\'t match''' ) # Store the position embeddings. lowerCAmelCase = pos_embeddings # The transformer. lowerCAmelCase = lm['transformer'] if 'transformer' in lm.keys() else lm['encoder'] # The regex to extract layer names. lowerCAmelCase = re.compile(R'layers\.(\d+)\.([a-z0-9_.]+)\.([a-z]+)' ) # The simple map of names for "automated" rules. lowerCAmelCase = { 'attention.dense': '.attn.c_proj.', 'self_attention.dense': '.attn.c_proj.', 'mlp.dense_h_to_4h': '.mlp.c_fc.', 'mlp.dense_4h_to_h': '.mlp.c_proj.', } # Extract the layers. for key, val in transformer.items(): # Match the name. lowerCAmelCase = layer_re.match(lowerCamelCase ) # Stop if that's not a layer if m is None: break # The index of the layer. lowerCAmelCase = int(m.group(1 ) ) # The name of the operation. lowerCAmelCase = m.group(2 ) # Is it a weight or a bias? lowerCAmelCase = m.group(3 ) # The name of the layer. lowerCAmelCase = f'''transformer.h.{layer_idx}''' # For layernorm(s), simply store the layer norm. if op_name.endswith('layernorm' ): lowerCAmelCase = 'ln_1' if op_name.startswith('input' ) else 'ln_2' lowerCAmelCase = val # Transpose the QKV matrix. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "weight": # Insert a tensor of 1x1xDxD bias. lowerCAmelCase = torch.tril(torch.ones((n_positions, n_positions) , dtype=torch.floataa ) ).view( 1 , 1 , lowerCamelCase , lowerCamelCase ) lowerCAmelCase = causal_mask # Insert a "dummy" tensor for masked_bias. lowerCAmelCase = torch.tensor(-1e4 , dtype=torch.floataa ) lowerCAmelCase = masked_bias lowerCAmelCase = fix_query_key_value_ordering(lowerCamelCase , lowerCamelCase , 3 , lowerCamelCase , lowerCamelCase ) # Megatron stores (3*D) x D but transformers-GPT2 expects D x 3*D. lowerCAmelCase = out_val.transpose(0 , 1 ).contiguous() # Store. lowerCAmelCase = out_val # Transpose the bias. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "bias": lowerCAmelCase = fix_query_key_value_ordering(lowerCamelCase , lowerCamelCase , 3 , lowerCamelCase , lowerCamelCase ) # Store. No change of shape. lowerCAmelCase = out_val # Transpose the weights. elif weight_or_bias == "weight": lowerCAmelCase = megatron_to_transformers[op_name] lowerCAmelCase = val.transpose(0 , 1 ) # Copy the bias. elif weight_or_bias == "bias": lowerCAmelCase = megatron_to_transformers[op_name] lowerCAmelCase = val # DEBUG. assert config.n_layer == layer_idx + 1 # The final layernorm. lowerCAmelCase = transformer['final_layernorm.weight'] lowerCAmelCase = transformer['final_layernorm.bias'] # For LM head, transformers' wants the matrix to weight embeddings. lowerCAmelCase = word_embeddings # It should be done! return output_state_dict def a_ ( ): # Create the argument parser. lowerCAmelCase = argparse.ArgumentParser() parser.add_argument('--print-checkpoint-structure' , action='store_true' ) parser.add_argument( 'path_to_checkpoint' , type=lowerCamelCase , help='Path to the checkpoint file (.zip archive or direct .pt file)' , ) parser.add_argument( '--config_file' , default='' , type=lowerCamelCase , help='An optional config json file describing the pre-trained model.' , ) lowerCAmelCase = parser.parse_args() # Extract the basename. lowerCAmelCase = os.path.dirname(args.path_to_checkpoint ) # Load the model. # the .zip is very optional, let's keep it for backward compatibility print(f'''Extracting PyTorch state dictionary from {args.path_to_checkpoint}''' ) if args.path_to_checkpoint.endswith('.zip' ): with zipfile.ZipFile(args.path_to_checkpoint , 'r' ) as checkpoint: with checkpoint.open('release/mp_rank_00/model_optim_rng.pt' ) as pytorch_dict: lowerCAmelCase = torch.load(lowerCamelCase , map_location='cpu' ) else: lowerCAmelCase = torch.load(args.path_to_checkpoint , map_location='cpu' ) lowerCAmelCase = input_state_dict.get('args' , lowerCamelCase ) # Read the config, or default to the model released by NVIDIA. if args.config_file == "": if ds_args is not None: if ds_args.bias_gelu_fusion: lowerCAmelCase = 'gelu_fast' elif ds_args.openai_gelu: lowerCAmelCase = 'gelu_new' else: lowerCAmelCase = 'gelu' else: # in the very early days this used to be "gelu_new" lowerCAmelCase = 'gelu_new' # Spell out all parameters in case the defaults change. lowerCAmelCase = GPTaConfig( vocab_size=50257 , n_positions=1024 , n_embd=1024 , n_layer=24 , n_head=16 , n_inner=4096 , activation_function=lowerCamelCase , resid_pdrop=0.1 , embd_pdrop=0.1 , attn_pdrop=0.1 , layer_norm_epsilon=1e-5 , initializer_range=0.02 , summary_type='cls_index' , summary_use_proj=lowerCamelCase , summary_activation=lowerCamelCase , summary_proj_to_labels=lowerCamelCase , summary_first_dropout=0.1 , scale_attn_weights=lowerCamelCase , use_cache=lowerCamelCase , bos_token_id=50256 , eos_token_id=50256 , ) else: lowerCAmelCase = GPTaConfig.from_json_file(args.config_file ) lowerCAmelCase = ['GPT2LMHeadModel'] # Convert. print('Converting' ) lowerCAmelCase = convert_megatron_checkpoint(lowerCamelCase , lowerCamelCase , lowerCamelCase ) # Print the structure of converted state dict. if args.print_checkpoint_structure: recursive_print(lowerCamelCase , lowerCamelCase ) # Add tokenizer class info to config # see https://github.com/huggingface/transformers/issues/13906) if ds_args is not None: lowerCAmelCase = ds_args.tokenizer_type if tokenizer_type == "GPT2BPETokenizer": lowerCAmelCase = 'gpt2' elif tokenizer_type == "PretrainedFromHF": lowerCAmelCase = ds_args.tokenizer_name_or_path else: raise ValueError(f'''Unrecognized tokenizer_type {tokenizer_type}''' ) else: lowerCAmelCase = 'gpt2' lowerCAmelCase = AutoTokenizer.from_pretrained(lowerCamelCase ) lowerCAmelCase = type(lowerCamelCase ).__name__ lowerCAmelCase = tokenizer_class # Store the config to file. print('Saving config' ) config.save_pretrained(lowerCamelCase ) # Save tokenizer based on args print(f'''Adding {tokenizer_class} tokenizer files''' ) tokenizer.save_pretrained(lowerCamelCase ) # Store the state_dict to file. lowerCAmelCase = os.path.join(lowerCamelCase , 'pytorch_model.bin' ) print(f'''Saving checkpoint to "{output_checkpoint_file}"''' ) torch.save(lowerCamelCase , lowerCamelCase ) #################################################################################################### if __name__ == "__main__": main() ####################################################################################################
4
'''simple docstring''' class UpperCAmelCase_ : def __init__( self : List[str] , UpperCAmelCase__ : list[int] ) -> None: lowerCAmelCase = len(UpperCAmelCase__ ) lowerCAmelCase = [0] * len_array if len_array > 0: lowerCAmelCase = array[0] for i in range(1 , UpperCAmelCase__ ): lowerCAmelCase = self.prefix_sum[i - 1] + array[i] def __UpperCAmelCase ( self : Optional[Any] , UpperCAmelCase__ : int , UpperCAmelCase__ : int ) -> int: if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def __UpperCAmelCase ( self : int , UpperCAmelCase__ : int ) -> bool: lowerCAmelCase = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(UpperCAmelCase__ ) return False if __name__ == "__main__": import doctest doctest.testmod()
4
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __snake_case ={ """configuration_git""": ["""GIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GitConfig""", """GitVisionConfig"""], """processing_git""": ["""GitProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case =[ """GIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """GitForCausalLM""", """GitModel""", """GitPreTrainedModel""", """GitVisionModel""", ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys __snake_case =_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
4
'''simple docstring''' def a_ ( lowerCamelCase : Optional[Any] ): return [ { 0: [1, 2], 1: [0, 2], 2: [0, 1, 3, 5], 3: [2, 4], 4: [3], 5: [2, 6, 8], 6: [5, 7], 7: [6, 8], 8: [5, 7], }, { 0: [6], 1: [9], 2: [4, 5], 3: [4], 4: [2, 3], 5: [2], 6: [0, 7], 7: [6], 8: [], 9: [1], }, { 0: [4], 1: [6], 2: [], 3: [5, 6, 7], 4: [0, 6], 5: [3, 8, 9], 6: [1, 3, 4, 7], 7: [3, 6, 8, 9], 8: [5, 7], 9: [5, 7], }, { 0: [1, 3], 1: [0, 2, 4], 2: [1, 3, 4], 3: [0, 2, 4], 4: [1, 2, 3], }, ][index] def a_ ( lowerCamelCase : dict[int, list[int]] ): lowerCAmelCase = 0 lowerCAmelCase = len(lowerCamelCase ) # No of vertices in graph lowerCAmelCase = [0] * n lowerCAmelCase = [False] * n def dfs(lowerCamelCase : Tuple , lowerCamelCase : str , lowerCamelCase : Dict , lowerCamelCase : str ): lowerCAmelCase = True lowerCAmelCase = id_ id_ += 1 for to in graph[at]: if to == parent: pass elif not visited[to]: dfs(lowerCamelCase , lowerCamelCase , lowerCamelCase , id_ ) lowerCAmelCase = min(low[at] , low[to] ) if id_ <= low[to]: bridges.append((at, to) if at < to else (to, at) ) else: # This edge is a back edge and cannot be a bridge lowerCAmelCase = min(low[at] , low[to] ) lowerCAmelCase = [] for i in range(lowerCamelCase ): if not visited[i]: dfs(lowerCamelCase , -1 , lowerCamelCase , id_ ) return bridges if __name__ == "__main__": import doctest doctest.testmod()
4
1
'''simple docstring''' import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoImageProcessor, ViTImageProcessor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / """utils""")) from test_module.custom_image_processing import CustomImageProcessor # noqa E402 __snake_case =get_tests_dir("""fixtures""") class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : List[Any] ) -> Optional[int]: # A mock response for an HTTP head request to emulate server down lowerCAmelCase = mock.Mock() lowerCAmelCase = 5_0_0 lowerCAmelCase = {} lowerCAmelCase = HTTPError lowerCAmelCase = {} # Download this model to make sure it's in the cache. lowerCAmelCase = ViTImageProcessor.from_pretrained('hf-internal-testing/tiny-random-vit' ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('requests.Session.request' , return_value=UpperCAmelCase__ ) as mock_head: lowerCAmelCase = ViTImageProcessor.from_pretrained('hf-internal-testing/tiny-random-vit' ) # This check we did call the fake head request mock_head.assert_called() def __UpperCAmelCase ( self : Dict ) -> List[str]: # This test is for deprecated behavior and can be removed in v5 lowerCAmelCase = ViTImageProcessor.from_pretrained( 'https://huggingface.co/hf-internal-testing/tiny-random-vit/resolve/main/preprocessor_config.json' ) def __UpperCAmelCase ( self : Optional[Any] ) -> List[Any]: with self.assertRaises(UpperCAmelCase__ ): # config is in subfolder, the following should not work without specifying the subfolder lowerCAmelCase = AutoImageProcessor.from_pretrained('hf-internal-testing/stable-diffusion-all-variants' ) lowerCAmelCase = AutoImageProcessor.from_pretrained( 'hf-internal-testing/stable-diffusion-all-variants' , subfolder='feature_extractor' ) self.assertIsNotNone(UpperCAmelCase__ ) @is_staging_test class UpperCAmelCase_ ( unittest.TestCase ): @classmethod def __UpperCAmelCase ( cls : List[str] ) -> Union[str, Any]: lowerCAmelCase = TOKEN HfFolder.save_token(UpperCAmelCase__ ) @classmethod def __UpperCAmelCase ( cls : Union[str, Any] ) -> Any: try: delete_repo(token=cls._token , repo_id='test-image-processor' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='valid_org/test-image-processor-org' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='test-dynamic-image-processor' ) except HTTPError: pass def __UpperCAmelCase ( self : Optional[Any] ) -> str: lowerCAmelCase = ViTImageProcessor.from_pretrained(UpperCAmelCase__ ) image_processor.push_to_hub('test-image-processor' , use_auth_token=self._token ) lowerCAmelCase = ViTImageProcessor.from_pretrained(F'''{USER}/test-image-processor''' ) for k, v in image_processor.__dict__.items(): self.assertEqual(UpperCAmelCase__ , getattr(UpperCAmelCase__ , UpperCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id='test-image-processor' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained( UpperCAmelCase__ , repo_id='test-image-processor' , push_to_hub=UpperCAmelCase__ , use_auth_token=self._token ) lowerCAmelCase = ViTImageProcessor.from_pretrained(F'''{USER}/test-image-processor''' ) for k, v in image_processor.__dict__.items(): self.assertEqual(UpperCAmelCase__ , getattr(UpperCAmelCase__ , UpperCAmelCase__ ) ) def __UpperCAmelCase ( self : int ) -> str: lowerCAmelCase = ViTImageProcessor.from_pretrained(UpperCAmelCase__ ) image_processor.push_to_hub('valid_org/test-image-processor' , use_auth_token=self._token ) lowerCAmelCase = ViTImageProcessor.from_pretrained('valid_org/test-image-processor' ) for k, v in image_processor.__dict__.items(): self.assertEqual(UpperCAmelCase__ , getattr(UpperCAmelCase__ , UpperCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id='valid_org/test-image-processor' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained( UpperCAmelCase__ , repo_id='valid_org/test-image-processor-org' , push_to_hub=UpperCAmelCase__ , use_auth_token=self._token ) lowerCAmelCase = ViTImageProcessor.from_pretrained('valid_org/test-image-processor-org' ) for k, v in image_processor.__dict__.items(): self.assertEqual(UpperCAmelCase__ , getattr(UpperCAmelCase__ , UpperCAmelCase__ ) ) def __UpperCAmelCase ( self : Union[str, Any] ) -> Dict: CustomImageProcessor.register_for_auto_class() lowerCAmelCase = CustomImageProcessor.from_pretrained(UpperCAmelCase__ ) image_processor.push_to_hub('test-dynamic-image-processor' , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( image_processor.auto_map , {'AutoImageProcessor': 'custom_image_processing.CustomImageProcessor'} , ) lowerCAmelCase = AutoImageProcessor.from_pretrained( F'''{USER}/test-dynamic-image-processor''' , trust_remote_code=UpperCAmelCase__ ) # Can't make an isinstance check because the new_image_processor is from the CustomImageProcessor class of a dynamic module self.assertEqual(new_image_processor.__class__.__name__ , 'CustomImageProcessor' )
4
'''simple docstring''' import argparse from collections import OrderedDict from pathlib import Path import requests import torch from PIL import Image from transformers import GLPNConfig, GLPNForDepthEstimation, GLPNImageProcessor from transformers.utils import logging logging.set_verbosity_info() __snake_case =logging.get_logger(__name__) def a_ ( lowerCamelCase : Any ): lowerCAmelCase = OrderedDict() for key, value in state_dict.items(): if key.startswith('module.encoder' ): lowerCAmelCase = key.replace('module.encoder' , 'glpn.encoder' ) if key.startswith('module.decoder' ): lowerCAmelCase = key.replace('module.decoder' , 'decoder.stages' ) if "patch_embed" in key: # replace for example patch_embed1 by patch_embeddings.0 lowerCAmelCase = key[key.find('patch_embed' ) + len('patch_embed' )] lowerCAmelCase = key.replace(f'''patch_embed{idx}''' , f'''patch_embeddings.{int(lowerCamelCase )-1}''' ) if "norm" in key: lowerCAmelCase = key.replace('norm' , 'layer_norm' ) if "glpn.encoder.layer_norm" in key: # replace for example layer_norm1 by layer_norm.0 lowerCAmelCase = key[key.find('glpn.encoder.layer_norm' ) + len('glpn.encoder.layer_norm' )] lowerCAmelCase = key.replace(f'''layer_norm{idx}''' , f'''layer_norm.{int(lowerCamelCase )-1}''' ) if "layer_norm1" in key: lowerCAmelCase = key.replace('layer_norm1' , 'layer_norm_1' ) if "layer_norm2" in key: lowerCAmelCase = key.replace('layer_norm2' , 'layer_norm_2' ) if "block" in key: # replace for example block1 by block.0 lowerCAmelCase = key[key.find('block' ) + len('block' )] lowerCAmelCase = key.replace(f'''block{idx}''' , f'''block.{int(lowerCamelCase )-1}''' ) if "attn.q" in key: lowerCAmelCase = key.replace('attn.q' , 'attention.self.query' ) if "attn.proj" in key: lowerCAmelCase = key.replace('attn.proj' , 'attention.output.dense' ) if "attn" in key: lowerCAmelCase = key.replace('attn' , 'attention.self' ) if "fc1" in key: lowerCAmelCase = key.replace('fc1' , 'dense1' ) if "fc2" in key: lowerCAmelCase = key.replace('fc2' , 'dense2' ) if "linear_pred" in key: lowerCAmelCase = key.replace('linear_pred' , 'classifier' ) if "linear_fuse" in key: lowerCAmelCase = key.replace('linear_fuse.conv' , 'linear_fuse' ) lowerCAmelCase = key.replace('linear_fuse.bn' , 'batch_norm' ) if "linear_c" in key: # replace for example linear_c4 by linear_c.3 lowerCAmelCase = key[key.find('linear_c' ) + len('linear_c' )] lowerCAmelCase = key.replace(f'''linear_c{idx}''' , f'''linear_c.{int(lowerCamelCase )-1}''' ) if "bot_conv" in key: lowerCAmelCase = key.replace('bot_conv' , '0.convolution' ) if "skip_conv1" in key: lowerCAmelCase = key.replace('skip_conv1' , '1.convolution' ) if "skip_conv2" in key: lowerCAmelCase = key.replace('skip_conv2' , '2.convolution' ) if "fusion1" in key: lowerCAmelCase = key.replace('fusion1' , '1.fusion' ) if "fusion2" in key: lowerCAmelCase = key.replace('fusion2' , '2.fusion' ) if "fusion3" in key: lowerCAmelCase = key.replace('fusion3' , '3.fusion' ) if "fusion" in key and "conv" in key: lowerCAmelCase = key.replace('conv' , 'convolutional_layer' ) if key.startswith('module.last_layer_depth' ): lowerCAmelCase = key.replace('module.last_layer_depth' , 'head.head' ) lowerCAmelCase = value return new_state_dict def a_ ( lowerCamelCase : List[str] , lowerCamelCase : str ): # for each of the encoder blocks: for i in range(config.num_encoder_blocks ): for j in range(config.depths[i] ): # read in weights + bias of keys and values (which is a single matrix in the original implementation) lowerCAmelCase = state_dict.pop(f'''glpn.encoder.block.{i}.{j}.attention.self.kv.weight''' ) lowerCAmelCase = state_dict.pop(f'''glpn.encoder.block.{i}.{j}.attention.self.kv.bias''' ) # next, add keys and values (in that order) to the state dict lowerCAmelCase = kv_weight[ : config.hidden_sizes[i], : ] lowerCAmelCase = kv_bias[: config.hidden_sizes[i]] lowerCAmelCase = kv_weight[ config.hidden_sizes[i] :, : ] lowerCAmelCase = kv_bias[config.hidden_sizes[i] :] def a_ ( ): lowerCAmelCase = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowerCAmelCase = Image.open(requests.get(lowerCamelCase , stream=lowerCamelCase ).raw ) return image @torch.no_grad() def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Tuple , lowerCamelCase : Optional[Any]=False , lowerCamelCase : List[str]=None ): lowerCAmelCase = GLPNConfig(hidden_sizes=[64, 128, 320, 512] , decoder_hidden_size=64 , depths=[3, 8, 27, 3] ) # load image processor (only resize + rescale) lowerCAmelCase = GLPNImageProcessor() # prepare image lowerCAmelCase = prepare_img() lowerCAmelCase = image_processor(images=lowerCamelCase , return_tensors='pt' ).pixel_values logger.info('Converting model...' ) # load original state dict lowerCAmelCase = torch.load(lowerCamelCase , map_location=torch.device('cpu' ) ) # rename keys lowerCAmelCase = rename_keys(lowerCamelCase ) # key and value matrices need special treatment read_in_k_v(lowerCamelCase , lowerCamelCase ) # create HuggingFace model and load state dict lowerCAmelCase = GLPNForDepthEstimation(lowerCamelCase ) model.load_state_dict(lowerCamelCase ) model.eval() # forward pass lowerCAmelCase = model(lowerCamelCase ) lowerCAmelCase = outputs.predicted_depth # verify output if model_name is not None: if "nyu" in model_name: lowerCAmelCase = torch.tensor( [[4.4_147, 4.0_873, 4.0_673], [3.7_890, 3.2_881, 3.1_525], [3.7_674, 3.5_423, 3.4_913]] ) elif "kitti" in model_name: lowerCAmelCase = torch.tensor( [[3.4_291, 2.7_865, 2.5_151], [3.2_841, 2.7_021, 2.3_502], [3.1_147, 2.4_625, 2.2_481]] ) else: raise ValueError(f'''Unknown model name: {model_name}''' ) lowerCAmelCase = torch.Size([1, 480, 640] ) assert predicted_depth.shape == expected_shape assert torch.allclose(predicted_depth[0, :3, :3] , lowerCamelCase , atol=1e-4 ) print('Looks ok!' ) # finally, push to hub if required if push_to_hub: logger.info('Pushing model and image processor to the hub...' ) model.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=lowerCamelCase , ) image_processor.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=lowerCamelCase , ) if __name__ == "__main__": __snake_case =argparse.ArgumentParser() parser.add_argument( """--checkpoint_path""", default=None, 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 folder to output PyTorch model.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to upload the model to the HuggingFace hub.""" ) parser.add_argument( """--model_name""", default="""glpn-kitti""", type=str, help="""Name of the model in case you're pushing to the hub.""", ) __snake_case =parser.parse_args() convert_glpn_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
4
1
'''simple docstring''' from itertools import product from cva import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey from numpy import dot, exp, mgrid, pi, ravel, square, uinta, zeros def a_ ( lowerCamelCase : List[str] , lowerCamelCase : int ): lowerCAmelCase = k_size // 2 lowerCAmelCase , lowerCAmelCase = mgrid[0 - center : k_size - center, 0 - center : k_size - center] lowerCAmelCase = 1 / (2 * pi * sigma) * exp(-(square(lowerCamelCase ) + square(lowerCamelCase )) / (2 * square(lowerCamelCase )) ) return g def a_ ( lowerCamelCase : Any , lowerCamelCase : Tuple , lowerCamelCase : List[str] ): lowerCAmelCase , lowerCAmelCase = image.shape[0], image.shape[1] # dst image height and width lowerCAmelCase = height - k_size + 1 lowerCAmelCase = width - k_size + 1 # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows lowerCAmelCase = zeros((dst_height * dst_width, k_size * k_size) ) lowerCAmelCase = 0 for i, j in product(range(lowerCamelCase ) , range(lowerCamelCase ) ): lowerCAmelCase = ravel(image[i : i + k_size, j : j + k_size] ) lowerCAmelCase = window row += 1 # turn the kernel into shape(k*k, 1) lowerCAmelCase = gen_gaussian_kernel(lowerCamelCase , lowerCamelCase ) lowerCAmelCase = ravel(lowerCamelCase ) # reshape and get the dst image lowerCAmelCase = dot(lowerCamelCase , lowerCamelCase ).reshape(lowerCamelCase , lowerCamelCase ).astype(lowerCamelCase ) return dst if __name__ == "__main__": # read original image __snake_case =imread(R"""../image_data/lena.jpg""") # turn image in gray scale value __snake_case =cvtColor(img, COLOR_BGR2GRAY) # get values with two different mask size __snake_case =gaussian_filter(gray, 3, sigma=1) __snake_case =gaussian_filter(gray, 5, sigma=0.8) # show result images imshow("""gaussian filter with 3x3 mask""", gaussianaxa) imshow("""gaussian filter with 5x5 mask""", gaussianaxa) waitKey()
4
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class UpperCAmelCase_ ( unittest.TestCase ): @slow def __UpperCAmelCase ( self : str ) -> List[str]: lowerCAmelCase = XLMRobertaModel.from_pretrained('xlm-roberta-base' ) lowerCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house lowerCAmelCase = torch.Size((1, 1_2, 7_6_8) ) # batch_size, sequence_length, embedding_vector_dim lowerCAmelCase = torch.tensor( [[-0.0_101, 0.1_218, -0.0_803, 0.0_801, 0.1_327, 0.0_776, -0.1_215, 0.2_383, 0.3_338, 0.3_106, 0.0_300, 0.0_252]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): lowerCAmelCase = model(UpperCAmelCase__ )['last_hidden_state'].detach() self.assertEqual(output.shape , UpperCAmelCase__ ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , UpperCAmelCase__ , atol=1E-3 ) ) @slow def __UpperCAmelCase ( self : List[Any] ) -> Tuple: lowerCAmelCase = XLMRobertaModel.from_pretrained('xlm-roberta-large' ) lowerCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house lowerCAmelCase = torch.Size((1, 1_2, 1_0_2_4) ) # batch_size, sequence_length, embedding_vector_dim lowerCAmelCase = torch.tensor( [[-0.0_699, -0.0_318, 0.0_705, -0.1_241, 0.0_999, -0.0_520, 0.1_004, -0.1_838, -0.4_704, 0.1_437, 0.0_821, 0.0_126]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): lowerCAmelCase = model(UpperCAmelCase__ )['last_hidden_state'].detach() self.assertEqual(output.shape , UpperCAmelCase__ ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , UpperCAmelCase__ , atol=1E-3 ) )
4
1
'''simple docstring''' import argparse import torch from safetensors.torch import load_file from diffusers import StableDiffusionPipeline def a_ ( lowerCamelCase : Union[str, Any] , lowerCamelCase : Tuple , lowerCamelCase : Optional[Any] , lowerCamelCase : Any , lowerCamelCase : Dict ): # load base model lowerCAmelCase = StableDiffusionPipeline.from_pretrained(lowerCamelCase , torch_dtype=torch.floataa ) # load LoRA weight from .safetensors lowerCAmelCase = load_file(lowerCamelCase ) lowerCAmelCase = [] # directly update weight in diffusers model for key in state_dict: # it is suggested to print out the key, it usually will be something like below # "lora_te_text_model_encoder_layers_0_self_attn_k_proj.lora_down.weight" # as we have set the alpha beforehand, so just skip if ".alpha" in key or key in visited: continue if "text" in key: lowerCAmelCase = key.split('.' )[0].split(LORA_PREFIX_TEXT_ENCODER + '_' )[-1].split('_' ) lowerCAmelCase = pipeline.text_encoder else: lowerCAmelCase = key.split('.' )[0].split(LORA_PREFIX_UNET + '_' )[-1].split('_' ) lowerCAmelCase = pipeline.unet # find the target layer lowerCAmelCase = layer_infos.pop(0 ) while len(lowerCamelCase ) > -1: try: lowerCAmelCase = curr_layer.__getattr__(lowerCamelCase ) if len(lowerCamelCase ) > 0: lowerCAmelCase = layer_infos.pop(0 ) elif len(lowerCamelCase ) == 0: break except Exception: if len(lowerCamelCase ) > 0: temp_name += "_" + layer_infos.pop(0 ) else: lowerCAmelCase = layer_infos.pop(0 ) lowerCAmelCase = [] if "lora_down" in key: pair_keys.append(key.replace('lora_down' , 'lora_up' ) ) pair_keys.append(lowerCamelCase ) else: pair_keys.append(lowerCamelCase ) pair_keys.append(key.replace('lora_up' , 'lora_down' ) ) # update weight if len(state_dict[pair_keys[0]].shape ) == 4: lowerCAmelCase = state_dict[pair_keys[0]].squeeze(3 ).squeeze(2 ).to(torch.floataa ) lowerCAmelCase = state_dict[pair_keys[1]].squeeze(3 ).squeeze(2 ).to(torch.floataa ) curr_layer.weight.data += alpha * torch.mm(lowerCamelCase , lowerCamelCase ).unsqueeze(2 ).unsqueeze(3 ) else: lowerCAmelCase = state_dict[pair_keys[0]].to(torch.floataa ) lowerCAmelCase = state_dict[pair_keys[1]].to(torch.floataa ) curr_layer.weight.data += alpha * torch.mm(lowerCamelCase , lowerCamelCase ) # update visited list for item in pair_keys: visited.append(lowerCamelCase ) return pipeline if __name__ == "__main__": __snake_case =argparse.ArgumentParser() parser.add_argument( """--base_model_path""", default=None, type=str, required=True, help="""Path to the base model in diffusers format.""" ) parser.add_argument( """--checkpoint_path""", default=None, type=str, required=True, help="""Path to the checkpoint to convert.""" ) parser.add_argument("""--dump_path""", default=None, type=str, required=True, help="""Path to the output model.""") parser.add_argument( """--lora_prefix_unet""", default="""lora_unet""", type=str, help="""The prefix of UNet weight in safetensors""" ) parser.add_argument( """--lora_prefix_text_encoder""", default="""lora_te""", type=str, help="""The prefix of text encoder weight in safetensors""", ) parser.add_argument("""--alpha""", default=0.7_5, type=float, help="""The merging ratio in W = W0 + alpha * deltaW""") parser.add_argument( """--to_safetensors""", action="""store_true""", help="""Whether to store pipeline in safetensors format or not.""" ) parser.add_argument("""--device""", type=str, help="""Device to use (e.g. cpu, cuda:0, cuda:1, etc.)""") __snake_case =parser.parse_args() __snake_case =args.base_model_path __snake_case =args.checkpoint_path __snake_case =args.dump_path __snake_case =args.lora_prefix_unet __snake_case =args.lora_prefix_text_encoder __snake_case =args.alpha __snake_case =convert(base_model_path, checkpoint_path, lora_prefix_unet, lora_prefix_text_encoder, alpha) __snake_case =pipe.to(args.device) pipe.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
4
'''simple docstring''' import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def a_ ( lowerCamelCase : Dict ): lowerCAmelCase = {} lowerCAmelCase = tokenizer(example['content'] , truncation=lowerCamelCase )['input_ids'] lowerCAmelCase = len(example['content'] ) / len(output['input_ids'] ) return output __snake_case =HfArgumentParser(PretokenizationArguments) __snake_case =parser.parse_args() if args.num_workers is None: __snake_case =multiprocessing.cpu_count() __snake_case =AutoTokenizer.from_pretrained(args.tokenizer_dir) __snake_case =time.time() __snake_case =load_dataset(args.dataset_name, split="""train""") print(F'''Dataset loaded in {time.time()-t_start:.2f}s''') __snake_case =time.time() __snake_case =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''') __snake_case =time.time() ds.push_to_hub(args.tokenized_data_repo) print(F'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
4
1
'''simple docstring''' from __future__ import annotations from typing import Any def a_ ( lowerCamelCase : list ): if not postfix_notation: return 0 lowerCAmelCase = {'+', '-', '*', '/'} lowerCAmelCase = [] for token in postfix_notation: if token in operations: lowerCAmelCase , lowerCAmelCase = stack.pop(), stack.pop() if token == "+": stack.append(a + b ) elif token == "-": stack.append(a - b ) elif token == "*": stack.append(a * b ) else: if a * b < 0 and a % b != 0: stack.append(a // b + 1 ) else: stack.append(a // b ) else: stack.append(int(lowerCamelCase ) ) return stack.pop() if __name__ == "__main__": import doctest doctest.testmod()
4
'''simple docstring''' import logging from dataclasses import dataclass, field from pathlib import Path from typing import Optional, Union from .generation.configuration_utils import GenerationConfig from .training_args import TrainingArguments from .utils import add_start_docstrings __snake_case =logging.getLogger(__name__) @dataclass @add_start_docstrings(TrainingArguments.__doc__ ) class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : bool = field(default=__lowercase , metadata={'''help''': '''Whether to use SortishSampler or not.'''} ) lowerCamelCase : bool = field( default=__lowercase , metadata={'''help''': '''Whether to use generate to calculate generative metrics (ROUGE, BLEU).'''} ) lowerCamelCase : Optional[int] = field( default=__lowercase , metadata={ '''help''': ( '''The `max_length` to use on each evaluation loop when `predict_with_generate=True`. Will default ''' '''to the `max_length` value of the model configuration.''' ) } , ) lowerCamelCase : Optional[int] = field( default=__lowercase , metadata={ '''help''': ( '''The `num_beams` to use on each evaluation loop when `predict_with_generate=True`. Will default ''' '''to the `num_beams` value of the model configuration.''' ) } , ) lowerCamelCase : Optional[Union[str, Path, GenerationConfig]] = field( default=__lowercase , metadata={ '''help''': '''Model id, file path or url pointing to a GenerationConfig json file, to use during prediction.''' } , ) def __UpperCAmelCase ( self : Dict ) -> List[str]: lowerCAmelCase = super().to_dict() for k, v in d.items(): if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): lowerCAmelCase = v.to_dict() return d
4
1
'''simple docstring''' import os import unittest from transformers.models.cpmant.tokenization_cpmant import VOCAB_FILES_NAMES, CpmAntTokenizer from transformers.testing_utils import require_jieba, tooslow from ...test_tokenization_common import TokenizerTesterMixin @require_jieba class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Optional[int] = CpmAntTokenizer lowerCamelCase : List[str] = False def __UpperCAmelCase ( self : Union[str, Any] ) -> Any: super().setUp() lowerCAmelCase = [ '<d>', '</d>', '<s>', '</s>', '</_>', '<unk>', '<pad>', '</n>', '我', '是', 'C', 'P', 'M', 'A', 'n', 't', ] lowerCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) @tooslow def __UpperCAmelCase ( self : Optional[Any] ) -> Any: lowerCAmelCase = CpmAntTokenizer.from_pretrained('openbmb/cpm-ant-10b' ) lowerCAmelCase = '今天天气真好!' lowerCAmelCase = ['今天', '天气', '真', '好', '!'] lowerCAmelCase = tokenizer.tokenize(UpperCAmelCase__ ) self.assertListEqual(UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = '今天天气真好!' lowerCAmelCase = [tokenizer.bos_token] + tokens lowerCAmelCase = [6, 9_8_0_2, 1_4_9_6_2, 2_0_8_2, 8_3_1, 2_4_4] self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase__ ) , UpperCAmelCase__ ) lowerCAmelCase = tokenizer.decode(UpperCAmelCase__ ) self.assertEqual(UpperCAmelCase__ , UpperCAmelCase__ )
4
'''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() __snake_case =logging.get_logger("""transformers.models.encodec""") __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_DECODER, } __snake_case ={ **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_ENCODER_48K, **MAPPING_DECODER, **MAPPING_DECODER_48K, } __snake_case =[] __snake_case =[] def a_ ( lowerCamelCase : Optional[Any] , lowerCamelCase : Union[str, Any] , lowerCamelCase : int , lowerCamelCase : Any , lowerCamelCase : List[str] ): for attribute in key.split('.' ): lowerCAmelCase = getattr(lowerCamelCase , lowerCamelCase ) if weight_type is not None: lowerCAmelCase = getattr(lowerCamelCase , lowerCamelCase ).shape else: lowerCAmelCase = 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": lowerCAmelCase = value elif weight_type == "weight_g": lowerCAmelCase = value elif weight_type == "weight_v": lowerCAmelCase = value elif weight_type == "bias": lowerCAmelCase = value elif weight_type == "running_mean": lowerCAmelCase = value elif weight_type == "running_var": lowerCAmelCase = value elif weight_type == "num_batches_tracked": lowerCAmelCase = value elif weight_type == "weight_ih_l0": lowerCAmelCase = value elif weight_type == "weight_hh_l0": lowerCAmelCase = value elif weight_type == "bias_ih_l0": lowerCAmelCase = value elif weight_type == "bias_hh_l0": lowerCAmelCase = value elif weight_type == "weight_ih_l1": lowerCAmelCase = value elif weight_type == "weight_hh_l1": lowerCAmelCase = value elif weight_type == "bias_ih_l1": lowerCAmelCase = value elif weight_type == "bias_hh_l1": lowerCAmelCase = value else: lowerCAmelCase = value logger.info(f'''{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.''' ) def a_ ( lowerCamelCase : Optional[Any] , lowerCamelCase : Optional[Any] ): for key in ignore_keys: if key.endswith('.*' ): if name.startswith(key[:-1] ): return True elif ".*." in key: lowerCAmelCase , lowerCAmelCase = key.split('.*.' ) if prefix in name and suffix in name: return True elif key in name: return True return False def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Any , lowerCamelCase : str ): lowerCAmelCase = [] if model_name == "encodec_24khz" or "encodec_32khz": lowerCAmelCase = MAPPING_24K elif model_name == "encodec_48khz": lowerCAmelCase = 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 lowerCAmelCase = False for key, mapped_key in MAPPING.items(): if "*" in key: lowerCAmelCase , lowerCAmelCase = key.split('.*.' ) if prefix in name and suffix in name: lowerCAmelCase = suffix if key in name: # HACK otherwise .embed gets initialized with .embed_avg too if key.endswith('embed' ) and name.endswith('embed_avg' ): continue lowerCAmelCase = True if "*" in mapped_key: lowerCAmelCase = name.split(lowerCamelCase )[0].split('.' )[-2] lowerCAmelCase = mapped_key.replace('*' , lowerCamelCase ) if "weight_g" in name: lowerCAmelCase = 'weight_g' elif "weight_v" in name: lowerCAmelCase = 'weight_v' elif "weight_ih_l0" in name: lowerCAmelCase = 'weight_ih_l0' elif "weight_hh_l0" in name: lowerCAmelCase = 'weight_hh_l0' elif "bias_ih_l0" in name: lowerCAmelCase = 'bias_ih_l0' elif "bias_hh_l0" in name: lowerCAmelCase = 'bias_hh_l0' elif "weight_ih_l1" in name: lowerCAmelCase = 'weight_ih_l1' elif "weight_hh_l1" in name: lowerCAmelCase = 'weight_hh_l1' elif "bias_ih_l1" in name: lowerCAmelCase = 'bias_ih_l1' elif "bias_hh_l1" in name: lowerCAmelCase = 'bias_hh_l1' elif "bias" in name: lowerCAmelCase = 'bias' elif "weight" in name: lowerCAmelCase = 'weight' elif "running_mean" in name: lowerCAmelCase = 'running_mean' elif "running_var" in name: lowerCAmelCase = 'running_var' elif "num_batches_tracked" in name: lowerCAmelCase = 'num_batches_tracked' else: lowerCAmelCase = 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 a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Union[str, Any] , lowerCamelCase : str , lowerCamelCase : Dict=None , lowerCamelCase : Union[str, Any]=None , ): if config_path is not None: lowerCAmelCase = EncodecConfig.from_pretrained(lowerCamelCase ) else: lowerCAmelCase = EncodecConfig() if model_name == "encodec_24khz": pass # config is already correct elif model_name == "encodec_32khz": lowerCAmelCase = [8, 5, 4, 4] lowerCAmelCase = [2.2] lowerCAmelCase = 64 lowerCAmelCase = 32000 lowerCAmelCase = 2048 lowerCAmelCase = False lowerCAmelCase = False lowerCAmelCase = False elif model_name == "encodec_48khz": lowerCAmelCase = [8, 5, 4, 2] lowerCAmelCase = [3.0, 6.0, 12.0, 24.0] lowerCAmelCase = 48000 lowerCAmelCase = 2 lowerCAmelCase = False lowerCAmelCase = 'time_group_norm' lowerCAmelCase = True lowerCAmelCase = 1.0 lowerCAmelCase = 0.01 else: raise ValueError(f'''Unknown model name: {model_name}''' ) lowerCAmelCase = EncodecModel(lowerCamelCase ) lowerCAmelCase = 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 ) lowerCAmelCase = 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 lowerCAmelCase = 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__": __snake_case =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.""" ) __snake_case =parser.parse_args() convert_checkpoint( args.model, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
4
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __snake_case =logging.get_logger(__name__) __snake_case ={ """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/config.json""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/config.json""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/config.json""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/config.json""", """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json""", """roberta-large-openai-detector""": """https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json""", } class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : int = '''roberta''' def __init__( self : Optional[Any] , UpperCAmelCase__ : Tuple=5_0_2_6_5 , UpperCAmelCase__ : str=7_6_8 , UpperCAmelCase__ : Optional[Any]=1_2 , UpperCAmelCase__ : Optional[int]=1_2 , UpperCAmelCase__ : int=3_0_7_2 , UpperCAmelCase__ : Any="gelu" , UpperCAmelCase__ : Union[str, Any]=0.1 , UpperCAmelCase__ : List[Any]=0.1 , UpperCAmelCase__ : Any=5_1_2 , UpperCAmelCase__ : List[Any]=2 , UpperCAmelCase__ : List[str]=0.02 , UpperCAmelCase__ : Optional[Any]=1E-12 , UpperCAmelCase__ : str=1 , UpperCAmelCase__ : Optional[int]=0 , UpperCAmelCase__ : str=2 , UpperCAmelCase__ : List[str]="absolute" , UpperCAmelCase__ : Optional[int]=True , UpperCAmelCase__ : List[str]=None , **UpperCAmelCase__ : List[Any] , ) -> Any: super().__init__(pad_token_id=UpperCAmelCase__ , bos_token_id=UpperCAmelCase__ , eos_token_id=UpperCAmelCase__ , **UpperCAmelCase__ ) lowerCAmelCase = vocab_size lowerCAmelCase = hidden_size lowerCAmelCase = num_hidden_layers lowerCAmelCase = num_attention_heads lowerCAmelCase = hidden_act lowerCAmelCase = intermediate_size lowerCAmelCase = hidden_dropout_prob lowerCAmelCase = attention_probs_dropout_prob lowerCAmelCase = max_position_embeddings lowerCAmelCase = type_vocab_size lowerCAmelCase = initializer_range lowerCAmelCase = layer_norm_eps lowerCAmelCase = position_embedding_type lowerCAmelCase = use_cache lowerCAmelCase = classifier_dropout class UpperCAmelCase_ ( __lowercase ): @property def __UpperCAmelCase ( self : List[Any] ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": lowerCAmelCase = {0: 'batch', 1: 'choice', 2: 'sequence'} else: lowerCAmelCase = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ] )
4
'''simple docstring''' import warnings from ...utils import logging from .image_processing_flava import FlavaImageProcessor __snake_case =logging.get_logger(__name__) class UpperCAmelCase_ ( __lowercase ): def __init__( self : Dict , *UpperCAmelCase__ : Dict , **UpperCAmelCase__ : List[str] ) -> None: warnings.warn( 'The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use FlavaImageProcessor instead.' , UpperCAmelCase__ , ) super().__init__(*UpperCAmelCase__ , **UpperCAmelCase__ )
4
1
'''simple docstring''' import argparse from collections import OrderedDict from pathlib import Path import requests import torch from PIL import Image from transformers import GLPNConfig, GLPNForDepthEstimation, GLPNImageProcessor from transformers.utils import logging logging.set_verbosity_info() __snake_case =logging.get_logger(__name__) def a_ ( lowerCamelCase : Any ): lowerCAmelCase = OrderedDict() for key, value in state_dict.items(): if key.startswith('module.encoder' ): lowerCAmelCase = key.replace('module.encoder' , 'glpn.encoder' ) if key.startswith('module.decoder' ): lowerCAmelCase = key.replace('module.decoder' , 'decoder.stages' ) if "patch_embed" in key: # replace for example patch_embed1 by patch_embeddings.0 lowerCAmelCase = key[key.find('patch_embed' ) + len('patch_embed' )] lowerCAmelCase = key.replace(f'''patch_embed{idx}''' , f'''patch_embeddings.{int(lowerCamelCase )-1}''' ) if "norm" in key: lowerCAmelCase = key.replace('norm' , 'layer_norm' ) if "glpn.encoder.layer_norm" in key: # replace for example layer_norm1 by layer_norm.0 lowerCAmelCase = key[key.find('glpn.encoder.layer_norm' ) + len('glpn.encoder.layer_norm' )] lowerCAmelCase = key.replace(f'''layer_norm{idx}''' , f'''layer_norm.{int(lowerCamelCase )-1}''' ) if "layer_norm1" in key: lowerCAmelCase = key.replace('layer_norm1' , 'layer_norm_1' ) if "layer_norm2" in key: lowerCAmelCase = key.replace('layer_norm2' , 'layer_norm_2' ) if "block" in key: # replace for example block1 by block.0 lowerCAmelCase = key[key.find('block' ) + len('block' )] lowerCAmelCase = key.replace(f'''block{idx}''' , f'''block.{int(lowerCamelCase )-1}''' ) if "attn.q" in key: lowerCAmelCase = key.replace('attn.q' , 'attention.self.query' ) if "attn.proj" in key: lowerCAmelCase = key.replace('attn.proj' , 'attention.output.dense' ) if "attn" in key: lowerCAmelCase = key.replace('attn' , 'attention.self' ) if "fc1" in key: lowerCAmelCase = key.replace('fc1' , 'dense1' ) if "fc2" in key: lowerCAmelCase = key.replace('fc2' , 'dense2' ) if "linear_pred" in key: lowerCAmelCase = key.replace('linear_pred' , 'classifier' ) if "linear_fuse" in key: lowerCAmelCase = key.replace('linear_fuse.conv' , 'linear_fuse' ) lowerCAmelCase = key.replace('linear_fuse.bn' , 'batch_norm' ) if "linear_c" in key: # replace for example linear_c4 by linear_c.3 lowerCAmelCase = key[key.find('linear_c' ) + len('linear_c' )] lowerCAmelCase = key.replace(f'''linear_c{idx}''' , f'''linear_c.{int(lowerCamelCase )-1}''' ) if "bot_conv" in key: lowerCAmelCase = key.replace('bot_conv' , '0.convolution' ) if "skip_conv1" in key: lowerCAmelCase = key.replace('skip_conv1' , '1.convolution' ) if "skip_conv2" in key: lowerCAmelCase = key.replace('skip_conv2' , '2.convolution' ) if "fusion1" in key: lowerCAmelCase = key.replace('fusion1' , '1.fusion' ) if "fusion2" in key: lowerCAmelCase = key.replace('fusion2' , '2.fusion' ) if "fusion3" in key: lowerCAmelCase = key.replace('fusion3' , '3.fusion' ) if "fusion" in key and "conv" in key: lowerCAmelCase = key.replace('conv' , 'convolutional_layer' ) if key.startswith('module.last_layer_depth' ): lowerCAmelCase = key.replace('module.last_layer_depth' , 'head.head' ) lowerCAmelCase = value return new_state_dict def a_ ( lowerCamelCase : List[str] , lowerCamelCase : str ): # for each of the encoder blocks: for i in range(config.num_encoder_blocks ): for j in range(config.depths[i] ): # read in weights + bias of keys and values (which is a single matrix in the original implementation) lowerCAmelCase = state_dict.pop(f'''glpn.encoder.block.{i}.{j}.attention.self.kv.weight''' ) lowerCAmelCase = state_dict.pop(f'''glpn.encoder.block.{i}.{j}.attention.self.kv.bias''' ) # next, add keys and values (in that order) to the state dict lowerCAmelCase = kv_weight[ : config.hidden_sizes[i], : ] lowerCAmelCase = kv_bias[: config.hidden_sizes[i]] lowerCAmelCase = kv_weight[ config.hidden_sizes[i] :, : ] lowerCAmelCase = kv_bias[config.hidden_sizes[i] :] def a_ ( ): lowerCAmelCase = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowerCAmelCase = Image.open(requests.get(lowerCamelCase , stream=lowerCamelCase ).raw ) return image @torch.no_grad() def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Tuple , lowerCamelCase : Optional[Any]=False , lowerCamelCase : List[str]=None ): lowerCAmelCase = GLPNConfig(hidden_sizes=[64, 128, 320, 512] , decoder_hidden_size=64 , depths=[3, 8, 27, 3] ) # load image processor (only resize + rescale) lowerCAmelCase = GLPNImageProcessor() # prepare image lowerCAmelCase = prepare_img() lowerCAmelCase = image_processor(images=lowerCamelCase , return_tensors='pt' ).pixel_values logger.info('Converting model...' ) # load original state dict lowerCAmelCase = torch.load(lowerCamelCase , map_location=torch.device('cpu' ) ) # rename keys lowerCAmelCase = rename_keys(lowerCamelCase ) # key and value matrices need special treatment read_in_k_v(lowerCamelCase , lowerCamelCase ) # create HuggingFace model and load state dict lowerCAmelCase = GLPNForDepthEstimation(lowerCamelCase ) model.load_state_dict(lowerCamelCase ) model.eval() # forward pass lowerCAmelCase = model(lowerCamelCase ) lowerCAmelCase = outputs.predicted_depth # verify output if model_name is not None: if "nyu" in model_name: lowerCAmelCase = torch.tensor( [[4.4_147, 4.0_873, 4.0_673], [3.7_890, 3.2_881, 3.1_525], [3.7_674, 3.5_423, 3.4_913]] ) elif "kitti" in model_name: lowerCAmelCase = torch.tensor( [[3.4_291, 2.7_865, 2.5_151], [3.2_841, 2.7_021, 2.3_502], [3.1_147, 2.4_625, 2.2_481]] ) else: raise ValueError(f'''Unknown model name: {model_name}''' ) lowerCAmelCase = torch.Size([1, 480, 640] ) assert predicted_depth.shape == expected_shape assert torch.allclose(predicted_depth[0, :3, :3] , lowerCamelCase , atol=1e-4 ) print('Looks ok!' ) # finally, push to hub if required if push_to_hub: logger.info('Pushing model and image processor to the hub...' ) model.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=lowerCamelCase , ) image_processor.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=lowerCamelCase , ) if __name__ == "__main__": __snake_case =argparse.ArgumentParser() parser.add_argument( """--checkpoint_path""", default=None, 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 folder to output PyTorch model.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to upload the model to the HuggingFace hub.""" ) parser.add_argument( """--model_name""", default="""glpn-kitti""", type=str, help="""Name of the model in case you're pushing to the hub.""", ) __snake_case =parser.parse_args() convert_glpn_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
4
'''simple docstring''' from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer __snake_case =logging.get_logger(__name__) __snake_case ={ """vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_config_file""": """tokenizer_config.json""", } __snake_case ={ """vocab_file""": { """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json""" }, """merges_file""": { """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt""" }, """tokenizer_config_file""": { """facebook/blenderbot_small-90M""": ( """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json""" ) }, } __snake_case ={ """facebook/blenderbot_small-90M""": 512, } class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Tuple = VOCAB_FILES_NAMES lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[Any] = BlenderbotSmallTokenizer def __init__( self : Any , UpperCAmelCase__ : Union[str, Any]=None , UpperCAmelCase__ : Dict=None , UpperCAmelCase__ : int="<|endoftext|>" , UpperCAmelCase__ : Dict="<|endoftext|>" , UpperCAmelCase__ : str="<|endoftext|>" , UpperCAmelCase__ : str=False , UpperCAmelCase__ : Tuple=True , **UpperCAmelCase__ : Optional[Any] , ) -> Any: super().__init__( ByteLevelBPETokenizer( vocab=UpperCAmelCase__ , merges=UpperCAmelCase__ , add_prefix_space=UpperCAmelCase__ , trim_offsets=UpperCAmelCase__ , ) , bos_token=UpperCAmelCase__ , eos_token=UpperCAmelCase__ , unk_token=UpperCAmelCase__ , **UpperCAmelCase__ , ) lowerCAmelCase = add_prefix_space def __UpperCAmelCase ( self : List[Any] , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Dict=None ) -> Any: lowerCAmelCase = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def __UpperCAmelCase ( self : Any , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
4
1
'''simple docstring''' import warnings from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline # noqa F401 warnings.warn( """The `inpainting.py` script is outdated. Please use directly `from diffusers import""" """ StableDiffusionInpaintPipeline` instead.""" )
4
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case =logging.get_logger(__name__) __snake_case ={ """facebook/s2t-wav2vec2-large-en-de""": ( """https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json""" ), # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2 } class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Union[str, Any] = '''speech_to_text_2''' lowerCamelCase : Any = ['''past_key_values'''] lowerCamelCase : Optional[Any] = {'''num_attention_heads''': '''decoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self : Optional[int] , UpperCAmelCase__ : Optional[Any]=1_0_0_0_0 , UpperCAmelCase__ : int=6 , UpperCAmelCase__ : Optional[Any]=2_0_4_8 , UpperCAmelCase__ : Union[str, Any]=4 , UpperCAmelCase__ : List[Any]=0.0 , UpperCAmelCase__ : str=True , UpperCAmelCase__ : str="relu" , UpperCAmelCase__ : Any=2_5_6 , UpperCAmelCase__ : Optional[Any]=0.1 , UpperCAmelCase__ : Any=0.0 , UpperCAmelCase__ : Tuple=0.0 , UpperCAmelCase__ : List[Any]=0.02 , UpperCAmelCase__ : List[Any]=2 , UpperCAmelCase__ : str=True , UpperCAmelCase__ : List[str]=1 , UpperCAmelCase__ : Any=0 , UpperCAmelCase__ : Dict=2 , UpperCAmelCase__ : int=1_0_2_4 , **UpperCAmelCase__ : Optional[Any] , ) -> Dict: lowerCAmelCase = vocab_size lowerCAmelCase = d_model lowerCAmelCase = decoder_ffn_dim lowerCAmelCase = decoder_layers lowerCAmelCase = decoder_attention_heads lowerCAmelCase = dropout lowerCAmelCase = attention_dropout lowerCAmelCase = activation_dropout lowerCAmelCase = activation_function lowerCAmelCase = init_std lowerCAmelCase = decoder_layerdrop lowerCAmelCase = use_cache lowerCAmelCase = decoder_layers lowerCAmelCase = scale_embedding # scale factor will be sqrt(d_model) if True lowerCAmelCase = max_target_positions super().__init__( pad_token_id=UpperCAmelCase__ , bos_token_id=UpperCAmelCase__ , eos_token_id=UpperCAmelCase__ , decoder_start_token_id=UpperCAmelCase__ , **UpperCAmelCase__ , )
4
1
'''simple docstring''' import argparse from pathlib import Path import torch from packaging import version from torch.onnx import export from diffusers import AutoencoderKL __snake_case =version.parse(version.parse(torch.__version__).base_version) < version.parse("""1.11""") def a_ ( lowerCamelCase : Dict , lowerCamelCase : tuple , lowerCamelCase : Path , lowerCamelCase : Dict , lowerCamelCase : List[str] , lowerCamelCase : Dict , lowerCamelCase : Optional[Any] , lowerCamelCase : Optional[int]=False , ): output_path.parent.mkdir(parents=lowerCamelCase , exist_ok=lowerCamelCase ) # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11, # so we check the torch version for backwards compatibility if is_torch_less_than_1_11: export( lowerCamelCase , lowerCamelCase , f=output_path.as_posix() , input_names=lowerCamelCase , output_names=lowerCamelCase , dynamic_axes=lowerCamelCase , do_constant_folding=lowerCamelCase , use_external_data_format=lowerCamelCase , enable_onnx_checker=lowerCamelCase , opset_version=lowerCamelCase , ) else: export( lowerCamelCase , lowerCamelCase , f=output_path.as_posix() , input_names=lowerCamelCase , output_names=lowerCamelCase , dynamic_axes=lowerCamelCase , do_constant_folding=lowerCamelCase , opset_version=lowerCamelCase , ) @torch.no_grad() def a_ ( lowerCamelCase : str , lowerCamelCase : str , lowerCamelCase : int , lowerCamelCase : bool = False ): lowerCAmelCase = torch.floataa if fpaa else torch.floataa if fpaa and torch.cuda.is_available(): lowerCAmelCase = 'cuda' elif fpaa and not torch.cuda.is_available(): raise ValueError('`float16` model export is only supported on GPUs with CUDA' ) else: lowerCAmelCase = 'cpu' lowerCAmelCase = Path(lowerCamelCase ) # VAE DECODER lowerCAmelCase = AutoencoderKL.from_pretrained(model_path + '/vae' ) lowerCAmelCase = vae_decoder.config.latent_channels # forward only through the decoder part lowerCAmelCase = vae_decoder.decode onnx_export( lowerCamelCase , model_args=( torch.randn(1 , lowerCamelCase , 25 , 25 ).to(device=lowerCamelCase , dtype=lowerCamelCase ), False, ) , output_path=output_path / 'vae_decoder' / 'model.onnx' , ordered_input_names=['latent_sample', 'return_dict'] , output_names=['sample'] , dynamic_axes={ 'latent_sample': {0: 'batch', 1: 'channels', 2: 'height', 3: 'width'}, } , opset=lowerCamelCase , ) del vae_decoder if __name__ == "__main__": __snake_case =argparse.ArgumentParser() parser.add_argument( """--model_path""", type=str, required=True, help="""Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).""", ) parser.add_argument("""--output_path""", type=str, required=True, help="""Path to the output model.""") parser.add_argument( """--opset""", default=14, type=int, help="""The version of the ONNX operator set to use.""", ) parser.add_argument("""--fp16""", action="""store_true""", default=False, help="""Export the models in `float16` mode""") __snake_case =parser.parse_args() print(args.output_path) convert_models(args.model_path, args.output_path, args.opset, args.fpaa) print("""SD: Done: ONNX""")
4
'''simple docstring''' from __future__ import annotations from bisect import bisect_left from functools import total_ordering from heapq import merge @total_ordering class UpperCAmelCase_ ( __lowercase ): def __lt__( self : Optional[int] , UpperCAmelCase__ : List[str] ) -> List[Any]: return self[-1] < other[-1] def __eq__( self : str , UpperCAmelCase__ : List[str] ) -> Tuple: return self[-1] == other[-1] def a_ ( lowerCamelCase : list ): lowerCAmelCase = [] # sort into stacks for element in collection: lowerCAmelCase = Stack([element] ) lowerCAmelCase = bisect_left(lowerCamelCase , lowerCamelCase ) if i != len(lowerCamelCase ): stacks[i].append(lowerCamelCase ) else: stacks.append(lowerCamelCase ) # use a heap-based merge to merge stack efficiently lowerCAmelCase = merge(*(reversed(lowerCamelCase ) for stack in stacks) ) return collection if __name__ == "__main__": __snake_case =input("""Enter numbers separated by a comma:\n""").strip() __snake_case =[int(item) for item in user_input.split(""",""")] print(patience_sort(unsorted))
4
1
'''simple docstring''' import unittest from transformers import AlbertTokenizer, AlbertTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin __snake_case =get_tests_dir("""fixtures/spiece.model""") @require_sentencepiece @require_tokenizers class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : List[str] = AlbertTokenizer lowerCamelCase : Dict = AlbertTokenizerFast lowerCamelCase : Optional[Any] = True lowerCamelCase : Any = True lowerCamelCase : str = True def __UpperCAmelCase ( self : int ) -> int: super().setUp() # We have a SentencePiece fixture for testing lowerCAmelCase = AlbertTokenizer(UpperCAmelCase__ ) tokenizer.save_pretrained(self.tmpdirname ) def __UpperCAmelCase ( self : Union[str, Any] , UpperCAmelCase__ : List[str] ) -> Union[str, Any]: lowerCAmelCase = 'this is a test' lowerCAmelCase = 'this is a test' return input_text, output_text def __UpperCAmelCase ( self : List[str] ) -> List[Any]: lowerCAmelCase = '<pad>' lowerCAmelCase = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCAmelCase__ ) , UpperCAmelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCAmelCase__ ) , UpperCAmelCase__ ) def __UpperCAmelCase ( self : Tuple ) -> int: lowerCAmelCase = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<pad>' ) self.assertEqual(vocab_keys[1] , '<unk>' ) self.assertEqual(vocab_keys[-1] , '▁eloquent' ) self.assertEqual(len(UpperCAmelCase__ ) , 3_0_0_0_0 ) def __UpperCAmelCase ( self : str ) -> List[str]: self.assertEqual(self.get_tokenizer().vocab_size , 3_0_0_0_0 ) def __UpperCAmelCase ( self : Tuple ) -> Union[str, Any]: if not self.test_rust_tokenizer: return lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = self.get_rust_tokenizer() lowerCAmelCase = 'I was born in 92000, and this is falsé.' lowerCAmelCase = tokenizer.tokenize(UpperCAmelCase__ ) lowerCAmelCase = rust_tokenizer.tokenize(UpperCAmelCase__ ) self.assertListEqual(UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = tokenizer.encode(UpperCAmelCase__ , add_special_tokens=UpperCAmelCase__ ) lowerCAmelCase = rust_tokenizer.encode(UpperCAmelCase__ , add_special_tokens=UpperCAmelCase__ ) self.assertListEqual(UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = self.get_rust_tokenizer() lowerCAmelCase = tokenizer.encode(UpperCAmelCase__ ) lowerCAmelCase = rust_tokenizer.encode(UpperCAmelCase__ ) self.assertListEqual(UpperCAmelCase__ , UpperCAmelCase__ ) def __UpperCAmelCase ( self : str ) -> str: lowerCAmelCase = AlbertTokenizer(UpperCAmelCase__ , keep_accents=UpperCAmelCase__ ) lowerCAmelCase = tokenizer.tokenize('This is a test' ) self.assertListEqual(UpperCAmelCase__ , ['▁this', '▁is', '▁a', '▁test'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase__ ) , [4_8, 2_5, 2_1, 1_2_8_9] ) lowerCAmelCase = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( UpperCAmelCase__ , ['▁i', '▁was', '▁born', '▁in', '▁9', '2000', ',', '▁and', '▁this', '▁is', '▁fal', 's', 'é', '.'] ) lowerCAmelCase = tokenizer.convert_tokens_to_ids(UpperCAmelCase__ ) self.assertListEqual(UpperCAmelCase__ , [3_1, 2_3, 3_8_6, 1_9, 5_6_1, 3_0_5_0, 1_5, 1_7, 4_8, 2_5, 8_2_5_6, 1_8, 1, 9] ) lowerCAmelCase = tokenizer.convert_ids_to_tokens(UpperCAmelCase__ ) self.assertListEqual( UpperCAmelCase__ , ['▁i', '▁was', '▁born', '▁in', '▁9', '2000', ',', '▁and', '▁this', '▁is', '▁fal', 's', '<unk>', '.'] , ) def __UpperCAmelCase ( self : str ) -> Tuple: lowerCAmelCase = AlbertTokenizer(UpperCAmelCase__ ) lowerCAmelCase = tokenizer.encode('sequence builders' ) lowerCAmelCase = tokenizer.encode('multi-sequence build' ) lowerCAmelCase = tokenizer.build_inputs_with_special_tokens(UpperCAmelCase__ ) lowerCAmelCase = tokenizer.build_inputs_with_special_tokens(UpperCAmelCase__ , UpperCAmelCase__ ) assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [ tokenizer.sep_token_id ] @slow def __UpperCAmelCase ( self : Dict ) -> int: # fmt: off lowerCAmelCase = {'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'input_ids': [[2, 2_1_9_7_0, 1_3, 5, 6_0_9_2, 1_6_7, 2_8, 7_1_0_3, 2_1_5_3, 6_7_3, 8, 7_0_2_8, 1_2_0_5_1, 1_8, 1_7, 7_1_0_3, 2_1_5_3, 6_7_3, 8, 3_5_1_5, 1_8_6_8_4, 8, 4_4_6_1, 6, 1_9_2_7, 2_9_7, 8, 1_2_0_6_0, 2_6_0_7, 1_8, 1_3, 5, 4_4_6_1, 1_5, 1_0_5_3_8, 3_8, 8, 1_3_5, 1_5, 8_2_2, 5_8, 1_5, 9_9_3, 1_0_3_6_3, 1_5, 1_4_6_0, 8_0_0_5, 4_4_6_1, 1_5, 9_9_3, 2_5_5, 2_3_2_8, 9, 9, 9, 6, 2_6, 1_1_1_2, 8_1_6, 3_2_6_0, 1_3, 5, 1_0_3, 2_3_7_7, 6, 1_7, 1_1_1_2, 8_1_6, 2_7_8_2, 1_3, 5, 1_0_3, 1_0_6_4_1, 6, 2_9, 8_4, 2_5_1_2, 2_4_3_0, 7_8_2, 1_8_6_8_4, 2_7_6_1, 1_9, 8_0_8, 2_4_3_0, 2_5_5_6, 1_7, 8_5_5, 1_4_8_0, 9_4_7_7, 4_0_9_1, 1_2_8, 1_1_7_1_2, 1_5, 7_1_0_3, 2_1_5_3, 6_7_3, 1_7, 2_4_8_8_3, 9_9_9_0, 9, 3], [2, 1_1_5_0_2, 2_5, 1_0_0_6, 2_0, 7_8_2, 8, 1_1_8_0_9, 8_5_5, 1_7_3_2, 1_9_3_9_3, 1_8_6_6_7, 3_7, 3_6_7, 2_1_0_1_8, 6_9, 1_8_5_4, 3_4, 1_1_8_6_0, 1_9_1_2_4, 2_7, 1_5_6, 2_2_5, 1_7, 1_9_3, 4_1_4_1, 1_9, 6_5, 9_1_2_4, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 1_4, 2_2_3_1, 8_8_6, 2_3_8_5, 1_7_6_5_9, 8_4, 1_4, 1_6_7_9_2, 1_9_5_2, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=UpperCAmelCase__ , model_name='albert-base-v2' , revision='6b6560eaf5ff2e250b00c50f380c5389a9c2d82e' , )
4
'''simple docstring''' import datasets from .nmt_bleu import compute_bleu # From: https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py __snake_case ="""\ @INPROCEEDINGS{Papineni02bleu:a, author = {Kishore Papineni and Salim Roukos and Todd Ward and Wei-jing Zhu}, title = {BLEU: a Method for Automatic Evaluation of Machine Translation}, booktitle = {}, year = {2002}, pages = {311--318} } @inproceedings{lin-och-2004-orange, title = \"{ORANGE}: a Method for Evaluating Automatic Evaluation Metrics for Machine Translation\", author = \"Lin, Chin-Yew and Och, Franz Josef\", booktitle = \"{COLING} 2004: Proceedings of the 20th International Conference on Computational Linguistics\", month = \"aug 23{--}aug 27\", year = \"2004\", address = \"Geneva, Switzerland\", publisher = \"COLING\", url = \"https://www.aclweb.org/anthology/C04-1072\", pages = \"501--507\", } """ __snake_case ="""\ BLEU (bilingual evaluation understudy) is an algorithm for evaluating the quality of text which has been machine-translated from one natural language to another. Quality is considered to be the correspondence between a machine's output and that of a human: \"the closer a machine translation is to a professional human translation, the better it is\" – this is the central idea behind BLEU. BLEU was one of the first metrics to claim a high correlation with human judgements of quality, and remains one of the most popular automated and inexpensive metrics. Scores are calculated for individual translated segments—generally sentences—by comparing them with a set of good quality reference translations. Those scores are then averaged over the whole corpus to reach an estimate of the translation's overall quality. Intelligibility or grammatical correctness are not taken into account[citation needed]. BLEU's output is always a number between 0 and 1. This value indicates how similar the candidate text is to the reference texts, with values closer to 1 representing more similar texts. Few human translations will attain a score of 1, since this would indicate that the candidate is identical to one of the reference translations. For this reason, it is not necessary to attain a score of 1. Because there are more opportunities to match, adding additional reference translations will increase the BLEU score. """ __snake_case =""" Computes BLEU score of translated segments against one or more references. Args: predictions: list of translations to score. Each translation should be tokenized into a list of tokens. references: list of lists of references for each translation. Each reference should be tokenized into a list of tokens. max_order: Maximum n-gram order to use when computing BLEU score. smooth: Whether or not to apply Lin et al. 2004 smoothing. Returns: 'bleu': bleu score, 'precisions': geometric mean of n-gram precisions, 'brevity_penalty': brevity penalty, 'length_ratio': ratio of lengths, 'translation_length': translation_length, 'reference_length': reference_length Examples: >>> predictions = [ ... [\"hello\", \"there\", \"general\", \"kenobi\"], # tokenized prediction of the first sample ... [\"foo\", \"bar\", \"foobar\"] # tokenized prediction of the second sample ... ] >>> references = [ ... [[\"hello\", \"there\", \"general\", \"kenobi\"], [\"hello\", \"there\", \"!\"]], # tokenized references for the first sample (2 references) ... [[\"foo\", \"bar\", \"foobar\"]] # tokenized references for the second sample (1 reference) ... ] >>> bleu = datasets.load_metric(\"bleu\") >>> results = bleu.compute(predictions=predictions, references=references) >>> print(results[\"bleu\"]) 1.0 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): def __UpperCAmelCase ( self : Tuple ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string' , id='token' ) , id='sequence' ), 'references': datasets.Sequence( datasets.Sequence(datasets.Value('string' , id='token' ) , id='sequence' ) , id='references' ), } ) , codebase_urls=['https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py'] , reference_urls=[ 'https://en.wikipedia.org/wiki/BLEU', 'https://towardsdatascience.com/evaluating-text-output-in-nlp-bleu-at-your-own-risk-e8609665a213', ] , ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Any , UpperCAmelCase__ : Union[str, Any]=4 , UpperCAmelCase__ : Optional[int]=False ) -> int: lowerCAmelCase = compute_bleu( reference_corpus=UpperCAmelCase__ , translation_corpus=UpperCAmelCase__ , max_order=UpperCAmelCase__ , smooth=UpperCAmelCase__ ) ((lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase)) = score return { "bleu": bleu, "precisions": precisions, "brevity_penalty": bp, "length_ratio": ratio, "translation_length": translation_length, "reference_length": reference_length, }
4
1
'''simple docstring''' import argparse import struct import unittest class UpperCAmelCase_ : def __init__( self : List[str] , UpperCAmelCase__ : bytes ) -> None: lowerCAmelCase = data # Initialize hash values lowerCAmelCase = [ 0X6_A_0_9_E_6_6_7, 0XB_B_6_7_A_E_8_5, 0X3_C_6_E_F_3_7_2, 0XA_5_4_F_F_5_3_A, 0X5_1_0_E_5_2_7_F, 0X9_B_0_5_6_8_8_C, 0X1_F_8_3_D_9_A_B, 0X5_B_E_0_C_D_1_9, ] # Initialize round constants lowerCAmelCase = [ 0X4_2_8_A_2_F_9_8, 0X7_1_3_7_4_4_9_1, 0XB_5_C_0_F_B_C_F, 0XE_9_B_5_D_B_A_5, 0X3_9_5_6_C_2_5_B, 0X5_9_F_1_1_1_F_1, 0X9_2_3_F_8_2_A_4, 0XA_B_1_C_5_E_D_5, 0XD_8_0_7_A_A_9_8, 0X1_2_8_3_5_B_0_1, 0X2_4_3_1_8_5_B_E, 0X5_5_0_C_7_D_C_3, 0X7_2_B_E_5_D_7_4, 0X8_0_D_E_B_1_F_E, 0X9_B_D_C_0_6_A_7, 0XC_1_9_B_F_1_7_4, 0XE_4_9_B_6_9_C_1, 0XE_F_B_E_4_7_8_6, 0X0_F_C_1_9_D_C_6, 0X2_4_0_C_A_1_C_C, 0X2_D_E_9_2_C_6_F, 0X4_A_7_4_8_4_A_A, 0X5_C_B_0_A_9_D_C, 0X7_6_F_9_8_8_D_A, 0X9_8_3_E_5_1_5_2, 0XA_8_3_1_C_6_6_D, 0XB_0_0_3_2_7_C_8, 0XB_F_5_9_7_F_C_7, 0XC_6_E_0_0_B_F_3, 0XD_5_A_7_9_1_4_7, 0X0_6_C_A_6_3_5_1, 0X1_4_2_9_2_9_6_7, 0X2_7_B_7_0_A_8_5, 0X2_E_1_B_2_1_3_8, 0X4_D_2_C_6_D_F_C, 0X5_3_3_8_0_D_1_3, 0X6_5_0_A_7_3_5_4, 0X7_6_6_A_0_A_B_B, 0X8_1_C_2_C_9_2_E, 0X9_2_7_2_2_C_8_5, 0XA_2_B_F_E_8_A_1, 0XA_8_1_A_6_6_4_B, 0XC_2_4_B_8_B_7_0, 0XC_7_6_C_5_1_A_3, 0XD_1_9_2_E_8_1_9, 0XD_6_9_9_0_6_2_4, 0XF_4_0_E_3_5_8_5, 0X1_0_6_A_A_0_7_0, 0X1_9_A_4_C_1_1_6, 0X1_E_3_7_6_C_0_8, 0X2_7_4_8_7_7_4_C, 0X3_4_B_0_B_C_B_5, 0X3_9_1_C_0_C_B_3, 0X4_E_D_8_A_A_4_A, 0X5_B_9_C_C_A_4_F, 0X6_8_2_E_6_F_F_3, 0X7_4_8_F_8_2_E_E, 0X7_8_A_5_6_3_6_F, 0X8_4_C_8_7_8_1_4, 0X8_C_C_7_0_2_0_8, 0X9_0_B_E_F_F_F_A, 0XA_4_5_0_6_C_E_B, 0XB_E_F_9_A_3_F_7, 0XC_6_7_1_7_8_F_2, ] lowerCAmelCase = self.preprocessing(self.data ) self.final_hash() @staticmethod def __UpperCAmelCase ( UpperCAmelCase__ : bytes ) -> bytes: lowerCAmelCase = b'\x80' + (b'\x00' * (6_3 - (len(UpperCAmelCase__ ) + 8) % 6_4)) lowerCAmelCase = struct.pack('>Q' , (len(UpperCAmelCase__ ) * 8) ) return data + padding + big_endian_integer def __UpperCAmelCase ( self : Tuple ) -> None: # Convert into blocks of 64 bytes lowerCAmelCase = [ self.preprocessed_data[x : x + 6_4] for x in range(0 , len(self.preprocessed_data ) , 6_4 ) ] for block in self.blocks: # Convert the given block into a list of 4 byte integers lowerCAmelCase = list(struct.unpack('>16L' , UpperCAmelCase__ ) ) # add 48 0-ed integers words += [0] * 4_8 lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = self.hashes for index in range(0 , 6_4 ): if index > 1_5: # modify the zero-ed indexes at the end of the array lowerCAmelCase = ( self.ror(words[index - 1_5] , 7 ) ^ self.ror(words[index - 1_5] , 1_8 ) ^ (words[index - 1_5] >> 3) ) lowerCAmelCase = ( self.ror(words[index - 2] , 1_7 ) ^ self.ror(words[index - 2] , 1_9 ) ^ (words[index - 2] >> 1_0) ) lowerCAmelCase = ( words[index - 1_6] + sa + words[index - 7] + sa ) % 0X1_0_0_0_0_0_0_0_0 # Compression lowerCAmelCase = self.ror(UpperCAmelCase__ , 6 ) ^ self.ror(UpperCAmelCase__ , 1_1 ) ^ self.ror(UpperCAmelCase__ , 2_5 ) lowerCAmelCase = (e & f) ^ ((~e & 0XF_F_F_F_F_F_F_F) & g) lowerCAmelCase = ( h + sa + ch + self.round_constants[index] + words[index] ) % 0X1_0_0_0_0_0_0_0_0 lowerCAmelCase = self.ror(UpperCAmelCase__ , 2 ) ^ self.ror(UpperCAmelCase__ , 1_3 ) ^ self.ror(UpperCAmelCase__ , 2_2 ) lowerCAmelCase = (a & b) ^ (a & c) ^ (b & c) lowerCAmelCase = (sa + maj) % 0X1_0_0_0_0_0_0_0_0 lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = ( g, f, e, ((d + tempa) % 0X1_0_0_0_0_0_0_0_0), c, b, a, ((tempa + tempa) % 0X1_0_0_0_0_0_0_0_0), ) lowerCAmelCase = [a, b, c, d, e, f, g, h] # Modify final values lowerCAmelCase = [ ((element + mutated_hash_values[index]) % 0X1_0_0_0_0_0_0_0_0) for index, element in enumerate(self.hashes ) ] lowerCAmelCase = ''.join([hex(UpperCAmelCase__ )[2:].zfill(8 ) for value in self.hashes] ) def __UpperCAmelCase ( self : Optional[Any] , UpperCAmelCase__ : int , UpperCAmelCase__ : int ) -> int: return 0XF_F_F_F_F_F_F_F & (value << (3_2 - rotations)) | (value >> rotations) class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : List[Any] ) -> None: import hashlib lowerCAmelCase = bytes('Test String' , 'utf-8' ) self.assertEqual(SHAaaa(UpperCAmelCase__ ).hash , hashlib.shaaaa(UpperCAmelCase__ ).hexdigest() ) def a_ ( ): import doctest doctest.testmod() lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( '-s' , '--string' , dest='input_string' , default='Hello World!! Welcome to Cryptography' , help='Hash the string' , ) parser.add_argument( '-f' , '--file' , dest='input_file' , help='Hash contents of a file' ) lowerCAmelCase = parser.parse_args() lowerCAmelCase = args.input_string # hash input should be a bytestring if args.input_file: with open(args.input_file , 'rb' ) as f: lowerCAmelCase = f.read() else: lowerCAmelCase = bytes(lowerCamelCase , 'utf-8' ) print(SHAaaa(lowerCamelCase ).hash ) if __name__ == "__main__": main()
4
'''simple docstring''' from sklearn.metrics import fa_score, matthews_corrcoef import datasets from .record_evaluation import evaluate as evaluate_record __snake_case ="""\ @article{wang2019superglue, title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems}, author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R}, journal={arXiv preprint arXiv:1905.00537}, year={2019} } """ __snake_case ="""\ SuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after GLUE with a new set of more difficult language understanding tasks, improved resources, and a new public leaderboard. """ __snake_case =""" Compute SuperGLUE evaluation metric associated to each SuperGLUE dataset. Args: predictions: list of predictions to score. Depending on the SuperGlUE subset: - for 'record': list of question-answer dictionaries with the following keys: - 'idx': index of the question as specified by the dataset - 'prediction_text': the predicted answer text - for 'multirc': list of question-answer dictionaries with the following keys: - 'idx': index of the question-answer pair as specified by the dataset - 'prediction': the predicted answer label - otherwise: list of predicted labels references: list of reference labels. Depending on the SuperGLUE subset: - for 'record': list of question-answers dictionaries with the following keys: - 'idx': index of the question as specified by the dataset - 'answers': list of possible answers - otherwise: list of reference labels Returns: depending on the SuperGLUE subset: - for 'record': - 'exact_match': Exact match between answer and gold answer - 'f1': F1 score - for 'multirc': - 'exact_match': Exact match between answer and gold answer - 'f1_m': Per-question macro-F1 score - 'f1_a': Average F1 score over all answers - for 'axb': 'matthews_correlation': Matthew Correlation - for 'cb': - 'accuracy': Accuracy - 'f1': F1 score - for all others: - 'accuracy': Accuracy Examples: >>> super_glue_metric = datasets.load_metric('super_glue', 'copa') # any of [\"copa\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"boolq\", \"axg\"] >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'cb') >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0, 'f1': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'record') >>> predictions = [{'idx': {'passage': 0, 'query': 0}, 'prediction_text': 'answer'}] >>> references = [{'idx': {'passage': 0, 'query': 0}, 'answers': ['answer', 'another_answer']}] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 1.0, 'f1': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'multirc') >>> predictions = [{'idx': {'answer': 0, 'paragraph': 0, 'question': 0}, 'prediction': 0}, {'idx': {'answer': 1, 'paragraph': 2, 'question': 3}, 'prediction': 1}] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 1.0, 'f1_m': 1.0, 'f1_a': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'axb') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'matthews_correlation': 1.0} """ def a_ ( lowerCamelCase : str , lowerCamelCase : Union[str, Any] ): return float((preds == labels).mean() ) def a_ ( lowerCamelCase : Union[str, Any] , lowerCamelCase : Dict , lowerCamelCase : str="binary" ): lowerCAmelCase = simple_accuracy(lowerCamelCase , lowerCamelCase ) lowerCAmelCase = float(fa_score(y_true=lowerCamelCase , y_pred=lowerCamelCase , average=lowerCamelCase ) ) return { "accuracy": acc, "f1": fa, } def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : List[Any] ): lowerCAmelCase = {} for id_pred, label in zip(lowerCamelCase , lowerCamelCase ): lowerCAmelCase = f'''{id_pred['idx']['paragraph']}-{id_pred['idx']['question']}''' lowerCAmelCase = id_pred['prediction'] if question_id in question_map: question_map[question_id].append((pred, label) ) else: lowerCAmelCase = [(pred, label)] lowerCAmelCase , lowerCAmelCase = [], [] for question, preds_labels in question_map.items(): lowerCAmelCase , lowerCAmelCase = zip(*lowerCamelCase ) lowerCAmelCase = fa_score(y_true=lowerCamelCase , y_pred=lowerCamelCase , average='macro' ) fas.append(lowerCamelCase ) lowerCAmelCase = int(sum(pred == label for pred, label in preds_labels ) == len(lowerCamelCase ) ) ems.append(lowerCamelCase ) lowerCAmelCase = float(sum(lowerCamelCase ) / len(lowerCamelCase ) ) lowerCAmelCase = sum(lowerCamelCase ) / len(lowerCamelCase ) lowerCAmelCase = float(fa_score(y_true=lowerCamelCase , y_pred=[id_pred['prediction'] for id_pred in ids_preds] ) ) return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a} @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): def __UpperCAmelCase ( self : List[str] ) -> List[Any]: if self.config_name not in [ "boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg", ]: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , codebase_urls=[] , reference_urls=[] , format='numpy' if not self.config_name == 'record' and not self.config_name == 'multirc' else None , ) def __UpperCAmelCase ( self : Union[str, Any] ) -> str: if self.config_name == "record": return { "predictions": { "idx": { "passage": datasets.Value('int64' ), "query": datasets.Value('int64' ), }, "prediction_text": datasets.Value('string' ), }, "references": { "idx": { "passage": datasets.Value('int64' ), "query": datasets.Value('int64' ), }, "answers": datasets.Sequence(datasets.Value('string' ) ), }, } elif self.config_name == "multirc": return { "predictions": { "idx": { "answer": datasets.Value('int64' ), "paragraph": datasets.Value('int64' ), "question": datasets.Value('int64' ), }, "prediction": datasets.Value('int64' ), }, "references": datasets.Value('int64' ), } else: return { "predictions": datasets.Value('int64' ), "references": datasets.Value('int64' ), } def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : Any , UpperCAmelCase__ : Optional[Any] ) -> Any: if self.config_name == "axb": return {"matthews_correlation": matthews_corrcoef(UpperCAmelCase__ , UpperCAmelCase__ )} elif self.config_name == "cb": return acc_and_fa(UpperCAmelCase__ , UpperCAmelCase__ , fa_avg='macro' ) elif self.config_name == "record": lowerCAmelCase = [ { 'qas': [ {'id': ref['idx']['query'], 'answers': [{'text': ans} for ans in ref['answers']]} for ref in references ] } ] lowerCAmelCase = {pred['idx']['query']: pred['prediction_text'] for pred in predictions} return evaluate_record(UpperCAmelCase__ , UpperCAmelCase__ )[0] elif self.config_name == "multirc": return evaluate_multirc(UpperCAmelCase__ , UpperCAmelCase__ ) elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]: return {"accuracy": simple_accuracy(UpperCAmelCase__ , UpperCAmelCase__ )} else: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]' )
4
1
'''simple docstring''' import numpy as np from transformers import BatchFeature from transformers.testing_utils import require_tf, require_torch from .test_feature_extraction_common import FeatureExtractionSavingTestMixin class UpperCAmelCase_ ( __lowercase ): # to overwrite at feature extractactor specific tests lowerCamelCase : Tuple = None lowerCamelCase : Any = None @property def __UpperCAmelCase ( self : Optional[Any] ) -> Tuple: return self.feat_extract_tester.prepare_feat_extract_dict() def __UpperCAmelCase ( self : int ) -> int: lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(UpperCAmelCase__ , 'feature_size' ) ) self.assertTrue(hasattr(UpperCAmelCase__ , 'sampling_rate' ) ) self.assertTrue(hasattr(UpperCAmelCase__ , 'padding_value' ) ) def __UpperCAmelCase ( self : Union[str, Any] ) -> int: lowerCAmelCase = self.feat_extract_tester.prepare_inputs_for_common() lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) lowerCAmelCase = feat_extract.model_input_names[0] lowerCAmelCase = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ) for x, y in zip(UpperCAmelCase__ , processed_features[input_name] ) ) ) lowerCAmelCase = self.feat_extract_tester.prepare_inputs_for_common(equal_length=UpperCAmelCase__ ) lowerCAmelCase = BatchFeature({input_name: speech_inputs} , tensor_type='np' ) lowerCAmelCase = processed_features[input_name] if len(batch_features_input.shape ) < 3: lowerCAmelCase = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.feature_size) ) @require_torch def __UpperCAmelCase ( self : Dict ) -> Union[str, Any]: lowerCAmelCase = self.feat_extract_tester.prepare_inputs_for_common(equal_length=UpperCAmelCase__ ) lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) lowerCAmelCase = feat_extract.model_input_names[0] lowerCAmelCase = BatchFeature({input_name: speech_inputs} , tensor_type='pt' ) lowerCAmelCase = processed_features[input_name] if len(batch_features_input.shape ) < 3: lowerCAmelCase = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.feature_size) ) @require_tf def __UpperCAmelCase ( self : Union[str, Any] ) -> int: lowerCAmelCase = self.feat_extract_tester.prepare_inputs_for_common(equal_length=UpperCAmelCase__ ) lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) lowerCAmelCase = feat_extract.model_input_names[0] lowerCAmelCase = BatchFeature({input_name: speech_inputs} , tensor_type='tf' ) lowerCAmelCase = processed_features[input_name] if len(batch_features_input.shape ) < 3: lowerCAmelCase = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.feature_size) ) def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : Union[str, Any]=False ) -> Tuple: def _inputs_have_equal_length(UpperCAmelCase__ : Optional[Any] ): lowerCAmelCase = len(input[0] ) for input_slice in input[1:]: if len(UpperCAmelCase__ ) != length: return False return True def _inputs_are_equal(UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Union[str, Any] ): if len(UpperCAmelCase__ ) != len(UpperCAmelCase__ ): return False for input_slice_a, input_slice_a in zip(UpperCAmelCase__ , UpperCAmelCase__ ): if not np.allclose(np.asarray(UpperCAmelCase__ ) , np.asarray(UpperCAmelCase__ ) , atol=1E-3 ): return False return True lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) lowerCAmelCase = self.feat_extract_tester.prepare_inputs_for_common(numpify=UpperCAmelCase__ ) lowerCAmelCase = feat_extract.model_input_names[0] lowerCAmelCase = BatchFeature({input_name: speech_inputs} ) lowerCAmelCase = self.feat_extract_tester.seq_length_diff lowerCAmelCase = self.feat_extract_tester.max_seq_length + pad_diff lowerCAmelCase = self.feat_extract_tester.min_seq_length lowerCAmelCase = self.feat_extract_tester.batch_size lowerCAmelCase = self.feat_extract_tester.feature_size # test padding for List[int] + numpy lowerCAmelCase = feat_extract.pad(UpperCAmelCase__ , padding=UpperCAmelCase__ ) lowerCAmelCase = input_a[input_name] lowerCAmelCase = feat_extract.pad(UpperCAmelCase__ , padding='longest' ) lowerCAmelCase = input_a[input_name] lowerCAmelCase = feat_extract.pad(UpperCAmelCase__ , padding='max_length' , max_length=len(speech_inputs[-1] ) ) lowerCAmelCase = input_a[input_name] lowerCAmelCase = feat_extract.pad(UpperCAmelCase__ , padding='longest' , return_tensors='np' ) lowerCAmelCase = input_a[input_name] # max_length parameter has to be provided when setting `padding="max_length"` with self.assertRaises(UpperCAmelCase__ ): feat_extract.pad(UpperCAmelCase__ , padding='max_length' )[input_name] lowerCAmelCase = feat_extract.pad( UpperCAmelCase__ , padding='max_length' , max_length=UpperCAmelCase__ , return_tensors='np' ) lowerCAmelCase = input_a[input_name] self.assertFalse(_inputs_have_equal_length(UpperCAmelCase__ ) ) self.assertTrue(_inputs_have_equal_length(UpperCAmelCase__ ) ) self.assertTrue(_inputs_have_equal_length(UpperCAmelCase__ ) ) self.assertTrue(_inputs_are_equal(UpperCAmelCase__ , UpperCAmelCase__ ) ) self.assertTrue(len(input_a[0] ) == pad_min_length ) self.assertTrue(len(input_a[1] ) == pad_min_length + pad_diff ) self.assertTrue(input_a.shape[:2] == (batch_size, len(input_a[0] )) ) self.assertTrue(input_a.shape[:2] == (batch_size, pad_max_length) ) if feature_size > 1: self.assertTrue(input_a.shape[2] == input_a.shape[2] == feature_size ) # test padding for `pad_to_multiple_of` for List[int] + numpy lowerCAmelCase = feat_extract.pad(UpperCAmelCase__ , pad_to_multiple_of=1_0 ) lowerCAmelCase = input_a[input_name] lowerCAmelCase = feat_extract.pad(UpperCAmelCase__ , padding='longest' , pad_to_multiple_of=1_0 ) lowerCAmelCase = input_a[input_name] lowerCAmelCase = feat_extract.pad( UpperCAmelCase__ , padding='max_length' , pad_to_multiple_of=1_0 , max_length=UpperCAmelCase__ ) lowerCAmelCase = input_a[input_name] lowerCAmelCase = feat_extract.pad( UpperCAmelCase__ , padding='max_length' , pad_to_multiple_of=1_0 , max_length=UpperCAmelCase__ , return_tensors='np' , ) lowerCAmelCase = input_a[input_name] self.assertTrue(all(len(UpperCAmelCase__ ) % 1_0 == 0 for x in input_a ) ) self.assertTrue(_inputs_are_equal(UpperCAmelCase__ , UpperCAmelCase__ ) ) lowerCAmelCase = pad_max_length if pad_max_length % 1_0 == 0 else (pad_max_length // 1_0 + 1) * 1_0 self.assertTrue(all(len(UpperCAmelCase__ ) == expected_mult_pad_length for x in input_a ) ) self.assertEqual(input_a.shape[:2] , (batch_size, expected_mult_pad_length) ) if feature_size > 1: self.assertTrue(input_a.shape[2] == feature_size ) # Check padding value is correct lowerCAmelCase = (np.ones(self.feat_extract_tester.feature_size ) * feat_extract.padding_value).sum() self.assertTrue( abs(np.asarray(input_a[0] )[pad_min_length:].sum() - padding_vector_sum * (pad_max_length - pad_min_length) ) < 1E-3 ) self.assertTrue( abs( np.asarray(input_a[1] )[pad_min_length + pad_diff :].sum() - padding_vector_sum * (pad_max_length - pad_min_length - pad_diff) ) < 1E-3 ) self.assertTrue( abs( np.asarray(input_a[2] )[pad_min_length + 2 * pad_diff :].sum() - padding_vector_sum * (pad_max_length - pad_min_length - 2 * pad_diff) ) < 1E-3 ) self.assertTrue( abs(input_a[0, pad_min_length:].sum() - padding_vector_sum * (pad_max_length - pad_min_length) ) < 1E-3 ) self.assertTrue( abs(input_a[0, pad_min_length:].sum() - padding_vector_sum * (expected_mult_pad_length - pad_min_length) ) < 1E-3 ) def __UpperCAmelCase ( self : Dict , UpperCAmelCase__ : str=False ) -> List[Any]: def _inputs_have_equal_length(UpperCAmelCase__ : Union[str, Any] ): lowerCAmelCase = len(input[0] ) for input_slice in input[1:]: if len(UpperCAmelCase__ ) != length: return False return True def _inputs_are_equal(UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Any ): if len(UpperCAmelCase__ ) != len(UpperCAmelCase__ ): return False for input_slice_a, input_slice_a in zip(UpperCAmelCase__ , UpperCAmelCase__ ): if not np.allclose(np.asarray(UpperCAmelCase__ ) , np.asarray(UpperCAmelCase__ ) , atol=1E-3 ): return False return True lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) lowerCAmelCase = self.feat_extract_tester.prepare_inputs_for_common(numpify=UpperCAmelCase__ ) lowerCAmelCase = feat_extract.model_input_names[0] lowerCAmelCase = BatchFeature({input_name: speech_inputs} ) # truncate to smallest lowerCAmelCase = feat_extract.pad( UpperCAmelCase__ , padding='max_length' , max_length=len(speech_inputs[0] ) , truncation=UpperCAmelCase__ ) lowerCAmelCase = input_a[input_name] lowerCAmelCase = feat_extract.pad(UpperCAmelCase__ , padding='max_length' , max_length=len(speech_inputs[0] ) ) lowerCAmelCase = input_a[input_name] self.assertTrue(_inputs_have_equal_length(UpperCAmelCase__ ) ) self.assertFalse(_inputs_have_equal_length(UpperCAmelCase__ ) ) # truncate to smallest with np lowerCAmelCase = feat_extract.pad( UpperCAmelCase__ , padding='max_length' , max_length=len(speech_inputs[0] ) , return_tensors='np' , truncation=UpperCAmelCase__ , ) lowerCAmelCase = input_a[input_name] lowerCAmelCase = feat_extract.pad( UpperCAmelCase__ , padding='max_length' , max_length=len(speech_inputs[0] ) , return_tensors='np' ) lowerCAmelCase = input_a[input_name] self.assertTrue(_inputs_have_equal_length(UpperCAmelCase__ ) ) self.assertTrue(input_a.shape[1] == len(speech_inputs[0] ) ) # since truncation forces padding to be smaller than longest input # function can't return `np.ndarray`, but has to return list self.assertFalse(_inputs_have_equal_length(UpperCAmelCase__ ) ) # truncate to middle lowerCAmelCase = feat_extract.pad( UpperCAmelCase__ , padding='max_length' , max_length=len(speech_inputs[1] ) , truncation=UpperCAmelCase__ , return_tensors='np' , ) lowerCAmelCase = input_a[input_name] lowerCAmelCase = feat_extract.pad( UpperCAmelCase__ , padding='max_length' , max_length=len(speech_inputs[1] ) , truncation=UpperCAmelCase__ ) lowerCAmelCase = input_a[input_name] lowerCAmelCase = feat_extract.pad( UpperCAmelCase__ , padding='max_length' , max_length=len(speech_inputs[1] ) , return_tensors='np' ) lowerCAmelCase = input_a[input_name] self.assertTrue(input_a.shape[1] == len(speech_inputs[1] ) ) self.assertTrue(_inputs_have_equal_length(UpperCAmelCase__ ) ) self.assertTrue(_inputs_have_equal_length(UpperCAmelCase__ ) ) self.assertTrue(_inputs_are_equal(UpperCAmelCase__ , UpperCAmelCase__ ) ) # since truncation forces padding to be smaller than longest input # function can't return `np.ndarray`, but has to return list self.assertFalse(_inputs_have_equal_length(UpperCAmelCase__ ) ) self.assertTrue(len(input_a[-1] ) == len(speech_inputs[-1] ) ) # padding has to be max_length when setting `truncation=True` with self.assertRaises(UpperCAmelCase__ ): feat_extract.pad(UpperCAmelCase__ , truncation=UpperCAmelCase__ )[input_name] # padding has to be max_length when setting `truncation=True` with self.assertRaises(UpperCAmelCase__ ): feat_extract.pad(UpperCAmelCase__ , padding='longest' , truncation=UpperCAmelCase__ )[input_name] # padding has to be max_length when setting `truncation=True` with self.assertRaises(UpperCAmelCase__ ): feat_extract.pad(UpperCAmelCase__ , padding='longest' , truncation=UpperCAmelCase__ )[input_name] # max_length parameter has to be provided when setting `truncation=True` and padding="max_length" with self.assertRaises(UpperCAmelCase__ ): feat_extract.pad(UpperCAmelCase__ , padding='max_length' , truncation=UpperCAmelCase__ )[input_name] # test truncation for `pad_to_multiple_of` for List[int] + numpy lowerCAmelCase = 1_2 lowerCAmelCase = feat_extract.pad( UpperCAmelCase__ , padding='max_length' , max_length=len(speech_inputs[0] ) , pad_to_multiple_of=UpperCAmelCase__ , truncation=UpperCAmelCase__ , ) lowerCAmelCase = input_a[input_name] lowerCAmelCase = feat_extract.pad( UpperCAmelCase__ , padding='max_length' , max_length=len(speech_inputs[0] ) , pad_to_multiple_of=UpperCAmelCase__ , ) lowerCAmelCase = input_a[input_name] # retrieve expected_length as multiple of pad_to_multiple_of lowerCAmelCase = len(speech_inputs[0] ) if expected_length % pad_to_multiple_of != 0: lowerCAmelCase = ((len(speech_inputs[0] ) // pad_to_multiple_of) + 1) * pad_to_multiple_of self.assertTrue(len(input_a[0] ) == expected_length ) self.assertTrue(_inputs_have_equal_length(UpperCAmelCase__ ) ) self.assertFalse(_inputs_have_equal_length(UpperCAmelCase__ ) ) def __UpperCAmelCase ( self : List[str] ) -> List[str]: self._check_padding(numpify=UpperCAmelCase__ ) def __UpperCAmelCase ( self : str ) -> Any: self._check_padding(numpify=UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[Any] ) -> List[Any]: self._check_truncation(numpify=UpperCAmelCase__ ) def __UpperCAmelCase ( self : str ) -> Optional[int]: self._check_truncation(numpify=UpperCAmelCase__ ) @require_torch def __UpperCAmelCase ( self : List[Any] ) -> str: lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) lowerCAmelCase = self.feat_extract_tester.prepare_inputs_for_common() lowerCAmelCase = feat_extract.model_input_names[0] lowerCAmelCase = BatchFeature({input_name: speech_inputs} ) lowerCAmelCase = feat_extract.pad(UpperCAmelCase__ , padding='longest' , return_tensors='np' )[input_name] lowerCAmelCase = feat_extract.pad(UpperCAmelCase__ , padding='longest' , return_tensors='pt' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) @require_tf def __UpperCAmelCase ( self : Dict ) -> List[Any]: lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) lowerCAmelCase = self.feat_extract_tester.prepare_inputs_for_common() lowerCAmelCase = feat_extract.model_input_names[0] lowerCAmelCase = BatchFeature({input_name: speech_inputs} ) lowerCAmelCase = feat_extract.pad(UpperCAmelCase__ , padding='longest' , return_tensors='np' )[input_name] lowerCAmelCase = feat_extract.pad(UpperCAmelCase__ , padding='longest' , return_tensors='tf' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_tf.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def __UpperCAmelCase ( self : Any ) -> Any: lowerCAmelCase = self.feat_extract_dict lowerCAmelCase = True lowerCAmelCase = self.feature_extraction_class(**UpperCAmelCase__ ) lowerCAmelCase = self.feat_extract_tester.prepare_inputs_for_common() lowerCAmelCase = [len(UpperCAmelCase__ ) for x in speech_inputs] lowerCAmelCase = feat_extract.model_input_names[0] lowerCAmelCase = BatchFeature({input_name: speech_inputs} ) lowerCAmelCase = feat_extract.pad(UpperCAmelCase__ , padding='longest' , return_tensors='np' ) self.assertIn('attention_mask' , UpperCAmelCase__ ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , UpperCAmelCase__ ) def __UpperCAmelCase ( self : Union[str, Any] ) -> Union[str, Any]: lowerCAmelCase = self.feat_extract_dict lowerCAmelCase = True lowerCAmelCase = self.feature_extraction_class(**UpperCAmelCase__ ) lowerCAmelCase = self.feat_extract_tester.prepare_inputs_for_common() lowerCAmelCase = [len(UpperCAmelCase__ ) for x in speech_inputs] lowerCAmelCase = feat_extract.model_input_names[0] lowerCAmelCase = BatchFeature({input_name: speech_inputs} ) lowerCAmelCase = min(UpperCAmelCase__ ) lowerCAmelCase = feat_extract.pad( UpperCAmelCase__ , padding='max_length' , max_length=UpperCAmelCase__ , truncation=UpperCAmelCase__ , return_tensors='np' ) self.assertIn('attention_mask' , UpperCAmelCase__ ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] )
4
'''simple docstring''' print((lambda quine: quine % quine)("""print((lambda quine: quine %% quine)(%r))"""))
4
1
'''simple docstring''' from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import KarrasVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : UNetaDModel lowerCamelCase : KarrasVeScheduler def __init__( self : str , UpperCAmelCase__ : UNetaDModel , UpperCAmelCase__ : KarrasVeScheduler ) -> int: super().__init__() self.register_modules(unet=UpperCAmelCase__ , scheduler=UpperCAmelCase__ ) @torch.no_grad() def __call__( self : Dict , UpperCAmelCase__ : int = 1 , UpperCAmelCase__ : int = 5_0 , UpperCAmelCase__ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , UpperCAmelCase__ : Optional[str] = "pil" , UpperCAmelCase__ : bool = True , **UpperCAmelCase__ : Dict , ) -> Union[Tuple, ImagePipelineOutput]: lowerCAmelCase = self.unet.config.sample_size lowerCAmelCase = (batch_size, 3, img_size, img_size) lowerCAmelCase = self.unet # sample x_0 ~ N(0, sigma_0^2 * I) lowerCAmelCase = randn_tensor(UpperCAmelCase__ , generator=UpperCAmelCase__ , device=self.device ) * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(UpperCAmelCase__ ) for t in self.progress_bar(self.scheduler.timesteps ): # here sigma_t == t_i from the paper lowerCAmelCase = self.scheduler.schedule[t] lowerCAmelCase = self.scheduler.schedule[t - 1] if t > 0 else 0 # 1. Select temporarily increased noise level sigma_hat # 2. Add new noise to move from sample_i to sample_hat lowerCAmelCase , lowerCAmelCase = self.scheduler.add_noise_to_input(UpperCAmelCase__ , UpperCAmelCase__ , generator=UpperCAmelCase__ ) # 3. Predict the noise residual given the noise magnitude `sigma_hat` # The model inputs and output are adjusted by following eq. (213) in [1]. lowerCAmelCase = (sigma_hat / 2) * model((sample_hat + 1) / 2 , sigma_hat / 2 ).sample # 4. Evaluate dx/dt at sigma_hat # 5. Take Euler step from sigma to sigma_prev lowerCAmelCase = self.scheduler.step(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) if sigma_prev != 0: # 6. Apply 2nd order correction # The model inputs and output are adjusted by following eq. (213) in [1]. lowerCAmelCase = (sigma_prev / 2) * model((step_output.prev_sample + 1) / 2 , sigma_prev / 2 ).sample lowerCAmelCase = self.scheduler.step_correct( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , step_output.prev_sample , step_output['derivative'] , ) lowerCAmelCase = step_output.prev_sample lowerCAmelCase = (sample / 2 + 0.5).clamp(0 , 1 ) lowerCAmelCase = sample.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": lowerCAmelCase = self.numpy_to_pil(UpperCAmelCase__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=UpperCAmelCase__ )
4
'''simple docstring''' import os __snake_case ={"""I""": 1, """V""": 5, """X""": 10, """L""": 50, """C""": 100, """D""": 500, """M""": 1_000} def a_ ( lowerCamelCase : str ): lowerCAmelCase = 0 lowerCAmelCase = 0 while index < len(lowerCamelCase ) - 1: lowerCAmelCase = SYMBOLS[numerals[index]] lowerCAmelCase = SYMBOLS[numerals[index + 1]] if current_value < next_value: total_value -= current_value else: total_value += current_value index += 1 total_value += SYMBOLS[numerals[index]] return total_value def a_ ( lowerCamelCase : int ): lowerCAmelCase = '' lowerCAmelCase = num // 1000 numerals += m_count * "M" num %= 1000 lowerCAmelCase = num // 100 if c_count == 9: numerals += "CM" c_count -= 9 elif c_count == 4: numerals += "CD" c_count -= 4 if c_count >= 5: numerals += "D" c_count -= 5 numerals += c_count * "C" num %= 100 lowerCAmelCase = num // 10 if x_count == 9: numerals += "XC" x_count -= 9 elif x_count == 4: numerals += "XL" x_count -= 4 if x_count >= 5: numerals += "L" x_count -= 5 numerals += x_count * "X" num %= 10 if num == 9: numerals += "IX" num -= 9 elif num == 4: numerals += "IV" num -= 4 if num >= 5: numerals += "V" num -= 5 numerals += num * "I" return numerals def a_ ( lowerCamelCase : str = "/p089_roman.txt" ): lowerCAmelCase = 0 with open(os.path.dirname(lowerCamelCase ) + roman_numerals_filename ) as filea: lowerCAmelCase = filea.readlines() for line in lines: lowerCAmelCase = line.strip() lowerCAmelCase = parse_roman_numerals(lowerCamelCase ) lowerCAmelCase = generate_roman_numerals(lowerCamelCase ) savings += len(lowerCamelCase ) - len(lowerCamelCase ) return savings if __name__ == "__main__": print(F'''{solution() = }''')
4
1
'''simple docstring''' __snake_case ={ "joule": 1.0, "kilojoule": 1_000, "megajoule": 1_000_000, "gigajoule": 1_000_000_000, "wattsecond": 1.0, "watthour": 3_600, "kilowatthour": 3_600_000, "newtonmeter": 1.0, "calorie_nutr": 4_186.8, "kilocalorie_nutr": 4_186_800.00, "electronvolt": 1.6_02_17_66_34e-19, "britishthermalunit_it": 1_055.05_585, "footpound": 1.3_5_5_8_1_8, } def a_ ( lowerCamelCase : str , lowerCamelCase : str , lowerCamelCase : float ): if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: lowerCAmelCase = ( 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()
4
'''simple docstring''' import itertools import random import unittest import numpy as np from transformers import ASTFeatureExtractor from transformers.testing_utils import require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin __snake_case =random.Random() if is_torch_available(): import torch def a_ ( lowerCamelCase : Dict , lowerCamelCase : Dict=1.0 , lowerCamelCase : List[Any]=None , lowerCamelCase : Union[str, Any]=None ): if rng is None: lowerCAmelCase = global_rng lowerCAmelCase = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class UpperCAmelCase_ ( unittest.TestCase ): def __init__( self : List[Any] , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : List[str]=7 , UpperCAmelCase__ : int=4_0_0 , UpperCAmelCase__ : int=2_0_0_0 , UpperCAmelCase__ : List[str]=1 , UpperCAmelCase__ : Tuple=0.0 , UpperCAmelCase__ : Tuple=1_6_0_0_0 , UpperCAmelCase__ : Tuple=True , UpperCAmelCase__ : Union[str, Any]=True , ) -> Any: lowerCAmelCase = parent lowerCAmelCase = batch_size lowerCAmelCase = min_seq_length lowerCAmelCase = max_seq_length lowerCAmelCase = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) lowerCAmelCase = feature_size lowerCAmelCase = padding_value lowerCAmelCase = sampling_rate lowerCAmelCase = return_attention_mask lowerCAmelCase = do_normalize def __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def __UpperCAmelCase ( self : str , UpperCAmelCase__ : Union[str, Any]=False , UpperCAmelCase__ : Union[str, Any]=False ) -> Optional[Any]: def _flatten(UpperCAmelCase__ : int ): return list(itertools.chain(*UpperCAmelCase__ ) ) if equal_length: lowerCAmelCase = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size lowerCAmelCase = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: lowerCAmelCase = [np.asarray(UpperCAmelCase__ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Dict = ASTFeatureExtractor def __UpperCAmelCase ( self : str ) -> Optional[int]: lowerCAmelCase = ASTFeatureExtractionTester(self ) def __UpperCAmelCase ( self : Optional[int] ) -> Optional[int]: # Tests that all call wrap to encode_plus and batch_encode_plus lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 lowerCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] lowerCAmelCase = [np.asarray(UpperCAmelCase__ ) for speech_input in speech_inputs] # Test not batched input lowerCAmelCase = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values lowerCAmelCase = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1E-3 ) ) # Test batched lowerCAmelCase = feat_extract(UpperCAmelCase__ , padding=UpperCAmelCase__ , return_tensors='np' ).input_values lowerCAmelCase = feat_extract(UpperCAmelCase__ , padding=UpperCAmelCase__ , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__ , UpperCAmelCase__ ): self.assertTrue(np.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. lowerCAmelCase = [floats_list((1, x) )[0] for x in (8_0_0, 8_0_0, 8_0_0)] lowerCAmelCase = np.asarray(UpperCAmelCase__ ) lowerCAmelCase = feat_extract(UpperCAmelCase__ , return_tensors='np' ).input_values lowerCAmelCase = feat_extract(UpperCAmelCase__ , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__ , UpperCAmelCase__ ): self.assertTrue(np.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1E-3 ) ) @require_torch def __UpperCAmelCase ( self : Union[str, Any] ) -> Optional[int]: import torch lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCAmelCase = np.random.rand(1_0_0 ).astype(np.floataa ) lowerCAmelCase = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: lowerCAmelCase = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) lowerCAmelCase = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def __UpperCAmelCase ( self : int , UpperCAmelCase__ : str ) -> Tuple: from datasets import load_dataset lowerCAmelCase = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech lowerCAmelCase = ds.sort('id' ).select(range(UpperCAmelCase__ ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] @require_torch def __UpperCAmelCase ( self : str ) -> Optional[Any]: # fmt: off lowerCAmelCase = torch.tensor( [-0.9_894, -1.2_776, -0.9_066, -1.2_776, -0.9_349, -1.2_609, -1.0_386, -1.2_776, -1.1_561, -1.2_776, -1.2_052, -1.2_723, -1.2_190, -1.2_132, -1.2_776, -1.1_133, -1.1_953, -1.1_343, -1.1_584, -1.2_203, -1.1_770, -1.2_474, -1.2_381, -1.1_936, -0.9_270, -0.8_317, -0.8_049, -0.7_706, -0.7_565, -0.7_869] ) # fmt: on lowerCAmelCase = self._load_datasamples(1 ) lowerCAmelCase = ASTFeatureExtractor() lowerCAmelCase = feature_extractor(UpperCAmelCase__ , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 1_0_2_4, 1_2_8) ) self.assertTrue(torch.allclose(input_values[0, 0, :3_0] , UpperCAmelCase__ , atol=1E-4 ) )
4
1
'''simple docstring''' import argparse import logging from collections import namedtuple import torch from model_bertabs import BertAbsSummarizer from models.model_builder import AbsSummarizer # The authors' implementation from transformers import BertTokenizer logging.basicConfig(level=logging.INFO) __snake_case =logging.getLogger(__name__) __snake_case ="""Hello world! cécé herlolip""" __snake_case =namedtuple( """BertAbsConfig""", [ """temp_dir""", """large""", """use_bert_emb""", """finetune_bert""", """encoder""", """share_emb""", """max_pos""", """enc_layers""", """enc_hidden_size""", """enc_heads""", """enc_ff_size""", """enc_dropout""", """dec_layers""", """dec_hidden_size""", """dec_heads""", """dec_ff_size""", """dec_dropout""", ], ) def a_ ( lowerCamelCase : Tuple , lowerCamelCase : Dict ): lowerCAmelCase = BertAbsConfig( temp_dir='.' , finetune_bert=lowerCamelCase , large=lowerCamelCase , share_emb=lowerCamelCase , use_bert_emb=lowerCamelCase , encoder='bert' , max_pos=512 , enc_layers=6 , enc_hidden_size=512 , enc_heads=8 , enc_ff_size=512 , enc_dropout=0.2 , dec_layers=6 , dec_hidden_size=768 , dec_heads=8 , dec_ff_size=2048 , dec_dropout=0.2 , ) lowerCAmelCase = torch.load(lowerCamelCase , lambda lowerCamelCase , lowerCamelCase : storage ) lowerCAmelCase = AbsSummarizer(lowerCamelCase , torch.device('cpu' ) , lowerCamelCase ) original.eval() lowerCAmelCase = BertAbsSummarizer(lowerCamelCase , torch.device('cpu' ) ) new_model.eval() # ------------------- # Convert the weights # ------------------- logging.info('convert the model' ) new_model.bert.load_state_dict(original.bert.state_dict() ) new_model.decoder.load_state_dict(original.decoder.state_dict() ) new_model.generator.load_state_dict(original.generator.state_dict() ) # ---------------------------------- # Make sure the outpus are identical # ---------------------------------- logging.info('Make sure that the models\' outputs are identical' ) lowerCAmelCase = BertTokenizer.from_pretrained('bert-base-uncased' ) # prepare the model inputs lowerCAmelCase = tokenizer.encode('This is sample éàalj\'-.' ) encoder_input_ids.extend([tokenizer.pad_token_id] * (512 - len(lowerCamelCase )) ) lowerCAmelCase = torch.tensor(lowerCamelCase ).unsqueeze(0 ) lowerCAmelCase = tokenizer.encode('This is sample 3 éàalj\'-.' ) decoder_input_ids.extend([tokenizer.pad_token_id] * (512 - len(lowerCamelCase )) ) lowerCAmelCase = torch.tensor(lowerCamelCase ).unsqueeze(0 ) # failsafe to make sure the weights reset does not affect the # loaded weights. assert torch.max(torch.abs(original.generator[0].weight - new_model.generator[0].weight ) ) == 0 # forward pass lowerCAmelCase = encoder_input_ids lowerCAmelCase = decoder_input_ids lowerCAmelCase = lowerCAmelCase = None lowerCAmelCase = None lowerCAmelCase = lowerCAmelCase = None lowerCAmelCase = lowerCAmelCase = None lowerCAmelCase = None # The original model does not apply the geneator layer immediatly but rather in # the beam search (where it combines softmax + linear layer). Since we already # apply the softmax in our generation process we only apply the linear layer here. # We make sure that the outputs of the full stack are identical lowerCAmelCase = original(lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase )[0] lowerCAmelCase = original.generator(lowerCamelCase ) lowerCAmelCase = new_model( lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase )[0] lowerCAmelCase = new_model.generator(lowerCamelCase ) lowerCAmelCase = torch.max(torch.abs(output_converted_model - output_original_model ) ).item() print('Maximum absolute difference beween weights: {:.2f}'.format(lowerCamelCase ) ) lowerCAmelCase = torch.max(torch.abs(output_converted_generator - output_original_generator ) ).item() print('Maximum absolute difference beween weights: {:.2f}'.format(lowerCamelCase ) ) lowerCAmelCase = torch.allclose(lowerCamelCase , lowerCamelCase , atol=1e-3 ) if are_identical: logging.info('all weights are equal up to 1e-3' ) else: raise ValueError('the weights are different. The new model is likely different from the original one.' ) # The model has been saved with torch.save(model) and this is bound to the exact # directory structure. We save the state_dict instead. logging.info('saving the model\'s state dictionary' ) torch.save( new_model.state_dict() , './bertabs-finetuned-cnndm-extractive-abstractive-summarization/pytorch_model.bin' ) if __name__ == "__main__": __snake_case =argparse.ArgumentParser() parser.add_argument( """--bertabs_checkpoint_path""", default=None, type=str, required=True, help="""Path the official PyTorch dump.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""", ) __snake_case =parser.parse_args() convert_bertabs_checkpoints( args.bertabs_checkpoint_path, args.pytorch_dump_folder_path, )
4
'''simple docstring''' import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : str ) -> List[Any]: lowerCAmelCase = torch.nn.Linear(1_0 , 1_0 ) lowerCAmelCase = torch.optim.SGD(model.parameters() , 0.1 ) lowerCAmelCase = Accelerator() lowerCAmelCase = accelerator.prepare(UpperCAmelCase__ ) try: pickle.loads(pickle.dumps(UpperCAmelCase__ ) ) except Exception as e: self.fail(F'''Accelerated optimizer pickling failed with {e}''' ) AcceleratorState._reset_state()
4
1
'''simple docstring''' import warnings from ...utils import logging from .image_processing_flava import FlavaImageProcessor __snake_case =logging.get_logger(__name__) class UpperCAmelCase_ ( __lowercase ): def __init__( self : Dict , *UpperCAmelCase__ : Dict , **UpperCAmelCase__ : List[str] ) -> None: warnings.warn( 'The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use FlavaImageProcessor instead.' , UpperCAmelCase__ , ) super().__init__(*UpperCAmelCase__ , **UpperCAmelCase__ )
4
'''simple docstring''' import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __snake_case =logging.get_logger(__name__) __snake_case ={ """vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_config_file""": """tokenizer_config.json""", } __snake_case ={ """vocab_file""": {"""facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json"""}, """merges_file""": {"""facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt"""}, """tokenizer_config_file""": { """facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json""" }, } __snake_case ={"""facebook/blenderbot-3B""": 128} class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : List[Any] = VOCAB_FILES_NAMES lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[Any] = ['''input_ids''', '''attention_mask'''] lowerCamelCase : List[Any] = BlenderbotTokenizer def __init__( self : Union[str, Any] , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : List[Any]=None , UpperCAmelCase__ : str="replace" , UpperCAmelCase__ : Dict="<s>" , UpperCAmelCase__ : Tuple="</s>" , UpperCAmelCase__ : Optional[Any]="</s>" , UpperCAmelCase__ : Any="<s>" , UpperCAmelCase__ : List[str]="<unk>" , UpperCAmelCase__ : int="<pad>" , UpperCAmelCase__ : Union[str, Any]="<mask>" , UpperCAmelCase__ : str=False , UpperCAmelCase__ : Union[str, Any]=True , **UpperCAmelCase__ : Optional[int] , ) -> int: super().__init__( UpperCAmelCase__ , UpperCAmelCase__ , tokenizer_file=UpperCAmelCase__ , errors=UpperCAmelCase__ , bos_token=UpperCAmelCase__ , eos_token=UpperCAmelCase__ , sep_token=UpperCAmelCase__ , cls_token=UpperCAmelCase__ , unk_token=UpperCAmelCase__ , pad_token=UpperCAmelCase__ , mask_token=UpperCAmelCase__ , add_prefix_space=UpperCAmelCase__ , trim_offsets=UpperCAmelCase__ , **UpperCAmelCase__ , ) lowerCAmelCase = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('add_prefix_space' , UpperCAmelCase__ ) != add_prefix_space: lowerCAmelCase = getattr(UpperCAmelCase__ , pre_tok_state.pop('type' ) ) lowerCAmelCase = add_prefix_space lowerCAmelCase = pre_tok_class(**UpperCAmelCase__ ) lowerCAmelCase = add_prefix_space lowerCAmelCase = 'post_processor' lowerCAmelCase = getattr(self.backend_tokenizer , UpperCAmelCase__ , UpperCAmelCase__ ) if tokenizer_component_instance: lowerCAmelCase = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: lowerCAmelCase = tuple(state['sep'] ) if "cls" in state: lowerCAmelCase = tuple(state['cls'] ) lowerCAmelCase = False if state.get('add_prefix_space' , UpperCAmelCase__ ) != add_prefix_space: lowerCAmelCase = add_prefix_space lowerCAmelCase = True if state.get('trim_offsets' , UpperCAmelCase__ ) != trim_offsets: lowerCAmelCase = trim_offsets lowerCAmelCase = True if changes_to_apply: lowerCAmelCase = getattr(UpperCAmelCase__ , state.pop('type' ) ) lowerCAmelCase = component_class(**UpperCAmelCase__ ) setattr(self.backend_tokenizer , UpperCAmelCase__ , UpperCAmelCase__ ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def __UpperCAmelCase ( self : Union[str, Any] ) -> str: if self._mask_token is None: if self.verbose: logger.error('Using mask_token, but it is not set yet.' ) return None return str(self._mask_token ) @mask_token.setter def __UpperCAmelCase ( self : int , UpperCAmelCase__ : Optional[Any] ) -> Tuple: lowerCAmelCase = AddedToken(UpperCAmelCase__ , lstrip=UpperCAmelCase__ , rstrip=UpperCAmelCase__ ) if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) else value lowerCAmelCase = value def __UpperCAmelCase ( self : Optional[Any] , *UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : List[str] ) -> BatchEncoding: lowerCAmelCase = kwargs.get('is_split_into_words' , UpperCAmelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*UpperCAmelCase__ , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : List[str] , *UpperCAmelCase__ : str , **UpperCAmelCase__ : List[str] ) -> BatchEncoding: lowerCAmelCase = kwargs.get('is_split_into_words' , UpperCAmelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*UpperCAmelCase__ , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : str , UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[str] = None ) -> Tuple[str]: lowerCAmelCase = self._tokenizer.model.save(UpperCAmelCase__ , name=UpperCAmelCase__ ) return tuple(UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> Any: return token_ids_a + [self.eos_token_id] def __UpperCAmelCase ( self : Union[str, Any] , UpperCAmelCase__ : "Conversation" ) -> List[int]: lowerCAmelCase = [] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(' ' + text ) else: # Generated responses should contain them already. inputs.append(UpperCAmelCase__ ) lowerCAmelCase = ' '.join(UpperCAmelCase__ ) lowerCAmelCase = self.encode(UpperCAmelCase__ ) if len(UpperCAmelCase__ ) > self.model_max_length: lowerCAmelCase = input_ids[-self.model_max_length :] logger.warning(F'''Trimmed input from conversation as it was longer than {self.model_max_length} tokens.''' ) return input_ids
4
1
'''simple docstring''' from ....configuration_utils import PretrainedConfig from ....utils import logging __snake_case =logging.get_logger(__name__) __snake_case ={ """Visual-Attention-Network/van-base""": ( """https://huggingface.co/Visual-Attention-Network/van-base/blob/main/config.json""" ), } class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : str = '''van''' def __init__( self : Tuple , UpperCAmelCase__ : List[str]=2_2_4 , UpperCAmelCase__ : Any=3 , UpperCAmelCase__ : Optional[Any]=[7, 3, 3, 3] , UpperCAmelCase__ : List[Any]=[4, 2, 2, 2] , UpperCAmelCase__ : Union[str, Any]=[6_4, 1_2_8, 3_2_0, 5_1_2] , UpperCAmelCase__ : Union[str, Any]=[3, 3, 1_2, 3] , UpperCAmelCase__ : List[str]=[8, 8, 4, 4] , UpperCAmelCase__ : List[Any]="gelu" , UpperCAmelCase__ : Tuple=0.02 , UpperCAmelCase__ : Optional[Any]=1E-6 , UpperCAmelCase__ : Optional[Any]=1E-2 , UpperCAmelCase__ : Optional[Any]=0.0 , UpperCAmelCase__ : Any=0.0 , **UpperCAmelCase__ : Union[str, Any] , ) -> Dict: super().__init__(**UpperCAmelCase__ ) lowerCAmelCase = image_size lowerCAmelCase = num_channels lowerCAmelCase = patch_sizes lowerCAmelCase = strides lowerCAmelCase = hidden_sizes lowerCAmelCase = depths lowerCAmelCase = mlp_ratios lowerCAmelCase = hidden_act lowerCAmelCase = initializer_range lowerCAmelCase = layer_norm_eps lowerCAmelCase = layer_scale_init_value lowerCAmelCase = drop_path_rate lowerCAmelCase = dropout_rate
4
'''simple docstring''' from __future__ import annotations from statistics import mean def a_ ( lowerCamelCase : list[int] , lowerCamelCase : list[int] , lowerCamelCase : int ): lowerCAmelCase = [0] * no_of_processes lowerCAmelCase = [0] * no_of_processes # Initialize remaining_time to waiting_time. for i in range(lowerCamelCase ): lowerCAmelCase = burst_time[i] lowerCAmelCase = [] lowerCAmelCase = 0 lowerCAmelCase = 0 # When processes are not completed, # A process whose arrival time has passed \ # and has remaining execution time is put into the ready_process. # The shortest process in the ready_process, target_process is executed. while completed != no_of_processes: lowerCAmelCase = [] lowerCAmelCase = -1 for i in range(lowerCamelCase ): if (arrival_time[i] <= total_time) and (remaining_time[i] > 0): ready_process.append(lowerCamelCase ) if len(lowerCamelCase ) > 0: lowerCAmelCase = ready_process[0] for i in ready_process: if remaining_time[i] < remaining_time[target_process]: lowerCAmelCase = i total_time += burst_time[target_process] completed += 1 lowerCAmelCase = 0 lowerCAmelCase = ( total_time - arrival_time[target_process] - burst_time[target_process] ) else: total_time += 1 return waiting_time def a_ ( lowerCamelCase : list[int] , lowerCamelCase : int , lowerCamelCase : list[int] ): lowerCAmelCase = [0] * no_of_processes for i in range(lowerCamelCase ): lowerCAmelCase = burst_time[i] + waiting_time[i] return turn_around_time if __name__ == "__main__": print("""[TEST CASE 01]""") __snake_case =4 __snake_case =[2, 5, 3, 7] __snake_case =[0, 0, 0, 0] __snake_case =calculate_waitingtime(arrival_time, burst_time, no_of_processes) __snake_case =calculate_turnaroundtime( burst_time, no_of_processes, waiting_time ) # Printing the Result print("""PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time""") for i, process_id in enumerate(list(range(1, 5))): print( F'''{process_id}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t''' F'''{waiting_time[i]}\t\t\t\t{turn_around_time[i]}''' ) print(F'''\nAverage waiting time = {mean(waiting_time):.5f}''') print(F'''Average turnaround time = {mean(turn_around_time):.5f}''')
4
1
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionAttendAndExcitePipeline, UNetaDConditionModel, ) from diffusers.utils import load_numpy, skip_mps, slow from diffusers.utils.testing_utils import require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin __snake_case =False @skip_mps class UpperCAmelCase_ ( __lowercase , __lowercase , __lowercase , unittest.TestCase ): lowerCamelCase : List[Any] = StableDiffusionAttendAndExcitePipeline lowerCamelCase : Any = False lowerCamelCase : List[str] = TEXT_TO_IMAGE_PARAMS lowerCamelCase : Tuple = TEXT_TO_IMAGE_BATCH_PARAMS.union({'''token_indices'''} ) lowerCamelCase : Dict = TEXT_TO_IMAGE_IMAGE_PARAMS lowerCamelCase : Optional[Any] = TEXT_TO_IMAGE_IMAGE_PARAMS @classmethod def __UpperCAmelCase ( cls : str ) -> Union[str, Any]: super().setUpClass() torch.use_deterministic_algorithms(UpperCAmelCase__ ) @classmethod def __UpperCAmelCase ( cls : int ) -> List[str]: super().tearDownClass() torch.use_deterministic_algorithms(UpperCAmelCase__ ) def __UpperCAmelCase ( self : int ) -> int: torch.manual_seed(0 ) lowerCAmelCase = UNetaDConditionModel( block_out_channels=(3_2, 6_4) , layers_per_block=1 , sample_size=3_2 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=3_2 , attention_head_dim=(2, 4) , use_linear_projection=UpperCAmelCase__ , ) lowerCAmelCase = DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=UpperCAmelCase__ , set_alpha_to_one=UpperCAmelCase__ , ) torch.manual_seed(0 ) lowerCAmelCase = AutoencoderKL( block_out_channels=[3_2, 6_4] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , sample_size=1_2_8 , ) torch.manual_seed(0 ) lowerCAmelCase = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=3_2 , intermediate_size=3_7 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_0_0_0 , hidden_act='gelu' , projection_dim=5_1_2 , ) lowerCAmelCase = CLIPTextModel(UpperCAmelCase__ ) lowerCAmelCase = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) lowerCAmelCase = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def __UpperCAmelCase ( self : Any , UpperCAmelCase__ : int , UpperCAmelCase__ : Union[str, Any]=0 ) -> Tuple: if str(UpperCAmelCase__ ).startswith('mps' ): lowerCAmelCase = torch.manual_seed(UpperCAmelCase__ ) else: lowerCAmelCase = torch.Generator(device=UpperCAmelCase__ ).manual_seed(UpperCAmelCase__ ) lowerCAmelCase = lowerCAmelCase = { 'prompt': 'a cat and a frog', 'token_indices': [2, 5], 'generator': generator, 'num_inference_steps': 1, 'guidance_scale': 6.0, 'output_type': 'numpy', 'max_iter_to_alter': 2, 'thresholds': {0: 0.7}, } return inputs def __UpperCAmelCase ( self : Optional[int] ) -> List[Any]: lowerCAmelCase = 'cpu' lowerCAmelCase = self.get_dummy_components() lowerCAmelCase = self.pipeline_class(**UpperCAmelCase__ ) pipe.to(UpperCAmelCase__ ) pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) lowerCAmelCase = self.get_dummy_inputs(UpperCAmelCase__ ) lowerCAmelCase = pipe(**UpperCAmelCase__ ).images lowerCAmelCase = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 6_4, 6_4, 3) ) lowerCAmelCase = np.array( [0.63_905_364, 0.62_897_307, 0.48_599_017, 0.5_133_624, 0.5_550_048, 0.45_769_516, 0.50_326_973, 0.5_023_139, 0.45_384_496] ) lowerCAmelCase = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(UpperCAmelCase__ , 1E-3 ) def __UpperCAmelCase ( self : List[Any] ) -> Optional[int]: super().test_cpu_offload_forward_pass(expected_max_diff=5E-4 ) def __UpperCAmelCase ( self : str ) -> int: # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def __UpperCAmelCase ( self : Optional[int] ) -> Optional[int]: self._test_inference_batch_single_identical(batch_size=2 , expected_max_diff=7E-4 ) def __UpperCAmelCase ( self : List[Any] ) -> int: super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) def __UpperCAmelCase ( self : Union[str, Any] ) -> Tuple: super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5E-4 ) def __UpperCAmelCase ( self : Optional[int] ) -> Any: super().test_save_load_local(expected_max_difference=5E-4 ) def __UpperCAmelCase ( self : List[Any] ) -> List[str]: super().test_save_load_optional_components(expected_max_difference=4E-4 ) @require_torch_gpu @slow class UpperCAmelCase_ ( unittest.TestCase ): @classmethod def __UpperCAmelCase ( cls : List[Any] ) -> List[Any]: super().setUpClass() torch.use_deterministic_algorithms(UpperCAmelCase__ ) @classmethod def __UpperCAmelCase ( cls : str ) -> Optional[Any]: super().tearDownClass() torch.use_deterministic_algorithms(UpperCAmelCase__ ) def __UpperCAmelCase ( self : Tuple ) -> int: super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self : List[str] ) -> Optional[Any]: lowerCAmelCase = torch.manual_seed(5_1 ) lowerCAmelCase = StableDiffusionAttendAndExcitePipeline.from_pretrained( 'CompVis/stable-diffusion-v1-4' , safety_checker=UpperCAmelCase__ , torch_dtype=torch.floataa ) pipe.to('cuda' ) lowerCAmelCase = 'a painting of an elephant with glasses' lowerCAmelCase = [5, 7] lowerCAmelCase = pipe( prompt=UpperCAmelCase__ , token_indices=UpperCAmelCase__ , guidance_scale=7.5 , generator=UpperCAmelCase__ , num_inference_steps=5 , max_iter_to_alter=5 , output_type='numpy' , ).images[0] lowerCAmelCase = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy' ) assert np.abs((expected_image - image).max() ) < 5E-1
4
'''simple docstring''' import gc import unittest import numpy as np import torch from diffusers import StableDiffusionKDiffusionPipeline from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : Optional[int] ) -> int: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self : Tuple ) -> Any: lowerCAmelCase = StableDiffusionKDiffusionPipeline.from_pretrained('CompVis/stable-diffusion-v1-4' ) lowerCAmelCase = sd_pipe.to(UpperCAmelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) sd_pipe.set_scheduler('sample_euler' ) lowerCAmelCase = 'A painting of a squirrel eating a burger' lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = sd_pipe([prompt] , generator=UpperCAmelCase__ , guidance_scale=9.0 , num_inference_steps=2_0 , output_type='np' ) lowerCAmelCase = output.images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase = np.array([0.0_447, 0.0_492, 0.0_468, 0.0_408, 0.0_383, 0.0_408, 0.0_354, 0.0_380, 0.0_339] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __UpperCAmelCase ( self : List[str] ) -> Dict: lowerCAmelCase = StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowerCAmelCase = sd_pipe.to(UpperCAmelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) sd_pipe.set_scheduler('sample_euler' ) lowerCAmelCase = 'A painting of a squirrel eating a burger' lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = sd_pipe([prompt] , generator=UpperCAmelCase__ , guidance_scale=9.0 , num_inference_steps=2_0 , output_type='np' ) lowerCAmelCase = output.images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase = np.array([0.1_237, 0.1_320, 0.1_438, 0.1_359, 0.1_390, 0.1_132, 0.1_277, 0.1_175, 0.1_112] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-1 def __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: lowerCAmelCase = StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowerCAmelCase = sd_pipe.to(UpperCAmelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) sd_pipe.set_scheduler('sample_dpmpp_2m' ) lowerCAmelCase = 'A painting of a squirrel eating a burger' lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = sd_pipe( [prompt] , generator=UpperCAmelCase__ , guidance_scale=7.5 , num_inference_steps=1_5 , output_type='np' , use_karras_sigmas=UpperCAmelCase__ , ) lowerCAmelCase = output.images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase = np.array( [0.11_381_689, 0.12_112_921, 0.1_389_457, 0.12_549_606, 0.1_244_964, 0.10_831_517, 0.11_562_866, 0.10_867_816, 0.10_499_048] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
4
1
'''simple docstring''' def a_ ( lowerCamelCase : Dict ): lowerCAmelCase = len(lowerCamelCase ) while cur > 1: # Find the maximum number in arr lowerCAmelCase = arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi lowerCAmelCase = arr[mi::-1] + arr[mi + 1 : len(lowerCamelCase )] # Reverse whole list lowerCAmelCase = arr[cur - 1 :: -1] + arr[cur : len(lowerCamelCase )] cur -= 1 return arr if __name__ == "__main__": __snake_case =input("""Enter numbers separated by a comma:\n""").strip() __snake_case =[int(item) for item in user_input.split(""",""")] print(pancake_sort(unsorted))
4
'''simple docstring''' # Copyright (c) 2021-, NVIDIA CORPORATION. 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. #################################################################################################### # # Note: If when running this conversion script you're getting an exception: # ModuleNotFoundError: No module named 'megatron.model.enums' # you need to tell python where to find the clone of Megatron-LM, e.g.: # # cd /tmp # git clone https://github.com/NVIDIA/Megatron-LM # PYTHONPATH=/tmp/Megatron-LM python src/transformers/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py ... # # if you already have it cloned elsewhere, simply adjust the path to the existing path # # If the training was done using a Megatron-LM fork, e.g., # https://github.com/microsoft/Megatron-DeepSpeed/ then chances are that you need to have that one # in your path, i.e., /path/to/Megatron-DeepSpeed/ # import argparse import os import re import zipfile import torch from transformers import AutoTokenizer, GPTaConfig def a_ ( lowerCamelCase : int , lowerCamelCase : Optional[Any] , lowerCamelCase : Union[str, Any]=0 ): # Format the message. if name is None: lowerCAmelCase = None else: lowerCAmelCase = '.' * max(0 , spaces - 2 ) + '# {:' + str(50 - spaces ) + 's}' lowerCAmelCase = fmt.format(lowerCamelCase ) # Print and recurse (if needed). if isinstance(lowerCamelCase , lowerCamelCase ): if msg is not None: print(lowerCamelCase ) for k in val.keys(): recursive_print(lowerCamelCase , val[k] , spaces + 2 ) elif isinstance(lowerCamelCase , torch.Tensor ): print(lowerCamelCase , ':' , val.size() ) else: print(lowerCamelCase , ':' , lowerCamelCase ) def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : List[str] , lowerCamelCase : List[Any] , lowerCamelCase : Dict , lowerCamelCase : Tuple ): # Permutes layout of param tensor to [num_splits * num_heads * hidden_size, :] # for compatibility with later versions of NVIDIA Megatron-LM. # The inverse operation is performed inside Megatron-LM to read checkpoints: # https://github.com/NVIDIA/Megatron-LM/blob/v2.4/megatron/checkpointing.py#L209 # If param is the weight tensor of the self-attention block, the returned tensor # will have to be transposed one more time to be read by HuggingFace GPT2. lowerCAmelCase = param.size() if checkpoint_version == 1.0: # version 1.0 stores [num_heads * hidden_size * num_splits, :] lowerCAmelCase = (num_heads, hidden_size, num_splits) + input_shape[1:] lowerCAmelCase = param.view(*lowerCamelCase ) lowerCAmelCase = param.transpose(0 , 2 ) lowerCAmelCase = param.transpose(1 , 2 ).contiguous() elif checkpoint_version >= 2.0: # other versions store [num_heads * num_splits * hidden_size, :] lowerCAmelCase = (num_heads, num_splits, hidden_size) + input_shape[1:] lowerCAmelCase = param.view(*lowerCamelCase ) lowerCAmelCase = param.transpose(0 , 1 ).contiguous() lowerCAmelCase = param.view(*lowerCamelCase ) return param def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Optional[int] , lowerCamelCase : str ): # The converted output model. lowerCAmelCase = {} # old versions did not store training args lowerCAmelCase = input_state_dict.get('args' , lowerCamelCase ) if ds_args is not None: # do not make the user write a config file when the exact dimensions/sizes are already in the checkpoint # from pprint import pprint # pprint(vars(ds_args)) lowerCAmelCase = ds_args.padded_vocab_size lowerCAmelCase = ds_args.max_position_embeddings lowerCAmelCase = ds_args.hidden_size lowerCAmelCase = ds_args.num_layers lowerCAmelCase = ds_args.num_attention_heads lowerCAmelCase = ds_args.ffn_hidden_size # pprint(config) # The number of heads. lowerCAmelCase = config.n_head # The hidden_size per head. lowerCAmelCase = config.n_embd // config.n_head # Megatron-LM checkpoint version if "checkpoint_version" in input_state_dict.keys(): lowerCAmelCase = input_state_dict['checkpoint_version'] else: lowerCAmelCase = 0.0 # The model. lowerCAmelCase = input_state_dict['model'] # The language model. lowerCAmelCase = model['language_model'] # The embeddings. lowerCAmelCase = lm['embedding'] # The word embeddings. lowerCAmelCase = embeddings['word_embeddings']['weight'] # Truncate the embedding table to vocab_size rows. lowerCAmelCase = word_embeddings[: config.vocab_size, :] lowerCAmelCase = word_embeddings # The position embeddings. lowerCAmelCase = embeddings['position_embeddings']['weight'] # Read the causal mask dimension (seqlen). [max_sequence_length, hidden_size] lowerCAmelCase = pos_embeddings.size(0 ) if n_positions != config.n_positions: raise ValueError( f'''pos_embeddings.max_sequence_length={n_positions} and config.n_positions={config.n_positions} don\'t match''' ) # Store the position embeddings. lowerCAmelCase = pos_embeddings # The transformer. lowerCAmelCase = lm['transformer'] if 'transformer' in lm.keys() else lm['encoder'] # The regex to extract layer names. lowerCAmelCase = re.compile(R'layers\.(\d+)\.([a-z0-9_.]+)\.([a-z]+)' ) # The simple map of names for "automated" rules. lowerCAmelCase = { 'attention.dense': '.attn.c_proj.', 'self_attention.dense': '.attn.c_proj.', 'mlp.dense_h_to_4h': '.mlp.c_fc.', 'mlp.dense_4h_to_h': '.mlp.c_proj.', } # Extract the layers. for key, val in transformer.items(): # Match the name. lowerCAmelCase = layer_re.match(lowerCamelCase ) # Stop if that's not a layer if m is None: break # The index of the layer. lowerCAmelCase = int(m.group(1 ) ) # The name of the operation. lowerCAmelCase = m.group(2 ) # Is it a weight or a bias? lowerCAmelCase = m.group(3 ) # The name of the layer. lowerCAmelCase = f'''transformer.h.{layer_idx}''' # For layernorm(s), simply store the layer norm. if op_name.endswith('layernorm' ): lowerCAmelCase = 'ln_1' if op_name.startswith('input' ) else 'ln_2' lowerCAmelCase = val # Transpose the QKV matrix. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "weight": # Insert a tensor of 1x1xDxD bias. lowerCAmelCase = torch.tril(torch.ones((n_positions, n_positions) , dtype=torch.floataa ) ).view( 1 , 1 , lowerCamelCase , lowerCamelCase ) lowerCAmelCase = causal_mask # Insert a "dummy" tensor for masked_bias. lowerCAmelCase = torch.tensor(-1e4 , dtype=torch.floataa ) lowerCAmelCase = masked_bias lowerCAmelCase = fix_query_key_value_ordering(lowerCamelCase , lowerCamelCase , 3 , lowerCamelCase , lowerCamelCase ) # Megatron stores (3*D) x D but transformers-GPT2 expects D x 3*D. lowerCAmelCase = out_val.transpose(0 , 1 ).contiguous() # Store. lowerCAmelCase = out_val # Transpose the bias. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "bias": lowerCAmelCase = fix_query_key_value_ordering(lowerCamelCase , lowerCamelCase , 3 , lowerCamelCase , lowerCamelCase ) # Store. No change of shape. lowerCAmelCase = out_val # Transpose the weights. elif weight_or_bias == "weight": lowerCAmelCase = megatron_to_transformers[op_name] lowerCAmelCase = val.transpose(0 , 1 ) # Copy the bias. elif weight_or_bias == "bias": lowerCAmelCase = megatron_to_transformers[op_name] lowerCAmelCase = val # DEBUG. assert config.n_layer == layer_idx + 1 # The final layernorm. lowerCAmelCase = transformer['final_layernorm.weight'] lowerCAmelCase = transformer['final_layernorm.bias'] # For LM head, transformers' wants the matrix to weight embeddings. lowerCAmelCase = word_embeddings # It should be done! return output_state_dict def a_ ( ): # Create the argument parser. lowerCAmelCase = argparse.ArgumentParser() parser.add_argument('--print-checkpoint-structure' , action='store_true' ) parser.add_argument( 'path_to_checkpoint' , type=lowerCamelCase , help='Path to the checkpoint file (.zip archive or direct .pt file)' , ) parser.add_argument( '--config_file' , default='' , type=lowerCamelCase , help='An optional config json file describing the pre-trained model.' , ) lowerCAmelCase = parser.parse_args() # Extract the basename. lowerCAmelCase = os.path.dirname(args.path_to_checkpoint ) # Load the model. # the .zip is very optional, let's keep it for backward compatibility print(f'''Extracting PyTorch state dictionary from {args.path_to_checkpoint}''' ) if args.path_to_checkpoint.endswith('.zip' ): with zipfile.ZipFile(args.path_to_checkpoint , 'r' ) as checkpoint: with checkpoint.open('release/mp_rank_00/model_optim_rng.pt' ) as pytorch_dict: lowerCAmelCase = torch.load(lowerCamelCase , map_location='cpu' ) else: lowerCAmelCase = torch.load(args.path_to_checkpoint , map_location='cpu' ) lowerCAmelCase = input_state_dict.get('args' , lowerCamelCase ) # Read the config, or default to the model released by NVIDIA. if args.config_file == "": if ds_args is not None: if ds_args.bias_gelu_fusion: lowerCAmelCase = 'gelu_fast' elif ds_args.openai_gelu: lowerCAmelCase = 'gelu_new' else: lowerCAmelCase = 'gelu' else: # in the very early days this used to be "gelu_new" lowerCAmelCase = 'gelu_new' # Spell out all parameters in case the defaults change. lowerCAmelCase = GPTaConfig( vocab_size=50257 , n_positions=1024 , n_embd=1024 , n_layer=24 , n_head=16 , n_inner=4096 , activation_function=lowerCamelCase , resid_pdrop=0.1 , embd_pdrop=0.1 , attn_pdrop=0.1 , layer_norm_epsilon=1e-5 , initializer_range=0.02 , summary_type='cls_index' , summary_use_proj=lowerCamelCase , summary_activation=lowerCamelCase , summary_proj_to_labels=lowerCamelCase , summary_first_dropout=0.1 , scale_attn_weights=lowerCamelCase , use_cache=lowerCamelCase , bos_token_id=50256 , eos_token_id=50256 , ) else: lowerCAmelCase = GPTaConfig.from_json_file(args.config_file ) lowerCAmelCase = ['GPT2LMHeadModel'] # Convert. print('Converting' ) lowerCAmelCase = convert_megatron_checkpoint(lowerCamelCase , lowerCamelCase , lowerCamelCase ) # Print the structure of converted state dict. if args.print_checkpoint_structure: recursive_print(lowerCamelCase , lowerCamelCase ) # Add tokenizer class info to config # see https://github.com/huggingface/transformers/issues/13906) if ds_args is not None: lowerCAmelCase = ds_args.tokenizer_type if tokenizer_type == "GPT2BPETokenizer": lowerCAmelCase = 'gpt2' elif tokenizer_type == "PretrainedFromHF": lowerCAmelCase = ds_args.tokenizer_name_or_path else: raise ValueError(f'''Unrecognized tokenizer_type {tokenizer_type}''' ) else: lowerCAmelCase = 'gpt2' lowerCAmelCase = AutoTokenizer.from_pretrained(lowerCamelCase ) lowerCAmelCase = type(lowerCamelCase ).__name__ lowerCAmelCase = tokenizer_class # Store the config to file. print('Saving config' ) config.save_pretrained(lowerCamelCase ) # Save tokenizer based on args print(f'''Adding {tokenizer_class} tokenizer files''' ) tokenizer.save_pretrained(lowerCamelCase ) # Store the state_dict to file. lowerCAmelCase = os.path.join(lowerCamelCase , 'pytorch_model.bin' ) print(f'''Saving checkpoint to "{output_checkpoint_file}"''' ) torch.save(lowerCamelCase , lowerCamelCase ) #################################################################################################### if __name__ == "__main__": main() ####################################################################################################
4
1
'''simple docstring''' import io import itertools import json from dataclasses import dataclass from typing import Optional import pyarrow as pa import pyarrow.json as paj import datasets from datasets.table import table_cast from datasets.utils.file_utils import readline __snake_case =datasets.utils.logging.get_logger(__name__) @dataclass class UpperCAmelCase_ ( datasets.BuilderConfig ): lowerCamelCase : Optional[datasets.Features] = None lowerCamelCase : str = "utf-8" lowerCamelCase : Optional[str] = None lowerCamelCase : Optional[str] = None lowerCamelCase : bool = True # deprecated lowerCamelCase : Optional[int] = None # deprecated lowerCamelCase : int = 10 << 20 # 10MB lowerCamelCase : Optional[bool] = None class UpperCAmelCase_ ( datasets.ArrowBasedBuilder ): lowerCamelCase : int = JsonConfig def __UpperCAmelCase ( self : List[str] ) -> Union[str, Any]: if self.config.block_size is not None: logger.warning('The JSON loader parameter `block_size` is deprecated. Please use `chunksize` instead' ) lowerCAmelCase = self.config.block_size if self.config.use_threads is not True: logger.warning( 'The JSON loader parameter `use_threads` is deprecated and doesn\'t have any effect anymore.' ) if self.config.newlines_in_values is not None: raise ValueError('The JSON loader parameter `newlines_in_values` is no longer supported' ) return datasets.DatasetInfo(features=self.config.features ) def __UpperCAmelCase ( self : Optional[Any] , UpperCAmelCase__ : Union[str, Any] ) -> Any: if not self.config.data_files: raise ValueError(F'''At least one data file must be specified, but got data_files={self.config.data_files}''' ) lowerCAmelCase = dl_manager.download_and_extract(self.config.data_files ) if isinstance(UpperCAmelCase__ , (str, list, tuple) ): lowerCAmelCase = data_files if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): lowerCAmelCase = [files] lowerCAmelCase = [dl_manager.iter_files(UpperCAmelCase__ ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'files': files} )] lowerCAmelCase = [] for split_name, files in data_files.items(): if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): lowerCAmelCase = [files] lowerCAmelCase = [dl_manager.iter_files(UpperCAmelCase__ ) for file in files] splits.append(datasets.SplitGenerator(name=UpperCAmelCase__ , gen_kwargs={'files': files} ) ) return splits def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : pa.Table ) -> pa.Table: if self.config.features is not None: # adding missing columns for column_name in set(self.config.features ) - set(pa_table.column_names ): lowerCAmelCase = self.config.features.arrow_schema.field(UpperCAmelCase__ ).type lowerCAmelCase = pa_table.append_column(UpperCAmelCase__ , pa.array([None] * len(UpperCAmelCase__ ) , type=UpperCAmelCase__ ) ) # more expensive cast to support nested structures with keys in a different order # allows str <-> int/float or str to Audio for example lowerCAmelCase = table_cast(UpperCAmelCase__ , self.config.features.arrow_schema ) return pa_table def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : List[str] ) -> Any: for file_idx, file in enumerate(itertools.chain.from_iterable(UpperCAmelCase__ ) ): # If the file is one json object and if we need to look at the list of items in one specific field if self.config.field is not None: with open(UpperCAmelCase__ , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: lowerCAmelCase = json.load(UpperCAmelCase__ ) # We keep only the field we are interested in lowerCAmelCase = dataset[self.config.field] # We accept two format: a list of dicts or a dict of lists if isinstance(UpperCAmelCase__ , (list, tuple) ): lowerCAmelCase = set().union(*[row.keys() for row in dataset] ) lowerCAmelCase = {col: [row.get(UpperCAmelCase__ ) for row in dataset] for col in keys} else: lowerCAmelCase = dataset lowerCAmelCase = pa.Table.from_pydict(UpperCAmelCase__ ) yield file_idx, self._cast_table(UpperCAmelCase__ ) # If the file has one json object per line else: with open(UpperCAmelCase__ , 'rb' ) as f: lowerCAmelCase = 0 # Use block_size equal to the chunk size divided by 32 to leverage multithreading # Set a default minimum value of 16kB if the chunk size is really small lowerCAmelCase = max(self.config.chunksize // 3_2 , 1_6 << 1_0 ) lowerCAmelCase = ( self.config.encoding_errors if self.config.encoding_errors is not None else 'strict' ) while True: lowerCAmelCase = f.read(self.config.chunksize ) if not batch: break # Finish current line try: batch += f.readline() except (AttributeError, io.UnsupportedOperation): batch += readline(UpperCAmelCase__ ) # PyArrow only accepts utf-8 encoded bytes if self.config.encoding != "utf-8": lowerCAmelCase = batch.decode(self.config.encoding , errors=UpperCAmelCase__ ).encode('utf-8' ) try: while True: try: lowerCAmelCase = paj.read_json( io.BytesIO(UpperCAmelCase__ ) , read_options=paj.ReadOptions(block_size=UpperCAmelCase__ ) ) break except (pa.ArrowInvalid, pa.ArrowNotImplementedError) as e: if ( isinstance(UpperCAmelCase__ , pa.ArrowInvalid ) and "straddling" not in str(UpperCAmelCase__ ) or block_size > len(UpperCAmelCase__ ) ): raise else: # Increase the block size in case it was too small. # The block size will be reset for the next file. logger.debug( F'''Batch of {len(UpperCAmelCase__ )} bytes couldn\'t be parsed with block_size={block_size}. Retrying with block_size={block_size * 2}.''' ) block_size *= 2 except pa.ArrowInvalid as e: try: with open( UpperCAmelCase__ , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: lowerCAmelCase = json.load(UpperCAmelCase__ ) except json.JSONDecodeError: logger.error(F'''Failed to read file \'{file}\' with error {type(UpperCAmelCase__ )}: {e}''' ) raise e # If possible, parse the file as a list of json objects and exit the loop if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): # list is the only sequence type supported in JSON try: lowerCAmelCase = set().union(*[row.keys() for row in dataset] ) lowerCAmelCase = {col: [row.get(UpperCAmelCase__ ) for row in dataset] for col in keys} lowerCAmelCase = pa.Table.from_pydict(UpperCAmelCase__ ) except (pa.ArrowInvalid, AttributeError) as e: logger.error(F'''Failed to read file \'{file}\' with error {type(UpperCAmelCase__ )}: {e}''' ) raise ValueError(F'''Not able to read records in the JSON file at {file}.''' ) from None yield file_idx, self._cast_table(UpperCAmelCase__ ) break else: logger.error(F'''Failed to read file \'{file}\' with error {type(UpperCAmelCase__ )}: {e}''' ) raise ValueError( F'''Not able to read records in the JSON file at {file}. ''' F'''You should probably indicate the field of the JSON file containing your records. ''' F'''This JSON file contain the following fields: {str(list(dataset.keys() ) )}. ''' F'''Select the correct one and provide it as `field=\'XXX\'` to the dataset loading method. ''' ) from None # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield (file_idx, batch_idx), self._cast_table(UpperCAmelCase__ ) batch_idx += 1
4
'''simple docstring''' from __future__ import annotations from typing import Any class UpperCAmelCase_ : def __init__( self : Optional[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : float = 0 ) -> None: lowerCAmelCase , lowerCAmelCase = row, column lowerCAmelCase = [[default_value for c in range(UpperCAmelCase__ )] for r in range(UpperCAmelCase__ )] def __str__( self : List[str] ) -> str: lowerCAmelCase = F'''Matrix consist of {self.row} rows and {self.column} columns\n''' # Make string identifier lowerCAmelCase = 0 for row_vector in self.array: for obj in row_vector: lowerCAmelCase = max(UpperCAmelCase__ , len(str(UpperCAmelCase__ ) ) ) lowerCAmelCase = F'''%{max_element_length}s''' # Make string and return def single_line(UpperCAmelCase__ : list[float] ) -> str: nonlocal string_format_identifier lowerCAmelCase = '[' line += ", ".join(string_format_identifier % (obj,) for obj in row_vector ) line += "]" return line s += "\n".join(single_line(UpperCAmelCase__ ) for row_vector in self.array ) return s def __repr__( self : List[str] ) -> str: return str(self ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : tuple[int, int] ) -> bool: if not (isinstance(UpperCAmelCase__ , (list, tuple) ) and len(UpperCAmelCase__ ) == 2): return False elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False else: return True def __getitem__( self : Any , UpperCAmelCase__ : tuple[int, int] ) -> Any: assert self.validate_indicies(UpperCAmelCase__ ) return self.array[loc[0]][loc[1]] def __setitem__( self : Dict , UpperCAmelCase__ : tuple[int, int] , UpperCAmelCase__ : float ) -> None: assert self.validate_indicies(UpperCAmelCase__ ) lowerCAmelCase = value def __add__( self : Any , UpperCAmelCase__ : Matrix ) -> Matrix: assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) assert self.row == another.row and self.column == another.column # Add lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] + another[r, c] return result def __neg__( self : int ) -> Matrix: lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = -self[r, c] return result def __sub__( self : str , UpperCAmelCase__ : Matrix ) -> Matrix: return self + (-another) def __mul__( self : str , UpperCAmelCase__ : int | float | Matrix ) -> Matrix: if isinstance(UpperCAmelCase__ , (int, float) ): # Scalar multiplication lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] * another return result elif isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): # Matrix multiplication assert self.column == another.row lowerCAmelCase = Matrix(self.row , another.column ) for r in range(self.row ): for c in range(another.column ): for i in range(self.column ): result[r, c] += self[r, i] * another[i, c] return result else: lowerCAmelCase = F'''Unsupported type given for another ({type(UpperCAmelCase__ )})''' raise TypeError(UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[Any] ) -> Matrix: lowerCAmelCase = Matrix(self.column , self.row ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] return result def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : Matrix , UpperCAmelCase__ : Matrix ) -> Any: assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) and isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) assert self.row == self.column == u.row == v.row # u, v should be column vector assert u.column == v.column == 1 # u, v should be column vector # Calculate lowerCAmelCase = v.transpose() lowerCAmelCase = (v_t * self * u)[0, 0] + 1 if numerator_factor == 0: return None # It's not invertable return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor)) # Testing if __name__ == "__main__": def a_ ( ): # a^(-1) lowerCAmelCase = Matrix(3 , 3 , 0 ) for i in range(3 ): lowerCAmelCase = 1 print(f'''a^(-1) is {ainv}''' ) # u, v lowerCAmelCase = Matrix(3 , 1 , 0 ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = 1, 2, -3 lowerCAmelCase = Matrix(3 , 1 , 0 ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = 4, -2, 5 print(f'''u is {u}''' ) print(f'''v is {v}''' ) print(f'''uv^T is {u * v.transpose()}''' ) # Sherman Morrison print(f'''(a + uv^T)^(-1) is {ainv.sherman_morrison(lowerCamelCase , lowerCamelCase )}''' ) def a_ ( ): import doctest doctest.testmod() testa()
4
1
'''simple docstring''' __snake_case ={ """A""": ["""B""", """C""", """E"""], """B""": ["""A""", """D""", """E"""], """C""": ["""A""", """F""", """G"""], """D""": ["""B"""], """E""": ["""A""", """B""", """D"""], """F""": ["""C"""], """G""": ["""C"""], } def a_ ( lowerCamelCase : dict , lowerCamelCase : List[str] , lowerCamelCase : Optional[int] ): lowerCAmelCase = set() # keep track of all the paths to be checked lowerCAmelCase = [[start]] # return path if start is goal if start == goal: return [start] # keeps looping until all possible paths have been checked while queue: # pop the first path from the queue lowerCAmelCase = queue.pop(0 ) # get the last node from the path lowerCAmelCase = path[-1] if node not in explored: lowerCAmelCase = graph[node] # go through all neighbour nodes, construct a new path and # push it into the queue for neighbour in neighbours: lowerCAmelCase = list(lowerCamelCase ) new_path.append(lowerCamelCase ) queue.append(lowerCamelCase ) # return path if neighbour is goal if neighbour == goal: return new_path # mark node as explored explored.add(lowerCamelCase ) # in case there's no path between the 2 nodes return [] def a_ ( lowerCamelCase : dict , lowerCamelCase : List[Any] , lowerCamelCase : List[Any] ): if not graph or start not in graph or target not in graph: return -1 if start == target: return 0 lowerCAmelCase = [start] lowerCAmelCase = set(lowerCamelCase ) # Keep tab on distances from `start` node. lowerCAmelCase = {start: 0, target: -1} while queue: lowerCAmelCase = queue.pop(0 ) if node == target: lowerCAmelCase = ( dist[node] if dist[target] == -1 else min(dist[target] , dist[node] ) ) for adjacent in graph[node]: if adjacent not in visited: visited.add(lowerCamelCase ) queue.append(lowerCamelCase ) lowerCAmelCase = dist[node] + 1 return dist[target] if __name__ == "__main__": print(bfs_shortest_path(demo_graph, """G""", """D""")) # returns ['G', 'C', 'A', 'B', 'D'] print(bfs_shortest_path_distance(demo_graph, """G""", """D""")) # returns 4
4
'''simple docstring''' class UpperCAmelCase_ : def __init__( self : List[str] , UpperCAmelCase__ : list[int] ) -> None: lowerCAmelCase = len(UpperCAmelCase__ ) lowerCAmelCase = [0] * len_array if len_array > 0: lowerCAmelCase = array[0] for i in range(1 , UpperCAmelCase__ ): lowerCAmelCase = self.prefix_sum[i - 1] + array[i] def __UpperCAmelCase ( self : Optional[Any] , UpperCAmelCase__ : int , UpperCAmelCase__ : int ) -> int: if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def __UpperCAmelCase ( self : int , UpperCAmelCase__ : int ) -> bool: lowerCAmelCase = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(UpperCAmelCase__ ) return False if __name__ == "__main__": import doctest doctest.testmod()
4
1
'''simple docstring''' import inspect import jax import jax.lax as lax import jax.numpy as jnp from ..utils import add_start_docstrings from ..utils.logging import get_logger __snake_case =get_logger(__name__) __snake_case =R""" Args: input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`): Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search kwargs (`Dict[str, Any]`, *optional*): Additional logits processor specific kwargs. Return: `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores. """ class UpperCAmelCase_ : @add_start_docstrings(UpperCAmelCase__ ) def __call__( self : Dict , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : jnp.ndarray ) -> jnp.ndarray: raise NotImplementedError( F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) class UpperCAmelCase_ : @add_start_docstrings(UpperCAmelCase__ ) def __call__( self : str , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : jnp.ndarray ) -> jnp.ndarray: raise NotImplementedError( F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) class UpperCAmelCase_ ( __lowercase ): @add_start_docstrings(UpperCAmelCase__ ) def __call__( self : int , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : int , **UpperCAmelCase__ : Tuple ) -> jnp.ndarray: for processor in self: lowerCAmelCase = inspect.signature(processor.__call__ ).parameters if len(UpperCAmelCase__ ) > 3: if not all(arg in kwargs for arg in list(function_args.keys() )[2:] ): raise ValueError( F'''Make sure that all the required parameters: {list(function_args.keys() )} for ''' F'''{processor.__class__} are passed to the logits processor.''' ) lowerCAmelCase = processor(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , **UpperCAmelCase__ ) else: lowerCAmelCase = processor(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) return scores class UpperCAmelCase_ ( __lowercase ): def __init__( self : Union[str, Any] , UpperCAmelCase__ : float ) -> int: if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) or not (temperature > 0): raise ValueError(F'''`temperature` has to be a strictly positive float, but is {temperature}''' ) lowerCAmelCase = temperature def __call__( self : Any , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : int ) -> jnp.ndarray: lowerCAmelCase = scores / self.temperature return scores class UpperCAmelCase_ ( __lowercase ): def __init__( self : List[str] , UpperCAmelCase__ : float , UpperCAmelCase__ : float = -float('Inf' ) , UpperCAmelCase__ : int = 1 ) -> Optional[int]: if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) or (top_p < 0 or top_p > 1.0): raise ValueError(F'''`top_p` has to be a float > 0 and < 1, but is {top_p}''' ) if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) or (min_tokens_to_keep < 1): raise ValueError(F'''`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}''' ) lowerCAmelCase = top_p lowerCAmelCase = filter_value lowerCAmelCase = min_tokens_to_keep def __call__( self : str , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : int ) -> jnp.ndarray: lowerCAmelCase , lowerCAmelCase = lax.top_k(UpperCAmelCase__ , scores.shape[-1] ) lowerCAmelCase = jnp.full_like(UpperCAmelCase__ , self.filter_value ) lowerCAmelCase = jax.nn.softmax(UpperCAmelCase__ , axis=-1 ).cumsum(axis=-1 ) lowerCAmelCase = cumulative_probs < self.top_p # include the token that is higher than top_p as well lowerCAmelCase = jnp.roll(UpperCAmelCase__ , 1 ) score_mask |= score_mask.at[:, 0].set(UpperCAmelCase__ ) # min tokens to keep lowerCAmelCase = score_mask.at[:, : self.min_tokens_to_keep].set(UpperCAmelCase__ ) lowerCAmelCase = jnp.where(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = jax.lax.sort_key_val(UpperCAmelCase__ , UpperCAmelCase__ )[-1] return next_scores class UpperCAmelCase_ ( __lowercase ): def __init__( self : List[str] , UpperCAmelCase__ : int , UpperCAmelCase__ : float = -float('Inf' ) , UpperCAmelCase__ : int = 1 ) -> Any: if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) or top_k <= 0: raise ValueError(F'''`top_k` has to be a strictly positive integer, but is {top_k}''' ) lowerCAmelCase = max(UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = filter_value def __call__( self : int , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : int ) -> jnp.ndarray: lowerCAmelCase , lowerCAmelCase = scores.shape lowerCAmelCase = jnp.full(batch_size * vocab_size , self.filter_value ) lowerCAmelCase = min(self.top_k , scores.shape[-1] ) # Safety check lowerCAmelCase , lowerCAmelCase = lax.top_k(UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = jnp.broadcast_to((jnp.arange(UpperCAmelCase__ ) * vocab_size)[:, None] , (batch_size, topk) ).flatten() lowerCAmelCase = topk_scores.flatten() lowerCAmelCase = topk_indices.flatten() + shift lowerCAmelCase = next_scores_flat.at[topk_indices_flat].set(UpperCAmelCase__ ) lowerCAmelCase = next_scores_flat.reshape(UpperCAmelCase__ , UpperCAmelCase__ ) return next_scores class UpperCAmelCase_ ( __lowercase ): def __init__( self : Optional[int] , UpperCAmelCase__ : int ) -> str: lowerCAmelCase = bos_token_id def __call__( self : Dict , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : int ) -> jnp.ndarray: lowerCAmelCase = jnp.full(scores.shape , -float('inf' ) ) lowerCAmelCase = 1 - jnp.bool_(cur_len - 1 ) lowerCAmelCase = jnp.where(UpperCAmelCase__ , new_scores.at[:, self.bos_token_id].set(0 ) , UpperCAmelCase__ ) return scores class UpperCAmelCase_ ( __lowercase ): def __init__( self : int , UpperCAmelCase__ : int , UpperCAmelCase__ : int ) -> List[Any]: lowerCAmelCase = max_length lowerCAmelCase = eos_token_id def __call__( self : str , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : int ) -> jnp.ndarray: lowerCAmelCase = jnp.full(scores.shape , -float('inf' ) ) lowerCAmelCase = 1 - jnp.bool_(cur_len - self.max_length + 1 ) lowerCAmelCase = jnp.where(UpperCAmelCase__ , new_scores.at[:, self.eos_token_id].set(0 ) , UpperCAmelCase__ ) return scores class UpperCAmelCase_ ( __lowercase ): def __init__( self : Optional[Any] , UpperCAmelCase__ : int , UpperCAmelCase__ : int ) -> str: if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) or min_length < 0: raise ValueError(F'''`min_length` has to be a positive integer, but is {min_length}''' ) if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) or eos_token_id < 0: raise ValueError(F'''`eos_token_id` has to be a positive integer, but is {eos_token_id}''' ) lowerCAmelCase = min_length lowerCAmelCase = eos_token_id def __call__( self : Union[str, Any] , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : int ) -> jnp.ndarray: # create boolean flag to decide if min length penalty should be applied lowerCAmelCase = 1 - jnp.clip(cur_len - self.min_length , 0 , 1 ) lowerCAmelCase = jnp.where(UpperCAmelCase__ , scores.at[:, self.eos_token_id].set(-float('inf' ) ) , UpperCAmelCase__ ) return scores class UpperCAmelCase_ ( __lowercase ): def __init__( self : Tuple , UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : List[str] ) -> List[str]: lowerCAmelCase = list(UpperCAmelCase__ ) lowerCAmelCase = begin_index def __call__( self : Optional[Any] , UpperCAmelCase__ : List[str] , UpperCAmelCase__ : int , UpperCAmelCase__ : int ) -> List[Any]: lowerCAmelCase = 1 - jnp.bool_(cur_len - self.begin_index ) lowerCAmelCase = jnp.where(UpperCAmelCase__ , scores.at[:, self.begin_suppress_tokens].set(-float('inf' ) ) , UpperCAmelCase__ ) return scores class UpperCAmelCase_ ( __lowercase ): def __init__( self : List[Any] , UpperCAmelCase__ : list ) -> List[str]: lowerCAmelCase = list(UpperCAmelCase__ ) def __call__( self : Optional[int] , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : int ) -> jnp.ndarray: lowerCAmelCase = scores.at[..., self.suppress_tokens].set(-float('inf' ) ) return scores class UpperCAmelCase_ ( __lowercase ): def __init__( self : Optional[Any] , UpperCAmelCase__ : List[Any] ) -> List[str]: lowerCAmelCase = dict(UpperCAmelCase__ ) # Converts the dictionary of format {index: token} containing the tokens to be forced to an array, where the # index of the array corresponds to the index of the token to be forced, for XLA compatibility. # Indexes without forced tokens will have a negative value. lowerCAmelCase = jnp.ones((max(force_token_map.keys() ) + 1) , dtype=jnp.intaa ) * -1 for index, token in force_token_map.items(): if token is not None: lowerCAmelCase = force_token_array.at[index].set(UpperCAmelCase__ ) lowerCAmelCase = jnp.intaa(UpperCAmelCase__ ) def __call__( self : Union[str, Any] , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : jnp.ndarray , UpperCAmelCase__ : int ) -> jnp.ndarray: def _force_token(UpperCAmelCase__ : Optional[int] ): lowerCAmelCase = scores.shape[0] lowerCAmelCase = self.force_token_array[generation_idx] lowerCAmelCase = jnp.ones_like(UpperCAmelCase__ , dtype=scores.dtype ) * -float('inf' ) lowerCAmelCase = jnp.zeros((batch_size, 1) , dtype=scores.dtype ) lowerCAmelCase = lax.dynamic_update_slice(UpperCAmelCase__ , UpperCAmelCase__ , (0, current_token) ) return new_scores lowerCAmelCase = lax.cond( cur_len >= self.force_token_array.shape[0] , lambda: scores , lambda: lax.cond( self.force_token_array[cur_len] >= 0 , lambda: _force_token(UpperCAmelCase__ ) , lambda: scores , ) , ) return scores class UpperCAmelCase_ ( __lowercase ): def __init__( self : Union[str, Any] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : int , UpperCAmelCase__ : List[str] ) -> List[str]: lowerCAmelCase = generate_config.eos_token_id lowerCAmelCase = generate_config.no_timestamps_token_id lowerCAmelCase = generate_config.no_timestamps_token_id + 1 lowerCAmelCase = decoder_input_length + 1 if generate_config.is_multilingual: # room for language token and task token self.begin_index += 2 if hasattr(UpperCAmelCase__ , 'max_initial_timestamp_index' ): lowerCAmelCase = generate_config.max_initial_timestamp_index else: lowerCAmelCase = model_config.vocab_size if self.max_initial_timestamp_index is None: lowerCAmelCase = model_config.vocab_size def __call__( self : List[Any] , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Optional[int] ) -> Union[str, Any]: # suppress <|notimestamps|> which is handled by without_timestamps lowerCAmelCase = scores.at[:, self.no_timestamps_token_id].set(-float('inf' ) ) def handle_pairs(UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : str ): lowerCAmelCase = jnp.where((cur_len - self.begin_index) >= 1 , UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = jnp.where( input_ids_k[cur_len - 1] >= self.timestamp_begin , True and last_was_timestamp , UpperCAmelCase__ , ) lowerCAmelCase = jnp.where((cur_len - self.begin_index) < 2 , UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = jnp.where( input_ids_k[cur_len - 2] >= self.timestamp_begin , UpperCAmelCase__ , UpperCAmelCase__ , ) return jnp.where( UpperCAmelCase__ , jnp.where( penultimate_was_timestamp > 0 , scores_k.at[self.timestamp_begin :].set(-float('inf' ) ) , scores_k.at[: self.eos_token_id].set(-float('inf' ) ) , ) , UpperCAmelCase__ , ) lowerCAmelCase = jax.vmap(UpperCAmelCase__ )(UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = jnp.where(cur_len == self.begin_index , UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase = jnp.where( self.max_initial_timestamp_index is not None , True and apply_max_initial_timestamp , UpperCAmelCase__ , ) lowerCAmelCase = self.timestamp_begin + self.max_initial_timestamp_index lowerCAmelCase = jnp.where( UpperCAmelCase__ , scores.at[:, last_allowed + 1 :].set(-float('inf' ) ) , UpperCAmelCase__ , ) # if sum of probability over timestamps is above any other token, sample timestamp lowerCAmelCase = jax.nn.log_softmax(UpperCAmelCase__ , axis=-1 ) def handle_cumulative_probs(UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : str ): lowerCAmelCase = jax.nn.logsumexp(logprobs_k[self.timestamp_begin :] , axis=-1 ) lowerCAmelCase = jnp.max(logprobs_k[: self.timestamp_begin] ) return jnp.where( timestamp_logprob > max_text_token_logprob , scores_k.at[: self.timestamp_begin].set(-float('inf' ) ) , UpperCAmelCase__ , ) lowerCAmelCase = jax.vmap(UpperCAmelCase__ )(UpperCAmelCase__ , UpperCAmelCase__ ) return scores
4
'''simple docstring''' def a_ ( lowerCamelCase : Optional[Any] ): return [ { 0: [1, 2], 1: [0, 2], 2: [0, 1, 3, 5], 3: [2, 4], 4: [3], 5: [2, 6, 8], 6: [5, 7], 7: [6, 8], 8: [5, 7], }, { 0: [6], 1: [9], 2: [4, 5], 3: [4], 4: [2, 3], 5: [2], 6: [0, 7], 7: [6], 8: [], 9: [1], }, { 0: [4], 1: [6], 2: [], 3: [5, 6, 7], 4: [0, 6], 5: [3, 8, 9], 6: [1, 3, 4, 7], 7: [3, 6, 8, 9], 8: [5, 7], 9: [5, 7], }, { 0: [1, 3], 1: [0, 2, 4], 2: [1, 3, 4], 3: [0, 2, 4], 4: [1, 2, 3], }, ][index] def a_ ( lowerCamelCase : dict[int, list[int]] ): lowerCAmelCase = 0 lowerCAmelCase = len(lowerCamelCase ) # No of vertices in graph lowerCAmelCase = [0] * n lowerCAmelCase = [False] * n def dfs(lowerCamelCase : Tuple , lowerCamelCase : str , lowerCamelCase : Dict , lowerCamelCase : str ): lowerCAmelCase = True lowerCAmelCase = id_ id_ += 1 for to in graph[at]: if to == parent: pass elif not visited[to]: dfs(lowerCamelCase , lowerCamelCase , lowerCamelCase , id_ ) lowerCAmelCase = min(low[at] , low[to] ) if id_ <= low[to]: bridges.append((at, to) if at < to else (to, at) ) else: # This edge is a back edge and cannot be a bridge lowerCAmelCase = min(low[at] , low[to] ) lowerCAmelCase = [] for i in range(lowerCamelCase ): if not visited[i]: dfs(lowerCamelCase , -1 , lowerCamelCase , id_ ) return bridges if __name__ == "__main__": import doctest doctest.testmod()
4
1
'''simple docstring''' import argparse from typing import List import evaluate import numpy as np import torch from datasets import DatasetDict, load_dataset # New Code # # We'll be using StratifiedKFold for this example from sklearn.model_selection import StratifiedKFold from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to perform Cross Validation, # and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## __snake_case =16 __snake_case =32 def a_ ( lowerCamelCase : Accelerator , lowerCamelCase : DatasetDict , lowerCamelCase : List[int] , lowerCamelCase : List[int] , lowerCamelCase : int = 16 ): lowerCAmelCase = AutoTokenizer.from_pretrained('bert-base-cased' ) lowerCAmelCase = DatasetDict( { 'train': dataset['train'].select(lowerCamelCase ), 'validation': dataset['train'].select(lowerCamelCase ), 'test': dataset['validation'], } ) def tokenize_function(lowerCamelCase : str ): # max_length=None => use the model max length (it's actually the default) lowerCAmelCase = 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 # starting with the main process first: with accelerator.main_process_first(): lowerCAmelCase = datasets.map( lowerCamelCase , batched=lowerCamelCase , remove_columns=['idx', 'sentence1', 'sentence2'] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library lowerCAmelCase = tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(lowerCamelCase : Dict ): # On TPU it's best to pad everything to the same length or training will be very slow. lowerCAmelCase = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": lowerCAmelCase = 16 elif accelerator.mixed_precision != "no": lowerCAmelCase = 8 else: lowerCAmelCase = None return tokenizer.pad( lowerCamelCase , padding='longest' , max_length=lowerCamelCase , pad_to_multiple_of=lowerCamelCase , return_tensors='pt' , ) # Instantiate dataloaders. lowerCAmelCase = DataLoader( tokenized_datasets['train'] , shuffle=lowerCamelCase , collate_fn=lowerCamelCase , batch_size=lowerCamelCase ) lowerCAmelCase = DataLoader( tokenized_datasets['validation'] , shuffle=lowerCamelCase , collate_fn=lowerCamelCase , batch_size=lowerCamelCase ) lowerCAmelCase = DataLoader( tokenized_datasets['test'] , shuffle=lowerCamelCase , collate_fn=lowerCamelCase , batch_size=lowerCamelCase ) return train_dataloader, eval_dataloader, test_dataloader def a_ ( lowerCamelCase : Tuple , lowerCamelCase : List[Any] ): # New Code # lowerCAmelCase = [] # Download the dataset lowerCAmelCase = load_dataset('glue' , 'mrpc' ) # Create our splits lowerCAmelCase = StratifiedKFold(n_splits=int(args.num_folds ) ) # Initialize accelerator lowerCAmelCase = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs lowerCAmelCase = config['lr'] lowerCAmelCase = int(config['num_epochs'] ) lowerCAmelCase = int(config['seed'] ) lowerCAmelCase = int(config['batch_size'] ) lowerCAmelCase = evaluate.load('glue' , 'mrpc' ) # If the batch size is too big we use gradient accumulation lowerCAmelCase = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: lowerCAmelCase = batch_size // MAX_GPU_BATCH_SIZE lowerCAmelCase = MAX_GPU_BATCH_SIZE set_seed(lowerCamelCase ) # New Code # # Create our folds: lowerCAmelCase = kfold.split(np.zeros(datasets['train'].num_rows ) , datasets['train']['label'] ) lowerCAmelCase = [] # Iterate over them for i, (train_idxs, valid_idxs) in enumerate(lowerCamelCase ): lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = get_fold_dataloaders( lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) lowerCAmelCase = AutoModelForSequenceClassification.from_pretrained('bert-base-cased' , return_dict=lowerCamelCase ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). lowerCAmelCase = model.to(accelerator.device ) # Instantiate optimizer lowerCAmelCase = AdamW(params=model.parameters() , lr=lowerCamelCase ) # Instantiate scheduler lowerCAmelCase = get_linear_schedule_with_warmup( optimizer=lowerCamelCase , num_warmup_steps=100 , num_training_steps=(len(lowerCamelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = accelerator.prepare( lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase ) # Now we train the model for epoch in range(lowerCamelCase ): model.train() for step, batch in enumerate(lowerCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) lowerCAmelCase = model(**lowerCamelCase ) lowerCAmelCase = outputs.loss lowerCAmelCase = loss / gradient_accumulation_steps accelerator.backward(lowerCamelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() 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(): lowerCAmelCase = model(**lowerCamelCase ) lowerCAmelCase = outputs.logits.argmax(dim=-1 ) lowerCAmelCase , lowerCAmelCase = accelerator.gather_for_metrics((predictions, batch['labels']) ) metric.add_batch( predictions=lowerCamelCase , references=lowerCamelCase , ) lowerCAmelCase = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f'''epoch {epoch}:''' , lowerCamelCase ) # New Code # # We also run predictions on the test set at the very end lowerCAmelCase = [] 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(): lowerCAmelCase = model(**lowerCamelCase ) lowerCAmelCase = outputs.logits lowerCAmelCase , lowerCAmelCase = accelerator.gather_for_metrics((predictions, batch['labels']) ) fold_predictions.append(predictions.cpu() ) if i == 0: # We need all of the test predictions test_references.append(references.cpu() ) # Use accelerator.print to print only on the main process. test_predictions.append(torch.cat(lowerCamelCase , dim=0 ) ) # We now need to release all our memory and get rid of the current model, optimizer, etc accelerator.free_memory() # New Code # # Finally we check the accuracy of our folded results: lowerCAmelCase = torch.cat(lowerCamelCase , dim=0 ) lowerCAmelCase = torch.stack(lowerCamelCase , dim=0 ).sum(dim=0 ).div(int(args.num_folds ) ).argmax(dim=-1 ) lowerCAmelCase = metric.compute(predictions=lowerCamelCase , references=lowerCamelCase ) accelerator.print('Average test metrics from all folds:' , lowerCamelCase ) def a_ ( ): lowerCAmelCase = argparse.ArgumentParser(description='Simple example of training script.' ) parser.add_argument( '--mixed_precision' , type=lowerCamelCase , default=lowerCamelCase , choices=['no', 'fp16', 'bf16', 'fp8'] , help='Whether to use mixed precision. Choose' 'between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.' 'and an Nvidia Ampere GPU.' , ) parser.add_argument('--cpu' , action='store_true' , help='If passed, will train on the CPU.' ) # New Code # parser.add_argument('--num_folds' , type=lowerCamelCase , default=3 , help='The number of splits to perform across the dataset' ) lowerCAmelCase = parser.parse_args() lowerCAmelCase = {'lr': 2e-5, 'num_epochs': 3, 'seed': 42, 'batch_size': 16} training_function(lowerCamelCase , lowerCamelCase ) if __name__ == "__main__": main()
4
'''simple docstring''' import argparse from collections import OrderedDict from pathlib import Path import requests import torch from PIL import Image from transformers import GLPNConfig, GLPNForDepthEstimation, GLPNImageProcessor from transformers.utils import logging logging.set_verbosity_info() __snake_case =logging.get_logger(__name__) def a_ ( lowerCamelCase : Any ): lowerCAmelCase = OrderedDict() for key, value in state_dict.items(): if key.startswith('module.encoder' ): lowerCAmelCase = key.replace('module.encoder' , 'glpn.encoder' ) if key.startswith('module.decoder' ): lowerCAmelCase = key.replace('module.decoder' , 'decoder.stages' ) if "patch_embed" in key: # replace for example patch_embed1 by patch_embeddings.0 lowerCAmelCase = key[key.find('patch_embed' ) + len('patch_embed' )] lowerCAmelCase = key.replace(f'''patch_embed{idx}''' , f'''patch_embeddings.{int(lowerCamelCase )-1}''' ) if "norm" in key: lowerCAmelCase = key.replace('norm' , 'layer_norm' ) if "glpn.encoder.layer_norm" in key: # replace for example layer_norm1 by layer_norm.0 lowerCAmelCase = key[key.find('glpn.encoder.layer_norm' ) + len('glpn.encoder.layer_norm' )] lowerCAmelCase = key.replace(f'''layer_norm{idx}''' , f'''layer_norm.{int(lowerCamelCase )-1}''' ) if "layer_norm1" in key: lowerCAmelCase = key.replace('layer_norm1' , 'layer_norm_1' ) if "layer_norm2" in key: lowerCAmelCase = key.replace('layer_norm2' , 'layer_norm_2' ) if "block" in key: # replace for example block1 by block.0 lowerCAmelCase = key[key.find('block' ) + len('block' )] lowerCAmelCase = key.replace(f'''block{idx}''' , f'''block.{int(lowerCamelCase )-1}''' ) if "attn.q" in key: lowerCAmelCase = key.replace('attn.q' , 'attention.self.query' ) if "attn.proj" in key: lowerCAmelCase = key.replace('attn.proj' , 'attention.output.dense' ) if "attn" in key: lowerCAmelCase = key.replace('attn' , 'attention.self' ) if "fc1" in key: lowerCAmelCase = key.replace('fc1' , 'dense1' ) if "fc2" in key: lowerCAmelCase = key.replace('fc2' , 'dense2' ) if "linear_pred" in key: lowerCAmelCase = key.replace('linear_pred' , 'classifier' ) if "linear_fuse" in key: lowerCAmelCase = key.replace('linear_fuse.conv' , 'linear_fuse' ) lowerCAmelCase = key.replace('linear_fuse.bn' , 'batch_norm' ) if "linear_c" in key: # replace for example linear_c4 by linear_c.3 lowerCAmelCase = key[key.find('linear_c' ) + len('linear_c' )] lowerCAmelCase = key.replace(f'''linear_c{idx}''' , f'''linear_c.{int(lowerCamelCase )-1}''' ) if "bot_conv" in key: lowerCAmelCase = key.replace('bot_conv' , '0.convolution' ) if "skip_conv1" in key: lowerCAmelCase = key.replace('skip_conv1' , '1.convolution' ) if "skip_conv2" in key: lowerCAmelCase = key.replace('skip_conv2' , '2.convolution' ) if "fusion1" in key: lowerCAmelCase = key.replace('fusion1' , '1.fusion' ) if "fusion2" in key: lowerCAmelCase = key.replace('fusion2' , '2.fusion' ) if "fusion3" in key: lowerCAmelCase = key.replace('fusion3' , '3.fusion' ) if "fusion" in key and "conv" in key: lowerCAmelCase = key.replace('conv' , 'convolutional_layer' ) if key.startswith('module.last_layer_depth' ): lowerCAmelCase = key.replace('module.last_layer_depth' , 'head.head' ) lowerCAmelCase = value return new_state_dict def a_ ( lowerCamelCase : List[str] , lowerCamelCase : str ): # for each of the encoder blocks: for i in range(config.num_encoder_blocks ): for j in range(config.depths[i] ): # read in weights + bias of keys and values (which is a single matrix in the original implementation) lowerCAmelCase = state_dict.pop(f'''glpn.encoder.block.{i}.{j}.attention.self.kv.weight''' ) lowerCAmelCase = state_dict.pop(f'''glpn.encoder.block.{i}.{j}.attention.self.kv.bias''' ) # next, add keys and values (in that order) to the state dict lowerCAmelCase = kv_weight[ : config.hidden_sizes[i], : ] lowerCAmelCase = kv_bias[: config.hidden_sizes[i]] lowerCAmelCase = kv_weight[ config.hidden_sizes[i] :, : ] lowerCAmelCase = kv_bias[config.hidden_sizes[i] :] def a_ ( ): lowerCAmelCase = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowerCAmelCase = Image.open(requests.get(lowerCamelCase , stream=lowerCamelCase ).raw ) return image @torch.no_grad() def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Tuple , lowerCamelCase : Optional[Any]=False , lowerCamelCase : List[str]=None ): lowerCAmelCase = GLPNConfig(hidden_sizes=[64, 128, 320, 512] , decoder_hidden_size=64 , depths=[3, 8, 27, 3] ) # load image processor (only resize + rescale) lowerCAmelCase = GLPNImageProcessor() # prepare image lowerCAmelCase = prepare_img() lowerCAmelCase = image_processor(images=lowerCamelCase , return_tensors='pt' ).pixel_values logger.info('Converting model...' ) # load original state dict lowerCAmelCase = torch.load(lowerCamelCase , map_location=torch.device('cpu' ) ) # rename keys lowerCAmelCase = rename_keys(lowerCamelCase ) # key and value matrices need special treatment read_in_k_v(lowerCamelCase , lowerCamelCase ) # create HuggingFace model and load state dict lowerCAmelCase = GLPNForDepthEstimation(lowerCamelCase ) model.load_state_dict(lowerCamelCase ) model.eval() # forward pass lowerCAmelCase = model(lowerCamelCase ) lowerCAmelCase = outputs.predicted_depth # verify output if model_name is not None: if "nyu" in model_name: lowerCAmelCase = torch.tensor( [[4.4_147, 4.0_873, 4.0_673], [3.7_890, 3.2_881, 3.1_525], [3.7_674, 3.5_423, 3.4_913]] ) elif "kitti" in model_name: lowerCAmelCase = torch.tensor( [[3.4_291, 2.7_865, 2.5_151], [3.2_841, 2.7_021, 2.3_502], [3.1_147, 2.4_625, 2.2_481]] ) else: raise ValueError(f'''Unknown model name: {model_name}''' ) lowerCAmelCase = torch.Size([1, 480, 640] ) assert predicted_depth.shape == expected_shape assert torch.allclose(predicted_depth[0, :3, :3] , lowerCamelCase , atol=1e-4 ) print('Looks ok!' ) # finally, push to hub if required if push_to_hub: logger.info('Pushing model and image processor to the hub...' ) model.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=lowerCamelCase , ) image_processor.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=lowerCamelCase , ) if __name__ == "__main__": __snake_case =argparse.ArgumentParser() parser.add_argument( """--checkpoint_path""", default=None, 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 folder to output PyTorch model.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to upload the model to the HuggingFace hub.""" ) parser.add_argument( """--model_name""", default="""glpn-kitti""", type=str, help="""Name of the model in case you're pushing to the hub.""", ) __snake_case =parser.parse_args() convert_glpn_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
4
1
'''simple docstring''' import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case =logging.get_logger(__name__) __snake_case ={ """Salesforce/blip-vqa-base""": """https://huggingface.co/Salesforce/blip-vqa-base/resolve/main/config.json""", """Salesforce/blip-vqa-capfit-large""": ( """https://huggingface.co/Salesforce/blip-vqa-base-capfit/resolve/main/config.json""" ), """Salesforce/blip-image-captioning-base""": ( """https://huggingface.co/Salesforce/blip-image-captioning-base/resolve/main/config.json""" ), """Salesforce/blip-image-captioning-large""": ( """https://huggingface.co/Salesforce/blip-image-captioning-large/resolve/main/config.json""" ), """Salesforce/blip-itm-base-coco""": """https://huggingface.co/Salesforce/blip-itm-base-coco/resolve/main/config.json""", """Salesforce/blip-itm-large-coco""": """https://huggingface.co/Salesforce/blip-itm-large-coco/resolve/main/config.json""", """Salesforce/blip-itm-base-flikr""": """https://huggingface.co/Salesforce/blip-itm-base-flikr/resolve/main/config.json""", """Salesforce/blip-itm-large-flikr""": ( """https://huggingface.co/Salesforce/blip-itm-large-flikr/resolve/main/config.json""" ), } class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Dict = '''blip_text_model''' def __init__( self : Tuple , UpperCAmelCase__ : Optional[int]=3_0_5_2_4 , UpperCAmelCase__ : List[Any]=7_6_8 , UpperCAmelCase__ : Tuple=7_6_8 , UpperCAmelCase__ : Optional[Any]=3_0_7_2 , UpperCAmelCase__ : Union[str, Any]=7_6_8 , UpperCAmelCase__ : Dict=1_2 , UpperCAmelCase__ : Dict=8 , UpperCAmelCase__ : List[str]=5_1_2 , UpperCAmelCase__ : List[str]="gelu" , UpperCAmelCase__ : Dict=1E-12 , UpperCAmelCase__ : List[str]=0.0 , UpperCAmelCase__ : str=0.0 , UpperCAmelCase__ : Union[str, Any]=0.02 , UpperCAmelCase__ : Tuple=3_0_5_2_2 , UpperCAmelCase__ : Tuple=2 , UpperCAmelCase__ : List[Any]=0 , UpperCAmelCase__ : str=1_0_2 , UpperCAmelCase__ : int=True , UpperCAmelCase__ : Optional[int]=True , **UpperCAmelCase__ : List[str] , ) -> Union[str, Any]: super().__init__( pad_token_id=UpperCAmelCase__ , bos_token_id=UpperCAmelCase__ , eos_token_id=UpperCAmelCase__ , sep_token_id=UpperCAmelCase__ , **UpperCAmelCase__ , ) lowerCAmelCase = vocab_size lowerCAmelCase = hidden_size lowerCAmelCase = encoder_hidden_size lowerCAmelCase = intermediate_size lowerCAmelCase = projection_dim lowerCAmelCase = hidden_dropout_prob lowerCAmelCase = num_hidden_layers lowerCAmelCase = num_attention_heads lowerCAmelCase = max_position_embeddings lowerCAmelCase = layer_norm_eps lowerCAmelCase = hidden_act lowerCAmelCase = initializer_range lowerCAmelCase = attention_probs_dropout_prob lowerCAmelCase = is_decoder lowerCAmelCase = use_cache @classmethod def __UpperCAmelCase ( cls : int , UpperCAmelCase__ : Union[str, os.PathLike] , **UpperCAmelCase__ : List[Any] ) -> "PretrainedConfig": cls._set_token_in_kwargs(UpperCAmelCase__ ) lowerCAmelCase , lowerCAmelCase = cls.get_config_dict(UpperCAmelCase__ , **UpperCAmelCase__ ) # get the text config dict if we are loading from BlipConfig if config_dict.get('model_type' ) == "blip": lowerCAmelCase = 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(UpperCAmelCase__ , **UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Optional[int] = '''blip_vision_model''' def __init__( self : Optional[int] , UpperCAmelCase__ : Tuple=7_6_8 , UpperCAmelCase__ : Optional[Any]=3_0_7_2 , UpperCAmelCase__ : Optional[Any]=5_1_2 , UpperCAmelCase__ : Optional[Any]=1_2 , UpperCAmelCase__ : List[str]=1_2 , UpperCAmelCase__ : int=3_8_4 , UpperCAmelCase__ : int=1_6 , UpperCAmelCase__ : List[Any]="gelu" , UpperCAmelCase__ : List[Any]=1E-5 , UpperCAmelCase__ : Any=0.0 , UpperCAmelCase__ : Tuple=1E-10 , **UpperCAmelCase__ : Optional[int] , ) -> int: super().__init__(**UpperCAmelCase__ ) lowerCAmelCase = hidden_size lowerCAmelCase = intermediate_size lowerCAmelCase = projection_dim lowerCAmelCase = num_hidden_layers lowerCAmelCase = num_attention_heads lowerCAmelCase = patch_size lowerCAmelCase = image_size lowerCAmelCase = initializer_range lowerCAmelCase = attention_dropout lowerCAmelCase = layer_norm_eps lowerCAmelCase = hidden_act @classmethod def __UpperCAmelCase ( cls : str , UpperCAmelCase__ : Union[str, os.PathLike] , **UpperCAmelCase__ : List[Any] ) -> "PretrainedConfig": cls._set_token_in_kwargs(UpperCAmelCase__ ) lowerCAmelCase , lowerCAmelCase = cls.get_config_dict(UpperCAmelCase__ , **UpperCAmelCase__ ) # get the vision config dict if we are loading from BlipConfig if config_dict.get('model_type' ) == "blip": lowerCAmelCase = 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(UpperCAmelCase__ , **UpperCAmelCase__ ) class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Dict = '''blip''' lowerCamelCase : Tuple = True def __init__( self : Union[str, Any] , UpperCAmelCase__ : str=None , UpperCAmelCase__ : Union[str, Any]=None , UpperCAmelCase__ : Optional[int]=5_1_2 , UpperCAmelCase__ : str=2.6_592 , UpperCAmelCase__ : str=2_5_6 , **UpperCAmelCase__ : List[Any] , ) -> Dict: super().__init__(**UpperCAmelCase__ ) if text_config is None: lowerCAmelCase = {} logger.info('`text_config` is `None`. Initializing the `BlipTextConfig` with default values.' ) if vision_config is None: lowerCAmelCase = {} logger.info('`vision_config` is `None`. Initializing the `BlipVisionConfig` with default values.' ) lowerCAmelCase = BlipTextConfig(**UpperCAmelCase__ ) lowerCAmelCase = BlipVisionConfig(**UpperCAmelCase__ ) lowerCAmelCase = self.vision_config.hidden_size lowerCAmelCase = projection_dim lowerCAmelCase = logit_scale_init_value lowerCAmelCase = 1.0 lowerCAmelCase = 0.02 lowerCAmelCase = image_text_hidden_size @classmethod def __UpperCAmelCase ( cls : Dict , UpperCAmelCase__ : BlipTextConfig , UpperCAmelCase__ : BlipVisionConfig , **UpperCAmelCase__ : str ) -> Optional[Any]: return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : Dict ) -> List[str]: lowerCAmelCase = copy.deepcopy(self.__dict__ ) lowerCAmelCase = self.text_config.to_dict() lowerCAmelCase = self.vision_config.to_dict() lowerCAmelCase = self.__class__.model_type return output
4
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class UpperCAmelCase_ ( unittest.TestCase ): @slow def __UpperCAmelCase ( self : str ) -> List[str]: lowerCAmelCase = XLMRobertaModel.from_pretrained('xlm-roberta-base' ) lowerCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house lowerCAmelCase = torch.Size((1, 1_2, 7_6_8) ) # batch_size, sequence_length, embedding_vector_dim lowerCAmelCase = torch.tensor( [[-0.0_101, 0.1_218, -0.0_803, 0.0_801, 0.1_327, 0.0_776, -0.1_215, 0.2_383, 0.3_338, 0.3_106, 0.0_300, 0.0_252]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): lowerCAmelCase = model(UpperCAmelCase__ )['last_hidden_state'].detach() self.assertEqual(output.shape , UpperCAmelCase__ ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , UpperCAmelCase__ , atol=1E-3 ) ) @slow def __UpperCAmelCase ( self : List[Any] ) -> Tuple: lowerCAmelCase = XLMRobertaModel.from_pretrained('xlm-roberta-large' ) lowerCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house lowerCAmelCase = torch.Size((1, 1_2, 1_0_2_4) ) # batch_size, sequence_length, embedding_vector_dim lowerCAmelCase = torch.tensor( [[-0.0_699, -0.0_318, 0.0_705, -0.1_241, 0.0_999, -0.0_520, 0.1_004, -0.1_838, -0.4_704, 0.1_437, 0.0_821, 0.0_126]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): lowerCAmelCase = model(UpperCAmelCase__ )['last_hidden_state'].detach() self.assertEqual(output.shape , UpperCAmelCase__ ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , UpperCAmelCase__ , atol=1E-3 ) )
4
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __snake_case ={"""configuration_plbart""": ["""PLBART_PRETRAINED_CONFIG_ARCHIVE_MAP""", """PLBartConfig"""]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case =["""PLBartTokenizer"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case =[ """PLBART_PRETRAINED_MODEL_ARCHIVE_LIST""", """PLBartForCausalLM""", """PLBartForConditionalGeneration""", """PLBartForSequenceClassification""", """PLBartModel""", """PLBartPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_plbart import PLBART_PRETRAINED_CONFIG_ARCHIVE_MAP, PLBartConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_plbart import PLBartTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_plbart import ( PLBART_PRETRAINED_MODEL_ARCHIVE_LIST, PLBartForCausalLM, PLBartForConditionalGeneration, PLBartForSequenceClassification, PLBartModel, PLBartPreTrainedModel, ) else: import sys __snake_case =_LazyModule(__name__, globals()["""__file__"""], _import_structure)
4
'''simple docstring''' import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def a_ ( lowerCamelCase : Dict ): lowerCAmelCase = {} lowerCAmelCase = tokenizer(example['content'] , truncation=lowerCamelCase )['input_ids'] lowerCAmelCase = len(example['content'] ) / len(output['input_ids'] ) return output __snake_case =HfArgumentParser(PretokenizationArguments) __snake_case =parser.parse_args() if args.num_workers is None: __snake_case =multiprocessing.cpu_count() __snake_case =AutoTokenizer.from_pretrained(args.tokenizer_dir) __snake_case =time.time() __snake_case =load_dataset(args.dataset_name, split="""train""") print(F'''Dataset loaded in {time.time()-t_start:.2f}s''') __snake_case =time.time() __snake_case =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''') __snake_case =time.time() ds.push_to_hub(args.tokenized_data_repo) print(F'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
4
1
'''simple docstring''' import secrets from random import shuffle from string import ascii_letters, ascii_lowercase, ascii_uppercase, digits, punctuation def a_ ( lowerCamelCase : int = 8 ): lowerCAmelCase = ascii_letters + digits + punctuation return "".join(secrets.choice(lowerCamelCase ) for _ in range(lowerCamelCase ) ) def a_ ( lowerCamelCase : str , lowerCamelCase : int ): # Password Generator = full boot with random_number, random_letters, and # random_character FUNCTIONS # Put your code here... i -= len(lowerCamelCase ) lowerCAmelCase = i // 3 lowerCAmelCase = i % 3 # chars = chars_incl + random_letters(ascii_letters, i / 3 + remainder) + # random_number(digits, i / 3) + random_characters(punctuation, i / 3) lowerCAmelCase = ( chars_incl + random(lowerCamelCase , quotient + remainder ) + random(lowerCamelCase , lowerCamelCase ) + random(lowerCamelCase , lowerCamelCase ) ) lowerCAmelCase = list(lowerCamelCase ) shuffle(lowerCamelCase ) return "".join(lowerCamelCase ) # random is a generalised function for letters, characters and numbers def a_ ( lowerCamelCase : str , lowerCamelCase : int ): return "".join(secrets.choice(lowerCamelCase ) for _ in range(lowerCamelCase ) ) def a_ ( lowerCamelCase : str , lowerCamelCase : Tuple ): pass # Put your code here... def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Tuple ): pass # Put your code here... def a_ ( lowerCamelCase : int , lowerCamelCase : Dict ): pass # Put your code here... def a_ ( lowerCamelCase : str , lowerCamelCase : int = 8 ): if len(lowerCamelCase ) < min_length: # Your Password must be at least 8 characters long return False lowerCAmelCase = any(char in ascii_uppercase for char in password ) lowerCAmelCase = any(char in ascii_lowercase for char in password ) lowerCAmelCase = any(char in digits for char in password ) lowerCAmelCase = any(char in punctuation for char in password ) return upper and lower and num and spec_char # Passwords should contain UPPERCASE, lowerase # numbers, and special characters def a_ ( ): lowerCAmelCase = int(input('Please indicate the max length of your password: ' ).strip() ) lowerCAmelCase = input( 'Please indicate the characters that must be in your password: ' ).strip() print('Password generated:' , password_generator(lowerCamelCase ) ) print( 'Alternative Password generated:' , alternative_password_generator(lowerCamelCase , lowerCamelCase ) , ) print('[If you are thinking of using this passsword, You better save it.]' ) if __name__ == "__main__": main()
4
'''simple docstring''' import logging from dataclasses import dataclass, field from pathlib import Path from typing import Optional, Union from .generation.configuration_utils import GenerationConfig from .training_args import TrainingArguments from .utils import add_start_docstrings __snake_case =logging.getLogger(__name__) @dataclass @add_start_docstrings(TrainingArguments.__doc__ ) class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : bool = field(default=__lowercase , metadata={'''help''': '''Whether to use SortishSampler or not.'''} ) lowerCamelCase : bool = field( default=__lowercase , metadata={'''help''': '''Whether to use generate to calculate generative metrics (ROUGE, BLEU).'''} ) lowerCamelCase : Optional[int] = field( default=__lowercase , metadata={ '''help''': ( '''The `max_length` to use on each evaluation loop when `predict_with_generate=True`. Will default ''' '''to the `max_length` value of the model configuration.''' ) } , ) lowerCamelCase : Optional[int] = field( default=__lowercase , metadata={ '''help''': ( '''The `num_beams` to use on each evaluation loop when `predict_with_generate=True`. Will default ''' '''to the `num_beams` value of the model configuration.''' ) } , ) lowerCamelCase : Optional[Union[str, Path, GenerationConfig]] = field( default=__lowercase , metadata={ '''help''': '''Model id, file path or url pointing to a GenerationConfig json file, to use during prediction.''' } , ) def __UpperCAmelCase ( self : Dict ) -> List[str]: lowerCAmelCase = super().to_dict() for k, v in d.items(): if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): lowerCAmelCase = v.to_dict() return d
4
1
'''simple docstring''' import argparse import json import os import fairseq import torch from torch import nn from transformers import ( SpeechaTextaConfig, SpeechaTextaForCausalLM, SpeechaTextaTokenizer, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() __snake_case =logging.get_logger(__name__) __snake_case ={ """post_extract_proj""": """feature_projection.projection""", """encoder.pos_conv.0""": """encoder.pos_conv_embed.conv""", """self_attn.k_proj""": """encoder.layers.*.attention.k_proj""", """self_attn.v_proj""": """encoder.layers.*.attention.v_proj""", """self_attn.q_proj""": """encoder.layers.*.attention.q_proj""", """self_attn.out_proj""": """encoder.layers.*.attention.out_proj""", """self_attn_layer_norm""": """encoder.layers.*.layer_norm""", """fc1""": """encoder.layers.*.feed_forward.intermediate_dense""", """fc2""": """encoder.layers.*.feed_forward.output_dense""", """final_layer_norm""": """encoder.layers.*.final_layer_norm""", """encoder.layer_norm""": """encoder.layer_norm""", """w2v_model.layer_norm""": """feature_projection.layer_norm""", """quantizer.weight_proj""": """quantizer.weight_proj""", """quantizer.vars""": """quantizer.codevectors""", """project_q""": """project_q""", """final_proj""": """project_hid""", """w2v_encoder.proj""": """lm_head""", """mask_emb""": """masked_spec_embed""", } __snake_case =[ """lm_head""", """quantizer.weight_proj""", """quantizer.codevectors""", """project_q""", """project_hid""", ] def a_ ( lowerCamelCase : Any , lowerCamelCase : Dict , lowerCamelCase : int , lowerCamelCase : List[Any] , lowerCamelCase : str ): for attribute in key.split('.' ): lowerCAmelCase = getattr(lowerCamelCase , lowerCamelCase ) if weight_type is not None: lowerCAmelCase = getattr(lowerCamelCase , lowerCamelCase ).shape else: lowerCAmelCase = hf_pointer.shape assert hf_shape == value.shape, ( 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": lowerCAmelCase = value elif weight_type == "weight_g": lowerCAmelCase = value elif weight_type == "weight_v": lowerCAmelCase = value elif weight_type == "bias": lowerCAmelCase = value else: lowerCAmelCase = value logger.info(f'''{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.''' ) def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Any ): lowerCAmelCase = [] lowerCAmelCase = fairseq_model.state_dict() lowerCAmelCase = hf_model.feature_extractor # if encoder has different dim to decoder -> use proj_weight lowerCAmelCase = None for name, value in fairseq_dict.items(): lowerCAmelCase = False if "conv_layers" in name: load_conv_layer( lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , hf_model.config.feat_extract_norm == 'group' , ) lowerCAmelCase = True elif name.split('.' )[0] == "proj": lowerCAmelCase = fairseq_model.proj lowerCAmelCase = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split('w2v_model.' )[-1] == name.split('.' )[0]: lowerCAmelCase = True if "*" in mapped_key: lowerCAmelCase = name.split(lowerCamelCase )[0].split('.' )[-2] lowerCAmelCase = mapped_key.replace('*' , lowerCamelCase ) if "weight_g" in name: lowerCAmelCase = 'weight_g' elif "weight_v" in name: lowerCAmelCase = 'weight_v' elif "bias" in name: lowerCAmelCase = 'bias' elif "weight" in name: lowerCAmelCase = 'weight' else: lowerCAmelCase = None set_recursively(lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase ) continue if not is_used: unused_weights.append(lowerCamelCase ) logger.warning(f'''Unused weights: {unused_weights}''' ) return proj_weight def a_ ( lowerCamelCase : List[str] , lowerCamelCase : List[str] , lowerCamelCase : Dict , lowerCamelCase : List[str] , lowerCamelCase : Optional[Any] ): lowerCAmelCase = full_name.split('conv_layers.' )[-1] lowerCAmelCase = name.split('.' ) lowerCAmelCase = int(items[0] ) lowerCAmelCase = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) lowerCAmelCase = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) lowerCAmelCase = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f'''{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was''' " found." ) lowerCAmelCase = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.''' ) lowerCAmelCase = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(lowerCamelCase ) def a_ ( lowerCamelCase : List[str] ): lowerCAmelCase , lowerCAmelCase = emb.weight.shape lowerCAmelCase = nn.Linear(lowerCamelCase , lowerCamelCase , bias=lowerCamelCase ) lowerCAmelCase = emb.weight.data return lin_layer def a_ ( lowerCamelCase : Optional[int] ): with open(lowerCamelCase , 'r' , encoding='utf-8' ) as f: lowerCAmelCase = f.readlines() lowerCAmelCase = [line.split(' ' )[0] for line in lines] lowerCAmelCase = len(lowerCamelCase ) lowerCAmelCase = { '<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3, } vocab_dict.update(dict(zip(lowerCamelCase , range(4 , num_words + 4 ) ) ) ) return vocab_dict @torch.no_grad() def a_ ( lowerCamelCase : str , lowerCamelCase : Any , lowerCamelCase : List[Any] , lowerCamelCase : Any , lowerCamelCase : Dict , lowerCamelCase : Union[str, Any] , lowerCamelCase : Tuple , ): lowerCAmelCase = WavaVecaConfig.from_pretrained(lowerCamelCase ) lowerCAmelCase = SpeechaTextaConfig.from_pretrained( lowerCamelCase , vocab_size=lowerCamelCase , decoder_layers=lowerCamelCase , do_stable_layer_norm=lowerCamelCase ) lowerCAmelCase = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=lowerCamelCase , return_attention_mask=lowerCamelCase , ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'data': '/'.join(dict_path.split('/' )[:-1] )} ) lowerCAmelCase = model[0].eval() # set weights for wav2vec2 encoder lowerCAmelCase = WavaVecaModel(lowerCamelCase ) lowerCAmelCase = recursively_load_weights_wavaveca(model.encoder , lowerCamelCase ) lowerCAmelCase = SpeechaTextaForCausalLM(lowerCamelCase ) lowerCAmelCase , lowerCAmelCase = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=lowerCamelCase ) # set output linear layer unexpected_keys.remove('embed_out' ) lowerCAmelCase = nn.Parameter(model.decoder.embed_out.detach() ) # layer norm is init to identity matrix so leaving it is fine logger.warning(f'''The following keys are missing when loading the decoder weights: {missing_keys}''' ) logger.warning(f'''The following keys are unexpected when loading the decoder weights: {unexpected_keys}''' ) lowerCAmelCase = SpeechEncoderDecoderModel(encoder=lowerCamelCase , decoder=lowerCamelCase ) lowerCAmelCase = False # add projection layer lowerCAmelCase = nn.Parameter(projection_layer.weight ) lowerCAmelCase = nn.Parameter(projection_layer.bias ) lowerCAmelCase = create_vocab_dict(lowerCamelCase ) with open(os.path.join(lowerCamelCase , 'vocab.json' ) , 'w' ) as fp: json.dump(lowerCamelCase , lowerCamelCase ) lowerCAmelCase = SpeechaTextaTokenizer(os.path.join(lowerCamelCase , 'vocab.json' ) ) tokenizer.save_pretrained(lowerCamelCase ) lowerCAmelCase = hf_wavavec.config.to_dict() lowerCAmelCase = tokenizer.pad_token_id lowerCAmelCase = tokenizer.bos_token_id lowerCAmelCase = tokenizer.eos_token_id lowerCAmelCase = 'speech_to_text_2' lowerCAmelCase = 'wav2vec2' lowerCAmelCase = SpeechEncoderDecoderConfig.from_dict(lowerCamelCase ) hf_wavavec.save_pretrained(lowerCamelCase ) feature_extractor.save_pretrained(lowerCamelCase ) if __name__ == "__main__": __snake_case =argparse.ArgumentParser() parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to fairseq checkpoint""") parser.add_argument("""--dict_path""", default=None, type=str, help="""Path to dict of fine-tuned model""") parser.add_argument( """--encoder_config_path""", default="""facebook/wav2vec2-large-lv60""", type=str, help="""Path to hf encoder wav2vec2 checkpoint config""", ) parser.add_argument( """--decoder_config_path""", default="""facebook/s2t-small-mustc-en-fr-st""", type=str, help="""Path to hf decoder s2t checkpoint config""", ) parser.add_argument("""--vocab_size""", default=10_224, type=int, help="""Vocab size of decoder""") parser.add_argument("""--num_decoder_layers""", default=7, type=int, help="""Number of decoder layers""") __snake_case =parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, vocab_size=args.vocab_size, num_decoder_layers=args.num_decoder_layers, )
4
'''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() __snake_case =logging.get_logger("""transformers.models.encodec""") __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_DECODER, } __snake_case ={ **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_ENCODER_48K, **MAPPING_DECODER, **MAPPING_DECODER_48K, } __snake_case =[] __snake_case =[] def a_ ( lowerCamelCase : Optional[Any] , lowerCamelCase : Union[str, Any] , lowerCamelCase : int , lowerCamelCase : Any , lowerCamelCase : List[str] ): for attribute in key.split('.' ): lowerCAmelCase = getattr(lowerCamelCase , lowerCamelCase ) if weight_type is not None: lowerCAmelCase = getattr(lowerCamelCase , lowerCamelCase ).shape else: lowerCAmelCase = 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": lowerCAmelCase = value elif weight_type == "weight_g": lowerCAmelCase = value elif weight_type == "weight_v": lowerCAmelCase = value elif weight_type == "bias": lowerCAmelCase = value elif weight_type == "running_mean": lowerCAmelCase = value elif weight_type == "running_var": lowerCAmelCase = value elif weight_type == "num_batches_tracked": lowerCAmelCase = value elif weight_type == "weight_ih_l0": lowerCAmelCase = value elif weight_type == "weight_hh_l0": lowerCAmelCase = value elif weight_type == "bias_ih_l0": lowerCAmelCase = value elif weight_type == "bias_hh_l0": lowerCAmelCase = value elif weight_type == "weight_ih_l1": lowerCAmelCase = value elif weight_type == "weight_hh_l1": lowerCAmelCase = value elif weight_type == "bias_ih_l1": lowerCAmelCase = value elif weight_type == "bias_hh_l1": lowerCAmelCase = value else: lowerCAmelCase = value logger.info(f'''{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.''' ) def a_ ( lowerCamelCase : Optional[Any] , lowerCamelCase : Optional[Any] ): for key in ignore_keys: if key.endswith('.*' ): if name.startswith(key[:-1] ): return True elif ".*." in key: lowerCAmelCase , lowerCAmelCase = key.split('.*.' ) if prefix in name and suffix in name: return True elif key in name: return True return False def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Any , lowerCamelCase : str ): lowerCAmelCase = [] if model_name == "encodec_24khz" or "encodec_32khz": lowerCAmelCase = MAPPING_24K elif model_name == "encodec_48khz": lowerCAmelCase = 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 lowerCAmelCase = False for key, mapped_key in MAPPING.items(): if "*" in key: lowerCAmelCase , lowerCAmelCase = key.split('.*.' ) if prefix in name and suffix in name: lowerCAmelCase = suffix if key in name: # HACK otherwise .embed gets initialized with .embed_avg too if key.endswith('embed' ) and name.endswith('embed_avg' ): continue lowerCAmelCase = True if "*" in mapped_key: lowerCAmelCase = name.split(lowerCamelCase )[0].split('.' )[-2] lowerCAmelCase = mapped_key.replace('*' , lowerCamelCase ) if "weight_g" in name: lowerCAmelCase = 'weight_g' elif "weight_v" in name: lowerCAmelCase = 'weight_v' elif "weight_ih_l0" in name: lowerCAmelCase = 'weight_ih_l0' elif "weight_hh_l0" in name: lowerCAmelCase = 'weight_hh_l0' elif "bias_ih_l0" in name: lowerCAmelCase = 'bias_ih_l0' elif "bias_hh_l0" in name: lowerCAmelCase = 'bias_hh_l0' elif "weight_ih_l1" in name: lowerCAmelCase = 'weight_ih_l1' elif "weight_hh_l1" in name: lowerCAmelCase = 'weight_hh_l1' elif "bias_ih_l1" in name: lowerCAmelCase = 'bias_ih_l1' elif "bias_hh_l1" in name: lowerCAmelCase = 'bias_hh_l1' elif "bias" in name: lowerCAmelCase = 'bias' elif "weight" in name: lowerCAmelCase = 'weight' elif "running_mean" in name: lowerCAmelCase = 'running_mean' elif "running_var" in name: lowerCAmelCase = 'running_var' elif "num_batches_tracked" in name: lowerCAmelCase = 'num_batches_tracked' else: lowerCAmelCase = 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 a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Union[str, Any] , lowerCamelCase : str , lowerCamelCase : Dict=None , lowerCamelCase : Union[str, Any]=None , ): if config_path is not None: lowerCAmelCase = EncodecConfig.from_pretrained(lowerCamelCase ) else: lowerCAmelCase = EncodecConfig() if model_name == "encodec_24khz": pass # config is already correct elif model_name == "encodec_32khz": lowerCAmelCase = [8, 5, 4, 4] lowerCAmelCase = [2.2] lowerCAmelCase = 64 lowerCAmelCase = 32000 lowerCAmelCase = 2048 lowerCAmelCase = False lowerCAmelCase = False lowerCAmelCase = False elif model_name == "encodec_48khz": lowerCAmelCase = [8, 5, 4, 2] lowerCAmelCase = [3.0, 6.0, 12.0, 24.0] lowerCAmelCase = 48000 lowerCAmelCase = 2 lowerCAmelCase = False lowerCAmelCase = 'time_group_norm' lowerCAmelCase = True lowerCAmelCase = 1.0 lowerCAmelCase = 0.01 else: raise ValueError(f'''Unknown model name: {model_name}''' ) lowerCAmelCase = EncodecModel(lowerCamelCase ) lowerCAmelCase = 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 ) lowerCAmelCase = 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 lowerCAmelCase = 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__": __snake_case =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.""" ) __snake_case =parser.parse_args() convert_checkpoint( args.model, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
4
1
'''simple docstring''' import argparse import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## __snake_case =16 __snake_case =32 def a_ ( lowerCamelCase : Accelerator , lowerCamelCase : int = 16 ): lowerCAmelCase = AutoTokenizer.from_pretrained('bert-base-cased' ) lowerCAmelCase = load_dataset('glue' , 'mrpc' ) def tokenize_function(lowerCamelCase : Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) lowerCAmelCase = 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 # starting with the main process first: with accelerator.main_process_first(): lowerCAmelCase = datasets.map( lowerCamelCase , batched=lowerCamelCase , remove_columns=['idx', 'sentence1', 'sentence2'] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library lowerCAmelCase = 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. lowerCAmelCase = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": lowerCAmelCase = 16 elif accelerator.mixed_precision != "no": lowerCAmelCase = 8 else: lowerCAmelCase = None return tokenizer.pad( lowerCamelCase , padding='longest' , max_length=lowerCamelCase , pad_to_multiple_of=lowerCamelCase , return_tensors='pt' , ) # Instantiate dataloaders. lowerCAmelCase = DataLoader( tokenized_datasets['train'] , shuffle=lowerCamelCase , collate_fn=lowerCamelCase , batch_size=lowerCamelCase , drop_last=lowerCamelCase ) lowerCAmelCase = DataLoader( tokenized_datasets['validation'] , shuffle=lowerCamelCase , collate_fn=lowerCamelCase , batch_size=lowerCamelCase , drop_last=(accelerator.mixed_precision == 'fp8') , ) return train_dataloader, eval_dataloader def a_ ( lowerCamelCase : Tuple , lowerCamelCase : Optional[int] ): # Initialize accelerator lowerCAmelCase = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs lowerCAmelCase = config['lr'] lowerCAmelCase = int(config['num_epochs'] ) lowerCAmelCase = int(config['seed'] ) lowerCAmelCase = int(config['batch_size'] ) lowerCAmelCase = evaluate.load('glue' , 'mrpc' ) # If the batch size is too big we use gradient accumulation lowerCAmelCase = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: lowerCAmelCase = batch_size // MAX_GPU_BATCH_SIZE lowerCAmelCase = MAX_GPU_BATCH_SIZE set_seed(lowerCamelCase ) lowerCAmelCase , lowerCAmelCase = get_dataloaders(lowerCamelCase , lowerCamelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) lowerCAmelCase = AutoModelForSequenceClassification.from_pretrained('bert-base-cased' , return_dict=lowerCamelCase ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). lowerCAmelCase = model.to(accelerator.device ) # Instantiate optimizer lowerCAmelCase = AdamW(params=model.parameters() , lr=lowerCamelCase ) # Instantiate scheduler lowerCAmelCase = get_linear_schedule_with_warmup( optimizer=lowerCamelCase , num_warmup_steps=100 , num_training_steps=(len(lowerCamelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = accelerator.prepare( lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase ) # Now we train the model for epoch in range(lowerCamelCase ): model.train() for step, batch in enumerate(lowerCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) lowerCAmelCase = model(**lowerCamelCase ) lowerCAmelCase = outputs.loss lowerCAmelCase = loss / gradient_accumulation_steps accelerator.backward(lowerCamelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() 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(): lowerCAmelCase = model(**lowerCamelCase ) lowerCAmelCase = outputs.logits.argmax(dim=-1 ) lowerCAmelCase , lowerCAmelCase = accelerator.gather_for_metrics((predictions, batch['labels']) ) metric.add_batch( predictions=lowerCamelCase , references=lowerCamelCase , ) lowerCAmelCase = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f'''epoch {epoch}:''' , lowerCamelCase ) def a_ ( ): lowerCAmelCase = argparse.ArgumentParser(description='Simple example of training script.' ) parser.add_argument( '--mixed_precision' , type=lowerCamelCase , default=lowerCamelCase , choices=['no', 'fp16', 'bf16', 'fp8'] , help='Whether to use mixed precision. Choose' 'between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.' 'and an Nvidia Ampere GPU.' , ) parser.add_argument('--cpu' , action='store_true' , help='If passed, will train on the CPU.' ) lowerCAmelCase = parser.parse_args() lowerCAmelCase = {'lr': 2e-5, 'num_epochs': 3, 'seed': 42, 'batch_size': 16} training_function(lowerCamelCase , lowerCamelCase ) if __name__ == "__main__": main()
4
'''simple docstring''' import warnings from ...utils import logging from .image_processing_flava import FlavaImageProcessor __snake_case =logging.get_logger(__name__) class UpperCAmelCase_ ( __lowercase ): def __init__( self : Dict , *UpperCAmelCase__ : Dict , **UpperCAmelCase__ : List[str] ) -> None: warnings.warn( 'The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use FlavaImageProcessor instead.' , UpperCAmelCase__ , ) super().__init__(*UpperCAmelCase__ , **UpperCAmelCase__ )
4
1
'''simple docstring''' from typing import List, Optional, Union import numpy as np import PIL.Image from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import rescale, resize, to_channel_dimension_format from ...image_utils import ( ChannelDimension, PILImageResampling, get_image_size, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging __snake_case =logging.get_logger(__name__) class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Tuple = ['''pixel_values'''] def __init__( self : List[Any] , UpperCAmelCase__ : bool = True , UpperCAmelCase__ : int = 3_2 , UpperCAmelCase__ : str=PILImageResampling.BILINEAR , UpperCAmelCase__ : bool = True , **UpperCAmelCase__ : int , ) -> None: lowerCAmelCase = do_resize lowerCAmelCase = do_rescale lowerCAmelCase = size_divisor lowerCAmelCase = resample super().__init__(**UpperCAmelCase__ ) def __UpperCAmelCase ( self : int , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : int , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Optional[ChannelDimension] = None , **UpperCAmelCase__ : int ) -> np.ndarray: lowerCAmelCase , lowerCAmelCase = get_image_size(UpperCAmelCase__ ) # Rounds the height and width down to the closest multiple of size_divisor lowerCAmelCase = height // size_divisor * size_divisor lowerCAmelCase = width // size_divisor * size_divisor lowerCAmelCase = resize(UpperCAmelCase__ , (new_h, new_w) , resample=UpperCAmelCase__ , data_format=UpperCAmelCase__ , **UpperCAmelCase__ ) return image def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : float , UpperCAmelCase__ : Optional[ChannelDimension] = None , **UpperCAmelCase__ : Optional[int] ) -> np.ndarray: return rescale(image=UpperCAmelCase__ , scale=UpperCAmelCase__ , data_format=UpperCAmelCase__ , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : Union[str, Any] , UpperCAmelCase__ : Union["PIL.Image.Image", TensorType, List["PIL.Image.Image"], List[TensorType]] , UpperCAmelCase__ : Optional[bool] = None , UpperCAmelCase__ : Optional[int] = None , UpperCAmelCase__ : Optional[Any]=None , UpperCAmelCase__ : Optional[bool] = None , UpperCAmelCase__ : Optional[Union[TensorType, str]] = None , UpperCAmelCase__ : ChannelDimension = ChannelDimension.FIRST , **UpperCAmelCase__ : List[Any] , ) -> BatchFeature: lowerCAmelCase = do_resize if do_resize is not None else self.do_resize lowerCAmelCase = do_rescale if do_rescale is not None else self.do_rescale lowerCAmelCase = size_divisor if size_divisor is not None else self.size_divisor lowerCAmelCase = resample if resample is not None else self.resample if do_resize and size_divisor is None: raise ValueError('size_divisor is required for resizing' ) lowerCAmelCase = make_list_of_images(UpperCAmelCase__ ) if not valid_images(UpperCAmelCase__ ): raise ValueError('Invalid image(s)' ) # All transformations expect numpy arrays. lowerCAmelCase = [to_numpy_array(UpperCAmelCase__ ) for img in images] if do_resize: lowerCAmelCase = [self.resize(UpperCAmelCase__ , size_divisor=UpperCAmelCase__ , resample=UpperCAmelCase__ ) for image in images] if do_rescale: lowerCAmelCase = [self.rescale(UpperCAmelCase__ , scale=1 / 2_5_5 ) for image in images] lowerCAmelCase = [to_channel_dimension_format(UpperCAmelCase__ , UpperCAmelCase__ ) for image in images] lowerCAmelCase = {'pixel_values': images} return BatchFeature(data=UpperCAmelCase__ , tensor_type=UpperCAmelCase__ )
4
'''simple docstring''' from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer __snake_case =logging.get_logger(__name__) __snake_case ={ """vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_config_file""": """tokenizer_config.json""", } __snake_case ={ """vocab_file""": { """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json""" }, """merges_file""": { """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt""" }, """tokenizer_config_file""": { """facebook/blenderbot_small-90M""": ( """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json""" ) }, } __snake_case ={ """facebook/blenderbot_small-90M""": 512, } class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Tuple = VOCAB_FILES_NAMES lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[Any] = BlenderbotSmallTokenizer def __init__( self : Any , UpperCAmelCase__ : Union[str, Any]=None , UpperCAmelCase__ : Dict=None , UpperCAmelCase__ : int="<|endoftext|>" , UpperCAmelCase__ : Dict="<|endoftext|>" , UpperCAmelCase__ : str="<|endoftext|>" , UpperCAmelCase__ : str=False , UpperCAmelCase__ : Tuple=True , **UpperCAmelCase__ : Optional[Any] , ) -> Any: super().__init__( ByteLevelBPETokenizer( vocab=UpperCAmelCase__ , merges=UpperCAmelCase__ , add_prefix_space=UpperCAmelCase__ , trim_offsets=UpperCAmelCase__ , ) , bos_token=UpperCAmelCase__ , eos_token=UpperCAmelCase__ , unk_token=UpperCAmelCase__ , **UpperCAmelCase__ , ) lowerCAmelCase = add_prefix_space def __UpperCAmelCase ( self : List[Any] , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Dict=None ) -> Any: lowerCAmelCase = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def __UpperCAmelCase ( self : Any , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
4
1
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 lowercase_ ( lowercase ): '''simple docstring''' __snake_case = 42 __snake_case = 42 class lowercase_ ( nn.Module ): '''simple docstring''' __snake_case = 42 __snake_case = (16, 32, 96, 2_56) __snake_case = jnp.floataa def __lowerCAmelCase ( self : Dict ) ->int: """simple docstring""" 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( __UpperCAmelCase , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) blocks.append(__UpperCAmelCase ) a = nn.Conv( __UpperCAmelCase , kernel_size=(3, 3) , strides=(2, 2) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) blocks.append(__UpperCAmelCase ) 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 : List[Any] , __UpperCAmelCase : Any ) ->Dict: """simple docstring""" a = self.conv_in(__UpperCAmelCase ) a = nn.silu(__UpperCAmelCase ) for block in self.blocks: a = block(__UpperCAmelCase ) a = nn.silu(__UpperCAmelCase ) a = self.conv_out(__UpperCAmelCase ) return embedding @flax_register_to_config class lowercase_ ( nn.Module , lowercase , lowercase ): '''simple docstring''' __snake_case = 32 __snake_case = 4 __snake_case = ( "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "DownBlock2D", ) __snake_case = False __snake_case = (3_20, 6_40, 12_80, 12_80) __snake_case = 2 __snake_case = 8 __snake_case = None __snake_case = 12_80 __snake_case = 0.0 __snake_case = False __snake_case = jnp.floataa __snake_case = True __snake_case = 0 __snake_case = "rgb" __snake_case = (16, 32, 96, 2_56) def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : jax.random.KeyArray ) ->FrozenDict: """simple docstring""" a = (1, self.in_channels, self.sample_size, self.sample_size) a = jnp.zeros(__UpperCAmelCase , 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(__UpperCAmelCase , dtype=jnp.floataa ) a , a = jax.random.split(__UpperCAmelCase ) a = {'''params''': params_rng, '''dropout''': dropout_rng} return self.init(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase )["params"] def __lowerCAmelCase ( self : int ) ->List[str]: """simple docstring""" 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(__UpperCAmelCase , 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(__UpperCAmelCase , __UpperCAmelCase ): a = (only_cross_attention,) * len(self.down_block_types ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = (num_attention_heads,) * len(self.down_block_types ) # down a = [] a = [] a = block_out_channels[0] a = nn.Conv( __UpperCAmelCase , 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(__UpperCAmelCase ) for i, down_block_type in enumerate(self.down_block_types ): a = output_channel a = block_out_channels[i] a = i == len(__UpperCAmelCase ) - 1 if down_block_type == "CrossAttnDownBlock2D": a = FlaxCrossAttnDownBlockaD( in_channels=__UpperCAmelCase , out_channels=__UpperCAmelCase , 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=__UpperCAmelCase , out_channels=__UpperCAmelCase , dropout=self.dropout , num_layers=self.layers_per_block , add_downsample=not is_final_block , dtype=self.dtype , ) down_blocks.append(__UpperCAmelCase ) for _ in range(self.layers_per_block ): a = nn.Conv( __UpperCAmelCase , 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(__UpperCAmelCase ) if not is_final_block: a = nn.Conv( __UpperCAmelCase , 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(__UpperCAmelCase ) a = down_blocks a = controlnet_down_blocks # mid a = block_out_channels[-1] a = FlaxUNetMidBlockaDCrossAttn( in_channels=__UpperCAmelCase , dropout=self.dropout , num_attention_heads=num_attention_heads[-1] , use_linear_projection=self.use_linear_projection , dtype=self.dtype , ) a = nn.Conv( __UpperCAmelCase , kernel_size=(1, 1) , padding='''VALID''' , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) def __call__( self : Any , __UpperCAmelCase : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : int , __UpperCAmelCase : Tuple , __UpperCAmelCase : float = 1.0 , __UpperCAmelCase : bool = True , __UpperCAmelCase : bool = False , ) ->Union[FlaxControlNetOutput, Tuple]: """simple docstring""" a = self.controlnet_conditioning_channel_order if channel_order == "bgr": a = jnp.flip(__UpperCAmelCase , axis=1 ) # 1. time if not isinstance(__UpperCAmelCase , jnp.ndarray ): a = jnp.array([timesteps] , dtype=jnp.intaa ) elif isinstance(__UpperCAmelCase , jnp.ndarray ) and len(timesteps.shape ) == 0: a = timesteps.astype(dtype=jnp.floataa ) a = jnp.expand_dims(__UpperCAmelCase , 0 ) a = self.time_proj(__UpperCAmelCase ) a = self.time_embedding(__UpperCAmelCase ) # 2. pre-process a = jnp.transpose(__UpperCAmelCase , (0, 2, 3, 1) ) a = self.conv_in(__UpperCAmelCase ) a = jnp.transpose(__UpperCAmelCase , (0, 2, 3, 1) ) a = self.controlnet_cond_embedding(__UpperCAmelCase ) sample += controlnet_cond # 3. down a = (sample,) for down_block in self.down_blocks: if isinstance(__UpperCAmelCase , __UpperCAmelCase ): a , a = down_block(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , deterministic=not train ) else: a , a = down_block(__UpperCAmelCase , __UpperCAmelCase , deterministic=not train ) down_block_res_samples += res_samples # 4. mid a = self.mid_block(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , deterministic=not train ) # 5. contronet blocks a = () for down_block_res_sample, controlnet_block in zip(__UpperCAmelCase , self.controlnet_down_blocks ): a = controlnet_block(__UpperCAmelCase ) controlnet_down_block_res_samples += (down_block_res_sample,) a = controlnet_down_block_res_samples a = self.controlnet_mid_block(__UpperCAmelCase ) # 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=__UpperCAmelCase , mid_block_res_sample=__UpperCAmelCase )
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case =logging.get_logger(__name__) __snake_case ={ """facebook/s2t-wav2vec2-large-en-de""": ( """https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json""" ), # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2 } class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Union[str, Any] = '''speech_to_text_2''' lowerCamelCase : Any = ['''past_key_values'''] lowerCamelCase : Optional[Any] = {'''num_attention_heads''': '''decoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self : Optional[int] , UpperCAmelCase__ : Optional[Any]=1_0_0_0_0 , UpperCAmelCase__ : int=6 , UpperCAmelCase__ : Optional[Any]=2_0_4_8 , UpperCAmelCase__ : Union[str, Any]=4 , UpperCAmelCase__ : List[Any]=0.0 , UpperCAmelCase__ : str=True , UpperCAmelCase__ : str="relu" , UpperCAmelCase__ : Any=2_5_6 , UpperCAmelCase__ : Optional[Any]=0.1 , UpperCAmelCase__ : Any=0.0 , UpperCAmelCase__ : Tuple=0.0 , UpperCAmelCase__ : List[Any]=0.02 , UpperCAmelCase__ : List[Any]=2 , UpperCAmelCase__ : str=True , UpperCAmelCase__ : List[str]=1 , UpperCAmelCase__ : Any=0 , UpperCAmelCase__ : Dict=2 , UpperCAmelCase__ : int=1_0_2_4 , **UpperCAmelCase__ : Optional[Any] , ) -> Dict: lowerCAmelCase = vocab_size lowerCAmelCase = d_model lowerCAmelCase = decoder_ffn_dim lowerCAmelCase = decoder_layers lowerCAmelCase = decoder_attention_heads lowerCAmelCase = dropout lowerCAmelCase = attention_dropout lowerCAmelCase = activation_dropout lowerCAmelCase = activation_function lowerCAmelCase = init_std lowerCAmelCase = decoder_layerdrop lowerCAmelCase = use_cache lowerCAmelCase = decoder_layers lowerCAmelCase = scale_embedding # scale factor will be sqrt(d_model) if True lowerCAmelCase = max_target_positions super().__init__( pad_token_id=UpperCAmelCase__ , bos_token_id=UpperCAmelCase__ , eos_token_id=UpperCAmelCase__ , decoder_start_token_id=UpperCAmelCase__ , **UpperCAmelCase__ , )
4
0
'''simple docstring''' import math import random from typing import Any from .hill_climbing import SearchProblem def lowerCAmelCase_ ( snake_case_ : Any , snake_case_ : bool = True , snake_case_ : float = math.inf , snake_case_ : float = -math.inf , snake_case_ : float = math.inf , snake_case_ : float = -math.inf , snake_case_ : bool = False , snake_case_ : float = 1_00 , snake_case_ : float = 0.01 , snake_case_ : float = 1 , ) -> Any: '''simple docstring''' UpperCAmelCase_ = False UpperCAmelCase_ = search_prob UpperCAmelCase_ = start_temperate UpperCAmelCase_ = [] UpperCAmelCase_ = 0 UpperCAmelCase_ = None while not search_end: UpperCAmelCase_ = current_state.score() if best_state is None or current_score > best_state.score(): UpperCAmelCase_ = current_state scores.append(snake_case_ ) iterations += 1 UpperCAmelCase_ = None UpperCAmelCase_ = current_state.get_neighbors() while ( next_state is None and neighbors ): # till we do not find a neighbor that we can move to UpperCAmelCase_ = random.randint(0 , len(snake_case_ ) - 1 ) # picking a random neighbor UpperCAmelCase_ = neighbors.pop(snake_case_ ) UpperCAmelCase_ = picked_neighbor.score() - current_score if ( picked_neighbor.x > max_x or picked_neighbor.x < min_x or picked_neighbor.y > max_y or picked_neighbor.y < min_y ): continue # neighbor outside our bounds if not find_max: UpperCAmelCase_ = change * -1 # in case we are finding minimum if change > 0: # improves the solution UpperCAmelCase_ = picked_neighbor else: UpperCAmelCase_ = (math.e) ** ( change / current_temp ) # probability generation function if random.random() < probability: # random number within probability UpperCAmelCase_ = picked_neighbor UpperCAmelCase_ = current_temp - (current_temp * rate_of_decrease) if current_temp < threshold_temp or next_state is None: # temperature below threshold, or could not find a suitable neighbor UpperCAmelCase_ = True else: UpperCAmelCase_ = next_state if visualization: from matplotlib import pyplot as plt plt.plot(range(snake_case_ ) , snake_case_ ) plt.xlabel("Iterations" ) plt.ylabel("Function values" ) plt.show() return best_state if __name__ == "__main__": def lowerCAmelCase_ ( snake_case_ : Optional[Any] , snake_case_ : int ) -> int: '''simple docstring''' return (x**2) + (y**2) # starting the problem with initial coordinates (12, 47) SCREAMING_SNAKE_CASE_: List[Any] =SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) SCREAMING_SNAKE_CASE_: Optional[Any] =simulated_annealing( prob, find_max=False, max_x=1_00, min_x=5, max_y=50, min_y=-5, visualization=True ) print( 'The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 ' f"and 50 > y > - 5 found via hill climbing: {local_min.score()}" ) # starting the problem with initial coordinates (12, 47) SCREAMING_SNAKE_CASE_: Tuple =SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) SCREAMING_SNAKE_CASE_: List[str] =simulated_annealing( prob, find_max=True, max_x=1_00, min_x=5, max_y=50, min_y=-5, visualization=True ) print( 'The maximum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 ' f"and 50 > y > - 5 found via hill climbing: {local_min.score()}" ) def lowerCAmelCase_ ( snake_case_ : int , snake_case_ : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' return (3 * x**2) - (6 * y) SCREAMING_SNAKE_CASE_: str =SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) SCREAMING_SNAKE_CASE_: Tuple =simulated_annealing(prob, find_max=False, visualization=True) print( 'The minimum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: ' f"{local_min.score()}" ) SCREAMING_SNAKE_CASE_: List[str] =SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) SCREAMING_SNAKE_CASE_: Tuple =simulated_annealing(prob, find_max=True, visualization=True) print( 'The maximum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: ' f"{local_min.score()}" )
1
'''simple docstring''' from __future__ import annotations from bisect import bisect_left from functools import total_ordering from heapq import merge @total_ordering class UpperCAmelCase_ ( __lowercase ): def __lt__( self : Optional[int] , UpperCAmelCase__ : List[str] ) -> List[Any]: return self[-1] < other[-1] def __eq__( self : str , UpperCAmelCase__ : List[str] ) -> Tuple: return self[-1] == other[-1] def a_ ( lowerCamelCase : list ): lowerCAmelCase = [] # sort into stacks for element in collection: lowerCAmelCase = Stack([element] ) lowerCAmelCase = bisect_left(lowerCamelCase , lowerCamelCase ) if i != len(lowerCamelCase ): stacks[i].append(lowerCamelCase ) else: stacks.append(lowerCamelCase ) # use a heap-based merge to merge stack efficiently lowerCAmelCase = merge(*(reversed(lowerCamelCase ) for stack in stacks) ) return collection if __name__ == "__main__": __snake_case =input("""Enter numbers separated by a comma:\n""").strip() __snake_case =[int(item) for item in user_input.split(""",""")] print(patience_sort(unsorted))
4
0
'''simple docstring''' import argparse import OmegaConf import torch from diffusers import DDIMScheduler, LDMPipeline, UNetLDMModel, VQModel def _SCREAMING_SNAKE_CASE (A , A , A ) -> List[Any]: """simple docstring""" lowercase__ = OmegaConf.load(A ) lowercase__ = torch.load(A , map_location='''cpu''' )['''model'''] lowercase__ = list(state_dict.keys() ) # extract state_dict for VQVAE lowercase__ = {} lowercase__ = '''first_stage_model.''' for key in keys: if key.startswith(A ): lowercase__ = state_dict[key] # extract state_dict for UNetLDM lowercase__ = {} lowercase__ = '''model.diffusion_model.''' for key in keys: if key.startswith(A ): lowercase__ = state_dict[key] lowercase__ = config.model.params.first_stage_config.params lowercase__ = config.model.params.unet_config.params lowercase__ = VQModel(**A ).eval() vqvae.load_state_dict(A ) lowercase__ = UNetLDMModel(**A ).eval() unet.load_state_dict(A ) lowercase__ = DDIMScheduler( timesteps=config.model.params.timesteps , beta_schedule='''scaled_linear''' , beta_start=config.model.params.linear_start , beta_end=config.model.params.linear_end , clip_sample=A , ) lowercase__ = LDMPipeline(A , A , A ) pipeline.save_pretrained(A ) if __name__ == "__main__": lowerCamelCase : int = argparse.ArgumentParser() parser.add_argument('--checkpoint_path', type=str, required=True) parser.add_argument('--config_path', type=str, required=True) parser.add_argument('--output_path', type=str, required=True) lowerCamelCase : Dict = parser.parse_args() convert_ldm_original(args.checkpoint_path, args.config_path, args.output_path)
2
'''simple docstring''' import datasets from .nmt_bleu import compute_bleu # From: https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py __snake_case ="""\ @INPROCEEDINGS{Papineni02bleu:a, author = {Kishore Papineni and Salim Roukos and Todd Ward and Wei-jing Zhu}, title = {BLEU: a Method for Automatic Evaluation of Machine Translation}, booktitle = {}, year = {2002}, pages = {311--318} } @inproceedings{lin-och-2004-orange, title = \"{ORANGE}: a Method for Evaluating Automatic Evaluation Metrics for Machine Translation\", author = \"Lin, Chin-Yew and Och, Franz Josef\", booktitle = \"{COLING} 2004: Proceedings of the 20th International Conference on Computational Linguistics\", month = \"aug 23{--}aug 27\", year = \"2004\", address = \"Geneva, Switzerland\", publisher = \"COLING\", url = \"https://www.aclweb.org/anthology/C04-1072\", pages = \"501--507\", } """ __snake_case ="""\ BLEU (bilingual evaluation understudy) is an algorithm for evaluating the quality of text which has been machine-translated from one natural language to another. Quality is considered to be the correspondence between a machine's output and that of a human: \"the closer a machine translation is to a professional human translation, the better it is\" – this is the central idea behind BLEU. BLEU was one of the first metrics to claim a high correlation with human judgements of quality, and remains one of the most popular automated and inexpensive metrics. Scores are calculated for individual translated segments—generally sentences—by comparing them with a set of good quality reference translations. Those scores are then averaged over the whole corpus to reach an estimate of the translation's overall quality. Intelligibility or grammatical correctness are not taken into account[citation needed]. BLEU's output is always a number between 0 and 1. This value indicates how similar the candidate text is to the reference texts, with values closer to 1 representing more similar texts. Few human translations will attain a score of 1, since this would indicate that the candidate is identical to one of the reference translations. For this reason, it is not necessary to attain a score of 1. Because there are more opportunities to match, adding additional reference translations will increase the BLEU score. """ __snake_case =""" Computes BLEU score of translated segments against one or more references. Args: predictions: list of translations to score. Each translation should be tokenized into a list of tokens. references: list of lists of references for each translation. Each reference should be tokenized into a list of tokens. max_order: Maximum n-gram order to use when computing BLEU score. smooth: Whether or not to apply Lin et al. 2004 smoothing. Returns: 'bleu': bleu score, 'precisions': geometric mean of n-gram precisions, 'brevity_penalty': brevity penalty, 'length_ratio': ratio of lengths, 'translation_length': translation_length, 'reference_length': reference_length Examples: >>> predictions = [ ... [\"hello\", \"there\", \"general\", \"kenobi\"], # tokenized prediction of the first sample ... [\"foo\", \"bar\", \"foobar\"] # tokenized prediction of the second sample ... ] >>> references = [ ... [[\"hello\", \"there\", \"general\", \"kenobi\"], [\"hello\", \"there\", \"!\"]], # tokenized references for the first sample (2 references) ... [[\"foo\", \"bar\", \"foobar\"]] # tokenized references for the second sample (1 reference) ... ] >>> bleu = datasets.load_metric(\"bleu\") >>> results = bleu.compute(predictions=predictions, references=references) >>> print(results[\"bleu\"]) 1.0 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): def __UpperCAmelCase ( self : Tuple ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string' , id='token' ) , id='sequence' ), 'references': datasets.Sequence( datasets.Sequence(datasets.Value('string' , id='token' ) , id='sequence' ) , id='references' ), } ) , codebase_urls=['https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py'] , reference_urls=[ 'https://en.wikipedia.org/wiki/BLEU', 'https://towardsdatascience.com/evaluating-text-output-in-nlp-bleu-at-your-own-risk-e8609665a213', ] , ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Any , UpperCAmelCase__ : Union[str, Any]=4 , UpperCAmelCase__ : Optional[int]=False ) -> int: lowerCAmelCase = compute_bleu( reference_corpus=UpperCAmelCase__ , translation_corpus=UpperCAmelCase__ , max_order=UpperCAmelCase__ , smooth=UpperCAmelCase__ ) ((lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase) , (lowerCAmelCase)) = score return { "bleu": bleu, "precisions": precisions, "brevity_penalty": bp, "length_ratio": ratio, "translation_length": translation_length, "reference_length": reference_length, }
4
0
'''simple docstring''' from typing import Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING lowercase : str = logging.get_logger(__name__) @add_end_docstrings(__snake_case ) class A ( __snake_case ): def __init__( self , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> List[Any]: """simple docstring""" super().__init__(*SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) self.check_model_type(SCREAMING_SNAKE_CASE ) def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE=None , SCREAMING_SNAKE_CASE=None , SCREAMING_SNAKE_CASE=None , **SCREAMING_SNAKE_CASE ) -> Tuple: """simple docstring""" A, A : Dict = {}, {} if padding is not None: A : List[str] = padding if truncation is not None: A : Dict = truncation if top_k is not None: A : Optional[Any] = top_k return preprocess_params, {}, postprocess_params def __call__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = None , **SCREAMING_SNAKE_CASE ) -> Dict: """simple docstring""" if isinstance(SCREAMING_SNAKE_CASE , (Image.Image, str) ) and isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): A : int = {'''image''': image, '''question''': question} else: A : Any = image A : Any = super().__call__(SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) return results def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=False , SCREAMING_SNAKE_CASE=False ) -> Any: """simple docstring""" A : Union[str, Any] = load_image(inputs['''image'''] ) A : Optional[Any] = self.tokenizer( inputs['''question'''] , return_tensors=self.framework , padding=SCREAMING_SNAKE_CASE , truncation=SCREAMING_SNAKE_CASE ) A : Dict = self.image_processor(images=SCREAMING_SNAKE_CASE , return_tensors=self.framework ) model_inputs.update(SCREAMING_SNAKE_CASE ) return model_inputs def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE ) -> List[str]: """simple docstring""" A : List[Any] = self.model(**SCREAMING_SNAKE_CASE ) return model_outputs def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=5 ) -> int: """simple docstring""" if top_k > self.model.config.num_labels: A : Dict = self.model.config.num_labels if self.framework == "pt": A : Optional[int] = model_outputs.logits.sigmoid()[0] A, A : int = probs.topk(SCREAMING_SNAKE_CASE ) else: raise ValueError(F'Unsupported framework: {self.framework}' ) A : int = scores.tolist() A : List[str] = ids.tolist() return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE )]
3
'''simple docstring''' from sklearn.metrics import fa_score, matthews_corrcoef import datasets from .record_evaluation import evaluate as evaluate_record __snake_case ="""\ @article{wang2019superglue, title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems}, author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R}, journal={arXiv preprint arXiv:1905.00537}, year={2019} } """ __snake_case ="""\ SuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after GLUE with a new set of more difficult language understanding tasks, improved resources, and a new public leaderboard. """ __snake_case =""" Compute SuperGLUE evaluation metric associated to each SuperGLUE dataset. Args: predictions: list of predictions to score. Depending on the SuperGlUE subset: - for 'record': list of question-answer dictionaries with the following keys: - 'idx': index of the question as specified by the dataset - 'prediction_text': the predicted answer text - for 'multirc': list of question-answer dictionaries with the following keys: - 'idx': index of the question-answer pair as specified by the dataset - 'prediction': the predicted answer label - otherwise: list of predicted labels references: list of reference labels. Depending on the SuperGLUE subset: - for 'record': list of question-answers dictionaries with the following keys: - 'idx': index of the question as specified by the dataset - 'answers': list of possible answers - otherwise: list of reference labels Returns: depending on the SuperGLUE subset: - for 'record': - 'exact_match': Exact match between answer and gold answer - 'f1': F1 score - for 'multirc': - 'exact_match': Exact match between answer and gold answer - 'f1_m': Per-question macro-F1 score - 'f1_a': Average F1 score over all answers - for 'axb': 'matthews_correlation': Matthew Correlation - for 'cb': - 'accuracy': Accuracy - 'f1': F1 score - for all others: - 'accuracy': Accuracy Examples: >>> super_glue_metric = datasets.load_metric('super_glue', 'copa') # any of [\"copa\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"boolq\", \"axg\"] >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'cb') >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0, 'f1': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'record') >>> predictions = [{'idx': {'passage': 0, 'query': 0}, 'prediction_text': 'answer'}] >>> references = [{'idx': {'passage': 0, 'query': 0}, 'answers': ['answer', 'another_answer']}] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 1.0, 'f1': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'multirc') >>> predictions = [{'idx': {'answer': 0, 'paragraph': 0, 'question': 0}, 'prediction': 0}, {'idx': {'answer': 1, 'paragraph': 2, 'question': 3}, 'prediction': 1}] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 1.0, 'f1_m': 1.0, 'f1_a': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'axb') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'matthews_correlation': 1.0} """ def a_ ( lowerCamelCase : str , lowerCamelCase : Union[str, Any] ): return float((preds == labels).mean() ) def a_ ( lowerCamelCase : Union[str, Any] , lowerCamelCase : Dict , lowerCamelCase : str="binary" ): lowerCAmelCase = simple_accuracy(lowerCamelCase , lowerCamelCase ) lowerCAmelCase = float(fa_score(y_true=lowerCamelCase , y_pred=lowerCamelCase , average=lowerCamelCase ) ) return { "accuracy": acc, "f1": fa, } def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : List[Any] ): lowerCAmelCase = {} for id_pred, label in zip(lowerCamelCase , lowerCamelCase ): lowerCAmelCase = f'''{id_pred['idx']['paragraph']}-{id_pred['idx']['question']}''' lowerCAmelCase = id_pred['prediction'] if question_id in question_map: question_map[question_id].append((pred, label) ) else: lowerCAmelCase = [(pred, label)] lowerCAmelCase , lowerCAmelCase = [], [] for question, preds_labels in question_map.items(): lowerCAmelCase , lowerCAmelCase = zip(*lowerCamelCase ) lowerCAmelCase = fa_score(y_true=lowerCamelCase , y_pred=lowerCamelCase , average='macro' ) fas.append(lowerCamelCase ) lowerCAmelCase = int(sum(pred == label for pred, label in preds_labels ) == len(lowerCamelCase ) ) ems.append(lowerCamelCase ) lowerCAmelCase = float(sum(lowerCamelCase ) / len(lowerCamelCase ) ) lowerCAmelCase = sum(lowerCamelCase ) / len(lowerCamelCase ) lowerCAmelCase = float(fa_score(y_true=lowerCamelCase , y_pred=[id_pred['prediction'] for id_pred in ids_preds] ) ) return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a} @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): def __UpperCAmelCase ( self : List[str] ) -> List[Any]: if self.config_name not in [ "boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg", ]: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , codebase_urls=[] , reference_urls=[] , format='numpy' if not self.config_name == 'record' and not self.config_name == 'multirc' else None , ) def __UpperCAmelCase ( self : Union[str, Any] ) -> str: if self.config_name == "record": return { "predictions": { "idx": { "passage": datasets.Value('int64' ), "query": datasets.Value('int64' ), }, "prediction_text": datasets.Value('string' ), }, "references": { "idx": { "passage": datasets.Value('int64' ), "query": datasets.Value('int64' ), }, "answers": datasets.Sequence(datasets.Value('string' ) ), }, } elif self.config_name == "multirc": return { "predictions": { "idx": { "answer": datasets.Value('int64' ), "paragraph": datasets.Value('int64' ), "question": datasets.Value('int64' ), }, "prediction": datasets.Value('int64' ), }, "references": datasets.Value('int64' ), } else: return { "predictions": datasets.Value('int64' ), "references": datasets.Value('int64' ), } def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : Any , UpperCAmelCase__ : Optional[Any] ) -> Any: if self.config_name == "axb": return {"matthews_correlation": matthews_corrcoef(UpperCAmelCase__ , UpperCAmelCase__ )} elif self.config_name == "cb": return acc_and_fa(UpperCAmelCase__ , UpperCAmelCase__ , fa_avg='macro' ) elif self.config_name == "record": lowerCAmelCase = [ { 'qas': [ {'id': ref['idx']['query'], 'answers': [{'text': ans} for ans in ref['answers']]} for ref in references ] } ] lowerCAmelCase = {pred['idx']['query']: pred['prediction_text'] for pred in predictions} return evaluate_record(UpperCAmelCase__ , UpperCAmelCase__ )[0] elif self.config_name == "multirc": return evaluate_multirc(UpperCAmelCase__ , UpperCAmelCase__ ) elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]: return {"accuracy": simple_accuracy(UpperCAmelCase__ , UpperCAmelCase__ )} else: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]' )
4
0
import argparse import gc import json import os import shutil import warnings import torch from transformers import LlamaConfig, LlamaForCausalLM, LlamaTokenizer try: from transformers import LlamaTokenizerFast except ImportError as e: warnings.warn(e) warnings.warn( '''The converted tokenizer will be the `slow` tokenizer. To use the fast, update your `tokenizers` library and re-run the tokenizer conversion''' ) UpperCAmelCase__ = None UpperCAmelCase__ = { '''7B''': 1_1008, '''13B''': 1_3824, '''30B''': 1_7920, '''65B''': 2_2016, '''70B''': 2_8672, } UpperCAmelCase__ = { '''7B''': 1, '''7Bf''': 1, '''13B''': 2, '''13Bf''': 2, '''30B''': 4, '''65B''': 8, '''70B''': 8, '''70Bf''': 8, } def UpperCAmelCase_ ( __snake_case , __snake_case=1 , __snake_case=256 ) -> Optional[int]: """simple docstring""" return multiple_of * ((int(ffn_dim_multiplier * int(8 * n / 3 ) ) + multiple_of - 1) // multiple_of) def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" with open(__snake_case , '''r''' ) as f: return json.load(__snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> List[str]: """simple docstring""" with open(__snake_case , '''w''' ) as f: json.dump(__snake_case , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case=True ) -> Optional[int]: """simple docstring""" os.makedirs(__snake_case , exist_ok=__snake_case ) _lowercase =os.path.join(__snake_case , '''tmp''' ) os.makedirs(__snake_case , exist_ok=__snake_case ) _lowercase =read_json(os.path.join(__snake_case , '''params.json''' ) ) _lowercase =NUM_SHARDS[model_size] _lowercase =params['''n_layers'''] _lowercase =params['''n_heads'''] _lowercase =n_heads // num_shards _lowercase =params['''dim'''] _lowercase =dim // n_heads _lowercase =1_00_00.0 _lowercase =1.0 / (base ** (torch.arange(0 , __snake_case , 2 ).float() / dims_per_head)) if "n_kv_heads" in params: _lowercase =params['''n_kv_heads'''] # for GQA / MQA _lowercase =n_heads_per_shard // num_key_value_heads _lowercase =dim // num_key_value_heads else: # compatibility with other checkpoints _lowercase =n_heads _lowercase =n_heads_per_shard _lowercase =dim # permute for sliced rotary def permute(__snake_case , __snake_case=n_heads , __snake_case=dim , __snake_case=dim ): return w.view(__snake_case , dima // n_heads // 2 , 2 , __snake_case ).transpose(1 , 2 ).reshape(__snake_case , __snake_case ) print(F"Fetching all parameters from the checkpoint at {input_base_path}." ) # Load weights if model_size == "7B": # Not sharded # (The sharded implementation would also work, but this is simpler.) _lowercase =torch.load(os.path.join(__snake_case , '''consolidated.00.pth''' ) , map_location='''cpu''' ) else: # Sharded _lowercase =[ torch.load(os.path.join(__snake_case , F"consolidated.{i:02d}.pth" ) , map_location='''cpu''' ) for i in range(__snake_case ) ] _lowercase =0 _lowercase ={'''weight_map''': {}} for layer_i in range(__snake_case ): _lowercase =F"pytorch_model-{layer_i + 1}-of-{n_layers + 1}.bin" if model_size == "7B": # Unsharded _lowercase ={ F"model.layers.{layer_i}.self_attn.q_proj.weight": permute( loaded[F"layers.{layer_i}.attention.wq.weight"] ), F"model.layers.{layer_i}.self_attn.k_proj.weight": permute( loaded[F"layers.{layer_i}.attention.wk.weight"] ), F"model.layers.{layer_i}.self_attn.v_proj.weight": loaded[F"layers.{layer_i}.attention.wv.weight"], F"model.layers.{layer_i}.self_attn.o_proj.weight": loaded[F"layers.{layer_i}.attention.wo.weight"], F"model.layers.{layer_i}.mlp.gate_proj.weight": loaded[F"layers.{layer_i}.feed_forward.w1.weight"], F"model.layers.{layer_i}.mlp.down_proj.weight": loaded[F"layers.{layer_i}.feed_forward.w2.weight"], F"model.layers.{layer_i}.mlp.up_proj.weight": loaded[F"layers.{layer_i}.feed_forward.w3.weight"], F"model.layers.{layer_i}.input_layernorm.weight": loaded[F"layers.{layer_i}.attention_norm.weight"], F"model.layers.{layer_i}.post_attention_layernorm.weight": loaded[F"layers.{layer_i}.ffn_norm.weight"], } else: # Sharded # Note that attention.w{q,k,v,o}, feed_fordward.w[1,2,3], attention_norm.weight and ffn_norm.weight share # the same storage object, saving attention_norm and ffn_norm will save other weights too, which is # redundant as other weights will be stitched from multiple shards. To avoid that, they are cloned. _lowercase ={ F"model.layers.{layer_i}.input_layernorm.weight": loaded[0][ F"layers.{layer_i}.attention_norm.weight" ].clone(), F"model.layers.{layer_i}.post_attention_layernorm.weight": loaded[0][ F"layers.{layer_i}.ffn_norm.weight" ].clone(), } _lowercase =permute( torch.cat( [ loaded[i][F"layers.{layer_i}.attention.wq.weight"].view(__snake_case , __snake_case , __snake_case ) for i in range(__snake_case ) ] , dim=0 , ).reshape(__snake_case , __snake_case ) ) _lowercase =permute( torch.cat( [ loaded[i][F"layers.{layer_i}.attention.wk.weight"].view( __snake_case , __snake_case , __snake_case ) for i in range(__snake_case ) ] , dim=0 , ).reshape(__snake_case , __snake_case ) , __snake_case , __snake_case , __snake_case , ) _lowercase =torch.cat( [ loaded[i][F"layers.{layer_i}.attention.wv.weight"].view( __snake_case , __snake_case , __snake_case ) for i in range(__snake_case ) ] , dim=0 , ).reshape(__snake_case , __snake_case ) _lowercase =torch.cat( [loaded[i][F"layers.{layer_i}.attention.wo.weight"] for i in range(__snake_case )] , dim=1 ) _lowercase =torch.cat( [loaded[i][F"layers.{layer_i}.feed_forward.w1.weight"] for i in range(__snake_case )] , dim=0 ) _lowercase =torch.cat( [loaded[i][F"layers.{layer_i}.feed_forward.w2.weight"] for i in range(__snake_case )] , dim=1 ) _lowercase =torch.cat( [loaded[i][F"layers.{layer_i}.feed_forward.w3.weight"] for i in range(__snake_case )] , dim=0 ) _lowercase =inv_freq for k, v in state_dict.items(): _lowercase =filename param_count += v.numel() torch.save(__snake_case , os.path.join(__snake_case , __snake_case ) ) _lowercase =F"pytorch_model-{n_layers + 1}-of-{n_layers + 1}.bin" if model_size == "7B": # Unsharded _lowercase ={ '''model.embed_tokens.weight''': loaded['''tok_embeddings.weight'''], '''model.norm.weight''': loaded['''norm.weight'''], '''lm_head.weight''': loaded['''output.weight'''], } else: _lowercase ={ '''model.norm.weight''': loaded[0]['''norm.weight'''], '''model.embed_tokens.weight''': torch.cat( [loaded[i]['''tok_embeddings.weight'''] for i in range(__snake_case )] , dim=1 ), '''lm_head.weight''': torch.cat([loaded[i]['''output.weight'''] for i in range(__snake_case )] , dim=0 ), } for k, v in state_dict.items(): _lowercase =filename param_count += v.numel() torch.save(__snake_case , os.path.join(__snake_case , __snake_case ) ) # Write configs _lowercase ={'''total_size''': param_count * 2} write_json(__snake_case , os.path.join(__snake_case , '''pytorch_model.bin.index.json''' ) ) _lowercase =params['''ffn_dim_multiplier'''] if '''ffn_dim_multiplier''' in params else 1 _lowercase =params['''multiple_of'''] if '''multiple_of''' in params else 256 _lowercase =LlamaConfig( hidden_size=__snake_case , intermediate_size=compute_intermediate_size(__snake_case , __snake_case , __snake_case ) , num_attention_heads=params['''n_heads'''] , num_hidden_layers=params['''n_layers'''] , rms_norm_eps=params['''norm_eps'''] , num_key_value_heads=__snake_case , ) config.save_pretrained(__snake_case ) # Make space so we can load the model properly now. del state_dict del loaded gc.collect() print('''Loading the checkpoint in a Llama model.''' ) _lowercase =LlamaForCausalLM.from_pretrained(__snake_case , torch_dtype=torch.floataa , low_cpu_mem_usage=__snake_case ) # Avoid saving this as part of the config. del model.config._name_or_path print('''Saving in the Transformers format.''' ) model.save_pretrained(__snake_case , safe_serialization=__snake_case ) shutil.rmtree(__snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> List[str]: """simple docstring""" _lowercase =LlamaTokenizer if LlamaTokenizerFast is None else LlamaTokenizerFast print(F"Saving a {tokenizer_class.__name__} to {tokenizer_path}." ) _lowercase =tokenizer_class(__snake_case ) tokenizer.save_pretrained(__snake_case ) def UpperCAmelCase_ ( ) -> Tuple: """simple docstring""" _lowercase =argparse.ArgumentParser() parser.add_argument( '''--input_dir''' , help='''Location of LLaMA weights, which contains tokenizer.model and model folders''' , ) parser.add_argument( '''--model_size''' , choices=['''7B''', '''7Bf''', '''13B''', '''13Bf''', '''30B''', '''65B''', '''70B''', '''70Bf''', '''tokenizer_only'''] , ) parser.add_argument( '''--output_dir''' , help='''Location to write HF model and tokenizer''' , ) parser.add_argument('''--safe_serialization''' , type=__snake_case , help='''Whether or not to save using `safetensors`.''' ) _lowercase =parser.parse_args() if args.model_size != "tokenizer_only": write_model( model_path=args.output_dir , input_base_path=os.path.join(args.input_dir , args.model_size ) , model_size=args.model_size , safe_serialization=args.safe_serialization , ) _lowercase =os.path.join(args.input_dir , '''tokenizer.model''' ) write_tokenizer(args.output_dir , __snake_case ) if __name__ == "__main__": main()
5
'''simple docstring''' print((lambda quine: quine % quine)("""print((lambda quine: quine %% quine)(%r))"""))
4
0
def __lowerCAmelCase ( a__ ) -> str: return "".join([hex(a__ )[2:].zfill(2 ).upper() for byte in list(a__ )] ) def __lowerCAmelCase ( a__ ) -> bytes: # Check data validity, following RFC3548 # https://www.ietf.org/rfc/rfc3548.txt if (len(a__ ) % 2) != 0: raise ValueError( '''Base16 encoded data is invalid: Data does not have an even number of hex digits.''' ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(a__ ) <= set('''0123456789ABCDEF''' ): raise ValueError( '''Base16 encoded data is invalid: Data is not uppercase hex or it contains invalid characters.''' ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(a__ ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
6
'''simple docstring''' import os __snake_case ={"""I""": 1, """V""": 5, """X""": 10, """L""": 50, """C""": 100, """D""": 500, """M""": 1_000} def a_ ( lowerCamelCase : str ): lowerCAmelCase = 0 lowerCAmelCase = 0 while index < len(lowerCamelCase ) - 1: lowerCAmelCase = SYMBOLS[numerals[index]] lowerCAmelCase = SYMBOLS[numerals[index + 1]] if current_value < next_value: total_value -= current_value else: total_value += current_value index += 1 total_value += SYMBOLS[numerals[index]] return total_value def a_ ( lowerCamelCase : int ): lowerCAmelCase = '' lowerCAmelCase = num // 1000 numerals += m_count * "M" num %= 1000 lowerCAmelCase = num // 100 if c_count == 9: numerals += "CM" c_count -= 9 elif c_count == 4: numerals += "CD" c_count -= 4 if c_count >= 5: numerals += "D" c_count -= 5 numerals += c_count * "C" num %= 100 lowerCAmelCase = num // 10 if x_count == 9: numerals += "XC" x_count -= 9 elif x_count == 4: numerals += "XL" x_count -= 4 if x_count >= 5: numerals += "L" x_count -= 5 numerals += x_count * "X" num %= 10 if num == 9: numerals += "IX" num -= 9 elif num == 4: numerals += "IV" num -= 4 if num >= 5: numerals += "V" num -= 5 numerals += num * "I" return numerals def a_ ( lowerCamelCase : str = "/p089_roman.txt" ): lowerCAmelCase = 0 with open(os.path.dirname(lowerCamelCase ) + roman_numerals_filename ) as filea: lowerCAmelCase = filea.readlines() for line in lines: lowerCAmelCase = line.strip() lowerCAmelCase = parse_roman_numerals(lowerCamelCase ) lowerCAmelCase = generate_roman_numerals(lowerCamelCase ) savings += len(lowerCamelCase ) - len(lowerCamelCase ) return savings if __name__ == "__main__": print(F'''{solution() = }''')
4
0
import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(_UpperCAmelCase ) , 'Tatoeba directory does not exist.' ) class A ( unittest.TestCase ): """simple docstring""" @cached_property def snake_case__ ( self : str )-> Union[str, Any]: '''simple docstring''' A__ = tempfile.mkdtemp() return TatoebaConverter(save_dir=lowercase_ ) @slow def snake_case__ ( self : List[str] )-> Optional[int]: '''simple docstring''' self.resolver.convert_models(['heb-eng'] ) @slow def snake_case__ ( self : int )-> Optional[int]: '''simple docstring''' A__ , A__ = self.resolver.write_model_card('opus-mt-he-en',dry_run=lowercase_ ) assert mmeta["long_pair"] == "heb-eng"
7
'''simple docstring''' import itertools import random import unittest import numpy as np from transformers import ASTFeatureExtractor from transformers.testing_utils import require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin __snake_case =random.Random() if is_torch_available(): import torch def a_ ( lowerCamelCase : Dict , lowerCamelCase : Dict=1.0 , lowerCamelCase : List[Any]=None , lowerCamelCase : Union[str, Any]=None ): if rng is None: lowerCAmelCase = global_rng lowerCAmelCase = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class UpperCAmelCase_ ( unittest.TestCase ): def __init__( self : List[Any] , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : List[str]=7 , UpperCAmelCase__ : int=4_0_0 , UpperCAmelCase__ : int=2_0_0_0 , UpperCAmelCase__ : List[str]=1 , UpperCAmelCase__ : Tuple=0.0 , UpperCAmelCase__ : Tuple=1_6_0_0_0 , UpperCAmelCase__ : Tuple=True , UpperCAmelCase__ : Union[str, Any]=True , ) -> Any: lowerCAmelCase = parent lowerCAmelCase = batch_size lowerCAmelCase = min_seq_length lowerCAmelCase = max_seq_length lowerCAmelCase = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) lowerCAmelCase = feature_size lowerCAmelCase = padding_value lowerCAmelCase = sampling_rate lowerCAmelCase = return_attention_mask lowerCAmelCase = do_normalize def __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def __UpperCAmelCase ( self : str , UpperCAmelCase__ : Union[str, Any]=False , UpperCAmelCase__ : Union[str, Any]=False ) -> Optional[Any]: def _flatten(UpperCAmelCase__ : int ): return list(itertools.chain(*UpperCAmelCase__ ) ) if equal_length: lowerCAmelCase = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size lowerCAmelCase = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: lowerCAmelCase = [np.asarray(UpperCAmelCase__ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): lowerCamelCase : Dict = ASTFeatureExtractor def __UpperCAmelCase ( self : str ) -> Optional[int]: lowerCAmelCase = ASTFeatureExtractionTester(self ) def __UpperCAmelCase ( self : Optional[int] ) -> Optional[int]: # Tests that all call wrap to encode_plus and batch_encode_plus lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 lowerCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] lowerCAmelCase = [np.asarray(UpperCAmelCase__ ) for speech_input in speech_inputs] # Test not batched input lowerCAmelCase = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values lowerCAmelCase = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1E-3 ) ) # Test batched lowerCAmelCase = feat_extract(UpperCAmelCase__ , padding=UpperCAmelCase__ , return_tensors='np' ).input_values lowerCAmelCase = feat_extract(UpperCAmelCase__ , padding=UpperCAmelCase__ , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__ , UpperCAmelCase__ ): self.assertTrue(np.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. lowerCAmelCase = [floats_list((1, x) )[0] for x in (8_0_0, 8_0_0, 8_0_0)] lowerCAmelCase = np.asarray(UpperCAmelCase__ ) lowerCAmelCase = feat_extract(UpperCAmelCase__ , return_tensors='np' ).input_values lowerCAmelCase = feat_extract(UpperCAmelCase__ , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__ , UpperCAmelCase__ ): self.assertTrue(np.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1E-3 ) ) @require_torch def __UpperCAmelCase ( self : Union[str, Any] ) -> Optional[int]: import torch lowerCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCAmelCase = np.random.rand(1_0_0 ).astype(np.floataa ) lowerCAmelCase = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: lowerCAmelCase = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) lowerCAmelCase = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def __UpperCAmelCase ( self : int , UpperCAmelCase__ : str ) -> Tuple: from datasets import load_dataset lowerCAmelCase = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech lowerCAmelCase = ds.sort('id' ).select(range(UpperCAmelCase__ ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] @require_torch def __UpperCAmelCase ( self : str ) -> Optional[Any]: # fmt: off lowerCAmelCase = torch.tensor( [-0.9_894, -1.2_776, -0.9_066, -1.2_776, -0.9_349, -1.2_609, -1.0_386, -1.2_776, -1.1_561, -1.2_776, -1.2_052, -1.2_723, -1.2_190, -1.2_132, -1.2_776, -1.1_133, -1.1_953, -1.1_343, -1.1_584, -1.2_203, -1.1_770, -1.2_474, -1.2_381, -1.1_936, -0.9_270, -0.8_317, -0.8_049, -0.7_706, -0.7_565, -0.7_869] ) # fmt: on lowerCAmelCase = self._load_datasamples(1 ) lowerCAmelCase = ASTFeatureExtractor() lowerCAmelCase = feature_extractor(UpperCAmelCase__ , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 1_0_2_4, 1_2_8) ) self.assertTrue(torch.allclose(input_values[0, 0, :3_0] , UpperCAmelCase__ , atol=1E-4 ) )
4
0
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, TensorType lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = { '''openai/imagegpt-small''': '''''', '''openai/imagegpt-medium''': '''''', '''openai/imagegpt-large''': '''''', } class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = "imagegpt" SCREAMING_SNAKE_CASE : List[str] = ["past_key_values"] SCREAMING_SNAKE_CASE : List[Any] = { "hidden_size": "n_embd", "max_position_embeddings": "n_positions", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : Union[str, Any] , _UpperCamelCase : Tuple=5_1_2 + 1 , _UpperCamelCase : List[Any]=3_2 * 3_2 , _UpperCamelCase : Optional[Any]=5_1_2 , _UpperCamelCase : Any=2_4 , _UpperCamelCase : Union[str, Any]=8 , _UpperCamelCase : List[str]=None , _UpperCamelCase : List[str]="quick_gelu" , _UpperCamelCase : List[Any]=0.1 , _UpperCamelCase : Optional[Any]=0.1 , _UpperCamelCase : Any=0.1 , _UpperCamelCase : Optional[int]=1e-5 , _UpperCamelCase : Dict=0.02 , _UpperCamelCase : List[Any]=True , _UpperCamelCase : int=True , _UpperCamelCase : Optional[int]=False , _UpperCamelCase : Tuple=False , _UpperCamelCase : Dict=False , **_UpperCamelCase : Dict , ) ->str: snake_case_ = vocab_size snake_case_ = n_positions snake_case_ = n_embd snake_case_ = n_layer snake_case_ = n_head snake_case_ = n_inner snake_case_ = activation_function snake_case_ = resid_pdrop snake_case_ = embd_pdrop snake_case_ = attn_pdrop snake_case_ = layer_norm_epsilon snake_case_ = initializer_range snake_case_ = scale_attn_weights snake_case_ = use_cache snake_case_ = scale_attn_by_inverse_layer_idx snake_case_ = reorder_and_upcast_attn snake_case_ = tie_word_embeddings super().__init__(tie_word_embeddings=_UpperCamelCase , **_UpperCamelCase ) class snake_case_ ( __A ): '''simple docstring''' @property def snake_case__( self : str ) ->Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ] ) def snake_case__( self : str , _UpperCamelCase : "FeatureExtractionMixin" , _UpperCamelCase : int = 1 , _UpperCamelCase : int = -1 , _UpperCamelCase : bool = False , _UpperCamelCase : Optional["TensorType"] = None , _UpperCamelCase : int = 3 , _UpperCamelCase : int = 3_2 , _UpperCamelCase : int = 3_2 , ) ->Mapping[str, Any]: snake_case_ = self._generate_dummy_images(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) snake_case_ = dict(preprocessor(images=_UpperCamelCase , return_tensors=_UpperCamelCase ) ) return inputs
8
'''simple docstring''' import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : str ) -> List[Any]: lowerCAmelCase = torch.nn.Linear(1_0 , 1_0 ) lowerCAmelCase = torch.optim.SGD(model.parameters() , 0.1 ) lowerCAmelCase = Accelerator() lowerCAmelCase = accelerator.prepare(UpperCAmelCase__ ) try: pickle.loads(pickle.dumps(UpperCAmelCase__ ) ) except Exception as e: self.fail(F'''Accelerated optimizer pickling failed with {e}''' ) AcceleratorState._reset_state()
4
0
from argparse import ArgumentParser, Namespace from ..utils import logging from . import BaseTransformersCLICommand def _UpperCamelCase ( lowercase__ ): return ConvertCommand( args.model_type , args.tf_checkpoint , args.pytorch_dump_output , args.config , args.finetuning_task_name ) __lowerCAmelCase : Optional[Any] ='\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n' class _lowercase ( A__ ): '''simple docstring''' @staticmethod def __magic_name__( lowerCAmelCase__ :ArgumentParser ) -> Any: __SCREAMING_SNAKE_CASE : List[Any] = parser.add_parser( '''convert''' , help='''CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.''' , ) train_parser.add_argument('''--model_type''' , type=lowerCAmelCase__ , required=lowerCAmelCase__ , help='''Model\'s type.''' ) train_parser.add_argument( '''--tf_checkpoint''' , type=lowerCAmelCase__ , required=lowerCAmelCase__ , help='''TensorFlow checkpoint path or folder.''' ) train_parser.add_argument( '''--pytorch_dump_output''' , type=lowerCAmelCase__ , required=lowerCAmelCase__ , help='''Path to the PyTorch saved model output.''' ) train_parser.add_argument('''--config''' , type=lowerCAmelCase__ , default='''''' , help='''Configuration file path or folder.''' ) train_parser.add_argument( '''--finetuning_task_name''' , type=lowerCAmelCase__ , default=lowerCAmelCase__ , help='''Optional fine-tuning task name if the TF model was a finetuned model.''' , ) train_parser.set_defaults(func=lowerCAmelCase__ ) def __init__( self :int , lowerCAmelCase__ :str , lowerCAmelCase__ :str , lowerCAmelCase__ :str , lowerCAmelCase__ :str , lowerCAmelCase__ :str , *lowerCAmelCase__ :List[Any] , ) -> Dict: __SCREAMING_SNAKE_CASE : str = logging.get_logger('''transformers-cli/converting''' ) self._logger.info(f'''Loading model {model_type}''' ) __SCREAMING_SNAKE_CASE : Optional[int] = model_type __SCREAMING_SNAKE_CASE : str = tf_checkpoint __SCREAMING_SNAKE_CASE : Optional[int] = pytorch_dump_output __SCREAMING_SNAKE_CASE : str = config __SCREAMING_SNAKE_CASE : Optional[int] = finetuning_task_name def __magic_name__( self :int ) -> List[str]: if self._model_type == "albert": try: from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(lowerCAmelCase__ ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "bert": try: from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(lowerCAmelCase__ ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "funnel": try: from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(lowerCAmelCase__ ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "t5": try: from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch except ImportError: raise ImportError(lowerCAmelCase__ ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "gpt": from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import ( convert_openai_checkpoint_to_pytorch, ) convert_openai_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "transfo_xl": try: from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import ( convert_transfo_xl_checkpoint_to_pytorch, ) except ImportError: raise ImportError(lowerCAmelCase__ ) if "ckpt" in self._tf_checkpoint.lower(): __SCREAMING_SNAKE_CASE : Dict = self._tf_checkpoint __SCREAMING_SNAKE_CASE : int = '''''' else: __SCREAMING_SNAKE_CASE : int = self._tf_checkpoint __SCREAMING_SNAKE_CASE : List[Any] = '''''' convert_transfo_xl_checkpoint_to_pytorch( lowerCAmelCase__ , self._config , self._pytorch_dump_output , lowerCAmelCase__ ) elif self._model_type == "gpt2": try: from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import ( convert_gpta_checkpoint_to_pytorch, ) except ImportError: raise ImportError(lowerCAmelCase__ ) convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "xlnet": try: from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import ( convert_xlnet_checkpoint_to_pytorch, ) except ImportError: raise ImportError(lowerCAmelCase__ ) convert_xlnet_checkpoint_to_pytorch( self._tf_checkpoint , self._config , self._pytorch_dump_output , self._finetuning_task_name ) elif self._model_type == "xlm": from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import ( convert_xlm_checkpoint_to_pytorch, ) convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "lxmert": from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import ( convert_lxmert_checkpoint_to_pytorch, ) convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "rembert": from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import ( convert_rembert_tf_checkpoint_to_pytorch, ) convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) else: raise ValueError( '''--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]''' )
9
'''simple docstring''' import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __snake_case =logging.get_logger(__name__) __snake_case ={ """vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_config_file""": """tokenizer_config.json""", } __snake_case ={ """vocab_file""": {"""facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json"""}, """merges_file""": {"""facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt"""}, """tokenizer_config_file""": { """facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json""" }, } __snake_case ={"""facebook/blenderbot-3B""": 128} class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : List[Any] = VOCAB_FILES_NAMES lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[Any] = ['''input_ids''', '''attention_mask'''] lowerCamelCase : List[Any] = BlenderbotTokenizer def __init__( self : Union[str, Any] , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : List[Any]=None , UpperCAmelCase__ : str="replace" , UpperCAmelCase__ : Dict="<s>" , UpperCAmelCase__ : Tuple="</s>" , UpperCAmelCase__ : Optional[Any]="</s>" , UpperCAmelCase__ : Any="<s>" , UpperCAmelCase__ : List[str]="<unk>" , UpperCAmelCase__ : int="<pad>" , UpperCAmelCase__ : Union[str, Any]="<mask>" , UpperCAmelCase__ : str=False , UpperCAmelCase__ : Union[str, Any]=True , **UpperCAmelCase__ : Optional[int] , ) -> int: super().__init__( UpperCAmelCase__ , UpperCAmelCase__ , tokenizer_file=UpperCAmelCase__ , errors=UpperCAmelCase__ , bos_token=UpperCAmelCase__ , eos_token=UpperCAmelCase__ , sep_token=UpperCAmelCase__ , cls_token=UpperCAmelCase__ , unk_token=UpperCAmelCase__ , pad_token=UpperCAmelCase__ , mask_token=UpperCAmelCase__ , add_prefix_space=UpperCAmelCase__ , trim_offsets=UpperCAmelCase__ , **UpperCAmelCase__ , ) lowerCAmelCase = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('add_prefix_space' , UpperCAmelCase__ ) != add_prefix_space: lowerCAmelCase = getattr(UpperCAmelCase__ , pre_tok_state.pop('type' ) ) lowerCAmelCase = add_prefix_space lowerCAmelCase = pre_tok_class(**UpperCAmelCase__ ) lowerCAmelCase = add_prefix_space lowerCAmelCase = 'post_processor' lowerCAmelCase = getattr(self.backend_tokenizer , UpperCAmelCase__ , UpperCAmelCase__ ) if tokenizer_component_instance: lowerCAmelCase = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: lowerCAmelCase = tuple(state['sep'] ) if "cls" in state: lowerCAmelCase = tuple(state['cls'] ) lowerCAmelCase = False if state.get('add_prefix_space' , UpperCAmelCase__ ) != add_prefix_space: lowerCAmelCase = add_prefix_space lowerCAmelCase = True if state.get('trim_offsets' , UpperCAmelCase__ ) != trim_offsets: lowerCAmelCase = trim_offsets lowerCAmelCase = True if changes_to_apply: lowerCAmelCase = getattr(UpperCAmelCase__ , state.pop('type' ) ) lowerCAmelCase = component_class(**UpperCAmelCase__ ) setattr(self.backend_tokenizer , UpperCAmelCase__ , UpperCAmelCase__ ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def __UpperCAmelCase ( self : Union[str, Any] ) -> str: if self._mask_token is None: if self.verbose: logger.error('Using mask_token, but it is not set yet.' ) return None return str(self._mask_token ) @mask_token.setter def __UpperCAmelCase ( self : int , UpperCAmelCase__ : Optional[Any] ) -> Tuple: lowerCAmelCase = AddedToken(UpperCAmelCase__ , lstrip=UpperCAmelCase__ , rstrip=UpperCAmelCase__ ) if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) else value lowerCAmelCase = value def __UpperCAmelCase ( self : Optional[Any] , *UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : List[str] ) -> BatchEncoding: lowerCAmelCase = kwargs.get('is_split_into_words' , UpperCAmelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*UpperCAmelCase__ , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : List[str] , *UpperCAmelCase__ : str , **UpperCAmelCase__ : List[str] ) -> BatchEncoding: lowerCAmelCase = kwargs.get('is_split_into_words' , UpperCAmelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*UpperCAmelCase__ , **UpperCAmelCase__ ) def __UpperCAmelCase ( self : str , UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[str] = None ) -> Tuple[str]: lowerCAmelCase = self._tokenizer.model.save(UpperCAmelCase__ , name=UpperCAmelCase__ ) return tuple(UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) -> Any: return token_ids_a + [self.eos_token_id] def __UpperCAmelCase ( self : Union[str, Any] , UpperCAmelCase__ : "Conversation" ) -> List[int]: lowerCAmelCase = [] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(' ' + text ) else: # Generated responses should contain them already. inputs.append(UpperCAmelCase__ ) lowerCAmelCase = ' '.join(UpperCAmelCase__ ) lowerCAmelCase = self.encode(UpperCAmelCase__ ) if len(UpperCAmelCase__ ) > self.model_max_length: lowerCAmelCase = input_ids[-self.model_max_length :] logger.warning(F'''Trimmed input from conversation as it was longer than {self.model_max_length} tokens.''' ) return input_ids
4
0
from __future__ import annotations __A = [ [-1, 0], # left [0, -1], # down [1, 0], # right [0, 1], # up ] def lowerCAmelCase_ ( __a , __a , __a , __a , __a , ) -> tuple[list[list[int]], list[list[int]]]: """simple docstring""" lowerCamelCase__: List[str] =[ [0 for col in range(len(grid[0] ) )] for row in range(len(__a ) ) ] # the reference grid lowerCamelCase__: Optional[int] =1 lowerCamelCase__: int =[ [0 for col in range(len(grid[0] ) )] for row in range(len(__a ) ) ] # the action grid lowerCamelCase__: Dict =init[0] lowerCamelCase__: Dict =init[1] lowerCamelCase__: List[str] =0 lowerCamelCase__: int =g + heuristic[x][y] # cost from starting cell to destination cell lowerCamelCase__: Optional[int] =[[f, g, x, y]] lowerCamelCase__: Tuple =False # flag that is set when search is complete lowerCamelCase__: Union[str, Any] =False # flag set if we can't find expand while not found and not resign: if len(__a ) == 0: raise ValueError("Algorithm is unable to find solution" ) else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() lowerCamelCase__: int =cell.pop() lowerCamelCase__: List[str] =next_cell[2] lowerCamelCase__: List[Any] =next_cell[3] lowerCamelCase__: Dict =next_cell[1] if x == goal[0] and y == goal[1]: lowerCamelCase__: Union[str, Any] =True else: for i in range(len(__a ) ): # to try out different valid actions lowerCamelCase__: Any =x + DIRECTIONS[i][0] lowerCamelCase__: List[Any] =y + DIRECTIONS[i][1] if xa >= 0 and xa < len(__a ) and ya >= 0 and ya < len(grid[0] ): if closed[xa][ya] == 0 and grid[xa][ya] == 0: lowerCamelCase__: Optional[int] =g + cost lowerCamelCase__: Optional[int] =ga + heuristic[xa][ya] cell.append([fa, ga, xa, ya] ) lowerCamelCase__: int =1 lowerCamelCase__: Any =i lowerCamelCase__: Tuple =[] lowerCamelCase__: Any =goal[0] lowerCamelCase__: Union[str, Any] =goal[1] invpath.append([x, y] ) # we get the reverse path from here while x != init[0] or y != init[1]: lowerCamelCase__: Any =x - DIRECTIONS[action[x][y]][0] lowerCamelCase__: int =y - DIRECTIONS[action[x][y]][1] lowerCamelCase__: Any =xa lowerCamelCase__: int =ya invpath.append([x, y] ) lowerCamelCase__: str =[] for i in range(len(__a ) ): path.append(invpath[len(__a ) - 1 - i] ) return path, action if __name__ == "__main__": __A = [ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], ] __A = [0, 0] # all coordinates are given in format [y,x] __A = [len(grid) - 1, len(grid[0]) - 1] __A = 1 # the cost map which pushes the path closer to the goal __A = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): __A = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: # added extra penalty in the heuristic map __A = 99 __A , __A = search(grid, init, goal, cost, heuristic) print("ACTION MAP") for i in range(len(action)): print(action[i]) for i in range(len(path)): print(path[i])
10
'''simple docstring''' from __future__ import annotations from statistics import mean def a_ ( lowerCamelCase : list[int] , lowerCamelCase : list[int] , lowerCamelCase : int ): lowerCAmelCase = [0] * no_of_processes lowerCAmelCase = [0] * no_of_processes # Initialize remaining_time to waiting_time. for i in range(lowerCamelCase ): lowerCAmelCase = burst_time[i] lowerCAmelCase = [] lowerCAmelCase = 0 lowerCAmelCase = 0 # When processes are not completed, # A process whose arrival time has passed \ # and has remaining execution time is put into the ready_process. # The shortest process in the ready_process, target_process is executed. while completed != no_of_processes: lowerCAmelCase = [] lowerCAmelCase = -1 for i in range(lowerCamelCase ): if (arrival_time[i] <= total_time) and (remaining_time[i] > 0): ready_process.append(lowerCamelCase ) if len(lowerCamelCase ) > 0: lowerCAmelCase = ready_process[0] for i in ready_process: if remaining_time[i] < remaining_time[target_process]: lowerCAmelCase = i total_time += burst_time[target_process] completed += 1 lowerCAmelCase = 0 lowerCAmelCase = ( total_time - arrival_time[target_process] - burst_time[target_process] ) else: total_time += 1 return waiting_time def a_ ( lowerCamelCase : list[int] , lowerCamelCase : int , lowerCamelCase : list[int] ): lowerCAmelCase = [0] * no_of_processes for i in range(lowerCamelCase ): lowerCAmelCase = burst_time[i] + waiting_time[i] return turn_around_time if __name__ == "__main__": print("""[TEST CASE 01]""") __snake_case =4 __snake_case =[2, 5, 3, 7] __snake_case =[0, 0, 0, 0] __snake_case =calculate_waitingtime(arrival_time, burst_time, no_of_processes) __snake_case =calculate_turnaroundtime( burst_time, no_of_processes, waiting_time ) # Printing the Result print("""PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time""") for i, process_id in enumerate(list(range(1, 5))): print( F'''{process_id}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t''' F'''{waiting_time[i]}\t\t\t\t{turn_around_time[i]}''' ) print(F'''\nAverage waiting time = {mean(waiting_time):.5f}''') print(F'''Average turnaround time = {mean(turn_around_time):.5f}''')
4
0
from __future__ import annotations from sys import maxsize from typing import Generic, TypeVar lowerCAmelCase__ = TypeVar('T') def _UpperCAmelCase (UpperCamelCase__ : int ): return (position - 1) // 2 def _UpperCAmelCase (UpperCamelCase__ : int ): return (2 * position) + 1 def _UpperCAmelCase (UpperCamelCase__ : int ): return (2 * position) + 2 class lowerCAmelCase__ ( Generic[T]): '''simple docstring''' def __init__( self) -> None: _A : list[tuple[T, int]] = [] _A : dict[T, int] = {} _A : int = 0 def __len__( self) -> int: return self.elements def __repr__( self) -> str: return str(self.heap) def _lowerCamelCase ( self) -> bool: # Check if the priority queue is empty return self.elements == 0 def _lowerCamelCase ( self , __lowerCamelCase , __lowerCamelCase) -> None: # Add an element with given priority to the queue self.heap.append((elem, weight)) _A : int = self.elements self.elements += 1 self._bubble_up(__lowerCamelCase) def _lowerCamelCase ( self) -> T: # Remove and return the element with lowest weight (highest priority) if self.elements > 1: self._swap_nodes(0 , self.elements - 1) _A , _A : Optional[Any] = self.heap.pop() del self.position_map[elem] self.elements -= 1 if self.elements > 0: _A , _A : str = self.heap[0] self._bubble_down(__lowerCamelCase) return elem def _lowerCamelCase ( self , __lowerCamelCase , __lowerCamelCase) -> None: # Update the weight of the given key _A : Optional[Any] = self.position_map[elem] _A : Any = (elem, weight) if position > 0: _A : Any = get_parent_position(__lowerCamelCase) _A , _A : Tuple = self.heap[parent_position] if parent_weight > weight: self._bubble_up(__lowerCamelCase) else: self._bubble_down(__lowerCamelCase) else: self._bubble_down(__lowerCamelCase) def _lowerCamelCase ( self , __lowerCamelCase) -> None: # Place a node at the proper position (upward movement) [to be used internally # only] _A : Optional[int] = self.position_map[elem] if curr_pos == 0: return None _A : Optional[Any] = get_parent_position(__lowerCamelCase) _A , _A : Any = self.heap[curr_pos] _A , _A : str = self.heap[parent_position] if parent_weight > weight: self._swap_nodes(__lowerCamelCase , __lowerCamelCase) return self._bubble_up(__lowerCamelCase) return None def _lowerCamelCase ( self , __lowerCamelCase) -> None: # Place a node at the proper position (downward movement) [to be used # internally only] _A : Any = self.position_map[elem] _A , _A : Optional[int] = self.heap[curr_pos] _A : Optional[int] = get_child_left_position(__lowerCamelCase) _A : List[str] = get_child_right_position(__lowerCamelCase) if child_left_position < self.elements and child_right_position < self.elements: _A , _A : str = self.heap[child_left_position] _A , _A : List[str] = self.heap[child_right_position] if child_right_weight < child_left_weight and child_right_weight < weight: self._swap_nodes(__lowerCamelCase , __lowerCamelCase) return self._bubble_down(__lowerCamelCase) if child_left_position < self.elements: _A , _A : int = self.heap[child_left_position] if child_left_weight < weight: self._swap_nodes(__lowerCamelCase , __lowerCamelCase) return self._bubble_down(__lowerCamelCase) else: return None if child_right_position < self.elements: _A , _A : int = self.heap[child_right_position] if child_right_weight < weight: self._swap_nodes(__lowerCamelCase , __lowerCamelCase) return self._bubble_down(__lowerCamelCase) return None def _lowerCamelCase ( self , __lowerCamelCase , __lowerCamelCase) -> None: # Swap the nodes at the given positions _A : str = self.heap[nodea_pos][0] _A : List[Any] = self.heap[nodea_pos][0] _A , _A : Dict = ( self.heap[nodea_pos], self.heap[nodea_pos], ) _A : Tuple = nodea_pos _A : int = nodea_pos class lowerCAmelCase__ ( Generic[T]): '''simple docstring''' def __init__( self) -> None: _A : dict[T, dict[T, int]] = {} _A : int = 0 def __repr__( self) -> str: return str(self.connections) def __len__( self) -> int: return self.nodes def _lowerCamelCase ( self , __lowerCamelCase) -> None: # Add a node in the graph if it is not in the graph if node not in self.connections: _A : Optional[int] = {} self.nodes += 1 def _lowerCamelCase ( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase) -> None: # Add an edge between 2 nodes in the graph self.add_node(__lowerCamelCase) self.add_node(__lowerCamelCase) _A : Optional[int] = weight _A : str = weight def _UpperCAmelCase (UpperCamelCase__ : GraphUndirectedWeighted[T] , ): _A : dict[T, int] = {node: maxsize for node in graph.connections} _A : dict[T, T | None] = {node: None for node in graph.connections} _A : MinPriorityQueue[T] = MinPriorityQueue() for node, weight in dist.items(): priority_queue.push(UpperCamelCase__ , UpperCamelCase__ ) if priority_queue.is_empty(): return dist, parent # initialization _A : List[str] = priority_queue.extract_min() _A : Tuple = 0 for neighbour in graph.connections[node]: if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: _A : Tuple = dist[node] + graph.connections[node][neighbour] priority_queue.update_key(UpperCamelCase__ , dist[neighbour] ) _A : int = node # running prim's algorithm while not priority_queue.is_empty(): _A : Optional[Any] = priority_queue.extract_min() for neighbour in graph.connections[node]: if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: _A : Optional[Any] = dist[node] + graph.connections[node][neighbour] priority_queue.update_key(UpperCamelCase__ , dist[neighbour] ) _A : Any = node return dist, parent
11
'''simple docstring''' import gc import unittest import numpy as np import torch from diffusers import StableDiffusionKDiffusionPipeline from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : Optional[int] ) -> int: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self : Tuple ) -> Any: lowerCAmelCase = StableDiffusionKDiffusionPipeline.from_pretrained('CompVis/stable-diffusion-v1-4' ) lowerCAmelCase = sd_pipe.to(UpperCAmelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) sd_pipe.set_scheduler('sample_euler' ) lowerCAmelCase = 'A painting of a squirrel eating a burger' lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = sd_pipe([prompt] , generator=UpperCAmelCase__ , guidance_scale=9.0 , num_inference_steps=2_0 , output_type='np' ) lowerCAmelCase = output.images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase = np.array([0.0_447, 0.0_492, 0.0_468, 0.0_408, 0.0_383, 0.0_408, 0.0_354, 0.0_380, 0.0_339] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __UpperCAmelCase ( self : List[str] ) -> Dict: lowerCAmelCase = StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowerCAmelCase = sd_pipe.to(UpperCAmelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) sd_pipe.set_scheduler('sample_euler' ) lowerCAmelCase = 'A painting of a squirrel eating a burger' lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = sd_pipe([prompt] , generator=UpperCAmelCase__ , guidance_scale=9.0 , num_inference_steps=2_0 , output_type='np' ) lowerCAmelCase = output.images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase = np.array([0.1_237, 0.1_320, 0.1_438, 0.1_359, 0.1_390, 0.1_132, 0.1_277, 0.1_175, 0.1_112] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-1 def __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: lowerCAmelCase = StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowerCAmelCase = sd_pipe.to(UpperCAmelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) sd_pipe.set_scheduler('sample_dpmpp_2m' ) lowerCAmelCase = 'A painting of a squirrel eating a burger' lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = sd_pipe( [prompt] , generator=UpperCAmelCase__ , guidance_scale=7.5 , num_inference_steps=1_5 , output_type='np' , use_karras_sigmas=UpperCAmelCase__ , ) lowerCAmelCase = output.images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase = np.array( [0.11_381_689, 0.12_112_921, 0.1_389_457, 0.12_549_606, 0.1_244_964, 0.10_831_517, 0.11_562_866, 0.10_867_816, 0.10_499_048] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
4
0
import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip UpperCAmelCase_ = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def lowerCamelCase__ ( A__ : Optional[int] ): '''simple docstring''' if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def lowerCamelCase__ ( A__ : List[str] , A__ : Any , A__ : Optional[int] ): '''simple docstring''' return max(metric_fn(A__ , A__ ) for gt in ground_truths ) def lowerCamelCase__ ( A__ : Dict , A__ : Optional[int] , A__ : List[str] ): '''simple docstring''' __lowerCamelCase = [line.strip() for line in open(A__ , """r""" ).readlines()] __lowerCamelCase = [] if args.gold_data_mode == "qa": __lowerCamelCase = pd.read_csv(A__ , sep="""\t""" , header=A__ ) for answer_list in data[1]: __lowerCamelCase = ast.literal_eval(A__ ) answers.append(A__ ) else: __lowerCamelCase = [line.strip() for line in open(A__ , """r""" ).readlines()] __lowerCamelCase = [[reference] for reference in references] __lowerCamelCase = __lowerCamelCase = __lowerCamelCase = 0 for prediction, ground_truths in zip(A__ , A__ ): total += 1 em += metric_max_over_ground_truths(A__ , A__ , A__ ) fa += metric_max_over_ground_truths(A__ , A__ , A__ ) __lowerCamelCase = 100.0 * em / total __lowerCamelCase = 100.0 * fa / total logger.info(f'F1: {fa:.2f}' ) logger.info(f'EM: {em:.2f}' ) def lowerCamelCase__ ( A__ : List[str] , A__ : Union[str, Any] , A__ : Optional[int] ): '''simple docstring''' __lowerCamelCase = args.k __lowerCamelCase = [line.strip() for line in open(A__ , """r""" ).readlines()] __lowerCamelCase = [line.strip() for line in open(A__ , """r""" ).readlines()] __lowerCamelCase = __lowerCamelCase = 0 for hypo, reference in zip(A__ , A__ ): __lowerCamelCase = set(hypo.split("""\t""" )[:k] ) __lowerCamelCase = set(reference.split("""\t""" ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k __lowerCamelCase = 100.0 * em / total logger.info(f'Precision@{k}: {em: .2f}' ) def lowerCamelCase__ ( A__ : int , A__ : Union[str, Any] , A__ : str ): '''simple docstring''' def strip_title(A__ : int ): if title.startswith("""\"""" ): __lowerCamelCase = title[1:] if title.endswith("""\"""" ): __lowerCamelCase = title[:-1] return title __lowerCamelCase = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( A__ , return_tensors="""pt""" , padding=A__ , truncation=A__ , )["""input_ids"""].to(args.device ) __lowerCamelCase = rag_model.rag.question_encoder(A__ ) __lowerCamelCase = question_enc_outputs[0] __lowerCamelCase = rag_model.retriever( A__ , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors="""pt""" , ) __lowerCamelCase = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) __lowerCamelCase = [] for docs in all_docs: __lowerCamelCase = [strip_title(A__ ) for title in docs["""title"""]] provenance_strings.append("""\t""".join(A__ ) ) return provenance_strings def lowerCamelCase__ ( A__ : Optional[int] , A__ : Dict , A__ : List[Any] ): '''simple docstring''' with torch.no_grad(): __lowerCamelCase = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( A__ , return_tensors="""pt""" , padding=A__ , truncation=A__ ) __lowerCamelCase = inputs_dict.input_ids.to(args.device ) __lowerCamelCase = inputs_dict.attention_mask.to(args.device ) __lowerCamelCase = rag_model.generate( # rag_model overwrites generate A__ , attention_mask=A__ , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=A__ , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) __lowerCamelCase = rag_model.retriever.generator_tokenizer.batch_decode(A__ , skip_special_tokens=A__ ) if args.print_predictions: for q, a in zip(A__ , A__ ): logger.info("""Q: {} - A: {}""".format(A__ , A__ ) ) return answers def lowerCamelCase__ ( ): '''simple docstring''' __lowerCamelCase = argparse.ArgumentParser() parser.add_argument( """--model_type""" , choices=["""rag_sequence""", """rag_token""", """bart"""] , type=A__ , help=( """RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the""" """ model_name_or_path""" ) , ) parser.add_argument( """--index_name""" , default=A__ , choices=["""exact""", """compressed""", """legacy"""] , type=A__ , help="""RAG model retriever type""" , ) parser.add_argument( """--index_path""" , default=A__ , type=A__ , help="""Path to the retrieval index""" , ) parser.add_argument("""--n_docs""" , default=5 , type=A__ , help="""Number of retrieved docs""" ) parser.add_argument( """--model_name_or_path""" , default=A__ , type=A__ , required=A__ , help="""Path to pretrained checkpoints or model identifier from huggingface.co/models""" , ) parser.add_argument( """--eval_mode""" , choices=["""e2e""", """retrieval"""] , default="""e2e""" , type=A__ , help=( """Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates""" """ precision@k.""" ) , ) parser.add_argument("""--k""" , default=1 , type=A__ , help="""k for the precision@k calculation""" ) parser.add_argument( """--evaluation_set""" , default=A__ , type=A__ , required=A__ , help="""Path to a file containing evaluation samples""" , ) parser.add_argument( """--gold_data_path""" , default=A__ , type=A__ , required=A__ , help="""Path to a tab-separated file with gold samples""" , ) parser.add_argument( """--gold_data_mode""" , default="""qa""" , type=A__ , choices=["""qa""", """ans"""] , help=( """Format of the gold data file""" """qa - a single line in the following format: question [tab] answer_list""" """ans - a single line of the gold file contains the expected answer string""" ) , ) parser.add_argument( """--predictions_path""" , type=A__ , default="""predictions.txt""" , help="""Name of the predictions file, to be stored in the checkpoints directory""" , ) parser.add_argument( """--eval_all_checkpoints""" , action="""store_true""" , help="""Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number""" , ) parser.add_argument( """--eval_batch_size""" , default=8 , type=A__ , help="""Batch size per GPU/CPU for evaluation.""" , ) parser.add_argument( """--recalculate""" , help="""Recalculate predictions even if the prediction file exists""" , action="""store_true""" , ) parser.add_argument( """--num_beams""" , default=4 , type=A__ , help="""Number of beams to be used when generating answers""" , ) parser.add_argument("""--min_length""" , default=1 , type=A__ , help="""Min length of the generated answers""" ) parser.add_argument("""--max_length""" , default=50 , type=A__ , help="""Max length of the generated answers""" ) parser.add_argument( """--print_predictions""" , action="""store_true""" , help="""If True, prints predictions while evaluating.""" , ) parser.add_argument( """--print_docs""" , action="""store_true""" , help="""If True, prints docs retried while generating.""" , ) __lowerCamelCase = parser.parse_args() __lowerCamelCase = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) return args def lowerCamelCase__ ( A__ : List[Any] ): '''simple docstring''' __lowerCamelCase = {} if args.model_type is None: __lowerCamelCase = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith("""rag""" ): __lowerCamelCase = RagTokenForGeneration if args.model_type == """rag_token""" else RagSequenceForGeneration __lowerCamelCase = args.n_docs if args.index_name is not None: __lowerCamelCase = args.index_name if args.index_path is not None: __lowerCamelCase = args.index_path else: __lowerCamelCase = BartForConditionalGeneration __lowerCamelCase = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info("""Evaluate the following checkpoints: %s""" , A__ ) __lowerCamelCase = get_scores if args.eval_mode == """e2e""" else get_precision_at_k __lowerCamelCase = evaluate_batch_eae if args.eval_mode == """e2e""" else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info("""Calculating metrics based on an existing predictions file: {}""".format(args.predictions_path ) ) score_fn(A__ , args.predictions_path , args.gold_data_path ) continue logger.info("""***** Running evaluation for {} *****""".format(A__ ) ) logger.info(""" Batch size = %d""" , args.eval_batch_size ) logger.info(""" Predictions will be stored under {}""".format(args.predictions_path ) ) if args.model_type.startswith("""rag""" ): __lowerCamelCase = RagRetriever.from_pretrained(A__ , **A__ ) __lowerCamelCase = model_class.from_pretrained(A__ , retriever=A__ , **A__ ) model.retriever.init_retrieval() else: __lowerCamelCase = model_class.from_pretrained(A__ , **A__ ) model.to(args.device ) with open(args.evaluation_set , """r""" ) as eval_file, open(args.predictions_path , """w""" ) as preds_file: __lowerCamelCase = [] for line in tqdm(A__ ): questions.append(line.strip() ) if len(A__ ) == args.eval_batch_size: __lowerCamelCase = evaluate_batch_fn(A__ , A__ , A__ ) preds_file.write("""\n""".join(A__ ) + """\n""" ) preds_file.flush() __lowerCamelCase = [] if len(A__ ) > 0: __lowerCamelCase = evaluate_batch_fn(A__ , A__ , A__ ) preds_file.write("""\n""".join(A__ ) ) preds_file.flush() score_fn(A__ , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": UpperCAmelCase_ = get_args() main(args)
12
'''simple docstring''' # Copyright (c) 2021-, NVIDIA CORPORATION. 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. #################################################################################################### # # Note: If when running this conversion script you're getting an exception: # ModuleNotFoundError: No module named 'megatron.model.enums' # you need to tell python where to find the clone of Megatron-LM, e.g.: # # cd /tmp # git clone https://github.com/NVIDIA/Megatron-LM # PYTHONPATH=/tmp/Megatron-LM python src/transformers/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py ... # # if you already have it cloned elsewhere, simply adjust the path to the existing path # # If the training was done using a Megatron-LM fork, e.g., # https://github.com/microsoft/Megatron-DeepSpeed/ then chances are that you need to have that one # in your path, i.e., /path/to/Megatron-DeepSpeed/ # import argparse import os import re import zipfile import torch from transformers import AutoTokenizer, GPTaConfig def a_ ( lowerCamelCase : int , lowerCamelCase : Optional[Any] , lowerCamelCase : Union[str, Any]=0 ): # Format the message. if name is None: lowerCAmelCase = None else: lowerCAmelCase = '.' * max(0 , spaces - 2 ) + '# {:' + str(50 - spaces ) + 's}' lowerCAmelCase = fmt.format(lowerCamelCase ) # Print and recurse (if needed). if isinstance(lowerCamelCase , lowerCamelCase ): if msg is not None: print(lowerCamelCase ) for k in val.keys(): recursive_print(lowerCamelCase , val[k] , spaces + 2 ) elif isinstance(lowerCamelCase , torch.Tensor ): print(lowerCamelCase , ':' , val.size() ) else: print(lowerCamelCase , ':' , lowerCamelCase ) def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : List[str] , lowerCamelCase : List[Any] , lowerCamelCase : Dict , lowerCamelCase : Tuple ): # Permutes layout of param tensor to [num_splits * num_heads * hidden_size, :] # for compatibility with later versions of NVIDIA Megatron-LM. # The inverse operation is performed inside Megatron-LM to read checkpoints: # https://github.com/NVIDIA/Megatron-LM/blob/v2.4/megatron/checkpointing.py#L209 # If param is the weight tensor of the self-attention block, the returned tensor # will have to be transposed one more time to be read by HuggingFace GPT2. lowerCAmelCase = param.size() if checkpoint_version == 1.0: # version 1.0 stores [num_heads * hidden_size * num_splits, :] lowerCAmelCase = (num_heads, hidden_size, num_splits) + input_shape[1:] lowerCAmelCase = param.view(*lowerCamelCase ) lowerCAmelCase = param.transpose(0 , 2 ) lowerCAmelCase = param.transpose(1 , 2 ).contiguous() elif checkpoint_version >= 2.0: # other versions store [num_heads * num_splits * hidden_size, :] lowerCAmelCase = (num_heads, num_splits, hidden_size) + input_shape[1:] lowerCAmelCase = param.view(*lowerCamelCase ) lowerCAmelCase = param.transpose(0 , 1 ).contiguous() lowerCAmelCase = param.view(*lowerCamelCase ) return param def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Optional[int] , lowerCamelCase : str ): # The converted output model. lowerCAmelCase = {} # old versions did not store training args lowerCAmelCase = input_state_dict.get('args' , lowerCamelCase ) if ds_args is not None: # do not make the user write a config file when the exact dimensions/sizes are already in the checkpoint # from pprint import pprint # pprint(vars(ds_args)) lowerCAmelCase = ds_args.padded_vocab_size lowerCAmelCase = ds_args.max_position_embeddings lowerCAmelCase = ds_args.hidden_size lowerCAmelCase = ds_args.num_layers lowerCAmelCase = ds_args.num_attention_heads lowerCAmelCase = ds_args.ffn_hidden_size # pprint(config) # The number of heads. lowerCAmelCase = config.n_head # The hidden_size per head. lowerCAmelCase = config.n_embd // config.n_head # Megatron-LM checkpoint version if "checkpoint_version" in input_state_dict.keys(): lowerCAmelCase = input_state_dict['checkpoint_version'] else: lowerCAmelCase = 0.0 # The model. lowerCAmelCase = input_state_dict['model'] # The language model. lowerCAmelCase = model['language_model'] # The embeddings. lowerCAmelCase = lm['embedding'] # The word embeddings. lowerCAmelCase = embeddings['word_embeddings']['weight'] # Truncate the embedding table to vocab_size rows. lowerCAmelCase = word_embeddings[: config.vocab_size, :] lowerCAmelCase = word_embeddings # The position embeddings. lowerCAmelCase = embeddings['position_embeddings']['weight'] # Read the causal mask dimension (seqlen). [max_sequence_length, hidden_size] lowerCAmelCase = pos_embeddings.size(0 ) if n_positions != config.n_positions: raise ValueError( f'''pos_embeddings.max_sequence_length={n_positions} and config.n_positions={config.n_positions} don\'t match''' ) # Store the position embeddings. lowerCAmelCase = pos_embeddings # The transformer. lowerCAmelCase = lm['transformer'] if 'transformer' in lm.keys() else lm['encoder'] # The regex to extract layer names. lowerCAmelCase = re.compile(R'layers\.(\d+)\.([a-z0-9_.]+)\.([a-z]+)' ) # The simple map of names for "automated" rules. lowerCAmelCase = { 'attention.dense': '.attn.c_proj.', 'self_attention.dense': '.attn.c_proj.', 'mlp.dense_h_to_4h': '.mlp.c_fc.', 'mlp.dense_4h_to_h': '.mlp.c_proj.', } # Extract the layers. for key, val in transformer.items(): # Match the name. lowerCAmelCase = layer_re.match(lowerCamelCase ) # Stop if that's not a layer if m is None: break # The index of the layer. lowerCAmelCase = int(m.group(1 ) ) # The name of the operation. lowerCAmelCase = m.group(2 ) # Is it a weight or a bias? lowerCAmelCase = m.group(3 ) # The name of the layer. lowerCAmelCase = f'''transformer.h.{layer_idx}''' # For layernorm(s), simply store the layer norm. if op_name.endswith('layernorm' ): lowerCAmelCase = 'ln_1' if op_name.startswith('input' ) else 'ln_2' lowerCAmelCase = val # Transpose the QKV matrix. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "weight": # Insert a tensor of 1x1xDxD bias. lowerCAmelCase = torch.tril(torch.ones((n_positions, n_positions) , dtype=torch.floataa ) ).view( 1 , 1 , lowerCamelCase , lowerCamelCase ) lowerCAmelCase = causal_mask # Insert a "dummy" tensor for masked_bias. lowerCAmelCase = torch.tensor(-1e4 , dtype=torch.floataa ) lowerCAmelCase = masked_bias lowerCAmelCase = fix_query_key_value_ordering(lowerCamelCase , lowerCamelCase , 3 , lowerCamelCase , lowerCamelCase ) # Megatron stores (3*D) x D but transformers-GPT2 expects D x 3*D. lowerCAmelCase = out_val.transpose(0 , 1 ).contiguous() # Store. lowerCAmelCase = out_val # Transpose the bias. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "bias": lowerCAmelCase = fix_query_key_value_ordering(lowerCamelCase , lowerCamelCase , 3 , lowerCamelCase , lowerCamelCase ) # Store. No change of shape. lowerCAmelCase = out_val # Transpose the weights. elif weight_or_bias == "weight": lowerCAmelCase = megatron_to_transformers[op_name] lowerCAmelCase = val.transpose(0 , 1 ) # Copy the bias. elif weight_or_bias == "bias": lowerCAmelCase = megatron_to_transformers[op_name] lowerCAmelCase = val # DEBUG. assert config.n_layer == layer_idx + 1 # The final layernorm. lowerCAmelCase = transformer['final_layernorm.weight'] lowerCAmelCase = transformer['final_layernorm.bias'] # For LM head, transformers' wants the matrix to weight embeddings. lowerCAmelCase = word_embeddings # It should be done! return output_state_dict def a_ ( ): # Create the argument parser. lowerCAmelCase = argparse.ArgumentParser() parser.add_argument('--print-checkpoint-structure' , action='store_true' ) parser.add_argument( 'path_to_checkpoint' , type=lowerCamelCase , help='Path to the checkpoint file (.zip archive or direct .pt file)' , ) parser.add_argument( '--config_file' , default='' , type=lowerCamelCase , help='An optional config json file describing the pre-trained model.' , ) lowerCAmelCase = parser.parse_args() # Extract the basename. lowerCAmelCase = os.path.dirname(args.path_to_checkpoint ) # Load the model. # the .zip is very optional, let's keep it for backward compatibility print(f'''Extracting PyTorch state dictionary from {args.path_to_checkpoint}''' ) if args.path_to_checkpoint.endswith('.zip' ): with zipfile.ZipFile(args.path_to_checkpoint , 'r' ) as checkpoint: with checkpoint.open('release/mp_rank_00/model_optim_rng.pt' ) as pytorch_dict: lowerCAmelCase = torch.load(lowerCamelCase , map_location='cpu' ) else: lowerCAmelCase = torch.load(args.path_to_checkpoint , map_location='cpu' ) lowerCAmelCase = input_state_dict.get('args' , lowerCamelCase ) # Read the config, or default to the model released by NVIDIA. if args.config_file == "": if ds_args is not None: if ds_args.bias_gelu_fusion: lowerCAmelCase = 'gelu_fast' elif ds_args.openai_gelu: lowerCAmelCase = 'gelu_new' else: lowerCAmelCase = 'gelu' else: # in the very early days this used to be "gelu_new" lowerCAmelCase = 'gelu_new' # Spell out all parameters in case the defaults change. lowerCAmelCase = GPTaConfig( vocab_size=50257 , n_positions=1024 , n_embd=1024 , n_layer=24 , n_head=16 , n_inner=4096 , activation_function=lowerCamelCase , resid_pdrop=0.1 , embd_pdrop=0.1 , attn_pdrop=0.1 , layer_norm_epsilon=1e-5 , initializer_range=0.02 , summary_type='cls_index' , summary_use_proj=lowerCamelCase , summary_activation=lowerCamelCase , summary_proj_to_labels=lowerCamelCase , summary_first_dropout=0.1 , scale_attn_weights=lowerCamelCase , use_cache=lowerCamelCase , bos_token_id=50256 , eos_token_id=50256 , ) else: lowerCAmelCase = GPTaConfig.from_json_file(args.config_file ) lowerCAmelCase = ['GPT2LMHeadModel'] # Convert. print('Converting' ) lowerCAmelCase = convert_megatron_checkpoint(lowerCamelCase , lowerCamelCase , lowerCamelCase ) # Print the structure of converted state dict. if args.print_checkpoint_structure: recursive_print(lowerCamelCase , lowerCamelCase ) # Add tokenizer class info to config # see https://github.com/huggingface/transformers/issues/13906) if ds_args is not None: lowerCAmelCase = ds_args.tokenizer_type if tokenizer_type == "GPT2BPETokenizer": lowerCAmelCase = 'gpt2' elif tokenizer_type == "PretrainedFromHF": lowerCAmelCase = ds_args.tokenizer_name_or_path else: raise ValueError(f'''Unrecognized tokenizer_type {tokenizer_type}''' ) else: lowerCAmelCase = 'gpt2' lowerCAmelCase = AutoTokenizer.from_pretrained(lowerCamelCase ) lowerCAmelCase = type(lowerCamelCase ).__name__ lowerCAmelCase = tokenizer_class # Store the config to file. print('Saving config' ) config.save_pretrained(lowerCamelCase ) # Save tokenizer based on args print(f'''Adding {tokenizer_class} tokenizer files''' ) tokenizer.save_pretrained(lowerCamelCase ) # Store the state_dict to file. lowerCAmelCase = os.path.join(lowerCamelCase , 'pytorch_model.bin' ) print(f'''Saving checkpoint to "{output_checkpoint_file}"''' ) torch.save(lowerCamelCase , lowerCamelCase ) #################################################################################################### if __name__ == "__main__": main() ####################################################################################################
4
0
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 __lowercase ( unittest.TestCase ): """simple docstring""" @slow def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Any = TFAutoModelForSeqaSeqLM.from_pretrained("google/mt5-small") SCREAMING_SNAKE_CASE_: List[Any] = AutoTokenizer.from_pretrained("google/mt5-small") SCREAMING_SNAKE_CASE_: int = tokenizer("Hello there" , return_tensors="tf").input_ids SCREAMING_SNAKE_CASE_: Any = tokenizer("Hi I am" , return_tensors="tf").input_ids SCREAMING_SNAKE_CASE_: Union[str, Any] = model(lowerCAmelCase__ , labels=lowerCAmelCase__).loss SCREAMING_SNAKE_CASE_: str = -tf.math.reduce_mean(lowerCAmelCase__).numpy() SCREAMING_SNAKE_CASE_: Tuple = -21.22_8168 self.assertTrue(abs(mtf_score - EXPECTED_SCORE) < 2E-4)
13
'''simple docstring''' from __future__ import annotations from typing import Any class UpperCAmelCase_ : def __init__( self : Optional[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : float = 0 ) -> None: lowerCAmelCase , lowerCAmelCase = row, column lowerCAmelCase = [[default_value for c in range(UpperCAmelCase__ )] for r in range(UpperCAmelCase__ )] def __str__( self : List[str] ) -> str: lowerCAmelCase = F'''Matrix consist of {self.row} rows and {self.column} columns\n''' # Make string identifier lowerCAmelCase = 0 for row_vector in self.array: for obj in row_vector: lowerCAmelCase = max(UpperCAmelCase__ , len(str(UpperCAmelCase__ ) ) ) lowerCAmelCase = F'''%{max_element_length}s''' # Make string and return def single_line(UpperCAmelCase__ : list[float] ) -> str: nonlocal string_format_identifier lowerCAmelCase = '[' line += ", ".join(string_format_identifier % (obj,) for obj in row_vector ) line += "]" return line s += "\n".join(single_line(UpperCAmelCase__ ) for row_vector in self.array ) return s def __repr__( self : List[str] ) -> str: return str(self ) def __UpperCAmelCase ( self : Optional[int] , UpperCAmelCase__ : tuple[int, int] ) -> bool: if not (isinstance(UpperCAmelCase__ , (list, tuple) ) and len(UpperCAmelCase__ ) == 2): return False elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False else: return True def __getitem__( self : Any , UpperCAmelCase__ : tuple[int, int] ) -> Any: assert self.validate_indicies(UpperCAmelCase__ ) return self.array[loc[0]][loc[1]] def __setitem__( self : Dict , UpperCAmelCase__ : tuple[int, int] , UpperCAmelCase__ : float ) -> None: assert self.validate_indicies(UpperCAmelCase__ ) lowerCAmelCase = value def __add__( self : Any , UpperCAmelCase__ : Matrix ) -> Matrix: assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) assert self.row == another.row and self.column == another.column # Add lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] + another[r, c] return result def __neg__( self : int ) -> Matrix: lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = -self[r, c] return result def __sub__( self : str , UpperCAmelCase__ : Matrix ) -> Matrix: return self + (-another) def __mul__( self : str , UpperCAmelCase__ : int | float | Matrix ) -> Matrix: if isinstance(UpperCAmelCase__ , (int, float) ): # Scalar multiplication lowerCAmelCase = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] * another return result elif isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): # Matrix multiplication assert self.column == another.row lowerCAmelCase = Matrix(self.row , another.column ) for r in range(self.row ): for c in range(another.column ): for i in range(self.column ): result[r, c] += self[r, i] * another[i, c] return result else: lowerCAmelCase = F'''Unsupported type given for another ({type(UpperCAmelCase__ )})''' raise TypeError(UpperCAmelCase__ ) def __UpperCAmelCase ( self : Optional[Any] ) -> Matrix: lowerCAmelCase = Matrix(self.column , self.row ) for r in range(self.row ): for c in range(self.column ): lowerCAmelCase = self[r, c] return result def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : Matrix , UpperCAmelCase__ : Matrix ) -> Any: assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) and isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) assert self.row == self.column == u.row == v.row # u, v should be column vector assert u.column == v.column == 1 # u, v should be column vector # Calculate lowerCAmelCase = v.transpose() lowerCAmelCase = (v_t * self * u)[0, 0] + 1 if numerator_factor == 0: return None # It's not invertable return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor)) # Testing if __name__ == "__main__": def a_ ( ): # a^(-1) lowerCAmelCase = Matrix(3 , 3 , 0 ) for i in range(3 ): lowerCAmelCase = 1 print(f'''a^(-1) is {ainv}''' ) # u, v lowerCAmelCase = Matrix(3 , 1 , 0 ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = 1, 2, -3 lowerCAmelCase = Matrix(3 , 1 , 0 ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = 4, -2, 5 print(f'''u is {u}''' ) print(f'''v is {v}''' ) print(f'''uv^T is {u * v.transpose()}''' ) # Sherman Morrison print(f'''(a + uv^T)^(-1) is {ainv.sherman_morrison(lowerCamelCase , lowerCamelCase )}''' ) def a_ ( ): import doctest doctest.testmod() testa()
4
0
from __future__ import annotations from fractions import Fraction def SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ ) -> bool: """simple docstring""" return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def SCREAMING_SNAKE_CASE ( lowercase_ ) -> list[str]: """simple docstring""" A__ = [] A__ = 11 A__ = int('''1''' + '''0''' * digit_len ) for num in range(lowercase_ , lowercase_ ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(lowercase_ , lowercase_ ): solutions.append(f"""{num}/{den}""" ) den += 1 num += 1 A__ = 10 return solutions def SCREAMING_SNAKE_CASE ( lowercase_ = 2 ) -> int: """simple docstring""" A__ = 1.0 for fraction in fraction_list(lowercase_ ): A__ = Fraction(lowercase_ ) result *= frac.denominator / frac.numerator return int(lowercase_ ) if __name__ == "__main__": print(solution())
14
'''simple docstring''' class UpperCAmelCase_ : def __init__( self : List[str] , UpperCAmelCase__ : list[int] ) -> None: lowerCAmelCase = len(UpperCAmelCase__ ) lowerCAmelCase = [0] * len_array if len_array > 0: lowerCAmelCase = array[0] for i in range(1 , UpperCAmelCase__ ): lowerCAmelCase = self.prefix_sum[i - 1] + array[i] def __UpperCAmelCase ( self : Optional[Any] , UpperCAmelCase__ : int , UpperCAmelCase__ : int ) -> int: if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def __UpperCAmelCase ( self : int , UpperCAmelCase__ : int ) -> bool: lowerCAmelCase = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(UpperCAmelCase__ ) return False if __name__ == "__main__": import doctest doctest.testmod()
4
0
import itertools import os from collections import Counter, defaultdict from concurrent.futures import ThreadPoolExecutor, as_completed import numpy as np import datasets from .execute import check_correctness SCREAMING_SNAKE_CASE :Dict = '\\n@misc{chen2021evaluating,\n title={Evaluating Large Language Models Trained on Code},\n author={Mark Chen and Jerry Tworek and Heewoo Jun and Qiming Yuan \\nand Henrique Ponde de Oliveira Pinto and Jared Kaplan and Harri Edwards \\nand Yuri Burda and Nicholas Joseph and Greg Brockman and Alex Ray \\nand Raul Puri and Gretchen Krueger and Michael Petrov and Heidy Khlaaf \\nand Girish Sastry and Pamela Mishkin and Brooke Chan and Scott Gray \\nand Nick Ryder and Mikhail Pavlov and Alethea Power and Lukasz Kaiser \\nand Mohammad Bavarian and Clemens Winter and Philippe Tillet \\nand Felipe Petroski Such and Dave Cummings and Matthias Plappert \\nand Fotios Chantzis and Elizabeth Barnes and Ariel Herbert-Voss \\nand William Hebgen Guss and Alex Nichol and Alex Paino and Nikolas Tezak \\nand Jie Tang and Igor Babuschkin and Suchir Balaji and Shantanu Jain \\nand William Saunders and Christopher Hesse and Andrew N. Carr \\nand Jan Leike and Josh Achiam and Vedant Misra and Evan Morikawa \\nand Alec Radford and Matthew Knight and Miles Brundage and Mira Murati \\nand Katie Mayer and Peter Welinder and Bob McGrew and Dario Amodei \\nand Sam McCandlish and Ilya Sutskever and Wojciech Zaremba},\n year={2021},\n eprint={2107.03374},\n archivePrefix={arXiv},\n primaryClass={cs.LG}\n}\n' SCREAMING_SNAKE_CASE :List[Any] = '\\nThis metric implements the evaluation harness for the HumanEval problem solving dataset\ndescribed in the paper "Evaluating Large Language Models Trained on Code"\n(https://arxiv.org/abs/2107.03374).\n' SCREAMING_SNAKE_CASE :Dict = '\nCalculates how good are predictions given some references, using certain scores\nArgs:\n predictions: list of candidates to evaluate. Each candidates should be a list\n of strings with several code candidates to solve the problem.\n references: a list with a test for each prediction. Each test should evaluate the\n correctness of a code candidate.\n k: number of code candidates to consider in the evaluation (Default: [1, 10, 100])\n num_workers: number of workers used to evaluate the canidate programs (Default: 4).\n timeout:\nReturns:\n pass_at_k: dict with pass rates for each k\n results: dict with granular results of each unittest\nExamples:\n >>> code_eval = datasets.load_metric("code_eval")\n >>> test_cases = ["assert add(2,3)==5"]\n >>> candidates = [["def add(a,b): return a*b", "def add(a, b): return a+b"]]\n >>> pass_at_k, results = code_eval.compute(references=test_cases, predictions=candidates, k=[1, 2])\n >>> print(pass_at_k)\n {\'pass@1\': 0.5, \'pass@2\': 1.0}\n' SCREAMING_SNAKE_CASE :Union[str, Any] = '\n################################################################################\n !!!WARNING!!!\n################################################################################\nThe "code_eval" metric executes untrusted model-generated code in Python.\nAlthough it is highly unlikely that model-generated code will do something\novertly malicious in response to this test suite, model-generated code may act\ndestructively due to a lack of model capability or alignment.\nUsers are strongly encouraged to sandbox this evaluation suite so that it\ndoes not perform destructive actions on their host or network. For more\ninformation on how OpenAI sandboxes its code, see the paper "Evaluating Large\nLanguage Models Trained on Code" (https://arxiv.org/abs/2107.03374).\n\nOnce you have read this disclaimer and taken appropriate precautions,\nset the environment variable HF_ALLOW_CODE_EVAL="1". Within Python you can to this\nwith:\n\n>>> import os\n>>> os.environ["HF_ALLOW_CODE_EVAL"] = "1"\n\n################################################################################\\n' SCREAMING_SNAKE_CASE :List[Any] = 'The MIT License\n\nCopyright (c) OpenAI (https://openai.com)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the "Software"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def UpperCamelCase_ ( self : Optional[Any] ): return datasets.MetricInfo( # This is the description that will appear on the metrics page. description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" ) ), "references": datasets.Value("string" ), } ) ,homepage="https://github.com/openai/human-eval" ,codebase_urls=["https://github.com/openai/human-eval"] ,reference_urls=["https://github.com/openai/human-eval"] ,license=_LICENSE ,) def UpperCamelCase_ ( self : str ,A : int ,A : Union[str, Any] ,A : Optional[int]=[1, 10, 1_00] ,A : Union[str, Any]=4 ,A : Dict=3.0 ): if os.getenv("HF_ALLOW_CODE_EVAL" ,0 ) != "1": raise ValueError(_WARNING ) if os.name == "nt": raise NotImplementedError("This metric is currently not supported on Windows." ) with ThreadPoolExecutor(max_workers=A ) as executor: __A = [] __A = Counter() __A = 0 __A = defaultdict(A ) for task_id, (candidates, test_case) in enumerate(zip(A ,A ) ): for candidate in candidates: __A = candidate + "\n" + test_case __A = (test_program, timeout, task_id, completion_id[task_id]) __A = executor.submit(A ,*A ) futures.append(A ) completion_id[task_id] += 1 n_samples += 1 for future in as_completed(A ): __A = future.result() results[result["task_id"]].append((result["completion_id"], result) ) __A , __A = [], [] for result in results.values(): result.sort() __A = [r[1]["passed"] for r in result] total.append(len(A ) ) correct.append(sum(A ) ) __A = np.array(A ) __A = np.array(A ) __A = k __A = {f'''pass@{k}''': estimate_pass_at_k(A ,A ,A ).mean() for k in ks if (total >= k).all()} return pass_at_k, results def UpperCAmelCase ( a_ , a_ , a_ ) -> Union[str, Any]: """simple docstring""" def estimator(a_ , a_ , a_ ) -> float: if n - c < k: return 1.0 return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1 , n + 1 ) ) if isinstance(a_ , a_ ): __A = itertools.repeat(a_ , len(a_ ) ) else: assert len(a_ ) == len(a_ ) __A = iter(a_ ) return np.array([estimator(int(a_ ) , int(a_ ) , a_ ) for n, c in zip(a_ , a_ )] )
15
'''simple docstring''' def a_ ( lowerCamelCase : Optional[Any] ): return [ { 0: [1, 2], 1: [0, 2], 2: [0, 1, 3, 5], 3: [2, 4], 4: [3], 5: [2, 6, 8], 6: [5, 7], 7: [6, 8], 8: [5, 7], }, { 0: [6], 1: [9], 2: [4, 5], 3: [4], 4: [2, 3], 5: [2], 6: [0, 7], 7: [6], 8: [], 9: [1], }, { 0: [4], 1: [6], 2: [], 3: [5, 6, 7], 4: [0, 6], 5: [3, 8, 9], 6: [1, 3, 4, 7], 7: [3, 6, 8, 9], 8: [5, 7], 9: [5, 7], }, { 0: [1, 3], 1: [0, 2, 4], 2: [1, 3, 4], 3: [0, 2, 4], 4: [1, 2, 3], }, ][index] def a_ ( lowerCamelCase : dict[int, list[int]] ): lowerCAmelCase = 0 lowerCAmelCase = len(lowerCamelCase ) # No of vertices in graph lowerCAmelCase = [0] * n lowerCAmelCase = [False] * n def dfs(lowerCamelCase : Tuple , lowerCamelCase : str , lowerCamelCase : Dict , lowerCamelCase : str ): lowerCAmelCase = True lowerCAmelCase = id_ id_ += 1 for to in graph[at]: if to == parent: pass elif not visited[to]: dfs(lowerCamelCase , lowerCamelCase , lowerCamelCase , id_ ) lowerCAmelCase = min(low[at] , low[to] ) if id_ <= low[to]: bridges.append((at, to) if at < to else (to, at) ) else: # This edge is a back edge and cannot be a bridge lowerCAmelCase = min(low[at] , low[to] ) lowerCAmelCase = [] for i in range(lowerCamelCase ): if not visited[i]: dfs(lowerCamelCase , -1 , lowerCamelCase , id_ ) return bridges if __name__ == "__main__": import doctest doctest.testmod()
4
0
"""simple docstring""" import argparse import torch from transformers import YosoConfig, YosoForMaskedLM def __UpperCAmelCase ( __lowerCamelCase ) -> Optional[int]: if "model" in orig_key: lowercase__ : Tuple = orig_key.replace('''model.''' , '''''' ) if "norm1" in orig_key: lowercase__ : List[str] = orig_key.replace('''norm1''' , '''attention.output.LayerNorm''' ) if "norm2" in orig_key: lowercase__ : List[str] = orig_key.replace('''norm2''' , '''output.LayerNorm''' ) if "norm" in orig_key: lowercase__ : List[str] = orig_key.replace('''norm''' , '''LayerNorm''' ) if "transformer" in orig_key: lowercase__ : Union[str, Any] = orig_key.split('''.''' )[0].split('''_''' )[-1] lowercase__ : List[str] = orig_key.replace(f"""transformer_{layer_num}""" , f"""encoder.layer.{layer_num}""" ) if "mha.attn" in orig_key: lowercase__ : Union[str, Any] = orig_key.replace('''mha.attn''' , '''attention.self''' ) if "mha" in orig_key: lowercase__ : str = orig_key.replace('''mha''' , '''attention''' ) if "W_q" in orig_key: lowercase__ : Any = orig_key.replace('''W_q''' , '''self.query''' ) if "W_k" in orig_key: lowercase__ : List[Any] = orig_key.replace('''W_k''' , '''self.key''' ) if "W_v" in orig_key: lowercase__ : Any = orig_key.replace('''W_v''' , '''self.value''' ) if "ff1" in orig_key: lowercase__ : Optional[int] = orig_key.replace('''ff1''' , '''intermediate.dense''' ) if "ff2" in orig_key: lowercase__ : Optional[Any] = orig_key.replace('''ff2''' , '''output.dense''' ) if "ff" in orig_key: lowercase__ : List[str] = orig_key.replace('''ff''' , '''output.dense''' ) if "mlm_class" in orig_key: lowercase__ : int = orig_key.replace('''mlm.mlm_class''' , '''cls.predictions.decoder''' ) if "mlm" in orig_key: lowercase__ : Optional[Any] = orig_key.replace('''mlm''' , '''cls.predictions.transform''' ) if "cls" not in orig_key: lowercase__ : Optional[Any] = '''yoso.''' + orig_key return orig_key def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase ) -> Optional[int]: for key in orig_state_dict.copy().keys(): lowercase__ : Optional[Any] = orig_state_dict.pop(__lowerCamelCase ) if ("pooler" in key) or ("sen_class" in key): continue else: lowercase__ : Tuple = val lowercase__ : Union[str, Any] = orig_state_dict['''cls.predictions.decoder.bias'''] lowercase__ : List[str] = torch.arange(__lowerCamelCase ).expand((1, -1) ) + 2 return orig_state_dict def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) -> Optional[Any]: lowercase__ : Tuple = torch.load(__lowerCamelCase , map_location='''cpu''' )['''model_state_dict'''] lowercase__ : List[Any] = YosoConfig.from_json_file(__lowerCamelCase ) lowercase__ : List[Any] = YosoForMaskedLM(__lowerCamelCase ) lowercase__ : Optional[Any] = convert_checkpoint_helper(config.max_position_embeddings , __lowerCamelCase ) print(model.load_state_dict(__lowerCamelCase ) ) model.eval() model.save_pretrained(__lowerCamelCase ) print(f"""Checkpoint successfuly converted. Model saved at {pytorch_dump_path}""" ) if __name__ == "__main__": lowerCAmelCase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '--pytorch_model_path', default=None, type=str, required=True, help='Path to YOSO pytorch checkpoint.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help='The json file for YOSO model config.', ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) lowerCAmelCase_ = parser.parse_args() convert_yoso_checkpoint(args.pytorch_model_path, args.config_file, args.pytorch_dump_path)
16
'''simple docstring''' import argparse from collections import OrderedDict from pathlib import Path import requests import torch from PIL import Image from transformers import GLPNConfig, GLPNForDepthEstimation, GLPNImageProcessor from transformers.utils import logging logging.set_verbosity_info() __snake_case =logging.get_logger(__name__) def a_ ( lowerCamelCase : Any ): lowerCAmelCase = OrderedDict() for key, value in state_dict.items(): if key.startswith('module.encoder' ): lowerCAmelCase = key.replace('module.encoder' , 'glpn.encoder' ) if key.startswith('module.decoder' ): lowerCAmelCase = key.replace('module.decoder' , 'decoder.stages' ) if "patch_embed" in key: # replace for example patch_embed1 by patch_embeddings.0 lowerCAmelCase = key[key.find('patch_embed' ) + len('patch_embed' )] lowerCAmelCase = key.replace(f'''patch_embed{idx}''' , f'''patch_embeddings.{int(lowerCamelCase )-1}''' ) if "norm" in key: lowerCAmelCase = key.replace('norm' , 'layer_norm' ) if "glpn.encoder.layer_norm" in key: # replace for example layer_norm1 by layer_norm.0 lowerCAmelCase = key[key.find('glpn.encoder.layer_norm' ) + len('glpn.encoder.layer_norm' )] lowerCAmelCase = key.replace(f'''layer_norm{idx}''' , f'''layer_norm.{int(lowerCamelCase )-1}''' ) if "layer_norm1" in key: lowerCAmelCase = key.replace('layer_norm1' , 'layer_norm_1' ) if "layer_norm2" in key: lowerCAmelCase = key.replace('layer_norm2' , 'layer_norm_2' ) if "block" in key: # replace for example block1 by block.0 lowerCAmelCase = key[key.find('block' ) + len('block' )] lowerCAmelCase = key.replace(f'''block{idx}''' , f'''block.{int(lowerCamelCase )-1}''' ) if "attn.q" in key: lowerCAmelCase = key.replace('attn.q' , 'attention.self.query' ) if "attn.proj" in key: lowerCAmelCase = key.replace('attn.proj' , 'attention.output.dense' ) if "attn" in key: lowerCAmelCase = key.replace('attn' , 'attention.self' ) if "fc1" in key: lowerCAmelCase = key.replace('fc1' , 'dense1' ) if "fc2" in key: lowerCAmelCase = key.replace('fc2' , 'dense2' ) if "linear_pred" in key: lowerCAmelCase = key.replace('linear_pred' , 'classifier' ) if "linear_fuse" in key: lowerCAmelCase = key.replace('linear_fuse.conv' , 'linear_fuse' ) lowerCAmelCase = key.replace('linear_fuse.bn' , 'batch_norm' ) if "linear_c" in key: # replace for example linear_c4 by linear_c.3 lowerCAmelCase = key[key.find('linear_c' ) + len('linear_c' )] lowerCAmelCase = key.replace(f'''linear_c{idx}''' , f'''linear_c.{int(lowerCamelCase )-1}''' ) if "bot_conv" in key: lowerCAmelCase = key.replace('bot_conv' , '0.convolution' ) if "skip_conv1" in key: lowerCAmelCase = key.replace('skip_conv1' , '1.convolution' ) if "skip_conv2" in key: lowerCAmelCase = key.replace('skip_conv2' , '2.convolution' ) if "fusion1" in key: lowerCAmelCase = key.replace('fusion1' , '1.fusion' ) if "fusion2" in key: lowerCAmelCase = key.replace('fusion2' , '2.fusion' ) if "fusion3" in key: lowerCAmelCase = key.replace('fusion3' , '3.fusion' ) if "fusion" in key and "conv" in key: lowerCAmelCase = key.replace('conv' , 'convolutional_layer' ) if key.startswith('module.last_layer_depth' ): lowerCAmelCase = key.replace('module.last_layer_depth' , 'head.head' ) lowerCAmelCase = value return new_state_dict def a_ ( lowerCamelCase : List[str] , lowerCamelCase : str ): # for each of the encoder blocks: for i in range(config.num_encoder_blocks ): for j in range(config.depths[i] ): # read in weights + bias of keys and values (which is a single matrix in the original implementation) lowerCAmelCase = state_dict.pop(f'''glpn.encoder.block.{i}.{j}.attention.self.kv.weight''' ) lowerCAmelCase = state_dict.pop(f'''glpn.encoder.block.{i}.{j}.attention.self.kv.bias''' ) # next, add keys and values (in that order) to the state dict lowerCAmelCase = kv_weight[ : config.hidden_sizes[i], : ] lowerCAmelCase = kv_bias[: config.hidden_sizes[i]] lowerCAmelCase = kv_weight[ config.hidden_sizes[i] :, : ] lowerCAmelCase = kv_bias[config.hidden_sizes[i] :] def a_ ( ): lowerCAmelCase = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowerCAmelCase = Image.open(requests.get(lowerCamelCase , stream=lowerCamelCase ).raw ) return image @torch.no_grad() def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Tuple , lowerCamelCase : Optional[Any]=False , lowerCamelCase : List[str]=None ): lowerCAmelCase = GLPNConfig(hidden_sizes=[64, 128, 320, 512] , decoder_hidden_size=64 , depths=[3, 8, 27, 3] ) # load image processor (only resize + rescale) lowerCAmelCase = GLPNImageProcessor() # prepare image lowerCAmelCase = prepare_img() lowerCAmelCase = image_processor(images=lowerCamelCase , return_tensors='pt' ).pixel_values logger.info('Converting model...' ) # load original state dict lowerCAmelCase = torch.load(lowerCamelCase , map_location=torch.device('cpu' ) ) # rename keys lowerCAmelCase = rename_keys(lowerCamelCase ) # key and value matrices need special treatment read_in_k_v(lowerCamelCase , lowerCamelCase ) # create HuggingFace model and load state dict lowerCAmelCase = GLPNForDepthEstimation(lowerCamelCase ) model.load_state_dict(lowerCamelCase ) model.eval() # forward pass lowerCAmelCase = model(lowerCamelCase ) lowerCAmelCase = outputs.predicted_depth # verify output if model_name is not None: if "nyu" in model_name: lowerCAmelCase = torch.tensor( [[4.4_147, 4.0_873, 4.0_673], [3.7_890, 3.2_881, 3.1_525], [3.7_674, 3.5_423, 3.4_913]] ) elif "kitti" in model_name: lowerCAmelCase = torch.tensor( [[3.4_291, 2.7_865, 2.5_151], [3.2_841, 2.7_021, 2.3_502], [3.1_147, 2.4_625, 2.2_481]] ) else: raise ValueError(f'''Unknown model name: {model_name}''' ) lowerCAmelCase = torch.Size([1, 480, 640] ) assert predicted_depth.shape == expected_shape assert torch.allclose(predicted_depth[0, :3, :3] , lowerCamelCase , atol=1e-4 ) print('Looks ok!' ) # finally, push to hub if required if push_to_hub: logger.info('Pushing model and image processor to the hub...' ) model.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=lowerCamelCase , ) image_processor.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=lowerCamelCase , ) if __name__ == "__main__": __snake_case =argparse.ArgumentParser() parser.add_argument( """--checkpoint_path""", default=None, 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 folder to output PyTorch model.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to upload the model to the HuggingFace hub.""" ) parser.add_argument( """--model_name""", default="""glpn-kitti""", type=str, help="""Name of the model in case you're pushing to the hub.""", ) __snake_case =parser.parse_args() convert_glpn_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
4
0
"""simple docstring""" from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import PIL from PIL import Image from ...utils import ( BaseOutput, OptionalDependencyNotAvailable, is_flax_available, is_k_diffusion_available, is_k_diffusion_version, is_onnx_available, is_torch_available, is_transformers_available, is_transformers_version, ) @dataclass class _lowerCAmelCase ( lowercase ): """simple docstring""" __UpperCAmelCase : Union[List[PIL.Image.Image], np.ndarray] __UpperCAmelCase : Optional[List[bool]] try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipeline_cycle_diffusion import CycleDiffusionPipeline from .pipeline_stable_diffusion import StableDiffusionPipeline from .pipeline_stable_diffusion_attend_and_excite import StableDiffusionAttendAndExcitePipeline from .pipeline_stable_diffusion_imgaimg import StableDiffusionImgaImgPipeline from .pipeline_stable_diffusion_inpaint import StableDiffusionInpaintPipeline from .pipeline_stable_diffusion_inpaint_legacy import StableDiffusionInpaintPipelineLegacy from .pipeline_stable_diffusion_instruct_pixapix import StableDiffusionInstructPixaPixPipeline from .pipeline_stable_diffusion_latent_upscale import StableDiffusionLatentUpscalePipeline from .pipeline_stable_diffusion_ldmad import StableDiffusionLDMaDPipeline from .pipeline_stable_diffusion_model_editing import StableDiffusionModelEditingPipeline from .pipeline_stable_diffusion_panorama import StableDiffusionPanoramaPipeline from .pipeline_stable_diffusion_paradigms import StableDiffusionParadigmsPipeline from .pipeline_stable_diffusion_sag import StableDiffusionSAGPipeline from .pipeline_stable_diffusion_upscale import StableDiffusionUpscalePipeline from .pipeline_stable_unclip import StableUnCLIPPipeline from .pipeline_stable_unclip_imgaimg import StableUnCLIPImgaImgPipeline from .safety_checker import StableDiffusionSafetyChecker from .stable_unclip_image_normalizer import StableUnCLIPImageNormalizer try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('>=', '4.25.0')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import StableDiffusionImageVariationPipeline else: from .pipeline_stable_diffusion_image_variation import StableDiffusionImageVariationPipeline try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('>=', '4.26.0')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ( StableDiffusionDepthaImgPipeline, StableDiffusionDiffEditPipeline, StableDiffusionPixaPixZeroPipeline, ) else: from .pipeline_stable_diffusion_depthaimg import StableDiffusionDepthaImgPipeline from .pipeline_stable_diffusion_diffedit import StableDiffusionDiffEditPipeline from .pipeline_stable_diffusion_pixapix_zero import StableDiffusionPixaPixZeroPipeline try: if not ( is_torch_available() and is_transformers_available() and is_k_diffusion_available() and is_k_diffusion_version('>=', '0.0.12') ): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_and_k_diffusion_objects import * # noqa F403 else: from .pipeline_stable_diffusion_k_diffusion import StableDiffusionKDiffusionPipeline try: if not (is_transformers_available() and is_onnx_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_onnx_objects import * # noqa F403 else: from .pipeline_onnx_stable_diffusion import OnnxStableDiffusionPipeline, StableDiffusionOnnxPipeline from .pipeline_onnx_stable_diffusion_imgaimg import OnnxStableDiffusionImgaImgPipeline from .pipeline_onnx_stable_diffusion_inpaint import OnnxStableDiffusionInpaintPipeline from .pipeline_onnx_stable_diffusion_inpaint_legacy import OnnxStableDiffusionInpaintPipelineLegacy from .pipeline_onnx_stable_diffusion_upscale import OnnxStableDiffusionUpscalePipeline if is_transformers_available() and is_flax_available(): import flax @flax.struct.dataclass class _lowerCAmelCase ( lowercase ): """simple docstring""" __UpperCAmelCase : np.ndarray __UpperCAmelCase : List[bool] from ...schedulers.scheduling_pndm_flax import PNDMSchedulerState from .pipeline_flax_stable_diffusion import FlaxStableDiffusionPipeline from .pipeline_flax_stable_diffusion_imgaimg import FlaxStableDiffusionImgaImgPipeline from .pipeline_flax_stable_diffusion_inpaint import FlaxStableDiffusionInpaintPipeline from .safety_checker_flax import FlaxStableDiffusionSafetyChecker
17
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class UpperCAmelCase_ ( unittest.TestCase ): @slow def __UpperCAmelCase ( self : str ) -> List[str]: lowerCAmelCase = XLMRobertaModel.from_pretrained('xlm-roberta-base' ) lowerCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house lowerCAmelCase = torch.Size((1, 1_2, 7_6_8) ) # batch_size, sequence_length, embedding_vector_dim lowerCAmelCase = torch.tensor( [[-0.0_101, 0.1_218, -0.0_803, 0.0_801, 0.1_327, 0.0_776, -0.1_215, 0.2_383, 0.3_338, 0.3_106, 0.0_300, 0.0_252]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): lowerCAmelCase = model(UpperCAmelCase__ )['last_hidden_state'].detach() self.assertEqual(output.shape , UpperCAmelCase__ ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , UpperCAmelCase__ , atol=1E-3 ) ) @slow def __UpperCAmelCase ( self : List[Any] ) -> Tuple: lowerCAmelCase = XLMRobertaModel.from_pretrained('xlm-roberta-large' ) lowerCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house lowerCAmelCase = torch.Size((1, 1_2, 1_0_2_4) ) # batch_size, sequence_length, embedding_vector_dim lowerCAmelCase = torch.tensor( [[-0.0_699, -0.0_318, 0.0_705, -0.1_241, 0.0_999, -0.0_520, 0.1_004, -0.1_838, -0.4_704, 0.1_437, 0.0_821, 0.0_126]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): lowerCAmelCase = model(UpperCAmelCase__ )['last_hidden_state'].detach() self.assertEqual(output.shape , UpperCAmelCase__ ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , UpperCAmelCase__ , atol=1E-3 ) )
4
0
def _snake_case ( lowerCAmelCase : int = 3 , lowerCAmelCase : int = 7 , lowerCAmelCase : int = 1_0_0_0_0_0_0 ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = 0 SCREAMING_SNAKE_CASE_ : List[str] = 1 for current_denominator in range(1 , limit + 1 ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = current_denominator * numerator // denominator if current_denominator % denominator == 0: current_numerator -= 1 if current_numerator * max_denominator > current_denominator * max_numerator: SCREAMING_SNAKE_CASE_ : List[str] = current_numerator SCREAMING_SNAKE_CASE_ : int = current_denominator return max_numerator if __name__ == "__main__": print(solution(numerator=3, denominator=7, limit=1_00_00_00))
18
'''simple docstring''' import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def a_ ( lowerCamelCase : Dict ): lowerCAmelCase = {} lowerCAmelCase = tokenizer(example['content'] , truncation=lowerCamelCase )['input_ids'] lowerCAmelCase = len(example['content'] ) / len(output['input_ids'] ) return output __snake_case =HfArgumentParser(PretokenizationArguments) __snake_case =parser.parse_args() if args.num_workers is None: __snake_case =multiprocessing.cpu_count() __snake_case =AutoTokenizer.from_pretrained(args.tokenizer_dir) __snake_case =time.time() __snake_case =load_dataset(args.dataset_name, split="""train""") print(F'''Dataset loaded in {time.time()-t_start:.2f}s''') __snake_case =time.time() __snake_case =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''') __snake_case =time.time() ds.push_to_hub(args.tokenized_data_repo) print(F'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
4
0
import unittest import numpy as np import torch from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE_( self ) -> str: lowerCamelCase_ = 10 def SCREAMING_SNAKE_CASE_( self ) -> str: lowerCamelCase_ = [1, 2, 3, 4] lowerCamelCase_ = [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] self.assertEqual(truncate_or_pad(lowercase , self.block_size , 0 ) , lowercase ) def SCREAMING_SNAKE_CASE_( self ) -> Dict: lowerCamelCase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] lowerCamelCase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(lowercase , self.block_size , 0 ) , lowercase ) def SCREAMING_SNAKE_CASE_( self ) -> int: lowerCamelCase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] lowerCamelCase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(lowercase , self.block_size , 0 ) , lowercase ) def SCREAMING_SNAKE_CASE_( self ) -> Union[str, Any]: lowerCamelCase_ = "It was the year of Our Lord one thousand seven hundred and\n seventy-five.\n\nSpiritual revelations were conceded to England at that\n favoured period, as at this." lowerCamelCase_ , lowerCamelCase_ = process_story(lowercase ) self.assertEqual(lowercase , [] ) def SCREAMING_SNAKE_CASE_( self ) -> Optional[Any]: lowerCamelCase_ = "" lowerCamelCase_ , lowerCamelCase_ = process_story(lowercase ) self.assertEqual(lowercase , [] ) self.assertEqual(lowercase , [] ) def SCREAMING_SNAKE_CASE_( self ) -> Optional[Any]: lowerCamelCase_ = ( "It was the year of Our Lord one thousand seven hundred and " "seventy-five\n\nSpiritual revelations were conceded to England " "at that favoured period, as at this.\n@highlight\n\nIt was the best of times" ) lowerCamelCase_ , lowerCamelCase_ = process_story(lowercase ) lowerCamelCase_ = [ "It was the year of Our Lord one thousand seven hundred and seventy-five.", "Spiritual revelations were conceded to England at that favoured period, as at this.", ] self.assertEqual(lowercase , lowercase ) lowerCamelCase_ = ["It was the best of times."] self.assertEqual(lowercase , lowercase ) def SCREAMING_SNAKE_CASE_( self ) -> Optional[Any]: lowerCamelCase_ = torch.tensor([1, 2, 3, 4] ) lowerCamelCase_ = torch.tensor([1, 1, 1, 1] ) np.testing.assert_array_equal(build_mask(lowercase , 0 ).numpy() , expected.numpy() ) def SCREAMING_SNAKE_CASE_( self ) -> Optional[Any]: lowerCamelCase_ = torch.tensor([1, 2, 3, 4, 23, 23, 23] ) lowerCamelCase_ = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(lowercase , 23 ).numpy() , expected.numpy() ) def SCREAMING_SNAKE_CASE_( self ) -> Tuple: lowerCamelCase_ = torch.tensor([8, 2, 3, 4, 1, 1, 1] ) lowerCamelCase_ = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(lowercase , 1 ).numpy() , expected.numpy() ) def SCREAMING_SNAKE_CASE_( self ) -> Any: lowerCamelCase_ = 101 lowerCamelCase_ = torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 101, 5, 6], [1, 101, 3, 4, 101, 6]] ) lowerCamelCase_ = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]] ) lowerCamelCase_ = compute_token_type_ids(lowercase , lowercase ) np.testing.assert_array_equal(lowercase , lowercase )
19
'''simple docstring''' import logging from dataclasses import dataclass, field from pathlib import Path from typing import Optional, Union from .generation.configuration_utils import GenerationConfig from .training_args import TrainingArguments from .utils import add_start_docstrings __snake_case =logging.getLogger(__name__) @dataclass @add_start_docstrings(TrainingArguments.__doc__ ) class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : bool = field(default=__lowercase , metadata={'''help''': '''Whether to use SortishSampler or not.'''} ) lowerCamelCase : bool = field( default=__lowercase , metadata={'''help''': '''Whether to use generate to calculate generative metrics (ROUGE, BLEU).'''} ) lowerCamelCase : Optional[int] = field( default=__lowercase , metadata={ '''help''': ( '''The `max_length` to use on each evaluation loop when `predict_with_generate=True`. Will default ''' '''to the `max_length` value of the model configuration.''' ) } , ) lowerCamelCase : Optional[int] = field( default=__lowercase , metadata={ '''help''': ( '''The `num_beams` to use on each evaluation loop when `predict_with_generate=True`. Will default ''' '''to the `num_beams` value of the model configuration.''' ) } , ) lowerCamelCase : Optional[Union[str, Path, GenerationConfig]] = field( default=__lowercase , metadata={ '''help''': '''Model id, file path or url pointing to a GenerationConfig json file, to use during prediction.''' } , ) def __UpperCAmelCase ( self : Dict ) -> List[str]: lowerCAmelCase = super().to_dict() for k, v in d.items(): if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): lowerCAmelCase = v.to_dict() return d
4
0
def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> float: def get_matched_characters(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: lowercase : Tuple = [] lowercase : Union[str, Any] = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): lowercase : Dict = int(max(0 , i - limit ) ) lowercase : Dict = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(SCREAMING_SNAKE_CASE__ ) lowercase : Any = f"{_stra[0:_stra.index(SCREAMING_SNAKE_CASE__ )]} {_stra[_stra.index(SCREAMING_SNAKE_CASE__ ) + 1:]}" return "".join(SCREAMING_SNAKE_CASE__ ) # matching characters lowercase : Optional[Any] = get_matched_characters(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = get_matched_characters(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = len(SCREAMING_SNAKE_CASE__ ) # transposition lowercase : List[Any] = ( len([(ca, ca) for ca, ca in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if ca != ca] ) // 2 ) if not match_count: lowercase : str = 0.0 else: lowercase : Union[str, Any] = ( 1 / 3 * ( match_count / len(SCREAMING_SNAKE_CASE__ ) + match_count / len(SCREAMING_SNAKE_CASE__ ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters lowercase : Union[str, Any] = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler("""hello""", """world"""))
20
'''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() __snake_case =logging.get_logger("""transformers.models.encodec""") __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ """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""", } __snake_case ={ **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_DECODER, } __snake_case ={ **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_ENCODER_48K, **MAPPING_DECODER, **MAPPING_DECODER_48K, } __snake_case =[] __snake_case =[] def a_ ( lowerCamelCase : Optional[Any] , lowerCamelCase : Union[str, Any] , lowerCamelCase : int , lowerCamelCase : Any , lowerCamelCase : List[str] ): for attribute in key.split('.' ): lowerCAmelCase = getattr(lowerCamelCase , lowerCamelCase ) if weight_type is not None: lowerCAmelCase = getattr(lowerCamelCase , lowerCamelCase ).shape else: lowerCAmelCase = 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": lowerCAmelCase = value elif weight_type == "weight_g": lowerCAmelCase = value elif weight_type == "weight_v": lowerCAmelCase = value elif weight_type == "bias": lowerCAmelCase = value elif weight_type == "running_mean": lowerCAmelCase = value elif weight_type == "running_var": lowerCAmelCase = value elif weight_type == "num_batches_tracked": lowerCAmelCase = value elif weight_type == "weight_ih_l0": lowerCAmelCase = value elif weight_type == "weight_hh_l0": lowerCAmelCase = value elif weight_type == "bias_ih_l0": lowerCAmelCase = value elif weight_type == "bias_hh_l0": lowerCAmelCase = value elif weight_type == "weight_ih_l1": lowerCAmelCase = value elif weight_type == "weight_hh_l1": lowerCAmelCase = value elif weight_type == "bias_ih_l1": lowerCAmelCase = value elif weight_type == "bias_hh_l1": lowerCAmelCase = value else: lowerCAmelCase = value logger.info(f'''{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.''' ) def a_ ( lowerCamelCase : Optional[Any] , lowerCamelCase : Optional[Any] ): for key in ignore_keys: if key.endswith('.*' ): if name.startswith(key[:-1] ): return True elif ".*." in key: lowerCAmelCase , lowerCAmelCase = key.split('.*.' ) if prefix in name and suffix in name: return True elif key in name: return True return False def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Any , lowerCamelCase : str ): lowerCAmelCase = [] if model_name == "encodec_24khz" or "encodec_32khz": lowerCAmelCase = MAPPING_24K elif model_name == "encodec_48khz": lowerCAmelCase = 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 lowerCAmelCase = False for key, mapped_key in MAPPING.items(): if "*" in key: lowerCAmelCase , lowerCAmelCase = key.split('.*.' ) if prefix in name and suffix in name: lowerCAmelCase = suffix if key in name: # HACK otherwise .embed gets initialized with .embed_avg too if key.endswith('embed' ) and name.endswith('embed_avg' ): continue lowerCAmelCase = True if "*" in mapped_key: lowerCAmelCase = name.split(lowerCamelCase )[0].split('.' )[-2] lowerCAmelCase = mapped_key.replace('*' , lowerCamelCase ) if "weight_g" in name: lowerCAmelCase = 'weight_g' elif "weight_v" in name: lowerCAmelCase = 'weight_v' elif "weight_ih_l0" in name: lowerCAmelCase = 'weight_ih_l0' elif "weight_hh_l0" in name: lowerCAmelCase = 'weight_hh_l0' elif "bias_ih_l0" in name: lowerCAmelCase = 'bias_ih_l0' elif "bias_hh_l0" in name: lowerCAmelCase = 'bias_hh_l0' elif "weight_ih_l1" in name: lowerCAmelCase = 'weight_ih_l1' elif "weight_hh_l1" in name: lowerCAmelCase = 'weight_hh_l1' elif "bias_ih_l1" in name: lowerCAmelCase = 'bias_ih_l1' elif "bias_hh_l1" in name: lowerCAmelCase = 'bias_hh_l1' elif "bias" in name: lowerCAmelCase = 'bias' elif "weight" in name: lowerCAmelCase = 'weight' elif "running_mean" in name: lowerCAmelCase = 'running_mean' elif "running_var" in name: lowerCAmelCase = 'running_var' elif "num_batches_tracked" in name: lowerCAmelCase = 'num_batches_tracked' else: lowerCAmelCase = 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 a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Union[str, Any] , lowerCamelCase : str , lowerCamelCase : Dict=None , lowerCamelCase : Union[str, Any]=None , ): if config_path is not None: lowerCAmelCase = EncodecConfig.from_pretrained(lowerCamelCase ) else: lowerCAmelCase = EncodecConfig() if model_name == "encodec_24khz": pass # config is already correct elif model_name == "encodec_32khz": lowerCAmelCase = [8, 5, 4, 4] lowerCAmelCase = [2.2] lowerCAmelCase = 64 lowerCAmelCase = 32000 lowerCAmelCase = 2048 lowerCAmelCase = False lowerCAmelCase = False lowerCAmelCase = False elif model_name == "encodec_48khz": lowerCAmelCase = [8, 5, 4, 2] lowerCAmelCase = [3.0, 6.0, 12.0, 24.0] lowerCAmelCase = 48000 lowerCAmelCase = 2 lowerCAmelCase = False lowerCAmelCase = 'time_group_norm' lowerCAmelCase = True lowerCAmelCase = 1.0 lowerCAmelCase = 0.01 else: raise ValueError(f'''Unknown model name: {model_name}''' ) lowerCAmelCase = EncodecModel(lowerCamelCase ) lowerCAmelCase = 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 ) lowerCAmelCase = 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 lowerCAmelCase = 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__": __snake_case =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.""" ) __snake_case =parser.parse_args() convert_checkpoint( args.model, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
4
0