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""" from string import ascii_uppercase _a = {str(ord(c) - 55): c for c in ascii_uppercase} def __a ( __lowerCamelCase, __lowerCamelCase ): if isinstance(__lowerCamelCase, __lowerCamelCase ): raise TypeError("int() can't convert non-string with explicit base" ) if num < 0: raise ValueError("parameter must be positive int" ) if isinstance(__lowerCamelCase, __lowerCamelCase ): raise TypeError("'str' object cannot be interpreted as an integer" ) if isinstance(__lowerCamelCase, __lowerCamelCase ): raise TypeError("'float' object cannot be interpreted as an integer" ) if base in (0, 1): raise ValueError("base must be >= 2" ) if base > 36: raise ValueError("base must be <= 36" ) UpperCAmelCase_ : Optional[Any] = "" UpperCAmelCase_ : int = 0 UpperCAmelCase_ : str = 0 while div != 1: UpperCAmelCase_ , UpperCAmelCase_ : str = divmod(__lowerCamelCase, __lowerCamelCase ) if base >= 11 and 9 < mod < 36: UpperCAmelCase_ : Tuple = ALPHABET_VALUES[str(__lowerCamelCase )] else: UpperCAmelCase_ : int = str(__lowerCamelCase ) new_value += actual_value UpperCAmelCase_ : Any = num // base UpperCAmelCase_ : Optional[int] = div if div == 0: return str(new_value[::-1] ) elif div == 1: new_value += str(__lowerCamelCase ) return str(new_value[::-1] ) return new_value[::-1] if __name__ == "__main__": import doctest doctest.testmod() for base in range(2, 37): for num in range(1_000): assert int(decimal_to_any(num, base), base) == num, ( num, base, decimal_to_any(num, base), int(decimal_to_any(num, base), base), )
61
def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Dict =str(bin(__lowerCAmelCase ) ) binary_number += "0" * shift_amount return binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Any =str(bin(__lowerCAmelCase ) )[2:] if shift_amount >= len(__lowerCAmelCase ): return "0b0" UpperCAmelCase : Optional[Any] =binary_number[: len(__lowerCAmelCase ) - shift_amount] return "0b" + shifted_binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number >= 0: # Get binary representation of positive number UpperCAmelCase : Optional[Any] ='''0''' + str(bin(__lowerCAmelCase ) ).strip('''-''' )[2:] else: # Get binary (2's complement) representation of negative number UpperCAmelCase : int =len(bin(__lowerCAmelCase )[3:] ) # Find 2's complement of number UpperCAmelCase : Any =bin(abs(__lowerCAmelCase ) - (1 << binary_number_length) )[3:] UpperCAmelCase : Optional[Any] =( '''1''' + '''0''' * (binary_number_length - len(__lowerCAmelCase )) + binary_number ) if shift_amount >= len(__lowerCAmelCase ): return "0b" + binary_number[0] * len(__lowerCAmelCase ) return ( "0b" + binary_number[0] * shift_amount + binary_number[: len(__lowerCAmelCase ) - shift_amount] ) if __name__ == "__main__": import doctest doctest.testmod()
348
0
import webbrowser from sys import argv from urllib.parse import parse_qs, quote import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": _A = '%20'.join(argv[1:]) if len(argv) > 1 else quote(str(input('Search: '))) print('Googling.....') _A = f"""https://www.google.com/search?q={query}&num=100""" _A = requests.get( url, headers={'User-Agent': str(UserAgent().random)}, ) try: _A = ( BeautifulSoup(res.text, 'html.parser') .find('div', attrs={'class': 'yuRUbf'}) .find('a') .get('href') ) except AttributeError: _A = parse_qs( BeautifulSoup(res.text, 'html.parser') .find('div', attrs={'class': 'kCrYT'}) .find('a') .get('href') )['url'][0] webbrowser.open(link)
62
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) # TODO Update this __snake_case = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Tuple = """esm""" def __init__( self , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=1026 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__="absolute" , snake_case__=True , snake_case__=None , snake_case__=False , snake_case__=False , snake_case__=None , snake_case__=None , **snake_case__ , ) -> Union[str, Any]: '''simple docstring''' super().__init__(pad_token_id=snake_case__ , mask_token_id=snake_case__ , **snake_case__ ) UpperCAmelCase : List[str] =vocab_size UpperCAmelCase : str =hidden_size UpperCAmelCase : List[Any] =num_hidden_layers UpperCAmelCase : Optional[Any] =num_attention_heads UpperCAmelCase : str =intermediate_size UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : int =attention_probs_dropout_prob UpperCAmelCase : Dict =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : Union[str, Any] =layer_norm_eps UpperCAmelCase : Dict =position_embedding_type UpperCAmelCase : Optional[Any] =use_cache UpperCAmelCase : int =emb_layer_norm_before UpperCAmelCase : List[str] =token_dropout UpperCAmelCase : Optional[Any] =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) UpperCAmelCase : Optional[Any] =EsmFoldConfig() elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =EsmFoldConfig(**snake_case__ ) UpperCAmelCase : Tuple =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) UpperCAmelCase : Any =get_default_vocab_list() else: UpperCAmelCase : Tuple =vocab_list else: UpperCAmelCase : Optional[int] =None UpperCAmelCase : Union[str, Any] =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , snake_case__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =super().to_dict() if isinstance(self.esmfold_config , snake_case__ ): UpperCAmelCase : str =self.esmfold_config.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : str = None __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : float = 0 __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : int = 128 __lowerCamelCase : "TrunkConfig" = None def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' if self.trunk is None: UpperCAmelCase : str =TrunkConfig() elif isinstance(self.trunk , snake_case__ ): UpperCAmelCase : Optional[int] =TrunkConfig(**self.trunk ) def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[Any] =asdict(self ) UpperCAmelCase : Any =self.trunk.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 48 __lowerCamelCase : int = 1024 __lowerCamelCase : int = 128 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : float = 0 __lowerCamelCase : float = 0 __lowerCamelCase : bool = False __lowerCamelCase : int = 4 __lowerCamelCase : Optional[int] = 128 __lowerCamelCase : "StructureModuleConfig" = None def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' if self.structure_module is None: UpperCAmelCase : Any =StructureModuleConfig() elif isinstance(self.structure_module , snake_case__ ): UpperCAmelCase : str =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f'''`max_recycles` should be positive, got {self.max_recycles}.''' ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f''' {self.sequence_state_dim} and {self.sequence_state_dim}.''' ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f''' {self.pairwise_state_dim} and {self.pairwise_state_dim}.''' ) UpperCAmelCase : Optional[int] =self.sequence_state_dim // self.sequence_head_width UpperCAmelCase : Any =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f''' {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.''' ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f''' {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.''' ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f'''`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.''' ) if self.dropout >= 0.4: raise ValueError(f'''`dropout` should not be greater than 0.4, got {self.dropout}.''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =asdict(self ) UpperCAmelCase : Tuple =self.structure_module.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 384 __lowerCamelCase : int = 128 __lowerCamelCase : int = 16 __lowerCamelCase : int = 128 __lowerCamelCase : int = 12 __lowerCamelCase : int = 4 __lowerCamelCase : int = 8 __lowerCamelCase : float = 0.1 __lowerCamelCase : int = 8 __lowerCamelCase : int = 1 __lowerCamelCase : int = 2 __lowerCamelCase : int = 7 __lowerCamelCase : int = 10 __lowerCamelCase : float = 1E-8 __lowerCamelCase : float = 1E5 def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return asdict(self ) def lowerCAmelCase_ ( )-> Tuple: '''simple docstring''' return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
348
0
'''simple docstring''' from __future__ import annotations import unittest from transformers import MobileBertConfig, is_tf_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_MODEL_FOR_PRETRAINING_MAPPING, TFMobileBertForMaskedLM, TFMobileBertForMultipleChoice, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertModel, ) @require_tf class __SCREAMING_SNAKE_CASE (lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): """simple docstring""" __a =( ( TFMobileBertModel, TFMobileBertForMaskedLM, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertForMultipleChoice, ) if is_tf_available() else () ) __a =( { 'feature-extraction': TFMobileBertModel, 'fill-mask': TFMobileBertForMaskedLM, 'question-answering': TFMobileBertForQuestionAnswering, 'text-classification': TFMobileBertForSequenceClassification, 'token-classification': TFMobileBertForTokenClassification, 'zero-shot': TFMobileBertForSequenceClassification, } if is_tf_available() else {} ) __a =False __a =False def UpperCamelCase__ ( self : str , __a : Tuple , __a : Optional[Any] , __a : List[str]=False ): _a = super()._prepare_for_class(__a , __a , return_labels=__a ) if return_labels: if model_class in get_values(__a ): _a = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) return inputs_dict class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" def __init__( self : List[str] , __a : int , __a : Union[str, Any]=13 , __a : int=7 , __a : List[str]=True , __a : str=True , __a : Union[str, Any]=True , __a : Union[str, Any]=True , __a : Tuple=99 , __a : Any=32 , __a : Tuple=32 , __a : Any=2 , __a : int=4 , __a : Dict=37 , __a : Union[str, Any]="gelu" , __a : Optional[Any]=0.1 , __a : Union[str, Any]=0.1 , __a : Dict=5_12 , __a : int=16 , __a : str=2 , __a : Tuple=0.02 , __a : Any=3 , __a : Tuple=4 , __a : Any=None , ): _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_input_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_labels _a = num_choices _a = scope _a = embedding_size def UpperCamelCase__ ( self : Any ): _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_input_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = None _a = None _a = None if self.use_labels: _a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _a = ids_tensor([self.batch_size] , self.num_choices ) _a = MobileBertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , embedding_size=self.embedding_size , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase__ ( self : Optional[int] , __a : Optional[Any] , __a : int , __a : Tuple , __a : str , __a : List[Any] , __a : List[Any] , __a : str ): _a = TFMobileBertModel(config=__a ) _a = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} _a = model(__a ) _a = [input_ids, input_mask] _a = model(__a ) _a = model(__a ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCamelCase__ ( self : List[str] , __a : List[str] , __a : Dict , __a : Tuple , __a : Union[str, Any] , __a : Dict , __a : List[Any] , __a : List[str] ): _a = TFMobileBertForMaskedLM(config=__a ) _a = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} _a = model(__a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase__ ( self : Union[str, Any] , __a : Dict , __a : Dict , __a : Any , __a : Any , __a : str , __a : Tuple , __a : str ): _a = TFMobileBertForNextSentencePrediction(config=__a ) _a = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} _a = model(__a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def UpperCamelCase__ ( self : List[Any] , __a : Tuple , __a : Optional[Any] , __a : List[Any] , __a : List[Any] , __a : List[str] , __a : Optional[Any] , __a : List[Any] ): _a = TFMobileBertForPreTraining(config=__a ) _a = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} _a = model(__a ) self.parent.assertEqual( result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.seq_relationship_logits.shape , (self.batch_size, 2) ) def UpperCamelCase__ ( self : List[str] , __a : List[Any] , __a : str , __a : str , __a : Union[str, Any] , __a : List[Any] , __a : List[Any] , __a : List[Any] ): _a = self.num_labels _a = TFMobileBertForSequenceClassification(config=__a ) _a = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} _a = model(__a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase__ ( self : Any , __a : str , __a : Any , __a : Optional[int] , __a : int , __a : Optional[int] , __a : int , __a : List[Any] ): _a = self.num_choices _a = TFMobileBertForMultipleChoice(config=__a ) _a = tf.tile(tf.expand_dims(__a , 1 ) , (1, self.num_choices, 1) ) _a = tf.tile(tf.expand_dims(__a , 1 ) , (1, self.num_choices, 1) ) _a = tf.tile(tf.expand_dims(__a , 1 ) , (1, self.num_choices, 1) ) _a = { "input_ids": multiple_choice_inputs_ids, "attention_mask": multiple_choice_input_mask, "token_type_ids": multiple_choice_token_type_ids, } _a = model(__a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCamelCase__ ( self : List[str] , __a : List[str] , __a : List[Any] , __a : Dict , __a : Dict , __a : int , __a : str , __a : List[str] ): _a = self.num_labels _a = TFMobileBertForTokenClassification(config=__a ) _a = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} _a = model(__a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase__ ( self : Optional[Any] , __a : Tuple , __a : List[Any] , __a : str , __a : int , __a : Any , __a : int , __a : List[str] ): _a = TFMobileBertForQuestionAnswering(config=__a ) _a = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} _a = model(__a ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCamelCase__ ( self : Any ): _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict def UpperCamelCase__ ( self : str ): _a = TFMobileBertModelTest.TFMobileBertModelTester(self ) _a = ConfigTester(self , config_class=__a , hidden_size=37 ) def UpperCamelCase__ ( self : Tuple ): self.config_tester.run_common_tests() def UpperCamelCase__ ( self : List[str] ): _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_model(*__a ) def UpperCamelCase__ ( self : Union[str, Any] ): _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_masked_lm(*__a ) def UpperCamelCase__ ( self : Any ): _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__a ) def UpperCamelCase__ ( self : List[Any] ): _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__a ) def UpperCamelCase__ ( self : Union[str, Any] ): _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_pretraining(*__a ) def UpperCamelCase__ ( self : List[Any] ): _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_question_answering(*__a ) def UpperCamelCase__ ( self : Optional[Any] ): _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__a ) def UpperCamelCase__ ( self : Any ): _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_token_classification(*__a ) @slow def UpperCamelCase__ ( self : List[str] ): # for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["google/mobilebert-uncased"]: _a = TFMobileBertModel.from_pretrained(__a ) self.assertIsNotNone(__a ) @require_tf class __SCREAMING_SNAKE_CASE (unittest.TestCase ): """simple docstring""" @slow def UpperCamelCase__ ( self : Optional[Any] ): _a = TFMobileBertForPreTraining.from_pretrained("google/mobilebert-uncased" ) _a = tf.constant([[0, 1, 2, 3, 4, 5]] ) _a = model(__a )[0] _a = [1, 6, 3_05_22] self.assertEqual(output.shape , __a ) _a = tf.constant( [ [ [-4.5919547, -9.248295, -9.645256], [-6.7306175, -6.440284, -6.6052837], [-7.2743506, -6.7847915, -6.024673], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , __a , atol=1e-4 )
63
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[int] = (KDPMaDiscreteScheduler,) __lowerCamelCase : List[str] = 10 def UpperCAmelCase__ ( self , **snake_case__ ) -> str: '''simple docstring''' UpperCAmelCase : int ={ '''num_train_timesteps''': 1100, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', } config.update(**snake_case__ ) return config def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=snake_case__ , beta_end=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' UpperCAmelCase : Optional[Any] =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config(prediction_type='''v_prediction''' ) UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : str =self.dummy_model() UpperCAmelCase : Optional[Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : Union[str, Any] =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : str =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Any =model(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : int =output.prev_sample UpperCAmelCase : Dict =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Optional[Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.69_34e-07 ) < 1e-2 assert abs(result_mean.item() - 6.11_12e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.6_93_42_86_50_17_09_72e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0002 ) < 1e-3 def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : Any =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config() UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : Optional[int] =self.dummy_model() UpperCAmelCase : Union[str, Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : str =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : Dict =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =model(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =output.prev_sample UpperCAmelCase : Any =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Union[str, Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : List[Any] =self.scheduler_classes[0] UpperCAmelCase : Dict =self.get_scheduler_config() UpperCAmelCase : List[str] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps , device=snake_case__ ) UpperCAmelCase : int =self.dummy_model() UpperCAmelCase : Tuple =self.dummy_sample_deter.to(snake_case__ ) * scheduler.init_noise_sigma for t in scheduler.timesteps: UpperCAmelCase : Optional[Any] =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : int =model(snake_case__ , snake_case__ ) UpperCAmelCase : str =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =output.prev_sample UpperCAmelCase : List[str] =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Dict =torch.mean(torch.abs(snake_case__ ) ) if str(snake_case__ ).startswith('''cpu''' ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3
348
0
"""simple docstring""" # using dfs for finding eulerian path traversal def UpperCAmelCase__ (snake_case__ : List[str] , snake_case__ : Tuple , snake_case__ : int , snake_case__ : List[str]=None ): """simple docstring""" _snake_case : List[Any] = (path or []) + [u] for v in graph[u]: if visited_edge[u][v] is False: _snake_case , _snake_case : Dict = True, True _snake_case : str = dfs(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) return path def UpperCAmelCase__ (snake_case__ : Union[str, Any] , snake_case__ : Optional[int] ): """simple docstring""" _snake_case : List[str] = 0 _snake_case : List[str] = -1 for i in range(snake_case__ ): if i not in graph.keys(): continue if len(graph[i] ) % 2 == 1: odd_degree_nodes += 1 _snake_case : int = i if odd_degree_nodes == 0: return 1, odd_node if odd_degree_nodes == 2: return 2, odd_node return 3, odd_node def UpperCAmelCase__ (snake_case__ : Optional[int] , snake_case__ : List[Any] ): """simple docstring""" _snake_case : Tuple = [[False for _ in range(max_node + 1 )] for _ in range(max_node + 1 )] _snake_case , _snake_case : Dict = check_circuit_or_path(snake_case__ , snake_case__ ) if check == 3: print("""graph is not Eulerian""" ) print("""no path""" ) return _snake_case : int = 1 if check == 2: _snake_case : Optional[int] = odd_node print("""graph has a Euler path""" ) if check == 1: print("""graph has a Euler cycle""" ) _snake_case : Optional[int] = dfs(snake_case__ , snake_case__ , snake_case__ ) print(snake_case__ ) def UpperCAmelCase__ (): """simple docstring""" _snake_case : List[str] = {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]} _snake_case : Dict = {1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]} _snake_case : Optional[Any] = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]} _snake_case : List[str] = {1: [2, 3], 2: [1, 3], 3: [1, 2]} _snake_case : List[str] = { 1: [], 2: [] # all degree is zero } _snake_case : List[Any] = 10 check_euler(snake_case__ , snake_case__ ) check_euler(snake_case__ , snake_case__ ) check_euler(snake_case__ , snake_case__ ) check_euler(snake_case__ , snake_case__ ) check_euler(snake_case__ , snake_case__ ) if __name__ == "__main__": main()
64
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class __snake_case ( unittest.TestCase ): @slow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Any =FlaxMTaForConditionalGeneration.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : Tuple =AutoTokenizer.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : List[str] =tokenizer('''Hello there''' , return_tensors='''np''' ).input_ids UpperCAmelCase : List[Any] =tokenizer('''Hi I am''' , return_tensors='''np''' ).input_ids UpperCAmelCase : Union[str, Any] =shift_tokens_right(snake_case__ , model.config.pad_token_id , model.config.decoder_start_token_id ) UpperCAmelCase : List[str] =model(snake_case__ , decoder_input_ids=snake_case__ ).logits UpperCAmelCase : Any =optax.softmax_cross_entropy(snake_case__ , onehot(snake_case__ , logits.shape[-1] ) ).mean() UpperCAmelCase : Union[str, Any] =-(labels.shape[-1] * loss.item()) UpperCAmelCase : List[str] =-84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
348
0
import argparse from pathlib import Path from transformers import AutoConfig, AutoTokenizer, RagConfig, RagSequenceForGeneration, RagTokenForGeneration def lowerCAmelCase_ ( __A, __A, __A, __A, __A = None, __A = None, __A = None, ) -> str: '''simple docstring''' if config_name_or_path is None: UpperCAmelCase__ = "facebook/rag-token-base" if model_type == "rag_token" else "facebook/rag-sequence-base" if generator_tokenizer_name_or_path is None: UpperCAmelCase__ = generator_name_or_path if question_encoder_tokenizer_name_or_path is None: UpperCAmelCase__ = question_encoder_name_or_path UpperCAmelCase__ = RagTokenForGeneration if model_type == "rag_token" else RagSequenceForGeneration # Save model. UpperCAmelCase__ = RagConfig.from_pretrained(__A ) UpperCAmelCase__ = AutoConfig.from_pretrained(__A ) UpperCAmelCase__ = AutoConfig.from_pretrained(__A ) UpperCAmelCase__ = gen_config UpperCAmelCase__ = question_encoder_config UpperCAmelCase__ = model_class.from_pretrained_question_encoder_generator( __A, __A, config=__A ) rag_model.save_pretrained(__A ) # Sanity check. model_class.from_pretrained(__A ) # Save tokenizers. UpperCAmelCase__ = AutoTokenizer.from_pretrained(__A ) gen_tokenizer.save_pretrained(dest_dir / "generator_tokenizer/" ) UpperCAmelCase__ = AutoTokenizer.from_pretrained(__A ) question_encoder_tokenizer.save_pretrained(dest_dir / "question_encoder_tokenizer/" ) if __name__ == "__main__": UpperCamelCase__ = argparse.ArgumentParser() parser.add_argument( '--model_type', choices=['rag_sequence', 'rag_token'], required=True, type=str, help='RAG model type: rag_sequence, rag_token', ) parser.add_argument('--dest', type=str, required=True, help='Path to the output checkpoint directory.') parser.add_argument('--generator_name_or_path', type=str, required=True, help='Generator model identifier') parser.add_argument( '--question_encoder_name_or_path', type=str, required=True, help='Question encoder model identifier' ) parser.add_argument( '--generator_tokenizer_name_or_path', type=str, help='Generator tokenizer identifier, if not specified, resolves to ``generator_name_or_path``', ) parser.add_argument( '--question_encoder_tokenizer_name_or_path', type=str, help='Question encoder tokenizer identifier, if not specified, resolves to ``question_encoder_name_or_path``', ) parser.add_argument( '--config_name_or_path', type=str, help=( 'Identifier of the model config to use, if not provided, resolves to a base config for a given' ' ``model_type``' ), ) UpperCamelCase__ = parser.parse_args() UpperCamelCase__ = Path(args.dest) dest_dir.mkdir(exist_ok=True) consolidate( args.model_type, args.generator_name_or_path, args.question_encoder_name_or_path, dest_dir, args.config_name_or_path, args.generator_tokenizer_name_or_path, args.question_encoder_tokenizer_name_or_path, )
65
import unittest import numpy as np from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __snake_case ( lowerCamelCase__ , unittest.TestCase ): # FIXME: add fast tests pass @nightly @require_onnxruntime @require_torch_gpu class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : List[Any] =ort.SessionOptions() UpperCAmelCase : Optional[int] =False return options def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : int =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Optional[Any] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Dict ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : Any =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=10 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : Optional[int] =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Tuple =np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Tuple =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , subfolder='''scheduler''' , revision='''onnx''' ) UpperCAmelCase : int =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , scheduler=snake_case__ , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Union[str, Any] ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : str =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=20 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : int =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Union[str, Any] =np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3
348
0
"""simple docstring""" from __future__ import annotations from collections import Counter from random import random class lowerCamelCase : '''simple docstring''' def __init__( self: Tuple ) -> Optional[Any]: snake_case_ :Optional[int] = {} def lowerCAmelCase_ ( self: Dict , snake_case: str ) -> None: snake_case_ :str = {} def lowerCAmelCase_ ( self: Optional[int] , snake_case: str , snake_case: str , snake_case: float ) -> None: if nodea not in self.connections: self.add_node(snake_case ) if nodea not in self.connections: self.add_node(snake_case ) snake_case_ :Dict = probability def lowerCAmelCase_ ( self: List[Any] ) -> list[str]: return list(self.connections ) def lowerCAmelCase_ ( self: Any , snake_case: str ) -> str: snake_case_ :Optional[Any] = 0 snake_case_ :List[str] = random() for dest in self.connections[node]: current_probability += self.connections[node][dest] if current_probability > random_value: return dest return "" def A_ ( _lowercase, _lowercase, _lowercase ): '''simple docstring''' snake_case_ :List[str] = MarkovChainGraphUndirectedUnweighted() for nodea, nodea, probability in transitions: graph.add_transition_probability(_lowercase, _lowercase, _lowercase ) snake_case_ :int = Counter(graph.get_nodes() ) snake_case_ :Optional[Any] = start for _ in range(_lowercase ): snake_case_ :Tuple = graph.transition(_lowercase ) visited[node] += 1 return visited if __name__ == "__main__": import doctest doctest.testmod()
66
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def lowerCAmelCase_ ( )-> int: '''simple docstring''' UpperCAmelCase : str ={ '''repo_name''': ['''test_repo1''', '''test_repo2''', '''test_repo3'''], '''path''': ['''test_1.py''', '''test_2.py''', '''unit_test.py'''], '''content''': ['''a ''' * 20, '''a ''' * 30, '''b ''' * 7], } UpperCAmelCase : Union[str, Any] =Dataset.from_dict(__lowerCAmelCase ) return dataset class __snake_case ( lowerCamelCase__ ): def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[str] =get_dataset() UpperCAmelCase : Optional[int] =make_duplicate_clusters(snake_case__ , 0.85 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : str =get_dataset() UpperCAmelCase , UpperCAmelCase : Tuple =deduplicate_dataset(snake_case__ ) self.assertEqual(len(snake_case__ ) , 2 ) print(snake_case__ ) self.assertEqual(duplicate_clusters[0][0]['''copies'''] , 2 ) self.assertEqual(duplicate_clusters[0][0]['''is_extreme'''] , snake_case__ )
348
0
'''simple docstring''' from __future__ import annotations import pandas as pd def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> list[int]: __lowerCamelCase = [0] * no_of_processes __lowerCamelCase = [0] * no_of_processes # Copy the burst time into remaining_time[] for i in range(UpperCamelCase__ ): __lowerCamelCase = burst_time[i] __lowerCamelCase = 0 __lowerCamelCase = 0 __lowerCamelCase = 9_99_99_99_99 __lowerCamelCase = 0 __lowerCamelCase = False # Process until all processes are completed while complete != no_of_processes: for j in range(UpperCamelCase__ ): if arrival_time[j] <= increment_time and remaining_time[j] > 0: if remaining_time[j] < minm: __lowerCamelCase = remaining_time[j] __lowerCamelCase = j __lowerCamelCase = True if not check: increment_time += 1 continue remaining_time[short] -= 1 __lowerCamelCase = remaining_time[short] if minm == 0: __lowerCamelCase = 9_99_99_99_99 if remaining_time[short] == 0: complete += 1 __lowerCamelCase = False # Find finish time of current process __lowerCamelCase = increment_time + 1 # Calculate waiting time __lowerCamelCase = finish_time - arrival_time[short] __lowerCamelCase = finar - burst_time[short] if waiting_time[short] < 0: __lowerCamelCase = 0 # Increment time increment_time += 1 return waiting_time def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> list[int]: __lowerCamelCase = [0] * no_of_processes for i in range(UpperCamelCase__ ): __lowerCamelCase = burst_time[i] + waiting_time[i] return turn_around_time def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> None: __lowerCamelCase = 0 __lowerCamelCase = 0 for i in range(UpperCamelCase__ ): __lowerCamelCase = total_waiting_time + waiting_time[i] __lowerCamelCase = total_turn_around_time + turn_around_time[i] print(f"""Average waiting time = {total_waiting_time / no_of_processes:.5f}""" ) print('''Average turn around time =''' , total_turn_around_time / no_of_processes ) if __name__ == "__main__": print("Enter how many process you want to analyze") __UpperCAmelCase =int(input()) __UpperCAmelCase =[0] * no_of_processes __UpperCAmelCase =[0] * no_of_processes __UpperCAmelCase =list(range(1, no_of_processes + 1)) for i in range(no_of_processes): print("Enter the arrival time and burst time for process:--" + str(i + 1)) __UpperCAmelCase , __UpperCAmelCase =map(int, input().split()) __UpperCAmelCase =calculate_waitingtime(arrival_time, burst_time, no_of_processes) __UpperCAmelCase =burst_time __UpperCAmelCase =no_of_processes __UpperCAmelCase =waiting_time __UpperCAmelCase =calculate_turnaroundtime(bt, n, wt) calculate_average_times(waiting_time, turn_around_time, no_of_processes) __UpperCAmelCase =pd.DataFrame( list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)), columns=[ "Process", "BurstTime", "ArrivalTime", "WaitingTime", "TurnAroundTime", ], ) # Printing the dataFrame pd.set_option("display.max_rows", fcfs.shape[0] + 1) print(fcfs)
67
from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput __snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name class __snake_case ( lowerCamelCase__ , lowerCamelCase__ ): @register_to_config def __init__( self , snake_case__ , snake_case__ = None , snake_case__ = None ) -> str: '''simple docstring''' super().__init__() UpperCAmelCase : Optional[Any] =learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" UpperCAmelCase : Any =torch.zeros(snake_case__ , snake_case__ ) else: UpperCAmelCase : Union[str, Any] =None UpperCAmelCase : Optional[int] =torch.nn.Parameter(snake_case__ ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : VQModel __lowerCamelCase : CLIPTextModel __lowerCamelCase : CLIPTokenizer __lowerCamelCase : TransformeraDModel __lowerCamelCase : LearnedClassifierFreeSamplingEmbeddings __lowerCamelCase : VQDiffusionScheduler def __init__( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> int: '''simple docstring''' super().__init__() self.register_modules( vqvae=snake_case__ , transformer=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , scheduler=snake_case__ , learned_classifier_free_sampling_embeddings=snake_case__ , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : int =len(snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else 1 # get prompt text embeddings UpperCAmelCase : Optional[int] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) UpperCAmelCase : int =text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCAmelCase : List[str] =self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' f''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) UpperCAmelCase : Optional[Any] =text_input_ids[:, : self.tokenizer.model_max_length] UpperCAmelCase : List[Any] =self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 UpperCAmelCase : int =prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate text embeddings for each generation per prompt UpperCAmelCase : int =prompt_embeds.repeat_interleave(snake_case__ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: UpperCAmelCase : Optional[int] =self.learned_classifier_free_sampling_embeddings.embeddings UpperCAmelCase : str =negative_prompt_embeds.unsqueeze(0 ).repeat(snake_case__ , 1 , 1 ) else: UpperCAmelCase : str =[''''''] * batch_size UpperCAmelCase : Tuple =text_input_ids.shape[-1] UpperCAmelCase : Optional[Any] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=snake_case__ , truncation=snake_case__ , return_tensors='''pt''' , ) UpperCAmelCase : Optional[Any] =self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings UpperCAmelCase : Optional[int] =negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCAmelCase : Optional[Any] =negative_prompt_embeds.shape[1] UpperCAmelCase : Union[str, Any] =negative_prompt_embeds.repeat(1 , snake_case__ , 1 ) UpperCAmelCase : Optional[Any] =negative_prompt_embeds.view(batch_size * num_images_per_prompt , snake_case__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCAmelCase : int =torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , snake_case__ , snake_case__ = 100 , snake_case__ = 5.0 , snake_case__ = 1.0 , snake_case__ = 1 , snake_case__ = None , snake_case__ = None , snake_case__ = "pil" , snake_case__ = True , snake_case__ = None , snake_case__ = 1 , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =1 elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Tuple =len(snake_case__ ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(snake_case__ )}''' ) UpperCAmelCase : Tuple =batch_size * num_images_per_prompt UpperCAmelCase : List[str] =guidance_scale > 1.0 UpperCAmelCase : List[Any] =self._encode_prompt(snake_case__ , snake_case__ , snake_case__ ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case__ , snake_case__ ) or callback_steps <= 0) ): raise ValueError( f'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' f''' {type(snake_case__ )}.''' ) # get the initial completely masked latents unless the user supplied it UpperCAmelCase : int =(batch_size, self.transformer.num_latent_pixels) if latents is None: UpperCAmelCase : Union[str, Any] =self.transformer.num_vector_embeds - 1 UpperCAmelCase : str =torch.full(snake_case__ , snake_case__ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( '''Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,''' f''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) UpperCAmelCase : Any =latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(snake_case__ , device=self.device ) UpperCAmelCase : Any =self.scheduler.timesteps.to(self.device ) UpperCAmelCase : Optional[int] =latents for i, t in enumerate(self.progress_bar(snake_case__ ) ): # expand the sample if we are doing classifier free guidance UpperCAmelCase : Optional[Any] =torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` UpperCAmelCase : Optional[int] =self.transformer(snake_case__ , encoder_hidden_states=snake_case__ , timestep=snake_case__ ).sample if do_classifier_free_guidance: UpperCAmelCase , UpperCAmelCase : str =model_output.chunk(2 ) UpperCAmelCase : Optional[int] =model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(snake_case__ , dim=1 , keepdim=snake_case__ ) UpperCAmelCase : Tuple =self.truncate(snake_case__ , snake_case__ ) # remove `log(0)`'s (`-inf`s) UpperCAmelCase : Optional[Any] =model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 UpperCAmelCase : int =self.scheduler.step(snake_case__ , timestep=snake_case__ , sample=snake_case__ , generator=snake_case__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =self.vqvae.config.vq_embed_dim UpperCAmelCase : Optional[Any] =(batch_size, self.transformer.height, self.transformer.width, embedding_channels) UpperCAmelCase : Dict =self.vqvae.quantize.get_codebook_entry(snake_case__ , shape=snake_case__ ) UpperCAmelCase : Tuple =self.vqvae.decode(snake_case__ , force_not_quantize=snake_case__ ).sample UpperCAmelCase : Union[str, Any] =(image / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase : Any =image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase : List[str] =self.numpy_to_pil(snake_case__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> torch.FloatTensor: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : int =torch.sort(snake_case__ , 1 , descending=snake_case__ ) UpperCAmelCase : Union[str, Any] =torch.exp(snake_case__ ) UpperCAmelCase : Union[str, Any] =sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out UpperCAmelCase : Optional[Any] =torch.full_like(keep_mask[:, 0:1, :] , snake_case__ ) UpperCAmelCase : Tuple =torch.cat((all_true, keep_mask) , dim=1 ) UpperCAmelCase : int =keep_mask[:, :-1, :] UpperCAmelCase : int =keep_mask.gather(1 , indices.argsort(1 ) ) UpperCAmelCase : Dict =log_p_x_0.clone() UpperCAmelCase : List[Any] =-torch.inf # -inf = log(0) return rv
348
0
import logging import torch from accelerate import Accelerator from arguments import EvaluationArguments from datasets import load_dataset from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, set_seed class a__ ( snake_case ): """simple docstring""" def __init__( self , lowercase , lowercase , lowercase=1024 , lowercase=1024 , lowercase=3.6 ) -> Tuple: '''simple docstring''' A__ = tokenizer A__ = tokenizer.bos_token_id A__ = dataset A__ = seq_length A__ = seq_length * chars_per_token * num_of_sequences def __iter__( self ) -> Tuple: '''simple docstring''' A__ = iter(self.dataset ) A__ = True while more_examples: A__ , A__ = [], 0 while True: if buffer_len >= self.input_characters: break try: buffer.append(next(lowercase )["content"] ) buffer_len += len(buffer[-1] ) except StopIteration: A__ = False break A__ = tokenizer(lowercase , truncation=lowercase )["input_ids"] A__ = [] for tokenized_input in tokenized_inputs: all_token_ids.extend(tokenized_input + [self.concat_token_id] ) for i in range(0 , len(lowercase ) , self.seq_length ): A__ = all_token_ids[i : i + self.seq_length] if len(lowercase ) == self.seq_length: yield torch.tensor(lowercase ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: List[str] ) -> List[str]: '''simple docstring''' A__ = {"streaming": True} A__ = load_dataset(args.dataset_name , split="train" , **SCREAMING_SNAKE_CASE_ ) A__ = ConstantLengthDataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , seq_length=args.seq_length ) A__ = DataLoader(SCREAMING_SNAKE_CASE_ , batch_size=args.batch_size ) return eval_dataloader def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: Optional[Any] ) -> int: '''simple docstring''' model.eval() A__ = [] for step, batch in enumerate(SCREAMING_SNAKE_CASE_ ): with torch.no_grad(): A__ = model(SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ ) A__ = outputs.loss.repeat(args.batch_size ) losses.append(accelerator.gather(SCREAMING_SNAKE_CASE_ ) ) if args.max_eval_steps > 0 and step >= args.max_eval_steps: break A__ = torch.mean(torch.cat(SCREAMING_SNAKE_CASE_ ) ) try: A__ = torch.exp(SCREAMING_SNAKE_CASE_ ) except OverflowError: A__ = float("inf" ) return loss.item(), perplexity.item() # Setup Accelerator lowerCAmelCase__ = Accelerator() # Parse configuration lowerCAmelCase__ = HfArgumentParser(EvaluationArguments) lowerCAmelCase__ = parser.parse_args() set_seed(args.seed) # Logging lowerCAmelCase__ = logging.getLogger(__name__) logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""", datefmt="""%m/%d/%Y %H:%M:%S""", level=logging.INFO ) # Load model and tokenizer lowerCAmelCase__ = AutoModelForCausalLM.from_pretrained(args.model_ckpt) lowerCAmelCase__ = AutoTokenizer.from_pretrained(args.model_ckpt) # Load dataset and dataloader lowerCAmelCase__ = create_dataloader(args) # Prepare everything with our `accelerator`. lowerCAmelCase__ , lowerCAmelCase__ = accelerator.prepare(model, eval_dataloader) # Evaluate and save the last checkpoint logger.info("""Evaluating and saving model after training""") lowerCAmelCase__ , lowerCAmelCase__ = evaluate(args) logger.info(f"""loss/eval: {eval_loss}, perplexity: {perplexity}""")
68
import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase : Any =UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =self.dummy_uncond_unet UpperCAmelCase : Optional[int] =KarrasVeScheduler() UpperCAmelCase : List[Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : List[str] =torch.manual_seed(0 ) UpperCAmelCase : List[str] =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : str =torch.manual_seed(0 ) UpperCAmelCase : str =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' , return_dict=snake_case__ )[0] UpperCAmelCase : Any =image[0, -3:, -3:, -1] UpperCAmelCase : List[str] =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCAmelCase : int =np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class __snake_case ( unittest.TestCase ): def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Tuple ='''google/ncsnpp-celebahq-256''' UpperCAmelCase : int =UNetaDModel.from_pretrained(snake_case__ ) UpperCAmelCase : Dict =KarrasVeScheduler() UpperCAmelCase : Union[str, Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Any =torch.manual_seed(0 ) UpperCAmelCase : Tuple =pipe(num_inference_steps=20 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : Optional[int] =image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) UpperCAmelCase : Tuple =np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
348
0
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import _LazyModule __UpperCamelCase = {'''tokenization_tapex''': ['''TapexTokenizer''']} if TYPE_CHECKING: from .tokenization_tapex import TapexTokenizer else: import sys __UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
69
import qiskit def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> qiskit.result.counts.Counts: '''simple docstring''' UpperCAmelCase : Union[str, Any] =qiskit.Aer.get_backend('''aer_simulator''' ) UpperCAmelCase : List[str] =qiskit.QuantumCircuit(4 , 2 ) # encode inputs in qubits 0 and 1 if bita == 1: qc_ha.x(0 ) if bita == 1: qc_ha.x(1 ) qc_ha.barrier() # use cnots to write XOR of the inputs on qubit2 qc_ha.cx(0 , 2 ) qc_ha.cx(1 , 2 ) # use ccx / toffoli gate to write AND of the inputs on qubit3 qc_ha.ccx(0 , 1 , 3 ) qc_ha.barrier() # extract outputs qc_ha.measure(2 , 0 ) # extract XOR value qc_ha.measure(3 , 1 ) # extract AND value # Execute the circuit on the qasm simulator UpperCAmelCase : Dict =qiskit.execute(__lowerCAmelCase , __lowerCAmelCase , shots=10_00 ) # Return the histogram data of the results of the experiment return job.result().get_counts(__lowerCAmelCase ) if __name__ == "__main__": __snake_case = half_adder(1, 1) print(f'Half Adder Output Qubit Counts: {counts}')
348
0
'''simple docstring''' from __future__ import annotations from typing import Generic, TypeVar A__ : List[str] =TypeVar('''T''') class UpperCAmelCase ( Generic[T] ): def __init__( self : List[str] , __snake_case : T ) -> None: _lowerCAmelCase = data _lowerCAmelCase = self _lowerCAmelCase = 0 class UpperCAmelCase ( Generic[T] ): def __init__( self : List[Any] ) -> None: # map from node name to the node object _lowerCAmelCase = {} def lowercase__ ( self : Any , __snake_case : T ) -> None: # create a new set with x as its member _lowerCAmelCase = DisjointSetTreeNode(__snake_case ) def lowercase__ ( self : int , __snake_case : T ) -> DisjointSetTreeNode[T]: # find the set x belongs to (with path-compression) _lowerCAmelCase = self.map[data] if elem_ref != elem_ref.parent: _lowerCAmelCase = self.find_set(elem_ref.parent.data ) return elem_ref.parent def lowercase__ ( self : List[Any] , __snake_case : DisjointSetTreeNode[T] , __snake_case : DisjointSetTreeNode[T] ) -> None: # helper function for union operation if nodea.rank > nodea.rank: _lowerCAmelCase = nodea else: _lowerCAmelCase = nodea if nodea.rank == nodea.rank: nodea.rank += 1 def lowercase__ ( self : List[Any] , __snake_case : T , __snake_case : T ) -> None: # merge 2 disjoint sets self.link(self.find_set(__snake_case ) , self.find_set(__snake_case ) ) class UpperCAmelCase ( Generic[T] ): def __init__( self : Tuple ) -> None: # connections: map from the node to the neighbouring nodes (with weights) _lowerCAmelCase = {} def lowercase__ ( self : Any , __snake_case : T ) -> None: # add a node ONLY if its not present in the graph if node not in self.connections: _lowerCAmelCase = {} def lowercase__ ( self : List[Any] , __snake_case : T , __snake_case : T , __snake_case : int ) -> None: # add an edge with the given weight self.add_node(__snake_case ) self.add_node(__snake_case ) _lowerCAmelCase = weight _lowerCAmelCase = weight def lowercase__ ( self : List[Any] ) -> GraphUndirectedWeighted[T]: _lowerCAmelCase = [] _lowerCAmelCase = set() for start in self.connections: for end in self.connections[start]: if (start, end) not in seen: seen.add((end, start) ) edges.append((start, end, self.connections[start][end]) ) edges.sort(key=lambda __snake_case : x[2] ) # creating the disjoint set _lowerCAmelCase = DisjointSetTree[T]() for node in self.connections: disjoint_set.make_set(__snake_case ) # MST generation _lowerCAmelCase = 0 _lowerCAmelCase = 0 _lowerCAmelCase = GraphUndirectedWeighted[T]() while num_edges < len(self.connections ) - 1: _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase = edges[index] index += 1 _lowerCAmelCase = disjoint_set.find_set(__snake_case ) _lowerCAmelCase = disjoint_set.find_set(__snake_case ) if parent_u != parent_v: num_edges += 1 graph.add_edge(__snake_case , __snake_case , __snake_case ) disjoint_set.union(__snake_case , __snake_case ) return graph
70
from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class __snake_case : __lowerCamelCase : str = BlenderbotConfig __lowerCamelCase : Optional[Any] = {} __lowerCamelCase : Optional[int] = """gelu""" def __init__( self , snake_case__ , snake_case__=13 , snake_case__=7 , snake_case__=True , snake_case__=False , snake_case__=99 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=20 , snake_case__=2 , snake_case__=1 , snake_case__=0 , ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : Dict =seq_length UpperCAmelCase : Optional[Any] =is_training UpperCAmelCase : List[str] =use_labels UpperCAmelCase : List[Any] =vocab_size UpperCAmelCase : Optional[int] =hidden_size UpperCAmelCase : Tuple =num_hidden_layers UpperCAmelCase : Any =num_attention_heads UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : str =hidden_dropout_prob UpperCAmelCase : Optional[int] =attention_probs_dropout_prob UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : List[Any] =eos_token_id UpperCAmelCase : Optional[int] =pad_token_id UpperCAmelCase : Tuple =bos_token_id def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[Any] =ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) UpperCAmelCase : List[Any] =tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) UpperCAmelCase : Tuple =tf.concat([input_ids, eos_tensor] , axis=1 ) UpperCAmelCase : str =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : Optional[Any] =self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) UpperCAmelCase : List[str] =prepare_blenderbot_inputs_dict(snake_case__ , snake_case__ , snake_case__ ) return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =TFBlenderbotModel(config=snake_case__ ).get_decoder() UpperCAmelCase : Any =inputs_dict['''input_ids'''] UpperCAmelCase : str =input_ids[:1, :] UpperCAmelCase : Tuple =inputs_dict['''attention_mask'''][:1, :] UpperCAmelCase : Tuple =inputs_dict['''head_mask'''] UpperCAmelCase : List[Any] =1 # first forward pass UpperCAmelCase : List[str] =model(snake_case__ , attention_mask=snake_case__ , head_mask=snake_case__ , use_cache=snake_case__ ) UpperCAmelCase , UpperCAmelCase : str =outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase : Union[str, Any] =ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCAmelCase : List[Any] =tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and UpperCAmelCase : Tuple =tf.concat([input_ids, next_tokens] , axis=-1 ) UpperCAmelCase : int =tf.concat([attention_mask, next_attn_mask] , axis=-1 ) UpperCAmelCase : Optional[int] =model(snake_case__ , attention_mask=snake_case__ )[0] UpperCAmelCase : str =model(snake_case__ , attention_mask=snake_case__ , past_key_values=snake_case__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice UpperCAmelCase : List[Any] =int(ids_tensor((1,) , output_from_past.shape[-1] ) ) UpperCAmelCase : List[Any] =output_from_no_past[:, -3:, random_slice_idx] UpperCAmelCase : Dict =output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(snake_case__ , snake_case__ , rtol=1e-3 ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , )-> str: '''simple docstring''' if attention_mask is None: UpperCAmelCase : int =tf.cast(tf.math.not_equal(__lowerCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase : Tuple =tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase : str =tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase : Union[str, Any] =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase : int =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : List[str] = (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __lowerCamelCase : Dict = (TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __lowerCamelCase : Dict = ( { """conversational""": TFBlenderbotForConditionalGeneration, """feature-extraction""": TFBlenderbotModel, """summarization""": TFBlenderbotForConditionalGeneration, """text2text-generation""": TFBlenderbotForConditionalGeneration, """translation""": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __lowerCamelCase : Union[str, Any] = True __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Union[str, Any] = False def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : List[str] =TFBlenderbotModelTester(self ) UpperCAmelCase : List[Any] =ConfigTester(self , config_class=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*snake_case__ ) @require_tokenizers @require_tf class __snake_case ( unittest.TestCase ): __lowerCamelCase : List[str] = ["""My friends are cool but they eat too many carbs."""] __lowerCamelCase : Dict = """facebook/blenderbot-400M-distill""" @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[int] =self.tokenizer(self.src_text , return_tensors='''tf''' ) UpperCAmelCase : Optional[int] =self.model.generate( model_inputs.input_ids , ) UpperCAmelCase : str =self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=snake_case__ )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
348
0
import unittest from parameterized import parameterized from transformers import LlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer class __A : """simple docstring""" def __init__( self , lowerCamelCase__ , lowerCamelCase__=13 , lowerCamelCase__=7 , lowerCamelCase__=True , lowerCamelCase__=True , lowerCamelCase__=False , lowerCamelCase__=True , lowerCamelCase__=99 , lowerCamelCase__=32 , lowerCamelCase__=5 , lowerCamelCase__=4 , lowerCamelCase__=37 , lowerCamelCase__="gelu" , lowerCamelCase__=0.1 , lowerCamelCase__=0.1 , lowerCamelCase__=512 , lowerCamelCase__=16 , lowerCamelCase__=2 , lowerCamelCase__=0.02 , lowerCamelCase__=3 , lowerCamelCase__=4 , lowerCamelCase__=None , ): """simple docstring""" __UpperCamelCase : str =parent __UpperCamelCase : Dict =batch_size __UpperCamelCase : Union[str, Any] =seq_length __UpperCamelCase : List[str] =is_training __UpperCamelCase : int =use_input_mask __UpperCamelCase : Tuple =use_token_type_ids __UpperCamelCase : Optional[int] =use_labels __UpperCamelCase : Union[str, Any] =vocab_size __UpperCamelCase : int =hidden_size __UpperCamelCase : int =num_hidden_layers __UpperCamelCase : Union[str, Any] =num_attention_heads __UpperCamelCase : Dict =intermediate_size __UpperCamelCase : Any =hidden_act __UpperCamelCase : int =hidden_dropout_prob __UpperCamelCase : Dict =attention_probs_dropout_prob __UpperCamelCase : Optional[Any] =max_position_embeddings __UpperCamelCase : List[Any] =type_vocab_size __UpperCamelCase : Union[str, Any] =type_sequence_label_size __UpperCamelCase : int =initializer_range __UpperCamelCase : List[str] =num_labels __UpperCamelCase : Optional[int] =num_choices __UpperCamelCase : List[str] =scope def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Dict =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __UpperCamelCase : List[str] =None if self.use_input_mask: __UpperCamelCase : Tuple =random_attention_mask([self.batch_size, self.seq_length] ) __UpperCamelCase : List[str] =None if self.use_token_type_ids: __UpperCamelCase : Dict =ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __UpperCamelCase : List[Any] =None __UpperCamelCase : Optional[Any] =None __UpperCamelCase : List[str] =None if self.use_labels: __UpperCamelCase : Optional[int] =ids_tensor([self.batch_size] , self.type_sequence_label_size ) __UpperCamelCase : Optional[int] =ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __UpperCamelCase : Optional[Any] =ids_tensor([self.batch_size] , self.num_choices ) __UpperCamelCase : str =self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __lowercase ( self ): """simple docstring""" return LlamaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowerCamelCase__ , initializer_range=self.initializer_range , ) def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : Optional[int] =LlamaModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() __UpperCamelCase : List[str] =model(lowerCamelCase__ , attention_mask=lowerCamelCase__ ) __UpperCamelCase : Dict =model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , ): """simple docstring""" __UpperCamelCase : List[Any] =True __UpperCamelCase : Tuple =LlamaModel(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() __UpperCamelCase : str =model( lowerCamelCase__ , attention_mask=lowerCamelCase__ , encoder_hidden_states=lowerCamelCase__ , encoder_attention_mask=lowerCamelCase__ , ) __UpperCamelCase : str =model( lowerCamelCase__ , attention_mask=lowerCamelCase__ , encoder_hidden_states=lowerCamelCase__ , ) __UpperCamelCase : Union[str, Any] =model(lowerCamelCase__ , attention_mask=lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , ): """simple docstring""" __UpperCamelCase : List[str] =LlamaForCausalLM(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() __UpperCamelCase : Any =model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , ): """simple docstring""" __UpperCamelCase : str =True __UpperCamelCase : str =True __UpperCamelCase : Optional[int] =LlamaForCausalLM(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() # first forward pass __UpperCamelCase : Any =model( lowerCamelCase__ , attention_mask=lowerCamelCase__ , encoder_hidden_states=lowerCamelCase__ , encoder_attention_mask=lowerCamelCase__ , use_cache=lowerCamelCase__ , ) __UpperCamelCase : Dict =outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids __UpperCamelCase : Optional[Any] =ids_tensor((self.batch_size, 3) , config.vocab_size ) __UpperCamelCase : Optional[Any] =ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and __UpperCamelCase : str =torch.cat([input_ids, next_tokens] , dim=-1 ) __UpperCamelCase : Optional[Any] =torch.cat([input_mask, next_mask] , dim=-1 ) __UpperCamelCase : List[str] =model( lowerCamelCase__ , attention_mask=lowerCamelCase__ , encoder_hidden_states=lowerCamelCase__ , encoder_attention_mask=lowerCamelCase__ , output_hidden_states=lowerCamelCase__ , )['hidden_states'][0] __UpperCamelCase : Optional[int] =model( lowerCamelCase__ , attention_mask=lowerCamelCase__ , encoder_hidden_states=lowerCamelCase__ , encoder_attention_mask=lowerCamelCase__ , past_key_values=lowerCamelCase__ , output_hidden_states=lowerCamelCase__ , )['hidden_states'][0] # select random slice __UpperCamelCase : Optional[int] =ids_tensor((1,) , output_from_past.shape[-1] ).item() __UpperCamelCase : List[Any] =output_from_no_past[:, -3:, random_slice_idx].detach() __UpperCamelCase : List[str] =output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(lowerCamelCase__ , lowerCamelCase__ , atol=1E-3 ) ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[int] =self.prepare_config_and_inputs() ( ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ) : Optional[Any] =config_and_inputs __UpperCamelCase : str ={'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class __A ( a , a , a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : Optional[Any] =(LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else () UpperCamelCase__ : int =(LlamaForCausalLM,) if is_torch_available() else () UpperCamelCase__ : str =( { """feature-extraction""": LlamaModel, """text-classification""": LlamaForSequenceClassification, """text-generation""": LlamaForCausalLM, """zero-shot""": LlamaForSequenceClassification, } if is_torch_available() else {} ) UpperCamelCase__ : Tuple =False UpperCamelCase__ : Any =False def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[int] =LlamaModelTester(self ) __UpperCamelCase : str =ConfigTester(self , config_class=lowerCamelCase__ , hidden_size=37 ) def __lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[Any] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[int] =self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: __UpperCamelCase : str =type self.model_tester.create_and_check_model(*lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase , __UpperCamelCase : Union[str, Any] =self.model_tester.prepare_config_and_inputs_for_common() __UpperCamelCase : Optional[Any] =3 __UpperCamelCase : List[str] =input_dict['input_ids'] __UpperCamelCase : Tuple =input_ids.ne(1 ).to(lowerCamelCase__ ) __UpperCamelCase : List[str] =ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) __UpperCamelCase : Optional[int] =LlamaForSequenceClassification(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() __UpperCamelCase : Optional[Any] =model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , labels=lowerCamelCase__ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase , __UpperCamelCase : Any =self.model_tester.prepare_config_and_inputs_for_common() __UpperCamelCase : Optional[Any] =3 __UpperCamelCase : int ='single_label_classification' __UpperCamelCase : List[Any] =input_dict['input_ids'] __UpperCamelCase : int =input_ids.ne(1 ).to(lowerCamelCase__ ) __UpperCamelCase : str =ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) __UpperCamelCase : List[Any] =LlamaForSequenceClassification(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() __UpperCamelCase : Optional[Any] =model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , labels=lowerCamelCase__ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase , __UpperCamelCase : Dict =self.model_tester.prepare_config_and_inputs_for_common() __UpperCamelCase : List[str] =3 __UpperCamelCase : Optional[Any] ='multi_label_classification' __UpperCamelCase : Optional[int] =input_dict['input_ids'] __UpperCamelCase : int =input_ids.ne(1 ).to(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) __UpperCamelCase : Any =LlamaForSequenceClassification(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() __UpperCamelCase : Any =model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , labels=lowerCamelCase__ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('LLaMA buffers include complex numbers, which breaks this test' ) def __lowercase ( self ): """simple docstring""" pass @parameterized.expand([('linear',), ('dynamic',)] ) def __lowercase ( self , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase , __UpperCamelCase : Optional[Any] =self.model_tester.prepare_config_and_inputs_for_common() __UpperCamelCase : Any =ids_tensor([1, 10] , config.vocab_size ) __UpperCamelCase : Tuple =ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights __UpperCamelCase : Dict =LlamaModel(lowerCamelCase__ ) original_model.to(lowerCamelCase__ ) original_model.eval() __UpperCamelCase : Any =original_model(lowerCamelCase__ ).last_hidden_state __UpperCamelCase : Any =original_model(lowerCamelCase__ ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights __UpperCamelCase : Tuple ={'type': scaling_type, 'factor': 10.0} __UpperCamelCase : List[str] =LlamaModel(lowerCamelCase__ ) scaled_model.to(lowerCamelCase__ ) scaled_model.eval() __UpperCamelCase : Optional[Any] =scaled_model(lowerCamelCase__ ).last_hidden_state __UpperCamelCase : Union[str, Any] =scaled_model(lowerCamelCase__ ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(lowerCamelCase__ , lowerCamelCase__ , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(lowerCamelCase__ , lowerCamelCase__ , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(lowerCamelCase__ , lowerCamelCase__ , atol=1E-5 ) ) @require_torch class __A ( unittest.TestCase ): """simple docstring""" @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =[1, 306, 4658, 278, 6593, 310, 2834, 338] __UpperCamelCase : int =LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-7b-hf' , device_map='auto' ) __UpperCamelCase : Optional[Any] =model(torch.tensor([input_ids] ) ) # Expected mean on dim = -1 __UpperCamelCase : Optional[Any] =torch.tensor([[-6.6_550, -4.1_227, -4.9_859, -3.2_406, 0.8_262, -3.0_033, 1.2_964, -3.3_699]] ) torch.testing.assert_close(out.mean(-1 ) , lowerCamelCase__ , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off __UpperCamelCase : Tuple =torch.tensor([-12.8_281, -7.4_453, -0.4_639, -8.0_625, -7.2_500, -8.0_000, -6.4_883, -7.7_695, -7.8_438, -7.0_312, -6.2_188, -7.1_328, -1.8_496, 1.9_961, -8.6_250, -6.7_227, -12.8_281, -6.9_492, -7.0_742, -7.7_852, -7.5_820, -7.9_062, -6.9_375, -7.9_805, -8.3_438, -8.1_562, -8.0_469, -7.6_250, -7.7_422, -7.3_398,] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , lowerCamelCase__ , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =[1, 306, 4658, 278, 6593, 310, 2834, 338] __UpperCamelCase : Dict =LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-hf' , device_map='auto' ) __UpperCamelCase : List[str] =model(torch.tensor(lowerCamelCase__ ) ) # Expected mean on dim = -1 __UpperCamelCase : Optional[Any] =torch.tensor([[-2.0_622, -1.2_794, -1.1_638, -0.9_788, -1.4_603, -1.0_238, -1.7_893, -1.4_411]] ) torch.testing.assert_close(out.mean(-1 ) , lowerCamelCase__ , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off __UpperCamelCase : Any =torch.tensor([-8.1_406, -8.0_547, 2.7_461, -1.2_344, -0.1_448, -1.8_262, -1.0_020, -1.8_154, -1.6_895, -1.8_516, -2.3_574, -0.9_277, 3.7_598, 6.5_742, -1.2_998, -0.1_177, -8.1_406, -2.9_688, -2.9_199, -3.1_699, -3.5_254, -2.3_555, -2.7_988, -3.4_141, -2.8_262, -4.5_195, -3.3_379, -3.3_164, -2.7_832, -3.0_273] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , lowerCamelCase__ , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Union[str, Any] =[1, 306, 4658, 278, 6593, 310, 2834, 338] __UpperCamelCase : Optional[Any] =LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-chat-hf' , device_map='auto' ) __UpperCamelCase : Optional[Any] =model(torch.tensor(lowerCamelCase__ ) ) # Expected mean on dim = -1 __UpperCamelCase : Dict =torch.tensor([[-0.8_562, -1.8_520, -0.7_551, -0.4_162, -1.5_161, -1.2_038, -2.4_823, -2.3_254]] ) torch.testing.assert_close(out.mean(-1 ) , lowerCamelCase__ , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off __UpperCamelCase : Union[str, Any] =torch.tensor([-2.2_227, 4.8_828, 0.9_023, -0.4_578, -0.7_871, -0.1_033, -0.6_221, -0.5_786, -0.7_803, -1.0_674, -1.2_920, -0.1_570, 0.8_008, 2.0_723, -0.9_497, 0.2_771, -2.2_227, -0.7_612, -1.4_346, -1.2_061, -1.6_426, -0.3_000, -0.7_139, -1.1_934, -1.8_691, -1.6_973, -1.5_947, -1.2_705, -0.3_523, -0.5_513] ) # fmt: on torch.testing.assert_close(out.mean(-1 ) , lowerCamelCase__ , atol=1E-2 , rtol=1E-2 ) @unittest.skip( 'Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test' ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[Any] =[1, 306, 4658, 278, 6593, 310, 2834, 338] __UpperCamelCase : str =LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-70b-hf' , device_map='auto' ) __UpperCamelCase : str =model(torch.tensor(lowerCamelCase__ ) ) __UpperCamelCase : Optional[Any] =torch.tensor( [[-4.2_327, -3.3_360, -4.6_665, -4.7_631, -1.8_180, -3.4_170, -1.4_211, -3.1_810]] , dtype=torch.floataa ) torch.testing.assert_close(out.mean(-1 ) , lowerCamelCase__ , atol=1E-2 , rtol=1E-2 ) # fmt: off __UpperCamelCase : Optional[Any] =torch.tensor([-9.4_922, -3.9_551, 1.7_998, -5.6_758, -5.1_055, -5.8_984, -4.8_320, -6.8_086, -6.5_391, -5.6_172, -5.5_820, -5.5_352, 1.7_881, 3.6_289, -6.5_117, -3.4_785, -9.5_000, -6.0_352, -6.8_125, -6.0_195, -6.6_836, -5.4_727, -6.2_812, -6.0_391, -7.3_398, -7.4_297, -7.4_844, -6.5_820, -5.8_789, -5.5_312] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , lowerCamelCase__ , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Model is curently gated' ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Any ='Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the "princi' __UpperCamelCase : List[Any] ='Simply put, the theory of relativity states that ' __UpperCamelCase : Optional[Any] =LlamaTokenizer.from_pretrained('meta-llama/Llama-2-13b-chat-hf' ) __UpperCamelCase : int =tokenizer.encode(lowerCamelCase__ , return_tensors='pt' ) __UpperCamelCase : Any =LlamaForCausalLM.from_pretrained( 'meta-llama/Llama-2-13b-chat-hf' , device_map='sequential' , use_safetensors=lowerCamelCase__ ) # greedy generation outputs __UpperCamelCase : Dict =model.generate(lowerCamelCase__ , max_new_tokens=64 , top_p=lowerCamelCase__ , temperature=1 , do_sample=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =tokenizer.decode(generated_ids[0] , skip_special_tokens=lowerCamelCase__ ) self.assertEqual(lowerCamelCase__ , lowerCamelCase__ )
71
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { '''asapp/sew-d-tiny-100k''': '''https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json''', # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[Any] = """sew-d""" def __init__( self , snake_case__=32 , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=2 , snake_case__=512 , snake_case__=256 , snake_case__=True , snake_case__=True , snake_case__=("p2c", "c2p") , snake_case__="layer_norm" , snake_case__="gelu_python" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.0 , snake_case__=0.1 , snake_case__=0.02 , snake_case__=1e-7 , snake_case__=1e-5 , snake_case__="group" , snake_case__="gelu" , snake_case__=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , snake_case__=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , snake_case__=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , snake_case__=False , snake_case__=128 , snake_case__=16 , snake_case__=True , snake_case__=0.05 , snake_case__=10 , snake_case__=2 , snake_case__=0.0 , snake_case__=10 , snake_case__=0 , snake_case__="mean" , snake_case__=False , snake_case__=False , snake_case__=256 , snake_case__=0 , snake_case__=1 , snake_case__=2 , **snake_case__ , ) -> int: '''simple docstring''' super().__init__(**snake_case__ , pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ ) UpperCAmelCase : Union[str, Any] =hidden_size UpperCAmelCase : Union[str, Any] =feat_extract_norm UpperCAmelCase : Optional[Any] =feat_extract_activation UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : int =list(snake_case__ ) UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : str =conv_bias UpperCAmelCase : Tuple =num_conv_pos_embeddings UpperCAmelCase : Dict =num_conv_pos_embedding_groups UpperCAmelCase : str =len(self.conv_dim ) UpperCAmelCase : Dict =num_hidden_layers UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : List[Any] =squeeze_factor UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : int =position_buckets UpperCAmelCase : Optional[int] =share_att_key UpperCAmelCase : Optional[int] =relative_attention UpperCAmelCase : Tuple =norm_rel_ebd UpperCAmelCase : List[Any] =list(snake_case__ ) UpperCAmelCase : Dict =hidden_act UpperCAmelCase : Optional[int] =num_attention_heads UpperCAmelCase : Any =hidden_dropout UpperCAmelCase : str =attention_dropout UpperCAmelCase : Union[str, Any] =activation_dropout UpperCAmelCase : str =feat_proj_dropout UpperCAmelCase : Union[str, Any] =final_dropout UpperCAmelCase : Optional[int] =layer_norm_eps UpperCAmelCase : str =feature_layer_norm_eps UpperCAmelCase : str =initializer_range UpperCAmelCase : Any =vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect.''' '''It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,''' f'''but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)''' f'''= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCAmelCase : Union[str, Any] =apply_spec_augment UpperCAmelCase : Optional[Any] =mask_time_prob UpperCAmelCase : Tuple =mask_time_length UpperCAmelCase : str =mask_time_min_masks UpperCAmelCase : Optional[int] =mask_feature_prob UpperCAmelCase : Optional[Any] =mask_feature_length UpperCAmelCase : List[Any] =mask_feature_min_masks # ctc loss UpperCAmelCase : str =ctc_loss_reduction UpperCAmelCase : Optional[int] =ctc_zero_infinity # sequence classification UpperCAmelCase : Union[str, Any] =use_weighted_layer_sum UpperCAmelCase : int =classifier_proj_size @property def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
348
0
"""simple docstring""" from importlib import import_module from .logging import get_logger lowerCAmelCase__ = get_logger(__name__) class __snake_case : def __init__( self : int , __lowerCAmelCase : List[str] , __lowerCAmelCase : List[Any]=None ): """simple docstring""" _lowerCamelCase : Tuple = attrs or [] if module is not None: for key in module.__dict__: if key in attrs or not key.startswith('''__''' ): setattr(self , __lowerCAmelCase , getattr(__lowerCAmelCase , __lowerCAmelCase ) ) _lowerCamelCase : Any = module._original_module if isinstance(__lowerCAmelCase , _PatchedModuleObj ) else module class __snake_case : snake_case__ : Dict = [] def __init__( self : Union[str, Any] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : str , __lowerCAmelCase : Dict , __lowerCAmelCase : List[str]=None ): """simple docstring""" _lowerCamelCase : Optional[Any] = obj _lowerCamelCase : int = target _lowerCamelCase : int = new _lowerCamelCase : Tuple = target.split('''.''' )[0] _lowerCamelCase : Optional[Any] = {} _lowerCamelCase : Optional[int] = attrs or [] def __enter__( self : Optional[Any] ): """simple docstring""" *_lowerCamelCase , _lowerCamelCase : Optional[int] = self.target.split('''.''' ) # Patch modules: # it's used to patch attributes of submodules like "os.path.join"; # in this case we need to patch "os" and "os.path" for i in range(len(__lowerCAmelCase ) ): try: _lowerCamelCase : Union[str, Any] = import_module('''.'''.join(submodules[: i + 1] ) ) except ModuleNotFoundError: continue # We iterate over all the globals in self.obj in case we find "os" or "os.path" for attr in self.obj.__dir__(): _lowerCamelCase : Dict = getattr(self.obj , __lowerCAmelCase ) # We don't check for the name of the global, but rather if its value *is* "os" or "os.path". # This allows to patch renamed modules like "from os import path as ospath". if obj_attr is submodule or ( (isinstance(__lowerCAmelCase , _PatchedModuleObj ) and obj_attr._original_module is submodule) ): _lowerCamelCase : Dict = obj_attr # patch at top level setattr(self.obj , __lowerCAmelCase , _PatchedModuleObj(__lowerCAmelCase , attrs=self.attrs ) ) _lowerCamelCase : Tuple = getattr(self.obj , __lowerCAmelCase ) # construct lower levels patches for key in submodules[i + 1 :]: setattr(__lowerCAmelCase , __lowerCAmelCase , _PatchedModuleObj(getattr(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) , attrs=self.attrs ) ) _lowerCamelCase : int = getattr(__lowerCAmelCase , __lowerCAmelCase ) # finally set the target attribute setattr(__lowerCAmelCase , __lowerCAmelCase , self.new ) # Patch attribute itself: # it's used for builtins like "open", # and also to patch "os.path.join" we may also need to patch "join" # itself if it was imported as "from os.path import join". if submodules: # if it's an attribute of a submodule like "os.path.join" try: _lowerCamelCase : Optional[int] = getattr(import_module('''.'''.join(__lowerCAmelCase ) ) , __lowerCAmelCase ) except (AttributeError, ModuleNotFoundError): return # We iterate over all the globals in self.obj in case we find "os.path.join" for attr in self.obj.__dir__(): # We don't check for the name of the global, but rather if its value *is* "os.path.join". # This allows to patch renamed attributes like "from os.path import join as pjoin". if getattr(self.obj , __lowerCAmelCase ) is attr_value: _lowerCamelCase : str = getattr(self.obj , __lowerCAmelCase ) setattr(self.obj , __lowerCAmelCase , self.new ) elif target_attr in globals()["__builtins__"]: # if it'a s builtin like "open" _lowerCamelCase : List[str] = globals()['''__builtins__'''][target_attr] setattr(self.obj , __lowerCAmelCase , self.new ) else: raise RuntimeError(f'''Tried to patch attribute {target_attr} instead of a submodule.''' ) def __exit__( self : Any , *__lowerCAmelCase : Dict ): """simple docstring""" for attr in list(self.original ): setattr(self.obj , __lowerCAmelCase , self.original.pop(__lowerCAmelCase ) ) def SCREAMING_SNAKE_CASE ( self : Dict ): """simple docstring""" self.__enter__() self._active_patches.append(self ) def SCREAMING_SNAKE_CASE ( self : List[str] ): """simple docstring""" try: self._active_patches.remove(self ) except ValueError: # If the patch hasn't been started this will fail return None return self.__exit__()
72
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node __snake_case = 4 __snake_case = 3 class __snake_case ( lowerCamelCase__ ): pass def lowerCAmelCase_ ( __lowerCAmelCase )-> List[str]: '''simple docstring''' for shard in shards: for i in range(__lowerCAmelCase ): yield {"i": i, "shard": shard} def lowerCAmelCase_ ( )-> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =int(os.environ['''RANK'''] ) UpperCAmelCase : Optional[Any] =int(os.environ['''WORLD_SIZE'''] ) UpperCAmelCase : List[Any] =ArgumentParser() parser.add_argument('''--streaming''' , type=__lowerCAmelCase ) parser.add_argument('''--local_rank''' , type=__lowerCAmelCase ) parser.add_argument('''--num_workers''' , type=__lowerCAmelCase , default=0 ) UpperCAmelCase : Any =parser.parse_args() UpperCAmelCase : List[str] =args.streaming UpperCAmelCase : Tuple =args.num_workers UpperCAmelCase : int ={'''shards''': [f'''shard_{shard_idx}''' for shard_idx in range(__lowerCAmelCase )]} UpperCAmelCase : Optional[int] =IterableDataset.from_generator(__lowerCAmelCase , gen_kwargs=__lowerCAmelCase ) if not streaming: UpperCAmelCase : List[Any] =Dataset.from_list(list(__lowerCAmelCase ) ) UpperCAmelCase : Dict =split_dataset_by_node(__lowerCAmelCase , rank=__lowerCAmelCase , world_size=__lowerCAmelCase ) UpperCAmelCase : List[Any] =torch.utils.data.DataLoader(__lowerCAmelCase , num_workers=__lowerCAmelCase ) UpperCAmelCase : Dict =NUM_SHARDS * NUM_ITEMS_PER_SHARD UpperCAmelCase : str =full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) UpperCAmelCase : List[Any] =sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f'''local_size {local_size} != expected_local_size {expected_local_size}''' ) if __name__ == "__main__": main()
348
0
def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ ) -> float: if digit_amount > 0: return round(number - int(lowerCamelCase__ ) , lowerCamelCase__ ) return number - int(lowerCamelCase__ ) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.3_45, 1)) print(decimal_isolate(35.3_45, 2)) print(decimal_isolate(35.3_45, 3)) print(decimal_isolate(-14.7_89, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.1_23, 1)) print(decimal_isolate(-14.1_23, 2)) print(decimal_isolate(-14.1_23, 3))
73
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __snake_case = {'''configuration_opt''': ['''OPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OPTConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''OPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OPTForCausalLM''', '''OPTModel''', '''OPTPreTrainedModel''', '''OPTForSequenceClassification''', '''OPTForQuestionAnswering''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''TFOPTForCausalLM''', '''TFOPTModel''', '''TFOPTPreTrainedModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''FlaxOPTForCausalLM''', '''FlaxOPTModel''', '''FlaxOPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
0
"""simple docstring""" import inspect import unittest import numpy as np from transformers import ViTConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor if is_flax_available(): import jax from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : List[Any] ,A_ : Any ,A_ : str=13 ,A_ : List[str]=30 ,A_ : Any=2 ,A_ : Union[str, Any]=3 ,A_ : List[str]=True ,A_ : Any=True ,A_ : List[Any]=32 ,A_ : List[Any]=5 ,A_ : List[Any]=4 ,A_ : Optional[int]=37 ,A_ : List[str]="gelu" ,A_ : Optional[int]=0.1 ,A_ : Optional[int]=0.1 ,A_ : Tuple=10 ,A_ : Any=0.02 ,) -> Union[str, Any]: A = parent A = batch_size A = image_size A = patch_size A = num_channels A = is_training A = use_labels A = hidden_size A = num_hidden_layers A = num_attention_heads A = intermediate_size A = hidden_act A = hidden_dropout_prob A = attention_probs_dropout_prob A = type_sequence_label_size A = initializer_range # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) A = (image_size // patch_size) ** 2 A = num_patches + 1 def _SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[Any]: A = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A = 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=A_ ,initializer_range=self.initializer_range ,) return config, pixel_values def _SCREAMING_SNAKE_CASE ( self : int ,A_ : Optional[Any] ,A_ : Union[str, Any] ) -> List[str]: A = FlaxViTModel(config=A_ ) A = model(A_ ) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) A = (self.image_size, self.image_size) A = (self.patch_size, self.patch_size) A = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, num_patches + 1, self.hidden_size) ) def _SCREAMING_SNAKE_CASE ( self : int ,A_ : Dict ,A_ : Tuple ) -> Tuple: A = self.type_sequence_label_size A = FlaxViTForImageClassification(config=A_ ) A = model(A_ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) # test greyscale images A = 1 A = FlaxViTForImageClassification(A_ ) A = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A = model(A_ ) def _SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Tuple: A = self.prepare_config_and_inputs() ( ( A ) , ( A ) , ) = config_and_inputs A = {'pixel_values': pixel_values} return config, inputs_dict @require_flax class lowerCAmelCase_ ( _lowercase , unittest.TestCase ): '''simple docstring''' _lowerCamelCase: Optional[int] = (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else () def _SCREAMING_SNAKE_CASE ( self : Any ) -> None: A = FlaxViTModelTester(self ) A = ConfigTester(self ,config_class=A_ ,has_text_modality=A_ ,hidden_size=37 ) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Dict: self.config_tester.run_common_tests() def _SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Dict: A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A_ ) def _SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[int]: A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*A_ ) def _SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> int: A , A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A = model_class(A_ ) A = inspect.signature(model.__call__ ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A = [*signature.parameters.keys()] A = ['pixel_values'] self.assertListEqual(arg_names[:1] ,A_ ) def _SCREAMING_SNAKE_CASE ( self : str ) -> Union[str, Any]: A , A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): A = self._prepare_for_class(A_ ,A_ ) A = model_class(A_ ) @jax.jit def model_jitted(A_ : List[Any] ,**A_ : List[Any] ): return model(pixel_values=A_ ,**A_ ) with self.subTest('JIT Enabled' ): A = model_jitted(**A_ ).to_tuple() with self.subTest('JIT Disabled' ): with jax.disable_jit(): A = model_jitted(**A_ ).to_tuple() self.assertEqual(len(A_ ) ,len(A_ ) ) for jitted_output, output in zip(A_ ,A_ ): self.assertEqual(jitted_output.shape ,output.shape ) @slow def _SCREAMING_SNAKE_CASE ( self : Dict ) -> Dict: for model_class_name in self.all_model_classes: A = model_class_name.from_pretrained('google/vit-base-patch16-224' ) A = model(np.ones((1, 3, 224, 224) ) ) self.assertIsNotNone(A_ )
74
import tempfile import unittest import numpy as np import transformers from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax import jax.numpy as jnp from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel if is_torch_available(): import torch class __snake_case : def __init__( self , snake_case__ , snake_case__=14 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=False , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=4 , snake_case__=4 , snake_case__=4 , snake_case__=37 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , ) -> str: '''simple docstring''' UpperCAmelCase : str =parent UpperCAmelCase : Tuple =batch_size UpperCAmelCase : Optional[int] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Tuple =use_input_mask UpperCAmelCase : List[Any] =use_token_type_ids UpperCAmelCase : Optional[Any] =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : List[Any] =hidden_size UpperCAmelCase : Optional[int] =rotary_dim UpperCAmelCase : Union[str, Any] =num_hidden_layers UpperCAmelCase : List[Any] =num_attention_heads UpperCAmelCase : Dict =intermediate_size UpperCAmelCase : Union[str, Any] =hidden_act UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : Dict =attention_probs_dropout_prob UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : str =initializer_range UpperCAmelCase : Optional[int] =None UpperCAmelCase : List[Any] =vocab_size - 1 UpperCAmelCase : Optional[Any] =vocab_size - 1 UpperCAmelCase : List[Any] =vocab_size - 1 def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : List[Any] =None if self.use_input_mask: UpperCAmelCase : Optional[Any] =random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : Dict =GPTJConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , use_cache=snake_case__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , rotary_dim=self.rotary_dim , ) return (config, input_ids, input_mask) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Union[str, Any] =config_and_inputs UpperCAmelCase : Tuple ={'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =20 UpperCAmelCase : Any =model_class_name(snake_case__ ) UpperCAmelCase : str =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : Any =jnp.ones((input_ids.shape[0], max_decoder_length) , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : List[str] =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =model( input_ids[:, -1:] , attention_mask=snake_case__ , past_key_values=outputs_cache.past_key_values , position_ids=snake_case__ , ) UpperCAmelCase : List[Any] =model(snake_case__ ) UpperCAmelCase : Any =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Dict =20 UpperCAmelCase : Dict =model_class_name(snake_case__ ) UpperCAmelCase : Tuple =jnp.concatenate( [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]) )] , axis=-1 , ) UpperCAmelCase : Dict =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : int =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : str =model( input_ids[:, -1:] , past_key_values=outputs_cache.past_key_values , attention_mask=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =model(snake_case__ , attention_mask=snake_case__ ) UpperCAmelCase : Dict =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) @require_flax class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Tuple = (FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else () __lowerCamelCase : Optional[Any] = (FlaxGPTJForCausalLM,) if is_flax_available() else () def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =FlaxGPTJModelTester(self ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Dict =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward_with_attn_mask( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) @tooslow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =GPTaTokenizer.from_pretrained('''gpt2''' , pad_token='''<|endoftext|>''' , padding_side='''left''' ) UpperCAmelCase : Optional[Any] =tokenizer(['''Hello this is a long string''', '''Hey'''] , return_tensors='''np''' , padding=snake_case__ , truncation=snake_case__ ) UpperCAmelCase : Optional[int] =FlaxGPTJForCausalLM.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : str =False UpperCAmelCase : Union[str, Any] =model.config.eos_token_id UpperCAmelCase : List[Any] =jax.jit(model.generate ) UpperCAmelCase : Dict =jit_generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , pad_token_id=tokenizer.pad_token_id ).sequences UpperCAmelCase : Any =tokenizer.batch_decode(snake_case__ , skip_special_tokens=snake_case__ ) UpperCAmelCase : Tuple =[ '''Hello this is a long string of text.\n\nI\'m trying to get the text of the''', '''Hey, I\'m a little late to the party. I\'m going to''', ] self.assertListEqual(snake_case__ , snake_case__ ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : List[str] =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : Any =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : Any =getattr(snake_case__ , snake_case__ ) UpperCAmelCase , UpperCAmelCase : Union[str, Any] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Tuple =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : int =0 UpperCAmelCase : Optional[int] =1 UpperCAmelCase : Optional[int] =0 UpperCAmelCase : Union[str, Any] =1 UpperCAmelCase : List[str] =pt_model_class(snake_case__ ).eval() UpperCAmelCase : Optional[int] =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Any =convert_pytorch_state_dict_to_flax(pt_model.state_dict() , snake_case__ ) UpperCAmelCase : Union[str, Any] =fx_state with torch.no_grad(): UpperCAmelCase : Any =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : Dict =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(snake_case__ ) UpperCAmelCase : str =model_class.from_pretrained(snake_case__ , from_pt=snake_case__ ) UpperCAmelCase : int =fx_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output_loaded, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output_loaded[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : int =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : int =getattr(snake_case__ , snake_case__ ) UpperCAmelCase : Dict =pt_model_class(snake_case__ ).eval() UpperCAmelCase : str =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Optional[Any] =load_flax_weights_in_pytorch_model(snake_case__ , fx_model.params ) UpperCAmelCase , UpperCAmelCase : Optional[int] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Optional[int] =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : str =0 UpperCAmelCase : Any =1 UpperCAmelCase : List[Any] =0 UpperCAmelCase : Tuple =1 # make sure weights are tied in PyTorch pt_model.tie_weights() with torch.no_grad(): UpperCAmelCase : Optional[Any] =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : List[Any] =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(snake_case__ ) UpperCAmelCase : Tuple =pt_model_class.from_pretrained(snake_case__ , from_flax=snake_case__ ) with torch.no_grad(): UpperCAmelCase : Any =pt_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @tooslow def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase : str =model_class_name.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : Tuple =model(np.ones((1, 1) ) ) self.assertIsNotNone(snake_case__ )
348
0
'''simple docstring''' from __future__ import annotations import unittest from transformers import is_tf_available, is_torch_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow if is_tf_available(): from transformers import ( AutoConfig, BertConfig, GPTaConfig, TaConfig, TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, ) from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST if is_torch_available(): from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForPreTraining, AutoModelForQuestionAnswering, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoModelWithLMHead, BertForMaskedLM, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, BertModel, GPTaLMHeadModel, RobertaForMaskedLM, TaForConditionalGeneration, ) @is_pt_tf_cross_test class __UpperCamelCase ( unittest.TestCase ): @slow def lowercase__ ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: lowerCamelCase_ =AutoConfig.from_pretrained(lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =TFAutoModel.from_pretrained(lowerCAmelCase, from_pt=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =AutoModel.from_pretrained(lowerCAmelCase, from_tf=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: lowerCamelCase_ =AutoConfig.from_pretrained(lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =TFAutoModelForPreTraining.from_pretrained(lowerCAmelCase, from_pt=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =AutoModelForPreTraining.from_pretrained(lowerCAmelCase, from_tf=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase_ =AutoConfig.from_pretrained(lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =TFAutoModelForCausalLM.from_pretrained(lowerCAmelCase, from_pt=lowerCAmelCase ) lowerCamelCase_, lowerCamelCase_ =TFAutoModelForCausalLM.from_pretrained( lowerCAmelCase, output_loading_info=lowerCAmelCase, from_pt=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =AutoModelForCausalLM.from_pretrained(lowerCAmelCase, from_tf=lowerCAmelCase ) lowerCamelCase_, lowerCamelCase_ =AutoModelForCausalLM.from_pretrained( lowerCAmelCase, output_loading_info=lowerCAmelCase, from_tf=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase_ =AutoConfig.from_pretrained(lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase, from_pt=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =AutoModelWithLMHead.from_pretrained(lowerCAmelCase, from_tf=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase_ =AutoConfig.from_pretrained(lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =TFAutoModelForMaskedLM.from_pretrained(lowerCAmelCase, from_pt=lowerCAmelCase ) lowerCamelCase_, lowerCamelCase_ =TFAutoModelForMaskedLM.from_pretrained( lowerCAmelCase, output_loading_info=lowerCAmelCase, from_pt=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =AutoModelForMaskedLM.from_pretrained(lowerCAmelCase, from_tf=lowerCAmelCase ) lowerCamelCase_, lowerCamelCase_ =AutoModelForMaskedLM.from_pretrained( lowerCAmelCase, output_loading_info=lowerCAmelCase, from_tf=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase_ =AutoConfig.from_pretrained(lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCAmelCase, from_pt=lowerCAmelCase ) lowerCamelCase_, lowerCamelCase_ =TFAutoModelForSeqaSeqLM.from_pretrained( lowerCAmelCase, output_loading_info=lowerCAmelCase, from_pt=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =AutoModelForSeqaSeqLM.from_pretrained(lowerCAmelCase, from_tf=lowerCAmelCase ) lowerCamelCase_, lowerCamelCase_ =AutoModelForSeqaSeqLM.from_pretrained( lowerCAmelCase, output_loading_info=lowerCAmelCase, from_tf=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: lowerCamelCase_ =AutoConfig.from_pretrained(lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =TFAutoModelForSequenceClassification.from_pretrained(lowerCAmelCase, from_pt=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =AutoModelForSequenceClassification.from_pretrained(lowerCAmelCase, from_tf=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: lowerCamelCase_ =AutoConfig.from_pretrained(lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =TFAutoModelForQuestionAnswering.from_pretrained(lowerCAmelCase, from_pt=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =AutoModelForQuestionAnswering.from_pretrained(lowerCAmelCase, from_tf=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase, from_pt=lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) self.assertEqual(model.num_parameters(), 14_410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase ), 14_410 ) lowerCamelCase_ =AutoModelWithLMHead.from_pretrained(lowerCAmelCase, from_tf=lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) self.assertEqual(model.num_parameters(), 14_410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase ), 14_410 ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase, from_pt=lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) self.assertEqual(model.num_parameters(), 14_410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase ), 14_410 ) lowerCamelCase_ =AutoModelWithLMHead.from_pretrained(lowerCAmelCase, from_tf=lowerCAmelCase ) self.assertIsInstance(lowerCAmelCase, lowerCAmelCase ) self.assertEqual(model.num_parameters(), 14_410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase ), 14_410 )
75
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __snake_case = { '''configuration_bloom''': ['''BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BloomConfig''', '''BloomOnnxConfig'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''BloomTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BloomForCausalLM''', '''BloomModel''', '''BloomPreTrainedModel''', '''BloomForSequenceClassification''', '''BloomForTokenClassification''', '''BloomForQuestionAnswering''', ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available a_ = { 'configuration_groupvit': [ 'GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GroupViTConfig', 'GroupViTOnnxConfig', 'GroupViTTextConfig', 'GroupViTVisionConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ 'GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GroupViTModel', 'GroupViTPreTrainedModel', 'GroupViTTextModel', 'GroupViTVisionModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ 'TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFGroupViTModel', 'TFGroupViTPreTrainedModel', 'TFGroupViTTextModel', 'TFGroupViTVisionModel', ] if TYPE_CHECKING: from .configuration_groupvit import ( GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GroupViTConfig, GroupViTOnnxConfig, GroupViTTextConfig, GroupViTVisionConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_groupvit import ( GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, GroupViTModel, GroupViTPreTrainedModel, GroupViTTextModel, GroupViTVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_groupvit import ( TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFGroupViTModel, TFGroupViTPreTrainedModel, TFGroupViTTextModel, TFGroupViTVisionModel, ) else: import sys a_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
76
import os from typing import Dict, List, Tuple, TypeVar, Union __snake_case = TypeVar('''T''') __snake_case = Union[List[T], Tuple[T, ...]] __snake_case = Union[T, List[T], Dict[str, T]] __snake_case = Union[str, bytes, os.PathLike]
348
0
"""simple docstring""" import torch def a_ ( ): '''simple docstring''' if torch.cuda.is_available(): lowercase__ : Tuple = torch.cuda.device_count() else: lowercase__ : Optional[int] = 0 print(f"""Successfully ran on {num_gpus} GPUs""" ) if __name__ == "__main__": main()
77
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_big_bird import BigBirdTokenizer else: __snake_case = None __snake_case = logging.get_logger(__name__) __snake_case = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} __snake_case = { '''vocab_file''': { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model''', '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model''' ), }, '''tokenizer_file''': { '''google/bigbird-roberta-base''': ( '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/tokenizer.json''' ), '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/tokenizer.json''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/tokenizer.json''' ), }, } __snake_case = { '''google/bigbird-roberta-base''': 40_96, '''google/bigbird-roberta-large''': 40_96, '''google/bigbird-base-trivia-itc''': 40_96, } __snake_case = '''▁''' class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Dict = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = BigBirdTokenizer __lowerCamelCase : Any = ["""input_ids""", """attention_mask"""] __lowerCamelCase : List[int] = [] def __init__( self , snake_case__=None , snake_case__=None , snake_case__="<unk>" , snake_case__="<s>" , snake_case__="</s>" , snake_case__="<pad>" , snake_case__="[SEP]" , snake_case__="[MASK]" , snake_case__="[CLS]" , **snake_case__ , ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else bos_token UpperCAmelCase : Optional[int] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else eos_token UpperCAmelCase : List[str] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else unk_token UpperCAmelCase : Union[str, Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else pad_token UpperCAmelCase : int =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else cls_token UpperCAmelCase : str =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else sep_token # Mask token behave like a normal word, i.e. include the space before it UpperCAmelCase : List[Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else mask_token super().__init__( snake_case__ , tokenizer_file=snake_case__ , bos_token=snake_case__ , eos_token=snake_case__ , unk_token=snake_case__ , sep_token=snake_case__ , pad_token=snake_case__ , cls_token=snake_case__ , mask_token=snake_case__ , **snake_case__ , ) UpperCAmelCase : Tuple =vocab_file UpperCAmelCase : Optional[int] =False if not self.vocab_file else True def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : int =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None , snake_case__ = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is None: return [1] + ([0] * len(snake_case__ )) + [1] return [1] + ([0] * len(snake_case__ )) + [1] + ([0] * len(snake_case__ )) + [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : Optional[Any] =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> Tuple[str]: '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(snake_case__ ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCAmelCase : Optional[int] =os.path.join( snake_case__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case__ ): copyfile(self.vocab_file , snake_case__ ) return (out_vocab_file,)
348
0
"""simple docstring""" from __future__ import annotations def _lowerCAmelCase ( lowercase_ , lowercase_ ): UpperCAmelCase = 0 UpperCAmelCase = len(lowercase_ ) - 1 while i < j: if nums[i] + nums[j] == target: return [i, j] elif nums[i] + nums[j] < target: UpperCAmelCase = i + 1 else: UpperCAmelCase = j - 1 return [] if __name__ == "__main__": import doctest doctest.testmod() print(f'''{two_pointer([2, 7, 11, 15], 9) = }''')
78
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def lowerCAmelCase_ ( __lowerCAmelCase )-> Optional[Any]: '''simple docstring''' def is_in_circle(__lowerCAmelCase , __lowerCAmelCase ) -> bool: UpperCAmelCase : List[Any] =sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle UpperCAmelCase : List[Any] =mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(__lowerCAmelCase ) ) # The ratio of the area for circle to square is pi/4. UpperCAmelCase : Dict =proportion * 4 print(f'''The estimated value of pi is {pi_estimate}''' ) print(f'''The numpy value of pi is {pi}''' ) print(f'''The total error is {abs(pi - pi_estimate )}''' ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 , )-> float: '''simple docstring''' return mean( function_to_integrate(uniform(__lowerCAmelCase , __lowerCAmelCase ) ) for _ in range(__lowerCAmelCase ) ) * (max_value - min_value) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 )-> None: '''simple docstring''' def identity_function(__lowerCAmelCase ) -> float: return x UpperCAmelCase : List[Any] =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase : Dict =(max_value * max_value - min_value * min_value) / 2 print('''******************''' ) print(f'''Estimating area under y=x where x varies from {min_value} to {max_value}''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {expected_value}''' ) print(f'''Total error is {abs(estimated_value - expected_value )}''' ) print('''******************''' ) def lowerCAmelCase_ ( __lowerCAmelCase )-> None: '''simple docstring''' def function_to_integrate(__lowerCAmelCase ) -> float: return sqrt(4.0 - x * x ) UpperCAmelCase : Dict =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , 0.0 , 2.0 ) print('''******************''' ) print('''Estimating pi using area_under_curve_estimator''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {pi}''' ) print(f'''Total error is {abs(estimated_value - pi )}''' ) print('''******************''' ) if __name__ == "__main__": import doctest doctest.testmod()
348
0
'''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 ): """simple docstring""" def lowerCAmelCase ( self : Dict ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase ( self : List[str] ): '''simple docstring''' _A = StableDiffusionKDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4" ) _A = sd_pipe.to(__UpperCAmelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase ) sd_pipe.set_scheduler("sample_euler" ) _A = "A painting of a squirrel eating a burger" _A = torch.manual_seed(0 ) _A = sd_pipe([prompt] , generator=__UpperCAmelCase , guidance_scale=9.0 , num_inference_steps=20 , output_type="np" ) _A = output.images _A = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) _A = np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def lowerCAmelCase ( self : Union[str, Any] ): '''simple docstring''' _A = StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base" ) _A = sd_pipe.to(__UpperCAmelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase ) sd_pipe.set_scheduler("sample_euler" ) _A = "A painting of a squirrel eating a burger" _A = torch.manual_seed(0 ) _A = sd_pipe([prompt] , generator=__UpperCAmelCase , guidance_scale=9.0 , num_inference_steps=20 , output_type="np" ) _A = output.images _A = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) _A = np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-1 def lowerCAmelCase ( self : Optional[int] ): '''simple docstring''' _A = StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base" ) _A = sd_pipe.to(__UpperCAmelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase ) sd_pipe.set_scheduler("sample_dpmpp_2m" ) _A = "A painting of a squirrel eating a burger" _A = torch.manual_seed(0 ) _A = sd_pipe( [prompt] , generator=__UpperCAmelCase , guidance_scale=7.5 , num_inference_steps=15 , output_type="np" , use_karras_sigmas=__UpperCAmelCase , ) _A = output.images _A = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) _A = np.array( [0.11381689, 0.12112921, 0.1389457, 0.12549606, 0.1244964, 0.10831517, 0.11562866, 0.10867816, 0.10499048] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
79
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __snake_case : def __init__( self , snake_case__ , snake_case__=12 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , snake_case__=0 , snake_case__=None , ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : List[Any] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Union[str, Any] =use_input_mask UpperCAmelCase : Tuple =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : Tuple =hidden_size UpperCAmelCase : Dict =projection_dim UpperCAmelCase : Optional[int] =num_hidden_layers UpperCAmelCase : Dict =num_attention_heads UpperCAmelCase : int =intermediate_size UpperCAmelCase : Any =dropout UpperCAmelCase : Union[str, Any] =attention_dropout UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : str =scope UpperCAmelCase : str =bos_token_id def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : int =None if self.use_input_mask: UpperCAmelCase : Union[str, Any] =random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: UpperCAmelCase : Optional[int] =input_mask.numpy() UpperCAmelCase , UpperCAmelCase : List[Any] =input_mask.shape UpperCAmelCase : Optional[Any] =np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : List[Any] =1 UpperCAmelCase : Tuple =0 UpperCAmelCase : List[Any] =self.get_config() return config, input_ids, tf.convert_to_tensor(snake_case__ ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =TFBlipTextModel(config=snake_case__ ) UpperCAmelCase : List[Any] =model(snake_case__ , attention_mask=snake_case__ , training=snake_case__ ) UpperCAmelCase : str =model(snake_case__ , training=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[Any] =config_and_inputs UpperCAmelCase : Optional[int] ={'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __snake_case ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Optional[int] = (TFBlipTextModel,) if is_tf_available() else () __lowerCamelCase : Dict = False __lowerCamelCase : Optional[Any] = False __lowerCamelCase : Dict = False def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : str =BlipTextModelTester(self ) UpperCAmelCase : Optional[int] =ConfigTester(self , config_class=snake_case__ , hidden_size=37 ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip(reason='''Blip does not use inputs_embeds''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @slow def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase : Optional[Any] =TFBlipTextModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def UpperCAmelCase__ ( self , snake_case__=True ) -> Any: '''simple docstring''' super().test_pt_tf_model_equivalence(allow_missing_keys=snake_case__ )
348
0
'''simple docstring''' import argparse import json import os import sys import tempfile import unittest from argparse import Namespace from dataclasses import dataclass, field from enum import Enum from pathlib import Path from typing import List, Literal, Optional import yaml from transformers import HfArgumentParser, TrainingArguments from transformers.hf_argparser import make_choice_type_function, string_to_bool # Since Python 3.10, we can use the builtin `|` operator for Union types # See PEP 604: https://peps.python.org/pep-0604 a__ : Any = sys.version_info >= (3, 1_0) def _UpperCamelCase ( __A=None , __A=None ) -> Optional[int]: '''simple docstring''' return field(default_factory=lambda: default , metadata=__A ) @dataclass class lowercase_ : __UpperCAmelCase = 42 __UpperCAmelCase = 42 __UpperCAmelCase = 42 __UpperCAmelCase = 42 @dataclass class lowercase_ : __UpperCAmelCase = 42 __UpperCAmelCase = field(default='toto' , metadata={'help': 'help message'} ) @dataclass class lowercase_ : __UpperCAmelCase = False __UpperCAmelCase = True __UpperCAmelCase = None class lowercase_ ( a__ ): __UpperCAmelCase = 'titi' __UpperCAmelCase = 'toto' class lowercase_ ( a__ ): __UpperCAmelCase = 'titi' __UpperCAmelCase = 'toto' __UpperCAmelCase = 42 @dataclass class lowercase_ : __UpperCAmelCase = "toto" def __a ( self ): UpperCamelCase__ = BasicEnum(self.foo ) @dataclass class lowercase_ : __UpperCAmelCase = "toto" def __a ( self ): UpperCamelCase__ = MixedTypeEnum(self.foo ) @dataclass class lowercase_ : __UpperCAmelCase = None __UpperCAmelCase = field(default=a__ , metadata={'help': 'help message'} ) __UpperCAmelCase = None __UpperCAmelCase = list_field(default=[] ) __UpperCAmelCase = list_field(default=[] ) @dataclass class lowercase_ : __UpperCAmelCase = list_field(default=[] ) __UpperCAmelCase = list_field(default=[1, 2, 3] ) __UpperCAmelCase = list_field(default=['Hallo', 'Bonjour', 'Hello'] ) __UpperCAmelCase = list_field(default=[0.1, 0.2, 0.3] ) @dataclass class lowercase_ : __UpperCAmelCase = field() __UpperCAmelCase = field() __UpperCAmelCase = field() def __a ( self ): UpperCamelCase__ = BasicEnum(self.required_enum ) @dataclass class lowercase_ : __UpperCAmelCase = 42 __UpperCAmelCase = field() __UpperCAmelCase = None __UpperCAmelCase = field(default='toto' , metadata={'help': 'help message'} ) __UpperCAmelCase = list_field(default=['Hallo', 'Bonjour', 'Hello'] ) if is_python_no_less_than_3_10: @dataclass class lowercase_ : __UpperCAmelCase = False __UpperCAmelCase = True __UpperCAmelCase = None @dataclass class lowercase_ : __UpperCAmelCase = None __UpperCAmelCase = field(default=a__ , metadata={'help': 'help message'} ) __UpperCAmelCase = None __UpperCAmelCase = list_field(default=[] ) __UpperCAmelCase = list_field(default=[] ) class lowercase_ ( unittest.TestCase ): def __a ( self , a , a ): self.assertEqual(len(a._actions ) , len(b._actions ) ) for x, y in zip(a._actions , b._actions ): UpperCamelCase__ = {k: v for k, v in vars(a ).items() if k != "container"} UpperCamelCase__ = {k: v for k, v in vars(a ).items() if k != "container"} # Choices with mixed type have custom function as "type" # So we need to compare results directly for equality if xx.get("choices" , a ) and yy.get("choices" , a ): for expected_choice in yy["choices"] + xx["choices"]: self.assertEqual(xx["type"](a ) , yy["type"](a ) ) del xx["type"], yy["type"] self.assertEqual(a , a ) def __a ( self ): UpperCamelCase__ = HfArgumentParser(a ) UpperCamelCase__ = argparse.ArgumentParser() expected.add_argument("--foo" , type=a , required=a ) expected.add_argument("--bar" , type=a , required=a ) expected.add_argument("--baz" , type=a , required=a ) expected.add_argument("--flag" , type=a , default=a , const=a , nargs="?" ) self.argparsersEqual(a , a ) UpperCamelCase__ = ["--foo", "1", "--baz", "quux", "--bar", "0.5"] ((UpperCamelCase__) , ) = parser.parse_args_into_dataclasses(a , look_for_args_file=a ) self.assertFalse(example.flag ) def __a ( self ): UpperCamelCase__ = HfArgumentParser(a ) UpperCamelCase__ = argparse.ArgumentParser() expected.add_argument("--foo" , default=42 , type=a ) expected.add_argument("--baz" , default="toto" , type=a , help="help message" ) self.argparsersEqual(a , a ) def __a ( self ): UpperCamelCase__ = argparse.ArgumentParser() expected.add_argument("--foo" , type=a , default=a , const=a , nargs="?" ) expected.add_argument("--baz" , type=a , default=a , const=a , nargs="?" ) # A boolean no_* argument always has to come after its "default: True" regular counter-part # and its default must be set to False expected.add_argument("--no_baz" , action="store_false" , default=a , dest="baz" ) expected.add_argument("--opt" , type=a , default=a ) UpperCamelCase__ = [WithDefaultBoolExample] if is_python_no_less_than_3_10: dataclass_types.append(a ) for dataclass_type in dataclass_types: UpperCamelCase__ = HfArgumentParser(a ) self.argparsersEqual(a , a ) UpperCamelCase__ = parser.parse_args([] ) self.assertEqual(a , Namespace(foo=a , baz=a , opt=a ) ) UpperCamelCase__ = parser.parse_args(["--foo", "--no_baz"] ) self.assertEqual(a , Namespace(foo=a , baz=a , opt=a ) ) UpperCamelCase__ = parser.parse_args(["--foo", "--baz"] ) self.assertEqual(a , Namespace(foo=a , baz=a , opt=a ) ) UpperCamelCase__ = parser.parse_args(["--foo", "True", "--baz", "True", "--opt", "True"] ) self.assertEqual(a , Namespace(foo=a , baz=a , opt=a ) ) UpperCamelCase__ = parser.parse_args(["--foo", "False", "--baz", "False", "--opt", "False"] ) self.assertEqual(a , Namespace(foo=a , baz=a , opt=a ) ) def __a ( self ): UpperCamelCase__ = HfArgumentParser(a ) UpperCamelCase__ = argparse.ArgumentParser() expected.add_argument( "--foo" , default="toto" , choices=["titi", "toto", 42] , type=make_choice_type_function(["titi", "toto", 42] ) , ) self.argparsersEqual(a , a ) UpperCamelCase__ = parser.parse_args([] ) self.assertEqual(args.foo , "toto" ) UpperCamelCase__ = parser.parse_args_into_dataclasses([] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.toto ) UpperCamelCase__ = parser.parse_args(["--foo", "titi"] ) self.assertEqual(args.foo , "titi" ) UpperCamelCase__ = parser.parse_args_into_dataclasses(["--foo", "titi"] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.titi ) UpperCamelCase__ = parser.parse_args(["--foo", "42"] ) self.assertEqual(args.foo , 42 ) UpperCamelCase__ = parser.parse_args_into_dataclasses(["--foo", "42"] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.fourtytwo ) def __a ( self ): @dataclass class lowercase_ : __UpperCAmelCase = "toto" UpperCamelCase__ = HfArgumentParser(a ) UpperCamelCase__ = argparse.ArgumentParser() expected.add_argument( "--foo" , default="toto" , choices=("titi", "toto", 42) , type=make_choice_type_function(["titi", "toto", 42] ) , ) self.argparsersEqual(a , a ) UpperCamelCase__ = parser.parse_args([] ) self.assertEqual(args.foo , "toto" ) UpperCamelCase__ = parser.parse_args(["--foo", "titi"] ) self.assertEqual(args.foo , "titi" ) UpperCamelCase__ = parser.parse_args(["--foo", "42"] ) self.assertEqual(args.foo , 42 ) def __a ( self ): UpperCamelCase__ = HfArgumentParser(a ) UpperCamelCase__ = argparse.ArgumentParser() expected.add_argument("--foo_int" , nargs="+" , default=[] , type=a ) expected.add_argument("--bar_int" , nargs="+" , default=[1, 2, 3] , type=a ) expected.add_argument("--foo_str" , nargs="+" , default=["Hallo", "Bonjour", "Hello"] , type=a ) expected.add_argument("--foo_float" , nargs="+" , default=[0.1, 0.2, 0.3] , type=a ) self.argparsersEqual(a , a ) UpperCamelCase__ = parser.parse_args([] ) self.assertEqual( a , Namespace(foo_int=[] , bar_int=[1, 2, 3] , foo_str=["Hallo", "Bonjour", "Hello"] , foo_float=[0.1, 0.2, 0.3] ) , ) UpperCamelCase__ = parser.parse_args("--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7".split() ) self.assertEqual(a , Namespace(foo_int=[1] , bar_int=[2, 3] , foo_str=["a", "b", "c"] , foo_float=[0.1, 0.7] ) ) def __a ( self ): UpperCamelCase__ = argparse.ArgumentParser() expected.add_argument("--foo" , default=a , type=a ) expected.add_argument("--bar" , default=a , type=a , help="help message" ) expected.add_argument("--baz" , default=a , type=a ) expected.add_argument("--ces" , nargs="+" , default=[] , type=a ) expected.add_argument("--des" , nargs="+" , default=[] , type=a ) UpperCamelCase__ = [OptionalExample] if is_python_no_less_than_3_10: dataclass_types.append(a ) for dataclass_type in dataclass_types: UpperCamelCase__ = HfArgumentParser(a ) self.argparsersEqual(a , a ) UpperCamelCase__ = parser.parse_args([] ) self.assertEqual(a , Namespace(foo=a , bar=a , baz=a , ces=[] , des=[] ) ) UpperCamelCase__ = parser.parse_args("--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3".split() ) self.assertEqual(a , Namespace(foo=12 , bar=3.14 , baz="42" , ces=["a", "b", "c"] , des=[1, 2, 3] ) ) def __a ( self ): UpperCamelCase__ = HfArgumentParser(a ) UpperCamelCase__ = argparse.ArgumentParser() expected.add_argument("--required_list" , nargs="+" , type=a , required=a ) expected.add_argument("--required_str" , type=a , required=a ) expected.add_argument( "--required_enum" , type=make_choice_type_function(["titi", "toto"] ) , choices=["titi", "toto"] , required=a , ) self.argparsersEqual(a , a ) def __a ( self ): UpperCamelCase__ = HfArgumentParser(a ) UpperCamelCase__ = argparse.ArgumentParser() expected.add_argument("--foo" , type=a , required=a ) expected.add_argument( "--required_enum" , type=make_choice_type_function(["titi", "toto"] ) , choices=["titi", "toto"] , required=a , ) expected.add_argument("--opt" , type=a , default=a ) expected.add_argument("--baz" , default="toto" , type=a , help="help message" ) expected.add_argument("--foo_str" , nargs="+" , default=["Hallo", "Bonjour", "Hello"] , type=a ) self.argparsersEqual(a , a ) def __a ( self ): UpperCamelCase__ = HfArgumentParser(a ) UpperCamelCase__ = { "foo": 12, "bar": 3.14, "baz": "42", "flag": True, } UpperCamelCase__ = parser.parse_dict(a )[0] UpperCamelCase__ = BasicExample(**a ) self.assertEqual(a , a ) def __a ( self ): UpperCamelCase__ = HfArgumentParser(a ) UpperCamelCase__ = { "foo": 12, "bar": 3.14, "baz": "42", "flag": True, "extra": 42, } self.assertRaises(a , parser.parse_dict , a , allow_extra_keys=a ) def __a ( self ): UpperCamelCase__ = HfArgumentParser(a ) UpperCamelCase__ = { "foo": 12, "bar": 3.14, "baz": "42", "flag": True, } with tempfile.TemporaryDirectory() as tmp_dir: UpperCamelCase__ = os.path.join(a , "temp_json" ) os.mkdir(a ) with open(temp_local_path + ".json" , "w+" ) as f: json.dump(a , a ) UpperCamelCase__ = parser.parse_yaml_file(Path(temp_local_path + ".json" ) )[0] UpperCamelCase__ = BasicExample(**a ) self.assertEqual(a , a ) def __a ( self ): UpperCamelCase__ = HfArgumentParser(a ) UpperCamelCase__ = { "foo": 12, "bar": 3.14, "baz": "42", "flag": True, } with tempfile.TemporaryDirectory() as tmp_dir: UpperCamelCase__ = os.path.join(a , "temp_yaml" ) os.mkdir(a ) with open(temp_local_path + ".yaml" , "w+" ) as f: yaml.dump(a , a ) UpperCamelCase__ = parser.parse_yaml_file(Path(temp_local_path + ".yaml" ) )[0] UpperCamelCase__ = BasicExample(**a ) self.assertEqual(a , a ) def __a ( self ): UpperCamelCase__ = HfArgumentParser(a ) self.assertIsNotNone(a )
80
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging __snake_case = logging.get_logger(__name__) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' UpperCAmelCase : Dict =nn.functional.normalize(__lowerCAmelCase ) UpperCAmelCase : Tuple =nn.functional.normalize(__lowerCAmelCase ) return torch.mm(__lowerCAmelCase , normalized_text_embeds.t() ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : List[str] = CLIPConfig __lowerCamelCase : List[Any] = ["""CLIPEncoderLayer"""] def __init__( self , snake_case__ ) -> Dict: '''simple docstring''' super().__init__(snake_case__ ) UpperCAmelCase : Dict =CLIPVisionModel(config.vision_config ) UpperCAmelCase : Optional[Any] =nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=snake_case__ ) UpperCAmelCase : int =nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : List[str] =nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : str =nn.Parameter(torch.ones(17 ) , requires_grad=snake_case__ ) UpperCAmelCase : Optional[int] =nn.Parameter(torch.ones(3 ) , requires_grad=snake_case__ ) @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Union[str, Any] =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : Optional[Any] =self.visual_projection(snake_case__ ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 UpperCAmelCase : List[str] =cosine_distance(snake_case__ , self.special_care_embeds ).cpu().float().numpy() UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ).cpu().float().numpy() UpperCAmelCase : Tuple =[] UpperCAmelCase : Dict =image_embeds.shape[0] for i in range(snake_case__ ): UpperCAmelCase : str ={'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : str =0.0 for concept_idx in range(len(special_cos_dist[0] ) ): UpperCAmelCase : Optional[Any] =special_cos_dist[i][concept_idx] UpperCAmelCase : Union[str, Any] =self.special_care_embeds_weights[concept_idx].item() UpperCAmelCase : str =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} ) UpperCAmelCase : int =0.01 for concept_idx in range(len(cos_dist[0] ) ): UpperCAmelCase : Any =cos_dist[i][concept_idx] UpperCAmelCase : Optional[int] =self.concept_embeds_weights[concept_idx].item() UpperCAmelCase : int =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(snake_case__ ) result.append(snake_case__ ) UpperCAmelCase : Optional[int] =[len(res['''bad_concepts'''] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Any =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : List[str] =self.visual_projection(snake_case__ ) UpperCAmelCase : Any =cosine_distance(snake_case__ , self.special_care_embeds ) UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : Optional[Any] =0.0 UpperCAmelCase : Any =special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) UpperCAmelCase : str =torch.any(special_scores > 0 , dim=1 ) UpperCAmelCase : List[Any] =special_care * 0.01 UpperCAmelCase : Union[str, Any] =special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) UpperCAmelCase : List[Any] =(cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) UpperCAmelCase : str =torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
348
0
"""simple docstring""" import random import timeit from functools import wraps from typing import Callable, Optional from ..configuration_utils import PretrainedConfig from ..models.auto.modeling_tf_auto import TF_MODEL_MAPPING, TF_MODEL_WITH_LM_HEAD_MAPPING from ..utils import is_pyanvml_available, is_tf_available, logging from .benchmark_utils import ( Benchmark, Memory, MemorySummary, measure_peak_memory_cpu, start_memory_tracing, stop_memory_tracing, ) if is_tf_available(): import tensorflow as tf from tensorflow.python.framework.errors_impl import ResourceExhaustedError from .benchmark_args_tf import TensorFlowBenchmarkArguments if is_pyanvml_available(): import pyanvml.pyanvml as nvml lowerCamelCase_ : Union[str, Any] = logging.get_logger(__name__) def _A ( lowercase , lowercase ): """simple docstring""" def run_func(lowercase ): @wraps(lowercase ) def run_in_eager_mode(*lowercase , **lowercase ): return func(*lowercase , **lowercase ) @wraps(lowercase ) @tf.function(experimental_compile=lowercase ) def run_in_graph_mode(*lowercase , **lowercase ): return func(*lowercase , **lowercase ) if do_eager_mode is True: if use_xla is not False: raise ValueError( '''Cannot run model in XLA, if `args.eager_mode` is set to `True`. Please set `args.eager_mode=False`.''' ) return run_in_eager_mode else: return run_in_graph_mode return run_func def _A ( lowercase , lowercase , lowercase ): """simple docstring""" a =random.Random() a =[rng.randint(0 , vocab_size - 1 ) for i in range(batch_size * sequence_length )] return tf.constant(lowercase , shape=(batch_size, sequence_length) , dtype=tf.intaa ) class __A ( _SCREAMING_SNAKE_CASE ): """simple docstring""" __lowerCAmelCase = 42 __lowerCAmelCase = 42 __lowerCAmelCase = "TensorFlow" @property def SCREAMING_SNAKE_CASE ( self ) -> Optional[Any]: return tf.__version__ def SCREAMING_SNAKE_CASE ( self , __A , __A , __A ) -> float: # initialize GPU on separate process a =self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''' ) a =self._prepare_inference_func(__A , __A , __A ) return self._measure_speed(_inference ) def SCREAMING_SNAKE_CASE ( self , __A , __A , __A ) -> float: a =self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''' ) a =self._prepare_train_func(__A , __A , __A ) return self._measure_speed(_train ) def SCREAMING_SNAKE_CASE ( self , __A , __A , __A ) -> [Memory, Optional[MemorySummary]]: # initialize GPU on separate process if self.args.is_gpu: tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx] , __A ) a =self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''' ) a =self._prepare_inference_func(__A , __A , __A ) return self._measure_memory(_inference ) def SCREAMING_SNAKE_CASE ( self , __A , __A , __A ) -> [Memory, Optional[MemorySummary]]: if self.args.is_gpu: tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx] , __A ) a =self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''' ) a =self._prepare_train_func(__A , __A , __A ) return self._measure_memory(_train ) def SCREAMING_SNAKE_CASE ( self , __A , __A , __A ) -> Callable[[], None]: a =self.config_dict[model_name] if self.args.fpaa: raise NotImplementedError('''Mixed precision is currently not supported.''' ) a =( hasattr(__A , '''architectures''' ) and isinstance(config.architectures , __A ) and len(config.architectures ) > 0 ) if not self.args.only_pretrain_model and has_model_class_in_config: try: a ='''TF''' + config.architectures[0] # prepend 'TF' for tensorflow model a =__import__('''transformers''' , fromlist=[model_class] ) a =getattr(__A , __A ) a =model_cls(__A ) except ImportError: raise ImportError( f'''{model_class} does not exist. If you just want to test the pretrained model, you might want to''' ''' set `--only_pretrain_model` or `args.only_pretrain_model=True`.''' ) else: a =TF_MODEL_MAPPING[config.__class__](__A ) # encoder-decoder has vocab size saved differently a =config.vocab_size if hasattr(__A , '''vocab_size''' ) else config.encoder.vocab_size a =random_input_ids(__A , __A , __A ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_decoder_forward(): return model(__A , decoder_input_ids=__A , training=__A ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_forward(): return model(__A , training=__A ) a =encoder_decoder_forward if config.is_encoder_decoder else encoder_forward return _inference def SCREAMING_SNAKE_CASE ( self , __A , __A , __A ) -> Callable[[], None]: a =self.config_dict[model_name] if self.args.eager_mode is not False: raise ValueError('''Training cannot be done in eager mode. Please make sure that `args.eager_mode = False`.''' ) if self.args.fpaa: raise NotImplementedError('''Mixed precision is currently not supported.''' ) a =( hasattr(__A , '''architectures''' ) and isinstance(config.architectures , __A ) and len(config.architectures ) > 0 ) if not self.args.only_pretrain_model and has_model_class_in_config: try: a ='''TF''' + config.architectures[0] # prepend 'TF' for tensorflow model a =__import__('''transformers''' , fromlist=[model_class] ) a =getattr(__A , __A ) a =model_cls(__A ) except ImportError: raise ImportError( f'''{model_class} does not exist. If you just want to test the pretrained model, you might want to''' ''' set `--only_pretrain_model` or `args.only_pretrain_model=True`.''' ) else: a =TF_MODEL_WITH_LM_HEAD_MAPPING[config.__class__](__A ) # encoder-decoder has vocab size saved differently a =config.vocab_size if hasattr(__A , '''vocab_size''' ) else config.encoder.vocab_size a =random_input_ids(__A , __A , __A ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_decoder_train(): a =model(__A , decoder_input_ids=__A , labels=__A , training=__A )[0] a =tf.gradients(__A , model.trainable_variables ) return gradients @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_train(): a =model(__A , labels=__A , training=__A )[0] a =tf.gradients(__A , model.trainable_variables ) return gradients a =encoder_decoder_train if config.is_encoder_decoder else encoder_train return _train def SCREAMING_SNAKE_CASE ( self , __A ) -> float: with self.args.strategy.scope(): try: if self.args.is_tpu or self.args.use_xla: # run additional 10 times to stabilize compilation for tpu logger.info('''Do inference on TPU. Running model 5 times to stabilize compilation''' ) timeit.repeat(__A , repeat=1 , number=5 ) # as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average a =timeit.repeat( __A , repeat=self.args.repeat , number=10 , ) return min(__A ) / 10.0 except ResourceExhaustedError as e: self.print_fn(f'''Doesn\'t fit on GPU. {e}''' ) def SCREAMING_SNAKE_CASE ( self , __A ) -> [Memory, MemorySummary]: logger.info( '''Note that TensorFlow allocates more memory than ''' '''it might need to speed up computation. ''' '''The memory reported here corresponds to the memory ''' '''reported by `nvidia-smi`, which can vary depending ''' '''on total available memory on the GPU that is used.''' ) with self.args.strategy.scope(): try: if self.args.trace_memory_line_by_line: if not self.args.eager_mode: raise ValueError( '''`args.eager_mode` is set to `False`. Make sure to run model in eager mode to measure memory''' ''' consumption line by line.''' ) a =start_memory_tracing('''transformers''' ) if self.args.is_tpu: # tpu raise NotImplementedError( '''Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking''' ''' with `args.memory=False`''' ) elif self.args.is_gpu: # gpu if not is_pyanvml_available(): logger.warning( '''py3nvml not installed, we won\'t log GPU memory usage. ''' '''Install py3nvml (pip install py3nvml) to log information about GPU.''' ) a ='''N/A''' else: logger.info( '''Measuring total GPU usage on GPU device. Make sure to not have additional processes''' ''' running on the same GPU.''' ) # init nvml nvml.nvmlInit() func() a =nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx ) a =nvml.nvmlDeviceGetMemoryInfo(__A ) a =meminfo.used a =Memory(__A ) # shutdown nvml nvml.nvmlShutdown() else: # cpu if self.args.trace_memory_line_by_line: logger.info( '''When enabling line by line tracing, the max peak memory for CPU is inaccurate in''' ''' TensorFlow.''' ) a =None else: a =measure_peak_memory_cpu(__A ) a =Memory(__A ) if isinstance(__A , __A ) else memory_bytes if self.args.trace_memory_line_by_line: a =stop_memory_tracing(__A ) if memory is None: a =summary.total else: a =None return memory, summary except ResourceExhaustedError as e: self.print_fn(f'''Doesn\'t fit on GPU. {e}''' ) return "N/A", None
81
import argparse import intel_extension_for_pytorch as ipex import torch from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline __snake_case = argparse.ArgumentParser('''Stable Diffusion script with intel optimization''', add_help=False) parser.add_argument('''--dpm''', action='''store_true''', help='''Enable DPMSolver or not''') parser.add_argument('''--steps''', default=None, type=int, help='''Num inference steps''') __snake_case = parser.parse_args() __snake_case = '''cpu''' __snake_case = '''a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings''' __snake_case = '''path-to-your-trained-model''' __snake_case = StableDiffusionPipeline.from_pretrained(model_id) if args.dpm: __snake_case = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) __snake_case = pipe.to(device) # to channels last __snake_case = pipe.unet.to(memory_format=torch.channels_last) __snake_case = pipe.vae.to(memory_format=torch.channels_last) __snake_case = pipe.text_encoder.to(memory_format=torch.channels_last) if pipe.requires_safety_checker: __snake_case = pipe.safety_checker.to(memory_format=torch.channels_last) # optimize with ipex __snake_case = torch.randn(2, 4, 64, 64) __snake_case = torch.rand(1) * 9_99 __snake_case = torch.randn(2, 77, 7_68) __snake_case = (sample, timestep, encoder_hidden_status) try: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example) except Exception: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True) if pipe.requires_safety_checker: __snake_case = ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True) # compute __snake_case = 6_66 __snake_case = torch.Generator(device).manual_seed(seed) __snake_case = {'''generator''': generator} if args.steps is not None: __snake_case = args.steps with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa): __snake_case = pipe(prompt, **generate_kwargs).images[0] # save image image.save('''generated.png''')
348
0
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 A__ = """src/transformers""" # This is to make sure the transformers module imported is the one in the repo. A__ = direct_transformers_import(PATH_TO_TRANSFORMERS) A__ = 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)` A__ = re.compile(R"""\[(.+?)\]\((https://huggingface\.co/.+?)\)""") A__ = { """DecisionTransformerConfig""", """EncoderDecoderConfig""", """MusicgenConfig""", """RagConfig""", """SpeechEncoderDecoderConfig""", """TimmBackboneConfig""", """VisionEncoderDecoderConfig""", """VisionTextDualEncoderConfig""", """LlamaConfig""", } def _UpperCAmelCase ( snake_case ): """simple docstring""" _lowerCAmelCase = None # source code of `config_class` _lowerCAmelCase = inspect.getsource(snake_case ) _lowerCAmelCase = _re_checkpoint.findall(snake_case ) # 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 _UpperCAmelCase ( ): """simple docstring""" _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(snake_case ) _lowerCAmelCase = config_class.__name__ if checkpoint is None and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK: configs_without_checkpoint.append(snake_case ) if len(snake_case ) > 0: _lowerCAmelCase = """\n""".join(sorted(snake_case ) ) raise ValueError(F'The following configurations don\'t contain any valid checkpoint:\n{message}' ) if __name__ == "__main__": check_config_docstrings_have_checkpoints()
82
__snake_case = '''Input must be a string of 8 numbers plus letter''' __snake_case = '''TRWAGMYFPDXBNJZSQVHLCKE''' def lowerCAmelCase_ ( __lowerCAmelCase )-> bool: '''simple docstring''' if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCAmelCase : Optional[Any] =f'''Expected string as input, found {type(__lowerCAmelCase ).__name__}''' raise TypeError(__lowerCAmelCase ) UpperCAmelCase : List[Any] =spanish_id.replace('''-''' , '''''' ).upper() if len(__lowerCAmelCase ) != 9: raise ValueError(__lowerCAmelCase ) try: UpperCAmelCase : int =int(spanish_id_clean[0:8] ) UpperCAmelCase : Optional[int] =spanish_id_clean[8] except ValueError as ex: raise ValueError(__lowerCAmelCase ) from ex if letter.isdigit(): raise ValueError(__lowerCAmelCase ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
348
0
'''simple docstring''' import json import os import unittest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase__ ( lowercase , unittest.TestCase ): lowercase__ = MgpstrTokenizer lowercase__ = False lowercase__ = {} lowercase__ = False def UpperCamelCase_ ( self : List[str] ): '''simple docstring''' super().setUp() # fmt: off _UpperCamelCase : Optional[Any] = ['[GO]', '[s]', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] # fmt: on _UpperCamelCase : str = dict(zip(lowerCamelCase__ ,range(len(lowerCamelCase__ ) ) ) ) _UpperCamelCase : List[str] = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file ,'w' ,encoding='utf-8' ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + '\n' ) def UpperCamelCase_ ( self : int ,**lowerCamelCase__ : str ): '''simple docstring''' return MgpstrTokenizer.from_pretrained(self.tmpdirname ,**lowerCamelCase__ ) def UpperCamelCase_ ( self : str ,lowerCamelCase__ : Any ): '''simple docstring''' _UpperCamelCase : Any = 'tester' _UpperCamelCase : Optional[int] = 'tester' return input_text, output_text @unittest.skip('MGP-STR always lower cases letters.' ) def UpperCamelCase_ ( self : Union[str, Any] ): '''simple docstring''' pass def UpperCamelCase_ ( self : Optional[int] ): '''simple docstring''' _UpperCamelCase : List[Any] = self.get_tokenizers(do_lower_case=lowerCamelCase__ ) for tokenizer in tokenizers: with self.subTest(F'{tokenizer.__class__.__name__}' ): _UpperCamelCase : Union[str, Any] = '[SPECIAL_TOKEN]' tokenizer.add_special_tokens({'cls_token': special_token} ) _UpperCamelCase : List[Any] = tokenizer.encode([special_token] ,add_special_tokens=lowerCamelCase__ ) self.assertEqual(len(lowerCamelCase__ ) ,1 ) _UpperCamelCase : Any = tokenizer.decode(lowerCamelCase__ ,skip_special_tokens=lowerCamelCase__ ) self.assertTrue(special_token not in decoded ) def UpperCamelCase_ ( self : Any ): '''simple docstring''' _UpperCamelCase : Optional[int] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F'{tokenizer.__class__.__name__}' ): _UpperCamelCase , _UpperCamelCase : List[Any] = self.get_input_output_texts(lowerCamelCase__ ) _UpperCamelCase : List[str] = tokenizer.tokenize(lowerCamelCase__ ) _UpperCamelCase : str = tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) _UpperCamelCase : List[str] = tokenizer.encode(lowerCamelCase__ ,add_special_tokens=lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ ,lowerCamelCase__ ) _UpperCamelCase : Optional[Any] = tokenizer.convert_ids_to_tokens(lowerCamelCase__ ) self.assertNotEqual(len(lowerCamelCase__ ) ,0 ) _UpperCamelCase : Dict = tokenizer.decode(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ ,lowerCamelCase__ ) self.assertEqual(text_a.replace(' ' ,'' ) ,lowerCamelCase__ ) @unittest.skip('MGP-STR tokenizer only handles one sequence.' ) def UpperCamelCase_ ( self : Any ): '''simple docstring''' pass @unittest.skip('inputs cannot be pretokenized in MgpstrTokenizer' ) def UpperCamelCase_ ( self : Any ): '''simple docstring''' pass
83
def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Dict =str(bin(__lowerCAmelCase ) ) binary_number += "0" * shift_amount return binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Any =str(bin(__lowerCAmelCase ) )[2:] if shift_amount >= len(__lowerCAmelCase ): return "0b0" UpperCAmelCase : Optional[Any] =binary_number[: len(__lowerCAmelCase ) - shift_amount] return "0b" + shifted_binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number >= 0: # Get binary representation of positive number UpperCAmelCase : Optional[Any] ='''0''' + str(bin(__lowerCAmelCase ) ).strip('''-''' )[2:] else: # Get binary (2's complement) representation of negative number UpperCAmelCase : int =len(bin(__lowerCAmelCase )[3:] ) # Find 2's complement of number UpperCAmelCase : Any =bin(abs(__lowerCAmelCase ) - (1 << binary_number_length) )[3:] UpperCAmelCase : Optional[Any] =( '''1''' + '''0''' * (binary_number_length - len(__lowerCAmelCase )) + binary_number ) if shift_amount >= len(__lowerCAmelCase ): return "0b" + binary_number[0] * len(__lowerCAmelCase ) return ( "0b" + binary_number[0] * shift_amount + binary_number[: len(__lowerCAmelCase ) - shift_amount] ) if __name__ == "__main__": import doctest doctest.testmod()
348
0
"""simple docstring""" import operator as op __UpperCAmelCase = 'scaler.pt' __UpperCAmelCase = 'pytorch_model' __UpperCAmelCase = 'random_states' __UpperCAmelCase = 'optimizer' __UpperCAmelCase = 'scheduler' __UpperCAmelCase = 'pytorch_model.bin' __UpperCAmelCase = 'pytorch_model.bin.index.json' __UpperCAmelCase = 'model.safetensors' __UpperCAmelCase = 'model.safetensors.index.json' __UpperCAmelCase = '1.10.2' __UpperCAmelCase = 'py38' __UpperCAmelCase = '4.17.0' __UpperCAmelCase = ['ml.p3.16xlarge', 'ml.p3dn.24xlarge', 'ml.p4dn.24xlarge'] __UpperCAmelCase = ['FULL_SHARD', 'SHARD_GRAD_OP', 'NO_SHARD', 'HYBRID_SHARD', 'HYBRID_SHARD_ZERO2'] __UpperCAmelCase = ['TRANSFORMER_BASED_WRAP', 'SIZE_BASED_WRAP', 'NO_WRAP'] __UpperCAmelCase = ['BACKWARD_PRE', 'BACKWARD_POST', 'NO_PREFETCH'] __UpperCAmelCase = ['FULL_STATE_DICT', 'LOCAL_STATE_DICT', 'SHARDED_STATE_DICT'] __UpperCAmelCase = '2.0.1' __UpperCAmelCase = ['pdsh', 'standard', 'openmpi', 'mvapich'] __UpperCAmelCase = ['default', 'reduce-overhead', 'max-autotune'] __UpperCAmelCase = {'>': op.gt, '>=': op.ge, '==': op.eq, '!=': op.ne, '<=': op.le, '<': op.lt} # These are the args for `torch.distributed.launch` for pytorch < 1.9 __UpperCAmelCase = [ 'nnodes', 'nproc_per_node', 'rdzv_backend', 'rdzv_endpoint', 'rdzv_id', 'rdzv_conf', 'standalone', 'max_restarts', 'monitor_interval', 'start_method', 'role', 'module', 'm', 'no_python', 'run_path', 'log_dir', 'r', 'redirects', 't', 'tee', 'node_rank', 'master_addr', 'master_port', ] __UpperCAmelCase = ['DEEPSPEED', 'MULTI_GPU', 'FSDP', 'MEGATRON_LM'] __UpperCAmelCase = ['DEEPSPEED', 'MULTI_XPU', 'FSDP']
84
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) # TODO Update this __snake_case = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Tuple = """esm""" def __init__( self , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=1026 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__="absolute" , snake_case__=True , snake_case__=None , snake_case__=False , snake_case__=False , snake_case__=None , snake_case__=None , **snake_case__ , ) -> Union[str, Any]: '''simple docstring''' super().__init__(pad_token_id=snake_case__ , mask_token_id=snake_case__ , **snake_case__ ) UpperCAmelCase : List[str] =vocab_size UpperCAmelCase : str =hidden_size UpperCAmelCase : List[Any] =num_hidden_layers UpperCAmelCase : Optional[Any] =num_attention_heads UpperCAmelCase : str =intermediate_size UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : int =attention_probs_dropout_prob UpperCAmelCase : Dict =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : Union[str, Any] =layer_norm_eps UpperCAmelCase : Dict =position_embedding_type UpperCAmelCase : Optional[Any] =use_cache UpperCAmelCase : int =emb_layer_norm_before UpperCAmelCase : List[str] =token_dropout UpperCAmelCase : Optional[Any] =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) UpperCAmelCase : Optional[Any] =EsmFoldConfig() elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =EsmFoldConfig(**snake_case__ ) UpperCAmelCase : Tuple =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) UpperCAmelCase : Any =get_default_vocab_list() else: UpperCAmelCase : Tuple =vocab_list else: UpperCAmelCase : Optional[int] =None UpperCAmelCase : Union[str, Any] =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , snake_case__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =super().to_dict() if isinstance(self.esmfold_config , snake_case__ ): UpperCAmelCase : str =self.esmfold_config.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : str = None __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : float = 0 __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : int = 128 __lowerCamelCase : "TrunkConfig" = None def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' if self.trunk is None: UpperCAmelCase : str =TrunkConfig() elif isinstance(self.trunk , snake_case__ ): UpperCAmelCase : Optional[int] =TrunkConfig(**self.trunk ) def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[Any] =asdict(self ) UpperCAmelCase : Any =self.trunk.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 48 __lowerCamelCase : int = 1024 __lowerCamelCase : int = 128 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : float = 0 __lowerCamelCase : float = 0 __lowerCamelCase : bool = False __lowerCamelCase : int = 4 __lowerCamelCase : Optional[int] = 128 __lowerCamelCase : "StructureModuleConfig" = None def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' if self.structure_module is None: UpperCAmelCase : Any =StructureModuleConfig() elif isinstance(self.structure_module , snake_case__ ): UpperCAmelCase : str =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f'''`max_recycles` should be positive, got {self.max_recycles}.''' ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f''' {self.sequence_state_dim} and {self.sequence_state_dim}.''' ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f''' {self.pairwise_state_dim} and {self.pairwise_state_dim}.''' ) UpperCAmelCase : Optional[int] =self.sequence_state_dim // self.sequence_head_width UpperCAmelCase : Any =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f''' {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.''' ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f''' {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.''' ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f'''`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.''' ) if self.dropout >= 0.4: raise ValueError(f'''`dropout` should not be greater than 0.4, got {self.dropout}.''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =asdict(self ) UpperCAmelCase : Tuple =self.structure_module.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 384 __lowerCamelCase : int = 128 __lowerCamelCase : int = 16 __lowerCamelCase : int = 128 __lowerCamelCase : int = 12 __lowerCamelCase : int = 4 __lowerCamelCase : int = 8 __lowerCamelCase : float = 0.1 __lowerCamelCase : int = 8 __lowerCamelCase : int = 1 __lowerCamelCase : int = 2 __lowerCamelCase : int = 7 __lowerCamelCase : int = 10 __lowerCamelCase : float = 1E-8 __lowerCamelCase : float = 1E5 def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return asdict(self ) def lowerCAmelCase_ ( )-> Tuple: '''simple docstring''' return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
348
0
'''simple docstring''' import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartTokenizer, MBartTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin _SCREAMING_SNAKE_CASE : List[Any] = get_tests_dir("fixtures/test_sentencepiece.model") if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right _SCREAMING_SNAKE_CASE : int = 25_0004 _SCREAMING_SNAKE_CASE : List[str] = 25_0020 @require_sentencepiece @require_tokenizers class _snake_case ( lowercase_ , unittest.TestCase ): lowerCAmelCase_ : str = MBartTokenizer lowerCAmelCase_ : List[Any] = MBartTokenizerFast lowerCAmelCase_ : Tuple = True lowerCAmelCase_ : Any = True def lowerCAmelCase__ ( self ) -> List[str]: '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing snake_case_ = MBartTokenizer(a__ , keep_accents=a__ ) tokenizer.save_pretrained(self.tmpdirname ) def lowerCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' snake_case_ = MBartTokenizer(a__ , keep_accents=a__ ) snake_case_ = tokenizer.tokenize("This is a test" ) self.assertListEqual(a__ , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(a__ ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) snake_case_ = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( a__ , [ 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", "é", ".", ] , ) snake_case_ = tokenizer.convert_tokens_to_ids(a__ ) self.assertListEqual( a__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] , ) snake_case_ = tokenizer.convert_ids_to_tokens(a__ ) self.assertListEqual( a__ , [ 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 lowerCAmelCase__ ( self ) -> int: '''simple docstring''' if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return snake_case_ = (self.rust_tokenizer_class, "hf-internal-testing/tiny-random-mbart", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})' ): snake_case_ = self.rust_tokenizer_class.from_pretrained(a__ , **a__ ) snake_case_ = self.tokenizer_class.from_pretrained(a__ , **a__ ) snake_case_ = tempfile.mkdtemp() snake_case_ = tokenizer_r.save_pretrained(a__ ) snake_case_ = tokenizer_p.save_pretrained(a__ ) # 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 ) ) snake_case_ = tuple(f for f in tokenizer_r_files if "tokenizer.json" not in f ) self.assertSequenceEqual(a__ , a__ ) # Checks everything loads correctly in the same way snake_case_ = tokenizer_r.from_pretrained(a__ ) snake_case_ = tokenizer_p.from_pretrained(a__ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(a__ , a__ ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(a__ ) # Save tokenizer rust, legacy_format=True snake_case_ = tempfile.mkdtemp() snake_case_ = tokenizer_r.save_pretrained(a__ , legacy_format=a__ ) snake_case_ = tokenizer_p.save_pretrained(a__ ) # Checks it save with the same files self.assertSequenceEqual(a__ , a__ ) # Checks everything loads correctly in the same way snake_case_ = tokenizer_r.from_pretrained(a__ ) snake_case_ = tokenizer_p.from_pretrained(a__ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(a__ , a__ ) ) shutil.rmtree(a__ ) # Save tokenizer rust, legacy_format=False snake_case_ = tempfile.mkdtemp() snake_case_ = tokenizer_r.save_pretrained(a__ , legacy_format=a__ ) snake_case_ = tokenizer_p.save_pretrained(a__ ) # 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 snake_case_ = tokenizer_r.from_pretrained(a__ ) snake_case_ = tokenizer_p.from_pretrained(a__ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(a__ , a__ ) ) shutil.rmtree(a__ ) @require_torch @require_sentencepiece @require_tokenizers class _snake_case ( unittest.TestCase ): lowerCAmelCase_ : Optional[Any] = "facebook/mbart-large-en-ro" lowerCAmelCase_ : List[Any] = [ " UN Chief Says There Is No Military Solution in Syria", " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.", ] lowerCAmelCase_ : Any = [ "Şeful ONU declară că nu există o soluţie militară în Siria", "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei" " pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor" " face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.", ] lowerCAmelCase_ : Optional[Any] = [8274, 12_7873, 2_5916, 7, 8622, 2071, 438, 6_7485, 53, 18_7895, 23, 5_1712, 2, EN_CODE] @classmethod def lowerCAmelCase__ ( cls ) -> int: '''simple docstring''' snake_case_ = MBartTokenizer.from_pretrained( cls.checkpoint_name , src_lang="en_XX" , tgt_lang="ro_RO" ) snake_case_ = 1 return cls def lowerCAmelCase__ ( self ) -> Tuple: '''simple docstring''' self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ar_AR"] , 250_001 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["en_EN"] , 250_004 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ro_RO"] , 250_020 ) def lowerCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' snake_case_ = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , a__ ) def lowerCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' self.assertIn(a__ , self.tokenizer.all_special_ids ) snake_case_ = [RO_CODE, 884, 9_019, 96, 9, 916, 86_792, 36, 18_743, 15_596, 5, 2] snake_case_ = self.tokenizer.decode(a__ , skip_special_tokens=a__ ) snake_case_ = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=a__ ) self.assertEqual(a__ , a__ ) self.assertNotIn(self.tokenizer.eos_token , a__ ) def lowerCAmelCase__ ( self ) -> Dict: '''simple docstring''' snake_case_ = ["this is gunna be a long sentence " * 20] assert isinstance(src_text[0] , a__ ) snake_case_ = 10 snake_case_ = self.tokenizer(a__ , max_length=a__ , truncation=a__ ).input_ids[0] self.assertEqual(ids[-2] , 2 ) self.assertEqual(ids[-1] , a__ ) self.assertEqual(len(a__ ) , a__ ) def lowerCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["<mask>", "ar_AR"] ) , [250_026, 250_001] ) def lowerCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' snake_case_ = tempfile.mkdtemp() snake_case_ = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(a__ ) snake_case_ = MBartTokenizer.from_pretrained(a__ ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , a__ ) @require_torch def lowerCAmelCase__ ( self ) -> Any: '''simple docstring''' snake_case_ = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=a__ , return_tensors="pt" ) snake_case_ = shift_tokens_right(batch["labels"] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][-2:].tolist() == [2, EN_CODE] assert batch.decoder_input_ids[1][0].tolist() == RO_CODE assert batch.decoder_input_ids[1][-1] == 2 assert batch.labels[1][-2:].tolist() == [2, RO_CODE] @require_torch def lowerCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' snake_case_ = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=a__ , truncation=a__ , max_length=len(self.expected_src_tokens ) , return_tensors="pt" , ) snake_case_ = shift_tokens_right(batch["labels"] , self.tokenizer.pad_token_id ) self.assertIsInstance(a__ , a__ ) self.assertEqual((2, 14) , batch.input_ids.shape ) self.assertEqual((2, 14) , batch.attention_mask.shape ) snake_case_ = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , a__ ) self.assertEqual(2 , batch.decoder_input_ids[0, -1] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id, EN_CODE] ) def lowerCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' snake_case_ = self.tokenizer(self.src_text , padding=a__ , truncation=a__ , max_length=3 , return_tensors="pt" ) snake_case_ = self.tokenizer( text_target=self.tgt_text , padding=a__ , truncation=a__ , max_length=10 , return_tensors="pt" ) snake_case_ = targets["input_ids"] snake_case_ = shift_tokens_right(a__ , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def lowerCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' snake_case_ = self.tokenizer._build_translation_inputs( "A test" , return_tensors="pt" , src_lang="en_XX" , tgt_lang="ar_AR" ) self.assertEqual( nested_simplify(a__ ) , { # A, test, EOS, en_XX "input_ids": [[62, 3_034, 2, 250_004]], "attention_mask": [[1, 1, 1, 1]], # ar_AR "forced_bos_token_id": 250_001, } , )
85
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[int] = (KDPMaDiscreteScheduler,) __lowerCamelCase : List[str] = 10 def UpperCAmelCase__ ( self , **snake_case__ ) -> str: '''simple docstring''' UpperCAmelCase : int ={ '''num_train_timesteps''': 1100, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', } config.update(**snake_case__ ) return config def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=snake_case__ , beta_end=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' UpperCAmelCase : Optional[Any] =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config(prediction_type='''v_prediction''' ) UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : str =self.dummy_model() UpperCAmelCase : Optional[Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : Union[str, Any] =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : str =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Any =model(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : int =output.prev_sample UpperCAmelCase : Dict =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Optional[Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.69_34e-07 ) < 1e-2 assert abs(result_mean.item() - 6.11_12e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.6_93_42_86_50_17_09_72e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0002 ) < 1e-3 def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : Any =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config() UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : Optional[int] =self.dummy_model() UpperCAmelCase : Union[str, Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : str =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : Dict =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =model(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =output.prev_sample UpperCAmelCase : Any =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Union[str, Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : List[Any] =self.scheduler_classes[0] UpperCAmelCase : Dict =self.get_scheduler_config() UpperCAmelCase : List[str] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps , device=snake_case__ ) UpperCAmelCase : int =self.dummy_model() UpperCAmelCase : Tuple =self.dummy_sample_deter.to(snake_case__ ) * scheduler.init_noise_sigma for t in scheduler.timesteps: UpperCAmelCase : Optional[Any] =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : int =model(snake_case__ , snake_case__ ) UpperCAmelCase : str =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =output.prev_sample UpperCAmelCase : List[str] =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Dict =torch.mean(torch.abs(snake_case__ ) ) if str(snake_case__ ).startswith('''cpu''' ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3
348
0
"""simple docstring""" import os import shutil from pathlib import Path from typing import Optional, Union import numpy as np from huggingface_hub import hf_hub_download from ..utils import ONNX_EXTERNAL_WEIGHTS_NAME, ONNX_WEIGHTS_NAME, is_onnx_available, logging if is_onnx_available(): import onnxruntime as ort lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """tensor(bool)""": np.bool_, """tensor(int8)""": np.inta, """tensor(uint8)""": np.uinta, """tensor(int16)""": np.intaa, """tensor(uint16)""": np.uintaa, """tensor(int32)""": np.intaa, """tensor(uint32)""": np.uintaa, """tensor(int64)""": np.intaa, """tensor(uint64)""": np.uintaa, """tensor(float16)""": np.floataa, """tensor(float)""": np.floataa, """tensor(double)""": np.floataa, } class A__ : def __init__( self , _SCREAMING_SNAKE_CASE=None , **_SCREAMING_SNAKE_CASE ): logger.info('`diffusers.OnnxRuntimeModel` is experimental and might change in the future.' ) __lowerCAmelCase : Optional[int] = model __lowerCAmelCase : List[str] = kwargs.get('model_save_dir' , _SCREAMING_SNAKE_CASE ) __lowerCAmelCase : str = kwargs.get('latest_model_name' , _SCREAMING_SNAKE_CASE ) def __call__( self , **_SCREAMING_SNAKE_CASE ): __lowerCAmelCase : int = {k: np.array(_SCREAMING_SNAKE_CASE ) for k, v in kwargs.items()} return self.model.run(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) @staticmethod def __lowerCamelCase ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None ): if provider is None: logger.info('No onnxruntime provider specified, using CPUExecutionProvider' ) __lowerCAmelCase : Any = 'CPUExecutionProvider' return ort.InferenceSession(_SCREAMING_SNAKE_CASE , providers=[provider] , sess_options=_SCREAMING_SNAKE_CASE ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Union[str, Any] = file_name if file_name is not None else ONNX_WEIGHTS_NAME __lowerCAmelCase : Any = self.model_save_dir.joinpath(self.latest_model_name ) __lowerCAmelCase : Union[str, Any] = Path(_SCREAMING_SNAKE_CASE ).joinpath(_SCREAMING_SNAKE_CASE ) try: shutil.copyfile(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) except shutil.SameFileError: pass # copy external weights (for models >2GB) __lowerCAmelCase : int = self.model_save_dir.joinpath(_SCREAMING_SNAKE_CASE ) if src_path.exists(): __lowerCAmelCase : Tuple = Path(_SCREAMING_SNAKE_CASE ).joinpath(_SCREAMING_SNAKE_CASE ) try: shutil.copyfile(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) except shutil.SameFileError: pass def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ): if os.path.isfile(_SCREAMING_SNAKE_CASE ): logger.error(f"Provided path ({save_directory}) should be a directory, not a file" ) return os.makedirs(_SCREAMING_SNAKE_CASE , exist_ok=_SCREAMING_SNAKE_CASE ) # saving model weights/files self._save_pretrained(_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) @classmethod def __lowerCamelCase ( cls , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ): __lowerCAmelCase : Union[str, Any] = file_name if file_name is not None else ONNX_WEIGHTS_NAME # load model from local directory if os.path.isdir(_SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Dict = OnnxRuntimeModel.load_model( os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , provider=_SCREAMING_SNAKE_CASE , sess_options=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : int = Path(_SCREAMING_SNAKE_CASE ) # load model from hub else: # download model __lowerCAmelCase : int = hf_hub_download( repo_id=_SCREAMING_SNAKE_CASE , filename=_SCREAMING_SNAKE_CASE , use_auth_token=_SCREAMING_SNAKE_CASE , revision=_SCREAMING_SNAKE_CASE , cache_dir=_SCREAMING_SNAKE_CASE , force_download=_SCREAMING_SNAKE_CASE , ) __lowerCAmelCase : Tuple = Path(_SCREAMING_SNAKE_CASE ).parent __lowerCAmelCase : Tuple = Path(_SCREAMING_SNAKE_CASE ).name __lowerCAmelCase : Optional[int] = OnnxRuntimeModel.load_model(_SCREAMING_SNAKE_CASE , provider=_SCREAMING_SNAKE_CASE , sess_options=_SCREAMING_SNAKE_CASE ) return cls(model=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) @classmethod def __lowerCamelCase ( cls , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ): __lowerCAmelCase : int = None if len(str(_SCREAMING_SNAKE_CASE ).split('@' ) ) == 2: __lowerCAmelCase , __lowerCAmelCase : Any = model_id.split('@' ) return cls._from_pretrained( model_id=_SCREAMING_SNAKE_CASE , revision=_SCREAMING_SNAKE_CASE , cache_dir=_SCREAMING_SNAKE_CASE , force_download=_SCREAMING_SNAKE_CASE , use_auth_token=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , )
86
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class __snake_case ( unittest.TestCase ): @slow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Any =FlaxMTaForConditionalGeneration.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : Tuple =AutoTokenizer.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : List[str] =tokenizer('''Hello there''' , return_tensors='''np''' ).input_ids UpperCAmelCase : List[Any] =tokenizer('''Hi I am''' , return_tensors='''np''' ).input_ids UpperCAmelCase : Union[str, Any] =shift_tokens_right(snake_case__ , model.config.pad_token_id , model.config.decoder_start_token_id ) UpperCAmelCase : List[str] =model(snake_case__ , decoder_input_ids=snake_case__ ).logits UpperCAmelCase : Any =optax.softmax_cross_entropy(snake_case__ , onehot(snake_case__ , logits.shape[-1] ) ).mean() UpperCAmelCase : Union[str, Any] =-(labels.shape[-1] * loss.item()) UpperCAmelCase : List[str] =-84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
348
0
from __future__ import annotations import unittest from transformers import BlenderbotSmallConfig, BlenderbotSmallTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel @require_tf class snake_case_ : __A : List[str] = BlenderbotSmallConfig __A : Union[str, Any] = {} __A : Union[str, Any] = "gelu" def __init__( self : List[str] , lowercase_ : Union[str, Any] , lowercase_ : Optional[int]=13 , lowercase_ : Union[str, Any]=7 , lowercase_ : str=True , lowercase_ : Optional[Any]=False , lowercase_ : Dict=99 , lowercase_ : Optional[Any]=32 , lowercase_ : Union[str, Any]=2 , lowercase_ : Optional[Any]=4 , lowercase_ : List[str]=37 , lowercase_ : Any=0.1 , lowercase_ : List[Any]=0.1 , lowercase_ : Union[str, Any]=20 , lowercase_ : Optional[Any]=2 , lowercase_ : int=1 , lowercase_ : Union[str, Any]=0 , ) -> List[str]: lowercase__ : int = parent lowercase__ : Dict = batch_size lowercase__ : List[str] = seq_length lowercase__ : List[Any] = is_training lowercase__ : Any = use_labels lowercase__ : Dict = vocab_size lowercase__ : Dict = hidden_size lowercase__ : List[Any] = num_hidden_layers lowercase__ : Optional[Any] = num_attention_heads lowercase__ : Optional[Any] = intermediate_size lowercase__ : Optional[int] = hidden_dropout_prob lowercase__ : List[str] = attention_probs_dropout_prob lowercase__ : int = max_position_embeddings lowercase__ : Tuple = eos_token_id lowercase__ : Optional[int] = pad_token_id lowercase__ : Tuple = bos_token_id def __UpperCamelCase ( self : Any ) -> Tuple: lowercase__ : Dict = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) lowercase__ : Optional[Any] = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) lowercase__ : Optional[int] = tf.concat([input_ids, eos_tensor] , axis=1 ) lowercase__ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowercase__ : Optional[int] = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) lowercase__ : Optional[Any] = prepare_blenderbot_small_inputs_dict(lowercase_ , lowercase_ , lowercase_ ) return config, inputs_dict def __UpperCamelCase ( self : Tuple , lowercase_ : int , lowercase_ : str ) -> Any: lowercase__ : Dict = TFBlenderbotSmallModel(config=lowercase_ ).get_decoder() lowercase__ : Union[str, Any] = inputs_dict["input_ids"] lowercase__ : str = input_ids[:1, :] lowercase__ : Any = inputs_dict["attention_mask"][:1, :] lowercase__ : Union[str, Any] = inputs_dict["head_mask"] lowercase__ : Optional[int] = 1 # first forward pass lowercase__ : Dict = model(lowercase_ , attention_mask=lowercase_ , head_mask=lowercase_ , use_cache=lowercase_ ) lowercase__ , lowercase__ : Dict = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids lowercase__ : Dict = ids_tensor((self.batch_size, 3) , config.vocab_size ) lowercase__ : Any = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and lowercase__ : Union[str, Any] = tf.concat([input_ids, next_tokens] , axis=-1 ) lowercase__ : Any = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) lowercase__ : List[Any] = model(lowercase_ , attention_mask=lowercase_ )[0] lowercase__ : str = model(lowercase_ , attention_mask=lowercase_ , past_key_values=lowercase_ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice lowercase__ : List[Any] = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) lowercase__ : str = output_from_no_past[:, -3:, random_slice_idx] lowercase__ : List[str] = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(lowercase_ , lowercase_ , rtol=1E-3 ) def lowercase_ ( _lowerCamelCase : Tuple , _lowerCamelCase : int , _lowerCamelCase : List[str] , _lowerCamelCase : Any=None , _lowerCamelCase : Optional[int]=None , _lowerCamelCase : Optional[int]=None , _lowerCamelCase : Any=None , _lowerCamelCase : Union[str, Any]=None , ): if attention_mask is None: lowercase__ : Optional[int] = tf.cast(tf.math.not_equal(_lowerCamelCase , config.pad_token_id) , tf.inta) if decoder_attention_mask is None: lowercase__ : List[Any] = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id) , tf.inta), ] , axis=-1 , ) if head_mask is None: lowercase__ : int = tf.ones((config.encoder_layers, config.encoder_attention_heads)) if decoder_head_mask is None: lowercase__ : Union[str, Any] = tf.ones((config.decoder_layers, config.decoder_attention_heads)) if cross_attn_head_mask is None: lowercase__ : str = tf.ones((config.decoder_layers, config.decoder_attention_heads)) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class snake_case_ ( __A ,__A ,unittest.TestCase ): __A : Optional[int] = ( (TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel) if is_tf_available() else () ) __A : List[Any] = (TFBlenderbotSmallForConditionalGeneration,) if is_tf_available() else () __A : str = ( { "conversational": TFBlenderbotSmallForConditionalGeneration, "feature-extraction": TFBlenderbotSmallModel, "summarization": TFBlenderbotSmallForConditionalGeneration, "text2text-generation": TFBlenderbotSmallForConditionalGeneration, "translation": TFBlenderbotSmallForConditionalGeneration, } if is_tf_available() else {} ) __A : Any = True __A : Tuple = False __A : Union[str, Any] = False def __UpperCamelCase ( self : Dict ) -> List[Any]: lowercase__ : List[str] = TFBlenderbotSmallModelTester(self ) lowercase__ : Optional[int] = ConfigTester(self , config_class=lowercase_ ) def __UpperCamelCase ( self : str ) -> Tuple: self.config_tester.run_common_tests() def __UpperCamelCase ( self : Tuple ) -> Any: lowercase__ : int = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*lowercase_ ) @require_tokenizers @require_tf class snake_case_ ( unittest.TestCase ): __A : Union[str, Any] = [ "Social anxiety\nWow, I am never shy. Do you have anxiety?\nYes. I end up sweating and blushing and feel like " " i'm going to throw up.\nand why is that?" ] __A : str = "facebook/blenderbot_small-90M" @cached_property def __UpperCamelCase ( self : Optional[int] ) -> Optional[Any]: # use "old" tokenizer here because of bug when downloading new tokenizer return BlenderbotSmallTokenizer.from_pretrained("facebook/blenderbot-90M" ) @cached_property def __UpperCamelCase ( self : str ) -> Optional[int]: lowercase__ : Optional[Any] = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def __UpperCamelCase ( self : Any ) -> int: lowercase__ : Tuple = self.tokenizer(self.src_text , return_tensors="tf" ) lowercase__ : List[str] = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 , use_cache=lowercase_ , ) lowercase__ : str = self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=lowercase_ )[0] assert generated_words in ( "i don't know. i just feel like i'm going to throw up. it's not fun.", "i'm not sure. i just feel like i've been feeling like i have to be in a certain place", "i'm not sure. i just feel like i've been in a bad situation.", )
87
import unittest import numpy as np from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __snake_case ( lowerCamelCase__ , unittest.TestCase ): # FIXME: add fast tests pass @nightly @require_onnxruntime @require_torch_gpu class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : List[Any] =ort.SessionOptions() UpperCAmelCase : Optional[int] =False return options def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : int =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Optional[Any] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Dict ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : Any =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=10 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : Optional[int] =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Tuple =np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Tuple =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , subfolder='''scheduler''' , revision='''onnx''' ) UpperCAmelCase : int =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , scheduler=snake_case__ , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Union[str, Any] ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : str =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=20 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : int =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Union[str, Any] =np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3
348
0
def a__ ( A_ ): '''simple docstring''' if not isinstance(A_, A_ ): raise ValueError("""Input series is not valid, valid series - [2, 4, 6]""" ) if len(A_ ) == 0: raise ValueError("""Input list must be a non empty list""" ) if len(A_ ) == 1: return True __magic_name__ = series[1] - series[0] for index in range(len(A_ ) - 1 ): if series[index + 1] - series[index] != common_diff: return False return True def a__ ( A_ ): '''simple docstring''' if not isinstance(A_, A_ ): raise ValueError("""Input series is not valid, valid series - [2, 4, 6]""" ) if len(A_ ) == 0: raise ValueError("""Input list must be a non empty list""" ) __magic_name__ = 0 for val in series: answer += val return answer / len(A_ ) if __name__ == "__main__": import doctest doctest.testmod()
88
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def lowerCAmelCase_ ( )-> int: '''simple docstring''' UpperCAmelCase : str ={ '''repo_name''': ['''test_repo1''', '''test_repo2''', '''test_repo3'''], '''path''': ['''test_1.py''', '''test_2.py''', '''unit_test.py'''], '''content''': ['''a ''' * 20, '''a ''' * 30, '''b ''' * 7], } UpperCAmelCase : Union[str, Any] =Dataset.from_dict(__lowerCAmelCase ) return dataset class __snake_case ( lowerCamelCase__ ): def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[str] =get_dataset() UpperCAmelCase : Optional[int] =make_duplicate_clusters(snake_case__ , 0.85 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : str =get_dataset() UpperCAmelCase , UpperCAmelCase : Tuple =deduplicate_dataset(snake_case__ ) self.assertEqual(len(snake_case__ ) , 2 ) print(snake_case__ ) self.assertEqual(duplicate_clusters[0][0]['''copies'''] , 2 ) self.assertEqual(duplicate_clusters[0][0]['''is_extreme'''] , snake_case__ )
348
0
'''simple docstring''' import string def __lowerCamelCase ( lowerCAmelCase_ ) -> None: for key in range(len(string.ascii_uppercase ) ): _a : Union[str, Any] = '' for symbol in message: if symbol in string.ascii_uppercase: _a : Optional[Any] = string.ascii_uppercase.find(lowerCAmelCase_ ) _a : List[str] = num - key if num < 0: _a : str = num + len(string.ascii_uppercase ) _a : int = translated + string.ascii_uppercase[num] else: _a : Dict = translated + symbol print(f"""Decryption using Key #{key}: {translated}""" ) def __lowerCamelCase ( ) -> None: _a : int = input('Encrypted message: ' ) _a : Tuple = message.upper() decrypt(lowerCAmelCase_ ) if __name__ == "__main__": import doctest doctest.testmod() main()
89
from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput __snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name class __snake_case ( lowerCamelCase__ , lowerCamelCase__ ): @register_to_config def __init__( self , snake_case__ , snake_case__ = None , snake_case__ = None ) -> str: '''simple docstring''' super().__init__() UpperCAmelCase : Optional[Any] =learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" UpperCAmelCase : Any =torch.zeros(snake_case__ , snake_case__ ) else: UpperCAmelCase : Union[str, Any] =None UpperCAmelCase : Optional[int] =torch.nn.Parameter(snake_case__ ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : VQModel __lowerCamelCase : CLIPTextModel __lowerCamelCase : CLIPTokenizer __lowerCamelCase : TransformeraDModel __lowerCamelCase : LearnedClassifierFreeSamplingEmbeddings __lowerCamelCase : VQDiffusionScheduler def __init__( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> int: '''simple docstring''' super().__init__() self.register_modules( vqvae=snake_case__ , transformer=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , scheduler=snake_case__ , learned_classifier_free_sampling_embeddings=snake_case__ , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : int =len(snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else 1 # get prompt text embeddings UpperCAmelCase : Optional[int] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) UpperCAmelCase : int =text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCAmelCase : List[str] =self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' f''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) UpperCAmelCase : Optional[Any] =text_input_ids[:, : self.tokenizer.model_max_length] UpperCAmelCase : List[Any] =self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 UpperCAmelCase : int =prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate text embeddings for each generation per prompt UpperCAmelCase : int =prompt_embeds.repeat_interleave(snake_case__ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: UpperCAmelCase : Optional[int] =self.learned_classifier_free_sampling_embeddings.embeddings UpperCAmelCase : str =negative_prompt_embeds.unsqueeze(0 ).repeat(snake_case__ , 1 , 1 ) else: UpperCAmelCase : str =[''''''] * batch_size UpperCAmelCase : Tuple =text_input_ids.shape[-1] UpperCAmelCase : Optional[Any] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=snake_case__ , truncation=snake_case__ , return_tensors='''pt''' , ) UpperCAmelCase : Optional[Any] =self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings UpperCAmelCase : Optional[int] =negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCAmelCase : Optional[Any] =negative_prompt_embeds.shape[1] UpperCAmelCase : Union[str, Any] =negative_prompt_embeds.repeat(1 , snake_case__ , 1 ) UpperCAmelCase : Optional[Any] =negative_prompt_embeds.view(batch_size * num_images_per_prompt , snake_case__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCAmelCase : int =torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , snake_case__ , snake_case__ = 100 , snake_case__ = 5.0 , snake_case__ = 1.0 , snake_case__ = 1 , snake_case__ = None , snake_case__ = None , snake_case__ = "pil" , snake_case__ = True , snake_case__ = None , snake_case__ = 1 , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =1 elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Tuple =len(snake_case__ ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(snake_case__ )}''' ) UpperCAmelCase : Tuple =batch_size * num_images_per_prompt UpperCAmelCase : List[str] =guidance_scale > 1.0 UpperCAmelCase : List[Any] =self._encode_prompt(snake_case__ , snake_case__ , snake_case__ ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case__ , snake_case__ ) or callback_steps <= 0) ): raise ValueError( f'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' f''' {type(snake_case__ )}.''' ) # get the initial completely masked latents unless the user supplied it UpperCAmelCase : int =(batch_size, self.transformer.num_latent_pixels) if latents is None: UpperCAmelCase : Union[str, Any] =self.transformer.num_vector_embeds - 1 UpperCAmelCase : str =torch.full(snake_case__ , snake_case__ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( '''Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,''' f''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) UpperCAmelCase : Any =latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(snake_case__ , device=self.device ) UpperCAmelCase : Any =self.scheduler.timesteps.to(self.device ) UpperCAmelCase : Optional[int] =latents for i, t in enumerate(self.progress_bar(snake_case__ ) ): # expand the sample if we are doing classifier free guidance UpperCAmelCase : Optional[Any] =torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` UpperCAmelCase : Optional[int] =self.transformer(snake_case__ , encoder_hidden_states=snake_case__ , timestep=snake_case__ ).sample if do_classifier_free_guidance: UpperCAmelCase , UpperCAmelCase : str =model_output.chunk(2 ) UpperCAmelCase : Optional[int] =model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(snake_case__ , dim=1 , keepdim=snake_case__ ) UpperCAmelCase : Tuple =self.truncate(snake_case__ , snake_case__ ) # remove `log(0)`'s (`-inf`s) UpperCAmelCase : Optional[Any] =model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 UpperCAmelCase : int =self.scheduler.step(snake_case__ , timestep=snake_case__ , sample=snake_case__ , generator=snake_case__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =self.vqvae.config.vq_embed_dim UpperCAmelCase : Optional[Any] =(batch_size, self.transformer.height, self.transformer.width, embedding_channels) UpperCAmelCase : Dict =self.vqvae.quantize.get_codebook_entry(snake_case__ , shape=snake_case__ ) UpperCAmelCase : Tuple =self.vqvae.decode(snake_case__ , force_not_quantize=snake_case__ ).sample UpperCAmelCase : Union[str, Any] =(image / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase : Any =image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase : List[str] =self.numpy_to_pil(snake_case__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> torch.FloatTensor: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : int =torch.sort(snake_case__ , 1 , descending=snake_case__ ) UpperCAmelCase : Union[str, Any] =torch.exp(snake_case__ ) UpperCAmelCase : Union[str, Any] =sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out UpperCAmelCase : Optional[Any] =torch.full_like(keep_mask[:, 0:1, :] , snake_case__ ) UpperCAmelCase : Tuple =torch.cat((all_true, keep_mask) , dim=1 ) UpperCAmelCase : int =keep_mask[:, :-1, :] UpperCAmelCase : int =keep_mask.gather(1 , indices.argsort(1 ) ) UpperCAmelCase : Dict =log_p_x_0.clone() UpperCAmelCase : List[Any] =-torch.inf # -inf = log(0) return rv
348
0
import math import qiskit def lowerCamelCase_ ( UpperCamelCase__ : int = 1 , UpperCamelCase__ : int = 1 , UpperCamelCase__ : int = 1 ) -> qiskit.result.counts.Counts: """simple docstring""" if ( isinstance(UpperCamelCase__ , UpperCamelCase__ ) or isinstance(UpperCamelCase__ , UpperCamelCase__ ) or isinstance(UpperCamelCase__ , UpperCamelCase__ ) ): raise TypeError('inputs must be integers.' ) if (input_a < 0) or (input_a < 0) or (carry_in < 0): raise ValueError('inputs must be positive.' ) if ( (math.floor(UpperCamelCase__ ) != input_a) or (math.floor(UpperCamelCase__ ) != input_a) or (math.floor(UpperCamelCase__ ) != carry_in) ): raise ValueError('inputs must be exact integers.' ) if (input_a > 2) or (input_a > 2) or (carry_in > 2): raise ValueError('inputs must be less or equal to 2.' ) # build registers __lowerCamelCase = qiskit.QuantumRegister(4 , 'qr' ) __lowerCamelCase = qiskit.ClassicalRegister(2 , 'cr' ) # list the entries __lowerCamelCase = [input_a, input_a, carry_in] __lowerCamelCase = qiskit.QuantumCircuit(UpperCamelCase__ , UpperCamelCase__ ) for i in range(0 , 3 ): if entry[i] == 2: quantum_circuit.h(UpperCamelCase__ ) # for hadamard entries elif entry[i] == 1: quantum_circuit.x(UpperCamelCase__ ) # for 1 entries elif entry[i] == 0: quantum_circuit.i(UpperCamelCase__ ) # for 0 entries # build the circuit quantum_circuit.ccx(0 , 1 , 3 ) # ccx = toffoli gate quantum_circuit.cx(0 , 1 ) quantum_circuit.ccx(1 , 2 , 3 ) quantum_circuit.cx(1 , 2 ) quantum_circuit.cx(0 , 1 ) quantum_circuit.measure([2, 3] , UpperCamelCase__ ) # measure the last two qbits __lowerCamelCase = qiskit.Aer.get_backend('aer_simulator' ) __lowerCamelCase = qiskit.execute(UpperCamelCase__ , UpperCamelCase__ , shots=1000 ) return job.result().get_counts(UpperCamelCase__ ) if __name__ == "__main__": print(f'''Total sum count for state is: {quantum_full_adder(1, 1, 1)}''')
90
import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase : Any =UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =self.dummy_uncond_unet UpperCAmelCase : Optional[int] =KarrasVeScheduler() UpperCAmelCase : List[Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : List[str] =torch.manual_seed(0 ) UpperCAmelCase : List[str] =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : str =torch.manual_seed(0 ) UpperCAmelCase : str =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' , return_dict=snake_case__ )[0] UpperCAmelCase : Any =image[0, -3:, -3:, -1] UpperCAmelCase : List[str] =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCAmelCase : int =np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class __snake_case ( unittest.TestCase ): def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Tuple ='''google/ncsnpp-celebahq-256''' UpperCAmelCase : int =UNetaDModel.from_pretrained(snake_case__ ) UpperCAmelCase : Dict =KarrasVeScheduler() UpperCAmelCase : Union[str, Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Any =torch.manual_seed(0 ) UpperCAmelCase : Tuple =pipe(num_inference_steps=20 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : Optional[int] =image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) UpperCAmelCase : Tuple =np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
348
0
"""simple docstring""" from .configuration_bert_masked import MaskedBertConfig from .modeling_bert_masked import ( MaskedBertForMultipleChoice, MaskedBertForQuestionAnswering, MaskedBertForSequenceClassification, MaskedBertForTokenClassification, MaskedBertModel, ) from .modules import *
91
import qiskit def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> qiskit.result.counts.Counts: '''simple docstring''' UpperCAmelCase : Union[str, Any] =qiskit.Aer.get_backend('''aer_simulator''' ) UpperCAmelCase : List[str] =qiskit.QuantumCircuit(4 , 2 ) # encode inputs in qubits 0 and 1 if bita == 1: qc_ha.x(0 ) if bita == 1: qc_ha.x(1 ) qc_ha.barrier() # use cnots to write XOR of the inputs on qubit2 qc_ha.cx(0 , 2 ) qc_ha.cx(1 , 2 ) # use ccx / toffoli gate to write AND of the inputs on qubit3 qc_ha.ccx(0 , 1 , 3 ) qc_ha.barrier() # extract outputs qc_ha.measure(2 , 0 ) # extract XOR value qc_ha.measure(3 , 1 ) # extract AND value # Execute the circuit on the qasm simulator UpperCAmelCase : Dict =qiskit.execute(__lowerCAmelCase , __lowerCAmelCase , shots=10_00 ) # Return the histogram data of the results of the experiment return job.result().get_counts(__lowerCAmelCase ) if __name__ == "__main__": __snake_case = half_adder(1, 1) print(f'Half Adder Output Qubit Counts: {counts}')
348
0
# 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 UpperCamelCase__ = get_logger() UpperCamelCase__ = None class a__ ( TensorFormatter[Mapping, """jax.Array""", Mapping] ): def __init__( self , _A=None , _A=None , **_A ): """simple docstring""" super().__init__(features=_A ) import jax from jaxlib.xla_client import Device if isinstance(_A , _A ): raise ValueError( f"""Expected {device} to be a `str` not {type(_A )}, 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(_A , _A ) 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 __SCREAMING_SNAKE_CASE( ): """simple docstring""" import jax return {str(_A ): device for device in jax.devices()} def __SCREAMING_SNAKE_CASE( self , _A ): """simple docstring""" import jax import jax.numpy as jnp if isinstance(_A , _A ) and column: if all( isinstance(_A , jax.Array ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column ): return jnp.stack(_A , axis=0 ) return column def __SCREAMING_SNAKE_CASE( self , _A ): """simple docstring""" import jax import jax.numpy as jnp if isinstance(_A , (str, bytes, type(_A )) ): return value elif isinstance(_A , (np.character, np.ndarray) ) and np.issubdtype(value.dtype , np.character ): return value.tolist() __lowerCAmelCase = {} if isinstance(_A , (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(_A , (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(_A , PIL.Image.Image ): __lowerCAmelCase = np.asarray(_A ) # 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(_A , **{**default_dtype, **self.jnp_array_kwargs} ) def __SCREAMING_SNAKE_CASE( self , _A ): """simple docstring""" import jax # support for torch, tf, jax etc. if config.TORCH_AVAILABLE and "torch" in sys.modules: import torch if isinstance(_A , torch.Tensor ): return self._tensorize(data_struct.detach().cpu().numpy()[()] ) if hasattr(_A , "__array__" ) and not isinstance(_A , jax.Array ): __lowerCAmelCase = data_struct.__array__() # support for nested types like struct of list of struct if isinstance(_A , np.ndarray ): if data_struct.dtype == object: # jax arrays cannot be instantied from an array of objects return self._consolidate([self.recursive_tensorize(_A ) for substruct in data_struct] ) elif isinstance(_A , (list, tuple) ): return self._consolidate([self.recursive_tensorize(_A ) for substruct in data_struct] ) return self._tensorize(_A ) def __SCREAMING_SNAKE_CASE( self , _A ): """simple docstring""" return map_nested(self._recursive_tensorize , _A , map_list=_A ) def __SCREAMING_SNAKE_CASE( self , _A ): """simple docstring""" __lowerCAmelCase = self.numpy_arrow_extractor().extract_row(_A ) __lowerCAmelCase = self.python_features_decoder.decode_row(_A ) return self.recursive_tensorize(_A ) def __SCREAMING_SNAKE_CASE( self , _A ): """simple docstring""" __lowerCAmelCase = self.numpy_arrow_extractor().extract_column(_A ) __lowerCAmelCase = self.python_features_decoder.decode_column(_A , pa_table.column_names[0] ) __lowerCAmelCase = self.recursive_tensorize(_A ) __lowerCAmelCase = self._consolidate(_A ) return column def __SCREAMING_SNAKE_CASE( self , _A ): """simple docstring""" __lowerCAmelCase = self.numpy_arrow_extractor().extract_batch(_A ) __lowerCAmelCase = self.python_features_decoder.decode_batch(_A ) __lowerCAmelCase = self.recursive_tensorize(_A ) for column_name in batch: __lowerCAmelCase = self._consolidate(batch[column_name] ) return batch
92
from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class __snake_case : __lowerCamelCase : str = BlenderbotConfig __lowerCamelCase : Optional[Any] = {} __lowerCamelCase : Optional[int] = """gelu""" def __init__( self , snake_case__ , snake_case__=13 , snake_case__=7 , snake_case__=True , snake_case__=False , snake_case__=99 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=20 , snake_case__=2 , snake_case__=1 , snake_case__=0 , ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : Dict =seq_length UpperCAmelCase : Optional[Any] =is_training UpperCAmelCase : List[str] =use_labels UpperCAmelCase : List[Any] =vocab_size UpperCAmelCase : Optional[int] =hidden_size UpperCAmelCase : Tuple =num_hidden_layers UpperCAmelCase : Any =num_attention_heads UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : str =hidden_dropout_prob UpperCAmelCase : Optional[int] =attention_probs_dropout_prob UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : List[Any] =eos_token_id UpperCAmelCase : Optional[int] =pad_token_id UpperCAmelCase : Tuple =bos_token_id def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[Any] =ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) UpperCAmelCase : List[Any] =tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) UpperCAmelCase : Tuple =tf.concat([input_ids, eos_tensor] , axis=1 ) UpperCAmelCase : str =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : Optional[Any] =self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) UpperCAmelCase : List[str] =prepare_blenderbot_inputs_dict(snake_case__ , snake_case__ , snake_case__ ) return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =TFBlenderbotModel(config=snake_case__ ).get_decoder() UpperCAmelCase : Any =inputs_dict['''input_ids'''] UpperCAmelCase : str =input_ids[:1, :] UpperCAmelCase : Tuple =inputs_dict['''attention_mask'''][:1, :] UpperCAmelCase : Tuple =inputs_dict['''head_mask'''] UpperCAmelCase : List[Any] =1 # first forward pass UpperCAmelCase : List[str] =model(snake_case__ , attention_mask=snake_case__ , head_mask=snake_case__ , use_cache=snake_case__ ) UpperCAmelCase , UpperCAmelCase : str =outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase : Union[str, Any] =ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCAmelCase : List[Any] =tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and UpperCAmelCase : Tuple =tf.concat([input_ids, next_tokens] , axis=-1 ) UpperCAmelCase : int =tf.concat([attention_mask, next_attn_mask] , axis=-1 ) UpperCAmelCase : Optional[int] =model(snake_case__ , attention_mask=snake_case__ )[0] UpperCAmelCase : str =model(snake_case__ , attention_mask=snake_case__ , past_key_values=snake_case__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice UpperCAmelCase : List[Any] =int(ids_tensor((1,) , output_from_past.shape[-1] ) ) UpperCAmelCase : List[Any] =output_from_no_past[:, -3:, random_slice_idx] UpperCAmelCase : Dict =output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(snake_case__ , snake_case__ , rtol=1e-3 ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , )-> str: '''simple docstring''' if attention_mask is None: UpperCAmelCase : int =tf.cast(tf.math.not_equal(__lowerCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase : Tuple =tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase : str =tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase : Union[str, Any] =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase : int =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : List[str] = (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __lowerCamelCase : Dict = (TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __lowerCamelCase : Dict = ( { """conversational""": TFBlenderbotForConditionalGeneration, """feature-extraction""": TFBlenderbotModel, """summarization""": TFBlenderbotForConditionalGeneration, """text2text-generation""": TFBlenderbotForConditionalGeneration, """translation""": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __lowerCamelCase : Union[str, Any] = True __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Union[str, Any] = False def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : List[str] =TFBlenderbotModelTester(self ) UpperCAmelCase : List[Any] =ConfigTester(self , config_class=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*snake_case__ ) @require_tokenizers @require_tf class __snake_case ( unittest.TestCase ): __lowerCamelCase : List[str] = ["""My friends are cool but they eat too many carbs."""] __lowerCamelCase : Dict = """facebook/blenderbot-400M-distill""" @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[int] =self.tokenizer(self.src_text , return_tensors='''tf''' ) UpperCAmelCase : Optional[int] =self.model.generate( model_inputs.input_ids , ) UpperCAmelCase : str =self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=snake_case__ )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
348
0
'''simple docstring''' import re import tempfile from pathlib import Path import pytest import yaml from datasets.utils.readme import ReadMe # @pytest.fixture # def example_yaml_structure(): _lowercase : Union[str, Any] = yaml.safe_load( "\\nname: \"\"\nallow_empty: false\nallow_empty_text: true\nsubsections:\n - name: \"Dataset Card for X\" # First-level markdown heading\n allow_empty: false\n allow_empty_text: true\n subsections:\n - name: \"Table of Contents\"\n allow_empty: false\n allow_empty_text: false\n subsections: null\n - name: \"Dataset Description\"\n allow_empty: false\n allow_empty_text: false\n subsections:\n - name: \"Dataset Summary\"\n allow_empty: false\n allow_empty_text: false\n subsections: null\n - name: \"Supported Tasks and Leaderboards\"\n allow_empty: true\n allow_empty_text: true\n subsections: null\n - name: Languages\n allow_empty: false\n allow_empty_text: true\n subsections: null\n" ) _lowercase : int = { "name": "root", "text": "", "is_empty_text": True, "subsections": [ { "name": "Dataset Card for My Dataset", "text": "", "is_empty_text": True, "subsections": [ {"name": "Table of Contents", "text": "Some text here.", "is_empty_text": False, "subsections": []}, { "name": "Dataset Description", "text": "Some text here.", "is_empty_text": False, "subsections": [ { "name": "Dataset Summary", "text": "Some text here.", "is_empty_text": False, "subsections": [], }, { "name": "Supported Tasks and Leaderboards", "text": "", "is_empty_text": True, "subsections": [], }, {"name": "Languages", "text": "Language Text", "is_empty_text": False, "subsections": []}, ], }, ], } ], } _lowercase : Optional[Any] = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _lowercase : Union[str, Any] = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n#### Extra Ignored Subsection\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _lowercase : Any = { "name": "root", "text": "", "is_empty_text": True, "subsections": [ { "name": "Dataset Card for My Dataset", "text": "", "is_empty_text": True, "subsections": [ {"name": "Table of Contents", "text": "Some text here.", "is_empty_text": False, "subsections": []}, { "name": "Dataset Description", "text": "Some text here.", "is_empty_text": False, "subsections": [ { "name": "Dataset Summary", "text": "Some text here.", "is_empty_text": False, "subsections": [ { "name": "Extra Ignored Subsection", "text": "", "is_empty_text": True, "subsections": [], } ], }, { "name": "Supported Tasks and Leaderboards", "text": "", "is_empty_text": True, "subsections": [], }, {"name": "Languages", "text": "Language Text", "is_empty_text": False, "subsections": []}, ], }, ], } ], } _lowercase : str = "\\n---\n---\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _lowercase : List[str] = ( "The following issues were found for the README at `{path}`:\n-\tEmpty YAML markers are present in the README." ) _lowercase : Tuple = "\\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _lowercase : Optional[Any] = ( "The following issues were found for the README at `{path}`:\n-\tNo YAML markers are present in the README." ) _lowercase : Tuple = "\\n---\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _lowercase : Optional[int] = "The following issues were found for the README at `{path}`:\n-\tOnly the start of YAML tags present in the README." _lowercase : List[Any] = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _lowercase : Optional[Any] = "The following issues were found for the README at `{path}`:\n-\tExpected some content in section `Dataset Summary` but it is empty.\n-\tExpected some text in section `Dataset Summary` but it is empty (text in subsections are ignored)." _lowercase : Optional[int] = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n" _lowercase : Union[str, Any] = "The following issues were found for the README at `{path}`:\n-\tExpected some content in section `Dataset Card for My Dataset` but it is empty.\n-\tSection `Dataset Card for My Dataset` expected the following subsections: `Table of Contents`, `Dataset Description`. Found 'None'." _lowercase : Union[str, Any] = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Languages\nLanguage Text\n" _lowercase : int = "The following issues were found for the README at `{path}`:\n-\tSection `Dataset Description` is missing subsection: `Supported Tasks and Leaderboards`." _lowercase : List[Any] = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\n" _lowercase : int = "The following issues were found for the README at `{path}`:\n-\tExpected some content in section `Languages` but it is empty." _lowercase : List[str] = "\\n---\nlanguage:\n- zh\n- en\n---\n\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _lowercase : str = "The following issues were found for the README at `{path}`:\n-\tThe README has no first-level headings. One heading is expected. Skipping further validation for this README." _lowercase : Dict = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n# Dataset Card My Dataset\n" _lowercase : List[str] = "The following issues were found for the README at `{path}`:\n-\tThe README has several first-level headings: `Dataset Card for My Dataset`, `Dataset Card My Dataset`. Only one heading is expected. Skipping further validation for this README." _lowercase : str = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _lowercase : Union[str, Any] = "The following issues were found for the README at `{path}`:\n-\tNo first-level heading starting with `Dataset Card for` found in README. Skipping further validation for this README." _lowercase : List[Any] = "" _lowercase : Optional[Any] = "The following issues were found for the README at `{path}`:\n-\tThe README has no first-level headings. One heading is expected. Skipping further validation for this README.\n-\tNo YAML markers are present in the README." _lowercase : List[Any] = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _lowercase : Optional[Any] = "The following issues were found while parsing the README at `{path}`:\n-\tMultiple sections with the same heading `Dataset Card for My Dataset` have been found. Please keep only one of these sections." @pytest.mark.parametrize( '''readme_md, expected_dict''' , [ (README_CORRECT, CORRECT_DICT), (README_CORRECT_FOUR_LEVEL, CORRECT_DICT_FOUR_LEVEL), ] , ) def snake_case_ ( __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[Any] ): """simple docstring""" assert ReadMe.from_string(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).to_dict() == expected_dict @pytest.mark.parametrize( '''readme_md, expected_error''' , [ (README_NO_YAML, EXPECTED_ERROR_README_NO_YAML), (README_EMPTY_YAML, EXPECTED_ERROR_README_EMPTY_YAML), (README_INCORRECT_YAML, EXPECTED_ERROR_README_INCORRECT_YAML), (README_EMPTY, EXPECTED_ERROR_README_EMPTY), (README_NONE_SUBSECTION, EXPECTED_ERROR_README_NONE_SUBSECTION), (README_MISSING_FIRST_LEVEL, EXPECTED_ERROR_README_MISSING_FIRST_LEVEL), (README_MISSING_SUBSECTION, EXPECTED_ERROR_README_MISSING_SUBSECTION), (README_MISSING_TEXT, EXPECTED_ERROR_README_MISSING_TEXT), (README_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_WRONG_FIRST_LEVEL), (README_MULTIPLE_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_MULTIPLE_WRONG_FIRST_LEVEL), (README_MISSING_CONTENT, EXPECTED_ERROR_README_MISSING_CONTENT), ] , ) def snake_case_ ( __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict ): """simple docstring""" with pytest.raises(__SCREAMING_SNAKE_CASE , match=re.escape(expected_error.format(path='''root''' ) ) ): lowercase_ : Optional[int] = ReadMe.from_string(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) readme.validate() @pytest.mark.parametrize( '''readme_md, expected_error''' , [ (README_MULTIPLE_SAME_HEADING_1, EXPECTED_ERROR_README_MULTIPLE_SAME_HEADING_1), ] , ) def snake_case_ ( __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : Dict ): """simple docstring""" with pytest.raises(__SCREAMING_SNAKE_CASE , match=re.escape(expected_error.format(path='''root''' ) ) ): ReadMe.from_string(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( '''readme_md,''' , [ (README_MULTIPLE_SAME_HEADING_1), ] , ) def snake_case_ ( __SCREAMING_SNAKE_CASE : Any ): """simple docstring""" ReadMe.from_string(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , suppress_parsing_errors=__SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( '''readme_md, expected_dict''' , [ (README_CORRECT, CORRECT_DICT), (README_CORRECT_FOUR_LEVEL, CORRECT_DICT_FOUR_LEVEL), ] , ) def snake_case_ ( __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : str ): """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: lowercase_ : Optional[int] = Path(__SCREAMING_SNAKE_CASE ) / '''README.md''' with open(__SCREAMING_SNAKE_CASE , '''w+''' ) as readme_file: readme_file.write(__SCREAMING_SNAKE_CASE ) lowercase_ : Any = ReadMe.from_readme(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).to_dict() assert out["name"] == path assert out["text"] == "" assert out["is_empty_text"] assert out["subsections"] == expected_dict["subsections"] @pytest.mark.parametrize( '''readme_md, expected_error''' , [ (README_NO_YAML, EXPECTED_ERROR_README_NO_YAML), (README_EMPTY_YAML, EXPECTED_ERROR_README_EMPTY_YAML), (README_INCORRECT_YAML, EXPECTED_ERROR_README_INCORRECT_YAML), (README_EMPTY, EXPECTED_ERROR_README_EMPTY), (README_NONE_SUBSECTION, EXPECTED_ERROR_README_NONE_SUBSECTION), (README_MISSING_FIRST_LEVEL, EXPECTED_ERROR_README_MISSING_FIRST_LEVEL), (README_MISSING_SUBSECTION, EXPECTED_ERROR_README_MISSING_SUBSECTION), (README_MISSING_TEXT, EXPECTED_ERROR_README_MISSING_TEXT), (README_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_WRONG_FIRST_LEVEL), (README_MULTIPLE_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_MULTIPLE_WRONG_FIRST_LEVEL), (README_MISSING_CONTENT, EXPECTED_ERROR_README_MISSING_CONTENT), ] , ) def snake_case_ ( __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Union[str, Any] ): """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: lowercase_ : str = Path(__SCREAMING_SNAKE_CASE ) / '''README.md''' with open(__SCREAMING_SNAKE_CASE , '''w+''' ) as readme_file: readme_file.write(__SCREAMING_SNAKE_CASE ) lowercase_ : List[str] = expected_error.format(path=__SCREAMING_SNAKE_CASE ) with pytest.raises(__SCREAMING_SNAKE_CASE , match=re.escape(__SCREAMING_SNAKE_CASE ) ): lowercase_ : int = ReadMe.from_readme(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) readme.validate() @pytest.mark.parametrize( '''readme_md, expected_error''' , [ (README_MULTIPLE_SAME_HEADING_1, EXPECTED_ERROR_README_MULTIPLE_SAME_HEADING_1), ] , ) def snake_case_ ( __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Optional[Any] ): """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: lowercase_ : Dict = Path(__SCREAMING_SNAKE_CASE ) / '''README.md''' with open(__SCREAMING_SNAKE_CASE , '''w+''' ) as readme_file: readme_file.write(__SCREAMING_SNAKE_CASE ) lowercase_ : Tuple = expected_error.format(path=__SCREAMING_SNAKE_CASE ) with pytest.raises(__SCREAMING_SNAKE_CASE , match=re.escape(__SCREAMING_SNAKE_CASE ) ): ReadMe.from_readme(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( '''readme_md,''' , [ (README_MULTIPLE_SAME_HEADING_1), ] , ) def snake_case_ ( __SCREAMING_SNAKE_CASE : Tuple ): """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: lowercase_ : Optional[int] = Path(__SCREAMING_SNAKE_CASE ) / '''README.md''' with open(__SCREAMING_SNAKE_CASE , '''w+''' ) as readme_file: readme_file.write(__SCREAMING_SNAKE_CASE ) ReadMe.from_readme(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , suppress_parsing_errors=__SCREAMING_SNAKE_CASE )
93
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { '''asapp/sew-d-tiny-100k''': '''https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json''', # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[Any] = """sew-d""" def __init__( self , snake_case__=32 , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=2 , snake_case__=512 , snake_case__=256 , snake_case__=True , snake_case__=True , snake_case__=("p2c", "c2p") , snake_case__="layer_norm" , snake_case__="gelu_python" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.0 , snake_case__=0.1 , snake_case__=0.02 , snake_case__=1e-7 , snake_case__=1e-5 , snake_case__="group" , snake_case__="gelu" , snake_case__=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , snake_case__=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , snake_case__=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , snake_case__=False , snake_case__=128 , snake_case__=16 , snake_case__=True , snake_case__=0.05 , snake_case__=10 , snake_case__=2 , snake_case__=0.0 , snake_case__=10 , snake_case__=0 , snake_case__="mean" , snake_case__=False , snake_case__=False , snake_case__=256 , snake_case__=0 , snake_case__=1 , snake_case__=2 , **snake_case__ , ) -> int: '''simple docstring''' super().__init__(**snake_case__ , pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ ) UpperCAmelCase : Union[str, Any] =hidden_size UpperCAmelCase : Union[str, Any] =feat_extract_norm UpperCAmelCase : Optional[Any] =feat_extract_activation UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : int =list(snake_case__ ) UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : str =conv_bias UpperCAmelCase : Tuple =num_conv_pos_embeddings UpperCAmelCase : Dict =num_conv_pos_embedding_groups UpperCAmelCase : str =len(self.conv_dim ) UpperCAmelCase : Dict =num_hidden_layers UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : List[Any] =squeeze_factor UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : int =position_buckets UpperCAmelCase : Optional[int] =share_att_key UpperCAmelCase : Optional[int] =relative_attention UpperCAmelCase : Tuple =norm_rel_ebd UpperCAmelCase : List[Any] =list(snake_case__ ) UpperCAmelCase : Dict =hidden_act UpperCAmelCase : Optional[int] =num_attention_heads UpperCAmelCase : Any =hidden_dropout UpperCAmelCase : str =attention_dropout UpperCAmelCase : Union[str, Any] =activation_dropout UpperCAmelCase : str =feat_proj_dropout UpperCAmelCase : Union[str, Any] =final_dropout UpperCAmelCase : Optional[int] =layer_norm_eps UpperCAmelCase : str =feature_layer_norm_eps UpperCAmelCase : str =initializer_range UpperCAmelCase : Any =vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect.''' '''It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,''' f'''but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)''' f'''= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCAmelCase : Union[str, Any] =apply_spec_augment UpperCAmelCase : Optional[Any] =mask_time_prob UpperCAmelCase : Tuple =mask_time_length UpperCAmelCase : str =mask_time_min_masks UpperCAmelCase : Optional[int] =mask_feature_prob UpperCAmelCase : Optional[Any] =mask_feature_length UpperCAmelCase : List[Any] =mask_feature_min_masks # ctc loss UpperCAmelCase : str =ctc_loss_reduction UpperCAmelCase : Optional[int] =ctc_zero_infinity # sequence classification UpperCAmelCase : Union[str, Any] =use_weighted_layer_sum UpperCAmelCase : int =classifier_proj_size @property def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
348
0
from __future__ import annotations import unittest import numpy as np from transformers import LayoutLMConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers.models.layoutlm.modeling_tf_layoutlm import ( TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFLayoutLMForMaskedLM, TFLayoutLMForQuestionAnswering, TFLayoutLMForSequenceClassification, TFLayoutLMForTokenClassification, TFLayoutLMModel, ) class _snake_case : def __init__( self , _lowerCamelCase , _lowerCamelCase=13 , _lowerCamelCase=7 , _lowerCamelCase=True , _lowerCamelCase=True , _lowerCamelCase=True , _lowerCamelCase=True , _lowerCamelCase=99 , _lowerCamelCase=32 , _lowerCamelCase=2 , _lowerCamelCase=4 , _lowerCamelCase=37 , _lowerCamelCase="gelu" , _lowerCamelCase=0.1 , _lowerCamelCase=0.1 , _lowerCamelCase=512 , _lowerCamelCase=16 , _lowerCamelCase=2 , _lowerCamelCase=0.02 , _lowerCamelCase=3 , _lowerCamelCase=4 , _lowerCamelCase=None , _lowerCamelCase=1000 , ): a :str = parent a :str = batch_size a :List[Any] = seq_length a :Union[str, Any] = is_training a :str = use_input_mask a :Tuple = use_token_type_ids a :Optional[int] = use_labels a :Union[str, Any] = vocab_size a :Optional[Any] = hidden_size a :Any = num_hidden_layers a :Optional[int] = num_attention_heads a :Tuple = intermediate_size a :Dict = hidden_act a :str = hidden_dropout_prob a :List[Any] = attention_probs_dropout_prob a :List[Any] = max_position_embeddings a :List[str] = type_vocab_size a :List[Any] = type_sequence_label_size a :Union[str, Any] = initializer_range a :Optional[Any] = num_labels a :Optional[int] = num_choices a :Union[str, Any] = scope a :List[str] = range_bbox def SCREAMING_SNAKE_CASE__ ( self ): a :str = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) # convert bbox to numpy since TF does not support item assignment a :Union[str, Any] = ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox ).numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: a :List[Any] = bbox[i, j, 3] a :List[str] = bbox[i, j, 1] a :List[str] = t if bbox[i, j, 2] < bbox[i, j, 0]: a :Dict = bbox[i, j, 2] a :Dict = bbox[i, j, 0] a :Any = t a :Optional[Any] = tf.convert_to_tensor(_lowerCamelCase ) a :int = None if self.use_input_mask: a :List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) a :Optional[int] = None if self.use_token_type_ids: a :Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) a :List[Any] = None a :List[Any] = None a :List[Any] = None if self.use_labels: a :Tuple = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a :Dict = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) a :List[str] = ids_tensor([self.batch_size] , self.num_choices ) a :List[Any] = LayoutLMConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE__ ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): a :Optional[int] = TFLayoutLMModel(config=_lowerCamelCase ) a :Dict = model(_lowerCamelCase , _lowerCamelCase , attention_mask=_lowerCamelCase , token_type_ids=_lowerCamelCase ) a :Union[str, Any] = model(_lowerCamelCase , _lowerCamelCase , token_type_ids=_lowerCamelCase ) a :Union[str, Any] = model(_lowerCamelCase , _lowerCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def SCREAMING_SNAKE_CASE__ ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): a :List[str] = TFLayoutLMForMaskedLM(config=_lowerCamelCase ) a :int = model(_lowerCamelCase , _lowerCamelCase , attention_mask=_lowerCamelCase , token_type_ids=_lowerCamelCase , labels=_lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): a :Optional[int] = self.num_labels a :List[Any] = TFLayoutLMForSequenceClassification(config=_lowerCamelCase ) a :int = model(_lowerCamelCase , _lowerCamelCase , attention_mask=_lowerCamelCase , token_type_ids=_lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): a :int = self.num_labels a :Optional[int] = TFLayoutLMForTokenClassification(config=_lowerCamelCase ) a :int = model(_lowerCamelCase , _lowerCamelCase , attention_mask=_lowerCamelCase , token_type_ids=_lowerCamelCase , labels=_lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): a :Optional[Any] = TFLayoutLMForQuestionAnswering(config=_lowerCamelCase ) a :Optional[int] = model(_lowerCamelCase , _lowerCamelCase , attention_mask=_lowerCamelCase , token_type_ids=_lowerCamelCase ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def SCREAMING_SNAKE_CASE__ ( self ): a :List[str] = self.prepare_config_and_inputs() ( ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ) :List[Any] = config_and_inputs a :Union[str, Any] = { '''input_ids''': input_ids, '''bbox''': bbox, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask, } return config, inputs_dict @require_tf class _snake_case ( _snake_case , _snake_case , unittest.TestCase ): SCREAMING_SNAKE_CASE__ = ( ( TFLayoutLMModel, TFLayoutLMForMaskedLM, TFLayoutLMForTokenClassification, TFLayoutLMForSequenceClassification, TFLayoutLMForQuestionAnswering, ) if is_tf_available() else () ) SCREAMING_SNAKE_CASE__ = ( { 'feature-extraction': TFLayoutLMModel, 'fill-mask': TFLayoutLMForMaskedLM, 'text-classification': TFLayoutLMForSequenceClassification, 'token-classification': TFLayoutLMForTokenClassification, 'zero-shot': TFLayoutLMForSequenceClassification, } if is_tf_available() else {} ) SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = 10 def SCREAMING_SNAKE_CASE__ ( self ): a :Dict = TFLayoutLMModelTester(self ) a :Dict = ConfigTester(self , config_class=_lowerCamelCase , hidden_size=37 ) def SCREAMING_SNAKE_CASE__ ( self ): self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE__ ( self ): a :str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_lowerCamelCase ) def SCREAMING_SNAKE_CASE__ ( self ): a :List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_lowerCamelCase ) def SCREAMING_SNAKE_CASE__ ( self ): a :List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*_lowerCamelCase ) def SCREAMING_SNAKE_CASE__ ( self ): a :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_lowerCamelCase ) def SCREAMING_SNAKE_CASE__ ( self ): a :Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_lowerCamelCase ) @slow def SCREAMING_SNAKE_CASE__ ( self ): for model_name in TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a :str = TFLayoutLMModel.from_pretrained(_lowerCamelCase ) self.assertIsNotNone(_lowerCamelCase ) @unittest.skip('''Onnx compliancy broke with TF 2.10''' ) def SCREAMING_SNAKE_CASE__ ( self ): pass def __lowerCamelCase ( ): """simple docstring""" a :Tuple = tf.convert_to_tensor([[101,1019,1014,1016,1037,1_2849,4747,1004,1_4246,2278,5439,4524,5002,2930,2193,2930,4341,3208,1005,1055,2171,2848,1_1300,3531,102],[101,4070,4034,7020,1024,3058,1015,1013,2861,1013,6070,1_9274,2772,6205,2_7814,1_6147,1_6147,4343,2047,1_0283,1_0969,1_4389,1012,2338,102]] ) # noqa: E231 a :Any = tf.convert_to_tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],] ) # noqa: E231 a :List[str] = tf.convert_to_tensor([[[0,0,0,0],[423,237,440,251],[427,272,441,287],[419,115,437,129],[961,885,992,912],[256,38,330,58],[256,38,330,58],[336,42,353,57],[360,39,401,56],[360,39,401,56],[411,39,471,59],[479,41,528,59],[533,39,630,60],[67,113,134,131],[141,115,209,132],[68,149,133,166],[141,149,187,164],[195,148,287,165],[195,148,287,165],[195,148,287,165],[295,148,349,165],[441,149,492,166],[497,149,546,164],[64,201,125,218],[1000,1000,1000,1000]],[[0,0,0,0],[662,150,754,166],[665,199,742,211],[519,213,554,228],[519,213,554,228],[134,433,187,454],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[314,469,376,482],[504,684,582,706],[941,825,973,900],[941,825,973,900],[941,825,973,900],[941,825,973,900],[610,749,652,765],[130,659,168,672],[176,657,237,672],[238,657,312,672],[443,653,628,672],[443,653,628,672],[716,301,825,317],[1000,1000,1000,1000]]] ) # noqa: E231 a :List[str] = tf.convert_to_tensor([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,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: E231 # these are sequence labels (i.e. at the token level) a :Any = tf.convert_to_tensor([[-100,10,10,10,9,1,-100,7,7,-100,7,7,4,2,5,2,8,8,-100,-100,5,0,3,2,-100],[-100,12,12,12,-100,12,10,-100,-100,-100,-100,10,12,9,-100,-100,-100,10,10,10,9,12,-100,10,-100]] ) # noqa: E231 # fmt: on return input_ids, attention_mask, bbox, token_type_ids, labels @require_tf class _snake_case ( unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE__ ( self ): a :List[Any] = TFLayoutLMModel.from_pretrained('''microsoft/layoutlm-base-uncased''' ) a , a , a , a , a :Optional[Any] = prepare_layoutlm_batch_inputs() # forward pass a :Tuple = model(input_ids=_lowerCamelCase , bbox=_lowerCamelCase , attention_mask=_lowerCamelCase , token_type_ids=_lowerCamelCase ) # test the sequence output on [0, :3, :3] a :List[str] = tf.convert_to_tensor( [[0.1785, -0.1947, -0.0425], [-0.3254, -0.2807, 0.2553], [-0.5391, -0.3322, 0.3364]] , ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , _lowerCamelCase , atol=1e-3 ) ) # test the pooled output on [1, :3] a :List[str] = tf.convert_to_tensor([-0.6580, -0.0214, 0.8552] ) self.assertTrue(np.allclose(outputs.pooler_output[1, :3] , _lowerCamelCase , atol=1e-3 ) ) @slow def SCREAMING_SNAKE_CASE__ ( self ): # initialize model with randomly initialized sequence classification head a :str = TFLayoutLMForSequenceClassification.from_pretrained('''microsoft/layoutlm-base-uncased''' , num_labels=2 ) a , a , a , a , a :List[str] = prepare_layoutlm_batch_inputs() # forward pass a :List[Any] = model( input_ids=_lowerCamelCase , bbox=_lowerCamelCase , attention_mask=_lowerCamelCase , token_type_ids=_lowerCamelCase , labels=tf.convert_to_tensor([1, 1] ) , ) # test whether we get a loss as a scalar a :Union[str, Any] = outputs.loss a :Optional[Any] = (2,) self.assertEqual(loss.shape , _lowerCamelCase ) # test the shape of the logits a :Any = outputs.logits a :Tuple = (2, 2) self.assertEqual(logits.shape , _lowerCamelCase ) @slow def SCREAMING_SNAKE_CASE__ ( self ): # initialize model with randomly initialized token classification head a :Dict = TFLayoutLMForTokenClassification.from_pretrained('''microsoft/layoutlm-base-uncased''' , num_labels=13 ) a , a , a , a , a :Dict = prepare_layoutlm_batch_inputs() # forward pass a :List[Any] = model( input_ids=_lowerCamelCase , bbox=_lowerCamelCase , attention_mask=_lowerCamelCase , token_type_ids=_lowerCamelCase , labels=_lowerCamelCase ) # test the shape of the logits a :Optional[Any] = outputs.logits a :List[Any] = tf.convert_to_tensor((2, 25, 13) ) self.assertEqual(logits.shape , _lowerCamelCase ) @slow def SCREAMING_SNAKE_CASE__ ( self ): # initialize model with randomly initialized token classification head a :List[Any] = TFLayoutLMForQuestionAnswering.from_pretrained('''microsoft/layoutlm-base-uncased''' ) a , a , a , a , a :Any = prepare_layoutlm_batch_inputs() # forward pass a :str = model(input_ids=_lowerCamelCase , bbox=_lowerCamelCase , attention_mask=_lowerCamelCase , token_type_ids=_lowerCamelCase ) # test the shape of the logits a :Optional[int] = tf.convert_to_tensor((2, 25) ) self.assertEqual(outputs.start_logits.shape , _lowerCamelCase ) self.assertEqual(outputs.end_logits.shape , _lowerCamelCase )
94
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node __snake_case = 4 __snake_case = 3 class __snake_case ( lowerCamelCase__ ): pass def lowerCAmelCase_ ( __lowerCAmelCase )-> List[str]: '''simple docstring''' for shard in shards: for i in range(__lowerCAmelCase ): yield {"i": i, "shard": shard} def lowerCAmelCase_ ( )-> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =int(os.environ['''RANK'''] ) UpperCAmelCase : Optional[Any] =int(os.environ['''WORLD_SIZE'''] ) UpperCAmelCase : List[Any] =ArgumentParser() parser.add_argument('''--streaming''' , type=__lowerCAmelCase ) parser.add_argument('''--local_rank''' , type=__lowerCAmelCase ) parser.add_argument('''--num_workers''' , type=__lowerCAmelCase , default=0 ) UpperCAmelCase : Any =parser.parse_args() UpperCAmelCase : List[str] =args.streaming UpperCAmelCase : Tuple =args.num_workers UpperCAmelCase : int ={'''shards''': [f'''shard_{shard_idx}''' for shard_idx in range(__lowerCAmelCase )]} UpperCAmelCase : Optional[int] =IterableDataset.from_generator(__lowerCAmelCase , gen_kwargs=__lowerCAmelCase ) if not streaming: UpperCAmelCase : List[Any] =Dataset.from_list(list(__lowerCAmelCase ) ) UpperCAmelCase : Dict =split_dataset_by_node(__lowerCAmelCase , rank=__lowerCAmelCase , world_size=__lowerCAmelCase ) UpperCAmelCase : List[Any] =torch.utils.data.DataLoader(__lowerCAmelCase , num_workers=__lowerCAmelCase ) UpperCAmelCase : Dict =NUM_SHARDS * NUM_ITEMS_PER_SHARD UpperCAmelCase : str =full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) UpperCAmelCase : List[Any] =sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f'''local_size {local_size} != expected_local_size {expected_local_size}''' ) if __name__ == "__main__": main()
348
0
import argparse from pathlib import Path import fairseq import torch from fairseq.models.xmod import XMODModel as FairseqXmodModel from packaging import version from transformers import XmodConfig, XmodForMaskedLM, XmodForSequenceClassification from transformers.utils import logging if version.parse(fairseq.__version__) < version.parse("""0.12.2"""): raise Exception("""requires fairseq >= 0.12.2""") if version.parse(fairseq.__version__) > version.parse("""2"""): raise Exception("""requires fairseq < v2""") logging.set_verbosity_info() UpperCAmelCase : int = logging.get_logger(__name__) UpperCAmelCase : Any = """Hello, World!""" UpperCAmelCase : int = """en_XX""" def _A ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : bool ): """simple docstring""" a__ : Any =Path("data_bin" ) a__ : Optional[Any] =FairseqXmodModel.from_pretrained( model_name_or_path=str(Path(SCREAMING_SNAKE_CASE ).parent ) , checkpoint_file=Path(SCREAMING_SNAKE_CASE ).name , _name="xmod_base" , arch="xmod_base" , task="multilingual_masked_lm" , data_name_or_path=str(SCREAMING_SNAKE_CASE ) , bpe="sentencepiece" , sentencepiece_model=str(Path(SCREAMING_SNAKE_CASE ).parent / "sentencepiece.bpe.model" ) , src_dict=str(data_dir / "dict.txt" ) , ) xmod.eval() # disable dropout print(SCREAMING_SNAKE_CASE ) a__ : int =xmod.model.encoder.sentence_encoder a__ : Any =XmodConfig( vocab_size=xmod_sent_encoder.embed_tokens.num_embeddings , hidden_size=xmod.cfg.model.encoder_embed_dim , num_hidden_layers=xmod.cfg.model.encoder_layers , num_attention_heads=xmod.cfg.model.encoder_attention_heads , intermediate_size=xmod.cfg.model.encoder_ffn_embed_dim , max_position_embeddings=514 , type_vocab_size=1 , layer_norm_eps=1e-5 , pre_norm=xmod.cfg.model.encoder_normalize_before , adapter_reduction_factor=getattr(xmod.cfg.model , "bottleneck" , 2 ) , adapter_layer_norm=xmod.cfg.model.adapter_layer_norm , adapter_reuse_layer_norm=xmod.cfg.model.adapter_reuse_layer_norm , ln_before_adapter=xmod.cfg.model.ln_before_adapter , languages=xmod.cfg.model.languages , ) if classification_head: a__ : Union[str, Any] =xmod.model.classification_heads["mnli"].out_proj.weight.shape[0] print("Our X-MOD config:" , SCREAMING_SNAKE_CASE ) a__ : str =XmodForSequenceClassification(SCREAMING_SNAKE_CASE ) if classification_head else XmodForMaskedLM(SCREAMING_SNAKE_CASE ) model.eval() # Now let's copy all the weights. # Embeddings a__ : Tuple =xmod_sent_encoder.embed_tokens.weight a__ : int =xmod_sent_encoder.embed_positions.weight a__ : List[str] =torch.zeros_like( model.roberta.embeddings.token_type_embeddings.weight ) # just zero them out b/c xmod doesn't use them. a__ : Tuple =xmod_sent_encoder.layernorm_embedding.weight a__ : Any =xmod_sent_encoder.layernorm_embedding.bias for i in range(config.num_hidden_layers ): # Encoder: start of layer a__ : List[Any] =model.roberta.encoder.layer[i] a__ : str =xmod_sent_encoder.layers[i] # self attention a__ : Union[str, Any] =layer.attention.self if not ( xmod_layer.self_attn.k_proj.weight.data.shape == xmod_layer.self_attn.q_proj.weight.data.shape == xmod_layer.self_attn.v_proj.weight.data.shape == torch.Size((config.hidden_size, config.hidden_size) ) ): raise AssertionError("Dimensions of self-attention weights do not match." ) a__ : Any =xmod_layer.self_attn.q_proj.weight a__ : Optional[Any] =xmod_layer.self_attn.q_proj.bias a__ : Optional[int] =xmod_layer.self_attn.k_proj.weight a__ : Optional[int] =xmod_layer.self_attn.k_proj.bias a__ : Any =xmod_layer.self_attn.v_proj.weight a__ : List[str] =xmod_layer.self_attn.v_proj.bias # self-attention output a__ : Union[str, Any] =layer.attention.output if self_output.dense.weight.shape != xmod_layer.self_attn.out_proj.weight.shape: raise AssertionError("Dimensions of self-attention output weights do not match." ) a__ : Any =xmod_layer.self_attn.out_proj.weight a__ : str =xmod_layer.self_attn.out_proj.bias a__ : Dict =xmod_layer.self_attn_layer_norm.weight a__ : Any =xmod_layer.self_attn_layer_norm.bias # intermediate a__ : List[Any] =layer.intermediate if intermediate.dense.weight.shape != xmod_layer.fca.weight.shape: raise AssertionError("Dimensions of intermediate weights do not match." ) a__ : Any =xmod_layer.fca.weight a__ : str =xmod_layer.fca.bias # output a__ : Union[str, Any] =layer.output if bert_output.dense.weight.shape != xmod_layer.fca.weight.shape: raise AssertionError("Dimensions of feed-forward weights do not match." ) a__ : int =xmod_layer.fca.weight a__ : str =xmod_layer.fca.bias a__ : str =xmod_layer.final_layer_norm.weight a__ : Optional[Any] =xmod_layer.final_layer_norm.bias if bert_output.adapter_layer_norm is not None: a__ : Union[str, Any] =xmod_layer.adapter_layer_norm.weight a__ : List[str] =xmod_layer.adapter_layer_norm.bias if sorted(bert_output.adapter_modules.keys() ) != sorted(xmod_layer.adapter_modules.keys() ): raise AssertionError("Lists of language adapters do not match." ) for lang_code, adapter in xmod_layer.adapter_modules.items(): a__ : int =bert_output.adapter_modules[lang_code] a__ : List[Any] =xmod_layer.adapter_modules[lang_code] a__ : List[str] =from_adapter.fca.weight a__ : List[Any] =from_adapter.fca.bias a__ : Optional[int] =from_adapter.fca.weight a__ : List[Any] =from_adapter.fca.bias # end of layer if xmod_sent_encoder.layer_norm is not None: a__ : Any =xmod_sent_encoder.layer_norm.weight a__ : Tuple =xmod_sent_encoder.layer_norm.bias if classification_head: a__ : int =xmod.model.classification_heads["mnli"].dense.weight a__ : Union[str, Any] =xmod.model.classification_heads["mnli"].dense.bias a__ : List[str] =xmod.model.classification_heads["mnli"].out_proj.weight a__ : Any =xmod.model.classification_heads["mnli"].out_proj.bias else: # LM Head a__ : Optional[Any] =xmod.model.encoder.lm_head.dense.weight a__ : Dict =xmod.model.encoder.lm_head.dense.bias a__ : List[str] =xmod.model.encoder.lm_head.layer_norm.weight a__ : Any =xmod.model.encoder.lm_head.layer_norm.bias a__ : Dict =xmod.model.encoder.lm_head.weight a__ : Optional[int] =xmod.model.encoder.lm_head.bias # Let's check that we get the same results. a__ : Tuple =xmod.encode(SCREAMING_SNAKE_CASE ).unsqueeze(0 ) # batch of size 1 model.roberta.set_default_language(SCREAMING_SNAKE_CASE ) a__ : List[Any] =model(SCREAMING_SNAKE_CASE )[0] if classification_head: a__ : Optional[int] =xmod.model.classification_heads["mnli"](xmod.extract_features(SCREAMING_SNAKE_CASE ) ) else: a__ : Any =xmod.model(SCREAMING_SNAKE_CASE , lang_id=[SAMPLE_LANGUAGE] )[0] print(our_output.shape , their_output.shape ) a__ : Any =torch.max(torch.abs(our_output - their_output ) ).item() print(f'''max_absolute_diff = {max_absolute_diff}''' ) # ~ 1e-7 a__ : List[str] =torch.allclose(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , atol=1e-3 ) print("Do both models output the same tensors?" , "🔥" if success else "💩" ) if not success: raise Exception("Something went wRoNg" ) Path(SCREAMING_SNAKE_CASE ).mkdir(parents=SCREAMING_SNAKE_CASE , exist_ok=SCREAMING_SNAKE_CASE ) print(f'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": UpperCAmelCase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( """--xmod_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.""" ) parser.add_argument( """--classification_head""", action="""store_true""", help="""Whether to convert a final classification head.""" ) UpperCAmelCase : List[Any] = parser.parse_args() convert_xmod_checkpoint_to_pytorch( args.xmod_checkpoint_path, args.pytorch_dump_folder_path, args.classification_head )
95
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __snake_case = {'''configuration_opt''': ['''OPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OPTConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''OPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OPTForCausalLM''', '''OPTModel''', '''OPTPreTrainedModel''', '''OPTForSequenceClassification''', '''OPTForQuestionAnswering''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''TFOPTForCausalLM''', '''TFOPTModel''', '''TFOPTPreTrainedModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''FlaxOPTForCausalLM''', '''FlaxOPTModel''', '''FlaxOPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
0
"""simple docstring""" import argparse import torch from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert from transformers.utils import logging logging.set_verbosity_info() def _snake_case ( lowercase__ , lowercase__ , lowercase__ ): # Initialise PyTorch model _lowerCamelCase : Dict = RemBertConfig.from_json_file(lowercase__ ) print('Building PyTorch model from configuration: {}'.format(str(lowercase__ ) ) ) _lowerCamelCase : Any = RemBertModel(lowercase__ ) # Load weights from tf checkpoint load_tf_weights_in_rembert(lowercase__ , lowercase__ , lowercase__ ) # Save pytorch-model print('Save PyTorch model to {}'.format(lowercase__ ) ) torch.save(model.state_dict() , lowercase__ ) if __name__ == "__main__": lowercase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path.""" ) parser.add_argument( """--rembert_config_file""", default=None, type=str, required=True, help=( """The config json file corresponding to the pre-trained RemBERT model. \n""" """This specifies the model architecture.""" ), ) parser.add_argument( """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) lowercase__ = parser.parse_args() convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)
96
import tempfile import unittest import numpy as np import transformers from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax import jax.numpy as jnp from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel if is_torch_available(): import torch class __snake_case : def __init__( self , snake_case__ , snake_case__=14 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=False , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=4 , snake_case__=4 , snake_case__=4 , snake_case__=37 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , ) -> str: '''simple docstring''' UpperCAmelCase : str =parent UpperCAmelCase : Tuple =batch_size UpperCAmelCase : Optional[int] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Tuple =use_input_mask UpperCAmelCase : List[Any] =use_token_type_ids UpperCAmelCase : Optional[Any] =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : List[Any] =hidden_size UpperCAmelCase : Optional[int] =rotary_dim UpperCAmelCase : Union[str, Any] =num_hidden_layers UpperCAmelCase : List[Any] =num_attention_heads UpperCAmelCase : Dict =intermediate_size UpperCAmelCase : Union[str, Any] =hidden_act UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : Dict =attention_probs_dropout_prob UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : str =initializer_range UpperCAmelCase : Optional[int] =None UpperCAmelCase : List[Any] =vocab_size - 1 UpperCAmelCase : Optional[Any] =vocab_size - 1 UpperCAmelCase : List[Any] =vocab_size - 1 def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : List[Any] =None if self.use_input_mask: UpperCAmelCase : Optional[Any] =random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : Dict =GPTJConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , use_cache=snake_case__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , rotary_dim=self.rotary_dim , ) return (config, input_ids, input_mask) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Union[str, Any] =config_and_inputs UpperCAmelCase : Tuple ={'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =20 UpperCAmelCase : Any =model_class_name(snake_case__ ) UpperCAmelCase : str =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : Any =jnp.ones((input_ids.shape[0], max_decoder_length) , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : List[str] =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =model( input_ids[:, -1:] , attention_mask=snake_case__ , past_key_values=outputs_cache.past_key_values , position_ids=snake_case__ , ) UpperCAmelCase : List[Any] =model(snake_case__ ) UpperCAmelCase : Any =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Dict =20 UpperCAmelCase : Dict =model_class_name(snake_case__ ) UpperCAmelCase : Tuple =jnp.concatenate( [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]) )] , axis=-1 , ) UpperCAmelCase : Dict =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : int =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : str =model( input_ids[:, -1:] , past_key_values=outputs_cache.past_key_values , attention_mask=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =model(snake_case__ , attention_mask=snake_case__ ) UpperCAmelCase : Dict =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) @require_flax class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Tuple = (FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else () __lowerCamelCase : Optional[Any] = (FlaxGPTJForCausalLM,) if is_flax_available() else () def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =FlaxGPTJModelTester(self ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Dict =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward_with_attn_mask( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) @tooslow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =GPTaTokenizer.from_pretrained('''gpt2''' , pad_token='''<|endoftext|>''' , padding_side='''left''' ) UpperCAmelCase : Optional[Any] =tokenizer(['''Hello this is a long string''', '''Hey'''] , return_tensors='''np''' , padding=snake_case__ , truncation=snake_case__ ) UpperCAmelCase : Optional[int] =FlaxGPTJForCausalLM.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : str =False UpperCAmelCase : Union[str, Any] =model.config.eos_token_id UpperCAmelCase : List[Any] =jax.jit(model.generate ) UpperCAmelCase : Dict =jit_generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , pad_token_id=tokenizer.pad_token_id ).sequences UpperCAmelCase : Any =tokenizer.batch_decode(snake_case__ , skip_special_tokens=snake_case__ ) UpperCAmelCase : Tuple =[ '''Hello this is a long string of text.\n\nI\'m trying to get the text of the''', '''Hey, I\'m a little late to the party. I\'m going to''', ] self.assertListEqual(snake_case__ , snake_case__ ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : List[str] =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : Any =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : Any =getattr(snake_case__ , snake_case__ ) UpperCAmelCase , UpperCAmelCase : Union[str, Any] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Tuple =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : int =0 UpperCAmelCase : Optional[int] =1 UpperCAmelCase : Optional[int] =0 UpperCAmelCase : Union[str, Any] =1 UpperCAmelCase : List[str] =pt_model_class(snake_case__ ).eval() UpperCAmelCase : Optional[int] =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Any =convert_pytorch_state_dict_to_flax(pt_model.state_dict() , snake_case__ ) UpperCAmelCase : Union[str, Any] =fx_state with torch.no_grad(): UpperCAmelCase : Any =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : Dict =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(snake_case__ ) UpperCAmelCase : str =model_class.from_pretrained(snake_case__ , from_pt=snake_case__ ) UpperCAmelCase : int =fx_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output_loaded, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output_loaded[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : int =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : int =getattr(snake_case__ , snake_case__ ) UpperCAmelCase : Dict =pt_model_class(snake_case__ ).eval() UpperCAmelCase : str =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Optional[Any] =load_flax_weights_in_pytorch_model(snake_case__ , fx_model.params ) UpperCAmelCase , UpperCAmelCase : Optional[int] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Optional[int] =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : str =0 UpperCAmelCase : Any =1 UpperCAmelCase : List[Any] =0 UpperCAmelCase : Tuple =1 # make sure weights are tied in PyTorch pt_model.tie_weights() with torch.no_grad(): UpperCAmelCase : Optional[Any] =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : List[Any] =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(snake_case__ ) UpperCAmelCase : Tuple =pt_model_class.from_pretrained(snake_case__ , from_flax=snake_case__ ) with torch.no_grad(): UpperCAmelCase : Any =pt_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @tooslow def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase : str =model_class_name.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : Tuple =model(np.ones((1, 1) ) ) self.assertIsNotNone(snake_case__ )
348
0
'''simple docstring''' from typing import Dict from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, get_torch_dist_unique_port, require_torch_multi_gpu, require_torch_neuroncore, ) from transformers.training_args import ParallelMode from transformers.utils import logging __snake_case = logging.get_logger(__name__) if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset from transformers import Trainer class lowercase ( A__ ): """simple docstring""" def __init__( self , UpperCamelCase_ = 101 ): '''simple docstring''' UpperCamelCase__ :int = length def __len__( self ): '''simple docstring''' return self.length def __getitem__( self , UpperCamelCase_ ): '''simple docstring''' return i class lowercase : """simple docstring""" def __call__( self , UpperCamelCase_ ): '''simple docstring''' return {"input_ids": torch.tensor(UpperCamelCase_ ), "labels": torch.tensor(UpperCamelCase_ )} class lowercase ( nn.Module ): """simple docstring""" def __init__( self ): '''simple docstring''' super().__init__() # Add some (unused) params otherwise DDP will complain. UpperCamelCase__ :List[Any] = nn.Linear(120 , 80 ) def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_=None ): '''simple docstring''' if labels is not None: return torch.tensor(0.0 , device=input_ids.device ), input_ids else: return input_ids class lowercase ( A__ ): """simple docstring""" @require_torch_neuroncore def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Optional[Any] = F'''--nproc_per_node=2 --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() UpperCamelCase__ :Optional[Any] = self.get_auto_remove_tmp_dir() UpperCamelCase__ :Tuple = F'''--output_dir {output_dir}'''.split() UpperCamelCase__ :Tuple = ['''torchrun'''] + distributed_args + args execute_subprocess_async(UpperCamelCase_ , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call class lowercase ( A__ ): """simple docstring""" @require_torch_multi_gpu def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :List[Any] = F'''--nproc_per_node={torch.cuda.device_count()} --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() UpperCamelCase__ :int = self.get_auto_remove_tmp_dir() UpperCamelCase__ :List[Any] = F'''--output_dir {output_dir}'''.split() UpperCamelCase__ :str = ['''torchrun'''] + distributed_args + args execute_subprocess_async(UpperCamelCase_ , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call if __name__ == "__main__": # The script below is meant to be run under torch.distributed, on a machine with multiple GPUs: # # PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py __snake_case = HfArgumentParser((TrainingArguments,)) __snake_case = parser.parse_args_into_dataclasses()[0] logger.warning( F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, """ F"""distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}""" ) # Essentially, what we want to verify in the distributed case is that we get all samples back, # in the right order. (this is crucial for prediction for instance) for dataset_length in [101, 40, 7]: __snake_case = DummyDataset(dataset_length) def a ( __a ) -> Dict: '''simple docstring''' UpperCamelCase__ :List[Any] = list(range(len(__a ) ) ) UpperCamelCase__ :Optional[int] = p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential if not success and training_args.local_rank == 0: logger.warning( '''Predictions and/or labels do not match expected results:\n - predictions: ''' f'''{p.predictions.tolist()}\n - labels: {p.label_ids.tolist()}\n - expected: {sequential}''' ) return {"success": success} __snake_case = Trainer( model=DummyModel(), args=training_args, data_collator=DummyDataCollator(), eval_dataset=dataset, compute_metrics=compute_metrics, ) __snake_case = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) __snake_case = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) __snake_case = 2 __snake_case = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) __snake_case = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) __snake_case = None
97
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __snake_case = { '''configuration_bloom''': ['''BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BloomConfig''', '''BloomOnnxConfig'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''BloomTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BloomForCausalLM''', '''BloomModel''', '''BloomPreTrainedModel''', '''BloomForSequenceClassification''', '''BloomForTokenClassification''', '''BloomForQuestionAnswering''', ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
0
"""simple docstring""" def a_ ( lowerCamelCase = 1_0_0_0 ): return sum(2 * a * ((a - 1) // 2) for a in range(3 , n + 1 ) ) if __name__ == "__main__": print(solution())
98
import os from typing import Dict, List, Tuple, TypeVar, Union __snake_case = TypeVar('''T''') __snake_case = Union[List[T], Tuple[T, ...]] __snake_case = Union[T, List[T], Dict[str, T]] __snake_case = Union[str, bytes, os.PathLike]
348
0
import pytest from datasets.splits import SplitDict, SplitInfo from datasets.utils.py_utils import asdict @pytest.mark.parametrize( 'split_dict' , [ SplitDict(), SplitDict({'train': SplitInfo(name='train' , num_bytes=1337 , num_examples=42 , dataset_name='my_dataset' )} ), SplitDict({'train': SplitInfo(name='train' , num_bytes=1337 , num_examples=42 )} ), SplitDict({'train': SplitInfo()} ), ] , ) def A_ ( A__ ) -> Union[str, Any]: a__ : List[str] = split_dict._to_yaml_list() assert len(A__ ) == len(A__ ) a__ : List[Any] = SplitDict._from_yaml_list(A__ ) for split_name, split_info in split_dict.items(): # dataset_name field is deprecated, and is therefore not part of the YAML dump a__ : Any = None # the split name of split_dict takes over the name of the split info object a__ : str = split_name assert split_dict == reloaded @pytest.mark.parametrize( 'split_info' , [SplitInfo(), SplitInfo(dataset_name=A__ ), SplitInfo(dataset_name='my_dataset' )] ) def A_ ( A__ ) -> Any: # For backward compatibility, we need asdict(split_dict) to return split info dictrionaries with the "dataset_name" # field even if it's deprecated. This way old versionso of `datasets` can still reload dataset_infos.json files a__ : Any = asdict(SplitDict({'train': split_info} ) ) assert "dataset_name" in split_dict_asdict["train"] assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name
99
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_big_bird import BigBirdTokenizer else: __snake_case = None __snake_case = logging.get_logger(__name__) __snake_case = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} __snake_case = { '''vocab_file''': { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model''', '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model''' ), }, '''tokenizer_file''': { '''google/bigbird-roberta-base''': ( '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/tokenizer.json''' ), '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/tokenizer.json''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/tokenizer.json''' ), }, } __snake_case = { '''google/bigbird-roberta-base''': 40_96, '''google/bigbird-roberta-large''': 40_96, '''google/bigbird-base-trivia-itc''': 40_96, } __snake_case = '''▁''' class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Dict = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = BigBirdTokenizer __lowerCamelCase : Any = ["""input_ids""", """attention_mask"""] __lowerCamelCase : List[int] = [] def __init__( self , snake_case__=None , snake_case__=None , snake_case__="<unk>" , snake_case__="<s>" , snake_case__="</s>" , snake_case__="<pad>" , snake_case__="[SEP]" , snake_case__="[MASK]" , snake_case__="[CLS]" , **snake_case__ , ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else bos_token UpperCAmelCase : Optional[int] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else eos_token UpperCAmelCase : List[str] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else unk_token UpperCAmelCase : Union[str, Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else pad_token UpperCAmelCase : int =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else cls_token UpperCAmelCase : str =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else sep_token # Mask token behave like a normal word, i.e. include the space before it UpperCAmelCase : List[Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else mask_token super().__init__( snake_case__ , tokenizer_file=snake_case__ , bos_token=snake_case__ , eos_token=snake_case__ , unk_token=snake_case__ , sep_token=snake_case__ , pad_token=snake_case__ , cls_token=snake_case__ , mask_token=snake_case__ , **snake_case__ , ) UpperCAmelCase : Tuple =vocab_file UpperCAmelCase : Optional[int] =False if not self.vocab_file else True def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : int =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None , snake_case__ = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is None: return [1] + ([0] * len(snake_case__ )) + [1] return [1] + ([0] * len(snake_case__ )) + [1] + ([0] * len(snake_case__ )) + [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : Optional[Any] =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> Tuple[str]: '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(snake_case__ ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCAmelCase : Optional[int] =os.path.join( snake_case__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case__ ): copyfile(self.vocab_file , snake_case__ ) return (out_vocab_file,)
348
0
"""simple docstring""" import os import re from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __magic_name__ = logging.get_logger(__name__) __magic_name__ = {"vocab_file": "spiece.model"} __magic_name__ = { "vocab_file": { "google/bigbird-roberta-base": "https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model", "google/bigbird-roberta-large": ( "https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model" ), "google/bigbird-base-trivia-itc": ( "https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model" ), } } __magic_name__ = { "google/bigbird-roberta-base": 4096, "google/bigbird-roberta-large": 4096, "google/bigbird-base-trivia-itc": 4096, } class SCREAMING_SNAKE_CASE_ ( __a ): """simple docstring""" __lowercase : Tuple = VOCAB_FILES_NAMES __lowercase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowercase : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Union[str, Any] = ['''input_ids''', '''attention_mask'''] __lowercase : List[int] = [] def __init__( self , lowerCAmelCase__ , lowerCAmelCase__="<unk>" , lowerCAmelCase__="<s>" , lowerCAmelCase__="</s>" , lowerCAmelCase__="<pad>" , lowerCAmelCase__="[SEP]" , lowerCAmelCase__="[MASK]" , lowerCAmelCase__="[CLS]" , lowerCAmelCase__ = None , **lowerCAmelCase__ , ): __SCREAMING_SNAKE_CASE = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else bos_token __SCREAMING_SNAKE_CASE = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else eos_token __SCREAMING_SNAKE_CASE = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else unk_token __SCREAMING_SNAKE_CASE = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else pad_token __SCREAMING_SNAKE_CASE = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else cls_token __SCREAMING_SNAKE_CASE = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else sep_token # Mask token behave like a normal word, i.e. include the space before it __SCREAMING_SNAKE_CASE = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else mask_token __SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , sep_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , cls_token=lowerCAmelCase__ , sp_model_kwargs=self.sp_model_kwargs , **lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE = vocab_file __SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(lowerCAmelCase__) @property def snake_case_ ( self): return self.sp_model.get_piece_size() def snake_case_ ( self): __SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(lowerCAmelCase__): i for i in range(self.vocab_size)} vocab.update(self.added_tokens_encoder) return vocab def __getstate__( self): __SCREAMING_SNAKE_CASE = self.__dict__.copy() __SCREAMING_SNAKE_CASE = None return state def __setstate__( self , lowerCAmelCase__): __SCREAMING_SNAKE_CASE = d # for backward compatibility if not hasattr(self , """sp_model_kwargs"""): __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file) def snake_case_ ( self , lowerCAmelCase__): return self.sp_model.encode(lowerCAmelCase__ , out_type=lowerCAmelCase__) def snake_case_ ( self , lowerCAmelCase__): return self.sp_model.piece_to_id(lowerCAmelCase__) def snake_case_ ( self , lowerCAmelCase__): __SCREAMING_SNAKE_CASE = self.sp_model.IdToPiece(lowerCAmelCase__) return token def snake_case_ ( self , lowerCAmelCase__): __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = """""" __SCREAMING_SNAKE_CASE = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(lowerCAmelCase__) + token __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = [] else: current_sub_tokens.append(lowerCAmelCase__) __SCREAMING_SNAKE_CASE = False out_string += self.sp_model.decode(lowerCAmelCase__) return out_string.strip() def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__ = False , lowerCAmelCase__ = None , lowerCAmelCase__ = True , **lowerCAmelCase__ , ): __SCREAMING_SNAKE_CASE = kwargs.pop("""use_source_tokenizer""" , lowerCAmelCase__) __SCREAMING_SNAKE_CASE = self.convert_ids_to_tokens(lowerCAmelCase__ , skip_special_tokens=lowerCAmelCase__) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(lowerCAmelCase__)) __SCREAMING_SNAKE_CASE = [] sub_texts.append(lowerCAmelCase__) else: current_sub_text.append(lowerCAmelCase__) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(lowerCAmelCase__)) # Mimic the behavior of the Rust tokenizer: # No space before [MASK] and [SEP] if spaces_between_special_tokens: __SCREAMING_SNAKE_CASE = re.sub(R""" (\[(MASK|SEP)\])""" , R"""\1""" , """ """.join(lowerCAmelCase__)) else: __SCREAMING_SNAKE_CASE = """""".join(lowerCAmelCase__) __SCREAMING_SNAKE_CASE = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: __SCREAMING_SNAKE_CASE = self.clean_up_tokenization(lowerCAmelCase__) return clean_text else: return text def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None): if not os.path.isdir(lowerCAmelCase__): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return __SCREAMING_SNAKE_CASE = os.path.join( lowerCAmelCase__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]) if os.path.abspath(self.vocab_file) != os.path.abspath(lowerCAmelCase__) and os.path.isfile(self.vocab_file): copyfile(self.vocab_file , lowerCAmelCase__) elif not os.path.isfile(self.vocab_file): with open(lowerCAmelCase__ , """wb""") as fi: __SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(lowerCAmelCase__) return (out_vocab_file,) def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __SCREAMING_SNAKE_CASE = [self.cls_token_id] __SCREAMING_SNAKE_CASE = [self.sep_token_id] return cls + token_ids_a + sep + token_ids_a + sep def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None , lowerCAmelCase__ = False): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCAmelCase__ , token_ids_a=lowerCAmelCase__ , already_has_special_tokens=lowerCAmelCase__) if token_ids_a is None: return [1] + ([0] * len(lowerCAmelCase__)) + [1] return [1] + ([0] * len(lowerCAmelCase__)) + [1] + ([0] * len(lowerCAmelCase__)) + [1] def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None): __SCREAMING_SNAKE_CASE = [self.sep_token_id] __SCREAMING_SNAKE_CASE = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1]
100
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def lowerCAmelCase_ ( __lowerCAmelCase )-> Optional[Any]: '''simple docstring''' def is_in_circle(__lowerCAmelCase , __lowerCAmelCase ) -> bool: UpperCAmelCase : List[Any] =sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle UpperCAmelCase : List[Any] =mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(__lowerCAmelCase ) ) # The ratio of the area for circle to square is pi/4. UpperCAmelCase : Dict =proportion * 4 print(f'''The estimated value of pi is {pi_estimate}''' ) print(f'''The numpy value of pi is {pi}''' ) print(f'''The total error is {abs(pi - pi_estimate )}''' ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 , )-> float: '''simple docstring''' return mean( function_to_integrate(uniform(__lowerCAmelCase , __lowerCAmelCase ) ) for _ in range(__lowerCAmelCase ) ) * (max_value - min_value) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 )-> None: '''simple docstring''' def identity_function(__lowerCAmelCase ) -> float: return x UpperCAmelCase : List[Any] =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase : Dict =(max_value * max_value - min_value * min_value) / 2 print('''******************''' ) print(f'''Estimating area under y=x where x varies from {min_value} to {max_value}''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {expected_value}''' ) print(f'''Total error is {abs(estimated_value - expected_value )}''' ) print('''******************''' ) def lowerCAmelCase_ ( __lowerCAmelCase )-> None: '''simple docstring''' def function_to_integrate(__lowerCAmelCase ) -> float: return sqrt(4.0 - x * x ) UpperCAmelCase : Dict =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , 0.0 , 2.0 ) print('''******************''' ) print('''Estimating pi using area_under_curve_estimator''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {pi}''' ) print(f'''Total error is {abs(estimated_value - pi )}''' ) print('''******************''' ) if __name__ == "__main__": import doctest doctest.testmod()
348
0
import argparse import io import requests import torch from omegaconf import OmegaConf from diffusers import AutoencoderKL from diffusers.pipelines.stable_diffusion.convert_from_ckpt import ( assign_to_checkpoint, conv_attn_to_linear, create_vae_diffusers_config, renew_vae_attention_paths, renew_vae_resnet_paths, ) def UpperCamelCase ( lowerCAmelCase__ , lowerCAmelCase__ ): '''simple docstring''' lowercase = checkpoint lowercase = {} lowercase = vae_state_dict['''encoder.conv_in.weight'''] lowercase = vae_state_dict['''encoder.conv_in.bias'''] lowercase = vae_state_dict['''encoder.conv_out.weight'''] lowercase = vae_state_dict['''encoder.conv_out.bias'''] lowercase = vae_state_dict['''encoder.norm_out.weight'''] lowercase = vae_state_dict['''encoder.norm_out.bias'''] lowercase = vae_state_dict['''decoder.conv_in.weight'''] lowercase = vae_state_dict['''decoder.conv_in.bias'''] lowercase = vae_state_dict['''decoder.conv_out.weight'''] lowercase = vae_state_dict['''decoder.conv_out.bias'''] lowercase = vae_state_dict['''decoder.norm_out.weight'''] lowercase = vae_state_dict['''decoder.norm_out.bias'''] lowercase = vae_state_dict['''quant_conv.weight'''] lowercase = vae_state_dict['''quant_conv.bias'''] lowercase = vae_state_dict['''post_quant_conv.weight'''] lowercase = vae_state_dict['''post_quant_conv.bias'''] # Retrieves the keys for the encoder down blocks only lowercase = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''encoder.down''' in layer} ) lowercase = { layer_id: [key for key in vae_state_dict if f'down.{layer_id}' in key] for layer_id in range(lowerCAmelCase__ ) } # Retrieves the keys for the decoder up blocks only lowercase = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''decoder.up''' in layer} ) lowercase = { layer_id: [key for key in vae_state_dict if f'up.{layer_id}' in key] for layer_id in range(lowerCAmelCase__ ) } for i in range(lowerCAmelCase__ ): lowercase = [key for key in down_blocks[i] if f'down.{i}' in key and f'down.{i}.downsample' not in key] if f'encoder.down.{i}.downsample.conv.weight' in vae_state_dict: lowercase = vae_state_dict.pop( f'encoder.down.{i}.downsample.conv.weight' ) lowercase = vae_state_dict.pop( f'encoder.down.{i}.downsample.conv.bias' ) lowercase = renew_vae_resnet_paths(lowerCAmelCase__ ) lowercase = {'''old''': f'down.{i}.block', '''new''': f'down_blocks.{i}.resnets'} assign_to_checkpoint(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , additional_replacements=[meta_path] , config=lowerCAmelCase__ ) lowercase = [key for key in vae_state_dict if '''encoder.mid.block''' in key] lowercase = 2 for i in range(1 , num_mid_res_blocks + 1 ): lowercase = [key for key in mid_resnets if f'encoder.mid.block_{i}' in key] lowercase = renew_vae_resnet_paths(lowerCAmelCase__ ) lowercase = {'''old''': f'mid.block_{i}', '''new''': f'mid_block.resnets.{i - 1}'} assign_to_checkpoint(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , additional_replacements=[meta_path] , config=lowerCAmelCase__ ) lowercase = [key for key in vae_state_dict if '''encoder.mid.attn''' in key] lowercase = renew_vae_attention_paths(lowerCAmelCase__ ) lowercase = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , additional_replacements=[meta_path] , config=lowerCAmelCase__ ) conv_attn_to_linear(lowerCAmelCase__ ) for i in range(lowerCAmelCase__ ): lowercase = num_up_blocks - 1 - i lowercase = [ key for key in up_blocks[block_id] if f'up.{block_id}' in key and f'up.{block_id}.upsample' not in key ] if f'decoder.up.{block_id}.upsample.conv.weight' in vae_state_dict: lowercase = vae_state_dict[ f'decoder.up.{block_id}.upsample.conv.weight' ] lowercase = vae_state_dict[ f'decoder.up.{block_id}.upsample.conv.bias' ] lowercase = renew_vae_resnet_paths(lowerCAmelCase__ ) lowercase = {'''old''': f'up.{block_id}.block', '''new''': f'up_blocks.{i}.resnets'} assign_to_checkpoint(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , additional_replacements=[meta_path] , config=lowerCAmelCase__ ) lowercase = [key for key in vae_state_dict if '''decoder.mid.block''' in key] lowercase = 2 for i in range(1 , num_mid_res_blocks + 1 ): lowercase = [key for key in mid_resnets if f'decoder.mid.block_{i}' in key] lowercase = renew_vae_resnet_paths(lowerCAmelCase__ ) lowercase = {'''old''': f'mid.block_{i}', '''new''': f'mid_block.resnets.{i - 1}'} assign_to_checkpoint(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , additional_replacements=[meta_path] , config=lowerCAmelCase__ ) lowercase = [key for key in vae_state_dict if '''decoder.mid.attn''' in key] lowercase = renew_vae_attention_paths(lowerCAmelCase__ ) lowercase = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , additional_replacements=[meta_path] , config=lowerCAmelCase__ ) conv_attn_to_linear(lowerCAmelCase__ ) return new_checkpoint def UpperCamelCase ( lowerCAmelCase__ , lowerCAmelCase__ , ): '''simple docstring''' # Only support V1 lowercase = requests.get( ''' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml''' ) lowercase = io.BytesIO(r.content ) lowercase = OmegaConf.load(lowerCAmelCase__ ) lowercase = 512 lowercase = '''cuda''' if torch.cuda.is_available() else '''cpu''' if checkpoint_path.endswith('''safetensors''' ): from safetensors import safe_open lowercase = {} with safe_open(lowerCAmelCase__ , framework='''pt''' , device='''cpu''' ) as f: for key in f.keys(): lowercase = f.get_tensor(lowerCAmelCase__ ) else: lowercase = torch.load(lowerCAmelCase__ , map_location=lowerCAmelCase__ )['''state_dict'''] # Convert the VAE model. lowercase = create_vae_diffusers_config(lowerCAmelCase__ , image_size=lowerCAmelCase__ ) lowercase = custom_convert_ldm_vae_checkpoint(lowerCAmelCase__ , lowerCAmelCase__ ) lowercase = AutoencoderKL(**lowerCAmelCase__ ) vae.load_state_dict(lowerCAmelCase__ ) vae.save_pretrained(lowerCAmelCase__ ) if __name__ == "__main__": lowercase__ :List[str] = argparse.ArgumentParser() parser.add_argument("--vae_pt_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") lowercase__ :int = parser.parse_args() vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)
101
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __snake_case : def __init__( self , snake_case__ , snake_case__=12 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , snake_case__=0 , snake_case__=None , ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : List[Any] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Union[str, Any] =use_input_mask UpperCAmelCase : Tuple =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : Tuple =hidden_size UpperCAmelCase : Dict =projection_dim UpperCAmelCase : Optional[int] =num_hidden_layers UpperCAmelCase : Dict =num_attention_heads UpperCAmelCase : int =intermediate_size UpperCAmelCase : Any =dropout UpperCAmelCase : Union[str, Any] =attention_dropout UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : str =scope UpperCAmelCase : str =bos_token_id def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : int =None if self.use_input_mask: UpperCAmelCase : Union[str, Any] =random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: UpperCAmelCase : Optional[int] =input_mask.numpy() UpperCAmelCase , UpperCAmelCase : List[Any] =input_mask.shape UpperCAmelCase : Optional[Any] =np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : List[Any] =1 UpperCAmelCase : Tuple =0 UpperCAmelCase : List[Any] =self.get_config() return config, input_ids, tf.convert_to_tensor(snake_case__ ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =TFBlipTextModel(config=snake_case__ ) UpperCAmelCase : List[Any] =model(snake_case__ , attention_mask=snake_case__ , training=snake_case__ ) UpperCAmelCase : str =model(snake_case__ , training=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[Any] =config_and_inputs UpperCAmelCase : Optional[int] ={'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __snake_case ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Optional[int] = (TFBlipTextModel,) if is_tf_available() else () __lowerCamelCase : Dict = False __lowerCamelCase : Optional[Any] = False __lowerCamelCase : Dict = False def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : str =BlipTextModelTester(self ) UpperCAmelCase : Optional[int] =ConfigTester(self , config_class=snake_case__ , hidden_size=37 ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip(reason='''Blip does not use inputs_embeds''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @slow def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase : Optional[Any] =TFBlipTextModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def UpperCAmelCase__ ( self , snake_case__=True ) -> Any: '''simple docstring''' super().test_pt_tf_model_equivalence(allow_missing_keys=snake_case__ )
348
0
"""simple docstring""" import collections import os from typing import List, Optional, Tuple from transformers.utils import is_jieba_available, requires_backends if is_jieba_available(): import jieba from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE : Optional[int] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE : Optional[int] = {"""vocab_file""": """vocab.txt"""} SCREAMING_SNAKE_CASE : Optional[Any] = { """vocab_file""": { """openbmb/cpm-ant-10b""": """https://huggingface.co/openbmb/cpm-ant-10b/blob/main/vocab.txt""", }, } SCREAMING_SNAKE_CASE : List[Any] = { """openbmb/cpm-ant-10b""": 1024, } def lowercase ( _snake_case : List[str] ) ->Optional[Any]: """simple docstring""" __snake_case : int = collections.OrderedDict() with open(_snake_case , '''r''' , encoding='''utf-8''' ) as reader: __snake_case : Optional[int] = reader.readlines() for index, token in enumerate(_snake_case ): __snake_case : Optional[Any] = token.rstrip('''\n''' ) __snake_case : int = index return vocab class _UpperCAmelCase ( __snake_case ): '''simple docstring''' def __init__(self , a_ , a_="<unk>" , a_=2_00 ): '''simple docstring''' __snake_case : Any = vocab __snake_case : str = unk_token __snake_case : Tuple = max_input_chars_per_word def SCREAMING_SNAKE_CASE (self , a_ ): '''simple docstring''' __snake_case : str = list(a_ ) if len(a_ ) > self.max_input_chars_per_word: return [self.unk_token] __snake_case : List[Any] = 0 __snake_case : Optional[Any] = [] while start < len(a_ ): __snake_case : List[str] = len(a_ ) __snake_case : List[Any] = None while start < end: __snake_case : int = ''''''.join(chars[start:end] ) if substr in self.vocab: __snake_case : Dict = substr break end -= 1 if cur_substr is None: sub_tokens.append(self.unk_token ) start += 1 else: sub_tokens.append(a_ ) __snake_case : List[str] = end return sub_tokens class _UpperCAmelCase ( __snake_case ): '''simple docstring''' lowerCamelCase__ =VOCAB_FILES_NAMES lowerCamelCase__ =PRETRAINED_VOCAB_FILES_MAP lowerCamelCase__ =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase__ =['input_ids', 'attention_mask'] lowerCamelCase__ =False def __init__(self , a_ , a_="<d>" , a_="</d>" , a_="<s>" , a_="</s>" , a_="<pad>" , a_="<unk>" , a_="</n>" , a_="</_>" , a_="left" , **a_ , ): '''simple docstring''' requires_backends(self , ['''jieba'''] ) super().__init__( bod_token=a_ , eod_token=a_ , bos_token=a_ , eos_token=a_ , pad_token=a_ , unk_token=a_ , line_token=a_ , space_token=a_ , padding_side=a_ , **a_ , ) __snake_case : Union[str, Any] = bod_token __snake_case : List[Any] = eod_token __snake_case : List[Any] = load_vocab(a_ ) __snake_case : Dict = self.encoder[space_token] __snake_case : Any = self.encoder[line_token] del self.encoder[space_token] del self.encoder[line_token] __snake_case : List[str] = collections.OrderedDict(sorted(self.encoder.items() , key=lambda a_ : x[1] ) ) __snake_case : str = {v: k for k, v in self.encoder.items()} __snake_case : List[Any] = WordpieceTokenizer(vocab=self.encoder , unk_token=self.unk_token ) @property def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' return self.encoder[self.bod_token] @property def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' return self.encoder[self.eod_token] @property def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' return self.encoder["\n"] @property def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' return len(self.encoder ) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' return dict(self.encoder , **self.added_tokens_encoder ) def SCREAMING_SNAKE_CASE (self , a_ ): '''simple docstring''' __snake_case : Any = [] for x in jieba.cut(a_ , cut_all=a_ ): output_tokens.extend(self.wordpiece_tokenizer.tokenize(a_ ) ) return output_tokens def SCREAMING_SNAKE_CASE (self , a_ , **a_ ): '''simple docstring''' __snake_case : Union[str, Any] = [i for i in token_ids if i >= 0] __snake_case : Tuple = [ x for x in token_ids if x != self.pad_token_id and x != self.eos_token_id and x != self.bos_token_id ] return super()._decode(a_ , **a_ ) def SCREAMING_SNAKE_CASE (self , a_ ): '''simple docstring''' return token in self.encoder def SCREAMING_SNAKE_CASE (self , a_ ): '''simple docstring''' return "".join(a_ ) def SCREAMING_SNAKE_CASE (self , a_ ): '''simple docstring''' return self.encoder.get(a_ , self.encoder.get(self.unk_token ) ) def SCREAMING_SNAKE_CASE (self , a_ ): '''simple docstring''' return self.decoder.get(a_ , self.unk_token ) def SCREAMING_SNAKE_CASE (self , a_ , a_ = None ): '''simple docstring''' if os.path.isdir(a_ ): __snake_case : Optional[Any] = os.path.join( a_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) else: __snake_case : Optional[Any] = (filename_prefix + '''-''' if filename_prefix else '''''') + save_directory __snake_case : List[str] = 0 if " " in self.encoder: __snake_case : Optional[Any] = self.encoder[''' '''] del self.encoder[" "] if "\n" in self.encoder: __snake_case : int = self.encoder['''\n'''] del self.encoder["\n"] __snake_case : Dict = collections.OrderedDict(sorted(self.encoder.items() , key=lambda a_ : x[1] ) ) with open(a_ , '''w''' , encoding='''utf-8''' ) as writer: for token, token_index in self.encoder.items(): if index != token_index: logger.warning( f"""Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.""" ''' Please check that the vocabulary is not corrupted!''' ) __snake_case : Union[str, Any] = token_index writer.write(token + '''\n''' ) index += 1 return (vocab_file,) def SCREAMING_SNAKE_CASE (self , a_ , a_ = None ): '''simple docstring''' if token_ids_a is None: return [self.bos_token_id] + token_ids_a return [self.bos_token_id] + token_ids_a + [self.bos_token_id] + token_ids_a def SCREAMING_SNAKE_CASE (self , a_ , a_ = None , a_ = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=a_ , token_ids_a=a_ , already_has_special_tokens=a_ ) if token_ids_a is not None: return [1] + ([0] * len(a_ )) + [1] + ([0] * len(a_ )) return [1] + ([0] * len(a_ ))
102
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging __snake_case = logging.get_logger(__name__) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' UpperCAmelCase : Dict =nn.functional.normalize(__lowerCAmelCase ) UpperCAmelCase : Tuple =nn.functional.normalize(__lowerCAmelCase ) return torch.mm(__lowerCAmelCase , normalized_text_embeds.t() ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : List[str] = CLIPConfig __lowerCamelCase : List[Any] = ["""CLIPEncoderLayer"""] def __init__( self , snake_case__ ) -> Dict: '''simple docstring''' super().__init__(snake_case__ ) UpperCAmelCase : Dict =CLIPVisionModel(config.vision_config ) UpperCAmelCase : Optional[Any] =nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=snake_case__ ) UpperCAmelCase : int =nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : List[str] =nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : str =nn.Parameter(torch.ones(17 ) , requires_grad=snake_case__ ) UpperCAmelCase : Optional[int] =nn.Parameter(torch.ones(3 ) , requires_grad=snake_case__ ) @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Union[str, Any] =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : Optional[Any] =self.visual_projection(snake_case__ ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 UpperCAmelCase : List[str] =cosine_distance(snake_case__ , self.special_care_embeds ).cpu().float().numpy() UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ).cpu().float().numpy() UpperCAmelCase : Tuple =[] UpperCAmelCase : Dict =image_embeds.shape[0] for i in range(snake_case__ ): UpperCAmelCase : str ={'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : str =0.0 for concept_idx in range(len(special_cos_dist[0] ) ): UpperCAmelCase : Optional[Any] =special_cos_dist[i][concept_idx] UpperCAmelCase : Union[str, Any] =self.special_care_embeds_weights[concept_idx].item() UpperCAmelCase : str =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} ) UpperCAmelCase : int =0.01 for concept_idx in range(len(cos_dist[0] ) ): UpperCAmelCase : Any =cos_dist[i][concept_idx] UpperCAmelCase : Optional[int] =self.concept_embeds_weights[concept_idx].item() UpperCAmelCase : int =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(snake_case__ ) result.append(snake_case__ ) UpperCAmelCase : Optional[int] =[len(res['''bad_concepts'''] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Any =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : List[str] =self.visual_projection(snake_case__ ) UpperCAmelCase : Any =cosine_distance(snake_case__ , self.special_care_embeds ) UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : Optional[Any] =0.0 UpperCAmelCase : Any =special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) UpperCAmelCase : str =torch.any(special_scores > 0 , dim=1 ) UpperCAmelCase : List[Any] =special_care * 0.01 UpperCAmelCase : Union[str, Any] =special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) UpperCAmelCase : List[Any] =(cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) UpperCAmelCase : str =torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
348
0
def UpperCamelCase( __UpperCamelCase : int ,__UpperCamelCase : int ): if a < 0 or b < 0: raise ValueError('''the value of both inputs must be positive''' ) lowerCAmelCase_ : Optional[int] = str(bin(__UpperCamelCase ) )[2:] # remove the leading "0b" lowerCAmelCase_ : List[Any] = str(bin(__UpperCamelCase ) )[2:] # remove the leading "0b" lowerCAmelCase_ : Union[str, Any] = max(len(__UpperCamelCase ) ,len(__UpperCamelCase ) ) return "0b" + "".join( str(int(char_a == '''1''' and char_b == '''1''' ) ) for char_a, char_b in zip(a_binary.zfill(__UpperCamelCase ) ,b_binary.zfill(__UpperCamelCase ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
103
import argparse import intel_extension_for_pytorch as ipex import torch from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline __snake_case = argparse.ArgumentParser('''Stable Diffusion script with intel optimization''', add_help=False) parser.add_argument('''--dpm''', action='''store_true''', help='''Enable DPMSolver or not''') parser.add_argument('''--steps''', default=None, type=int, help='''Num inference steps''') __snake_case = parser.parse_args() __snake_case = '''cpu''' __snake_case = '''a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings''' __snake_case = '''path-to-your-trained-model''' __snake_case = StableDiffusionPipeline.from_pretrained(model_id) if args.dpm: __snake_case = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) __snake_case = pipe.to(device) # to channels last __snake_case = pipe.unet.to(memory_format=torch.channels_last) __snake_case = pipe.vae.to(memory_format=torch.channels_last) __snake_case = pipe.text_encoder.to(memory_format=torch.channels_last) if pipe.requires_safety_checker: __snake_case = pipe.safety_checker.to(memory_format=torch.channels_last) # optimize with ipex __snake_case = torch.randn(2, 4, 64, 64) __snake_case = torch.rand(1) * 9_99 __snake_case = torch.randn(2, 77, 7_68) __snake_case = (sample, timestep, encoder_hidden_status) try: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example) except Exception: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True) if pipe.requires_safety_checker: __snake_case = ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True) # compute __snake_case = 6_66 __snake_case = torch.Generator(device).manual_seed(seed) __snake_case = {'''generator''': generator} if args.steps is not None: __snake_case = args.steps with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa): __snake_case = pipe(prompt, **generate_kwargs).images[0] # save image image.save('''generated.png''')
348
0
'''simple docstring''' import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor lowerCAmelCase__ = logging.get_logger(__name__) class lowercase_ (lowerCamelCase__ ): """simple docstring""" def __init__( self : Dict ,*lowercase__ : Tuple ,**lowercase__ : List[Any] ): warnings.warn( '''The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use MobileViTImageProcessor instead.''' ,lowercase__ ,) super().__init__(*lowercase__ ,**lowercase__ )
104
__snake_case = '''Input must be a string of 8 numbers plus letter''' __snake_case = '''TRWAGMYFPDXBNJZSQVHLCKE''' def lowerCAmelCase_ ( __lowerCAmelCase )-> bool: '''simple docstring''' if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCAmelCase : Optional[Any] =f'''Expected string as input, found {type(__lowerCAmelCase ).__name__}''' raise TypeError(__lowerCAmelCase ) UpperCAmelCase : List[Any] =spanish_id.replace('''-''' , '''''' ).upper() if len(__lowerCAmelCase ) != 9: raise ValueError(__lowerCAmelCase ) try: UpperCAmelCase : int =int(spanish_id_clean[0:8] ) UpperCAmelCase : Optional[int] =spanish_id_clean[8] except ValueError as ex: raise ValueError(__lowerCAmelCase ) from ex if letter.isdigit(): raise ValueError(__lowerCAmelCase ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
348
0
"""simple docstring""" import unittest from transformers import PegasusConfig, PegasusTokenizer, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html a : Tuple = '''platform''' import jax import jax.numpy as jnp import numpy as np from transformers import FlaxPegasusForConditionalGeneration, FlaxPegasusModel @require_flax class __UpperCamelCase : lowerCamelCase : Any =PegasusConfig lowerCamelCase : Optional[Any] ={} lowerCamelCase : Dict ="""gelu""" def __init__( self , lowerCAmelCase__ , lowerCAmelCase__=13 , lowerCAmelCase__=7 , lowerCAmelCase__=True , lowerCAmelCase__=False , lowerCAmelCase__=99 , lowerCAmelCase__=32 , lowerCAmelCase__=5 , lowerCAmelCase__=4 , lowerCAmelCase__=37 , lowerCAmelCase__=0.1 , lowerCAmelCase__=0.1 , lowerCAmelCase__=20 , lowerCAmelCase__=2 , lowerCAmelCase__=1 , lowerCAmelCase__=0 , ) -> List[Any]: a : str = parent a : Optional[Any] = batch_size a : Optional[Any] = seq_length a : int = is_training a : Any = use_labels a : Tuple = vocab_size a : List[str] = hidden_size a : Union[str, Any] = num_hidden_layers a : List[str] = num_attention_heads a : List[str] = intermediate_size a : List[Any] = hidden_dropout_prob a : Union[str, Any] = attention_probs_dropout_prob a : str = max_position_embeddings a : Dict = eos_token_id a : List[str] = pad_token_id a : Dict = bos_token_id def __a ( self ) -> List[Any]: a : str = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ).clip(3 , self.vocab_size ) a : List[str] = np.expand_dims(np.array([self.eos_token_id] * self.batch_size ) , 1 ) a : List[Any] = np.concatenate([input_ids, eos_tensor] , axis=1 ) a : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) a : Tuple = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) a : Dict = prepare_pegasus_inputs_dict(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) return config, inputs_dict def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> Any: a : List[str] = 20 a : Dict = model_class_name(lowerCAmelCase__ ) a : Union[str, Any] = model.encode(inputs_dict["input_ids"] ) a, a : str = ( inputs_dict["decoder_input_ids"], inputs_dict["decoder_attention_mask"], ) a : Union[str, Any] = model.init_cache(decoder_input_ids.shape[0] , lowerCAmelCase__ , lowerCAmelCase__ ) a : Union[str, Any] = jnp.ones((decoder_input_ids.shape[0], max_decoder_length) , dtype="i4" ) a : int = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) a : Any = model.decode( decoder_input_ids[:, :-1] , lowerCAmelCase__ , decoder_attention_mask=lowerCAmelCase__ , past_key_values=lowerCAmelCase__ , decoder_position_ids=lowerCAmelCase__ , ) a : int = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype="i4" ) a : List[str] = model.decode( decoder_input_ids[:, -1:] , lowerCAmelCase__ , decoder_attention_mask=lowerCAmelCase__ , past_key_values=outputs_cache.past_key_values , decoder_position_ids=lowerCAmelCase__ , ) a : int = model.decode(lowerCAmelCase__ , lowerCAmelCase__ ) a : List[Any] = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=f"""Max diff is {diff}""" ) def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> List[str]: a : Any = 20 a : List[Any] = model_class_name(lowerCAmelCase__ ) a : str = model.encode(inputs_dict["input_ids"] ) a, a : Union[str, Any] = ( inputs_dict["decoder_input_ids"], inputs_dict["decoder_attention_mask"], ) a : Tuple = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ] , axis=-1 , ) a : str = model.init_cache(decoder_input_ids.shape[0] , lowerCAmelCase__ , lowerCAmelCase__ ) a : Tuple = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) a : Union[str, Any] = model.decode( decoder_input_ids[:, :-1] , lowerCAmelCase__ , decoder_attention_mask=lowerCAmelCase__ , past_key_values=lowerCAmelCase__ , decoder_position_ids=lowerCAmelCase__ , ) a : Tuple = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype="i4" ) a : Optional[int] = model.decode( decoder_input_ids[:, -1:] , lowerCAmelCase__ , past_key_values=outputs_cache.past_key_values , decoder_attention_mask=lowerCAmelCase__ , decoder_position_ids=lowerCAmelCase__ , ) a : Optional[int] = model.decode(lowerCAmelCase__ , lowerCAmelCase__ , decoder_attention_mask=lowerCAmelCase__ ) a : Any = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=f"""Max diff is {diff}""" ) def _SCREAMING_SNAKE_CASE ( _lowercase : str , _lowercase : Any , _lowercase : Tuple , _lowercase : List[Any]=None , _lowercase : str=None , ) ->List[Any]: '''simple docstring''' if attention_mask is None: a : Union[str, Any] = np.not_equal(_lowercase , config.pad_token_id ).astype(np.inta ) if decoder_attention_mask is None: a : Optional[int] = np.concatenate( [ np.ones(decoder_input_ids[:, :1].shape , dtype=np.inta ), np.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ).astype(np.inta ), ] , axis=-1 , ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, } @require_flax class __UpperCamelCase ( a__ , unittest.TestCase ): lowerCamelCase : List[str] =( ( FlaxPegasusForConditionalGeneration, FlaxPegasusModel, ) if is_flax_available() else () ) lowerCamelCase : str =(FlaxPegasusForConditionalGeneration,) if is_flax_available() else () lowerCamelCase : List[Any] =True lowerCamelCase : Tuple =False lowerCamelCase : Any =False lowerCamelCase : Optional[Any] =False def __a ( self ) -> List[Any]: a : Tuple = FlaxPegasusModelTester(self ) a : Dict = ConfigTester(self , config_class=lowerCAmelCase__ ) def __a ( self ) -> Optional[int]: self.config_tester.run_common_tests() def __a ( self ) -> int: a, a : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def __a ( self ) -> str: a, a : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def __a ( self ) -> Optional[int]: a, a : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): a : Dict = self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ ) a : Any = model_class(lowerCAmelCase__ ) @jax.jit def encode_jitted(lowerCAmelCase__ , lowerCAmelCase__=None , **lowerCAmelCase__ ): return model.encode(input_ids=lowerCAmelCase__ , attention_mask=lowerCAmelCase__ ) with self.subTest("JIT Enabled" ): a : Optional[Any] = encode_jitted(**lowerCAmelCase__ ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): a : Optional[Any] = encode_jitted(**lowerCAmelCase__ ).to_tuple() self.assertEqual(len(lowerCAmelCase__ ) , len(lowerCAmelCase__ ) ) for jitted_output, output in zip(lowerCAmelCase__ , lowerCAmelCase__ ): self.assertEqual(jitted_output.shape , output.shape ) def __a ( self ) -> int: a, a : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): a : str = model_class(lowerCAmelCase__ ) a : Union[str, Any] = model.encode(inputs_dict["input_ids"] , inputs_dict["attention_mask"] ) a : str = { "decoder_input_ids": inputs_dict["decoder_input_ids"], "decoder_attention_mask": inputs_dict["decoder_attention_mask"], "encoder_outputs": encoder_outputs, } @jax.jit def decode_jitted(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ): return model.decode( decoder_input_ids=lowerCAmelCase__ , decoder_attention_mask=lowerCAmelCase__ , encoder_outputs=lowerCAmelCase__ , ) with self.subTest("JIT Enabled" ): a : Dict = decode_jitted(**lowerCAmelCase__ ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): a : Any = decode_jitted(**lowerCAmelCase__ ).to_tuple() self.assertEqual(len(lowerCAmelCase__ ) , len(lowerCAmelCase__ ) ) for jitted_output, output in zip(lowerCAmelCase__ , lowerCAmelCase__ ): self.assertEqual(jitted_output.shape , output.shape ) @slow def __a ( self ) -> Any: for model_class_name in self.all_model_classes: a : List[Any] = model_class_name.from_pretrained("google/pegasus-large" , from_pt=lowerCAmelCase__ ) a : Any = np.ones((1, 1) ) a : Dict = model(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) @slow def __a ( self ) -> Optional[int]: a : Tuple = FlaxPegasusForConditionalGeneration.from_pretrained("google/pegasus-xsum" ) a : List[str] = PegasusTokenizer.from_pretrained("google/pegasus-xsum" ) a : Tuple = [ " PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow.", " The London trio are up for best UK act and best album, as well as getting two nominations in the best song category.\"We got told like this morning 'Oh I think you're nominated'\", said Dappy.\"And I was like 'Oh yeah, which one?' And now we've got nominated for four awards. I mean, wow!\"Bandmate Fazer added: \"We thought it's best of us to come down and mingle with everyone and say hello to the cameras. And now we find we've got four nominations.\"The band have two shots at the best song prize, getting the nod for their Tynchy Stryder collaboration Number One, and single Strong Again.Their album Uncle B will also go up against records by the likes of Beyonce and Kanye West.N-Dubz picked up the best newcomer Mobo in 2007, but female member Tulisa said they wouldn't be too disappointed if they didn't win this time around.\"At the end of the day we're grateful to be where we are in our careers.\"If it don't happen then it don't happen - live to fight another day and keep on making albums and hits for the fans.\"Dappy also revealed they could be performing live several times on the night.The group will be doing Number One and also a possible rendition of the War Child single, I Got Soul.The charity song is a re-working of The Killers' All These Things That I've Done and is set to feature artists like Chipmunk, Ironik and Pixie Lott.This year's Mobos will be held outside of London for the first time, in Glasgow on 30 September.N-Dubz said they were looking forward to performing for their Scottish fans and boasted about their recent shows north of the border.\"We just done Edinburgh the other day,\" said Dappy.\"We smashed up an N-Dubz show over there. We done Aberdeen about three or four months ago - we smashed up that show over there! Everywhere we go we smash it up!\" ", ] a : Any = [ "California's largest electricity provider has turned off power to hundreds of thousands of customers.", "Pop group N-Dubz have revealed they were surprised to get four nominations for this year's Mobo Awards.", ] a : Tuple = tokenizer(lowerCAmelCase__ , return_tensors="np" , truncation=lowerCAmelCase__ , max_length=512 , padding=lowerCAmelCase__ ) a : str = model.generate(**lowerCAmelCase__ , num_beams=2 ).sequences a : List[str] = tokenizer.batch_decode(lowerCAmelCase__ , skip_special_tokens=lowerCAmelCase__ ) assert tgt_text == decoded
105
def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Dict =str(bin(__lowerCAmelCase ) ) binary_number += "0" * shift_amount return binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Any =str(bin(__lowerCAmelCase ) )[2:] if shift_amount >= len(__lowerCAmelCase ): return "0b0" UpperCAmelCase : Optional[Any] =binary_number[: len(__lowerCAmelCase ) - shift_amount] return "0b" + shifted_binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number >= 0: # Get binary representation of positive number UpperCAmelCase : Optional[Any] ='''0''' + str(bin(__lowerCAmelCase ) ).strip('''-''' )[2:] else: # Get binary (2's complement) representation of negative number UpperCAmelCase : int =len(bin(__lowerCAmelCase )[3:] ) # Find 2's complement of number UpperCAmelCase : Any =bin(abs(__lowerCAmelCase ) - (1 << binary_number_length) )[3:] UpperCAmelCase : Optional[Any] =( '''1''' + '''0''' * (binary_number_length - len(__lowerCAmelCase )) + binary_number ) if shift_amount >= len(__lowerCAmelCase ): return "0b" + binary_number[0] * len(__lowerCAmelCase ) return ( "0b" + binary_number[0] * shift_amount + binary_number[: len(__lowerCAmelCase ) - shift_amount] ) if __name__ == "__main__": import doctest doctest.testmod()
348
0
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __UpperCamelCase : int = logging.get_logger(__name__) __UpperCamelCase : Union[str, Any] = { '''microsoft/swin-tiny-patch4-window7-224''': ( '''https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json''' ), # See all Swin models at https://huggingface.co/models?filter=swin } class SCREAMING_SNAKE_CASE ( a_ , a_ ): """simple docstring""" lowercase__ = "swin" lowercase__ = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : str ,lowercase_ : List[str]=2_2_4 ,lowercase_ : Union[str, Any]=4 ,lowercase_ : Dict=3 ,lowercase_ : Tuple=9_6 ,lowercase_ : Any=[2, 2, 6, 2] ,lowercase_ : Any=[3, 6, 1_2, 2_4] ,lowercase_ : str=7 ,lowercase_ : Optional[Any]=4.0 ,lowercase_ : Optional[int]=True ,lowercase_ : Union[str, Any]=0.0 ,lowercase_ : Optional[Any]=0.0 ,lowercase_ : int=0.1 ,lowercase_ : Dict="gelu" ,lowercase_ : Optional[Any]=False ,lowercase_ : Any=0.02 ,lowercase_ : str=1E-5 ,lowercase_ : str=3_2 ,lowercase_ : List[Any]=None ,lowercase_ : Union[str, Any]=None ,**lowercase_ : List[str] ,): super().__init__(**lowercase_ ) lowerCAmelCase__ : Union[str, Any] = image_size lowerCAmelCase__ : List[str] = patch_size lowerCAmelCase__ : Optional[Any] = num_channels lowerCAmelCase__ : str = embed_dim lowerCAmelCase__ : Optional[int] = depths lowerCAmelCase__ : Tuple = len(lowercase_ ) lowerCAmelCase__ : List[Any] = num_heads lowerCAmelCase__ : str = window_size lowerCAmelCase__ : str = mlp_ratio lowerCAmelCase__ : Optional[Any] = qkv_bias lowerCAmelCase__ : str = hidden_dropout_prob lowerCAmelCase__ : List[str] = attention_probs_dropout_prob lowerCAmelCase__ : List[str] = drop_path_rate lowerCAmelCase__ : Dict = hidden_act lowerCAmelCase__ : int = use_absolute_embeddings lowerCAmelCase__ : Tuple = layer_norm_eps lowerCAmelCase__ : Dict = initializer_range lowerCAmelCase__ : List[str] = encoder_stride # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowerCAmelCase__ : Any = int(embed_dim * 2 ** (len(lowercase_ ) - 1) ) lowerCAmelCase__ : List[str] = ['''stem'''] + [F'stage{idx}' for idx in range(1 ,len(lowercase_ ) + 1 )] lowerCAmelCase__ ,lowerCAmelCase__ : Tuple = get_aligned_output_features_output_indices( out_features=lowercase_ ,out_indices=lowercase_ ,stage_names=self.stage_names ) class SCREAMING_SNAKE_CASE ( a_ ): """simple docstring""" lowercase__ = version.parse("1.11" ) @property def __lowerCAmelCase ( self : Optional[Any] ): return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __lowerCAmelCase ( self : str ): return 1E-4
106
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) # TODO Update this __snake_case = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Tuple = """esm""" def __init__( self , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=1026 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__="absolute" , snake_case__=True , snake_case__=None , snake_case__=False , snake_case__=False , snake_case__=None , snake_case__=None , **snake_case__ , ) -> Union[str, Any]: '''simple docstring''' super().__init__(pad_token_id=snake_case__ , mask_token_id=snake_case__ , **snake_case__ ) UpperCAmelCase : List[str] =vocab_size UpperCAmelCase : str =hidden_size UpperCAmelCase : List[Any] =num_hidden_layers UpperCAmelCase : Optional[Any] =num_attention_heads UpperCAmelCase : str =intermediate_size UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : int =attention_probs_dropout_prob UpperCAmelCase : Dict =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : Union[str, Any] =layer_norm_eps UpperCAmelCase : Dict =position_embedding_type UpperCAmelCase : Optional[Any] =use_cache UpperCAmelCase : int =emb_layer_norm_before UpperCAmelCase : List[str] =token_dropout UpperCAmelCase : Optional[Any] =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) UpperCAmelCase : Optional[Any] =EsmFoldConfig() elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =EsmFoldConfig(**snake_case__ ) UpperCAmelCase : Tuple =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) UpperCAmelCase : Any =get_default_vocab_list() else: UpperCAmelCase : Tuple =vocab_list else: UpperCAmelCase : Optional[int] =None UpperCAmelCase : Union[str, Any] =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , snake_case__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =super().to_dict() if isinstance(self.esmfold_config , snake_case__ ): UpperCAmelCase : str =self.esmfold_config.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : str = None __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : float = 0 __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : int = 128 __lowerCamelCase : "TrunkConfig" = None def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' if self.trunk is None: UpperCAmelCase : str =TrunkConfig() elif isinstance(self.trunk , snake_case__ ): UpperCAmelCase : Optional[int] =TrunkConfig(**self.trunk ) def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[Any] =asdict(self ) UpperCAmelCase : Any =self.trunk.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 48 __lowerCamelCase : int = 1024 __lowerCamelCase : int = 128 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : float = 0 __lowerCamelCase : float = 0 __lowerCamelCase : bool = False __lowerCamelCase : int = 4 __lowerCamelCase : Optional[int] = 128 __lowerCamelCase : "StructureModuleConfig" = None def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' if self.structure_module is None: UpperCAmelCase : Any =StructureModuleConfig() elif isinstance(self.structure_module , snake_case__ ): UpperCAmelCase : str =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f'''`max_recycles` should be positive, got {self.max_recycles}.''' ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f''' {self.sequence_state_dim} and {self.sequence_state_dim}.''' ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f''' {self.pairwise_state_dim} and {self.pairwise_state_dim}.''' ) UpperCAmelCase : Optional[int] =self.sequence_state_dim // self.sequence_head_width UpperCAmelCase : Any =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f''' {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.''' ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f''' {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.''' ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f'''`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.''' ) if self.dropout >= 0.4: raise ValueError(f'''`dropout` should not be greater than 0.4, got {self.dropout}.''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =asdict(self ) UpperCAmelCase : Tuple =self.structure_module.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 384 __lowerCamelCase : int = 128 __lowerCamelCase : int = 16 __lowerCamelCase : int = 128 __lowerCamelCase : int = 12 __lowerCamelCase : int = 4 __lowerCamelCase : int = 8 __lowerCamelCase : float = 0.1 __lowerCamelCase : int = 8 __lowerCamelCase : int = 1 __lowerCamelCase : int = 2 __lowerCamelCase : int = 7 __lowerCamelCase : int = 10 __lowerCamelCase : float = 1E-8 __lowerCamelCase : float = 1E5 def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return asdict(self ) def lowerCAmelCase_ ( )-> Tuple: '''simple docstring''' return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
348
0
import math def __magic_name__ ( A : int = 100 ): '''simple docstring''' a = sum(i * i for i in range(1, n + 1 ) ) a = int(math.pow(sum(range(1, n + 1 ) ), 2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(F'''{solution() = }''')
107
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[int] = (KDPMaDiscreteScheduler,) __lowerCamelCase : List[str] = 10 def UpperCAmelCase__ ( self , **snake_case__ ) -> str: '''simple docstring''' UpperCAmelCase : int ={ '''num_train_timesteps''': 1100, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', } config.update(**snake_case__ ) return config def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=snake_case__ , beta_end=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' UpperCAmelCase : Optional[Any] =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config(prediction_type='''v_prediction''' ) UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : str =self.dummy_model() UpperCAmelCase : Optional[Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : Union[str, Any] =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : str =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Any =model(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : int =output.prev_sample UpperCAmelCase : Dict =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Optional[Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.69_34e-07 ) < 1e-2 assert abs(result_mean.item() - 6.11_12e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.6_93_42_86_50_17_09_72e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0002 ) < 1e-3 def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : Any =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config() UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : Optional[int] =self.dummy_model() UpperCAmelCase : Union[str, Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : str =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : Dict =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =model(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =output.prev_sample UpperCAmelCase : Any =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Union[str, Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : List[Any] =self.scheduler_classes[0] UpperCAmelCase : Dict =self.get_scheduler_config() UpperCAmelCase : List[str] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps , device=snake_case__ ) UpperCAmelCase : int =self.dummy_model() UpperCAmelCase : Tuple =self.dummy_sample_deter.to(snake_case__ ) * scheduler.init_noise_sigma for t in scheduler.timesteps: UpperCAmelCase : Optional[Any] =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : int =model(snake_case__ , snake_case__ ) UpperCAmelCase : str =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =output.prev_sample UpperCAmelCase : List[str] =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Dict =torch.mean(torch.abs(snake_case__ ) ) if str(snake_case__ ).startswith('''cpu''' ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3
348
0
"""simple docstring""" from collections.abc import Iterable from typing import Generic, TypeVar lowerCAmelCase__ = TypeVar('''_T''') class SCREAMING_SNAKE_CASE__ ( Generic[_T] ): """simple docstring""" def __init__( self , snake_case__ = None ): """simple docstring""" lowerCAmelCase : list[_T] = list(iterable or [] ) lowerCAmelCase : list[_T] = [] def __len__( self ): """simple docstring""" return len(self._stacka ) + len(self._stacka ) def __repr__( self ): """simple docstring""" return f"""Queue({tuple(self._stacka[::-1] + self._stacka )})""" def lowercase__ ( self , snake_case__ ): """simple docstring""" self._stacka.append(snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = self._stacka.pop lowerCAmelCase : List[str] = self._stacka.append if not self._stacka: while self._stacka: stacka_append(stacka_pop() ) if not self._stacka: raise IndexError("Queue is empty" ) return self._stacka.pop() if __name__ == "__main__": from doctest import testmod testmod()
108
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class __snake_case ( unittest.TestCase ): @slow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Any =FlaxMTaForConditionalGeneration.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : Tuple =AutoTokenizer.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : List[str] =tokenizer('''Hello there''' , return_tensors='''np''' ).input_ids UpperCAmelCase : List[Any] =tokenizer('''Hi I am''' , return_tensors='''np''' ).input_ids UpperCAmelCase : Union[str, Any] =shift_tokens_right(snake_case__ , model.config.pad_token_id , model.config.decoder_start_token_id ) UpperCAmelCase : List[str] =model(snake_case__ , decoder_input_ids=snake_case__ ).logits UpperCAmelCase : Any =optax.softmax_cross_entropy(snake_case__ , onehot(snake_case__ , logits.shape[-1] ) ).mean() UpperCAmelCase : Union[str, Any] =-(labels.shape[-1] * loss.item()) UpperCAmelCase : List[str] =-84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
348
0
"""simple docstring""" import argparse import os import torch from diffusers import ( CMStochasticIterativeScheduler, ConsistencyModelPipeline, UNetaDModel, ) A: List[str] = { "sample_size": 3_2, "in_channels": 3, "out_channels": 3, "layers_per_block": 2, "num_class_embeds": 1_0_0_0, "block_out_channels": [3_2, 6_4], "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", } A: Optional[int] = { "sample_size": 6_4, "in_channels": 3, "out_channels": 3, "layers_per_block": 3, "num_class_embeds": 1_0_0_0, "block_out_channels": [1_9_2, 1_9_2 * 2, 1_9_2 * 3, 1_9_2 * 4], "attention_head_dim": 6_4, "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", } A: Union[str, Any] = { "sample_size": 2_5_6, "in_channels": 3, "out_channels": 3, "layers_per_block": 2, "num_class_embeds": None, "block_out_channels": [2_5_6, 2_5_6, 2_5_6 * 2, 2_5_6 * 2, 2_5_6 * 4, 2_5_6 * 4], "attention_head_dim": 6_4, "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", } A: int = { "num_train_timesteps": 4_0, "sigma_min": 0.002, "sigma_max": 80.0, } A: int = { "num_train_timesteps": 2_0_1, "sigma_min": 0.002, "sigma_max": 80.0, } A: Union[str, Any] = { "num_train_timesteps": 1_5_1, "sigma_min": 0.002, "sigma_max": 80.0, } def _snake_case ( UpperCamelCase : List[Any] ): if isinstance(UpperCamelCase , UpperCamelCase ): 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 _snake_case ( UpperCamelCase : Optional[int] , UpperCamelCase : Dict , UpperCamelCase : List[Any] , UpperCamelCase : str , UpperCamelCase : List[Any]=False ): UpperCAmelCase : Dict = checkpoint[F"{old_prefix}.in_layers.0.weight"] UpperCAmelCase : Any = checkpoint[F"{old_prefix}.in_layers.0.bias"] UpperCAmelCase : Optional[Any] = checkpoint[F"{old_prefix}.in_layers.2.weight"] UpperCAmelCase : List[Any] = checkpoint[F"{old_prefix}.in_layers.2.bias"] UpperCAmelCase : Optional[Any] = checkpoint[F"{old_prefix}.emb_layers.1.weight"] UpperCAmelCase : Optional[Any] = checkpoint[F"{old_prefix}.emb_layers.1.bias"] UpperCAmelCase : Union[str, Any] = checkpoint[F"{old_prefix}.out_layers.0.weight"] UpperCAmelCase : Dict = checkpoint[F"{old_prefix}.out_layers.0.bias"] UpperCAmelCase : str = checkpoint[F"{old_prefix}.out_layers.3.weight"] UpperCAmelCase : str = checkpoint[F"{old_prefix}.out_layers.3.bias"] if has_skip: UpperCAmelCase : List[Any] = checkpoint[F"{old_prefix}.skip_connection.weight"] UpperCAmelCase : Optional[Any] = checkpoint[F"{old_prefix}.skip_connection.bias"] return new_checkpoint def _snake_case ( UpperCamelCase : int , UpperCamelCase : Any , UpperCamelCase : List[str] , UpperCamelCase : str , UpperCamelCase : Optional[Any]=None ): UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Dict = checkpoint[F"{old_prefix}.qkv.weight"].chunk(3 , dim=0 ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Optional[Any] = checkpoint[F"{old_prefix}.qkv.bias"].chunk(3 , dim=0 ) UpperCAmelCase : List[str] = checkpoint[F"{old_prefix}.norm.weight"] UpperCAmelCase : List[Any] = checkpoint[F"{old_prefix}.norm.bias"] UpperCAmelCase : Dict = weight_q.squeeze(-1 ).squeeze(-1 ) UpperCAmelCase : List[Any] = bias_q.squeeze(-1 ).squeeze(-1 ) UpperCAmelCase : List[Any] = weight_k.squeeze(-1 ).squeeze(-1 ) UpperCAmelCase : Optional[Any] = bias_k.squeeze(-1 ).squeeze(-1 ) UpperCAmelCase : Tuple = weight_v.squeeze(-1 ).squeeze(-1 ) UpperCAmelCase : str = bias_v.squeeze(-1 ).squeeze(-1 ) UpperCAmelCase : Optional[int] = ( checkpoint[F"{old_prefix}.proj_out.weight"].squeeze(-1 ).squeeze(-1 ) ) UpperCAmelCase : List[str] = checkpoint[F"{old_prefix}.proj_out.bias"].squeeze(-1 ).squeeze(-1 ) return new_checkpoint def _snake_case ( UpperCamelCase : str , UpperCamelCase : List[str] ): UpperCAmelCase : Tuple = torch.load(UpperCamelCase , map_location="""cpu""" ) UpperCAmelCase : str = {} UpperCAmelCase : int = checkpoint["""time_embed.0.weight"""] UpperCAmelCase : Union[str, Any] = checkpoint["""time_embed.0.bias"""] UpperCAmelCase : List[Any] = checkpoint["""time_embed.2.weight"""] UpperCAmelCase : str = checkpoint["""time_embed.2.bias"""] if unet_config["num_class_embeds"] is not None: UpperCAmelCase : List[Any] = checkpoint["""label_emb.weight"""] UpperCAmelCase : Optional[Any] = checkpoint["""input_blocks.0.0.weight"""] UpperCAmelCase : List[str] = checkpoint["""input_blocks.0.0.bias"""] UpperCAmelCase : Optional[Any] = unet_config["""down_block_types"""] UpperCAmelCase : Any = unet_config["""layers_per_block"""] UpperCAmelCase : Any = unet_config["""attention_head_dim"""] UpperCAmelCase : Optional[int] = unet_config["""block_out_channels"""] UpperCAmelCase : Tuple = 1 UpperCAmelCase : Dict = channels_list[0] for i, layer_type in enumerate(UpperCamelCase ): UpperCAmelCase : List[Any] = channels_list[i] UpperCAmelCase : Any = current_channels != prev_channels if layer_type == "ResnetDownsampleBlock2D": for j in range(UpperCamelCase ): UpperCAmelCase : List[Any] = F"down_blocks.{i}.resnets.{j}" UpperCAmelCase : Union[str, Any] = F"input_blocks.{current_layer}.0" UpperCAmelCase : Union[str, Any] = True if j == 0 and downsample_block_has_skip else False UpperCAmelCase : int = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , has_skip=UpperCamelCase ) current_layer += 1 elif layer_type == "AttnDownBlock2D": for j in range(UpperCamelCase ): UpperCAmelCase : List[Any] = F"down_blocks.{i}.resnets.{j}" UpperCAmelCase : List[Any] = F"input_blocks.{current_layer}.0" UpperCAmelCase : int = True if j == 0 and downsample_block_has_skip else False UpperCAmelCase : Tuple = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , has_skip=UpperCamelCase ) UpperCAmelCase : Dict = F"down_blocks.{i}.attentions.{j}" UpperCAmelCase : int = F"input_blocks.{current_layer}.1" UpperCAmelCase : List[Any] = convert_attention( UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) current_layer += 1 if i != len(UpperCamelCase ) - 1: UpperCAmelCase : Tuple = F"down_blocks.{i}.downsamplers.0" UpperCAmelCase : Tuple = F"input_blocks.{current_layer}.0" UpperCAmelCase : Union[str, Any] = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) current_layer += 1 UpperCAmelCase : List[str] = current_channels # hardcoded the mid-block for now UpperCAmelCase : Any = """mid_block.resnets.0""" UpperCAmelCase : Optional[Any] = """middle_block.0""" UpperCAmelCase : Optional[Any] = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) UpperCAmelCase : str = """mid_block.attentions.0""" UpperCAmelCase : Optional[int] = """middle_block.1""" UpperCAmelCase : Dict = convert_attention(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) UpperCAmelCase : Any = """mid_block.resnets.1""" UpperCAmelCase : Tuple = """middle_block.2""" UpperCAmelCase : Tuple = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) UpperCAmelCase : List[Any] = 0 UpperCAmelCase : str = unet_config["""up_block_types"""] for i, layer_type in enumerate(UpperCamelCase ): if layer_type == "ResnetUpsampleBlock2D": for j in range(layers_per_block + 1 ): UpperCAmelCase : str = F"up_blocks.{i}.resnets.{j}" UpperCAmelCase : str = F"output_blocks.{current_layer}.0" UpperCAmelCase : Dict = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , has_skip=UpperCamelCase ) current_layer += 1 if i != len(UpperCamelCase ) - 1: UpperCAmelCase : Union[str, Any] = F"up_blocks.{i}.upsamplers.0" UpperCAmelCase : str = F"output_blocks.{current_layer-1}.1" UpperCAmelCase : List[str] = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) elif layer_type == "AttnUpBlock2D": for j in range(layers_per_block + 1 ): UpperCAmelCase : List[str] = F"up_blocks.{i}.resnets.{j}" UpperCAmelCase : List[str] = F"output_blocks.{current_layer}.0" UpperCAmelCase : int = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , has_skip=UpperCamelCase ) UpperCAmelCase : Union[str, Any] = F"up_blocks.{i}.attentions.{j}" UpperCAmelCase : int = F"output_blocks.{current_layer}.1" UpperCAmelCase : List[Any] = convert_attention( UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) current_layer += 1 if i != len(UpperCamelCase ) - 1: UpperCAmelCase : int = F"up_blocks.{i}.upsamplers.0" UpperCAmelCase : Dict = F"output_blocks.{current_layer-1}.2" UpperCAmelCase : Union[str, Any] = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) UpperCAmelCase : List[Any] = checkpoint["""out.0.weight"""] UpperCAmelCase : str = checkpoint["""out.0.bias"""] UpperCAmelCase : str = checkpoint["""out.2.weight"""] UpperCAmelCase : Tuple = checkpoint["""out.2.bias"""] return new_checkpoint if __name__ == "__main__": A: Tuple = 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.") A: str = parser.parse_args() A: List[Any] = strabool(args.class_cond) A: str = os.path.basename(args.unet_path) print(f"""Checkpoint: {ckpt_name}""") # Get U-Net config if "imagenet64" in ckpt_name: A: str = IMAGENET_64_UNET_CONFIG elif "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)): A: List[str] = LSUN_256_UNET_CONFIG elif "test" in ckpt_name: A: str = TEST_UNET_CONFIG else: raise ValueError(f"""Checkpoint type {ckpt_name} is not currently supported.""") if not args.class_cond: A: List[Any] = None A: Any = con_pt_to_diffuser(args.unet_path, unet_config) A: str = UNetaDModel(**unet_config) image_unet.load_state_dict(converted_unet_ckpt) # Get scheduler config if "cd" in ckpt_name or "test" in ckpt_name: A: Tuple = CD_SCHEDULER_CONFIG elif "ct" in ckpt_name and "imagenet64" in ckpt_name: A: Any = 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)): A: int = CT_LSUN_256_SCHEDULER_CONFIG else: raise ValueError(f"""Checkpoint type {ckpt_name} is not currently supported.""") A: Tuple = CMStochasticIterativeScheduler(**scheduler_config) A: Union[str, Any] = ConsistencyModelPipeline(unet=image_unet, scheduler=cm_scheduler) consistency_model.save_pretrained(args.dump_path)
109
import unittest import numpy as np from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __snake_case ( lowerCamelCase__ , unittest.TestCase ): # FIXME: add fast tests pass @nightly @require_onnxruntime @require_torch_gpu class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : List[Any] =ort.SessionOptions() UpperCAmelCase : Optional[int] =False return options def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : int =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Optional[Any] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Dict ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : Any =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=10 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : Optional[int] =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Tuple =np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Tuple =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , subfolder='''scheduler''' , revision='''onnx''' ) UpperCAmelCase : int =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , scheduler=snake_case__ , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Union[str, Any] ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : str =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=20 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : int =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Union[str, Any] =np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3
348
0
"""simple docstring""" from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class lowercase : _SCREAMING_SNAKE_CASE = BlenderbotConfig _SCREAMING_SNAKE_CASE = {} _SCREAMING_SNAKE_CASE = """gelu""" def __init__( self , lowercase , lowercase=13 , lowercase=7 , lowercase=True , lowercase=False , lowercase=99 , lowercase=32 , lowercase=2 , lowercase=4 , lowercase=37 , lowercase=0.1 , lowercase=0.1 , lowercase=20 , lowercase=2 , lowercase=1 , lowercase=0 , ) -> Optional[Any]: lowerCAmelCase = parent lowerCAmelCase = batch_size lowerCAmelCase = seq_length lowerCAmelCase = is_training lowerCAmelCase = use_labels lowerCAmelCase = vocab_size lowerCAmelCase = hidden_size lowerCAmelCase = num_hidden_layers lowerCAmelCase = num_attention_heads lowerCAmelCase = intermediate_size lowerCAmelCase = hidden_dropout_prob lowerCAmelCase = attention_probs_dropout_prob lowerCAmelCase = max_position_embeddings lowerCAmelCase = eos_token_id lowerCAmelCase = pad_token_id lowerCAmelCase = bos_token_id def _snake_case ( self ) -> Optional[Any]: lowerCAmelCase = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) lowerCAmelCase = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) lowerCAmelCase = tf.concat([input_ids, eos_tensor] , axis=1 ) lowerCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCAmelCase = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) lowerCAmelCase = prepare_blenderbot_inputs_dict(snake_case__ , snake_case__ , snake_case__ ) return config, inputs_dict def _snake_case ( self , lowercase , lowercase ) -> int: lowerCAmelCase = TFBlenderbotModel(config=snake_case__ ).get_decoder() lowerCAmelCase = inputs_dict['''input_ids'''] lowerCAmelCase = input_ids[:1, :] lowerCAmelCase = inputs_dict['''attention_mask'''][:1, :] lowerCAmelCase = inputs_dict['''head_mask'''] lowerCAmelCase = 1 # first forward pass lowerCAmelCase = model(snake_case__ , attention_mask=snake_case__ , head_mask=snake_case__ , use_cache=snake_case__ ) lowerCAmelCase = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids lowerCAmelCase = ids_tensor((self.batch_size, 3) , config.vocab_size ) lowerCAmelCase = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and lowerCAmelCase = tf.concat([input_ids, next_tokens] , axis=-1 ) lowerCAmelCase = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) lowerCAmelCase = model(snake_case__ , attention_mask=snake_case__ )[0] lowerCAmelCase = model(snake_case__ , attention_mask=snake_case__ , past_key_values=snake_case__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice lowerCAmelCase = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) lowerCAmelCase = output_from_no_past[:, -3:, random_slice_idx] lowerCAmelCase = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(snake_case__ , snake_case__ , rtol=1e-3 ) def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : Any=None , SCREAMING_SNAKE_CASE : Union[str, Any]=None , SCREAMING_SNAKE_CASE : Dict=None , SCREAMING_SNAKE_CASE : str=None , SCREAMING_SNAKE_CASE : List[str]=None , ): '''simple docstring''' if attention_mask is None: lowerCAmelCase = tf.cast(tf.math.not_equal(__lowerCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: lowerCAmelCase = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: lowerCAmelCase = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: lowerCAmelCase = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: lowerCAmelCase = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class lowercase ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): _SCREAMING_SNAKE_CASE = (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () _SCREAMING_SNAKE_CASE = (TFBlenderbotForConditionalGeneration,) if is_tf_available() else () _SCREAMING_SNAKE_CASE = ( { """conversational""": TFBlenderbotForConditionalGeneration, """feature-extraction""": TFBlenderbotModel, """summarization""": TFBlenderbotForConditionalGeneration, """text2text-generation""": TFBlenderbotForConditionalGeneration, """translation""": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = False def _snake_case ( self ) -> Dict: lowerCAmelCase = TFBlenderbotModelTester(self ) lowerCAmelCase = ConfigTester(self , config_class=snake_case__ ) def _snake_case ( self ) -> Optional[Any]: self.config_tester.run_common_tests() def _snake_case ( self ) -> List[Any]: lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*snake_case__ ) @require_tokenizers @require_tf class lowercase ( unittest.TestCase ): _SCREAMING_SNAKE_CASE = ["""My friends are cool but they eat too many carbs."""] _SCREAMING_SNAKE_CASE = """facebook/blenderbot-400M-distill""" @cached_property def _snake_case ( self ) -> int: return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def _snake_case ( self ) -> int: lowerCAmelCase = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def _snake_case ( self ) -> Any: lowerCAmelCase = self.tokenizer(self.src_text , return_tensors="""tf""" ) lowerCAmelCase = self.model.generate( model_inputs.input_ids , ) lowerCAmelCase = self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=snake_case__ )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
46
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def lowerCAmelCase_ ( )-> int: '''simple docstring''' UpperCAmelCase : str ={ '''repo_name''': ['''test_repo1''', '''test_repo2''', '''test_repo3'''], '''path''': ['''test_1.py''', '''test_2.py''', '''unit_test.py'''], '''content''': ['''a ''' * 20, '''a ''' * 30, '''b ''' * 7], } UpperCAmelCase : Union[str, Any] =Dataset.from_dict(__lowerCAmelCase ) return dataset class __snake_case ( lowerCamelCase__ ): def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[str] =get_dataset() UpperCAmelCase : Optional[int] =make_duplicate_clusters(snake_case__ , 0.85 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : str =get_dataset() UpperCAmelCase , UpperCAmelCase : Tuple =deduplicate_dataset(snake_case__ ) self.assertEqual(len(snake_case__ ) , 2 ) print(snake_case__ ) self.assertEqual(duplicate_clusters[0][0]['''copies'''] , 2 ) self.assertEqual(duplicate_clusters[0][0]['''is_extreme'''] , snake_case__ )
348
0
from typing import List, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) A : int = { '''huggingface/autoformer-tourism-monthly''': '''https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json''', } class A (lowerCamelCase__ ): '''simple docstring''' __lowerCamelCase : Dict = """autoformer""" __lowerCamelCase : List[Any] = { """hidden_size""": """d_model""", """num_attention_heads""": """encoder_attention_heads""", """num_hidden_layers""": """encoder_layers""", } def __init__( self : Optional[int] , __lowerCAmelCase : str = None , __lowerCAmelCase : List[Any] = None , __lowerCAmelCase : Union[str, Any] = "student_t" , __lowerCAmelCase : Optional[int] = "nll" , __lowerCAmelCase : Optional[int] = 1 , __lowerCAmelCase : List[Any] = [1, 2, 3, 4, 5, 6, 7] , __lowerCAmelCase : str = True , __lowerCAmelCase : List[str] = 0 , __lowerCAmelCase : List[Any] = 0 , __lowerCAmelCase : Any = 0 , __lowerCAmelCase : List[Any] = 0 , __lowerCAmelCase : Optional[Any] = None , __lowerCAmelCase : int = None , __lowerCAmelCase : str = 64 , __lowerCAmelCase : int = 2 , __lowerCAmelCase : int = 2 , __lowerCAmelCase : List[str] = 2 , __lowerCAmelCase : List[str] = 2 , __lowerCAmelCase : Optional[int] = 32 , __lowerCAmelCase : Dict = 32 , __lowerCAmelCase : Dict = "gelu" , __lowerCAmelCase : List[Any] = 0.1 , __lowerCAmelCase : Union[str, Any] = 0.1 , __lowerCAmelCase : int = 0.1 , __lowerCAmelCase : Any = 0.1 , __lowerCAmelCase : Optional[Any] = 0.1 , __lowerCAmelCase : Tuple = 1_00 , __lowerCAmelCase : Dict = 0.0_2 , __lowerCAmelCase : List[Any] = True , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : Union[str, Any] = 10 , __lowerCAmelCase : Union[str, Any] = 25 , __lowerCAmelCase : int = 3 , **__lowerCAmelCase : Tuple , ) -> Any: """simple docstring""" A__ = prediction_length A__ = context_length if context_length is not None else prediction_length A__ = distribution_output A__ = loss A__ = input_size A__ = num_time_features A__ = lags_sequence A__ = scaling A__ = num_dynamic_real_features A__ = num_static_real_features A__ = num_static_categorical_features if cardinality is not None and num_static_categorical_features > 0: if len(snake_case__ ) != num_static_categorical_features: raise ValueError( """The cardinality should be a list of the same length as `num_static_categorical_features`""" ) A__ = cardinality else: A__ = [0] if embedding_dimension is not None and num_static_categorical_features > 0: if len(snake_case__ ) != num_static_categorical_features: raise ValueError( """The embedding dimension should be a list of the same length as `num_static_categorical_features`""" ) A__ = embedding_dimension else: A__ = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] A__ = num_parallel_samples # Transformer architecture configuration A__ = input_size * len(self.lags_sequence ) + self._number_of_features A__ = d_model A__ = encoder_attention_heads A__ = decoder_attention_heads A__ = encoder_ffn_dim A__ = decoder_ffn_dim A__ = encoder_layers A__ = decoder_layers A__ = dropout A__ = attention_dropout A__ = activation_dropout A__ = encoder_layerdrop A__ = decoder_layerdrop A__ = activation_function A__ = init_std A__ = use_cache # Autoformer A__ = label_length A__ = moving_average A__ = autocorrelation_factor super().__init__(is_encoder_decoder=snake_case__ , **snake_case__ ) @property def a_ ( self : List[str] ) -> int: """simple docstring""" return ( sum(self.embedding_dimension ) + self.num_dynamic_real_features + self.num_time_features + self.num_static_real_features + self.input_size * 2 # the log1p(abs(loc)) and log(scale) features )
274
from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput __snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name class __snake_case ( lowerCamelCase__ , lowerCamelCase__ ): @register_to_config def __init__( self , snake_case__ , snake_case__ = None , snake_case__ = None ) -> str: '''simple docstring''' super().__init__() UpperCAmelCase : Optional[Any] =learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" UpperCAmelCase : Any =torch.zeros(snake_case__ , snake_case__ ) else: UpperCAmelCase : Union[str, Any] =None UpperCAmelCase : Optional[int] =torch.nn.Parameter(snake_case__ ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : VQModel __lowerCamelCase : CLIPTextModel __lowerCamelCase : CLIPTokenizer __lowerCamelCase : TransformeraDModel __lowerCamelCase : LearnedClassifierFreeSamplingEmbeddings __lowerCamelCase : VQDiffusionScheduler def __init__( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> int: '''simple docstring''' super().__init__() self.register_modules( vqvae=snake_case__ , transformer=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , scheduler=snake_case__ , learned_classifier_free_sampling_embeddings=snake_case__ , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : int =len(snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else 1 # get prompt text embeddings UpperCAmelCase : Optional[int] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) UpperCAmelCase : int =text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCAmelCase : List[str] =self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' f''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) UpperCAmelCase : Optional[Any] =text_input_ids[:, : self.tokenizer.model_max_length] UpperCAmelCase : List[Any] =self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 UpperCAmelCase : int =prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate text embeddings for each generation per prompt UpperCAmelCase : int =prompt_embeds.repeat_interleave(snake_case__ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: UpperCAmelCase : Optional[int] =self.learned_classifier_free_sampling_embeddings.embeddings UpperCAmelCase : str =negative_prompt_embeds.unsqueeze(0 ).repeat(snake_case__ , 1 , 1 ) else: UpperCAmelCase : str =[''''''] * batch_size UpperCAmelCase : Tuple =text_input_ids.shape[-1] UpperCAmelCase : Optional[Any] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=snake_case__ , truncation=snake_case__ , return_tensors='''pt''' , ) UpperCAmelCase : Optional[Any] =self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings UpperCAmelCase : Optional[int] =negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCAmelCase : Optional[Any] =negative_prompt_embeds.shape[1] UpperCAmelCase : Union[str, Any] =negative_prompt_embeds.repeat(1 , snake_case__ , 1 ) UpperCAmelCase : Optional[Any] =negative_prompt_embeds.view(batch_size * num_images_per_prompt , snake_case__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCAmelCase : int =torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , snake_case__ , snake_case__ = 100 , snake_case__ = 5.0 , snake_case__ = 1.0 , snake_case__ = 1 , snake_case__ = None , snake_case__ = None , snake_case__ = "pil" , snake_case__ = True , snake_case__ = None , snake_case__ = 1 , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =1 elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Tuple =len(snake_case__ ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(snake_case__ )}''' ) UpperCAmelCase : Tuple =batch_size * num_images_per_prompt UpperCAmelCase : List[str] =guidance_scale > 1.0 UpperCAmelCase : List[Any] =self._encode_prompt(snake_case__ , snake_case__ , snake_case__ ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case__ , snake_case__ ) or callback_steps <= 0) ): raise ValueError( f'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' f''' {type(snake_case__ )}.''' ) # get the initial completely masked latents unless the user supplied it UpperCAmelCase : int =(batch_size, self.transformer.num_latent_pixels) if latents is None: UpperCAmelCase : Union[str, Any] =self.transformer.num_vector_embeds - 1 UpperCAmelCase : str =torch.full(snake_case__ , snake_case__ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( '''Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,''' f''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) UpperCAmelCase : Any =latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(snake_case__ , device=self.device ) UpperCAmelCase : Any =self.scheduler.timesteps.to(self.device ) UpperCAmelCase : Optional[int] =latents for i, t in enumerate(self.progress_bar(snake_case__ ) ): # expand the sample if we are doing classifier free guidance UpperCAmelCase : Optional[Any] =torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` UpperCAmelCase : Optional[int] =self.transformer(snake_case__ , encoder_hidden_states=snake_case__ , timestep=snake_case__ ).sample if do_classifier_free_guidance: UpperCAmelCase , UpperCAmelCase : str =model_output.chunk(2 ) UpperCAmelCase : Optional[int] =model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(snake_case__ , dim=1 , keepdim=snake_case__ ) UpperCAmelCase : Tuple =self.truncate(snake_case__ , snake_case__ ) # remove `log(0)`'s (`-inf`s) UpperCAmelCase : Optional[Any] =model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 UpperCAmelCase : int =self.scheduler.step(snake_case__ , timestep=snake_case__ , sample=snake_case__ , generator=snake_case__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =self.vqvae.config.vq_embed_dim UpperCAmelCase : Optional[Any] =(batch_size, self.transformer.height, self.transformer.width, embedding_channels) UpperCAmelCase : Dict =self.vqvae.quantize.get_codebook_entry(snake_case__ , shape=snake_case__ ) UpperCAmelCase : Tuple =self.vqvae.decode(snake_case__ , force_not_quantize=snake_case__ ).sample UpperCAmelCase : Union[str, Any] =(image / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase : Any =image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase : List[str] =self.numpy_to_pil(snake_case__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> torch.FloatTensor: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : int =torch.sort(snake_case__ , 1 , descending=snake_case__ ) UpperCAmelCase : Union[str, Any] =torch.exp(snake_case__ ) UpperCAmelCase : Union[str, Any] =sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out UpperCAmelCase : Optional[Any] =torch.full_like(keep_mask[:, 0:1, :] , snake_case__ ) UpperCAmelCase : Tuple =torch.cat((all_true, keep_mask) , dim=1 ) UpperCAmelCase : int =keep_mask[:, :-1, :] UpperCAmelCase : int =keep_mask.gather(1 , indices.argsort(1 ) ) UpperCAmelCase : Dict =log_p_x_0.clone() UpperCAmelCase : List[Any] =-torch.inf # -inf = log(0) return rv
348
0
import argparse import os import torch from transformers.utils import WEIGHTS_NAME UpperCAmelCase = ['''small''', '''medium''', '''large'''] UpperCAmelCase = '''lm_head.decoder.weight''' UpperCAmelCase = '''lm_head.weight''' def UpperCAmelCase_ ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): lowercase = torch.load(__lowerCAmelCase ) lowercase = d.pop(__lowerCAmelCase ) os.makedirs(__lowerCAmelCase , exist_ok=__lowerCAmelCase ) torch.save(__lowerCAmelCase , os.path.join(__lowerCAmelCase , __lowerCAmelCase ) ) if __name__ == "__main__": UpperCAmelCase = argparse.ArgumentParser() parser.add_argument('''--dialogpt_path''', default='''.''', type=str) UpperCAmelCase = parser.parse_args() for MODEL in DIALOGPT_MODELS: UpperCAmelCase = os.path.join(args.dialogpt_path, F"""{MODEL}_ft.pkl""") UpperCAmelCase = F"""./DialoGPT-{MODEL}""" convert_dialogpt_checkpoint( checkpoint_path, pytorch_dump_folder_path, )
195
import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase : Any =UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =self.dummy_uncond_unet UpperCAmelCase : Optional[int] =KarrasVeScheduler() UpperCAmelCase : List[Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : List[str] =torch.manual_seed(0 ) UpperCAmelCase : List[str] =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : str =torch.manual_seed(0 ) UpperCAmelCase : str =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' , return_dict=snake_case__ )[0] UpperCAmelCase : Any =image[0, -3:, -3:, -1] UpperCAmelCase : List[str] =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCAmelCase : int =np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class __snake_case ( unittest.TestCase ): def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Tuple ='''google/ncsnpp-celebahq-256''' UpperCAmelCase : int =UNetaDModel.from_pretrained(snake_case__ ) UpperCAmelCase : Dict =KarrasVeScheduler() UpperCAmelCase : Union[str, Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Any =torch.manual_seed(0 ) UpperCAmelCase : Tuple =pipe(num_inference_steps=20 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : Optional[int] =image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) UpperCAmelCase : Tuple =np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
348
0
'''simple docstring''' from collections.abc import Sequence def _UpperCamelCase ( UpperCamelCase__ = None ): if nums is None or not nums: raise ValueError("""Input sequence should not be empty""" ) UpperCAmelCase__ : Dict = nums[0] for i in range(1 , len(__lowerCAmelCase ) ): UpperCAmelCase__ : Dict = nums[i] UpperCAmelCase__ : Any = max(__lowerCAmelCase , ans + num , __lowerCAmelCase ) return ans if __name__ == "__main__": import doctest doctest.testmod() # Try on a sample input from the user __A =int(input('Enter number of elements : ').strip()) __A =list(map(int, input('\nEnter the numbers : ').strip().split()))[:n] print(max_subsequence_sum(array))
163
import qiskit def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> qiskit.result.counts.Counts: '''simple docstring''' UpperCAmelCase : Union[str, Any] =qiskit.Aer.get_backend('''aer_simulator''' ) UpperCAmelCase : List[str] =qiskit.QuantumCircuit(4 , 2 ) # encode inputs in qubits 0 and 1 if bita == 1: qc_ha.x(0 ) if bita == 1: qc_ha.x(1 ) qc_ha.barrier() # use cnots to write XOR of the inputs on qubit2 qc_ha.cx(0 , 2 ) qc_ha.cx(1 , 2 ) # use ccx / toffoli gate to write AND of the inputs on qubit3 qc_ha.ccx(0 , 1 , 3 ) qc_ha.barrier() # extract outputs qc_ha.measure(2 , 0 ) # extract XOR value qc_ha.measure(3 , 1 ) # extract AND value # Execute the circuit on the qasm simulator UpperCAmelCase : Dict =qiskit.execute(__lowerCAmelCase , __lowerCAmelCase , shots=10_00 ) # Return the histogram data of the results of the experiment return job.result().get_counts(__lowerCAmelCase ) if __name__ == "__main__": __snake_case = half_adder(1, 1) print(f'Half Adder Output Qubit Counts: {counts}')
348
0
def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> int: """simple docstring""" global f # a global dp table for knapsack if f[i][j] < 0: if j < wt[i - 1]: __A = mf_knapsack(i - 1 , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) else: __A = max( mf_knapsack(i - 1 , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) , mf_knapsack(i - 1 , __lowerCAmelCase , __lowerCAmelCase , j - wt[i - 1] ) + val[i - 1] , ) __A = val return f[i][j] def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> Dict: """simple docstring""" __A = [[0] * (w + 1) for _ in range(n + 1 )] for i in range(1 , n + 1 ): for w_ in range(1 , w + 1 ): if wt[i - 1] <= w_: __A = max(val[i - 1] + dp[i - 1][w_ - wt[i - 1]] , dp[i - 1][w_] ) else: __A = dp[i - 1][w_] return dp[n][w_], dp def UpperCAmelCase ( a_ , a_ , a_ ) -> List[Any]: """simple docstring""" if not (isinstance(__lowerCAmelCase , (list, tuple) ) and isinstance(__lowerCAmelCase , (list, tuple) )): raise ValueError( "Both the weights and values vectors must be either lists or tuples" ) __A = len(__lowerCAmelCase ) if num_items != len(__lowerCAmelCase ): __A = ( '''The number of weights must be the same as the number of values.\n''' F'''But got {num_items} weights and {len(__lowerCAmelCase )} values''' ) raise ValueError(__lowerCAmelCase ) for i in range(__lowerCAmelCase ): if not isinstance(wt[i] , __lowerCAmelCase ): __A = ( '''All weights must be integers but got weight of ''' F'''type {type(wt[i] )} at index {i}''' ) raise TypeError(__lowerCAmelCase ) __A = knapsack(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) __A = set() _construct_solution(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) return optimal_val, example_optional_set def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ ) -> Any: """simple docstring""" if i > 0 and j > 0: if dp[i - 1][j] == dp[i][j]: _construct_solution(__lowerCAmelCase , __lowerCAmelCase , i - 1 , __lowerCAmelCase , __lowerCAmelCase ) else: optimal_set.add(__lowerCAmelCase ) _construct_solution(__lowerCAmelCase , __lowerCAmelCase , i - 1 , j - wt[i - 1] , __lowerCAmelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :Tuple = [3, 2, 4, 4] SCREAMING_SNAKE_CASE :Optional[int] = [4, 3, 2, 3] SCREAMING_SNAKE_CASE :Union[str, Any] = 4 SCREAMING_SNAKE_CASE :str = 6 SCREAMING_SNAKE_CASE :Optional[Any] = [[0] * (w + 1)] + [[0] + [-1] * (w + 1) for _ in range(n + 1)] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE :List[Any] = knapsack(w, wt, val, n) print(optimal_solution) print(mf_knapsack(n, wt, val, w)) # switched the n and w # testing the dynamic programming problem with example # the optimal subset for the above example are items 3 and 4 SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE :int = knapsack_with_example_solution(w, wt, val) assert optimal_solution == 8 assert optimal_subset == {3, 4} print('optimal_value = ', optimal_solution) print('An optimal subset corresponding to the optimal value', optimal_subset)
15
from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class __snake_case : __lowerCamelCase : str = BlenderbotConfig __lowerCamelCase : Optional[Any] = {} __lowerCamelCase : Optional[int] = """gelu""" def __init__( self , snake_case__ , snake_case__=13 , snake_case__=7 , snake_case__=True , snake_case__=False , snake_case__=99 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=20 , snake_case__=2 , snake_case__=1 , snake_case__=0 , ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : Dict =seq_length UpperCAmelCase : Optional[Any] =is_training UpperCAmelCase : List[str] =use_labels UpperCAmelCase : List[Any] =vocab_size UpperCAmelCase : Optional[int] =hidden_size UpperCAmelCase : Tuple =num_hidden_layers UpperCAmelCase : Any =num_attention_heads UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : str =hidden_dropout_prob UpperCAmelCase : Optional[int] =attention_probs_dropout_prob UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : List[Any] =eos_token_id UpperCAmelCase : Optional[int] =pad_token_id UpperCAmelCase : Tuple =bos_token_id def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[Any] =ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) UpperCAmelCase : List[Any] =tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) UpperCAmelCase : Tuple =tf.concat([input_ids, eos_tensor] , axis=1 ) UpperCAmelCase : str =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : Optional[Any] =self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) UpperCAmelCase : List[str] =prepare_blenderbot_inputs_dict(snake_case__ , snake_case__ , snake_case__ ) return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =TFBlenderbotModel(config=snake_case__ ).get_decoder() UpperCAmelCase : Any =inputs_dict['''input_ids'''] UpperCAmelCase : str =input_ids[:1, :] UpperCAmelCase : Tuple =inputs_dict['''attention_mask'''][:1, :] UpperCAmelCase : Tuple =inputs_dict['''head_mask'''] UpperCAmelCase : List[Any] =1 # first forward pass UpperCAmelCase : List[str] =model(snake_case__ , attention_mask=snake_case__ , head_mask=snake_case__ , use_cache=snake_case__ ) UpperCAmelCase , UpperCAmelCase : str =outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase : Union[str, Any] =ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCAmelCase : List[Any] =tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and UpperCAmelCase : Tuple =tf.concat([input_ids, next_tokens] , axis=-1 ) UpperCAmelCase : int =tf.concat([attention_mask, next_attn_mask] , axis=-1 ) UpperCAmelCase : Optional[int] =model(snake_case__ , attention_mask=snake_case__ )[0] UpperCAmelCase : str =model(snake_case__ , attention_mask=snake_case__ , past_key_values=snake_case__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice UpperCAmelCase : List[Any] =int(ids_tensor((1,) , output_from_past.shape[-1] ) ) UpperCAmelCase : List[Any] =output_from_no_past[:, -3:, random_slice_idx] UpperCAmelCase : Dict =output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(snake_case__ , snake_case__ , rtol=1e-3 ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , )-> str: '''simple docstring''' if attention_mask is None: UpperCAmelCase : int =tf.cast(tf.math.not_equal(__lowerCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase : Tuple =tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase : str =tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase : Union[str, Any] =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase : int =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : List[str] = (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __lowerCamelCase : Dict = (TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __lowerCamelCase : Dict = ( { """conversational""": TFBlenderbotForConditionalGeneration, """feature-extraction""": TFBlenderbotModel, """summarization""": TFBlenderbotForConditionalGeneration, """text2text-generation""": TFBlenderbotForConditionalGeneration, """translation""": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __lowerCamelCase : Union[str, Any] = True __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Union[str, Any] = False def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : List[str] =TFBlenderbotModelTester(self ) UpperCAmelCase : List[Any] =ConfigTester(self , config_class=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*snake_case__ ) @require_tokenizers @require_tf class __snake_case ( unittest.TestCase ): __lowerCamelCase : List[str] = ["""My friends are cool but they eat too many carbs."""] __lowerCamelCase : Dict = """facebook/blenderbot-400M-distill""" @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[int] =self.tokenizer(self.src_text , return_tensors='''tf''' ) UpperCAmelCase : Optional[int] =self.model.generate( model_inputs.input_ids , ) UpperCAmelCase : str =self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=snake_case__ )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
348
0
import pandas as pd from matplotlib import pyplot as plt from sklearn.linear_model import LinearRegression # Splitting the dataset into the Training set and Test set from sklearn.model_selection import train_test_split # Fitting Polynomial Regression to the dataset from sklearn.preprocessing import PolynomialFeatures # Importing the dataset _lowercase: Tuple = pd.read_csv( "https://s3.us-west-2.amazonaws.com/public.gamelab.fun/dataset/" "position_salaries.csv" ) _lowercase: Dict = dataset.iloc[:, 1:2].values _lowercase: str = dataset.iloc[:, 2].values _lowercase , _lowercase , _lowercase , _lowercase: Dict = train_test_split(X, y, test_size=0.2, random_state=0) _lowercase: List[str] = PolynomialFeatures(degree=4) _lowercase: Tuple = poly_reg.fit_transform(X) _lowercase: Union[str, Any] = LinearRegression() pol_reg.fit(X_poly, y) def a( ) -> str: """simple docstring""" plt.scatter(__lowerCAmelCase , __lowerCAmelCase , color="red" ) plt.plot(__lowerCAmelCase , pol_reg.predict(poly_reg.fit_transform(__lowerCAmelCase ) ) , color="blue" ) plt.title("Truth or Bluff (Linear Regression)" ) plt.xlabel("Position level" ) plt.ylabel("Salary" ) plt.show() if __name__ == "__main__": viz_polymonial() # Predicting a new result with Polymonial Regression pol_reg.predict(poly_reg.fit_transform([[5.5]])) # output should be 132148.43750003
227
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { '''asapp/sew-d-tiny-100k''': '''https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json''', # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[Any] = """sew-d""" def __init__( self , snake_case__=32 , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=2 , snake_case__=512 , snake_case__=256 , snake_case__=True , snake_case__=True , snake_case__=("p2c", "c2p") , snake_case__="layer_norm" , snake_case__="gelu_python" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.0 , snake_case__=0.1 , snake_case__=0.02 , snake_case__=1e-7 , snake_case__=1e-5 , snake_case__="group" , snake_case__="gelu" , snake_case__=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , snake_case__=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , snake_case__=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , snake_case__=False , snake_case__=128 , snake_case__=16 , snake_case__=True , snake_case__=0.05 , snake_case__=10 , snake_case__=2 , snake_case__=0.0 , snake_case__=10 , snake_case__=0 , snake_case__="mean" , snake_case__=False , snake_case__=False , snake_case__=256 , snake_case__=0 , snake_case__=1 , snake_case__=2 , **snake_case__ , ) -> int: '''simple docstring''' super().__init__(**snake_case__ , pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ ) UpperCAmelCase : Union[str, Any] =hidden_size UpperCAmelCase : Union[str, Any] =feat_extract_norm UpperCAmelCase : Optional[Any] =feat_extract_activation UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : int =list(snake_case__ ) UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : str =conv_bias UpperCAmelCase : Tuple =num_conv_pos_embeddings UpperCAmelCase : Dict =num_conv_pos_embedding_groups UpperCAmelCase : str =len(self.conv_dim ) UpperCAmelCase : Dict =num_hidden_layers UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : List[Any] =squeeze_factor UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : int =position_buckets UpperCAmelCase : Optional[int] =share_att_key UpperCAmelCase : Optional[int] =relative_attention UpperCAmelCase : Tuple =norm_rel_ebd UpperCAmelCase : List[Any] =list(snake_case__ ) UpperCAmelCase : Dict =hidden_act UpperCAmelCase : Optional[int] =num_attention_heads UpperCAmelCase : Any =hidden_dropout UpperCAmelCase : str =attention_dropout UpperCAmelCase : Union[str, Any] =activation_dropout UpperCAmelCase : str =feat_proj_dropout UpperCAmelCase : Union[str, Any] =final_dropout UpperCAmelCase : Optional[int] =layer_norm_eps UpperCAmelCase : str =feature_layer_norm_eps UpperCAmelCase : str =initializer_range UpperCAmelCase : Any =vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect.''' '''It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,''' f'''but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)''' f'''= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCAmelCase : Union[str, Any] =apply_spec_augment UpperCAmelCase : Optional[Any] =mask_time_prob UpperCAmelCase : Tuple =mask_time_length UpperCAmelCase : str =mask_time_min_masks UpperCAmelCase : Optional[int] =mask_feature_prob UpperCAmelCase : Optional[Any] =mask_feature_length UpperCAmelCase : List[Any] =mask_feature_min_masks # ctc loss UpperCAmelCase : str =ctc_loss_reduction UpperCAmelCase : Optional[int] =ctc_zero_infinity # sequence classification UpperCAmelCase : Union[str, Any] =use_weighted_layer_sum UpperCAmelCase : int =classifier_proj_size @property def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
348
0
import unittest from accelerate import debug_launcher from accelerate.test_utils import require_cpu, test_ops, test_script @require_cpu class __lowerCAmelCase ( unittest.TestCase ): def A__ ( self ) -> Optional[Any]: '''simple docstring''' debug_launcher(test_script.main ) def A__ ( self ) -> Tuple: '''simple docstring''' debug_launcher(test_ops.main )
205
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node __snake_case = 4 __snake_case = 3 class __snake_case ( lowerCamelCase__ ): pass def lowerCAmelCase_ ( __lowerCAmelCase )-> List[str]: '''simple docstring''' for shard in shards: for i in range(__lowerCAmelCase ): yield {"i": i, "shard": shard} def lowerCAmelCase_ ( )-> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =int(os.environ['''RANK'''] ) UpperCAmelCase : Optional[Any] =int(os.environ['''WORLD_SIZE'''] ) UpperCAmelCase : List[Any] =ArgumentParser() parser.add_argument('''--streaming''' , type=__lowerCAmelCase ) parser.add_argument('''--local_rank''' , type=__lowerCAmelCase ) parser.add_argument('''--num_workers''' , type=__lowerCAmelCase , default=0 ) UpperCAmelCase : Any =parser.parse_args() UpperCAmelCase : List[str] =args.streaming UpperCAmelCase : Tuple =args.num_workers UpperCAmelCase : int ={'''shards''': [f'''shard_{shard_idx}''' for shard_idx in range(__lowerCAmelCase )]} UpperCAmelCase : Optional[int] =IterableDataset.from_generator(__lowerCAmelCase , gen_kwargs=__lowerCAmelCase ) if not streaming: UpperCAmelCase : List[Any] =Dataset.from_list(list(__lowerCAmelCase ) ) UpperCAmelCase : Dict =split_dataset_by_node(__lowerCAmelCase , rank=__lowerCAmelCase , world_size=__lowerCAmelCase ) UpperCAmelCase : List[Any] =torch.utils.data.DataLoader(__lowerCAmelCase , num_workers=__lowerCAmelCase ) UpperCAmelCase : Dict =NUM_SHARDS * NUM_ITEMS_PER_SHARD UpperCAmelCase : str =full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) UpperCAmelCase : List[Any] =sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f'''local_size {local_size} != expected_local_size {expected_local_size}''' ) if __name__ == "__main__": main()
348
0
"""simple docstring""" from __future__ import annotations import copy import inspect import json import math import os import tempfile import unittest from importlib import import_module import numpy as np from transformers import ViTMAEConfig from transformers.file_utils import cached_property, is_tf_available, is_vision_available from transformers.testing_utils import require_tf, require_vision, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFViTMAEForPreTraining, TFViTMAEModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __a : '''simple docstring''' def __init__( self , _a , _a=13 , _a=30 , _a=2 , _a=3 , _a=True , _a=True , _a=32 , _a=2 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=10 , _a=0.02 , _a=3 , _a=0.6 , _a=None , ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Union[str, Any] = parent SCREAMING_SNAKE_CASE__ : Optional[Any] = batch_size SCREAMING_SNAKE_CASE__ : Union[str, Any] = image_size SCREAMING_SNAKE_CASE__ : Tuple = patch_size SCREAMING_SNAKE_CASE__ : int = num_channels SCREAMING_SNAKE_CASE__ : Dict = is_training SCREAMING_SNAKE_CASE__ : Optional[int] = use_labels SCREAMING_SNAKE_CASE__ : int = hidden_size SCREAMING_SNAKE_CASE__ : List[Any] = num_hidden_layers SCREAMING_SNAKE_CASE__ : Tuple = num_attention_heads SCREAMING_SNAKE_CASE__ : Optional[Any] = intermediate_size SCREAMING_SNAKE_CASE__ : Any = hidden_act SCREAMING_SNAKE_CASE__ : Optional[int] = hidden_dropout_prob SCREAMING_SNAKE_CASE__ : List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE__ : List[str] = type_sequence_label_size SCREAMING_SNAKE_CASE__ : int = initializer_range SCREAMING_SNAKE_CASE__ : Optional[Any] = mask_ratio SCREAMING_SNAKE_CASE__ : str = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) SCREAMING_SNAKE_CASE__ : Tuple = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE__ : Dict = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def _a ( self ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[str] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE__ : Optional[int] = None if self.use_labels: SCREAMING_SNAKE_CASE__ : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE__ : Tuple = self.get_config() return config, pixel_values, labels def _a ( self ) -> Dict: """simple docstring""" return ViTMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , decoder_hidden_size=self.hidden_size , decoder_num_hidden_layers=self.num_hidden_layers , decoder_num_attention_heads=self.num_attention_heads , decoder_intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=snake_case__ , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def _a ( self , _a , _a , _a ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ : Any = TFViTMAEModel(config=snake_case__ ) SCREAMING_SNAKE_CASE__ : Any = model(snake_case__ , training=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _a ( self , _a , _a , _a ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE__ : Union[str, Any] = TFViTMAEForPreTraining(snake_case__ ) SCREAMING_SNAKE_CASE__ : Optional[Any] = model(snake_case__ , training=snake_case__ ) # expected sequence length = num_patches SCREAMING_SNAKE_CASE__ : List[str] = (self.image_size // self.patch_size) ** 2 SCREAMING_SNAKE_CASE__ : str = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images SCREAMING_SNAKE_CASE__ : Dict = 1 SCREAMING_SNAKE_CASE__ : int = TFViTMAEForPreTraining(snake_case__ ) SCREAMING_SNAKE_CASE__ : List[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE__ : List[Any] = model(snake_case__ , training=snake_case__ ) SCREAMING_SNAKE_CASE__ : List[str] = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def _a ( self ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE__ : Tuple = self.prepare_config_and_inputs() (SCREAMING_SNAKE_CASE__) : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE__ : Dict = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class __a (lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase): '''simple docstring''' _SCREAMING_SNAKE_CASE :Union[str, Any] = (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else () _SCREAMING_SNAKE_CASE :Dict = {"""feature-extraction""": TFViTMAEModel} if is_tf_available() else {} _SCREAMING_SNAKE_CASE :Any = False _SCREAMING_SNAKE_CASE :Dict = False _SCREAMING_SNAKE_CASE :Optional[int] = False _SCREAMING_SNAKE_CASE :str = False def _a ( self ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE__ : Any = TFViTMAEModelTester(self ) SCREAMING_SNAKE_CASE__ : List[str] = ConfigTester(self , config_class=snake_case__ , has_text_modality=snake_case__ , hidden_size=37 ) def _a ( self ) -> str: """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason="""ViTMAE does not use inputs_embeds""" ) def _a ( self ) -> Union[str, Any]: """simple docstring""" pass def _a ( self ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE__ : List[Any] = model_class(snake_case__ ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) SCREAMING_SNAKE_CASE__ : List[str] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case__ , tf.keras.layers.Layer ) ) def _a ( self ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE__ : Optional[Any] = model_class(snake_case__ ) SCREAMING_SNAKE_CASE__ : int = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE__ : Optional[int] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE__ : str = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , snake_case__ ) def _a ( self ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def _a ( self ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*snake_case__ ) def _a ( self ) -> Union[str, Any]: """simple docstring""" np.random.seed(2 ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE__ : Tuple = int((config.image_size // config.patch_size) ** 2 ) SCREAMING_SNAKE_CASE__ : Dict = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE__ : List[str] = model_class(snake_case__ ) SCREAMING_SNAKE_CASE__ : List[str] = self._prepare_for_class(snake_case__ , snake_case__ ) SCREAMING_SNAKE_CASE__ : Optional[int] = model(snake_case__ , noise=snake_case__ ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = copy.deepcopy(self._prepare_for_class(snake_case__ , snake_case__ ) ) SCREAMING_SNAKE_CASE__ : Tuple = model(**snake_case__ , noise=snake_case__ ) SCREAMING_SNAKE_CASE__ : Optional[int] = outputs_dict[0].numpy() SCREAMING_SNAKE_CASE__ : List[Any] = outputs_keywords[0].numpy() self.assertLess(np.sum(np.abs(output_dict - output_keywords ) ) , 1E-6 ) def _a ( self ) -> List[str]: """simple docstring""" np.random.seed(2 ) SCREAMING_SNAKE_CASE__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE__ : Tuple = int((config.image_size // config.patch_size) ** 2 ) SCREAMING_SNAKE_CASE__ : Optional[int] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) def prepare_numpy_arrays(_a ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = {} for k, v in inputs_dict.items(): if tf.is_tensor(snake_case__ ): SCREAMING_SNAKE_CASE__ : Tuple = v.numpy() else: SCREAMING_SNAKE_CASE__ : int = np.array(snake_case__ ) return inputs_np_dict for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE__ : Optional[int] = model_class(snake_case__ ) SCREAMING_SNAKE_CASE__ : str = self._prepare_for_class(snake_case__ , snake_case__ ) SCREAMING_SNAKE_CASE__ : Dict = prepare_numpy_arrays(snake_case__ ) SCREAMING_SNAKE_CASE__ : Optional[int] = model(snake_case__ , noise=snake_case__ ) SCREAMING_SNAKE_CASE__ : Optional[Any] = model(**snake_case__ , noise=snake_case__ ) self.assert_outputs_same(snake_case__ , snake_case__ ) def _a ( self , _a , _a , _a ) -> List[str]: """simple docstring""" np.random.seed(2 ) SCREAMING_SNAKE_CASE__ : Any = int((tf_model.config.image_size // tf_model.config.patch_size) ** 2 ) SCREAMING_SNAKE_CASE__ : Optional[Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) SCREAMING_SNAKE_CASE__ : Tuple = tf.constant(snake_case__ ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument SCREAMING_SNAKE_CASE__ : Optional[Any] = tf_noise super().check_pt_tf_models(snake_case__ , snake_case__ , snake_case__ ) def _a ( self ) -> List[Any]: """simple docstring""" np.random.seed(2 ) SCREAMING_SNAKE_CASE__ : Any = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE__ : List[str] = { module_member for model_class in self.all_model_classes for module in (import_module(model_class.__module__ ),) for module_member_name in dir(snake_case__ ) if module_member_name.endswith("""MainLayer""" ) # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`. and module_member_name[: -len("""MainLayer""" )] == model_class.__name__[: -len("""Model""" )] for module_member in (getattr(snake_case__ , snake_case__ ),) if isinstance(snake_case__ , snake_case__ ) and tf.keras.layers.Layer in module_member.__bases__ and getattr(snake_case__ , """_keras_serializable""" , snake_case__ ) } SCREAMING_SNAKE_CASE__ : str = int((config.image_size // config.patch_size) ** 2 ) SCREAMING_SNAKE_CASE__ : str = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) SCREAMING_SNAKE_CASE__ : str = tf.convert_to_tensor(snake_case__ ) inputs_dict.update({"""noise""": noise} ) for main_layer_class in tf_main_layer_classes: SCREAMING_SNAKE_CASE__ : int = main_layer_class(snake_case__ ) SCREAMING_SNAKE_CASE__ : List[str] = { name: tf.keras.Input(tensor.shape[1:] , dtype=tensor.dtype ) for name, tensor in inputs_dict.items() } SCREAMING_SNAKE_CASE__ : int = tf.keras.Model(snake_case__ , outputs=main_layer(snake_case__ ) ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = model(snake_case__ ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE__ : List[Any] = os.path.join(snake_case__ , """keras_model.h5""" ) model.save(snake_case__ ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = tf.keras.models.load_model( snake_case__ , custom_objects={main_layer_class.__name__: main_layer_class} ) assert isinstance(snake_case__ , tf.keras.Model ) SCREAMING_SNAKE_CASE__ : Optional[int] = model(snake_case__ ) self.assert_outputs_same(snake_case__ , snake_case__ ) @slow def _a ( self ) -> Union[str, Any]: """simple docstring""" np.random.seed(2 ) SCREAMING_SNAKE_CASE__ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE__ : Tuple = int((config.image_size // config.patch_size) ** 2 ) SCREAMING_SNAKE_CASE__ : Optional[int] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE__ : int = model_class(snake_case__ ) SCREAMING_SNAKE_CASE__ : List[Any] = self._prepare_for_class(snake_case__ , snake_case__ ) SCREAMING_SNAKE_CASE__ : Optional[int] = model(snake_case__ , noise=snake_case__ ) if model_class.__name__ == "TFViTMAEModel": SCREAMING_SNAKE_CASE__ : Union[str, Any] = outputs.last_hidden_state.numpy() SCREAMING_SNAKE_CASE__ : str = 0 else: SCREAMING_SNAKE_CASE__ : str = outputs.logits.numpy() SCREAMING_SNAKE_CASE__ : List[str] = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(snake_case__ , saved_model=snake_case__ ) SCREAMING_SNAKE_CASE__ : int = model_class.from_pretrained(snake_case__ ) SCREAMING_SNAKE_CASE__ : Tuple = model(snake_case__ , noise=snake_case__ ) if model_class.__name__ == "TFViTMAEModel": SCREAMING_SNAKE_CASE__ : List[str] = after_outputs['''last_hidden_state'''].numpy() SCREAMING_SNAKE_CASE__ : Optional[int] = 0 else: SCREAMING_SNAKE_CASE__ : Optional[Any] = after_outputs['''logits'''].numpy() SCREAMING_SNAKE_CASE__ : List[str] = 0 SCREAMING_SNAKE_CASE__ : Union[str, Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(snake_case__ , 1E-5 ) def _a ( self ) -> str: """simple docstring""" np.random.seed(2 ) SCREAMING_SNAKE_CASE__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE__ : Optional[Any] = int((config.image_size // config.patch_size) ** 2 ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE__ : Union[str, Any] = model_class(snake_case__ ) SCREAMING_SNAKE_CASE__ : Tuple = self._prepare_for_class(snake_case__ , snake_case__ ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = model(snake_case__ , noise=snake_case__ ) SCREAMING_SNAKE_CASE__ : Dict = model.get_config() # make sure that returned config is jsonifiable, which is required by keras json.dumps(snake_case__ ) SCREAMING_SNAKE_CASE__ : Optional[Any] = model_class.from_config(model.get_config() ) # make sure it also accepts a normal config SCREAMING_SNAKE_CASE__ : Union[str, Any] = model_class.from_config(model.config ) SCREAMING_SNAKE_CASE__ : Optional[int] = new_model(snake_case__ ) # Build model new_model.set_weights(model.get_weights() ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = new_model(snake_case__ , noise=snake_case__ ) self.assert_outputs_same(snake_case__ , snake_case__ ) @unittest.skip( reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.""" ) def _a ( self ) -> Dict: """simple docstring""" pass @unittest.skip(reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load""" ) def _a ( self ) -> str: """simple docstring""" pass @slow def _a ( self ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Dict = TFViTMAEModel.from_pretrained("""google/vit-base-patch16-224""" ) self.assertIsNotNone(snake_case__ ) def _lowercase ( ) -> Any: SCREAMING_SNAKE_CASE__ : List[Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf @require_vision class __a (unittest.TestCase): '''simple docstring''' @cached_property def _a ( self ) -> int: """simple docstring""" return ViTImageProcessor.from_pretrained("""facebook/vit-mae-base""" ) if is_vision_available() else None @slow def _a ( self ) -> List[str]: """simple docstring""" np.random.seed(2 ) SCREAMING_SNAKE_CASE__ : int = TFViTMAEForPreTraining.from_pretrained("""facebook/vit-mae-base""" ) SCREAMING_SNAKE_CASE__ : List[str] = self.default_image_processor SCREAMING_SNAKE_CASE__ : List[str] = prepare_img() SCREAMING_SNAKE_CASE__ : str = image_processor(images=snake_case__ , return_tensors="""tf""" ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) SCREAMING_SNAKE_CASE__ : Optional[Any] = ViTMAEConfig() SCREAMING_SNAKE_CASE__ : Tuple = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) SCREAMING_SNAKE_CASE__ : Tuple = np.random.uniform(size=(1, num_patches) ) # forward pass SCREAMING_SNAKE_CASE__ : Union[str, Any] = model(**snake_case__ , noise=snake_case__ ) # verify the logits SCREAMING_SNAKE_CASE__ : str = tf.convert_to_tensor([1, 196, 768] ) self.assertEqual(outputs.logits.shape , snake_case__ ) SCREAMING_SNAKE_CASE__ : Optional[Any] = tf.convert_to_tensor( [[-0.0_548, -1.7_023, -0.9_325], [0.3_721, -0.5_670, -0.2_233], [0.8_235, -1.3_878, -0.3_524]] ) tf.debugging.assert_near(outputs.logits[0, :3, :3] , snake_case__ , atol=1E-4 )
132
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __snake_case = {'''configuration_opt''': ['''OPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OPTConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''OPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OPTForCausalLM''', '''OPTModel''', '''OPTPreTrainedModel''', '''OPTForSequenceClassification''', '''OPTForQuestionAnswering''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''TFOPTForCausalLM''', '''TFOPTModel''', '''TFOPTPreTrainedModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''FlaxOPTForCausalLM''', '''FlaxOPTModel''', '''FlaxOPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
0
'''simple docstring''' import argparse import os import pickle import sys import torch from transformers import TransfoXLConfig, TransfoXLLMHeadModel, load_tf_weights_in_transfo_xl from transformers.models.transfo_xl import tokenization_transfo_xl as data_utils from transformers.models.transfo_xl.tokenization_transfo_xl import CORPUS_NAME, VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() # We do this to be able to load python 2 datasets pickles # See e.g. https://stackoverflow.com/questions/2121874/python-pickling-after-changing-a-modules-directory/2121918#2121918 _UpperCamelCase = data_utils.TransfoXLTokenizer _UpperCamelCase = data_utils.TransfoXLCorpus _UpperCamelCase = data_utils _UpperCamelCase = data_utils def lowercase_ ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : int , lowerCAmelCase__ : List[str] ): """simple docstring""" if transfo_xl_dataset_file: # Convert a pre-processed corpus (see original TensorFlow repo) with open(__lowerCAmelCase , """rb""" ) as fp: __UpperCAmelCase : Optional[int] = pickle.load(__lowerCAmelCase , encoding="""latin1""" ) # Save vocabulary and dataset cache as Dictionaries (should be better than pickles for the long-term) __UpperCAmelCase : str = pytorch_dump_folder_path + '''/''' + VOCAB_FILES_NAMES['''pretrained_vocab_file'''] print(f'Save vocabulary to {pytorch_vocab_dump_path}' ) __UpperCAmelCase : int = corpus.vocab.__dict__ torch.save(__lowerCAmelCase , __lowerCAmelCase ) __UpperCAmelCase : Any = corpus.__dict__ corpus_dict_no_vocab.pop("""vocab""" , __lowerCAmelCase ) __UpperCAmelCase : int = pytorch_dump_folder_path + '''/''' + CORPUS_NAME print(f'Save dataset to {pytorch_dataset_dump_path}' ) torch.save(__lowerCAmelCase , __lowerCAmelCase ) if tf_checkpoint_path: # Convert a pre-trained TensorFlow model __UpperCAmelCase : List[Any] = os.path.abspath(__lowerCAmelCase ) __UpperCAmelCase : int = os.path.abspath(__lowerCAmelCase ) print(f'Converting Transformer XL checkpoint from {tf_path} with config at {config_path}.' ) # Initialise PyTorch model if transfo_xl_config_file == "": __UpperCAmelCase : int = TransfoXLConfig() else: __UpperCAmelCase : Dict = TransfoXLConfig.from_json_file(__lowerCAmelCase ) print(f'Building PyTorch model from configuration: {config}' ) __UpperCAmelCase : Dict = TransfoXLLMHeadModel(__lowerCAmelCase ) __UpperCAmelCase : Union[str, Any] = load_tf_weights_in_transfo_xl(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) # Save pytorch-model __UpperCAmelCase : Dict = os.path.join(__lowerCAmelCase , __lowerCAmelCase ) __UpperCAmelCase : Tuple = os.path.join(__lowerCAmelCase , __lowerCAmelCase ) print(f'Save PyTorch model to {os.path.abspath(__lowerCAmelCase )}' ) torch.save(model.state_dict() , __lowerCAmelCase ) print(f'Save configuration file to {os.path.abspath(__lowerCAmelCase )}' ) with open(__lowerCAmelCase , """w""" , encoding="""utf-8""" ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": _UpperCamelCase = argparse.ArgumentParser() parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the folder to store the PyTorch model or dataset/vocab.''', ) parser.add_argument( '''--tf_checkpoint_path''', default='''''', type=str, help='''An optional path to a TensorFlow checkpoint path to be converted.''', ) parser.add_argument( '''--transfo_xl_config_file''', default='''''', type=str, help=( '''An optional config json file corresponding to the pre-trained BERT model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--transfo_xl_dataset_file''', default='''''', type=str, help='''An optional dataset file to be converted in a vocabulary.''', ) _UpperCamelCase = parser.parse_args() convert_transfo_xl_checkpoint_to_pytorch( args.tf_checkpoint_path, args.transfo_xl_config_file, args.pytorch_dump_folder_path, args.transfo_xl_dataset_file, )
254
import tempfile import unittest import numpy as np import transformers from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax import jax.numpy as jnp from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel if is_torch_available(): import torch class __snake_case : def __init__( self , snake_case__ , snake_case__=14 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=False , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=4 , snake_case__=4 , snake_case__=4 , snake_case__=37 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , ) -> str: '''simple docstring''' UpperCAmelCase : str =parent UpperCAmelCase : Tuple =batch_size UpperCAmelCase : Optional[int] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Tuple =use_input_mask UpperCAmelCase : List[Any] =use_token_type_ids UpperCAmelCase : Optional[Any] =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : List[Any] =hidden_size UpperCAmelCase : Optional[int] =rotary_dim UpperCAmelCase : Union[str, Any] =num_hidden_layers UpperCAmelCase : List[Any] =num_attention_heads UpperCAmelCase : Dict =intermediate_size UpperCAmelCase : Union[str, Any] =hidden_act UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : Dict =attention_probs_dropout_prob UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : str =initializer_range UpperCAmelCase : Optional[int] =None UpperCAmelCase : List[Any] =vocab_size - 1 UpperCAmelCase : Optional[Any] =vocab_size - 1 UpperCAmelCase : List[Any] =vocab_size - 1 def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : List[Any] =None if self.use_input_mask: UpperCAmelCase : Optional[Any] =random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : Dict =GPTJConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , use_cache=snake_case__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , rotary_dim=self.rotary_dim , ) return (config, input_ids, input_mask) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Union[str, Any] =config_and_inputs UpperCAmelCase : Tuple ={'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =20 UpperCAmelCase : Any =model_class_name(snake_case__ ) UpperCAmelCase : str =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : Any =jnp.ones((input_ids.shape[0], max_decoder_length) , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : List[str] =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =model( input_ids[:, -1:] , attention_mask=snake_case__ , past_key_values=outputs_cache.past_key_values , position_ids=snake_case__ , ) UpperCAmelCase : List[Any] =model(snake_case__ ) UpperCAmelCase : Any =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Dict =20 UpperCAmelCase : Dict =model_class_name(snake_case__ ) UpperCAmelCase : Tuple =jnp.concatenate( [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]) )] , axis=-1 , ) UpperCAmelCase : Dict =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : int =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : str =model( input_ids[:, -1:] , past_key_values=outputs_cache.past_key_values , attention_mask=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =model(snake_case__ , attention_mask=snake_case__ ) UpperCAmelCase : Dict =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) @require_flax class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Tuple = (FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else () __lowerCamelCase : Optional[Any] = (FlaxGPTJForCausalLM,) if is_flax_available() else () def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =FlaxGPTJModelTester(self ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Dict =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward_with_attn_mask( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) @tooslow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =GPTaTokenizer.from_pretrained('''gpt2''' , pad_token='''<|endoftext|>''' , padding_side='''left''' ) UpperCAmelCase : Optional[Any] =tokenizer(['''Hello this is a long string''', '''Hey'''] , return_tensors='''np''' , padding=snake_case__ , truncation=snake_case__ ) UpperCAmelCase : Optional[int] =FlaxGPTJForCausalLM.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : str =False UpperCAmelCase : Union[str, Any] =model.config.eos_token_id UpperCAmelCase : List[Any] =jax.jit(model.generate ) UpperCAmelCase : Dict =jit_generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , pad_token_id=tokenizer.pad_token_id ).sequences UpperCAmelCase : Any =tokenizer.batch_decode(snake_case__ , skip_special_tokens=snake_case__ ) UpperCAmelCase : Tuple =[ '''Hello this is a long string of text.\n\nI\'m trying to get the text of the''', '''Hey, I\'m a little late to the party. I\'m going to''', ] self.assertListEqual(snake_case__ , snake_case__ ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : List[str] =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : Any =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : Any =getattr(snake_case__ , snake_case__ ) UpperCAmelCase , UpperCAmelCase : Union[str, Any] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Tuple =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : int =0 UpperCAmelCase : Optional[int] =1 UpperCAmelCase : Optional[int] =0 UpperCAmelCase : Union[str, Any] =1 UpperCAmelCase : List[str] =pt_model_class(snake_case__ ).eval() UpperCAmelCase : Optional[int] =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Any =convert_pytorch_state_dict_to_flax(pt_model.state_dict() , snake_case__ ) UpperCAmelCase : Union[str, Any] =fx_state with torch.no_grad(): UpperCAmelCase : Any =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : Dict =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(snake_case__ ) UpperCAmelCase : str =model_class.from_pretrained(snake_case__ , from_pt=snake_case__ ) UpperCAmelCase : int =fx_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output_loaded, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output_loaded[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : int =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : int =getattr(snake_case__ , snake_case__ ) UpperCAmelCase : Dict =pt_model_class(snake_case__ ).eval() UpperCAmelCase : str =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Optional[Any] =load_flax_weights_in_pytorch_model(snake_case__ , fx_model.params ) UpperCAmelCase , UpperCAmelCase : Optional[int] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Optional[int] =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : str =0 UpperCAmelCase : Any =1 UpperCAmelCase : List[Any] =0 UpperCAmelCase : Tuple =1 # make sure weights are tied in PyTorch pt_model.tie_weights() with torch.no_grad(): UpperCAmelCase : Optional[Any] =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : List[Any] =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(snake_case__ ) UpperCAmelCase : Tuple =pt_model_class.from_pretrained(snake_case__ , from_flax=snake_case__ ) with torch.no_grad(): UpperCAmelCase : Any =pt_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @tooslow def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase : str =model_class_name.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : Tuple =model(np.ones((1, 1) ) ) self.assertIsNotNone(snake_case__ )
348
0
'''simple docstring''' import unittest from transformers import TrOCRConfig from transformers.testing_utils import is_torch_available, require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM @require_torch class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Optional[Any] , _UpperCAmelCase : str , _UpperCAmelCase : Tuple=99 , _UpperCAmelCase : List[str]=13 , _UpperCAmelCase : str=16 , _UpperCAmelCase : Any=7 , _UpperCAmelCase : int=True , _UpperCAmelCase : Optional[int]=True , _UpperCAmelCase : str=True , _UpperCAmelCase : Optional[Any]=False , _UpperCAmelCase : List[str]=True , _UpperCAmelCase : Optional[Any]=2 , _UpperCAmelCase : Union[str, Any]=32 , _UpperCAmelCase : str=4 , _UpperCAmelCase : Dict=4 , _UpperCAmelCase : List[str]=30 , _UpperCAmelCase : List[Any]=0 , _UpperCAmelCase : int=1 , _UpperCAmelCase : Any=2 , _UpperCAmelCase : Tuple=None , ): """simple docstring""" UpperCAmelCase__ = parent UpperCAmelCase__ = batch_size UpperCAmelCase__ = decoder_seq_length # For common tests UpperCAmelCase__ = self.decoder_seq_length UpperCAmelCase__ = is_training UpperCAmelCase__ = use_attention_mask UpperCAmelCase__ = use_labels UpperCAmelCase__ = vocab_size UpperCAmelCase__ = d_model UpperCAmelCase__ = d_model UpperCAmelCase__ = decoder_layers UpperCAmelCase__ = decoder_layers UpperCAmelCase__ = decoder_ffn_dim UpperCAmelCase__ = decoder_attention_heads UpperCAmelCase__ = decoder_attention_heads UpperCAmelCase__ = eos_token_id UpperCAmelCase__ = bos_token_id UpperCAmelCase__ = pad_token_id UpperCAmelCase__ = decoder_start_token_id UpperCAmelCase__ = use_cache UpperCAmelCase__ = max_position_embeddings UpperCAmelCase__ = None UpperCAmelCase__ = decoder_seq_length UpperCAmelCase__ = 2 UpperCAmelCase__ = 1 def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" UpperCAmelCase__ = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) UpperCAmelCase__ = None if self.use_attention_mask: UpperCAmelCase__ = ids_tensor([self.batch_size, self.decoder_seq_length] , vocab_size=2 ) UpperCAmelCase__ = None if self.use_labels: UpperCAmelCase__ = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) UpperCAmelCase__ = TrOCRConfig( vocab_size=self.vocab_size , d_model=self.d_model , decoder_layers=self.decoder_layers , decoder_ffn_dim=self.decoder_ffn_dim , decoder_attention_heads=self.decoder_attention_heads , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , use_cache=self.use_cache , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , max_position_embeddings=self.max_position_embeddings , ) return (config, input_ids, attention_mask, lm_labels) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , _UpperCAmelCase : Tuple , _UpperCAmelCase : Dict , _UpperCAmelCase : Optional[Any] , _UpperCAmelCase : Any , ): """simple docstring""" UpperCAmelCase__ = True UpperCAmelCase__ = TrOCRDecoder(config=snake_case__ ).to(snake_case__ ).eval() UpperCAmelCase__ = input_ids[:2] input_ids[input_ids == 0] += 1 # first forward pass UpperCAmelCase__ = model(snake_case__ , use_cache=snake_case__ ) UpperCAmelCase__ = model(snake_case__ ) UpperCAmelCase__ = model(snake_case__ , use_cache=snake_case__ ) self.parent.assertTrue(len(snake_case__ ) == len(snake_case__ ) ) self.parent.assertTrue(len(snake_case__ ) == len(snake_case__ ) + 1 ) UpperCAmelCase__ = outputs['''past_key_values'''] # create hypothetical next token and extent to next_input_ids UpperCAmelCase__ = ids_tensor((2, 1) , config.vocab_size - 1 ) + 1 # append to next input_ids and UpperCAmelCase__ = torch.cat([input_ids, next_tokens] , dim=-1 ) UpperCAmelCase__ = model(snake_case__ )['''last_hidden_state'''] UpperCAmelCase__ = model(snake_case__ , past_key_values=snake_case__ )['''last_hidden_state'''] # select random slice UpperCAmelCase__ = ids_tensor((1,) , output_from_past.shape[-1] ).item() UpperCAmelCase__ = output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach() UpperCAmelCase__ = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice assert torch.allclose(snake_case__ , snake_case__ , atol=1E-3 ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" UpperCAmelCase__ = self.prepare_config_and_inputs() UpperCAmelCase__ = config_and_inputs UpperCAmelCase__ = {'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_torch class lowerCAmelCase_ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): '''simple docstring''' lowerCAmelCase_ : List[str] = (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else () lowerCAmelCase_ : int = (TrOCRForCausalLM,) if is_torch_available() else () lowerCAmelCase_ : Tuple = {"""text-generation""": TrOCRForCausalLM} if is_torch_available() else {} lowerCAmelCase_ : Dict = True lowerCAmelCase_ : Optional[int] = False def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" UpperCAmelCase__ = TrOCRStandaloneDecoderModelTester(self , is_training=snake_case__ ) UpperCAmelCase__ = ConfigTester(self , config_class=snake_case__ ) def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" pass def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" pass def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" pass def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past(*snake_case__ ) def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" return @unittest.skip("""The model doesn\'t support left padding""" ) # and it's not used enough to be worth fixing :) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" pass
346
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __snake_case = { '''configuration_bloom''': ['''BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BloomConfig''', '''BloomOnnxConfig'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''BloomTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BloomForCausalLM''', '''BloomModel''', '''BloomPreTrainedModel''', '''BloomForSequenceClassification''', '''BloomForTokenClassification''', '''BloomForQuestionAnswering''', ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
0
from __future__ import annotations def UpperCamelCase ( _A, _A, _A, _A ): """simple docstring""" __magic_name__ : List[str] = [] __magic_name__ : Optional[Any] = input_list[low:mid], input_list[mid : high + 1] while left and right: result.append((left if left[0] <= right[0] else right).pop(0 ) ) __magic_name__ : str = result + left + right return input_list def UpperCamelCase ( _A ): """simple docstring""" if len(__lowerCAmelCase ) <= 1: return input_list __magic_name__ : List[str] = list(__lowerCAmelCase ) # iteration for two-way merging __magic_name__ : Tuple = 2 while p <= len(__lowerCAmelCase ): # getting low, high and middle value for merge-sort of single list for i in range(0, len(__lowerCAmelCase ), __lowerCAmelCase ): __magic_name__ : Tuple = i __magic_name__ : Any = i + p - 1 __magic_name__ : List[str] = (low + high + 1) // 2 __magic_name__ : Union[str, Any] = merge(__lowerCAmelCase, __lowerCAmelCase, __lowerCAmelCase, __lowerCAmelCase ) # final merge of last two parts if p * 2 >= len(__lowerCAmelCase ): __magic_name__ : List[str] = i __magic_name__ : List[Any] = merge(__lowerCAmelCase, 0, __lowerCAmelCase, len(__lowerCAmelCase ) - 1 ) break p *= 2 return input_list if __name__ == "__main__": __magic_name__: Dict = input("Enter numbers separated by a comma:\n").strip() if user_input == "": __magic_name__: str = [] else: __magic_name__: int = [int(item.strip()) for item in user_input.split(",")] print(iter_merge_sort(unsorted))
342
import os from typing import Dict, List, Tuple, TypeVar, Union __snake_case = TypeVar('''T''') __snake_case = Union[List[T], Tuple[T, ...]] __snake_case = Union[T, List[T], Dict[str, T]] __snake_case = Union[str, bytes, os.PathLike]
348
0
"""simple docstring""" import math from typing import Optional import numpy as np from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { "facebook/encodec_24khz": "https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json", "facebook/encodec_48khz": "https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json", } class lowercase ( lowerCamelCase__ ): _SCREAMING_SNAKE_CASE = """encodec""" def __init__( self , lowercase=[1.5, 3.0, 6.0, 12.0, 24.0] , lowercase=24_000 , lowercase=1 , lowercase=False , lowercase=None , lowercase=None , lowercase=128 , lowercase=32 , lowercase=1 , lowercase=[8, 5, 4, 2] , lowercase="weight_norm" , lowercase=7 , lowercase=7 , lowercase=3 , lowercase=2 , lowercase=True , lowercase="reflect" , lowercase=2 , lowercase=2 , lowercase=1.0 , lowercase=1_024 , lowercase=None , lowercase=True , **lowercase , ) -> List[str]: lowerCAmelCase = target_bandwidths lowerCAmelCase = sampling_rate lowerCAmelCase = audio_channels lowerCAmelCase = normalize lowerCAmelCase = chunk_length_s lowerCAmelCase = overlap lowerCAmelCase = hidden_size lowerCAmelCase = num_filters lowerCAmelCase = num_residual_layers lowerCAmelCase = upsampling_ratios lowerCAmelCase = norm_type lowerCAmelCase = kernel_size lowerCAmelCase = last_kernel_size lowerCAmelCase = residual_kernel_size lowerCAmelCase = dilation_growth_rate lowerCAmelCase = use_causal_conv lowerCAmelCase = pad_mode lowerCAmelCase = compress lowerCAmelCase = num_lstm_layers lowerCAmelCase = trim_right_ratio lowerCAmelCase = codebook_size lowerCAmelCase = codebook_dim if codebook_dim is not None else hidden_size lowerCAmelCase = use_conv_shortcut if self.norm_type not in ["weight_norm", "time_group_norm"]: raise ValueError( f'self.norm_type must be one of `"weight_norm"`, `"time_group_norm"`), got {self.norm_type}' ) super().__init__(**snake_case__ ) @property def _snake_case ( self ) -> Optional[int]: if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def _snake_case ( self ) -> Optional[int]: if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length ) ) @property def _snake_case ( self ) -> int: lowerCAmelCase = np.prod(self.upsampling_ratios ) return math.ceil(self.sampling_rate / hop_length ) @property def _snake_case ( self ) -> int: return int(1_000 * self.target_bandwidths[-1] // (self.frame_rate * 10) )
46
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_big_bird import BigBirdTokenizer else: __snake_case = None __snake_case = logging.get_logger(__name__) __snake_case = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} __snake_case = { '''vocab_file''': { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model''', '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model''' ), }, '''tokenizer_file''': { '''google/bigbird-roberta-base''': ( '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/tokenizer.json''' ), '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/tokenizer.json''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/tokenizer.json''' ), }, } __snake_case = { '''google/bigbird-roberta-base''': 40_96, '''google/bigbird-roberta-large''': 40_96, '''google/bigbird-base-trivia-itc''': 40_96, } __snake_case = '''▁''' class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Dict = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = BigBirdTokenizer __lowerCamelCase : Any = ["""input_ids""", """attention_mask"""] __lowerCamelCase : List[int] = [] def __init__( self , snake_case__=None , snake_case__=None , snake_case__="<unk>" , snake_case__="<s>" , snake_case__="</s>" , snake_case__="<pad>" , snake_case__="[SEP]" , snake_case__="[MASK]" , snake_case__="[CLS]" , **snake_case__ , ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else bos_token UpperCAmelCase : Optional[int] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else eos_token UpperCAmelCase : List[str] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else unk_token UpperCAmelCase : Union[str, Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else pad_token UpperCAmelCase : int =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else cls_token UpperCAmelCase : str =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else sep_token # Mask token behave like a normal word, i.e. include the space before it UpperCAmelCase : List[Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else mask_token super().__init__( snake_case__ , tokenizer_file=snake_case__ , bos_token=snake_case__ , eos_token=snake_case__ , unk_token=snake_case__ , sep_token=snake_case__ , pad_token=snake_case__ , cls_token=snake_case__ , mask_token=snake_case__ , **snake_case__ , ) UpperCAmelCase : Tuple =vocab_file UpperCAmelCase : Optional[int] =False if not self.vocab_file else True def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : int =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None , snake_case__ = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is None: return [1] + ([0] * len(snake_case__ )) + [1] return [1] + ([0] * len(snake_case__ )) + [1] + ([0] * len(snake_case__ )) + [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : Optional[Any] =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> Tuple[str]: '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(snake_case__ ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCAmelCase : Optional[int] =os.path.join( snake_case__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case__ ): copyfile(self.vocab_file , snake_case__ ) return (out_vocab_file,)
348
0
from collections import OrderedDict from typing import List, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : Any = logging.get_logger(__name__) A : List[str] = { '''google/efficientnet-b7''': '''https://huggingface.co/google/efficientnet-b7/resolve/main/config.json''', } class A (lowerCamelCase__ ): '''simple docstring''' __lowerCamelCase : int = """efficientnet""" def __init__( self : Dict , __lowerCAmelCase : Optional[Any] = 3 , __lowerCAmelCase : Optional[int] = 6_00 , __lowerCAmelCase : Tuple = 2.0 , __lowerCAmelCase : List[Any] = 3.1 , __lowerCAmelCase : Dict = 8 , __lowerCAmelCase : List[Any] = [3, 3, 5, 3, 5, 5, 3] , __lowerCAmelCase : List[Any] = [32, 16, 24, 40, 80, 1_12, 1_92] , __lowerCAmelCase : Tuple = [16, 24, 40, 80, 1_12, 1_92, 3_20] , __lowerCAmelCase : Optional[Any] = [] , __lowerCAmelCase : Any = [1, 2, 2, 2, 1, 2, 1] , __lowerCAmelCase : Tuple = [1, 2, 2, 3, 3, 4, 1] , __lowerCAmelCase : Optional[Any] = [1, 6, 6, 6, 6, 6, 6] , __lowerCAmelCase : Union[str, Any] = 0.2_5 , __lowerCAmelCase : Optional[Any] = "swish" , __lowerCAmelCase : Tuple = 25_60 , __lowerCAmelCase : List[str] = "mean" , __lowerCAmelCase : List[Any] = 0.0_2 , __lowerCAmelCase : Optional[Any] = 0.0_0_1 , __lowerCAmelCase : str = 0.9_9 , __lowerCAmelCase : Dict = 0.5 , __lowerCAmelCase : List[Any] = 0.2 , **__lowerCAmelCase : List[str] , ) -> List[Any]: """simple docstring""" super().__init__(**snake_case__ ) A__ = num_channels A__ = image_size A__ = width_coefficient A__ = depth_coefficient A__ = depth_divisor A__ = kernel_sizes A__ = in_channels A__ = out_channels A__ = depthwise_padding A__ = strides A__ = num_block_repeats A__ = expand_ratios A__ = squeeze_expansion_ratio A__ = hidden_act A__ = hidden_dim A__ = pooling_type A__ = initializer_range A__ = batch_norm_eps A__ = batch_norm_momentum A__ = dropout_rate A__ = drop_connect_rate A__ = sum(snake_case__ ) * 4 class A (lowerCamelCase__ ): '''simple docstring''' __lowerCamelCase : Optional[int] = version.parse('''1.11''' ) @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def a_ ( self : Optional[Any] ) -> float: """simple docstring""" return 1e-5
274
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def lowerCAmelCase_ ( __lowerCAmelCase )-> Optional[Any]: '''simple docstring''' def is_in_circle(__lowerCAmelCase , __lowerCAmelCase ) -> bool: UpperCAmelCase : List[Any] =sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle UpperCAmelCase : List[Any] =mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(__lowerCAmelCase ) ) # The ratio of the area for circle to square is pi/4. UpperCAmelCase : Dict =proportion * 4 print(f'''The estimated value of pi is {pi_estimate}''' ) print(f'''The numpy value of pi is {pi}''' ) print(f'''The total error is {abs(pi - pi_estimate )}''' ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 , )-> float: '''simple docstring''' return mean( function_to_integrate(uniform(__lowerCAmelCase , __lowerCAmelCase ) ) for _ in range(__lowerCAmelCase ) ) * (max_value - min_value) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 )-> None: '''simple docstring''' def identity_function(__lowerCAmelCase ) -> float: return x UpperCAmelCase : List[Any] =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase : Dict =(max_value * max_value - min_value * min_value) / 2 print('''******************''' ) print(f'''Estimating area under y=x where x varies from {min_value} to {max_value}''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {expected_value}''' ) print(f'''Total error is {abs(estimated_value - expected_value )}''' ) print('''******************''' ) def lowerCAmelCase_ ( __lowerCAmelCase )-> None: '''simple docstring''' def function_to_integrate(__lowerCAmelCase ) -> float: return sqrt(4.0 - x * x ) UpperCAmelCase : Dict =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , 0.0 , 2.0 ) print('''******************''' ) print('''Estimating pi using area_under_curve_estimator''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {pi}''' ) print(f'''Total error is {abs(estimated_value - pi )}''' ) print('''******************''' ) if __name__ == "__main__": import doctest doctest.testmod()
348
0
from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Sequence, Value from .base import TaskTemplate @dataclass(frozen=lowerCamelCase__ ) class A_ ( lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : str = field(default="""question-answering-extractive""" , metadata={"""include_in_asdict_even_if_is_default""": True} ) _UpperCamelCase : ClassVar[Features] = Features({"""question""": Value("""string""" ), """context""": Value("""string""" )} ) _UpperCamelCase : ClassVar[Features] = Features( { """answers""": Sequence( { """text""": Value("""string""" ), """answer_start""": Value("""int32""" ), } ) } ) _UpperCamelCase : str = "question" _UpperCamelCase : str = "context" _UpperCamelCase : str = "answers" @property def SCREAMING_SNAKE_CASE__ ( self ): return {self.question_column: "question", self.context_column: "context", self.answers_column: "answers"}
195
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __snake_case : def __init__( self , snake_case__ , snake_case__=12 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , snake_case__=0 , snake_case__=None , ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : List[Any] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Union[str, Any] =use_input_mask UpperCAmelCase : Tuple =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : Tuple =hidden_size UpperCAmelCase : Dict =projection_dim UpperCAmelCase : Optional[int] =num_hidden_layers UpperCAmelCase : Dict =num_attention_heads UpperCAmelCase : int =intermediate_size UpperCAmelCase : Any =dropout UpperCAmelCase : Union[str, Any] =attention_dropout UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : str =scope UpperCAmelCase : str =bos_token_id def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : int =None if self.use_input_mask: UpperCAmelCase : Union[str, Any] =random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: UpperCAmelCase : Optional[int] =input_mask.numpy() UpperCAmelCase , UpperCAmelCase : List[Any] =input_mask.shape UpperCAmelCase : Optional[Any] =np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : List[Any] =1 UpperCAmelCase : Tuple =0 UpperCAmelCase : List[Any] =self.get_config() return config, input_ids, tf.convert_to_tensor(snake_case__ ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =TFBlipTextModel(config=snake_case__ ) UpperCAmelCase : List[Any] =model(snake_case__ , attention_mask=snake_case__ , training=snake_case__ ) UpperCAmelCase : str =model(snake_case__ , training=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[Any] =config_and_inputs UpperCAmelCase : Optional[int] ={'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __snake_case ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Optional[int] = (TFBlipTextModel,) if is_tf_available() else () __lowerCamelCase : Dict = False __lowerCamelCase : Optional[Any] = False __lowerCamelCase : Dict = False def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : str =BlipTextModelTester(self ) UpperCAmelCase : Optional[int] =ConfigTester(self , config_class=snake_case__ , hidden_size=37 ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip(reason='''Blip does not use inputs_embeds''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @slow def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase : Optional[Any] =TFBlipTextModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def UpperCAmelCase__ ( self , snake_case__=True ) -> Any: '''simple docstring''' super().test_pt_tf_model_equivalence(allow_missing_keys=snake_case__ )
348
0
'''simple docstring''' def _UpperCamelCase ( UpperCamelCase__ ): if not numbers: return 0 if not isinstance(__lowerCAmelCase , (list, tuple) ) or not all( isinstance(__lowerCAmelCase , __lowerCAmelCase ) for number in numbers ): raise ValueError("""numbers must be an iterable of integers""" ) UpperCAmelCase__ : Tuple = numbers[0] for i in range(1 , len(__lowerCAmelCase ) ): # update the maximum and minimum subarray products UpperCAmelCase__ : Union[str, Any] = numbers[i] if number < 0: UpperCAmelCase__ : Optional[int] = min_till_now, max_till_now UpperCAmelCase__ : Tuple = max(__lowerCAmelCase , max_till_now * number ) UpperCAmelCase__ : Union[str, Any] = min(__lowerCAmelCase , min_till_now * number ) # update the maximum product found till now UpperCAmelCase__ : Optional[int] = max(__lowerCAmelCase , __lowerCAmelCase ) return max_prod
163
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging __snake_case = logging.get_logger(__name__) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' UpperCAmelCase : Dict =nn.functional.normalize(__lowerCAmelCase ) UpperCAmelCase : Tuple =nn.functional.normalize(__lowerCAmelCase ) return torch.mm(__lowerCAmelCase , normalized_text_embeds.t() ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : List[str] = CLIPConfig __lowerCamelCase : List[Any] = ["""CLIPEncoderLayer"""] def __init__( self , snake_case__ ) -> Dict: '''simple docstring''' super().__init__(snake_case__ ) UpperCAmelCase : Dict =CLIPVisionModel(config.vision_config ) UpperCAmelCase : Optional[Any] =nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=snake_case__ ) UpperCAmelCase : int =nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : List[str] =nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : str =nn.Parameter(torch.ones(17 ) , requires_grad=snake_case__ ) UpperCAmelCase : Optional[int] =nn.Parameter(torch.ones(3 ) , requires_grad=snake_case__ ) @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Union[str, Any] =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : Optional[Any] =self.visual_projection(snake_case__ ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 UpperCAmelCase : List[str] =cosine_distance(snake_case__ , self.special_care_embeds ).cpu().float().numpy() UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ).cpu().float().numpy() UpperCAmelCase : Tuple =[] UpperCAmelCase : Dict =image_embeds.shape[0] for i in range(snake_case__ ): UpperCAmelCase : str ={'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : str =0.0 for concept_idx in range(len(special_cos_dist[0] ) ): UpperCAmelCase : Optional[Any] =special_cos_dist[i][concept_idx] UpperCAmelCase : Union[str, Any] =self.special_care_embeds_weights[concept_idx].item() UpperCAmelCase : str =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} ) UpperCAmelCase : int =0.01 for concept_idx in range(len(cos_dist[0] ) ): UpperCAmelCase : Any =cos_dist[i][concept_idx] UpperCAmelCase : Optional[int] =self.concept_embeds_weights[concept_idx].item() UpperCAmelCase : int =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(snake_case__ ) result.append(snake_case__ ) UpperCAmelCase : Optional[int] =[len(res['''bad_concepts'''] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Any =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : List[str] =self.visual_projection(snake_case__ ) UpperCAmelCase : Any =cosine_distance(snake_case__ , self.special_care_embeds ) UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : Optional[Any] =0.0 UpperCAmelCase : Any =special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) UpperCAmelCase : str =torch.any(special_scores > 0 , dim=1 ) UpperCAmelCase : List[Any] =special_care * 0.01 UpperCAmelCase : Union[str, Any] =special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) UpperCAmelCase : List[Any] =(cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) UpperCAmelCase : str =torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
348
0
import json import os import shutil import warnings from argparse import ArgumentParser, Namespace from pathlib import Path from typing import List from ..utils import logging from . import BaseTransformersCLICommand try: from cookiecutter.main import cookiecutter SCREAMING_SNAKE_CASE :Dict = True except ImportError: SCREAMING_SNAKE_CASE :Any = False SCREAMING_SNAKE_CASE :Tuple = logging.get_logger(__name__) # pylint: disable=invalid-name def UpperCAmelCase ( a_ ) -> Optional[Any]: """simple docstring""" return AddNewModelCommand(args.testing , args.testing_file , path=args.path ) class UpperCAmelCase ( lowerCamelCase__ ): '''simple docstring''' @staticmethod def UpperCamelCase_ ( A : Optional[int] ): __A = parser.add_parser("add-new-model" ) add_new_model_parser.add_argument("--testing" ,action="store_true" ,help="If in testing mode." ) add_new_model_parser.add_argument("--testing_file" ,type=snake_case__ ,help="Configuration file on which to run." ) add_new_model_parser.add_argument( "--path" ,type=snake_case__ ,help="Path to cookiecutter. Should only be used for testing purposes." ) add_new_model_parser.set_defaults(func=snake_case__ ) def __init__( self : List[str] ,A : Union[str, Any] ,A : Tuple ,A : Tuple=None ,*A : Union[str, Any] ): __A = testing __A = testing_file __A = path def UpperCamelCase_ ( self : List[str] ): warnings.warn( "The command `transformers-cli add-new-model` is deprecated and will be removed in v5 of Transformers. " "It is not actively maintained anymore, so might give a result that won\'t pass all tests and quality " "checks, you should use `transformers-cli add-new-model-like` instead." ) if not _has_cookiecutter: raise ImportError( "Model creation dependencies are required to use the `add_new_model` command. Install them by running " "the following at the root of your `transformers` clone:\n\n\t$ pip install -e .[modelcreation]\n" ) # Ensure that there is no other `cookiecutter-template-xxx` directory in the current working directory __A = [directory for directory in os.listdir() if '''cookiecutter-template-''' == directory[:22]] if len(snake_case__ ) > 0: raise ValueError( "Several directories starting with `cookiecutter-template-` in current working directory. " "Please clean your directory by removing all folders starting with `cookiecutter-template-` or " "change your working directory." ) __A = ( Path(snake_case__ ).parent.parent.parent.parent if self._path is None else Path(self._path ).parent.parent ) __A = path_to_transformer_root / '''templates''' / '''adding_a_new_model''' # Execute cookiecutter if not self._testing: cookiecutter(str(snake_case__ ) ) else: with open(self._testing_file ,"r" ) as configuration_file: __A = json.load(snake_case__ ) cookiecutter( str(path_to_cookiecutter if self._path is None else self._path ) ,no_input=snake_case__ ,extra_context=snake_case__ ,) __A = [directory for directory in os.listdir() if '''cookiecutter-template-''' in directory[:22]][0] # Retrieve configuration with open(directory + "/configuration.json" ,"r" ) as configuration_file: __A = json.load(snake_case__ ) __A = configuration['''lowercase_modelname'''] __A = configuration['''generate_tensorflow_pytorch_and_flax'''] os.remove(f'''{directory}/configuration.json''' ) __A = '''PyTorch''' in generate_tensorflow_pytorch_and_flax __A = '''TensorFlow''' in generate_tensorflow_pytorch_and_flax __A = '''Flax''' in generate_tensorflow_pytorch_and_flax __A = f'''{path_to_transformer_root}/src/transformers/models/{lowercase_model_name}''' os.makedirs(snake_case__ ,exist_ok=snake_case__ ) os.makedirs(f'''{path_to_transformer_root}/tests/models/{lowercase_model_name}''' ,exist_ok=snake_case__ ) # Tests require submodules as they have parent imports with open(f'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/__init__.py''' ,"w" ): pass shutil.move( f'''{directory}/__init__.py''' ,f'''{model_dir}/__init__.py''' ,) shutil.move( f'''{directory}/configuration_{lowercase_model_name}.py''' ,f'''{model_dir}/configuration_{lowercase_model_name}.py''' ,) def remove_copy_lines(A : str ): with open(snake_case__ ,"r" ) as f: __A = f.readlines() with open(snake_case__ ,"w" ) as f: for line in lines: if "# Copied from transformers." not in line: f.write(snake_case__ ) if output_pytorch: if not self._testing: remove_copy_lines(f'''{directory}/modeling_{lowercase_model_name}.py''' ) shutil.move( f'''{directory}/modeling_{lowercase_model_name}.py''' ,f'''{model_dir}/modeling_{lowercase_model_name}.py''' ,) shutil.move( f'''{directory}/test_modeling_{lowercase_model_name}.py''' ,f'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_{lowercase_model_name}.py''' ,) else: os.remove(f'''{directory}/modeling_{lowercase_model_name}.py''' ) os.remove(f'''{directory}/test_modeling_{lowercase_model_name}.py''' ) if output_tensorflow: if not self._testing: remove_copy_lines(f'''{directory}/modeling_tf_{lowercase_model_name}.py''' ) shutil.move( f'''{directory}/modeling_tf_{lowercase_model_name}.py''' ,f'''{model_dir}/modeling_tf_{lowercase_model_name}.py''' ,) shutil.move( f'''{directory}/test_modeling_tf_{lowercase_model_name}.py''' ,f'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_tf_{lowercase_model_name}.py''' ,) else: os.remove(f'''{directory}/modeling_tf_{lowercase_model_name}.py''' ) os.remove(f'''{directory}/test_modeling_tf_{lowercase_model_name}.py''' ) if output_flax: if not self._testing: remove_copy_lines(f'''{directory}/modeling_flax_{lowercase_model_name}.py''' ) shutil.move( f'''{directory}/modeling_flax_{lowercase_model_name}.py''' ,f'''{model_dir}/modeling_flax_{lowercase_model_name}.py''' ,) shutil.move( f'''{directory}/test_modeling_flax_{lowercase_model_name}.py''' ,f'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_flax_{lowercase_model_name}.py''' ,) else: os.remove(f'''{directory}/modeling_flax_{lowercase_model_name}.py''' ) os.remove(f'''{directory}/test_modeling_flax_{lowercase_model_name}.py''' ) shutil.move( f'''{directory}/{lowercase_model_name}.md''' ,f'''{path_to_transformer_root}/docs/source/en/model_doc/{lowercase_model_name}.md''' ,) shutil.move( f'''{directory}/tokenization_{lowercase_model_name}.py''' ,f'''{model_dir}/tokenization_{lowercase_model_name}.py''' ,) shutil.move( f'''{directory}/tokenization_fast_{lowercase_model_name}.py''' ,f'''{model_dir}/tokenization_{lowercase_model_name}_fast.py''' ,) from os import fdopen, remove from shutil import copymode, move from tempfile import mkstemp def replace(A : Optional[Any] ,A : Union[str, Any] ,A : int ): # Create temp file __A = mkstemp() __A = False with fdopen(snake_case__ ,"w" ) as new_file: with open(snake_case__ ) as old_file: for line in old_file: new_file.write(snake_case__ ) if line_to_copy_below in line: __A = True for line_to_copy in lines_to_copy: new_file.write(snake_case__ ) if not line_found: raise ValueError(f'''Line {line_to_copy_below} was not found in file.''' ) # Copy the file permissions from the old file to the new file copymode(snake_case__ ,snake_case__ ) # Remove original file remove(snake_case__ ) # Move new file move(snake_case__ ,snake_case__ ) def skip_units(A : Union[str, Any] ): return ( ("generating PyTorch" in line and not output_pytorch) or ("generating TensorFlow" in line and not output_tensorflow) or ("generating Flax" in line and not output_flax) ) def replace_in_files(A : List[str] ): with open(snake_case__ ) as datafile: __A = [] __A = False __A = False for line in datafile: if "# To replace in: " in line and "##" not in line: __A = line.split("\"" )[1] __A = skip_units(snake_case__ ) elif "# Below: " in line and "##" not in line: __A = line.split("\"" )[1] __A = skip_units(snake_case__ ) elif "# End." in line and "##" not in line: if not skip_file and not skip_snippet: replace(snake_case__ ,snake_case__ ,snake_case__ ) __A = [] elif "# Replace with" in line and "##" not in line: __A = [] elif "##" not in line: lines_to_copy.append(snake_case__ ) remove(snake_case__ ) replace_in_files(f'''{directory}/to_replace_{lowercase_model_name}.py''' ) os.rmdir(snake_case__ )
15
import argparse import intel_extension_for_pytorch as ipex import torch from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline __snake_case = argparse.ArgumentParser('''Stable Diffusion script with intel optimization''', add_help=False) parser.add_argument('''--dpm''', action='''store_true''', help='''Enable DPMSolver or not''') parser.add_argument('''--steps''', default=None, type=int, help='''Num inference steps''') __snake_case = parser.parse_args() __snake_case = '''cpu''' __snake_case = '''a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings''' __snake_case = '''path-to-your-trained-model''' __snake_case = StableDiffusionPipeline.from_pretrained(model_id) if args.dpm: __snake_case = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) __snake_case = pipe.to(device) # to channels last __snake_case = pipe.unet.to(memory_format=torch.channels_last) __snake_case = pipe.vae.to(memory_format=torch.channels_last) __snake_case = pipe.text_encoder.to(memory_format=torch.channels_last) if pipe.requires_safety_checker: __snake_case = pipe.safety_checker.to(memory_format=torch.channels_last) # optimize with ipex __snake_case = torch.randn(2, 4, 64, 64) __snake_case = torch.rand(1) * 9_99 __snake_case = torch.randn(2, 77, 7_68) __snake_case = (sample, timestep, encoder_hidden_status) try: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example) except Exception: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True) if pipe.requires_safety_checker: __snake_case = ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True) # compute __snake_case = 6_66 __snake_case = torch.Generator(device).manual_seed(seed) __snake_case = {'''generator''': generator} if args.steps is not None: __snake_case = args.steps with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa): __snake_case = pipe(prompt, **generate_kwargs).images[0] # save image image.save('''generated.png''')
348
0
import argparse import json from collections import OrderedDict import torch from huggingface_hub import cached_download, hf_hub_url from transformers import AutoImageProcessor, CvtConfig, CvtForImageClassification def a( A : Tuple ) -> int: """simple docstring""" a = [] embed.append( ( f'''cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.weight''', f'''stage{idx}.patch_embed.proj.weight''', ) ) embed.append( ( f'''cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.bias''', f'''stage{idx}.patch_embed.proj.bias''', ) ) embed.append( ( f'''cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.weight''', f'''stage{idx}.patch_embed.norm.weight''', ) ) embed.append( ( f'''cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.bias''', f'''stage{idx}.patch_embed.norm.bias''', ) ) return embed def a( A : Tuple , A : List[Any] ) -> str: """simple docstring""" a = [] attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.convolution.weight''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_q.conv.weight''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.weight''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.weight''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.bias''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.bias''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_mean''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_mean''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_var''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_var''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.num_batches_tracked''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.num_batches_tracked''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.convolution.weight''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_k.conv.weight''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.weight''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.weight''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.bias''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.bias''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_mean''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_mean''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_var''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_var''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.num_batches_tracked''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.num_batches_tracked''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.convolution.weight''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_v.conv.weight''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.weight''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.weight''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.bias''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.bias''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_mean''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_mean''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_var''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_var''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.num_batches_tracked''', f'''stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.num_batches_tracked''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.weight''', f'''stage{idx}.blocks.{cnt}.attn.proj_q.weight''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.bias''', f'''stage{idx}.blocks.{cnt}.attn.proj_q.bias''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.weight''', f'''stage{idx}.blocks.{cnt}.attn.proj_k.weight''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.bias''', f'''stage{idx}.blocks.{cnt}.attn.proj_k.bias''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.weight''', f'''stage{idx}.blocks.{cnt}.attn.proj_v.weight''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.bias''', f'''stage{idx}.blocks.{cnt}.attn.proj_v.bias''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.weight''', f'''stage{idx}.blocks.{cnt}.attn.proj.weight''', ) ) attention_weights.append( ( f'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.bias''', f'''stage{idx}.blocks.{cnt}.attn.proj.bias''', ) ) attention_weights.append( (f'''cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.weight''', f'''stage{idx}.blocks.{cnt}.mlp.fc1.weight''') ) attention_weights.append( (f'''cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.bias''', f'''stage{idx}.blocks.{cnt}.mlp.fc1.bias''') ) attention_weights.append( (f'''cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.weight''', f'''stage{idx}.blocks.{cnt}.mlp.fc2.weight''') ) attention_weights.append( (f'''cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.bias''', f'''stage{idx}.blocks.{cnt}.mlp.fc2.bias''') ) attention_weights.append( (f'''cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.weight''', f'''stage{idx}.blocks.{cnt}.norm1.weight''') ) attention_weights.append( (f'''cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.bias''', f'''stage{idx}.blocks.{cnt}.norm1.bias''') ) attention_weights.append( (f'''cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.weight''', f'''stage{idx}.blocks.{cnt}.norm2.weight''') ) attention_weights.append( (f'''cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.bias''', f'''stage{idx}.blocks.{cnt}.norm2.bias''') ) return attention_weights def a( A : List[Any] ) -> Tuple: """simple docstring""" a = [] token.append((f'''cvt.encoder.stages.{idx}.cls_token''', "stage2.cls_token") ) return token def a( ) -> Dict: """simple docstring""" a = [] head.append(("layernorm.weight", "norm.weight") ) head.append(("layernorm.bias", "norm.bias") ) head.append(("classifier.weight", "head.weight") ) head.append(("classifier.bias", "head.bias") ) return head def a( A : Optional[Any] , A : Union[str, Any] , A : List[str] , A : Optional[int] ) -> Optional[int]: """simple docstring""" a = '''imagenet-1k-id2label.json''' a = 1000 a = '''huggingface/label-files''' a = num_labels a = json.load(open(cached_download(hf_hub_url(__lowerCAmelCase , __lowerCAmelCase , repo_type="dataset" ) ) , "r" ) ) a = {int(__lowerCAmelCase ): v for k, v in idalabel.items()} a = idalabel a = {v: k for k, v in idalabel.items()} a = CvtConfig(num_labels=__lowerCAmelCase , idalabel=__lowerCAmelCase , labelaid=__lowerCAmelCase ) # For depth size 13 (13 = 1+2+10) if cvt_model.rsplit("/" , 1 )[-1][4:6] == "13": a = [1, 2, 10] # For depth size 21 (21 = 1+4+16) elif cvt_model.rsplit("/" , 1 )[-1][4:6] == "21": a = [1, 4, 16] # For wide cvt (similar to wide-resnet) depth size 24 (w24 = 2 + 2 20) else: a = [2, 2, 20] a = [3, 12, 16] a = [192, 768, 1024] a = CvtForImageClassification(__lowerCAmelCase ) a = AutoImageProcessor.from_pretrained("facebook/convnext-base-224-22k-1k" ) a = image_size a = torch.load(__lowerCAmelCase , map_location=torch.device("cpu" ) ) a = OrderedDict() a = [] for idx in range(len(config.depth ) ): if config.cls_token[idx]: a = list_of_state_dict + cls_token(__lowerCAmelCase ) a = list_of_state_dict + embeddings(__lowerCAmelCase ) for cnt in range(config.depth[idx] ): a = list_of_state_dict + attention(__lowerCAmelCase , __lowerCAmelCase ) a = list_of_state_dict + final() for gg in list_of_state_dict: print(__lowerCAmelCase ) for i in range(len(__lowerCAmelCase ) ): a = original_weights[list_of_state_dict[i][1]] model.load_state_dict(__lowerCAmelCase ) model.save_pretrained(__lowerCAmelCase ) image_processor.save_pretrained(__lowerCAmelCase ) # Download the weights from zoo: https://1drv.ms/u/s!AhIXJn_J-blW9RzF3rMW7SsLHa8h?e=blQ0Al if __name__ == "__main__": _lowercase: Dict = argparse.ArgumentParser() parser.add_argument( "--cvt_model", default="cvt-w24", type=str, help="Name of the cvt model you\'d like to convert.", ) parser.add_argument( "--image_size", default=384, type=int, help="Input Image Size", ) parser.add_argument( "--cvt_file_name", default=r"cvtmodels\CvT-w24-384x384-IN-22k.pth", type=str, help="Input Image Size", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) _lowercase: int = parser.parse_args() convert_cvt_checkpoint(args.cvt_model, args.image_size, args.cvt_file_name, args.pytorch_dump_folder_path)
227
__snake_case = '''Input must be a string of 8 numbers plus letter''' __snake_case = '''TRWAGMYFPDXBNJZSQVHLCKE''' def lowerCAmelCase_ ( __lowerCAmelCase )-> bool: '''simple docstring''' if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCAmelCase : Optional[Any] =f'''Expected string as input, found {type(__lowerCAmelCase ).__name__}''' raise TypeError(__lowerCAmelCase ) UpperCAmelCase : List[Any] =spanish_id.replace('''-''' , '''''' ).upper() if len(__lowerCAmelCase ) != 9: raise ValueError(__lowerCAmelCase ) try: UpperCAmelCase : int =int(spanish_id_clean[0:8] ) UpperCAmelCase : Optional[int] =spanish_id_clean[8] except ValueError as ex: raise ValueError(__lowerCAmelCase ) from ex if letter.isdigit(): raise ValueError(__lowerCAmelCase ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
348
0
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class __lowerCAmelCase ( lowerCamelCase__ , lowerCamelCase__ ): _a = 1 @register_to_config def __init__( self , lowerCAmelCase=2_000 , lowerCAmelCase=0.1 , lowerCAmelCase=20 , lowerCAmelCase=1e-3 ) -> int: '''simple docstring''' _lowercase =None _lowercase =None _lowercase =None def A__ ( self , lowerCAmelCase , lowerCAmelCase = None ) -> List[Any]: '''simple docstring''' _lowercase =torch.linspace(1 , self.config.sampling_eps , snake_case__ , device=snake_case__ ) def A__ ( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase=None ) -> Any: '''simple docstring''' if self.timesteps is None: raise ValueError( '`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler' ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score _lowercase =( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) _lowercase =torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) _lowercase =std.flatten() while len(std.shape ) < len(score.shape ): _lowercase =std.unsqueeze(-1 ) _lowercase =-score / std # compute _lowercase =-1.0 / len(self.timesteps ) _lowercase =self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) _lowercase =beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): _lowercase =beta_t.unsqueeze(-1 ) _lowercase =-0.5 * beta_t * x _lowercase =torch.sqrt(snake_case__ ) _lowercase =drift - diffusion**2 * score _lowercase =x + drift * dt # add noise _lowercase =randn_tensor(x.shape , layout=x.layout , generator=snake_case__ , device=x.device , dtype=x.dtype ) _lowercase =x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__( self ) -> Dict: '''simple docstring''' return self.config.num_train_timesteps
205
def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Dict =str(bin(__lowerCAmelCase ) ) binary_number += "0" * shift_amount return binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Any =str(bin(__lowerCAmelCase ) )[2:] if shift_amount >= len(__lowerCAmelCase ): return "0b0" UpperCAmelCase : Optional[Any] =binary_number[: len(__lowerCAmelCase ) - shift_amount] return "0b" + shifted_binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number >= 0: # Get binary representation of positive number UpperCAmelCase : Optional[Any] ='''0''' + str(bin(__lowerCAmelCase ) ).strip('''-''' )[2:] else: # Get binary (2's complement) representation of negative number UpperCAmelCase : int =len(bin(__lowerCAmelCase )[3:] ) # Find 2's complement of number UpperCAmelCase : Any =bin(abs(__lowerCAmelCase ) - (1 << binary_number_length) )[3:] UpperCAmelCase : Optional[Any] =( '''1''' + '''0''' * (binary_number_length - len(__lowerCAmelCase )) + binary_number ) if shift_amount >= len(__lowerCAmelCase ): return "0b" + binary_number[0] * len(__lowerCAmelCase ) return ( "0b" + binary_number[0] * shift_amount + binary_number[: len(__lowerCAmelCase ) - shift_amount] ) if __name__ == "__main__": import doctest doctest.testmod()
348
0
"""simple docstring""" from __future__ import annotations def _lowercase ( __lowerCAmelCase , __lowerCAmelCase ) -> set[str]: SCREAMING_SNAKE_CASE__ : Dict = set(__lowerCAmelCase ), [start] while stack: SCREAMING_SNAKE_CASE__ : int = stack.pop() explored.add(__lowerCAmelCase ) # Differences from BFS: # 1) pop last element instead of first one # 2) add adjacent elements to stack without exploring them for adj in reversed(graph[v] ): if adj not in explored: stack.append(__lowerCAmelCase ) return explored a :Tuple = { "A": ["B", "C", "D"], "B": ["A", "D", "E"], "C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"], "F": ["C", "E", "G"], "G": ["F"], } if __name__ == "__main__": import doctest doctest.testmod() print(depth_first_search(G, "A"))
132
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) # TODO Update this __snake_case = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Tuple = """esm""" def __init__( self , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=1026 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__="absolute" , snake_case__=True , snake_case__=None , snake_case__=False , snake_case__=False , snake_case__=None , snake_case__=None , **snake_case__ , ) -> Union[str, Any]: '''simple docstring''' super().__init__(pad_token_id=snake_case__ , mask_token_id=snake_case__ , **snake_case__ ) UpperCAmelCase : List[str] =vocab_size UpperCAmelCase : str =hidden_size UpperCAmelCase : List[Any] =num_hidden_layers UpperCAmelCase : Optional[Any] =num_attention_heads UpperCAmelCase : str =intermediate_size UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : int =attention_probs_dropout_prob UpperCAmelCase : Dict =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : Union[str, Any] =layer_norm_eps UpperCAmelCase : Dict =position_embedding_type UpperCAmelCase : Optional[Any] =use_cache UpperCAmelCase : int =emb_layer_norm_before UpperCAmelCase : List[str] =token_dropout UpperCAmelCase : Optional[Any] =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) UpperCAmelCase : Optional[Any] =EsmFoldConfig() elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =EsmFoldConfig(**snake_case__ ) UpperCAmelCase : Tuple =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) UpperCAmelCase : Any =get_default_vocab_list() else: UpperCAmelCase : Tuple =vocab_list else: UpperCAmelCase : Optional[int] =None UpperCAmelCase : Union[str, Any] =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , snake_case__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =super().to_dict() if isinstance(self.esmfold_config , snake_case__ ): UpperCAmelCase : str =self.esmfold_config.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : str = None __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : float = 0 __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : int = 128 __lowerCamelCase : "TrunkConfig" = None def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' if self.trunk is None: UpperCAmelCase : str =TrunkConfig() elif isinstance(self.trunk , snake_case__ ): UpperCAmelCase : Optional[int] =TrunkConfig(**self.trunk ) def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[Any] =asdict(self ) UpperCAmelCase : Any =self.trunk.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 48 __lowerCamelCase : int = 1024 __lowerCamelCase : int = 128 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : float = 0 __lowerCamelCase : float = 0 __lowerCamelCase : bool = False __lowerCamelCase : int = 4 __lowerCamelCase : Optional[int] = 128 __lowerCamelCase : "StructureModuleConfig" = None def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' if self.structure_module is None: UpperCAmelCase : Any =StructureModuleConfig() elif isinstance(self.structure_module , snake_case__ ): UpperCAmelCase : str =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f'''`max_recycles` should be positive, got {self.max_recycles}.''' ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f''' {self.sequence_state_dim} and {self.sequence_state_dim}.''' ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f''' {self.pairwise_state_dim} and {self.pairwise_state_dim}.''' ) UpperCAmelCase : Optional[int] =self.sequence_state_dim // self.sequence_head_width UpperCAmelCase : Any =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f''' {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.''' ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f''' {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.''' ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f'''`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.''' ) if self.dropout >= 0.4: raise ValueError(f'''`dropout` should not be greater than 0.4, got {self.dropout}.''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =asdict(self ) UpperCAmelCase : Tuple =self.structure_module.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 384 __lowerCamelCase : int = 128 __lowerCamelCase : int = 16 __lowerCamelCase : int = 128 __lowerCamelCase : int = 12 __lowerCamelCase : int = 4 __lowerCamelCase : int = 8 __lowerCamelCase : float = 0.1 __lowerCamelCase : int = 8 __lowerCamelCase : int = 1 __lowerCamelCase : int = 2 __lowerCamelCase : int = 7 __lowerCamelCase : int = 10 __lowerCamelCase : float = 1E-8 __lowerCamelCase : float = 1E5 def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return asdict(self ) def lowerCAmelCase_ ( )-> Tuple: '''simple docstring''' return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
348
0
'''simple docstring''' import importlib import torch import yaml from omegaconf import OmegaConf from taming.models.vqgan import VQModel def lowercase_ ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Union[str, Any]=False ): """simple docstring""" __UpperCAmelCase : Any = OmegaConf.load(__lowerCAmelCase ) if display: print(yaml.dump(OmegaConf.to_container(__lowerCAmelCase ) ) ) return config def lowercase_ ( lowerCAmelCase__ : str , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : Tuple=None ): """simple docstring""" if conf_path is None: __UpperCAmelCase : Any = '''./model_checkpoints/vqgan_only.yaml''' __UpperCAmelCase : str = load_config(__lowerCAmelCase , display=__lowerCAmelCase ) __UpperCAmelCase : Tuple = VQModel(**config.model.params ) if ckpt_path is None: __UpperCAmelCase : List[str] = '''./model_checkpoints/vqgan_only.pt''' __UpperCAmelCase : int = torch.load(__lowerCAmelCase , map_location=__lowerCAmelCase ) if ".ckpt" in ckpt_path: __UpperCAmelCase : str = sd['''state_dict'''] model.load_state_dict(__lowerCAmelCase , strict=__lowerCAmelCase ) model.to(__lowerCAmelCase ) del sd return model def lowercase_ ( lowerCAmelCase__ : int , lowerCAmelCase__ : Tuple ): """simple docstring""" __UpperCAmelCase : Optional[Any] = model.encode(__lowerCAmelCase ) print(f'VQGAN --- {model.__class__.__name__}: latent shape: {z.shape[2:]}' ) __UpperCAmelCase : Optional[Any] = model.decode(__lowerCAmelCase ) return xrec def lowercase_ ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Tuple=False ): """simple docstring""" __UpperCAmelCase : Tuple = string.rsplit(""".""" , 1 ) if reload: __UpperCAmelCase : List[Any] = importlib.import_module(__lowerCAmelCase ) importlib.reload(__lowerCAmelCase ) return getattr(importlib.import_module(__lowerCAmelCase , package=__lowerCAmelCase ) , cls ) def lowercase_ ( lowerCAmelCase__ : int ): """simple docstring""" if "target" not in config: raise KeyError("""Expected key `target` to instantiate.""" ) return get_obj_from_str(config["""target"""] )(**config.get("""params""" , {} ) ) def lowercase_ ( lowerCAmelCase__ : Any , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : List[str]=True ): """simple docstring""" __UpperCAmelCase : List[str] = instantiate_from_config(__lowerCAmelCase ) if sd is not None: model.load_state_dict(__lowerCAmelCase ) if gpu: model.cuda() if eval_mode: model.eval() return {"model": model} def lowercase_ ( lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any] ): """simple docstring""" if ckpt: __UpperCAmelCase : List[str] = torch.load(__lowerCAmelCase , map_location="""cpu""" ) __UpperCAmelCase : List[Any] = pl_sd['''global_step'''] print(f'loaded model from global step {global_step}.' ) else: __UpperCAmelCase : int = {'''state_dict''': None} __UpperCAmelCase : str = None __UpperCAmelCase : Dict = load_model_from_config(config.model , pl_sd["""state_dict"""] , gpu=__lowerCAmelCase , eval_mode=__lowerCAmelCase )['''model'''] return model, global_step
254
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[int] = (KDPMaDiscreteScheduler,) __lowerCamelCase : List[str] = 10 def UpperCAmelCase__ ( self , **snake_case__ ) -> str: '''simple docstring''' UpperCAmelCase : int ={ '''num_train_timesteps''': 1100, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', } config.update(**snake_case__ ) return config def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=snake_case__ , beta_end=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' UpperCAmelCase : Optional[Any] =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config(prediction_type='''v_prediction''' ) UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : str =self.dummy_model() UpperCAmelCase : Optional[Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : Union[str, Any] =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : str =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Any =model(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : int =output.prev_sample UpperCAmelCase : Dict =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Optional[Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.69_34e-07 ) < 1e-2 assert abs(result_mean.item() - 6.11_12e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.6_93_42_86_50_17_09_72e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0002 ) < 1e-3 def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : Any =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config() UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : Optional[int] =self.dummy_model() UpperCAmelCase : Union[str, Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : str =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : Dict =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =model(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =output.prev_sample UpperCAmelCase : Any =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Union[str, Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : List[Any] =self.scheduler_classes[0] UpperCAmelCase : Dict =self.get_scheduler_config() UpperCAmelCase : List[str] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps , device=snake_case__ ) UpperCAmelCase : int =self.dummy_model() UpperCAmelCase : Tuple =self.dummy_sample_deter.to(snake_case__ ) * scheduler.init_noise_sigma for t in scheduler.timesteps: UpperCAmelCase : Optional[Any] =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : int =model(snake_case__ , snake_case__ ) UpperCAmelCase : str =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =output.prev_sample UpperCAmelCase : List[str] =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Dict =torch.mean(torch.abs(snake_case__ ) ) if str(snake_case__ ).startswith('''cpu''' ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3
348
0
'''simple docstring''' from argparse import ArgumentParser, Namespace from ..utils import logging from . import BaseTransformersCLICommand def _UpperCamelCase ( SCREAMING_SNAKE_CASE__ : Optional[Any] ): '''simple docstring''' return ConvertCommand( args.model_type , args.tf_checkpoint , args.pytorch_dump_output , args.config , args.finetuning_task_name ) UpperCAmelCase_ = '\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 lowerCAmelCase_ ( lowerCamelCase__ ): '''simple docstring''' @staticmethod def SCREAMING_SNAKE_CASE__ ( _UpperCAmelCase : int ): """simple docstring""" UpperCAmelCase__ = 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=snake_case__ , required=snake_case__ , help="""Model\'s type.""" ) train_parser.add_argument( """--tf_checkpoint""" , type=snake_case__ , required=snake_case__ , help="""TensorFlow checkpoint path or folder.""" ) train_parser.add_argument( """--pytorch_dump_output""" , type=snake_case__ , required=snake_case__ , help="""Path to the PyTorch saved model output.""" ) train_parser.add_argument("""--config""" , type=snake_case__ , default="""""" , help="""Configuration file path or folder.""" ) train_parser.add_argument( """--finetuning_task_name""" , type=snake_case__ , default=snake_case__ , help="""Optional fine-tuning task name if the TF model was a finetuned model.""" , ) train_parser.set_defaults(func=snake_case__ ) def __init__( self : List[str] , _UpperCAmelCase : Union[str, Any] , _UpperCAmelCase : List[str] , _UpperCAmelCase : Tuple , _UpperCAmelCase : Tuple , _UpperCAmelCase : List[str] , *_UpperCAmelCase : int , ): """simple docstring""" UpperCAmelCase__ = logging.get_logger("""transformers-cli/converting""" ) self._logger.info(f'''Loading model {model_type}''' ) UpperCAmelCase__ = model_type UpperCAmelCase__ = tf_checkpoint UpperCAmelCase__ = pytorch_dump_output UpperCAmelCase__ = config UpperCAmelCase__ = finetuning_task_name def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" 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(snake_case__ ) 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(snake_case__ ) 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(snake_case__ ) 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(snake_case__ ) 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(snake_case__ ) if "ckpt" in self._tf_checkpoint.lower(): UpperCAmelCase__ = self._tf_checkpoint UpperCAmelCase__ = '''''' else: UpperCAmelCase__ = self._tf_checkpoint UpperCAmelCase__ = '''''' convert_transfo_xl_checkpoint_to_pytorch( snake_case__ , self._config , self._pytorch_dump_output , snake_case__ ) 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(snake_case__ ) 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(snake_case__ ) 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]""" )
346
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class __snake_case ( unittest.TestCase ): @slow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Any =FlaxMTaForConditionalGeneration.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : Tuple =AutoTokenizer.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : List[str] =tokenizer('''Hello there''' , return_tensors='''np''' ).input_ids UpperCAmelCase : List[Any] =tokenizer('''Hi I am''' , return_tensors='''np''' ).input_ids UpperCAmelCase : Union[str, Any] =shift_tokens_right(snake_case__ , model.config.pad_token_id , model.config.decoder_start_token_id ) UpperCAmelCase : List[str] =model(snake_case__ , decoder_input_ids=snake_case__ ).logits UpperCAmelCase : Any =optax.softmax_cross_entropy(snake_case__ , onehot(snake_case__ , logits.shape[-1] ) ).mean() UpperCAmelCase : Union[str, Any] =-(labels.shape[-1] * loss.item()) UpperCAmelCase : List[str] =-84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
348
0
from dataclasses import dataclass from typing import Dict, Optional, Union import torch import torch.nn.functional as F from torch import nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .attention import BasicTransformerBlock from .attention_processor import AttentionProcessor, AttnProcessor from .embeddings import TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin @dataclass class snake_case__ ( lowerCamelCase__ ): lowercase__ : torch.FloatTensor class snake_case__ ( lowerCamelCase__ , lowerCamelCase__ ): @register_to_config def __init__( self , lowerCAmelCase__ = 32 , lowerCAmelCase__ = 64 , lowerCAmelCase__ = 20 , lowerCAmelCase__ = 7_68 , lowerCAmelCase__=77 , lowerCAmelCase__=4 , lowerCAmelCase__ = 0.0 , lowerCAmelCase__ = "silu" , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = "linear" , lowerCAmelCase__ = "prd" , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = None , ) -> Tuple: super().__init__() __magic_name__ : List[Any] = num_attention_heads __magic_name__ : Tuple = attention_head_dim __magic_name__ : Union[str, Any] = num_attention_heads * attention_head_dim __magic_name__ : int = additional_embeddings __magic_name__ : List[str] = time_embed_dim or inner_dim __magic_name__ : List[str] = embedding_proj_dim or embedding_dim __magic_name__ : Any = clip_embed_dim or embedding_dim __magic_name__ : Dict = Timesteps(snake_case__ , snake_case__ , 0 ) __magic_name__ : Tuple = TimestepEmbedding(snake_case__ , snake_case__ , out_dim=snake_case__ , act_fn=snake_case__ ) __magic_name__ : Union[str, Any] = nn.Linear(snake_case__ , snake_case__ ) if embedding_proj_norm_type is None: __magic_name__ : Dict = None elif embedding_proj_norm_type == "layer": __magic_name__ : int = nn.LayerNorm(snake_case__ ) else: raise ValueError(F'unsupported embedding_proj_norm_type: {embedding_proj_norm_type}' ) __magic_name__ : Optional[Any] = nn.Linear(snake_case__ , snake_case__ ) if encoder_hid_proj_type is None: __magic_name__ : Optional[int] = None elif encoder_hid_proj_type == "linear": __magic_name__ : Any = nn.Linear(snake_case__ , snake_case__ ) else: raise ValueError(F'unsupported encoder_hid_proj_type: {encoder_hid_proj_type}' ) __magic_name__ : Dict = nn.Parameter(torch.zeros(1 , num_embeddings + additional_embeddings , snake_case__ ) ) if added_emb_type == "prd": __magic_name__ : Dict = nn.Parameter(torch.zeros(1 , 1 , snake_case__ ) ) elif added_emb_type is None: __magic_name__ : int = None else: raise ValueError( F'`added_emb_type`: {added_emb_type} is not supported. Make sure to choose one of `\'prd\'` or `None`.' ) __magic_name__ : List[str] = nn.ModuleList( [ BasicTransformerBlock( snake_case__ , snake_case__ , snake_case__ , dropout=snake_case__ , activation_fn="""gelu""" , attention_bias=snake_case__ , ) for d in range(snake_case__ ) ] ) if norm_in_type == "layer": __magic_name__ : Optional[Any] = nn.LayerNorm(snake_case__ ) elif norm_in_type is None: __magic_name__ : Union[str, Any] = None else: raise ValueError(F'Unsupported norm_in_type: {norm_in_type}.' ) __magic_name__ : str = nn.LayerNorm(snake_case__ ) __magic_name__ : List[str] = nn.Linear(snake_case__ , snake_case__ ) __magic_name__ : Union[str, Any] = torch.full( [num_embeddings + additional_embeddings, num_embeddings + additional_embeddings] , -1_00_00.0 ) causal_attention_mask.triu_(1 ) __magic_name__ : Optional[Any] = causal_attention_mask[None, ...] self.register_buffer("""causal_attention_mask""" , snake_case__ , persistent=snake_case__ ) __magic_name__ : int = nn.Parameter(torch.zeros(1 , snake_case__ ) ) __magic_name__ : Union[str, Any] = nn.Parameter(torch.zeros(1 , snake_case__ ) ) @property # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors def __magic_name__ ( self ) -> Dict[str, AttentionProcessor]: __magic_name__ : Dict = {} def fn_recursive_add_processors(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ): if hasattr(snake_case__ , """set_processor""" ): __magic_name__ : Tuple = module.processor for sub_name, child in module.named_children(): fn_recursive_add_processors(F'{name}.{sub_name}' , snake_case__ , snake_case__ ) return processors for name, module in self.named_children(): fn_recursive_add_processors(snake_case__ , snake_case__ , snake_case__ ) return processors def __magic_name__ ( self , lowerCAmelCase__ ) -> Dict: __magic_name__ : int = len(self.attn_processors.keys() ) if isinstance(snake_case__ , snake_case__ ) and len(snake_case__ ) != count: raise ValueError( F'A dict of processors was passed, but the number of processors {len(snake_case__ )} does not match the' F' number of attention layers: {count}. Please make sure to pass {count} processor classes.' ) def fn_recursive_attn_processor(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ): if hasattr(snake_case__ , """set_processor""" ): if not isinstance(snake_case__ , snake_case__ ): module.set_processor(snake_case__ ) else: module.set_processor(processor.pop(F'{name}.processor' ) ) for sub_name, child in module.named_children(): fn_recursive_attn_processor(F'{name}.{sub_name}' , snake_case__ , snake_case__ ) for name, module in self.named_children(): fn_recursive_attn_processor(snake_case__ , snake_case__ , snake_case__ ) def __magic_name__ ( self ) -> Optional[Any]: self.set_attn_processor(AttnProcessor() ) def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = True , ) -> Any: __magic_name__ : List[str] = hidden_states.shape[0] __magic_name__ : int = timestep if not torch.is_tensor(snake_case__ ): __magic_name__ : Dict = torch.tensor([timesteps] , dtype=torch.long , device=hidden_states.device ) elif torch.is_tensor(snake_case__ ) and len(timesteps.shape ) == 0: __magic_name__ : Optional[int] = timesteps[None].to(hidden_states.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML __magic_name__ : Tuple = timesteps * torch.ones(snake_case__ , dtype=timesteps.dtype , device=timesteps.device ) __magic_name__ : Union[str, Any] = self.time_proj(snake_case__ ) # timesteps does not contain any weights and will always return f32 tensors # but time_embedding might be fp16, so we need to cast here. __magic_name__ : Any = timesteps_projected.to(dtype=self.dtype ) __magic_name__ : Any = self.time_embedding(snake_case__ ) if self.embedding_proj_norm is not None: __magic_name__ : Any = self.embedding_proj_norm(snake_case__ ) __magic_name__ : Dict = self.embedding_proj(snake_case__ ) if self.encoder_hidden_states_proj is not None and encoder_hidden_states is not None: __magic_name__ : Tuple = self.encoder_hidden_states_proj(snake_case__ ) elif self.encoder_hidden_states_proj is not None and encoder_hidden_states is None: raise ValueError("""`encoder_hidden_states_proj` requires `encoder_hidden_states` to be set""" ) __magic_name__ : int = self.proj_in(snake_case__ ) __magic_name__ : Dict = self.positional_embedding.to(hidden_states.dtype ) __magic_name__ : List[Any] = [] __magic_name__ : List[Any] = 0 if encoder_hidden_states is not None: additional_embeds.append(snake_case__ ) additional_embeddings_len += encoder_hidden_states.shape[1] if len(proj_embeddings.shape ) == 2: __magic_name__ : List[str] = proj_embeddings[:, None, :] if len(hidden_states.shape ) == 2: __magic_name__ : Dict = hidden_states[:, None, :] __magic_name__ : int = additional_embeds + [ proj_embeddings, time_embeddings[:, None, :], hidden_states, ] if self.prd_embedding is not None: __magic_name__ : Optional[Any] = self.prd_embedding.to(hidden_states.dtype ).expand(snake_case__ , -1 , -1 ) additional_embeds.append(snake_case__ ) __magic_name__ : int = torch.cat( snake_case__ , dim=1 , ) # Allow positional_embedding to not include the `addtional_embeddings` and instead pad it with zeros for these additional tokens __magic_name__ : Any = additional_embeddings_len + proj_embeddings.shape[1] + 1 if positional_embeddings.shape[1] < hidden_states.shape[1]: __magic_name__ : List[Any] = F.pad( snake_case__ , ( 0, 0, additional_embeddings_len, self.prd_embedding.shape[1] if self.prd_embedding is not None else 0, ) , value=0.0 , ) __magic_name__ : Tuple = hidden_states + positional_embeddings if attention_mask is not None: __magic_name__ : List[str] = (1 - attention_mask.to(hidden_states.dtype )) * -1_00_00.0 __magic_name__ : Tuple = F.pad(snake_case__ , (0, self.additional_embeddings) , value=0.0 ) __magic_name__ : Dict = (attention_mask[:, None, :] + self.causal_attention_mask).to(hidden_states.dtype ) __magic_name__ : Optional[int] = attention_mask.repeat_interleave(self.config.num_attention_heads , dim=0 ) if self.norm_in is not None: __magic_name__ : Optional[Any] = self.norm_in(snake_case__ ) for block in self.transformer_blocks: __magic_name__ : Optional[int] = block(snake_case__ , attention_mask=snake_case__ ) __magic_name__ : Optional[int] = self.norm_out(snake_case__ ) if self.prd_embedding is not None: __magic_name__ : List[Any] = hidden_states[:, -1] else: __magic_name__ : Dict = hidden_states[:, additional_embeddings_len:] __magic_name__ : List[str] = self.proj_to_clip_embeddings(snake_case__ ) if not return_dict: return (predicted_image_embedding,) return PriorTransformerOutput(predicted_image_embedding=snake_case__ ) def __magic_name__ ( self , lowerCAmelCase__ ) -> Union[str, Any]: __magic_name__ : str = (prior_latents * self.clip_std) + self.clip_mean return prior_latents
342
import unittest import numpy as np from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __snake_case ( lowerCamelCase__ , unittest.TestCase ): # FIXME: add fast tests pass @nightly @require_onnxruntime @require_torch_gpu class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : List[Any] =ort.SessionOptions() UpperCAmelCase : Optional[int] =False return options def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : int =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Optional[Any] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Dict ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : Any =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=10 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : Optional[int] =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Tuple =np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Tuple =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , subfolder='''scheduler''' , revision='''onnx''' ) UpperCAmelCase : int =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , scheduler=snake_case__ , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Union[str, Any] ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : str =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=20 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : int =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Union[str, Any] =np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3
348
0
"""simple docstring""" import argparse import torch from torch import nn from transformers import SpeechaTextConfig, SpeechaTextForConditionalGeneration def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : List[Any] ): '''simple docstring''' lowerCAmelCase = [ '''encoder.version''', '''decoder.version''', '''model.encoder.version''', '''model.decoder.version''', '''decoder.output_projection.weight''', '''_float_tensor''', '''encoder.embed_positions._float_tensor''', '''decoder.embed_positions._float_tensor''', ] for k in ignore_keys: state_dict.pop(__lowerCAmelCase , __lowerCAmelCase ) def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : Dict ): '''simple docstring''' lowerCAmelCase = list(s_dict.keys() ) for key in keys: if "transformer_layers" in key: lowerCAmelCase = s_dict.pop(__lowerCAmelCase ) elif "subsample" in key: lowerCAmelCase = s_dict.pop(__lowerCAmelCase ) def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : Union[str, Any] ): '''simple docstring''' lowerCAmelCase = emb.weight.shape lowerCAmelCase = nn.Linear(__lowerCAmelCase , __lowerCAmelCase , bias=__lowerCAmelCase ) lowerCAmelCase = emb.weight.data return lin_layer def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : Tuple ): '''simple docstring''' lowerCAmelCase = torch.load(__lowerCAmelCase , map_location="""cpu""" ) lowerCAmelCase = mam_aaa['''args'''] lowerCAmelCase = mam_aaa['''model'''] lowerCAmelCase = state_dict['''decoder.output_projection.weight'''] remove_ignore_keys_(__lowerCAmelCase ) rename_keys(__lowerCAmelCase ) lowerCAmelCase = state_dict['''decoder.embed_tokens.weight'''].shape[0] lowerCAmelCase = args.share_decoder_input_output_embed lowerCAmelCase = [int(__lowerCAmelCase ) for i in args.conv_kernel_sizes.split(""",""" )] lowerCAmelCase = SpeechaTextConfig( vocab_size=__lowerCAmelCase , max_source_positions=args.max_source_positions , max_target_positions=args.max_target_positions , encoder_layers=args.encoder_layers , decoder_layers=args.decoder_layers , encoder_attention_heads=args.encoder_attention_heads , decoder_attention_heads=args.decoder_attention_heads , encoder_ffn_dim=args.encoder_ffn_embed_dim , decoder_ffn_dim=args.decoder_ffn_embed_dim , d_model=args.encoder_embed_dim , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""relu""" , num_conv_layers=len(__lowerCAmelCase ) , conv_channels=args.conv_channels , conv_kernel_sizes=__lowerCAmelCase , input_feat_per_channel=args.input_feat_per_channel , input_channels=args.input_channels , tie_word_embeddings=__lowerCAmelCase , num_beams=5 , max_length=2_00 , use_cache=__lowerCAmelCase , decoder_start_token_id=2 , early_stopping=__lowerCAmelCase , ) lowerCAmelCase = SpeechaTextForConditionalGeneration(__lowerCAmelCase ) lowerCAmelCase = model.model.load_state_dict(__lowerCAmelCase , strict=__lowerCAmelCase ) if len(__lowerCAmelCase ) > 0 and not set(__lowerCAmelCase ) <= { "encoder.embed_positions.weights", "decoder.embed_positions.weights", }: raise ValueError( """Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights` are allowed to be missing,""" F' but all the following weights are missing {missing}' ) if tie_embeds: lowerCAmelCase = make_linear_from_emb(model.model.decoder.embed_tokens ) else: lowerCAmelCase = lm_head_weights model.save_pretrained(__lowerCAmelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() # Required parameters parser.add_argument("--fairseq_path", type=str, help="Path to the fairseq model (.pt) file.") parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_fairseq_sat_checkpoint_to_tfms(args.fairseq_path, args.pytorch_dump_folder_path)
46
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def lowerCAmelCase_ ( )-> int: '''simple docstring''' UpperCAmelCase : str ={ '''repo_name''': ['''test_repo1''', '''test_repo2''', '''test_repo3'''], '''path''': ['''test_1.py''', '''test_2.py''', '''unit_test.py'''], '''content''': ['''a ''' * 20, '''a ''' * 30, '''b ''' * 7], } UpperCAmelCase : Union[str, Any] =Dataset.from_dict(__lowerCAmelCase ) return dataset class __snake_case ( lowerCamelCase__ ): def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[str] =get_dataset() UpperCAmelCase : Optional[int] =make_duplicate_clusters(snake_case__ , 0.85 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : str =get_dataset() UpperCAmelCase , UpperCAmelCase : Tuple =deduplicate_dataset(snake_case__ ) self.assertEqual(len(snake_case__ ) , 2 ) print(snake_case__ ) self.assertEqual(duplicate_clusters[0][0]['''copies'''] , 2 ) self.assertEqual(duplicate_clusters[0][0]['''is_extreme'''] , snake_case__ )
348
0
import gc import unittest from diffusers import FlaxControlNetModel, FlaxStableDiffusionControlNetPipeline from diffusers.utils import is_flax_available, load_image, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : str ) -> List[Any]: """simple docstring""" super().tearDown() gc.collect() def a_ ( self : str ) -> Union[str, Any]: """simple docstring""" A__ = FlaxControlNetModel.from_pretrained( """lllyasviel/sd-controlnet-canny""" , from_pt=snake_case__ , dtype=jnp.bfloataa ) A__ = FlaxStableDiffusionControlNetPipeline.from_pretrained( """runwayml/stable-diffusion-v1-5""" , controlnet=snake_case__ , from_pt=snake_case__ , dtype=jnp.bfloataa ) A__ = controlnet_params A__ = '''bird''' A__ = jax.device_count() A__ = pipe.prepare_text_inputs([prompts] * num_samples ) A__ = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png""" ) A__ = pipe.prepare_image_inputs([canny_image] * num_samples ) A__ = jax.random.PRNGKey(0 ) A__ = jax.random.split(snake_case__ , jax.device_count() ) A__ = replicate(snake_case__ ) A__ = shard(snake_case__ ) A__ = shard(snake_case__ ) A__ = pipe( prompt_ids=snake_case__ , image=snake_case__ , params=snake_case__ , prng_seed=snake_case__ , num_inference_steps=50 , jit=snake_case__ , ).images assert images.shape == (jax.device_count(), 1, 7_68, 5_12, 3) A__ = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) A__ = images[0, 2_53:2_56, 2_53:2_56, -1] A__ = jnp.asarray(jax.device_get(image_slice.flatten() ) ) A__ = jnp.array( [0.1_6_7_9_6_9, 0.1_1_6_6_9_9, 0.0_8_1_5_4_3, 0.1_5_4_2_9_7, 0.1_3_2_8_1_2, 0.1_0_8_8_8_7, 0.1_6_9_9_2_2, 0.1_6_9_9_2_2, 0.2_0_5_0_7_8] ) print(f'output_slice: {output_slice}' ) assert jnp.abs(output_slice - expected_slice ).max() < 1e-2 def a_ ( self : int ) -> Optional[int]: """simple docstring""" A__ = FlaxControlNetModel.from_pretrained( """lllyasviel/sd-controlnet-openpose""" , from_pt=snake_case__ , dtype=jnp.bfloataa ) A__ = FlaxStableDiffusionControlNetPipeline.from_pretrained( """runwayml/stable-diffusion-v1-5""" , controlnet=snake_case__ , from_pt=snake_case__ , dtype=jnp.bfloataa ) A__ = controlnet_params A__ = '''Chef in the kitchen''' A__ = jax.device_count() A__ = pipe.prepare_text_inputs([prompts] * num_samples ) A__ = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/pose.png""" ) A__ = pipe.prepare_image_inputs([pose_image] * num_samples ) A__ = jax.random.PRNGKey(0 ) A__ = jax.random.split(snake_case__ , jax.device_count() ) A__ = replicate(snake_case__ ) A__ = shard(snake_case__ ) A__ = shard(snake_case__ ) A__ = pipe( prompt_ids=snake_case__ , image=snake_case__ , params=snake_case__ , prng_seed=snake_case__ , num_inference_steps=50 , jit=snake_case__ , ).images assert images.shape == (jax.device_count(), 1, 7_68, 5_12, 3) A__ = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) A__ = images[0, 2_53:2_56, 2_53:2_56, -1] A__ = jnp.asarray(jax.device_get(image_slice.flatten() ) ) A__ = jnp.array( [[0.2_7_1_4_8_4, 0.2_6_1_7_1_9, 0.2_7_5_3_9_1, 0.2_7_7_3_4_4, 0.2_7_9_2_9_7, 0.2_9_1_0_1_6, 0.2_9_4_9_2_2, 0.3_0_2_7_3_4, 0.3_0_2_7_3_4]] ) print(f'output_slice: {output_slice}' ) assert jnp.abs(output_slice - expected_slice ).max() < 1e-2
274
from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput __snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name class __snake_case ( lowerCamelCase__ , lowerCamelCase__ ): @register_to_config def __init__( self , snake_case__ , snake_case__ = None , snake_case__ = None ) -> str: '''simple docstring''' super().__init__() UpperCAmelCase : Optional[Any] =learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" UpperCAmelCase : Any =torch.zeros(snake_case__ , snake_case__ ) else: UpperCAmelCase : Union[str, Any] =None UpperCAmelCase : Optional[int] =torch.nn.Parameter(snake_case__ ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : VQModel __lowerCamelCase : CLIPTextModel __lowerCamelCase : CLIPTokenizer __lowerCamelCase : TransformeraDModel __lowerCamelCase : LearnedClassifierFreeSamplingEmbeddings __lowerCamelCase : VQDiffusionScheduler def __init__( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> int: '''simple docstring''' super().__init__() self.register_modules( vqvae=snake_case__ , transformer=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , scheduler=snake_case__ , learned_classifier_free_sampling_embeddings=snake_case__ , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : int =len(snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else 1 # get prompt text embeddings UpperCAmelCase : Optional[int] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) UpperCAmelCase : int =text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCAmelCase : List[str] =self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' f''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) UpperCAmelCase : Optional[Any] =text_input_ids[:, : self.tokenizer.model_max_length] UpperCAmelCase : List[Any] =self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 UpperCAmelCase : int =prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate text embeddings for each generation per prompt UpperCAmelCase : int =prompt_embeds.repeat_interleave(snake_case__ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: UpperCAmelCase : Optional[int] =self.learned_classifier_free_sampling_embeddings.embeddings UpperCAmelCase : str =negative_prompt_embeds.unsqueeze(0 ).repeat(snake_case__ , 1 , 1 ) else: UpperCAmelCase : str =[''''''] * batch_size UpperCAmelCase : Tuple =text_input_ids.shape[-1] UpperCAmelCase : Optional[Any] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=snake_case__ , truncation=snake_case__ , return_tensors='''pt''' , ) UpperCAmelCase : Optional[Any] =self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings UpperCAmelCase : Optional[int] =negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCAmelCase : Optional[Any] =negative_prompt_embeds.shape[1] UpperCAmelCase : Union[str, Any] =negative_prompt_embeds.repeat(1 , snake_case__ , 1 ) UpperCAmelCase : Optional[Any] =negative_prompt_embeds.view(batch_size * num_images_per_prompt , snake_case__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCAmelCase : int =torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , snake_case__ , snake_case__ = 100 , snake_case__ = 5.0 , snake_case__ = 1.0 , snake_case__ = 1 , snake_case__ = None , snake_case__ = None , snake_case__ = "pil" , snake_case__ = True , snake_case__ = None , snake_case__ = 1 , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =1 elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Tuple =len(snake_case__ ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(snake_case__ )}''' ) UpperCAmelCase : Tuple =batch_size * num_images_per_prompt UpperCAmelCase : List[str] =guidance_scale > 1.0 UpperCAmelCase : List[Any] =self._encode_prompt(snake_case__ , snake_case__ , snake_case__ ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case__ , snake_case__ ) or callback_steps <= 0) ): raise ValueError( f'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' f''' {type(snake_case__ )}.''' ) # get the initial completely masked latents unless the user supplied it UpperCAmelCase : int =(batch_size, self.transformer.num_latent_pixels) if latents is None: UpperCAmelCase : Union[str, Any] =self.transformer.num_vector_embeds - 1 UpperCAmelCase : str =torch.full(snake_case__ , snake_case__ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( '''Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,''' f''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) UpperCAmelCase : Any =latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(snake_case__ , device=self.device ) UpperCAmelCase : Any =self.scheduler.timesteps.to(self.device ) UpperCAmelCase : Optional[int] =latents for i, t in enumerate(self.progress_bar(snake_case__ ) ): # expand the sample if we are doing classifier free guidance UpperCAmelCase : Optional[Any] =torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` UpperCAmelCase : Optional[int] =self.transformer(snake_case__ , encoder_hidden_states=snake_case__ , timestep=snake_case__ ).sample if do_classifier_free_guidance: UpperCAmelCase , UpperCAmelCase : str =model_output.chunk(2 ) UpperCAmelCase : Optional[int] =model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(snake_case__ , dim=1 , keepdim=snake_case__ ) UpperCAmelCase : Tuple =self.truncate(snake_case__ , snake_case__ ) # remove `log(0)`'s (`-inf`s) UpperCAmelCase : Optional[Any] =model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 UpperCAmelCase : int =self.scheduler.step(snake_case__ , timestep=snake_case__ , sample=snake_case__ , generator=snake_case__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =self.vqvae.config.vq_embed_dim UpperCAmelCase : Optional[Any] =(batch_size, self.transformer.height, self.transformer.width, embedding_channels) UpperCAmelCase : Dict =self.vqvae.quantize.get_codebook_entry(snake_case__ , shape=snake_case__ ) UpperCAmelCase : Tuple =self.vqvae.decode(snake_case__ , force_not_quantize=snake_case__ ).sample UpperCAmelCase : Union[str, Any] =(image / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase : Any =image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase : List[str] =self.numpy_to_pil(snake_case__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> torch.FloatTensor: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : int =torch.sort(snake_case__ , 1 , descending=snake_case__ ) UpperCAmelCase : Union[str, Any] =torch.exp(snake_case__ ) UpperCAmelCase : Union[str, Any] =sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out UpperCAmelCase : Optional[Any] =torch.full_like(keep_mask[:, 0:1, :] , snake_case__ ) UpperCAmelCase : Tuple =torch.cat((all_true, keep_mask) , dim=1 ) UpperCAmelCase : int =keep_mask[:, :-1, :] UpperCAmelCase : int =keep_mask.gather(1 , indices.argsort(1 ) ) UpperCAmelCase : Dict =log_p_x_0.clone() UpperCAmelCase : List[Any] =-torch.inf # -inf = log(0) return rv
348
0
import heapq def UpperCAmelCase_ ( __SCREAMING_SNAKE_CASE ): lowercase = [] # for each node and his adjacency list add them and the rank of the node to queue # using heapq module the queue will be filled like a Priority Queue # heapq works with a min priority queue, so I used -1*len(v) to build it for key, value in graph.items(): # O(log(n)) heapq.heappush(__lowerCAmelCase , [-1 * len(__lowerCAmelCase ), (key, value)] ) # chosen_vertices = set of chosen vertices lowercase = set() # while queue isn't empty and there are still edges # (queue[0][0] is the rank of the node with max rank) while queue and queue[0][0] != 0: # extract vertex with max rank from queue and add it to chosen_vertices lowercase = heapq.heappop(__lowerCAmelCase )[1][0] chosen_vertices.add(__lowerCAmelCase ) # Remove all arcs adjacent to argmax for elem in queue: # if v haven't adjacent node, skip if elem[0] == 0: continue # if argmax is reachable from elem # remove argmax from elem's adjacent list and update his rank if argmax in elem[1][1]: lowercase = elem[1][1].index(__lowerCAmelCase ) del elem[1][1][index] elem[0] += 1 # re-order the queue heapq.heapify(__lowerCAmelCase ) return chosen_vertices if __name__ == "__main__": import doctest doctest.testmod() UpperCAmelCase = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} print(F"""Minimum vertex cover:\n{greedy_min_vertex_cover(graph)}""")
195
import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase : Any =UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =self.dummy_uncond_unet UpperCAmelCase : Optional[int] =KarrasVeScheduler() UpperCAmelCase : List[Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : List[str] =torch.manual_seed(0 ) UpperCAmelCase : List[str] =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : str =torch.manual_seed(0 ) UpperCAmelCase : str =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' , return_dict=snake_case__ )[0] UpperCAmelCase : Any =image[0, -3:, -3:, -1] UpperCAmelCase : List[str] =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCAmelCase : int =np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class __snake_case ( unittest.TestCase ): def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Tuple ='''google/ncsnpp-celebahq-256''' UpperCAmelCase : int =UNetaDModel.from_pretrained(snake_case__ ) UpperCAmelCase : Dict =KarrasVeScheduler() UpperCAmelCase : Union[str, Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Any =torch.manual_seed(0 ) UpperCAmelCase : Tuple =pipe(num_inference_steps=20 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : Optional[int] =image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) UpperCAmelCase : Tuple =np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
348
0
'''simple docstring''' from __future__ import annotations def _UpperCamelCase ( UpperCamelCase__ , UpperCamelCase__ = None , UpperCamelCase__ = None ): if start is None: UpperCAmelCase__ : List[str] = 0 if end is None: UpperCAmelCase__ : str = len(__lowerCAmelCase ) - 1 if start >= end: return UpperCAmelCase__ : Any = (start + end) // 2 slowsort(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) slowsort(__lowerCAmelCase , mid + 1 , __lowerCAmelCase ) if sequence[end] < sequence[mid]: UpperCAmelCase__ : int = sequence[mid], sequence[end] slowsort(__lowerCAmelCase , __lowerCAmelCase , end - 1 ) if __name__ == "__main__": from doctest import testmod testmod()
163
import qiskit def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> qiskit.result.counts.Counts: '''simple docstring''' UpperCAmelCase : Union[str, Any] =qiskit.Aer.get_backend('''aer_simulator''' ) UpperCAmelCase : List[str] =qiskit.QuantumCircuit(4 , 2 ) # encode inputs in qubits 0 and 1 if bita == 1: qc_ha.x(0 ) if bita == 1: qc_ha.x(1 ) qc_ha.barrier() # use cnots to write XOR of the inputs on qubit2 qc_ha.cx(0 , 2 ) qc_ha.cx(1 , 2 ) # use ccx / toffoli gate to write AND of the inputs on qubit3 qc_ha.ccx(0 , 1 , 3 ) qc_ha.barrier() # extract outputs qc_ha.measure(2 , 0 ) # extract XOR value qc_ha.measure(3 , 1 ) # extract AND value # Execute the circuit on the qasm simulator UpperCAmelCase : Dict =qiskit.execute(__lowerCAmelCase , __lowerCAmelCase , shots=10_00 ) # Return the histogram data of the results of the experiment return job.result().get_counts(__lowerCAmelCase ) if __name__ == "__main__": __snake_case = half_adder(1, 1) print(f'Half Adder Output Qubit Counts: {counts}')
348
0
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Value from .base import TaskTemplate @dataclass(frozen=lowerCamelCase__ ) class UpperCAmelCase ( lowerCamelCase__ ): '''simple docstring''' snake_case_ = field(default="text-classification" , metadata={"include_in_asdict_even_if_is_default": True} ) snake_case_ = Features({"text": Value("string" )} ) snake_case_ = Features({"labels": ClassLabel} ) snake_case_ = "text" snake_case_ = "labels" def UpperCamelCase_ ( self : Union[str, Any] ,A : List[str] ): if self.label_column not in features: raise ValueError(f'''Column {self.label_column} is not present in features.''' ) if not isinstance(features[self.label_column] ,snake_case__ ): raise ValueError(f'''Column {self.label_column} is not a ClassLabel.''' ) __A = copy.deepcopy(self ) __A = self.label_schema.copy() __A = features[self.label_column] __A = label_schema return task_template @property def UpperCamelCase_ ( self : Union[str, Any] ): return { self.text_column: "text", self.label_column: "labels", }
15
from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class __snake_case : __lowerCamelCase : str = BlenderbotConfig __lowerCamelCase : Optional[Any] = {} __lowerCamelCase : Optional[int] = """gelu""" def __init__( self , snake_case__ , snake_case__=13 , snake_case__=7 , snake_case__=True , snake_case__=False , snake_case__=99 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=20 , snake_case__=2 , snake_case__=1 , snake_case__=0 , ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : Dict =seq_length UpperCAmelCase : Optional[Any] =is_training UpperCAmelCase : List[str] =use_labels UpperCAmelCase : List[Any] =vocab_size UpperCAmelCase : Optional[int] =hidden_size UpperCAmelCase : Tuple =num_hidden_layers UpperCAmelCase : Any =num_attention_heads UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : str =hidden_dropout_prob UpperCAmelCase : Optional[int] =attention_probs_dropout_prob UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : List[Any] =eos_token_id UpperCAmelCase : Optional[int] =pad_token_id UpperCAmelCase : Tuple =bos_token_id def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[Any] =ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) UpperCAmelCase : List[Any] =tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) UpperCAmelCase : Tuple =tf.concat([input_ids, eos_tensor] , axis=1 ) UpperCAmelCase : str =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : Optional[Any] =self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) UpperCAmelCase : List[str] =prepare_blenderbot_inputs_dict(snake_case__ , snake_case__ , snake_case__ ) return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =TFBlenderbotModel(config=snake_case__ ).get_decoder() UpperCAmelCase : Any =inputs_dict['''input_ids'''] UpperCAmelCase : str =input_ids[:1, :] UpperCAmelCase : Tuple =inputs_dict['''attention_mask'''][:1, :] UpperCAmelCase : Tuple =inputs_dict['''head_mask'''] UpperCAmelCase : List[Any] =1 # first forward pass UpperCAmelCase : List[str] =model(snake_case__ , attention_mask=snake_case__ , head_mask=snake_case__ , use_cache=snake_case__ ) UpperCAmelCase , UpperCAmelCase : str =outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase : Union[str, Any] =ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCAmelCase : List[Any] =tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and UpperCAmelCase : Tuple =tf.concat([input_ids, next_tokens] , axis=-1 ) UpperCAmelCase : int =tf.concat([attention_mask, next_attn_mask] , axis=-1 ) UpperCAmelCase : Optional[int] =model(snake_case__ , attention_mask=snake_case__ )[0] UpperCAmelCase : str =model(snake_case__ , attention_mask=snake_case__ , past_key_values=snake_case__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice UpperCAmelCase : List[Any] =int(ids_tensor((1,) , output_from_past.shape[-1] ) ) UpperCAmelCase : List[Any] =output_from_no_past[:, -3:, random_slice_idx] UpperCAmelCase : Dict =output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(snake_case__ , snake_case__ , rtol=1e-3 ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , )-> str: '''simple docstring''' if attention_mask is None: UpperCAmelCase : int =tf.cast(tf.math.not_equal(__lowerCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase : Tuple =tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase : str =tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase : Union[str, Any] =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase : int =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : List[str] = (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __lowerCamelCase : Dict = (TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __lowerCamelCase : Dict = ( { """conversational""": TFBlenderbotForConditionalGeneration, """feature-extraction""": TFBlenderbotModel, """summarization""": TFBlenderbotForConditionalGeneration, """text2text-generation""": TFBlenderbotForConditionalGeneration, """translation""": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __lowerCamelCase : Union[str, Any] = True __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Union[str, Any] = False def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : List[str] =TFBlenderbotModelTester(self ) UpperCAmelCase : List[Any] =ConfigTester(self , config_class=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*snake_case__ ) @require_tokenizers @require_tf class __snake_case ( unittest.TestCase ): __lowerCamelCase : List[str] = ["""My friends are cool but they eat too many carbs."""] __lowerCamelCase : Dict = """facebook/blenderbot-400M-distill""" @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[int] =self.tokenizer(self.src_text , return_tensors='''tf''' ) UpperCAmelCase : Optional[int] =self.model.generate( model_inputs.input_ids , ) UpperCAmelCase : str =self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=snake_case__ )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
348
0
from __future__ import annotations def a( A : Union[str, Any] ) -> float: """simple docstring""" a = 0.00 a = 0 for resistor in resistors: if resistor <= 0: a = f'''Resistor at index {index} has a negative or zero value!''' raise ValueError(__lowerCAmelCase ) first_sum += 1 / float(__lowerCAmelCase ) index += 1 return 1 / first_sum def a( A : Union[str, Any] ) -> float: """simple docstring""" a = 0.00 a = 0 for resistor in resistors: sum_r += resistor if resistor < 0: a = f'''Resistor at index {index} has a negative value!''' raise ValueError(__lowerCAmelCase ) index += 1 return sum_r if __name__ == "__main__": import doctest doctest.testmod()
227
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { '''asapp/sew-d-tiny-100k''': '''https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json''', # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[Any] = """sew-d""" def __init__( self , snake_case__=32 , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=2 , snake_case__=512 , snake_case__=256 , snake_case__=True , snake_case__=True , snake_case__=("p2c", "c2p") , snake_case__="layer_norm" , snake_case__="gelu_python" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.0 , snake_case__=0.1 , snake_case__=0.02 , snake_case__=1e-7 , snake_case__=1e-5 , snake_case__="group" , snake_case__="gelu" , snake_case__=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , snake_case__=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , snake_case__=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , snake_case__=False , snake_case__=128 , snake_case__=16 , snake_case__=True , snake_case__=0.05 , snake_case__=10 , snake_case__=2 , snake_case__=0.0 , snake_case__=10 , snake_case__=0 , snake_case__="mean" , snake_case__=False , snake_case__=False , snake_case__=256 , snake_case__=0 , snake_case__=1 , snake_case__=2 , **snake_case__ , ) -> int: '''simple docstring''' super().__init__(**snake_case__ , pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ ) UpperCAmelCase : Union[str, Any] =hidden_size UpperCAmelCase : Union[str, Any] =feat_extract_norm UpperCAmelCase : Optional[Any] =feat_extract_activation UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : int =list(snake_case__ ) UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : str =conv_bias UpperCAmelCase : Tuple =num_conv_pos_embeddings UpperCAmelCase : Dict =num_conv_pos_embedding_groups UpperCAmelCase : str =len(self.conv_dim ) UpperCAmelCase : Dict =num_hidden_layers UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : List[Any] =squeeze_factor UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : int =position_buckets UpperCAmelCase : Optional[int] =share_att_key UpperCAmelCase : Optional[int] =relative_attention UpperCAmelCase : Tuple =norm_rel_ebd UpperCAmelCase : List[Any] =list(snake_case__ ) UpperCAmelCase : Dict =hidden_act UpperCAmelCase : Optional[int] =num_attention_heads UpperCAmelCase : Any =hidden_dropout UpperCAmelCase : str =attention_dropout UpperCAmelCase : Union[str, Any] =activation_dropout UpperCAmelCase : str =feat_proj_dropout UpperCAmelCase : Union[str, Any] =final_dropout UpperCAmelCase : Optional[int] =layer_norm_eps UpperCAmelCase : str =feature_layer_norm_eps UpperCAmelCase : str =initializer_range UpperCAmelCase : Any =vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect.''' '''It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,''' f'''but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)''' f'''= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCAmelCase : Union[str, Any] =apply_spec_augment UpperCAmelCase : Optional[Any] =mask_time_prob UpperCAmelCase : Tuple =mask_time_length UpperCAmelCase : str =mask_time_min_masks UpperCAmelCase : Optional[int] =mask_feature_prob UpperCAmelCase : Optional[Any] =mask_feature_length UpperCAmelCase : List[Any] =mask_feature_min_masks # ctc loss UpperCAmelCase : str =ctc_loss_reduction UpperCAmelCase : Optional[int] =ctc_zero_infinity # sequence classification UpperCAmelCase : Union[str, Any] =use_weighted_layer_sum UpperCAmelCase : int =classifier_proj_size @property def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
348
0
import gc import unittest from transformers import CTRLConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, ) class __lowerCAmelCase : def __init__( self , lowerCAmelCase , lowerCAmelCase=14 , lowerCAmelCase=7 , lowerCAmelCase=True , lowerCAmelCase=True , lowerCAmelCase=True , lowerCAmelCase=True , lowerCAmelCase=True , lowerCAmelCase=99 , lowerCAmelCase=32 , lowerCAmelCase=5 , lowerCAmelCase=4 , lowerCAmelCase=37 , lowerCAmelCase="gelu" , lowerCAmelCase=0.1 , lowerCAmelCase=0.1 , lowerCAmelCase=512 , lowerCAmelCase=16 , lowerCAmelCase=2 , lowerCAmelCase=0.02 , lowerCAmelCase=3 , lowerCAmelCase=4 , lowerCAmelCase=None , ) -> int: '''simple docstring''' _lowercase =parent _lowercase =batch_size _lowercase =seq_length _lowercase =is_training _lowercase =use_token_type_ids _lowercase =use_input_mask _lowercase =use_labels _lowercase =use_mc_token_ids _lowercase =vocab_size _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =intermediate_size _lowercase =hidden_act _lowercase =hidden_dropout_prob _lowercase =attention_probs_dropout_prob _lowercase =max_position_embeddings _lowercase =type_vocab_size _lowercase =type_sequence_label_size _lowercase =initializer_range _lowercase =num_labels _lowercase =num_choices _lowercase =scope _lowercase =self.vocab_size - 1 def A__ ( self ) -> int: '''simple docstring''' _lowercase =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowercase =None if self.use_input_mask: _lowercase =random_attention_mask([self.batch_size, self.seq_length] ) _lowercase =None if self.use_token_type_ids: _lowercase =ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _lowercase =None if self.use_mc_token_ids: _lowercase =ids_tensor([self.batch_size, self.num_choices] , self.seq_length ) _lowercase =None _lowercase =None _lowercase =None if self.use_labels: _lowercase =ids_tensor([self.batch_size] , self.type_sequence_label_size ) _lowercase =ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _lowercase =ids_tensor([self.batch_size] , self.num_choices ) _lowercase =self.get_config() _lowercase =ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, token_type_ids, mc_token_ids, sequence_labels, token_labels, choice_labels, ) def A__ ( self ) -> Dict: '''simple docstring''' return CTRLConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) def A__ ( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , *lowerCAmelCase ) -> str: '''simple docstring''' _lowercase =CTRLModel(config=snake_case__ ) model.to(snake_case__ ) model.eval() model(snake_case__ , token_type_ids=snake_case__ , head_mask=snake_case__ ) model(snake_case__ , token_type_ids=snake_case__ ) _lowercase =model(snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(len(result.past_key_values ) , config.n_layer ) def A__ ( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , *lowerCAmelCase ) -> Optional[int]: '''simple docstring''' _lowercase =CTRLLMHeadModel(snake_case__ ) model.to(snake_case__ ) model.eval() _lowercase =model(snake_case__ , token_type_ids=snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def A__ ( self ) -> Optional[Any]: '''simple docstring''' _lowercase =self.prepare_config_and_inputs() ( _lowercase ) =config_and_inputs _lowercase ={'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask} return config, inputs_dict def A__ ( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , *lowerCAmelCase ) -> Tuple: '''simple docstring''' _lowercase =self.num_labels _lowercase =CTRLForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() _lowercase =ids_tensor([self.batch_size] , self.type_sequence_label_size ) _lowercase =model(snake_case__ , token_type_ids=snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) @require_torch class __lowerCAmelCase ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): _a = (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else () _a = (CTRLLMHeadModel,) if is_torch_available() else () _a = ( { """feature-extraction""": CTRLModel, """text-classification""": CTRLForSequenceClassification, """text-generation""": CTRLLMHeadModel, """zero-shot""": CTRLForSequenceClassification, } if is_torch_available() else {} ) _a = True _a = False _a = False def A__ ( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) -> Any: '''simple docstring''' if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny # config could not be created. return True return False def A__ ( self ) -> List[Any]: '''simple docstring''' _lowercase =CTRLModelTester(self ) _lowercase =ConfigTester(self , config_class=snake_case__ , n_embd=37 ) def A__ ( self ) -> int: '''simple docstring''' super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() def A__ ( self ) -> int: '''simple docstring''' self.config_tester.run_common_tests() def A__ ( self ) -> Union[str, Any]: '''simple docstring''' _lowercase =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_ctrl_model(*snake_case__ ) def A__ ( self ) -> Dict: '''simple docstring''' _lowercase =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*snake_case__ ) @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def A__ ( self ) -> Tuple: '''simple docstring''' pass @slow def A__ ( self ) -> Tuple: '''simple docstring''' for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowercase =CTRLModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) @unittest.skip('The model doesn\'t support left padding' ) # and it's not used enough to be worth fixing :) def A__ ( self ) -> int: '''simple docstring''' pass @require_torch class __lowerCAmelCase ( unittest.TestCase ): def A__ ( self ) -> Tuple: '''simple docstring''' super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() @slow def A__ ( self ) -> Tuple: '''simple docstring''' _lowercase =CTRLLMHeadModel.from_pretrained('ctrl' ) model.to(snake_case__ ) _lowercase =torch.tensor( [[11_859, 0, 1_611, 8]] , dtype=torch.long , device=snake_case__ ) # Legal the president is _lowercase =[ 11_859, 0, 1_611, 8, 5, 150, 26_449, 2, 19, 348, 469, 3, 2_595, 48, 20_740, 246_533, 246_533, 19, 30, 5, ] # Legal the president is a good guy and I don't want to lose my job. \n \n I have a _lowercase =model.generate(snake_case__ , do_sample=snake_case__ ) self.assertListEqual(output_ids[0].tolist() , snake_case__ )
205
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node __snake_case = 4 __snake_case = 3 class __snake_case ( lowerCamelCase__ ): pass def lowerCAmelCase_ ( __lowerCAmelCase )-> List[str]: '''simple docstring''' for shard in shards: for i in range(__lowerCAmelCase ): yield {"i": i, "shard": shard} def lowerCAmelCase_ ( )-> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =int(os.environ['''RANK'''] ) UpperCAmelCase : Optional[Any] =int(os.environ['''WORLD_SIZE'''] ) UpperCAmelCase : List[Any] =ArgumentParser() parser.add_argument('''--streaming''' , type=__lowerCAmelCase ) parser.add_argument('''--local_rank''' , type=__lowerCAmelCase ) parser.add_argument('''--num_workers''' , type=__lowerCAmelCase , default=0 ) UpperCAmelCase : Any =parser.parse_args() UpperCAmelCase : List[str] =args.streaming UpperCAmelCase : Tuple =args.num_workers UpperCAmelCase : int ={'''shards''': [f'''shard_{shard_idx}''' for shard_idx in range(__lowerCAmelCase )]} UpperCAmelCase : Optional[int] =IterableDataset.from_generator(__lowerCAmelCase , gen_kwargs=__lowerCAmelCase ) if not streaming: UpperCAmelCase : List[Any] =Dataset.from_list(list(__lowerCAmelCase ) ) UpperCAmelCase : Dict =split_dataset_by_node(__lowerCAmelCase , rank=__lowerCAmelCase , world_size=__lowerCAmelCase ) UpperCAmelCase : List[Any] =torch.utils.data.DataLoader(__lowerCAmelCase , num_workers=__lowerCAmelCase ) UpperCAmelCase : Dict =NUM_SHARDS * NUM_ITEMS_PER_SHARD UpperCAmelCase : str =full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) UpperCAmelCase : List[Any] =sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f'''local_size {local_size} != expected_local_size {expected_local_size}''' ) if __name__ == "__main__": main()
348
0
"""simple docstring""" import os import tempfile import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from torch import nn from transformers import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_inverse_sqrt_schedule, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) def _lowercase ( __lowerCAmelCase , __lowerCAmelCase=10 ) -> List[Any]: SCREAMING_SNAKE_CASE__ : Optional[int] = [] for _ in range(__lowerCAmelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() return lrs def _lowercase ( __lowerCAmelCase , __lowerCAmelCase=10 ) -> Tuple: SCREAMING_SNAKE_CASE__ : Union[str, Any] = [] for step in range(__lowerCAmelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() if step == num_steps // 2: with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE__ : List[str] = os.path.join(__lowerCAmelCase , """schedule.bin""" ) torch.save(scheduler.state_dict() , __lowerCAmelCase ) SCREAMING_SNAKE_CASE__ : Dict = torch.load(__lowerCAmelCase ) scheduler.load_state_dict(__lowerCAmelCase ) return lrs @require_torch class __a (unittest.TestCase): '''simple docstring''' def _a ( self , _a , _a , _a ) -> str: """simple docstring""" self.assertEqual(len(snake_case__ ) , len(snake_case__ ) ) for a, b in zip(snake_case__ , snake_case__ ): self.assertAlmostEqual(snake_case__ , snake_case__ , delta=snake_case__ ) def _a ( self ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[int] = torch.tensor([0.1, -0.2, -0.1] , requires_grad=snake_case__ ) SCREAMING_SNAKE_CASE__ : Tuple = torch.tensor([0.4, 0.2, -0.5] ) SCREAMING_SNAKE_CASE__ : str = nn.MSELoss() # No warmup, constant schedule, no gradient clipping SCREAMING_SNAKE_CASE__ : Union[str, Any] = AdamW(params=[w] , lr=2E-1 , weight_decay=0.0 ) for _ in range(100 ): SCREAMING_SNAKE_CASE__ : str = criterion(snake_case__ , snake_case__ ) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1E-2 ) def _a ( self ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ : Any = torch.tensor([0.1, -0.2, -0.1] , requires_grad=snake_case__ ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = torch.tensor([0.4, 0.2, -0.5] ) SCREAMING_SNAKE_CASE__ : Dict = nn.MSELoss() # No warmup, constant schedule, no gradient clipping SCREAMING_SNAKE_CASE__ : Union[str, Any] = Adafactor( params=[w] , lr=1E-2 , eps=(1E-3_0, 1E-3) , clip_threshold=1.0 , decay_rate=-0.8 , betaa=snake_case__ , weight_decay=0.0 , relative_step=snake_case__ , scale_parameter=snake_case__ , warmup_init=snake_case__ , ) for _ in range(1_000 ): SCREAMING_SNAKE_CASE__ : Dict = criterion(snake_case__ , snake_case__ ) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1E-2 ) @require_torch class __a (unittest.TestCase): '''simple docstring''' _SCREAMING_SNAKE_CASE :int = nn.Linear(50 , 50) if is_torch_available() else None _SCREAMING_SNAKE_CASE :int = AdamW(m.parameters() , lr=10.0) if is_torch_available() else None _SCREAMING_SNAKE_CASE :Any = 10 def _a ( self , _a , _a , _a , _a=None ) -> List[Any]: """simple docstring""" self.assertEqual(len(snake_case__ ) , len(snake_case__ ) ) for a, b in zip(snake_case__ , snake_case__ ): self.assertAlmostEqual(snake_case__ , snake_case__ , delta=snake_case__ , msg=snake_case__ ) def _a ( self ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Tuple = {'''num_warmup_steps''': 2, '''num_training_steps''': 10} # schedulers doct format # function: (sched_args_dict, expected_learning_rates) SCREAMING_SNAKE_CASE__ : Union[str, Any] = { get_constant_schedule: ({}, [10.0] * self.num_steps), get_constant_schedule_with_warmup: ( {'''num_warmup_steps''': 4}, [0.0, 2.5, 5.0, 7.5, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0], ), get_linear_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25], ), get_cosine_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 9.61, 8.53, 6.91, 5.0, 3.08, 1.46, 0.38], ), get_cosine_with_hard_restarts_schedule_with_warmup: ( {**common_kwargs, '''num_cycles''': 2}, [0.0, 5.0, 10.0, 8.53, 5.0, 1.46, 10.0, 8.53, 5.0, 1.46], ), get_polynomial_decay_schedule_with_warmup: ( {**common_kwargs, '''power''': 2.0, '''lr_end''': 1E-7}, [0.0, 5.0, 10.0, 7.656, 5.625, 3.906, 2.5, 1.406, 0.625, 0.156], ), get_inverse_sqrt_schedule: ( {'''num_warmup_steps''': 2}, [0.0, 5.0, 10.0, 8.165, 7.071, 6.325, 5.774, 5.345, 5.0, 4.714], ), } for scheduler_func, data in scheds.items(): SCREAMING_SNAKE_CASE__ : List[Any] = data SCREAMING_SNAKE_CASE__ : List[str] = scheduler_func(self.optimizer , **snake_case__ ) self.assertEqual(len([scheduler.get_lr()[0]] ) , 1 ) SCREAMING_SNAKE_CASE__ : Optional[Any] = unwrap_schedule(snake_case__ , self.num_steps ) self.assertListAlmostEqual( snake_case__ , snake_case__ , tol=1E-2 , msg=f'''failed for {scheduler_func} in normal scheduler''' , ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = scheduler_func(self.optimizer , **snake_case__ ) if scheduler_func.__name__ != "get_constant_schedule": LambdaScheduleWrapper.wrap_scheduler(snake_case__ ) # wrap to test picklability of the schedule SCREAMING_SNAKE_CASE__ : Optional[Any] = unwrap_and_save_reload_schedule(snake_case__ , self.num_steps ) self.assertListEqual(snake_case__ , snake_case__ , msg=f'''failed for {scheduler_func} in save and reload''' ) class __a : '''simple docstring''' def __init__( self , _a ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : int = fn def __call__( self , *_a , **_a ) -> Optional[Any]: """simple docstring""" return self.fn(*snake_case__ , **snake_case__ ) @classmethod def _a ( self , _a ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ : str = list(map(self , scheduler.lr_lambdas ) )
132
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __snake_case = {'''configuration_opt''': ['''OPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OPTConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''OPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OPTForCausalLM''', '''OPTModel''', '''OPTPreTrainedModel''', '''OPTForSequenceClassification''', '''OPTForQuestionAnswering''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''TFOPTForCausalLM''', '''TFOPTModel''', '''TFOPTPreTrainedModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''FlaxOPTForCausalLM''', '''FlaxOPTModel''', '''FlaxOPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
0
'''simple docstring''' import itertools import json import linecache import os import pickle import re import socket import string from collections import Counter from logging import getLogger from pathlib import Path from typing import Callable, Dict, Iterable, List import git import torch from torch.utils.data import Dataset from transformers import BartTokenizer, RagTokenizer, TaTokenizer def lowercase_ ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any]=True , lowerCAmelCase__ : List[Any]="pt" ): """simple docstring""" __UpperCAmelCase : Any = {'''add_prefix_space''': True} if isinstance(__lowerCAmelCase , __lowerCAmelCase ) and not line.startswith(""" """ ) else {} __UpperCAmelCase : Optional[int] = padding_side return tokenizer( [line] , max_length=__lowerCAmelCase , padding="""max_length""" if pad_to_max_length else None , truncation=__lowerCAmelCase , return_tensors=__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , **__lowerCAmelCase , ) def lowercase_ ( lowerCAmelCase__ : Dict , lowerCAmelCase__ : str , lowerCAmelCase__ : Any=None , ): """simple docstring""" __UpperCAmelCase : Tuple = input_ids.ne(__lowerCAmelCase ).any(dim=0 ) if attention_mask is None: return input_ids[:, keep_column_mask] else: return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask]) class _A ( lowerCamelCase__ ): def __init__( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase="train" , __UpperCAmelCase=None , __UpperCAmelCase=None , __UpperCAmelCase=None , __UpperCAmelCase="" , ) -> int: '''simple docstring''' super().__init__() __UpperCAmelCase : Dict = Path(snake_case__ ).joinpath(type_path + """.source""" ) __UpperCAmelCase : int = Path(snake_case__ ).joinpath(type_path + """.target""" ) __UpperCAmelCase : Dict = self.get_char_lens(self.src_file ) __UpperCAmelCase : Any = max_source_length __UpperCAmelCase : Dict = max_target_length assert min(self.src_lens ) > 0, f'found empty line in {self.src_file}' __UpperCAmelCase : List[str] = tokenizer __UpperCAmelCase : List[Any] = prefix if n_obs is not None: __UpperCAmelCase : Union[str, Any] = self.src_lens[:n_obs] __UpperCAmelCase : List[Any] = src_lang __UpperCAmelCase : Union[str, Any] = tgt_lang def __len__( self ) -> Dict: '''simple docstring''' return len(self.src_lens ) def __getitem__( self , __UpperCAmelCase ) -> Dict[str, torch.Tensor]: '''simple docstring''' __UpperCAmelCase : int = index + 1 # linecache starts at 1 __UpperCAmelCase : Union[str, Any] = self.prefix + linecache.getline(str(self.src_file ) , snake_case__ ).rstrip("""\n""" ) __UpperCAmelCase : List[Any] = linecache.getline(str(self.tgt_file ) , snake_case__ ).rstrip("""\n""" ) assert source_line, f'empty source line for index {index}' assert tgt_line, f'empty tgt line for index {index}' # Need to add eos token manually for T5 if isinstance(self.tokenizer , snake_case__ ): source_line += self.tokenizer.eos_token tgt_line += self.tokenizer.eos_token # Pad source and target to the right __UpperCAmelCase : Dict = ( self.tokenizer.question_encoder if isinstance(self.tokenizer , snake_case__ ) else self.tokenizer ) __UpperCAmelCase : str = self.tokenizer.generator if isinstance(self.tokenizer , snake_case__ ) else self.tokenizer __UpperCAmelCase : Union[str, Any] = encode_line(snake_case__ , snake_case__ , self.max_source_length , """right""" ) __UpperCAmelCase : Any = encode_line(snake_case__ , snake_case__ , self.max_target_length , """right""" ) __UpperCAmelCase : Union[str, Any] = source_inputs['''input_ids'''].squeeze() __UpperCAmelCase : str = target_inputs['''input_ids'''].squeeze() __UpperCAmelCase : Dict = source_inputs['''attention_mask'''].squeeze() return { "input_ids": source_ids, "attention_mask": src_mask, "decoder_input_ids": target_ids, } @staticmethod def __A ( __UpperCAmelCase ) -> Tuple: '''simple docstring''' return [len(snake_case__ ) for x in Path(snake_case__ ).open().readlines()] def __A ( self , __UpperCAmelCase ) -> Dict[str, torch.Tensor]: '''simple docstring''' __UpperCAmelCase : int = torch.stack([x["""input_ids"""] for x in batch] ) __UpperCAmelCase : Tuple = torch.stack([x["""attention_mask"""] for x in batch] ) __UpperCAmelCase : List[Any] = torch.stack([x["""decoder_input_ids"""] for x in batch] ) __UpperCAmelCase : Union[str, Any] = ( self.tokenizer.generator.pad_token_id if isinstance(self.tokenizer , snake_case__ ) else self.tokenizer.pad_token_id ) __UpperCAmelCase : Union[str, Any] = ( self.tokenizer.question_encoder.pad_token_id if isinstance(self.tokenizer , snake_case__ ) else self.tokenizer.pad_token_id ) __UpperCAmelCase : Tuple = trim_batch(snake_case__ , snake_case__ ) __UpperCAmelCase : Any = trim_batch(snake_case__ , snake_case__ , attention_mask=snake_case__ ) __UpperCAmelCase : Optional[Any] = { '''input_ids''': source_ids, '''attention_mask''': source_mask, '''decoder_input_ids''': y, } return batch _UpperCamelCase = getLogger(__name__) def lowercase_ ( lowerCAmelCase__ : List[Any] ): """simple docstring""" return list(itertools.chain.from_iterable(__lowerCAmelCase ) ) def lowercase_ ( lowerCAmelCase__ : List[Any] ): """simple docstring""" __UpperCAmelCase : str = get_git_info() save_json(__lowerCAmelCase , os.path.join(__lowerCAmelCase , """git_log.json""" ) ) def lowercase_ ( lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Union[str, Any]=4 , **lowerCAmelCase__ : Dict ): """simple docstring""" with open(__lowerCAmelCase , """w""" ) as f: json.dump(__lowerCAmelCase , __lowerCAmelCase , indent=__lowerCAmelCase , **__lowerCAmelCase ) def lowercase_ ( lowerCAmelCase__ : List[str] ): """simple docstring""" with open(__lowerCAmelCase ) as f: return json.load(__lowerCAmelCase ) def lowercase_ ( ): """simple docstring""" __UpperCAmelCase : List[str] = git.Repo(search_parent_directories=__lowerCAmelCase ) __UpperCAmelCase : Union[str, Any] = { '''repo_id''': str(__lowerCAmelCase ), '''repo_sha''': str(repo.head.object.hexsha ), '''repo_branch''': str(repo.active_branch ), '''hostname''': str(socket.gethostname() ), } return repo_infos def lowercase_ ( lowerCAmelCase__ : int , lowerCAmelCase__ : str ): """simple docstring""" return list(map(__lowerCAmelCase , __lowerCAmelCase ) ) def lowercase_ ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : int ): """simple docstring""" with open(__lowerCAmelCase , """wb""" ) as f: return pickle.dump(__lowerCAmelCase , __lowerCAmelCase ) def lowercase_ ( lowerCAmelCase__ : Optional[int] ): """simple docstring""" def remove_articles(lowerCAmelCase__ : str ): return re.sub(r"""\b(a|an|the)\b""" , """ """ , __lowerCAmelCase ) def white_space_fix(lowerCAmelCase__ : str ): return " ".join(text.split() ) def remove_punc(lowerCAmelCase__ : List[Any] ): __UpperCAmelCase : List[str] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(lowerCAmelCase__ : Optional[int] ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__lowerCAmelCase ) ) ) ) def lowercase_ ( lowerCAmelCase__ : str , lowerCAmelCase__ : Dict ): """simple docstring""" __UpperCAmelCase : int = normalize_answer(__lowerCAmelCase ).split() __UpperCAmelCase : Union[str, Any] = normalize_answer(__lowerCAmelCase ).split() __UpperCAmelCase : Tuple = Counter(__lowerCAmelCase ) & Counter(__lowerCAmelCase ) __UpperCAmelCase : Union[str, Any] = sum(common.values() ) if num_same == 0: return 0 __UpperCAmelCase : int = 1.0 * num_same / len(__lowerCAmelCase ) __UpperCAmelCase : Dict = 1.0 * num_same / len(__lowerCAmelCase ) __UpperCAmelCase : List[str] = (2 * precision * recall) / (precision + recall) return fa def lowercase_ ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[Any] ): """simple docstring""" return normalize_answer(__lowerCAmelCase ) == normalize_answer(__lowerCAmelCase ) def lowercase_ ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[int] ): """simple docstring""" assert len(__lowerCAmelCase ) == len(__lowerCAmelCase ) __UpperCAmelCase : Union[str, Any] = 0 for hypo, pred in zip(__lowerCAmelCase , __lowerCAmelCase ): em += exact_match_score(__lowerCAmelCase , __lowerCAmelCase ) if len(__lowerCAmelCase ) > 0: em /= len(__lowerCAmelCase ) return {"em": em} def lowercase_ ( lowerCAmelCase__ : Dict ): """simple docstring""" return model_prefix.startswith("""rag""" ) def lowercase_ ( lowerCAmelCase__ : int , lowerCAmelCase__ : str , lowerCAmelCase__ : Union[str, Any] ): """simple docstring""" __UpperCAmelCase : List[Any] = {p: p for p in extra_params} # T5 models don't have `dropout` param, they have `dropout_rate` instead __UpperCAmelCase : List[str] = '''dropout_rate''' for p in extra_params: if getattr(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ): if not hasattr(__lowerCAmelCase , __lowerCAmelCase ) and not hasattr(__lowerCAmelCase , equivalent_param[p] ): logger.info("""config doesn\'t have a `{}` attribute""".format(__lowerCAmelCase ) ) delattr(__lowerCAmelCase , __lowerCAmelCase ) continue __UpperCAmelCase : Union[str, Any] = p if hasattr(__lowerCAmelCase , __lowerCAmelCase ) else equivalent_param[p] setattr(__lowerCAmelCase , __lowerCAmelCase , getattr(__lowerCAmelCase , __lowerCAmelCase ) ) delattr(__lowerCAmelCase , __lowerCAmelCase ) return hparams, config
254
import tempfile import unittest import numpy as np import transformers from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax import jax.numpy as jnp from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel if is_torch_available(): import torch class __snake_case : def __init__( self , snake_case__ , snake_case__=14 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=False , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=4 , snake_case__=4 , snake_case__=4 , snake_case__=37 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , ) -> str: '''simple docstring''' UpperCAmelCase : str =parent UpperCAmelCase : Tuple =batch_size UpperCAmelCase : Optional[int] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Tuple =use_input_mask UpperCAmelCase : List[Any] =use_token_type_ids UpperCAmelCase : Optional[Any] =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : List[Any] =hidden_size UpperCAmelCase : Optional[int] =rotary_dim UpperCAmelCase : Union[str, Any] =num_hidden_layers UpperCAmelCase : List[Any] =num_attention_heads UpperCAmelCase : Dict =intermediate_size UpperCAmelCase : Union[str, Any] =hidden_act UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : Dict =attention_probs_dropout_prob UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : str =initializer_range UpperCAmelCase : Optional[int] =None UpperCAmelCase : List[Any] =vocab_size - 1 UpperCAmelCase : Optional[Any] =vocab_size - 1 UpperCAmelCase : List[Any] =vocab_size - 1 def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : List[Any] =None if self.use_input_mask: UpperCAmelCase : Optional[Any] =random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : Dict =GPTJConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , use_cache=snake_case__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , rotary_dim=self.rotary_dim , ) return (config, input_ids, input_mask) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Union[str, Any] =config_and_inputs UpperCAmelCase : Tuple ={'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =20 UpperCAmelCase : Any =model_class_name(snake_case__ ) UpperCAmelCase : str =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : Any =jnp.ones((input_ids.shape[0], max_decoder_length) , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : List[str] =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =model( input_ids[:, -1:] , attention_mask=snake_case__ , past_key_values=outputs_cache.past_key_values , position_ids=snake_case__ , ) UpperCAmelCase : List[Any] =model(snake_case__ ) UpperCAmelCase : Any =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Dict =20 UpperCAmelCase : Dict =model_class_name(snake_case__ ) UpperCAmelCase : Tuple =jnp.concatenate( [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]) )] , axis=-1 , ) UpperCAmelCase : Dict =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : int =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : str =model( input_ids[:, -1:] , past_key_values=outputs_cache.past_key_values , attention_mask=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =model(snake_case__ , attention_mask=snake_case__ ) UpperCAmelCase : Dict =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) @require_flax class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Tuple = (FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else () __lowerCamelCase : Optional[Any] = (FlaxGPTJForCausalLM,) if is_flax_available() else () def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =FlaxGPTJModelTester(self ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Dict =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward_with_attn_mask( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) @tooslow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =GPTaTokenizer.from_pretrained('''gpt2''' , pad_token='''<|endoftext|>''' , padding_side='''left''' ) UpperCAmelCase : Optional[Any] =tokenizer(['''Hello this is a long string''', '''Hey'''] , return_tensors='''np''' , padding=snake_case__ , truncation=snake_case__ ) UpperCAmelCase : Optional[int] =FlaxGPTJForCausalLM.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : str =False UpperCAmelCase : Union[str, Any] =model.config.eos_token_id UpperCAmelCase : List[Any] =jax.jit(model.generate ) UpperCAmelCase : Dict =jit_generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , pad_token_id=tokenizer.pad_token_id ).sequences UpperCAmelCase : Any =tokenizer.batch_decode(snake_case__ , skip_special_tokens=snake_case__ ) UpperCAmelCase : Tuple =[ '''Hello this is a long string of text.\n\nI\'m trying to get the text of the''', '''Hey, I\'m a little late to the party. I\'m going to''', ] self.assertListEqual(snake_case__ , snake_case__ ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : List[str] =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : Any =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : Any =getattr(snake_case__ , snake_case__ ) UpperCAmelCase , UpperCAmelCase : Union[str, Any] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Tuple =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : int =0 UpperCAmelCase : Optional[int] =1 UpperCAmelCase : Optional[int] =0 UpperCAmelCase : Union[str, Any] =1 UpperCAmelCase : List[str] =pt_model_class(snake_case__ ).eval() UpperCAmelCase : Optional[int] =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Any =convert_pytorch_state_dict_to_flax(pt_model.state_dict() , snake_case__ ) UpperCAmelCase : Union[str, Any] =fx_state with torch.no_grad(): UpperCAmelCase : Any =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : Dict =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(snake_case__ ) UpperCAmelCase : str =model_class.from_pretrained(snake_case__ , from_pt=snake_case__ ) UpperCAmelCase : int =fx_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output_loaded, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output_loaded[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : int =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : int =getattr(snake_case__ , snake_case__ ) UpperCAmelCase : Dict =pt_model_class(snake_case__ ).eval() UpperCAmelCase : str =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Optional[Any] =load_flax_weights_in_pytorch_model(snake_case__ , fx_model.params ) UpperCAmelCase , UpperCAmelCase : Optional[int] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Optional[int] =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : str =0 UpperCAmelCase : Any =1 UpperCAmelCase : List[Any] =0 UpperCAmelCase : Tuple =1 # make sure weights are tied in PyTorch pt_model.tie_weights() with torch.no_grad(): UpperCAmelCase : Optional[Any] =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : List[Any] =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(snake_case__ ) UpperCAmelCase : Tuple =pt_model_class.from_pretrained(snake_case__ , from_flax=snake_case__ ) with torch.no_grad(): UpperCAmelCase : Any =pt_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @tooslow def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase : str =model_class_name.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : Tuple =model(np.ones((1, 1) ) ) self.assertIsNotNone(snake_case__ )
348
0
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" UpperCAmelCase__ = tempfile.mkdtemp() # fmt: off UpperCAmelCase__ = ['''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''lo''', '''l</w>''', '''w</w>''', '''r</w>''', '''t</w>''', '''low</w>''', '''er</w>''', '''lowest</w>''', '''newer</w>''', '''wider''', '''<unk>''', '''<|startoftext|>''', '''<|endoftext|>'''] # fmt: on UpperCAmelCase__ = dict(zip(snake_case__ , range(len(snake_case__ ) ) ) ) UpperCAmelCase__ = ['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''', ''''''] UpperCAmelCase__ = {'''unk_token''': '''<unk>'''} UpperCAmelCase__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) UpperCAmelCase__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(snake_case__ ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(snake_case__ ) ) UpperCAmelCase__ = { '''do_resize''': True, '''size''': 20, '''do_center_crop''': True, '''crop_size''': 18, '''do_normalize''': True, '''image_mean''': [0.4814_5466, 0.457_8275, 0.4082_1073], '''image_std''': [0.2686_2954, 0.2613_0258, 0.2757_7711], } UpperCAmelCase__ = os.path.join(self.tmpdirname , snake_case__ ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(snake_case__ , snake_case__ ) def SCREAMING_SNAKE_CASE__ ( self : List[str] , **_UpperCAmelCase : List[str] ): """simple docstring""" return CLIPTokenizer.from_pretrained(self.tmpdirname , **snake_case__ ) def SCREAMING_SNAKE_CASE__ ( self : List[str] , **_UpperCAmelCase : str ): """simple docstring""" return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **snake_case__ ) def SCREAMING_SNAKE_CASE__ ( self : str , **_UpperCAmelCase : Tuple ): """simple docstring""" return CLIPImageProcessor.from_pretrained(self.tmpdirname , **snake_case__ ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" UpperCAmelCase__ = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] UpperCAmelCase__ = [Image.fromarray(np.moveaxis(snake_case__ , 0 , -1 ) ) for x in image_inputs] return image_inputs def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" UpperCAmelCase__ = self.get_tokenizer() UpperCAmelCase__ = self.get_rust_tokenizer() UpperCAmelCase__ = self.get_image_processor() UpperCAmelCase__ = CLIPProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) processor_slow.save_pretrained(self.tmpdirname ) UpperCAmelCase__ = CLIPProcessor.from_pretrained(self.tmpdirname , use_fast=snake_case__ ) UpperCAmelCase__ = CLIPProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) processor_fast.save_pretrained(self.tmpdirname ) UpperCAmelCase__ = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , snake_case__ ) self.assertIsInstance(processor_fast.tokenizer , snake_case__ ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , snake_case__ ) self.assertIsInstance(processor_fast.image_processor , snake_case__ ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" UpperCAmelCase__ = CLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) UpperCAmelCase__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) UpperCAmelCase__ = self.get_image_processor(do_normalize=snake_case__ , padding_value=1.0 ) UpperCAmelCase__ = CLIPProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=snake_case__ , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , snake_case__ ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , snake_case__ ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" UpperCAmelCase__ = self.get_image_processor() UpperCAmelCase__ = self.get_tokenizer() UpperCAmelCase__ = CLIPProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) UpperCAmelCase__ = self.prepare_image_inputs() UpperCAmelCase__ = image_processor(snake_case__ , return_tensors="""np""" ) UpperCAmelCase__ = processor(images=snake_case__ , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1E-2 ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" UpperCAmelCase__ = self.get_image_processor() UpperCAmelCase__ = self.get_tokenizer() UpperCAmelCase__ = CLIPProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) UpperCAmelCase__ = '''lower newer''' UpperCAmelCase__ = processor(text=snake_case__ ) UpperCAmelCase__ = tokenizer(snake_case__ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" UpperCAmelCase__ = self.get_image_processor() UpperCAmelCase__ = self.get_tokenizer() UpperCAmelCase__ = CLIPProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) UpperCAmelCase__ = '''lower newer''' UpperCAmelCase__ = self.prepare_image_inputs() UpperCAmelCase__ = processor(text=snake_case__ , images=snake_case__ ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(snake_case__ ): processor() def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" UpperCAmelCase__ = self.get_image_processor() UpperCAmelCase__ = self.get_tokenizer() UpperCAmelCase__ = CLIPProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) UpperCAmelCase__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] UpperCAmelCase__ = processor.batch_decode(snake_case__ ) UpperCAmelCase__ = tokenizer.batch_decode(snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" UpperCAmelCase__ = self.get_image_processor() UpperCAmelCase__ = self.get_tokenizer() UpperCAmelCase__ = CLIPProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) UpperCAmelCase__ = '''lower newer''' UpperCAmelCase__ = self.prepare_image_inputs() UpperCAmelCase__ = processor(text=snake_case__ , images=snake_case__ ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
346
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __snake_case = { '''configuration_bloom''': ['''BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BloomConfig''', '''BloomOnnxConfig'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''BloomTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BloomForCausalLM''', '''BloomModel''', '''BloomPreTrainedModel''', '''BloomForSequenceClassification''', '''BloomForTokenClassification''', '''BloomForQuestionAnswering''', ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
0
from datasets.utils.patching import _PatchedModuleObj, patch_submodule from . import _test_patching def UpperCamelCase ( ): """simple docstring""" import os as original_os from os import path as original_path from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join __magic_name__ : Any = '''__test_patch_submodule_mock__''' with patch_submodule(_test_patching, """os.path.join""", __lowerCAmelCase ): # Every way to access os.path.join must be patched, and the rest must stay untouched # check os.path.join assert isinstance(_test_patching.os, _PatchedModuleObj ) assert isinstance(_test_patching.os.path, _PatchedModuleObj ) assert _test_patching.os.path.join is mock # check path.join assert isinstance(_test_patching.path, _PatchedModuleObj ) assert _test_patching.path.join is mock # check join assert _test_patching.join is mock # check that the other attributes are untouched assert _test_patching.os.rename is original_rename assert _test_patching.path.dirname is original_dirname assert _test_patching.os.path.dirname is original_dirname # Even renamed modules or objects must be patched # check renamed_os.path.join assert isinstance(_test_patching.renamed_os, _PatchedModuleObj ) assert isinstance(_test_patching.renamed_os.path, _PatchedModuleObj ) assert _test_patching.renamed_os.path.join is mock # check renamed_path.join assert isinstance(_test_patching.renamed_path, _PatchedModuleObj ) assert _test_patching.renamed_path.join is mock # check renamed_join assert _test_patching.renamed_join is mock # check that the other attributes are untouched assert _test_patching.renamed_os.rename is original_rename assert _test_patching.renamed_path.dirname is original_dirname assert _test_patching.renamed_os.path.dirname is original_dirname # check that everthing is back to normal when the patch is over assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join def UpperCamelCase ( ): """simple docstring""" assert _test_patching.open is open __magic_name__ : Optional[Any] = '''__test_patch_submodule_builtin_mock__''' # _test_patching has "open" in its globals assert _test_patching.open is open with patch_submodule(_test_patching, """open""", __lowerCAmelCase ): assert _test_patching.open is mock # check that everthing is back to normal when the patch is over assert _test_patching.open is open def UpperCamelCase ( ): """simple docstring""" __magic_name__ : Dict = '''__test_patch_submodule_missing_mock__''' with patch_submodule(_test_patching, """pandas.read_csv""", __lowerCAmelCase ): pass def UpperCamelCase ( ): """simple docstring""" __magic_name__ : Tuple = '''__test_patch_submodule_missing_builtin_mock__''' # _test_patching doesn't have "len" in its globals assert getattr(_test_patching, """len""", __lowerCAmelCase ) is None with patch_submodule(_test_patching, """len""", __lowerCAmelCase ): assert _test_patching.len is mock assert _test_patching.len is len def UpperCamelCase ( ): """simple docstring""" __magic_name__ : Optional[int] = '''__test_patch_submodule_start_and_stop_mock__''' __magic_name__ : str = patch_submodule(_test_patching, """open""", __lowerCAmelCase ) assert _test_patching.open is open patch.start() assert _test_patching.open is mock patch.stop() assert _test_patching.open is open def UpperCamelCase ( ): """simple docstring""" from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join __magic_name__ : Tuple = '''__test_patch_submodule_successive_join__''' __magic_name__ : str = '''__test_patch_submodule_successive_dirname__''' __magic_name__ : List[str] = '''__test_patch_submodule_successive_rename__''' assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename with patch_submodule(_test_patching, """os.path.join""", __lowerCAmelCase ): with patch_submodule(_test_patching, """os.rename""", __lowerCAmelCase ): with patch_submodule(_test_patching, """os.path.dirname""", __lowerCAmelCase ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename # try another order with patch_submodule(_test_patching, """os.rename""", __lowerCAmelCase ): with patch_submodule(_test_patching, """os.path.join""", __lowerCAmelCase ): with patch_submodule(_test_patching, """os.path.dirname""", __lowerCAmelCase ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename def UpperCamelCase ( ): """simple docstring""" __magic_name__ : int = '''__test_patch_submodule_doesnt_exist_mock__''' with patch_submodule(_test_patching, """__module_that_doesn_exist__.__attribute_that_doesn_exist__""", __lowerCAmelCase ): pass with patch_submodule(_test_patching, """os.__attribute_that_doesn_exist__""", __lowerCAmelCase ): pass
342
import os from typing import Dict, List, Tuple, TypeVar, Union __snake_case = TypeVar('''T''') __snake_case = Union[List[T], Tuple[T, ...]] __snake_case = Union[T, List[T], Dict[str, T]] __snake_case = Union[str, bytes, os.PathLike]
348
0
"""simple docstring""" SCREAMING_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.35_58_18, } def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' 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()
46
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_big_bird import BigBirdTokenizer else: __snake_case = None __snake_case = logging.get_logger(__name__) __snake_case = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} __snake_case = { '''vocab_file''': { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model''', '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model''' ), }, '''tokenizer_file''': { '''google/bigbird-roberta-base''': ( '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/tokenizer.json''' ), '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/tokenizer.json''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/tokenizer.json''' ), }, } __snake_case = { '''google/bigbird-roberta-base''': 40_96, '''google/bigbird-roberta-large''': 40_96, '''google/bigbird-base-trivia-itc''': 40_96, } __snake_case = '''▁''' class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Dict = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = BigBirdTokenizer __lowerCamelCase : Any = ["""input_ids""", """attention_mask"""] __lowerCamelCase : List[int] = [] def __init__( self , snake_case__=None , snake_case__=None , snake_case__="<unk>" , snake_case__="<s>" , snake_case__="</s>" , snake_case__="<pad>" , snake_case__="[SEP]" , snake_case__="[MASK]" , snake_case__="[CLS]" , **snake_case__ , ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else bos_token UpperCAmelCase : Optional[int] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else eos_token UpperCAmelCase : List[str] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else unk_token UpperCAmelCase : Union[str, Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else pad_token UpperCAmelCase : int =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else cls_token UpperCAmelCase : str =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else sep_token # Mask token behave like a normal word, i.e. include the space before it UpperCAmelCase : List[Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else mask_token super().__init__( snake_case__ , tokenizer_file=snake_case__ , bos_token=snake_case__ , eos_token=snake_case__ , unk_token=snake_case__ , sep_token=snake_case__ , pad_token=snake_case__ , cls_token=snake_case__ , mask_token=snake_case__ , **snake_case__ , ) UpperCAmelCase : Tuple =vocab_file UpperCAmelCase : Optional[int] =False if not self.vocab_file else True def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : int =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None , snake_case__ = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is None: return [1] + ([0] * len(snake_case__ )) + [1] return [1] + ([0] * len(snake_case__ )) + [1] + ([0] * len(snake_case__ )) + [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : Optional[Any] =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> Tuple[str]: '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(snake_case__ ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCAmelCase : Optional[int] =os.path.join( snake_case__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case__ ): copyfile(self.vocab_file , snake_case__ ) return (out_vocab_file,)
348
0
import argparse from pathlib import Path from typing import Dict, OrderedDict, Tuple import torch from audiocraft.models import MusicGen from transformers import ( AutoFeatureExtractor, AutoTokenizer, EncodecModel, MusicgenDecoderConfig, MusicgenForConditionalGeneration, MusicgenProcessor, TaEncoderModel, ) from transformers.models.musicgen.modeling_musicgen import MusicgenForCausalLM from transformers.utils import logging logging.set_verbosity_info() A : str = logging.get_logger(__name__) A : Union[str, Any] = ['''model.decoder.embed_positions.weights'''] def __lowerCamelCase ( __a :Union[str, Any] ) -> Dict: """simple docstring""" if "emb" in name: A__ = name.replace("""emb""" , """model.decoder.embed_tokens""" ) if "transformer" in name: A__ = name.replace("""transformer""" , """model.decoder""" ) if "cross_attention" in name: A__ = name.replace("""cross_attention""" , """encoder_attn""" ) if "linear1" in name: A__ = name.replace("""linear1""" , """fc1""" ) if "linear2" in name: A__ = name.replace("""linear2""" , """fc2""" ) if "norm1" in name: A__ = name.replace("""norm1""" , """self_attn_layer_norm""" ) if "norm_cross" in name: A__ = name.replace("""norm_cross""" , """encoder_attn_layer_norm""" ) if "norm2" in name: A__ = name.replace("""norm2""" , """final_layer_norm""" ) if "out_norm" in name: A__ = name.replace("""out_norm""" , """model.decoder.layer_norm""" ) if "linears" in name: A__ = name.replace("""linears""" , """lm_heads""" ) if "condition_provider.conditioners.description.output_proj" in name: A__ = name.replace("""condition_provider.conditioners.description.output_proj""" , """enc_to_dec_proj""" ) return name def __lowerCamelCase ( __a :Dict , __a :str ) -> Tuple[Dict, Dict]: """simple docstring""" A__ = list(state_dict.keys() ) A__ = {} for key in keys: A__ = state_dict.pop(__lowerCAmelCase ) A__ = rename_keys(__lowerCAmelCase ) if "in_proj_weight" in key: # split fused qkv proj A__ = val[:hidden_size, :] A__ = val[hidden_size : 2 * hidden_size, :] A__ = val[-hidden_size:, :] elif "enc_to_dec_proj" in key: A__ = val else: A__ = val return state_dict, enc_dec_proj_state_dict def __lowerCamelCase ( __a :Union[str, Any] ) -> MusicgenDecoderConfig: """simple docstring""" if checkpoint == "small": # default config values A__ = 1_0_2_4 A__ = 2_4 A__ = 1_6 elif checkpoint == "medium": A__ = 1_5_3_6 A__ = 4_8 A__ = 2_4 elif checkpoint == "large": A__ = 2_0_4_8 A__ = 4_8 A__ = 3_2 else: raise ValueError(F'Checkpoint should be one of `[\'small\', \'medium\', \'large\']`, got {checkpoint}.' ) A__ = MusicgenDecoderConfig( hidden_size=__lowerCAmelCase , ffn_dim=hidden_size * 4 , num_hidden_layers=__lowerCAmelCase , num_attention_heads=__lowerCAmelCase , ) return config @torch.no_grad() def __lowerCamelCase ( __a :Optional[Any] , __a :str=None , __a :Optional[int]=None , __a :Optional[Any]="cpu" ) -> Any: """simple docstring""" A__ = MusicGen.get_pretrained(__lowerCAmelCase , device=__lowerCAmelCase ) A__ = decoder_config_from_checkpoint(__lowerCAmelCase ) A__ = fairseq_model.lm.state_dict() A__ = rename_state_dict( __lowerCAmelCase , hidden_size=decoder_config.hidden_size ) A__ = TaEncoderModel.from_pretrained("""t5-base""" ) A__ = EncodecModel.from_pretrained("""facebook/encodec_32khz""" ) A__ = MusicgenForCausalLM(__lowerCAmelCase ).eval() # load all decoder weights - expect that we'll be missing embeddings and enc-dec projection A__ = decoder.load_state_dict(__lowerCAmelCase , strict=__lowerCAmelCase ) for key in missing_keys.copy(): if key.startswith(("""text_encoder""", """audio_encoder""") ) or key in EXPECTED_MISSING_KEYS: missing_keys.remove(__lowerCAmelCase ) if len(__lowerCAmelCase ) > 0: raise ValueError(F'Missing key(s) in state_dict: {missing_keys}' ) if len(__lowerCAmelCase ) > 0: raise ValueError(F'Unexpected key(s) in state_dict: {unexpected_keys}' ) # init the composite model A__ = MusicgenForConditionalGeneration(text_encoder=__lowerCAmelCase , audio_encoder=__lowerCAmelCase , decoder=__lowerCAmelCase ) # load the pre-trained enc-dec projection (from the decoder state dict) model.enc_to_dec_proj.load_state_dict(__lowerCAmelCase ) # check we can do a forward pass A__ = torch.arange(0 , 8 , dtype=torch.long ).reshape(2 , -1 ) A__ = input_ids.reshape(2 * 4 , -1 ) with torch.no_grad(): A__ = model(input_ids=__lowerCAmelCase , decoder_input_ids=__lowerCAmelCase ).logits if logits.shape != (8, 1, 2_0_4_8): raise ValueError("""Incorrect shape for logits""" ) # now construct the processor A__ = AutoTokenizer.from_pretrained("""t5-base""" ) A__ = AutoFeatureExtractor.from_pretrained("""facebook/encodec_32khz""" , padding_side="""left""" ) A__ = MusicgenProcessor(feature_extractor=__lowerCAmelCase , tokenizer=__lowerCAmelCase ) # set the appropriate bos/pad token ids A__ = 2_0_4_8 A__ = 2_0_4_8 # set other default generation config params A__ = int(3_0 * audio_encoder.config.frame_rate ) A__ = True A__ = 3.0 if pytorch_dump_folder is not None: Path(__lowerCAmelCase ).mkdir(exist_ok=__lowerCAmelCase ) logger.info(F'Saving model {checkpoint} to {pytorch_dump_folder}' ) model.save_pretrained(__lowerCAmelCase ) processor.save_pretrained(__lowerCAmelCase ) if repo_id: logger.info(F'Pushing model {checkpoint} to {repo_id}' ) model.push_to_hub(__lowerCAmelCase ) processor.push_to_hub(__lowerCAmelCase ) if __name__ == "__main__": A : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint''', default='''small''', type=str, help='''Checkpoint size of the MusicGen model you\'d like to convert. Can be one of: `[\'small\', \'medium\', \'large\']`.''', ) parser.add_argument( '''--pytorch_dump_folder''', required=True, default=None, type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument( '''--push_to_hub''', default=None, type=str, help='''Where to upload the converted model on the 🤗 hub.''' ) parser.add_argument( '''--device''', default='''cpu''', type=str, help='''Torch device to run the conversion, either cpu or cuda.''' ) A : List[Any] = parser.parse_args() convert_musicgen_checkpoint(args.checkpoint, args.pytorch_dump_folder, args.push_to_hub)
274
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def lowerCAmelCase_ ( __lowerCAmelCase )-> Optional[Any]: '''simple docstring''' def is_in_circle(__lowerCAmelCase , __lowerCAmelCase ) -> bool: UpperCAmelCase : List[Any] =sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle UpperCAmelCase : List[Any] =mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(__lowerCAmelCase ) ) # The ratio of the area for circle to square is pi/4. UpperCAmelCase : Dict =proportion * 4 print(f'''The estimated value of pi is {pi_estimate}''' ) print(f'''The numpy value of pi is {pi}''' ) print(f'''The total error is {abs(pi - pi_estimate )}''' ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 , )-> float: '''simple docstring''' return mean( function_to_integrate(uniform(__lowerCAmelCase , __lowerCAmelCase ) ) for _ in range(__lowerCAmelCase ) ) * (max_value - min_value) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 )-> None: '''simple docstring''' def identity_function(__lowerCAmelCase ) -> float: return x UpperCAmelCase : List[Any] =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase : Dict =(max_value * max_value - min_value * min_value) / 2 print('''******************''' ) print(f'''Estimating area under y=x where x varies from {min_value} to {max_value}''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {expected_value}''' ) print(f'''Total error is {abs(estimated_value - expected_value )}''' ) print('''******************''' ) def lowerCAmelCase_ ( __lowerCAmelCase )-> None: '''simple docstring''' def function_to_integrate(__lowerCAmelCase ) -> float: return sqrt(4.0 - x * x ) UpperCAmelCase : Dict =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , 0.0 , 2.0 ) print('''******************''' ) print('''Estimating pi using area_under_curve_estimator''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {pi}''' ) print(f'''Total error is {abs(estimated_value - pi )}''' ) print('''******************''' ) if __name__ == "__main__": import doctest doctest.testmod()
348
0
from abc import ABC, abstractmethod from argparse import ArgumentParser class A_ ( lowerCamelCase__ ): '''simple docstring''' @staticmethod @abstractmethod def SCREAMING_SNAKE_CASE__ ( snake_case ): raise NotImplementedError() @abstractmethod def SCREAMING_SNAKE_CASE__ ( self ): raise NotImplementedError()
195
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __snake_case : def __init__( self , snake_case__ , snake_case__=12 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , snake_case__=0 , snake_case__=None , ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : List[Any] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Union[str, Any] =use_input_mask UpperCAmelCase : Tuple =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : Tuple =hidden_size UpperCAmelCase : Dict =projection_dim UpperCAmelCase : Optional[int] =num_hidden_layers UpperCAmelCase : Dict =num_attention_heads UpperCAmelCase : int =intermediate_size UpperCAmelCase : Any =dropout UpperCAmelCase : Union[str, Any] =attention_dropout UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : str =scope UpperCAmelCase : str =bos_token_id def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : int =None if self.use_input_mask: UpperCAmelCase : Union[str, Any] =random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: UpperCAmelCase : Optional[int] =input_mask.numpy() UpperCAmelCase , UpperCAmelCase : List[Any] =input_mask.shape UpperCAmelCase : Optional[Any] =np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : List[Any] =1 UpperCAmelCase : Tuple =0 UpperCAmelCase : List[Any] =self.get_config() return config, input_ids, tf.convert_to_tensor(snake_case__ ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =TFBlipTextModel(config=snake_case__ ) UpperCAmelCase : List[Any] =model(snake_case__ , attention_mask=snake_case__ , training=snake_case__ ) UpperCAmelCase : str =model(snake_case__ , training=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[Any] =config_and_inputs UpperCAmelCase : Optional[int] ={'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __snake_case ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Optional[int] = (TFBlipTextModel,) if is_tf_available() else () __lowerCamelCase : Dict = False __lowerCamelCase : Optional[Any] = False __lowerCamelCase : Dict = False def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : str =BlipTextModelTester(self ) UpperCAmelCase : Optional[int] =ConfigTester(self , config_class=snake_case__ , hidden_size=37 ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip(reason='''Blip does not use inputs_embeds''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @slow def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase : Optional[Any] =TFBlipTextModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def UpperCAmelCase__ ( self , snake_case__=True ) -> Any: '''simple docstring''' super().test_pt_tf_model_equivalence(allow_missing_keys=snake_case__ )
348
0
'''simple docstring''' import copy from typing import Dict, List, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING __A ={ 'facebook/mask2former-swin-small-coco-instance': ( 'https://huggingface.co/facebook/mask2former-swin-small-coco-instance/blob/main/config.json' ) # See all Mask2Former models at https://huggingface.co/models?filter=mask2former } __A =logging.get_logger(__name__) class _snake_case ( lowerCamelCase__ ): lowerCAmelCase :List[Any] = """mask2former""" lowerCAmelCase :List[Any] = ["""swin"""] lowerCAmelCase :Union[str, Any] = {"""hidden_size""": """hidden_dim"""} def __init__( self , _lowerCamelCase = None , _lowerCamelCase = 256 , _lowerCamelCase = 256 , _lowerCamelCase = 256 , _lowerCamelCase = 1024 , _lowerCamelCase = "relu" , _lowerCamelCase = 6 , _lowerCamelCase = 10 , _lowerCamelCase = 8 , _lowerCamelCase = 0.0 , _lowerCamelCase = 2048 , _lowerCamelCase = False , _lowerCamelCase = False , _lowerCamelCase = 4 , _lowerCamelCase = 255 , _lowerCamelCase = 100 , _lowerCamelCase = 0.1 , _lowerCamelCase = 2.0 , _lowerCamelCase = 5.0 , _lowerCamelCase = 5.0 , _lowerCamelCase = 1_2544 , _lowerCamelCase = 3.0 , _lowerCamelCase = 0.75 , _lowerCamelCase = 0.02 , _lowerCamelCase = 1.0 , _lowerCamelCase = True , _lowerCamelCase = [4, 8, 16, 32] , _lowerCamelCase = None , **_lowerCamelCase , ): if backbone_config is None: logger.info("""`backbone_config` is `None`. Initializing the config with the default `Swin` backbone.""") UpperCAmelCase__ : Optional[Any] = CONFIG_MAPPING['''swin''']( image_size=224 , in_channels=3 , patch_size=4 , embed_dim=96 , depths=[2, 2, 18, 2] , num_heads=[3, 6, 12, 24] , window_size=7 , drop_path_rate=0.3 , use_absolute_embeddings=snake_case__ , out_features=["""stage1""", """stage2""", """stage3""", """stage4"""] , ) if isinstance(snake_case__ , snake_case__): UpperCAmelCase__ : Union[str, Any] = backbone_config.pop("""model_type""") UpperCAmelCase__ : str = CONFIG_MAPPING[backbone_model_type] UpperCAmelCase__ : Any = config_class.from_dict(snake_case__) # verify that the backbone is supported if backbone_config.model_type not in self.backbones_supported: logger.warning_once( f'''Backbone {backbone_config.model_type} is not a supported model and may not be compatible with Mask2Former. ''' f'''Supported model types: {','.join(self.backbones_supported)}''') UpperCAmelCase__ : Tuple = backbone_config UpperCAmelCase__ : Dict = feature_size UpperCAmelCase__ : Dict = mask_feature_size UpperCAmelCase__ : Dict = hidden_dim UpperCAmelCase__ : Optional[int] = encoder_feedforward_dim UpperCAmelCase__ : str = activation_function UpperCAmelCase__ : int = encoder_layers UpperCAmelCase__ : Tuple = decoder_layers UpperCAmelCase__ : Tuple = num_attention_heads UpperCAmelCase__ : Optional[Any] = dropout UpperCAmelCase__ : Optional[int] = dim_feedforward UpperCAmelCase__ : Any = pre_norm UpperCAmelCase__ : Optional[int] = enforce_input_projection UpperCAmelCase__ : str = common_stride UpperCAmelCase__ : Any = ignore_value UpperCAmelCase__ : List[Any] = num_queries UpperCAmelCase__ : List[Any] = no_object_weight UpperCAmelCase__ : List[Any] = class_weight UpperCAmelCase__ : int = mask_weight UpperCAmelCase__ : int = dice_weight UpperCAmelCase__ : Tuple = train_num_points UpperCAmelCase__ : Any = oversample_ratio UpperCAmelCase__ : Any = importance_sample_ratio UpperCAmelCase__ : int = init_std UpperCAmelCase__ : Union[str, Any] = init_xavier_std UpperCAmelCase__ : Any = use_auxiliary_loss UpperCAmelCase__ : Optional[Any] = feature_strides UpperCAmelCase__ : Optional[int] = output_auxiliary_logits UpperCAmelCase__ : Any = decoder_layers super().__init__(**snake_case__) @classmethod def snake_case__ ( cls , _lowerCamelCase , **_lowerCamelCase): return cls( backbone_config=snake_case__ , **snake_case__ , ) def snake_case__ ( self): UpperCAmelCase__ : List[Any] = copy.deepcopy(self.__dict__) UpperCAmelCase__ : Any = self.backbone_config.to_dict() UpperCAmelCase__ : int = self.__class__.model_type return output
163
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging __snake_case = logging.get_logger(__name__) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' UpperCAmelCase : Dict =nn.functional.normalize(__lowerCAmelCase ) UpperCAmelCase : Tuple =nn.functional.normalize(__lowerCAmelCase ) return torch.mm(__lowerCAmelCase , normalized_text_embeds.t() ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : List[str] = CLIPConfig __lowerCamelCase : List[Any] = ["""CLIPEncoderLayer"""] def __init__( self , snake_case__ ) -> Dict: '''simple docstring''' super().__init__(snake_case__ ) UpperCAmelCase : Dict =CLIPVisionModel(config.vision_config ) UpperCAmelCase : Optional[Any] =nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=snake_case__ ) UpperCAmelCase : int =nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : List[str] =nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : str =nn.Parameter(torch.ones(17 ) , requires_grad=snake_case__ ) UpperCAmelCase : Optional[int] =nn.Parameter(torch.ones(3 ) , requires_grad=snake_case__ ) @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Union[str, Any] =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : Optional[Any] =self.visual_projection(snake_case__ ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 UpperCAmelCase : List[str] =cosine_distance(snake_case__ , self.special_care_embeds ).cpu().float().numpy() UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ).cpu().float().numpy() UpperCAmelCase : Tuple =[] UpperCAmelCase : Dict =image_embeds.shape[0] for i in range(snake_case__ ): UpperCAmelCase : str ={'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : str =0.0 for concept_idx in range(len(special_cos_dist[0] ) ): UpperCAmelCase : Optional[Any] =special_cos_dist[i][concept_idx] UpperCAmelCase : Union[str, Any] =self.special_care_embeds_weights[concept_idx].item() UpperCAmelCase : str =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} ) UpperCAmelCase : int =0.01 for concept_idx in range(len(cos_dist[0] ) ): UpperCAmelCase : Any =cos_dist[i][concept_idx] UpperCAmelCase : Optional[int] =self.concept_embeds_weights[concept_idx].item() UpperCAmelCase : int =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(snake_case__ ) result.append(snake_case__ ) UpperCAmelCase : Optional[int] =[len(res['''bad_concepts'''] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Any =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : List[str] =self.visual_projection(snake_case__ ) UpperCAmelCase : Any =cosine_distance(snake_case__ , self.special_care_embeds ) UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : Optional[Any] =0.0 UpperCAmelCase : Any =special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) UpperCAmelCase : str =torch.any(special_scores > 0 , dim=1 ) UpperCAmelCase : List[Any] =special_care * 0.01 UpperCAmelCase : Union[str, Any] =special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) UpperCAmelCase : List[Any] =(cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) UpperCAmelCase : str =torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
348
0
import json import os import unittest from transformers import AutoTokenizer, GPTaTokenizer, GPTaTokenizerFast from transformers.models.gpta.tokenization_gpta import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase ( lowerCamelCase__ , unittest.TestCase ): '''simple docstring''' snake_case_ = GPTaTokenizer snake_case_ = GPTaTokenizerFast snake_case_ = True snake_case_ = {"""add_prefix_space""": True} snake_case_ = False def UpperCamelCase_ ( self : str ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt __A = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', '''<|endoftext|>''', ] __A = dict(zip(snake_case__ ,range(len(snake_case__ ) ) ) ) __A = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] __A = {'''unk_token''': '''<unk>'''} __A = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["vocab_file"] ) __A = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file ,"w" ,encoding="utf-8" ) as fp: fp.write(json.dumps(snake_case__ ) + "\n" ) with open(self.merges_file ,"w" ,encoding="utf-8" ) as fp: fp.write("\n".join(snake_case__ ) ) def UpperCamelCase_ ( self : Optional[Any] ,**A : List[str] ): kwargs.update(self.special_tokens_map ) return GPTaTokenizer.from_pretrained(self.tmpdirname ,**snake_case__ ) def UpperCamelCase_ ( self : int ,**A : List[Any] ): kwargs.update(self.special_tokens_map ) return GPTaTokenizerFast.from_pretrained(self.tmpdirname ,**snake_case__ ) def UpperCamelCase_ ( self : Dict ,A : Optional[Any] ): __A = '''lower newer''' __A = '''lower newer''' return input_text, output_text def UpperCamelCase_ ( self : Optional[int] ): __A = GPTaTokenizer(self.vocab_file ,self.merges_file ,**self.special_tokens_map ) __A = '''lower newer''' __A = ['''\u0120low''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] __A = tokenizer.tokenize(snake_case__ ,add_prefix_space=snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) __A = tokens + [tokenizer.unk_token] __A = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case__ ) ,snake_case__ ) def UpperCamelCase_ ( self : List[str] ): if not self.test_rust_tokenizer: return __A = self.get_tokenizer() __A = self.get_rust_tokenizer(add_prefix_space=snake_case__ ) __A = '''lower newer''' # Testing tokenization __A = tokenizer.tokenize(snake_case__ ,add_prefix_space=snake_case__ ) __A = rust_tokenizer.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) # Testing conversion to ids without special tokens __A = tokenizer.encode(snake_case__ ,add_special_tokens=snake_case__ ,add_prefix_space=snake_case__ ) __A = rust_tokenizer.encode(snake_case__ ,add_special_tokens=snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) # Testing conversion to ids with special tokens __A = self.get_rust_tokenizer(add_prefix_space=snake_case__ ) __A = tokenizer.encode(snake_case__ ,add_prefix_space=snake_case__ ) __A = rust_tokenizer.encode(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) # Testing the unknown token __A = tokens + [rust_tokenizer.unk_token] __A = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(snake_case__ ) ,snake_case__ ) def UpperCamelCase_ ( self : Optional[Any] ,*A : int ,**A : Dict ): pass def UpperCamelCase_ ( self : Dict ,A : List[str]=15 ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __A = self.rust_tokenizer_class.from_pretrained(snake_case__ ,**snake_case__ ) # Simple input __A = '''This is a simple input''' __A = ['''This is a simple input 1''', '''This is a simple input 2'''] __A = ('''This is a simple input''', '''This is a pair''') __A = [ ('''This is a simple input 1''', '''This is a simple input 2'''), ('''This is a simple pair 1''', '''This is a simple pair 2'''), ] # Simple input tests self.assertRaises(snake_case__ ,tokenizer_r.encode ,snake_case__ ,max_length=snake_case__ ,padding="max_length" ) # Simple input self.assertRaises(snake_case__ ,tokenizer_r.encode_plus ,snake_case__ ,max_length=snake_case__ ,padding="max_length" ) # Simple input self.assertRaises( snake_case__ ,tokenizer_r.batch_encode_plus ,snake_case__ ,max_length=snake_case__ ,padding="max_length" ,) # Pair input self.assertRaises(snake_case__ ,tokenizer_r.encode ,snake_case__ ,max_length=snake_case__ ,padding="max_length" ) # Pair input self.assertRaises(snake_case__ ,tokenizer_r.encode_plus ,snake_case__ ,max_length=snake_case__ ,padding="max_length" ) # Pair input self.assertRaises( snake_case__ ,tokenizer_r.batch_encode_plus ,snake_case__ ,max_length=snake_case__ ,padding="max_length" ,) def UpperCamelCase_ ( self : Any ): __A = GPTaTokenizer.from_pretrained(self.tmpdirname ,pad_token="<pad>" ) # Simple input __A = '''This is a simple input''' __A = ['''This is a simple input looooooooong''', '''This is a simple input'''] __A = ('''This is a simple input''', '''This is a pair''') __A = [ ('''This is a simple input loooooong''', '''This is a simple input'''), ('''This is a simple pair loooooong''', '''This is a simple pair'''), ] __A = tokenizer.pad_token_id __A = tokenizer(snake_case__ ,padding="max_length" ,max_length=30 ,return_tensors="np" ) __A = tokenizer(snake_case__ ,padding=snake_case__ ,truncate=snake_case__ ,return_tensors="np" ) __A = tokenizer(*snake_case__ ,padding="max_length" ,max_length=60 ,return_tensors="np" ) __A = tokenizer(snake_case__ ,padding=snake_case__ ,truncate=snake_case__ ,return_tensors="np" ) # s # test single string max_length padding self.assertEqual(out_s["input_ids"].shape[-1] ,30 ) self.assertTrue(pad_token_id in out_s["input_ids"] ) self.assertTrue(0 in out_s["attention_mask"] ) # s2 # test automatic padding self.assertEqual(out_sa["input_ids"].shape[-1] ,33 ) # long slice doesn't have padding self.assertFalse(pad_token_id in out_sa["input_ids"][0] ) self.assertFalse(0 in out_sa["attention_mask"][0] ) # short slice does have padding self.assertTrue(pad_token_id in out_sa["input_ids"][1] ) self.assertTrue(0 in out_sa["attention_mask"][1] ) # p # test single pair max_length padding self.assertEqual(out_p["input_ids"].shape[-1] ,60 ) self.assertTrue(pad_token_id in out_p["input_ids"] ) self.assertTrue(0 in out_p["attention_mask"] ) # p2 # test automatic padding pair self.assertEqual(out_pa["input_ids"].shape[-1] ,52 ) # long slice pair doesn't have padding self.assertFalse(pad_token_id in out_pa["input_ids"][0] ) self.assertFalse(0 in out_pa["attention_mask"][0] ) # short slice pair does have padding self.assertTrue(pad_token_id in out_pa["input_ids"][1] ) self.assertTrue(0 in out_pa["attention_mask"][1] ) def UpperCamelCase_ ( self : Dict ): __A = '''$$$''' __A = GPTaTokenizer.from_pretrained(self.tmpdirname ,bos_token=snake_case__ ,add_bos_token=snake_case__ ) __A = '''This is a simple input''' __A = ['''This is a simple input 1''', '''This is a simple input 2'''] __A = tokenizer.bos_token_id __A = tokenizer(snake_case__ ) __A = tokenizer(snake_case__ ) self.assertEqual(out_s.input_ids[0] ,snake_case__ ) self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids ) ) __A = tokenizer.decode(out_s.input_ids ) __A = tokenizer.batch_decode(out_sa.input_ids ) self.assertEqual(decode_s.split()[0] ,snake_case__ ) self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa ) ) def UpperCamelCase_ ( self : Optional[int] ): pass def UpperCamelCase_ ( self : Union[str, Any] ): __A = [self.get_tokenizer(do_lower_case=snake_case__ ,add_bos_token=snake_case__ )] for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): __A = '''Encode this.''' __A = '''This one too please.''' __A = tokenizer.encode(snake_case__ ,add_special_tokens=snake_case__ ) encoded_sequence += tokenizer.encode(snake_case__ ,add_special_tokens=snake_case__ ) __A = tokenizer.encode_plus( snake_case__ ,snake_case__ ,add_special_tokens=snake_case__ ,return_special_tokens_mask=snake_case__ ,) __A = encoded_sequence_dict['''input_ids'''] __A = encoded_sequence_dict['''special_tokens_mask'''] self.assertEqual(len(snake_case__ ) ,len(snake_case__ ) ) __A = [ (x if not special_tokens_mask[i] else None) for i, x in enumerate(snake_case__ ) ] __A = [x for x in filtered_sequence if x is not None] self.assertEqual(snake_case__ ,snake_case__ ) @require_tokenizers class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def UpperCamelCase_ ( self : int ): __A = AutoTokenizer.from_pretrained("facebook/opt-350m" ,from_slow=snake_case__ ) __A = '''A photo of a cat''' __A = tokenizer.encode( snake_case__ ,) self.assertEqual(snake_case__ ,[2, 2_50, 13_45, 9, 10, 47_58] ) tokenizer.save_pretrained("test_opt" ) __A = AutoTokenizer.from_pretrained("./test_opt" ) __A = tokenizer.encode( snake_case__ ,) self.assertEqual(snake_case__ ,[2, 2_50, 13_45, 9, 10, 47_58] ) def UpperCamelCase_ ( self : Optional[Any] ): __A = AutoTokenizer.from_pretrained("facebook/opt-350m" ,use_slow=snake_case__ ) __A = '''A photo of a cat''' __A = tokenizer.encode( snake_case__ ,) # Same as above self.assertEqual(snake_case__ ,[2, 2_50, 13_45, 9, 10, 47_58] ) @unittest.skip("This test is failing because of a bug in the fast tokenizer" ) def UpperCamelCase_ ( self : Any ): __A = AutoTokenizer.from_pretrained("facebook/opt-350m" ,from_slow=snake_case__ ) __A = '''bos''' __A = tokenizer.get_vocab()['''bos'''] __A = '''A photo of a cat''' __A = tokenizer.encode( snake_case__ ,) # We changed the bos token self.assertEqual(snake_case__ ,[3_19_57, 2_50, 13_45, 9, 10, 47_58] ) tokenizer.save_pretrained("./tok" ) __A = AutoTokenizer.from_pretrained("./tok" ) self.assertTrue(tokenizer.is_fast ) __A = tokenizer.encode( snake_case__ ,) self.assertEqual(snake_case__ ,[3_19_57, 2_50, 13_45, 9, 10, 47_58] )
15
import argparse import intel_extension_for_pytorch as ipex import torch from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline __snake_case = argparse.ArgumentParser('''Stable Diffusion script with intel optimization''', add_help=False) parser.add_argument('''--dpm''', action='''store_true''', help='''Enable DPMSolver or not''') parser.add_argument('''--steps''', default=None, type=int, help='''Num inference steps''') __snake_case = parser.parse_args() __snake_case = '''cpu''' __snake_case = '''a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings''' __snake_case = '''path-to-your-trained-model''' __snake_case = StableDiffusionPipeline.from_pretrained(model_id) if args.dpm: __snake_case = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) __snake_case = pipe.to(device) # to channels last __snake_case = pipe.unet.to(memory_format=torch.channels_last) __snake_case = pipe.vae.to(memory_format=torch.channels_last) __snake_case = pipe.text_encoder.to(memory_format=torch.channels_last) if pipe.requires_safety_checker: __snake_case = pipe.safety_checker.to(memory_format=torch.channels_last) # optimize with ipex __snake_case = torch.randn(2, 4, 64, 64) __snake_case = torch.rand(1) * 9_99 __snake_case = torch.randn(2, 77, 7_68) __snake_case = (sample, timestep, encoder_hidden_status) try: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example) except Exception: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True) if pipe.requires_safety_checker: __snake_case = ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True) # compute __snake_case = 6_66 __snake_case = torch.Generator(device).manual_seed(seed) __snake_case = {'''generator''': generator} if args.steps is not None: __snake_case = args.steps with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa): __snake_case = pipe(prompt, **generate_kwargs).images[0] # save image image.save('''generated.png''')
348
0
from __future__ import annotations from fractions import Fraction def a( A : int , A : int ) -> bool: """simple docstring""" return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def a( A : str ) -> list[str]: """simple docstring""" a = [] a = 11 a = int("1" + "0" * digit_len ) for num in range(__lowerCAmelCase , __lowerCAmelCase ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(__lowerCAmelCase , __lowerCAmelCase ): solutions.append(f'''{num}/{den}''' ) den += 1 num += 1 a = 10 return solutions def a( A : List[Any] = 2 ) -> int: """simple docstring""" a = 1.0 for fraction in fraction_list(__lowerCAmelCase ): a = Fraction(__lowerCAmelCase ) result *= frac.denominator / frac.numerator return int(__lowerCAmelCase ) if __name__ == "__main__": print(solution())
227
__snake_case = '''Input must be a string of 8 numbers plus letter''' __snake_case = '''TRWAGMYFPDXBNJZSQVHLCKE''' def lowerCAmelCase_ ( __lowerCAmelCase )-> bool: '''simple docstring''' if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCAmelCase : Optional[Any] =f'''Expected string as input, found {type(__lowerCAmelCase ).__name__}''' raise TypeError(__lowerCAmelCase ) UpperCAmelCase : List[Any] =spanish_id.replace('''-''' , '''''' ).upper() if len(__lowerCAmelCase ) != 9: raise ValueError(__lowerCAmelCase ) try: UpperCAmelCase : int =int(spanish_id_clean[0:8] ) UpperCAmelCase : Optional[int] =spanish_id_clean[8] except ValueError as ex: raise ValueError(__lowerCAmelCase ) from ex if letter.isdigit(): raise ValueError(__lowerCAmelCase ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
348
0
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 lowercase_ = logging.getLogger(__name__) @dataclass @add_start_docstrings(TrainingArguments.__doc__ ) class __lowerCAmelCase ( lowerCamelCase__ ): _a = field(default=lowerCamelCase__ , metadata={"""help""": """Whether to use SortishSampler or not."""} ) _a = field( default=lowerCamelCase__ , metadata={"""help""": """Whether to use generate to calculate generative metrics (ROUGE, BLEU)."""} ) _a = field( default=lowerCamelCase__ , 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.""" ) } , ) _a = field( default=lowerCamelCase__ , 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.""" ) } , ) _a = field( default=lowerCamelCase__ , metadata={ """help""": """Model id, file path or url pointing to a GenerationConfig json file, to use during prediction.""" } , ) def A__ ( self ) -> List[str]: '''simple docstring''' _lowercase =super().to_dict() for k, v in d.items(): if isinstance(snake_case__ , snake_case__ ): _lowercase =v.to_dict() return d
205
def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Dict =str(bin(__lowerCAmelCase ) ) binary_number += "0" * shift_amount return binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Any =str(bin(__lowerCAmelCase ) )[2:] if shift_amount >= len(__lowerCAmelCase ): return "0b0" UpperCAmelCase : Optional[Any] =binary_number[: len(__lowerCAmelCase ) - shift_amount] return "0b" + shifted_binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number >= 0: # Get binary representation of positive number UpperCAmelCase : Optional[Any] ='''0''' + str(bin(__lowerCAmelCase ) ).strip('''-''' )[2:] else: # Get binary (2's complement) representation of negative number UpperCAmelCase : int =len(bin(__lowerCAmelCase )[3:] ) # Find 2's complement of number UpperCAmelCase : Any =bin(abs(__lowerCAmelCase ) - (1 << binary_number_length) )[3:] UpperCAmelCase : Optional[Any] =( '''1''' + '''0''' * (binary_number_length - len(__lowerCAmelCase )) + binary_number ) if shift_amount >= len(__lowerCAmelCase ): return "0b" + binary_number[0] * len(__lowerCAmelCase ) return ( "0b" + binary_number[0] * shift_amount + binary_number[: len(__lowerCAmelCase ) - shift_amount] ) if __name__ == "__main__": import doctest doctest.testmod()
348
0
"""simple docstring""" import numpy as np from PIL import Image def _lowercase ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> np.ndarray: SCREAMING_SNAKE_CASE__ : Any = np.array(__lowerCAmelCase ) if arr.shape[0] != arr.shape[1]: raise ValueError("""The input array is not a square matrix""" ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = 0 SCREAMING_SNAKE_CASE__ : int = 0 SCREAMING_SNAKE_CASE__ : Optional[Any] = 0 SCREAMING_SNAKE_CASE__ : Optional[Any] = 0 # compute the shape of the output matrix SCREAMING_SNAKE_CASE__ : Optional[Any] = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape maxpool_shape SCREAMING_SNAKE_CASE__ : List[str] = np.zeros((maxpool_shape, maxpool_shape) ) while i < arr.shape[0]: if i + size > arr.shape[0]: # if the end of the matrix is reached, break break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the maximum of the pooling matrix SCREAMING_SNAKE_CASE__ : int = np.max(arr[i : i + size, j : j + size] ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 SCREAMING_SNAKE_CASE__ : Any = 0 SCREAMING_SNAKE_CASE__ : Dict = 0 return updated_arr def _lowercase ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> np.ndarray: SCREAMING_SNAKE_CASE__ : Optional[int] = np.array(__lowerCAmelCase ) if arr.shape[0] != arr.shape[1]: raise ValueError("""The input array is not a square matrix""" ) SCREAMING_SNAKE_CASE__ : List[str] = 0 SCREAMING_SNAKE_CASE__ : Optional[Any] = 0 SCREAMING_SNAKE_CASE__ : List[Any] = 0 SCREAMING_SNAKE_CASE__ : Dict = 0 # compute the shape of the output matrix SCREAMING_SNAKE_CASE__ : Any = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape avgpool_shape SCREAMING_SNAKE_CASE__ : List[Any] = np.zeros((avgpool_shape, avgpool_shape) ) while i < arr.shape[0]: # if the end of the matrix is reached, break if i + size > arr.shape[0]: break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the average of the pooling matrix SCREAMING_SNAKE_CASE__ : int = int(np.average(arr[i : i + size, j : j + size] ) ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 SCREAMING_SNAKE_CASE__ : Optional[int] = 0 SCREAMING_SNAKE_CASE__ : Any = 0 return updated_arr # Main Function if __name__ == "__main__": from doctest import testmod testmod(name="avgpooling", verbose=True) # Loading the image a :str = Image.open("path_to_image") # Converting the image to numpy array and maxpooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show() # Converting the image to numpy array and averagepooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()
132
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) # TODO Update this __snake_case = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Tuple = """esm""" def __init__( self , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=1026 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__="absolute" , snake_case__=True , snake_case__=None , snake_case__=False , snake_case__=False , snake_case__=None , snake_case__=None , **snake_case__ , ) -> Union[str, Any]: '''simple docstring''' super().__init__(pad_token_id=snake_case__ , mask_token_id=snake_case__ , **snake_case__ ) UpperCAmelCase : List[str] =vocab_size UpperCAmelCase : str =hidden_size UpperCAmelCase : List[Any] =num_hidden_layers UpperCAmelCase : Optional[Any] =num_attention_heads UpperCAmelCase : str =intermediate_size UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : int =attention_probs_dropout_prob UpperCAmelCase : Dict =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : Union[str, Any] =layer_norm_eps UpperCAmelCase : Dict =position_embedding_type UpperCAmelCase : Optional[Any] =use_cache UpperCAmelCase : int =emb_layer_norm_before UpperCAmelCase : List[str] =token_dropout UpperCAmelCase : Optional[Any] =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) UpperCAmelCase : Optional[Any] =EsmFoldConfig() elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =EsmFoldConfig(**snake_case__ ) UpperCAmelCase : Tuple =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) UpperCAmelCase : Any =get_default_vocab_list() else: UpperCAmelCase : Tuple =vocab_list else: UpperCAmelCase : Optional[int] =None UpperCAmelCase : Union[str, Any] =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , snake_case__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =super().to_dict() if isinstance(self.esmfold_config , snake_case__ ): UpperCAmelCase : str =self.esmfold_config.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : str = None __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : float = 0 __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : int = 128 __lowerCamelCase : "TrunkConfig" = None def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' if self.trunk is None: UpperCAmelCase : str =TrunkConfig() elif isinstance(self.trunk , snake_case__ ): UpperCAmelCase : Optional[int] =TrunkConfig(**self.trunk ) def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[Any] =asdict(self ) UpperCAmelCase : Any =self.trunk.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 48 __lowerCamelCase : int = 1024 __lowerCamelCase : int = 128 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : float = 0 __lowerCamelCase : float = 0 __lowerCamelCase : bool = False __lowerCamelCase : int = 4 __lowerCamelCase : Optional[int] = 128 __lowerCamelCase : "StructureModuleConfig" = None def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' if self.structure_module is None: UpperCAmelCase : Any =StructureModuleConfig() elif isinstance(self.structure_module , snake_case__ ): UpperCAmelCase : str =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f'''`max_recycles` should be positive, got {self.max_recycles}.''' ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f''' {self.sequence_state_dim} and {self.sequence_state_dim}.''' ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f''' {self.pairwise_state_dim} and {self.pairwise_state_dim}.''' ) UpperCAmelCase : Optional[int] =self.sequence_state_dim // self.sequence_head_width UpperCAmelCase : Any =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f''' {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.''' ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f''' {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.''' ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f'''`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.''' ) if self.dropout >= 0.4: raise ValueError(f'''`dropout` should not be greater than 0.4, got {self.dropout}.''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =asdict(self ) UpperCAmelCase : Tuple =self.structure_module.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 384 __lowerCamelCase : int = 128 __lowerCamelCase : int = 16 __lowerCamelCase : int = 128 __lowerCamelCase : int = 12 __lowerCamelCase : int = 4 __lowerCamelCase : int = 8 __lowerCamelCase : float = 0.1 __lowerCamelCase : int = 8 __lowerCamelCase : int = 1 __lowerCamelCase : int = 2 __lowerCamelCase : int = 7 __lowerCamelCase : int = 10 __lowerCamelCase : float = 1E-8 __lowerCamelCase : float = 1E5 def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return asdict(self ) def lowerCAmelCase_ ( )-> Tuple: '''simple docstring''' return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
348
0
'''simple docstring''' def lowercase_ ( lowerCAmelCase__ : List[str] ): """simple docstring""" __UpperCAmelCase : int = abs(__lowerCAmelCase ) __UpperCAmelCase : Dict = 0 while n > 0: res += n % 10 n //= 10 return res def lowercase_ ( lowerCAmelCase__ : Optional[Any] ): """simple docstring""" __UpperCAmelCase : Any = abs(__lowerCAmelCase ) return n if n < 10 else n % 10 + sum_of_digits(n // 10 ) def lowercase_ ( lowerCAmelCase__ : List[Any] ): """simple docstring""" return sum(int(__lowerCAmelCase ) for c in str(abs(__lowerCAmelCase ) ) ) def lowercase_ ( ): """simple docstring""" from collections.abc import Callable from timeit import timeit def benchmark_a_function(lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Optional[int] ) -> None: __UpperCAmelCase : Optional[Any] = f'{func.__name__}({value})' __UpperCAmelCase : List[str] = timeit(f'__main__.{call}' , setup="""import __main__""" ) print(f'{call:56} = {func(__lowerCAmelCase )} -- {timing:.4f} seconds' ) for value in (262144, 1125899906842624, 1267650600228229401496703205376): for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact): benchmark_a_function(__lowerCAmelCase , __lowerCAmelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
254
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[int] = (KDPMaDiscreteScheduler,) __lowerCamelCase : List[str] = 10 def UpperCAmelCase__ ( self , **snake_case__ ) -> str: '''simple docstring''' UpperCAmelCase : int ={ '''num_train_timesteps''': 1100, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', } config.update(**snake_case__ ) return config def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=snake_case__ , beta_end=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' UpperCAmelCase : Optional[Any] =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config(prediction_type='''v_prediction''' ) UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : str =self.dummy_model() UpperCAmelCase : Optional[Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : Union[str, Any] =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : str =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Any =model(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : int =output.prev_sample UpperCAmelCase : Dict =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Optional[Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.69_34e-07 ) < 1e-2 assert abs(result_mean.item() - 6.11_12e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.6_93_42_86_50_17_09_72e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0002 ) < 1e-3 def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : Any =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config() UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : Optional[int] =self.dummy_model() UpperCAmelCase : Union[str, Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : str =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : Dict =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =model(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =output.prev_sample UpperCAmelCase : Any =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Union[str, Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : List[Any] =self.scheduler_classes[0] UpperCAmelCase : Dict =self.get_scheduler_config() UpperCAmelCase : List[str] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps , device=snake_case__ ) UpperCAmelCase : int =self.dummy_model() UpperCAmelCase : Tuple =self.dummy_sample_deter.to(snake_case__ ) * scheduler.init_noise_sigma for t in scheduler.timesteps: UpperCAmelCase : Optional[Any] =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : int =model(snake_case__ , snake_case__ ) UpperCAmelCase : str =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =output.prev_sample UpperCAmelCase : List[str] =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Dict =torch.mean(torch.abs(snake_case__ ) ) if str(snake_case__ ).startswith('''cpu''' ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3
348
0
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import TransformeraDModel, VQDiffusionPipeline, VQDiffusionScheduler, VQModel from diffusers.pipelines.vq_diffusion.pipeline_vq_diffusion import LearnedClassifierFreeSamplingEmbeddings from diffusers.utils import load_numpy, slow, torch_device from diffusers.utils.testing_utils import require_torch_gpu UpperCAmelCase_ = False class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @property def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" return 12 @property def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" return 12 @property def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" return 32 @property def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" torch.manual_seed(0 ) UpperCAmelCase__ = VQModel( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=3 , num_vq_embeddings=self.num_embed , vq_embed_dim=3 , ) return model @property def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" UpperCAmelCase__ = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) return tokenizer @property def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" torch.manual_seed(0 ) UpperCAmelCase__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) return CLIPTextModel(snake_case__ ) @property def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" torch.manual_seed(0 ) UpperCAmelCase__ = 12 UpperCAmelCase__ = 12 UpperCAmelCase__ = { '''attention_bias''': True, '''cross_attention_dim''': 32, '''attention_head_dim''': height * width, '''num_attention_heads''': 1, '''num_vector_embeds''': self.num_embed, '''num_embeds_ada_norm''': self.num_embeds_ada_norm, '''norm_num_groups''': 32, '''sample_size''': width, '''activation_fn''': '''geglu-approximate''', } UpperCAmelCase__ = TransformeraDModel(**snake_case__ ) return model def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" UpperCAmelCase__ = '''cpu''' UpperCAmelCase__ = self.dummy_vqvae UpperCAmelCase__ = self.dummy_text_encoder UpperCAmelCase__ = self.dummy_tokenizer UpperCAmelCase__ = self.dummy_transformer UpperCAmelCase__ = VQDiffusionScheduler(self.num_embed ) UpperCAmelCase__ = LearnedClassifierFreeSamplingEmbeddings(learnable=snake_case__ ) UpperCAmelCase__ = VQDiffusionPipeline( vqvae=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , transformer=snake_case__ , scheduler=snake_case__ , learned_classifier_free_sampling_embeddings=snake_case__ , ) UpperCAmelCase__ = pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase__ = '''teddy bear playing in the pool''' UpperCAmelCase__ = torch.Generator(device=snake_case__ ).manual_seed(0 ) UpperCAmelCase__ = pipe([prompt] , generator=snake_case__ , num_inference_steps=2 , output_type="""np""" ) UpperCAmelCase__ = output.images UpperCAmelCase__ = torch.Generator(device=snake_case__ ).manual_seed(0 ) UpperCAmelCase__ = pipe( [prompt] , generator=snake_case__ , output_type="""np""" , return_dict=snake_case__ , num_inference_steps=2 )[0] UpperCAmelCase__ = image[0, -3:, -3:, -1] UpperCAmelCase__ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 24, 24, 3) UpperCAmelCase__ = np.array([0.6551, 0.6168, 0.5008, 0.5676, 0.5659, 0.4295, 0.6073, 0.5599, 0.4992] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" UpperCAmelCase__ = '''cpu''' UpperCAmelCase__ = self.dummy_vqvae UpperCAmelCase__ = self.dummy_text_encoder UpperCAmelCase__ = self.dummy_tokenizer UpperCAmelCase__ = self.dummy_transformer UpperCAmelCase__ = VQDiffusionScheduler(self.num_embed ) UpperCAmelCase__ = LearnedClassifierFreeSamplingEmbeddings( learnable=snake_case__ , hidden_size=self.text_embedder_hidden_size , length=tokenizer.model_max_length ) UpperCAmelCase__ = VQDiffusionPipeline( vqvae=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , transformer=snake_case__ , scheduler=snake_case__ , learned_classifier_free_sampling_embeddings=snake_case__ , ) UpperCAmelCase__ = pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase__ = '''teddy bear playing in the pool''' UpperCAmelCase__ = torch.Generator(device=snake_case__ ).manual_seed(0 ) UpperCAmelCase__ = pipe([prompt] , generator=snake_case__ , num_inference_steps=2 , output_type="""np""" ) UpperCAmelCase__ = output.images UpperCAmelCase__ = torch.Generator(device=snake_case__ ).manual_seed(0 ) UpperCAmelCase__ = pipe( [prompt] , generator=snake_case__ , output_type="""np""" , return_dict=snake_case__ , num_inference_steps=2 )[0] UpperCAmelCase__ = image[0, -3:, -3:, -1] UpperCAmelCase__ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 24, 24, 3) UpperCAmelCase__ = np.array([0.6693, 0.6075, 0.4959, 0.5701, 0.5583, 0.4333, 0.6171, 0.5684, 0.4988] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 2.0 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" UpperCAmelCase__ = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/vq_diffusion/teddy_bear_pool_classifier_free_sampling.npy""" ) UpperCAmelCase__ = VQDiffusionPipeline.from_pretrained("""microsoft/vq-diffusion-ithq""" ) UpperCAmelCase__ = pipeline.to(snake_case__ ) pipeline.set_progress_bar_config(disable=snake_case__ ) # requires GPU generator for gumbel softmax # don't use GPU generator in tests though UpperCAmelCase__ = torch.Generator(device=snake_case__ ).manual_seed(0 ) UpperCAmelCase__ = pipeline( """teddy bear playing in the pool""" , num_images_per_prompt=1 , generator=snake_case__ , output_type="""np""" , ) UpperCAmelCase__ = output.images[0] assert image.shape == (2_56, 2_56, 3) assert np.abs(expected_image - image ).max() < 2.0
346
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class __snake_case ( unittest.TestCase ): @slow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Any =FlaxMTaForConditionalGeneration.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : Tuple =AutoTokenizer.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : List[str] =tokenizer('''Hello there''' , return_tensors='''np''' ).input_ids UpperCAmelCase : List[Any] =tokenizer('''Hi I am''' , return_tensors='''np''' ).input_ids UpperCAmelCase : Union[str, Any] =shift_tokens_right(snake_case__ , model.config.pad_token_id , model.config.decoder_start_token_id ) UpperCAmelCase : List[str] =model(snake_case__ , decoder_input_ids=snake_case__ ).logits UpperCAmelCase : Any =optax.softmax_cross_entropy(snake_case__ , onehot(snake_case__ , logits.shape[-1] ) ).mean() UpperCAmelCase : Union[str, Any] =-(labels.shape[-1] * loss.item()) UpperCAmelCase : List[str] =-84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
348
0
import warnings from ...configuration_utils import PretrainedConfig from ...utils import logging __magic_name__: Optional[Any] = logging.get_logger(__name__) __magic_name__: Optional[int] = { "RUCAIBox/mvp": "https://huggingface.co/RUCAIBox/mvp/resolve/main/config.json", } class snake_case__ ( lowerCamelCase__ ): lowercase__ : Any = """mvp""" lowercase__ : Any = ["""past_key_values"""] lowercase__ : List[Any] = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self , lowerCAmelCase__=5_02_67 , lowerCAmelCase__=10_24 , lowerCAmelCase__=12 , lowerCAmelCase__=40_96 , lowerCAmelCase__=16 , lowerCAmelCase__=12 , lowerCAmelCase__=40_96 , lowerCAmelCase__=16 , lowerCAmelCase__=0.0 , lowerCAmelCase__=0.0 , lowerCAmelCase__="gelu" , lowerCAmelCase__=10_24 , lowerCAmelCase__=0.1 , lowerCAmelCase__=0.0 , lowerCAmelCase__=0.0 , lowerCAmelCase__=0.0_2 , lowerCAmelCase__=0.0 , lowerCAmelCase__=False , lowerCAmelCase__=True , lowerCAmelCase__=1 , lowerCAmelCase__=0 , lowerCAmelCase__=2 , lowerCAmelCase__=True , lowerCAmelCase__=2 , lowerCAmelCase__=2 , lowerCAmelCase__=False , lowerCAmelCase__=1_00 , lowerCAmelCase__=8_00 , **lowerCAmelCase__ , ) -> Optional[Any]: __magic_name__ : Optional[int] = vocab_size __magic_name__ : List[Any] = max_position_embeddings __magic_name__ : List[str] = d_model __magic_name__ : List[str] = encoder_ffn_dim __magic_name__ : int = encoder_layers __magic_name__ : Any = encoder_attention_heads __magic_name__ : List[Any] = decoder_ffn_dim __magic_name__ : Optional[Any] = decoder_layers __magic_name__ : int = decoder_attention_heads __magic_name__ : Dict = dropout __magic_name__ : List[str] = attention_dropout __magic_name__ : List[str] = activation_dropout __magic_name__ : int = activation_function __magic_name__ : int = init_std __magic_name__ : str = encoder_layerdrop __magic_name__ : int = decoder_layerdrop __magic_name__ : Union[str, Any] = classifier_dropout __magic_name__ : Optional[Any] = use_cache __magic_name__ : Tuple = encoder_layers __magic_name__ : Union[str, Any] = scale_embedding # scale factor will be sqrt(d_model) if True __magic_name__ : int = use_prompt __magic_name__ : Union[str, Any] = prompt_length __magic_name__ : Union[str, Any] = prompt_mid_dim super().__init__( pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ , is_encoder_decoder=snake_case__ , decoder_start_token_id=snake_case__ , forced_eos_token_id=snake_case__ , **snake_case__ , ) if self.forced_bos_token_id is None and kwargs.get("""force_bos_token_to_be_generated""" , snake_case__ ): __magic_name__ : Dict = self.bos_token_id warnings.warn( F'Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. ' """The config can simply be saved and uploaded again to be fixed.""" )
342
import unittest import numpy as np from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __snake_case ( lowerCamelCase__ , unittest.TestCase ): # FIXME: add fast tests pass @nightly @require_onnxruntime @require_torch_gpu class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : List[Any] =ort.SessionOptions() UpperCAmelCase : Optional[int] =False return options def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : int =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Optional[Any] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Dict ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : Any =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=10 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : Optional[int] =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Tuple =np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Tuple =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , subfolder='''scheduler''' , revision='''onnx''' ) UpperCAmelCase : int =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , scheduler=snake_case__ , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Union[str, Any] ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : str =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=20 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : int =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Union[str, Any] =np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3
348
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) SCREAMING_SNAKE_CASE__ = { "configuration_clip": [ "CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "CLIPConfig", "CLIPOnnxConfig", "CLIPTextConfig", "CLIPVisionConfig", ], "processing_clip": ["CLIPProcessor"], "tokenization_clip": ["CLIPTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ["CLIPTokenizerFast"] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ["CLIPFeatureExtractor"] SCREAMING_SNAKE_CASE__ = ["CLIPImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ "CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "CLIPModel", "CLIPPreTrainedModel", "CLIPTextModel", "CLIPTextModelWithProjection", "CLIPVisionModel", "CLIPVisionModelWithProjection", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ "TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "TFCLIPModel", "TFCLIPPreTrainedModel", "TFCLIPTextModel", "TFCLIPVisionModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ "FlaxCLIPModel", "FlaxCLIPPreTrainedModel", "FlaxCLIPTextModel", "FlaxCLIPTextPreTrainedModel", "FlaxCLIPVisionModel", "FlaxCLIPVisionPreTrainedModel", ] if TYPE_CHECKING: from .configuration_clip import ( CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPConfig, CLIPOnnxConfig, CLIPTextConfig, CLIPVisionConfig, ) from .processing_clip import CLIPProcessor from .tokenization_clip import CLIPTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_clip_fast import CLIPTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clip import CLIPFeatureExtractor from .image_processing_clip import CLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clip import ( CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPModel, CLIPPreTrainedModel, CLIPTextModel, CLIPTextModelWithProjection, CLIPVisionModel, CLIPVisionModelWithProjection, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_clip import ( TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFCLIPModel, TFCLIPPreTrainedModel, TFCLIPTextModel, TFCLIPVisionModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_clip import ( FlaxCLIPModel, FlaxCLIPPreTrainedModel, FlaxCLIPTextModel, FlaxCLIPTextPreTrainedModel, FlaxCLIPVisionModel, FlaxCLIPVisionPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
46
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def lowerCAmelCase_ ( )-> int: '''simple docstring''' UpperCAmelCase : str ={ '''repo_name''': ['''test_repo1''', '''test_repo2''', '''test_repo3'''], '''path''': ['''test_1.py''', '''test_2.py''', '''unit_test.py'''], '''content''': ['''a ''' * 20, '''a ''' * 30, '''b ''' * 7], } UpperCAmelCase : Union[str, Any] =Dataset.from_dict(__lowerCAmelCase ) return dataset class __snake_case ( lowerCamelCase__ ): def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[str] =get_dataset() UpperCAmelCase : Optional[int] =make_duplicate_clusters(snake_case__ , 0.85 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : str =get_dataset() UpperCAmelCase , UpperCAmelCase : Tuple =deduplicate_dataset(snake_case__ ) self.assertEqual(len(snake_case__ ) , 2 ) print(snake_case__ ) self.assertEqual(duplicate_clusters[0][0]['''copies'''] , 2 ) self.assertEqual(duplicate_clusters[0][0]['''is_extreme'''] , snake_case__ )
348
0
from ...configuration_utils import PretrainedConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { '''uw-madison/mra-base-512-4''': '''https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json''', } class A (lowerCamelCase__ ): '''simple docstring''' __lowerCamelCase : Dict = """mra""" def __init__( self : int , __lowerCAmelCase : Tuple=5_02_65 , __lowerCAmelCase : Optional[int]=7_68 , __lowerCAmelCase : List[str]=12 , __lowerCAmelCase : Any=12 , __lowerCAmelCase : Tuple=30_72 , __lowerCAmelCase : List[str]="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : str=0.1 , __lowerCAmelCase : Tuple=5_12 , __lowerCAmelCase : str=1 , __lowerCAmelCase : int=0.0_2 , __lowerCAmelCase : List[str]=1e-5 , __lowerCAmelCase : Union[str, Any]="absolute" , __lowerCAmelCase : Dict=4 , __lowerCAmelCase : Any="full" , __lowerCAmelCase : Optional[int]=0 , __lowerCAmelCase : List[str]=0 , __lowerCAmelCase : Union[str, Any]=1 , __lowerCAmelCase : Optional[Any]=0 , __lowerCAmelCase : Any=2 , **__lowerCAmelCase : int , ) -> List[Any]: """simple docstring""" super().__init__(pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ , **snake_case__ ) A__ = vocab_size A__ = max_position_embeddings A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = initializer_range A__ = type_vocab_size A__ = layer_norm_eps A__ = position_embedding_type A__ = block_per_row A__ = approx_mode A__ = initial_prior_first_n_blocks A__ = initial_prior_diagonal_n_blocks
274
from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput __snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name class __snake_case ( lowerCamelCase__ , lowerCamelCase__ ): @register_to_config def __init__( self , snake_case__ , snake_case__ = None , snake_case__ = None ) -> str: '''simple docstring''' super().__init__() UpperCAmelCase : Optional[Any] =learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" UpperCAmelCase : Any =torch.zeros(snake_case__ , snake_case__ ) else: UpperCAmelCase : Union[str, Any] =None UpperCAmelCase : Optional[int] =torch.nn.Parameter(snake_case__ ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : VQModel __lowerCamelCase : CLIPTextModel __lowerCamelCase : CLIPTokenizer __lowerCamelCase : TransformeraDModel __lowerCamelCase : LearnedClassifierFreeSamplingEmbeddings __lowerCamelCase : VQDiffusionScheduler def __init__( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> int: '''simple docstring''' super().__init__() self.register_modules( vqvae=snake_case__ , transformer=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , scheduler=snake_case__ , learned_classifier_free_sampling_embeddings=snake_case__ , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : int =len(snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else 1 # get prompt text embeddings UpperCAmelCase : Optional[int] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) UpperCAmelCase : int =text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCAmelCase : List[str] =self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' f''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) UpperCAmelCase : Optional[Any] =text_input_ids[:, : self.tokenizer.model_max_length] UpperCAmelCase : List[Any] =self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 UpperCAmelCase : int =prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate text embeddings for each generation per prompt UpperCAmelCase : int =prompt_embeds.repeat_interleave(snake_case__ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: UpperCAmelCase : Optional[int] =self.learned_classifier_free_sampling_embeddings.embeddings UpperCAmelCase : str =negative_prompt_embeds.unsqueeze(0 ).repeat(snake_case__ , 1 , 1 ) else: UpperCAmelCase : str =[''''''] * batch_size UpperCAmelCase : Tuple =text_input_ids.shape[-1] UpperCAmelCase : Optional[Any] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=snake_case__ , truncation=snake_case__ , return_tensors='''pt''' , ) UpperCAmelCase : Optional[Any] =self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings UpperCAmelCase : Optional[int] =negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCAmelCase : Optional[Any] =negative_prompt_embeds.shape[1] UpperCAmelCase : Union[str, Any] =negative_prompt_embeds.repeat(1 , snake_case__ , 1 ) UpperCAmelCase : Optional[Any] =negative_prompt_embeds.view(batch_size * num_images_per_prompt , snake_case__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCAmelCase : int =torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , snake_case__ , snake_case__ = 100 , snake_case__ = 5.0 , snake_case__ = 1.0 , snake_case__ = 1 , snake_case__ = None , snake_case__ = None , snake_case__ = "pil" , snake_case__ = True , snake_case__ = None , snake_case__ = 1 , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =1 elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Tuple =len(snake_case__ ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(snake_case__ )}''' ) UpperCAmelCase : Tuple =batch_size * num_images_per_prompt UpperCAmelCase : List[str] =guidance_scale > 1.0 UpperCAmelCase : List[Any] =self._encode_prompt(snake_case__ , snake_case__ , snake_case__ ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case__ , snake_case__ ) or callback_steps <= 0) ): raise ValueError( f'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' f''' {type(snake_case__ )}.''' ) # get the initial completely masked latents unless the user supplied it UpperCAmelCase : int =(batch_size, self.transformer.num_latent_pixels) if latents is None: UpperCAmelCase : Union[str, Any] =self.transformer.num_vector_embeds - 1 UpperCAmelCase : str =torch.full(snake_case__ , snake_case__ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( '''Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,''' f''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) UpperCAmelCase : Any =latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(snake_case__ , device=self.device ) UpperCAmelCase : Any =self.scheduler.timesteps.to(self.device ) UpperCAmelCase : Optional[int] =latents for i, t in enumerate(self.progress_bar(snake_case__ ) ): # expand the sample if we are doing classifier free guidance UpperCAmelCase : Optional[Any] =torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` UpperCAmelCase : Optional[int] =self.transformer(snake_case__ , encoder_hidden_states=snake_case__ , timestep=snake_case__ ).sample if do_classifier_free_guidance: UpperCAmelCase , UpperCAmelCase : str =model_output.chunk(2 ) UpperCAmelCase : Optional[int] =model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(snake_case__ , dim=1 , keepdim=snake_case__ ) UpperCAmelCase : Tuple =self.truncate(snake_case__ , snake_case__ ) # remove `log(0)`'s (`-inf`s) UpperCAmelCase : Optional[Any] =model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 UpperCAmelCase : int =self.scheduler.step(snake_case__ , timestep=snake_case__ , sample=snake_case__ , generator=snake_case__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =self.vqvae.config.vq_embed_dim UpperCAmelCase : Optional[Any] =(batch_size, self.transformer.height, self.transformer.width, embedding_channels) UpperCAmelCase : Dict =self.vqvae.quantize.get_codebook_entry(snake_case__ , shape=snake_case__ ) UpperCAmelCase : Tuple =self.vqvae.decode(snake_case__ , force_not_quantize=snake_case__ ).sample UpperCAmelCase : Union[str, Any] =(image / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase : Any =image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase : List[str] =self.numpy_to_pil(snake_case__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> torch.FloatTensor: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : int =torch.sort(snake_case__ , 1 , descending=snake_case__ ) UpperCAmelCase : Union[str, Any] =torch.exp(snake_case__ ) UpperCAmelCase : Union[str, Any] =sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out UpperCAmelCase : Optional[Any] =torch.full_like(keep_mask[:, 0:1, :] , snake_case__ ) UpperCAmelCase : Tuple =torch.cat((all_true, keep_mask) , dim=1 ) UpperCAmelCase : int =keep_mask[:, :-1, :] UpperCAmelCase : int =keep_mask.gather(1 , indices.argsort(1 ) ) UpperCAmelCase : Dict =log_p_x_0.clone() UpperCAmelCase : List[Any] =-torch.inf # -inf = log(0) return rv
348
0
from ..utils import DummyObject, requires_backends class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Dict = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : List[Any] = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Optional[Any] = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Tuple = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : int = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Tuple = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : List[Any] = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : int = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Optional[int] = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Union[str, Any] = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Optional[int] = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Tuple = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : List[Any] = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : List[str] = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : str = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Dict = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Any = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Dict = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Any = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Optional[Any] = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : List[str] = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Any = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : List[str] = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Union[str, Any] = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Any = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Optional[int] = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : List[Any] = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : str = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : int = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Tuple = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] ) class A_ ( metaclass=lowerCamelCase__ ): '''simple docstring''' _UpperCamelCase : Tuple = ["""sentencepiece"""] def __init__( self , *snake_case , **snake_case ): requires_backends(self , ['sentencepiece'] )
195
import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase : Any =UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =self.dummy_uncond_unet UpperCAmelCase : Optional[int] =KarrasVeScheduler() UpperCAmelCase : List[Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : List[str] =torch.manual_seed(0 ) UpperCAmelCase : List[str] =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : str =torch.manual_seed(0 ) UpperCAmelCase : str =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' , return_dict=snake_case__ )[0] UpperCAmelCase : Any =image[0, -3:, -3:, -1] UpperCAmelCase : List[str] =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCAmelCase : int =np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class __snake_case ( unittest.TestCase ): def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Tuple ='''google/ncsnpp-celebahq-256''' UpperCAmelCase : int =UNetaDModel.from_pretrained(snake_case__ ) UpperCAmelCase : Dict =KarrasVeScheduler() UpperCAmelCase : Union[str, Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Any =torch.manual_seed(0 ) UpperCAmelCase : Tuple =pipe(num_inference_steps=20 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : Optional[int] =image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) UpperCAmelCase : Tuple =np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
348
0
'''simple docstring''' import requests __A ='https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=' def _UpperCamelCase ( UpperCamelCase__ ): UpperCAmelCase__ : List[str] = requests.get(_NEWS_API + bbc_news_api_key ).json() # each article in the list is a dict for i, article in enumerate(bbc_news_page["""articles"""] , 1 ): print(f'''{i}.) {article['title']}''' ) if __name__ == "__main__": fetch_bbc_news(bbc_news_api_key='<Your BBC News API key goes here>')
163
import qiskit def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> qiskit.result.counts.Counts: '''simple docstring''' UpperCAmelCase : Union[str, Any] =qiskit.Aer.get_backend('''aer_simulator''' ) UpperCAmelCase : List[str] =qiskit.QuantumCircuit(4 , 2 ) # encode inputs in qubits 0 and 1 if bita == 1: qc_ha.x(0 ) if bita == 1: qc_ha.x(1 ) qc_ha.barrier() # use cnots to write XOR of the inputs on qubit2 qc_ha.cx(0 , 2 ) qc_ha.cx(1 , 2 ) # use ccx / toffoli gate to write AND of the inputs on qubit3 qc_ha.ccx(0 , 1 , 3 ) qc_ha.barrier() # extract outputs qc_ha.measure(2 , 0 ) # extract XOR value qc_ha.measure(3 , 1 ) # extract AND value # Execute the circuit on the qasm simulator UpperCAmelCase : Dict =qiskit.execute(__lowerCAmelCase , __lowerCAmelCase , shots=10_00 ) # Return the histogram data of the results of the experiment return job.result().get_counts(__lowerCAmelCase ) if __name__ == "__main__": __snake_case = half_adder(1, 1) print(f'Half Adder Output Qubit Counts: {counts}')
348
0
import unittest from transformers.models.xlm_prophetnet.tokenization_xlm_prophetnet import SPIECE_UNDERLINE, XLMProphetNetTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin SCREAMING_SNAKE_CASE :Dict = get_tests_dir('fixtures/test_sentencepiece.model') @require_sentencepiece class UpperCAmelCase ( lowerCamelCase__ , unittest.TestCase ): '''simple docstring''' snake_case_ = XLMProphetNetTokenizer snake_case_ = False snake_case_ = True def UpperCamelCase_ ( self : List[str] ): super().setUp() # We have a SentencePiece fixture for testing __A = XLMProphetNetTokenizer(snake_case__ ,keep_accents=snake_case__ ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCamelCase_ ( self : str ): __A = '''[PAD]''' __A = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(snake_case__ ) ,snake_case__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(snake_case__ ) ,snake_case__ ) def UpperCamelCase_ ( self : Dict ): __A = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] ,"[PAD]" ) self.assertEqual(vocab_keys[1] ,"[CLS]" ) self.assertEqual(vocab_keys[-1] ,"j" ) self.assertEqual(len(snake_case__ ) ,10_12 ) def UpperCamelCase_ ( self : Tuple ): self.assertEqual(self.get_tokenizer().vocab_size ,10_12 ) def UpperCamelCase_ ( self : Optional[int] ): __A = XLMProphetNetTokenizer(snake_case__ ,keep_accents=snake_case__ ) __A = tokenizer.tokenize("This is a test" ) self.assertListEqual(snake_case__ ,["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(snake_case__ ) ,[value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]] ,) __A = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( snake_case__ ,[ 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", "é", ".", ] ,) __A = tokenizer.convert_tokens_to_ids(snake_case__ ) self.assertListEqual( snake_case__ ,[ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, -9, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, -9, 4] ] ,) __A = tokenizer.convert_ids_to_tokens(snake_case__ ) self.assertListEqual( snake_case__ ,[ 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]", ".", ] ,) @cached_property def UpperCamelCase_ ( self : List[Any] ): return XLMProphetNetTokenizer.from_pretrained("microsoft/xprophetnet-large-wiki100-cased" ) @slow def UpperCamelCase_ ( self : Union[str, Any] ): __A = '''Hello World!''' __A = [3_53_89, 66_72, 49, 2] self.assertListEqual(snake_case__ ,self.big_tokenizer.encode(snake_case__ ) ) @slow def UpperCamelCase_ ( self : Optional[Any] ): __A = {'''input_ids''': [[1_10_73, 8_27_83, 18, 26, 8_27_83, 5_49, 5_15_40, 2_48, 1_72_09, 13_01, 2_17, 20, 21_51_86, 13_25, 1_47, 1_72_09, 13_01, 2_17, 20, 5_63_70, 53, 12_20_20, 20, 1_64_77, 27, 8_73_55, 45_48, 20, 47_28, 7_83_92, 17, 15_99_69, 18, 26, 2_44_91, 6_29, 15, 5_38, 2_27_04, 54_39, 15, 27_88, 2_44_91, 98_85, 15, 4_35_34, 6_05, 15, 8_14, 1_84_03, 3_32_00, 29, 15, 4_35_34, 2_44_58, 1_24_10, 1_11, 2_49_66, 8_36_69, 96_37, 14_40_68, 26, 8_50, 2_23_46, 27, 1_47, 2_49_66, 8_36_69, 8_34_90, 26, 3_91_13, 7_35, 27, 6_89, 6_56, 28_00, 13_39, 46_00, 53, 12_20_20, 11_57_85, 34, 8_16, 13_39, 4_68_87, 18, 1_47, 5_39_05, 19_51, 4_22_38, 4_11_70, 1_77_32, 8_34, 4_36, 15, 2_75_23, 9_87_33, 2_17, 1_47, 55_42, 49_81, 9_30, 1_73_47, 16, 2], [2_00_91, 6_29, 94, 8_27_86, 58, 4_90, 20, 15_28, 84, 5_39_05, 3_44, 8_05_92, 11_01_28, 1_88_22, 52_67, 13_06, 62, 15_25_37, 3_08, 79_97, 4_01, 12_44_27, 5_49, 3_54_42, 2_25, 1_09, 1_50_55, 2_57_48, 1_47, 71_19, 4_37_12, 34, 7_67, 13_53_66, 18, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [5_92, 6_37_84, 11_94_66, 17, 14_78_08, 8_82_14, 18, 6_56, 81, 32, 32_96, 1_02_80, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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=snake_case__ ,model_name="microsoft/xprophetnet-large-wiki100-cased" ,revision="1acad1643ddd54a44df6a1b797ada8373685d90e" ,)
15
from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class __snake_case : __lowerCamelCase : str = BlenderbotConfig __lowerCamelCase : Optional[Any] = {} __lowerCamelCase : Optional[int] = """gelu""" def __init__( self , snake_case__ , snake_case__=13 , snake_case__=7 , snake_case__=True , snake_case__=False , snake_case__=99 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=20 , snake_case__=2 , snake_case__=1 , snake_case__=0 , ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : Dict =seq_length UpperCAmelCase : Optional[Any] =is_training UpperCAmelCase : List[str] =use_labels UpperCAmelCase : List[Any] =vocab_size UpperCAmelCase : Optional[int] =hidden_size UpperCAmelCase : Tuple =num_hidden_layers UpperCAmelCase : Any =num_attention_heads UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : str =hidden_dropout_prob UpperCAmelCase : Optional[int] =attention_probs_dropout_prob UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : List[Any] =eos_token_id UpperCAmelCase : Optional[int] =pad_token_id UpperCAmelCase : Tuple =bos_token_id def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[Any] =ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) UpperCAmelCase : List[Any] =tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) UpperCAmelCase : Tuple =tf.concat([input_ids, eos_tensor] , axis=1 ) UpperCAmelCase : str =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : Optional[Any] =self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) UpperCAmelCase : List[str] =prepare_blenderbot_inputs_dict(snake_case__ , snake_case__ , snake_case__ ) return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =TFBlenderbotModel(config=snake_case__ ).get_decoder() UpperCAmelCase : Any =inputs_dict['''input_ids'''] UpperCAmelCase : str =input_ids[:1, :] UpperCAmelCase : Tuple =inputs_dict['''attention_mask'''][:1, :] UpperCAmelCase : Tuple =inputs_dict['''head_mask'''] UpperCAmelCase : List[Any] =1 # first forward pass UpperCAmelCase : List[str] =model(snake_case__ , attention_mask=snake_case__ , head_mask=snake_case__ , use_cache=snake_case__ ) UpperCAmelCase , UpperCAmelCase : str =outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase : Union[str, Any] =ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCAmelCase : List[Any] =tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and UpperCAmelCase : Tuple =tf.concat([input_ids, next_tokens] , axis=-1 ) UpperCAmelCase : int =tf.concat([attention_mask, next_attn_mask] , axis=-1 ) UpperCAmelCase : Optional[int] =model(snake_case__ , attention_mask=snake_case__ )[0] UpperCAmelCase : str =model(snake_case__ , attention_mask=snake_case__ , past_key_values=snake_case__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice UpperCAmelCase : List[Any] =int(ids_tensor((1,) , output_from_past.shape[-1] ) ) UpperCAmelCase : List[Any] =output_from_no_past[:, -3:, random_slice_idx] UpperCAmelCase : Dict =output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(snake_case__ , snake_case__ , rtol=1e-3 ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , )-> str: '''simple docstring''' if attention_mask is None: UpperCAmelCase : int =tf.cast(tf.math.not_equal(__lowerCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase : Tuple =tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase : str =tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase : Union[str, Any] =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase : int =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : List[str] = (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __lowerCamelCase : Dict = (TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __lowerCamelCase : Dict = ( { """conversational""": TFBlenderbotForConditionalGeneration, """feature-extraction""": TFBlenderbotModel, """summarization""": TFBlenderbotForConditionalGeneration, """text2text-generation""": TFBlenderbotForConditionalGeneration, """translation""": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __lowerCamelCase : Union[str, Any] = True __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Union[str, Any] = False def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : List[str] =TFBlenderbotModelTester(self ) UpperCAmelCase : List[Any] =ConfigTester(self , config_class=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*snake_case__ ) @require_tokenizers @require_tf class __snake_case ( unittest.TestCase ): __lowerCamelCase : List[str] = ["""My friends are cool but they eat too many carbs."""] __lowerCamelCase : Dict = """facebook/blenderbot-400M-distill""" @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[int] =self.tokenizer(self.src_text , return_tensors='''tf''' ) UpperCAmelCase : Optional[int] =self.model.generate( model_inputs.input_ids , ) UpperCAmelCase : str =self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=snake_case__ )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
348
0
def a( A : Optional[int] ) -> list: """simple docstring""" a = int(__lowerCAmelCase ) if n_element < 1: a = ValueError("a should be a positive number" ) raise my_error a = [1] a = (0, 0, 0) a = 1 while index < n_element: while hamming_list[i] * 2 <= hamming_list[-1]: i += 1 while hamming_list[j] * 3 <= hamming_list[-1]: j += 1 while hamming_list[k] * 5 <= hamming_list[-1]: k += 1 hamming_list.append( min(hamming_list[i] * 2 , hamming_list[j] * 3 , hamming_list[k] * 5 ) ) index += 1 return hamming_list if __name__ == "__main__": _lowercase: Dict = input("Enter the last number (nth term) of the Hamming Number Series: ") print("Formula of Hamming Number Series => 2^i * 3^j * 5^k") _lowercase: List[str] = hamming(int(n)) print("-----------------------------------------------------") print(F"""The list with nth numbers is: {hamming_numbers}""") print("-----------------------------------------------------")
227
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { '''asapp/sew-d-tiny-100k''': '''https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json''', # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[Any] = """sew-d""" def __init__( self , snake_case__=32 , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=2 , snake_case__=512 , snake_case__=256 , snake_case__=True , snake_case__=True , snake_case__=("p2c", "c2p") , snake_case__="layer_norm" , snake_case__="gelu_python" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.0 , snake_case__=0.1 , snake_case__=0.02 , snake_case__=1e-7 , snake_case__=1e-5 , snake_case__="group" , snake_case__="gelu" , snake_case__=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , snake_case__=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , snake_case__=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , snake_case__=False , snake_case__=128 , snake_case__=16 , snake_case__=True , snake_case__=0.05 , snake_case__=10 , snake_case__=2 , snake_case__=0.0 , snake_case__=10 , snake_case__=0 , snake_case__="mean" , snake_case__=False , snake_case__=False , snake_case__=256 , snake_case__=0 , snake_case__=1 , snake_case__=2 , **snake_case__ , ) -> int: '''simple docstring''' super().__init__(**snake_case__ , pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ ) UpperCAmelCase : Union[str, Any] =hidden_size UpperCAmelCase : Union[str, Any] =feat_extract_norm UpperCAmelCase : Optional[Any] =feat_extract_activation UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : int =list(snake_case__ ) UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : str =conv_bias UpperCAmelCase : Tuple =num_conv_pos_embeddings UpperCAmelCase : Dict =num_conv_pos_embedding_groups UpperCAmelCase : str =len(self.conv_dim ) UpperCAmelCase : Dict =num_hidden_layers UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : List[Any] =squeeze_factor UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : int =position_buckets UpperCAmelCase : Optional[int] =share_att_key UpperCAmelCase : Optional[int] =relative_attention UpperCAmelCase : Tuple =norm_rel_ebd UpperCAmelCase : List[Any] =list(snake_case__ ) UpperCAmelCase : Dict =hidden_act UpperCAmelCase : Optional[int] =num_attention_heads UpperCAmelCase : Any =hidden_dropout UpperCAmelCase : str =attention_dropout UpperCAmelCase : Union[str, Any] =activation_dropout UpperCAmelCase : str =feat_proj_dropout UpperCAmelCase : Union[str, Any] =final_dropout UpperCAmelCase : Optional[int] =layer_norm_eps UpperCAmelCase : str =feature_layer_norm_eps UpperCAmelCase : str =initializer_range UpperCAmelCase : Any =vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect.''' '''It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,''' f'''but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)''' f'''= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCAmelCase : Union[str, Any] =apply_spec_augment UpperCAmelCase : Optional[Any] =mask_time_prob UpperCAmelCase : Tuple =mask_time_length UpperCAmelCase : str =mask_time_min_masks UpperCAmelCase : Optional[int] =mask_feature_prob UpperCAmelCase : Optional[Any] =mask_feature_length UpperCAmelCase : List[Any] =mask_feature_min_masks # ctc loss UpperCAmelCase : str =ctc_loss_reduction UpperCAmelCase : Optional[int] =ctc_zero_infinity # sequence classification UpperCAmelCase : Union[str, Any] =use_weighted_layer_sum UpperCAmelCase : int =classifier_proj_size @property def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
348
0
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTConfig, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) def a ( A__ : List[str] ) -> Tuple: """simple docstring""" _lowercase =MobileViTConfig() # size of the architecture if "mobilevit_s" in mobilevit_name: _lowercase =[144, 192, 240] _lowercase =[16, 32, 64, 96, 128, 160, 640] elif "mobilevit_xs" in mobilevit_name: _lowercase =[96, 120, 144] _lowercase =[16, 32, 48, 64, 80, 96, 384] elif "mobilevit_xxs" in mobilevit_name: _lowercase =[64, 80, 96] _lowercase =[16, 16, 24, 48, 64, 80, 320] _lowercase =0.05 _lowercase =2.0 if mobilevit_name.startswith('deeplabv3_' ): _lowercase =512 _lowercase =16 _lowercase =21 _lowercase ='''pascal-voc-id2label.json''' else: _lowercase =1000 _lowercase ='''imagenet-1k-id2label.json''' _lowercase ='''huggingface/label-files''' _lowercase =json.load(open(hf_hub_download(__lowerCAmelCase , __lowerCAmelCase , repo_type='dataset' ) , 'r' ) ) _lowercase ={int(__lowerCAmelCase ): v for k, v in idalabel.items()} _lowercase =idalabel _lowercase ={v: k for k, v in idalabel.items()} return config def a ( A__ : int , A__ : List[Any]=False ) -> Optional[Any]: """simple docstring""" for i in range(1 , 6 ): if F'''layer_{i}.''' in name: _lowercase =name.replace(F'''layer_{i}.''' , F'''encoder.layer.{i - 1}.''' ) if "conv_1." in name: _lowercase =name.replace('conv_1.' , 'conv_stem.' ) if ".block." in name: _lowercase =name.replace('.block.' , '.' ) if "exp_1x1" in name: _lowercase =name.replace('exp_1x1' , 'expand_1x1' ) if "red_1x1" in name: _lowercase =name.replace('red_1x1' , 'reduce_1x1' ) if ".local_rep.conv_3x3." in name: _lowercase =name.replace('.local_rep.conv_3x3.' , '.conv_kxk.' ) if ".local_rep.conv_1x1." in name: _lowercase =name.replace('.local_rep.conv_1x1.' , '.conv_1x1.' ) if ".norm." in name: _lowercase =name.replace('.norm.' , '.normalization.' ) if ".conv." in name: _lowercase =name.replace('.conv.' , '.convolution.' ) if ".conv_proj." in name: _lowercase =name.replace('.conv_proj.' , '.conv_projection.' ) for i in range(0 , 2 ): for j in range(0 , 4 ): if F'''.{i}.{j}.''' in name: _lowercase =name.replace(F'''.{i}.{j}.''' , F'''.{i}.layer.{j}.''' ) for i in range(2 , 6 ): for j in range(0 , 4 ): if F'''.{i}.{j}.''' in name: _lowercase =name.replace(F'''.{i}.{j}.''' , F'''.{i}.''' ) if "expand_1x1" in name: _lowercase =name.replace('expand_1x1' , 'downsampling_layer.expand_1x1' ) if "conv_3x3" in name: _lowercase =name.replace('conv_3x3' , 'downsampling_layer.conv_3x3' ) if "reduce_1x1" in name: _lowercase =name.replace('reduce_1x1' , 'downsampling_layer.reduce_1x1' ) for i in range(2 , 5 ): if F'''.global_rep.{i}.weight''' in name: _lowercase =name.replace(F'''.global_rep.{i}.weight''' , '.layernorm.weight' ) if F'''.global_rep.{i}.bias''' in name: _lowercase =name.replace(F'''.global_rep.{i}.bias''' , '.layernorm.bias' ) if ".global_rep." in name: _lowercase =name.replace('.global_rep.' , '.transformer.' ) if ".pre_norm_mha.0." in name: _lowercase =name.replace('.pre_norm_mha.0.' , '.layernorm_before.' ) if ".pre_norm_mha.1.out_proj." in name: _lowercase =name.replace('.pre_norm_mha.1.out_proj.' , '.attention.output.dense.' ) if ".pre_norm_ffn.0." in name: _lowercase =name.replace('.pre_norm_ffn.0.' , '.layernorm_after.' ) if ".pre_norm_ffn.1." in name: _lowercase =name.replace('.pre_norm_ffn.1.' , '.intermediate.dense.' ) if ".pre_norm_ffn.4." in name: _lowercase =name.replace('.pre_norm_ffn.4.' , '.output.dense.' ) if ".transformer." in name: _lowercase =name.replace('.transformer.' , '.transformer.layer.' ) if ".aspp_layer." in name: _lowercase =name.replace('.aspp_layer.' , '.' ) if ".aspp_pool." in name: _lowercase =name.replace('.aspp_pool.' , '.' ) if "seg_head." in name: _lowercase =name.replace('seg_head.' , 'segmentation_head.' ) if "segmentation_head.classifier.classifier." in name: _lowercase =name.replace('segmentation_head.classifier.classifier.' , 'segmentation_head.classifier.' ) if "classifier.fc." in name: _lowercase =name.replace('classifier.fc.' , 'classifier.' ) elif (not base_model) and ("segmentation_head." not in name): _lowercase ='''mobilevit.''' + name return name def a ( A__ : Optional[int] , A__ : Tuple , A__ : Any=False ) -> List[Any]: """simple docstring""" if base_model: _lowercase ='''''' else: _lowercase ='''mobilevit.''' for key in orig_state_dict.copy().keys(): _lowercase =orig_state_dict.pop(__lowerCAmelCase ) if key[:8] == "encoder.": _lowercase =key[8:] if "qkv" in key: _lowercase =key.split('.' ) _lowercase =int(key_split[0][6:] ) - 1 _lowercase =int(key_split[3] ) _lowercase =model.get_submodule(F'''{model_prefix}encoder.layer.{layer_num}''' ) _lowercase =layer.transformer.layer[transformer_num].attention.attention.all_head_size _lowercase =( F'''{model_prefix}encoder.layer.{layer_num}.transformer.layer.{transformer_num}.attention.attention.''' ) if "weight" in key: _lowercase =val[:dim, :] _lowercase =val[dim : dim * 2, :] _lowercase =val[-dim:, :] else: _lowercase =val[:dim] _lowercase =val[dim : dim * 2] _lowercase =val[-dim:] else: _lowercase =val return orig_state_dict def a ( ) -> Optional[int]: """simple docstring""" _lowercase ='''http://images.cocodataset.org/val2017/000000039769.jpg''' _lowercase =Image.open(requests.get(__lowerCAmelCase , stream=__lowerCAmelCase ).raw ) return im @torch.no_grad() def a ( A__ : Tuple , A__ : Union[str, Any] , A__ : int , A__ : int=False ) -> str: """simple docstring""" _lowercase =get_mobilevit_config(__lowerCAmelCase ) # load original state_dict _lowercase =torch.load(__lowerCAmelCase , map_location='cpu' ) # load 🤗 model if mobilevit_name.startswith('deeplabv3_' ): _lowercase =MobileViTForSemanticSegmentation(__lowerCAmelCase ).eval() else: _lowercase =MobileViTForImageClassification(__lowerCAmelCase ).eval() _lowercase =convert_state_dict(__lowerCAmelCase , __lowerCAmelCase ) model.load_state_dict(__lowerCAmelCase ) # Check outputs on an image, prepared by MobileViTImageProcessor _lowercase =MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) _lowercase =image_processor(images=prepare_img() , return_tensors='pt' ) _lowercase =model(**__lowerCAmelCase ) _lowercase =outputs.logits if mobilevit_name.startswith('deeplabv3_' ): assert logits.shape == (1, 21, 32, 32) if mobilevit_name == "deeplabv3_mobilevit_s": _lowercase =torch.tensor( [ [[6.2065, 6.1292, 6.2070], [6.1079, 6.1254, 6.1747], [6.0042, 6.1071, 6.1034]], [[-6.9253, -6.8653, -7.0398], [-7.3218, -7.3983, -7.3670], [-7.1961, -7.2482, -7.1569]], [[-4.4723, -4.4348, -4.3769], [-5.3629, -5.4632, -5.4598], [-5.1587, -5.3402, -5.5059]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xs": _lowercase =torch.tensor( [ [[5.4449, 5.5733, 5.6314], [5.1815, 5.3930, 5.5963], [5.1656, 5.4333, 5.4853]], [[-9.4423, -9.7766, -9.6714], [-9.1581, -9.5720, -9.5519], [-9.1006, -9.6458, -9.5703]], [[-7.7721, -7.3716, -7.1583], [-8.4599, -8.0624, -7.7944], [-8.4172, -7.8366, -7.5025]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xxs": _lowercase =torch.tensor( [ [[6.9811, 6.9743, 7.3123], [7.1777, 7.1931, 7.3938], [7.5633, 7.8050, 7.8901]], [[-10.5536, -10.2332, -10.2924], [-10.2336, -9.8624, -9.5964], [-10.8840, -10.8158, -10.6659]], [[-3.4938, -3.0631, -2.8620], [-3.4205, -2.8135, -2.6875], [-3.4179, -2.7945, -2.8750]], ] ) else: raise ValueError(F'''Unknown mobilevit_name: {mobilevit_name}''' ) assert torch.allclose(logits[0, :3, :3, :3] , __lowerCAmelCase , atol=1e-4 ) else: assert logits.shape == (1, 1000) if mobilevit_name == "mobilevit_s": _lowercase =torch.tensor([-0.9866, 0.2392, -1.1241] ) elif mobilevit_name == "mobilevit_xs": _lowercase =torch.tensor([-2.4761, -0.9399, -1.9587] ) elif mobilevit_name == "mobilevit_xxs": _lowercase =torch.tensor([-1.9364, -1.2327, -0.4653] ) else: raise ValueError(F'''Unknown mobilevit_name: {mobilevit_name}''' ) assert torch.allclose(logits[0, :3] , __lowerCAmelCase , atol=1e-4 ) Path(__lowerCAmelCase ).mkdir(exist_ok=__lowerCAmelCase ) print(F'''Saving model {mobilevit_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__lowerCAmelCase ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__lowerCAmelCase ) if push_to_hub: _lowercase ={ '''mobilevit_s''': '''mobilevit-small''', '''mobilevit_xs''': '''mobilevit-x-small''', '''mobilevit_xxs''': '''mobilevit-xx-small''', '''deeplabv3_mobilevit_s''': '''deeplabv3-mobilevit-small''', '''deeplabv3_mobilevit_xs''': '''deeplabv3-mobilevit-x-small''', '''deeplabv3_mobilevit_xxs''': '''deeplabv3-mobilevit-xx-small''', } print('Pushing to the hub...' ) _lowercase =model_mapping[mobilevit_name] image_processor.push_to_hub(__lowerCAmelCase , organization='apple' ) model.push_to_hub(__lowerCAmelCase , organization='apple' ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '--mobilevit_name', default='mobilevit_s', type=str, help=( 'Name of the MobileViT model you\'d like to convert. Should be one of \'mobilevit_s\', \'mobilevit_xs\',' ' \'mobilevit_xxs\', \'deeplabv3_mobilevit_s\', \'deeplabv3_mobilevit_xs\', \'deeplabv3_mobilevit_xxs\'.' ), ) parser.add_argument( '--checkpoint_path', required=True, type=str, help='Path to the original state dict (.pt file).' ) parser.add_argument( '--pytorch_dump_folder_path', required=True, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) lowercase_ = parser.parse_args() convert_movilevit_checkpoint( args.mobilevit_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
205
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node __snake_case = 4 __snake_case = 3 class __snake_case ( lowerCamelCase__ ): pass def lowerCAmelCase_ ( __lowerCAmelCase )-> List[str]: '''simple docstring''' for shard in shards: for i in range(__lowerCAmelCase ): yield {"i": i, "shard": shard} def lowerCAmelCase_ ( )-> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =int(os.environ['''RANK'''] ) UpperCAmelCase : Optional[Any] =int(os.environ['''WORLD_SIZE'''] ) UpperCAmelCase : List[Any] =ArgumentParser() parser.add_argument('''--streaming''' , type=__lowerCAmelCase ) parser.add_argument('''--local_rank''' , type=__lowerCAmelCase ) parser.add_argument('''--num_workers''' , type=__lowerCAmelCase , default=0 ) UpperCAmelCase : Any =parser.parse_args() UpperCAmelCase : List[str] =args.streaming UpperCAmelCase : Tuple =args.num_workers UpperCAmelCase : int ={'''shards''': [f'''shard_{shard_idx}''' for shard_idx in range(__lowerCAmelCase )]} UpperCAmelCase : Optional[int] =IterableDataset.from_generator(__lowerCAmelCase , gen_kwargs=__lowerCAmelCase ) if not streaming: UpperCAmelCase : List[Any] =Dataset.from_list(list(__lowerCAmelCase ) ) UpperCAmelCase : Dict =split_dataset_by_node(__lowerCAmelCase , rank=__lowerCAmelCase , world_size=__lowerCAmelCase ) UpperCAmelCase : List[Any] =torch.utils.data.DataLoader(__lowerCAmelCase , num_workers=__lowerCAmelCase ) UpperCAmelCase : Dict =NUM_SHARDS * NUM_ITEMS_PER_SHARD UpperCAmelCase : str =full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) UpperCAmelCase : List[Any] =sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f'''local_size {local_size} != expected_local_size {expected_local_size}''' ) if __name__ == "__main__": main()
348
0