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""" def lowercase ( A_ = 10**12 )-> int: '''simple docstring''' a : List[str] = 1 a : Optional[Any] = 0 a : Any = 1 a : Optional[int] = 1 while numerator <= 2 * min_total - 1: prev_numerator += 2 * numerator numerator += 2 * prev_numerator prev_denominator += 2 * denominator denominator += 2 * prev_denominator return (denominator + 1) // 2 if __name__ == "__main__": print(f'''{solution() = }''')
40
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A_ :Tuple = { '''configuration_x_clip''': [ '''XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XCLIPConfig''', '''XCLIPTextConfig''', '''XCLIPVisionConfig''', ], '''processing_x_clip''': ['''XCLIPProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ :Union[str, Any] = [ '''XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XCLIPModel''', '''XCLIPPreTrainedModel''', '''XCLIPTextModel''', '''XCLIPVisionModel''', ] if TYPE_CHECKING: from .configuration_x_clip import ( XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, XCLIPConfig, XCLIPTextConfig, XCLIPVisionConfig, ) from .processing_x_clip import XCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_x_clip import ( XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, XCLIPModel, XCLIPPreTrainedModel, XCLIPTextModel, XCLIPVisionModel, ) else: import sys A_ :Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
71
0
'''simple docstring''' def SCREAMING_SNAKE_CASE_ (UpperCamelCase ) -> int: assert column_title.isupper() lowerCamelCase__ : Dict = 0 lowerCamelCase__ : str = len(UpperCamelCase ) - 1 lowerCamelCase__ : str = 0 while index >= 0: lowerCamelCase__ : List[str] = (ord(column_title[index] ) - 64) * pow(26 , UpperCamelCase ) answer += value power += 1 index -= 1 return answer if __name__ == "__main__": from doctest import testmod testmod()
41
from ...configuration_utils import PretrainedConfig from ...utils import logging A_ :Any = logging.get_logger(__name__) A_ :int = { '''sayakpaul/vit-msn-base''': '''https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json''', # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class __A ( a ): """simple docstring""" UpperCamelCase__ : Optional[int] ="""vit_msn""" def __init__( self , lowerCamelCase__=768 , lowerCamelCase__=12 , lowerCamelCase__=12 , lowerCamelCase__=3072 , lowerCamelCase__="gelu" , lowerCamelCase__=0.0 , lowerCamelCase__=0.0 , lowerCamelCase__=0.02 , lowerCamelCase__=1E-06 , lowerCamelCase__=224 , lowerCamelCase__=16 , lowerCamelCase__=3 , lowerCamelCase__=True , **lowerCamelCase__ , ): """simple docstring""" super().__init__(**lowerCamelCase__ ) __UpperCamelCase : int =hidden_size __UpperCamelCase : List[Any] =num_hidden_layers __UpperCamelCase : Union[str, Any] =num_attention_heads __UpperCamelCase : List[str] =intermediate_size __UpperCamelCase : Union[str, Any] =hidden_act __UpperCamelCase : str =hidden_dropout_prob __UpperCamelCase : Union[str, Any] =attention_probs_dropout_prob __UpperCamelCase : Union[str, Any] =initializer_range __UpperCamelCase : Tuple =layer_norm_eps __UpperCamelCase : Optional[Any] =image_size __UpperCamelCase : Optional[int] =patch_size __UpperCamelCase : Any =num_channels __UpperCamelCase : str =qkv_bias
71
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowercase : Any = { "configuration_chinese_clip": [ "CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "ChineseCLIPConfig", "ChineseCLIPOnnxConfig", "ChineseCLIPTextConfig", "ChineseCLIPVisionConfig", ], "processing_chinese_clip": ["ChineseCLIPProcessor"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Optional[Any] = ["ChineseCLIPFeatureExtractor"] lowercase : List[Any] = ["ChineseCLIPImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Any = [ "CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "ChineseCLIPModel", "ChineseCLIPPreTrainedModel", "ChineseCLIPTextModel", "ChineseCLIPVisionModel", ] if TYPE_CHECKING: from .configuration_chinese_clip import ( CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, ChineseCLIPConfig, ChineseCLIPOnnxConfig, ChineseCLIPTextConfig, ChineseCLIPVisionConfig, ) from .processing_chinese_clip import ChineseCLIPProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_chinese_clip import ChineseCLIPFeatureExtractor, ChineseCLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_chinese_clip import ( CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, ChineseCLIPModel, ChineseCLIPPreTrainedModel, ChineseCLIPTextModel, ChineseCLIPVisionModel, ) else: import sys lowercase : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
42
import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __A ( a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : str =DDIMPipeline UpperCamelCase__ : List[Any] =UNCONDITIONAL_IMAGE_GENERATION_PARAMS UpperCamelCase__ : Tuple =PipelineTesterMixin.required_optional_params - { """num_images_per_prompt""", """latents""", """callback""", """callback_steps""", } UpperCamelCase__ : Tuple =UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS UpperCamelCase__ : Any =False def __lowercase ( self ): """simple docstring""" torch.manual_seed(0 ) __UpperCamelCase : Optional[int] =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') , ) __UpperCamelCase : int =DDIMScheduler() __UpperCamelCase : Optional[int] ={'unet': unet, 'scheduler': scheduler} return components def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : str =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : Optional[int] =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Tuple ={ 'batch_size': 1, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Any ='cpu' __UpperCamelCase : Optional[Any] =self.get_dummy_components() __UpperCamelCase : Tuple =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =self.get_dummy_inputs(lowerCamelCase__ ) __UpperCamelCase : int =pipe(**lowerCamelCase__ ).images __UpperCamelCase : Dict =image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) __UpperCamelCase : Tuple =np.array( [1.000E00, 5.717E-01, 4.717E-01, 1.000E00, 0.000E00, 1.000E00, 3.000E-04, 0.000E00, 9.000E-04] ) __UpperCamelCase : Tuple =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) def __lowercase ( self ): """simple docstring""" super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_save_load_local(expected_max_difference=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_save_load_optional_components(expected_max_difference=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __A ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str ='google/ddpm-cifar10-32' __UpperCamelCase : str =UNetaDModel.from_pretrained(lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =DDIMScheduler() __UpperCamelCase : List[Any] =DDIMPipeline(unet=lowerCamelCase__ , scheduler=lowerCamelCase__ ) ddim.to(lowerCamelCase__ ) ddim.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Optional[int] =torch.manual_seed(0 ) __UpperCamelCase : List[str] =ddim(generator=lowerCamelCase__ , eta=0.0 , output_type='numpy' ).images __UpperCamelCase : Union[str, Any] =image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) __UpperCamelCase : str =np.array([0.1_723, 0.1_617, 0.1_600, 0.1_626, 0.1_497, 0.1_513, 0.1_505, 0.1_442, 0.1_453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[Any] ='google/ddpm-ema-bedroom-256' __UpperCamelCase : Any =UNetaDModel.from_pretrained(lowerCamelCase__ ) __UpperCamelCase : int =DDIMScheduler.from_pretrained(lowerCamelCase__ ) __UpperCamelCase : Dict =DDIMPipeline(unet=lowerCamelCase__ , scheduler=lowerCamelCase__ ) ddpm.to(lowerCamelCase__ ) ddpm.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Tuple =torch.manual_seed(0 ) __UpperCamelCase : Union[str, Any] =ddpm(generator=lowerCamelCase__ , output_type='numpy' ).images __UpperCamelCase : Tuple =image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) __UpperCamelCase : Optional[Any] =np.array([0.0_060, 0.0_201, 0.0_344, 0.0_024, 0.0_018, 0.0_002, 0.0_022, 0.0_000, 0.0_069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
71
0
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase = logging.get_logger(__name__) __lowercase = { '''microsoft/markuplm-base''': '''https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json''', '''microsoft/markuplm-large''': '''https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json''', } class lowerCamelCase_ ( UpperCAmelCase_ ): '''simple docstring''' a__ : List[Any] = """markuplm""" def __init__( self , __lowercase=30_522 , __lowercase=768 , __lowercase=12 , __lowercase=12 , __lowercase=3_072 , __lowercase="gelu" , __lowercase=0.1 , __lowercase=0.1 , __lowercase=512 , __lowercase=2 , __lowercase=0.02 , __lowercase=1E-1_2 , __lowercase=0 , __lowercase=0 , __lowercase=2 , __lowercase=256 , __lowercase=1_024 , __lowercase=216 , __lowercase=1_001 , __lowercase=32 , __lowercase=50 , __lowercase="absolute" , __lowercase=True , __lowercase=None , **__lowercase , ) -> str: super().__init__( pad_token_id=__lowercase , bos_token_id=__lowercase , eos_token_id=__lowercase , **__lowercase , ) __UpperCamelCase :Any = vocab_size __UpperCamelCase :List[Any] = hidden_size __UpperCamelCase :str = num_hidden_layers __UpperCamelCase :str = num_attention_heads __UpperCamelCase :Dict = hidden_act __UpperCamelCase :List[str] = intermediate_size __UpperCamelCase :int = hidden_dropout_prob __UpperCamelCase :Union[str, Any] = attention_probs_dropout_prob __UpperCamelCase :Dict = max_position_embeddings __UpperCamelCase :List[str] = type_vocab_size __UpperCamelCase :Optional[int] = initializer_range __UpperCamelCase :Any = layer_norm_eps __UpperCamelCase :Optional[Any] = position_embedding_type __UpperCamelCase :List[Any] = use_cache __UpperCamelCase :Optional[int] = classifier_dropout # additional properties __UpperCamelCase :Optional[Any] = max_depth __UpperCamelCase :Optional[Any] = max_xpath_tag_unit_embeddings __UpperCamelCase :str = max_xpath_subs_unit_embeddings __UpperCamelCase :Union[str, Any] = tag_pad_id __UpperCamelCase :Optional[int] = subs_pad_id __UpperCamelCase :Optional[int] = xpath_unit_hidden_size
43
from __future__ import annotations import copy import tempfile import unittest from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available from transformers.testing_utils import ( DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tensorflow_probability, require_tf, slow, ) from ..bert.test_modeling_bert import BertModelTester if is_tf_available(): from transformers import ( TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelForTableQuestionAnswering, TFAutoModelForTokenClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFFunnelBaseModel, TFFunnelModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, TFTapasForQuestionAnswering, ) from transformers.models.auto.modeling_tf_auto import ( TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_MAPPING, ) 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 from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST class __A ( a ): """simple docstring""" UpperCamelCase__ : Optional[int] ="""new-model""" if is_tf_available(): class __A ( a ): """simple docstring""" UpperCamelCase__ : List[str] =NewModelConfig @require_tf class __A ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] ='bert-base-cased' __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Union[str, Any] ='bert-base-cased' __UpperCamelCase : Optional[int] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Tuple =TFAutoModelForPreTraining.from_pretrained(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]: __UpperCamelCase : str =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModelForCausalLM.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : str =TFAutoModelForCausalLM.from_pretrained(lowerCamelCase__ , output_loading_info=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]: __UpperCamelCase : Optional[int] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Any =TFAutoModelWithLMHead.from_pretrained(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]: __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Any =TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Union[str, Any] =TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase__ , output_loading_info=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]: __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : str =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ , output_loading_info=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForSequenceClassification.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: __UpperCamelCase : List[Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[int] =TFAutoModelForQuestionAnswering.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow @require_tensorflow_probability def __lowercase ( self ): """simple docstring""" for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]: __UpperCamelCase : Any =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModelForTableQuestionAnswering.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : List[str] =TFAutoModelForTableQuestionAnswering.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[Any] =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Dict =TFAutoModel.from_pretrained('sgugger/funnel-random-tiny' ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[int] =copy.deepcopy(model.config ) __UpperCamelCase : Optional[Any] =['FunnelBaseModel'] __UpperCamelCase : Tuple =TFAutoModel.from_config(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : List[Any] =TFAutoModel.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" try: AutoConfig.register('new-model' , lowerCamelCase__ ) __UpperCamelCase : int =[ TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSequenceClassification, TFAutoModelForTokenClassification, ] for auto_class in auto_classes: with self.subTest(auto_class.__name__ ): # Wrong config class will raise an error with self.assertRaises(lowerCamelCase__ ): auto_class.register(lowerCamelCase__ , lowerCamelCase__ ) auto_class.register(lowerCamelCase__ , lowerCamelCase__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(lowerCamelCase__ ): auto_class.register(lowerCamelCase__ , lowerCamelCase__ ) # Now that the config is registered, it can be used as any other config with the auto-API __UpperCamelCase : List[str] =BertModelTester(self ).get_config() __UpperCamelCase : Optional[Any] =NewModelConfig(**tiny_config.to_dict() ) __UpperCamelCase : Dict =auto_class.from_config(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =auto_class.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) finally: if "new-model" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["new-model"] for mapping in ( TF_MODEL_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, ): if NewModelConfig in mapping._extra_content: del mapping._extra_content[NewModelConfig] def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCamelCase__ , 'bert-base is not a local folder and is not a valid model identifier' ): __UpperCamelCase : Dict =TFAutoModel.from_pretrained('bert-base' ) def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCamelCase__ , R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)' ): __UpperCamelCase : Union[str, Any] =TFAutoModel.from_pretrained(lowerCamelCase__ , revision='aaaaaa' ) def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCamelCase__ , 'hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin' , ): __UpperCamelCase : List[str] =TFAutoModel.from_pretrained('hf-internal-testing/config-no-model' ) def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex(lowerCamelCase__ , 'Use `from_pt=True` to load this model' ): __UpperCamelCase : List[Any] =TFAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only' ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[int] =TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert' ) with RequestCounter() as counter: __UpperCamelCase : Dict =TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 ) # With a sharded checkpoint __UpperCamelCase : Dict =TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded' ) with RequestCounter() as counter: __UpperCamelCase : Union[str, Any] =TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
71
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _a : Dict = { 'configuration_jukebox': [ 'JUKEBOX_PRETRAINED_CONFIG_ARCHIVE_MAP', 'JukeboxConfig', 'JukeboxPriorConfig', 'JukeboxVQVAEConfig', ], 'tokenization_jukebox': ['JukeboxTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Tuple = [ 'JUKEBOX_PRETRAINED_MODEL_ARCHIVE_LIST', 'JukeboxModel', 'JukeboxPreTrainedModel', 'JukeboxVQVAE', 'JukeboxPrior', ] if TYPE_CHECKING: from .configuration_jukebox import ( JUKEBOX_PRETRAINED_CONFIG_ARCHIVE_MAP, JukeboxConfig, JukeboxPriorConfig, JukeboxVQVAEConfig, ) from .tokenization_jukebox import JukeboxTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_jukebox import ( JUKEBOX_PRETRAINED_MODEL_ARCHIVE_LIST, JukeboxModel, JukeboxPreTrainedModel, JukeboxPrior, JukeboxVQVAE, ) else: import sys _a : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
44
import argparse import json import os import re import torch from transformers import BloomConfig, BloomModel from transformers.file_utils import CONFIG_NAME, WEIGHTS_NAME from transformers.utils import logging logging.set_verbosity_info() A_ :List[str] = [ '''word_embeddings_layernorm.weight''', '''word_embeddings_layernorm.bias''', '''input_layernorm.weight''', '''input_layernorm.bias''', '''post_attention_layernorm.weight''', '''post_attention_layernorm.bias''', '''self_attention.dense.bias''', '''mlp.dense_4h_to_h.bias''', '''ln_f.weight''', '''ln_f.bias''', ] A_ :Optional[Any] = [ '''mlp.dense_4h_to_h.weight''', '''self_attention.dense.weight''', ] def A ( a_ ,a_ ) -> str: __UpperCamelCase : Any ={ 'word_embeddings.weight': 'word_embeddings.weight', 'word_embeddings.norm.weight': 'word_embeddings_layernorm.weight', 'word_embeddings.norm.bias': 'word_embeddings_layernorm.bias', 'weight': 'ln_f.weight', 'bias': 'ln_f.bias', } if key in layer_rename_map: return layer_rename_map[key] # Handle transformer blocks __UpperCamelCase : Tuple =int(re.match(r'.*layer_(\d*).*' ,a_ )[1] ) layer_number -= 3 return F'h.{layer_number}.' + key def A ( a_ ) -> Any: if dtype == torch.bool: return 1 / 8 __UpperCamelCase : Dict =re.search(r'[^\d](\d+)$' ,str(a_ ) ) if bit_search is None: raise ValueError(F'`dtype` is not a valid dtype: {dtype}.' ) __UpperCamelCase : Tuple =int(bit_search.groups()[0] ) return bit_size // 8 def A ( a_ ,a_ ,a_ ,a_ ,a_ ) -> Dict: # Construct model if bloom_config_file == "": __UpperCamelCase : List[Any] =BloomConfig() else: __UpperCamelCase : List[str] =BloomConfig.from_json_file(a_ ) if shard_model: __UpperCamelCase : int =os.listdir(a_ ) __UpperCamelCase : Union[str, Any] =sorted(filter(lambda a_ : s.startswith('layer' ) and "model_00" in s ,a_ ) ) __UpperCamelCase : Optional[Any] ={'weight_map': {}, 'metadata': {}} __UpperCamelCase : Dict =0 __UpperCamelCase : int =None __UpperCamelCase : Any =BloomConfig() for j, file in enumerate(a_ ): print('Processing file: {}'.format(a_ ) ) __UpperCamelCase : Optional[int] =None for i in range(a_ ): # load all TP files __UpperCamelCase : Dict =file.replace('model_00' ,F'model_0{i}' ) __UpperCamelCase : Optional[Any] =torch.load(os.path.join(a_ ,a_ ) ,map_location='cpu' ) # Rename keys in the transformers names __UpperCamelCase : int =list(temp.keys() ) for key in keys: __UpperCamelCase : Dict =temp.pop(a_ ) if tensors is None: __UpperCamelCase : Any =temp else: for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel __UpperCamelCase : List[Any] =1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks __UpperCamelCase : Any =torch.cat([tensors[key], temp[key]] ,dim=a_ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): __UpperCamelCase : Optional[Any] =tensors[key] / pretraining_tp torch.save( a_ ,os.path.join( a_ ,'pytorch_model_{}-of-{}.bin'.format(str(j + 1 ).zfill(5 ) ,str(len(a_ ) ).zfill(5 ) ) ,) ,) for key in tensors.keys(): __UpperCamelCase : Union[str, Any] =tensors[key] total_size += value.numel() * get_dtype_size(value.dtype ) if key not in index_dict["weight_map"]: __UpperCamelCase : int ='pytorch_model_{}-of-{}.bin'.format( str(j + 1 ).zfill(5 ) ,str(len(a_ ) ).zfill(5 ) ) __UpperCamelCase : Union[str, Any] =BloomConfig() __UpperCamelCase : Tuple =pytorch_dump_folder_path + '/' + CONFIG_NAME __UpperCamelCase : Optional[int] =total_size with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(config.to_json_string() ) with open(os.path.join(a_ ,WEIGHTS_NAME + '.index.json' ) ,'w' ,encoding='utf-8' ) as f: __UpperCamelCase : List[Any] =json.dumps(a_ ,indent=2 ,sort_keys=a_ ) + '\n' f.write(a_ ) else: __UpperCamelCase : List[Any] =BloomModel(a_ ) __UpperCamelCase : Optional[Any] =os.listdir(a_ ) __UpperCamelCase : Dict =sorted(filter(lambda a_ : s.startswith('layer' ) and "model_00" in s ,a_ ) ) __UpperCamelCase : Any =None for i, file in enumerate(a_ ): __UpperCamelCase : Union[str, Any] =None for i in range(a_ ): # load all TP files __UpperCamelCase : Optional[Any] =file.replace('model_00' ,F'model_0{i}' ) __UpperCamelCase : str =torch.load(os.path.join(a_ ,a_ ) ,map_location='cpu' ) # Rename keys in the transformers names __UpperCamelCase : List[str] =list(temp.keys() ) for key in keys: __UpperCamelCase : Union[str, Any] =temp.pop(a_ ) if tensors is None: __UpperCamelCase : Optional[Any] =temp else: for key in tensors.keys(): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel __UpperCamelCase : Optional[int] =1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks __UpperCamelCase : int =torch.cat([tensors[key], temp[key]] ,dim=a_ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): __UpperCamelCase : Dict =tensors[key] / pretraining_tp __UpperCamelCase : str =model.load_state_dict(a_ ,strict=a_ ) assert not other_keys.unexpected_keys, F'The keys {other_keys.unexpected_keys} are unexpected' if missing_keys is None: __UpperCamelCase : str =set(other_keys.missing_keys ) else: __UpperCamelCase : int =missing_keys.intersection(set(other_keys.missing_keys ) ) assert not missing_keys, F'The keys {missing_keys} are missing' # Save pytorch-model os.makedirs(a_ ,exist_ok=a_ ) __UpperCamelCase : Optional[int] =pytorch_dump_folder_path + '/' + WEIGHTS_NAME __UpperCamelCase : Dict =pytorch_dump_folder_path + '/' + CONFIG_NAME print(F'Save PyTorch model to {pytorch_weights_dump_path} with dtype {config.torch_dtype}' ) if config.torch_dtype is not None: __UpperCamelCase : List[str] =model.to(config.torch_dtype ) torch.save(model.state_dict() ,a_ ) print(F'Save configuration file to {pytorch_config_dump_path}' ) with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": A_ :Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--bloom_checkpoint_path''', default=None, type=str, required=True, help='''Path to the Megatron-LM checkpoint path.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--bloom_config_file''', default='''''', type=str, help=( '''An optional config json file corresponding to the pre-trained model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--shard_model''', action='''store_true''', help='''An optional setting to shard the output model \nThis enables sharding the converted checkpoint''', ) parser.add_argument( '''--pretraining_tp''', default=4, type=int, help='''Pretraining TP rank that has been used when training the model in Megatron-LM \n''', ) A_ :str = parser.parse_args() convert_bloom_checkpoint_to_pytorch( args.bloom_checkpoint_path, args.bloom_config_file, args.pytorch_dump_folder_path, args.shard_model, args.pretraining_tp, )
71
0
"""simple docstring""" from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __UpperCAmelCase ( self ): return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def __UpperCAmelCase ( self ): __a = {'''col_1''': [3, 2, 1, 0], '''col_2''': ['''a''', '''b''', '''c''', '''d''']} return Dataset.from_dict(_a ) def __UpperCAmelCase ( self ): __a = self._create_example_records() __a = Dataset.from_list(_a ) self.assertListEqual(dset.column_names , ['''col_1''', '''col_2'''] ) for i, r in enumerate(_a ): self.assertDictEqual(_a , example_records[i] ) def __UpperCAmelCase ( self ): __a = self._create_example_records() __a = Dataset.from_list(_a ) __a = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]} ) self.assertEqual(dset.info , dset_from_dict.info ) def __UpperCAmelCase ( self ): # checks what happens with missing columns __a = [{'''col_1''': 1}, {'''col_2''': '''x'''}] __a = Dataset.from_list(_a ) self.assertDictEqual(dset[0] , {'''col_1''': 1} ) self.assertDictEqual(dset[1] , {'''col_1''': None} ) # NB: first record is used for columns def __UpperCAmelCase ( self ): # checks if the type can be inferred from the second record __a = [{'''col_1''': []}, {'''col_1''': [1, 2]}] __a = Dataset.from_list(_a ) self.assertEqual(dset.info.features['''col_1'''] , Sequence(Value('''int64''' ) ) ) def __UpperCAmelCase ( self ): __a = Dataset.from_list([] ) self.assertEqual(len(_a ) , 0 ) self.assertListEqual(dset.column_names , [] )
45
from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class __A : """simple docstring""" UpperCamelCase__ : int =XGLMConfig UpperCamelCase__ : Optional[Any] ={} UpperCamelCase__ : List[str] ="""gelu""" def __init__( self , lowerCamelCase__ , lowerCamelCase__=14 , lowerCamelCase__=7 , 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__=0.02 , ): """simple docstring""" __UpperCamelCase : Tuple =parent __UpperCamelCase : List[str] =batch_size __UpperCamelCase : str =seq_length __UpperCamelCase : Dict =is_training __UpperCamelCase : Tuple =use_input_mask __UpperCamelCase : List[Any] =use_labels __UpperCamelCase : Any =vocab_size __UpperCamelCase : List[Any] =d_model __UpperCamelCase : Optional[int] =num_hidden_layers __UpperCamelCase : List[str] =num_attention_heads __UpperCamelCase : Optional[int] =ffn_dim __UpperCamelCase : str =activation_function __UpperCamelCase : Any =activation_dropout __UpperCamelCase : Optional[int] =attention_dropout __UpperCamelCase : Optional[int] =max_position_embeddings __UpperCamelCase : Any =initializer_range __UpperCamelCase : Dict =None __UpperCamelCase : Optional[int] =0 __UpperCamelCase : Optional[Any] =2 __UpperCamelCase : str =1 def __lowercase ( self ): """simple docstring""" return XGLMConfig.from_pretrained('facebook/xglm-564M' ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[Any] =tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) , clip_value_min=0 , clip_value_max=3 ) __UpperCamelCase : Union[str, Any] =None if self.use_input_mask: __UpperCamelCase : Dict =random_attention_mask([self.batch_size, self.seq_length] ) __UpperCamelCase : Any =self.get_config() __UpperCamelCase : Optional[Any] =floats_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, ) def __lowercase ( self ): """simple docstring""" return XGLMConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , num_layers=self.num_hidden_layers , attention_heads=self.num_attention_heads , ffn_dim=self.ffn_dim , activation_function=self.activation_function , activation_dropout=self.activation_dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , use_cache=lowerCamelCase__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , return_dict=lowerCamelCase__ , ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =self.prepare_config_and_inputs() ( ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ) : int =config_and_inputs __UpperCamelCase : Optional[Any] ={ 'input_ids': input_ids, 'head_mask': head_mask, } return config, inputs_dict @require_tf class __A ( a , a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : Union[str, Any] =(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () UpperCamelCase__ : str =(TFXGLMForCausalLM,) if is_tf_available() else () UpperCamelCase__ : Optional[Any] =( {"""feature-extraction""": TFXGLMModel, """text-generation""": TFXGLMForCausalLM} if is_tf_available() else {} ) UpperCamelCase__ : Tuple =False UpperCamelCase__ : Tuple =False UpperCamelCase__ : Optional[Any] =False def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Tuple =TFXGLMModelTester(self ) __UpperCamelCase : Dict =ConfigTester(self , config_class=lowerCamelCase__ , n_embd=37 ) def __lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() @slow def __lowercase ( self ): """simple docstring""" for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __UpperCamelCase : Optional[Any] =TFXGLMModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @unittest.skip(reason='Currently, model embeddings are going to undergo a major refactor.' ) def __lowercase ( self ): """simple docstring""" super().test_resize_token_embeddings() @require_tf class __A ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self , lowerCamelCase__=True ): """simple docstring""" __UpperCamelCase : int =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : List[str] =tf.convert_to_tensor([[2, 268, 9865]] , dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off __UpperCamelCase : str =[2, 268, 9865, 67, 11, 1988, 57252, 9865, 5, 984, 67, 1988, 213838, 1658, 53, 70446, 33, 6657, 278, 1581] # fmt: on __UpperCamelCase : Optional[Any] =model.generate(lowerCamelCase__ , do_sample=lowerCamelCase__ , num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist() , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : Union[str, Any] =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) tf.random.set_seed(0 ) __UpperCamelCase : str =tokenizer('Today is a nice day and' , return_tensors='tf' ) __UpperCamelCase : Union[str, Any] =tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(':/CPU:0' ): __UpperCamelCase : Any =model.generate(lowerCamelCase__ , do_sample=lowerCamelCase__ , seed=[7, 0] ) __UpperCamelCase : Tuple =tokenizer.decode(output_ids[0] , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : List[Any] =( 'Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due' ) self.assertEqual(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Tuple =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : Optional[Any] =XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : Optional[Any] ='left' # use different length sentences to test batching __UpperCamelCase : Optional[int] =[ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When', 'Hello, my dog is a little', ] __UpperCamelCase : List[Any] =tokenizer(lowerCamelCase__ , return_tensors='tf' , padding=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =inputs['input_ids'] __UpperCamelCase : Dict =model.generate(input_ids=lowerCamelCase__ , attention_mask=inputs['attention_mask'] , max_new_tokens=12 ) __UpperCamelCase : List[Any] =tokenizer(sentences[0] , return_tensors='tf' ).input_ids __UpperCamelCase : Dict =model.generate(input_ids=lowerCamelCase__ , max_new_tokens=12 ) __UpperCamelCase : Any =tokenizer(sentences[1] , return_tensors='tf' ).input_ids __UpperCamelCase : Optional[Any] =model.generate(input_ids=lowerCamelCase__ , max_new_tokens=12 ) __UpperCamelCase : Optional[int] =tokenizer.batch_decode(lowerCamelCase__ , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =tokenizer.decode(output_non_padded[0] , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : int =tokenizer.decode(output_padded[0] , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : Any =[ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When left padding is applied, the sequence will be ' 'a single', 'Hello, my dog is a little bit of a shy one, but he is very friendly', ] self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , [non_padded_sentence, padded_sentence] )
71
0
"""simple docstring""" import collections import tempfile import unittest import numpy as np from transformers.testing_utils import ( is_pt_flax_cross_test, require_flax, require_torch, require_vision, slow, torch_device, ) from transformers.utils import is_flax_available, is_torch_available, is_vision_available from ...test_modeling_flax_common import floats_tensor, ids_tensor, random_attention_mask from ..bert.test_modeling_flax_bert import FlaxBertModelTester from ..clip.test_modeling_flax_clip import FlaxCLIPVisionModelTester from ..vit.test_modeling_flax_vit import FlaxViTModelTester if is_flax_available(): from transformers import ( FlaxBertModel, FlaxCLIPVisionModel, FlaxVisionTextDualEncoderModel, FlaxViTModel, VisionTextDualEncoderConfig, VisionTextDualEncoderProcessor, ) from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) if is_torch_available(): import torch from transformers import VisionTextDualEncoderModel if is_vision_available(): from PIL import Image def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' if isinstance(SCREAMING_SNAKE_CASE , collections.abc.Iterable ): return x return (x, x) @require_flax class lowercase : def _snake_case ( self , lowercase , lowercase ) -> List[str]: pass def _snake_case ( self ) -> Any: pass def _snake_case ( self ) -> Union[str, Any]: pass def _snake_case ( self , lowercase , lowercase , lowercase ) -> Union[str, Any]: lowerCAmelCase = np.abs((a - b) ).max() self.assertLessEqual(lowercase , lowercase , f'Difference between torch and flax is {diff} (>= {tol}).' ) def _snake_case ( self , lowercase , lowercase , lowercase , lowercase , lowercase=None , **lowercase ) -> Optional[int]: lowerCAmelCase = VisionTextDualEncoderConfig.from_vision_text_configs(lowercase , lowercase ) lowerCAmelCase = FlaxVisionTextDualEncoderModel(lowercase ) lowerCAmelCase = model(input_ids=lowercase , pixel_values=lowercase , attention_mask=lowercase ) self.assertEqual(output["""text_embeds"""].shape , (input_ids.shape[0], config.projection_dim) ) self.assertEqual(output["""image_embeds"""].shape , (pixel_values.shape[0], config.projection_dim) ) def _snake_case ( self , lowercase , lowercase , lowercase , lowercase , lowercase=None , **lowercase ) -> Optional[Any]: lowerCAmelCase , lowerCAmelCase = self.get_vision_text_model(lowercase , lowercase ) lowerCAmelCase = {"""vision_model""": vision_model, """text_model""": text_model} lowerCAmelCase = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowercase ) lowerCAmelCase = model(input_ids=lowercase , pixel_values=lowercase , attention_mask=lowercase ) self.assertEqual(output["""text_embeds"""].shape , (input_ids.shape[0], model.config.projection_dim) ) self.assertEqual(output["""image_embeds"""].shape , (pixel_values.shape[0], model.config.projection_dim) ) def _snake_case ( self , lowercase , lowercase , lowercase , lowercase , lowercase=None , **lowercase ) -> Union[str, Any]: lowerCAmelCase , lowerCAmelCase = self.get_vision_text_model(lowercase , lowercase ) lowerCAmelCase = {"""vision_model""": vision_model, """text_model""": text_model} lowerCAmelCase = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowercase ) lowerCAmelCase = model(input_ids=lowercase , pixel_values=lowercase , attention_mask=lowercase ) lowerCAmelCase = output[0] with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(lowercase ) lowerCAmelCase = FlaxVisionTextDualEncoderModel.from_pretrained(lowercase ) lowerCAmelCase = model(input_ids=lowercase , pixel_values=lowercase , attention_mask=lowercase ) lowerCAmelCase = after_output[0] lowerCAmelCase = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(lowercase , 1e-3 ) def _snake_case ( self , lowercase , lowercase , lowercase , lowercase , lowercase=None , **lowercase ) -> Optional[int]: lowerCAmelCase , lowerCAmelCase = self.get_vision_text_model(lowercase , lowercase ) lowerCAmelCase = {"""vision_model""": vision_model, """text_model""": text_model} lowerCAmelCase = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowercase ) lowerCAmelCase = model( input_ids=lowercase , pixel_values=lowercase , attention_mask=lowercase , output_attentions=lowercase ) lowerCAmelCase = output.vision_model_output.attentions self.assertEqual(len(lowercase ) , vision_config.num_hidden_layers ) # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token) lowerCAmelCase = to_atuple(vision_model.config.image_size ) lowerCAmelCase = to_atuple(vision_model.config.patch_size ) lowerCAmelCase = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) lowerCAmelCase = num_patches + 1 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len) ) lowerCAmelCase = output.text_model_output.attentions self.assertEqual(len(lowercase ) , text_config.num_hidden_layers ) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def _snake_case ( self , lowercase , lowercase , lowercase ) -> Dict: pt_model.to(lowercase ) pt_model.eval() # prepare inputs lowerCAmelCase = inputs_dict lowerCAmelCase = {k: torch.tensor(v.tolist() ) for k, v in flax_inputs.items()} with torch.no_grad(): lowerCAmelCase = pt_model(**lowercase ).to_tuple() lowerCAmelCase = fx_model(**lowercase ).to_tuple() self.assertEqual(len(lowercase ) , len(lowercase ) , """Output lengths differ between Flax and PyTorch""" ) for fx_output, pt_output in zip(fx_outputs[:4] , pt_outputs[:4] ): self.assert_almost_equals(lowercase , pt_output.numpy() , 4e-2 ) # PT -> Flax with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(lowercase ) lowerCAmelCase = FlaxVisionTextDualEncoderModel.from_pretrained(lowercase , from_pt=lowercase ) lowerCAmelCase = fx_model_loaded(**lowercase ).to_tuple() self.assertEqual(len(lowercase ) , len(lowercase ) , """Output lengths differ between Flax and PyTorch""" ) for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4] , pt_outputs[:4] ): self.assert_almost_equals(lowercase , pt_output.numpy() , 4e-2 ) # Flax -> PT with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(lowercase ) lowerCAmelCase = VisionTextDualEncoderModel.from_pretrained(lowercase , from_flax=lowercase ) pt_model_loaded.to(lowercase ) pt_model_loaded.eval() with torch.no_grad(): lowerCAmelCase = pt_model_loaded(**lowercase ).to_tuple() self.assertEqual(len(lowercase ) , len(lowercase ) , """Output lengths differ between Flax and PyTorch""" ) for fx_output, pt_output_loaded in zip(fx_outputs[:4] , pt_outputs_loaded[:4] ): self.assert_almost_equals(lowercase , pt_output_loaded.numpy() , 4e-2 ) def _snake_case ( self , lowercase , lowercase , lowercase ) -> Dict: lowerCAmelCase = VisionTextDualEncoderConfig.from_vision_text_configs(lowercase , lowercase ) lowerCAmelCase = VisionTextDualEncoderModel(lowercase ) lowerCAmelCase = FlaxVisionTextDualEncoderModel(lowercase ) lowerCAmelCase = convert_pytorch_state_dict_to_flax(pt_model.state_dict() , lowercase ) lowerCAmelCase = fx_state self.check_pt_flax_equivalence(lowercase , lowercase , lowercase ) def _snake_case ( self , lowercase , lowercase , lowercase ) -> Any: lowerCAmelCase = VisionTextDualEncoderConfig.from_vision_text_configs(lowercase , lowercase ) lowerCAmelCase = VisionTextDualEncoderModel(lowercase ) lowerCAmelCase = FlaxVisionTextDualEncoderModel(lowercase ) lowerCAmelCase = load_flax_weights_in_pytorch_model(lowercase , fx_model.params ) self.check_pt_flax_equivalence(lowercase , lowercase , lowercase ) def _snake_case ( self ) -> Any: lowerCAmelCase = self.prepare_config_and_inputs() self.check_model_from_pretrained_configs(**lowercase ) def _snake_case ( self ) -> List[Any]: lowerCAmelCase = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_from_pretrained(**lowercase ) def _snake_case ( self ) -> Dict: lowerCAmelCase = self.prepare_config_and_inputs() self.check_save_load(**lowercase ) def _snake_case ( self ) -> Dict: lowerCAmelCase = self.prepare_config_and_inputs() self.check_vision_text_output_attention(**lowercase ) @is_pt_flax_cross_test def _snake_case ( self ) -> str: lowerCAmelCase = self.prepare_config_and_inputs() lowerCAmelCase = config_inputs_dict.pop("""vision_config""" ) lowerCAmelCase = config_inputs_dict.pop("""text_config""" ) lowerCAmelCase = config_inputs_dict self.check_equivalence_pt_to_flax(lowercase , lowercase , lowercase ) self.check_equivalence_flax_to_pt(lowercase , lowercase , lowercase ) @slow def _snake_case ( self ) -> Optional[Any]: lowerCAmelCase , lowerCAmelCase = self.get_pretrained_model_and_inputs() lowerCAmelCase = model_a(**lowercase ) lowerCAmelCase = outputs[0] with tempfile.TemporaryDirectory() as tmp_dirname: model_a.save_pretrained(lowercase ) lowerCAmelCase = FlaxVisionTextDualEncoderModel.from_pretrained(lowercase ) lowerCAmelCase = model_a(**lowercase ) lowerCAmelCase = after_outputs[0] lowerCAmelCase = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(lowercase , 1e-5 ) @require_flax class lowercase ( _UpperCAmelCase , unittest.TestCase ): def _snake_case ( self ) -> Optional[int]: lowerCAmelCase = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained( """hf-internal-testing/tiny-random-vit""" , """hf-internal-testing/tiny-bert""" , vision_from_pt=lowercase , text_from_pt=lowercase , ) lowerCAmelCase = 13 lowerCAmelCase = floats_tensor( [ batch_size, model.config.vision_config.num_channels, model.config.vision_config.image_size, model.config.vision_config.image_size, ] ) lowerCAmelCase = ids_tensor([batch_size, 4] , model.config.text_config.vocab_size ) lowerCAmelCase = random_attention_mask([batch_size, 4] ) lowerCAmelCase = {"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask} return model, inputs def _snake_case ( self , lowercase , lowercase ) -> Dict: lowerCAmelCase = FlaxViTModel(lowercase ) lowerCAmelCase = FlaxBertModel(lowercase ) return vision_model, text_model def _snake_case ( self ) -> Tuple: lowerCAmelCase = FlaxViTModelTester(self ) lowerCAmelCase = FlaxBertModelTester(self ) lowerCAmelCase = vit_model_tester.prepare_config_and_inputs() lowerCAmelCase = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase , lowerCAmelCase = vision_config_and_inputs lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = text_config_and_inputs # make sure that cross attention layers are added return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": attention_mask, "input_ids": input_ids, "token_type_ids": token_type_ids, } @require_torch class lowercase ( _UpperCAmelCase , unittest.TestCase ): def _snake_case ( self ) -> str: lowerCAmelCase = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained( """hf-internal-testing/tiny-random-clip""" , """hf-internal-testing/tiny-bert""" , vision_from_pt=lowercase , text_from_pt=lowercase , ) lowerCAmelCase = 13 lowerCAmelCase = floats_tensor( [ batch_size, model.config.vision_config.num_channels, model.config.vision_config.image_size, model.config.vision_config.image_size, ] ) lowerCAmelCase = ids_tensor([batch_size, 4] , model.config.text_config.vocab_size ) lowerCAmelCase = random_attention_mask([batch_size, 4] ) lowerCAmelCase = {"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask} return model, inputs def _snake_case ( self , lowercase , lowercase ) -> Optional[Any]: lowerCAmelCase = FlaxCLIPVisionModel(lowercase ) lowerCAmelCase = FlaxBertModel(lowercase ) return vision_model, text_model def _snake_case ( self ) -> Optional[int]: lowerCAmelCase = FlaxCLIPVisionModelTester(self ) lowerCAmelCase = FlaxBertModelTester(self ) lowerCAmelCase = clip_model_tester.prepare_config_and_inputs() lowerCAmelCase = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase , lowerCAmelCase = vision_config_and_inputs lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = text_config_and_inputs # make sure that cross attention layers are added return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": attention_mask, "input_ids": input_ids, "token_type_ids": token_type_ids, } @require_flax @require_vision class lowercase ( unittest.TestCase ): @slow def _snake_case ( self ) -> int: lowerCAmelCase = FlaxVisionTextDualEncoderModel.from_pretrained("""clip-italian/clip-italian""" , logit_scale_init_value=1.0 ) lowerCAmelCase = VisionTextDualEncoderProcessor.from_pretrained("""clip-italian/clip-italian""" ) lowerCAmelCase = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) lowerCAmelCase = processor( text=["""una foto di un gatto""", """una foto di un cane"""] , images=lowercase , padding=lowercase , return_tensors="""np""" ) lowerCAmelCase = model(**lowercase ) # verify the logits self.assertEqual(outputs.logits_per_image.shape , (inputs.pixel_values.shape[0], inputs.input_ids.shape[0]) ) self.assertEqual( outputs.logits_per_text.shape , (inputs.input_ids.shape[0], inputs.pixel_values.shape[0]) , ) lowerCAmelCase = np.array([[1.2_284_727, 0.3_104_122]] ) self.assertTrue(np.allclose(outputs.logits_per_image , lowercase , atol=1e-3 ) )
46
import argparse import json import numpy import torch from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def A ( a_ ,a_ ) -> Optional[Any]: # Load checkpoint __UpperCamelCase : int =torch.load(a_ ,map_location='cpu' ) __UpperCamelCase : List[Any] =chkpt['model'] # We have the base model one level deeper than the original XLM repository __UpperCamelCase : str ={} for k, v in state_dict.items(): if "pred_layer" in k: __UpperCamelCase : Optional[Any] =v else: __UpperCamelCase : Optional[Any] =v __UpperCamelCase : List[Any] =chkpt['params'] __UpperCamelCase : str ={n: v for n, v in config.items() if not isinstance(a_ ,(torch.FloatTensor, numpy.ndarray) )} __UpperCamelCase : str =chkpt['dico_word2id'] __UpperCamelCase : Dict ={s + '</w>' if s.find('@@' ) == -1 and i > 13 else s.replace('@@' ,'' ): i for s, i in vocab.items()} # Save pytorch-model __UpperCamelCase : List[Any] =pytorch_dump_folder_path + '/' + WEIGHTS_NAME __UpperCamelCase : Tuple =pytorch_dump_folder_path + '/' + CONFIG_NAME __UpperCamelCase : Any =pytorch_dump_folder_path + '/' + VOCAB_FILES_NAMES['vocab_file'] print(F'Save PyTorch model to {pytorch_weights_dump_path}' ) torch.save(a_ ,a_ ) print(F'Save configuration file to {pytorch_config_dump_path}' ) with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(json.dumps(a_ ,indent=2 ) + '\n' ) print(F'Save vocab file to {pytorch_config_dump_path}' ) with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(json.dumps(a_ ,indent=2 ) + '\n' ) if __name__ == "__main__": A_ :str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--xlm_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.''' ) A_ :List[Any] = parser.parse_args() convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)
71
0
'''simple docstring''' def _lowerCAmelCase ( _UpperCamelCase : list ) -> list: """simple docstring""" for i in range(len(_UpperCamelCase ) - 1 , 0 , -1 ): _SCREAMING_SNAKE_CASE =False for j in range(_UpperCamelCase , 0 , -1 ): if unsorted[j] < unsorted[j - 1]: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =unsorted[j - 1], unsorted[j] _SCREAMING_SNAKE_CASE =True for j in range(_UpperCamelCase ): if unsorted[j] > unsorted[j + 1]: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =unsorted[j + 1], unsorted[j] _SCREAMING_SNAKE_CASE =True if not swapped: break return unsorted if __name__ == "__main__": import doctest doctest.testmod() lowerCamelCase : Tuple = input("Enter numbers separated by a comma:\n").strip() lowerCamelCase : Dict = [int(item) for item in user_input.split(",")] print(f'''{cocktail_shaker_sort(unsorted) = }''')
47
import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class __A ( a ): """simple docstring""" def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str =tempfile.mkdtemp() __UpperCamelCase : Optional[int] =8 # DPR tok __UpperCamelCase : str =[ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] __UpperCamelCase : Optional[Any] =os.path.join(self.tmpdirname , 'dpr_tokenizer' ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) __UpperCamelCase : Dict =os.path.join(lowerCamelCase__ , DPR_VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) # BART tok __UpperCamelCase : Optional[int] =[ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] __UpperCamelCase : str =dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) __UpperCamelCase : Optional[int] =['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] __UpperCamelCase : Any ={'unk_token': '<unk>'} __UpperCamelCase : Any =os.path.join(self.tmpdirname , 'bart_tokenizer' ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) __UpperCamelCase : Any =os.path.join(lowerCamelCase__ , BART_VOCAB_FILES_NAMES['vocab_file'] ) __UpperCamelCase : Dict =os.path.join(lowerCamelCase__ , BART_VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(lowerCamelCase__ ) ) def __lowercase ( self ): """simple docstring""" return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'dpr_tokenizer' ) ) def __lowercase ( self ): """simple docstring""" return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'bart_tokenizer' ) ) def __lowercase ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) @require_tokenizers def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =os.path.join(self.tmpdirname , 'rag_tokenizer' ) __UpperCamelCase : Dict =RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) __UpperCamelCase : List[Any] =RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(lowerCamelCase__ ) rag_tokenizer.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : int =RagTokenizer.from_pretrained(lowerCamelCase__ , config=lowerCamelCase__ ) self.assertIsInstance(new_rag_tokenizer.question_encoder , lowerCamelCase__ ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , lowerCamelCase__ ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Dict =RagTokenizer.from_pretrained('facebook/rag-token-nq' ) __UpperCamelCase : Union[str, Any] =[ 'who got the first nobel prize in physics', 'when is the next deadpool movie being released', 'which mode is used for short wave broadcast service', 'who is the owner of reading football club', 'when is the next scandal episode coming out', 'when is the last time the philadelphia won the superbowl', 'what is the most current adobe flash player version', 'how many episodes are there in dragon ball z', 'what is the first step in the evolution of the eye', 'where is gall bladder situated in human body', 'what is the main mineral in lithium batteries', 'who is the president of usa right now', 'where do the greasers live in the outsiders', 'panda is a national animal of which country', 'what is the name of manchester united stadium', ] __UpperCamelCase : int =tokenizer(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =RagTokenizer.from_pretrained('facebook/rag-sequence-nq' ) __UpperCamelCase : Union[str, Any] =[ 'who got the first nobel prize in physics', 'when is the next deadpool movie being released', 'which mode is used for short wave broadcast service', 'who is the owner of reading football club', 'when is the next scandal episode coming out', 'when is the last time the philadelphia won the superbowl', 'what is the most current adobe flash player version', 'how many episodes are there in dragon ball z', 'what is the first step in the evolution of the eye', 'where is gall bladder situated in human body', 'what is the main mineral in lithium batteries', 'who is the president of usa right now', 'where do the greasers live in the outsiders', 'panda is a national animal of which country', 'what is the name of manchester united stadium', ] __UpperCamelCase : Any =tokenizer(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ )
71
0
import inspect from typing import Callable, List, Optional, Union import torch from transformers import ( CLIPImageProcessor, CLIPTextModel, CLIPTokenizer, WhisperForConditionalGeneration, WhisperProcessor, ) from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.utils import logging SCREAMING_SNAKE_CASE__ : Tuple = logging.get_logger(__name__) # pylint: disable=invalid-name class UpperCamelCase__ (lowerCAmelCase__ ): '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ) -> Any: super().__init__() if safety_checker is None: logger.warning( F'''You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure''' " that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered" " results in services or applications open to the public. Both the diffusers team and Hugging Face" " strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling" " it only for use-cases that involve analyzing network behavior or auditing its results. For more" " information, please have a look at https://github.com/huggingface/diffusers/pull/254 ." ) self.register_modules( speech_model=UpperCamelCase__ , speech_processor=UpperCamelCase__ , vae=UpperCamelCase__ , text_encoder=UpperCamelCase__ , tokenizer=UpperCamelCase__ , unet=UpperCamelCase__ , scheduler=UpperCamelCase__ , feature_extractor=UpperCamelCase__ , ) def _lowercase ( self , UpperCamelCase__ = "auto" ) -> Optional[Any]: if slice_size == "auto": lowerCamelCase : Union[str, Any] = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(UpperCamelCase__ ) def _lowercase ( self ) -> Union[str, Any]: self.enable_attention_slicing(UpperCamelCase__ ) @torch.no_grad() def __call__( self , UpperCamelCase__ , UpperCamelCase__=1_6000 , UpperCamelCase__ = 512 , UpperCamelCase__ = 512 , UpperCamelCase__ = 50 , UpperCamelCase__ = 7.5 , UpperCamelCase__ = None , UpperCamelCase__ = 1 , UpperCamelCase__ = 0.0 , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = "pil" , UpperCamelCase__ = True , UpperCamelCase__ = None , UpperCamelCase__ = 1 , **UpperCamelCase__ , ) -> str: lowerCamelCase : Dict = self.speech_processor.feature_extractor( UpperCamelCase__ , return_tensors="pt" , sampling_rate=UpperCamelCase__ ).input_features.to(self.device ) lowerCamelCase : Any = self.speech_model.generate(UpperCamelCase__ , max_length=48_0000 ) lowerCamelCase : List[str] = self.speech_processor.tokenizer.batch_decode(UpperCamelCase__ , skip_special_tokens=UpperCamelCase__ , normalize=UpperCamelCase__ )[ 0 ] if isinstance(UpperCamelCase__ , UpperCamelCase__ ): lowerCamelCase : str = 1 elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): lowerCamelCase : Any = len(UpperCamelCase__ ) else: raise ValueError(F'''`prompt` has to be of type `str` or `list` but is {type(UpperCamelCase__ )}''' ) if height % 8 != 0 or width % 8 != 0: raise ValueError(F'''`height` and `width` have to be divisible by 8 but are {height} and {width}.''' ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(UpperCamelCase__ , UpperCamelCase__ ) or callback_steps <= 0) ): raise ValueError( F'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' F''' {type(UpperCamelCase__ )}.''' ) # get prompt text embeddings lowerCamelCase : Union[str, Any] = self.tokenizer( UpperCamelCase__ , padding="max_length" , max_length=self.tokenizer.model_max_length , return_tensors="pt" , ) lowerCamelCase : List[str] = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: lowerCamelCase : Any = 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}''' ) lowerCamelCase : Any = text_input_ids[:, : self.tokenizer.model_max_length] lowerCamelCase : Any = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method lowerCamelCase , lowerCamelCase , lowerCamelCase : List[str] = text_embeddings.shape lowerCamelCase : Dict = text_embeddings.repeat(1 , UpperCamelCase__ , 1 ) lowerCamelCase : int = text_embeddings.view(bs_embed * num_images_per_prompt , UpperCamelCase__ , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. lowerCamelCase : Union[str, Any] = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: lowerCamelCase : List[str] if negative_prompt is None: lowerCamelCase : Dict = [""] * batch_size elif type(UpperCamelCase__ ) is not type(UpperCamelCase__ ): raise TypeError( F'''`negative_prompt` should be the same type to `prompt`, but got {type(UpperCamelCase__ )} !=''' F''' {type(UpperCamelCase__ )}.''' ) elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): lowerCamelCase : Union[str, Any] = [negative_prompt] elif batch_size != len(UpperCamelCase__ ): raise ValueError( F'''`negative_prompt`: {negative_prompt} has batch size {len(UpperCamelCase__ )}, but `prompt`:''' F''' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches''' " the batch size of `prompt`." ) else: lowerCamelCase : Tuple = negative_prompt lowerCamelCase : Any = text_input_ids.shape[-1] lowerCamelCase : Dict = self.tokenizer( UpperCamelCase__ , padding="max_length" , max_length=UpperCamelCase__ , truncation=UpperCamelCase__ , return_tensors="pt" , ) lowerCamelCase : Optional[int] = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method lowerCamelCase : Optional[Any] = uncond_embeddings.shape[1] lowerCamelCase : Dict = uncond_embeddings.repeat(1 , UpperCamelCase__ , 1 ) lowerCamelCase : List[str] = uncond_embeddings.view(batch_size * num_images_per_prompt , UpperCamelCase__ , -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 lowerCamelCase : Dict = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. lowerCamelCase : Optional[Any] = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) lowerCamelCase : str = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps lowerCamelCase : Optional[int] = torch.randn(UpperCamelCase__ , generator=UpperCamelCase__ , device="cpu" , dtype=UpperCamelCase__ ).to( self.device ) else: lowerCamelCase : Dict = torch.randn(UpperCamelCase__ , generator=UpperCamelCase__ , device=self.device , dtype=UpperCamelCase__ ) else: if latents.shape != latents_shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) lowerCamelCase : List[str] = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(UpperCamelCase__ ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand lowerCamelCase : Any = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler lowerCamelCase : Dict = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] lowerCamelCase : int = "eta" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) lowerCamelCase : List[str] = {} if accepts_eta: lowerCamelCase : Tuple = eta for i, t in enumerate(self.progress_bar(UpperCamelCase__ ) ): # expand the latents if we are doing classifier free guidance lowerCamelCase : str = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowerCamelCase : List[Any] = self.scheduler.scale_model_input(UpperCamelCase__ , UpperCamelCase__ ) # predict the noise residual lowerCamelCase : Optional[int] = self.unet(UpperCamelCase__ , UpperCamelCase__ , encoder_hidden_states=UpperCamelCase__ ).sample # perform guidance if do_classifier_free_guidance: lowerCamelCase , lowerCamelCase : Tuple = noise_pred.chunk(2 ) lowerCamelCase : Any = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 lowerCamelCase : Optional[int] = self.scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) lowerCamelCase : List[str] = 1 / 0.18215 * latents lowerCamelCase : List[Any] = self.vae.decode(UpperCamelCase__ ).sample lowerCamelCase : Tuple = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 lowerCamelCase : int = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": lowerCamelCase : Optional[int] = self.numpy_to_pil(UpperCamelCase__ ) if not return_dict: return image return StableDiffusionPipelineOutput(images=UpperCamelCase__ , nsfw_content_detected=UpperCamelCase__ )
48
A_ :Optional[int] = ''' # Installazione di Transformers ! pip install transformers datasets # Per installare dalla fonte invece dell\'ultima versione rilasciata, commenta il comando sopra e # rimuovi la modalità commento al comando seguente. # ! pip install git+https://github.com/huggingface/transformers.git ''' A_ :Union[str, Any] = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] A_ :Optional[Any] = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
71
0
__snake_case :Tuple = [ 999, 800, 799, 600, 599, 500, 400, 399, 377, 355, 333, 311, 288, 266, 244, 222, 200, 199, 177, 155, 133, 111, 88, 66, 44, 22, 0, ] __snake_case :str = [ 999, 976, 952, 928, 905, 882, 858, 857, 810, 762, 715, 714, 572, 429, 428, 286, 285, 238, 190, 143, 142, 118, 95, 71, 47, 24, 0, ] __snake_case :Any = [ 999, 988, 977, 966, 955, 944, 933, 922, 911, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 350, 300, 299, 266, 233, 200, 199, 179, 159, 140, 120, 100, 99, 88, 77, 66, 55, 44, 33, 22, 11, 0, ] __snake_case :List[str] = [ 999, 995, 992, 989, 985, 981, 978, 975, 971, 967, 964, 961, 957, 956, 951, 947, 942, 937, 933, 928, 923, 919, 914, 913, 908, 903, 897, 892, 887, 881, 876, 871, 870, 864, 858, 852, 846, 840, 834, 828, 827, 820, 813, 806, 799, 792, 785, 784, 777, 770, 763, 756, 749, 742, 741, 733, 724, 716, 707, 699, 698, 688, 677, 666, 656, 655, 645, 634, 623, 613, 612, 598, 584, 570, 569, 555, 541, 527, 526, 505, 484, 483, 462, 440, 439, 396, 395, 352, 351, 308, 307, 264, 263, 220, 219, 176, 132, 88, 44, 0, ] __snake_case :str = [ 999, 997, 995, 992, 990, 988, 986, 984, 981, 979, 977, 975, 972, 970, 968, 966, 964, 961, 959, 957, 956, 954, 951, 949, 946, 944, 941, 939, 936, 934, 931, 929, 926, 924, 921, 919, 916, 914, 913, 910, 907, 905, 902, 899, 896, 893, 891, 888, 885, 882, 879, 877, 874, 871, 870, 867, 864, 861, 858, 855, 852, 849, 846, 843, 840, 837, 834, 831, 828, 827, 824, 821, 817, 814, 811, 808, 804, 801, 798, 795, 791, 788, 785, 784, 780, 777, 774, 770, 766, 763, 760, 756, 752, 749, 746, 742, 741, 737, 733, 730, 726, 722, 718, 714, 710, 707, 703, 699, 698, 694, 690, 685, 681, 677, 673, 669, 664, 660, 656, 655, 650, 646, 641, 636, 632, 627, 622, 618, 613, 612, 607, 602, 596, 591, 586, 580, 575, 570, 569, 563, 557, 551, 545, 539, 533, 527, 526, 519, 512, 505, 498, 491, 484, 483, 474, 466, 457, 449, 440, 439, 428, 418, 407, 396, 395, 381, 366, 352, 351, 330, 308, 307, 286, 264, 263, 242, 220, 219, 176, 175, 132, 131, 88, 44, 0, ] __snake_case :List[str] = [ 999, 991, 982, 974, 966, 958, 950, 941, 933, 925, 916, 908, 900, 899, 874, 850, 825, 800, 799, 700, 600, 500, 400, 300, 200, 100, 0, ] __snake_case :int = [ 999, 992, 985, 978, 971, 964, 957, 949, 942, 935, 928, 921, 914, 907, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 300, 299, 200, 199, 100, 99, 0, ] __snake_case :str = [ 999, 996, 992, 989, 985, 982, 979, 975, 972, 968, 965, 961, 958, 955, 951, 948, 944, 941, 938, 934, 931, 927, 924, 920, 917, 914, 910, 907, 903, 900, 899, 891, 884, 876, 869, 861, 853, 846, 838, 830, 823, 815, 808, 800, 799, 788, 777, 766, 755, 744, 733, 722, 711, 700, 699, 688, 677, 666, 655, 644, 633, 622, 611, 600, 599, 585, 571, 557, 542, 528, 514, 500, 499, 485, 471, 457, 442, 428, 414, 400, 399, 379, 359, 340, 320, 300, 299, 279, 259, 240, 220, 200, 199, 166, 133, 100, 99, 66, 33, 0, ]
49
import argparse import hashlib import os import urllib import warnings import torch from torch import nn from tqdm import tqdm from transformers import WhisperConfig, WhisperForConditionalGeneration A_ :Optional[Any] = { '''tiny.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt''', '''tiny''': '''https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt''', '''base.en''': '''https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt''', '''base''': '''https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt''', '''small.en''': '''https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt''', '''small''': '''https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt''', '''medium.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt''', '''medium''': '''https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt''', '''large''': '''https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large.pt''', '''large-v2''': '''https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt''', } def A ( a_ ) -> List[Any]: __UpperCamelCase : Any =['layers', 'blocks'] for k in ignore_keys: state_dict.pop(a_ ,a_ ) A_ :int = { '''blocks''': '''layers''', '''mlp.0''': '''fc1''', '''mlp.2''': '''fc2''', '''mlp_ln''': '''final_layer_norm''', '''.attn.query''': '''.self_attn.q_proj''', '''.attn.key''': '''.self_attn.k_proj''', '''.attn.value''': '''.self_attn.v_proj''', '''.attn_ln''': '''.self_attn_layer_norm''', '''.attn.out''': '''.self_attn.out_proj''', '''.cross_attn.query''': '''.encoder_attn.q_proj''', '''.cross_attn.key''': '''.encoder_attn.k_proj''', '''.cross_attn.value''': '''.encoder_attn.v_proj''', '''.cross_attn_ln''': '''.encoder_attn_layer_norm''', '''.cross_attn.out''': '''.encoder_attn.out_proj''', '''decoder.ln.''': '''decoder.layer_norm.''', '''encoder.ln.''': '''encoder.layer_norm.''', '''token_embedding''': '''embed_tokens''', '''encoder.positional_embedding''': '''encoder.embed_positions.weight''', '''decoder.positional_embedding''': '''decoder.embed_positions.weight''', '''ln_post''': '''layer_norm''', } def A ( a_ ) -> Union[str, Any]: __UpperCamelCase : str =list(s_dict.keys() ) for key in keys: __UpperCamelCase : str =key for k, v in WHISPER_MAPPING.items(): if k in key: __UpperCamelCase : Optional[Any] =new_key.replace(a_ ,a_ ) print(F'{key} -> {new_key}' ) __UpperCamelCase : Dict =s_dict.pop(a_ ) return s_dict def A ( a_ ) -> Optional[Any]: __UpperCamelCase , __UpperCamelCase : Tuple =emb.weight.shape __UpperCamelCase : Tuple =nn.Linear(a_ ,a_ ,bias=a_ ) __UpperCamelCase : List[Any] =emb.weight.data return lin_layer def A ( a_ ,a_ ) -> bytes: os.makedirs(a_ ,exist_ok=a_ ) __UpperCamelCase : Optional[int] =os.path.basename(a_ ) __UpperCamelCase : Union[str, Any] =url.split('/' )[-2] __UpperCamelCase : Union[str, Any] =os.path.join(a_ ,a_ ) if os.path.exists(a_ ) and not os.path.isfile(a_ ): raise RuntimeError(F'{download_target} exists and is not a regular file' ) if os.path.isfile(a_ ): __UpperCamelCase : str =open(a_ ,'rb' ).read() if hashlib.shaaaa(a_ ).hexdigest() == expected_shaaaa: return model_bytes else: warnings.warn(F'{download_target} exists, but the SHA256 checksum does not match; re-downloading the file' ) with urllib.request.urlopen(a_ ) as source, open(a_ ,'wb' ) as output: with tqdm( total=int(source.info().get('Content-Length' ) ) ,ncols=80 ,unit='iB' ,unit_scale=a_ ,unit_divisor=1_024 ) as loop: while True: __UpperCamelCase : Optional[Any] =source.read(8_192 ) if not buffer: break output.write(a_ ) loop.update(len(a_ ) ) __UpperCamelCase : List[Any] =open(a_ ,'rb' ).read() if hashlib.shaaaa(a_ ).hexdigest() != expected_shaaaa: raise RuntimeError( 'Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model.' ) return model_bytes def A ( a_ ,a_ ) -> Optional[Any]: if ".pt" not in checkpoint_path: __UpperCamelCase : int =_download(_MODELS[checkpoint_path] ) else: __UpperCamelCase : List[str] =torch.load(a_ ,map_location='cpu' ) __UpperCamelCase : Union[str, Any] =original_checkpoint['dims'] __UpperCamelCase : List[Any] =original_checkpoint['model_state_dict'] __UpperCamelCase : Dict =state_dict['decoder.token_embedding.weight'] remove_ignore_keys_(a_ ) rename_keys(a_ ) __UpperCamelCase : List[str] =True __UpperCamelCase : str =state_dict['decoder.layers.0.fc1.weight'].shape[0] __UpperCamelCase : Optional[int] =WhisperConfig( vocab_size=dimensions['n_vocab'] ,encoder_ffn_dim=a_ ,decoder_ffn_dim=a_ ,num_mel_bins=dimensions['n_mels'] ,d_model=dimensions['n_audio_state'] ,max_target_positions=dimensions['n_text_ctx'] ,encoder_layers=dimensions['n_audio_layer'] ,encoder_attention_heads=dimensions['n_audio_head'] ,decoder_layers=dimensions['n_text_layer'] ,decoder_attention_heads=dimensions['n_text_state'] ,max_source_positions=dimensions['n_audio_ctx'] ,) __UpperCamelCase : List[str] =WhisperForConditionalGeneration(a_ ) __UpperCamelCase , __UpperCamelCase : Union[str, Any] =model.model.load_state_dict(a_ ,strict=a_ ) if len(a_ ) > 0 and not set(a_ ) <= { "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: __UpperCamelCase : Optional[int] =make_linear_from_emb(model.model.decoder.embed_tokens ) else: __UpperCamelCase : List[str] =proj_out_weights model.save_pretrained(a_ ) if __name__ == "__main__": A_ :List[Any] = argparse.ArgumentParser() # # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Patht to the downloaded checkpoints''') parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') A_ :List[Any] = parser.parse_args() convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)
71
0
import argparse import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _UpperCAmelCase : str = 16 _UpperCAmelCase : str = 32 def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase = 16 ) -> str: lowerCamelCase__ : int = AutoTokenizer.from_pretrained('bert-base-cased' ) lowerCamelCase__ : Union[str, Any] = load_dataset('glue' , 'mrpc' ) def tokenize_function(_UpperCAmelCase ): # max_length=None => use the model max length (it's actually the default) lowerCamelCase__ : int = tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=_UpperCAmelCase , max_length=_UpperCAmelCase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): lowerCamelCase__ : Any = datasets.map( _UpperCAmelCase , batched=_UpperCAmelCase , remove_columns=['idx', 'sentence1', 'sentence2'] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library lowerCamelCase__ : Dict = tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(_UpperCAmelCase ): # On TPU it's best to pad everything to the same length or training will be very slow. lowerCamelCase__ : Dict = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": lowerCamelCase__ : List[Any] = 16 elif accelerator.mixed_precision != "no": lowerCamelCase__ : Dict = 8 else: lowerCamelCase__ : Any = None return tokenizer.pad( _UpperCAmelCase , padding='longest' , max_length=_UpperCAmelCase , pad_to_multiple_of=_UpperCAmelCase , return_tensors='pt' , ) # Instantiate dataloaders. lowerCamelCase__ : List[str] = DataLoader( tokenized_datasets['train'] , shuffle=_UpperCAmelCase , collate_fn=_UpperCAmelCase , batch_size=_UpperCAmelCase , drop_last=_UpperCAmelCase ) lowerCamelCase__ : str = DataLoader( tokenized_datasets['validation'] , shuffle=_UpperCAmelCase , collate_fn=_UpperCAmelCase , batch_size=_UpperCAmelCase , drop_last=(accelerator.mixed_precision == 'fp8') , ) return train_dataloader, eval_dataloader def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase ) -> Optional[Any]: # Initialize accelerator lowerCamelCase__ : Union[str, Any] = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs lowerCamelCase__ : int = config['lr'] lowerCamelCase__ : Tuple = int(config['num_epochs'] ) lowerCamelCase__ : Union[str, Any] = int(config['seed'] ) lowerCamelCase__ : List[str] = int(config['batch_size'] ) lowerCamelCase__ : List[str] = evaluate.load('glue' , 'mrpc' ) # If the batch size is too big we use gradient accumulation lowerCamelCase__ : Dict = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: lowerCamelCase__ : Optional[Any] = batch_size // MAX_GPU_BATCH_SIZE lowerCamelCase__ : Any = MAX_GPU_BATCH_SIZE set_seed(_UpperCAmelCase ) lowerCamelCase__ , lowerCamelCase__ : Dict = get_dataloaders(_UpperCAmelCase , _UpperCAmelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) lowerCamelCase__ : Any = AutoModelForSequenceClassification.from_pretrained('bert-base-cased' , return_dict=_UpperCAmelCase ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). lowerCamelCase__ : Dict = model.to(accelerator.device ) # Instantiate optimizer lowerCamelCase__ : int = AdamW(params=model.parameters() , lr=_UpperCAmelCase ) # Instantiate scheduler lowerCamelCase__ : Dict = get_linear_schedule_with_warmup( optimizer=_UpperCAmelCase , num_warmup_steps=100 , num_training_steps=(len(_UpperCAmelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : int = accelerator.prepare( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # Now we train the model for epoch in range(_UpperCAmelCase ): model.train() for step, batch in enumerate(_UpperCAmelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) lowerCamelCase__ : Optional[int] = model(**_UpperCAmelCase ) lowerCamelCase__ : Any = outputs.loss lowerCamelCase__ : Optional[Any] = loss / gradient_accumulation_steps accelerator.backward(_UpperCAmelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(_UpperCAmelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): lowerCamelCase__ : List[Any] = model(**_UpperCAmelCase ) lowerCamelCase__ : Any = outputs.logits.argmax(dim=-1 ) lowerCamelCase__ , lowerCamelCase__ : Optional[int] = accelerator.gather_for_metrics((predictions, batch['labels']) ) metric.add_batch( predictions=_UpperCAmelCase , references=_UpperCAmelCase , ) lowerCamelCase__ : List[Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"""epoch {epoch}:""" , _UpperCAmelCase ) def SCREAMING_SNAKE_CASE ( ) -> Optional[Any]: lowerCamelCase__ : str = argparse.ArgumentParser(description='Simple example of training script.' ) parser.add_argument( '--mixed_precision' , type=_UpperCAmelCase , default=_UpperCAmelCase , choices=['no', 'fp16', 'bf16', 'fp8'] , help='Whether to use mixed precision. Choose' 'between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.' 'and an Nvidia Ampere GPU.' , ) parser.add_argument('--cpu' , action='store_true' , help='If passed, will train on the CPU.' ) lowerCamelCase__ : Tuple = parser.parse_args() lowerCamelCase__ : Dict = {'lr': 2e-5, 'num_epochs': 3, 'seed': 42, 'batch_size': 16} training_function(_UpperCAmelCase , _UpperCAmelCase ) if __name__ == "__main__": main()
50
import os from datetime import datetime as dt from github import Github A_ :str = [ '''good first issue''', '''feature request''', '''wip''', ] def A ( ) -> Any: __UpperCamelCase : Any =Github(os.environ['GITHUB_TOKEN'] ) __UpperCamelCase : Union[str, Any] =g.get_repo('huggingface/accelerate' ) __UpperCamelCase : Tuple =repo.get_issues(state='open' ) for issue in open_issues: __UpperCamelCase : List[Any] =sorted([comment for comment in issue.get_comments()] ,key=lambda a_ : i.created_at ,reverse=a_ ) __UpperCamelCase : str =comments[0] if len(a_ ) > 0 else None __UpperCamelCase : Any =dt.utcnow() __UpperCamelCase : List[str] =(current_time - issue.updated_at).days __UpperCamelCase : Union[str, Any] =(current_time - issue.created_at).days if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and days_since_updated > 7 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Close issue since it has been 7 days of inactivity since bot mention. issue.edit(state='closed' ) elif ( days_since_updated > 23 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Add stale comment issue.create_comment( 'This issue has been automatically marked as stale because it has not had ' 'recent activity. If you think this still needs to be addressed ' 'please comment on this thread.\n\nPlease note that issues that do not follow the ' '[contributing guidelines](https://github.com/huggingface/accelerate/blob/main/CONTRIBUTING.md) ' 'are likely to be ignored.' ) if __name__ == "__main__": main()
71
0
import os import sys from contextlib import contextmanager # Windows only if os.name == "nt": import ctypes import msvcrt # noqa class __snake_case ( ctypes.Structure ): # _fields is a specific attr expected by ctypes UpperCAmelCase__ : str = [('''size''', ctypes.c_int), ('''visible''', ctypes.c_byte)] def A () -> Dict: """simple docstring""" if os.name == "nt": UpperCAmelCase_ = CursorInfo() UpperCAmelCase_ = ctypes.windll.kernelaa.GetStdHandle(-11 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(__A , ctypes.byref(__A ) ) UpperCAmelCase_ = False ctypes.windll.kernelaa.SetConsoleCursorInfo(__A , ctypes.byref(__A ) ) elif os.name == "posix": sys.stdout.write('''\033[?25l''' ) sys.stdout.flush() def A () -> Any: """simple docstring""" if os.name == "nt": UpperCAmelCase_ = CursorInfo() UpperCAmelCase_ = ctypes.windll.kernelaa.GetStdHandle(-11 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(__A , ctypes.byref(__A ) ) UpperCAmelCase_ = True ctypes.windll.kernelaa.SetConsoleCursorInfo(__A , ctypes.byref(__A ) ) elif os.name == "posix": sys.stdout.write('''\033[?25h''' ) sys.stdout.flush() @contextmanager def A () -> Dict: """simple docstring""" try: hide_cursor() yield finally: show_cursor()
51
import re def A ( a_ ) -> bool: __UpperCamelCase : Any =re.compile( r'^(?:0|94|\+94|0{2}94)' r'7(0|1|2|4|5|6|7|8)' r'(-| |)' r'\d{7}$' ) return bool(re.search(a_ ,a_ ) ) if __name__ == "__main__": A_ :List[str] = '''0094702343221''' print(is_sri_lankan_phone_number(phone))
71
0
import argparse import glob import logging import os from argparse import Namespace from importlib import import_module import numpy as np import torch from lightning_base import BaseTransformer, add_generic_args, generic_train from seqeval.metrics import accuracy_score, fa_score, precision_score, recall_score from torch.nn import CrossEntropyLoss from torch.utils.data import DataLoader, TensorDataset from utils_ner import TokenClassificationTask __lowerCamelCase : Dict = logging.getLogger(__name__) class A__ ( __snake_case ): _UpperCAmelCase :List[str] = 'token-classification' def __init__( self , A_ ): '''simple docstring''' if type(A_ ) == dict: UpperCamelCase : int = Namespace(**A_ ) UpperCamelCase : List[Any] = import_module("tasks" ) try: UpperCamelCase : List[str] = getattr(A_ , hparams.task_type ) UpperCamelCase : TokenClassificationTask = token_classification_task_clazz() except AttributeError: raise ValueError( F"""Task {hparams.task_type} needs to be defined as a TokenClassificationTask subclass in {module}. """ F"""Available tasks classes are: {TokenClassificationTask.__subclasses__()}""" ) UpperCamelCase : str = self.token_classification_task.get_labels(hparams.labels ) UpperCamelCase : List[Any] = CrossEntropyLoss().ignore_index super().__init__(A_ , len(self.labels ) , self.mode ) def __UpperCamelCase( self , **A_ ): '''simple docstring''' return self.model(**A_ ) def __UpperCamelCase( self , A_ , A_ ): '''simple docstring''' UpperCamelCase : Optional[int] = {"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]} if self.config.model_type != "distilbert": UpperCamelCase : Dict = ( batch[2] if self.config.model_type in ["bert", "xlnet"] else None ) # XLM and RoBERTa don"t use token_type_ids UpperCamelCase : int = self(**A_ ) UpperCamelCase : List[Any] = outputs[0] # tensorboard_logs = {"loss": loss, "rate": self.lr_scheduler.get_last_lr()[-1]} return {"loss": loss} def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : List[Any] = self.hparams for mode in ["train", "dev", "test"]: UpperCamelCase : Dict = self._feature_file(A_ ) if os.path.exists(A_ ) and not args.overwrite_cache: logger.info("Loading features from cached file %s" , A_ ) UpperCamelCase : Dict = torch.load(A_ ) else: logger.info("Creating features from dataset file at %s" , args.data_dir ) UpperCamelCase : List[Any] = self.token_classification_task.read_examples_from_file(args.data_dir , A_ ) UpperCamelCase : int = self.token_classification_task.convert_examples_to_features( A_ , self.labels , args.max_seq_length , self.tokenizer , cls_token_at_end=bool(self.config.model_type in ["xlnet"] ) , cls_token=self.tokenizer.cls_token , cls_token_segment_id=2 if self.config.model_type in ["xlnet"] else 0 , sep_token=self.tokenizer.sep_token , sep_token_extra=A_ , pad_on_left=bool(self.config.model_type in ["xlnet"] ) , pad_token=self.tokenizer.pad_token_id , pad_token_segment_id=self.tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) logger.info("Saving features into cached file %s" , A_ ) torch.save(A_ , A_ ) def __UpperCamelCase( self , A_ , A_ , A_ = False ): '''simple docstring''' UpperCamelCase : Optional[Any] = self._feature_file(A_ ) logger.info("Loading features from cached file %s" , A_ ) UpperCamelCase : Optional[int] = torch.load(A_ ) UpperCamelCase : Optional[int] = torch.tensor([f.input_ids for f in features] , dtype=torch.long ) UpperCamelCase : Optional[Any] = torch.tensor([f.attention_mask for f in features] , dtype=torch.long ) if features[0].token_type_ids is not None: UpperCamelCase : Tuple = torch.tensor([f.token_type_ids for f in features] , dtype=torch.long ) else: UpperCamelCase : List[str] = torch.tensor([0 for f in features] , dtype=torch.long ) # HACK(we will not use this anymore soon) UpperCamelCase : int = torch.tensor([f.label_ids for f in features] , dtype=torch.long ) return DataLoader( TensorDataset(A_ , A_ , A_ , A_ ) , batch_size=A_ ) def __UpperCamelCase( self , A_ , A_ ): '''simple docstring''' """Compute validation""" "" UpperCamelCase : str = {"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]} if self.config.model_type != "distilbert": UpperCamelCase : Optional[int] = ( batch[2] if self.config.model_type in ["bert", "xlnet"] else None ) # XLM and RoBERTa don"t use token_type_ids UpperCamelCase : List[str] = self(**A_ ) UpperCamelCase , UpperCamelCase : List[Any] = outputs[:2] UpperCamelCase : int = logits.detach().cpu().numpy() UpperCamelCase : Dict = inputs["labels"].detach().cpu().numpy() return {"val_loss": tmp_eval_loss.detach().cpu(), "pred": preds, "target": out_label_ids} def __UpperCamelCase( self , A_ ): '''simple docstring''' UpperCamelCase : List[str] = torch.stack([x["val_loss"] for x in outputs] ).mean() UpperCamelCase : Union[str, Any] = np.concatenate([x["pred"] for x in outputs] , axis=0 ) UpperCamelCase : int = np.argmax(A_ , axis=2 ) UpperCamelCase : Optional[int] = np.concatenate([x["target"] for x in outputs] , axis=0 ) UpperCamelCase : List[Any] = dict(enumerate(self.labels ) ) UpperCamelCase : Union[str, Any] = [[] for _ in range(out_label_ids.shape[0] )] UpperCamelCase : Optional[int] = [[] for _ in range(out_label_ids.shape[0] )] for i in range(out_label_ids.shape[0] ): for j in range(out_label_ids.shape[1] ): if out_label_ids[i, j] != self.pad_token_label_id: out_label_list[i].append(label_map[out_label_ids[i][j]] ) preds_list[i].append(label_map[preds[i][j]] ) UpperCamelCase : Dict = { "val_loss": val_loss_mean, "accuracy_score": accuracy_score(A_ , A_ ), "precision": precision_score(A_ , A_ ), "recall": recall_score(A_ , A_ ), "f1": fa_score(A_ , A_ ), } UpperCamelCase : List[str] = dict(results.items() ) UpperCamelCase : Union[str, Any] = results return ret, preds_list, out_label_list def __UpperCamelCase( self , A_ ): '''simple docstring''' UpperCamelCase , UpperCamelCase , UpperCamelCase : int = self._eval_end(A_ ) UpperCamelCase : Union[str, Any] = ret["log"] return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs} def __UpperCamelCase( self , A_ ): '''simple docstring''' UpperCamelCase , UpperCamelCase , UpperCamelCase : str = self._eval_end(A_ ) # Converting to the dict required by pl # https://github.com/PyTorchLightning/pytorch-lightning/blob/master/\ # pytorch_lightning/trainer/logging.py#L139 UpperCamelCase : Optional[int] = ret["log"] # `val_loss` is the key returned by `self._eval_end()` but actually refers to `test_loss` return {"avg_test_loss": logs["val_loss"], "log": logs, "progress_bar": logs} @staticmethod def __UpperCamelCase( A_ , A_ ): '''simple docstring''' BaseTransformer.add_model_specific_args(A_ , A_ ) parser.add_argument( "--task_type" , default="NER" , type=A_ , help="Task type to fine tune in training (e.g. NER, POS, etc)" ) parser.add_argument( "--max_seq_length" , default=128 , type=A_ , help=( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) , ) parser.add_argument( "--labels" , default="" , type=A_ , help="Path to a file containing all labels. If not specified, CoNLL-2003 labels are used." , ) parser.add_argument( "--gpus" , default=0 , type=A_ , help="The number of GPUs allocated for this, it is by default 0 meaning none" , ) parser.add_argument( "--overwrite_cache" , action="store_true" , help="Overwrite the cached training and evaluation sets" ) return parser if __name__ == "__main__": __lowerCamelCase : List[str] = argparse.ArgumentParser() add_generic_args(parser, os.getcwd()) __lowerCamelCase : Tuple = NERTransformer.add_model_specific_args(parser, os.getcwd()) __lowerCamelCase : List[str] = parser.parse_args() __lowerCamelCase : Any = NERTransformer(args) __lowerCamelCase : Any = generic_train(model, args) if args.do_predict: # See https://github.com/huggingface/transformers/issues/3159 # pl use this default format to create a checkpoint: # https://github.com/PyTorchLightning/pytorch-lightning/blob/master\ # /pytorch_lightning/callbacks/model_checkpoint.py#L322 __lowerCamelCase : Optional[int] = sorted(glob.glob(os.path.join(args.output_dir, """checkpoint-epoch=*.ckpt"""), recursive=True)) __lowerCamelCase : Dict = model.load_from_checkpoint(checkpoints[-1]) trainer.test(model)
52
A_ :str = '''0.21.0''' from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
71
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) a__ : Optional[Any] ={ '''configuration_mobilebert''': [ '''MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MobileBertConfig''', '''MobileBertOnnxConfig''', ], '''tokenization_mobilebert''': ['''MobileBertTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ : Any =['''MobileBertTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ : Dict =[ '''MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MobileBertForMaskedLM''', '''MobileBertForMultipleChoice''', '''MobileBertForNextSentencePrediction''', '''MobileBertForPreTraining''', '''MobileBertForQuestionAnswering''', '''MobileBertForSequenceClassification''', '''MobileBertForTokenClassification''', '''MobileBertLayer''', '''MobileBertModel''', '''MobileBertPreTrainedModel''', '''load_tf_weights_in_mobilebert''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ : List[Any] =[ '''TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFMobileBertForMaskedLM''', '''TFMobileBertForMultipleChoice''', '''TFMobileBertForNextSentencePrediction''', '''TFMobileBertForPreTraining''', '''TFMobileBertForQuestionAnswering''', '''TFMobileBertForSequenceClassification''', '''TFMobileBertForTokenClassification''', '''TFMobileBertMainLayer''', '''TFMobileBertModel''', '''TFMobileBertPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mobilebert import ( MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileBertConfig, MobileBertOnnxConfig, ) from .tokenization_mobilebert import MobileBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mobilebert_fast import MobileBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilebert import ( MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, MobileBertForMaskedLM, MobileBertForMultipleChoice, MobileBertForNextSentencePrediction, MobileBertForPreTraining, MobileBertForQuestionAnswering, MobileBertForSequenceClassification, MobileBertForTokenClassification, MobileBertLayer, MobileBertModel, MobileBertPreTrainedModel, load_tf_weights_in_mobilebert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mobilebert import ( TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFMobileBertForMaskedLM, TFMobileBertForMultipleChoice, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertMainLayer, TFMobileBertModel, TFMobileBertPreTrainedModel, ) else: import sys a__ : Tuple =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
53
A_ :Union[str, Any] = { 0: '''0''', 1: '''1''', 2: '''2''', 3: '''3''', 4: '''4''', 5: '''5''', 6: '''6''', 7: '''7''', 8: '''8''', 9: '''9''', 10: '''a''', 11: '''b''', 12: '''c''', 13: '''d''', 14: '''e''', 15: '''f''', } def A ( a_ ) -> str: assert type(a_ ) in (int, float) and decimal == int(a_ ) __UpperCamelCase : Union[str, Any] =int(a_ ) __UpperCamelCase : List[str] ='' __UpperCamelCase : Optional[Any] =False if decimal < 0: __UpperCamelCase : Tuple =True decimal *= -1 while decimal > 0: __UpperCamelCase , __UpperCamelCase : Optional[Any] =divmod(a_ ,16 ) __UpperCamelCase : Tuple =values[remainder] + hexadecimal __UpperCamelCase : Dict ='0x' + hexadecimal if negative: __UpperCamelCase : int ='-' + hexadecimal return hexadecimal if __name__ == "__main__": import doctest doctest.testmod()
71
0
"""simple docstring""" import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger a__ : List[str] = get_logger(__name__) a__ : Optional[Any] = Path(__file__).parent / '''model_card_template.md''' a__ : Optional[int] = uuida().hex a__ : Any = os.getenv('''HF_HUB_OFFLINE''', '''''').upper() in ENV_VARS_TRUE_VALUES a__ : Any = os.getenv('''DISABLE_TELEMETRY''', '''''').upper() in ENV_VARS_TRUE_VALUES a__ : int = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '''/api/telemetry/''' def UpperCAmelCase__ (lowerCAmelCase_ = None ): '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}""" if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += f"""; torch/{_torch_version}""" if is_flax_available(): ua += f"""; jax/{_jax_version}""" ua += f"""; flax/{_flax_version}""" if is_onnx_available(): ua += f"""; onnxruntime/{_onnxruntime_version}""" # CI will set this value to True if os.environ.get("DIFFUSERS_IS_CI" , "" ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): ua += "; " + "; ".join(f"""{k}/{v}""" for k, v in user_agent.items() ) elif isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): ua += "; " + user_agent return ua def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ = None , lowerCAmelCase_ = None ): '''simple docstring''' if token is None: __SCREAMING_SNAKE_CASE = HfFolder.get_token() if organization is None: __SCREAMING_SNAKE_CASE = whoami(lowerCAmelCase_ )["name"] return f"""{username}/{model_id}""" else: return f"""{organization}/{model_id}""" def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ ): '''simple docstring''' if not is_jinja_available(): raise ValueError( "Modelcard rendering is based on Jinja templates." " Please make sure to have `jinja` installed before using `create_model_card`." " To install it, please run `pip install Jinja2`." ) if hasattr(lowerCAmelCase_ , "local_rank" ) and args.local_rank not in [-1, 0]: return __SCREAMING_SNAKE_CASE = args.hub_token if hasattr(lowerCAmelCase_ , "hub_token" ) else None __SCREAMING_SNAKE_CASE = get_full_repo_name(lowerCAmelCase_ , token=lowerCAmelCase_ ) __SCREAMING_SNAKE_CASE = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language="en" , license="apache-2.0" , library_name="diffusers" , tags=[] , datasets=args.dataset_name , metrics=[] , ) , template_path=lowerCAmelCase_ , model_name=lowerCAmelCase_ , repo_name=lowerCAmelCase_ , dataset_name=args.dataset_name if hasattr(lowerCAmelCase_ , "dataset_name" ) else None , learning_rate=args.learning_rate , train_batch_size=args.train_batch_size , eval_batch_size=args.eval_batch_size , gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(lowerCAmelCase_ , "gradient_accumulation_steps" ) else None ) , adam_betaa=args.adam_betaa if hasattr(lowerCAmelCase_ , "adam_beta1" ) else None , adam_betaa=args.adam_betaa if hasattr(lowerCAmelCase_ , "adam_beta2" ) else None , adam_weight_decay=args.adam_weight_decay if hasattr(lowerCAmelCase_ , "adam_weight_decay" ) else None , adam_epsilon=args.adam_epsilon if hasattr(lowerCAmelCase_ , "adam_epsilon" ) else None , lr_scheduler=args.lr_scheduler if hasattr(lowerCAmelCase_ , "lr_scheduler" ) else None , lr_warmup_steps=args.lr_warmup_steps if hasattr(lowerCAmelCase_ , "lr_warmup_steps" ) else None , ema_inv_gamma=args.ema_inv_gamma if hasattr(lowerCAmelCase_ , "ema_inv_gamma" ) else None , ema_power=args.ema_power if hasattr(lowerCAmelCase_ , "ema_power" ) else None , ema_max_decay=args.ema_max_decay if hasattr(lowerCAmelCase_ , "ema_max_decay" ) else None , mixed_precision=args.mixed_precision , ) __SCREAMING_SNAKE_CASE = os.path.join(args.output_dir , "README.md" ) model_card.save(lowerCAmelCase_ ) def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ = None ): '''simple docstring''' if resolved_file is None or commit_hash is not None: return commit_hash __SCREAMING_SNAKE_CASE = str(Path(lowerCAmelCase_ ).as_posix() ) __SCREAMING_SNAKE_CASE = re.search(R"snapshots/([^/]+)/" , lowerCAmelCase_ ) if search is None: return None __SCREAMING_SNAKE_CASE = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(lowerCAmelCase_ ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. a__ : Tuple = os.path.expanduser( os.getenv('''HF_HOME''', os.path.join(os.getenv('''XDG_CACHE_HOME''', '''~/.cache'''), '''huggingface''')) ) a__ : int = os.path.join(hf_cache_home, '''diffusers''') def UpperCAmelCase__ (lowerCAmelCase_ = None , lowerCAmelCase_ = None ): '''simple docstring''' if new_cache_dir is None: __SCREAMING_SNAKE_CASE = DIFFUSERS_CACHE if old_cache_dir is None: __SCREAMING_SNAKE_CASE = old_diffusers_cache __SCREAMING_SNAKE_CASE = Path(lowerCAmelCase_ ).expanduser() __SCREAMING_SNAKE_CASE = Path(lowerCAmelCase_ ).expanduser() for old_blob_path in old_cache_dir.glob("**/blobs/*" ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): __SCREAMING_SNAKE_CASE = new_cache_dir / old_blob_path.relative_to(lowerCAmelCase_ ) new_blob_path.parent.mkdir(parents=lowerCAmelCase_ , exist_ok=lowerCAmelCase_ ) os.replace(lowerCAmelCase_ , lowerCAmelCase_ ) try: os.symlink(lowerCAmelCase_ , lowerCAmelCase_ ) except OSError: logger.warning( "Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded." ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). a__ : int = os.path.join(DIFFUSERS_CACHE, '''version_diffusers_cache.txt''') if not os.path.isfile(cache_version_file): a__ : Any = 0 else: with open(cache_version_file) as f: try: a__ : Optional[int] = int(f.read()) except ValueError: a__ : Optional[int] = 0 if cache_version < 1: a__ : List[Any] = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( '''The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ''' '''existing cached models. This is a one-time operation, you can interrupt it or run it ''' '''later by calling `diffusers.utils.hub_utils.move_cache()`.''' ) try: move_cache() except Exception as e: a__ : Union[str, Any] = '''\n'''.join(traceback.format_tb(e.__traceback__)) logger.error( F"There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease " '''file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ''' '''message and we will do our best to help.''' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, '''w''') as f: f.write('''1''') except Exception: logger.warning( F"There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure " '''the directory exists and can be written to.''' ) def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ = None ): '''simple docstring''' if variant is not None: __SCREAMING_SNAKE_CASE = weights_name.split("." ) __SCREAMING_SNAKE_CASE = splits[:-1] + [variant] + splits[-1:] __SCREAMING_SNAKE_CASE = ".".join(lowerCAmelCase_ ) return weights_name def UpperCAmelCase__ (lowerCAmelCase_ , *, lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_=None , ): '''simple docstring''' __SCREAMING_SNAKE_CASE = str(lowerCAmelCase_ ) if os.path.isfile(lowerCAmelCase_ ): return pretrained_model_name_or_path elif os.path.isdir(lowerCAmelCase_ ): if os.path.isfile(os.path.join(lowerCAmelCase_ , lowerCAmelCase_ ) ): # Load from a PyTorch checkpoint __SCREAMING_SNAKE_CASE = os.path.join(lowerCAmelCase_ , lowerCAmelCase_ ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) ): __SCREAMING_SNAKE_CASE = os.path.join(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) return model_file else: raise EnvironmentError( f"""Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.""" ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(lowerCAmelCase_ ).base_version ) >= version.parse("0.20.0" ) ): try: __SCREAMING_SNAKE_CASE = hf_hub_download( lowerCAmelCase_ , filename=_add_variant(lowerCAmelCase_ , lowerCAmelCase_ ) , cache_dir=lowerCAmelCase_ , force_download=lowerCAmelCase_ , proxies=lowerCAmelCase_ , resume_download=lowerCAmelCase_ , local_files_only=lowerCAmelCase_ , use_auth_token=lowerCAmelCase_ , user_agent=lowerCAmelCase_ , subfolder=lowerCAmelCase_ , revision=revision or commit_hash , ) warnings.warn( f"""Loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'` is deprecated. Loading instead from `revision='main'` with `variant={revision}`. Loading model variants via `revision='{revision}'` will be removed in diffusers v1. Please use `variant='{revision}'` instead.""" , lowerCAmelCase_ , ) return model_file except: # noqa: E722 warnings.warn( f"""You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant='{revision}'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(lowerCAmelCase_ , lowerCAmelCase_ )} file in the 'main' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title '{pretrained_model_name_or_path} is missing {_add_variant(lowerCAmelCase_ , lowerCAmelCase_ )}' so that the correct variant file can be added.""" , lowerCAmelCase_ , ) try: # 2. Load model file as usual __SCREAMING_SNAKE_CASE = hf_hub_download( lowerCAmelCase_ , filename=lowerCAmelCase_ , cache_dir=lowerCAmelCase_ , force_download=lowerCAmelCase_ , proxies=lowerCAmelCase_ , resume_download=lowerCAmelCase_ , local_files_only=lowerCAmelCase_ , use_auth_token=lowerCAmelCase_ , user_agent=lowerCAmelCase_ , subfolder=lowerCAmelCase_ , revision=revision or commit_hash , ) return model_file except RepositoryNotFoundError: raise EnvironmentError( f"""{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier """ "listed on 'https://huggingface.co/models'\nIf this is a private repository, make sure to pass a " "token having permission to this repo with `use_auth_token` or log in with `huggingface-cli " "login`." ) except RevisionNotFoundError: raise EnvironmentError( f"""{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for """ "this model name. Check the model page at " f"""'https://huggingface.co/{pretrained_model_name_or_path}' for available revisions.""" ) except EntryNotFoundError: raise EnvironmentError( f"""{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.""" ) except HTTPError as err: raise EnvironmentError( f"""There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}""" ) except ValueError: raise EnvironmentError( f"""We couldn't connect to '{HUGGINGFACE_CO_RESOLVE_ENDPOINT}' to load this model, couldn't find it""" f""" in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a""" f""" directory containing a file named {weights_name} or""" " \nCheckout your internet connection or see how to run the library in" " offline mode at 'https://huggingface.co/docs/diffusers/installation#offline-mode'." ) except EnvironmentError: raise EnvironmentError( f"""Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it from """ "'https://huggingface.co/models', make sure you don't have a local directory with the same name. " f"""Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory """ f"""containing a file named {weights_name}""" )
54
import itertools import os from collections import Counter, defaultdict from concurrent.futures import ThreadPoolExecutor, as_completed import numpy as np import datasets from .execute import check_correctness A_ :List[str] = '''\ @misc{chen2021evaluating, title={Evaluating Large Language Models Trained on Code}, author={Mark Chen and Jerry Tworek and Heewoo Jun and Qiming Yuan \ and Henrique Ponde de Oliveira Pinto and Jared Kaplan and Harri Edwards \ and Yuri Burda and Nicholas Joseph and Greg Brockman and Alex Ray \ and Raul Puri and Gretchen Krueger and Michael Petrov and Heidy Khlaaf \ and Girish Sastry and Pamela Mishkin and Brooke Chan and Scott Gray \ and Nick Ryder and Mikhail Pavlov and Alethea Power and Lukasz Kaiser \ and Mohammad Bavarian and Clemens Winter and Philippe Tillet \ and Felipe Petroski Such and Dave Cummings and Matthias Plappert \ and Fotios Chantzis and Elizabeth Barnes and Ariel Herbert-Voss \ and William Hebgen Guss and Alex Nichol and Alex Paino and Nikolas Tezak \ and Jie Tang and Igor Babuschkin and Suchir Balaji and Shantanu Jain \ and William Saunders and Christopher Hesse and Andrew N. Carr \ and Jan Leike and Josh Achiam and Vedant Misra and Evan Morikawa \ and Alec Radford and Matthew Knight and Miles Brundage and Mira Murati \ and Katie Mayer and Peter Welinder and Bob McGrew and Dario Amodei \ and Sam McCandlish and Ilya Sutskever and Wojciech Zaremba}, year={2021}, eprint={2107.03374}, archivePrefix={arXiv}, primaryClass={cs.LG} } ''' A_ :Any = '''\ This metric implements the evaluation harness for the HumanEval problem solving dataset described in the paper "Evaluating Large Language Models Trained on Code" (https://arxiv.org/abs/2107.03374). ''' A_ :Tuple = ''' Calculates how good are predictions given some references, using certain scores Args: predictions: list of candidates to evaluate. Each candidates should be a list of strings with several code candidates to solve the problem. references: a list with a test for each prediction. Each test should evaluate the correctness of a code candidate. k: number of code candidates to consider in the evaluation (Default: [1, 10, 100]) num_workers: number of workers used to evaluate the canidate programs (Default: 4). timeout: Returns: pass_at_k: dict with pass rates for each k results: dict with granular results of each unittest Examples: >>> code_eval = datasets.load_metric("code_eval") >>> test_cases = ["assert add(2,3)==5"] >>> candidates = [["def add(a,b): return a*b", "def add(a, b): return a+b"]] >>> pass_at_k, results = code_eval.compute(references=test_cases, predictions=candidates, k=[1, 2]) >>> print(pass_at_k) {\'pass@1\': 0.5, \'pass@2\': 1.0} ''' A_ :List[str] = ''' ################################################################################ !!!WARNING!!! ################################################################################ The "code_eval" metric executes untrusted model-generated code in Python. Although it is highly unlikely that model-generated code will do something overtly malicious in response to this test suite, model-generated code may act destructively due to a lack of model capability or alignment. Users are strongly encouraged to sandbox this evaluation suite so that it does not perform destructive actions on their host or network. For more information on how OpenAI sandboxes its code, see the paper "Evaluating Large Language Models Trained on Code" (https://arxiv.org/abs/2107.03374). Once you have read this disclaimer and taken appropriate precautions, set the environment variable HF_ALLOW_CODE_EVAL="1". Within Python you can to this with: >>> import os >>> os.environ["HF_ALLOW_CODE_EVAL"] = "1" ################################################################################\ ''' A_ :Tuple = '''The MIT License Copyright (c) OpenAI (https://openai.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __A ( datasets.Metric ): """simple docstring""" def __lowercase ( self ): """simple docstring""" return datasets.MetricInfo( # This is the description that will appear on the metrics page. description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string' ) ), 'references': datasets.Value('string' ), } ) , homepage='https://github.com/openai/human-eval' , codebase_urls=['https://github.com/openai/human-eval'] , reference_urls=['https://github.com/openai/human-eval'] , license=_LICENSE , ) def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__=[1, 10, 100] , lowerCamelCase__=4 , lowerCamelCase__=3.0 ): """simple docstring""" if os.getenv('HF_ALLOW_CODE_EVAL' , 0 ) != "1": raise ValueError(_WARNING ) if os.name == "nt": raise NotImplementedError('This metric is currently not supported on Windows.' ) with ThreadPoolExecutor(max_workers=lowerCamelCase__ ) as executor: __UpperCamelCase : List[str] =[] __UpperCamelCase : Any =Counter() __UpperCamelCase : List[Any] =0 __UpperCamelCase : int =defaultdict(lowerCamelCase__ ) for task_id, (candidates, test_case) in enumerate(zip(lowerCamelCase__ , lowerCamelCase__ ) ): for candidate in candidates: __UpperCamelCase : str =candidate + '\n' + test_case __UpperCamelCase : Any =(test_program, timeout, task_id, completion_id[task_id]) __UpperCamelCase : Optional[Any] =executor.submit(lowerCamelCase__ , *lowerCamelCase__ ) futures.append(lowerCamelCase__ ) completion_id[task_id] += 1 n_samples += 1 for future in as_completed(lowerCamelCase__ ): __UpperCamelCase : str =future.result() results[result["task_id"]].append((result['completion_id'], result) ) __UpperCamelCase , __UpperCamelCase : int =[], [] for result in results.values(): result.sort() __UpperCamelCase : str =[r[1]['passed'] for r in result] total.append(len(lowerCamelCase__ ) ) correct.append(sum(lowerCamelCase__ ) ) __UpperCamelCase : Optional[int] =np.array(lowerCamelCase__ ) __UpperCamelCase : List[str] =np.array(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =k __UpperCamelCase : List[Any] ={f'pass@{k}': estimate_pass_at_k(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ).mean() for k in ks if (total >= k).all()} return pass_at_k, results def A ( a_ ,a_ ,a_ ) -> Optional[int]: def estimator(a_ ,a_ ,a_ ) -> float: if n - c < k: return 1.0 return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1 ,n + 1 ) ) if isinstance(a_ ,a_ ): __UpperCamelCase : Optional[int] =itertools.repeat(a_ ,len(a_ ) ) else: assert len(a_ ) == len(a_ ) __UpperCamelCase : List[Any] =iter(a_ ) return np.array([estimator(int(a_ ) ,int(a_ ) ,a_ ) for n, c in zip(a_ ,a_ )] )
71
0
'''simple docstring''' import io import os import unicodedata from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging a_ : Union[str, Any] = logging.get_logger(__name__) a_ : List[Any] = """▁""" a_ : List[Any] = {"""vocab_file""": """vocab.txt""", """sentencepiece_model_ckpt""": """sentencepiece.bpe.model"""} a_ : Optional[int] = { """sentencepiece_model_file""": """sentencepiece.bpe.model""", """vocab_file""": """vocab.txt""", } a_ : Tuple = { """vocab_file""": { """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""", """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""", }, """sentencepiece_model_file""": { """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""", """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""", }, } a_ : Dict = { """ernie-m-base""": 514, """ernie-m-large""": 514, } a_ : Any = { """ernie-m-base""": {"""do_lower_case""": False}, """ernie-m-large""": {"""do_lower_case""": False}, } class snake_case ( lowercase ): """simple docstring""" _lowerCamelCase = ["input_ids"] _lowerCamelCase = VOCAB_FILES_NAMES _lowerCamelCase = PRETRAINED_INIT_CONFIGURATION _lowerCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCamelCase = PRETRAINED_VOCAB_FILES_MAP _lowerCamelCase = RESOURCE_FILES_NAMES def __init__( self , UpperCamelCase , UpperCamelCase=None , UpperCamelCase=False , UpperCamelCase="utf8" , UpperCamelCase="[UNK]" , UpperCamelCase="[SEP]" , UpperCamelCase="[PAD]" , UpperCamelCase="[CLS]" , UpperCamelCase="[MASK]" , UpperCamelCase = None , **UpperCamelCase , ): """simple docstring""" # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. lowerCamelCase_ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=UpperCamelCase , unk_token=UpperCamelCase , sep_token=UpperCamelCase , pad_token=UpperCamelCase , cls_token=UpperCamelCase , mask_token=UpperCamelCase , vocab_file=UpperCamelCase , encoding=UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **UpperCamelCase , ) lowerCamelCase_ = do_lower_case lowerCamelCase_ = sentencepiece_model_ckpt lowerCamelCase_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(UpperCamelCase ) # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning if vocab_file is not None: lowerCamelCase_ = self.load_vocab(filepath=UpperCamelCase ) else: lowerCamelCase_ = {self.sp_model.id_to_piece(UpperCamelCase ): id for id in range(self.sp_model.get_piece_size() )} lowerCamelCase_ = {v: k for k, v in self.vocab.items()} def snake_case ( self , UpperCamelCase ): """simple docstring""" if text is None: return None lowerCamelCase_ = self.tokenize(UpperCamelCase ) lowerCamelCase_ ,lowerCamelCase_ = "", [] for i, ch in enumerate(UpperCamelCase ): if ch in self.SP_CHAR_MAPPING: lowerCamelCase_ = self.SP_CHAR_MAPPING.get(UpperCamelCase ) else: lowerCamelCase_ = unicodedata.normalize("NFKC" , UpperCamelCase ) if self.is_whitespace(UpperCamelCase ): continue normalized_text += ch char_mapping.extend([i] * len(UpperCamelCase ) ) lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ = normalized_text, [], 0 if self.do_lower_case: lowerCamelCase_ = text.lower() for token in split_tokens: if token[:1] == "▁": lowerCamelCase_ = token[1:] lowerCamelCase_ = text[offset:].index(UpperCamelCase ) + offset lowerCamelCase_ = start + len(UpperCamelCase ) token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1) ) lowerCamelCase_ = end return token_mapping @property def snake_case ( self ): """simple docstring""" return len(self.vocab ) def snake_case ( self ): """simple docstring""" return dict(self.vocab , **self.added_tokens_encoder ) def __getstate__( self ): """simple docstring""" lowerCamelCase_ = self.__dict__.copy() lowerCamelCase_ = None return state def __setstate__( self , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): lowerCamelCase_ = {} lowerCamelCase_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.sentencepiece_model_ckpt ) def snake_case ( self , UpperCamelCase ): """simple docstring""" return "".join((self.SP_CHAR_MAPPING.get(UpperCamelCase , UpperCamelCase ) for c in text) ) def snake_case ( self , UpperCamelCase , UpperCamelCase=False , UpperCamelCase=64 , UpperCamelCase=0.1 ): """simple docstring""" if self.sp_model_kwargs.get("enable_sampling" ) is True: lowerCamelCase_ = True if self.sp_model_kwargs.get("alpha" ) is not None: lowerCamelCase_ = self.sp_model_kwargs.get("alpha" ) if self.sp_model_kwargs.get("nbest_size" ) is not None: lowerCamelCase_ = self.sp_model_kwargs.get("nbest_size" ) if not enable_sampling: lowerCamelCase_ = self.sp_model.EncodeAsPieces(UpperCamelCase ) else: lowerCamelCase_ = self.sp_model.SampleEncodeAsPieces(UpperCamelCase , UpperCamelCase , UpperCamelCase ) lowerCamelCase_ = [] for pi, piece in enumerate(UpperCamelCase ): if piece == SPIECE_UNDERLINE: if not pieces[pi + 1].startswith(UpperCamelCase ) and pi != 0: new_pieces.append(UpperCamelCase ) continue else: continue lowerCamelCase_ = 0 for i, chunk in enumerate(UpperCamelCase ): if chunk == SPIECE_UNDERLINE: continue if self.is_ch_char(UpperCamelCase ) or self.is_punct(UpperCamelCase ): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) new_pieces.append(UpperCamelCase ) lowerCamelCase_ = i + 1 elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) lowerCamelCase_ = i elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) lowerCamelCase_ = i if len(UpperCamelCase ) > lst_i: new_pieces.append(piece[lst_i:] ) return new_pieces def snake_case ( self , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = "".join(UpperCamelCase ).replace(UpperCamelCase , " " ).strip() return out_string def snake_case ( self , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = self.convert_ids_to_tokens(UpperCamelCase ) lowerCamelCase_ = "".join(UpperCamelCase ).replace(UpperCamelCase , " " ).strip() return out_string def snake_case ( self , UpperCamelCase ): """simple docstring""" return self.vocab.get(UpperCamelCase , self.vocab.get(self.unk_token ) ) def snake_case ( self , UpperCamelCase ): """simple docstring""" return self.reverse_vocab.get(UpperCamelCase , self.unk_token ) def snake_case ( self , UpperCamelCase , UpperCamelCase=None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] lowerCamelCase_ = [self.cls_token_id] lowerCamelCase_ = [self.sep_token_id] return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep def snake_case ( self , UpperCamelCase , UpperCamelCase=None ): """simple docstring""" if offset_mapping_a is None: return [(0, 0)] + offset_mapping_a + [(0, 0)] return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)] def snake_case ( self , UpperCamelCase , UpperCamelCase=None , UpperCamelCase=False ): """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 not None: return [1] + ([0] * len(UpperCamelCase )) + [1, 1] + ([0] * len(UpperCamelCase )) + [1] return [1] + ([0] * len(UpperCamelCase )) + [1] def snake_case ( self , UpperCamelCase , UpperCamelCase = None ): """simple docstring""" # called when `add_special_tokens` is True, so align with `build_inputs_with_special_tokens` method if token_ids_a is None: # [CLS] X [SEP] return (len(UpperCamelCase ) + 2) * [0] # [CLS] A [SEP] [SEP] B [SEP] return [0] * (len(UpperCamelCase ) + 1) + [1] * (len(UpperCamelCase ) + 3) def snake_case ( self , UpperCamelCase ): """simple docstring""" if "\u4e00" <= char <= "\u9fff": return True return False def snake_case ( self , UpperCamelCase ): """simple docstring""" if ("a" <= char <= "z") or ("A" <= char <= "Z"): return True return False def snake_case ( self , UpperCamelCase ): """simple docstring""" if char in ",;:.?!~,;:。?!《》【】": return True return False def snake_case ( self , UpperCamelCase ): """simple docstring""" if char == " " or char == "\t" or char == "\n" or char == "\r": return True if len(UpperCamelCase ) == 1: lowerCamelCase_ = unicodedata.category(UpperCamelCase ) if cat == "Zs": return True return False def snake_case ( self , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = {} with io.open(UpperCamelCase , "r" , encoding="utf-8" ) as f: for index, line in enumerate(UpperCamelCase ): lowerCamelCase_ = line.rstrip("\n" ) lowerCamelCase_ = int(UpperCamelCase ) return token_to_idx def snake_case ( self , UpperCamelCase , UpperCamelCase = None ): """simple docstring""" lowerCamelCase_ = 0 if os.path.isdir(UpperCamelCase ): lowerCamelCase_ = os.path.join( UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) else: lowerCamelCase_ = (filename_prefix + "-" if filename_prefix else "") + save_directory with open(UpperCamelCase , "w" , encoding="utf-8" ) as writer: for token, token_index in sorted(self.vocab.items() , key=lambda UpperCamelCase : kv[1] ): 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!" ) lowerCamelCase_ = token_index writer.write(token + "\n" ) index += 1 lowerCamelCase_ = os.path.join(UpperCamelCase , "sentencepiece.bpe.model" ) with open(UpperCamelCase , "wb" ) as fi: lowerCamelCase_ = self.sp_model.serialized_model_proto() fi.write(UpperCamelCase ) return (vocab_file,)
55
import gc import random import tempfile import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMInverseScheduler, DDIMScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, StableDiffusionDiffEditPipeline, UNetaDConditionModel, ) from diffusers.utils import load_image, slow from diffusers.utils.testing_utils import enable_full_determinism, floats_tensor, require_torch_gpu, torch_device from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __A ( a , a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : Optional[Any] =StableDiffusionDiffEditPipeline UpperCamelCase__ : str =TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""height""", """width""", """image"""} | {"""image_latents"""} UpperCamelCase__ : Optional[Any] =TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS - {"""image"""} | {"""image_latents"""} UpperCamelCase__ : Dict =frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess UpperCamelCase__ : Any =frozenset([] ) def __lowercase ( self ): """simple docstring""" torch.manual_seed(0 ) __UpperCamelCase : Dict =UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=lowerCamelCase__ , ) __UpperCamelCase : List[str] =DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=lowerCamelCase__ , set_alpha_to_one=lowerCamelCase__ , ) __UpperCamelCase : Union[str, Any] =DDIMInverseScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=lowerCamelCase__ , set_alpha_to_zero=lowerCamelCase__ , ) torch.manual_seed(0 ) __UpperCamelCase : Optional[int] =AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) __UpperCamelCase : Tuple =CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act='gelu' , projection_dim=512 , ) __UpperCamelCase : Any =CLIPTextModel(lowerCamelCase__ ) __UpperCamelCase : int =CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) __UpperCamelCase : Union[str, Any] ={ 'unet': unet, 'scheduler': scheduler, 'inverse_scheduler': inverse_scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" __UpperCamelCase : int =floats_tensor((1, 16, 16) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) __UpperCamelCase : Any =floats_tensor((1, 2, 4, 16, 16) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : Any =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : Optional[int] =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Dict ={ 'prompt': 'a dog and a newt', 'mask_image': mask, 'image_latents': latents, 'generator': generator, 'num_inference_steps': 2, 'inpaint_strength': 1.0, 'guidance_scale': 6.0, 'output_type': 'numpy', } return inputs def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" __UpperCamelCase : Tuple =floats_tensor((1, 3, 32, 32) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) __UpperCamelCase : int =image.cpu().permute(0 , 2 , 3 , 1 )[0] __UpperCamelCase : Optional[Any] =Image.fromarray(np.uinta(lowerCamelCase__ ) ).convert('RGB' ) if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : List[Any] =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : Any =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Optional[int] ={ 'image': image, 'source_prompt': 'a cat and a frog', 'target_prompt': 'a dog and a newt', 'generator': generator, 'num_inference_steps': 2, 'num_maps_per_mask': 2, 'mask_encode_strength': 1.0, 'guidance_scale': 6.0, 'output_type': 'numpy', } return inputs def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" __UpperCamelCase : str =floats_tensor((1, 3, 32, 32) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) __UpperCamelCase : Any =image.cpu().permute(0 , 2 , 3 , 1 )[0] __UpperCamelCase : int =Image.fromarray(np.uinta(lowerCamelCase__ ) ).convert('RGB' ) if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : Any =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : int =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Optional[int] ={ 'image': image, 'prompt': 'a cat and a frog', 'generator': generator, 'num_inference_steps': 2, 'inpaint_strength': 1.0, 'guidance_scale': 6.0, 'decode_latents': True, 'output_type': 'numpy', } return inputs def __lowercase ( self ): """simple docstring""" if not hasattr(self.pipeline_class , '_optional_components' ): return __UpperCamelCase : Optional[Any] =self.get_dummy_components() __UpperCamelCase : List[str] =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) # set all optional components to None and update pipeline config accordingly for optional_component in pipe._optional_components: setattr(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) pipe.register_modules(**{optional_component: None for optional_component in pipe._optional_components} ) __UpperCamelCase : Union[str, Any] =self.get_dummy_inputs(lowerCamelCase__ ) __UpperCamelCase : List[Any] =pipe(**lowerCamelCase__ )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : Tuple =self.pipeline_class.from_pretrained(lowerCamelCase__ ) pipe_loaded.to(lowerCamelCase__ ) pipe_loaded.set_progress_bar_config(disable=lowerCamelCase__ ) for optional_component in pipe._optional_components: self.assertTrue( getattr(lowerCamelCase__ , lowerCamelCase__ ) is None , f'`{optional_component}` did not stay set to None after loading.' , ) __UpperCamelCase : str =self.get_dummy_inputs(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =pipe_loaded(**lowerCamelCase__ )[0] __UpperCamelCase : Tuple =np.abs(output - output_loaded ).max() self.assertLess(lowerCamelCase__ , 1E-4 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Any ='cpu' __UpperCamelCase : Union[str, Any] =self.get_dummy_components() __UpperCamelCase : Any =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : int =self.get_dummy_mask_inputs(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =pipe.generate_mask(**lowerCamelCase__ ) __UpperCamelCase : int =mask[0, -3:, -3:] self.assertEqual(mask.shape , (1, 16, 16) ) __UpperCamelCase : Tuple =np.array([0] * 9 ) __UpperCamelCase : str =np.abs(mask_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) self.assertEqual(mask[0, -3, -4] , 0 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : int ='cpu' __UpperCamelCase : Union[str, Any] =self.get_dummy_components() __UpperCamelCase : Optional[Any] =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Dict =self.get_dummy_inversion_inputs(lowerCamelCase__ ) __UpperCamelCase : List[Any] =pipe.invert(**lowerCamelCase__ ).images __UpperCamelCase : Optional[Any] =image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) __UpperCamelCase : List[str] =np.array( [0.5_150, 0.5_134, 0.5_043, 0.5_376, 0.4_694, 0.51_050, 0.5_015, 0.4_407, 0.4_799] , ) __UpperCamelCase : int =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) def __lowercase ( self ): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=5E-3 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] ='cpu' __UpperCamelCase : int =self.get_dummy_components() __UpperCamelCase : str ={'beta_start': 0.00_085, 'beta_end': 0.012, 'beta_schedule': 'scaled_linear'} __UpperCamelCase : str =DPMSolverMultistepScheduler(**lowerCamelCase__ ) __UpperCamelCase : Dict =DPMSolverMultistepInverseScheduler(**lowerCamelCase__ ) __UpperCamelCase : Any =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Tuple =self.get_dummy_inversion_inputs(lowerCamelCase__ ) __UpperCamelCase : str =pipe.invert(**lowerCamelCase__ ).images __UpperCamelCase : List[Any] =image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) __UpperCamelCase : List[str] =np.array( [0.5_150, 0.5_134, 0.5_043, 0.5_376, 0.4_694, 0.51_050, 0.5_015, 0.4_407, 0.4_799] , ) __UpperCamelCase : Optional[Any] =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) @require_torch_gpu @slow class __A ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @classmethod def __lowercase ( cls ): """simple docstring""" __UpperCamelCase : Optional[int] =load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/diffedit/fruit.png' ) __UpperCamelCase : Union[str, Any] =raw_image.convert('RGB' ).resize((768, 768) ) __UpperCamelCase : List[Any] =raw_image def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[int] =torch.manual_seed(0 ) __UpperCamelCase : Dict =StableDiffusionDiffEditPipeline.from_pretrained( 'stabilityai/stable-diffusion-2-1' , safety_checker=lowerCamelCase__ , torch_dtype=torch.floataa ) __UpperCamelCase : List[str] =DDIMScheduler.from_config(pipe.scheduler.config ) __UpperCamelCase : List[str] =DDIMInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : List[str] ='a bowl of fruit' __UpperCamelCase : Dict ='a bowl of pears' __UpperCamelCase : Tuple =pipe.generate_mask( image=self.raw_image , source_prompt=lowerCamelCase__ , target_prompt=lowerCamelCase__ , generator=lowerCamelCase__ , ) __UpperCamelCase : int =pipe.invert( prompt=lowerCamelCase__ , image=self.raw_image , inpaint_strength=0.7 , generator=lowerCamelCase__ ).latents __UpperCamelCase : Dict =pipe( prompt=lowerCamelCase__ , mask_image=lowerCamelCase__ , image_latents=lowerCamelCase__ , generator=lowerCamelCase__ , negative_prompt=lowerCamelCase__ , inpaint_strength=0.7 , output_type='numpy' , ).images[0] __UpperCamelCase : str =( np.array( load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/diffedit/pears.png' ).resize((768, 768) ) ) / 255 ) assert np.abs((expected_image - image).max() ) < 5E-1 def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Any =torch.manual_seed(0 ) __UpperCamelCase : List[Any] =StableDiffusionDiffEditPipeline.from_pretrained( 'stabilityai/stable-diffusion-2-1' , safety_checker=lowerCamelCase__ , torch_dtype=torch.floataa ) __UpperCamelCase : Optional[Any] =DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) __UpperCamelCase : Optional[int] =DPMSolverMultistepInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Optional[Any] ='a bowl of fruit' __UpperCamelCase : int ='a bowl of pears' __UpperCamelCase : str =pipe.generate_mask( image=self.raw_image , source_prompt=lowerCamelCase__ , target_prompt=lowerCamelCase__ , generator=lowerCamelCase__ , ) __UpperCamelCase : List[str] =pipe.invert( prompt=lowerCamelCase__ , image=self.raw_image , inpaint_strength=0.7 , generator=lowerCamelCase__ , num_inference_steps=25 , ).latents __UpperCamelCase : List[str] =pipe( prompt=lowerCamelCase__ , mask_image=lowerCamelCase__ , image_latents=lowerCamelCase__ , generator=lowerCamelCase__ , negative_prompt=lowerCamelCase__ , inpaint_strength=0.7 , num_inference_steps=25 , output_type='numpy' , ).images[0] __UpperCamelCase : Tuple =( np.array( load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/diffedit/pears.png' ).resize((768, 768) ) ) / 255 ) assert np.abs((expected_image - image).max() ) < 5E-1
71
0
'''simple docstring''' import argparse import torch from torch import nn from transformers import SpeechaTextConfig, SpeechaTextForConditionalGeneration def __magic_name__ ( __UpperCAmelCase ) -> str: '''simple docstring''' snake_case_ = [ '''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(__UpperCAmelCase, __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase ) -> Optional[int]: '''simple docstring''' snake_case_ = list(s_dict.keys() ) for key in keys: if "transformer_layers" in key: snake_case_ = s_dict.pop(__UpperCAmelCase ) elif "subsample" in key: snake_case_ = s_dict.pop(__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' snake_case_ ,snake_case_ = emb.weight.shape snake_case_ = nn.Linear(__UpperCAmelCase, __UpperCAmelCase, bias=__UpperCAmelCase ) snake_case_ = emb.weight.data return lin_layer def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase ) -> Dict: '''simple docstring''' snake_case_ = torch.load(__UpperCAmelCase, map_location='''cpu''' ) snake_case_ = mam_aaa['''args'''] snake_case_ = mam_aaa['''model'''] snake_case_ = state_dict['''decoder.output_projection.weight'''] remove_ignore_keys_(__UpperCAmelCase ) rename_keys(__UpperCAmelCase ) snake_case_ = state_dict['''decoder.embed_tokens.weight'''].shape[0] snake_case_ = args.share_decoder_input_output_embed snake_case_ = [int(__UpperCAmelCase ) for i in args.conv_kernel_sizes.split(''',''' )] snake_case_ = SpeechaTextConfig( vocab_size=__UpperCAmelCase, 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(__UpperCAmelCase ), conv_channels=args.conv_channels, conv_kernel_sizes=__UpperCAmelCase, input_feat_per_channel=args.input_feat_per_channel, input_channels=args.input_channels, tie_word_embeddings=__UpperCAmelCase, num_beams=5, max_length=200, use_cache=__UpperCAmelCase, decoder_start_token_id=2, early_stopping=__UpperCAmelCase, ) snake_case_ = SpeechaTextForConditionalGeneration(__UpperCAmelCase ) snake_case_ ,snake_case_ = model.model.load_state_dict(__UpperCAmelCase, strict=__UpperCAmelCase ) if len(__UpperCAmelCase ) > 0 and not set(__UpperCAmelCase ) <= { "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: snake_case_ = make_linear_from_emb(model.model.decoder.embed_tokens ) else: snake_case_ = lm_head_weights model.save_pretrained(__UpperCAmelCase ) if __name__ == "__main__": a : Any = 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.') a : List[Any] = parser.parse_args() convert_fairseq_sat_checkpoint_to_tfms(args.fairseq_path, args.pytorch_dump_folder_path)
56
import random from .binary_exp_mod import bin_exp_mod def A ( a_ ,a_=1_000 ) -> Optional[Any]: if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd __UpperCamelCase : List[Any] =n - 1 __UpperCamelCase : Dict =0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) __UpperCamelCase : Optional[Any] =0 while count < prec: __UpperCamelCase : Dict =random.randint(2 ,n - 1 ) __UpperCamelCase : Optional[Any] =bin_exp_mod(a_ ,a_ ,a_ ) if b != 1: __UpperCamelCase : List[str] =True for _ in range(a_ ): if b == n - 1: __UpperCamelCase : Tuple =False break __UpperCamelCase : Dict =b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": A_ :str = abs(int(input('''Enter bound : ''').strip())) print('''Here\'s the list of primes:''') print(''', '''.join(str(i) for i in range(n + 1) if is_prime_big(i)))
71
0
"""simple docstring""" import importlib import torch import yaml from omegaconf import OmegaConf from taming.models.vqgan import VQModel def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase=False ): '''simple docstring''' __lowerCAmelCase = OmegaConf.load(_UpperCamelCase ) if display: print(yaml.dump(OmegaConf.to_container(_UpperCamelCase ) ) ) return config def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase=None , _UpperCamelCase=None ): '''simple docstring''' if conf_path is None: __lowerCAmelCase = "./model_checkpoints/vqgan_only.yaml" __lowerCAmelCase = load_config(_UpperCamelCase , display=_UpperCamelCase ) __lowerCAmelCase = VQModel(**config.model.params ) if ckpt_path is None: __lowerCAmelCase = "./model_checkpoints/vqgan_only.pt" __lowerCAmelCase = torch.load(_UpperCamelCase , map_location=_UpperCamelCase ) if ".ckpt" in ckpt_path: __lowerCAmelCase = sd["state_dict"] model.load_state_dict(_UpperCamelCase , strict=_UpperCamelCase ) model.to(_UpperCamelCase ) del sd return model def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = model.encode(_UpperCamelCase ) print(f"VQGAN --- {model.__class__.__name__}: latent shape: {z.shape[2:]}" ) __lowerCAmelCase = model.decode(_UpperCamelCase ) return xrec def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase=False ): '''simple docstring''' __lowerCAmelCase , __lowerCAmelCase = string.rsplit("." , 1 ) if reload: __lowerCAmelCase = importlib.import_module(_UpperCamelCase ) importlib.reload(_UpperCamelCase ) return getattr(importlib.import_module(_UpperCamelCase , package=_UpperCamelCase ) , cls ) def _lowerCamelCase ( _UpperCamelCase ): '''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 _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase=True , _UpperCamelCase=True ): '''simple docstring''' __lowerCAmelCase = instantiate_from_config(_UpperCamelCase ) if sd is not None: model.load_state_dict(_UpperCamelCase ) if gpu: model.cuda() if eval_mode: model.eval() return {"model": model} def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' if ckpt: __lowerCAmelCase = torch.load(_UpperCamelCase , map_location="cpu" ) __lowerCAmelCase = pl_sd["global_step"] print(f"loaded model from global step {global_step}." ) else: __lowerCAmelCase = {"state_dict": None} __lowerCAmelCase = None __lowerCAmelCase = load_model_from_config(config.model , pl_sd["state_dict"] , gpu=_UpperCamelCase , eval_mode=_UpperCamelCase )["model"] return model, global_step
57
from torch import nn class __A ( nn.Module ): """simple docstring""" def __init__( self , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" super().__init__() __UpperCamelCase : Dict =class_size __UpperCamelCase : Any =embed_size # self.mlp1 = nn.Linear(embed_size, embed_size) # self.mlp2 = (nn.Linear(embed_size, class_size)) __UpperCamelCase : Any =nn.Linear(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : List[Any] =self.mlp(lowerCamelCase__ ) return logits
71
0
'''simple docstring''' def lowerCamelCase ( __lowerCamelCase : list[int] ) ->int: 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""" ) _SCREAMING_SNAKE_CASE = _SCREAMING_SNAKE_CASE = _SCREAMING_SNAKE_CASE = numbers[0] for i in range(1 , len(__lowerCamelCase ) ): # update the maximum and minimum subarray products _SCREAMING_SNAKE_CASE = numbers[i] if number < 0: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = min_till_now, max_till_now _SCREAMING_SNAKE_CASE = max(__lowerCamelCase , max_till_now * number ) _SCREAMING_SNAKE_CASE = min(__lowerCamelCase , min_till_now * number ) # update the maximum product found till now _SCREAMING_SNAKE_CASE = max(__lowerCamelCase , __lowerCamelCase ) return max_prod
58
def A ( a_ ,a_ ,a_ ) -> int: def update_area_of_max_square(a_ ,a_ ) -> int: # BASE CASE if row >= rows or col >= cols: return 0 __UpperCamelCase : Optional[int] =update_area_of_max_square(a_ ,col + 1 ) __UpperCamelCase : List[str] =update_area_of_max_square(row + 1 ,col + 1 ) __UpperCamelCase : List[Any] =update_area_of_max_square(row + 1 ,a_ ) if mat[row][col]: __UpperCamelCase : Optional[Any] =1 + min([right, diagonal, down] ) __UpperCamelCase : Dict =max(largest_square_area[0] ,a_ ) return sub_problem_sol else: return 0 __UpperCamelCase : Union[str, Any] =[0] update_area_of_max_square(0 ,0 ) return largest_square_area[0] def A ( a_ ,a_ ,a_ ) -> int: def update_area_of_max_square_using_dp_array( a_ ,a_ ,a_ ) -> int: if row >= rows or col >= cols: return 0 if dp_array[row][col] != -1: return dp_array[row][col] __UpperCamelCase : Tuple =update_area_of_max_square_using_dp_array(a_ ,col + 1 ,a_ ) __UpperCamelCase : Optional[int] =update_area_of_max_square_using_dp_array(row + 1 ,col + 1 ,a_ ) __UpperCamelCase : Any =update_area_of_max_square_using_dp_array(row + 1 ,a_ ,a_ ) if mat[row][col]: __UpperCamelCase : Optional[Any] =1 + min([right, diagonal, down] ) __UpperCamelCase : str =max(largest_square_area[0] ,a_ ) __UpperCamelCase : Any =sub_problem_sol return sub_problem_sol else: return 0 __UpperCamelCase : Tuple =[0] __UpperCamelCase : List[Any] =[[-1] * cols for _ in range(a_ )] update_area_of_max_square_using_dp_array(0 ,0 ,a_ ) return largest_square_area[0] def A ( a_ ,a_ ,a_ ) -> int: __UpperCamelCase : Dict =[[0] * (cols + 1) for _ in range(rows + 1 )] __UpperCamelCase : int =0 for row in range(rows - 1 ,-1 ,-1 ): for col in range(cols - 1 ,-1 ,-1 ): __UpperCamelCase : Optional[Any] =dp_array[row][col + 1] __UpperCamelCase : int =dp_array[row + 1][col + 1] __UpperCamelCase : Tuple =dp_array[row + 1][col] if mat[row][col] == 1: __UpperCamelCase : Tuple =1 + min(a_ ,a_ ,a_ ) __UpperCamelCase : Any =max(dp_array[row][col] ,a_ ) else: __UpperCamelCase : Dict =0 return largest_square_area def A ( a_ ,a_ ,a_ ) -> int: __UpperCamelCase : Any =[0] * (cols + 1) __UpperCamelCase : List[Any] =[0] * (cols + 1) __UpperCamelCase : Tuple =0 for row in range(rows - 1 ,-1 ,-1 ): for col in range(cols - 1 ,-1 ,-1 ): __UpperCamelCase : Any =current_row[col + 1] __UpperCamelCase : Optional[Any] =next_row[col + 1] __UpperCamelCase : Union[str, Any] =next_row[col] if mat[row][col] == 1: __UpperCamelCase : Any =1 + min(a_ ,a_ ,a_ ) __UpperCamelCase : Optional[int] =max(current_row[col] ,a_ ) else: __UpperCamelCase : List[str] =0 __UpperCamelCase : Optional[Any] =current_row return largest_square_area if __name__ == "__main__": import doctest doctest.testmod() print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))
71
0
import pytest from datasets import inspect_metric, list_metrics, load_metric @pytest.fixture def UpperCamelCase ( __lowerCamelCase : Optional[int] ): monkeypatch.setattr("datasets.utils.deprecation_utils._emitted_deprecation_warnings" , set() ) @pytest.fixture def UpperCamelCase ( __lowerCamelCase : str ): class UpperCAmelCase : def __init__(self : Optional[int] , snake_case__ : str ) -> Any: '''simple docstring''' snake_case : List[str] = metric_id class UpperCAmelCase : A__ : List[str] = [MetricMock(A_ ) for metric_id in ["accuracy", "mse", "precision", "codeparrot/apps_metric"]] def _SCREAMING_SNAKE_CASE (self : int ) -> List[str]: '''simple docstring''' return self._metrics monkeypatch.setattr("datasets.inspect.huggingface_hub" , HfhMock() ) @pytest.mark.parametrize( "func, args" , [(load_metric, ("metrics/mse",)), (list_metrics, ()), (inspect_metric, ("metrics/mse", "tmp_path"))] ) def UpperCamelCase ( __lowerCamelCase : Optional[int] , __lowerCamelCase : Any , __lowerCamelCase : Any , __lowerCamelCase : int , __lowerCamelCase : Any ): if "tmp_path" in args: snake_case : str = tuple(arg if arg != "tmp_path" else tmp_path for arg in args ) with pytest.warns(__lowerCamelCase , match="https://huggingface.co/docs/evaluate" ): func(*__lowerCamelCase )
59
def A ( a_ ) -> int: __UpperCamelCase : Any =len(a_ ) while cur > 1: # Find the maximum number in arr __UpperCamelCase : Any =arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi __UpperCamelCase : Any =arr[mi::-1] + arr[mi + 1 : len(a_ )] # Reverse whole list __UpperCamelCase : str =arr[cur - 1 :: -1] + arr[cur : len(a_ )] cur -= 1 return arr if __name__ == "__main__": A_ :Dict = input('''Enter numbers separated by a comma:\n''').strip() A_ :Any = [int(item) for item in user_input.split(''',''')] print(pancake_sort(unsorted))
71
0
"""simple docstring""" import itertools import string from collections.abc import Generator, Iterable def _snake_case ( _snake_case : Iterable[str] , _snake_case : int ): lowerCAmelCase : Optional[int] = iter(_snake_case ) while True: lowerCAmelCase : Tuple = tuple(itertools.islice(_snake_case , _snake_case ) ) if not chunk: return yield chunk def _snake_case ( _snake_case : str ): lowerCAmelCase : List[Any] = ''''''.join([c.upper() for c in dirty if c in string.ascii_letters] ) lowerCAmelCase : Union[str, Any] = '''''' if len(_snake_case ) < 2: return dirty for i in range(len(_snake_case ) - 1 ): clean += dirty[i] if dirty[i] == dirty[i + 1]: clean += "X" clean += dirty[-1] if len(_snake_case ) & 1: clean += "X" return clean def _snake_case ( _snake_case : str ): # I and J are used interchangeably to allow # us to use a 5x5 table (25 letters) lowerCAmelCase : Dict = '''ABCDEFGHIKLMNOPQRSTUVWXYZ''' # we're using a list instead of a '2d' array because it makes the math # for setting up the table and doing the actual encoding/decoding simpler lowerCAmelCase : Any = [] # copy key chars into the table if they are in `alphabet` ignoring duplicates for char in key.upper(): if char not in table and char in alphabet: table.append(_snake_case ) # fill the rest of the table in with the remaining alphabet chars for char in alphabet: if char not in table: table.append(_snake_case ) return table def _snake_case ( _snake_case : str , _snake_case : str ): lowerCAmelCase : Union[str, Any] = generate_table(_snake_case ) lowerCAmelCase : str = prepare_input(_snake_case ) lowerCAmelCase : Any = '''''' # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(_snake_case , 2 ): lowerCAmelCase, lowerCAmelCase : List[Any] = divmod(table.index(_snake_case ) , 5 ) lowerCAmelCase, lowerCAmelCase : List[str] = divmod(table.index(_snake_case ) , 5 ) if rowa == rowa: ciphertext += table[rowa * 5 + (cola + 1) % 5] ciphertext += table[rowa * 5 + (cola + 1) % 5] elif cola == cola: ciphertext += table[((rowa + 1) % 5) * 5 + cola] ciphertext += table[((rowa + 1) % 5) * 5 + cola] else: # rectangle ciphertext += table[rowa * 5 + cola] ciphertext += table[rowa * 5 + cola] return ciphertext def _snake_case ( _snake_case : str , _snake_case : str ): lowerCAmelCase : Dict = generate_table(_snake_case ) lowerCAmelCase : Union[str, Any] = '''''' # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(_snake_case , 2 ): lowerCAmelCase, lowerCAmelCase : List[str] = divmod(table.index(_snake_case ) , 5 ) lowerCAmelCase, lowerCAmelCase : Union[str, Any] = divmod(table.index(_snake_case ) , 5 ) if rowa == rowa: plaintext += table[rowa * 5 + (cola - 1) % 5] plaintext += table[rowa * 5 + (cola - 1) % 5] elif cola == cola: plaintext += table[((rowa - 1) % 5) * 5 + cola] plaintext += table[((rowa - 1) % 5) * 5 + cola] else: # rectangle plaintext += table[rowa * 5 + cola] plaintext += table[rowa * 5 + cola] return plaintext
60
import random def A ( a_ ,a_ ,a_ = False ) -> dict: __UpperCamelCase : dict ={i: [] for i in range(a_ )} # if probability is greater or equal than 1, then generate a complete graph if probability >= 1: return complete_graph(a_ ) # if probability is lower or equal than 0, then return a graph without edges if probability <= 0: return graph # for each couple of nodes, add an edge from u to v # if the number randomly generated is greater than probability probability for i in range(a_ ): for j in range(i + 1 ,a_ ): if random.random() < probability: graph[i].append(a_ ) if not directed: # if the graph is undirected, add an edge in from j to i, either graph[j].append(a_ ) return graph def A ( a_ ) -> dict: return { i: [j for j in range(a_ ) if i != j] for i in range(a_ ) } if __name__ == "__main__": import doctest doctest.testmod()
71
0
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_fnet import FNetTokenizer else: _a = None _a = logging.get_logger(__name__) _a = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} _a = { 'vocab_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/spiece.model', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/spiece.model', }, 'tokenizer_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json', }, } _a = { 'google/fnet-base': 512, 'google/fnet-large': 512, } _a = '▁' class A_ (lowercase__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[str] = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE__ : Tuple = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE__ : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE__ : Union[str, Any] = ["""input_ids""", """token_type_ids"""] SCREAMING_SNAKE_CASE__ : Tuple = FNetTokenizer def __init__( self , lowercase_=None , lowercase_=None , lowercase_=False , lowercase_=True , lowercase_=True , lowercase_="<unk>" , lowercase_="[SEP]" , lowercase_="<pad>" , lowercase_="[CLS]" , lowercase_="[MASK]" , **lowercase_ , ): """simple docstring""" # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. UpperCAmelCase_ : int = ( AddedToken(lowercase_ , lstrip=lowercase_ , rstrip=lowercase_ , normalized=lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else mask_token ) super().__init__( lowercase_ , tokenizer_file=lowercase_ , do_lower_case=lowercase_ , remove_space=lowercase_ , keep_accents=lowercase_ , unk_token=lowercase_ , sep_token=lowercase_ , pad_token=lowercase_ , cls_token=lowercase_ , mask_token=lowercase_ , **lowercase_ , ) UpperCAmelCase_ : Any = do_lower_case UpperCAmelCase_ : Tuple = remove_space UpperCAmelCase_ : str = keep_accents UpperCAmelCase_ : Any = vocab_file UpperCAmelCase_ : List[Any] = False if not self.vocab_file else True def UpperCamelCase__ ( self , lowercase_ , lowercase_ = None ): """simple docstring""" UpperCAmelCase_ : Tuple = [self.sep_token_id] UpperCAmelCase_ : Any = [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 , lowercase_ , lowercase_ = None ): """simple docstring""" UpperCAmelCase_ : Any = [self.sep_token_id] UpperCAmelCase_ : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCamelCase__ ( self , lowercase_ , lowercase_ = None ): """simple docstring""" if not os.path.isdir(lowercase_ ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return UpperCAmelCase_ : List[str] = os.path.join( lowercase_ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowercase_ ): copyfile(self.vocab_file , lowercase_ ) return (out_vocab_file,)
61
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 __A ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: __UpperCamelCase : List[str] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModel.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[Any] =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"]: __UpperCamelCase : Any =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForPreTraining.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =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]: __UpperCamelCase : Tuple =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModelForCausalLM.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Tuple =TFAutoModelForCausalLM.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[int] =AutoModelForCausalLM.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Union[str, Any] =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]: __UpperCamelCase : int =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Dict =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[Any] =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]: __UpperCamelCase : List[Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Tuple =TFAutoModelForMaskedLM.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =AutoModelForMaskedLM.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Optional[Any] =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]: __UpperCamelCase : Any =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[Any] =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : List[str] =TFAutoModelForSeqaSeqLM.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Dict =AutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : List[Any] =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"]: __UpperCamelCase : str =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =TFAutoModelForSequenceClassification.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =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"]: __UpperCamelCase : List[Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Tuple =TFAutoModelForQuestionAnswering.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =AutoModelForQuestionAnswering.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : int =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) __UpperCamelCase : str =AutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Union[str, Any] =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) __UpperCamelCase : int =AutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 )
71
0
import math import unittest from transformers import BioGptConfig, 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 ( BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptTokenizer, ) from transformers.models.biogpt.modeling_biogpt import BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST class UpperCAmelCase__ : """simple docstring""" def __init__( self , A_ , A_=13 , A_=7 , A_=True , A_=True , A_=False , A_=True , A_=99 , A_=32 , A_=5 , A_=4 , A_=37 , A_="gelu" , A_=0.1 , A_=0.1 , A_=512 , A_=16 , A_=2 , A_=0.02 , A_=3 , A_=4 , A_=None , ) -> Optional[int]: __UpperCamelCase =parent __UpperCamelCase =batch_size __UpperCamelCase =seq_length __UpperCamelCase =is_training __UpperCamelCase =use_input_mask __UpperCamelCase =use_token_type_ids __UpperCamelCase =use_labels __UpperCamelCase =vocab_size __UpperCamelCase =hidden_size __UpperCamelCase =num_hidden_layers __UpperCamelCase =num_attention_heads __UpperCamelCase =intermediate_size __UpperCamelCase =hidden_act __UpperCamelCase =hidden_dropout_prob __UpperCamelCase =attention_probs_dropout_prob __UpperCamelCase =max_position_embeddings __UpperCamelCase =type_vocab_size __UpperCamelCase =type_sequence_label_size __UpperCamelCase =initializer_range __UpperCamelCase =num_labels __UpperCamelCase =num_choices __UpperCamelCase =scope def _a ( self ) -> Dict: __UpperCamelCase =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __UpperCamelCase =None if self.use_input_mask: __UpperCamelCase =random_attention_mask([self.batch_size, self.seq_length] ) __UpperCamelCase =None if self.use_token_type_ids: __UpperCamelCase =ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __UpperCamelCase =None __UpperCamelCase =None __UpperCamelCase =None if self.use_labels: __UpperCamelCase =ids_tensor([self.batch_size] , self.type_sequence_label_size ) __UpperCamelCase =ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __UpperCamelCase =ids_tensor([self.batch_size] , self.num_choices ) __UpperCamelCase =self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _a ( self ) -> Optional[Any]: return BioGptConfig( 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=A_ , initializer_range=self.initializer_range , ) def _a ( self , A_ , A_ , A_ , A_ , A_ , A_ , A_ ) -> List[str]: __UpperCamelCase =BioGptModel(config=A_ ) model.to(A_ ) model.eval() __UpperCamelCase =model(A_ , attention_mask=A_ ) __UpperCamelCase =model(A_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _a ( self , A_ , A_ , A_ , A_ , A_ , A_ , A_ , A_ , A_ , ) -> Union[str, Any]: __UpperCamelCase =BioGptForCausalLM(config=A_ ) model.to(A_ ) model.eval() __UpperCamelCase =model(A_ , attention_mask=A_ , token_type_ids=A_ , labels=A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _a ( self , A_ , A_ , A_ , A_ , A_ , *A_ ) -> str: __UpperCamelCase =BioGptModel(config=A_ ) model.to(A_ ) model.eval() # create attention mask __UpperCamelCase =torch.ones(input_ids.shape , dtype=torch.long , device=A_ ) __UpperCamelCase =self.seq_length // 2 __UpperCamelCase =0 # first forward pass __UpperCamelCase , __UpperCamelCase =model(A_ , attention_mask=A_ ).to_tuple() # create hypothetical next token and extent to next_input_ids __UpperCamelCase =ids_tensor((self.batch_size, 1) , config.vocab_size ) # change a random masked slice from input_ids __UpperCamelCase =ids_tensor((1,) , A_ ).item() + 1 __UpperCamelCase =ids_tensor((self.batch_size, 1) , config.vocab_size ).squeeze(-1 ) __UpperCamelCase =random_other_next_tokens # append to next input_ids and attn_mask __UpperCamelCase =torch.cat([input_ids, next_tokens] , dim=-1 ) __UpperCamelCase =torch.cat( [attn_mask, torch.ones((attn_mask.shape[0], 1) , dtype=torch.long , device=A_ )] , dim=1 , ) # get two different outputs __UpperCamelCase =model(A_ , attention_mask=A_ )['last_hidden_state'] __UpperCamelCase =model(A_ , past_key_values=A_ , attention_mask=A_ )['last_hidden_state'] # select random slice __UpperCamelCase =ids_tensor((1,) , output_from_past.shape[-1] ).item() __UpperCamelCase =output_from_no_past[:, -1, random_slice_idx].detach() __UpperCamelCase =output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(A_ , A_ , atol=1E-3 ) ) def _a ( self , A_ , A_ , A_ , A_ , A_ , *A_ ) -> Tuple: __UpperCamelCase =BioGptModel(config=A_ ).to(A_ ).eval() __UpperCamelCase =torch.ones(input_ids.shape , dtype=torch.long , device=A_ ) # first forward pass __UpperCamelCase =model(A_ , attention_mask=A_ , use_cache=A_ ) __UpperCamelCase , __UpperCamelCase =outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids __UpperCamelCase =ids_tensor((self.batch_size, 3) , config.vocab_size ) __UpperCamelCase =ids_tensor((self.batch_size, 3) , 2 ) # append to next input_ids and __UpperCamelCase =torch.cat([input_ids, next_tokens] , dim=-1 ) __UpperCamelCase =torch.cat([attention_mask, next_attn_mask] , dim=-1 ) __UpperCamelCase =model(A_ , attention_mask=A_ )['last_hidden_state'] __UpperCamelCase =model(A_ , attention_mask=A_ , past_key_values=A_ )[ 'last_hidden_state' ] # select random slice __UpperCamelCase =ids_tensor((1,) , output_from_past.shape[-1] ).item() __UpperCamelCase =output_from_no_past[:, -3:, random_slice_idx].detach() __UpperCamelCase =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(A_ , A_ , atol=1E-3 ) ) def _a ( self , A_ , A_ , A_ , A_ , A_ , *A_ , A_=False ) -> List[str]: __UpperCamelCase =BioGptForCausalLM(A_ ) model.to(A_ ) if gradient_checkpointing: model.gradient_checkpointing_enable() __UpperCamelCase =model(A_ , labels=A_ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) result.loss.backward() def _a ( self , A_ , *A_ ) -> Tuple: __UpperCamelCase =BioGptModel(A_ ) __UpperCamelCase =model.config.initializer_range / math.sqrt(2 * model.config.num_hidden_layers ) for key in model.state_dict().keys(): if "c_proj" in key and "weight" in key: self.parent.assertLessEqual(abs(torch.std(model.state_dict()[key] ) - model_std ) , 0.001 ) self.parent.assertLessEqual(abs(torch.mean(model.state_dict()[key] ) - 0.0 ) , 0.01 ) def _a ( self , A_ , A_ , A_ , A_ , A_ , *A_ ) -> Tuple: __UpperCamelCase =self.num_labels __UpperCamelCase =BioGptForTokenClassification(A_ ) model.to(A_ ) model.eval() __UpperCamelCase =model(A_ , attention_mask=A_ , token_type_ids=A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _a ( self ) -> int: __UpperCamelCase =self.prepare_config_and_inputs() ( ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ) =config_and_inputs __UpperCamelCase ={'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class UpperCAmelCase__ ( A_ , A_ , A_ , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : Optional[Any] = ( (BioGptModel, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification) if is_torch_available() else () ) UpperCAmelCase__ : int = (BioGptForCausalLM,) if is_torch_available() else () UpperCAmelCase__ : Any = ( { "feature-extraction": BioGptModel, "text-classification": BioGptForSequenceClassification, "text-generation": BioGptForCausalLM, "token-classification": BioGptForTokenClassification, "zero-shot": BioGptForSequenceClassification, } if is_torch_available() else {} ) UpperCAmelCase__ : Tuple = False def _a ( self ) -> Dict: __UpperCamelCase =BioGptModelTester(self ) __UpperCamelCase =ConfigTester(self , config_class=A_ , hidden_size=37 ) def _a ( self ) -> Optional[int]: self.config_tester.run_common_tests() def _a ( self ) -> str: __UpperCamelCase =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A_ ) def _a ( self ) -> Optional[Any]: __UpperCamelCase =self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: __UpperCamelCase =type self.model_tester.create_and_check_model(*A_ ) def _a ( self ) -> int: __UpperCamelCase =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_model_attention_mask_past(*A_ ) def _a ( self ) -> Optional[int]: __UpperCamelCase =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_forward_and_backwards(*A_ , gradient_checkpointing=A_ ) def _a ( self ) -> Any: __UpperCamelCase =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_model_past_large_inputs(*A_ ) def _a ( self ) -> List[str]: __UpperCamelCase =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_weight_initialization(*A_ ) def _a ( self ) -> Any: __UpperCamelCase =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_for_token_classification(*A_ ) @slow def _a ( self ) -> Union[str, Any]: __UpperCamelCase =BioGptForCausalLM.from_pretrained('microsoft/biogpt' ) model.to(A_ ) __UpperCamelCase =BioGptTokenizer.from_pretrained('microsoft/biogpt' ) __UpperCamelCase ='left' # Define PAD Token = EOS Token = 50256 __UpperCamelCase =tokenizer.eos_token __UpperCamelCase =model.config.eos_token_id # use different length sentences to test batching __UpperCamelCase =[ 'Hello, my dog is a little', 'Today, I', ] __UpperCamelCase =tokenizer(A_ , return_tensors='pt' , padding=A_ ) __UpperCamelCase =inputs['input_ids'].to(A_ ) __UpperCamelCase =model.generate( input_ids=A_ , attention_mask=inputs['attention_mask'].to(A_ ) , ) __UpperCamelCase =tokenizer(sentences[0] , return_tensors='pt' ).input_ids.to(A_ ) __UpperCamelCase =model.generate(input_ids=A_ ) __UpperCamelCase =inputs_non_padded.shape[-1] - inputs['attention_mask'][-1].long().sum().cpu().item() __UpperCamelCase =tokenizer(sentences[1] , return_tensors='pt' ).input_ids.to(A_ ) __UpperCamelCase =model.generate(input_ids=A_ , max_length=model.config.max_length - num_paddings ) __UpperCamelCase =tokenizer.batch_decode(A_ , skip_special_tokens=A_ ) __UpperCamelCase =tokenizer.decode(output_non_padded[0] , skip_special_tokens=A_ ) __UpperCamelCase =tokenizer.decode(output_padded[0] , skip_special_tokens=A_ ) __UpperCamelCase =[ 'Hello, my dog is a little bit bigger than a little bit.', 'Today, I have a good idea of how to use the information', ] self.assertListEqual(A_ , A_ ) self.assertListEqual(A_ , [non_padded_sentence, padded_sentence] ) @slow def _a ( self ) -> List[Any]: for model_name in BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __UpperCamelCase =BioGptModel.from_pretrained(A_ ) self.assertIsNotNone(A_ ) def _a ( self ) -> Union[str, Any]: __UpperCamelCase , __UpperCamelCase =self.model_tester.prepare_config_and_inputs_for_common() __UpperCamelCase =3 __UpperCamelCase =input_dict['input_ids'] __UpperCamelCase =input_ids.ne(1 ).to(A_ ) __UpperCamelCase =ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) __UpperCamelCase =BioGptForSequenceClassification(A_ ) model.to(A_ ) model.eval() __UpperCamelCase =model(A_ , attention_mask=A_ , labels=A_ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _a ( self ) -> Any: __UpperCamelCase , __UpperCamelCase =self.model_tester.prepare_config_and_inputs_for_common() __UpperCamelCase =3 __UpperCamelCase ='multi_label_classification' __UpperCamelCase =input_dict['input_ids'] __UpperCamelCase =input_ids.ne(1 ).to(A_ ) __UpperCamelCase =ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) __UpperCamelCase =BioGptForSequenceClassification(A_ ) model.to(A_ ) model.eval() __UpperCamelCase =model(A_ , attention_mask=A_ , labels=A_ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @require_torch class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" @slow def _a ( self ) -> Tuple: __UpperCamelCase =BioGptForCausalLM.from_pretrained('microsoft/biogpt' ) __UpperCamelCase =torch.tensor([[2, 4805, 9, 656, 21]] ) __UpperCamelCase =model(A_ )[0] __UpperCamelCase =42384 __UpperCamelCase =torch.Size((1, 5, vocab_size) ) self.assertEqual(output.shape , A_ ) __UpperCamelCase =torch.tensor( [[[-9.5236, -9.8918, 10.4557], [-11.0469, -9.6423, 8.1022], [-8.8664, -7.8826, 5.5325]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , A_ , atol=1E-4 ) ) @slow def _a ( self ) -> Optional[int]: __UpperCamelCase =BioGptTokenizer.from_pretrained('microsoft/biogpt' ) __UpperCamelCase =BioGptForCausalLM.from_pretrained('microsoft/biogpt' ) model.to(A_ ) torch.manual_seed(0 ) __UpperCamelCase =tokenizer('COVID-19 is' , return_tensors='pt' ).to(A_ ) __UpperCamelCase =model.generate( **A_ , min_length=100 , max_length=1024 , num_beams=5 , early_stopping=A_ , ) __UpperCamelCase =tokenizer.decode(output_ids[0] , skip_special_tokens=A_ ) __UpperCamelCase =( 'COVID-19 is a global pandemic caused by severe acute respiratory syndrome coronavirus 2 (SARS-CoV-2), the' ' causative agent of coronavirus disease 2019 (COVID-19), which has spread to more than 200 countries and' ' territories, including the United States (US), Canada, Australia, New Zealand, the United Kingdom (UK),' ' and the United States of America (USA), as of March 11, 2020, with more than 800,000 confirmed cases and' ' more than 800,000 deaths.' ) self.assertEqual(A_ , A_ )
62
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A_ :Tuple = { '''configuration_x_clip''': [ '''XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XCLIPConfig''', '''XCLIPTextConfig''', '''XCLIPVisionConfig''', ], '''processing_x_clip''': ['''XCLIPProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ :Union[str, Any] = [ '''XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XCLIPModel''', '''XCLIPPreTrainedModel''', '''XCLIPTextModel''', '''XCLIPVisionModel''', ] if TYPE_CHECKING: from .configuration_x_clip import ( XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, XCLIPConfig, XCLIPTextConfig, XCLIPVisionConfig, ) from .processing_x_clip import XCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_x_clip import ( XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, XCLIPModel, XCLIPPreTrainedModel, XCLIPTextModel, XCLIPVisionModel, ) else: import sys A_ :Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
71
0
'''simple docstring''' import math def _lowerCamelCase ( lowercase : int = 100 ) -> int: _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() = }""")
63
from ...configuration_utils import PretrainedConfig from ...utils import logging A_ :Any = logging.get_logger(__name__) A_ :int = { '''sayakpaul/vit-msn-base''': '''https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json''', # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class __A ( a ): """simple docstring""" UpperCamelCase__ : Optional[int] ="""vit_msn""" def __init__( self , lowerCamelCase__=768 , lowerCamelCase__=12 , lowerCamelCase__=12 , lowerCamelCase__=3072 , lowerCamelCase__="gelu" , lowerCamelCase__=0.0 , lowerCamelCase__=0.0 , lowerCamelCase__=0.02 , lowerCamelCase__=1E-06 , lowerCamelCase__=224 , lowerCamelCase__=16 , lowerCamelCase__=3 , lowerCamelCase__=True , **lowerCamelCase__ , ): """simple docstring""" super().__init__(**lowerCamelCase__ ) __UpperCamelCase : int =hidden_size __UpperCamelCase : List[Any] =num_hidden_layers __UpperCamelCase : Union[str, Any] =num_attention_heads __UpperCamelCase : List[str] =intermediate_size __UpperCamelCase : Union[str, Any] =hidden_act __UpperCamelCase : str =hidden_dropout_prob __UpperCamelCase : Union[str, Any] =attention_probs_dropout_prob __UpperCamelCase : Union[str, Any] =initializer_range __UpperCamelCase : Tuple =layer_norm_eps __UpperCamelCase : Optional[Any] =image_size __UpperCamelCase : Optional[int] =patch_size __UpperCamelCase : Any =num_channels __UpperCamelCase : str =qkv_bias
71
0
"""simple docstring""" # Usage: # ./gen-card-allenai-wmt16.py import os from pathlib import Path def UpperCAmelCase__ (snake_case__ : Optional[int] , snake_case__ : str , snake_case__ : Optional[Any] , snake_case__ : str ): """simple docstring""" _snake_case : Tuple = { """en""": """Machine learning is great, isn't it?""", """ru""": """Машинное обучение - это здорово, не так ли?""", """de""": """Maschinelles Lernen ist großartig, nicht wahr?""", } # BLUE scores as follows: # "pair": [fairseq, transformers] _snake_case : int = { """wmt16-en-de-dist-12-1""": [28.3, 27.52], """wmt16-en-de-dist-6-1""": [27.4, 27.11], """wmt16-en-de-12-1""": [26.9, 25.75], } _snake_case : Union[str, Any] = F"{src_lang}-{tgt_lang}" _snake_case : Dict = F"\n---\nlanguage:\n- {src_lang}\n- {tgt_lang}\nthumbnail:\ntags:\n- translation\n- wmt16\n- allenai\nlicense: apache-2.0\ndatasets:\n- wmt16\nmetrics:\n- bleu\n---\n\n# FSMT\n\n## Model description\n\nThis is a ported version of fairseq-based [wmt16 transformer](https://github.com/jungokasai/deep-shallow/) for {src_lang}-{tgt_lang}.\n\nFor more details, please, see [Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation](https://arxiv.org/abs/2006.10369).\n\nAll 3 models are available:\n\n* [wmt16-en-de-dist-12-1](https://huggingface.co/allenai/wmt16-en-de-dist-12-1)\n* [wmt16-en-de-dist-6-1](https://huggingface.co/allenai/wmt16-en-de-dist-6-1)\n* [wmt16-en-de-12-1](https://huggingface.co/allenai/wmt16-en-de-12-1)\n\n\n## Intended uses & limitations\n\n#### How to use\n\n```python\nfrom transformers import FSMTForConditionalGeneration, FSMTTokenizer\nmname = \"allenai/{model_name}\"\ntokenizer = FSMTTokenizer.from_pretrained(mname)\nmodel = FSMTForConditionalGeneration.from_pretrained(mname)\n\ninput = \"{texts[src_lang]}\"\ninput_ids = tokenizer.encode(input, return_tensors=\"pt\")\noutputs = model.generate(input_ids)\ndecoded = tokenizer.decode(outputs[0], skip_special_tokens=True)\nprint(decoded) # {texts[tgt_lang]}\n\n```\n\n#### Limitations and bias\n\n\n## Training data\n\nPretrained weights were left identical to the original model released by allenai. For more details, please, see the [paper](https://arxiv.org/abs/2006.10369).\n\n## Eval results\n\nHere are the BLEU scores:\n\nmodel | fairseq | transformers\n-------|---------|----------\n{model_name} | {scores[model_name][0]} | {scores[model_name][1]}\n\nThe score is slightly below the score reported in the paper, as the researchers don't use `sacrebleu` and measure the score on tokenized outputs. `transformers` score was measured using `sacrebleu` on detokenized outputs.\n\nThe score was calculated using this code:\n\n```bash\ngit clone https://github.com/huggingface/transformers\ncd transformers\nexport PAIR={pair}\nexport DATA_DIR=data/$PAIR\nexport SAVE_DIR=data/$PAIR\nexport BS=8\nexport NUM_BEAMS=5\nmkdir -p $DATA_DIR\nsacrebleu -t wmt16 -l $PAIR --echo src > $DATA_DIR/val.source\nsacrebleu -t wmt16 -l $PAIR --echo ref > $DATA_DIR/val.target\necho $PAIR\nPYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py allenai/{model_name} $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS\n```\n\n## Data Sources\n\n- [training, etc.](http://www.statmt.org/wmt16/)\n- [test set](http://matrix.statmt.org/test_sets/newstest2016.tgz?1504722372)\n\n\n### BibTeX entry and citation info\n\n```\n@misc{{kasai2020deep,\n title={{Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation}},\n author={{Jungo Kasai and Nikolaos Pappas and Hao Peng and James Cross and Noah A. Smith}},\n year={{2020}},\n eprint={{2006.10369}},\n archivePrefix={{arXiv}},\n primaryClass={{cs.CL}}\n}}\n```\n\n" model_card_dir.mkdir(parents=snake_case__ , exist_ok=snake_case__ ) _snake_case : Any = os.path.join(snake_case__ , """README.md""" ) print(F"Generating {path}" ) with open(snake_case__ , """w""" , encoding="""utf-8""" ) as f: f.write(snake_case__ ) # make sure we are under the root of the project A_ = Path(__file__).resolve().parent.parent.parent A_ = repo_dir / '''model_cards''' for model_name in ["wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1"]: A_ = model_cards_dir / '''allenai''' / model_name write_model_card(model_card_dir, src_lang='''en''', tgt_lang='''de''', model_name=model_name)
64
import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __A ( a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : str =DDIMPipeline UpperCamelCase__ : List[Any] =UNCONDITIONAL_IMAGE_GENERATION_PARAMS UpperCamelCase__ : Tuple =PipelineTesterMixin.required_optional_params - { """num_images_per_prompt""", """latents""", """callback""", """callback_steps""", } UpperCamelCase__ : Tuple =UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS UpperCamelCase__ : Any =False def __lowercase ( self ): """simple docstring""" torch.manual_seed(0 ) __UpperCamelCase : Optional[int] =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') , ) __UpperCamelCase : int =DDIMScheduler() __UpperCamelCase : Optional[int] ={'unet': unet, 'scheduler': scheduler} return components def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : str =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : Optional[int] =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Tuple ={ 'batch_size': 1, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Any ='cpu' __UpperCamelCase : Optional[Any] =self.get_dummy_components() __UpperCamelCase : Tuple =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =self.get_dummy_inputs(lowerCamelCase__ ) __UpperCamelCase : int =pipe(**lowerCamelCase__ ).images __UpperCamelCase : Dict =image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) __UpperCamelCase : Tuple =np.array( [1.000E00, 5.717E-01, 4.717E-01, 1.000E00, 0.000E00, 1.000E00, 3.000E-04, 0.000E00, 9.000E-04] ) __UpperCamelCase : Tuple =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) def __lowercase ( self ): """simple docstring""" super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_save_load_local(expected_max_difference=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_save_load_optional_components(expected_max_difference=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __A ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str ='google/ddpm-cifar10-32' __UpperCamelCase : str =UNetaDModel.from_pretrained(lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =DDIMScheduler() __UpperCamelCase : List[Any] =DDIMPipeline(unet=lowerCamelCase__ , scheduler=lowerCamelCase__ ) ddim.to(lowerCamelCase__ ) ddim.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Optional[int] =torch.manual_seed(0 ) __UpperCamelCase : List[str] =ddim(generator=lowerCamelCase__ , eta=0.0 , output_type='numpy' ).images __UpperCamelCase : Union[str, Any] =image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) __UpperCamelCase : str =np.array([0.1_723, 0.1_617, 0.1_600, 0.1_626, 0.1_497, 0.1_513, 0.1_505, 0.1_442, 0.1_453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[Any] ='google/ddpm-ema-bedroom-256' __UpperCamelCase : Any =UNetaDModel.from_pretrained(lowerCamelCase__ ) __UpperCamelCase : int =DDIMScheduler.from_pretrained(lowerCamelCase__ ) __UpperCamelCase : Dict =DDIMPipeline(unet=lowerCamelCase__ , scheduler=lowerCamelCase__ ) ddpm.to(lowerCamelCase__ ) ddpm.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Tuple =torch.manual_seed(0 ) __UpperCamelCase : Union[str, Any] =ddpm(generator=lowerCamelCase__ , output_type='numpy' ).images __UpperCamelCase : Tuple =image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) __UpperCamelCase : Optional[Any] =np.array([0.0_060, 0.0_201, 0.0_344, 0.0_024, 0.0_018, 0.0_002, 0.0_022, 0.0_000, 0.0_069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
71
0
import os import socket from contextlib import contextmanager import torch from ..commands.config.default import write_basic_config # noqa: F401 from ..state import PartialState from .dataclasses import DistributedType from .imports import is_deepspeed_available, is_tpu_available from .transformer_engine import convert_model from .versions import is_torch_version if is_deepspeed_available(): from deepspeed import DeepSpeedEngine if is_tpu_available(check_device=False): import torch_xla.core.xla_model as xm def lowerCAmelCase_ ( __A ) -> Optional[int]: '''simple docstring''' if is_torch_version("<", "2.0.0" ) or not hasattr(__A, "_dynamo" ): return False return isinstance(__A, torch._dynamo.eval_frame.OptimizedModule ) def lowerCAmelCase_ ( __A, __A = True ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase__ = (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel) UpperCAmelCase__ = is_compiled_module(__A ) if is_compiled: UpperCAmelCase__ = model UpperCAmelCase__ = model._orig_mod if is_deepspeed_available(): options += (DeepSpeedEngine,) while isinstance(__A, __A ): UpperCAmelCase__ = model.module if not keep_fpaa_wrapper: UpperCAmelCase__ = getattr(__A, "forward" ) UpperCAmelCase__ = model.__dict__.pop("_original_forward", __A ) if original_forward is not None: while hasattr(__A, "__wrapped__" ): UpperCAmelCase__ = forward.__wrapped__ if forward == original_forward: break UpperCAmelCase__ = forward if getattr(__A, "_converted_to_transformer_engine", __A ): convert_model(__A, to_transformer_engine=__A ) if is_compiled: UpperCAmelCase__ = model UpperCAmelCase__ = compiled_model return model def lowerCAmelCase_ ( ) -> Dict: '''simple docstring''' PartialState().wait_for_everyone() def lowerCAmelCase_ ( __A, __A ) -> int: '''simple docstring''' if PartialState().distributed_type == DistributedType.TPU: xm.save(__A, __A ) elif PartialState().local_process_index == 0: torch.save(__A, __A ) @contextmanager def lowerCAmelCase_ ( **__A ) -> Optional[int]: '''simple docstring''' for key, value in kwargs.items(): UpperCAmelCase__ = str(__A ) yield for key in kwargs: if key.upper() in os.environ: del os.environ[key.upper()] def lowerCAmelCase_ ( __A ) -> Tuple: '''simple docstring''' if not hasattr(__A, "__qualname__" ) and not hasattr(__A, "__name__" ): UpperCAmelCase__ = getattr(__A, "__class__", __A ) if hasattr(__A, "__qualname__" ): return obj.__qualname__ if hasattr(__A, "__name__" ): return obj.__name__ return str(__A ) def lowerCAmelCase_ ( __A, __A ) -> Tuple: '''simple docstring''' for key, value in source.items(): if isinstance(__A, __A ): UpperCAmelCase__ = destination.setdefault(__A, {} ) merge_dicts(__A, __A ) else: UpperCAmelCase__ = value return destination def lowerCAmelCase_ ( __A = None ) -> bool: '''simple docstring''' if port is None: UpperCAmelCase__ = 29_500 with socket.socket(socket.AF_INET, socket.SOCK_STREAM ) as s: return s.connect_ex(("localhost", port) ) == 0
65
from __future__ import annotations import copy import tempfile import unittest from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available from transformers.testing_utils import ( DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tensorflow_probability, require_tf, slow, ) from ..bert.test_modeling_bert import BertModelTester if is_tf_available(): from transformers import ( TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelForTableQuestionAnswering, TFAutoModelForTokenClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFFunnelBaseModel, TFFunnelModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, TFTapasForQuestionAnswering, ) from transformers.models.auto.modeling_tf_auto import ( TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_MAPPING, ) 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 from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST class __A ( a ): """simple docstring""" UpperCamelCase__ : Optional[int] ="""new-model""" if is_tf_available(): class __A ( a ): """simple docstring""" UpperCamelCase__ : List[str] =NewModelConfig @require_tf class __A ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] ='bert-base-cased' __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Union[str, Any] ='bert-base-cased' __UpperCamelCase : Optional[int] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Tuple =TFAutoModelForPreTraining.from_pretrained(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]: __UpperCamelCase : str =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModelForCausalLM.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : str =TFAutoModelForCausalLM.from_pretrained(lowerCamelCase__ , output_loading_info=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]: __UpperCamelCase : Optional[int] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Any =TFAutoModelWithLMHead.from_pretrained(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]: __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Any =TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Union[str, Any] =TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase__ , output_loading_info=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]: __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : str =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ , output_loading_info=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForSequenceClassification.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: __UpperCamelCase : List[Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[int] =TFAutoModelForQuestionAnswering.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow @require_tensorflow_probability def __lowercase ( self ): """simple docstring""" for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]: __UpperCamelCase : Any =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModelForTableQuestionAnswering.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : List[str] =TFAutoModelForTableQuestionAnswering.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[Any] =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Dict =TFAutoModel.from_pretrained('sgugger/funnel-random-tiny' ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[int] =copy.deepcopy(model.config ) __UpperCamelCase : Optional[Any] =['FunnelBaseModel'] __UpperCamelCase : Tuple =TFAutoModel.from_config(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : List[Any] =TFAutoModel.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" try: AutoConfig.register('new-model' , lowerCamelCase__ ) __UpperCamelCase : int =[ TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSequenceClassification, TFAutoModelForTokenClassification, ] for auto_class in auto_classes: with self.subTest(auto_class.__name__ ): # Wrong config class will raise an error with self.assertRaises(lowerCamelCase__ ): auto_class.register(lowerCamelCase__ , lowerCamelCase__ ) auto_class.register(lowerCamelCase__ , lowerCamelCase__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(lowerCamelCase__ ): auto_class.register(lowerCamelCase__ , lowerCamelCase__ ) # Now that the config is registered, it can be used as any other config with the auto-API __UpperCamelCase : List[str] =BertModelTester(self ).get_config() __UpperCamelCase : Optional[Any] =NewModelConfig(**tiny_config.to_dict() ) __UpperCamelCase : Dict =auto_class.from_config(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =auto_class.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) finally: if "new-model" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["new-model"] for mapping in ( TF_MODEL_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, ): if NewModelConfig in mapping._extra_content: del mapping._extra_content[NewModelConfig] def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCamelCase__ , 'bert-base is not a local folder and is not a valid model identifier' ): __UpperCamelCase : Dict =TFAutoModel.from_pretrained('bert-base' ) def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCamelCase__ , R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)' ): __UpperCamelCase : Union[str, Any] =TFAutoModel.from_pretrained(lowerCamelCase__ , revision='aaaaaa' ) def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCamelCase__ , 'hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin' , ): __UpperCamelCase : List[str] =TFAutoModel.from_pretrained('hf-internal-testing/config-no-model' ) def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex(lowerCamelCase__ , 'Use `from_pt=True` to load this model' ): __UpperCamelCase : List[Any] =TFAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only' ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[int] =TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert' ) with RequestCounter() as counter: __UpperCamelCase : Dict =TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 ) # With a sharded checkpoint __UpperCamelCase : Dict =TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded' ) with RequestCounter() as counter: __UpperCamelCase : Union[str, Any] =TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
71
0
"""simple docstring""" import argparse import glob import logging import os import sys import time from collections import defaultdict from pathlib import Path from typing import Dict, List, Tuple import numpy as np import pytorch_lightning as pl import torch from callbacks import SeqaSeqLoggingCallback, get_checkpoint_callback, get_early_stopping_callback from torch import nn from torch.utils.data import DataLoader from transformers import MBartTokenizer, TaForConditionalGeneration from transformers.models.bart.modeling_bart import shift_tokens_right from utils import ( ROUGE_KEYS, LegacySeqaSeqDataset, SeqaSeqDataset, assert_all_frozen, calculate_bleu, calculate_rouge, check_output_dir, flatten_list, freeze_embeds, freeze_params, get_git_info, label_smoothed_nll_loss, lmap, pickle_save, save_git_info, save_json, use_task_specific_params, ) # need the parent dir module sys.path.insert(2, str(Path(__file__).resolve().parents[1])) from lightning_base import BaseTransformer, add_generic_args, generic_train # noqa __a = logging.getLogger(__name__) class lowerCamelCase ( _lowerCAmelCase ): '''simple docstring''' _A : str = """summarization""" _A : Union[str, Any] = ["""loss"""] _A : Dict = ROUGE_KEYS _A : Tuple = """rouge2""" def __init__( self: List[Any] , snake_case: List[str] , **snake_case: Tuple ) -> str: if hparams.sortish_sampler and hparams.gpus > 1: snake_case_ :List[str] = False elif hparams.max_tokens_per_batch is not None: if hparams.gpus > 1: raise NotImplementedError("""Dynamic Batch size does not work for multi-gpu training""" ) if hparams.sortish_sampler: raise ValueError("""--sortish_sampler and --max_tokens_per_batch may not be used simultaneously""" ) super().__init__(snake_case , num_labels=snake_case , mode=self.mode , **snake_case ) use_task_specific_params(self.model , """summarization""" ) save_git_info(self.hparams.output_dir ) snake_case_ :Dict = Path(self.output_dir ) / """metrics.json""" snake_case_ :Dict = Path(self.output_dir ) / """hparams.pkl""" pickle_save(self.hparams , self.hparams_save_path ) snake_case_ :Dict = 0 snake_case_ :List[Any] = defaultdict(snake_case ) snake_case_ :List[str] = self.config.model_type snake_case_ :Any = self.config.tgt_vocab_size if self.model_type == """fsmt""" else self.config.vocab_size snake_case_ :dict = { "data_dir": self.hparams.data_dir, "max_source_length": self.hparams.max_source_length, "prefix": self.model.config.prefix or "", } snake_case_ :List[Any] = { """train""": self.hparams.n_train, """val""": self.hparams.n_val, """test""": self.hparams.n_test, } snake_case_ :Tuple = {k: v if v >= 0 else None for k, v in n_observations_per_split.items()} snake_case_ :int = { """train""": self.hparams.max_target_length, """val""": self.hparams.val_max_target_length, """test""": self.hparams.test_max_target_length, } assert self.target_lens["train"] <= self.target_lens["val"], f"""target_lens: {self.target_lens}""" assert self.target_lens["train"] <= self.target_lens["test"], f"""target_lens: {self.target_lens}""" if self.hparams.freeze_embeds: freeze_embeds(self.model ) if self.hparams.freeze_encoder: freeze_params(self.model.get_encoder() ) assert_all_frozen(self.model.get_encoder() ) snake_case_ :Dict = get_git_info()["""repo_sha"""] snake_case_ :Optional[int] = hparams.num_workers snake_case_ :Dict = None # default to config if self.model.config.decoder_start_token_id is None and isinstance(self.tokenizer , snake_case ): snake_case_ :Optional[Any] = self.tokenizer.lang_code_to_id[hparams.tgt_lang] snake_case_ :Any = self.decoder_start_token_id snake_case_ :Optional[Any] = ( SeqaSeqDataset if hasattr(self.tokenizer , """prepare_seq2seq_batch""" ) else LegacySeqaSeqDataset ) snake_case_ :Any = False snake_case_ :Any = self.model.config.num_beams if self.hparams.eval_beams is None else self.hparams.eval_beams if self.hparams.eval_max_gen_length is not None: snake_case_ :Union[str, Any] = self.hparams.eval_max_gen_length else: snake_case_ :Optional[Any] = self.model.config.max_length snake_case_ :Tuple = self.default_val_metric if self.hparams.val_metric is None else self.hparams.val_metric def lowerCAmelCase_ ( self: str , snake_case: Dict[str, torch.Tensor] ) -> Dict[str, List[str]]: snake_case_ :List[str] = { k: self.tokenizer.batch_decode(v.tolist() ) if """mask""" not in k else v.shape for k, v in batch.items() } save_json(snake_case , Path(self.output_dir ) / """text_batch.json""" ) save_json({k: v.tolist() for k, v in batch.items()} , Path(self.output_dir ) / """tok_batch.json""" ) snake_case_ :int = True return readable_batch def lowerCAmelCase_ ( self: str , snake_case: Dict , **snake_case: List[Any] ) -> List[Any]: return self.model(snake_case , **snake_case ) def lowerCAmelCase_ ( self: List[str] , snake_case: List[int] ) -> Dict: snake_case_ :Tuple = self.tokenizer.batch_decode( snake_case , skip_special_tokens=snake_case , clean_up_tokenization_spaces=snake_case ) return lmap(str.strip , snake_case ) def lowerCAmelCase_ ( self: str , snake_case: dict ) -> Tuple: snake_case_ :Tuple = self.tokenizer.pad_token_id snake_case_, snake_case_ :List[str] = batch["""input_ids"""], batch["""attention_mask"""] snake_case_ :List[Any] = batch["""labels"""] if isinstance(self.model , snake_case ): snake_case_ :Dict = self.model._shift_right(snake_case ) else: snake_case_ :List[Any] = shift_tokens_right(snake_case , snake_case ) if not self.already_saved_batch: # This would be slightly better if it only happened on rank zero snake_case_ :Optional[Any] = decoder_input_ids self.save_readable_batch(snake_case ) snake_case_ :List[Any] = self(snake_case , attention_mask=snake_case , decoder_input_ids=snake_case , use_cache=snake_case ) snake_case_ :Dict = outputs["""logits"""] if self.hparams.label_smoothing == 0: # Same behavior as modeling_bart.py, besides ignoring pad_token_id snake_case_ :Optional[int] = nn.CrossEntropyLoss(ignore_index=snake_case ) assert lm_logits.shape[-1] == self.vocab_size snake_case_ :Optional[Any] = ce_loss_fct(lm_logits.view(-1 , lm_logits.shape[-1] ) , tgt_ids.view(-1 ) ) else: snake_case_ :Dict = nn.functional.log_softmax(snake_case , dim=-1 ) snake_case_, snake_case_ :Dict = label_smoothed_nll_loss( snake_case , snake_case , self.hparams.label_smoothing , ignore_index=snake_case ) return (loss,) @property def lowerCAmelCase_ ( self: List[Any] ) -> int: return self.tokenizer.pad_token_id def lowerCAmelCase_ ( self: Any , snake_case: Union[str, Any] , snake_case: int ) -> Dict: snake_case_ :Optional[Any] = self._step(snake_case ) snake_case_ :Optional[Any] = dict(zip(self.loss_names , snake_case ) ) # tokens per batch snake_case_ :List[Any] = batch["""input_ids"""].ne(self.pad ).sum() + batch["""labels"""].ne(self.pad ).sum() snake_case_ :List[Any] = batch["""input_ids"""].shape[0] snake_case_ :str = batch["""input_ids"""].eq(self.pad ).sum() snake_case_ :Optional[Any] = batch["""input_ids"""].eq(self.pad ).float().mean() # TODO(SS): make a wandb summary metric for this return {"loss": loss_tensors[0], "log": logs} def lowerCAmelCase_ ( self: Union[str, Any] , snake_case: str , snake_case: int ) -> Dict: return self._generative_step(snake_case ) def lowerCAmelCase_ ( self: Tuple , snake_case: str , snake_case: List[Any]="val" ) -> Dict: self.step_count += 1 snake_case_ :int = {k: torch.stack([x[k] for x in outputs] ).mean() for k in self.loss_names} snake_case_ :List[Any] = losses["""loss"""] snake_case_ :Tuple = { k: np.array([x[k] for x in outputs] ).mean() for k in self.metric_names + ["""gen_time""", """gen_len"""] } snake_case_ :Optional[Any] = ( generative_metrics[self.val_metric] if self.val_metric in generative_metrics else losses[self.val_metric] ) snake_case_ :torch.FloatTensor = torch.tensor(snake_case ).type_as(snake_case ) generative_metrics.update({k: v.item() for k, v in losses.items()} ) losses.update(snake_case ) snake_case_ :List[str] = {f"""{prefix}_avg_{k}""": x for k, x in losses.items()} snake_case_ :Optional[int] = self.step_count self.metrics[prefix].append(snake_case ) # callback writes this to self.metrics_save_path snake_case_ :Any = flatten_list([x["""preds"""] for x in outputs] ) return { "log": all_metrics, "preds": preds, f"""{prefix}_loss""": loss, f"""{prefix}_{self.val_metric}""": metric_tensor, } def lowerCAmelCase_ ( self: int , snake_case: str , snake_case: Optional[int] ) -> Dict: return calculate_rouge(snake_case , snake_case ) def lowerCAmelCase_ ( self: Optional[int] , snake_case: dict ) -> dict: snake_case_ :str = time.time() # parser.add_argument('--eval_max_gen_length', type=int, default=None, help='never generate more than n tokens') snake_case_ :Tuple = self.model.generate( batch["""input_ids"""] , attention_mask=batch["""attention_mask"""] , use_cache=snake_case , decoder_start_token_id=self.decoder_start_token_id , num_beams=self.eval_beams , max_length=self.eval_max_length , ) snake_case_ :List[str] = (time.time() - ta) / batch["""input_ids"""].shape[0] snake_case_ :List[str] = self.ids_to_clean_text(snake_case ) snake_case_ :List[str] = self.ids_to_clean_text(batch["""labels"""] ) snake_case_ :Dict = self._step(snake_case ) snake_case_ :Optional[int] = dict(zip(self.loss_names , snake_case ) ) snake_case_ :Dict = self.calc_generative_metrics(snake_case , snake_case ) snake_case_ :Any = np.mean(lmap(snake_case , snake_case ) ) base_metrics.update(gen_time=snake_case , gen_len=snake_case , preds=snake_case , target=snake_case , **snake_case ) return base_metrics def lowerCAmelCase_ ( self: Optional[Any] , snake_case: str , snake_case: List[Any] ) -> Optional[Any]: return self._generative_step(snake_case ) def lowerCAmelCase_ ( self: str , snake_case: Optional[int] ) -> List[Any]: return self.validation_epoch_end(snake_case , prefix="""test""" ) def lowerCAmelCase_ ( self: Any , snake_case: Optional[int] ) -> SeqaSeqDataset: snake_case_ :Optional[Any] = self.n_obs[type_path] snake_case_ :Optional[int] = self.target_lens[type_path] snake_case_ :List[str] = self.dataset_class( self.tokenizer , type_path=snake_case , n_obs=snake_case , max_target_length=snake_case , **self.dataset_kwargs , ) return dataset def lowerCAmelCase_ ( self: List[Any] , snake_case: str , snake_case: int , snake_case: bool = False ) -> DataLoader: snake_case_ :Any = self.get_dataset(snake_case ) if self.hparams.sortish_sampler and type_path != "test" and type_path != "val": snake_case_ :Optional[int] = dataset.make_sortish_sampler(snake_case , distributed=self.hparams.gpus > 1 ) return DataLoader( snake_case , batch_size=snake_case , collate_fn=dataset.collate_fn , shuffle=snake_case , num_workers=self.num_workers , sampler=snake_case , ) elif self.hparams.max_tokens_per_batch is not None and type_path != "test" and type_path != "val": snake_case_ :Union[str, Any] = dataset.make_dynamic_sampler( self.hparams.max_tokens_per_batch , distributed=self.hparams.gpus > 1 ) return DataLoader( snake_case , batch_sampler=snake_case , collate_fn=dataset.collate_fn , num_workers=self.num_workers , ) else: return DataLoader( snake_case , batch_size=snake_case , collate_fn=dataset.collate_fn , shuffle=snake_case , num_workers=self.num_workers , sampler=snake_case , ) def lowerCAmelCase_ ( self: List[Any] ) -> DataLoader: snake_case_ :Any = self.get_dataloader("""train""" , batch_size=self.hparams.train_batch_size , shuffle=snake_case ) return dataloader def lowerCAmelCase_ ( self: Union[str, Any] ) -> DataLoader: return self.get_dataloader("""val""" , batch_size=self.hparams.eval_batch_size ) def lowerCAmelCase_ ( self: Dict ) -> DataLoader: return self.get_dataloader("""test""" , batch_size=self.hparams.eval_batch_size ) @staticmethod def lowerCAmelCase_ ( snake_case: List[Any] , snake_case: List[str] ) -> List[Any]: BaseTransformer.add_model_specific_args(snake_case , snake_case ) add_generic_args(snake_case , snake_case ) parser.add_argument( """--max_source_length""" , default=1_024 , type=snake_case , help=( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) , ) parser.add_argument( """--max_target_length""" , default=56 , type=snake_case , help=( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) , ) parser.add_argument( """--val_max_target_length""" , default=142 , type=snake_case , help=( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) , ) parser.add_argument( """--test_max_target_length""" , default=142 , type=snake_case , help=( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) , ) parser.add_argument("""--freeze_encoder""" , action="""store_true""" ) parser.add_argument("""--freeze_embeds""" , action="""store_true""" ) parser.add_argument("""--sortish_sampler""" , action="""store_true""" , default=snake_case ) parser.add_argument("""--overwrite_output_dir""" , action="""store_true""" , default=snake_case ) parser.add_argument("""--max_tokens_per_batch""" , type=snake_case , default=snake_case ) parser.add_argument("""--logger_name""" , type=snake_case , choices=["""default""", """wandb""", """wandb_shared"""] , default="""default""" ) parser.add_argument("""--n_train""" , type=snake_case , default=-1 , required=snake_case , help="""# examples. -1 means use all.""" ) parser.add_argument("""--n_val""" , type=snake_case , default=500 , required=snake_case , help="""# examples. -1 means use all.""" ) parser.add_argument("""--n_test""" , type=snake_case , default=-1 , required=snake_case , help="""# examples. -1 means use all.""" ) parser.add_argument( """--task""" , type=snake_case , default="""summarization""" , required=snake_case , help="""# examples. -1 means use all.""" ) parser.add_argument("""--label_smoothing""" , type=snake_case , default=0.0 , required=snake_case ) parser.add_argument("""--src_lang""" , type=snake_case , default="""""" , required=snake_case ) parser.add_argument("""--tgt_lang""" , type=snake_case , default="""""" , required=snake_case ) parser.add_argument("""--eval_beams""" , type=snake_case , default=snake_case , required=snake_case ) parser.add_argument( """--val_metric""" , type=snake_case , default=snake_case , required=snake_case , choices=["""bleu""", """rouge2""", """loss""", None] ) parser.add_argument("""--eval_max_gen_length""" , type=snake_case , default=snake_case , help="""never generate more than n tokens""" ) parser.add_argument("""--save_top_k""" , type=snake_case , default=1 , required=snake_case , help="""How many checkpoints to save""" ) parser.add_argument( """--early_stopping_patience""" , type=snake_case , default=-1 , required=snake_case , help=( """-1 means never early stop. early_stopping_patience is measured in validation checks, not epochs. So""" """ val_check_interval will effect it.""" ) , ) return parser class lowerCamelCase ( _lowerCAmelCase ): '''simple docstring''' _A : Any = """translation""" _A : Tuple = ["""loss"""] _A : List[Any] = ["""bleu"""] _A : Tuple = """bleu""" def __init__( self: str , snake_case: Tuple , **snake_case: int ) -> List[Any]: super().__init__(snake_case , **snake_case ) snake_case_ :Dict = hparams.src_lang snake_case_ :List[Any] = hparams.tgt_lang def lowerCAmelCase_ ( self: Any , snake_case: Dict , snake_case: Optional[int] ) -> dict: return calculate_bleu(snake_case , snake_case ) def A_ ( _lowercase, _lowercase=None ): '''simple docstring''' Path(args.output_dir ).mkdir(exist_ok=_lowercase ) check_output_dir(_lowercase, expected_items=3 ) if model is None: if "summarization" in args.task: snake_case_ :SummarizationModule = SummarizationModule(_lowercase ) else: snake_case_ :SummarizationModule = TranslationModule(_lowercase ) snake_case_ :Tuple = Path(args.data_dir ).name if ( args.logger_name == "default" or args.fast_dev_run or str(args.output_dir ).startswith("""/tmp""" ) or str(args.output_dir ).startswith("""/var""" ) ): snake_case_ :List[str] = True # don't pollute wandb logs unnecessarily elif args.logger_name == "wandb": from pytorch_lightning.loggers import WandbLogger snake_case_ :List[Any] = os.environ.get("""WANDB_PROJECT""", _lowercase ) snake_case_ :Optional[int] = WandbLogger(name=model.output_dir.name, project=_lowercase ) elif args.logger_name == "wandb_shared": from pytorch_lightning.loggers import WandbLogger snake_case_ :Union[str, Any] = WandbLogger(name=model.output_dir.name, project=f"""hf_{dataset}""" ) if args.early_stopping_patience >= 0: snake_case_ :Any = get_early_stopping_callback(model.val_metric, args.early_stopping_patience ) else: snake_case_ :List[Any] = False snake_case_ :int = args.val_metric == """loss""" snake_case_ :pl.Trainer = generic_train( _lowercase, _lowercase, logging_callback=SeqaSeqLoggingCallback(), checkpoint_callback=get_checkpoint_callback( args.output_dir, model.val_metric, args.save_top_k, _lowercase ), early_stopping_callback=_lowercase, logger=_lowercase, ) pickle_save(model.hparams, model.output_dir / """hparams.pkl""" ) if not args.do_predict: return model snake_case_ :Tuple = """""" snake_case_ :Union[str, Any] = sorted(glob.glob(os.path.join(args.output_dir, """*.ckpt""" ), recursive=_lowercase ) ) if checkpoints: snake_case_ :Any = checkpoints[-1] snake_case_ :int = checkpoints[-1] trainer.logger.log_hyperparams(model.hparams ) # test() without a model tests using the best checkpoint automatically trainer.test() return model if __name__ == "__main__": __a = argparse.ArgumentParser() __a = pl.Trainer.add_argparse_args(parser) __a = SummarizationModule.add_model_specific_args(parser, os.getcwd()) __a = parser.parse_args() main(args)
66
import argparse import json import os import re import torch from transformers import BloomConfig, BloomModel from transformers.file_utils import CONFIG_NAME, WEIGHTS_NAME from transformers.utils import logging logging.set_verbosity_info() A_ :List[str] = [ '''word_embeddings_layernorm.weight''', '''word_embeddings_layernorm.bias''', '''input_layernorm.weight''', '''input_layernorm.bias''', '''post_attention_layernorm.weight''', '''post_attention_layernorm.bias''', '''self_attention.dense.bias''', '''mlp.dense_4h_to_h.bias''', '''ln_f.weight''', '''ln_f.bias''', ] A_ :Optional[Any] = [ '''mlp.dense_4h_to_h.weight''', '''self_attention.dense.weight''', ] def A ( a_ ,a_ ) -> str: __UpperCamelCase : Any ={ 'word_embeddings.weight': 'word_embeddings.weight', 'word_embeddings.norm.weight': 'word_embeddings_layernorm.weight', 'word_embeddings.norm.bias': 'word_embeddings_layernorm.bias', 'weight': 'ln_f.weight', 'bias': 'ln_f.bias', } if key in layer_rename_map: return layer_rename_map[key] # Handle transformer blocks __UpperCamelCase : Tuple =int(re.match(r'.*layer_(\d*).*' ,a_ )[1] ) layer_number -= 3 return F'h.{layer_number}.' + key def A ( a_ ) -> Any: if dtype == torch.bool: return 1 / 8 __UpperCamelCase : Dict =re.search(r'[^\d](\d+)$' ,str(a_ ) ) if bit_search is None: raise ValueError(F'`dtype` is not a valid dtype: {dtype}.' ) __UpperCamelCase : Tuple =int(bit_search.groups()[0] ) return bit_size // 8 def A ( a_ ,a_ ,a_ ,a_ ,a_ ) -> Dict: # Construct model if bloom_config_file == "": __UpperCamelCase : List[Any] =BloomConfig() else: __UpperCamelCase : List[str] =BloomConfig.from_json_file(a_ ) if shard_model: __UpperCamelCase : int =os.listdir(a_ ) __UpperCamelCase : Union[str, Any] =sorted(filter(lambda a_ : s.startswith('layer' ) and "model_00" in s ,a_ ) ) __UpperCamelCase : Optional[Any] ={'weight_map': {}, 'metadata': {}} __UpperCamelCase : Dict =0 __UpperCamelCase : int =None __UpperCamelCase : Any =BloomConfig() for j, file in enumerate(a_ ): print('Processing file: {}'.format(a_ ) ) __UpperCamelCase : Optional[int] =None for i in range(a_ ): # load all TP files __UpperCamelCase : Dict =file.replace('model_00' ,F'model_0{i}' ) __UpperCamelCase : Optional[Any] =torch.load(os.path.join(a_ ,a_ ) ,map_location='cpu' ) # Rename keys in the transformers names __UpperCamelCase : int =list(temp.keys() ) for key in keys: __UpperCamelCase : Dict =temp.pop(a_ ) if tensors is None: __UpperCamelCase : Any =temp else: for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel __UpperCamelCase : List[Any] =1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks __UpperCamelCase : Any =torch.cat([tensors[key], temp[key]] ,dim=a_ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): __UpperCamelCase : Optional[Any] =tensors[key] / pretraining_tp torch.save( a_ ,os.path.join( a_ ,'pytorch_model_{}-of-{}.bin'.format(str(j + 1 ).zfill(5 ) ,str(len(a_ ) ).zfill(5 ) ) ,) ,) for key in tensors.keys(): __UpperCamelCase : Union[str, Any] =tensors[key] total_size += value.numel() * get_dtype_size(value.dtype ) if key not in index_dict["weight_map"]: __UpperCamelCase : int ='pytorch_model_{}-of-{}.bin'.format( str(j + 1 ).zfill(5 ) ,str(len(a_ ) ).zfill(5 ) ) __UpperCamelCase : Union[str, Any] =BloomConfig() __UpperCamelCase : Tuple =pytorch_dump_folder_path + '/' + CONFIG_NAME __UpperCamelCase : Optional[int] =total_size with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(config.to_json_string() ) with open(os.path.join(a_ ,WEIGHTS_NAME + '.index.json' ) ,'w' ,encoding='utf-8' ) as f: __UpperCamelCase : List[Any] =json.dumps(a_ ,indent=2 ,sort_keys=a_ ) + '\n' f.write(a_ ) else: __UpperCamelCase : List[Any] =BloomModel(a_ ) __UpperCamelCase : Optional[Any] =os.listdir(a_ ) __UpperCamelCase : Dict =sorted(filter(lambda a_ : s.startswith('layer' ) and "model_00" in s ,a_ ) ) __UpperCamelCase : Any =None for i, file in enumerate(a_ ): __UpperCamelCase : Union[str, Any] =None for i in range(a_ ): # load all TP files __UpperCamelCase : Optional[Any] =file.replace('model_00' ,F'model_0{i}' ) __UpperCamelCase : str =torch.load(os.path.join(a_ ,a_ ) ,map_location='cpu' ) # Rename keys in the transformers names __UpperCamelCase : List[str] =list(temp.keys() ) for key in keys: __UpperCamelCase : Union[str, Any] =temp.pop(a_ ) if tensors is None: __UpperCamelCase : Optional[Any] =temp else: for key in tensors.keys(): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel __UpperCamelCase : Optional[int] =1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks __UpperCamelCase : int =torch.cat([tensors[key], temp[key]] ,dim=a_ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): __UpperCamelCase : Dict =tensors[key] / pretraining_tp __UpperCamelCase : str =model.load_state_dict(a_ ,strict=a_ ) assert not other_keys.unexpected_keys, F'The keys {other_keys.unexpected_keys} are unexpected' if missing_keys is None: __UpperCamelCase : str =set(other_keys.missing_keys ) else: __UpperCamelCase : int =missing_keys.intersection(set(other_keys.missing_keys ) ) assert not missing_keys, F'The keys {missing_keys} are missing' # Save pytorch-model os.makedirs(a_ ,exist_ok=a_ ) __UpperCamelCase : Optional[int] =pytorch_dump_folder_path + '/' + WEIGHTS_NAME __UpperCamelCase : Dict =pytorch_dump_folder_path + '/' + CONFIG_NAME print(F'Save PyTorch model to {pytorch_weights_dump_path} with dtype {config.torch_dtype}' ) if config.torch_dtype is not None: __UpperCamelCase : List[str] =model.to(config.torch_dtype ) torch.save(model.state_dict() ,a_ ) print(F'Save configuration file to {pytorch_config_dump_path}' ) with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": A_ :Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--bloom_checkpoint_path''', default=None, type=str, required=True, help='''Path to the Megatron-LM checkpoint path.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--bloom_config_file''', default='''''', type=str, help=( '''An optional config json file corresponding to the pre-trained model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--shard_model''', action='''store_true''', help='''An optional setting to shard the output model \nThis enables sharding the converted checkpoint''', ) parser.add_argument( '''--pretraining_tp''', default=4, type=int, help='''Pretraining TP rank that has been used when training the model in Megatron-LM \n''', ) A_ :str = parser.parse_args() convert_bloom_checkpoint_to_pytorch( args.bloom_checkpoint_path, args.bloom_config_file, args.pytorch_dump_folder_path, args.shard_model, args.pretraining_tp, )
71
0
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={ "camembert-base": "https://huggingface.co/camembert-base/resolve/main/config.json", "umberto-commoncrawl-cased-v1": ( "https://huggingface.co/Musixmatch/umberto-commoncrawl-cased-v1/resolve/main/config.json" ), "umberto-wikipedia-uncased-v1": ( "https://huggingface.co/Musixmatch/umberto-wikipedia-uncased-v1/resolve/main/config.json" ), } class a__ ( UpperCAmelCase__ ): lowerCamelCase : Union[str, Any] ="camembert" def __init__( self : int , a : Dict=3_05_22 , a : List[Any]=7_68 , a : int=12 , a : Union[str, Any]=12 , a : int=30_72 , a : Any="gelu" , a : Optional[Any]=0.1 , a : Any=0.1 , a : List[Any]=5_12 , a : List[str]=2 , a : Union[str, Any]=0.02 , a : List[Any]=1e-1_2 , a : Any=1 , a : List[str]=0 , a : Optional[int]=2 , a : Union[str, Any]="absolute" , a : Optional[int]=True , a : List[str]=None , **a : Any , ): """simple docstring""" super().__init__(pad_token_id=a , bos_token_id=a , eos_token_id=a , **a ) __lowerCamelCase = vocab_size __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = hidden_act __lowerCamelCase = intermediate_size __lowerCamelCase = hidden_dropout_prob __lowerCamelCase = attention_probs_dropout_prob __lowerCamelCase = max_position_embeddings __lowerCamelCase = type_vocab_size __lowerCamelCase = initializer_range __lowerCamelCase = layer_norm_eps __lowerCamelCase = position_embedding_type __lowerCamelCase = use_cache __lowerCamelCase = classifier_dropout class a__ ( UpperCAmelCase__ ): @property def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" if self.task == "multiple-choice": __lowerCamelCase = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: __lowerCamelCase = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
67
from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class __A : """simple docstring""" UpperCamelCase__ : int =XGLMConfig UpperCamelCase__ : Optional[Any] ={} UpperCamelCase__ : List[str] ="""gelu""" def __init__( self , lowerCamelCase__ , lowerCamelCase__=14 , lowerCamelCase__=7 , 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__=0.02 , ): """simple docstring""" __UpperCamelCase : Tuple =parent __UpperCamelCase : List[str] =batch_size __UpperCamelCase : str =seq_length __UpperCamelCase : Dict =is_training __UpperCamelCase : Tuple =use_input_mask __UpperCamelCase : List[Any] =use_labels __UpperCamelCase : Any =vocab_size __UpperCamelCase : List[Any] =d_model __UpperCamelCase : Optional[int] =num_hidden_layers __UpperCamelCase : List[str] =num_attention_heads __UpperCamelCase : Optional[int] =ffn_dim __UpperCamelCase : str =activation_function __UpperCamelCase : Any =activation_dropout __UpperCamelCase : Optional[int] =attention_dropout __UpperCamelCase : Optional[int] =max_position_embeddings __UpperCamelCase : Any =initializer_range __UpperCamelCase : Dict =None __UpperCamelCase : Optional[int] =0 __UpperCamelCase : Optional[Any] =2 __UpperCamelCase : str =1 def __lowercase ( self ): """simple docstring""" return XGLMConfig.from_pretrained('facebook/xglm-564M' ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[Any] =tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) , clip_value_min=0 , clip_value_max=3 ) __UpperCamelCase : Union[str, Any] =None if self.use_input_mask: __UpperCamelCase : Dict =random_attention_mask([self.batch_size, self.seq_length] ) __UpperCamelCase : Any =self.get_config() __UpperCamelCase : Optional[Any] =floats_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, ) def __lowercase ( self ): """simple docstring""" return XGLMConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , num_layers=self.num_hidden_layers , attention_heads=self.num_attention_heads , ffn_dim=self.ffn_dim , activation_function=self.activation_function , activation_dropout=self.activation_dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , use_cache=lowerCamelCase__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , return_dict=lowerCamelCase__ , ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =self.prepare_config_and_inputs() ( ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ) : int =config_and_inputs __UpperCamelCase : Optional[Any] ={ 'input_ids': input_ids, 'head_mask': head_mask, } return config, inputs_dict @require_tf class __A ( a , a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : Union[str, Any] =(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () UpperCamelCase__ : str =(TFXGLMForCausalLM,) if is_tf_available() else () UpperCamelCase__ : Optional[Any] =( {"""feature-extraction""": TFXGLMModel, """text-generation""": TFXGLMForCausalLM} if is_tf_available() else {} ) UpperCamelCase__ : Tuple =False UpperCamelCase__ : Tuple =False UpperCamelCase__ : Optional[Any] =False def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Tuple =TFXGLMModelTester(self ) __UpperCamelCase : Dict =ConfigTester(self , config_class=lowerCamelCase__ , n_embd=37 ) def __lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() @slow def __lowercase ( self ): """simple docstring""" for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __UpperCamelCase : Optional[Any] =TFXGLMModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @unittest.skip(reason='Currently, model embeddings are going to undergo a major refactor.' ) def __lowercase ( self ): """simple docstring""" super().test_resize_token_embeddings() @require_tf class __A ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self , lowerCamelCase__=True ): """simple docstring""" __UpperCamelCase : int =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : List[str] =tf.convert_to_tensor([[2, 268, 9865]] , dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off __UpperCamelCase : str =[2, 268, 9865, 67, 11, 1988, 57252, 9865, 5, 984, 67, 1988, 213838, 1658, 53, 70446, 33, 6657, 278, 1581] # fmt: on __UpperCamelCase : Optional[Any] =model.generate(lowerCamelCase__ , do_sample=lowerCamelCase__ , num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist() , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : Union[str, Any] =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) tf.random.set_seed(0 ) __UpperCamelCase : str =tokenizer('Today is a nice day and' , return_tensors='tf' ) __UpperCamelCase : Union[str, Any] =tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(':/CPU:0' ): __UpperCamelCase : Any =model.generate(lowerCamelCase__ , do_sample=lowerCamelCase__ , seed=[7, 0] ) __UpperCamelCase : Tuple =tokenizer.decode(output_ids[0] , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : List[Any] =( 'Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due' ) self.assertEqual(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Tuple =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : Optional[Any] =XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : Optional[Any] ='left' # use different length sentences to test batching __UpperCamelCase : Optional[int] =[ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When', 'Hello, my dog is a little', ] __UpperCamelCase : List[Any] =tokenizer(lowerCamelCase__ , return_tensors='tf' , padding=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =inputs['input_ids'] __UpperCamelCase : Dict =model.generate(input_ids=lowerCamelCase__ , attention_mask=inputs['attention_mask'] , max_new_tokens=12 ) __UpperCamelCase : List[Any] =tokenizer(sentences[0] , return_tensors='tf' ).input_ids __UpperCamelCase : Dict =model.generate(input_ids=lowerCamelCase__ , max_new_tokens=12 ) __UpperCamelCase : Any =tokenizer(sentences[1] , return_tensors='tf' ).input_ids __UpperCamelCase : Optional[Any] =model.generate(input_ids=lowerCamelCase__ , max_new_tokens=12 ) __UpperCamelCase : Optional[int] =tokenizer.batch_decode(lowerCamelCase__ , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =tokenizer.decode(output_non_padded[0] , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : int =tokenizer.decode(output_padded[0] , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : Any =[ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When left padding is applied, the sequence will be ' 'a single', 'Hello, my dog is a little bit of a shy one, but he is very friendly', ] self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , [non_padded_sentence, padded_sentence] )
71
0
from typing import Dict, List, Optional, Union import numpy as np from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy lowerCAmelCase__ = logging.get_logger(__name__) class a__ ( snake_case ): """simple docstring""" def __init__( self , lowercase , lowercase , lowercase , **lowercase ) -> Union[str, Any]: '''simple docstring''' A__ = feature_size A__ = sampling_rate A__ = padding_value A__ = kwargs.pop("padding_side" , "right" ) A__ = kwargs.pop("return_attention_mask" , lowercase ) super().__init__(**lowercase ) def UpperCamelCase ( self , lowercase , lowercase = True , lowercase = None , lowercase = False , lowercase = None , lowercase = None , lowercase = None , ) -> BatchFeature: '''simple docstring''' if isinstance(lowercase , (list, tuple) ) and isinstance(processed_features[0] , (dict, BatchFeature) ): A__ = { key: [example[key] for example in processed_features] for key in processed_features[0].keys() } # The model's main input name, usually `input_values`, has be passed for padding if self.model_input_names[0] not in processed_features: raise ValueError( "You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`" F' to this method that includes {self.model_input_names[0]}, but you provided' F' {list(processed_features.keys() )}' ) A__ = processed_features[self.model_input_names[0]] A__ = ( return_attention_mask if return_attention_mask is not None else self.return_attention_mask ) if len(lowercase ) == 0: if return_attention_mask: A__ = [] return processed_features # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays # and rebuild them afterwards if no return_tensors is specified # Note that we lose the specific device the tensor may be on for PyTorch A__ = required_input[0] if isinstance(lowercase , (list, tuple) ): # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element. A__ = 0 while len(required_input[index] ) == 0: index += 1 if index < len(lowercase ): A__ = required_input[index][0] if return_tensors is None: if is_tf_tensor(lowercase ): A__ = "tf" elif is_torch_tensor(lowercase ): A__ = "pt" elif isinstance(lowercase , (int, float, list, tuple, np.ndarray) ): A__ = "np" else: raise ValueError( F'type of {first_element} unknown: {type(lowercase )}. ' "Should be one of a python, numpy, pytorch or tensorflow object." ) for key, value in processed_features.items(): if isinstance(value[0] , (int, float) ): A__ = to_numpy(lowercase ) else: A__ = [to_numpy(lowercase ) for v in value] # Convert padding_strategy in PaddingStrategy A__ = self._get_padding_strategies(padding=lowercase , max_length=lowercase ) A__ = processed_features[self.model_input_names[0]] A__ = len(lowercase ) if not all(len(lowercase ) == batch_size for v in processed_features.values() ): raise ValueError("Some items in the output dictionary have a different batch size than others." ) A__ = [] for i in range(lowercase ): A__ = {k: v[i] for k, v in processed_features.items()} # truncation A__ = self._truncate( lowercase , max_length=lowercase , pad_to_multiple_of=lowercase , truncation=lowercase , ) truncated_inputs.append(lowercase ) if padding_strategy == PaddingStrategy.LONGEST: # make sure that `max_length` cannot be longer than the longest truncated length A__ = max(len(input_slice[self.model_input_names[0]] ) for input_slice in truncated_inputs ) A__ = PaddingStrategy.MAX_LENGTH A__ = {} for i in range(lowercase ): # padding A__ = self._pad( truncated_inputs[i] , max_length=lowercase , padding_strategy=lowercase , pad_to_multiple_of=lowercase , return_attention_mask=lowercase , ) for key, value in outputs.items(): if key not in batch_outputs: A__ = [] if value.dtype is np.dtype(np.floataa ): A__ = value.astype(np.floataa ) batch_outputs[key].append(lowercase ) return BatchFeature(lowercase , tensor_type=lowercase ) def UpperCamelCase ( self , lowercase , lowercase = None , lowercase = PaddingStrategy.DO_NOT_PAD , lowercase = None , lowercase = None , ) -> dict: '''simple docstring''' A__ = processed_features[self.model_input_names[0]] if padding_strategy == PaddingStrategy.LONGEST: A__ = len(lowercase ) if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): A__ = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of A__ = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(lowercase ) < max_length if return_attention_mask and "attention_mask" not in processed_features: A__ = np.ones(len(lowercase ) , dtype=np.intaa ) if needs_to_be_padded: A__ = max_length - len(lowercase ) if self.padding_side == "right": if return_attention_mask: A__ = np.pad( processed_features["attention_mask"] , (0, difference) ) A__ = ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference) A__ = np.pad( lowercase , lowercase , "constant" , constant_values=self.padding_value ) elif self.padding_side == "left": if return_attention_mask: A__ = np.pad( processed_features["attention_mask"] , (difference, 0) ) A__ = ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0) A__ = np.pad( lowercase , lowercase , "constant" , constant_values=self.padding_value ) else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return processed_features def UpperCamelCase ( self , lowercase , lowercase = None , lowercase = None , lowercase = None , ) -> Union[str, Any]: '''simple docstring''' if not truncation: return processed_features elif truncation and max_length is None: raise ValueError("When setting ``truncation=True``, make sure that ``max_length`` is defined." ) A__ = processed_features[self.model_input_names[0]] # find `max_length` that fits `pad_to_multiple_of` if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): A__ = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of A__ = len(lowercase ) > max_length if needs_to_be_truncated: A__ = processed_features[self.model_input_names[0]][:max_length] if "attention_mask" in processed_features: A__ = processed_features["attention_mask"][:max_length] return processed_features def UpperCamelCase ( self , lowercase=False , lowercase=None ) -> Any: '''simple docstring''' if padding is not False: if padding is True: A__ = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch elif not isinstance(lowercase , lowercase ): A__ = PaddingStrategy(lowercase ) elif isinstance(lowercase , lowercase ): A__ = padding else: A__ = PaddingStrategy.DO_NOT_PAD # Set max length if needed if max_length is None: if padding_strategy == PaddingStrategy.MAX_LENGTH: raise ValueError( F'When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined' ) # Test if we have a padding value if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None): raise ValueError( "Asking to pad but the feature_extractor does not have a padding value. Please select a value to use" " as `padding_value`. For example: `feature_extractor.padding_value = 0.0`." ) return padding_strategy
68
import argparse import json import numpy import torch from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def A ( a_ ,a_ ) -> Optional[Any]: # Load checkpoint __UpperCamelCase : int =torch.load(a_ ,map_location='cpu' ) __UpperCamelCase : List[Any] =chkpt['model'] # We have the base model one level deeper than the original XLM repository __UpperCamelCase : str ={} for k, v in state_dict.items(): if "pred_layer" in k: __UpperCamelCase : Optional[Any] =v else: __UpperCamelCase : Optional[Any] =v __UpperCamelCase : List[Any] =chkpt['params'] __UpperCamelCase : str ={n: v for n, v in config.items() if not isinstance(a_ ,(torch.FloatTensor, numpy.ndarray) )} __UpperCamelCase : str =chkpt['dico_word2id'] __UpperCamelCase : Dict ={s + '</w>' if s.find('@@' ) == -1 and i > 13 else s.replace('@@' ,'' ): i for s, i in vocab.items()} # Save pytorch-model __UpperCamelCase : List[Any] =pytorch_dump_folder_path + '/' + WEIGHTS_NAME __UpperCamelCase : Tuple =pytorch_dump_folder_path + '/' + CONFIG_NAME __UpperCamelCase : Any =pytorch_dump_folder_path + '/' + VOCAB_FILES_NAMES['vocab_file'] print(F'Save PyTorch model to {pytorch_weights_dump_path}' ) torch.save(a_ ,a_ ) print(F'Save configuration file to {pytorch_config_dump_path}' ) with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(json.dumps(a_ ,indent=2 ) + '\n' ) print(F'Save vocab file to {pytorch_config_dump_path}' ) with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(json.dumps(a_ ,indent=2 ) + '\n' ) if __name__ == "__main__": A_ :str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--xlm_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.''' ) A_ :List[Any] = parser.parse_args() convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)
71
0
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, ChannelDimension, ImageInput, PILImageResampling, is_batched, to_numpy_array, valid_images, ) from ...utils import TensorType, logging __UpperCamelCase = logging.get_logger(__name__) class UpperCamelCase ( lowerCAmelCase__ ): SCREAMING_SNAKE_CASE_ = ["pixel_values"] def __init__( self, lowerCAmelCase__ = True, lowerCAmelCase__ = None, lowerCAmelCase__ = PILImageResampling.BICUBIC, lowerCAmelCase__ = True, lowerCAmelCase__ = True, lowerCAmelCase__ = 1 / 255, lowerCAmelCase__ = None, lowerCAmelCase__ = True, lowerCAmelCase__ = None, lowerCAmelCase__ = None, **lowerCAmelCase__, ) -> None: super().__init__(**lowerCAmelCase__) snake_case_ = size if size is not None else {'height': 224, 'width': 224} snake_case_ = get_size_dict(lowerCAmelCase__) snake_case_ = crop_size if crop_size is not None else {'height': 224, 'width': 224} snake_case_ = get_size_dict(lowerCAmelCase__, default_to_square=lowerCAmelCase__, param_name='crop_size') snake_case_ = do_resize snake_case_ = do_rescale snake_case_ = do_normalize snake_case_ = do_center_crop snake_case_ = crop_size snake_case_ = size snake_case_ = resample snake_case_ = rescale_factor snake_case_ = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN snake_case_ = image_std if image_std is not None else IMAGENET_DEFAULT_STD def a_ ( self, lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ = PILImageResampling.BILINEAR, lowerCAmelCase__ = None, **lowerCAmelCase__, ) -> np.ndarray: snake_case_ = get_size_dict(lowerCAmelCase__) if "shortest_edge" in size: snake_case_ = get_resize_output_image_size(lowerCAmelCase__, size=size['shortest_edge'], default_to_square=lowerCAmelCase__) # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"]) elif "height" in size and "width" in size: snake_case_ = (size['height'], size['width']) else: raise ValueError(f'Size must contain \'height\' and \'width\' keys or \'shortest_edge\' key. Got {size.keys()}') return resize(lowerCAmelCase__, size=lowerCAmelCase__, resample=lowerCAmelCase__, data_format=lowerCAmelCase__, **lowerCAmelCase__) def a_ ( self, lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ = None, **lowerCAmelCase__, ) -> np.ndarray: snake_case_ = get_size_dict(lowerCAmelCase__) if "height" not in size or "width" not in size: raise ValueError(f'The `size` parameter must contain the keys (height, width). Got {size.keys()}') return center_crop(lowerCAmelCase__, size=(size['height'], size['width']), data_format=lowerCAmelCase__, **lowerCAmelCase__) def a_ ( self, lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ = None, **lowerCAmelCase__) -> np.ndarray: return rescale(lowerCAmelCase__, scale=lowerCAmelCase__, data_format=lowerCAmelCase__, **lowerCAmelCase__) def a_ ( self, lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ = None, **lowerCAmelCase__, ) -> np.ndarray: return normalize(lowerCAmelCase__, mean=lowerCAmelCase__, std=lowerCAmelCase__, data_format=lowerCAmelCase__, **lowerCAmelCase__) def a_ ( self, lowerCAmelCase__, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = ChannelDimension.FIRST, **lowerCAmelCase__, ) -> BatchFeature: snake_case_ = do_resize if do_resize is not None else self.do_resize snake_case_ = do_rescale if do_rescale is not None else self.do_rescale snake_case_ = do_normalize if do_normalize is not None else self.do_normalize snake_case_ = do_center_crop if do_center_crop is not None else self.do_center_crop snake_case_ = crop_size if crop_size is not None else self.crop_size snake_case_ = get_size_dict(lowerCAmelCase__, param_name='crop_size', default_to_square=lowerCAmelCase__) snake_case_ = resample if resample is not None else self.resample snake_case_ = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case_ = image_mean if image_mean is not None else self.image_mean snake_case_ = image_std if image_std is not None else self.image_std snake_case_ = size if size is not None else self.size snake_case_ = get_size_dict(lowerCAmelCase__) if not is_batched(lowerCAmelCase__): snake_case_ = [images] if not valid_images(lowerCAmelCase__): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.') if do_resize and size is None: raise ValueError('Size must be specified if do_resize is True.') if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.') if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.') # All transformations expect numpy arrays. snake_case_ = [to_numpy_array(lowerCAmelCase__) for image in images] if do_resize: snake_case_ = [self.resize(image=lowerCAmelCase__, size=lowerCAmelCase__, resample=lowerCAmelCase__) for image in images] if do_center_crop: snake_case_ = [self.center_crop(image=lowerCAmelCase__, size=lowerCAmelCase__) for image in images] if do_rescale: snake_case_ = [self.rescale(image=lowerCAmelCase__, scale=lowerCAmelCase__) for image in images] if do_normalize: snake_case_ = [self.normalize(image=lowerCAmelCase__, mean=lowerCAmelCase__, std=lowerCAmelCase__) for image in images] snake_case_ = [to_channel_dimension_format(lowerCAmelCase__, lowerCAmelCase__) for image in images] snake_case_ = {'pixel_values': images} return BatchFeature(data=lowerCAmelCase__, tensor_type=lowerCAmelCase__)
69
import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class __A ( a ): """simple docstring""" def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str =tempfile.mkdtemp() __UpperCamelCase : Optional[int] =8 # DPR tok __UpperCamelCase : str =[ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] __UpperCamelCase : Optional[Any] =os.path.join(self.tmpdirname , 'dpr_tokenizer' ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) __UpperCamelCase : Dict =os.path.join(lowerCamelCase__ , DPR_VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) # BART tok __UpperCamelCase : Optional[int] =[ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] __UpperCamelCase : str =dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) __UpperCamelCase : Optional[int] =['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] __UpperCamelCase : Any ={'unk_token': '<unk>'} __UpperCamelCase : Any =os.path.join(self.tmpdirname , 'bart_tokenizer' ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) __UpperCamelCase : Any =os.path.join(lowerCamelCase__ , BART_VOCAB_FILES_NAMES['vocab_file'] ) __UpperCamelCase : Dict =os.path.join(lowerCamelCase__ , BART_VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(lowerCamelCase__ ) ) def __lowercase ( self ): """simple docstring""" return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'dpr_tokenizer' ) ) def __lowercase ( self ): """simple docstring""" return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'bart_tokenizer' ) ) def __lowercase ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) @require_tokenizers def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =os.path.join(self.tmpdirname , 'rag_tokenizer' ) __UpperCamelCase : Dict =RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) __UpperCamelCase : List[Any] =RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(lowerCamelCase__ ) rag_tokenizer.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : int =RagTokenizer.from_pretrained(lowerCamelCase__ , config=lowerCamelCase__ ) self.assertIsInstance(new_rag_tokenizer.question_encoder , lowerCamelCase__ ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , lowerCamelCase__ ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Dict =RagTokenizer.from_pretrained('facebook/rag-token-nq' ) __UpperCamelCase : Union[str, Any] =[ 'who got the first nobel prize in physics', 'when is the next deadpool movie being released', 'which mode is used for short wave broadcast service', 'who is the owner of reading football club', 'when is the next scandal episode coming out', 'when is the last time the philadelphia won the superbowl', 'what is the most current adobe flash player version', 'how many episodes are there in dragon ball z', 'what is the first step in the evolution of the eye', 'where is gall bladder situated in human body', 'what is the main mineral in lithium batteries', 'who is the president of usa right now', 'where do the greasers live in the outsiders', 'panda is a national animal of which country', 'what is the name of manchester united stadium', ] __UpperCamelCase : int =tokenizer(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =RagTokenizer.from_pretrained('facebook/rag-sequence-nq' ) __UpperCamelCase : Union[str, Any] =[ 'who got the first nobel prize in physics', 'when is the next deadpool movie being released', 'which mode is used for short wave broadcast service', 'who is the owner of reading football club', 'when is the next scandal episode coming out', 'when is the last time the philadelphia won the superbowl', 'what is the most current adobe flash player version', 'how many episodes are there in dragon ball z', 'what is the first step in the evolution of the eye', 'where is gall bladder situated in human body', 'what is the main mineral in lithium batteries', 'who is the president of usa right now', 'where do the greasers live in the outsiders', 'panda is a national animal of which country', 'what is the name of manchester united stadium', ] __UpperCamelCase : Any =tokenizer(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ )
71
0
'''simple docstring''' import json import os import shutil import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoConfig, BertConfig, GPTaConfig from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import TOKEN, USER, is_staging_test sys.path.append(str(Path(__file__).parent.parent / '''utils''')) from test_module.custom_configuration import CustomConfig # noqa E402 A__ : Any ={ '''return_dict''': False, '''output_hidden_states''': True, '''output_attentions''': True, '''torchscript''': True, '''torch_dtype''': '''float16''', '''use_bfloat16''': True, '''tf_legacy_loss''': True, '''pruned_heads''': {'''a''': 1}, '''tie_word_embeddings''': False, '''is_decoder''': True, '''cross_attention_hidden_size''': 1_28, '''add_cross_attention''': True, '''tie_encoder_decoder''': True, '''max_length''': 50, '''min_length''': 3, '''do_sample''': True, '''early_stopping''': True, '''num_beams''': 3, '''num_beam_groups''': 3, '''diversity_penalty''': 0.5, '''temperature''': 2.0, '''top_k''': 10, '''top_p''': 0.7, '''typical_p''': 0.2, '''repetition_penalty''': 0.8, '''length_penalty''': 0.8, '''no_repeat_ngram_size''': 5, '''encoder_no_repeat_ngram_size''': 5, '''bad_words_ids''': [1, 2, 3], '''num_return_sequences''': 3, '''chunk_size_feed_forward''': 5, '''output_scores''': True, '''return_dict_in_generate''': True, '''forced_bos_token_id''': 2, '''forced_eos_token_id''': 3, '''remove_invalid_values''': True, '''architectures''': ['''BertModel'''], '''finetuning_task''': '''translation''', '''id2label''': {0: '''label'''}, '''label2id''': {'''label''': '''0'''}, '''tokenizer_class''': '''BertTokenizerFast''', '''prefix''': '''prefix''', '''bos_token_id''': 6, '''pad_token_id''': 7, '''eos_token_id''': 8, '''sep_token_id''': 9, '''decoder_start_token_id''': 10, '''exponential_decay_length_penalty''': (5, 1.01), '''suppress_tokens''': [0, 1], '''begin_suppress_tokens''': 2, '''task_specific_params''': {'''translation''': '''some_params'''}, '''problem_type''': '''regression''', } @is_staging_test class UpperCAmelCase ( unittest.TestCase ): @classmethod def lowercase__ ( cls : Optional[Any] ) -> Tuple: _lowerCAmelCase = TOKEN HfFolder.save_token(__snake_case ) @classmethod def lowercase__ ( cls : Optional[int] ) -> List[str]: try: delete_repo(token=cls._token , repo_id="""test-config""" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="""valid_org/test-config-org""" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="""test-dynamic-config""" ) except HTTPError: pass def lowercase__ ( self : int ) -> Union[str, Any]: _lowerCAmelCase = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) config.push_to_hub("""test-config""" , use_auth_token=self._token ) _lowerCAmelCase = BertConfig.from_pretrained(f"{USER}/test-config" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__snake_case , getattr(__snake_case , __snake_case ) ) # Reset repo delete_repo(token=self._token , repo_id="""test-config""" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(__snake_case , repo_id="""test-config""" , push_to_hub=__snake_case , use_auth_token=self._token ) _lowerCAmelCase = BertConfig.from_pretrained(f"{USER}/test-config" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__snake_case , getattr(__snake_case , __snake_case ) ) def lowercase__ ( self : Union[str, Any] ) -> List[Any]: _lowerCAmelCase = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) config.push_to_hub("""valid_org/test-config-org""" , use_auth_token=self._token ) _lowerCAmelCase = BertConfig.from_pretrained("""valid_org/test-config-org""" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__snake_case , getattr(__snake_case , __snake_case ) ) # Reset repo delete_repo(token=self._token , repo_id="""valid_org/test-config-org""" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( __snake_case , repo_id="""valid_org/test-config-org""" , push_to_hub=__snake_case , use_auth_token=self._token ) _lowerCAmelCase = BertConfig.from_pretrained("""valid_org/test-config-org""" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__snake_case , getattr(__snake_case , __snake_case ) ) def lowercase__ ( self : Dict ) -> Any: CustomConfig.register_for_auto_class() _lowerCAmelCase = CustomConfig(attribute=42 ) config.push_to_hub("""test-dynamic-config""" , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual(config.auto_map , {"""AutoConfig""": """custom_configuration.CustomConfig"""} ) _lowerCAmelCase = AutoConfig.from_pretrained(f"{USER}/test-dynamic-config" , trust_remote_code=__snake_case ) # Can't make an isinstance check because the new_config is from the FakeConfig class of a dynamic module self.assertEqual(new_config.__class__.__name__ , """CustomConfig""" ) self.assertEqual(new_config.attribute , 42 ) class UpperCAmelCase ( unittest.TestCase ): def lowercase__ ( self : List[Any] ) -> Optional[Any]: _lowerCAmelCase = GPTaConfig() # attempt to modify each of int/float/bool/str config records and verify they were updated _lowerCAmelCase = c.n_embd + 1 # int _lowerCAmelCase = c.resid_pdrop + 1.0 # float _lowerCAmelCase = not c.scale_attn_weights # bool _lowerCAmelCase = c.summary_type + """foo""" # str c.update_from_string( f"n_embd={n_embd},resid_pdrop={resid_pdrop},scale_attn_weights={scale_attn_weights},summary_type={summary_type}" ) self.assertEqual(__snake_case , c.n_embd , """mismatch for key: n_embd""" ) self.assertEqual(__snake_case , c.resid_pdrop , """mismatch for key: resid_pdrop""" ) self.assertEqual(__snake_case , c.scale_attn_weights , """mismatch for key: scale_attn_weights""" ) self.assertEqual(__snake_case , c.summary_type , """mismatch for key: summary_type""" ) def lowercase__ ( self : Optional[Any] ) -> Any: _lowerCAmelCase = PretrainedConfig() _lowerCAmelCase = [key for key in base_config.__dict__ if key not in config_common_kwargs] # If this part of the test fails, you have arguments to addin config_common_kwargs above. self.assertListEqual( __snake_case , ["""is_encoder_decoder""", """_name_or_path""", """_commit_hash""", """transformers_version"""] ) _lowerCAmelCase = [key for key, value in config_common_kwargs.items() if value == getattr(__snake_case , __snake_case )] if len(__snake_case ) > 0: raise ValueError( """The following keys are set with the default values in""" """ `test_configuration_common.config_common_kwargs` pick another value for them:""" f" {', '.join(__snake_case )}." ) def lowercase__ ( self : List[str] ) -> List[Any]: with self.assertRaises(__snake_case ): # config is in subfolder, the following should not work without specifying the subfolder _lowerCAmelCase = BertConfig.from_pretrained("""hf-internal-testing/tiny-random-bert-subfolder""" ) _lowerCAmelCase = BertConfig.from_pretrained("""hf-internal-testing/tiny-random-bert-subfolder""" , subfolder="""bert""" ) self.assertIsNotNone(__snake_case ) def lowercase__ ( self : Optional[Any] ) -> str: # A mock response for an HTTP head request to emulate server down _lowerCAmelCase = mock.Mock() _lowerCAmelCase = 5_00 _lowerCAmelCase = {} _lowerCAmelCase = HTTPError _lowerCAmelCase = {} # Download this model to make sure it's in the cache. _lowerCAmelCase = BertConfig.from_pretrained("""hf-internal-testing/tiny-random-bert""" ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch("""requests.Session.request""" , return_value=__snake_case ) as mock_head: _lowerCAmelCase = BertConfig.from_pretrained("""hf-internal-testing/tiny-random-bert""" ) # This check we did call the fake head request mock_head.assert_called() def lowercase__ ( self : str ) -> Union[str, Any]: # This test is for deprecated behavior and can be removed in v5 _lowerCAmelCase = BertConfig.from_pretrained( """https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json""" ) def lowercase__ ( self : str ) -> Union[str, Any]: _lowerCAmelCase = AutoConfig.from_pretrained("""bert-base-cased""" ) _lowerCAmelCase = ["""config.4.0.0.json"""] with tempfile.TemporaryDirectory() as tmp_dir: configuration.save_pretrained(__snake_case ) _lowerCAmelCase = 2 json.dump(configuration.to_dict() , open(os.path.join(__snake_case , """config.4.0.0.json""" ) , """w""" ) ) # This should pick the new configuration file as the version of Transformers is > 4.0.0 _lowerCAmelCase = AutoConfig.from_pretrained(__snake_case ) self.assertEqual(new_configuration.hidden_size , 2 ) # Will need to be adjusted if we reach v42 and this test is still here. # Should pick the old configuration file as the version of Transformers is < 4.42.0 _lowerCAmelCase = ["""config.42.0.0.json"""] _lowerCAmelCase = 7_68 configuration.save_pretrained(__snake_case ) shutil.move(os.path.join(__snake_case , """config.4.0.0.json""" ) , os.path.join(__snake_case , """config.42.0.0.json""" ) ) _lowerCAmelCase = AutoConfig.from_pretrained(__snake_case ) self.assertEqual(new_configuration.hidden_size , 7_68 ) def lowercase__ ( self : Optional[Any] ) -> Optional[Any]: # This repo has two configuration files, one for v4.0.0 and above with a different hidden size. _lowerCAmelCase = """hf-internal-testing/test-two-configs""" import transformers as new_transformers _lowerCAmelCase = """v4.0.0""" _lowerCAmelCase , _lowerCAmelCase = new_transformers.models.auto.AutoConfig.from_pretrained( __snake_case , return_unused_kwargs=__snake_case ) self.assertEqual(new_configuration.hidden_size , 2 ) # This checks `_configuration_file` ia not kept in the kwargs by mistake. self.assertDictEqual(__snake_case , {} ) # Testing an older version by monkey-patching the version in the module it's used. import transformers as old_transformers _lowerCAmelCase = """v3.0.0""" _lowerCAmelCase = old_transformers.models.auto.AutoConfig.from_pretrained(__snake_case ) self.assertEqual(old_configuration.hidden_size , 7_68 )
70
A_ :Optional[int] = ''' # Installazione di Transformers ! pip install transformers datasets # Per installare dalla fonte invece dell\'ultima versione rilasciata, commenta il comando sopra e # rimuovi la modalità commento al comando seguente. # ! pip install git+https://github.com/huggingface/transformers.git ''' A_ :Union[str, Any] = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] A_ :Optional[Any] = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
71
0
"""simple docstring""" lowerCAmelCase__ = { '''A''': ['''B''', '''C''', '''E'''], '''B''': ['''A''', '''D''', '''E'''], '''C''': ['''A''', '''F''', '''G'''], '''D''': ['''B'''], '''E''': ['''A''', '''B''', '''D'''], '''F''': ['''C'''], '''G''': ['''C'''], } def snake_case_ ( A_ : dict, A_ : int, A_ : int ): '''simple docstring''' _lowerCamelCase : List[str] = set() # keep track of all the paths to be checked _lowerCamelCase : str = [[start]] # return path if start is goal if start == goal: return [start] # keeps looping until all possible paths have been checked while queue: # pop the first path from the queue _lowerCamelCase : str = queue.pop(0 ) # get the last node from the path _lowerCamelCase : List[Any] = path[-1] if node not in explored: _lowerCamelCase : Union[str, Any] = graph[node] # go through all neighbour nodes, construct a new path and # push it into the queue for neighbour in neighbours: _lowerCamelCase : Union[str, Any] = list(A_ ) new_path.append(A_ ) queue.append(A_ ) # return path if neighbour is goal if neighbour == goal: return new_path # mark node as explored explored.add(A_ ) # in case there's no path between the 2 nodes return [] def snake_case_ ( A_ : dict, A_ : int, A_ : Dict ): '''simple docstring''' if not graph or start not in graph or target not in graph: return -1 if start == target: return 0 _lowerCamelCase : Optional[int] = [start] _lowerCamelCase : int = set(A_ ) # Keep tab on distances from `start` node. _lowerCamelCase : int = {start: 0, target: -1} while queue: _lowerCamelCase : Optional[Any] = queue.pop(0 ) if node == target: _lowerCamelCase : Any = ( dist[node] if dist[target] == -1 else min(dist[target], dist[node] ) ) for adjacent in graph[node]: if adjacent not in visited: visited.add(A_ ) queue.append(A_ ) _lowerCamelCase : Any = dist[node] + 1 return dist[target] if __name__ == "__main__": print(bfs_shortest_path(demo_graph, '''G''', '''D''')) # returns ['G', 'C', 'A', 'B', 'D'] print(bfs_shortest_path_distance(demo_graph, '''G''', '''D''')) # returns 4
72
import argparse import hashlib import os import urllib import warnings import torch from torch import nn from tqdm import tqdm from transformers import WhisperConfig, WhisperForConditionalGeneration A_ :Optional[Any] = { '''tiny.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt''', '''tiny''': '''https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt''', '''base.en''': '''https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt''', '''base''': '''https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt''', '''small.en''': '''https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt''', '''small''': '''https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt''', '''medium.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt''', '''medium''': '''https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt''', '''large''': '''https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large.pt''', '''large-v2''': '''https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt''', } def A ( a_ ) -> List[Any]: __UpperCamelCase : Any =['layers', 'blocks'] for k in ignore_keys: state_dict.pop(a_ ,a_ ) A_ :int = { '''blocks''': '''layers''', '''mlp.0''': '''fc1''', '''mlp.2''': '''fc2''', '''mlp_ln''': '''final_layer_norm''', '''.attn.query''': '''.self_attn.q_proj''', '''.attn.key''': '''.self_attn.k_proj''', '''.attn.value''': '''.self_attn.v_proj''', '''.attn_ln''': '''.self_attn_layer_norm''', '''.attn.out''': '''.self_attn.out_proj''', '''.cross_attn.query''': '''.encoder_attn.q_proj''', '''.cross_attn.key''': '''.encoder_attn.k_proj''', '''.cross_attn.value''': '''.encoder_attn.v_proj''', '''.cross_attn_ln''': '''.encoder_attn_layer_norm''', '''.cross_attn.out''': '''.encoder_attn.out_proj''', '''decoder.ln.''': '''decoder.layer_norm.''', '''encoder.ln.''': '''encoder.layer_norm.''', '''token_embedding''': '''embed_tokens''', '''encoder.positional_embedding''': '''encoder.embed_positions.weight''', '''decoder.positional_embedding''': '''decoder.embed_positions.weight''', '''ln_post''': '''layer_norm''', } def A ( a_ ) -> Union[str, Any]: __UpperCamelCase : str =list(s_dict.keys() ) for key in keys: __UpperCamelCase : str =key for k, v in WHISPER_MAPPING.items(): if k in key: __UpperCamelCase : Optional[Any] =new_key.replace(a_ ,a_ ) print(F'{key} -> {new_key}' ) __UpperCamelCase : Dict =s_dict.pop(a_ ) return s_dict def A ( a_ ) -> Optional[Any]: __UpperCamelCase , __UpperCamelCase : Tuple =emb.weight.shape __UpperCamelCase : Tuple =nn.Linear(a_ ,a_ ,bias=a_ ) __UpperCamelCase : List[Any] =emb.weight.data return lin_layer def A ( a_ ,a_ ) -> bytes: os.makedirs(a_ ,exist_ok=a_ ) __UpperCamelCase : Optional[int] =os.path.basename(a_ ) __UpperCamelCase : Union[str, Any] =url.split('/' )[-2] __UpperCamelCase : Union[str, Any] =os.path.join(a_ ,a_ ) if os.path.exists(a_ ) and not os.path.isfile(a_ ): raise RuntimeError(F'{download_target} exists and is not a regular file' ) if os.path.isfile(a_ ): __UpperCamelCase : str =open(a_ ,'rb' ).read() if hashlib.shaaaa(a_ ).hexdigest() == expected_shaaaa: return model_bytes else: warnings.warn(F'{download_target} exists, but the SHA256 checksum does not match; re-downloading the file' ) with urllib.request.urlopen(a_ ) as source, open(a_ ,'wb' ) as output: with tqdm( total=int(source.info().get('Content-Length' ) ) ,ncols=80 ,unit='iB' ,unit_scale=a_ ,unit_divisor=1_024 ) as loop: while True: __UpperCamelCase : Optional[Any] =source.read(8_192 ) if not buffer: break output.write(a_ ) loop.update(len(a_ ) ) __UpperCamelCase : List[Any] =open(a_ ,'rb' ).read() if hashlib.shaaaa(a_ ).hexdigest() != expected_shaaaa: raise RuntimeError( 'Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model.' ) return model_bytes def A ( a_ ,a_ ) -> Optional[Any]: if ".pt" not in checkpoint_path: __UpperCamelCase : int =_download(_MODELS[checkpoint_path] ) else: __UpperCamelCase : List[str] =torch.load(a_ ,map_location='cpu' ) __UpperCamelCase : Union[str, Any] =original_checkpoint['dims'] __UpperCamelCase : List[Any] =original_checkpoint['model_state_dict'] __UpperCamelCase : Dict =state_dict['decoder.token_embedding.weight'] remove_ignore_keys_(a_ ) rename_keys(a_ ) __UpperCamelCase : List[str] =True __UpperCamelCase : str =state_dict['decoder.layers.0.fc1.weight'].shape[0] __UpperCamelCase : Optional[int] =WhisperConfig( vocab_size=dimensions['n_vocab'] ,encoder_ffn_dim=a_ ,decoder_ffn_dim=a_ ,num_mel_bins=dimensions['n_mels'] ,d_model=dimensions['n_audio_state'] ,max_target_positions=dimensions['n_text_ctx'] ,encoder_layers=dimensions['n_audio_layer'] ,encoder_attention_heads=dimensions['n_audio_head'] ,decoder_layers=dimensions['n_text_layer'] ,decoder_attention_heads=dimensions['n_text_state'] ,max_source_positions=dimensions['n_audio_ctx'] ,) __UpperCamelCase : List[str] =WhisperForConditionalGeneration(a_ ) __UpperCamelCase , __UpperCamelCase : Union[str, Any] =model.model.load_state_dict(a_ ,strict=a_ ) if len(a_ ) > 0 and not set(a_ ) <= { "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: __UpperCamelCase : Optional[int] =make_linear_from_emb(model.model.decoder.embed_tokens ) else: __UpperCamelCase : List[str] =proj_out_weights model.save_pretrained(a_ ) if __name__ == "__main__": A_ :List[Any] = argparse.ArgumentParser() # # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Patht to the downloaded checkpoints''') parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') A_ :List[Any] = parser.parse_args() convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)
71
0
from __future__ import annotations from random import random class A_ : def __init__( self : Any ,SCREAMING_SNAKE_CASE__ : int | None = None): __lowerCamelCase : Union[str, Any] = value __lowerCamelCase : int = random() __lowerCamelCase : Node | None = None __lowerCamelCase : Node | None = None def __repr__( self : List[Any]): from pprint import pformat if self.left is None and self.right is None: return F"'{self.value}: {self.prior:.5}'" else: return pformat( {F"{self.value}: {self.prior:.5}": (self.left, self.right)} ,indent=1) def __str__( self : Union[str, Any]): __lowerCamelCase : Optional[int] = str(self.value) + ' ' __lowerCamelCase : Optional[Any] = str(self.left or '') __lowerCamelCase : List[Any] = str(self.right or '') return value + left + right def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ ) -> tuple[Node | None, Node | None]: if root is None: # None tree is split into 2 Nones return None, None elif root.value is None: return None, None else: if value < root.value: __lowerCamelCase , __lowerCamelCase : Union[str, Any] = split(root.left , lowerCamelCase__ ) return left, root else: __lowerCamelCase , __lowerCamelCase : List[str] = split(root.right , lowerCamelCase__ ) return root, right def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ ) -> Node | None: if (not left) or (not right): # If one node is None, return the other return left or right elif left.prior < right.prior: __lowerCamelCase : Optional[Any] = merge(left.right , lowerCamelCase__ ) return left else: __lowerCamelCase : List[str] = merge(lowerCamelCase__ , right.left ) return right def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ ) -> Node | None: __lowerCamelCase : List[Any] = Node(lowerCamelCase__ ) __lowerCamelCase , __lowerCamelCase : Optional[int] = split(lowerCamelCase__ , lowerCamelCase__ ) return merge(merge(lowerCamelCase__ , lowerCamelCase__ ) , lowerCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ ) -> Node | None: __lowerCamelCase , __lowerCamelCase : List[str] = split(lowerCamelCase__ , value - 1 ) __lowerCamelCase , __lowerCamelCase : Any = split(lowerCamelCase__ , lowerCamelCase__ ) return merge(lowerCamelCase__ , lowerCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> None: if not root: # None return else: inorder(root.left ) print(root.value , end=',' ) inorder(root.right ) def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ ) -> Node | None: for arg in args.split(): if arg[0] == "+": __lowerCamelCase : str = insert(lowerCamelCase__ , int(arg[1:] ) ) elif arg[0] == "-": __lowerCamelCase : List[Any] = erase(lowerCamelCase__ , int(arg[1:] ) ) else: print('Unknown command' ) return root def SCREAMING_SNAKE_CASE__ ( ) -> None: __lowerCamelCase : int = None print( 'enter numbers to create a tree, + value to add value into treap, ' '- value to erase all nodes with value. \'q\' to quit. ' ) __lowerCamelCase : Optional[int] = input() while args != "q": __lowerCamelCase : Optional[int] = interact_treap(lowerCamelCase__ , lowerCamelCase__ ) print(lowerCamelCase__ ) __lowerCamelCase : int = input() print('good by!' ) if __name__ == "__main__": import doctest doctest.testmod() main()
73
import os from datetime import datetime as dt from github import Github A_ :str = [ '''good first issue''', '''feature request''', '''wip''', ] def A ( ) -> Any: __UpperCamelCase : Any =Github(os.environ['GITHUB_TOKEN'] ) __UpperCamelCase : Union[str, Any] =g.get_repo('huggingface/accelerate' ) __UpperCamelCase : Tuple =repo.get_issues(state='open' ) for issue in open_issues: __UpperCamelCase : List[Any] =sorted([comment for comment in issue.get_comments()] ,key=lambda a_ : i.created_at ,reverse=a_ ) __UpperCamelCase : str =comments[0] if len(a_ ) > 0 else None __UpperCamelCase : Any =dt.utcnow() __UpperCamelCase : List[str] =(current_time - issue.updated_at).days __UpperCamelCase : Union[str, Any] =(current_time - issue.created_at).days if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and days_since_updated > 7 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Close issue since it has been 7 days of inactivity since bot mention. issue.edit(state='closed' ) elif ( days_since_updated > 23 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Add stale comment issue.create_comment( 'This issue has been automatically marked as stale because it has not had ' 'recent activity. If you think this still needs to be addressed ' 'please comment on this thread.\n\nPlease note that issues that do not follow the ' '[contributing guidelines](https://github.com/huggingface/accelerate/blob/main/CONTRIBUTING.md) ' 'are likely to be ignored.' ) if __name__ == "__main__": main()
71
0
"""simple docstring""" import argparse import logging import pickle import random import time import numpy as np from transformers import BertTokenizer, GPTaTokenizer, RobertaTokenizer logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) _lowercase = logging.getLogger(__name__) def _snake_case ( ): A = argparse.ArgumentParser( description='Preprocess the data to avoid re-doing it several times by (tokenization + token_to_ids).' ) parser.add_argument('--file_path' , type=snake_case__ , default='data/dump.txt' , help='The path to the data.' ) parser.add_argument('--tokenizer_type' , type=snake_case__ , default='bert' , choices=['bert', 'roberta', 'gpt2'] ) parser.add_argument('--tokenizer_name' , type=snake_case__ , default='bert-base-uncased' , help='The tokenizer to use.' ) parser.add_argument('--dump_file' , type=snake_case__ , default='data/dump' , help='The dump file prefix.' ) A = parser.parse_args() logger.info(F'Loading Tokenizer ({args.tokenizer_name})' ) if args.tokenizer_type == "bert": A = BertTokenizer.from_pretrained(args.tokenizer_name ) A = tokenizer.special_tokens_map['cls_token'] # `[CLS]` A = tokenizer.special_tokens_map['sep_token'] # `[SEP]` elif args.tokenizer_type == "roberta": A = RobertaTokenizer.from_pretrained(args.tokenizer_name ) A = tokenizer.special_tokens_map['cls_token'] # `<s>` A = tokenizer.special_tokens_map['sep_token'] # `</s>` elif args.tokenizer_type == "gpt2": A = GPTaTokenizer.from_pretrained(args.tokenizer_name ) A = tokenizer.special_tokens_map['bos_token'] # `<|endoftext|>` A = tokenizer.special_tokens_map['eos_token'] # `<|endoftext|>` logger.info(F'Loading text from {args.file_path}' ) with open(args.file_path , 'r' , encoding='utf8' ) as fp: A = fp.readlines() logger.info('Start encoding' ) logger.info(F'{len(snake_case__ )} examples to process.' ) A = [] A = 0 A = 1_0000 A = time.time() for text in data: A = F'{bos} {text.strip()} {sep}' A = tokenizer.encode(snake_case__ , add_special_tokens=snake_case__ ) rslt.append(snake_case__ ) iter += 1 if iter % interval == 0: A = time.time() logger.info(F'{iter} examples processed. - {(end-start):.2f}s/{interval}expl' ) A = time.time() logger.info('Finished binarization' ) logger.info(F'{len(snake_case__ )} examples processed.' ) A = F'{args.dump_file}.{args.tokenizer_name}.pickle' A = tokenizer.vocab_size if vocab_size < (1 << 16): A = [np.uintaa(snake_case__ ) for d in rslt] else: A = [np.intaa(snake_case__ ) for d in rslt] random.shuffle(rslt_ ) logger.info(F'Dump to {dp_file}' ) with open(snake_case__ , 'wb' ) as handle: pickle.dump(rslt_ , snake_case__ , protocol=pickle.HIGHEST_PROTOCOL ) if __name__ == "__main__": main()
74
import re def A ( a_ ) -> bool: __UpperCamelCase : Any =re.compile( r'^(?:0|94|\+94|0{2}94)' r'7(0|1|2|4|5|6|7|8)' r'(-| |)' r'\d{7}$' ) return bool(re.search(a_ ,a_ ) ) if __name__ == "__main__": A_ :List[str] = '''0094702343221''' print(is_sri_lankan_phone_number(phone))
71
0
'''simple docstring''' from math import loga def a_ ( __snake_case : int ) -> int: """simple docstring""" if a < 0: raise ValueError('''Input value must be a positive integer''' ) elif isinstance(__snake_case , __snake_case ): raise TypeError('''Input value must be a \'int\' type''' ) return 0 if (a == 0) else int(loga(a & -a ) ) if __name__ == "__main__": import doctest doctest.testmod()
75
A_ :str = '''0.21.0''' from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
71
0
import argparse import os import re import tensorflow as tf import torch from transformers import BertConfig, BertModel from transformers.utils import logging logging.set_verbosity_info() a_ = logging.get_logger(__name__) def lowerCamelCase__ ( _a , _a , _a): SCREAMING_SNAKE_CASE : Dict = os.path.abspath(_a) logger.info(f"Converting TensorFlow checkpoint from {tf_path}") # Load weights from TF model SCREAMING_SNAKE_CASE : Any = tf.train.list_variables(_a) SCREAMING_SNAKE_CASE : str = [] SCREAMING_SNAKE_CASE : Tuple = [] SCREAMING_SNAKE_CASE : Optional[Any] = [] for full_name, shape in init_vars: # logger.info(f"Loading TF weight {name} with shape {shape}") SCREAMING_SNAKE_CASE : Tuple = full_name.split("/") if full_name == "_CHECKPOINTABLE_OBJECT_GRAPH" or name[0] in ["global_step", "save_counter"]: logger.info(f"Skipping non-model layer {full_name}") continue if "optimizer" in full_name: logger.info(f"Skipping optimization layer {full_name}") continue if name[0] == "model": # ignore initial 'model' SCREAMING_SNAKE_CASE : Optional[Any] = name[1:] # figure out how many levels deep the name is SCREAMING_SNAKE_CASE : Dict = 0 for _name in name: if _name.startswith("layer_with_weights"): depth += 1 else: break layer_depth.append(_a) # read data SCREAMING_SNAKE_CASE : Optional[int] = tf.train.load_variable(_a , _a) names.append("/".join(_a)) arrays.append(_a) logger.info(f"Read a total of {len(_a):,} layers") # Sanity check if len(set(_a)) != 1: raise ValueError(f"Found layer names with different depths (layer depth {list(set(_a))})") SCREAMING_SNAKE_CASE : int = list(set(_a))[0] if layer_depth != 1: raise ValueError( "The model contains more than just the embedding/encoder layers. This script does not handle MLM/NSP" " heads.") # convert layers logger.info("Converting weights...") for full_name, array in zip(_a , _a): SCREAMING_SNAKE_CASE : int = full_name.split("/") SCREAMING_SNAKE_CASE : List[str] = model SCREAMING_SNAKE_CASE : List[str] = [] for i, m_name in enumerate(_a): if m_name == ".ATTRIBUTES": # variable names end with .ATTRIBUTES/VARIABLE_VALUE break if m_name.startswith("layer_with_weights"): SCREAMING_SNAKE_CASE : List[str] = int(m_name.split("-")[-1]) if layer_num <= 2: # embedding layers # layer_num 0: word_embeddings # layer_num 1: position_embeddings # layer_num 2: token_type_embeddings continue elif layer_num == 3: # embedding LayerNorm trace.extend(["embeddings", "LayerNorm"]) SCREAMING_SNAKE_CASE : List[Any] = getattr(_a , "embeddings") SCREAMING_SNAKE_CASE : Any = getattr(_a , "LayerNorm") elif layer_num > 3 and layer_num < config.num_hidden_layers + 4: # encoder layers trace.extend(["encoder", "layer", str(layer_num - 4)]) SCREAMING_SNAKE_CASE : Optional[int] = getattr(_a , "encoder") SCREAMING_SNAKE_CASE : Union[str, Any] = getattr(_a , "layer") SCREAMING_SNAKE_CASE : str = pointer[layer_num - 4] elif layer_num == config.num_hidden_layers + 4: # pooler layer trace.extend(["pooler", "dense"]) SCREAMING_SNAKE_CASE : Dict = getattr(_a , "pooler") SCREAMING_SNAKE_CASE : Union[str, Any] = getattr(_a , "dense") elif m_name == "embeddings": trace.append("embeddings") SCREAMING_SNAKE_CASE : Optional[Any] = getattr(_a , "embeddings") if layer_num == 0: trace.append("word_embeddings") SCREAMING_SNAKE_CASE : str = getattr(_a , "word_embeddings") elif layer_num == 1: trace.append("position_embeddings") SCREAMING_SNAKE_CASE : Optional[int] = getattr(_a , "position_embeddings") elif layer_num == 2: trace.append("token_type_embeddings") SCREAMING_SNAKE_CASE : Optional[int] = getattr(_a , "token_type_embeddings") else: raise ValueError(f"Unknown embedding layer with name {full_name}") trace.append("weight") SCREAMING_SNAKE_CASE : str = getattr(_a , "weight") elif m_name == "_attention_layer": # self-attention layer trace.extend(["attention", "self"]) SCREAMING_SNAKE_CASE : List[str] = getattr(_a , "attention") SCREAMING_SNAKE_CASE : Union[str, Any] = getattr(_a , "self") elif m_name == "_attention_layer_norm": # output attention norm trace.extend(["attention", "output", "LayerNorm"]) SCREAMING_SNAKE_CASE : str = getattr(_a , "attention") SCREAMING_SNAKE_CASE : Optional[Any] = getattr(_a , "output") SCREAMING_SNAKE_CASE : Tuple = getattr(_a , "LayerNorm") elif m_name == "_attention_output_dense": # output attention dense trace.extend(["attention", "output", "dense"]) SCREAMING_SNAKE_CASE : int = getattr(_a , "attention") SCREAMING_SNAKE_CASE : List[str] = getattr(_a , "output") SCREAMING_SNAKE_CASE : int = getattr(_a , "dense") elif m_name == "_output_dense": # output dense trace.extend(["output", "dense"]) SCREAMING_SNAKE_CASE : Union[str, Any] = getattr(_a , "output") SCREAMING_SNAKE_CASE : str = getattr(_a , "dense") elif m_name == "_output_layer_norm": # output dense trace.extend(["output", "LayerNorm"]) SCREAMING_SNAKE_CASE : Dict = getattr(_a , "output") SCREAMING_SNAKE_CASE : List[str] = getattr(_a , "LayerNorm") elif m_name == "_key_dense": # attention key trace.append("key") SCREAMING_SNAKE_CASE : Optional[int] = getattr(_a , "key") elif m_name == "_query_dense": # attention query trace.append("query") SCREAMING_SNAKE_CASE : int = getattr(_a , "query") elif m_name == "_value_dense": # attention value trace.append("value") SCREAMING_SNAKE_CASE : Union[str, Any] = getattr(_a , "value") elif m_name == "_intermediate_dense": # attention intermediate dense trace.extend(["intermediate", "dense"]) SCREAMING_SNAKE_CASE : Optional[Any] = getattr(_a , "intermediate") SCREAMING_SNAKE_CASE : Any = getattr(_a , "dense") elif m_name == "_output_layer_norm": # output layer norm trace.append("output") SCREAMING_SNAKE_CASE : Any = getattr(_a , "output") # weights & biases elif m_name in ["bias", "beta"]: trace.append("bias") SCREAMING_SNAKE_CASE : Optional[int] = getattr(_a , "bias") elif m_name in ["kernel", "gamma"]: trace.append("weight") SCREAMING_SNAKE_CASE : Optional[int] = getattr(_a , "weight") else: logger.warning(f"Ignored {m_name}") # for certain layers reshape is necessary SCREAMING_SNAKE_CASE : List[str] = ".".join(_a) if re.match(r"(\S+)\.attention\.self\.(key|value|query)\.(bias|weight)" , _a) or re.match( r"(\S+)\.attention\.output\.dense\.weight" , _a): SCREAMING_SNAKE_CASE : Optional[Any] = array.reshape(pointer.data.shape) if "kernel" in full_name: SCREAMING_SNAKE_CASE : int = array.transpose() if pointer.shape == array.shape: SCREAMING_SNAKE_CASE : Optional[Any] = torch.from_numpy(_a) else: raise ValueError( f"Shape mismatch in layer {full_name}: Model expects shape {pointer.shape} but layer contains shape:" f" {array.shape}") logger.info(f"Successfully set variable {full_name} to PyTorch layer {trace}") return model def lowerCamelCase__ ( _a , _a , _a): # Instantiate model logger.info(f"Loading model based on config from {config_path}...") SCREAMING_SNAKE_CASE : Optional[int] = BertConfig.from_json_file(_a) SCREAMING_SNAKE_CASE : Dict = BertModel(_a) # Load weights from checkpoint logger.info(f"Loading weights from checkpoint {tf_checkpoint_path}...") load_tfa_weights_in_bert(_a , _a , _a) # Save pytorch-model logger.info(f"Saving PyTorch model to {pytorch_dump_path}...") torch.save(model.state_dict() , _a) if __name__ == "__main__": a_ = argparse.ArgumentParser() parser.add_argument( '--tf_checkpoint_path', type=str, required=True, help='Path to the TensorFlow 2.x checkpoint path.' ) parser.add_argument( '--bert_config_file', type=str, required=True, help='The config json file corresponding to the BERT model. This specifies the model architecture.', ) parser.add_argument( '--pytorch_dump_path', type=str, required=True, help='Path to the output PyTorch model (must include filename).', ) a_ = parser.parse_args() convert_tfa_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)
76
A_ :Union[str, Any] = { 0: '''0''', 1: '''1''', 2: '''2''', 3: '''3''', 4: '''4''', 5: '''5''', 6: '''6''', 7: '''7''', 8: '''8''', 9: '''9''', 10: '''a''', 11: '''b''', 12: '''c''', 13: '''d''', 14: '''e''', 15: '''f''', } def A ( a_ ) -> str: assert type(a_ ) in (int, float) and decimal == int(a_ ) __UpperCamelCase : Union[str, Any] =int(a_ ) __UpperCamelCase : List[str] ='' __UpperCamelCase : Optional[Any] =False if decimal < 0: __UpperCamelCase : Tuple =True decimal *= -1 while decimal > 0: __UpperCamelCase , __UpperCamelCase : Optional[Any] =divmod(a_ ,16 ) __UpperCamelCase : Tuple =values[remainder] + hexadecimal __UpperCamelCase : Dict ='0x' + hexadecimal if negative: __UpperCamelCase : int ='-' + hexadecimal return hexadecimal if __name__ == "__main__": import doctest doctest.testmod()
71
0
"""simple docstring""" import os import pytest from attr import dataclass _UpperCamelCase : Any = "us-east-1" # defaults region @dataclass class UpperCAmelCase_ : lowerCamelCase__ : str lowerCamelCase__ : List[Any] = "arn:aws:iam::558105141721:role/sagemaker_execution_role" lowerCamelCase__ : str = { "task_name": "mnli", "per_device_train_batch_size": 1_6, "per_device_eval_batch_size": 1_6, "do_train": True, "do_eval": True, "do_predict": True, "output_dir": "/opt/ml/model", "overwrite_output_dir": True, "max_steps": 5_0_0, "save_steps": 5_5_0_0, } lowerCamelCase__ : Optional[int] = {**hyperparameters, "max_steps": 1_0_0_0} @property def _UpperCAmelCase ( self ) -> str: if self.framework == "pytorch": return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"}, {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"}, ] else: return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"}, {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"}, ] @property def _UpperCAmelCase ( self ) -> str: return f"""{self.framework}-transfromers-test""" @property def _UpperCAmelCase ( self ) -> str: return f"""./tests/sagemaker/scripts/{self.framework}""" @property def _UpperCAmelCase ( self ) -> str: if self.framework == "pytorch": return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04" else: return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04" @pytest.fixture(scope='class' ) def a_ ( _lowerCAmelCase : int ): '''simple docstring''' lowercase__ : Tuple = SageMakerTestEnvironment(framework=request.cls.framework )
77
import itertools import os from collections import Counter, defaultdict from concurrent.futures import ThreadPoolExecutor, as_completed import numpy as np import datasets from .execute import check_correctness A_ :List[str] = '''\ @misc{chen2021evaluating, title={Evaluating Large Language Models Trained on Code}, author={Mark Chen and Jerry Tworek and Heewoo Jun and Qiming Yuan \ and Henrique Ponde de Oliveira Pinto and Jared Kaplan and Harri Edwards \ and Yuri Burda and Nicholas Joseph and Greg Brockman and Alex Ray \ and Raul Puri and Gretchen Krueger and Michael Petrov and Heidy Khlaaf \ and Girish Sastry and Pamela Mishkin and Brooke Chan and Scott Gray \ and Nick Ryder and Mikhail Pavlov and Alethea Power and Lukasz Kaiser \ and Mohammad Bavarian and Clemens Winter and Philippe Tillet \ and Felipe Petroski Such and Dave Cummings and Matthias Plappert \ and Fotios Chantzis and Elizabeth Barnes and Ariel Herbert-Voss \ and William Hebgen Guss and Alex Nichol and Alex Paino and Nikolas Tezak \ and Jie Tang and Igor Babuschkin and Suchir Balaji and Shantanu Jain \ and William Saunders and Christopher Hesse and Andrew N. Carr \ and Jan Leike and Josh Achiam and Vedant Misra and Evan Morikawa \ and Alec Radford and Matthew Knight and Miles Brundage and Mira Murati \ and Katie Mayer and Peter Welinder and Bob McGrew and Dario Amodei \ and Sam McCandlish and Ilya Sutskever and Wojciech Zaremba}, year={2021}, eprint={2107.03374}, archivePrefix={arXiv}, primaryClass={cs.LG} } ''' A_ :Any = '''\ This metric implements the evaluation harness for the HumanEval problem solving dataset described in the paper "Evaluating Large Language Models Trained on Code" (https://arxiv.org/abs/2107.03374). ''' A_ :Tuple = ''' Calculates how good are predictions given some references, using certain scores Args: predictions: list of candidates to evaluate. Each candidates should be a list of strings with several code candidates to solve the problem. references: a list with a test for each prediction. Each test should evaluate the correctness of a code candidate. k: number of code candidates to consider in the evaluation (Default: [1, 10, 100]) num_workers: number of workers used to evaluate the canidate programs (Default: 4). timeout: Returns: pass_at_k: dict with pass rates for each k results: dict with granular results of each unittest Examples: >>> code_eval = datasets.load_metric("code_eval") >>> test_cases = ["assert add(2,3)==5"] >>> candidates = [["def add(a,b): return a*b", "def add(a, b): return a+b"]] >>> pass_at_k, results = code_eval.compute(references=test_cases, predictions=candidates, k=[1, 2]) >>> print(pass_at_k) {\'pass@1\': 0.5, \'pass@2\': 1.0} ''' A_ :List[str] = ''' ################################################################################ !!!WARNING!!! ################################################################################ The "code_eval" metric executes untrusted model-generated code in Python. Although it is highly unlikely that model-generated code will do something overtly malicious in response to this test suite, model-generated code may act destructively due to a lack of model capability or alignment. Users are strongly encouraged to sandbox this evaluation suite so that it does not perform destructive actions on their host or network. For more information on how OpenAI sandboxes its code, see the paper "Evaluating Large Language Models Trained on Code" (https://arxiv.org/abs/2107.03374). Once you have read this disclaimer and taken appropriate precautions, set the environment variable HF_ALLOW_CODE_EVAL="1". Within Python you can to this with: >>> import os >>> os.environ["HF_ALLOW_CODE_EVAL"] = "1" ################################################################################\ ''' A_ :Tuple = '''The MIT License Copyright (c) OpenAI (https://openai.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __A ( datasets.Metric ): """simple docstring""" def __lowercase ( self ): """simple docstring""" return datasets.MetricInfo( # This is the description that will appear on the metrics page. description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string' ) ), 'references': datasets.Value('string' ), } ) , homepage='https://github.com/openai/human-eval' , codebase_urls=['https://github.com/openai/human-eval'] , reference_urls=['https://github.com/openai/human-eval'] , license=_LICENSE , ) def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__=[1, 10, 100] , lowerCamelCase__=4 , lowerCamelCase__=3.0 ): """simple docstring""" if os.getenv('HF_ALLOW_CODE_EVAL' , 0 ) != "1": raise ValueError(_WARNING ) if os.name == "nt": raise NotImplementedError('This metric is currently not supported on Windows.' ) with ThreadPoolExecutor(max_workers=lowerCamelCase__ ) as executor: __UpperCamelCase : List[str] =[] __UpperCamelCase : Any =Counter() __UpperCamelCase : List[Any] =0 __UpperCamelCase : int =defaultdict(lowerCamelCase__ ) for task_id, (candidates, test_case) in enumerate(zip(lowerCamelCase__ , lowerCamelCase__ ) ): for candidate in candidates: __UpperCamelCase : str =candidate + '\n' + test_case __UpperCamelCase : Any =(test_program, timeout, task_id, completion_id[task_id]) __UpperCamelCase : Optional[Any] =executor.submit(lowerCamelCase__ , *lowerCamelCase__ ) futures.append(lowerCamelCase__ ) completion_id[task_id] += 1 n_samples += 1 for future in as_completed(lowerCamelCase__ ): __UpperCamelCase : str =future.result() results[result["task_id"]].append((result['completion_id'], result) ) __UpperCamelCase , __UpperCamelCase : int =[], [] for result in results.values(): result.sort() __UpperCamelCase : str =[r[1]['passed'] for r in result] total.append(len(lowerCamelCase__ ) ) correct.append(sum(lowerCamelCase__ ) ) __UpperCamelCase : Optional[int] =np.array(lowerCamelCase__ ) __UpperCamelCase : List[str] =np.array(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =k __UpperCamelCase : List[Any] ={f'pass@{k}': estimate_pass_at_k(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ).mean() for k in ks if (total >= k).all()} return pass_at_k, results def A ( a_ ,a_ ,a_ ) -> Optional[int]: def estimator(a_ ,a_ ,a_ ) -> float: if n - c < k: return 1.0 return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1 ,n + 1 ) ) if isinstance(a_ ,a_ ): __UpperCamelCase : Optional[int] =itertools.repeat(a_ ,len(a_ ) ) else: assert len(a_ ) == len(a_ ) __UpperCamelCase : List[Any] =iter(a_ ) return np.array([estimator(int(a_ ) ,int(a_ ) ,a_ ) for n, c in zip(a_ ,a_ )] )
71
0
"""simple docstring""" import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class A_ ( SCREAMING_SNAKE_CASE_ ): """simple docstring""" __UpperCamelCase = ["""image_processor""", """tokenizer"""] __UpperCamelCase = """LayoutLMv3ImageProcessor""" __UpperCamelCase = ("""LayoutLMv3Tokenizer""", """LayoutLMv3TokenizerFast""") def __init__( self :Dict , lowercase_ :Tuple=None , lowercase_ :Optional[int]=None , **lowercase_ :Optional[int] ) -> List[Any]: UpperCAmelCase = None if "feature_extractor" in kwargs: warnings.warn( 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`' ' instead.' , lowercase_ , ) UpperCAmelCase = kwargs.pop('feature_extractor' ) UpperCAmelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('You need to specify an `image_processor`.' ) if tokenizer is None: raise ValueError('You need to specify a `tokenizer`.' ) super().__init__(lowercase_ , lowercase_ ) def __call__( self :List[Any] , lowercase_ :Union[str, Any] , lowercase_ :Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , lowercase_ :Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None , lowercase_ :Union[List[List[int]], List[List[List[int]]]] = None , lowercase_ :Optional[Union[List[int], List[List[int]]]] = None , lowercase_ :bool = True , lowercase_ :Union[bool, str, PaddingStrategy] = False , lowercase_ :Union[bool, str, TruncationStrategy] = None , lowercase_ :Optional[int] = None , lowercase_ :int = 0 , lowercase_ :Optional[int] = None , lowercase_ :Optional[bool] = None , lowercase_ :Optional[bool] = None , lowercase_ :bool = False , lowercase_ :bool = False , lowercase_ :bool = False , lowercase_ :bool = False , lowercase_ :bool = True , lowercase_ :Optional[Union[str, TensorType]] = None , **lowercase_ :Tuple , ) -> BatchEncoding: # verify input if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( 'You cannot provide bounding boxes if you initialized the image processor with apply_ocr set to True.' ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( 'You cannot provide word labels if you initialized the image processor with apply_ocr set to True.' ) # first, apply the image processor UpperCAmelCase = self.image_processor(images=lowercase_ , return_tensors=lowercase_ ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(lowercase_ , lowercase_ ): UpperCAmelCase = [text] # add batch dimension (as the image processor always adds a batch dimension) UpperCAmelCase = features['words'] UpperCAmelCase = self.tokenizer( text=text if text is not None else features['words'] , text_pair=text_pair if text_pair is not None else None , boxes=boxes if boxes is not None else features['boxes'] , word_labels=lowercase_ , add_special_tokens=lowercase_ , padding=lowercase_ , truncation=lowercase_ , max_length=lowercase_ , stride=lowercase_ , pad_to_multiple_of=lowercase_ , return_token_type_ids=lowercase_ , return_attention_mask=lowercase_ , return_overflowing_tokens=lowercase_ , return_special_tokens_mask=lowercase_ , return_offsets_mapping=lowercase_ , return_length=lowercase_ , verbose=lowercase_ , return_tensors=lowercase_ , **lowercase_ , ) # add pixel values UpperCAmelCase = features.pop('pixel_values' ) if return_overflowing_tokens is True: UpperCAmelCase = self.get_overflowing_images(lowercase_ , encoded_inputs['overflow_to_sample_mapping'] ) UpperCAmelCase = images return encoded_inputs def UpperCAmelCase__ ( self :Optional[int] , lowercase_ :Optional[Any] , lowercase_ :Union[str, Any] ) -> Union[str, Any]: # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image UpperCAmelCase = [] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(lowercase_ ) != len(lowercase_ ): raise ValueError( 'Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got' f""" {len(lowercase_ )} and {len(lowercase_ )}""" ) return images_with_overflow def UpperCAmelCase__ ( self :Optional[Any] , *lowercase_ :Any , **lowercase_ :Dict ) -> List[str]: return self.tokenizer.batch_decode(*lowercase_ , **lowercase_ ) def UpperCAmelCase__ ( self :Tuple , *lowercase_ :str , **lowercase_ :Tuple ) -> Dict: return self.tokenizer.decode(*lowercase_ , **lowercase_ ) @property def UpperCAmelCase__ ( self :Any ) -> Any: return ["input_ids", "bbox", "attention_mask", "pixel_values"] @property def UpperCAmelCase__ ( self :Any ) -> Tuple: warnings.warn( '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.' , lowercase_ , ) return self.image_processor_class @property def UpperCAmelCase__ ( self :Dict ) -> int: warnings.warn( '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.' , lowercase_ , ) return self.image_processor
78
import gc import random import tempfile import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMInverseScheduler, DDIMScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, StableDiffusionDiffEditPipeline, UNetaDConditionModel, ) from diffusers.utils import load_image, slow from diffusers.utils.testing_utils import enable_full_determinism, floats_tensor, require_torch_gpu, torch_device from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __A ( a , a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : Optional[Any] =StableDiffusionDiffEditPipeline UpperCamelCase__ : str =TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""height""", """width""", """image"""} | {"""image_latents"""} UpperCamelCase__ : Optional[Any] =TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS - {"""image"""} | {"""image_latents"""} UpperCamelCase__ : Dict =frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess UpperCamelCase__ : Any =frozenset([] ) def __lowercase ( self ): """simple docstring""" torch.manual_seed(0 ) __UpperCamelCase : Dict =UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=lowerCamelCase__ , ) __UpperCamelCase : List[str] =DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=lowerCamelCase__ , set_alpha_to_one=lowerCamelCase__ , ) __UpperCamelCase : Union[str, Any] =DDIMInverseScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=lowerCamelCase__ , set_alpha_to_zero=lowerCamelCase__ , ) torch.manual_seed(0 ) __UpperCamelCase : Optional[int] =AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) __UpperCamelCase : Tuple =CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act='gelu' , projection_dim=512 , ) __UpperCamelCase : Any =CLIPTextModel(lowerCamelCase__ ) __UpperCamelCase : int =CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) __UpperCamelCase : Union[str, Any] ={ 'unet': unet, 'scheduler': scheduler, 'inverse_scheduler': inverse_scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" __UpperCamelCase : int =floats_tensor((1, 16, 16) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) __UpperCamelCase : Any =floats_tensor((1, 2, 4, 16, 16) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : Any =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : Optional[int] =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Dict ={ 'prompt': 'a dog and a newt', 'mask_image': mask, 'image_latents': latents, 'generator': generator, 'num_inference_steps': 2, 'inpaint_strength': 1.0, 'guidance_scale': 6.0, 'output_type': 'numpy', } return inputs def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" __UpperCamelCase : Tuple =floats_tensor((1, 3, 32, 32) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) __UpperCamelCase : int =image.cpu().permute(0 , 2 , 3 , 1 )[0] __UpperCamelCase : Optional[Any] =Image.fromarray(np.uinta(lowerCamelCase__ ) ).convert('RGB' ) if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : List[Any] =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : Any =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Optional[int] ={ 'image': image, 'source_prompt': 'a cat and a frog', 'target_prompt': 'a dog and a newt', 'generator': generator, 'num_inference_steps': 2, 'num_maps_per_mask': 2, 'mask_encode_strength': 1.0, 'guidance_scale': 6.0, 'output_type': 'numpy', } return inputs def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" __UpperCamelCase : str =floats_tensor((1, 3, 32, 32) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) __UpperCamelCase : Any =image.cpu().permute(0 , 2 , 3 , 1 )[0] __UpperCamelCase : int =Image.fromarray(np.uinta(lowerCamelCase__ ) ).convert('RGB' ) if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : Any =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : int =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Optional[int] ={ 'image': image, 'prompt': 'a cat and a frog', 'generator': generator, 'num_inference_steps': 2, 'inpaint_strength': 1.0, 'guidance_scale': 6.0, 'decode_latents': True, 'output_type': 'numpy', } return inputs def __lowercase ( self ): """simple docstring""" if not hasattr(self.pipeline_class , '_optional_components' ): return __UpperCamelCase : Optional[Any] =self.get_dummy_components() __UpperCamelCase : List[str] =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) # set all optional components to None and update pipeline config accordingly for optional_component in pipe._optional_components: setattr(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) pipe.register_modules(**{optional_component: None for optional_component in pipe._optional_components} ) __UpperCamelCase : Union[str, Any] =self.get_dummy_inputs(lowerCamelCase__ ) __UpperCamelCase : List[Any] =pipe(**lowerCamelCase__ )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : Tuple =self.pipeline_class.from_pretrained(lowerCamelCase__ ) pipe_loaded.to(lowerCamelCase__ ) pipe_loaded.set_progress_bar_config(disable=lowerCamelCase__ ) for optional_component in pipe._optional_components: self.assertTrue( getattr(lowerCamelCase__ , lowerCamelCase__ ) is None , f'`{optional_component}` did not stay set to None after loading.' , ) __UpperCamelCase : str =self.get_dummy_inputs(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =pipe_loaded(**lowerCamelCase__ )[0] __UpperCamelCase : Tuple =np.abs(output - output_loaded ).max() self.assertLess(lowerCamelCase__ , 1E-4 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Any ='cpu' __UpperCamelCase : Union[str, Any] =self.get_dummy_components() __UpperCamelCase : Any =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : int =self.get_dummy_mask_inputs(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =pipe.generate_mask(**lowerCamelCase__ ) __UpperCamelCase : int =mask[0, -3:, -3:] self.assertEqual(mask.shape , (1, 16, 16) ) __UpperCamelCase : Tuple =np.array([0] * 9 ) __UpperCamelCase : str =np.abs(mask_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) self.assertEqual(mask[0, -3, -4] , 0 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : int ='cpu' __UpperCamelCase : Union[str, Any] =self.get_dummy_components() __UpperCamelCase : Optional[Any] =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Dict =self.get_dummy_inversion_inputs(lowerCamelCase__ ) __UpperCamelCase : List[Any] =pipe.invert(**lowerCamelCase__ ).images __UpperCamelCase : Optional[Any] =image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) __UpperCamelCase : List[str] =np.array( [0.5_150, 0.5_134, 0.5_043, 0.5_376, 0.4_694, 0.51_050, 0.5_015, 0.4_407, 0.4_799] , ) __UpperCamelCase : int =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) def __lowercase ( self ): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=5E-3 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] ='cpu' __UpperCamelCase : int =self.get_dummy_components() __UpperCamelCase : str ={'beta_start': 0.00_085, 'beta_end': 0.012, 'beta_schedule': 'scaled_linear'} __UpperCamelCase : str =DPMSolverMultistepScheduler(**lowerCamelCase__ ) __UpperCamelCase : Dict =DPMSolverMultistepInverseScheduler(**lowerCamelCase__ ) __UpperCamelCase : Any =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Tuple =self.get_dummy_inversion_inputs(lowerCamelCase__ ) __UpperCamelCase : str =pipe.invert(**lowerCamelCase__ ).images __UpperCamelCase : List[Any] =image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) __UpperCamelCase : List[str] =np.array( [0.5_150, 0.5_134, 0.5_043, 0.5_376, 0.4_694, 0.51_050, 0.5_015, 0.4_407, 0.4_799] , ) __UpperCamelCase : Optional[Any] =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) @require_torch_gpu @slow class __A ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @classmethod def __lowercase ( cls ): """simple docstring""" __UpperCamelCase : Optional[int] =load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/diffedit/fruit.png' ) __UpperCamelCase : Union[str, Any] =raw_image.convert('RGB' ).resize((768, 768) ) __UpperCamelCase : List[Any] =raw_image def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[int] =torch.manual_seed(0 ) __UpperCamelCase : Dict =StableDiffusionDiffEditPipeline.from_pretrained( 'stabilityai/stable-diffusion-2-1' , safety_checker=lowerCamelCase__ , torch_dtype=torch.floataa ) __UpperCamelCase : List[str] =DDIMScheduler.from_config(pipe.scheduler.config ) __UpperCamelCase : List[str] =DDIMInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : List[str] ='a bowl of fruit' __UpperCamelCase : Dict ='a bowl of pears' __UpperCamelCase : Tuple =pipe.generate_mask( image=self.raw_image , source_prompt=lowerCamelCase__ , target_prompt=lowerCamelCase__ , generator=lowerCamelCase__ , ) __UpperCamelCase : int =pipe.invert( prompt=lowerCamelCase__ , image=self.raw_image , inpaint_strength=0.7 , generator=lowerCamelCase__ ).latents __UpperCamelCase : Dict =pipe( prompt=lowerCamelCase__ , mask_image=lowerCamelCase__ , image_latents=lowerCamelCase__ , generator=lowerCamelCase__ , negative_prompt=lowerCamelCase__ , inpaint_strength=0.7 , output_type='numpy' , ).images[0] __UpperCamelCase : str =( np.array( load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/diffedit/pears.png' ).resize((768, 768) ) ) / 255 ) assert np.abs((expected_image - image).max() ) < 5E-1 def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Any =torch.manual_seed(0 ) __UpperCamelCase : List[Any] =StableDiffusionDiffEditPipeline.from_pretrained( 'stabilityai/stable-diffusion-2-1' , safety_checker=lowerCamelCase__ , torch_dtype=torch.floataa ) __UpperCamelCase : Optional[Any] =DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) __UpperCamelCase : Optional[int] =DPMSolverMultistepInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Optional[Any] ='a bowl of fruit' __UpperCamelCase : int ='a bowl of pears' __UpperCamelCase : str =pipe.generate_mask( image=self.raw_image , source_prompt=lowerCamelCase__ , target_prompt=lowerCamelCase__ , generator=lowerCamelCase__ , ) __UpperCamelCase : List[str] =pipe.invert( prompt=lowerCamelCase__ , image=self.raw_image , inpaint_strength=0.7 , generator=lowerCamelCase__ , num_inference_steps=25 , ).latents __UpperCamelCase : List[str] =pipe( prompt=lowerCamelCase__ , mask_image=lowerCamelCase__ , image_latents=lowerCamelCase__ , generator=lowerCamelCase__ , negative_prompt=lowerCamelCase__ , inpaint_strength=0.7 , num_inference_steps=25 , output_type='numpy' , ).images[0] __UpperCamelCase : Tuple =( np.array( load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/diffedit/pears.png' ).resize((768, 768) ) ) / 255 ) assert np.abs((expected_image - image).max() ) < 5E-1
71
0
'''simple docstring''' import warnings from transformers import AutoTokenizer from transformers.utils import is_torch_available from transformers.utils.generic import ExplicitEnum from ...processing_utils import ProcessorMixin if is_torch_available(): import torch class _UpperCAmelCase ( snake_case_ ): """simple docstring""" snake_case = '''char''' snake_case = '''bpe''' snake_case = '''wp''' lowerCamelCase_ = (DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE) class _UpperCAmelCase ( snake_case_ ): """simple docstring""" snake_case = ['''image_processor''', '''char_tokenizer'''] snake_case = '''ViTImageProcessor''' snake_case = '''MgpstrTokenizer''' def __init__( self : List[str] , __UpperCAmelCase : Optional[int]=None , __UpperCAmelCase : Tuple=None , **__UpperCAmelCase : str ): '''simple docstring''' _A = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , __UpperCAmelCase , ) _A = kwargs.pop("feature_extractor" ) _A = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) _A = tokenizer _A = AutoTokenizer.from_pretrained("gpt2" ) _A = AutoTokenizer.from_pretrained("bert-base-uncased" ) super().__init__(__UpperCAmelCase , __UpperCAmelCase ) def __call__( self : int , __UpperCAmelCase : Optional[int]=None , __UpperCAmelCase : int=None , __UpperCAmelCase : str=None , **__UpperCAmelCase : List[Any] ): '''simple docstring''' if images is None and text is None: raise ValueError("You need to specify either an `images` or `text` input to process." ) if images is not None: _A = self.image_processor(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase ) if text is not None: _A = self.char_tokenizer(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase ) if text is None: return inputs elif images is None: return encodings else: _A = encodings["input_ids"] return inputs def lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : List[Any] ): '''simple docstring''' _A , _A , _A = sequences _A = char_preds.size(0 ) _A , _A = self._decode_helper(__UpperCAmelCase , "char" ) _A , _A = self._decode_helper(__UpperCAmelCase , "bpe" ) _A , _A = self._decode_helper(__UpperCAmelCase , "wp" ) _A = [] _A = [] for i in range(__UpperCAmelCase ): _A = [char_scores[i], bpe_scores[i], wp_scores[i]] _A = [char_strs[i], bpe_strs[i], wp_strs[i]] _A = scores.index(max(__UpperCAmelCase ) ) final_strs.append(strs[max_score_index] ) final_scores.append(scores[max_score_index] ) _A = {} _A = final_strs _A = final_scores _A = char_strs _A = bpe_strs _A = wp_strs return out def lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Tuple , __UpperCAmelCase : List[str] ): '''simple docstring''' if format == DecodeType.CHARACTER: _A = self.char_decode _A = 1 _A = "[s]" elif format == DecodeType.BPE: _A = self.bpe_decode _A = 2 _A = "#" elif format == DecodeType.WORDPIECE: _A = self.wp_decode _A = 102 _A = "[SEP]" else: raise ValueError(f'''Format {format} is not supported.''' ) _A , _A = [], [] _A = pred_logits.size(0 ) _A = pred_logits.size(1 ) _A , _A = pred_logits.topk(1 , dim=-1 , largest=__UpperCAmelCase , sorted=__UpperCAmelCase ) _A = preds_index.view(-1 , __UpperCAmelCase )[:, 1:] _A = decoder(__UpperCAmelCase ) _A , _A = torch.nn.functional.softmax(__UpperCAmelCase , dim=2 ).max(dim=2 ) _A = preds_max_prob[:, 1:] for index in range(__UpperCAmelCase ): _A = preds_str[index].find(__UpperCAmelCase ) _A = preds_str[index][:pred_eos] _A = preds_index[index].cpu().tolist() _A = pred_index.index(__UpperCAmelCase ) if eos_token in pred_index else -1 _A = preds_max_prob[index][: pred_eos_index + 1] _A = pred_max_prob.cumprod(dim=0 )[-1] if pred_max_prob.nelement() != 0 else 0.0 dec_strs.append(__UpperCAmelCase ) conf_scores.append(__UpperCAmelCase ) return dec_strs, conf_scores def lowerCAmelCase ( self : Dict , __UpperCAmelCase : List[Any] ): '''simple docstring''' _A = [seq.replace(" " , "" ) for seq in self.char_tokenizer.batch_decode(__UpperCAmelCase )] return decode_strs def lowerCAmelCase ( self : Dict , __UpperCAmelCase : List[str] ): '''simple docstring''' return self.bpe_tokenizer.batch_decode(__UpperCAmelCase ) def lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Union[str, Any] ): '''simple docstring''' _A = [seq.replace(" " , "" ) for seq in self.wp_tokenizer.batch_decode(__UpperCAmelCase )] return decode_strs
79
import random from .binary_exp_mod import bin_exp_mod def A ( a_ ,a_=1_000 ) -> Optional[Any]: if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd __UpperCamelCase : List[Any] =n - 1 __UpperCamelCase : Dict =0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) __UpperCamelCase : Optional[Any] =0 while count < prec: __UpperCamelCase : Dict =random.randint(2 ,n - 1 ) __UpperCamelCase : Optional[Any] =bin_exp_mod(a_ ,a_ ,a_ ) if b != 1: __UpperCamelCase : List[str] =True for _ in range(a_ ): if b == n - 1: __UpperCamelCase : Tuple =False break __UpperCamelCase : Dict =b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": A_ :str = abs(int(input('''Enter bound : ''').strip())) print('''Here\'s the list of primes:''') print(''', '''.join(str(i) for i in range(n + 1) if is_prime_big(i)))
71
0
'''simple docstring''' # limitations under the License. from typing import Optional, Tuple, Union import torch from diffusers import DiffusionPipeline, ImagePipelineOutput class lowercase_ ( a__ ): def __init__( self , a , a ): super().__init__() self.register_modules(unet=a , scheduler=a ) @torch.no_grad() def __call__( self , a = 1 , a = None , a = 50 , a = "pil" , a = True , **a , ): UpperCamelCase__ = torch.randn( (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size) , generator=a , ) UpperCamelCase__ = image.to(self.device ) # set step values self.scheduler.set_timesteps(a ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output UpperCamelCase__ = self.unet(a , a ).sample # 2. predict previous mean of image x_t-1 and add variance depending on eta # eta corresponds to η in paper and should be between [0, 1] # do x_t -> x_t-1 UpperCamelCase__ = self.scheduler.step(a , a , a ).prev_sample UpperCamelCase__ = (image / 2 + 0.5).clamp(0 , 1 ) UpperCamelCase__ = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCamelCase__ = self.numpy_to_pil(a ) if not return_dict: return (image,), "This is a local test" return ImagePipelineOutput(images=a ), "This is a local test"
80
from torch import nn class __A ( nn.Module ): """simple docstring""" def __init__( self , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" super().__init__() __UpperCamelCase : Dict =class_size __UpperCamelCase : Any =embed_size # self.mlp1 = nn.Linear(embed_size, embed_size) # self.mlp2 = (nn.Linear(embed_size, class_size)) __UpperCamelCase : Any =nn.Linear(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : List[Any] =self.mlp(lowerCamelCase__ ) return logits
71
0
"""simple docstring""" from ..utils import DummyObject, requires_backends class __A ( metaclass=_SCREAMING_SNAKE_CASE ): """simple docstring""" __lowerCAmelCase = ["torch", "torchsde"] def __init__( self , *__A , **__A ) -> Dict: requires_backends(self , ['''torch''', '''torchsde'''] ) @classmethod def SCREAMING_SNAKE_CASE ( cls , *__A , **__A ) -> int: requires_backends(cls , ['''torch''', '''torchsde'''] ) @classmethod def SCREAMING_SNAKE_CASE ( cls , *__A , **__A ) -> List[Any]: requires_backends(cls , ['''torch''', '''torchsde'''] )
81
def A ( a_ ,a_ ,a_ ) -> int: def update_area_of_max_square(a_ ,a_ ) -> int: # BASE CASE if row >= rows or col >= cols: return 0 __UpperCamelCase : Optional[int] =update_area_of_max_square(a_ ,col + 1 ) __UpperCamelCase : List[str] =update_area_of_max_square(row + 1 ,col + 1 ) __UpperCamelCase : List[Any] =update_area_of_max_square(row + 1 ,a_ ) if mat[row][col]: __UpperCamelCase : Optional[Any] =1 + min([right, diagonal, down] ) __UpperCamelCase : Dict =max(largest_square_area[0] ,a_ ) return sub_problem_sol else: return 0 __UpperCamelCase : Union[str, Any] =[0] update_area_of_max_square(0 ,0 ) return largest_square_area[0] def A ( a_ ,a_ ,a_ ) -> int: def update_area_of_max_square_using_dp_array( a_ ,a_ ,a_ ) -> int: if row >= rows or col >= cols: return 0 if dp_array[row][col] != -1: return dp_array[row][col] __UpperCamelCase : Tuple =update_area_of_max_square_using_dp_array(a_ ,col + 1 ,a_ ) __UpperCamelCase : Optional[int] =update_area_of_max_square_using_dp_array(row + 1 ,col + 1 ,a_ ) __UpperCamelCase : Any =update_area_of_max_square_using_dp_array(row + 1 ,a_ ,a_ ) if mat[row][col]: __UpperCamelCase : Optional[Any] =1 + min([right, diagonal, down] ) __UpperCamelCase : str =max(largest_square_area[0] ,a_ ) __UpperCamelCase : Any =sub_problem_sol return sub_problem_sol else: return 0 __UpperCamelCase : Tuple =[0] __UpperCamelCase : List[Any] =[[-1] * cols for _ in range(a_ )] update_area_of_max_square_using_dp_array(0 ,0 ,a_ ) return largest_square_area[0] def A ( a_ ,a_ ,a_ ) -> int: __UpperCamelCase : Dict =[[0] * (cols + 1) for _ in range(rows + 1 )] __UpperCamelCase : int =0 for row in range(rows - 1 ,-1 ,-1 ): for col in range(cols - 1 ,-1 ,-1 ): __UpperCamelCase : Optional[Any] =dp_array[row][col + 1] __UpperCamelCase : int =dp_array[row + 1][col + 1] __UpperCamelCase : Tuple =dp_array[row + 1][col] if mat[row][col] == 1: __UpperCamelCase : Tuple =1 + min(a_ ,a_ ,a_ ) __UpperCamelCase : Any =max(dp_array[row][col] ,a_ ) else: __UpperCamelCase : Dict =0 return largest_square_area def A ( a_ ,a_ ,a_ ) -> int: __UpperCamelCase : Any =[0] * (cols + 1) __UpperCamelCase : List[Any] =[0] * (cols + 1) __UpperCamelCase : Tuple =0 for row in range(rows - 1 ,-1 ,-1 ): for col in range(cols - 1 ,-1 ,-1 ): __UpperCamelCase : Any =current_row[col + 1] __UpperCamelCase : Optional[Any] =next_row[col + 1] __UpperCamelCase : Union[str, Any] =next_row[col] if mat[row][col] == 1: __UpperCamelCase : Any =1 + min(a_ ,a_ ,a_ ) __UpperCamelCase : Optional[int] =max(current_row[col] ,a_ ) else: __UpperCamelCase : List[str] =0 __UpperCamelCase : Optional[Any] =current_row return largest_square_area if __name__ == "__main__": import doctest doctest.testmod() print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))
71
0
import inspect import unittest import numpy as np from transformers import BeitConfig from transformers.testing_utils import require_flax, require_vision, slow from transformers.utils import cached_property, is_flax_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor if is_flax_available(): import jax from transformers import FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel if is_vision_available(): from PIL import Image from transformers import BeitImageProcessor class __lowerCAmelCase ( unittest.TestCase ): def __init__( self , _snake_case , _snake_case=100 , _snake_case=13 , _snake_case=30 , _snake_case=2 , _snake_case=3 , _snake_case=True , _snake_case=True , _snake_case=32 , _snake_case=5 , _snake_case=4 , _snake_case=37 , _snake_case="gelu" , _snake_case=0.1 , _snake_case=0.1 , _snake_case=10 , _snake_case=0.02 , _snake_case=3 , ): """simple docstring""" _lowerCAmelCase = parent _lowerCAmelCase = vocab_size _lowerCAmelCase = batch_size _lowerCAmelCase = image_size _lowerCAmelCase = patch_size _lowerCAmelCase = num_channels _lowerCAmelCase = is_training _lowerCAmelCase = use_labels _lowerCAmelCase = hidden_size _lowerCAmelCase = num_hidden_layers _lowerCAmelCase = num_attention_heads _lowerCAmelCase = intermediate_size _lowerCAmelCase = hidden_act _lowerCAmelCase = hidden_dropout_prob _lowerCAmelCase = attention_probs_dropout_prob _lowerCAmelCase = type_sequence_label_size _lowerCAmelCase = initializer_range # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) _lowerCAmelCase = (image_size // patch_size) ** 2 _lowerCAmelCase = num_patches + 1 def snake_case ( self ): """simple docstring""" _lowerCAmelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _lowerCAmelCase = None if self.use_labels: _lowerCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _lowerCAmelCase = BeitConfig( vocab_size=self.vocab_size , image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_snake_case , initializer_range=self.initializer_range , ) return config, pixel_values, labels def snake_case ( self , _snake_case , _snake_case , _snake_case ): """simple docstring""" _lowerCAmelCase = FlaxBeitModel(config=_snake_case ) _lowerCAmelCase = model(_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def snake_case ( self , _snake_case , _snake_case , _snake_case ): """simple docstring""" _lowerCAmelCase = FlaxBeitForMaskedImageModeling(config=_snake_case ) _lowerCAmelCase = model(_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def snake_case ( self , _snake_case , _snake_case , _snake_case ): """simple docstring""" _lowerCAmelCase = self.type_sequence_label_size _lowerCAmelCase = FlaxBeitForImageClassification(config=_snake_case ) _lowerCAmelCase = model(_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images _lowerCAmelCase = 1 _lowerCAmelCase = FlaxBeitForImageClassification(_snake_case ) _lowerCAmelCase = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) _lowerCAmelCase = model(_snake_case ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = self.prepare_config_and_inputs() ( ( _lowerCAmelCase ) , ( _lowerCAmelCase ) , ( _lowerCAmelCase ) , ) = config_and_inputs _lowerCAmelCase = {"""pixel_values""": pixel_values} return config, inputs_dict @require_flax class __lowerCAmelCase ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase = ( (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else () ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = FlaxBeitModelTester(self ) _lowerCAmelCase = ConfigTester(self , config_class=_snake_case , has_text_modality=_snake_case , hidden_size=37 ) def snake_case ( self ): """simple docstring""" self.config_tester.run_common_tests() def snake_case ( self ): """simple docstring""" _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCAmelCase = model_class(_snake_case ) _lowerCAmelCase = inspect.signature(model.__call__ ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _lowerCAmelCase = [*signature.parameters.keys()] _lowerCAmelCase = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _snake_case ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): _lowerCAmelCase = self._prepare_for_class(_snake_case , _snake_case ) _lowerCAmelCase = model_class(_snake_case ) @jax.jit def model_jitted(_snake_case , **_snake_case ): return model(pixel_values=_snake_case , **_snake_case ) with self.subTest("""JIT Enabled""" ): _lowerCAmelCase = model_jitted(**_snake_case ).to_tuple() with self.subTest("""JIT Disabled""" ): with jax.disable_jit(): _lowerCAmelCase = model_jitted(**_snake_case ).to_tuple() self.assertEqual(len(_snake_case ) , len(_snake_case ) ) for jitted_output, output in zip(_snake_case , _snake_case ): self.assertEqual(jitted_output.shape , output.shape ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_snake_case ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_snake_case ) @slow def snake_case ( self ): """simple docstring""" for model_class_name in self.all_model_classes: _lowerCAmelCase = model_class_name.from_pretrained("""microsoft/beit-base-patch16-224""" ) _lowerCAmelCase = model(np.ones((1, 3, 224, 224) ) ) self.assertIsNotNone(_snake_case ) def _UpperCAmelCase ( ): """simple docstring""" _lowerCAmelCase = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_vision @require_flax class __lowerCAmelCase ( unittest.TestCase ): @cached_property def snake_case ( self ): """simple docstring""" return BeitImageProcessor.from_pretrained("""microsoft/beit-base-patch16-224""" ) if is_vision_available() else None @slow def snake_case ( self ): """simple docstring""" _lowerCAmelCase = FlaxBeitForMaskedImageModeling.from_pretrained("""microsoft/beit-base-patch16-224-pt22k""" ) _lowerCAmelCase = self.default_image_processor _lowerCAmelCase = prepare_img() _lowerCAmelCase = image_processor(images=_snake_case , return_tensors="""np""" ).pixel_values # prepare bool_masked_pos _lowerCAmelCase = np.ones((1, 196) , dtype=_snake_case ) # forward pass _lowerCAmelCase = model(pixel_values=_snake_case , bool_masked_pos=_snake_case ) _lowerCAmelCase = outputs.logits # verify the logits _lowerCAmelCase = (1, 196, 8192) self.assertEqual(logits.shape , _snake_case ) _lowerCAmelCase = np.array( [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]] ) self.assertTrue(np.allclose(logits[bool_masked_pos][:3, :3] , _snake_case , atol=1e-2 ) ) @slow def snake_case ( self ): """simple docstring""" _lowerCAmelCase = FlaxBeitForImageClassification.from_pretrained("""microsoft/beit-base-patch16-224""" ) _lowerCAmelCase = self.default_image_processor _lowerCAmelCase = prepare_img() _lowerCAmelCase = image_processor(images=_snake_case , return_tensors="""np""" ) # forward pass _lowerCAmelCase = model(**_snake_case ) _lowerCAmelCase = outputs.logits # verify the logits _lowerCAmelCase = (1, 1000) self.assertEqual(logits.shape , _snake_case ) _lowerCAmelCase = np.array([-1.2385, -1.0987, -1.0108] ) self.assertTrue(np.allclose(logits[0, :3] , _snake_case , atol=1e-4 ) ) _lowerCAmelCase = 281 self.assertEqual(logits.argmax(-1 ).item() , _snake_case ) @slow def snake_case ( self ): """simple docstring""" _lowerCAmelCase = FlaxBeitForImageClassification.from_pretrained("""microsoft/beit-large-patch16-224-pt22k-ft22k""" ) _lowerCAmelCase = self.default_image_processor _lowerCAmelCase = prepare_img() _lowerCAmelCase = image_processor(images=_snake_case , return_tensors="""np""" ) # forward pass _lowerCAmelCase = model(**_snake_case ) _lowerCAmelCase = outputs.logits # verify the logits _lowerCAmelCase = (1, 21841) self.assertEqual(logits.shape , _snake_case ) _lowerCAmelCase = np.array([1.6881, -0.2787, 0.5901] ) self.assertTrue(np.allclose(logits[0, :3] , _snake_case , atol=1e-4 ) ) _lowerCAmelCase = 2396 self.assertEqual(logits.argmax(-1 ).item() , _snake_case )
82
def A ( a_ ) -> int: __UpperCamelCase : Any =len(a_ ) while cur > 1: # Find the maximum number in arr __UpperCamelCase : Any =arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi __UpperCamelCase : Any =arr[mi::-1] + arr[mi + 1 : len(a_ )] # Reverse whole list __UpperCamelCase : str =arr[cur - 1 :: -1] + arr[cur : len(a_ )] cur -= 1 return arr if __name__ == "__main__": A_ :Dict = input('''Enter numbers separated by a comma:\n''').strip() A_ :Any = [int(item) for item in user_input.split(''',''')] print(pancake_sort(unsorted))
71
0
'''simple docstring''' import numpy as np class lowercase__ : def __init__( self : Any ): '''simple docstring''' _UpperCamelCase : Dict = (0, 0) _UpperCamelCase : str = None _UpperCamelCase : Union[str, Any] = 0 _UpperCamelCase : List[Any] = 0 _UpperCamelCase : str = 0 def __eq__( self : Dict ,lowerCamelCase__ : Dict ): '''simple docstring''' return self.position == cell.position def UpperCamelCase_ ( self : Any ): '''simple docstring''' print(self.position ) class lowercase__ : def __init__( self : List[Any] ,lowerCamelCase__ : Tuple=(5, 5) ): '''simple docstring''' _UpperCamelCase : Optional[Any] = np.zeros(lowerCamelCase__ ) _UpperCamelCase : Tuple = world_size[0] _UpperCamelCase : Dict = world_size[1] def UpperCamelCase_ ( self : List[str] ): '''simple docstring''' print(self.w ) def UpperCamelCase_ ( self : List[str] ,lowerCamelCase__ : Optional[int] ): '''simple docstring''' _UpperCamelCase : Optional[Any] = [ (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1), ] _UpperCamelCase : Any = cell.position[0] _UpperCamelCase : Any = cell.position[1] _UpperCamelCase : Tuple = [] for n in neughbour_cord: _UpperCamelCase : str = current_x + n[0] _UpperCamelCase : Optional[int] = current_y + n[1] if 0 <= x < self.world_x_limit and 0 <= y < self.world_y_limit: _UpperCamelCase : List[str] = Cell() _UpperCamelCase : Dict = (x, y) _UpperCamelCase : Union[str, Any] = cell neighbours.append(lowerCamelCase__ ) return neighbours def A__ ( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ): _UpperCamelCase : Dict = [] _UpperCamelCase : List[str] = [] _open.append(UpperCAmelCase_ ) while _open: _UpperCamelCase : int = np.argmin([n.f for n in _open] ) _UpperCamelCase : int = _open[min_f] _closed.append(_open.pop(UpperCAmelCase_ ) ) if current == goal: break for n in world.get_neigbours(UpperCAmelCase_ ): for c in _closed: if c == n: continue _UpperCamelCase : str = current.g + 1 _UpperCamelCase , _UpperCamelCase : str = n.position _UpperCamelCase , _UpperCamelCase : Optional[Any] = goal.position _UpperCamelCase : Optional[Any] = (ya - ya) ** 2 + (xa - xa) ** 2 _UpperCamelCase : List[Any] = n.h + n.g for c in _open: if c == n and c.f < n.f: continue _open.append(UpperCAmelCase_ ) _UpperCamelCase : Optional[Any] = [] while current.parent is not None: path.append(current.position ) _UpperCamelCase : Union[str, Any] = current.parent path.append(current.position ) return path[::-1] if __name__ == "__main__": snake_case_ : str = Gridworld() # Start position and goal snake_case_ : Optional[int] = Cell() snake_case_ : Dict = (0, 0) snake_case_ : str = Cell() snake_case_ : Optional[Any] = (4, 4) print(F"""path from {start.position} to {goal.position}""") snake_case_ : Union[str, Any] = astar(world, start, goal) # Just for visual reasons. for i in s: snake_case_ : Optional[int] = 1 print(world.w)
83
import random def A ( a_ ,a_ ,a_ = False ) -> dict: __UpperCamelCase : dict ={i: [] for i in range(a_ )} # if probability is greater or equal than 1, then generate a complete graph if probability >= 1: return complete_graph(a_ ) # if probability is lower or equal than 0, then return a graph without edges if probability <= 0: return graph # for each couple of nodes, add an edge from u to v # if the number randomly generated is greater than probability probability for i in range(a_ ): for j in range(i + 1 ,a_ ): if random.random() < probability: graph[i].append(a_ ) if not directed: # if the graph is undirected, add an edge in from j to i, either graph[j].append(a_ ) return graph def A ( a_ ) -> dict: return { i: [j for j in range(a_ ) if i != j] for i in range(a_ ) } if __name__ == "__main__": import doctest doctest.testmod()
71
0
"""simple docstring""" def _snake_case ( lowercase__ : list[int] ) -> list[list[int]]: '''simple docstring''' lowerCAmelCase_ :Optional[Any] = [] if len(lowercase__ ) == 1: return [nums.copy()] for _ in range(len(lowercase__ ) ): lowerCAmelCase_ :Optional[Any] = nums.pop(0 ) lowerCAmelCase_ :str = permute(lowercase__ ) for perm in permutations: perm.append(lowercase__ ) result.extend(lowercase__ ) nums.append(lowercase__ ) return result def _snake_case ( lowercase__ : Tuple ) -> List[str]: '''simple docstring''' def backtrack(lowercase__ : str ): if start == len(lowercase__ ) - 1: output.append(nums[:] ) else: for i in range(lowercase__ , len(lowercase__ ) ): lowerCAmelCase_ , lowerCAmelCase_ :str = nums[i], nums[start] backtrack(start + 1 ) lowerCAmelCase_ , lowerCAmelCase_ :str = nums[i], nums[start] # backtrack lowerCAmelCase_ :int = [] backtrack(0 ) return output if __name__ == "__main__": import doctest # use res to print the data in permute2 function __UpperCAmelCase = permutea([1, 2, 3]) print(res) doctest.testmod()
84
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 __A ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: __UpperCamelCase : List[str] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModel.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[Any] =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"]: __UpperCamelCase : Any =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForPreTraining.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =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]: __UpperCamelCase : Tuple =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModelForCausalLM.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Tuple =TFAutoModelForCausalLM.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[int] =AutoModelForCausalLM.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Union[str, Any] =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]: __UpperCamelCase : int =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Dict =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[Any] =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]: __UpperCamelCase : List[Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Tuple =TFAutoModelForMaskedLM.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =AutoModelForMaskedLM.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Optional[Any] =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]: __UpperCamelCase : Any =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[Any] =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : List[str] =TFAutoModelForSeqaSeqLM.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Dict =AutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : List[Any] =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"]: __UpperCamelCase : str =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =TFAutoModelForSequenceClassification.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =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"]: __UpperCamelCase : List[Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Tuple =TFAutoModelForQuestionAnswering.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =AutoModelForQuestionAnswering.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : int =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) __UpperCamelCase : str =AutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Union[str, Any] =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) __UpperCamelCase : int =AutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 )
71
0
'''simple docstring''' import unittest from transformers import AutoTokenizer, FalconConfig, 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 ( FalconForCausalLM, FalconForQuestionAnswering, FalconForSequenceClassification, FalconForTokenClassification, FalconModel, ) class _snake_case : def __init__( self , a__ , a__=3 , a__=7 , a__=True , a__=True , a__=False , a__=True , a__=99 , a__=32 , a__=5 , a__=4 , a__=37 , a__="gelu" , a__=0.1 , a__=0.1 , a__=512 , a__=16 , a__=2 , a__=0.0_2 , a__=3 , a__=4 , a__=None , ) -> List[str]: '''simple docstring''' snake_case_ = parent snake_case_ = batch_size snake_case_ = seq_length snake_case_ = is_training snake_case_ = use_input_mask snake_case_ = use_token_type_ids snake_case_ = use_labels snake_case_ = vocab_size snake_case_ = hidden_size snake_case_ = num_hidden_layers snake_case_ = num_attention_heads snake_case_ = intermediate_size snake_case_ = hidden_act snake_case_ = hidden_dropout_prob snake_case_ = attention_probs_dropout_prob snake_case_ = max_position_embeddings snake_case_ = type_vocab_size snake_case_ = type_sequence_label_size snake_case_ = initializer_range snake_case_ = num_labels snake_case_ = num_choices snake_case_ = scope def lowerCAmelCase__ ( self ) -> int: '''simple docstring''' snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case_ = None if self.use_input_mask: snake_case_ = random_attention_mask([self.batch_size, self.seq_length] ) snake_case_ = None snake_case_ = None snake_case_ = None snake_case_ = None if self.use_labels: snake_case_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) snake_case_ = ids_tensor([self.batch_size] , self.num_choices ) snake_case_ = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowerCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' return FalconConfig( 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=a__ , initializer_range=self.initializer_range , pad_token_id=1 , new_decoder_architecture=a__ , ) def lowerCAmelCase__ ( self , a__ , a__ , a__ , a__ , a__ , a__ , a__ ) -> Optional[Any]: '''simple docstring''' snake_case_ = FalconModel(config=a__ ) model.to(a__ ) model.eval() snake_case_ = model(a__ , attention_mask=a__ ) snake_case_ = model(a__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCAmelCase__ ( self , a__ , a__ , a__ , a__ , a__ , a__ , a__ , a__ , a__ , ) -> Optional[int]: '''simple docstring''' snake_case_ = True snake_case_ = FalconModel(a__ ) model.to(a__ ) model.eval() snake_case_ = model( a__ , attention_mask=a__ , encoder_hidden_states=a__ , encoder_attention_mask=a__ , ) snake_case_ = model( a__ , attention_mask=a__ , encoder_hidden_states=a__ , ) snake_case_ = model(a__ , attention_mask=a__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCAmelCase__ ( self , a__ , a__ , a__ , a__ , a__ , a__ , a__ , a__ , a__ , ) -> str: '''simple docstring''' snake_case_ = FalconForCausalLM(config=a__ ) model.to(a__ ) model.eval() snake_case_ = model(a__ , attention_mask=a__ , labels=a__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCAmelCase__ ( self , a__ , a__ , a__ , a__ , a__ , a__ , a__ , a__ , a__ , ) -> Dict: '''simple docstring''' snake_case_ = True snake_case_ = True snake_case_ = FalconForCausalLM(config=a__ ) model.to(a__ ) model.eval() # first forward pass snake_case_ = model( a__ , attention_mask=a__ , encoder_hidden_states=a__ , encoder_attention_mask=a__ , use_cache=a__ , ) snake_case_ = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids snake_case_ = ids_tensor((self.batch_size, 3) , config.vocab_size ) snake_case_ = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and snake_case_ = torch.cat([input_ids, next_tokens] , dim=-1 ) snake_case_ = torch.cat([input_mask, next_mask] , dim=-1 ) snake_case_ = model( a__ , attention_mask=a__ , encoder_hidden_states=a__ , encoder_attention_mask=a__ , output_hidden_states=a__ , )["hidden_states"][0] snake_case_ = model( a__ , attention_mask=a__ , encoder_hidden_states=a__ , encoder_attention_mask=a__ , past_key_values=a__ , output_hidden_states=a__ , )["hidden_states"][0] # select random slice snake_case_ = ids_tensor((1,) , output_from_past.shape[-1] ).item() snake_case_ = output_from_no_past[:, -3:, random_slice_idx].detach() snake_case_ = 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(a__ , a__ , atol=1e-3 ) ) def lowerCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' snake_case_ = self.prepare_config_and_inputs() ( ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ) = config_and_inputs snake_case_ = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class _snake_case ( lowercase_ , lowercase_ , lowercase_ , unittest.TestCase ): lowerCAmelCase_ : Optional[int] = ( ( FalconModel, FalconForCausalLM, FalconForSequenceClassification, FalconForTokenClassification, FalconForQuestionAnswering, ) if is_torch_available() else () ) lowerCAmelCase_ : List[Any] = (FalconForCausalLM,) if is_torch_available() else () lowerCAmelCase_ : Union[str, Any] = ( { "feature-extraction": FalconModel, "text-classification": FalconForSequenceClassification, "text-generation": FalconForCausalLM, "question-answering": FalconForQuestionAnswering, "token-classification": FalconForTokenClassification, "zero-shot": FalconForSequenceClassification, } if is_torch_available() else {} ) lowerCAmelCase_ : Optional[Any] = False lowerCAmelCase_ : List[Any] = False def lowerCAmelCase__ ( self ) -> int: '''simple docstring''' snake_case_ = FalconModelTester(self ) snake_case_ = ConfigTester(self , config_class=a__ , hidden_size=37 ) def lowerCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' self.config_tester.run_common_tests() def lowerCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a__ ) def lowerCAmelCase__ ( self ) -> List[str]: '''simple docstring''' snake_case_ , *snake_case_ = self.model_tester.prepare_config_and_inputs() for alibi in [True, False]: snake_case_ = alibi self.model_tester.create_and_check_model(a__ , *a__ ) def lowerCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' snake_case_ , snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ = 3 snake_case_ = input_dict["input_ids"] snake_case_ = input_ids.ne(1 ).to(a__ ) snake_case_ = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) snake_case_ = FalconForSequenceClassification(a__ ) model.to(a__ ) model.eval() snake_case_ = model(a__ , attention_mask=a__ , labels=a__ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def lowerCAmelCase__ ( self ) -> List[str]: '''simple docstring''' snake_case_ , snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ = 3 snake_case_ = "single_label_classification" snake_case_ = input_dict["input_ids"] snake_case_ = input_ids.ne(1 ).to(a__ ) snake_case_ = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) snake_case_ = FalconForSequenceClassification(a__ ) model.to(a__ ) model.eval() snake_case_ = model(a__ , attention_mask=a__ , labels=a__ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def lowerCAmelCase__ ( self ) -> str: '''simple docstring''' snake_case_ , snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ = input_dict["input_ids"] snake_case_ = FalconForCausalLM(a__ ) model.to(a__ ) model.eval() snake_case_ = model(a__ , use_cache=a__ ) snake_case_ = input_ids.shape[0] snake_case_ = model._convert_to_rw_cache(result.past_key_values ) snake_case_ = model._convert_cache_to_standard_format(a__ , a__ ) for layer in range(len(a__ ) ): for tensor_idx in range(2 ): self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3 ) self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4 ) self.assertTrue( torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx] ) ) def lowerCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' snake_case_ , snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ = 3 snake_case_ = "multi_label_classification" snake_case_ = input_dict["input_ids"] snake_case_ = input_ids.ne(1 ).to(a__ ) snake_case_ = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) snake_case_ = FalconForSequenceClassification(a__ ) model.to(a__ ) model.eval() snake_case_ = model(a__ , attention_mask=a__ , labels=a__ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def lowerCAmelCase__ ( self ) -> str: '''simple docstring''' for model_class in self.all_generative_model_classes: snake_case_ , snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() # If it doesn't support cache, pass the test if not hasattr(a__ , "use_cache" ): return snake_case_ = model_class(a__ ).to(a__ ) if "use_cache" not in inputs: snake_case_ = True snake_case_ = model(**a__ ) # If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format) if "past_key_values" not in outputs: return snake_case_ = ( getattr(a__ , "decoder_layers" , a__ ) or getattr(a__ , "num_decoder_layers" , a__ ) or config.num_hidden_layers ) snake_case_ = getattr(a__ , "num_kv_heads" , config.num_attention_heads ) snake_case_ = getattr(a__ , "d_model" , config.hidden_size ) snake_case_ = embed_dim // num_attention_heads snake_case_ = outputs["past_key_values"] self.assertEqual(len(a__ ) , a__ ) snake_case_ , snake_case_ = inputs["input_ids"].shape for i in range(a__ ): if config.new_decoder_architecture: snake_case_ = config.num_attention_heads elif config.multi_query: snake_case_ = 1 self.assertEqual(len(past_kv[0] ) , 2 ) # K V for the decoder = 2 self.assertEqual( past_kv[i][0].shape , (batch_size, num_attention_heads, seq_length, per_head_embed_dim) ) self.assertEqual( past_kv[i][1].shape , (batch_size, num_attention_heads, seq_length, per_head_embed_dim) ) @require_torch class _snake_case ( unittest.TestCase ): @slow def lowerCAmelCase__ ( self ) -> Any: '''simple docstring''' snake_case_ = AutoTokenizer.from_pretrained("Rocketknight1/falcon-rw-1b" ) snake_case_ = FalconForCausalLM.from_pretrained("Rocketknight1/falcon-rw-1b" ) model.eval() model.to(a__ ) snake_case_ = tokenizer("My favorite food is" , return_tensors="pt" ).to(a__ ) snake_case_ = ( "My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday." ) snake_case_ = model.generate(**a__ , do_sample=a__ , max_new_tokens=19 ) snake_case_ = tokenizer.batch_decode(a__ )[0] self.assertEqual(a__ , a__ ) @slow def lowerCAmelCase__ ( self ) -> Any: '''simple docstring''' for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]: snake_case_ = AutoTokenizer.from_pretrained(a__ ) snake_case_ = FalconForCausalLM.from_pretrained(a__ ) model.eval() model.to(a__ ) snake_case_ = tokenizer("My favorite food is" , return_tensors="pt" ).to(a__ ) # We just test that these run without errors - the models are randomly initialized # and so the actual text outputs will be garbage model.generate(**a__ , do_sample=a__ , max_new_tokens=4 ) model.generate(**a__ , do_sample=a__ , max_new_tokens=4 ) model.generate(**a__ , num_beams=2 , max_new_tokens=4 ) @slow def lowerCAmelCase__ ( self ) -> int: '''simple docstring''' with torch.no_grad(): for repo in [ "Rocketknight1/falcon-rw-1b", "Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b", ]: snake_case_ = AutoTokenizer.from_pretrained(a__ ) snake_case_ = FalconForCausalLM.from_pretrained(a__ ) model.eval() model.to(device=a__ ) snake_case_ = tokenizer("My favorite food is" , return_tensors="pt" ).to(a__ ) # Test results are the same with and without cache snake_case_ = model.generate(**a__ , do_sample=a__ , max_new_tokens=20 , use_cache=a__ ) snake_case_ = model.generate(**a__ , do_sample=a__ , max_new_tokens=20 , use_cache=a__ ) self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0 )
85
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A_ :Tuple = { '''configuration_x_clip''': [ '''XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XCLIPConfig''', '''XCLIPTextConfig''', '''XCLIPVisionConfig''', ], '''processing_x_clip''': ['''XCLIPProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ :Union[str, Any] = [ '''XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XCLIPModel''', '''XCLIPPreTrainedModel''', '''XCLIPTextModel''', '''XCLIPVisionModel''', ] if TYPE_CHECKING: from .configuration_x_clip import ( XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, XCLIPConfig, XCLIPTextConfig, XCLIPVisionConfig, ) from .processing_x_clip import XCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_x_clip import ( XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, XCLIPModel, XCLIPPreTrainedModel, XCLIPTextModel, XCLIPVisionModel, ) else: import sys A_ :Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
71
0
"""simple docstring""" import gc import unittest import numpy as np import torch from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS, CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class A__ ( _lowerCamelCase , unittest.TestCase): A_ : Union[str, Any] = DiTPipeline A_ : Union[str, Any] = CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS A_ : List[Any] = PipelineTesterMixin.required_optional_params - { 'latents', 'num_images_per_prompt', 'callback', 'callback_steps', } A_ : Optional[Any] = CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS A_ : Tuple = False def __lowerCamelCase ( self ): torch.manual_seed(0 ) __lowerCAmelCase : List[str] = TransformeraDModel( sample_size=16 , num_layers=2 , patch_size=4 , attention_head_dim=8 , num_attention_heads=2 , in_channels=4 , out_channels=8 , attention_bias=_SCREAMING_SNAKE_CASE , activation_fn='gelu-approximate' , num_embeds_ada_norm=10_00 , norm_type='ada_norm_zero' , norm_elementwise_affine=_SCREAMING_SNAKE_CASE , ) __lowerCAmelCase : str = AutoencoderKL() __lowerCAmelCase : Union[str, Any] = DDIMScheduler() __lowerCAmelCase : Dict = {'transformer': transformer.eval(), 'vae': vae.eval(), 'scheduler': scheduler} return components def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=0 ): if str(_SCREAMING_SNAKE_CASE ).startswith('mps' ): __lowerCAmelCase : List[str] = torch.manual_seed(_SCREAMING_SNAKE_CASE ) else: __lowerCAmelCase : List[str] = torch.Generator(device=_SCREAMING_SNAKE_CASE ).manual_seed(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Any = { 'class_labels': [1], 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs def __lowerCamelCase ( self ): __lowerCAmelCase : List[str] = 'cpu' __lowerCAmelCase : Any = self.get_dummy_components() __lowerCAmelCase : Union[str, Any] = self.pipeline_class(**_SCREAMING_SNAKE_CASE ) pipe.to(_SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Tuple = self.get_dummy_inputs(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : List[str] = pipe(**_SCREAMING_SNAKE_CASE ).images __lowerCAmelCase : List[str] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 16, 16, 3) ) __lowerCAmelCase : Optional[int] = np.array([0.2946, 0.6601, 0.4329, 0.3296, 0.4144, 0.5319, 0.7273, 0.5013, 0.4457] ) __lowerCAmelCase : List[Any] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 ) def __lowerCamelCase ( self ): self._test_inference_batch_single_identical(relax_max_difference=_SCREAMING_SNAKE_CASE , expected_max_diff=1E-3 ) @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available() , reason='XFormers attention is only available with CUDA and `xformers` installed' , ) def __lowerCamelCase ( self ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) @require_torch_gpu @slow class A__ ( unittest.TestCase): def __lowerCamelCase ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCamelCase ( self ): __lowerCAmelCase : Dict = torch.manual_seed(0 ) __lowerCAmelCase : int = DiTPipeline.from_pretrained('facebook/DiT-XL-2-256' ) pipe.to('cuda' ) __lowerCAmelCase : Optional[Any] = ['vase', 'umbrella', 'white shark', 'white wolf'] __lowerCAmelCase : Optional[Any] = pipe.get_label_ids(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[Any] = pipe(_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , num_inference_steps=40 , output_type='np' ).images for word, image in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Tuple = load_numpy( f"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy" ) assert np.abs((expected_image - image).max() ) < 1E-2 def __lowerCamelCase ( self ): __lowerCAmelCase : Any = DiTPipeline.from_pretrained('facebook/DiT-XL-2-512' ) __lowerCAmelCase : Union[str, Any] = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.to('cuda' ) __lowerCAmelCase : Dict = ['vase', 'umbrella'] __lowerCAmelCase : List[str] = pipe.get_label_ids(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Any = torch.manual_seed(0 ) __lowerCAmelCase : Optional[Any] = pipe(_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , num_inference_steps=25 , output_type='np' ).images for word, image in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Dict = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' f"/dit/{word}_512.npy" ) assert np.abs((expected_image - image).max() ) < 1E-1
86
from ...configuration_utils import PretrainedConfig from ...utils import logging A_ :Any = logging.get_logger(__name__) A_ :int = { '''sayakpaul/vit-msn-base''': '''https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json''', # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class __A ( a ): """simple docstring""" UpperCamelCase__ : Optional[int] ="""vit_msn""" def __init__( self , lowerCamelCase__=768 , lowerCamelCase__=12 , lowerCamelCase__=12 , lowerCamelCase__=3072 , lowerCamelCase__="gelu" , lowerCamelCase__=0.0 , lowerCamelCase__=0.0 , lowerCamelCase__=0.02 , lowerCamelCase__=1E-06 , lowerCamelCase__=224 , lowerCamelCase__=16 , lowerCamelCase__=3 , lowerCamelCase__=True , **lowerCamelCase__ , ): """simple docstring""" super().__init__(**lowerCamelCase__ ) __UpperCamelCase : int =hidden_size __UpperCamelCase : List[Any] =num_hidden_layers __UpperCamelCase : Union[str, Any] =num_attention_heads __UpperCamelCase : List[str] =intermediate_size __UpperCamelCase : Union[str, Any] =hidden_act __UpperCamelCase : str =hidden_dropout_prob __UpperCamelCase : Union[str, Any] =attention_probs_dropout_prob __UpperCamelCase : Union[str, Any] =initializer_range __UpperCamelCase : Tuple =layer_norm_eps __UpperCamelCase : Optional[Any] =image_size __UpperCamelCase : Optional[int] =patch_size __UpperCamelCase : Any =num_channels __UpperCamelCase : str =qkv_bias
71
0
import math import sys def lowercase_ ( _lowerCamelCase : int): if number != int(_lowerCamelCase): raise ValueError("the value of input must be a natural number") if number < 0: raise ValueError("the value of input must not be a negative number") if number == 0: return 1 lowercase__ : Optional[Any] = [-1] * (number + 1) lowercase__ : Any = 0 for i in range(1 , number + 1): lowercase__ : Optional[Any] = sys.maxsize lowercase__ : Dict = int(math.sqrt(_lowerCamelCase)) for j in range(1 , root + 1): lowercase__ : Optional[Any] = 1 + answers[i - (j**2)] lowercase__ : str = min(_lowerCamelCase , _lowerCamelCase) lowercase__ : Union[str, Any] = answer return answers[number] if __name__ == "__main__": import doctest doctest.testmod()
87
import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __A ( a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : str =DDIMPipeline UpperCamelCase__ : List[Any] =UNCONDITIONAL_IMAGE_GENERATION_PARAMS UpperCamelCase__ : Tuple =PipelineTesterMixin.required_optional_params - { """num_images_per_prompt""", """latents""", """callback""", """callback_steps""", } UpperCamelCase__ : Tuple =UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS UpperCamelCase__ : Any =False def __lowercase ( self ): """simple docstring""" torch.manual_seed(0 ) __UpperCamelCase : Optional[int] =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') , ) __UpperCamelCase : int =DDIMScheduler() __UpperCamelCase : Optional[int] ={'unet': unet, 'scheduler': scheduler} return components def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : str =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : Optional[int] =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Tuple ={ 'batch_size': 1, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Any ='cpu' __UpperCamelCase : Optional[Any] =self.get_dummy_components() __UpperCamelCase : Tuple =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =self.get_dummy_inputs(lowerCamelCase__ ) __UpperCamelCase : int =pipe(**lowerCamelCase__ ).images __UpperCamelCase : Dict =image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) __UpperCamelCase : Tuple =np.array( [1.000E00, 5.717E-01, 4.717E-01, 1.000E00, 0.000E00, 1.000E00, 3.000E-04, 0.000E00, 9.000E-04] ) __UpperCamelCase : Tuple =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) def __lowercase ( self ): """simple docstring""" super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_save_load_local(expected_max_difference=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_save_load_optional_components(expected_max_difference=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __A ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str ='google/ddpm-cifar10-32' __UpperCamelCase : str =UNetaDModel.from_pretrained(lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =DDIMScheduler() __UpperCamelCase : List[Any] =DDIMPipeline(unet=lowerCamelCase__ , scheduler=lowerCamelCase__ ) ddim.to(lowerCamelCase__ ) ddim.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Optional[int] =torch.manual_seed(0 ) __UpperCamelCase : List[str] =ddim(generator=lowerCamelCase__ , eta=0.0 , output_type='numpy' ).images __UpperCamelCase : Union[str, Any] =image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) __UpperCamelCase : str =np.array([0.1_723, 0.1_617, 0.1_600, 0.1_626, 0.1_497, 0.1_513, 0.1_505, 0.1_442, 0.1_453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[Any] ='google/ddpm-ema-bedroom-256' __UpperCamelCase : Any =UNetaDModel.from_pretrained(lowerCamelCase__ ) __UpperCamelCase : int =DDIMScheduler.from_pretrained(lowerCamelCase__ ) __UpperCamelCase : Dict =DDIMPipeline(unet=lowerCamelCase__ , scheduler=lowerCamelCase__ ) ddpm.to(lowerCamelCase__ ) ddpm.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Tuple =torch.manual_seed(0 ) __UpperCamelCase : Union[str, Any] =ddpm(generator=lowerCamelCase__ , output_type='numpy' ).images __UpperCamelCase : Tuple =image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) __UpperCamelCase : Optional[Any] =np.array([0.0_060, 0.0_201, 0.0_344, 0.0_024, 0.0_018, 0.0_002, 0.0_022, 0.0_000, 0.0_069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
71
0
import os import tempfile import unittest from pathlib import Path from transformers import AutoConfig, is_tf_available from transformers.testing_utils import require_tf if is_tf_available(): import tensorflow as tf from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments @require_tf class UpperCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _lowercase ( self : List[str] , UpperCamelCase__ : int ) -> str: """simple docstring""" for model_result in results.values(): for batch_size, sequence_length in zip(model_result["""bs"""] , model_result["""ss"""] ): __magic_name__ = model_result["""result"""][batch_size][sequence_length] self.assertIsNotNone(UpperCamelCase__ ) def _lowercase ( self : Union[str, Any] ) -> Optional[int]: """simple docstring""" __magic_name__ = """sshleifer/tiny-gpt2""" __magic_name__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase__ , inference=UpperCamelCase__ , sequence_lengths=[8] , batch_sizes=[1] , eager_mode=UpperCamelCase__ , multi_process=UpperCamelCase__ , ) __magic_name__ = TensorFlowBenchmark(UpperCamelCase__ ) __magic_name__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _lowercase ( self : Optional[Any] ) -> List[Any]: """simple docstring""" __magic_name__ = """sgugger/tiny-distilbert-classification""" __magic_name__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase__ , inference=UpperCamelCase__ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=UpperCamelCase__ , only_pretrain_model=UpperCamelCase__ , ) __magic_name__ = TensorFlowBenchmark(UpperCamelCase__ ) __magic_name__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _lowercase ( self : Any ) -> List[Any]: """simple docstring""" __magic_name__ = """sshleifer/tiny-gpt2""" __magic_name__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase__ , inference=UpperCamelCase__ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=UpperCamelCase__ , ) __magic_name__ = TensorFlowBenchmark(UpperCamelCase__ ) __magic_name__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _lowercase ( self : Tuple ) -> List[Any]: """simple docstring""" __magic_name__ = """sshleifer/tiny-gpt2""" __magic_name__ = AutoConfig.from_pretrained(UpperCamelCase__ ) __magic_name__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase__ , inference=UpperCamelCase__ , sequence_lengths=[8] , batch_sizes=[1] , eager_mode=UpperCamelCase__ , multi_process=UpperCamelCase__ , ) __magic_name__ = TensorFlowBenchmark(UpperCamelCase__ , [config] ) __magic_name__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _lowercase ( self : Any ) -> Union[str, Any]: """simple docstring""" __magic_name__ = """sshleifer/tiny-gpt2""" __magic_name__ = AutoConfig.from_pretrained(UpperCamelCase__ ) __magic_name__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase__ , inference=UpperCamelCase__ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=UpperCamelCase__ , ) __magic_name__ = TensorFlowBenchmark(UpperCamelCase__ , [config] ) __magic_name__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _lowercase ( self : List[Any] ) -> Dict: """simple docstring""" __magic_name__ = """sshleifer/tiny-gpt2""" __magic_name__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase__ , inference=UpperCamelCase__ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=UpperCamelCase__ , ) __magic_name__ = TensorFlowBenchmark(UpperCamelCase__ ) __magic_name__ = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def _lowercase ( self : Optional[Any] ) -> List[str]: """simple docstring""" __magic_name__ = """sshleifer/tiny-gpt2""" __magic_name__ = AutoConfig.from_pretrained(UpperCamelCase__ ) __magic_name__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase__ , inference=UpperCamelCase__ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=UpperCamelCase__ , ) __magic_name__ = TensorFlowBenchmark(UpperCamelCase__ , [config] ) __magic_name__ = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def _lowercase ( self : Union[str, Any] ) -> Any: """simple docstring""" __magic_name__ = """patrickvonplaten/t5-tiny-random""" __magic_name__ = AutoConfig.from_pretrained(UpperCamelCase__ ) __magic_name__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase__ , inference=UpperCamelCase__ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=UpperCamelCase__ , ) __magic_name__ = TensorFlowBenchmark(UpperCamelCase__ , configs=[config] ) __magic_name__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) @unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices("""GPU""" ) ) == 0 , """Cannot do xla on CPU.""" ) def _lowercase ( self : Tuple ) -> int: """simple docstring""" __magic_name__ = """sshleifer/tiny-gpt2""" __magic_name__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase__ , inference=UpperCamelCase__ , sequence_lengths=[8] , batch_sizes=[1] , use_xla=UpperCamelCase__ , multi_process=UpperCamelCase__ , ) __magic_name__ = TensorFlowBenchmark(UpperCamelCase__ ) __magic_name__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _lowercase ( self : Union[str, Any] ) -> Dict: """simple docstring""" __magic_name__ = """sshleifer/tiny-gpt2""" with tempfile.TemporaryDirectory() as tmp_dir: __magic_name__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , inference=UpperCamelCase__ , save_to_csv=UpperCamelCase__ , sequence_lengths=[8] , batch_sizes=[1] , inference_time_csv_file=os.path.join(UpperCamelCase__ , """inf_time.csv""" ) , inference_memory_csv_file=os.path.join(UpperCamelCase__ , """inf_mem.csv""" ) , env_info_csv_file=os.path.join(UpperCamelCase__ , """env.csv""" ) , multi_process=UpperCamelCase__ , ) __magic_name__ = TensorFlowBenchmark(UpperCamelCase__ ) benchmark.run() self.assertTrue(Path(os.path.join(UpperCamelCase__ , """inf_time.csv""" ) ).exists() ) self.assertTrue(Path(os.path.join(UpperCamelCase__ , """inf_mem.csv""" ) ).exists() ) self.assertTrue(Path(os.path.join(UpperCamelCase__ , """env.csv""" ) ).exists() ) def _lowercase ( self : int ) -> Optional[Any]: """simple docstring""" __magic_name__ = """sshleifer/tiny-gpt2""" def _check_summary_is_not_empty(UpperCamelCase__ : Dict ): self.assertTrue(hasattr(UpperCamelCase__ , """sequential""" ) ) self.assertTrue(hasattr(UpperCamelCase__ , """cumulative""" ) ) self.assertTrue(hasattr(UpperCamelCase__ , """current""" ) ) self.assertTrue(hasattr(UpperCamelCase__ , """total""" ) ) with tempfile.TemporaryDirectory() as tmp_dir: __magic_name__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , inference=UpperCamelCase__ , sequence_lengths=[8] , batch_sizes=[1] , log_filename=os.path.join(UpperCamelCase__ , """log.txt""" ) , log_print=UpperCamelCase__ , trace_memory_line_by_line=UpperCamelCase__ , eager_mode=UpperCamelCase__ , multi_process=UpperCamelCase__ , ) __magic_name__ = TensorFlowBenchmark(UpperCamelCase__ ) __magic_name__ = benchmark.run() _check_summary_is_not_empty(result.inference_summary ) self.assertTrue(Path(os.path.join(UpperCamelCase__ , """log.txt""" ) ).exists() )
88
from __future__ import annotations import copy import tempfile import unittest from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available from transformers.testing_utils import ( DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tensorflow_probability, require_tf, slow, ) from ..bert.test_modeling_bert import BertModelTester if is_tf_available(): from transformers import ( TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelForTableQuestionAnswering, TFAutoModelForTokenClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFFunnelBaseModel, TFFunnelModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, TFTapasForQuestionAnswering, ) from transformers.models.auto.modeling_tf_auto import ( TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_MAPPING, ) 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 from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST class __A ( a ): """simple docstring""" UpperCamelCase__ : Optional[int] ="""new-model""" if is_tf_available(): class __A ( a ): """simple docstring""" UpperCamelCase__ : List[str] =NewModelConfig @require_tf class __A ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] ='bert-base-cased' __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Union[str, Any] ='bert-base-cased' __UpperCamelCase : Optional[int] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Tuple =TFAutoModelForPreTraining.from_pretrained(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]: __UpperCamelCase : str =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModelForCausalLM.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : str =TFAutoModelForCausalLM.from_pretrained(lowerCamelCase__ , output_loading_info=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]: __UpperCamelCase : Optional[int] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Any =TFAutoModelWithLMHead.from_pretrained(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]: __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Any =TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Union[str, Any] =TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase__ , output_loading_info=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]: __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : str =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ , output_loading_info=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForSequenceClassification.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: __UpperCamelCase : List[Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[int] =TFAutoModelForQuestionAnswering.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow @require_tensorflow_probability def __lowercase ( self ): """simple docstring""" for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]: __UpperCamelCase : Any =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModelForTableQuestionAnswering.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : List[str] =TFAutoModelForTableQuestionAnswering.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[Any] =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Dict =TFAutoModel.from_pretrained('sgugger/funnel-random-tiny' ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[int] =copy.deepcopy(model.config ) __UpperCamelCase : Optional[Any] =['FunnelBaseModel'] __UpperCamelCase : Tuple =TFAutoModel.from_config(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : List[Any] =TFAutoModel.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" try: AutoConfig.register('new-model' , lowerCamelCase__ ) __UpperCamelCase : int =[ TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSequenceClassification, TFAutoModelForTokenClassification, ] for auto_class in auto_classes: with self.subTest(auto_class.__name__ ): # Wrong config class will raise an error with self.assertRaises(lowerCamelCase__ ): auto_class.register(lowerCamelCase__ , lowerCamelCase__ ) auto_class.register(lowerCamelCase__ , lowerCamelCase__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(lowerCamelCase__ ): auto_class.register(lowerCamelCase__ , lowerCamelCase__ ) # Now that the config is registered, it can be used as any other config with the auto-API __UpperCamelCase : List[str] =BertModelTester(self ).get_config() __UpperCamelCase : Optional[Any] =NewModelConfig(**tiny_config.to_dict() ) __UpperCamelCase : Dict =auto_class.from_config(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =auto_class.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) finally: if "new-model" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["new-model"] for mapping in ( TF_MODEL_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, ): if NewModelConfig in mapping._extra_content: del mapping._extra_content[NewModelConfig] def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCamelCase__ , 'bert-base is not a local folder and is not a valid model identifier' ): __UpperCamelCase : Dict =TFAutoModel.from_pretrained('bert-base' ) def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCamelCase__ , R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)' ): __UpperCamelCase : Union[str, Any] =TFAutoModel.from_pretrained(lowerCamelCase__ , revision='aaaaaa' ) def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCamelCase__ , 'hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin' , ): __UpperCamelCase : List[str] =TFAutoModel.from_pretrained('hf-internal-testing/config-no-model' ) def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex(lowerCamelCase__ , 'Use `from_pt=True` to load this model' ): __UpperCamelCase : List[Any] =TFAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only' ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[int] =TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert' ) with RequestCounter() as counter: __UpperCamelCase : Dict =TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 ) # With a sharded checkpoint __UpperCamelCase : Dict =TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded' ) with RequestCounter() as counter: __UpperCamelCase : Union[str, Any] =TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
71
0
'''simple docstring''' def __lowerCamelCase ( lowerCAmelCase_ ) -> bool: _a : List[str] = 0 for ch in input_str: _a : Optional[Any] = ord(lowerCAmelCase_ ) _a : Tuple = pow(2 , lowerCAmelCase_ ) # If we already turned on bit for current character's unicode if bitmap >> ch_unicode & 1 == 1: return False bitmap |= ch_bit_index_on return True if __name__ == "__main__": import doctest doctest.testmod()
89
import argparse import json import os import re import torch from transformers import BloomConfig, BloomModel from transformers.file_utils import CONFIG_NAME, WEIGHTS_NAME from transformers.utils import logging logging.set_verbosity_info() A_ :List[str] = [ '''word_embeddings_layernorm.weight''', '''word_embeddings_layernorm.bias''', '''input_layernorm.weight''', '''input_layernorm.bias''', '''post_attention_layernorm.weight''', '''post_attention_layernorm.bias''', '''self_attention.dense.bias''', '''mlp.dense_4h_to_h.bias''', '''ln_f.weight''', '''ln_f.bias''', ] A_ :Optional[Any] = [ '''mlp.dense_4h_to_h.weight''', '''self_attention.dense.weight''', ] def A ( a_ ,a_ ) -> str: __UpperCamelCase : Any ={ 'word_embeddings.weight': 'word_embeddings.weight', 'word_embeddings.norm.weight': 'word_embeddings_layernorm.weight', 'word_embeddings.norm.bias': 'word_embeddings_layernorm.bias', 'weight': 'ln_f.weight', 'bias': 'ln_f.bias', } if key in layer_rename_map: return layer_rename_map[key] # Handle transformer blocks __UpperCamelCase : Tuple =int(re.match(r'.*layer_(\d*).*' ,a_ )[1] ) layer_number -= 3 return F'h.{layer_number}.' + key def A ( a_ ) -> Any: if dtype == torch.bool: return 1 / 8 __UpperCamelCase : Dict =re.search(r'[^\d](\d+)$' ,str(a_ ) ) if bit_search is None: raise ValueError(F'`dtype` is not a valid dtype: {dtype}.' ) __UpperCamelCase : Tuple =int(bit_search.groups()[0] ) return bit_size // 8 def A ( a_ ,a_ ,a_ ,a_ ,a_ ) -> Dict: # Construct model if bloom_config_file == "": __UpperCamelCase : List[Any] =BloomConfig() else: __UpperCamelCase : List[str] =BloomConfig.from_json_file(a_ ) if shard_model: __UpperCamelCase : int =os.listdir(a_ ) __UpperCamelCase : Union[str, Any] =sorted(filter(lambda a_ : s.startswith('layer' ) and "model_00" in s ,a_ ) ) __UpperCamelCase : Optional[Any] ={'weight_map': {}, 'metadata': {}} __UpperCamelCase : Dict =0 __UpperCamelCase : int =None __UpperCamelCase : Any =BloomConfig() for j, file in enumerate(a_ ): print('Processing file: {}'.format(a_ ) ) __UpperCamelCase : Optional[int] =None for i in range(a_ ): # load all TP files __UpperCamelCase : Dict =file.replace('model_00' ,F'model_0{i}' ) __UpperCamelCase : Optional[Any] =torch.load(os.path.join(a_ ,a_ ) ,map_location='cpu' ) # Rename keys in the transformers names __UpperCamelCase : int =list(temp.keys() ) for key in keys: __UpperCamelCase : Dict =temp.pop(a_ ) if tensors is None: __UpperCamelCase : Any =temp else: for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel __UpperCamelCase : List[Any] =1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks __UpperCamelCase : Any =torch.cat([tensors[key], temp[key]] ,dim=a_ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): __UpperCamelCase : Optional[Any] =tensors[key] / pretraining_tp torch.save( a_ ,os.path.join( a_ ,'pytorch_model_{}-of-{}.bin'.format(str(j + 1 ).zfill(5 ) ,str(len(a_ ) ).zfill(5 ) ) ,) ,) for key in tensors.keys(): __UpperCamelCase : Union[str, Any] =tensors[key] total_size += value.numel() * get_dtype_size(value.dtype ) if key not in index_dict["weight_map"]: __UpperCamelCase : int ='pytorch_model_{}-of-{}.bin'.format( str(j + 1 ).zfill(5 ) ,str(len(a_ ) ).zfill(5 ) ) __UpperCamelCase : Union[str, Any] =BloomConfig() __UpperCamelCase : Tuple =pytorch_dump_folder_path + '/' + CONFIG_NAME __UpperCamelCase : Optional[int] =total_size with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(config.to_json_string() ) with open(os.path.join(a_ ,WEIGHTS_NAME + '.index.json' ) ,'w' ,encoding='utf-8' ) as f: __UpperCamelCase : List[Any] =json.dumps(a_ ,indent=2 ,sort_keys=a_ ) + '\n' f.write(a_ ) else: __UpperCamelCase : List[Any] =BloomModel(a_ ) __UpperCamelCase : Optional[Any] =os.listdir(a_ ) __UpperCamelCase : Dict =sorted(filter(lambda a_ : s.startswith('layer' ) and "model_00" in s ,a_ ) ) __UpperCamelCase : Any =None for i, file in enumerate(a_ ): __UpperCamelCase : Union[str, Any] =None for i in range(a_ ): # load all TP files __UpperCamelCase : Optional[Any] =file.replace('model_00' ,F'model_0{i}' ) __UpperCamelCase : str =torch.load(os.path.join(a_ ,a_ ) ,map_location='cpu' ) # Rename keys in the transformers names __UpperCamelCase : List[str] =list(temp.keys() ) for key in keys: __UpperCamelCase : Union[str, Any] =temp.pop(a_ ) if tensors is None: __UpperCamelCase : Optional[Any] =temp else: for key in tensors.keys(): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel __UpperCamelCase : Optional[int] =1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks __UpperCamelCase : int =torch.cat([tensors[key], temp[key]] ,dim=a_ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): __UpperCamelCase : Dict =tensors[key] / pretraining_tp __UpperCamelCase : str =model.load_state_dict(a_ ,strict=a_ ) assert not other_keys.unexpected_keys, F'The keys {other_keys.unexpected_keys} are unexpected' if missing_keys is None: __UpperCamelCase : str =set(other_keys.missing_keys ) else: __UpperCamelCase : int =missing_keys.intersection(set(other_keys.missing_keys ) ) assert not missing_keys, F'The keys {missing_keys} are missing' # Save pytorch-model os.makedirs(a_ ,exist_ok=a_ ) __UpperCamelCase : Optional[int] =pytorch_dump_folder_path + '/' + WEIGHTS_NAME __UpperCamelCase : Dict =pytorch_dump_folder_path + '/' + CONFIG_NAME print(F'Save PyTorch model to {pytorch_weights_dump_path} with dtype {config.torch_dtype}' ) if config.torch_dtype is not None: __UpperCamelCase : List[str] =model.to(config.torch_dtype ) torch.save(model.state_dict() ,a_ ) print(F'Save configuration file to {pytorch_config_dump_path}' ) with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": A_ :Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--bloom_checkpoint_path''', default=None, type=str, required=True, help='''Path to the Megatron-LM checkpoint path.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--bloom_config_file''', default='''''', type=str, help=( '''An optional config json file corresponding to the pre-trained model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--shard_model''', action='''store_true''', help='''An optional setting to shard the output model \nThis enables sharding the converted checkpoint''', ) parser.add_argument( '''--pretraining_tp''', default=4, type=int, help='''Pretraining TP rank that has been used when training the model in Megatron-LM \n''', ) A_ :str = parser.parse_args() convert_bloom_checkpoint_to_pytorch( args.bloom_checkpoint_path, args.bloom_config_file, args.pytorch_dump_folder_path, args.shard_model, args.pretraining_tp, )
71
0
from math import ceil, sqrt def lowerCamelCase_ ( UpperCamelCase__ : int = 100_0000 ) -> int: """simple docstring""" __lowerCamelCase = 0 for outer_width in range(3 , (limit // 4) + 2 ): if outer_width**2 > limit: __lowerCamelCase = max(ceil(sqrt(outer_width**2 - limit ) ) , 1 ) else: __lowerCamelCase = 1 if (outer_width - hole_width_lower_bound) % 2: hole_width_lower_bound += 1 answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 return answer if __name__ == "__main__": print(f'''{solution() = }''')
90
from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class __A : """simple docstring""" UpperCamelCase__ : int =XGLMConfig UpperCamelCase__ : Optional[Any] ={} UpperCamelCase__ : List[str] ="""gelu""" def __init__( self , lowerCamelCase__ , lowerCamelCase__=14 , lowerCamelCase__=7 , 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__=0.02 , ): """simple docstring""" __UpperCamelCase : Tuple =parent __UpperCamelCase : List[str] =batch_size __UpperCamelCase : str =seq_length __UpperCamelCase : Dict =is_training __UpperCamelCase : Tuple =use_input_mask __UpperCamelCase : List[Any] =use_labels __UpperCamelCase : Any =vocab_size __UpperCamelCase : List[Any] =d_model __UpperCamelCase : Optional[int] =num_hidden_layers __UpperCamelCase : List[str] =num_attention_heads __UpperCamelCase : Optional[int] =ffn_dim __UpperCamelCase : str =activation_function __UpperCamelCase : Any =activation_dropout __UpperCamelCase : Optional[int] =attention_dropout __UpperCamelCase : Optional[int] =max_position_embeddings __UpperCamelCase : Any =initializer_range __UpperCamelCase : Dict =None __UpperCamelCase : Optional[int] =0 __UpperCamelCase : Optional[Any] =2 __UpperCamelCase : str =1 def __lowercase ( self ): """simple docstring""" return XGLMConfig.from_pretrained('facebook/xglm-564M' ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[Any] =tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) , clip_value_min=0 , clip_value_max=3 ) __UpperCamelCase : Union[str, Any] =None if self.use_input_mask: __UpperCamelCase : Dict =random_attention_mask([self.batch_size, self.seq_length] ) __UpperCamelCase : Any =self.get_config() __UpperCamelCase : Optional[Any] =floats_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, ) def __lowercase ( self ): """simple docstring""" return XGLMConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , num_layers=self.num_hidden_layers , attention_heads=self.num_attention_heads , ffn_dim=self.ffn_dim , activation_function=self.activation_function , activation_dropout=self.activation_dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , use_cache=lowerCamelCase__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , return_dict=lowerCamelCase__ , ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =self.prepare_config_and_inputs() ( ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ) : int =config_and_inputs __UpperCamelCase : Optional[Any] ={ 'input_ids': input_ids, 'head_mask': head_mask, } return config, inputs_dict @require_tf class __A ( a , a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : Union[str, Any] =(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () UpperCamelCase__ : str =(TFXGLMForCausalLM,) if is_tf_available() else () UpperCamelCase__ : Optional[Any] =( {"""feature-extraction""": TFXGLMModel, """text-generation""": TFXGLMForCausalLM} if is_tf_available() else {} ) UpperCamelCase__ : Tuple =False UpperCamelCase__ : Tuple =False UpperCamelCase__ : Optional[Any] =False def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Tuple =TFXGLMModelTester(self ) __UpperCamelCase : Dict =ConfigTester(self , config_class=lowerCamelCase__ , n_embd=37 ) def __lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() @slow def __lowercase ( self ): """simple docstring""" for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __UpperCamelCase : Optional[Any] =TFXGLMModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @unittest.skip(reason='Currently, model embeddings are going to undergo a major refactor.' ) def __lowercase ( self ): """simple docstring""" super().test_resize_token_embeddings() @require_tf class __A ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self , lowerCamelCase__=True ): """simple docstring""" __UpperCamelCase : int =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : List[str] =tf.convert_to_tensor([[2, 268, 9865]] , dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off __UpperCamelCase : str =[2, 268, 9865, 67, 11, 1988, 57252, 9865, 5, 984, 67, 1988, 213838, 1658, 53, 70446, 33, 6657, 278, 1581] # fmt: on __UpperCamelCase : Optional[Any] =model.generate(lowerCamelCase__ , do_sample=lowerCamelCase__ , num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist() , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : Union[str, Any] =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) tf.random.set_seed(0 ) __UpperCamelCase : str =tokenizer('Today is a nice day and' , return_tensors='tf' ) __UpperCamelCase : Union[str, Any] =tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(':/CPU:0' ): __UpperCamelCase : Any =model.generate(lowerCamelCase__ , do_sample=lowerCamelCase__ , seed=[7, 0] ) __UpperCamelCase : Tuple =tokenizer.decode(output_ids[0] , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : List[Any] =( 'Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due' ) self.assertEqual(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Tuple =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : Optional[Any] =XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : Optional[Any] ='left' # use different length sentences to test batching __UpperCamelCase : Optional[int] =[ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When', 'Hello, my dog is a little', ] __UpperCamelCase : List[Any] =tokenizer(lowerCamelCase__ , return_tensors='tf' , padding=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =inputs['input_ids'] __UpperCamelCase : Dict =model.generate(input_ids=lowerCamelCase__ , attention_mask=inputs['attention_mask'] , max_new_tokens=12 ) __UpperCamelCase : List[Any] =tokenizer(sentences[0] , return_tensors='tf' ).input_ids __UpperCamelCase : Dict =model.generate(input_ids=lowerCamelCase__ , max_new_tokens=12 ) __UpperCamelCase : Any =tokenizer(sentences[1] , return_tensors='tf' ).input_ids __UpperCamelCase : Optional[Any] =model.generate(input_ids=lowerCamelCase__ , max_new_tokens=12 ) __UpperCamelCase : Optional[int] =tokenizer.batch_decode(lowerCamelCase__ , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =tokenizer.decode(output_non_padded[0] , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : int =tokenizer.decode(output_padded[0] , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : Any =[ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When left padding is applied, the sequence will be ' 'a single', 'Hello, my dog is a little bit of a shy one, but he is very friendly', ] self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , [non_padded_sentence, padded_sentence] )
71
0
"""simple docstring""" # Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from accelerate import PartialState from accelerate.utils.operations import broadcast, gather, gather_object, pad_across_processes, reduce def _A (__a ) -> Dict: """simple docstring""" return (torch.arange(state.num_processes ) + 1.0 + (state.num_processes * state.process_index)).to(state.device ) def _A (__a ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = create_tensor(__a ) SCREAMING_SNAKE_CASE_ : List[Any] = gather(__a ) assert gathered_tensor.tolist() == list(range(1 , state.num_processes**2 + 1 ) ) def _A (__a ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = [state.process_index] SCREAMING_SNAKE_CASE_ : int = gather_object(__a ) assert len(__a ) == state.num_processes, f'{gathered_obj}, {len(__a )} != {state.num_processes}' assert gathered_obj == list(range(state.num_processes ) ), f'{gathered_obj} != {list(range(state.num_processes ) )}' def _A (__a ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = create_tensor(__a ) SCREAMING_SNAKE_CASE_ : List[str] = broadcast(__a ) assert broadcasted_tensor.shape == torch.Size([state.num_processes] ) assert broadcasted_tensor.tolist() == list(range(1 , state.num_processes + 1 ) ) def _A (__a ) -> List[Any]: """simple docstring""" if state.is_main_process: SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.arange(state.num_processes + 1 ).to(state.device ) else: SCREAMING_SNAKE_CASE_ : Tuple = torch.arange(state.num_processes ).to(state.device ) SCREAMING_SNAKE_CASE_ : int = pad_across_processes(__a ) assert padded_tensor.shape == torch.Size([state.num_processes + 1] ) if not state.is_main_process: assert padded_tensor.tolist() == list(range(0 , state.num_processes ) ) + [0] def _A (__a ) -> int: """simple docstring""" if state.num_processes != 2: return SCREAMING_SNAKE_CASE_ : Tuple = create_tensor(__a ) SCREAMING_SNAKE_CASE_ : Dict = reduce(__a , '''sum''' ) SCREAMING_SNAKE_CASE_ : Any = torch.tensor([4.0, 6] ).to(state.device ) assert torch.allclose(__a , __a ), f'{reduced_tensor} != {truth_tensor}' def _A (__a ) -> List[str]: """simple docstring""" if state.num_processes != 2: return SCREAMING_SNAKE_CASE_ : Tuple = create_tensor(__a ) SCREAMING_SNAKE_CASE_ : int = reduce(__a , '''mean''' ) SCREAMING_SNAKE_CASE_ : Optional[int] = torch.tensor([2.0, 3] ).to(state.device ) assert torch.allclose(__a , __a ), f'{reduced_tensor} != {truth_tensor}' def _A (__a ) -> Optional[Any]: """simple docstring""" main() def _A () -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = PartialState() state.print(f'State: {state}' ) state.print('''testing gather''' ) test_gather(__a ) state.print('''testing gather_object''' ) test_gather_object(__a ) state.print('''testing broadcast''' ) test_broadcast(__a ) state.print('''testing pad_across_processes''' ) test_pad_across_processes(__a ) state.print('''testing reduce_sum''' ) test_reduce_sum(__a ) state.print('''testing reduce_mean''' ) test_reduce_mean(__a ) if __name__ == "__main__": main()
91
import argparse import json import numpy import torch from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def A ( a_ ,a_ ) -> Optional[Any]: # Load checkpoint __UpperCamelCase : int =torch.load(a_ ,map_location='cpu' ) __UpperCamelCase : List[Any] =chkpt['model'] # We have the base model one level deeper than the original XLM repository __UpperCamelCase : str ={} for k, v in state_dict.items(): if "pred_layer" in k: __UpperCamelCase : Optional[Any] =v else: __UpperCamelCase : Optional[Any] =v __UpperCamelCase : List[Any] =chkpt['params'] __UpperCamelCase : str ={n: v for n, v in config.items() if not isinstance(a_ ,(torch.FloatTensor, numpy.ndarray) )} __UpperCamelCase : str =chkpt['dico_word2id'] __UpperCamelCase : Dict ={s + '</w>' if s.find('@@' ) == -1 and i > 13 else s.replace('@@' ,'' ): i for s, i in vocab.items()} # Save pytorch-model __UpperCamelCase : List[Any] =pytorch_dump_folder_path + '/' + WEIGHTS_NAME __UpperCamelCase : Tuple =pytorch_dump_folder_path + '/' + CONFIG_NAME __UpperCamelCase : Any =pytorch_dump_folder_path + '/' + VOCAB_FILES_NAMES['vocab_file'] print(F'Save PyTorch model to {pytorch_weights_dump_path}' ) torch.save(a_ ,a_ ) print(F'Save configuration file to {pytorch_config_dump_path}' ) with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(json.dumps(a_ ,indent=2 ) + '\n' ) print(F'Save vocab file to {pytorch_config_dump_path}' ) with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(json.dumps(a_ ,indent=2 ) + '\n' ) if __name__ == "__main__": A_ :str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--xlm_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.''' ) A_ :List[Any] = parser.parse_args() convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)
71
0
import argparse import os import re import packaging.version UpperCamelCase__ = """examples/""" UpperCamelCase__ = { """examples""": (re.compile(R"""^check_min_version\(\"[^\"]+\"\)\s*$""", re.MULTILINE), """check_min_version(\"VERSION\")\n"""), """init""": (re.compile(R"""^__version__\s+=\s+\"([^\"]+)\"\s*$""", re.MULTILINE), """__version__ = \"VERSION\"\n"""), """setup""": (re.compile(R"""^(\s*)version\s*=\s*\"[^\"]+\",""", re.MULTILINE), R"""\1version=\"VERSION\","""), """doc""": (re.compile(R"""^(\s*)release\s*=\s*\"[^\"]+\"$""", re.MULTILINE), """release = \"VERSION\"\n"""), } UpperCamelCase__ = { """init""": """src/transformers/__init__.py""", """setup""": """setup.py""", } UpperCamelCase__ = """README.md""" def _a ( SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : List[str] ): with open(SCREAMING_SNAKE_CASE_ , "r" , encoding="utf-8" , newline="\n" ) as f: __lowerCAmelCase = f.read() __lowerCAmelCase , __lowerCAmelCase = REPLACE_PATTERNS[pattern] __lowerCAmelCase = replace.replace("VERSION" , SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = re_pattern.sub(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) with open(SCREAMING_SNAKE_CASE_ , "w" , encoding="utf-8" , newline="\n" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) def _a ( SCREAMING_SNAKE_CASE_ : List[Any] ): for folder, directories, fnames in os.walk(SCREAMING_SNAKE_CASE_ ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove("research_projects" ) if "legacy" in directories: directories.remove("legacy" ) for fname in fnames: if fname.endswith(".py" ): update_version_in_file(os.path.join(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ , pattern="examples" ) def _a ( SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Optional[int]=False ): for pattern, fname in REPLACE_FILES.items(): update_version_in_file(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if not patch: update_version_in_examples(SCREAMING_SNAKE_CASE_ ) def _a ( ): __lowerCAmelCase = "🤗 Transformers currently provides the following architectures" __lowerCAmelCase = "1. Want to contribute a new model?" with open(SCREAMING_SNAKE_CASE_ , "r" , encoding="utf-8" , newline="\n" ) as f: __lowerCAmelCase = f.readlines() # Find the start of the list. __lowerCAmelCase = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 __lowerCAmelCase = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith("1." ): __lowerCAmelCase = lines[index].replace( "https://huggingface.co/docs/transformers/main/model_doc" , "https://huggingface.co/docs/transformers/model_doc" , ) index += 1 with open(SCREAMING_SNAKE_CASE_ , "w" , encoding="utf-8" , newline="\n" ) as f: f.writelines(SCREAMING_SNAKE_CASE_ ) def _a ( ): with open(REPLACE_FILES["init"] , "r" ) as f: __lowerCAmelCase = f.read() __lowerCAmelCase = REPLACE_PATTERNS["init"][0].search(SCREAMING_SNAKE_CASE_ ).groups()[0] return packaging.version.parse(SCREAMING_SNAKE_CASE_ ) def _a ( SCREAMING_SNAKE_CASE_ : List[Any]=False ): __lowerCAmelCase = get_version() if patch and default_version.is_devrelease: raise ValueError("Can't create a patch version from the dev branch, checkout a released version!" ) if default_version.is_devrelease: __lowerCAmelCase = default_version.base_version elif patch: __lowerCAmelCase = F"""{default_version.major}.{default_version.minor}.{default_version.micro + 1}""" else: __lowerCAmelCase = F"""{default_version.major}.{default_version.minor + 1}.0""" # Now let's ask nicely if that's the right one. __lowerCAmelCase = input(F"""Which version are you releasing? [{default_version}]""" ) if len(SCREAMING_SNAKE_CASE_ ) == 0: __lowerCAmelCase = default_version print(F"""Updating version to {version}.""" ) global_version_update(SCREAMING_SNAKE_CASE_ , patch=SCREAMING_SNAKE_CASE_ ) if not patch: print("Cleaning main README, don't forget to run `make fix-copies`." ) clean_main_ref_in_model_list() def _a ( ): __lowerCAmelCase = get_version() __lowerCAmelCase = F"""{current_version.major}.{current_version.minor + 1}.0.dev0""" __lowerCAmelCase = current_version.base_version # Check with the user we got that right. __lowerCAmelCase = input(F"""Which version are we developing now? [{dev_version}]""" ) if len(SCREAMING_SNAKE_CASE_ ) == 0: __lowerCAmelCase = dev_version print(F"""Updating version to {version}.""" ) global_version_update(SCREAMING_SNAKE_CASE_ ) print("Cleaning main README, don't forget to run `make fix-copies`." ) clean_main_ref_in_model_list() if __name__ == "__main__": UpperCamelCase__ = argparse.ArgumentParser() parser.add_argument("""--post_release""", action="""store_true""", help="""Whether this is pre or post release.""") parser.add_argument("""--patch""", action="""store_true""", help="""Whether or not this is a patch release.""") UpperCamelCase__ = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print("""Nothing to do after a patch :-)""") else: post_release_work()
92
import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class __A ( a ): """simple docstring""" def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str =tempfile.mkdtemp() __UpperCamelCase : Optional[int] =8 # DPR tok __UpperCamelCase : str =[ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] __UpperCamelCase : Optional[Any] =os.path.join(self.tmpdirname , 'dpr_tokenizer' ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) __UpperCamelCase : Dict =os.path.join(lowerCamelCase__ , DPR_VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) # BART tok __UpperCamelCase : Optional[int] =[ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] __UpperCamelCase : str =dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) __UpperCamelCase : Optional[int] =['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] __UpperCamelCase : Any ={'unk_token': '<unk>'} __UpperCamelCase : Any =os.path.join(self.tmpdirname , 'bart_tokenizer' ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) __UpperCamelCase : Any =os.path.join(lowerCamelCase__ , BART_VOCAB_FILES_NAMES['vocab_file'] ) __UpperCamelCase : Dict =os.path.join(lowerCamelCase__ , BART_VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(lowerCamelCase__ ) ) def __lowercase ( self ): """simple docstring""" return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'dpr_tokenizer' ) ) def __lowercase ( self ): """simple docstring""" return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'bart_tokenizer' ) ) def __lowercase ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) @require_tokenizers def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =os.path.join(self.tmpdirname , 'rag_tokenizer' ) __UpperCamelCase : Dict =RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) __UpperCamelCase : List[Any] =RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(lowerCamelCase__ ) rag_tokenizer.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : int =RagTokenizer.from_pretrained(lowerCamelCase__ , config=lowerCamelCase__ ) self.assertIsInstance(new_rag_tokenizer.question_encoder , lowerCamelCase__ ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , lowerCamelCase__ ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Dict =RagTokenizer.from_pretrained('facebook/rag-token-nq' ) __UpperCamelCase : Union[str, Any] =[ 'who got the first nobel prize in physics', 'when is the next deadpool movie being released', 'which mode is used for short wave broadcast service', 'who is the owner of reading football club', 'when is the next scandal episode coming out', 'when is the last time the philadelphia won the superbowl', 'what is the most current adobe flash player version', 'how many episodes are there in dragon ball z', 'what is the first step in the evolution of the eye', 'where is gall bladder situated in human body', 'what is the main mineral in lithium batteries', 'who is the president of usa right now', 'where do the greasers live in the outsiders', 'panda is a national animal of which country', 'what is the name of manchester united stadium', ] __UpperCamelCase : int =tokenizer(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =RagTokenizer.from_pretrained('facebook/rag-sequence-nq' ) __UpperCamelCase : Union[str, Any] =[ 'who got the first nobel prize in physics', 'when is the next deadpool movie being released', 'which mode is used for short wave broadcast service', 'who is the owner of reading football club', 'when is the next scandal episode coming out', 'when is the last time the philadelphia won the superbowl', 'what is the most current adobe flash player version', 'how many episodes are there in dragon ball z', 'what is the first step in the evolution of the eye', 'where is gall bladder situated in human body', 'what is the main mineral in lithium batteries', 'who is the president of usa right now', 'where do the greasers live in the outsiders', 'panda is a national animal of which country', 'what is the name of manchester united stadium', ] __UpperCamelCase : Any =tokenizer(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ )
71
0
'''simple docstring''' import datetime import platform import subprocess from typing import Optional, Tuple, Union import numpy as np def snake_case_ ( __SCREAMING_SNAKE_CASE : bytes , __SCREAMING_SNAKE_CASE : int ): """simple docstring""" lowercase_ : Any = F'''{sampling_rate}''' lowercase_ : Union[str, Any] = '''1''' lowercase_ : Optional[Any] = '''f32le''' lowercase_ : Optional[Any] = [ '''ffmpeg''', '''-i''', '''pipe:0''', '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] try: with subprocess.Popen(__SCREAMING_SNAKE_CASE , stdin=subprocess.PIPE , stdout=subprocess.PIPE ) as ffmpeg_process: lowercase_ : List[str] = ffmpeg_process.communicate(__SCREAMING_SNAKE_CASE ) except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to load audio files from filename''' ) from error lowercase_ : int = output_stream[0] lowercase_ : Dict = np.frombuffer(__SCREAMING_SNAKE_CASE , np.floataa ) if audio.shape[0] == 0: raise ValueError('''Malformed soundfile''' ) return audio def snake_case_ ( __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : float , __SCREAMING_SNAKE_CASE : str = "f32le" , ): """simple docstring""" lowercase_ : str = F'''{sampling_rate}''' lowercase_ : Optional[Any] = '''1''' if format_for_conversion == "s16le": lowercase_ : Any = 2 elif format_for_conversion == "f32le": lowercase_ : Optional[int] = 4 else: raise ValueError(F'''Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`''' ) lowercase_ : List[Any] = platform.system() if system == "Linux": lowercase_ : Optional[int] = '''alsa''' lowercase_ : Dict = '''default''' elif system == "Darwin": lowercase_ : int = '''avfoundation''' lowercase_ : str = ''':0''' elif system == "Windows": lowercase_ : int = '''dshow''' lowercase_ : Optional[Any] = '''default''' lowercase_ : Dict = [ '''ffmpeg''', '''-f''', format_, '''-i''', input_, '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-fflags''', '''nobuffer''', '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] lowercase_ : Any = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample lowercase_ : int = _ffmpeg_stream(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) for item in iterator: yield item def snake_case_ ( __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : float , __SCREAMING_SNAKE_CASE : Optional[int] = None , __SCREAMING_SNAKE_CASE : Optional[Union[Tuple[float, float], float]] = None , __SCREAMING_SNAKE_CASE : str = "f32le" , ): """simple docstring""" if stream_chunk_s is not None: lowercase_ : List[str] = stream_chunk_s else: lowercase_ : Optional[int] = chunk_length_s lowercase_ : List[Any] = ffmpeg_microphone(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , format_for_conversion=__SCREAMING_SNAKE_CASE ) if format_for_conversion == "s16le": lowercase_ : Tuple = np.intaa lowercase_ : Dict = 2 elif format_for_conversion == "f32le": lowercase_ : List[Any] = np.floataa lowercase_ : List[Any] = 4 else: raise ValueError(F'''Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`''' ) if stride_length_s is None: lowercase_ : List[str] = chunk_length_s / 6 lowercase_ : Any = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample if isinstance(__SCREAMING_SNAKE_CASE , (int, float) ): lowercase_ : Tuple = [stride_length_s, stride_length_s] lowercase_ : str = int(round(sampling_rate * stride_length_s[0] ) ) * size_of_sample lowercase_ : int = int(round(sampling_rate * stride_length_s[1] ) ) * size_of_sample lowercase_ : Tuple = datetime.datetime.now() lowercase_ : Dict = datetime.timedelta(seconds=__SCREAMING_SNAKE_CASE ) for item in chunk_bytes_iter(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , stride=(stride_left, stride_right) , stream=__SCREAMING_SNAKE_CASE ): # Put everything back in numpy scale lowercase_ : int = np.frombuffer(item['''raw'''] , dtype=__SCREAMING_SNAKE_CASE ) lowercase_ : Union[str, Any] = ( item['''stride'''][0] // size_of_sample, item['''stride'''][1] // size_of_sample, ) lowercase_ : Optional[Any] = sampling_rate audio_time += delta if datetime.datetime.now() > audio_time + 10 * delta: # We're late !! SKIP continue yield item def snake_case_ ( __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Tuple[int, int] , __SCREAMING_SNAKE_CASE : bool = False ): """simple docstring""" lowercase_ : Optional[int] = B'''''' lowercase_ , lowercase_ : Dict = stride if stride_left + stride_right >= chunk_len: raise ValueError( F'''Stride needs to be strictly smaller than chunk_len: ({stride_left}, {stride_right}) vs {chunk_len}''' ) lowercase_ : Any = 0 for raw in iterator: acc += raw if stream and len(__SCREAMING_SNAKE_CASE ) < chunk_len: lowercase_ : List[str] = (_stride_left, 0) yield {"raw": acc[:chunk_len], "stride": stride, "partial": True} else: while len(__SCREAMING_SNAKE_CASE ) >= chunk_len: # We are flushing the accumulator lowercase_ : Tuple = (_stride_left, stride_right) lowercase_ : List[str] = {'''raw''': acc[:chunk_len], '''stride''': stride} if stream: lowercase_ : int = False yield item lowercase_ : Optional[Any] = stride_left lowercase_ : Union[str, Any] = acc[chunk_len - stride_left - stride_right :] # Last chunk if len(__SCREAMING_SNAKE_CASE ) > stride_left: lowercase_ : Union[str, Any] = {'''raw''': acc, '''stride''': (_stride_left, 0)} if stream: lowercase_ : Union[str, Any] = False yield item def snake_case_ ( __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int ): """simple docstring""" lowercase_ : str = 2**24 # 16Mo try: with subprocess.Popen(__SCREAMING_SNAKE_CASE , stdout=subprocess.PIPE , bufsize=__SCREAMING_SNAKE_CASE ) as ffmpeg_process: while True: lowercase_ : Any = ffmpeg_process.stdout.read(__SCREAMING_SNAKE_CASE ) if raw == b"": break yield raw except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to stream audio files from filename''' ) from error
93
A_ :Optional[int] = ''' # Installazione di Transformers ! pip install transformers datasets # Per installare dalla fonte invece dell\'ultima versione rilasciata, commenta il comando sopra e # rimuovi la modalità commento al comando seguente. # ! pip install git+https://github.com/huggingface/transformers.git ''' A_ :Union[str, Any] = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] A_ :Optional[Any] = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
71
0
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from pathlib import Path import torch from ...utils import is_npu_available, is_xpu_available from .config_args import ClusterConfig, default_json_config_file from .config_utils import SubcommandHelpFormatter snake_case : int = '''Create a default config file for Accelerate with only a few flags set.''' def __lowerCamelCase ( UpperCAmelCase_ : Optional[Any]="no" , UpperCAmelCase_ : str = default_json_config_file , UpperCAmelCase_ : bool = False ): """simple docstring""" a :List[str] = Path(UpperCAmelCase_ ) path.parent.mkdir(parents=UpperCAmelCase_ , exist_ok=UpperCAmelCase_ ) if path.exists(): print( F'''Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.''' ) return False a :Optional[Any] = mixed_precision.lower() if mixed_precision not in ["no", "fp16", "bf16", "fp8"]: raise ValueError( F'''`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}''' ) a :List[Any] = { '''compute_environment''': '''LOCAL_MACHINE''', '''mixed_precision''': mixed_precision, } if torch.cuda.is_available(): a :Dict = torch.cuda.device_count() a :Tuple = num_gpus a :int = False if num_gpus > 1: a :str = '''MULTI_GPU''' else: a :List[Any] = '''NO''' elif is_xpu_available() and use_xpu: a :List[Any] = torch.xpu.device_count() a :Optional[int] = num_xpus a :List[Any] = False if num_xpus > 1: a :int = '''MULTI_XPU''' else: a :str = '''NO''' elif is_npu_available(): a :List[str] = torch.npu.device_count() a :Any = num_npus a :Optional[int] = False if num_npus > 1: a :List[str] = '''MULTI_NPU''' else: a :Dict = '''NO''' else: a :str = 0 a :Optional[Any] = True a :Optional[Any] = 1 a :str = '''NO''' a :List[str] = ClusterConfig(**UpperCAmelCase_ ) config.to_json_file(UpperCAmelCase_ ) return path def __lowerCamelCase ( UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Union[str, Any] ): """simple docstring""" a :List[Any] = parser.add_parser('''default''' , parents=UpperCAmelCase_ , help=UpperCAmelCase_ , formatter_class=UpperCAmelCase_ ) parser.add_argument( '''--config_file''' , default=UpperCAmelCase_ , help=( '''The path to use to store the config file. Will default to a file named default_config.yaml in the cache ''' '''location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have ''' '''such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed ''' '''with \'huggingface\'.''' ) , dest='''save_location''' , ) parser.add_argument( '''--mixed_precision''' , choices=['''no''', '''fp16''', '''bf16'''] , type=UpperCAmelCase_ , help='''Whether or not to use mixed precision training. ''' '''Choose between FP16 and BF16 (bfloat16) training. ''' '''BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.''' , default='''no''' , ) parser.set_defaults(func=UpperCAmelCase_ ) return parser def __lowerCamelCase ( UpperCAmelCase_ : int ): """simple docstring""" a :Optional[Any] = write_basic_config(args.mixed_precision , args.save_location ) if config_file: print(F'''accelerate configuration saved at {config_file}''' )
94
import argparse import hashlib import os import urllib import warnings import torch from torch import nn from tqdm import tqdm from transformers import WhisperConfig, WhisperForConditionalGeneration A_ :Optional[Any] = { '''tiny.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt''', '''tiny''': '''https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt''', '''base.en''': '''https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt''', '''base''': '''https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt''', '''small.en''': '''https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt''', '''small''': '''https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt''', '''medium.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt''', '''medium''': '''https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt''', '''large''': '''https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large.pt''', '''large-v2''': '''https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt''', } def A ( a_ ) -> List[Any]: __UpperCamelCase : Any =['layers', 'blocks'] for k in ignore_keys: state_dict.pop(a_ ,a_ ) A_ :int = { '''blocks''': '''layers''', '''mlp.0''': '''fc1''', '''mlp.2''': '''fc2''', '''mlp_ln''': '''final_layer_norm''', '''.attn.query''': '''.self_attn.q_proj''', '''.attn.key''': '''.self_attn.k_proj''', '''.attn.value''': '''.self_attn.v_proj''', '''.attn_ln''': '''.self_attn_layer_norm''', '''.attn.out''': '''.self_attn.out_proj''', '''.cross_attn.query''': '''.encoder_attn.q_proj''', '''.cross_attn.key''': '''.encoder_attn.k_proj''', '''.cross_attn.value''': '''.encoder_attn.v_proj''', '''.cross_attn_ln''': '''.encoder_attn_layer_norm''', '''.cross_attn.out''': '''.encoder_attn.out_proj''', '''decoder.ln.''': '''decoder.layer_norm.''', '''encoder.ln.''': '''encoder.layer_norm.''', '''token_embedding''': '''embed_tokens''', '''encoder.positional_embedding''': '''encoder.embed_positions.weight''', '''decoder.positional_embedding''': '''decoder.embed_positions.weight''', '''ln_post''': '''layer_norm''', } def A ( a_ ) -> Union[str, Any]: __UpperCamelCase : str =list(s_dict.keys() ) for key in keys: __UpperCamelCase : str =key for k, v in WHISPER_MAPPING.items(): if k in key: __UpperCamelCase : Optional[Any] =new_key.replace(a_ ,a_ ) print(F'{key} -> {new_key}' ) __UpperCamelCase : Dict =s_dict.pop(a_ ) return s_dict def A ( a_ ) -> Optional[Any]: __UpperCamelCase , __UpperCamelCase : Tuple =emb.weight.shape __UpperCamelCase : Tuple =nn.Linear(a_ ,a_ ,bias=a_ ) __UpperCamelCase : List[Any] =emb.weight.data return lin_layer def A ( a_ ,a_ ) -> bytes: os.makedirs(a_ ,exist_ok=a_ ) __UpperCamelCase : Optional[int] =os.path.basename(a_ ) __UpperCamelCase : Union[str, Any] =url.split('/' )[-2] __UpperCamelCase : Union[str, Any] =os.path.join(a_ ,a_ ) if os.path.exists(a_ ) and not os.path.isfile(a_ ): raise RuntimeError(F'{download_target} exists and is not a regular file' ) if os.path.isfile(a_ ): __UpperCamelCase : str =open(a_ ,'rb' ).read() if hashlib.shaaaa(a_ ).hexdigest() == expected_shaaaa: return model_bytes else: warnings.warn(F'{download_target} exists, but the SHA256 checksum does not match; re-downloading the file' ) with urllib.request.urlopen(a_ ) as source, open(a_ ,'wb' ) as output: with tqdm( total=int(source.info().get('Content-Length' ) ) ,ncols=80 ,unit='iB' ,unit_scale=a_ ,unit_divisor=1_024 ) as loop: while True: __UpperCamelCase : Optional[Any] =source.read(8_192 ) if not buffer: break output.write(a_ ) loop.update(len(a_ ) ) __UpperCamelCase : List[Any] =open(a_ ,'rb' ).read() if hashlib.shaaaa(a_ ).hexdigest() != expected_shaaaa: raise RuntimeError( 'Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model.' ) return model_bytes def A ( a_ ,a_ ) -> Optional[Any]: if ".pt" not in checkpoint_path: __UpperCamelCase : int =_download(_MODELS[checkpoint_path] ) else: __UpperCamelCase : List[str] =torch.load(a_ ,map_location='cpu' ) __UpperCamelCase : Union[str, Any] =original_checkpoint['dims'] __UpperCamelCase : List[Any] =original_checkpoint['model_state_dict'] __UpperCamelCase : Dict =state_dict['decoder.token_embedding.weight'] remove_ignore_keys_(a_ ) rename_keys(a_ ) __UpperCamelCase : List[str] =True __UpperCamelCase : str =state_dict['decoder.layers.0.fc1.weight'].shape[0] __UpperCamelCase : Optional[int] =WhisperConfig( vocab_size=dimensions['n_vocab'] ,encoder_ffn_dim=a_ ,decoder_ffn_dim=a_ ,num_mel_bins=dimensions['n_mels'] ,d_model=dimensions['n_audio_state'] ,max_target_positions=dimensions['n_text_ctx'] ,encoder_layers=dimensions['n_audio_layer'] ,encoder_attention_heads=dimensions['n_audio_head'] ,decoder_layers=dimensions['n_text_layer'] ,decoder_attention_heads=dimensions['n_text_state'] ,max_source_positions=dimensions['n_audio_ctx'] ,) __UpperCamelCase : List[str] =WhisperForConditionalGeneration(a_ ) __UpperCamelCase , __UpperCamelCase : Union[str, Any] =model.model.load_state_dict(a_ ,strict=a_ ) if len(a_ ) > 0 and not set(a_ ) <= { "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: __UpperCamelCase : Optional[int] =make_linear_from_emb(model.model.decoder.embed_tokens ) else: __UpperCamelCase : List[str] =proj_out_weights model.save_pretrained(a_ ) if __name__ == "__main__": A_ :List[Any] = argparse.ArgumentParser() # # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Patht to the downloaded checkpoints''') parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') A_ :List[Any] = parser.parse_args() convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)
71
0
from collections.abc import Generator from math import sin def _A ( SCREAMING_SNAKE_CASE : bytes ): """simple docstring""" if len(SCREAMING_SNAKE_CASE ) != 32: raise ValueError("Input must be of length 32" ) a__ : Optional[Any] =b"" for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def _A ( SCREAMING_SNAKE_CASE : int ): """simple docstring""" if i < 0: raise ValueError("Input must be non-negative" ) a__ : Optional[Any] =format(SCREAMING_SNAKE_CASE , "08x" )[-8:] a__ : Tuple =b"" for i in [3, 2, 1, 0]: little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode("utf-8" ) return little_endian_hex def _A ( SCREAMING_SNAKE_CASE : bytes ): """simple docstring""" a__ : Any =b"" for char in message: bit_string += format(SCREAMING_SNAKE_CASE , "08b" ).encode("utf-8" ) a__ : Optional[Any] =format(len(SCREAMING_SNAKE_CASE ) , "064b" ).encode("utf-8" ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(SCREAMING_SNAKE_CASE ) % 512 != 448: bit_string += b"0" bit_string += to_little_endian(start_len[32:] ) + to_little_endian(start_len[:32] ) return bit_string def _A ( SCREAMING_SNAKE_CASE : bytes ): """simple docstring""" if len(SCREAMING_SNAKE_CASE ) % 512 != 0: raise ValueError("Input must have length that's a multiple of 512" ) for pos in range(0 , len(SCREAMING_SNAKE_CASE ) , 512 ): a__ : List[str] =bit_string[pos : pos + 512] a__ : Union[str, Any] =[] for i in range(0 , 512 , 32 ): block_words.append(int(to_little_endian(block[i : i + 32] ) , 2 ) ) yield block_words def _A ( SCREAMING_SNAKE_CASE : int ): """simple docstring""" if i < 0: raise ValueError("Input must be non-negative" ) a__ : Optional[int] =format(SCREAMING_SNAKE_CASE , "032b" ) a__ : Optional[int] ="" for c in i_str: new_str += "1" if c == "0" else "0" return int(SCREAMING_SNAKE_CASE , 2 ) def _A ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ): """simple docstring""" return (a + b) % 2**32 def _A ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ): """simple docstring""" if i < 0: raise ValueError("Input must be non-negative" ) if shift < 0: raise ValueError("Shift must be non-negative" ) return ((i << shift) ^ (i >> (32 - shift))) % 2**32 def _A ( SCREAMING_SNAKE_CASE : bytes ): """simple docstring""" a__ : str =preprocess(SCREAMING_SNAKE_CASE ) a__ : List[str] =[int(2**32 * abs(sin(i + 1 ) ) ) for i in range(64 )] # Starting states a__ : List[Any] =0x67_452_301 a__ : Optional[Any] =0xEF_CDA_B89 a__ : List[Any] =0x98_BAD_CFE a__ : Any =0x10_325_476 a__ : str =[ 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, ] # Process bit string in chunks, each with 16 32-char words for block_words in get_block_words(SCREAMING_SNAKE_CASE ): a__ : List[Any] =aa a__ : List[Any] =ba a__ : List[Any] =ca a__ : List[str] =da # Hash current chunk for i in range(64 ): if i <= 15: # f = (b & c) | (not_32(b) & d) # Alternate definition for f a__ : Tuple =d ^ (b & (c ^ d)) a__ : str =i elif i <= 31: # f = (d & b) | (not_32(d) & c) # Alternate definition for f a__ : Any =c ^ (d & (b ^ c)) a__ : str =(5 * i + 1) % 16 elif i <= 47: a__ : Dict =b ^ c ^ d a__ : Optional[Any] =(3 * i + 5) % 16 else: a__ : Optional[Any] =c ^ (b | not_aa(SCREAMING_SNAKE_CASE )) a__ : str =(7 * i) % 16 a__ : Optional[Any] =(f + a + added_consts[i] + block_words[g]) % 2**32 a__ : List[Any] =d a__ : List[str] =c a__ : int =b a__ : Dict =sum_aa(SCREAMING_SNAKE_CASE , left_rotate_aa(SCREAMING_SNAKE_CASE , shift_amounts[i] ) ) # Add hashed chunk to running total a__ : Union[str, Any] =sum_aa(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) a__ : List[Any] =sum_aa(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) a__ : int =sum_aa(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) a__ : List[str] =sum_aa(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) a__ : Tuple =reformat_hex(SCREAMING_SNAKE_CASE ) + reformat_hex(SCREAMING_SNAKE_CASE ) + reformat_hex(SCREAMING_SNAKE_CASE ) + reformat_hex(SCREAMING_SNAKE_CASE ) return digest if __name__ == "__main__": import doctest doctest.testmod()
95
import os from datetime import datetime as dt from github import Github A_ :str = [ '''good first issue''', '''feature request''', '''wip''', ] def A ( ) -> Any: __UpperCamelCase : Any =Github(os.environ['GITHUB_TOKEN'] ) __UpperCamelCase : Union[str, Any] =g.get_repo('huggingface/accelerate' ) __UpperCamelCase : Tuple =repo.get_issues(state='open' ) for issue in open_issues: __UpperCamelCase : List[Any] =sorted([comment for comment in issue.get_comments()] ,key=lambda a_ : i.created_at ,reverse=a_ ) __UpperCamelCase : str =comments[0] if len(a_ ) > 0 else None __UpperCamelCase : Any =dt.utcnow() __UpperCamelCase : List[str] =(current_time - issue.updated_at).days __UpperCamelCase : Union[str, Any] =(current_time - issue.created_at).days if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and days_since_updated > 7 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Close issue since it has been 7 days of inactivity since bot mention. issue.edit(state='closed' ) elif ( days_since_updated > 23 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Add stale comment issue.create_comment( 'This issue has been automatically marked as stale because it has not had ' 'recent activity. If you think this still needs to be addressed ' 'please comment on this thread.\n\nPlease note that issues that do not follow the ' '[contributing guidelines](https://github.com/huggingface/accelerate/blob/main/CONTRIBUTING.md) ' 'are likely to be ignored.' ) if __name__ == "__main__": main()
71
0
"""simple docstring""" import argparse import requests import torch from PIL import Image from transformers import SwinConfig, SwinForMaskedImageModeling, ViTImageProcessor def _snake_case ( lowercase__ ): _lowerCamelCase : Optional[int] = SwinConfig(image_size=192 ) if "base" in model_name: _lowerCamelCase : Optional[int] = 6 _lowerCamelCase : int = 128 _lowerCamelCase : List[Any] = (2, 2, 18, 2) _lowerCamelCase : Optional[int] = (4, 8, 16, 32) elif "large" in model_name: _lowerCamelCase : int = 12 _lowerCamelCase : str = 192 _lowerCamelCase : Tuple = (2, 2, 18, 2) _lowerCamelCase : Optional[int] = (6, 12, 24, 48) else: raise ValueError('Model not supported, only supports base and large variants' ) _lowerCamelCase : List[str] = window_size _lowerCamelCase : int = embed_dim _lowerCamelCase : Optional[Any] = depths _lowerCamelCase : Any = num_heads return config def _snake_case ( lowercase__ ): if "encoder.mask_token" in name: _lowerCamelCase : Dict = name.replace('encoder.mask_token' , 'embeddings.mask_token' ) if "encoder.patch_embed.proj" in name: _lowerCamelCase : Any = name.replace('encoder.patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "encoder.patch_embed.norm" in name: _lowerCamelCase : List[str] = name.replace('encoder.patch_embed.norm' , 'embeddings.norm' ) if "attn.proj" in name: _lowerCamelCase : Tuple = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: _lowerCamelCase : str = name.replace('attn' , 'attention.self' ) if "norm1" in name: _lowerCamelCase : Dict = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: _lowerCamelCase : Union[str, Any] = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: _lowerCamelCase : Optional[Any] = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: _lowerCamelCase : int = name.replace('mlp.fc2' , 'output.dense' ) if name == "encoder.norm.weight": _lowerCamelCase : int = 'layernorm.weight' if name == "encoder.norm.bias": _lowerCamelCase : List[str] = 'layernorm.bias' if "decoder" in name: pass else: _lowerCamelCase : Union[str, Any] = 'swin.' + name return name def _snake_case ( lowercase__ , lowercase__ ): for key in orig_state_dict.copy().keys(): _lowerCamelCase : Any = orig_state_dict.pop(lowercase__ ) if "attn_mask" in key: pass elif "qkv" in key: _lowerCamelCase : List[Any] = key.split('.' ) _lowerCamelCase : Dict = int(key_split[2] ) _lowerCamelCase : List[Any] = int(key_split[4] ) _lowerCamelCase : Optional[Any] = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: _lowerCamelCase : Optional[Any] = val[:dim, :] _lowerCamelCase : Any = val[ dim : dim * 2, : ] _lowerCamelCase : Optional[Any] = val[-dim:, :] else: _lowerCamelCase : str = val[ :dim ] _lowerCamelCase : Optional[int] = val[ dim : dim * 2 ] _lowerCamelCase : List[Any] = val[ -dim: ] else: _lowerCamelCase : Optional[int] = val return orig_state_dict def _snake_case ( lowercase__ , lowercase__ , lowercase__ , lowercase__ ): _lowerCamelCase : Optional[Any] = torch.load(lowercase__ , map_location='cpu' )['model'] _lowerCamelCase : Optional[Any] = get_swin_config(lowercase__ ) _lowerCamelCase : Optional[int] = SwinForMaskedImageModeling(lowercase__ ) model.eval() _lowerCamelCase : List[Any] = convert_state_dict(lowercase__ , lowercase__ ) model.load_state_dict(lowercase__ ) _lowerCamelCase : int = 'http://images.cocodataset.org/val2017/000000039769.jpg' _lowerCamelCase : int = ViTImageProcessor(size={'height': 192, 'width': 192} ) _lowerCamelCase : Union[str, Any] = Image.open(requests.get(lowercase__ , stream=lowercase__ ).raw ) _lowerCamelCase : Union[str, Any] = image_processor(images=lowercase__ , return_tensors='pt' ) with torch.no_grad(): _lowerCamelCase : List[Any] = model(**lowercase__ ).logits print(outputs.keys() ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: print(f'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(lowercase__ ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(lowercase__ ) if push_to_hub: print(f'''Pushing model and image processor for {model_name} to hub''' ) model.push_to_hub(f'''microsoft/{model_name}''' ) image_processor.push_to_hub(f'''microsoft/{model_name}''' ) if __name__ == "__main__": lowercase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""swin-base-simmim-window6-192""", type=str, choices=["""swin-base-simmim-window6-192""", """swin-large-simmim-window12-192"""], help="""Name of the Swin SimMIM model you'd like to convert.""", ) parser.add_argument( """--checkpoint_path""", default="""/Users/nielsrogge/Documents/SwinSimMIM/simmim_pretrain__swin_base__img192_window6__100ep.pth""", type=str, help="""Path to the original PyTorch checkpoint (.pth file).""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether or not to push the converted model to the 🤗 hub.""" ) lowercase__ = parser.parse_args() convert_swin_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
96
import re def A ( a_ ) -> bool: __UpperCamelCase : Any =re.compile( r'^(?:0|94|\+94|0{2}94)' r'7(0|1|2|4|5|6|7|8)' r'(-| |)' r'\d{7}$' ) return bool(re.search(a_ ,a_ ) ) if __name__ == "__main__": A_ :List[str] = '''0094702343221''' print(is_sri_lankan_phone_number(phone))
71
0
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType __snake_case = logging.get_logger(__name__) __snake_case = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class lowercase ( A__ ): """simple docstring""" _a = 'layoutlmv3' def __init__( self , UpperCamelCase_=50265 , UpperCamelCase_=768 , UpperCamelCase_=12 , UpperCamelCase_=12 , UpperCamelCase_=3072 , UpperCamelCase_="gelu" , UpperCamelCase_=0.1 , UpperCamelCase_=0.1 , UpperCamelCase_=512 , UpperCamelCase_=2 , UpperCamelCase_=0.02 , UpperCamelCase_=1e-5 , UpperCamelCase_=1 , UpperCamelCase_=0 , UpperCamelCase_=2 , UpperCamelCase_=1024 , UpperCamelCase_=128 , UpperCamelCase_=128 , UpperCamelCase_=True , UpperCamelCase_=32 , UpperCamelCase_=128 , UpperCamelCase_=64 , UpperCamelCase_=256 , UpperCamelCase_=True , UpperCamelCase_=True , UpperCamelCase_=True , UpperCamelCase_=224 , UpperCamelCase_=3 , UpperCamelCase_=16 , UpperCamelCase_=None , **UpperCamelCase_ , ): '''simple docstring''' super().__init__( vocab_size=UpperCamelCase_ , hidden_size=UpperCamelCase_ , num_hidden_layers=UpperCamelCase_ , num_attention_heads=UpperCamelCase_ , intermediate_size=UpperCamelCase_ , hidden_act=UpperCamelCase_ , hidden_dropout_prob=UpperCamelCase_ , attention_probs_dropout_prob=UpperCamelCase_ , max_position_embeddings=UpperCamelCase_ , type_vocab_size=UpperCamelCase_ , initializer_range=UpperCamelCase_ , layer_norm_eps=UpperCamelCase_ , pad_token_id=UpperCamelCase_ , bos_token_id=UpperCamelCase_ , eos_token_id=UpperCamelCase_ , **UpperCamelCase_ , ) UpperCamelCase__ :Tuple = max_ad_position_embeddings UpperCamelCase__ :List[Any] = coordinate_size UpperCamelCase__ :Union[str, Any] = shape_size UpperCamelCase__ :int = has_relative_attention_bias UpperCamelCase__ :Any = rel_pos_bins UpperCamelCase__ :Tuple = max_rel_pos UpperCamelCase__ :Optional[Any] = has_spatial_attention_bias UpperCamelCase__ :Union[str, Any] = rel_ad_pos_bins UpperCamelCase__ :Union[str, Any] = max_rel_ad_pos UpperCamelCase__ :Dict = text_embed UpperCamelCase__ :Optional[Any] = visual_embed UpperCamelCase__ :Dict = input_size UpperCamelCase__ :List[Any] = num_channels UpperCamelCase__ :Optional[int] = patch_size UpperCamelCase__ :str = classifier_dropout class lowercase ( A__ ): """simple docstring""" _a = version.parse('1.12' ) @property def lowerCAmelCase__ ( self ): '''simple docstring''' if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) else: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels'''}), ] ) @property def lowerCAmelCase__ ( self ): '''simple docstring''' return 1e-5 @property def lowerCAmelCase__ ( self ): '''simple docstring''' return 12 def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_ = -1 , UpperCamelCase_ = -1 , UpperCamelCase_ = False , UpperCamelCase_ = None , UpperCamelCase_ = 3 , UpperCamelCase_ = 40 , UpperCamelCase_ = 40 , ): '''simple docstring''' setattr(processor.image_processor , '''apply_ocr''' , UpperCamelCase_ ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX UpperCamelCase__ :Optional[int] = compute_effective_axis_dimension( UpperCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX UpperCamelCase__ :Union[str, Any] = processor.tokenizer.num_special_tokens_to_add(UpperCamelCase_ ) UpperCamelCase__ :Union[str, Any] = compute_effective_axis_dimension( UpperCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=UpperCamelCase_ ) # Generate dummy inputs according to compute batch and sequence UpperCamelCase__ :Union[str, Any] = [[''' '''.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes UpperCamelCase__ :str = [[[48, 84, 73, 128]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) UpperCamelCase__ :Optional[Any] = self._generate_dummy_images(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) UpperCamelCase__ :Any = dict( processor( UpperCamelCase_ , text=UpperCamelCase_ , boxes=UpperCamelCase_ , return_tensors=UpperCamelCase_ , ) ) return inputs
97
A_ :str = '''0.21.0''' from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
71
0
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class snake_case ( __UpperCAmelCase , unittest.TestCase ): """simple docstring""" snake_case__ = KandinskyInpaintPipeline snake_case__ = ["prompt", "image_embeds", "negative_image_embeds", "image", "mask_image"] snake_case__ = [ "prompt", "negative_prompt", "image_embeds", "negative_image_embeds", "image", "mask_image", ] snake_case__ = [ "generator", "height", "width", "latents", "guidance_scale", "negative_prompt", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] snake_case__ = False @property def __lowerCAmelCase ( self : Any ): return 32 @property def __lowerCAmelCase ( self : Union[str, Any] ): return 32 @property def __lowerCAmelCase ( self : Dict ): return self.time_input_dim @property def __lowerCAmelCase ( self : Dict ): return self.time_input_dim * 4 @property def __lowerCAmelCase ( self : Dict ): return 100 @property def __lowerCAmelCase ( self : int ): UpperCAmelCase__ = XLMRobertaTokenizerFast.from_pretrained('YiYiXu/tiny-random-mclip-base' ) return tokenizer @property def __lowerCAmelCase ( self : str ): torch.manual_seed(0 ) UpperCAmelCase__ = MCLIPConfig( numDims=self.cross_attention_dim ,transformerDimensions=self.text_embedder_hidden_size ,hidden_size=self.text_embedder_hidden_size ,intermediate_size=37 ,num_attention_heads=4 ,num_hidden_layers=5 ,vocab_size=1_005 ,) UpperCAmelCase__ = MultilingualCLIP(lowerCamelCase__ ) UpperCAmelCase__ = text_encoder.eval() return text_encoder @property def __lowerCAmelCase ( self : Optional[Any] ): torch.manual_seed(0 ) UpperCAmelCase__ = { 'in_channels': 9, # Out channels is double in channels because predicts mean and variance 'out_channels': 8, 'addition_embed_type': 'text_image', 'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'), 'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'), 'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn', 'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2), 'layers_per_block': 1, 'encoder_hid_dim': self.text_embedder_hidden_size, 'encoder_hid_dim_type': 'text_image_proj', 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': None, } UpperCAmelCase__ = UNetaDConditionModel(**lowerCamelCase__ ) return model @property def __lowerCAmelCase ( self : str ): return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __lowerCAmelCase ( self : str ): torch.manual_seed(0 ) UpperCAmelCase__ = VQModel(**self.dummy_movq_kwargs ) return model def __lowerCAmelCase ( self : int ): UpperCAmelCase__ = self.dummy_text_encoder UpperCAmelCase__ = self.dummy_tokenizer UpperCAmelCase__ = self.dummy_unet UpperCAmelCase__ = self.dummy_movq UpperCAmelCase__ = DDIMScheduler( num_train_timesteps=1_000 ,beta_schedule='linear' ,beta_start=0.0_0_0_8_5 ,beta_end=0.0_1_2 ,clip_sample=lowerCamelCase__ ,set_alpha_to_one=lowerCamelCase__ ,steps_offset=1 ,prediction_type='epsilon' ,thresholding=lowerCamelCase__ ,) UpperCAmelCase__ = { 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'unet': unet, 'scheduler': scheduler, 'movq': movq, } return components def __lowerCAmelCase ( self : str ,lowerCamelCase__ : Union[str, Any] ,lowerCamelCase__ : Dict=0 ): UpperCAmelCase__ = floats_tensor((1, self.cross_attention_dim) ,rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) UpperCAmelCase__ = floats_tensor((1, self.cross_attention_dim) ,rng=random.Random(seed + 1 ) ).to(lowerCamelCase__ ) # create init_image UpperCAmelCase__ = floats_tensor((1, 3, 64, 64) ,rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) UpperCAmelCase__ = image.cpu().permute(0 ,2 ,3 ,1 )[0] UpperCAmelCase__ = Image.fromarray(np.uinta(lowerCamelCase__ ) ).convert('RGB' ).resize((256, 256) ) # create mask UpperCAmelCase__ = np.ones((64, 64) ,dtype=np.floataa ) UpperCAmelCase__ = 0 if str(lowerCamelCase__ ).startswith('mps' ): UpperCAmelCase__ = torch.manual_seed(lowerCamelCase__ ) else: UpperCAmelCase__ = torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) UpperCAmelCase__ = { 'prompt': 'horse', 'image': init_image, 'mask_image': mask, 'image_embeds': image_embeds, 'negative_image_embeds': negative_image_embeds, 'generator': generator, 'height': 64, 'width': 64, 'num_inference_steps': 2, 'guidance_scale': 4.0, 'output_type': 'np', } return inputs def __lowerCAmelCase ( self : Optional[int] ): UpperCAmelCase__ = 'cpu' UpperCAmelCase__ = self.get_dummy_components() UpperCAmelCase__ = self.pipeline_class(**lowerCamelCase__ ) UpperCAmelCase__ = pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCAmelCase__ = pipe(**self.get_dummy_inputs(lowerCamelCase__ ) ) UpperCAmelCase__ = output.images UpperCAmelCase__ = pipe( **self.get_dummy_inputs(lowerCamelCase__ ) ,return_dict=lowerCamelCase__ ,)[0] UpperCAmelCase__ = image[0, -3:, -3:, -1] UpperCAmelCase__ = image_from_tuple[0, -3:, -3:, -1] print(f'''image.shape {image.shape}''' ) assert image.shape == (1, 64, 64, 3) UpperCAmelCase__ = np.array( [0.8_3_2_6_9_1_9, 0.7_3_7_9_0_4_6_7, 0.2_0_9_1_8_5_8_1, 0.9_3_0_9_6_1_2, 0.5_5_1_1_7_9_1, 0.4_3_7_1_3_3_2_8, 0.5_5_1_3_3_2_1, 0.4_9_9_2_2_9_3_4, 0.5_9_4_9_7_7_8_6] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), f''' expected_slice {expected_slice}, but got {image_slice.flatten()}''' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), f''' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}''' def __lowerCAmelCase ( self : Optional[Any] ): super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class snake_case ( unittest.TestCase ): """simple docstring""" def __lowerCAmelCase ( self : int ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCAmelCase ( self : Dict ): UpperCAmelCase__ = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy' ) UpperCAmelCase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/cat.png' ) UpperCAmelCase__ = np.ones((768, 768) ,dtype=np.floataa ) UpperCAmelCase__ = 0 UpperCAmelCase__ = 'a hat' UpperCAmelCase__ = KandinskyPriorPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-1-prior' ,torch_dtype=torch.floataa ) pipe_prior.to(lowerCamelCase__ ) UpperCAmelCase__ = KandinskyInpaintPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-1-inpaint' ,torch_dtype=torch.floataa ) UpperCAmelCase__ = pipeline.to(lowerCamelCase__ ) pipeline.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCAmelCase__ = torch.Generator(device='cpu' ).manual_seed(0 ) UpperCAmelCase__ , UpperCAmelCase__ = pipe_prior( lowerCamelCase__ ,generator=lowerCamelCase__ ,num_inference_steps=5 ,negative_prompt='' ,).to_tuple() UpperCAmelCase__ = pipeline( lowerCamelCase__ ,image=lowerCamelCase__ ,mask_image=lowerCamelCase__ ,image_embeds=lowerCamelCase__ ,negative_image_embeds=lowerCamelCase__ ,generator=lowerCamelCase__ ,num_inference_steps=100 ,height=768 ,width=768 ,output_type='np' ,) UpperCAmelCase__ = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(lowerCamelCase__ ,lowerCamelCase__ )
98
A_ :Union[str, Any] = { 0: '''0''', 1: '''1''', 2: '''2''', 3: '''3''', 4: '''4''', 5: '''5''', 6: '''6''', 7: '''7''', 8: '''8''', 9: '''9''', 10: '''a''', 11: '''b''', 12: '''c''', 13: '''d''', 14: '''e''', 15: '''f''', } def A ( a_ ) -> str: assert type(a_ ) in (int, float) and decimal == int(a_ ) __UpperCamelCase : Union[str, Any] =int(a_ ) __UpperCamelCase : List[str] ='' __UpperCamelCase : Optional[Any] =False if decimal < 0: __UpperCamelCase : Tuple =True decimal *= -1 while decimal > 0: __UpperCamelCase , __UpperCamelCase : Optional[Any] =divmod(a_ ,16 ) __UpperCamelCase : Tuple =values[remainder] + hexadecimal __UpperCamelCase : Dict ='0x' + hexadecimal if negative: __UpperCamelCase : int ='-' + hexadecimal return hexadecimal if __name__ == "__main__": import doctest doctest.testmod()
71
0
def A_ ( A__ , A__ ) -> int: return int((input_a, input_a).count(0 ) != 0 ) def A_ ( ) -> None: assert nand_gate(0 , 0 ) == 1 assert nand_gate(0 , 1 ) == 1 assert nand_gate(1 , 0 ) == 1 assert nand_gate(1 , 1 ) == 0 if __name__ == "__main__": print(nand_gate(0, 0)) print(nand_gate(0, 1)) print(nand_gate(1, 0)) print(nand_gate(1, 1))
99
import itertools import os from collections import Counter, defaultdict from concurrent.futures import ThreadPoolExecutor, as_completed import numpy as np import datasets from .execute import check_correctness A_ :List[str] = '''\ @misc{chen2021evaluating, title={Evaluating Large Language Models Trained on Code}, author={Mark Chen and Jerry Tworek and Heewoo Jun and Qiming Yuan \ and Henrique Ponde de Oliveira Pinto and Jared Kaplan and Harri Edwards \ and Yuri Burda and Nicholas Joseph and Greg Brockman and Alex Ray \ and Raul Puri and Gretchen Krueger and Michael Petrov and Heidy Khlaaf \ and Girish Sastry and Pamela Mishkin and Brooke Chan and Scott Gray \ and Nick Ryder and Mikhail Pavlov and Alethea Power and Lukasz Kaiser \ and Mohammad Bavarian and Clemens Winter and Philippe Tillet \ and Felipe Petroski Such and Dave Cummings and Matthias Plappert \ and Fotios Chantzis and Elizabeth Barnes and Ariel Herbert-Voss \ and William Hebgen Guss and Alex Nichol and Alex Paino and Nikolas Tezak \ and Jie Tang and Igor Babuschkin and Suchir Balaji and Shantanu Jain \ and William Saunders and Christopher Hesse and Andrew N. Carr \ and Jan Leike and Josh Achiam and Vedant Misra and Evan Morikawa \ and Alec Radford and Matthew Knight and Miles Brundage and Mira Murati \ and Katie Mayer and Peter Welinder and Bob McGrew and Dario Amodei \ and Sam McCandlish and Ilya Sutskever and Wojciech Zaremba}, year={2021}, eprint={2107.03374}, archivePrefix={arXiv}, primaryClass={cs.LG} } ''' A_ :Any = '''\ This metric implements the evaluation harness for the HumanEval problem solving dataset described in the paper "Evaluating Large Language Models Trained on Code" (https://arxiv.org/abs/2107.03374). ''' A_ :Tuple = ''' Calculates how good are predictions given some references, using certain scores Args: predictions: list of candidates to evaluate. Each candidates should be a list of strings with several code candidates to solve the problem. references: a list with a test for each prediction. Each test should evaluate the correctness of a code candidate. k: number of code candidates to consider in the evaluation (Default: [1, 10, 100]) num_workers: number of workers used to evaluate the canidate programs (Default: 4). timeout: Returns: pass_at_k: dict with pass rates for each k results: dict with granular results of each unittest Examples: >>> code_eval = datasets.load_metric("code_eval") >>> test_cases = ["assert add(2,3)==5"] >>> candidates = [["def add(a,b): return a*b", "def add(a, b): return a+b"]] >>> pass_at_k, results = code_eval.compute(references=test_cases, predictions=candidates, k=[1, 2]) >>> print(pass_at_k) {\'pass@1\': 0.5, \'pass@2\': 1.0} ''' A_ :List[str] = ''' ################################################################################ !!!WARNING!!! ################################################################################ The "code_eval" metric executes untrusted model-generated code in Python. Although it is highly unlikely that model-generated code will do something overtly malicious in response to this test suite, model-generated code may act destructively due to a lack of model capability or alignment. Users are strongly encouraged to sandbox this evaluation suite so that it does not perform destructive actions on their host or network. For more information on how OpenAI sandboxes its code, see the paper "Evaluating Large Language Models Trained on Code" (https://arxiv.org/abs/2107.03374). Once you have read this disclaimer and taken appropriate precautions, set the environment variable HF_ALLOW_CODE_EVAL="1". Within Python you can to this with: >>> import os >>> os.environ["HF_ALLOW_CODE_EVAL"] = "1" ################################################################################\ ''' A_ :Tuple = '''The MIT License Copyright (c) OpenAI (https://openai.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __A ( datasets.Metric ): """simple docstring""" def __lowercase ( self ): """simple docstring""" return datasets.MetricInfo( # This is the description that will appear on the metrics page. description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string' ) ), 'references': datasets.Value('string' ), } ) , homepage='https://github.com/openai/human-eval' , codebase_urls=['https://github.com/openai/human-eval'] , reference_urls=['https://github.com/openai/human-eval'] , license=_LICENSE , ) def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__=[1, 10, 100] , lowerCamelCase__=4 , lowerCamelCase__=3.0 ): """simple docstring""" if os.getenv('HF_ALLOW_CODE_EVAL' , 0 ) != "1": raise ValueError(_WARNING ) if os.name == "nt": raise NotImplementedError('This metric is currently not supported on Windows.' ) with ThreadPoolExecutor(max_workers=lowerCamelCase__ ) as executor: __UpperCamelCase : List[str] =[] __UpperCamelCase : Any =Counter() __UpperCamelCase : List[Any] =0 __UpperCamelCase : int =defaultdict(lowerCamelCase__ ) for task_id, (candidates, test_case) in enumerate(zip(lowerCamelCase__ , lowerCamelCase__ ) ): for candidate in candidates: __UpperCamelCase : str =candidate + '\n' + test_case __UpperCamelCase : Any =(test_program, timeout, task_id, completion_id[task_id]) __UpperCamelCase : Optional[Any] =executor.submit(lowerCamelCase__ , *lowerCamelCase__ ) futures.append(lowerCamelCase__ ) completion_id[task_id] += 1 n_samples += 1 for future in as_completed(lowerCamelCase__ ): __UpperCamelCase : str =future.result() results[result["task_id"]].append((result['completion_id'], result) ) __UpperCamelCase , __UpperCamelCase : int =[], [] for result in results.values(): result.sort() __UpperCamelCase : str =[r[1]['passed'] for r in result] total.append(len(lowerCamelCase__ ) ) correct.append(sum(lowerCamelCase__ ) ) __UpperCamelCase : Optional[int] =np.array(lowerCamelCase__ ) __UpperCamelCase : List[str] =np.array(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =k __UpperCamelCase : List[Any] ={f'pass@{k}': estimate_pass_at_k(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ).mean() for k in ks if (total >= k).all()} return pass_at_k, results def A ( a_ ,a_ ,a_ ) -> Optional[int]: def estimator(a_ ,a_ ,a_ ) -> float: if n - c < k: return 1.0 return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1 ,n + 1 ) ) if isinstance(a_ ,a_ ): __UpperCamelCase : Optional[int] =itertools.repeat(a_ ,len(a_ ) ) else: assert len(a_ ) == len(a_ ) __UpperCamelCase : List[Any] =iter(a_ ) return np.array([estimator(int(a_ ) ,int(a_ ) ,a_ ) for n, c in zip(a_ ,a_ )] )
71
0
"""simple docstring""" from __future__ import annotations __magic_name__ = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] __magic_name__ = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def _lowerCAmelCase ( UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = len(UpperCamelCase_ ) for i in range(UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = -1 for j in range(i + 1 , UpperCamelCase_ ): if arr[i] < arr[j]: __SCREAMING_SNAKE_CASE = arr[j] break result.append(UpperCamelCase_ ) return result def _lowerCAmelCase ( UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = [] for i, outer in enumerate(UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = -1 for inner in arr[i + 1 :]: if outer < inner: __SCREAMING_SNAKE_CASE = inner break result.append(UpperCamelCase_ ) return result def _lowerCAmelCase ( UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = len(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = [-1] * arr_size for index in reversed(range(UpperCamelCase_ ) ): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: __SCREAMING_SNAKE_CASE = stack[-1] stack.append(arr[index] ) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) __magic_name__ = ( "from __main__ import arr, next_greatest_element_slow, " "next_greatest_element_fast, next_greatest_element" ) print( "next_greatest_element_slow():", timeit("next_greatest_element_slow(arr)", setup=setup), ) print( "next_greatest_element_fast():", timeit("next_greatest_element_fast(arr)", setup=setup), ) print( " next_greatest_element():", timeit("next_greatest_element(arr)", setup=setup), )
100
import gc import random import tempfile import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMInverseScheduler, DDIMScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, StableDiffusionDiffEditPipeline, UNetaDConditionModel, ) from diffusers.utils import load_image, slow from diffusers.utils.testing_utils import enable_full_determinism, floats_tensor, require_torch_gpu, torch_device from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __A ( a , a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : Optional[Any] =StableDiffusionDiffEditPipeline UpperCamelCase__ : str =TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""height""", """width""", """image"""} | {"""image_latents"""} UpperCamelCase__ : Optional[Any] =TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS - {"""image"""} | {"""image_latents"""} UpperCamelCase__ : Dict =frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess UpperCamelCase__ : Any =frozenset([] ) def __lowercase ( self ): """simple docstring""" torch.manual_seed(0 ) __UpperCamelCase : Dict =UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=lowerCamelCase__ , ) __UpperCamelCase : List[str] =DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=lowerCamelCase__ , set_alpha_to_one=lowerCamelCase__ , ) __UpperCamelCase : Union[str, Any] =DDIMInverseScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=lowerCamelCase__ , set_alpha_to_zero=lowerCamelCase__ , ) torch.manual_seed(0 ) __UpperCamelCase : Optional[int] =AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) __UpperCamelCase : Tuple =CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act='gelu' , projection_dim=512 , ) __UpperCamelCase : Any =CLIPTextModel(lowerCamelCase__ ) __UpperCamelCase : int =CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) __UpperCamelCase : Union[str, Any] ={ 'unet': unet, 'scheduler': scheduler, 'inverse_scheduler': inverse_scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" __UpperCamelCase : int =floats_tensor((1, 16, 16) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) __UpperCamelCase : Any =floats_tensor((1, 2, 4, 16, 16) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : Any =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : Optional[int] =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Dict ={ 'prompt': 'a dog and a newt', 'mask_image': mask, 'image_latents': latents, 'generator': generator, 'num_inference_steps': 2, 'inpaint_strength': 1.0, 'guidance_scale': 6.0, 'output_type': 'numpy', } return inputs def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" __UpperCamelCase : Tuple =floats_tensor((1, 3, 32, 32) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) __UpperCamelCase : int =image.cpu().permute(0 , 2 , 3 , 1 )[0] __UpperCamelCase : Optional[Any] =Image.fromarray(np.uinta(lowerCamelCase__ ) ).convert('RGB' ) if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : List[Any] =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : Any =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Optional[int] ={ 'image': image, 'source_prompt': 'a cat and a frog', 'target_prompt': 'a dog and a newt', 'generator': generator, 'num_inference_steps': 2, 'num_maps_per_mask': 2, 'mask_encode_strength': 1.0, 'guidance_scale': 6.0, 'output_type': 'numpy', } return inputs def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" __UpperCamelCase : str =floats_tensor((1, 3, 32, 32) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) __UpperCamelCase : Any =image.cpu().permute(0 , 2 , 3 , 1 )[0] __UpperCamelCase : int =Image.fromarray(np.uinta(lowerCamelCase__ ) ).convert('RGB' ) if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : Any =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : int =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Optional[int] ={ 'image': image, 'prompt': 'a cat and a frog', 'generator': generator, 'num_inference_steps': 2, 'inpaint_strength': 1.0, 'guidance_scale': 6.0, 'decode_latents': True, 'output_type': 'numpy', } return inputs def __lowercase ( self ): """simple docstring""" if not hasattr(self.pipeline_class , '_optional_components' ): return __UpperCamelCase : Optional[Any] =self.get_dummy_components() __UpperCamelCase : List[str] =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) # set all optional components to None and update pipeline config accordingly for optional_component in pipe._optional_components: setattr(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) pipe.register_modules(**{optional_component: None for optional_component in pipe._optional_components} ) __UpperCamelCase : Union[str, Any] =self.get_dummy_inputs(lowerCamelCase__ ) __UpperCamelCase : List[Any] =pipe(**lowerCamelCase__ )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : Tuple =self.pipeline_class.from_pretrained(lowerCamelCase__ ) pipe_loaded.to(lowerCamelCase__ ) pipe_loaded.set_progress_bar_config(disable=lowerCamelCase__ ) for optional_component in pipe._optional_components: self.assertTrue( getattr(lowerCamelCase__ , lowerCamelCase__ ) is None , f'`{optional_component}` did not stay set to None after loading.' , ) __UpperCamelCase : str =self.get_dummy_inputs(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =pipe_loaded(**lowerCamelCase__ )[0] __UpperCamelCase : Tuple =np.abs(output - output_loaded ).max() self.assertLess(lowerCamelCase__ , 1E-4 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Any ='cpu' __UpperCamelCase : Union[str, Any] =self.get_dummy_components() __UpperCamelCase : Any =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : int =self.get_dummy_mask_inputs(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =pipe.generate_mask(**lowerCamelCase__ ) __UpperCamelCase : int =mask[0, -3:, -3:] self.assertEqual(mask.shape , (1, 16, 16) ) __UpperCamelCase : Tuple =np.array([0] * 9 ) __UpperCamelCase : str =np.abs(mask_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) self.assertEqual(mask[0, -3, -4] , 0 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : int ='cpu' __UpperCamelCase : Union[str, Any] =self.get_dummy_components() __UpperCamelCase : Optional[Any] =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Dict =self.get_dummy_inversion_inputs(lowerCamelCase__ ) __UpperCamelCase : List[Any] =pipe.invert(**lowerCamelCase__ ).images __UpperCamelCase : Optional[Any] =image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) __UpperCamelCase : List[str] =np.array( [0.5_150, 0.5_134, 0.5_043, 0.5_376, 0.4_694, 0.51_050, 0.5_015, 0.4_407, 0.4_799] , ) __UpperCamelCase : int =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) def __lowercase ( self ): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=5E-3 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] ='cpu' __UpperCamelCase : int =self.get_dummy_components() __UpperCamelCase : str ={'beta_start': 0.00_085, 'beta_end': 0.012, 'beta_schedule': 'scaled_linear'} __UpperCamelCase : str =DPMSolverMultistepScheduler(**lowerCamelCase__ ) __UpperCamelCase : Dict =DPMSolverMultistepInverseScheduler(**lowerCamelCase__ ) __UpperCamelCase : Any =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Tuple =self.get_dummy_inversion_inputs(lowerCamelCase__ ) __UpperCamelCase : str =pipe.invert(**lowerCamelCase__ ).images __UpperCamelCase : List[Any] =image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) __UpperCamelCase : List[str] =np.array( [0.5_150, 0.5_134, 0.5_043, 0.5_376, 0.4_694, 0.51_050, 0.5_015, 0.4_407, 0.4_799] , ) __UpperCamelCase : Optional[Any] =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) @require_torch_gpu @slow class __A ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @classmethod def __lowercase ( cls ): """simple docstring""" __UpperCamelCase : Optional[int] =load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/diffedit/fruit.png' ) __UpperCamelCase : Union[str, Any] =raw_image.convert('RGB' ).resize((768, 768) ) __UpperCamelCase : List[Any] =raw_image def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[int] =torch.manual_seed(0 ) __UpperCamelCase : Dict =StableDiffusionDiffEditPipeline.from_pretrained( 'stabilityai/stable-diffusion-2-1' , safety_checker=lowerCamelCase__ , torch_dtype=torch.floataa ) __UpperCamelCase : List[str] =DDIMScheduler.from_config(pipe.scheduler.config ) __UpperCamelCase : List[str] =DDIMInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : List[str] ='a bowl of fruit' __UpperCamelCase : Dict ='a bowl of pears' __UpperCamelCase : Tuple =pipe.generate_mask( image=self.raw_image , source_prompt=lowerCamelCase__ , target_prompt=lowerCamelCase__ , generator=lowerCamelCase__ , ) __UpperCamelCase : int =pipe.invert( prompt=lowerCamelCase__ , image=self.raw_image , inpaint_strength=0.7 , generator=lowerCamelCase__ ).latents __UpperCamelCase : Dict =pipe( prompt=lowerCamelCase__ , mask_image=lowerCamelCase__ , image_latents=lowerCamelCase__ , generator=lowerCamelCase__ , negative_prompt=lowerCamelCase__ , inpaint_strength=0.7 , output_type='numpy' , ).images[0] __UpperCamelCase : str =( np.array( load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/diffedit/pears.png' ).resize((768, 768) ) ) / 255 ) assert np.abs((expected_image - image).max() ) < 5E-1 def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Any =torch.manual_seed(0 ) __UpperCamelCase : List[Any] =StableDiffusionDiffEditPipeline.from_pretrained( 'stabilityai/stable-diffusion-2-1' , safety_checker=lowerCamelCase__ , torch_dtype=torch.floataa ) __UpperCamelCase : Optional[Any] =DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) __UpperCamelCase : Optional[int] =DPMSolverMultistepInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Optional[Any] ='a bowl of fruit' __UpperCamelCase : int ='a bowl of pears' __UpperCamelCase : str =pipe.generate_mask( image=self.raw_image , source_prompt=lowerCamelCase__ , target_prompt=lowerCamelCase__ , generator=lowerCamelCase__ , ) __UpperCamelCase : List[str] =pipe.invert( prompt=lowerCamelCase__ , image=self.raw_image , inpaint_strength=0.7 , generator=lowerCamelCase__ , num_inference_steps=25 , ).latents __UpperCamelCase : List[str] =pipe( prompt=lowerCamelCase__ , mask_image=lowerCamelCase__ , image_latents=lowerCamelCase__ , generator=lowerCamelCase__ , negative_prompt=lowerCamelCase__ , inpaint_strength=0.7 , num_inference_steps=25 , output_type='numpy' , ).images[0] __UpperCamelCase : Tuple =( np.array( load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/diffedit/pears.png' ).resize((768, 768) ) ) / 255 ) assert np.abs((expected_image - image).max() ) < 5E-1
71
0
import math import numpy as np import qiskit from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute def UpperCamelCase ( lowerCAmelCase__ = 3 ): '''simple docstring''' if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise TypeError('''number of qubits must be a integer.''' ) if number_of_qubits <= 0: raise ValueError('''number of qubits must be > 0.''' ) if math.floor(lowerCAmelCase__ ) != number_of_qubits: raise ValueError('''number of qubits must be exact integer.''' ) if number_of_qubits > 10: raise ValueError('''number of qubits too large to simulate(>10).''' ) lowercase = QuantumRegister(lowerCAmelCase__ , '''qr''' ) lowercase = ClassicalRegister(lowerCAmelCase__ , '''cr''' ) lowercase = QuantumCircuit(lowerCAmelCase__ , lowerCAmelCase__ ) lowercase = number_of_qubits for i in range(lowerCAmelCase__ ): quantum_circuit.h(number_of_qubits - i - 1 ) counter -= 1 for j in range(lowerCAmelCase__ ): quantum_circuit.cp(np.pi / 2 ** (counter - j) , lowerCAmelCase__ , lowerCAmelCase__ ) for k in range(number_of_qubits // 2 ): quantum_circuit.swap(lowerCAmelCase__ , number_of_qubits - k - 1 ) # measure all the qubits quantum_circuit.measure(lowerCAmelCase__ , lowerCAmelCase__ ) # simulate with 10000 shots lowercase = Aer.get_backend('''qasm_simulator''' ) lowercase = execute(lowerCAmelCase__ , lowerCAmelCase__ , shots=1_0000 ) return job.result().get_counts(lowerCAmelCase__ ) if __name__ == "__main__": print( F'Total count for quantum fourier transform state is: \ {quantum_fourier_transform(3)}' )
101
import random from .binary_exp_mod import bin_exp_mod def A ( a_ ,a_=1_000 ) -> Optional[Any]: if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd __UpperCamelCase : List[Any] =n - 1 __UpperCamelCase : Dict =0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) __UpperCamelCase : Optional[Any] =0 while count < prec: __UpperCamelCase : Dict =random.randint(2 ,n - 1 ) __UpperCamelCase : Optional[Any] =bin_exp_mod(a_ ,a_ ,a_ ) if b != 1: __UpperCamelCase : List[str] =True for _ in range(a_ ): if b == n - 1: __UpperCamelCase : Tuple =False break __UpperCamelCase : Dict =b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": A_ :str = abs(int(input('''Enter bound : ''').strip())) print('''Here\'s the list of primes:''') print(''', '''.join(str(i) for i in range(n + 1) if is_prime_big(i)))
71
0
"""simple docstring""" import random import unittest import numpy as np from diffusers import ( DPMSolverMultistepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, LMSDiscreteScheduler, OnnxStableDiffusionImgaImgPipeline, PNDMScheduler, ) from diffusers.utils import floats_tensor 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 _UpperCAmelCase ( __snake_case, unittest.TestCase ): '''simple docstring''' lowerCamelCase__ ='hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline' def SCREAMING_SNAKE_CASE (self , a_=0 ): '''simple docstring''' __snake_case : Optional[int] = floats_tensor((1, 3, 1_28, 1_28) , rng=random.Random(a_ ) ) __snake_case : str = np.random.RandomState(a_ ) __snake_case : Optional[int] = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''generator''': generator, '''num_inference_steps''': 3, '''strength''': 0.75, '''guidance_scale''': 7.5, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : List[str] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) pipe.set_progress_bar_config(disable=a_ ) __snake_case : List[str] = self.get_dummy_inputs() __snake_case : str = pipe(**a_ ).images __snake_case : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 1_28, 1_28, 3) __snake_case : int = np.array([0.6_9643, 0.5_8484, 0.5_0314, 0.5_8760, 0.5_5368, 0.5_9643, 0.5_1529, 0.4_1217, 0.4_9087] ) assert np.abs(image_slice - expected_slice ).max() < 1E-1 def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : List[Any] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) __snake_case : Union[str, Any] = PNDMScheduler.from_config(pipe.scheduler.config , skip_prk_steps=a_ ) pipe.set_progress_bar_config(disable=a_ ) __snake_case : Tuple = self.get_dummy_inputs() __snake_case : Optional[Any] = pipe(**a_ ).images __snake_case : Dict = image[0, -3:, -3:, -1] assert image.shape == (1, 1_28, 1_28, 3) __snake_case : Optional[int] = np.array([0.6_1737, 0.5_4642, 0.5_3183, 0.5_4465, 0.5_2742, 0.6_0525, 0.4_9969, 0.4_0655, 0.4_8154] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : List[str] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) __snake_case : Optional[Any] = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=a_ ) # warmup pass to apply optimizations __snake_case : Optional[int] = pipe(**self.get_dummy_inputs() ) __snake_case : int = self.get_dummy_inputs() __snake_case : int = pipe(**a_ ).images __snake_case : str = image[0, -3:, -3:, -1] assert image.shape == (1, 1_28, 1_28, 3) __snake_case : Union[str, Any] = np.array([0.5_2761, 0.5_9977, 0.4_9033, 0.4_9619, 0.5_4282, 0.5_0311, 0.4_7600, 0.4_0918, 0.4_5203] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : Optional[int] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) __snake_case : List[Any] = EulerDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=a_ ) __snake_case : List[str] = self.get_dummy_inputs() __snake_case : Tuple = pipe(**a_ ).images __snake_case : Tuple = image[0, -3:, -3:, -1] assert image.shape == (1, 1_28, 1_28, 3) __snake_case : str = np.array([0.5_2911, 0.6_0004, 0.4_9229, 0.4_9805, 0.5_4502, 0.5_0680, 0.4_7777, 0.4_1028, 0.4_5304] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : Optional[int] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) __snake_case : Union[str, Any] = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=a_ ) __snake_case : List[str] = self.get_dummy_inputs() __snake_case : List[Any] = pipe(**a_ ).images __snake_case : str = image[0, -3:, -3:, -1] assert image.shape == (1, 1_28, 1_28, 3) __snake_case : int = np.array([0.5_2911, 0.6_0004, 0.4_9229, 0.4_9805, 0.5_4502, 0.5_0680, 0.4_7777, 0.4_1028, 0.4_5304] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : List[str] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) __snake_case : Optional[int] = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=a_ ) __snake_case : Dict = self.get_dummy_inputs() __snake_case : Union[str, Any] = pipe(**a_ ).images __snake_case : List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 1_28, 1_28, 3) __snake_case : List[Any] = np.array([0.6_5331, 0.5_8277, 0.4_8204, 0.5_6059, 0.5_3665, 0.5_6235, 0.5_0969, 0.4_0009, 0.4_6552] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 @nightly @require_onnxruntime @require_torch_gpu class _UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @property def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : Any = ort.SessionOptions() __snake_case : Union[str, Any] = False return options def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : str = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/img2img/sketch-mountains-input.jpg''' ) __snake_case : Tuple = init_image.resize((7_68, 5_12) ) # using the PNDM scheduler by default __snake_case : List[Any] = OnnxStableDiffusionImgaImgPipeline.from_pretrained( '''CompVis/stable-diffusion-v1-4''' , revision='''onnx''' , safety_checker=a_ , feature_extractor=a_ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=a_ ) __snake_case : Any = '''A fantasy landscape, trending on artstation''' __snake_case : int = np.random.RandomState(0 ) __snake_case : int = pipe( prompt=a_ , image=a_ , strength=0.75 , guidance_scale=7.5 , num_inference_steps=10 , generator=a_ , output_type='''np''' , ) __snake_case : Optional[int] = output.images __snake_case : Optional[int] = images[0, 2_55:2_58, 3_83:3_86, -1] assert images.shape == (1, 5_12, 7_68, 3) __snake_case : Optional[int] = np.array([0.4909, 0.5059, 0.5372, 0.4623, 0.4876, 0.5049, 0.4820, 0.4956, 0.5019] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2E-2 def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : List[str] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/img2img/sketch-mountains-input.jpg''' ) __snake_case : Tuple = init_image.resize((7_68, 5_12) ) __snake_case : Dict = LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-v1-5''' , subfolder='''scheduler''' , revision='''onnx''' ) __snake_case : Union[str, Any] = OnnxStableDiffusionImgaImgPipeline.from_pretrained( '''runwayml/stable-diffusion-v1-5''' , revision='''onnx''' , scheduler=a_ , safety_checker=a_ , feature_extractor=a_ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=a_ ) __snake_case : Optional[Any] = '''A fantasy landscape, trending on artstation''' __snake_case : Union[str, Any] = np.random.RandomState(0 ) __snake_case : int = pipe( prompt=a_ , image=a_ , strength=0.75 , guidance_scale=7.5 , num_inference_steps=20 , generator=a_ , output_type='''np''' , ) __snake_case : Optional[Any] = output.images __snake_case : Optional[Any] = images[0, 2_55:2_58, 3_83:3_86, -1] assert images.shape == (1, 5_12, 7_68, 3) __snake_case : Tuple = np.array([0.8043, 0.926, 0.9581, 0.8119, 0.8954, 0.913, 0.7209, 0.7463, 0.7431] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2E-2
102
from torch import nn class __A ( nn.Module ): """simple docstring""" def __init__( self , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" super().__init__() __UpperCamelCase : Dict =class_size __UpperCamelCase : Any =embed_size # self.mlp1 = nn.Linear(embed_size, embed_size) # self.mlp2 = (nn.Linear(embed_size, class_size)) __UpperCamelCase : Any =nn.Linear(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : List[Any] =self.mlp(lowerCamelCase__ ) return logits
71
0
import numpy as np import torch from imwatermark import WatermarkEncoder # Copied from https://github.com/Stability-AI/generative-models/blob/613af104c6b85184091d42d374fef420eddb356d/scripts/demo/streamlit_helpers.py#L66 A__ : List[str] = 0b10_11_00_11_11_10_11_00_10_01_00_00_01_11_10_11_10_11_00_01_10_01_11_10 # bin(x)[2:] gives bits of x as str, use int to convert them to 0/1 A__ : List[str] = [int(bit) for bit in bin(WATERMARK_MESSAGE)[2:]] class __snake_case : def __init__( self : List[Any]): lowerCAmelCase_ : Tuple = WATERMARK_BITS lowerCAmelCase_ : int = WatermarkEncoder() self.encoder.set_watermark('''bits''' , self.watermark) def UpperCAmelCase__ ( self : Optional[Any] , A_ : torch.FloatTensor): # can't encode images that are smaller than 256 if images.shape[-1] < 2_5_6: return images lowerCAmelCase_ : Optional[Any] = (2_5_5 * (images / 2 + 0.5)).cpu().permute(0 , 2 , 3 , 1).float().numpy() lowerCAmelCase_ : Any = [self.encoder.encode(A_ , '''dwtDct''') for image in images] lowerCAmelCase_ : Optional[Any] = torch.from_numpy(np.array(A_)).permute(0 , 3 , 1 , 2) lowerCAmelCase_ : Optional[Any] = torch.clamp(2 * (images / 2_5_5 - 0.5) , min=-1.0 , max=1.0) return images
103
def A ( a_ ,a_ ,a_ ) -> int: def update_area_of_max_square(a_ ,a_ ) -> int: # BASE CASE if row >= rows or col >= cols: return 0 __UpperCamelCase : Optional[int] =update_area_of_max_square(a_ ,col + 1 ) __UpperCamelCase : List[str] =update_area_of_max_square(row + 1 ,col + 1 ) __UpperCamelCase : List[Any] =update_area_of_max_square(row + 1 ,a_ ) if mat[row][col]: __UpperCamelCase : Optional[Any] =1 + min([right, diagonal, down] ) __UpperCamelCase : Dict =max(largest_square_area[0] ,a_ ) return sub_problem_sol else: return 0 __UpperCamelCase : Union[str, Any] =[0] update_area_of_max_square(0 ,0 ) return largest_square_area[0] def A ( a_ ,a_ ,a_ ) -> int: def update_area_of_max_square_using_dp_array( a_ ,a_ ,a_ ) -> int: if row >= rows or col >= cols: return 0 if dp_array[row][col] != -1: return dp_array[row][col] __UpperCamelCase : Tuple =update_area_of_max_square_using_dp_array(a_ ,col + 1 ,a_ ) __UpperCamelCase : Optional[int] =update_area_of_max_square_using_dp_array(row + 1 ,col + 1 ,a_ ) __UpperCamelCase : Any =update_area_of_max_square_using_dp_array(row + 1 ,a_ ,a_ ) if mat[row][col]: __UpperCamelCase : Optional[Any] =1 + min([right, diagonal, down] ) __UpperCamelCase : str =max(largest_square_area[0] ,a_ ) __UpperCamelCase : Any =sub_problem_sol return sub_problem_sol else: return 0 __UpperCamelCase : Tuple =[0] __UpperCamelCase : List[Any] =[[-1] * cols for _ in range(a_ )] update_area_of_max_square_using_dp_array(0 ,0 ,a_ ) return largest_square_area[0] def A ( a_ ,a_ ,a_ ) -> int: __UpperCamelCase : Dict =[[0] * (cols + 1) for _ in range(rows + 1 )] __UpperCamelCase : int =0 for row in range(rows - 1 ,-1 ,-1 ): for col in range(cols - 1 ,-1 ,-1 ): __UpperCamelCase : Optional[Any] =dp_array[row][col + 1] __UpperCamelCase : int =dp_array[row + 1][col + 1] __UpperCamelCase : Tuple =dp_array[row + 1][col] if mat[row][col] == 1: __UpperCamelCase : Tuple =1 + min(a_ ,a_ ,a_ ) __UpperCamelCase : Any =max(dp_array[row][col] ,a_ ) else: __UpperCamelCase : Dict =0 return largest_square_area def A ( a_ ,a_ ,a_ ) -> int: __UpperCamelCase : Any =[0] * (cols + 1) __UpperCamelCase : List[Any] =[0] * (cols + 1) __UpperCamelCase : Tuple =0 for row in range(rows - 1 ,-1 ,-1 ): for col in range(cols - 1 ,-1 ,-1 ): __UpperCamelCase : Any =current_row[col + 1] __UpperCamelCase : Optional[Any] =next_row[col + 1] __UpperCamelCase : Union[str, Any] =next_row[col] if mat[row][col] == 1: __UpperCamelCase : Any =1 + min(a_ ,a_ ,a_ ) __UpperCamelCase : Optional[int] =max(current_row[col] ,a_ ) else: __UpperCamelCase : List[str] =0 __UpperCamelCase : Optional[Any] =current_row return largest_square_area if __name__ == "__main__": import doctest doctest.testmod() print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))
71
0
'''simple docstring''' import os from argparse import ArgumentParser, Namespace from ..data import SingleSentenceClassificationProcessor as Processor from ..pipelines import TextClassificationPipeline from ..utils import is_tf_available, is_torch_available, logging from . import BaseTransformersCLICommand if not is_tf_available() and not is_torch_available(): raise RuntimeError('''At least one of PyTorch or TensorFlow 2.0+ should be installed to use CLI training''') # TF training parameters lowerCAmelCase__ = False lowerCAmelCase__ = False def _A ( A__ ): """simple docstring""" return TrainCommand(A__ ) class lowercase_ (lowerCamelCase__ ): """simple docstring""" @staticmethod def SCREAMING_SNAKE_CASE ( lowercase__ : ArgumentParser ): __lowercase = parser.add_parser('''train''' ,help='''CLI tool to train a model on a task.''' ) train_parser.add_argument( '''--train_data''' ,type=lowercase__ ,required=lowercase__ ,help='''path to train (and optionally evaluation) dataset as a csv with tab separated labels and sentences.''' ,) train_parser.add_argument( '''--column_label''' ,type=lowercase__ ,default=0 ,help='''Column of the dataset csv file with example labels.''' ) train_parser.add_argument( '''--column_text''' ,type=lowercase__ ,default=1 ,help='''Column of the dataset csv file with example texts.''' ) train_parser.add_argument( '''--column_id''' ,type=lowercase__ ,default=2 ,help='''Column of the dataset csv file with example ids.''' ) train_parser.add_argument( '''--skip_first_row''' ,action='''store_true''' ,help='''Skip the first row of the csv file (headers).''' ) train_parser.add_argument('''--validation_data''' ,type=lowercase__ ,default='''''' ,help='''path to validation dataset.''' ) train_parser.add_argument( '''--validation_split''' ,type=lowercase__ ,default=0.1 ,help='''if validation dataset is not provided, fraction of train dataset to use as validation dataset.''' ,) train_parser.add_argument('''--output''' ,type=lowercase__ ,default='''./''' ,help='''path to saved the trained model.''' ) train_parser.add_argument( '''--task''' ,type=lowercase__ ,default='''text_classification''' ,help='''Task to train the model on.''' ) train_parser.add_argument( '''--model''' ,type=lowercase__ ,default='''bert-base-uncased''' ,help='''Model\'s name or path to stored model.''' ) train_parser.add_argument('''--train_batch_size''' ,type=lowercase__ ,default=3_2 ,help='''Batch size for training.''' ) train_parser.add_argument('''--valid_batch_size''' ,type=lowercase__ ,default=6_4 ,help='''Batch size for validation.''' ) train_parser.add_argument('''--learning_rate''' ,type=lowercase__ ,default=3e-5 ,help='''Learning rate.''' ) train_parser.add_argument('''--adam_epsilon''' ,type=lowercase__ ,default=1e-0_8 ,help='''Epsilon for Adam optimizer.''' ) train_parser.set_defaults(func=lowercase__ ) def __init__( self : int ,lowercase__ : Namespace ): __lowercase = logging.get_logger('''transformers-cli/training''' ) __lowercase = '''tf''' if is_tf_available() else '''torch''' os.makedirs(args.output ,exist_ok=lowercase__ ) __lowercase = args.output __lowercase = args.column_label __lowercase = args.column_text __lowercase = args.column_id self.logger.info(F"Loading {args.task} pipeline for {args.model}" ) if args.task == "text_classification": __lowercase = TextClassificationPipeline.from_pretrained(args.model ) elif args.task == "token_classification": raise NotImplementedError elif args.task == "question_answering": raise NotImplementedError self.logger.info(F"Loading dataset from {args.train_data}" ) __lowercase = Processor.create_from_csv( args.train_data ,column_label=args.column_label ,column_text=args.column_text ,column_id=args.column_id ,skip_first_row=args.skip_first_row ,) __lowercase = None if args.validation_data: self.logger.info(F"Loading validation dataset from {args.validation_data}" ) __lowercase = Processor.create_from_csv( args.validation_data ,column_label=args.column_label ,column_text=args.column_text ,column_id=args.column_id ,skip_first_row=args.skip_first_row ,) __lowercase = args.validation_split __lowercase = args.train_batch_size __lowercase = args.valid_batch_size __lowercase = args.learning_rate __lowercase = args.adam_epsilon def SCREAMING_SNAKE_CASE ( self : Any ): if self.framework == "tf": return self.run_tf() return self.run_torch() def SCREAMING_SNAKE_CASE ( self : int ): raise NotImplementedError def SCREAMING_SNAKE_CASE ( self : Tuple ): self.pipeline.fit( self.train_dataset ,validation_data=self.valid_dataset ,validation_split=self.validation_split ,learning_rate=self.learning_rate ,adam_epsilon=self.adam_epsilon ,train_batch_size=self.train_batch_size ,valid_batch_size=self.valid_batch_size ,) # Save trained pipeline self.pipeline.save_pretrained(self.output )
104
def A ( a_ ) -> int: __UpperCamelCase : Any =len(a_ ) while cur > 1: # Find the maximum number in arr __UpperCamelCase : Any =arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi __UpperCamelCase : Any =arr[mi::-1] + arr[mi + 1 : len(a_ )] # Reverse whole list __UpperCamelCase : str =arr[cur - 1 :: -1] + arr[cur : len(a_ )] cur -= 1 return arr if __name__ == "__main__": A_ :Dict = input('''Enter numbers separated by a comma:\n''').strip() A_ :Any = [int(item) for item in user_input.split(''',''')] print(pancake_sort(unsorted))
71
0
"""simple docstring""" import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process a : List[str] = logging.getLogger(__name__) def _SCREAMING_SNAKE_CASE ( _lowercase : Union[str, Any] , _lowercase : Union[str, Any] ) ->int: '''simple docstring''' return (preds == labels).mean() @dataclass class __UpperCamelCase : lowerCamelCase : str =field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) lowerCamelCase : Optional[str] =field( default=a__ , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) lowerCamelCase : Optional[str] =field( default=a__ , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) lowerCamelCase : Optional[str] =field( default=a__ , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) @dataclass class __UpperCamelCase : lowerCamelCase : str =field(metadata={"""help""": """The name of the task to train on: """ + """, """.join(processors.keys() )} ) lowerCamelCase : str =field(metadata={"""help""": """Should contain the data files for the task."""} ) lowerCamelCase : int =field( default=128 , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) lowerCamelCase : bool =field( default=a__ , metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) def _SCREAMING_SNAKE_CASE ( ) ->str: '''simple docstring''' a : Any = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) a, a, a : Union[str, Any] = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. Use""" " --overwrite_output_dir to overcome." ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s" , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info("Training/evaluation parameters %s" , _lowercase ) # Set seed set_seed(training_args.seed ) try: a : Union[str, Any] = processors[data_args.task_name]() a : Any = processor.get_labels() a : Optional[Any] = len(_lowercase ) except KeyError: raise ValueError("Task not found: %s" % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. a : List[Any] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=_lowercase , finetuning_task=data_args.task_name , cache_dir=model_args.cache_dir , ) a : Optional[int] = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) a : str = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool(".ckpt" in model_args.model_name_or_path ) , config=_lowercase , cache_dir=model_args.cache_dir , ) # Get datasets a : str = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=_lowercase , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) a : Any = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=_lowercase , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.dev , ) if training_args.do_eval else None ) def compute_metrics(_lowercase : EvalPrediction ) -> Dict: a : int = np.argmax(p.predictions , axis=1 ) return {"acc": simple_accuracy(_lowercase , p.label_ids )} # Data collator a : List[Any] = DataCollatorWithPadding(_lowercase , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer a : Dict = Trainer( model=_lowercase , args=_lowercase , train_dataset=_lowercase , eval_dataset=_lowercase , compute_metrics=_lowercase , data_collator=_lowercase , ) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation a : Dict = {} if training_args.do_eval: logger.info("*** Evaluate ***" ) a : str = trainer.evaluate() a : List[str] = os.path.join(training_args.output_dir , "eval_results.txt" ) if trainer.is_world_master(): with open(_lowercase , "w" ) as writer: logger.info("***** Eval results *****" ) for key, value in result.items(): logger.info(" %s = %s" , _lowercase , _lowercase ) writer.write("%s = %s\n" % (key, value) ) results.update(_lowercase ) return results def _SCREAMING_SNAKE_CASE ( _lowercase : Union[str, Any] ) ->Any: '''simple docstring''' main() if __name__ == "__main__": main()
105
import random def A ( a_ ,a_ ,a_ = False ) -> dict: __UpperCamelCase : dict ={i: [] for i in range(a_ )} # if probability is greater or equal than 1, then generate a complete graph if probability >= 1: return complete_graph(a_ ) # if probability is lower or equal than 0, then return a graph without edges if probability <= 0: return graph # for each couple of nodes, add an edge from u to v # if the number randomly generated is greater than probability probability for i in range(a_ ): for j in range(i + 1 ,a_ ): if random.random() < probability: graph[i].append(a_ ) if not directed: # if the graph is undirected, add an edge in from j to i, either graph[j].append(a_ ) return graph def A ( a_ ) -> dict: return { i: [j for j in range(a_ ) if i != j] for i in range(a_ ) } if __name__ == "__main__": import doctest doctest.testmod()
71
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available, is_vision_available, ) __UpperCamelCase : Union[str, Any] = {'''configuration_beit''': ['''BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BeitConfig''', '''BeitOnnxConfig''']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : int = ['''BeitFeatureExtractor'''] __UpperCamelCase : Optional[int] = ['''BeitImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : int = [ '''BEIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BeitForImageClassification''', '''BeitForMaskedImageModeling''', '''BeitForSemanticSegmentation''', '''BeitModel''', '''BeitPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Optional[int] = [ '''FlaxBeitForImageClassification''', '''FlaxBeitForMaskedImageModeling''', '''FlaxBeitModel''', '''FlaxBeitPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_beit import BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, BeitConfig, BeitOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_beit import BeitFeatureExtractor from .image_processing_beit import BeitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_beit import ( BEIT_PRETRAINED_MODEL_ARCHIVE_LIST, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, BeitPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_beit import ( FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel, FlaxBeitPreTrainedModel, ) else: import sys __UpperCamelCase : Optional[Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
106
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 __A ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: __UpperCamelCase : List[str] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModel.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[Any] =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"]: __UpperCamelCase : Any =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForPreTraining.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =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]: __UpperCamelCase : Tuple =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModelForCausalLM.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Tuple =TFAutoModelForCausalLM.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[int] =AutoModelForCausalLM.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Union[str, Any] =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]: __UpperCamelCase : int =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Dict =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[Any] =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]: __UpperCamelCase : List[Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Tuple =TFAutoModelForMaskedLM.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =AutoModelForMaskedLM.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Optional[Any] =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]: __UpperCamelCase : Any =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[Any] =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : List[str] =TFAutoModelForSeqaSeqLM.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Dict =AutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : List[Any] =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"]: __UpperCamelCase : str =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =TFAutoModelForSequenceClassification.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =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"]: __UpperCamelCase : List[Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Tuple =TFAutoModelForQuestionAnswering.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =AutoModelForQuestionAnswering.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : int =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) __UpperCamelCase : str =AutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Union[str, Any] =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) __UpperCamelCase : int =AutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 )
71
0
from __future__ import annotations from scipy.special import comb # type: ignore class snake_case__ : """simple docstring""" def __init__( self : Any , __lowerCamelCase : list[tuple[float, float]] ) -> Tuple: a = list_of_points # Degree determines the flexibility of the curve. # Degree = 1 will produce a straight line. a = len(__lowerCamelCase ) - 1 def __UpperCAmelCase ( self : Union[str, Any] , __lowerCamelCase : float ) -> list[float]: assert 0 <= t <= 1, "Time t must be between 0 and 1." a = [] for i in range(len(self.list_of_points ) ): # basis function for each i output_values.append( comb(self.degree , __lowerCamelCase ) * ((1 - t) ** (self.degree - i)) * (t**i) ) # the basis must sum up to 1 for it to produce a valid Bezier curve. assert round(sum(__lowerCamelCase ) , 5 ) == 1 return output_values def __UpperCAmelCase ( self : Optional[Any] , __lowerCamelCase : float ) -> tuple[float, float]: assert 0 <= t <= 1, "Time t must be between 0 and 1." a = self.basis_function(__lowerCamelCase ) a = 0.0 a = 0.0 for i in range(len(self.list_of_points ) ): # For all points, sum up the product of i-th basis function and i-th point. x += basis_function[i] * self.list_of_points[i][0] y += basis_function[i] * self.list_of_points[i][1] return (x, y) def __UpperCAmelCase ( self : Optional[Any] , __lowerCamelCase : float = 0.01 ) -> List[str]: from matplotlib import pyplot as plt # type: ignore a = [] # x coordinates of points to plot a = [] # y coordinates of points to plot a = 0.0 while t <= 1: a = self.bezier_curve_function(__lowerCamelCase ) to_plot_x.append(value[0] ) to_plot_y.append(value[1] ) t += step_size a = [i[0] for i in self.list_of_points] a = [i[1] for i in self.list_of_points] plt.plot( __lowerCamelCase , __lowerCamelCase , color="blue" , label="Curve of Degree " + str(self.degree ) , ) plt.scatter(__lowerCamelCase , __lowerCamelCase , color="red" , label="Control Points" ) plt.legend() plt.show() if __name__ == "__main__": import doctest doctest.testmod() BezierCurve([(1, 2), (3, 5)]).plot_curve() # degree 1 BezierCurve([(0, 0), (5, 5), (5, 0)]).plot_curve() # degree 2 BezierCurve([(0, 0), (5, 5), (5, 0), (2.5, -2.5)]).plot_curve() # degree 3
107
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A_ :Tuple = { '''configuration_x_clip''': [ '''XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XCLIPConfig''', '''XCLIPTextConfig''', '''XCLIPVisionConfig''', ], '''processing_x_clip''': ['''XCLIPProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ :Union[str, Any] = [ '''XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XCLIPModel''', '''XCLIPPreTrainedModel''', '''XCLIPTextModel''', '''XCLIPVisionModel''', ] if TYPE_CHECKING: from .configuration_x_clip import ( XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, XCLIPConfig, XCLIPTextConfig, XCLIPVisionConfig, ) from .processing_x_clip import XCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_x_clip import ( XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, XCLIPModel, XCLIPPreTrainedModel, XCLIPTextModel, XCLIPVisionModel, ) else: import sys A_ :Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
71
0
"""simple docstring""" from __future__ import annotations from collections import namedtuple from dataclasses import dataclass @dataclass class SCREAMING_SNAKE_CASE__ : """simple docstring""" a : int a : TreeNode | None =None a : TreeNode | None =None lowerCAmelCase__ = namedtuple('''CoinsDistribResult''', '''moves excess''') def a__ ( SCREAMING_SNAKE_CASE : TreeNode | None ): '''simple docstring''' if root is None: return 0 # Validation def count_nodes(SCREAMING_SNAKE_CASE : TreeNode | None ) -> int: if node is None: return 0 return count_nodes(node.left ) + count_nodes(node.right ) + 1 def count_coins(SCREAMING_SNAKE_CASE : TreeNode | None ) -> int: if node is None: return 0 return count_coins(node.left ) + count_coins(node.right ) + node.data if count_nodes(SCREAMING_SNAKE_CASE ) != count_coins(SCREAMING_SNAKE_CASE ): raise ValueError("The nodes number should be same as the number of coins" ) # Main calculation def get_distrib(SCREAMING_SNAKE_CASE : TreeNode | None ) -> CoinsDistribResult: if node is None: return CoinsDistribResult(0 , 1 ) lowerCAmelCase , lowerCAmelCase : List[str] = get_distrib(node.left ) lowerCAmelCase , lowerCAmelCase : List[str] = get_distrib(node.right ) lowerCAmelCase : Union[str, Any] = 1 - left_distrib_excess lowerCAmelCase : List[Any] = 1 - right_distrib_excess lowerCAmelCase : int = ( left_distrib_moves + right_distrib_moves + abs(SCREAMING_SNAKE_CASE ) + abs(SCREAMING_SNAKE_CASE ) ) lowerCAmelCase : int = node.data - coins_to_left - coins_to_right return CoinsDistribResult(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) return get_distrib(SCREAMING_SNAKE_CASE )[0] if __name__ == "__main__": import doctest doctest.testmod()
108
from ...configuration_utils import PretrainedConfig from ...utils import logging A_ :Any = logging.get_logger(__name__) A_ :int = { '''sayakpaul/vit-msn-base''': '''https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json''', # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class __A ( a ): """simple docstring""" UpperCamelCase__ : Optional[int] ="""vit_msn""" def __init__( self , lowerCamelCase__=768 , lowerCamelCase__=12 , lowerCamelCase__=12 , lowerCamelCase__=3072 , lowerCamelCase__="gelu" , lowerCamelCase__=0.0 , lowerCamelCase__=0.0 , lowerCamelCase__=0.02 , lowerCamelCase__=1E-06 , lowerCamelCase__=224 , lowerCamelCase__=16 , lowerCamelCase__=3 , lowerCamelCase__=True , **lowerCamelCase__ , ): """simple docstring""" super().__init__(**lowerCamelCase__ ) __UpperCamelCase : int =hidden_size __UpperCamelCase : List[Any] =num_hidden_layers __UpperCamelCase : Union[str, Any] =num_attention_heads __UpperCamelCase : List[str] =intermediate_size __UpperCamelCase : Union[str, Any] =hidden_act __UpperCamelCase : str =hidden_dropout_prob __UpperCamelCase : Union[str, Any] =attention_probs_dropout_prob __UpperCamelCase : Union[str, Any] =initializer_range __UpperCamelCase : Tuple =layer_norm_eps __UpperCamelCase : Optional[Any] =image_size __UpperCamelCase : Optional[int] =patch_size __UpperCamelCase : Any =num_channels __UpperCamelCase : str =qkv_bias
71
0
"""simple docstring""" from ... import PretrainedConfig A: Any = { "sijunhe/nezha-cn-base": "https://huggingface.co/sijunhe/nezha-cn-base/resolve/main/config.json", } class SCREAMING_SNAKE_CASE__ ( UpperCAmelCase__ ): __lowerCAmelCase : Optional[Any] = NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP __lowerCAmelCase : Tuple = 'nezha' def __init__( self , _SCREAMING_SNAKE_CASE=21128 , _SCREAMING_SNAKE_CASE=768 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=3072 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=512 , _SCREAMING_SNAKE_CASE=64 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=1E-12 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=True , **_SCREAMING_SNAKE_CASE , ) -> Any: '''simple docstring''' super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE , bos_token_id=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) UpperCAmelCase : List[str] = vocab_size UpperCAmelCase : Optional[Any] = hidden_size UpperCAmelCase : List[Any] = num_hidden_layers UpperCAmelCase : Union[str, Any] = num_attention_heads UpperCAmelCase : List[str] = hidden_act UpperCAmelCase : Tuple = intermediate_size UpperCAmelCase : Optional[int] = hidden_dropout_prob UpperCAmelCase : Optional[int] = attention_probs_dropout_prob UpperCAmelCase : Any = max_position_embeddings UpperCAmelCase : Any = max_relative_position UpperCAmelCase : Tuple = type_vocab_size UpperCAmelCase : str = initializer_range UpperCAmelCase : Optional[int] = layer_norm_eps UpperCAmelCase : str = classifier_dropout UpperCAmelCase : List[str] = use_cache
109
import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __A ( a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : str =DDIMPipeline UpperCamelCase__ : List[Any] =UNCONDITIONAL_IMAGE_GENERATION_PARAMS UpperCamelCase__ : Tuple =PipelineTesterMixin.required_optional_params - { """num_images_per_prompt""", """latents""", """callback""", """callback_steps""", } UpperCamelCase__ : Tuple =UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS UpperCamelCase__ : Any =False def __lowercase ( self ): """simple docstring""" torch.manual_seed(0 ) __UpperCamelCase : Optional[int] =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') , ) __UpperCamelCase : int =DDIMScheduler() __UpperCamelCase : Optional[int] ={'unet': unet, 'scheduler': scheduler} return components def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : str =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : Optional[int] =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Tuple ={ 'batch_size': 1, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Any ='cpu' __UpperCamelCase : Optional[Any] =self.get_dummy_components() __UpperCamelCase : Tuple =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =self.get_dummy_inputs(lowerCamelCase__ ) __UpperCamelCase : int =pipe(**lowerCamelCase__ ).images __UpperCamelCase : Dict =image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) __UpperCamelCase : Tuple =np.array( [1.000E00, 5.717E-01, 4.717E-01, 1.000E00, 0.000E00, 1.000E00, 3.000E-04, 0.000E00, 9.000E-04] ) __UpperCamelCase : Tuple =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) def __lowercase ( self ): """simple docstring""" super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_save_load_local(expected_max_difference=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_save_load_optional_components(expected_max_difference=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __A ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str ='google/ddpm-cifar10-32' __UpperCamelCase : str =UNetaDModel.from_pretrained(lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =DDIMScheduler() __UpperCamelCase : List[Any] =DDIMPipeline(unet=lowerCamelCase__ , scheduler=lowerCamelCase__ ) ddim.to(lowerCamelCase__ ) ddim.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Optional[int] =torch.manual_seed(0 ) __UpperCamelCase : List[str] =ddim(generator=lowerCamelCase__ , eta=0.0 , output_type='numpy' ).images __UpperCamelCase : Union[str, Any] =image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) __UpperCamelCase : str =np.array([0.1_723, 0.1_617, 0.1_600, 0.1_626, 0.1_497, 0.1_513, 0.1_505, 0.1_442, 0.1_453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[Any] ='google/ddpm-ema-bedroom-256' __UpperCamelCase : Any =UNetaDModel.from_pretrained(lowerCamelCase__ ) __UpperCamelCase : int =DDIMScheduler.from_pretrained(lowerCamelCase__ ) __UpperCamelCase : Dict =DDIMPipeline(unet=lowerCamelCase__ , scheduler=lowerCamelCase__ ) ddpm.to(lowerCamelCase__ ) ddpm.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Tuple =torch.manual_seed(0 ) __UpperCamelCase : Union[str, Any] =ddpm(generator=lowerCamelCase__ , output_type='numpy' ).images __UpperCamelCase : Tuple =image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) __UpperCamelCase : Optional[Any] =np.array([0.0_060, 0.0_201, 0.0_344, 0.0_024, 0.0_018, 0.0_002, 0.0_022, 0.0_000, 0.0_069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
71
0
from __future__ import annotations def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , ): """simple docstring""" if (stress, tangential_force, area).count(0 ) != 1: raise ValueError('''You cannot supply more or less than 2 values''' ) elif stress < 0: raise ValueError('''Stress cannot be negative''' ) elif tangential_force < 0: raise ValueError('''Tangential Force cannot be negative''' ) elif area < 0: raise ValueError('''Area cannot be negative''' ) elif stress == 0: return ( "stress", tangential_force / area, ) elif tangential_force == 0: return ( "tangential_force", stress * area, ) else: return ( "area", tangential_force / stress, ) if __name__ == "__main__": import doctest doctest.testmod()
110
from __future__ import annotations import copy import tempfile import unittest from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available from transformers.testing_utils import ( DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tensorflow_probability, require_tf, slow, ) from ..bert.test_modeling_bert import BertModelTester if is_tf_available(): from transformers import ( TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelForTableQuestionAnswering, TFAutoModelForTokenClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFFunnelBaseModel, TFFunnelModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, TFTapasForQuestionAnswering, ) from transformers.models.auto.modeling_tf_auto import ( TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_MAPPING, ) 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 from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST class __A ( a ): """simple docstring""" UpperCamelCase__ : Optional[int] ="""new-model""" if is_tf_available(): class __A ( a ): """simple docstring""" UpperCamelCase__ : List[str] =NewModelConfig @require_tf class __A ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] ='bert-base-cased' __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Union[str, Any] ='bert-base-cased' __UpperCamelCase : Optional[int] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Tuple =TFAutoModelForPreTraining.from_pretrained(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]: __UpperCamelCase : str =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModelForCausalLM.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : str =TFAutoModelForCausalLM.from_pretrained(lowerCamelCase__ , output_loading_info=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]: __UpperCamelCase : Optional[int] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Any =TFAutoModelWithLMHead.from_pretrained(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]: __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Any =TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Union[str, Any] =TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase__ , output_loading_info=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]: __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : str =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ , output_loading_info=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForSequenceClassification.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: __UpperCamelCase : List[Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[int] =TFAutoModelForQuestionAnswering.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow @require_tensorflow_probability def __lowercase ( self ): """simple docstring""" for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]: __UpperCamelCase : Any =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModelForTableQuestionAnswering.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : List[str] =TFAutoModelForTableQuestionAnswering.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[Any] =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Dict =TFAutoModel.from_pretrained('sgugger/funnel-random-tiny' ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[int] =copy.deepcopy(model.config ) __UpperCamelCase : Optional[Any] =['FunnelBaseModel'] __UpperCamelCase : Tuple =TFAutoModel.from_config(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : List[Any] =TFAutoModel.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" try: AutoConfig.register('new-model' , lowerCamelCase__ ) __UpperCamelCase : int =[ TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSequenceClassification, TFAutoModelForTokenClassification, ] for auto_class in auto_classes: with self.subTest(auto_class.__name__ ): # Wrong config class will raise an error with self.assertRaises(lowerCamelCase__ ): auto_class.register(lowerCamelCase__ , lowerCamelCase__ ) auto_class.register(lowerCamelCase__ , lowerCamelCase__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(lowerCamelCase__ ): auto_class.register(lowerCamelCase__ , lowerCamelCase__ ) # Now that the config is registered, it can be used as any other config with the auto-API __UpperCamelCase : List[str] =BertModelTester(self ).get_config() __UpperCamelCase : Optional[Any] =NewModelConfig(**tiny_config.to_dict() ) __UpperCamelCase : Dict =auto_class.from_config(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =auto_class.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) finally: if "new-model" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["new-model"] for mapping in ( TF_MODEL_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, ): if NewModelConfig in mapping._extra_content: del mapping._extra_content[NewModelConfig] def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCamelCase__ , 'bert-base is not a local folder and is not a valid model identifier' ): __UpperCamelCase : Dict =TFAutoModel.from_pretrained('bert-base' ) def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCamelCase__ , R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)' ): __UpperCamelCase : Union[str, Any] =TFAutoModel.from_pretrained(lowerCamelCase__ , revision='aaaaaa' ) def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCamelCase__ , 'hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin' , ): __UpperCamelCase : List[str] =TFAutoModel.from_pretrained('hf-internal-testing/config-no-model' ) def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex(lowerCamelCase__ , 'Use `from_pt=True` to load this model' ): __UpperCamelCase : List[Any] =TFAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only' ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[int] =TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert' ) with RequestCounter() as counter: __UpperCamelCase : Dict =TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 ) # With a sharded checkpoint __UpperCamelCase : Dict =TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded' ) with RequestCounter() as counter: __UpperCamelCase : Union[str, Any] =TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
71
0
"""simple docstring""" import numpy as np def __lowerCamelCase ( a_ : int ) -> np.array: return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
191
import argparse import json import os import re import torch from transformers import BloomConfig, BloomModel from transformers.file_utils import CONFIG_NAME, WEIGHTS_NAME from transformers.utils import logging logging.set_verbosity_info() A_ :List[str] = [ '''word_embeddings_layernorm.weight''', '''word_embeddings_layernorm.bias''', '''input_layernorm.weight''', '''input_layernorm.bias''', '''post_attention_layernorm.weight''', '''post_attention_layernorm.bias''', '''self_attention.dense.bias''', '''mlp.dense_4h_to_h.bias''', '''ln_f.weight''', '''ln_f.bias''', ] A_ :Optional[Any] = [ '''mlp.dense_4h_to_h.weight''', '''self_attention.dense.weight''', ] def A ( a_ ,a_ ) -> str: __UpperCamelCase : Any ={ 'word_embeddings.weight': 'word_embeddings.weight', 'word_embeddings.norm.weight': 'word_embeddings_layernorm.weight', 'word_embeddings.norm.bias': 'word_embeddings_layernorm.bias', 'weight': 'ln_f.weight', 'bias': 'ln_f.bias', } if key in layer_rename_map: return layer_rename_map[key] # Handle transformer blocks __UpperCamelCase : Tuple =int(re.match(r'.*layer_(\d*).*' ,a_ )[1] ) layer_number -= 3 return F'h.{layer_number}.' + key def A ( a_ ) -> Any: if dtype == torch.bool: return 1 / 8 __UpperCamelCase : Dict =re.search(r'[^\d](\d+)$' ,str(a_ ) ) if bit_search is None: raise ValueError(F'`dtype` is not a valid dtype: {dtype}.' ) __UpperCamelCase : Tuple =int(bit_search.groups()[0] ) return bit_size // 8 def A ( a_ ,a_ ,a_ ,a_ ,a_ ) -> Dict: # Construct model if bloom_config_file == "": __UpperCamelCase : List[Any] =BloomConfig() else: __UpperCamelCase : List[str] =BloomConfig.from_json_file(a_ ) if shard_model: __UpperCamelCase : int =os.listdir(a_ ) __UpperCamelCase : Union[str, Any] =sorted(filter(lambda a_ : s.startswith('layer' ) and "model_00" in s ,a_ ) ) __UpperCamelCase : Optional[Any] ={'weight_map': {}, 'metadata': {}} __UpperCamelCase : Dict =0 __UpperCamelCase : int =None __UpperCamelCase : Any =BloomConfig() for j, file in enumerate(a_ ): print('Processing file: {}'.format(a_ ) ) __UpperCamelCase : Optional[int] =None for i in range(a_ ): # load all TP files __UpperCamelCase : Dict =file.replace('model_00' ,F'model_0{i}' ) __UpperCamelCase : Optional[Any] =torch.load(os.path.join(a_ ,a_ ) ,map_location='cpu' ) # Rename keys in the transformers names __UpperCamelCase : int =list(temp.keys() ) for key in keys: __UpperCamelCase : Dict =temp.pop(a_ ) if tensors is None: __UpperCamelCase : Any =temp else: for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel __UpperCamelCase : List[Any] =1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks __UpperCamelCase : Any =torch.cat([tensors[key], temp[key]] ,dim=a_ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): __UpperCamelCase : Optional[Any] =tensors[key] / pretraining_tp torch.save( a_ ,os.path.join( a_ ,'pytorch_model_{}-of-{}.bin'.format(str(j + 1 ).zfill(5 ) ,str(len(a_ ) ).zfill(5 ) ) ,) ,) for key in tensors.keys(): __UpperCamelCase : Union[str, Any] =tensors[key] total_size += value.numel() * get_dtype_size(value.dtype ) if key not in index_dict["weight_map"]: __UpperCamelCase : int ='pytorch_model_{}-of-{}.bin'.format( str(j + 1 ).zfill(5 ) ,str(len(a_ ) ).zfill(5 ) ) __UpperCamelCase : Union[str, Any] =BloomConfig() __UpperCamelCase : Tuple =pytorch_dump_folder_path + '/' + CONFIG_NAME __UpperCamelCase : Optional[int] =total_size with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(config.to_json_string() ) with open(os.path.join(a_ ,WEIGHTS_NAME + '.index.json' ) ,'w' ,encoding='utf-8' ) as f: __UpperCamelCase : List[Any] =json.dumps(a_ ,indent=2 ,sort_keys=a_ ) + '\n' f.write(a_ ) else: __UpperCamelCase : List[Any] =BloomModel(a_ ) __UpperCamelCase : Optional[Any] =os.listdir(a_ ) __UpperCamelCase : Dict =sorted(filter(lambda a_ : s.startswith('layer' ) and "model_00" in s ,a_ ) ) __UpperCamelCase : Any =None for i, file in enumerate(a_ ): __UpperCamelCase : Union[str, Any] =None for i in range(a_ ): # load all TP files __UpperCamelCase : Optional[Any] =file.replace('model_00' ,F'model_0{i}' ) __UpperCamelCase : str =torch.load(os.path.join(a_ ,a_ ) ,map_location='cpu' ) # Rename keys in the transformers names __UpperCamelCase : List[str] =list(temp.keys() ) for key in keys: __UpperCamelCase : Union[str, Any] =temp.pop(a_ ) if tensors is None: __UpperCamelCase : Optional[Any] =temp else: for key in tensors.keys(): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel __UpperCamelCase : Optional[int] =1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks __UpperCamelCase : int =torch.cat([tensors[key], temp[key]] ,dim=a_ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): __UpperCamelCase : Dict =tensors[key] / pretraining_tp __UpperCamelCase : str =model.load_state_dict(a_ ,strict=a_ ) assert not other_keys.unexpected_keys, F'The keys {other_keys.unexpected_keys} are unexpected' if missing_keys is None: __UpperCamelCase : str =set(other_keys.missing_keys ) else: __UpperCamelCase : int =missing_keys.intersection(set(other_keys.missing_keys ) ) assert not missing_keys, F'The keys {missing_keys} are missing' # Save pytorch-model os.makedirs(a_ ,exist_ok=a_ ) __UpperCamelCase : Optional[int] =pytorch_dump_folder_path + '/' + WEIGHTS_NAME __UpperCamelCase : Dict =pytorch_dump_folder_path + '/' + CONFIG_NAME print(F'Save PyTorch model to {pytorch_weights_dump_path} with dtype {config.torch_dtype}' ) if config.torch_dtype is not None: __UpperCamelCase : List[str] =model.to(config.torch_dtype ) torch.save(model.state_dict() ,a_ ) print(F'Save configuration file to {pytorch_config_dump_path}' ) with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": A_ :Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--bloom_checkpoint_path''', default=None, type=str, required=True, help='''Path to the Megatron-LM checkpoint path.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--bloom_config_file''', default='''''', type=str, help=( '''An optional config json file corresponding to the pre-trained model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--shard_model''', action='''store_true''', help='''An optional setting to shard the output model \nThis enables sharding the converted checkpoint''', ) parser.add_argument( '''--pretraining_tp''', default=4, type=int, help='''Pretraining TP rank that has been used when training the model in Megatron-LM \n''', ) A_ :str = parser.parse_args() convert_bloom_checkpoint_to_pytorch( args.bloom_checkpoint_path, args.bloom_config_file, args.pytorch_dump_folder_path, args.shard_model, args.pretraining_tp, )
71
0
"""simple docstring""" import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, Features, Value from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE_ ) class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : str = field(default="automatic-speech-recognition" , metadata={"include_in_asdict_even_if_is_default": True} ) _UpperCamelCase : ClassVar[Features] = Features({"audio": Audio()} ) _UpperCamelCase : ClassVar[Features] = Features({"transcription": Value("string" )} ) _UpperCamelCase : str = "audio" _UpperCamelCase : str = "transcription" def __A ( self , a__ ): if self.audio_column not in features: raise ValueError(F"Column {self.audio_column} is not present in features." ) if not isinstance(features[self.audio_column] , lowerCamelCase__ ): raise ValueError(F"Column {self.audio_column} is not an Audio type." ) _lowerCAmelCase : Optional[int] = copy.deepcopy(self ) _lowerCAmelCase : int = self.input_schema.copy() _lowerCAmelCase : Union[str, Any] = features[self.audio_column] _lowerCAmelCase : List[Any] = input_schema return task_template @property def __A ( self ): return {self.audio_column: "audio", self.transcription_column: "transcription"}
44
from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class __A : """simple docstring""" UpperCamelCase__ : int =XGLMConfig UpperCamelCase__ : Optional[Any] ={} UpperCamelCase__ : List[str] ="""gelu""" def __init__( self , lowerCamelCase__ , lowerCamelCase__=14 , lowerCamelCase__=7 , 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__=0.02 , ): """simple docstring""" __UpperCamelCase : Tuple =parent __UpperCamelCase : List[str] =batch_size __UpperCamelCase : str =seq_length __UpperCamelCase : Dict =is_training __UpperCamelCase : Tuple =use_input_mask __UpperCamelCase : List[Any] =use_labels __UpperCamelCase : Any =vocab_size __UpperCamelCase : List[Any] =d_model __UpperCamelCase : Optional[int] =num_hidden_layers __UpperCamelCase : List[str] =num_attention_heads __UpperCamelCase : Optional[int] =ffn_dim __UpperCamelCase : str =activation_function __UpperCamelCase : Any =activation_dropout __UpperCamelCase : Optional[int] =attention_dropout __UpperCamelCase : Optional[int] =max_position_embeddings __UpperCamelCase : Any =initializer_range __UpperCamelCase : Dict =None __UpperCamelCase : Optional[int] =0 __UpperCamelCase : Optional[Any] =2 __UpperCamelCase : str =1 def __lowercase ( self ): """simple docstring""" return XGLMConfig.from_pretrained('facebook/xglm-564M' ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[Any] =tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) , clip_value_min=0 , clip_value_max=3 ) __UpperCamelCase : Union[str, Any] =None if self.use_input_mask: __UpperCamelCase : Dict =random_attention_mask([self.batch_size, self.seq_length] ) __UpperCamelCase : Any =self.get_config() __UpperCamelCase : Optional[Any] =floats_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, ) def __lowercase ( self ): """simple docstring""" return XGLMConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , num_layers=self.num_hidden_layers , attention_heads=self.num_attention_heads , ffn_dim=self.ffn_dim , activation_function=self.activation_function , activation_dropout=self.activation_dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , use_cache=lowerCamelCase__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , return_dict=lowerCamelCase__ , ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =self.prepare_config_and_inputs() ( ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ) : int =config_and_inputs __UpperCamelCase : Optional[Any] ={ 'input_ids': input_ids, 'head_mask': head_mask, } return config, inputs_dict @require_tf class __A ( a , a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : Union[str, Any] =(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () UpperCamelCase__ : str =(TFXGLMForCausalLM,) if is_tf_available() else () UpperCamelCase__ : Optional[Any] =( {"""feature-extraction""": TFXGLMModel, """text-generation""": TFXGLMForCausalLM} if is_tf_available() else {} ) UpperCamelCase__ : Tuple =False UpperCamelCase__ : Tuple =False UpperCamelCase__ : Optional[Any] =False def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Tuple =TFXGLMModelTester(self ) __UpperCamelCase : Dict =ConfigTester(self , config_class=lowerCamelCase__ , n_embd=37 ) def __lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() @slow def __lowercase ( self ): """simple docstring""" for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __UpperCamelCase : Optional[Any] =TFXGLMModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @unittest.skip(reason='Currently, model embeddings are going to undergo a major refactor.' ) def __lowercase ( self ): """simple docstring""" super().test_resize_token_embeddings() @require_tf class __A ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self , lowerCamelCase__=True ): """simple docstring""" __UpperCamelCase : int =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : List[str] =tf.convert_to_tensor([[2, 268, 9865]] , dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off __UpperCamelCase : str =[2, 268, 9865, 67, 11, 1988, 57252, 9865, 5, 984, 67, 1988, 213838, 1658, 53, 70446, 33, 6657, 278, 1581] # fmt: on __UpperCamelCase : Optional[Any] =model.generate(lowerCamelCase__ , do_sample=lowerCamelCase__ , num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist() , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : Union[str, Any] =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) tf.random.set_seed(0 ) __UpperCamelCase : str =tokenizer('Today is a nice day and' , return_tensors='tf' ) __UpperCamelCase : Union[str, Any] =tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(':/CPU:0' ): __UpperCamelCase : Any =model.generate(lowerCamelCase__ , do_sample=lowerCamelCase__ , seed=[7, 0] ) __UpperCamelCase : Tuple =tokenizer.decode(output_ids[0] , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : List[Any] =( 'Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due' ) self.assertEqual(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Tuple =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : Optional[Any] =XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : Optional[Any] ='left' # use different length sentences to test batching __UpperCamelCase : Optional[int] =[ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When', 'Hello, my dog is a little', ] __UpperCamelCase : List[Any] =tokenizer(lowerCamelCase__ , return_tensors='tf' , padding=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =inputs['input_ids'] __UpperCamelCase : Dict =model.generate(input_ids=lowerCamelCase__ , attention_mask=inputs['attention_mask'] , max_new_tokens=12 ) __UpperCamelCase : List[Any] =tokenizer(sentences[0] , return_tensors='tf' ).input_ids __UpperCamelCase : Dict =model.generate(input_ids=lowerCamelCase__ , max_new_tokens=12 ) __UpperCamelCase : Any =tokenizer(sentences[1] , return_tensors='tf' ).input_ids __UpperCamelCase : Optional[Any] =model.generate(input_ids=lowerCamelCase__ , max_new_tokens=12 ) __UpperCamelCase : Optional[int] =tokenizer.batch_decode(lowerCamelCase__ , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =tokenizer.decode(output_non_padded[0] , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : int =tokenizer.decode(output_padded[0] , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : Any =[ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When left padding is applied, the sequence will be ' 'a single', 'Hello, my dog is a little bit of a shy one, but he is very friendly', ] self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , [non_padded_sentence, padded_sentence] )
71
0
from itertools import product from cva import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey from numpy import dot, exp, mgrid, pi, ravel, square, uinta, zeros def A ( _UpperCAmelCase : Any , _UpperCAmelCase : int ) -> Union[str, Any]: '''simple docstring''' _UpperCAmelCase = k_size // 2 _UpperCAmelCase = mgrid[0 - center : k_size - center, 0 - center : k_size - center] _UpperCAmelCase = 1 / (2 * pi * sigma) * exp(-(square(a_ ) + square(a_ )) / (2 * square(a_ )) ) return g def A ( _UpperCAmelCase : List[Any] , _UpperCAmelCase : List[str] , _UpperCAmelCase : Union[str, Any] ) -> Any: '''simple docstring''' _UpperCAmelCase = image.shape[0], image.shape[1] # dst image height and width _UpperCAmelCase = height - k_size + 1 _UpperCAmelCase = width - k_size + 1 # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows _UpperCAmelCase = zeros((dst_height * dst_width, k_size * k_size) ) _UpperCAmelCase = 0 for i, j in product(range(a_ ) , range(a_ ) ): _UpperCAmelCase = ravel(image[i : i + k_size, j : j + k_size] ) _UpperCAmelCase = window row += 1 # turn the kernel into shape(k*k, 1) _UpperCAmelCase = gen_gaussian_kernel(a_ , a_ ) _UpperCAmelCase = ravel(a_ ) # reshape and get the dst image _UpperCAmelCase = dot(a_ , a_ ).reshape(a_ , a_ ).astype(a_ ) return dst if __name__ == "__main__": # read original image UpperCAmelCase__ = imread(r"../image_data/lena.jpg") # turn image in gray scale value UpperCAmelCase__ = cvtColor(img, COLOR_BGR2GRAY) # get values with two different mask size UpperCAmelCase__ = gaussian_filter(gray, 3, sigma=1) UpperCAmelCase__ = gaussian_filter(gray, 5, sigma=0.8) # show result images imshow("gaussian filter with 3x3 mask", gaussianaxa) imshow("gaussian filter with 5x5 mask", gaussianaxa) waitKey()
339
import argparse import json import numpy import torch from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def A ( a_ ,a_ ) -> Optional[Any]: # Load checkpoint __UpperCamelCase : int =torch.load(a_ ,map_location='cpu' ) __UpperCamelCase : List[Any] =chkpt['model'] # We have the base model one level deeper than the original XLM repository __UpperCamelCase : str ={} for k, v in state_dict.items(): if "pred_layer" in k: __UpperCamelCase : Optional[Any] =v else: __UpperCamelCase : Optional[Any] =v __UpperCamelCase : List[Any] =chkpt['params'] __UpperCamelCase : str ={n: v for n, v in config.items() if not isinstance(a_ ,(torch.FloatTensor, numpy.ndarray) )} __UpperCamelCase : str =chkpt['dico_word2id'] __UpperCamelCase : Dict ={s + '</w>' if s.find('@@' ) == -1 and i > 13 else s.replace('@@' ,'' ): i for s, i in vocab.items()} # Save pytorch-model __UpperCamelCase : List[Any] =pytorch_dump_folder_path + '/' + WEIGHTS_NAME __UpperCamelCase : Tuple =pytorch_dump_folder_path + '/' + CONFIG_NAME __UpperCamelCase : Any =pytorch_dump_folder_path + '/' + VOCAB_FILES_NAMES['vocab_file'] print(F'Save PyTorch model to {pytorch_weights_dump_path}' ) torch.save(a_ ,a_ ) print(F'Save configuration file to {pytorch_config_dump_path}' ) with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(json.dumps(a_ ,indent=2 ) + '\n' ) print(F'Save vocab file to {pytorch_config_dump_path}' ) with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(json.dumps(a_ ,indent=2 ) + '\n' ) if __name__ == "__main__": A_ :str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--xlm_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.''' ) A_ :List[Any] = parser.parse_args() convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)
71
0
"""simple docstring""" class __lowerCamelCase : '''simple docstring''' def __init__( self : Union[str, Any] , a_ : Optional[int] ): lowerCAmelCase_ : str = set_counts lowerCAmelCase_ : Optional[Any] = max(lowerCamelCase__ ) lowerCAmelCase_ : Dict = len(lowerCamelCase__ ) lowerCAmelCase_ : Optional[Any] = [1] * num_sets lowerCAmelCase_ : Optional[Any] = list(range(lowerCamelCase__ ) ) def lowerCamelCase ( self : int , a_ : str , a_ : Dict ): lowerCAmelCase_ : List[Any] = self.get_parent(lowerCamelCase__ ) lowerCAmelCase_ : List[str] = self.get_parent(lowerCamelCase__ ) if src_parent == dst_parent: return False if self.ranks[dst_parent] >= self.ranks[src_parent]: self.set_counts[dst_parent] += self.set_counts[src_parent] lowerCAmelCase_ : int = 0 lowerCAmelCase_ : Any = dst_parent if self.ranks[dst_parent] == self.ranks[src_parent]: self.ranks[dst_parent] += 1 lowerCAmelCase_ : List[str] = self.set_counts[dst_parent] else: self.set_counts[src_parent] += self.set_counts[dst_parent] lowerCAmelCase_ : List[Any] = 0 lowerCAmelCase_ : int = src_parent lowerCAmelCase_ : int = self.set_counts[src_parent] lowerCAmelCase_ : Tuple = max(self.max_set , lowerCamelCase__ ) return True def lowerCamelCase ( self : str , a_ : int ): if self.parents[disj_set] == disj_set: return disj_set lowerCAmelCase_ : Dict = self.get_parent(self.parents[disj_set] ) return self.parents[disj_set]
241
import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class __A ( a ): """simple docstring""" def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str =tempfile.mkdtemp() __UpperCamelCase : Optional[int] =8 # DPR tok __UpperCamelCase : str =[ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] __UpperCamelCase : Optional[Any] =os.path.join(self.tmpdirname , 'dpr_tokenizer' ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) __UpperCamelCase : Dict =os.path.join(lowerCamelCase__ , DPR_VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) # BART tok __UpperCamelCase : Optional[int] =[ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] __UpperCamelCase : str =dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) __UpperCamelCase : Optional[int] =['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] __UpperCamelCase : Any ={'unk_token': '<unk>'} __UpperCamelCase : Any =os.path.join(self.tmpdirname , 'bart_tokenizer' ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) __UpperCamelCase : Any =os.path.join(lowerCamelCase__ , BART_VOCAB_FILES_NAMES['vocab_file'] ) __UpperCamelCase : Dict =os.path.join(lowerCamelCase__ , BART_VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(lowerCamelCase__ ) ) def __lowercase ( self ): """simple docstring""" return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'dpr_tokenizer' ) ) def __lowercase ( self ): """simple docstring""" return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'bart_tokenizer' ) ) def __lowercase ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) @require_tokenizers def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =os.path.join(self.tmpdirname , 'rag_tokenizer' ) __UpperCamelCase : Dict =RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) __UpperCamelCase : List[Any] =RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(lowerCamelCase__ ) rag_tokenizer.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : int =RagTokenizer.from_pretrained(lowerCamelCase__ , config=lowerCamelCase__ ) self.assertIsInstance(new_rag_tokenizer.question_encoder , lowerCamelCase__ ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , lowerCamelCase__ ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Dict =RagTokenizer.from_pretrained('facebook/rag-token-nq' ) __UpperCamelCase : Union[str, Any] =[ 'who got the first nobel prize in physics', 'when is the next deadpool movie being released', 'which mode is used for short wave broadcast service', 'who is the owner of reading football club', 'when is the next scandal episode coming out', 'when is the last time the philadelphia won the superbowl', 'what is the most current adobe flash player version', 'how many episodes are there in dragon ball z', 'what is the first step in the evolution of the eye', 'where is gall bladder situated in human body', 'what is the main mineral in lithium batteries', 'who is the president of usa right now', 'where do the greasers live in the outsiders', 'panda is a national animal of which country', 'what is the name of manchester united stadium', ] __UpperCamelCase : int =tokenizer(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =RagTokenizer.from_pretrained('facebook/rag-sequence-nq' ) __UpperCamelCase : Union[str, Any] =[ 'who got the first nobel prize in physics', 'when is the next deadpool movie being released', 'which mode is used for short wave broadcast service', 'who is the owner of reading football club', 'when is the next scandal episode coming out', 'when is the last time the philadelphia won the superbowl', 'what is the most current adobe flash player version', 'how many episodes are there in dragon ball z', 'what is the first step in the evolution of the eye', 'where is gall bladder situated in human body', 'what is the main mineral in lithium batteries', 'who is the president of usa right now', 'where do the greasers live in the outsiders', 'panda is a national animal of which country', 'what is the name of manchester united stadium', ] __UpperCamelCase : Any =tokenizer(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ )
71
0
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_fnet import FNetTokenizer else: A__: Optional[int] = None A__: int = logging.get_logger(__name__) A__: Any = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A__: Dict = { '''vocab_file''': { '''google/fnet-base''': '''https://huggingface.co/google/fnet-base/resolve/main/spiece.model''', '''google/fnet-large''': '''https://huggingface.co/google/fnet-large/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''google/fnet-base''': '''https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json''', '''google/fnet-large''': '''https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json''', }, } A__: Optional[int] = { '''google/fnet-base''': 512, '''google/fnet-large''': 512, } A__: Optional[int] = '''▁''' class _a ( UpperCamelCase__): """simple docstring""" UpperCamelCase__ = VOCAB_FILES_NAMES UpperCamelCase__ = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase__ = ["""input_ids""", """token_type_ids"""] UpperCamelCase__ = FNetTokenizer def __init__( self: Dict , __lowerCamelCase: str=None , __lowerCamelCase: List[str]=None , __lowerCamelCase: Optional[Any]=False , __lowerCamelCase: int=True , __lowerCamelCase: Union[str, Any]=True , __lowerCamelCase: List[Any]="<unk>" , __lowerCamelCase: Tuple="[SEP]" , __lowerCamelCase: Tuple="<pad>" , __lowerCamelCase: Optional[int]="[CLS]" , __lowerCamelCase: List[Any]="[MASK]" , **__lowerCamelCase: Union[str, Any] , ): '''simple docstring''' UpperCamelCase__: Optional[Any] = ( AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ , normalized=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else mask_token ) super().__init__( lowerCamelCase__ , tokenizer_file=lowerCamelCase__ , do_lower_case=lowerCamelCase__ , remove_space=lowerCamelCase__ , keep_accents=lowerCamelCase__ , unk_token=lowerCamelCase__ , sep_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , cls_token=lowerCamelCase__ , mask_token=lowerCamelCase__ , **lowerCamelCase__ , ) UpperCamelCase__: List[str] = do_lower_case UpperCamelCase__: Tuple = remove_space UpperCamelCase__: List[str] = keep_accents UpperCamelCase__: int = vocab_file UpperCamelCase__: Dict = False if not self.vocab_file else True def UpperCAmelCase_ ( self: List[str] , __lowerCamelCase: Tuple , __lowerCamelCase: Optional[int] = None ): '''simple docstring''' UpperCamelCase__: Optional[Any] = [self.sep_token_id] UpperCamelCase__: Any = [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: Tuple , __lowerCamelCase: Tuple , __lowerCamelCase: Optional[int] = None ): '''simple docstring''' UpperCamelCase__: Union[str, Any] = [self.sep_token_id] UpperCamelCase__: Tuple = [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: List[Any] , __lowerCamelCase: Optional[Any] , __lowerCamelCase: List[str] = None ): '''simple docstring''' if not os.path.isdir(lowerCamelCase__ ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return UpperCamelCase__: Optional[int] = 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__ ): copyfile(self.vocab_file , lowerCamelCase__ ) return (out_vocab_file,)
149
A_ :Optional[int] = ''' # Installazione di Transformers ! pip install transformers datasets # Per installare dalla fonte invece dell\'ultima versione rilasciata, commenta il comando sopra e # rimuovi la modalità commento al comando seguente. # ! pip install git+https://github.com/huggingface/transformers.git ''' A_ :Union[str, Any] = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] A_ :Optional[Any] = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
71
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available a : Dict = { '''configuration_mvp''': ['''MVP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MvpConfig''', '''MvpOnnxConfig'''], '''tokenization_mvp''': ['''MvpTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a : Optional[Any] = ['''MvpTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a : List[str] = [ '''MVP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MvpForCausalLM''', '''MvpForConditionalGeneration''', '''MvpForQuestionAnswering''', '''MvpForSequenceClassification''', '''MvpModel''', '''MvpPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mvp import MVP_PRETRAINED_CONFIG_ARCHIVE_MAP, MvpConfig, MvpOnnxConfig from .tokenization_mvp import MvpTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mvp_fast import MvpTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mvp import ( MVP_PRETRAINED_MODEL_ARCHIVE_LIST, MvpForCausalLM, MvpForConditionalGeneration, MvpForQuestionAnswering, MvpForSequenceClassification, MvpModel, MvpPreTrainedModel, ) else: import sys a : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
56
import argparse import hashlib import os import urllib import warnings import torch from torch import nn from tqdm import tqdm from transformers import WhisperConfig, WhisperForConditionalGeneration A_ :Optional[Any] = { '''tiny.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt''', '''tiny''': '''https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt''', '''base.en''': '''https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt''', '''base''': '''https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt''', '''small.en''': '''https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt''', '''small''': '''https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt''', '''medium.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt''', '''medium''': '''https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt''', '''large''': '''https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large.pt''', '''large-v2''': '''https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt''', } def A ( a_ ) -> List[Any]: __UpperCamelCase : Any =['layers', 'blocks'] for k in ignore_keys: state_dict.pop(a_ ,a_ ) A_ :int = { '''blocks''': '''layers''', '''mlp.0''': '''fc1''', '''mlp.2''': '''fc2''', '''mlp_ln''': '''final_layer_norm''', '''.attn.query''': '''.self_attn.q_proj''', '''.attn.key''': '''.self_attn.k_proj''', '''.attn.value''': '''.self_attn.v_proj''', '''.attn_ln''': '''.self_attn_layer_norm''', '''.attn.out''': '''.self_attn.out_proj''', '''.cross_attn.query''': '''.encoder_attn.q_proj''', '''.cross_attn.key''': '''.encoder_attn.k_proj''', '''.cross_attn.value''': '''.encoder_attn.v_proj''', '''.cross_attn_ln''': '''.encoder_attn_layer_norm''', '''.cross_attn.out''': '''.encoder_attn.out_proj''', '''decoder.ln.''': '''decoder.layer_norm.''', '''encoder.ln.''': '''encoder.layer_norm.''', '''token_embedding''': '''embed_tokens''', '''encoder.positional_embedding''': '''encoder.embed_positions.weight''', '''decoder.positional_embedding''': '''decoder.embed_positions.weight''', '''ln_post''': '''layer_norm''', } def A ( a_ ) -> Union[str, Any]: __UpperCamelCase : str =list(s_dict.keys() ) for key in keys: __UpperCamelCase : str =key for k, v in WHISPER_MAPPING.items(): if k in key: __UpperCamelCase : Optional[Any] =new_key.replace(a_ ,a_ ) print(F'{key} -> {new_key}' ) __UpperCamelCase : Dict =s_dict.pop(a_ ) return s_dict def A ( a_ ) -> Optional[Any]: __UpperCamelCase , __UpperCamelCase : Tuple =emb.weight.shape __UpperCamelCase : Tuple =nn.Linear(a_ ,a_ ,bias=a_ ) __UpperCamelCase : List[Any] =emb.weight.data return lin_layer def A ( a_ ,a_ ) -> bytes: os.makedirs(a_ ,exist_ok=a_ ) __UpperCamelCase : Optional[int] =os.path.basename(a_ ) __UpperCamelCase : Union[str, Any] =url.split('/' )[-2] __UpperCamelCase : Union[str, Any] =os.path.join(a_ ,a_ ) if os.path.exists(a_ ) and not os.path.isfile(a_ ): raise RuntimeError(F'{download_target} exists and is not a regular file' ) if os.path.isfile(a_ ): __UpperCamelCase : str =open(a_ ,'rb' ).read() if hashlib.shaaaa(a_ ).hexdigest() == expected_shaaaa: return model_bytes else: warnings.warn(F'{download_target} exists, but the SHA256 checksum does not match; re-downloading the file' ) with urllib.request.urlopen(a_ ) as source, open(a_ ,'wb' ) as output: with tqdm( total=int(source.info().get('Content-Length' ) ) ,ncols=80 ,unit='iB' ,unit_scale=a_ ,unit_divisor=1_024 ) as loop: while True: __UpperCamelCase : Optional[Any] =source.read(8_192 ) if not buffer: break output.write(a_ ) loop.update(len(a_ ) ) __UpperCamelCase : List[Any] =open(a_ ,'rb' ).read() if hashlib.shaaaa(a_ ).hexdigest() != expected_shaaaa: raise RuntimeError( 'Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model.' ) return model_bytes def A ( a_ ,a_ ) -> Optional[Any]: if ".pt" not in checkpoint_path: __UpperCamelCase : int =_download(_MODELS[checkpoint_path] ) else: __UpperCamelCase : List[str] =torch.load(a_ ,map_location='cpu' ) __UpperCamelCase : Union[str, Any] =original_checkpoint['dims'] __UpperCamelCase : List[Any] =original_checkpoint['model_state_dict'] __UpperCamelCase : Dict =state_dict['decoder.token_embedding.weight'] remove_ignore_keys_(a_ ) rename_keys(a_ ) __UpperCamelCase : List[str] =True __UpperCamelCase : str =state_dict['decoder.layers.0.fc1.weight'].shape[0] __UpperCamelCase : Optional[int] =WhisperConfig( vocab_size=dimensions['n_vocab'] ,encoder_ffn_dim=a_ ,decoder_ffn_dim=a_ ,num_mel_bins=dimensions['n_mels'] ,d_model=dimensions['n_audio_state'] ,max_target_positions=dimensions['n_text_ctx'] ,encoder_layers=dimensions['n_audio_layer'] ,encoder_attention_heads=dimensions['n_audio_head'] ,decoder_layers=dimensions['n_text_layer'] ,decoder_attention_heads=dimensions['n_text_state'] ,max_source_positions=dimensions['n_audio_ctx'] ,) __UpperCamelCase : List[str] =WhisperForConditionalGeneration(a_ ) __UpperCamelCase , __UpperCamelCase : Union[str, Any] =model.model.load_state_dict(a_ ,strict=a_ ) if len(a_ ) > 0 and not set(a_ ) <= { "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: __UpperCamelCase : Optional[int] =make_linear_from_emb(model.model.decoder.embed_tokens ) else: __UpperCamelCase : List[str] =proj_out_weights model.save_pretrained(a_ ) if __name__ == "__main__": A_ :List[Any] = argparse.ArgumentParser() # # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Patht to the downloaded checkpoints''') parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') A_ :List[Any] = parser.parse_args() convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)
71
0
'''simple docstring''' UpperCAmelCase_ = '''0.21.0''' from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
346
import os from datetime import datetime as dt from github import Github A_ :str = [ '''good first issue''', '''feature request''', '''wip''', ] def A ( ) -> Any: __UpperCamelCase : Any =Github(os.environ['GITHUB_TOKEN'] ) __UpperCamelCase : Union[str, Any] =g.get_repo('huggingface/accelerate' ) __UpperCamelCase : Tuple =repo.get_issues(state='open' ) for issue in open_issues: __UpperCamelCase : List[Any] =sorted([comment for comment in issue.get_comments()] ,key=lambda a_ : i.created_at ,reverse=a_ ) __UpperCamelCase : str =comments[0] if len(a_ ) > 0 else None __UpperCamelCase : Any =dt.utcnow() __UpperCamelCase : List[str] =(current_time - issue.updated_at).days __UpperCamelCase : Union[str, Any] =(current_time - issue.created_at).days if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and days_since_updated > 7 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Close issue since it has been 7 days of inactivity since bot mention. issue.edit(state='closed' ) elif ( days_since_updated > 23 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Add stale comment issue.create_comment( 'This issue has been automatically marked as stale because it has not had ' 'recent activity. If you think this still needs to be addressed ' 'please comment on this thread.\n\nPlease note that issues that do not follow the ' '[contributing guidelines](https://github.com/huggingface/accelerate/blob/main/CONTRIBUTING.md) ' 'are likely to be ignored.' ) if __name__ == "__main__": main()
71
0
"""simple docstring""" import argparse import json from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import Callable, Dict, List, Tuple import timm import torch import torch.nn as nn from classy_vision.models.regnet import RegNet, RegNetParams, RegNetYaagf, RegNetYaagf, RegNetYaaagf from huggingface_hub import cached_download, hf_hub_url from torch import Tensor from vissl.models.model_helpers import get_trunk_forward_outputs from transformers import AutoImageProcessor, RegNetConfig, RegNetForImageClassification, RegNetModel from transformers.utils import logging logging.set_verbosity_info() a = logging.get_logger() @dataclass class SCREAMING_SNAKE_CASE__ : _a = 42 _a = field(default_factory=_a ) _a = field(default_factory=_a ) def __lowercase ( self : str , lowerCAmelCase : str , lowerCAmelCase : Union[str, Any] , lowerCAmelCase : Tuple ): lowerCAmelCase = len(list(m.modules() ) ) == 1 or isinstance(lowerCamelCase__ , nn.Convad ) or isinstance(lowerCamelCase__ , nn.BatchNormad ) if has_not_submodules: self.traced.append(lowerCamelCase__ ) def __call__( self : List[str] , lowerCAmelCase : Union[str, Any] ): for m in self.module.modules(): self.handles.append(m.register_forward_hook(self._forward_hook ) ) self.module(lowerCamelCase__ ) [x.remove() for x in self.handles] return self @property def __lowercase ( self : str ): return list(filter(lambda lowerCAmelCase : len(list(x.state_dict().keys() ) ) > 0 , self.traced ) ) @dataclass class SCREAMING_SNAKE_CASE__ : _a = 42 _a = 42 _a = 1 _a = field(default_factory=_a ) _a = field(default_factory=_a ) _a = True def __call__( self : int , lowerCAmelCase : List[str] ): lowerCAmelCase = Tracker(self.dest )(lowerCamelCase__ ).parametrized lowerCAmelCase = Tracker(self.src )(lowerCamelCase__ ).parametrized lowerCAmelCase = list(filter(lambda lowerCAmelCase : type(lowerCamelCase__ ) not in self.src_skip , lowerCamelCase__ ) ) lowerCAmelCase = list(filter(lambda lowerCAmelCase : type(lowerCamelCase__ ) not in self.dest_skip , lowerCamelCase__ ) ) if len(lowerCamelCase__ ) != len(lowerCamelCase__ ) and self.raise_if_mismatch: raise Exception( f'''Numbers of operations are different. Source module has {len(lowerCamelCase__ )} operations while''' f''' destination module has {len(lowerCamelCase__ )}.''' ) for dest_m, src_m in zip(lowerCamelCase__ , lowerCamelCase__ ): dest_m.load_state_dict(src_m.state_dict() ) if self.verbose == 1: print(f'''Transfered from={src_m} to={dest_m}''' ) class SCREAMING_SNAKE_CASE__ ( nn.Module ): def __init__( self : Tuple , lowerCAmelCase : Dict ): super().__init__() lowerCAmelCase = [] # - get the stem feature_blocks.append(("""conv1""", model.stem) ) # - get all the feature blocks for k, v in model.trunk_output.named_children(): assert k.startswith("""block""" ), f'''Unexpected layer name {k}''' lowerCAmelCase = len(lowerCamelCase__ ) + 1 feature_blocks.append((f'''res{block_index}''', v) ) lowerCAmelCase = nn.ModuleDict(lowerCamelCase__ ) def __lowercase ( self : List[str] , lowerCAmelCase : Optional[int] ): return get_trunk_forward_outputs( lowerCamelCase__ , out_feat_keys=lowerCamelCase__ , feature_blocks=self._feature_blocks , ) class SCREAMING_SNAKE_CASE__ ( _a ): def __lowercase ( self : int , lowerCAmelCase : int ): lowerCAmelCase = x.split("""-""" ) return x_split[0] + x_split[1] + "_" + "".join(x_split[2:] ) def __getitem__( self : List[str] , lowerCAmelCase : Optional[int] ): if x not in self: lowerCAmelCase = self.convert_name_to_timm(lowerCamelCase__ ) lowerCAmelCase = partial(lambda: (timm.create_model(lowerCamelCase__ , pretrained=lowerCamelCase__ ).eval(), None) ) else: lowerCAmelCase = super().__getitem__(lowerCamelCase__ ) return val class SCREAMING_SNAKE_CASE__ ( _a ): def __getitem__( self : Any , lowerCAmelCase : List[Any] ): if "seer" in x and "in1k" not in x: lowerCAmelCase = RegNetModel else: lowerCAmelCase = RegNetForImageClassification return val def lowercase (snake_case__ : Optional[int] , snake_case__ : Dict , snake_case__ : Tuple ) -> Any: '''simple docstring''' for from_key, to_key in keys: lowerCAmelCase = from_state_dict[from_key].clone() print(f'''Copied key={from_key} to={to_key}''' ) return to_state_dict def lowercase (snake_case__ : Union[str, Any] , snake_case__ : int , snake_case__ : int , snake_case__ : List[Any] , snake_case__ : str , snake_case__ : List[Any] = True , ) -> Union[str, Any]: '''simple docstring''' print(f'''Converting {name}...''' ) with torch.no_grad(): lowerCAmelCase = from_model_func() lowerCAmelCase = our_model_func(a_ ).eval() lowerCAmelCase = ModuleTransfer(src=a_ , dest=a_ , raise_if_mismatch=a_ ) lowerCAmelCase = torch.randn((1, 3, 224, 224) ) module_transfer(a_ ) if from_state_dict is not None: lowerCAmelCase = [] # for seer - in1k finetuned we have to manually copy the head if "seer" in name and "in1k" in name: lowerCAmelCase = [('0.clf.0.weight', 'classifier.1.weight'), ('0.clf.0.bias', 'classifier.1.bias')] lowerCAmelCase = manually_copy_vissl_head(a_ , our_model.state_dict() , a_ ) our_model.load_state_dict(a_ ) lowerCAmelCase = our_model(a_ , output_hidden_states=a_ ) lowerCAmelCase = ( our_outputs.logits if isinstance(a_ , a_ ) else our_outputs.last_hidden_state ) lowerCAmelCase = from_model(a_ ) lowerCAmelCase = from_output[-1] if type(a_ ) is list else from_output # now since I don't want to use any config files, vissl seer model doesn't actually have an head, so let's just check the last hidden state if "seer" in name and "in1k" in name: lowerCAmelCase = our_outputs.hidden_states[-1] assert torch.allclose(a_ , a_ ), "The model logits don't match the original one." if push_to_hub: our_model.push_to_hub( repo_path_or_name=save_directory / name , commit_message="""Add model""" , use_temp_dir=a_ , ) lowerCAmelCase = 224 if 'seer' not in name else 384 # we can use the convnext one lowerCAmelCase = AutoImageProcessor.from_pretrained("""facebook/convnext-base-224-22k-1k""" , size=a_ ) image_processor.push_to_hub( repo_path_or_name=save_directory / name , commit_message="""Add image processor""" , use_temp_dir=a_ , ) print(f'''Pushed {name}''' ) def lowercase (snake_case__ : Dict , snake_case__ : Union[str, Any] = None , snake_case__ : List[str] = True ) -> Union[str, Any]: '''simple docstring''' lowerCAmelCase = 'imagenet-1k-id2label.json' lowerCAmelCase = 1_000 lowerCAmelCase = (1, num_labels) lowerCAmelCase = 'huggingface/label-files' lowerCAmelCase = num_labels lowerCAmelCase = json.load(open(cached_download(hf_hub_url(a_ , a_ , repo_type="""dataset""" ) ) , """r""" ) ) lowerCAmelCase = {int(a_ ): v for k, v in idalabel.items()} lowerCAmelCase = idalabel lowerCAmelCase = {v: k for k, v in idalabel.items()} lowerCAmelCase = partial(a_ , num_labels=a_ , idalabel=a_ , labelaid=a_ ) lowerCAmelCase = { 'regnet-x-002': ImageNetPreTrainedConfig( depths=[1, 1, 4, 7] , hidden_sizes=[24, 56, 152, 368] , groups_width=8 , layer_type="""x""" ), 'regnet-x-004': ImageNetPreTrainedConfig( depths=[1, 2, 7, 12] , hidden_sizes=[32, 64, 160, 384] , groups_width=16 , layer_type="""x""" ), 'regnet-x-006': ImageNetPreTrainedConfig( depths=[1, 3, 5, 7] , hidden_sizes=[48, 96, 240, 528] , groups_width=24 , layer_type="""x""" ), 'regnet-x-008': ImageNetPreTrainedConfig( depths=[1, 3, 7, 5] , hidden_sizes=[64, 128, 288, 672] , groups_width=16 , layer_type="""x""" ), 'regnet-x-016': ImageNetPreTrainedConfig( depths=[2, 4, 10, 2] , hidden_sizes=[72, 168, 408, 912] , groups_width=24 , layer_type="""x""" ), 'regnet-x-032': ImageNetPreTrainedConfig( depths=[2, 6, 15, 2] , hidden_sizes=[96, 192, 432, 1_008] , groups_width=48 , layer_type="""x""" ), 'regnet-x-040': ImageNetPreTrainedConfig( depths=[2, 5, 14, 2] , hidden_sizes=[80, 240, 560, 1_360] , groups_width=40 , layer_type="""x""" ), 'regnet-x-064': ImageNetPreTrainedConfig( depths=[2, 4, 10, 1] , hidden_sizes=[168, 392, 784, 1_624] , groups_width=56 , layer_type="""x""" ), 'regnet-x-080': ImageNetPreTrainedConfig( depths=[2, 5, 15, 1] , hidden_sizes=[80, 240, 720, 1_920] , groups_width=120 , layer_type="""x""" ), 'regnet-x-120': ImageNetPreTrainedConfig( depths=[2, 5, 11, 1] , hidden_sizes=[224, 448, 896, 2_240] , groups_width=112 , layer_type="""x""" ), 'regnet-x-160': ImageNetPreTrainedConfig( depths=[2, 6, 13, 1] , hidden_sizes=[256, 512, 896, 2_048] , groups_width=128 , layer_type="""x""" ), 'regnet-x-320': ImageNetPreTrainedConfig( depths=[2, 7, 13, 1] , hidden_sizes=[336, 672, 1_344, 2_520] , groups_width=168 , layer_type="""x""" ), # y variant 'regnet-y-002': ImageNetPreTrainedConfig(depths=[1, 1, 4, 7] , hidden_sizes=[24, 56, 152, 368] , groups_width=8 ), 'regnet-y-004': ImageNetPreTrainedConfig( depths=[1, 3, 6, 6] , hidden_sizes=[48, 104, 208, 440] , groups_width=8 ), 'regnet-y-006': ImageNetPreTrainedConfig( depths=[1, 3, 7, 4] , hidden_sizes=[48, 112, 256, 608] , groups_width=16 ), 'regnet-y-008': ImageNetPreTrainedConfig( depths=[1, 3, 8, 2] , hidden_sizes=[64, 128, 320, 768] , groups_width=16 ), 'regnet-y-016': ImageNetPreTrainedConfig( depths=[2, 6, 17, 2] , hidden_sizes=[48, 120, 336, 888] , groups_width=24 ), 'regnet-y-032': ImageNetPreTrainedConfig( depths=[2, 5, 13, 1] , hidden_sizes=[72, 216, 576, 1_512] , groups_width=24 ), 'regnet-y-040': ImageNetPreTrainedConfig( depths=[2, 6, 12, 2] , hidden_sizes=[128, 192, 512, 1_088] , groups_width=64 ), 'regnet-y-064': ImageNetPreTrainedConfig( depths=[2, 7, 14, 2] , hidden_sizes=[144, 288, 576, 1_296] , groups_width=72 ), 'regnet-y-080': ImageNetPreTrainedConfig( depths=[2, 4, 10, 1] , hidden_sizes=[168, 448, 896, 2_016] , groups_width=56 ), 'regnet-y-120': ImageNetPreTrainedConfig( depths=[2, 5, 11, 1] , hidden_sizes=[224, 448, 896, 2_240] , groups_width=112 ), 'regnet-y-160': ImageNetPreTrainedConfig( depths=[2, 4, 11, 1] , hidden_sizes=[224, 448, 1_232, 3_024] , groups_width=112 ), 'regnet-y-320': ImageNetPreTrainedConfig( depths=[2, 5, 12, 1] , hidden_sizes=[232, 696, 1_392, 3_712] , groups_width=232 ), # models created by SEER -> https://arxiv.org/abs/2202.08360 'regnet-y-320-seer': RegNetConfig(depths=[2, 5, 12, 1] , hidden_sizes=[232, 696, 1_392, 3_712] , groups_width=232 ), 'regnet-y-640-seer': RegNetConfig(depths=[2, 5, 12, 1] , hidden_sizes=[328, 984, 1_968, 4_920] , groups_width=328 ), 'regnet-y-1280-seer': RegNetConfig( depths=[2, 7, 17, 1] , hidden_sizes=[528, 1_056, 2_904, 7_392] , groups_width=264 ), 'regnet-y-2560-seer': RegNetConfig( depths=[3, 7, 16, 1] , hidden_sizes=[640, 1_696, 2_544, 5_088] , groups_width=640 ), 'regnet-y-10b-seer': ImageNetPreTrainedConfig( depths=[2, 7, 17, 1] , hidden_sizes=[2_020, 4_040, 11_110, 28_280] , groups_width=1_010 ), # finetuned on imagenet 'regnet-y-320-seer-in1k': ImageNetPreTrainedConfig( depths=[2, 5, 12, 1] , hidden_sizes=[232, 696, 1_392, 3_712] , groups_width=232 ), 'regnet-y-640-seer-in1k': ImageNetPreTrainedConfig( depths=[2, 5, 12, 1] , hidden_sizes=[328, 984, 1_968, 4_920] , groups_width=328 ), 'regnet-y-1280-seer-in1k': ImageNetPreTrainedConfig( depths=[2, 7, 17, 1] , hidden_sizes=[528, 1_056, 2_904, 7_392] , groups_width=264 ), 'regnet-y-2560-seer-in1k': ImageNetPreTrainedConfig( depths=[3, 7, 16, 1] , hidden_sizes=[640, 1_696, 2_544, 5_088] , groups_width=640 ), 'regnet-y-10b-seer-in1k': ImageNetPreTrainedConfig( depths=[2, 7, 17, 1] , hidden_sizes=[2_020, 4_040, 11_110, 28_280] , groups_width=1_010 ), } lowerCAmelCase = NameToOurModelFuncMap() lowerCAmelCase = NameToFromModelFuncMap() # add seer weights logic def load_using_classy_vision(snake_case__ : Dict , snake_case__ : Optional[int] ) -> Tuple[nn.Module, Dict]: lowerCAmelCase = torch.hub.load_state_dict_from_url(a_ , model_dir=str(a_ ) , map_location="""cpu""" ) lowerCAmelCase = model_func() # check if we have a head, if yes add it lowerCAmelCase = files['classy_state_dict']['base_model']['model'] lowerCAmelCase = model_state_dict['trunk'] model.load_state_dict(a_ ) return model.eval(), model_state_dict["heads"] # pretrained lowerCAmelCase = partial( a_ , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet32d/seer_regnet32gf_model_iteration244000.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) lowerCAmelCase = partial( a_ , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet64/seer_regnet64gf_model_final_checkpoint_phase0.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) lowerCAmelCase = partial( a_ , """https://dl.fbaipublicfiles.com/vissl/model_zoo/swav_ig1b_regnet128Gf_cnstant_bs32_node16_sinkhorn10_proto16k_syncBN64_warmup8k/model_final_checkpoint_phase0.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ) , ) lowerCAmelCase = partial( a_ , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet10B/model_iteration124500_conso.torch""" , lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=27 , group_width=1_010 , w_a=1_744 , w_a=620.83 , w_m=2.52 ) ) ) , ) # IN1K finetuned lowerCAmelCase = partial( a_ , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet32_finetuned_in1k_model_final_checkpoint_phase78.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) lowerCAmelCase = partial( a_ , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet64_finetuned_in1k_model_final_checkpoint_phase78.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) lowerCAmelCase = partial( a_ , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet128_finetuned_in1k_model_final_checkpoint_phase78.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ) , ) lowerCAmelCase = partial( a_ , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_10b_finetuned_in1k_model_phase28_conso.torch""" , lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=27 , group_width=1_010 , w_a=1_744 , w_a=620.83 , w_m=2.52 ) ) ) , ) if model_name: convert_weight_and_push( a_ , names_to_from_model_map[model_name] , names_to_ours_model_map[model_name] , names_to_config[model_name] , a_ , a_ , ) else: for model_name, config in names_to_config.items(): convert_weight_and_push( a_ , names_to_from_model_map[model_name] , names_to_ours_model_map[model_name] , a_ , a_ , a_ , ) return config, expected_shape if __name__ == "__main__": a = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default=None, type=str, help=( 'The name of the model you wish to convert, it must be one of the supported regnet* architecture,' ' currently: regnetx-*, regnety-*. If `None`, all of them will the converted.' ), ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=Path, required=True, help='Path to the output PyTorch model directory.', ) parser.add_argument( '--push_to_hub', default=True, type=bool, required=False, help='If True, push model and image processor to the hub.', ) a = parser.parse_args() a = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
155
import re def A ( a_ ) -> bool: __UpperCamelCase : Any =re.compile( r'^(?:0|94|\+94|0{2}94)' r'7(0|1|2|4|5|6|7|8)' r'(-| |)' r'\d{7}$' ) return bool(re.search(a_ ,a_ ) ) if __name__ == "__main__": A_ :List[str] = '''0094702343221''' print(is_sri_lankan_phone_number(phone))
71
0
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast from transformers.models.bert.tokenization_bert 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 AlignProcessor, EfficientNetImageProcessor @require_vision class lowerCamelCase ( unittest.TestCase ): '''simple docstring''' def lowercase__ ( self : Dict ) -> Optional[Any]: '''simple docstring''' A__ : Any =tempfile.mkdtemp() A__ : int =[ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] A__ : Dict =os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as vocab_writer: vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens] ) ) A__ : Any ={ 'do_resize': True, 'size': 20, 'do_center_crop': True, 'crop_size': 18, 'do_normalize': True, 'image_mean': [0.48145466, 0.4578275, 0.40821073], 'image_std': [0.26862954, 0.26130258, 0.27577711], } A__ : Optional[int] =os.path.join(self.tmpdirname , lowerCamelCase__ ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(lowerCamelCase__ , lowerCamelCase__ ) def lowercase__ ( self : Dict , **lowerCAmelCase_ : Optional[int] ) -> Tuple: '''simple docstring''' return BertTokenizer.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def lowercase__ ( self : int , **lowerCAmelCase_ : Optional[int] ) -> Optional[int]: '''simple docstring''' return BertTokenizerFast.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def lowercase__ ( self : str , **lowerCAmelCase_ : List[Any] ) -> Dict: '''simple docstring''' return EfficientNetImageProcessor.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def lowercase__ ( self : Union[str, Any] ) -> Optional[int]: '''simple docstring''' shutil.rmtree(self.tmpdirname ) def lowercase__ ( self : List[str] ) -> Tuple: '''simple docstring''' A__ : List[Any] =[np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] A__ : str =[Image.fromarray(np.moveaxis(lowerCamelCase__ , 0 , -1 ) ) for x in image_inputs] return image_inputs def lowercase__ ( self : Optional[Any] ) -> Any: '''simple docstring''' A__ : List[Any] =self.get_tokenizer() A__ : List[Any] =self.get_rust_tokenizer() A__ : Optional[Any] =self.get_image_processor() A__ : Any =AlignProcessor(tokenizer=lowerCamelCase__ , image_processor=lowerCamelCase__ ) processor_slow.save_pretrained(self.tmpdirname ) A__ : Any =AlignProcessor.from_pretrained(self.tmpdirname , use_fast=lowerCamelCase__ ) A__ : Union[str, Any] =AlignProcessor(tokenizer=lowerCamelCase__ , image_processor=lowerCamelCase__ ) processor_fast.save_pretrained(self.tmpdirname ) A__ : List[Any] =AlignProcessor.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 , lowerCamelCase__ ) self.assertIsInstance(processor_fast.tokenizer , lowerCamelCase__ ) 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 , lowerCamelCase__ ) self.assertIsInstance(processor_fast.image_processor , lowerCamelCase__ ) def lowercase__ ( self : List[Any] ) -> int: '''simple docstring''' A__ : Optional[Any] =AlignProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ : int =self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ : Optional[int] =self.get_image_processor(do_normalize=lowerCamelCase__ , padding_value=1.0 ) A__ : List[Any] =AlignProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=lowerCamelCase__ , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , lowerCamelCase__ ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , lowerCamelCase__ ) def lowercase__ ( self : List[str] ) -> List[str]: '''simple docstring''' A__ : Tuple =self.get_image_processor() A__ : int =self.get_tokenizer() A__ : List[Any] =AlignProcessor(tokenizer=lowerCamelCase__ , image_processor=lowerCamelCase__ ) A__ : Optional[int] =self.prepare_image_inputs() A__ : Union[str, Any] =image_processor(lowerCamelCase__ , return_tensors="""np""" ) A__ : Union[str, Any] =processor(images=lowerCamelCase__ , 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 lowercase__ ( self : List[Any] ) -> Optional[int]: '''simple docstring''' A__ : int =self.get_image_processor() A__ : Dict =self.get_tokenizer() A__ : List[Any] =AlignProcessor(tokenizer=lowerCamelCase__ , image_processor=lowerCamelCase__ ) A__ : Union[str, Any] ='lower newer' A__ : Union[str, Any] =processor(text=lowerCamelCase__ ) A__ : List[str] =tokenizer(lowerCamelCase__ , padding="""max_length""" , max_length=64 ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def lowercase__ ( self : str ) -> List[str]: '''simple docstring''' A__ : Tuple =self.get_image_processor() A__ : Tuple =self.get_tokenizer() A__ : Optional[int] =AlignProcessor(tokenizer=lowerCamelCase__ , image_processor=lowerCamelCase__ ) A__ : Optional[Any] ='lower newer' A__ : str =self.prepare_image_inputs() A__ : Optional[Any] =processor(text=lowerCamelCase__ , images=lowerCamelCase__ ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(lowerCamelCase__ ): processor() def lowercase__ ( self : List[Any] ) -> Optional[Any]: '''simple docstring''' A__ : Optional[int] =self.get_image_processor() A__ : Dict =self.get_tokenizer() A__ : int =AlignProcessor(tokenizer=lowerCamelCase__ , image_processor=lowerCamelCase__ ) A__ : int =[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A__ : int =processor.batch_decode(lowerCamelCase__ ) A__ : Dict =tokenizer.batch_decode(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) def lowercase__ ( self : str ) -> Dict: '''simple docstring''' A__ : List[str] =self.get_image_processor() A__ : Tuple =self.get_tokenizer() A__ : List[str] =AlignProcessor(tokenizer=lowerCamelCase__ , image_processor=lowerCamelCase__ ) A__ : Tuple ='lower newer' A__ : List[Any] =self.prepare_image_inputs() A__ : List[Any] =processor(text=lowerCamelCase__ , images=lowerCamelCase__ ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
134
A_ :str = '''0.21.0''' from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
71
0
import gc import random import unittest import numpy as np import torch from diffusers import ( DDIMScheduler, KandinskyVaaControlnetPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class A ( _UpperCAmelCase , unittest.TestCase ): """simple docstring""" lowerCamelCase = KandinskyVaaControlnetPipeline lowerCamelCase = ["""image_embeds""", """negative_image_embeds""", """hint"""] lowerCamelCase = ["""image_embeds""", """negative_image_embeds""", """hint"""] lowerCamelCase = [ """generator""", """height""", """width""", """latents""", """guidance_scale""", """num_inference_steps""", """return_dict""", """guidance_scale""", """num_images_per_prompt""", """output_type""", """return_dict""", ] lowerCamelCase = False @property def snake_case__ ( self : Optional[int] )-> str: '''simple docstring''' return 3_2 @property def snake_case__ ( self : Dict )-> Tuple: '''simple docstring''' return 3_2 @property def snake_case__ ( self : Dict )-> Union[str, Any]: '''simple docstring''' return self.time_input_dim @property def snake_case__ ( self : List[str] )-> Tuple: '''simple docstring''' return self.time_input_dim * 4 @property def snake_case__ ( self : List[str] )-> Union[str, Any]: '''simple docstring''' return 1_0_0 @property def snake_case__ ( self : Tuple )-> Optional[Any]: '''simple docstring''' torch.manual_seed(0 ) A__ = { 'in_channels': 8, # Out channels is double in channels because predicts mean and variance 'out_channels': 8, 'addition_embed_type': 'image_hint', 'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'), 'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'), 'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn', 'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2), 'layers_per_block': 1, 'encoder_hid_dim': self.text_embedder_hidden_size, 'encoder_hid_dim_type': 'image_proj', 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': None, } A__ = UNetaDConditionModel(**lowerCamelCase__ ) return model @property def snake_case__ ( self : Tuple )-> Optional[Any]: '''simple docstring''' return { "block_out_channels": [3_2, 3_2, 6_4, 6_4], "down_block_types": [ "DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D", "AttnDownEncoderBlock2D", ], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 1_2, "out_channels": 3, "up_block_types": ["AttnUpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"], "vq_embed_dim": 4, } @property def snake_case__ ( self : List[str] )-> int: '''simple docstring''' torch.manual_seed(0 ) A__ = VQModel(**self.dummy_movq_kwargs ) return model def snake_case__ ( self : Dict )-> Union[str, Any]: '''simple docstring''' A__ = self.dummy_unet A__ = self.dummy_movq A__ = DDIMScheduler( num_train_timesteps=1_0_0_0,beta_schedule='linear',beta_start=0.00_085,beta_end=0.012,clip_sample=lowerCamelCase__,set_alpha_to_one=lowerCamelCase__,steps_offset=1,prediction_type='epsilon',thresholding=lowerCamelCase__,) A__ = { 'unet': unet, 'scheduler': scheduler, 'movq': movq, } return components def snake_case__ ( self : List[Any],lowercase_ : Optional[Any],lowercase_ : Optional[Any]=0 )-> Dict: '''simple docstring''' A__ = floats_tensor((1, self.text_embedder_hidden_size),rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) A__ = floats_tensor((1, self.text_embedder_hidden_size),rng=random.Random(seed + 1 ) ).to( lowerCamelCase__ ) # create hint A__ = floats_tensor((1, 3, 6_4, 6_4),rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) if str(lowerCamelCase__ ).startswith('mps' ): A__ = torch.manual_seed(lowerCamelCase__ ) else: A__ = torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) A__ = { 'image_embeds': image_embeds, 'negative_image_embeds': negative_image_embeds, 'hint': hint, 'generator': generator, 'height': 6_4, 'width': 6_4, 'guidance_scale': 4.0, 'num_inference_steps': 2, 'output_type': 'np', } return inputs def snake_case__ ( self : Any )-> Tuple: '''simple docstring''' A__ = 'cpu' A__ = self.get_dummy_components() A__ = self.pipeline_class(**lowerCamelCase__ ) A__ = pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) A__ = pipe(**self.get_dummy_inputs(lowerCamelCase__ ) ) A__ = output.images A__ = pipe( **self.get_dummy_inputs(lowerCamelCase__ ),return_dict=lowerCamelCase__,)[0] A__ = image[0, -3:, -3:, -1] A__ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) A__ = np.array( [0.6_959_826, 0.868_279, 0.7_558_092, 0.68_769_467, 0.85_805_804, 0.65_977_496, 0.44_885_302, 0.5_959_111, 0.4_251_595] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), F' expected_slice {expected_slice}, but got {image_slice.flatten()}' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), F' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}' @slow @require_torch_gpu class A ( unittest.TestCase ): """simple docstring""" def snake_case__ ( self : str )-> int: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def snake_case__ ( self : List[str] )-> Tuple: '''simple docstring''' A__ = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinskyv22/kandinskyv22_controlnet_robotcat_fp16.npy' ) A__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinskyv22/hint_image_cat.png' ) A__ = torch.from_numpy(np.array(lowerCamelCase__ ) ).float() / 255.0 A__ = hint.permute(2,0,1 ).unsqueeze(0 ) A__ = KandinskyVaaPriorPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-2-prior',torch_dtype=torch.floataa ) pipe_prior.to(lowerCamelCase__ ) A__ = KandinskyVaaControlnetPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-2-controlnet-depth',torch_dtype=torch.floataa ) A__ = pipeline.to(lowerCamelCase__ ) pipeline.set_progress_bar_config(disable=lowerCamelCase__ ) A__ = 'A robot, 4k photo' A__ = torch.Generator(device='cuda' ).manual_seed(0 ) A__ = pipe_prior( lowerCamelCase__,generator=lowerCamelCase__,num_inference_steps=5,negative_prompt='',).to_tuple() A__ = torch.Generator(device='cuda' ).manual_seed(0 ) A__ = pipeline( image_embeds=lowerCamelCase__,negative_image_embeds=lowerCamelCase__,hint=lowerCamelCase__,generator=lowerCamelCase__,num_inference_steps=1_0_0,output_type='np',) A__ = output.images[0] assert image.shape == (5_1_2, 5_1_2, 3) assert_mean_pixel_difference(lowerCamelCase__,lowerCamelCase__ )
7
A_ :Union[str, Any] = { 0: '''0''', 1: '''1''', 2: '''2''', 3: '''3''', 4: '''4''', 5: '''5''', 6: '''6''', 7: '''7''', 8: '''8''', 9: '''9''', 10: '''a''', 11: '''b''', 12: '''c''', 13: '''d''', 14: '''e''', 15: '''f''', } def A ( a_ ) -> str: assert type(a_ ) in (int, float) and decimal == int(a_ ) __UpperCamelCase : Union[str, Any] =int(a_ ) __UpperCamelCase : List[str] ='' __UpperCamelCase : Optional[Any] =False if decimal < 0: __UpperCamelCase : Tuple =True decimal *= -1 while decimal > 0: __UpperCamelCase , __UpperCamelCase : Optional[Any] =divmod(a_ ,16 ) __UpperCamelCase : Tuple =values[remainder] + hexadecimal __UpperCamelCase : Dict ='0x' + hexadecimal if negative: __UpperCamelCase : int ='-' + hexadecimal return hexadecimal if __name__ == "__main__": import doctest doctest.testmod()
71
0
def UpperCAmelCase__ ( lowerCamelCase = 1000 ): lowercase :Dict = 2**power lowercase :List[Any] = 0 while n: lowercase :Any = r + n % 10, n // 10 return r if __name__ == "__main__": print(solution(int(str(input()).strip())))
236
import itertools import os from collections import Counter, defaultdict from concurrent.futures import ThreadPoolExecutor, as_completed import numpy as np import datasets from .execute import check_correctness A_ :List[str] = '''\ @misc{chen2021evaluating, title={Evaluating Large Language Models Trained on Code}, author={Mark Chen and Jerry Tworek and Heewoo Jun and Qiming Yuan \ and Henrique Ponde de Oliveira Pinto and Jared Kaplan and Harri Edwards \ and Yuri Burda and Nicholas Joseph and Greg Brockman and Alex Ray \ and Raul Puri and Gretchen Krueger and Michael Petrov and Heidy Khlaaf \ and Girish Sastry and Pamela Mishkin and Brooke Chan and Scott Gray \ and Nick Ryder and Mikhail Pavlov and Alethea Power and Lukasz Kaiser \ and Mohammad Bavarian and Clemens Winter and Philippe Tillet \ and Felipe Petroski Such and Dave Cummings and Matthias Plappert \ and Fotios Chantzis and Elizabeth Barnes and Ariel Herbert-Voss \ and William Hebgen Guss and Alex Nichol and Alex Paino and Nikolas Tezak \ and Jie Tang and Igor Babuschkin and Suchir Balaji and Shantanu Jain \ and William Saunders and Christopher Hesse and Andrew N. Carr \ and Jan Leike and Josh Achiam and Vedant Misra and Evan Morikawa \ and Alec Radford and Matthew Knight and Miles Brundage and Mira Murati \ and Katie Mayer and Peter Welinder and Bob McGrew and Dario Amodei \ and Sam McCandlish and Ilya Sutskever and Wojciech Zaremba}, year={2021}, eprint={2107.03374}, archivePrefix={arXiv}, primaryClass={cs.LG} } ''' A_ :Any = '''\ This metric implements the evaluation harness for the HumanEval problem solving dataset described in the paper "Evaluating Large Language Models Trained on Code" (https://arxiv.org/abs/2107.03374). ''' A_ :Tuple = ''' Calculates how good are predictions given some references, using certain scores Args: predictions: list of candidates to evaluate. Each candidates should be a list of strings with several code candidates to solve the problem. references: a list with a test for each prediction. Each test should evaluate the correctness of a code candidate. k: number of code candidates to consider in the evaluation (Default: [1, 10, 100]) num_workers: number of workers used to evaluate the canidate programs (Default: 4). timeout: Returns: pass_at_k: dict with pass rates for each k results: dict with granular results of each unittest Examples: >>> code_eval = datasets.load_metric("code_eval") >>> test_cases = ["assert add(2,3)==5"] >>> candidates = [["def add(a,b): return a*b", "def add(a, b): return a+b"]] >>> pass_at_k, results = code_eval.compute(references=test_cases, predictions=candidates, k=[1, 2]) >>> print(pass_at_k) {\'pass@1\': 0.5, \'pass@2\': 1.0} ''' A_ :List[str] = ''' ################################################################################ !!!WARNING!!! ################################################################################ The "code_eval" metric executes untrusted model-generated code in Python. Although it is highly unlikely that model-generated code will do something overtly malicious in response to this test suite, model-generated code may act destructively due to a lack of model capability or alignment. Users are strongly encouraged to sandbox this evaluation suite so that it does not perform destructive actions on their host or network. For more information on how OpenAI sandboxes its code, see the paper "Evaluating Large Language Models Trained on Code" (https://arxiv.org/abs/2107.03374). Once you have read this disclaimer and taken appropriate precautions, set the environment variable HF_ALLOW_CODE_EVAL="1". Within Python you can to this with: >>> import os >>> os.environ["HF_ALLOW_CODE_EVAL"] = "1" ################################################################################\ ''' A_ :Tuple = '''The MIT License Copyright (c) OpenAI (https://openai.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __A ( datasets.Metric ): """simple docstring""" def __lowercase ( self ): """simple docstring""" return datasets.MetricInfo( # This is the description that will appear on the metrics page. description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string' ) ), 'references': datasets.Value('string' ), } ) , homepage='https://github.com/openai/human-eval' , codebase_urls=['https://github.com/openai/human-eval'] , reference_urls=['https://github.com/openai/human-eval'] , license=_LICENSE , ) def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__=[1, 10, 100] , lowerCamelCase__=4 , lowerCamelCase__=3.0 ): """simple docstring""" if os.getenv('HF_ALLOW_CODE_EVAL' , 0 ) != "1": raise ValueError(_WARNING ) if os.name == "nt": raise NotImplementedError('This metric is currently not supported on Windows.' ) with ThreadPoolExecutor(max_workers=lowerCamelCase__ ) as executor: __UpperCamelCase : List[str] =[] __UpperCamelCase : Any =Counter() __UpperCamelCase : List[Any] =0 __UpperCamelCase : int =defaultdict(lowerCamelCase__ ) for task_id, (candidates, test_case) in enumerate(zip(lowerCamelCase__ , lowerCamelCase__ ) ): for candidate in candidates: __UpperCamelCase : str =candidate + '\n' + test_case __UpperCamelCase : Any =(test_program, timeout, task_id, completion_id[task_id]) __UpperCamelCase : Optional[Any] =executor.submit(lowerCamelCase__ , *lowerCamelCase__ ) futures.append(lowerCamelCase__ ) completion_id[task_id] += 1 n_samples += 1 for future in as_completed(lowerCamelCase__ ): __UpperCamelCase : str =future.result() results[result["task_id"]].append((result['completion_id'], result) ) __UpperCamelCase , __UpperCamelCase : int =[], [] for result in results.values(): result.sort() __UpperCamelCase : str =[r[1]['passed'] for r in result] total.append(len(lowerCamelCase__ ) ) correct.append(sum(lowerCamelCase__ ) ) __UpperCamelCase : Optional[int] =np.array(lowerCamelCase__ ) __UpperCamelCase : List[str] =np.array(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =k __UpperCamelCase : List[Any] ={f'pass@{k}': estimate_pass_at_k(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ).mean() for k in ks if (total >= k).all()} return pass_at_k, results def A ( a_ ,a_ ,a_ ) -> Optional[int]: def estimator(a_ ,a_ ,a_ ) -> float: if n - c < k: return 1.0 return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1 ,n + 1 ) ) if isinstance(a_ ,a_ ): __UpperCamelCase : Optional[int] =itertools.repeat(a_ ,len(a_ ) ) else: assert len(a_ ) == len(a_ ) __UpperCamelCase : List[Any] =iter(a_ ) return np.array([estimator(int(a_ ) ,int(a_ ) ,a_ ) for n, c in zip(a_ ,a_ )] )
71
0
"""simple docstring""" import logging import os import sys from dataclasses import dataclass, field from typing import Optional import numpy as np import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor import transformers from transformers import ( CONFIG_MAPPING, IMAGE_PROCESSOR_MAPPING, MODEL_FOR_MASKED_IMAGE_MODELING_MAPPING, AutoConfig, AutoImageProcessor, AutoModelForMaskedImageModeling, HfArgumentParser, Trainer, TrainingArguments, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version lowerCamelCase_ = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/image-pretraining/requirements.txt") lowerCamelCase_ = list(MODEL_FOR_MASKED_IMAGE_MODELING_MAPPING.keys()) lowerCamelCase_ = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class _SCREAMING_SNAKE_CASE: SCREAMING_SNAKE_CASE_ : Optional[str] = field( default='''cifar10''' , metadata={'''help''': '''Name of a dataset from the datasets package'''} ) SCREAMING_SNAKE_CASE_ : Optional[str] = field( default=A , metadata={'''help''': '''The configuration name of the dataset to use (via the datasets library).'''} ) SCREAMING_SNAKE_CASE_ : Optional[str] = field( default=A , metadata={'''help''': '''The column name of the images in the files. If not set, will try to use \'image\' or \'img\'.'''} , ) SCREAMING_SNAKE_CASE_ : Optional[str] = field(default=A , metadata={'''help''': '''A folder containing the training data.'''} ) SCREAMING_SNAKE_CASE_ : Optional[str] = field(default=A , metadata={'''help''': '''A folder containing the validation data.'''} ) SCREAMING_SNAKE_CASE_ : Optional[float] = field( default=0.15 , metadata={'''help''': '''Percent to split off of train for validation.'''} ) SCREAMING_SNAKE_CASE_ : int = field(default=32 , metadata={'''help''': '''The size of the square patches to use for masking.'''} ) SCREAMING_SNAKE_CASE_ : float = field( default=0.6 , metadata={'''help''': '''Percentage of patches to mask.'''} , ) SCREAMING_SNAKE_CASE_ : Optional[int] = field( default=A , metadata={ '''help''': ( '''For debugging purposes or quicker training, truncate the number of training examples to this ''' '''value if set.''' ) } , ) SCREAMING_SNAKE_CASE_ : Optional[int] = field( default=A , metadata={ '''help''': ( '''For debugging purposes or quicker training, truncate the number of evaluation examples to this ''' '''value if set.''' ) } , ) def _UpperCamelCase ( self ) -> List[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE :int = {} if self.train_dir is not None: __SCREAMING_SNAKE_CASE :Dict = self.train_dir if self.validation_dir is not None: __SCREAMING_SNAKE_CASE :Any = self.validation_dir __SCREAMING_SNAKE_CASE :Dict = data_files if data_files else None @dataclass class _SCREAMING_SNAKE_CASE: SCREAMING_SNAKE_CASE_ : str = field( default=A , metadata={ '''help''': ( '''The model checkpoint for weights initialization. Can be a local path to a pytorch_model.bin or a ''' '''checkpoint identifier on the hub. ''' '''Don\'t set if you want to train a model from scratch.''' ) } , ) SCREAMING_SNAKE_CASE_ : Optional[str] = field( default=A , metadata={'''help''': '''If training from scratch, pass a model type from the list: ''' + ''', '''.join(A )} , ) SCREAMING_SNAKE_CASE_ : Optional[str] = field( default=A , metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) SCREAMING_SNAKE_CASE_ : Optional[str] = field( default=A , metadata={ '''help''': ( '''Override some existing default config settings when a model is trained from scratch. Example: ''' '''n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index''' ) } , ) SCREAMING_SNAKE_CASE_ : Optional[str] = field( default=A , metadata={'''help''': '''Where do you want to store (cache) the pretrained models/datasets downloaded from the hub'''} , ) SCREAMING_SNAKE_CASE_ : str = field( default='''main''' , metadata={'''help''': '''The specific model version to use (can be a branch name, tag name or commit id).'''} , ) SCREAMING_SNAKE_CASE_ : str = field(default=A , metadata={'''help''': '''Name or path of preprocessor config.'''} ) SCREAMING_SNAKE_CASE_ : bool = field( default=A , metadata={ '''help''': ( '''Will use the token generated when running `huggingface-cli login` (necessary to use this script ''' '''with private models).''' ) } , ) SCREAMING_SNAKE_CASE_ : Optional[int] = field( default=A , metadata={ '''help''': ( '''The size (resolution) of each image. If not specified, will use `image_size` of the configuration.''' ) } , ) SCREAMING_SNAKE_CASE_ : Optional[int] = field( default=A , metadata={ '''help''': ( '''The size (resolution) of each patch. If not specified, will use `patch_size` of the configuration.''' ) } , ) SCREAMING_SNAKE_CASE_ : Optional[int] = field( default=A , metadata={'''help''': '''Stride to use for the encoder.'''} , ) class _SCREAMING_SNAKE_CASE: def __init__( self ,SCREAMING_SNAKE_CASE__=1_92 ,SCREAMING_SNAKE_CASE__=32 ,SCREAMING_SNAKE_CASE__=4 ,SCREAMING_SNAKE_CASE__=0.6 ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE :int = input_size __SCREAMING_SNAKE_CASE :Any = mask_patch_size __SCREAMING_SNAKE_CASE :List[Any] = model_patch_size __SCREAMING_SNAKE_CASE :Union[str, Any] = mask_ratio if self.input_size % self.mask_patch_size != 0: raise ValueError('''Input size must be divisible by mask patch size''' ) if self.mask_patch_size % self.model_patch_size != 0: raise ValueError('''Mask patch size must be divisible by model patch size''' ) __SCREAMING_SNAKE_CASE :Any = self.input_size // self.mask_patch_size __SCREAMING_SNAKE_CASE :Dict = self.mask_patch_size // self.model_patch_size __SCREAMING_SNAKE_CASE :List[Any] = self.rand_size**2 __SCREAMING_SNAKE_CASE :List[Any] = int(np.ceil(self.token_count * self.mask_ratio ) ) def __call__( self ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE :str = np.random.permutation(self.token_count )[: self.mask_count] __SCREAMING_SNAKE_CASE :Optional[int] = np.zeros(self.token_count ,dtype=lowerCamelCase__ ) __SCREAMING_SNAKE_CASE :int = 1 __SCREAMING_SNAKE_CASE :Any = mask.reshape((self.rand_size, self.rand_size) ) __SCREAMING_SNAKE_CASE :Tuple = mask.repeat(self.scale ,axis=0 ).repeat(self.scale ,axis=1 ) return torch.tensor(mask.flatten() ) def __lowerCamelCase ( a_ : List[str] ) -> int: __SCREAMING_SNAKE_CASE :List[str] = torch.stack([example['''pixel_values'''] for example in examples] ) __SCREAMING_SNAKE_CASE :Dict = torch.stack([example['''mask'''] for example in examples] ) return {"pixel_values": pixel_values, "bool_masked_pos": mask} def __lowerCamelCase ( ) -> List[str]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. __SCREAMING_SNAKE_CASE :str = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. __SCREAMING_SNAKE_CASE :Tuple = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: __SCREAMING_SNAKE_CASE :Dict = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('''run_mim''' , a_ , a_ ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() __SCREAMING_SNAKE_CASE :Optional[Any] = training_args.get_process_log_level() logger.setLevel(a_ ) transformers.utils.logging.set_verbosity(a_ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}''' + f'''distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}''' ) logger.info(f'''Training/evaluation parameters {training_args}''' ) # Detecting last checkpoint. __SCREAMING_SNAKE_CASE :Any = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: __SCREAMING_SNAKE_CASE :int = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Initialize our dataset. __SCREAMING_SNAKE_CASE :Union[str, Any] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. __SCREAMING_SNAKE_CASE :int = None if 'validation' in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , a_ ) and data_args.train_val_split > 0.0: __SCREAMING_SNAKE_CASE :int = ds['train'].train_test_split(data_args.train_val_split ) __SCREAMING_SNAKE_CASE :Optional[Any] = split['train'] __SCREAMING_SNAKE_CASE :List[str] = split['test'] # Create config # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. __SCREAMING_SNAKE_CASE :Dict = { 'cache_dir': model_args.cache_dir, 'revision': model_args.model_revision, 'use_auth_token': True if model_args.use_auth_token else None, } if model_args.config_name_or_path: __SCREAMING_SNAKE_CASE :Tuple = AutoConfig.from_pretrained(model_args.config_name_or_path , **a_ ) elif model_args.model_name_or_path: __SCREAMING_SNAKE_CASE :List[str] = AutoConfig.from_pretrained(model_args.model_name_or_path , **a_ ) else: __SCREAMING_SNAKE_CASE :Any = CONFIG_MAPPING[model_args.model_type]() logger.warning('''You are instantiating a new config instance from scratch.''' ) if model_args.config_overrides is not None: logger.info(f'''Overriding config: {model_args.config_overrides}''' ) config.update_from_string(model_args.config_overrides ) logger.info(f'''New config: {config}''' ) # make sure the decoder_type is "simmim" (only relevant for BEiT) if hasattr(a_ , '''decoder_type''' ): __SCREAMING_SNAKE_CASE :List[str] = 'simmim' # adapt config __SCREAMING_SNAKE_CASE :Dict = model_args.image_size if model_args.image_size is not None else config.image_size __SCREAMING_SNAKE_CASE :Optional[Any] = model_args.patch_size if model_args.patch_size is not None else config.patch_size __SCREAMING_SNAKE_CASE :int = ( model_args.encoder_stride if model_args.encoder_stride is not None else config.encoder_stride ) config.update( { '''image_size''': model_args.image_size, '''patch_size''': model_args.patch_size, '''encoder_stride''': model_args.encoder_stride, } ) # create image processor if model_args.image_processor_name: __SCREAMING_SNAKE_CASE :Optional[Any] = AutoImageProcessor.from_pretrained(model_args.image_processor_name , **a_ ) elif model_args.model_name_or_path: __SCREAMING_SNAKE_CASE :Tuple = AutoImageProcessor.from_pretrained(model_args.model_name_or_path , **a_ ) else: __SCREAMING_SNAKE_CASE :List[str] = { conf.model_type: image_processor_class for conf, image_processor_class in IMAGE_PROCESSOR_MAPPING.items() } __SCREAMING_SNAKE_CASE :Dict = IMAGE_PROCESSOR_TYPES[model_args.model_type]() # create model if model_args.model_name_or_path: __SCREAMING_SNAKE_CASE :List[Any] = AutoModelForMaskedImageModeling.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info('''Training new model from scratch''' ) __SCREAMING_SNAKE_CASE :Tuple = AutoModelForMaskedImageModeling.from_config(a_ ) if training_args.do_train: __SCREAMING_SNAKE_CASE :Union[str, Any] = ds['train'].column_names else: __SCREAMING_SNAKE_CASE :Dict = ds['validation'].column_names if data_args.image_column_name is not None: __SCREAMING_SNAKE_CASE :Optional[Any] = data_args.image_column_name elif "image" in column_names: __SCREAMING_SNAKE_CASE :List[Any] = 'image' elif "img" in column_names: __SCREAMING_SNAKE_CASE :Optional[int] = 'img' else: __SCREAMING_SNAKE_CASE :List[Any] = column_names[0] # transformations as done in original SimMIM paper # source: https://github.com/microsoft/SimMIM/blob/main/data/data_simmim.py __SCREAMING_SNAKE_CASE :int = Compose( [ Lambda(lambda a_ : img.convert('''RGB''' ) if img.mode != "RGB" else img ), RandomResizedCrop(model_args.image_size , scale=(0.67, 1.0) , ratio=(3.0 / 4.0, 4.0 / 3.0) ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) # create mask generator __SCREAMING_SNAKE_CASE :Tuple = MaskGenerator( input_size=model_args.image_size , mask_patch_size=data_args.mask_patch_size , model_patch_size=model_args.patch_size , mask_ratio=data_args.mask_ratio , ) def preprocess_images(a_ : Tuple ): __SCREAMING_SNAKE_CASE :Dict = [transforms(a_ ) for image in examples[image_column_name]] __SCREAMING_SNAKE_CASE :int = [mask_generator() for i in range(len(examples[image_column_name] ) )] return examples if training_args.do_train: if "train" not in ds: raise ValueError('''--do_train requires a train dataset''' ) if data_args.max_train_samples is not None: __SCREAMING_SNAKE_CASE :Optional[int] = ds['train'].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(a_ ) if training_args.do_eval: if "validation" not in ds: raise ValueError('''--do_eval requires a validation dataset''' ) if data_args.max_eval_samples is not None: __SCREAMING_SNAKE_CASE :int = ( ds['validation'].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(a_ ) # Initialize our trainer __SCREAMING_SNAKE_CASE :int = Trainer( model=a_ , args=a_ , train_dataset=ds['''train'''] if training_args.do_train else None , eval_dataset=ds['''validation'''] if training_args.do_eval else None , tokenizer=a_ , data_collator=a_ , ) # Training if training_args.do_train: __SCREAMING_SNAKE_CASE :Optional[Any] = None if training_args.resume_from_checkpoint is not None: __SCREAMING_SNAKE_CASE :List[str] = training_args.resume_from_checkpoint elif last_checkpoint is not None: __SCREAMING_SNAKE_CASE :List[Any] = last_checkpoint __SCREAMING_SNAKE_CASE :List[str] = trainer.train(resume_from_checkpoint=a_ ) trainer.save_model() trainer.log_metrics('''train''' , train_result.metrics ) trainer.save_metrics('''train''' , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: __SCREAMING_SNAKE_CASE :Optional[int] = trainer.evaluate() trainer.log_metrics('''eval''' , a_ ) trainer.save_metrics('''eval''' , a_ ) # Write model card and (optionally) push to hub __SCREAMING_SNAKE_CASE :Tuple = { 'finetuned_from': model_args.model_name_or_path, 'tasks': 'masked-image-modeling', 'dataset': data_args.dataset_name, 'tags': ['masked-image-modeling'], } if training_args.push_to_hub: trainer.push_to_hub(**a_ ) else: trainer.create_model_card(**a_ ) if __name__ == "__main__": main()
191
import gc import random import tempfile import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMInverseScheduler, DDIMScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, StableDiffusionDiffEditPipeline, UNetaDConditionModel, ) from diffusers.utils import load_image, slow from diffusers.utils.testing_utils import enable_full_determinism, floats_tensor, require_torch_gpu, torch_device from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __A ( a , a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : Optional[Any] =StableDiffusionDiffEditPipeline UpperCamelCase__ : str =TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""height""", """width""", """image"""} | {"""image_latents"""} UpperCamelCase__ : Optional[Any] =TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS - {"""image"""} | {"""image_latents"""} UpperCamelCase__ : Dict =frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess UpperCamelCase__ : Any =frozenset([] ) def __lowercase ( self ): """simple docstring""" torch.manual_seed(0 ) __UpperCamelCase : Dict =UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=lowerCamelCase__ , ) __UpperCamelCase : List[str] =DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=lowerCamelCase__ , set_alpha_to_one=lowerCamelCase__ , ) __UpperCamelCase : Union[str, Any] =DDIMInverseScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=lowerCamelCase__ , set_alpha_to_zero=lowerCamelCase__ , ) torch.manual_seed(0 ) __UpperCamelCase : Optional[int] =AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) __UpperCamelCase : Tuple =CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act='gelu' , projection_dim=512 , ) __UpperCamelCase : Any =CLIPTextModel(lowerCamelCase__ ) __UpperCamelCase : int =CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) __UpperCamelCase : Union[str, Any] ={ 'unet': unet, 'scheduler': scheduler, 'inverse_scheduler': inverse_scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" __UpperCamelCase : int =floats_tensor((1, 16, 16) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) __UpperCamelCase : Any =floats_tensor((1, 2, 4, 16, 16) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : Any =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : Optional[int] =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Dict ={ 'prompt': 'a dog and a newt', 'mask_image': mask, 'image_latents': latents, 'generator': generator, 'num_inference_steps': 2, 'inpaint_strength': 1.0, 'guidance_scale': 6.0, 'output_type': 'numpy', } return inputs def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" __UpperCamelCase : Tuple =floats_tensor((1, 3, 32, 32) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) __UpperCamelCase : int =image.cpu().permute(0 , 2 , 3 , 1 )[0] __UpperCamelCase : Optional[Any] =Image.fromarray(np.uinta(lowerCamelCase__ ) ).convert('RGB' ) if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : List[Any] =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : Any =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Optional[int] ={ 'image': image, 'source_prompt': 'a cat and a frog', 'target_prompt': 'a dog and a newt', 'generator': generator, 'num_inference_steps': 2, 'num_maps_per_mask': 2, 'mask_encode_strength': 1.0, 'guidance_scale': 6.0, 'output_type': 'numpy', } return inputs def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" __UpperCamelCase : str =floats_tensor((1, 3, 32, 32) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) __UpperCamelCase : Any =image.cpu().permute(0 , 2 , 3 , 1 )[0] __UpperCamelCase : int =Image.fromarray(np.uinta(lowerCamelCase__ ) ).convert('RGB' ) if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : Any =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : int =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Optional[int] ={ 'image': image, 'prompt': 'a cat and a frog', 'generator': generator, 'num_inference_steps': 2, 'inpaint_strength': 1.0, 'guidance_scale': 6.0, 'decode_latents': True, 'output_type': 'numpy', } return inputs def __lowercase ( self ): """simple docstring""" if not hasattr(self.pipeline_class , '_optional_components' ): return __UpperCamelCase : Optional[Any] =self.get_dummy_components() __UpperCamelCase : List[str] =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) # set all optional components to None and update pipeline config accordingly for optional_component in pipe._optional_components: setattr(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) pipe.register_modules(**{optional_component: None for optional_component in pipe._optional_components} ) __UpperCamelCase : Union[str, Any] =self.get_dummy_inputs(lowerCamelCase__ ) __UpperCamelCase : List[Any] =pipe(**lowerCamelCase__ )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : Tuple =self.pipeline_class.from_pretrained(lowerCamelCase__ ) pipe_loaded.to(lowerCamelCase__ ) pipe_loaded.set_progress_bar_config(disable=lowerCamelCase__ ) for optional_component in pipe._optional_components: self.assertTrue( getattr(lowerCamelCase__ , lowerCamelCase__ ) is None , f'`{optional_component}` did not stay set to None after loading.' , ) __UpperCamelCase : str =self.get_dummy_inputs(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =pipe_loaded(**lowerCamelCase__ )[0] __UpperCamelCase : Tuple =np.abs(output - output_loaded ).max() self.assertLess(lowerCamelCase__ , 1E-4 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Any ='cpu' __UpperCamelCase : Union[str, Any] =self.get_dummy_components() __UpperCamelCase : Any =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : int =self.get_dummy_mask_inputs(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =pipe.generate_mask(**lowerCamelCase__ ) __UpperCamelCase : int =mask[0, -3:, -3:] self.assertEqual(mask.shape , (1, 16, 16) ) __UpperCamelCase : Tuple =np.array([0] * 9 ) __UpperCamelCase : str =np.abs(mask_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) self.assertEqual(mask[0, -3, -4] , 0 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : int ='cpu' __UpperCamelCase : Union[str, Any] =self.get_dummy_components() __UpperCamelCase : Optional[Any] =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Dict =self.get_dummy_inversion_inputs(lowerCamelCase__ ) __UpperCamelCase : List[Any] =pipe.invert(**lowerCamelCase__ ).images __UpperCamelCase : Optional[Any] =image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) __UpperCamelCase : List[str] =np.array( [0.5_150, 0.5_134, 0.5_043, 0.5_376, 0.4_694, 0.51_050, 0.5_015, 0.4_407, 0.4_799] , ) __UpperCamelCase : int =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) def __lowercase ( self ): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=5E-3 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] ='cpu' __UpperCamelCase : int =self.get_dummy_components() __UpperCamelCase : str ={'beta_start': 0.00_085, 'beta_end': 0.012, 'beta_schedule': 'scaled_linear'} __UpperCamelCase : str =DPMSolverMultistepScheduler(**lowerCamelCase__ ) __UpperCamelCase : Dict =DPMSolverMultistepInverseScheduler(**lowerCamelCase__ ) __UpperCamelCase : Any =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Tuple =self.get_dummy_inversion_inputs(lowerCamelCase__ ) __UpperCamelCase : str =pipe.invert(**lowerCamelCase__ ).images __UpperCamelCase : List[Any] =image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) __UpperCamelCase : List[str] =np.array( [0.5_150, 0.5_134, 0.5_043, 0.5_376, 0.4_694, 0.51_050, 0.5_015, 0.4_407, 0.4_799] , ) __UpperCamelCase : Optional[Any] =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) @require_torch_gpu @slow class __A ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @classmethod def __lowercase ( cls ): """simple docstring""" __UpperCamelCase : Optional[int] =load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/diffedit/fruit.png' ) __UpperCamelCase : Union[str, Any] =raw_image.convert('RGB' ).resize((768, 768) ) __UpperCamelCase : List[Any] =raw_image def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[int] =torch.manual_seed(0 ) __UpperCamelCase : Dict =StableDiffusionDiffEditPipeline.from_pretrained( 'stabilityai/stable-diffusion-2-1' , safety_checker=lowerCamelCase__ , torch_dtype=torch.floataa ) __UpperCamelCase : List[str] =DDIMScheduler.from_config(pipe.scheduler.config ) __UpperCamelCase : List[str] =DDIMInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : List[str] ='a bowl of fruit' __UpperCamelCase : Dict ='a bowl of pears' __UpperCamelCase : Tuple =pipe.generate_mask( image=self.raw_image , source_prompt=lowerCamelCase__ , target_prompt=lowerCamelCase__ , generator=lowerCamelCase__ , ) __UpperCamelCase : int =pipe.invert( prompt=lowerCamelCase__ , image=self.raw_image , inpaint_strength=0.7 , generator=lowerCamelCase__ ).latents __UpperCamelCase : Dict =pipe( prompt=lowerCamelCase__ , mask_image=lowerCamelCase__ , image_latents=lowerCamelCase__ , generator=lowerCamelCase__ , negative_prompt=lowerCamelCase__ , inpaint_strength=0.7 , output_type='numpy' , ).images[0] __UpperCamelCase : str =( np.array( load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/diffedit/pears.png' ).resize((768, 768) ) ) / 255 ) assert np.abs((expected_image - image).max() ) < 5E-1 def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Any =torch.manual_seed(0 ) __UpperCamelCase : List[Any] =StableDiffusionDiffEditPipeline.from_pretrained( 'stabilityai/stable-diffusion-2-1' , safety_checker=lowerCamelCase__ , torch_dtype=torch.floataa ) __UpperCamelCase : Optional[Any] =DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) __UpperCamelCase : Optional[int] =DPMSolverMultistepInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Optional[Any] ='a bowl of fruit' __UpperCamelCase : int ='a bowl of pears' __UpperCamelCase : str =pipe.generate_mask( image=self.raw_image , source_prompt=lowerCamelCase__ , target_prompt=lowerCamelCase__ , generator=lowerCamelCase__ , ) __UpperCamelCase : List[str] =pipe.invert( prompt=lowerCamelCase__ , image=self.raw_image , inpaint_strength=0.7 , generator=lowerCamelCase__ , num_inference_steps=25 , ).latents __UpperCamelCase : List[str] =pipe( prompt=lowerCamelCase__ , mask_image=lowerCamelCase__ , image_latents=lowerCamelCase__ , generator=lowerCamelCase__ , negative_prompt=lowerCamelCase__ , inpaint_strength=0.7 , num_inference_steps=25 , output_type='numpy' , ).images[0] __UpperCamelCase : Tuple =( np.array( load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/diffedit/pears.png' ).resize((768, 768) ) ) / 255 ) assert np.abs((expected_image - image).max() ) < 5E-1
71
0
"""simple docstring""" import os import socket from contextlib import contextmanager import torch from ..commands.config.default import write_basic_config # noqa: F401 from ..state import PartialState from .dataclasses import DistributedType from .imports import is_deepspeed_available, is_tpu_available from .transformer_engine import convert_model from .versions import is_torch_version if is_deepspeed_available(): from deepspeed import DeepSpeedEngine if is_tpu_available(check_device=False): import torch_xla.core.xla_model as xm def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[int] ) -> Optional[Any]: if is_torch_version("""<""" ,"""2.0.0""" ) or not hasattr(a_ ,"""_dynamo""" ): return False return isinstance(a_ ,torch._dynamo.eval_frame.OptimizedModule ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Dict ,_lowerCamelCase : int = True ) -> Optional[int]: _lowerCAmelCase : Union[str, Any] = (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel) _lowerCAmelCase : List[Any] = is_compiled_module(a_ ) if is_compiled: _lowerCAmelCase : Optional[Any] = model _lowerCAmelCase : List[Any] = model._orig_mod if is_deepspeed_available(): options += (DeepSpeedEngine,) while isinstance(a_ ,a_ ): _lowerCAmelCase : Any = model.module if not keep_fpaa_wrapper: _lowerCAmelCase : Any = getattr(a_ ,"""forward""" ) _lowerCAmelCase : List[Any] = model.__dict__.pop("""_original_forward""" ,a_ ) if original_forward is not None: while hasattr(a_ ,"""__wrapped__""" ): _lowerCAmelCase : Optional[int] = forward.__wrapped__ if forward == original_forward: break _lowerCAmelCase : str = forward if getattr(a_ ,"""_converted_to_transformer_engine""" ,a_ ): convert_model(a_ ,to_transformer_engine=a_ ) if is_compiled: _lowerCAmelCase : Dict = model _lowerCAmelCase : int = compiled_model return model def SCREAMING_SNAKE_CASE ( ) -> Optional[Any]: PartialState().wait_for_everyone() def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[Any] ,_lowerCamelCase : Tuple ) -> List[Any]: if PartialState().distributed_type == DistributedType.TPU: xm.save(a_ ,a_ ) elif PartialState().local_process_index == 0: torch.save(a_ ,a_ ) @contextmanager def SCREAMING_SNAKE_CASE ( **_lowerCamelCase : Optional[Any] ) -> int: for key, value in kwargs.items(): _lowerCAmelCase : Optional[Any] = str(a_ ) yield for key in kwargs: if key.upper() in os.environ: del os.environ[key.upper()] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[int] ) -> List[Any]: if not hasattr(a_ ,"""__qualname__""" ) and not hasattr(a_ ,"""__name__""" ): _lowerCAmelCase : int = getattr(a_ ,"""__class__""" ,a_ ) if hasattr(a_ ,"""__qualname__""" ): return obj.__qualname__ if hasattr(a_ ,"""__name__""" ): return obj.__name__ return str(a_ ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ,_lowerCamelCase : Optional[int] ) -> List[str]: for key, value in source.items(): if isinstance(a_ ,a_ ): _lowerCAmelCase : Optional[int] = destination.setdefault(a_ ,{} ) merge_dicts(a_ ,a_ ) else: _lowerCAmelCase : Optional[int] = value return destination def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[Any] = None ) -> bool: if port is None: _lowerCAmelCase : Any = 29500 with socket.socket(socket.AF_INET ,socket.SOCK_STREAM ) as s: return s.connect_ex(("""localhost""", port) ) == 0
44
import random from .binary_exp_mod import bin_exp_mod def A ( a_ ,a_=1_000 ) -> Optional[Any]: if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd __UpperCamelCase : List[Any] =n - 1 __UpperCamelCase : Dict =0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) __UpperCamelCase : Optional[Any] =0 while count < prec: __UpperCamelCase : Dict =random.randint(2 ,n - 1 ) __UpperCamelCase : Optional[Any] =bin_exp_mod(a_ ,a_ ,a_ ) if b != 1: __UpperCamelCase : List[str] =True for _ in range(a_ ): if b == n - 1: __UpperCamelCase : Tuple =False break __UpperCamelCase : Dict =b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": A_ :str = abs(int(input('''Enter bound : ''').strip())) print('''Here\'s the list of primes:''') print(''', '''.join(str(i) for i in range(n + 1) if is_prime_big(i)))
71
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''andreasmadsen/efficient_mlm_m0.40''': ( '''https://huggingface.co/andreasmadsen/efficient_mlm_m0.40/resolve/main/config.json''' ), } class __lowerCAmelCase ( A ): UpperCamelCase = """roberta-prelayernorm""" def __init__( self : List[Any] , A : Tuple=5_02_65 , A : Optional[Any]=7_68 , A : Union[str, Any]=12 , A : Any=12 , A : str=30_72 , A : List[str]="gelu" , A : Tuple=0.1 , A : Any=0.1 , A : List[str]=5_12 , A : int=2 , A : int=0.0_2 , A : Optional[int]=1E-12 , A : List[str]=1 , A : Optional[Any]=0 , A : List[str]=2 , A : Optional[int]="absolute" , A : Any=True , A : Tuple=None , **A : Union[str, Any] , ) -> Union[str, Any]: """simple docstring""" super().__init__(pad_token_id=lowerCamelCase__ , bos_token_id=lowerCamelCase__ , eos_token_id=lowerCamelCase__ , **lowerCamelCase__) _UpperCAmelCase = vocab_size _UpperCAmelCase = hidden_size _UpperCAmelCase = num_hidden_layers _UpperCAmelCase = num_attention_heads _UpperCAmelCase = hidden_act _UpperCAmelCase = intermediate_size _UpperCAmelCase = hidden_dropout_prob _UpperCAmelCase = attention_probs_dropout_prob _UpperCAmelCase = max_position_embeddings _UpperCAmelCase = type_vocab_size _UpperCAmelCase = initializer_range _UpperCAmelCase = layer_norm_eps _UpperCAmelCase = position_embedding_type _UpperCAmelCase = use_cache _UpperCAmelCase = classifier_dropout class __lowerCAmelCase ( A ): @property def _lowerCamelCase ( self : List[Any]) -> int: """simple docstring""" if self.task == "multiple-choice": _UpperCAmelCase = {0: 'batch', 1: 'choice', 2: 'sequence'} else: _UpperCAmelCase = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ])
339
from torch import nn class __A ( nn.Module ): """simple docstring""" def __init__( self , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" super().__init__() __UpperCamelCase : Dict =class_size __UpperCamelCase : Any =embed_size # self.mlp1 = nn.Linear(embed_size, embed_size) # self.mlp2 = (nn.Linear(embed_size, class_size)) __UpperCamelCase : Any =nn.Linear(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : List[Any] =self.mlp(lowerCamelCase__ ) return logits
71
0
"""simple docstring""" def __lowerCamelCase ( __UpperCamelCase , __UpperCamelCase ) -> list: """simple docstring""" lowerCAmelCase_ : Tuple = len(a_ ) lowerCAmelCase_ : Tuple = [] for i in range(len(a_ ) - pat_len + 1 ): lowerCAmelCase_ : List[Any] = True for j in range(a_ ): if s[i + j] != pattern[j]: lowerCAmelCase_ : Union[str, Any] = False break if match_found: position.append(a_ ) return position if __name__ == "__main__": assert naive_pattern_search("""ABCDEFG""", """DE""") == [3] print(naive_pattern_search("""ABAAABCDBBABCDDEBCABC""", """ABC"""))
241
def A ( a_ ,a_ ,a_ ) -> int: def update_area_of_max_square(a_ ,a_ ) -> int: # BASE CASE if row >= rows or col >= cols: return 0 __UpperCamelCase : Optional[int] =update_area_of_max_square(a_ ,col + 1 ) __UpperCamelCase : List[str] =update_area_of_max_square(row + 1 ,col + 1 ) __UpperCamelCase : List[Any] =update_area_of_max_square(row + 1 ,a_ ) if mat[row][col]: __UpperCamelCase : Optional[Any] =1 + min([right, diagonal, down] ) __UpperCamelCase : Dict =max(largest_square_area[0] ,a_ ) return sub_problem_sol else: return 0 __UpperCamelCase : Union[str, Any] =[0] update_area_of_max_square(0 ,0 ) return largest_square_area[0] def A ( a_ ,a_ ,a_ ) -> int: def update_area_of_max_square_using_dp_array( a_ ,a_ ,a_ ) -> int: if row >= rows or col >= cols: return 0 if dp_array[row][col] != -1: return dp_array[row][col] __UpperCamelCase : Tuple =update_area_of_max_square_using_dp_array(a_ ,col + 1 ,a_ ) __UpperCamelCase : Optional[int] =update_area_of_max_square_using_dp_array(row + 1 ,col + 1 ,a_ ) __UpperCamelCase : Any =update_area_of_max_square_using_dp_array(row + 1 ,a_ ,a_ ) if mat[row][col]: __UpperCamelCase : Optional[Any] =1 + min([right, diagonal, down] ) __UpperCamelCase : str =max(largest_square_area[0] ,a_ ) __UpperCamelCase : Any =sub_problem_sol return sub_problem_sol else: return 0 __UpperCamelCase : Tuple =[0] __UpperCamelCase : List[Any] =[[-1] * cols for _ in range(a_ )] update_area_of_max_square_using_dp_array(0 ,0 ,a_ ) return largest_square_area[0] def A ( a_ ,a_ ,a_ ) -> int: __UpperCamelCase : Dict =[[0] * (cols + 1) for _ in range(rows + 1 )] __UpperCamelCase : int =0 for row in range(rows - 1 ,-1 ,-1 ): for col in range(cols - 1 ,-1 ,-1 ): __UpperCamelCase : Optional[Any] =dp_array[row][col + 1] __UpperCamelCase : int =dp_array[row + 1][col + 1] __UpperCamelCase : Tuple =dp_array[row + 1][col] if mat[row][col] == 1: __UpperCamelCase : Tuple =1 + min(a_ ,a_ ,a_ ) __UpperCamelCase : Any =max(dp_array[row][col] ,a_ ) else: __UpperCamelCase : Dict =0 return largest_square_area def A ( a_ ,a_ ,a_ ) -> int: __UpperCamelCase : Any =[0] * (cols + 1) __UpperCamelCase : List[Any] =[0] * (cols + 1) __UpperCamelCase : Tuple =0 for row in range(rows - 1 ,-1 ,-1 ): for col in range(cols - 1 ,-1 ,-1 ): __UpperCamelCase : Any =current_row[col + 1] __UpperCamelCase : Optional[Any] =next_row[col + 1] __UpperCamelCase : Union[str, Any] =next_row[col] if mat[row][col] == 1: __UpperCamelCase : Any =1 + min(a_ ,a_ ,a_ ) __UpperCamelCase : Optional[int] =max(current_row[col] ,a_ ) else: __UpperCamelCase : List[str] =0 __UpperCamelCase : Optional[Any] =current_row return largest_square_area if __name__ == "__main__": import doctest doctest.testmod() print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))
71
0
import fire from utils import calculate_rouge, save_json def lowerCAmelCase_ ( A_ ,A_ ,A_=None ,**A_): UpperCamelCase__: Dict = [x.strip() for x in open(a_).readlines()] UpperCamelCase__: str = [x.strip() for x in open(a_).readlines()][: len(a_)] UpperCamelCase__: Union[str, Any] = calculate_rouge(a_ ,a_ ,**a_) if save_path is not None: save_json(a_ ,a_ ,indent=a_) return metrics # these print nicely if __name__ == "__main__": fire.Fire(calculate_rouge_path)
149
def A ( a_ ) -> int: __UpperCamelCase : Any =len(a_ ) while cur > 1: # Find the maximum number in arr __UpperCamelCase : Any =arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi __UpperCamelCase : Any =arr[mi::-1] + arr[mi + 1 : len(a_ )] # Reverse whole list __UpperCamelCase : str =arr[cur - 1 :: -1] + arr[cur : len(a_ )] cur -= 1 return arr if __name__ == "__main__": A_ :Dict = input('''Enter numbers separated by a comma:\n''').strip() A_ :Any = [int(item) for item in user_input.split(''',''')] print(pancake_sort(unsorted))
71
0
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging a : Union[str, Any] = logging.get_logger(__name__) a : List[Any] = { '''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 a ( _lowerCamelCase ): snake_case_ = """sew-d""" def __init__( self : List[str] , lowercase_ : Optional[int]=32 , lowercase_ : Dict=768 , lowercase_ : List[Any]=12 , lowercase_ : int=12 , lowercase_ : str=3072 , lowercase_ : Any=2 , lowercase_ : List[Any]=512 , lowercase_ : Dict=256 , lowercase_ : Union[str, Any]=True , lowercase_ : Optional[int]=True , lowercase_ : str=("p2c", "c2p") , lowercase_ : str="layer_norm" , lowercase_ : int="gelu_python" , lowercase_ : str=0.1 , lowercase_ : Union[str, Any]=0.1 , lowercase_ : Any=0.1 , lowercase_ : Any=0.0 , lowercase_ : int=0.1 , lowercase_ : List[str]=0.02 , lowercase_ : str=1e-7 , lowercase_ : Tuple=1e-5 , lowercase_ : Any="group" , lowercase_ : str="gelu" , lowercase_ : Any=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , lowercase_ : Optional[Any]=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , lowercase_ : List[str]=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , lowercase_ : List[Any]=False , lowercase_ : str=128 , lowercase_ : Tuple=16 , lowercase_ : List[str]=True , lowercase_ : List[Any]=0.05 , lowercase_ : Union[str, Any]=10 , lowercase_ : Optional[Any]=2 , lowercase_ : str=0.0 , lowercase_ : Dict=10 , lowercase_ : Any=0 , lowercase_ : Optional[Any]="mean" , lowercase_ : Tuple=False , lowercase_ : Any=False , lowercase_ : Optional[Any]=256 , lowercase_ : Optional[int]=0 , lowercase_ : Union[str, Any]=1 , lowercase_ : Optional[int]=2 , **lowercase_ : Tuple , ): super().__init__(**lowerCamelCase__ , pad_token_id=lowerCamelCase__ , bos_token_id=lowerCamelCase__ , eos_token_id=lowerCamelCase__ ) snake_case_ = hidden_size snake_case_ = feat_extract_norm snake_case_ = feat_extract_activation snake_case_ = list(lowerCamelCase__ ) snake_case_ = list(lowerCamelCase__ ) snake_case_ = list(lowerCamelCase__ ) snake_case_ = conv_bias snake_case_ = num_conv_pos_embeddings snake_case_ = num_conv_pos_embedding_groups snake_case_ = len(self.conv_dim ) snake_case_ = num_hidden_layers snake_case_ = intermediate_size snake_case_ = squeeze_factor snake_case_ = max_position_embeddings snake_case_ = position_buckets snake_case_ = share_att_key snake_case_ = relative_attention snake_case_ = norm_rel_ebd snake_case_ = list(lowerCamelCase__ ) snake_case_ = hidden_act snake_case_ = num_attention_heads snake_case_ = hidden_dropout snake_case_ = attention_dropout snake_case_ = activation_dropout snake_case_ = feat_proj_dropout snake_case_ = final_dropout snake_case_ = layer_norm_eps snake_case_ = feature_layer_norm_eps snake_case_ = initializer_range snake_case_ = 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 snake_case_ = apply_spec_augment snake_case_ = mask_time_prob snake_case_ = mask_time_length snake_case_ = mask_time_min_masks snake_case_ = mask_feature_prob snake_case_ = mask_feature_length snake_case_ = mask_feature_min_masks # ctc loss snake_case_ = ctc_loss_reduction snake_case_ = ctc_zero_infinity # sequence classification snake_case_ = use_weighted_layer_sum snake_case_ = classifier_proj_size @property def A_ ( self : Optional[int] ): return functools.reduce(operator.mul , self.conv_stride , 1 )
56
import random def A ( a_ ,a_ ,a_ = False ) -> dict: __UpperCamelCase : dict ={i: [] for i in range(a_ )} # if probability is greater or equal than 1, then generate a complete graph if probability >= 1: return complete_graph(a_ ) # if probability is lower or equal than 0, then return a graph without edges if probability <= 0: return graph # for each couple of nodes, add an edge from u to v # if the number randomly generated is greater than probability probability for i in range(a_ ): for j in range(i + 1 ,a_ ): if random.random() < probability: graph[i].append(a_ ) if not directed: # if the graph is undirected, add an edge in from j to i, either graph[j].append(a_ ) return graph def A ( a_ ) -> dict: return { i: [j for j in range(a_ ) if i != j] for i in range(a_ ) } if __name__ == "__main__": import doctest doctest.testmod()
71
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices UpperCAmelCase_ = logging.get_logger(__name__) UpperCAmelCase_ = { '''facebook/convnextv2-tiny-1k-224''': '''https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json''', } class lowerCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : Dict = """convnextv2""" def __init__( self : Dict , _UpperCAmelCase : Optional[Any]=3 , _UpperCAmelCase : Optional[Any]=4 , _UpperCAmelCase : List[Any]=4 , _UpperCAmelCase : Optional[int]=None , _UpperCAmelCase : List[Any]=None , _UpperCAmelCase : List[str]="gelu" , _UpperCAmelCase : List[str]=0.02 , _UpperCAmelCase : List[Any]=1E-12 , _UpperCAmelCase : Dict=0.0 , _UpperCAmelCase : Optional[int]=2_24 , _UpperCAmelCase : Optional[int]=None , _UpperCAmelCase : Any=None , **_UpperCAmelCase : str , ): """simple docstring""" super().__init__(**lowerCamelCase__ ) UpperCAmelCase__ = num_channels UpperCAmelCase__ = patch_size UpperCAmelCase__ = num_stages UpperCAmelCase__ = [96, 1_92, 3_84, 7_68] if hidden_sizes is None else hidden_sizes UpperCAmelCase__ = [3, 3, 9, 3] if depths is None else depths UpperCAmelCase__ = hidden_act UpperCAmelCase__ = initializer_range UpperCAmelCase__ = layer_norm_eps UpperCAmelCase__ = drop_path_rate UpperCAmelCase__ = image_size UpperCAmelCase__ = ['stem'] + [f'''stage{idx}''' for idx in range(1 , len(self.depths ) + 1 )] UpperCAmelCase__ = get_aligned_output_features_output_indices( out_features=lowerCamelCase__ , out_indices=lowerCamelCase__ , stage_names=self.stage_names )
346
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 __A ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: __UpperCamelCase : List[str] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModel.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[Any] =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"]: __UpperCamelCase : Any =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForPreTraining.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =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]: __UpperCamelCase : Tuple =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModelForCausalLM.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Tuple =TFAutoModelForCausalLM.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[int] =AutoModelForCausalLM.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Union[str, Any] =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]: __UpperCamelCase : int =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Dict =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[Any] =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]: __UpperCamelCase : List[Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Tuple =TFAutoModelForMaskedLM.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =AutoModelForMaskedLM.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Optional[Any] =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]: __UpperCamelCase : Any =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[Any] =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : List[str] =TFAutoModelForSeqaSeqLM.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Dict =AutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : List[Any] =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"]: __UpperCamelCase : str =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =TFAutoModelForSequenceClassification.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =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"]: __UpperCamelCase : List[Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Tuple =TFAutoModelForQuestionAnswering.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =AutoModelForQuestionAnswering.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : int =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) __UpperCamelCase : str =AutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Union[str, Any] =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_pt=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) __UpperCamelCase : int =AutoModelWithLMHead.from_pretrained(lowerCamelCase__ , from_tf=lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 )
71
0
"""simple docstring""" import json import os import shutil import tempfile import unittest from multiprocessing import get_context from pathlib import Path import datasets import numpy as np from datasets import load_dataset from parameterized import parameterized from transformers import AutoProcessor from transformers.models.wavaveca import WavaVecaCTCTokenizer, WavaVecaFeatureExtractor from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.testing_utils import require_pyctcdecode, require_torch, require_torchaudio, slow from transformers.utils import FEATURE_EXTRACTOR_NAME, is_pyctcdecode_available, is_torch_available from ..wavaveca.test_feature_extraction_wavaveca import floats_list if is_pyctcdecode_available(): from huggingface_hub import snapshot_download from pyctcdecode import BeamSearchDecoderCTC from transformers.models.wavaveca_with_lm import WavaVecaProcessorWithLM from transformers.models.wavaveca_with_lm.processing_wavaveca_with_lm import WavaVecaDecoderWithLMOutput if is_torch_available(): from transformers import WavaVecaForCTC @require_pyctcdecode class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): def __lowercase ( self : Optional[Any] ): lowerCAmelCase = '| <pad> <unk> <s> </s> a b c d e f g h i j k'.split() lowerCAmelCase = dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) lowerCAmelCase = { 'unk_token': '<unk>', 'bos_token': '<s>', 'eos_token': '</s>', } lowerCAmelCase = { 'feature_size': 1, 'padding_value': 0.0, 'sampling_rate': 1_6000, 'return_attention_mask': False, 'do_normalize': True, } lowerCAmelCase = tempfile.mkdtemp() lowerCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) lowerCAmelCase = os.path.join(self.tmpdirname , lowerCamelCase__ ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + """\n""" ) with open(self.feature_extraction_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + """\n""" ) # load decoder from hub lowerCAmelCase = 'hf-internal-testing/ngram-beam-search-decoder' def __lowercase ( self : Tuple , **lowerCAmelCase : int ): lowerCAmelCase = self.add_kwargs_tokens_map.copy() kwargs.update(lowerCamelCase__ ) return WavaVecaCTCTokenizer.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def __lowercase ( self : int , **lowerCAmelCase : Optional[int] ): return WavaVecaFeatureExtractor.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def __lowercase ( self : Optional[int] , **lowerCAmelCase : Union[str, Any] ): return BeamSearchDecoderCTC.load_from_hf_hub(self.decoder_name , **lowerCamelCase__ ) def __lowercase ( self : Any ): shutil.rmtree(self.tmpdirname ) def __lowercase ( self : int ): lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = self.get_feature_extractor() lowerCAmelCase = self.get_decoder() lowerCAmelCase = WavaVecaProcessorWithLM(tokenizer=lowerCamelCase__ , feature_extractor=lowerCamelCase__ , decoder=lowerCamelCase__ ) processor.save_pretrained(self.tmpdirname ) lowerCAmelCase = WavaVecaProcessorWithLM.from_pretrained(self.tmpdirname ) # tokenizer self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , lowerCamelCase__ ) # feature extractor self.assertEqual(processor.feature_extractor.to_json_string() , feature_extractor.to_json_string() ) self.assertIsInstance(processor.feature_extractor , lowerCamelCase__ ) # decoder self.assertEqual(processor.decoder._alphabet.labels , decoder._alphabet.labels ) self.assertEqual( processor.decoder.model_container[decoder._model_key]._unigram_set , decoder.model_container[decoder._model_key]._unigram_set , ) self.assertIsInstance(processor.decoder , lowerCamelCase__ ) def __lowercase ( self : List[Any] ): lowerCAmelCase = WavaVecaProcessorWithLM( tokenizer=self.get_tokenizer() , feature_extractor=self.get_feature_extractor() , decoder=self.get_decoder() ) processor.save_pretrained(self.tmpdirname ) # make sure that error is thrown when decoder alphabet doesn't match lowerCAmelCase = WavaVecaProcessorWithLM.from_pretrained( self.tmpdirname , alpha=5.0 , beta=3.0 , score_boundary=-7.0 , unk_score_offset=3 ) # decoder self.assertEqual(processor.language_model.alpha , 5.0 ) self.assertEqual(processor.language_model.beta , 3.0 ) self.assertEqual(processor.language_model.score_boundary , -7.0 ) self.assertEqual(processor.language_model.unk_score_offset , 3 ) def __lowercase ( self : int ): lowerCAmelCase = self.get_tokenizer() # add token to trigger raise tokenizer.add_tokens(["""xx"""] ) with self.assertRaisesRegex(lowerCamelCase__ , """include""" ): WavaVecaProcessorWithLM( tokenizer=lowerCamelCase__ , feature_extractor=self.get_feature_extractor() , decoder=self.get_decoder() ) def __lowercase ( self : List[Any] ): lowerCAmelCase = self.get_feature_extractor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = self.get_decoder() lowerCAmelCase = WavaVecaProcessorWithLM(tokenizer=lowerCamelCase__ , feature_extractor=lowerCamelCase__ , decoder=lowerCamelCase__ ) lowerCAmelCase = floats_list((3, 1000) ) lowerCAmelCase = feature_extractor(lowerCamelCase__ , return_tensors="""np""" ) lowerCAmelCase = processor(lowerCamelCase__ , return_tensors="""np""" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowercase ( self : str ): lowerCAmelCase = self.get_feature_extractor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = self.get_decoder() lowerCAmelCase = WavaVecaProcessorWithLM(tokenizer=lowerCamelCase__ , feature_extractor=lowerCamelCase__ , decoder=lowerCamelCase__ ) lowerCAmelCase = 'This is a test string' lowerCAmelCase = processor(text=lowerCamelCase__ ) lowerCAmelCase = tokenizer(lowerCamelCase__ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __lowercase ( self : Union[str, Any] , lowerCAmelCase : List[str]=(2, 10, 16) , lowerCAmelCase : Tuple=77 ): np.random.seed(lowerCamelCase__ ) return np.random.rand(*lowerCamelCase__ ) def __lowercase ( self : Any ): lowerCAmelCase = self.get_feature_extractor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = self.get_decoder() lowerCAmelCase = WavaVecaProcessorWithLM(tokenizer=lowerCamelCase__ , feature_extractor=lowerCamelCase__ , decoder=lowerCamelCase__ ) lowerCAmelCase = self._get_dummy_logits(shape=(10, 16) , seed=13 ) lowerCAmelCase = processor.decode(lowerCamelCase__ ) lowerCAmelCase = decoder.decode_beams(lowerCamelCase__ )[0] self.assertEqual(decoded_decoder[0] , decoded_processor.text ) self.assertEqual("""</s> <s> </s>""" , decoded_processor.text ) self.assertEqual(decoded_decoder[-2] , decoded_processor.logit_score ) self.assertEqual(decoded_decoder[-1] , decoded_processor.lm_score ) @parameterized.expand([[None], ["""fork"""], ["""spawn"""]] ) def __lowercase ( self : int , lowerCAmelCase : Optional[Any] ): lowerCAmelCase = self.get_feature_extractor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = self.get_decoder() lowerCAmelCase = WavaVecaProcessorWithLM(tokenizer=lowerCamelCase__ , feature_extractor=lowerCamelCase__ , decoder=lowerCamelCase__ ) lowerCAmelCase = self._get_dummy_logits() # note: pool should be instantiated *after* Wav2Vec2ProcessorWithLM. # otherwise, the LM won't be available to the pool's sub-processes. # manual logic used to allow parameterized test for both pool=None and pool=Pool(...) if pool_context is None: lowerCAmelCase = processor.batch_decode(lowerCamelCase__ ) else: with get_context(lowerCamelCase__ ).Pool() as pool: lowerCAmelCase = processor.batch_decode(lowerCamelCase__ , lowerCamelCase__ ) lowerCAmelCase = list(lowerCamelCase__ ) with get_context("""fork""" ).Pool() as p: lowerCAmelCase = decoder.decode_beams_batch(lowerCamelCase__ , lowerCamelCase__ ) lowerCAmelCase = [], [], [] for beams in decoded_beams: texts_decoder.append(beams[0][0] ) logit_scores_decoder.append(beams[0][-2] ) lm_scores_decoder.append(beams[0][-1] ) self.assertListEqual(lowerCamelCase__ , decoded_processor.text ) self.assertListEqual(["""<s> <s> </s>""", """<s> <s> <s>"""] , decoded_processor.text ) self.assertListEqual(lowerCamelCase__ , decoded_processor.logit_score ) self.assertListEqual(lowerCamelCase__ , decoded_processor.lm_score ) def __lowercase ( self : Dict ): lowerCAmelCase = self.get_feature_extractor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = self.get_decoder() lowerCAmelCase = WavaVecaProcessorWithLM(tokenizer=lowerCamelCase__ , feature_extractor=lowerCamelCase__ , decoder=lowerCamelCase__ ) lowerCAmelCase = self._get_dummy_logits() lowerCAmelCase = 15 lowerCAmelCase = -20.0 lowerCAmelCase = -4.0 lowerCAmelCase = processor.batch_decode( lowerCamelCase__ , beam_width=lowerCamelCase__ , beam_prune_logp=lowerCamelCase__ , token_min_logp=lowerCamelCase__ , ) lowerCAmelCase = decoded_processor_out.text lowerCAmelCase = list(lowerCamelCase__ ) with get_context("""fork""" ).Pool() as pool: lowerCAmelCase = decoder.decode_beams_batch( lowerCamelCase__ , lowerCamelCase__ , beam_width=lowerCamelCase__ , beam_prune_logp=lowerCamelCase__ , token_min_logp=lowerCamelCase__ , ) lowerCAmelCase = [d[0][0] for d in decoded_decoder_out] lowerCAmelCase = [d[0][2] for d in decoded_decoder_out] lowerCAmelCase = [d[0][3] for d in decoded_decoder_out] self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) self.assertListEqual(["""</s> <s> <s>""", """<s> <s> <s>"""] , lowerCamelCase__ ) self.assertTrue(np.array_equal(lowerCamelCase__ , decoded_processor_out.logit_score ) ) self.assertTrue(np.allclose([-20.054, -18.447] , lowerCamelCase__ , atol=1e-3 ) ) self.assertTrue(np.array_equal(lowerCamelCase__ , decoded_processor_out.lm_score ) ) self.assertTrue(np.allclose([-15.554, -13.9474] , lowerCamelCase__ , atol=1e-3 ) ) def __lowercase ( self : Optional[Any] ): lowerCAmelCase = self.get_feature_extractor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = self.get_decoder() lowerCAmelCase = WavaVecaProcessorWithLM(tokenizer=lowerCamelCase__ , feature_extractor=lowerCamelCase__ , decoder=lowerCamelCase__ ) lowerCAmelCase = self._get_dummy_logits() lowerCAmelCase = 2.0 lowerCAmelCase = 5.0 lowerCAmelCase = -20.0 lowerCAmelCase = True lowerCAmelCase = processor.batch_decode( lowerCamelCase__ , alpha=lowerCamelCase__ , beta=lowerCamelCase__ , unk_score_offset=lowerCamelCase__ , lm_score_boundary=lowerCamelCase__ , ) lowerCAmelCase = decoded_processor_out.text lowerCAmelCase = list(lowerCamelCase__ ) decoder.reset_params( alpha=lowerCamelCase__ , beta=lowerCamelCase__ , unk_score_offset=lowerCamelCase__ , lm_score_boundary=lowerCamelCase__ , ) with get_context("""fork""" ).Pool() as pool: lowerCAmelCase = decoder.decode_beams_batch( lowerCamelCase__ , lowerCamelCase__ , ) lowerCAmelCase = [d[0][0] for d in decoded_decoder_out] self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) self.assertListEqual(["""<s> </s> <s> </s> </s>""", """</s> </s> <s> </s> </s>"""] , lowerCamelCase__ ) lowerCAmelCase = processor.decoder.model_container[processor.decoder._model_key] self.assertEqual(lm_model.alpha , 2.0 ) self.assertEqual(lm_model.beta , 5.0 ) self.assertEqual(lm_model.unk_score_offset , -20.0 ) self.assertEqual(lm_model.score_boundary , lowerCamelCase__ ) def __lowercase ( self : int ): lowerCAmelCase = WavaVecaProcessorWithLM.from_pretrained("""hf-internal-testing/processor_with_lm""" ) lowerCAmelCase = processor.decoder.model_container[processor.decoder._model_key] lowerCAmelCase = Path(language_model._kenlm_model.path.decode("""utf-8""" ) ).parent.parent.absolute() lowerCAmelCase = os.listdir(lowerCamelCase__ ) lowerCAmelCase = ['alphabet.json', 'language_model'] downloaded_decoder_files.sort() expected_decoder_files.sort() # test that only decoder relevant files from # https://huggingface.co/hf-internal-testing/processor_with_lm/tree/main # are downloaded and none of the rest (e.g. README.md, ...) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self : Tuple ): lowerCAmelCase = snapshot_download("""hf-internal-testing/processor_with_lm""" ) lowerCAmelCase = WavaVecaProcessorWithLM.from_pretrained(lowerCamelCase__ ) lowerCAmelCase = processor.decoder.model_container[processor.decoder._model_key] lowerCAmelCase = Path(language_model._kenlm_model.path.decode("""utf-8""" ) ).parent.parent.absolute() lowerCAmelCase = os.listdir(lowerCamelCase__ ) lowerCAmelCase = os.listdir(lowerCamelCase__ ) local_decoder_files.sort() expected_decoder_files.sort() # test that both decoder form hub and local files in cache are the same self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self : Any ): lowerCAmelCase = WavaVecaProcessorWithLM.from_pretrained("""hf-internal-testing/processor_with_lm""" ) lowerCAmelCase = AutoProcessor.from_pretrained("""hf-internal-testing/processor_with_lm""" ) lowerCAmelCase = floats_list((3, 1000) ) lowerCAmelCase = processor_wavaveca(lowerCamelCase__ , return_tensors="""np""" ) lowerCAmelCase = processor_auto(lowerCamelCase__ , return_tensors="""np""" ) for key in input_wavaveca.keys(): self.assertAlmostEqual(input_wavaveca[key].sum() , input_auto[key].sum() , delta=1e-2 ) lowerCAmelCase = self._get_dummy_logits() lowerCAmelCase = processor_wavaveca.batch_decode(lowerCamelCase__ ) lowerCAmelCase = processor_auto.batch_decode(lowerCamelCase__ ) self.assertListEqual(decoded_wavaveca.text , decoded_auto.text ) def __lowercase ( self : Any ): lowerCAmelCase = self.get_feature_extractor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = self.get_decoder() lowerCAmelCase = WavaVecaProcessorWithLM(tokenizer=lowerCamelCase__ , feature_extractor=lowerCamelCase__ , decoder=lowerCamelCase__ ) self.assertListEqual( processor.model_input_names , feature_extractor.model_input_names , msg="""`processor` and `feature_extractor` model input names do not match""" , ) @staticmethod def __lowercase ( lowerCAmelCase : List[Any] , lowerCAmelCase : Optional[Any] ): lowerCAmelCase = [d[key] for d in offsets] return retrieved_list def __lowercase ( self : List[str] ): lowerCAmelCase = WavaVecaProcessorWithLM.from_pretrained("""hf-internal-testing/processor_with_lm""" ) lowerCAmelCase = self._get_dummy_logits()[0] lowerCAmelCase = processor.decode(lowerCamelCase__ , output_word_offsets=lowerCamelCase__ ) # check Wav2Vec2CTCTokenizerOutput keys for word self.assertEqual(len(outputs.keys() ) , 4 ) self.assertTrue("""text""" in outputs ) self.assertTrue("""word_offsets""" in outputs ) self.assertTrue(isinstance(lowerCamelCase__ , lowerCamelCase__ ) ) self.assertEqual(""" """.join(self.get_from_offsets(outputs["""word_offsets"""] , """word""" ) ) , outputs.text ) self.assertListEqual(self.get_from_offsets(outputs["""word_offsets"""] , """word""" ) , ["""<s>""", """<s>""", """</s>"""] ) self.assertListEqual(self.get_from_offsets(outputs["""word_offsets"""] , """start_offset""" ) , [0, 2, 4] ) self.assertListEqual(self.get_from_offsets(outputs["""word_offsets"""] , """end_offset""" ) , [1, 3, 5] ) def __lowercase ( self : Union[str, Any] ): lowerCAmelCase = WavaVecaProcessorWithLM.from_pretrained("""hf-internal-testing/processor_with_lm""" ) lowerCAmelCase = self._get_dummy_logits() lowerCAmelCase = processor.batch_decode(lowerCamelCase__ , output_word_offsets=lowerCamelCase__ ) # check Wav2Vec2CTCTokenizerOutput keys for word self.assertEqual(len(outputs.keys() ) , 4 ) self.assertTrue("""text""" in outputs ) self.assertTrue("""word_offsets""" in outputs ) self.assertTrue(isinstance(lowerCamelCase__ , lowerCamelCase__ ) ) self.assertListEqual( [""" """.join(self.get_from_offsets(lowerCamelCase__ , """word""" ) ) for o in outputs["""word_offsets"""]] , outputs.text ) self.assertListEqual(self.get_from_offsets(outputs["""word_offsets"""][0] , """word""" ) , ["""<s>""", """<s>""", """</s>"""] ) self.assertListEqual(self.get_from_offsets(outputs["""word_offsets"""][0] , """start_offset""" ) , [0, 2, 4] ) self.assertListEqual(self.get_from_offsets(outputs["""word_offsets"""][0] , """end_offset""" ) , [1, 3, 5] ) @slow @require_torch @require_torchaudio def __lowercase ( self : str ): import torch lowerCAmelCase = load_dataset("""common_voice""" , """en""" , split="""train""" , streaming=lowerCamelCase__ ) lowerCAmelCase = ds.cast_column("""audio""" , datasets.Audio(sampling_rate=1_6000 ) ) lowerCAmelCase = iter(lowerCamelCase__ ) lowerCAmelCase = next(lowerCamelCase__ ) lowerCAmelCase = AutoProcessor.from_pretrained("""patrickvonplaten/wav2vec2-base-100h-with-lm""" ) lowerCAmelCase = WavaVecaForCTC.from_pretrained("""patrickvonplaten/wav2vec2-base-100h-with-lm""" ) # compare to filename `common_voice_en_100038.mp3` of dataset viewer on https://huggingface.co/datasets/common_voice/viewer/en/train lowerCAmelCase = processor(sample["""audio"""]["""array"""] , return_tensors="""pt""" ).input_values with torch.no_grad(): lowerCAmelCase = model(lowerCamelCase__ ).logits.cpu().numpy() lowerCAmelCase = processor.decode(logits[0] , output_word_offsets=lowerCamelCase__ ) lowerCAmelCase = model.config.inputs_to_logits_ratio / processor.feature_extractor.sampling_rate lowerCAmelCase = [ { 'start_time': d['start_offset'] * time_offset, 'end_time': d['end_offset'] * time_offset, 'word': d['word'], } for d in output['word_offsets'] ] lowerCAmelCase = 'WHY DOES MILISANDRA LOOK LIKE SHE WANTS TO CONSUME JOHN SNOW ON THE RIVER AT THE WALL' # output words self.assertEqual(""" """.join(self.get_from_offsets(lowerCamelCase__ , """word""" ) ) , lowerCamelCase__ ) self.assertEqual(""" """.join(self.get_from_offsets(lowerCamelCase__ , """word""" ) ) , output.text ) # output times lowerCAmelCase = torch.tensor(self.get_from_offsets(lowerCamelCase__ , """start_time""" ) ) lowerCAmelCase = torch.tensor(self.get_from_offsets(lowerCamelCase__ , """end_time""" ) ) # fmt: off lowerCAmelCase = torch.tensor([1.4199, 1.6599, 2.2599, 3.0, 3.24, 3.5999, 3.7999, 4.0999, 4.26, 4.94, 5.28, 5.6599, 5.78, 5.94, 6.32, 6.5399, 6.6599] ) lowerCAmelCase = torch.tensor([1.5399, 1.8999, 2.9, 3.16, 3.5399, 3.72, 4.0199, 4.1799, 4.76, 5.1599, 5.5599, 5.6999, 5.86, 6.1999, 6.38, 6.6199, 6.94] ) # fmt: on self.assertTrue(torch.allclose(lowerCamelCase__ , lowerCamelCase__ , atol=0.01 ) ) self.assertTrue(torch.allclose(lowerCamelCase__ , lowerCamelCase__ , atol=0.01 ) )
155
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A_ :Tuple = { '''configuration_x_clip''': [ '''XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XCLIPConfig''', '''XCLIPTextConfig''', '''XCLIPVisionConfig''', ], '''processing_x_clip''': ['''XCLIPProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ :Union[str, Any] = [ '''XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XCLIPModel''', '''XCLIPPreTrainedModel''', '''XCLIPTextModel''', '''XCLIPVisionModel''', ] if TYPE_CHECKING: from .configuration_x_clip import ( XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, XCLIPConfig, XCLIPTextConfig, XCLIPVisionConfig, ) from .processing_x_clip import XCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_x_clip import ( XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, XCLIPModel, XCLIPPreTrainedModel, XCLIPTextModel, XCLIPVisionModel, ) else: import sys A_ :Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
71
0
'''simple docstring''' __snake_case : Union[str, Any] = { 0: '''0''', 1: '''1''', 2: '''2''', 3: '''3''', 4: '''4''', 5: '''5''', 6: '''6''', 7: '''7''', 8: '''8''', 9: '''9''', 10: '''a''', 11: '''b''', 12: '''c''', 13: '''d''', 14: '''e''', 15: '''f''', } def __lowerCamelCase ( __snake_case : Dict ) -> str: """simple docstring""" assert type(a_ ) in (int, float) and decimal == int(a_ ) A__ : Union[str, Any] =int(a_ ) A__ : List[str] ='' A__ : Optional[Any] =False if decimal < 0: A__ : Tuple =True decimal *= -1 while decimal > 0: A__ : Optional[Any] =divmod(a_, 16 ) A__ : Tuple =values[remainder] + hexadecimal A__ : Dict ='0x' + hexadecimal if negative: A__ : int ='-' + hexadecimal return hexadecimal if __name__ == "__main__": import doctest doctest.testmod()
134
from ...configuration_utils import PretrainedConfig from ...utils import logging A_ :Any = logging.get_logger(__name__) A_ :int = { '''sayakpaul/vit-msn-base''': '''https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json''', # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class __A ( a ): """simple docstring""" UpperCamelCase__ : Optional[int] ="""vit_msn""" def __init__( self , lowerCamelCase__=768 , lowerCamelCase__=12 , lowerCamelCase__=12 , lowerCamelCase__=3072 , lowerCamelCase__="gelu" , lowerCamelCase__=0.0 , lowerCamelCase__=0.0 , lowerCamelCase__=0.02 , lowerCamelCase__=1E-06 , lowerCamelCase__=224 , lowerCamelCase__=16 , lowerCamelCase__=3 , lowerCamelCase__=True , **lowerCamelCase__ , ): """simple docstring""" super().__init__(**lowerCamelCase__ ) __UpperCamelCase : int =hidden_size __UpperCamelCase : List[Any] =num_hidden_layers __UpperCamelCase : Union[str, Any] =num_attention_heads __UpperCamelCase : List[str] =intermediate_size __UpperCamelCase : Union[str, Any] =hidden_act __UpperCamelCase : str =hidden_dropout_prob __UpperCamelCase : Union[str, Any] =attention_probs_dropout_prob __UpperCamelCase : Union[str, Any] =initializer_range __UpperCamelCase : Tuple =layer_norm_eps __UpperCamelCase : Optional[Any] =image_size __UpperCamelCase : Optional[int] =patch_size __UpperCamelCase : Any =num_channels __UpperCamelCase : str =qkv_bias
71
0
from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class A ( _UpperCAmelCase ): """simple docstring""" lowerCamelCase = ["""image_processor""", """tokenizer"""] lowerCamelCase = """BridgeTowerImageProcessor""" lowerCamelCase = ("""RobertaTokenizer""", """RobertaTokenizerFast""") def __init__( self : str,lowercase_ : str,lowercase_ : List[Any] )-> Dict: '''simple docstring''' super().__init__(lowerCamelCase__,lowerCamelCase__ ) def __call__( self : Dict,lowercase_ : List[Any],lowercase_ : Tuple = None,lowercase_ : Optional[int] = True,lowercase_ : Dict = False,lowercase_ : str = None,lowercase_ : List[Any] = None,lowercase_ : List[Any] = 0,lowercase_ : str = None,lowercase_ : Optional[Any] = None,lowercase_ : Union[str, Any] = None,lowercase_ : Tuple = False,lowercase_ : Any = False,lowercase_ : List[Any] = False,lowercase_ : Tuple = False,lowercase_ : Optional[int] = True,lowercase_ : str = None,**lowercase_ : Optional[int],)-> List[str]: '''simple docstring''' A__ = self.tokenizer( text=lowerCamelCase__,add_special_tokens=lowerCamelCase__,padding=lowerCamelCase__,truncation=lowerCamelCase__,max_length=lowerCamelCase__,stride=lowerCamelCase__,pad_to_multiple_of=lowerCamelCase__,return_token_type_ids=lowerCamelCase__,return_attention_mask=lowerCamelCase__,return_overflowing_tokens=lowerCamelCase__,return_special_tokens_mask=lowerCamelCase__,return_offsets_mapping=lowerCamelCase__,return_length=lowerCamelCase__,verbose=lowerCamelCase__,return_tensors=lowerCamelCase__,**lowerCamelCase__,) # add pixel_values + pixel_mask A__ = self.image_processor( lowerCamelCase__,return_tensors=lowerCamelCase__,do_normalize=lowerCamelCase__,do_center_crop=lowerCamelCase__,**lowerCamelCase__ ) encoding.update(lowerCamelCase__ ) return encoding def snake_case__ ( self : int,*lowercase_ : List[Any],**lowercase_ : int )-> Union[str, Any]: '''simple docstring''' return self.tokenizer.batch_decode(*lowerCamelCase__,**lowerCamelCase__ ) def snake_case__ ( self : Union[str, Any],*lowercase_ : Tuple,**lowercase_ : Union[str, Any] )-> Tuple: '''simple docstring''' return self.tokenizer.decode(*lowerCamelCase__,**lowerCamelCase__ ) @property def snake_case__ ( self : Union[str, Any] )-> List[Any]: '''simple docstring''' A__ = self.tokenizer.model_input_names A__ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
7
import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __A ( a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : str =DDIMPipeline UpperCamelCase__ : List[Any] =UNCONDITIONAL_IMAGE_GENERATION_PARAMS UpperCamelCase__ : Tuple =PipelineTesterMixin.required_optional_params - { """num_images_per_prompt""", """latents""", """callback""", """callback_steps""", } UpperCamelCase__ : Tuple =UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS UpperCamelCase__ : Any =False def __lowercase ( self ): """simple docstring""" torch.manual_seed(0 ) __UpperCamelCase : Optional[int] =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') , ) __UpperCamelCase : int =DDIMScheduler() __UpperCamelCase : Optional[int] ={'unet': unet, 'scheduler': scheduler} return components def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : str =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : Optional[int] =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Tuple ={ 'batch_size': 1, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Any ='cpu' __UpperCamelCase : Optional[Any] =self.get_dummy_components() __UpperCamelCase : Tuple =self.pipeline_class(**lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =self.get_dummy_inputs(lowerCamelCase__ ) __UpperCamelCase : int =pipe(**lowerCamelCase__ ).images __UpperCamelCase : Dict =image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) __UpperCamelCase : Tuple =np.array( [1.000E00, 5.717E-01, 4.717E-01, 1.000E00, 0.000E00, 1.000E00, 3.000E-04, 0.000E00, 9.000E-04] ) __UpperCamelCase : Tuple =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCamelCase__ , 1E-3 ) def __lowercase ( self ): """simple docstring""" super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_save_load_local(expected_max_difference=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_save_load_optional_components(expected_max_difference=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __A ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str ='google/ddpm-cifar10-32' __UpperCamelCase : str =UNetaDModel.from_pretrained(lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =DDIMScheduler() __UpperCamelCase : List[Any] =DDIMPipeline(unet=lowerCamelCase__ , scheduler=lowerCamelCase__ ) ddim.to(lowerCamelCase__ ) ddim.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Optional[int] =torch.manual_seed(0 ) __UpperCamelCase : List[str] =ddim(generator=lowerCamelCase__ , eta=0.0 , output_type='numpy' ).images __UpperCamelCase : Union[str, Any] =image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) __UpperCamelCase : str =np.array([0.1_723, 0.1_617, 0.1_600, 0.1_626, 0.1_497, 0.1_513, 0.1_505, 0.1_442, 0.1_453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[Any] ='google/ddpm-ema-bedroom-256' __UpperCamelCase : Any =UNetaDModel.from_pretrained(lowerCamelCase__ ) __UpperCamelCase : int =DDIMScheduler.from_pretrained(lowerCamelCase__ ) __UpperCamelCase : Dict =DDIMPipeline(unet=lowerCamelCase__ , scheduler=lowerCamelCase__ ) ddpm.to(lowerCamelCase__ ) ddpm.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Tuple =torch.manual_seed(0 ) __UpperCamelCase : Union[str, Any] =ddpm(generator=lowerCamelCase__ , output_type='numpy' ).images __UpperCamelCase : Tuple =image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) __UpperCamelCase : Optional[Any] =np.array([0.0_060, 0.0_201, 0.0_344, 0.0_024, 0.0_018, 0.0_002, 0.0_022, 0.0_000, 0.0_069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
71
0
import json import os import unittest from transformers import OpenAIGPTTokenizer, OpenAIGPTTokenizerFast from transformers.models.openai.tokenization_openai import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_spacy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __lowerCAmelCase ( lowerCAmelCase , unittest.TestCase): _a = OpenAIGPTTokenizer _a = OpenAIGPTTokenizerFast _a = True _a = False def SCREAMING_SNAKE_CASE ( self: int ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt lowercase :Any = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'w</w>', 'r</w>', 't</w>', 'lo', 'low', 'er</w>', 'low</w>', 'lowest</w>', 'newer</w>', 'wider</w>', '<unk>', ] lowercase :Optional[Any] = dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) lowercase :List[str] = ['#version: 0.2', 'l o', 'lo w', 'e r</w>', ''] lowercase :List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) lowercase :str = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" ) as fp: fp.write(json.dumps(lowerCamelCase__ ) ) with open(self.merges_file , "w" ) as fp: fp.write("\n".join(lowerCamelCase__ ) ) def SCREAMING_SNAKE_CASE ( self: Any , _lowerCAmelCase: Tuple ): return "lower newer", "lower newer" def SCREAMING_SNAKE_CASE ( self: Tuple ): lowercase :Tuple = OpenAIGPTTokenizer(self.vocab_file , self.merges_file ) lowercase :Any = 'lower' lowercase :Any = ['low', 'er</w>'] lowercase :Optional[int] = tokenizer.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) lowercase :List[Any] = tokens + ['<unk>'] lowercase :Optional[Any] = [14, 15, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) , lowerCamelCase__ ) def SCREAMING_SNAKE_CASE ( self: Dict , _lowerCAmelCase: Optional[Any]=15 ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): lowercase :str = self.rust_tokenizer_class.from_pretrained(lowerCamelCase__ , **lowerCamelCase__ ) # Simple input lowercase :str = 'This is a simple input' lowercase :str = ['This is a simple input 1', 'This is a simple input 2'] lowercase :Union[str, Any] = ('This is a simple input', 'This is a pair') lowercase :List[Any] = [ ('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(lowerCamelCase__ , tokenizer_r.encode , lowerCamelCase__ , max_length=lowerCamelCase__ , padding="max_length" ) # Simple input self.assertRaises(lowerCamelCase__ , tokenizer_r.encode_plus , lowerCamelCase__ , max_length=lowerCamelCase__ , padding="max_length" ) # Simple input self.assertRaises( lowerCamelCase__ , tokenizer_r.batch_encode_plus , lowerCamelCase__ , max_length=lowerCamelCase__ , padding="max_length" , ) # Pair input self.assertRaises(lowerCamelCase__ , tokenizer_r.encode , lowerCamelCase__ , max_length=lowerCamelCase__ , padding="max_length" ) # Pair input self.assertRaises(lowerCamelCase__ , tokenizer_r.encode_plus , lowerCamelCase__ , max_length=lowerCamelCase__ , padding="max_length" ) # Pair input self.assertRaises( lowerCamelCase__ , tokenizer_r.batch_encode_plus , lowerCamelCase__ , max_length=lowerCamelCase__ , padding="max_length" , ) def SCREAMING_SNAKE_CASE ( self: Dict ): pass @require_ftfy @require_spacy @require_tokenizers class __lowerCAmelCase ( lowerCAmelCase): pass
236
from __future__ import annotations import copy import tempfile import unittest from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available from transformers.testing_utils import ( DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tensorflow_probability, require_tf, slow, ) from ..bert.test_modeling_bert import BertModelTester if is_tf_available(): from transformers import ( TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelForTableQuestionAnswering, TFAutoModelForTokenClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFFunnelBaseModel, TFFunnelModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, TFTapasForQuestionAnswering, ) from transformers.models.auto.modeling_tf_auto import ( TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_MAPPING, ) 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 from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST class __A ( a ): """simple docstring""" UpperCamelCase__ : Optional[int] ="""new-model""" if is_tf_available(): class __A ( a ): """simple docstring""" UpperCamelCase__ : List[str] =NewModelConfig @require_tf class __A ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] ='bert-base-cased' __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Union[str, Any] ='bert-base-cased' __UpperCamelCase : Optional[int] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Tuple =TFAutoModelForPreTraining.from_pretrained(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]: __UpperCamelCase : str =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModelForCausalLM.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : str =TFAutoModelForCausalLM.from_pretrained(lowerCamelCase__ , output_loading_info=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]: __UpperCamelCase : Optional[int] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Any =TFAutoModelWithLMHead.from_pretrained(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]: __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Any =TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : Union[str, Any] =TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase__ , output_loading_info=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]: __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : str =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__ , output_loading_info=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: __UpperCamelCase : Union[str, Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : List[str] =TFAutoModelForSequenceClassification.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: __UpperCamelCase : List[Any] =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[int] =TFAutoModelForQuestionAnswering.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) @slow @require_tensorflow_probability def __lowercase ( self ): """simple docstring""" for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]: __UpperCamelCase : Any =AutoConfig.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : str =TFAutoModelForTableQuestionAnswering.from_pretrained(lowerCamelCase__ ) __UpperCamelCase , __UpperCamelCase : List[str] =TFAutoModelForTableQuestionAnswering.from_pretrained( lowerCamelCase__ , output_loading_info=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[Any] =TFAutoModelWithLMHead.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase__ ) , 14410 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Dict =TFAutoModel.from_pretrained('sgugger/funnel-random-tiny' ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Optional[int] =copy.deepcopy(model.config ) __UpperCamelCase : Optional[Any] =['FunnelBaseModel'] __UpperCamelCase : Tuple =TFAutoModel.from_config(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : List[Any] =TFAutoModel.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" try: AutoConfig.register('new-model' , lowerCamelCase__ ) __UpperCamelCase : int =[ TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSequenceClassification, TFAutoModelForTokenClassification, ] for auto_class in auto_classes: with self.subTest(auto_class.__name__ ): # Wrong config class will raise an error with self.assertRaises(lowerCamelCase__ ): auto_class.register(lowerCamelCase__ , lowerCamelCase__ ) auto_class.register(lowerCamelCase__ , lowerCamelCase__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(lowerCamelCase__ ): auto_class.register(lowerCamelCase__ , lowerCamelCase__ ) # Now that the config is registered, it can be used as any other config with the auto-API __UpperCamelCase : List[str] =BertModelTester(self ).get_config() __UpperCamelCase : Optional[Any] =NewModelConfig(**tiny_config.to_dict() ) __UpperCamelCase : Dict =auto_class.from_config(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =auto_class.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) finally: if "new-model" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["new-model"] for mapping in ( TF_MODEL_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, ): if NewModelConfig in mapping._extra_content: del mapping._extra_content[NewModelConfig] def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCamelCase__ , 'bert-base is not a local folder and is not a valid model identifier' ): __UpperCamelCase : Dict =TFAutoModel.from_pretrained('bert-base' ) def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCamelCase__ , R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)' ): __UpperCamelCase : Union[str, Any] =TFAutoModel.from_pretrained(lowerCamelCase__ , revision='aaaaaa' ) def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCamelCase__ , 'hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin' , ): __UpperCamelCase : List[str] =TFAutoModel.from_pretrained('hf-internal-testing/config-no-model' ) def __lowercase ( self ): """simple docstring""" with self.assertRaisesRegex(lowerCamelCase__ , 'Use `from_pt=True` to load this model' ): __UpperCamelCase : List[Any] =TFAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only' ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[int] =TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert' ) with RequestCounter() as counter: __UpperCamelCase : Dict =TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 ) # With a sharded checkpoint __UpperCamelCase : Dict =TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded' ) with RequestCounter() as counter: __UpperCamelCase : Union[str, Any] =TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
71
0
"""simple docstring""" import gc import random import unittest import numpy as np import torch from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModel, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableUnCLIPImgaImgPipeline, UNetaDConditionModel from diffusers.pipelines.pipeline_utils import DiffusionPipeline from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import ( enable_full_determinism, floats_tensor, load_image, load_numpy, require_torch_gpu, skip_mps, slow, torch_device, ) from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import ( PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin, assert_mean_pixel_difference, ) enable_full_determinism() class _SCREAMING_SNAKE_CASE( A , A , A , unittest.TestCase ): SCREAMING_SNAKE_CASE_ : Dict = StableUnCLIPImgaImgPipeline SCREAMING_SNAKE_CASE_ : Optional[int] = TEXT_GUIDED_IMAGE_VARIATION_PARAMS SCREAMING_SNAKE_CASE_ : int = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS SCREAMING_SNAKE_CASE_ : Tuple = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess SCREAMING_SNAKE_CASE_ : Optional[Any] = frozenset([] ) def _UpperCamelCase ( self ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE :Optional[int] = 32 __SCREAMING_SNAKE_CASE :Optional[int] = embedder_hidden_size # image encoding components __SCREAMING_SNAKE_CASE :Optional[int] = CLIPImageProcessor(crop_size=32 ,size=32 ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE :List[str] = CLIPVisionModelWithProjection( CLIPVisionConfig( hidden_size=lowerCamelCase__ ,projection_dim=lowerCamelCase__ ,num_hidden_layers=5 ,num_attention_heads=4 ,image_size=32 ,intermediate_size=37 ,patch_size=1 ,) ) # regular denoising components torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE :Any = StableUnCLIPImageNormalizer(embedding_dim=lowerCamelCase__ ) __SCREAMING_SNAKE_CASE :Any = DDPMScheduler(beta_schedule='''squaredcos_cap_v2''' ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE :Optional[Any] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE :Union[str, Any] = CLIPTextModel( CLIPTextConfig( bos_token_id=0 ,eos_token_id=2 ,hidden_size=lowerCamelCase__ ,projection_dim=32 ,intermediate_size=37 ,layer_norm_eps=1E-05 ,num_attention_heads=4 ,num_hidden_layers=5 ,pad_token_id=1 ,vocab_size=10_00 ,) ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE :int = UNetaDConditionModel( sample_size=32 ,in_channels=4 ,out_channels=4 ,down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') ,up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') ,block_out_channels=(32, 64) ,attention_head_dim=(2, 4) ,class_embed_type='''projection''' ,projection_class_embeddings_input_dim=embedder_projection_dim * 2 ,cross_attention_dim=lowerCamelCase__ ,layers_per_block=1 ,upcast_attention=lowerCamelCase__ ,use_linear_projection=lowerCamelCase__ ,) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE :int = DDIMScheduler( beta_schedule='''scaled_linear''' ,beta_start=0.0_0_0_8_5 ,beta_end=0.0_1_2 ,prediction_type='''v_prediction''' ,set_alpha_to_one=lowerCamelCase__ ,steps_offset=1 ,) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE :int = AutoencoderKL() __SCREAMING_SNAKE_CASE :int = { # image encoding components 'feature_extractor': feature_extractor, 'image_encoder': image_encoder.eval(), # image noising components 'image_normalizer': image_normalizer.eval(), 'image_noising_scheduler': image_noising_scheduler, # regular denoising components 'tokenizer': tokenizer, 'text_encoder': text_encoder.eval(), 'unet': unet.eval(), 'scheduler': scheduler, 'vae': vae.eval(), } return components def _UpperCamelCase ( self ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__=0 ,SCREAMING_SNAKE_CASE__=True ) -> Tuple: """simple docstring""" if str(lowerCamelCase__ ).startswith('''mps''' ): __SCREAMING_SNAKE_CASE :Union[str, Any] = torch.manual_seed(lowerCamelCase__ ) else: __SCREAMING_SNAKE_CASE :Optional[Any] = torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __SCREAMING_SNAKE_CASE :int = floats_tensor((1, 3, 32, 32) ,rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) if pil_image: __SCREAMING_SNAKE_CASE :Union[str, Any] = input_image * 0.5 + 0.5 __SCREAMING_SNAKE_CASE :List[Any] = input_image.clamp(0 ,1 ) __SCREAMING_SNAKE_CASE :Optional[int] = input_image.cpu().permute(0 ,2 ,3 ,1 ).float().numpy() __SCREAMING_SNAKE_CASE :Dict = DiffusionPipeline.numpy_to_pil(lowerCamelCase__ )[0] return { "prompt": "An anime racoon running a marathon", "image": input_image, "generator": generator, "num_inference_steps": 2, "output_type": "np", } @skip_mps def _UpperCamelCase ( self ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE :Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator __SCREAMING_SNAKE_CASE :List[str] = self.get_dummy_components() __SCREAMING_SNAKE_CASE :Union[str, Any] = StableUnCLIPImgaImgPipeline(**lowerCamelCase__ ) __SCREAMING_SNAKE_CASE :Tuple = sd_pipe.to(lowerCamelCase__ ) sd_pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __SCREAMING_SNAKE_CASE :Tuple = self.get_dummy_inputs(lowerCamelCase__ ) inputs.update({'''image_embeds''': None} ) __SCREAMING_SNAKE_CASE :Optional[int] = sd_pipe(**lowerCamelCase__ ).images __SCREAMING_SNAKE_CASE :int = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) __SCREAMING_SNAKE_CASE :Union[str, Any] = np.array([0.3_8_7_2, 0.7_2_2_4, 0.5_6_0_1, 0.4_7_4_1, 0.6_8_7_2, 0.5_8_1_4, 0.4_6_3_6, 0.3_8_6_7, 0.5_0_7_8] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def _UpperCamelCase ( self ) -> Tuple: """simple docstring""" __SCREAMING_SNAKE_CASE :Optional[Any] = torch_device in ['cpu', 'mps'] self._test_attention_slicing_forward_pass(test_max_difference=lowerCamelCase__ ) def _UpperCamelCase ( self ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE :Tuple = torch_device in ['cpu', 'mps'] self._test_inference_batch_single_identical(test_max_difference=lowerCamelCase__ ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() ,reason='''XFormers attention is only available with CUDA and `xformers` installed''' ,) def _UpperCamelCase ( self ) -> List[Any]: """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(test_max_difference=lowerCamelCase__ ) @slow @require_torch_gpu class _SCREAMING_SNAKE_CASE( unittest.TestCase ): def _UpperCamelCase ( self ) -> Optional[Any]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE :Union[str, Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png''' ) __SCREAMING_SNAKE_CASE :List[str] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_img2img_anime_turtle_fp16.npy''' ) __SCREAMING_SNAKE_CASE :str = StableUnCLIPImgaImgPipeline.from_pretrained( '''fusing/stable-unclip-2-1-l-img2img''' ,torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) # stable unclip will oom when integration tests are run on a V100, # so turn on memory savings pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() __SCREAMING_SNAKE_CASE :Optional[Any] = torch.Generator(device='''cpu''' ).manual_seed(0 ) __SCREAMING_SNAKE_CASE :Union[str, Any] = pipe(lowerCamelCase__ ,'''anime turle''' ,generator=lowerCamelCase__ ,output_type='''np''' ) __SCREAMING_SNAKE_CASE :Union[str, Any] = output.images[0] assert image.shape == (7_68, 7_68, 3) assert_mean_pixel_difference(lowerCamelCase__ ,lowerCamelCase__ ) def _UpperCamelCase ( self ) -> Tuple: """simple docstring""" __SCREAMING_SNAKE_CASE :Union[str, Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png''' ) __SCREAMING_SNAKE_CASE :int = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_h_img2img_anime_turtle_fp16.npy''' ) __SCREAMING_SNAKE_CASE :Union[str, Any] = StableUnCLIPImgaImgPipeline.from_pretrained( '''fusing/stable-unclip-2-1-h-img2img''' ,torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) # stable unclip will oom when integration tests are run on a V100, # so turn on memory savings pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() __SCREAMING_SNAKE_CASE :int = torch.Generator(device='''cpu''' ).manual_seed(0 ) __SCREAMING_SNAKE_CASE :Union[str, Any] = pipe(lowerCamelCase__ ,'''anime turle''' ,generator=lowerCamelCase__ ,output_type='''np''' ) __SCREAMING_SNAKE_CASE :Dict = output.images[0] assert image.shape == (7_68, 7_68, 3) assert_mean_pixel_difference(lowerCamelCase__ ,lowerCamelCase__ ) def _UpperCamelCase ( self ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE :Optional[int] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png''' ) torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __SCREAMING_SNAKE_CASE :int = StableUnCLIPImgaImgPipeline.from_pretrained( '''fusing/stable-unclip-2-1-h-img2img''' ,torch_dtype=torch.floataa ) __SCREAMING_SNAKE_CASE :Dict = pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() __SCREAMING_SNAKE_CASE :Tuple = pipe( lowerCamelCase__ ,'''anime turtle''' ,num_inference_steps=2 ,output_type='''np''' ,) __SCREAMING_SNAKE_CASE :List[Any] = torch.cuda.max_memory_allocated() # make sure that less than 7 GB is allocated assert mem_bytes < 7 * 10**9
191
import argparse import json import os import re import torch from transformers import BloomConfig, BloomModel from transformers.file_utils import CONFIG_NAME, WEIGHTS_NAME from transformers.utils import logging logging.set_verbosity_info() A_ :List[str] = [ '''word_embeddings_layernorm.weight''', '''word_embeddings_layernorm.bias''', '''input_layernorm.weight''', '''input_layernorm.bias''', '''post_attention_layernorm.weight''', '''post_attention_layernorm.bias''', '''self_attention.dense.bias''', '''mlp.dense_4h_to_h.bias''', '''ln_f.weight''', '''ln_f.bias''', ] A_ :Optional[Any] = [ '''mlp.dense_4h_to_h.weight''', '''self_attention.dense.weight''', ] def A ( a_ ,a_ ) -> str: __UpperCamelCase : Any ={ 'word_embeddings.weight': 'word_embeddings.weight', 'word_embeddings.norm.weight': 'word_embeddings_layernorm.weight', 'word_embeddings.norm.bias': 'word_embeddings_layernorm.bias', 'weight': 'ln_f.weight', 'bias': 'ln_f.bias', } if key in layer_rename_map: return layer_rename_map[key] # Handle transformer blocks __UpperCamelCase : Tuple =int(re.match(r'.*layer_(\d*).*' ,a_ )[1] ) layer_number -= 3 return F'h.{layer_number}.' + key def A ( a_ ) -> Any: if dtype == torch.bool: return 1 / 8 __UpperCamelCase : Dict =re.search(r'[^\d](\d+)$' ,str(a_ ) ) if bit_search is None: raise ValueError(F'`dtype` is not a valid dtype: {dtype}.' ) __UpperCamelCase : Tuple =int(bit_search.groups()[0] ) return bit_size // 8 def A ( a_ ,a_ ,a_ ,a_ ,a_ ) -> Dict: # Construct model if bloom_config_file == "": __UpperCamelCase : List[Any] =BloomConfig() else: __UpperCamelCase : List[str] =BloomConfig.from_json_file(a_ ) if shard_model: __UpperCamelCase : int =os.listdir(a_ ) __UpperCamelCase : Union[str, Any] =sorted(filter(lambda a_ : s.startswith('layer' ) and "model_00" in s ,a_ ) ) __UpperCamelCase : Optional[Any] ={'weight_map': {}, 'metadata': {}} __UpperCamelCase : Dict =0 __UpperCamelCase : int =None __UpperCamelCase : Any =BloomConfig() for j, file in enumerate(a_ ): print('Processing file: {}'.format(a_ ) ) __UpperCamelCase : Optional[int] =None for i in range(a_ ): # load all TP files __UpperCamelCase : Dict =file.replace('model_00' ,F'model_0{i}' ) __UpperCamelCase : Optional[Any] =torch.load(os.path.join(a_ ,a_ ) ,map_location='cpu' ) # Rename keys in the transformers names __UpperCamelCase : int =list(temp.keys() ) for key in keys: __UpperCamelCase : Dict =temp.pop(a_ ) if tensors is None: __UpperCamelCase : Any =temp else: for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel __UpperCamelCase : List[Any] =1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks __UpperCamelCase : Any =torch.cat([tensors[key], temp[key]] ,dim=a_ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): __UpperCamelCase : Optional[Any] =tensors[key] / pretraining_tp torch.save( a_ ,os.path.join( a_ ,'pytorch_model_{}-of-{}.bin'.format(str(j + 1 ).zfill(5 ) ,str(len(a_ ) ).zfill(5 ) ) ,) ,) for key in tensors.keys(): __UpperCamelCase : Union[str, Any] =tensors[key] total_size += value.numel() * get_dtype_size(value.dtype ) if key not in index_dict["weight_map"]: __UpperCamelCase : int ='pytorch_model_{}-of-{}.bin'.format( str(j + 1 ).zfill(5 ) ,str(len(a_ ) ).zfill(5 ) ) __UpperCamelCase : Union[str, Any] =BloomConfig() __UpperCamelCase : Tuple =pytorch_dump_folder_path + '/' + CONFIG_NAME __UpperCamelCase : Optional[int] =total_size with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(config.to_json_string() ) with open(os.path.join(a_ ,WEIGHTS_NAME + '.index.json' ) ,'w' ,encoding='utf-8' ) as f: __UpperCamelCase : List[Any] =json.dumps(a_ ,indent=2 ,sort_keys=a_ ) + '\n' f.write(a_ ) else: __UpperCamelCase : List[Any] =BloomModel(a_ ) __UpperCamelCase : Optional[Any] =os.listdir(a_ ) __UpperCamelCase : Dict =sorted(filter(lambda a_ : s.startswith('layer' ) and "model_00" in s ,a_ ) ) __UpperCamelCase : Any =None for i, file in enumerate(a_ ): __UpperCamelCase : Union[str, Any] =None for i in range(a_ ): # load all TP files __UpperCamelCase : Optional[Any] =file.replace('model_00' ,F'model_0{i}' ) __UpperCamelCase : str =torch.load(os.path.join(a_ ,a_ ) ,map_location='cpu' ) # Rename keys in the transformers names __UpperCamelCase : List[str] =list(temp.keys() ) for key in keys: __UpperCamelCase : Union[str, Any] =temp.pop(a_ ) if tensors is None: __UpperCamelCase : Optional[Any] =temp else: for key in tensors.keys(): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel __UpperCamelCase : Optional[int] =1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks __UpperCamelCase : int =torch.cat([tensors[key], temp[key]] ,dim=a_ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): __UpperCamelCase : Dict =tensors[key] / pretraining_tp __UpperCamelCase : str =model.load_state_dict(a_ ,strict=a_ ) assert not other_keys.unexpected_keys, F'The keys {other_keys.unexpected_keys} are unexpected' if missing_keys is None: __UpperCamelCase : str =set(other_keys.missing_keys ) else: __UpperCamelCase : int =missing_keys.intersection(set(other_keys.missing_keys ) ) assert not missing_keys, F'The keys {missing_keys} are missing' # Save pytorch-model os.makedirs(a_ ,exist_ok=a_ ) __UpperCamelCase : Optional[int] =pytorch_dump_folder_path + '/' + WEIGHTS_NAME __UpperCamelCase : Dict =pytorch_dump_folder_path + '/' + CONFIG_NAME print(F'Save PyTorch model to {pytorch_weights_dump_path} with dtype {config.torch_dtype}' ) if config.torch_dtype is not None: __UpperCamelCase : List[str] =model.to(config.torch_dtype ) torch.save(model.state_dict() ,a_ ) print(F'Save configuration file to {pytorch_config_dump_path}' ) with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": A_ :Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--bloom_checkpoint_path''', default=None, type=str, required=True, help='''Path to the Megatron-LM checkpoint path.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--bloom_config_file''', default='''''', type=str, help=( '''An optional config json file corresponding to the pre-trained model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--shard_model''', action='''store_true''', help='''An optional setting to shard the output model \nThis enables sharding the converted checkpoint''', ) parser.add_argument( '''--pretraining_tp''', default=4, type=int, help='''Pretraining TP rank that has been used when training the model in Megatron-LM \n''', ) A_ :str = parser.parse_args() convert_bloom_checkpoint_to_pytorch( args.bloom_checkpoint_path, args.bloom_config_file, args.pytorch_dump_folder_path, args.shard_model, args.pretraining_tp, )
71
0
"""simple docstring""" from __future__ import annotations def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple ,_lowerCamelCase : Tuple = None ,_lowerCamelCase : str = None ) -> None: if start is None: _lowerCAmelCase : int = 0 if end is None: _lowerCAmelCase : Optional[Any] = len(a_ ) - 1 if start >= end: return _lowerCAmelCase : List[Any] = (start + end) // 2 slowsort(a_ ,a_ ,a_ ) slowsort(a_ ,mid + 1 ,a_ ) if sequence[end] < sequence[mid]: _lowerCAmelCase : Dict = sequence[mid], sequence[end] slowsort(a_ ,a_ ,end - 1 ) if __name__ == "__main__": from doctest import testmod testmod()
44
from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class __A : """simple docstring""" UpperCamelCase__ : int =XGLMConfig UpperCamelCase__ : Optional[Any] ={} UpperCamelCase__ : List[str] ="""gelu""" def __init__( self , lowerCamelCase__ , lowerCamelCase__=14 , lowerCamelCase__=7 , 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__=0.02 , ): """simple docstring""" __UpperCamelCase : Tuple =parent __UpperCamelCase : List[str] =batch_size __UpperCamelCase : str =seq_length __UpperCamelCase : Dict =is_training __UpperCamelCase : Tuple =use_input_mask __UpperCamelCase : List[Any] =use_labels __UpperCamelCase : Any =vocab_size __UpperCamelCase : List[Any] =d_model __UpperCamelCase : Optional[int] =num_hidden_layers __UpperCamelCase : List[str] =num_attention_heads __UpperCamelCase : Optional[int] =ffn_dim __UpperCamelCase : str =activation_function __UpperCamelCase : Any =activation_dropout __UpperCamelCase : Optional[int] =attention_dropout __UpperCamelCase : Optional[int] =max_position_embeddings __UpperCamelCase : Any =initializer_range __UpperCamelCase : Dict =None __UpperCamelCase : Optional[int] =0 __UpperCamelCase : Optional[Any] =2 __UpperCamelCase : str =1 def __lowercase ( self ): """simple docstring""" return XGLMConfig.from_pretrained('facebook/xglm-564M' ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[Any] =tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) , clip_value_min=0 , clip_value_max=3 ) __UpperCamelCase : Union[str, Any] =None if self.use_input_mask: __UpperCamelCase : Dict =random_attention_mask([self.batch_size, self.seq_length] ) __UpperCamelCase : Any =self.get_config() __UpperCamelCase : Optional[Any] =floats_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, ) def __lowercase ( self ): """simple docstring""" return XGLMConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , num_layers=self.num_hidden_layers , attention_heads=self.num_attention_heads , ffn_dim=self.ffn_dim , activation_function=self.activation_function , activation_dropout=self.activation_dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , use_cache=lowerCamelCase__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , return_dict=lowerCamelCase__ , ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =self.prepare_config_and_inputs() ( ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ( __UpperCamelCase ) , ) : int =config_and_inputs __UpperCamelCase : Optional[Any] ={ 'input_ids': input_ids, 'head_mask': head_mask, } return config, inputs_dict @require_tf class __A ( a , a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : Union[str, Any] =(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () UpperCamelCase__ : str =(TFXGLMForCausalLM,) if is_tf_available() else () UpperCamelCase__ : Optional[Any] =( {"""feature-extraction""": TFXGLMModel, """text-generation""": TFXGLMForCausalLM} if is_tf_available() else {} ) UpperCamelCase__ : Tuple =False UpperCamelCase__ : Tuple =False UpperCamelCase__ : Optional[Any] =False def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Tuple =TFXGLMModelTester(self ) __UpperCamelCase : Dict =ConfigTester(self , config_class=lowerCamelCase__ , n_embd=37 ) def __lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() @slow def __lowercase ( self ): """simple docstring""" for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __UpperCamelCase : Optional[Any] =TFXGLMModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @unittest.skip(reason='Currently, model embeddings are going to undergo a major refactor.' ) def __lowercase ( self ): """simple docstring""" super().test_resize_token_embeddings() @require_tf class __A ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self , lowerCamelCase__=True ): """simple docstring""" __UpperCamelCase : int =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : List[str] =tf.convert_to_tensor([[2, 268, 9865]] , dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off __UpperCamelCase : str =[2, 268, 9865, 67, 11, 1988, 57252, 9865, 5, 984, 67, 1988, 213838, 1658, 53, 70446, 33, 6657, 278, 1581] # fmt: on __UpperCamelCase : Optional[Any] =model.generate(lowerCamelCase__ , do_sample=lowerCamelCase__ , num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist() , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : Union[str, Any] =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) tf.random.set_seed(0 ) __UpperCamelCase : str =tokenizer('Today is a nice day and' , return_tensors='tf' ) __UpperCamelCase : Union[str, Any] =tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(':/CPU:0' ): __UpperCamelCase : Any =model.generate(lowerCamelCase__ , do_sample=lowerCamelCase__ , seed=[7, 0] ) __UpperCamelCase : Tuple =tokenizer.decode(output_ids[0] , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : List[Any] =( 'Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due' ) self.assertEqual(lowerCamelCase__ , lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Tuple =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : Optional[Any] =XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase : Optional[Any] ='left' # use different length sentences to test batching __UpperCamelCase : Optional[int] =[ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When', 'Hello, my dog is a little', ] __UpperCamelCase : List[Any] =tokenizer(lowerCamelCase__ , return_tensors='tf' , padding=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =inputs['input_ids'] __UpperCamelCase : Dict =model.generate(input_ids=lowerCamelCase__ , attention_mask=inputs['attention_mask'] , max_new_tokens=12 ) __UpperCamelCase : List[Any] =tokenizer(sentences[0] , return_tensors='tf' ).input_ids __UpperCamelCase : Dict =model.generate(input_ids=lowerCamelCase__ , max_new_tokens=12 ) __UpperCamelCase : Any =tokenizer(sentences[1] , return_tensors='tf' ).input_ids __UpperCamelCase : Optional[Any] =model.generate(input_ids=lowerCamelCase__ , max_new_tokens=12 ) __UpperCamelCase : Optional[int] =tokenizer.batch_decode(lowerCamelCase__ , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =tokenizer.decode(output_non_padded[0] , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : int =tokenizer.decode(output_padded[0] , skip_special_tokens=lowerCamelCase__ ) __UpperCamelCase : Any =[ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When left padding is applied, the sequence will be ' 'a single', 'Hello, my dog is a little bit of a shy one, but he is very friendly', ] self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , [non_padded_sentence, padded_sentence] )
71
0
import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class __lowerCAmelCase ( A ): def _lowerCamelCase ( self : List[str]) -> List[str]: """simple docstring""" _UpperCAmelCase = tempfile.mkdtemp() _UpperCAmelCase = 8 # DPR tok _UpperCAmelCase = [ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] _UpperCAmelCase = os.path.join(self.tmpdirname , 'dpr_tokenizer') os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__) _UpperCAmelCase = os.path.join(lowerCamelCase__ , DPR_VOCAB_FILES_NAMES['vocab_file']) with open(self.vocab_file , 'w' , encoding='utf-8') as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens])) # BART tok _UpperCAmelCase = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] _UpperCAmelCase = dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__)))) _UpperCAmelCase = ['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] _UpperCAmelCase = {'unk_token': '<unk>'} _UpperCAmelCase = os.path.join(self.tmpdirname , 'bart_tokenizer') os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__) _UpperCAmelCase = os.path.join(lowerCamelCase__ , BART_VOCAB_FILES_NAMES['vocab_file']) _UpperCAmelCase = os.path.join(lowerCamelCase__ , BART_VOCAB_FILES_NAMES['merges_file']) with open(self.vocab_file , 'w' , encoding='utf-8') as fp: fp.write(json.dumps(lowerCamelCase__) + '\n') with open(self.merges_file , 'w' , encoding='utf-8') as fp: fp.write('\n'.join(lowerCamelCase__)) def _lowerCamelCase ( self : Optional[int]) -> str: """simple docstring""" return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'dpr_tokenizer')) def _lowerCamelCase ( self : Any) -> Union[str, Any]: """simple docstring""" return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'bart_tokenizer')) def _lowerCamelCase ( self : int) -> Any: """simple docstring""" shutil.rmtree(self.tmpdirname) @require_tokenizers def _lowerCamelCase ( self : Union[str, Any]) -> Union[str, Any]: """simple docstring""" _UpperCAmelCase = os.path.join(self.tmpdirname , 'rag_tokenizer') _UpperCAmelCase = RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict()) _UpperCAmelCase = RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer()) rag_config.save_pretrained(lowerCamelCase__) rag_tokenizer.save_pretrained(lowerCamelCase__) _UpperCAmelCase = RagTokenizer.from_pretrained(lowerCamelCase__ , config=lowerCamelCase__) self.assertIsInstance(new_rag_tokenizer.question_encoder , lowerCamelCase__) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab()) self.assertIsInstance(new_rag_tokenizer.generator , lowerCamelCase__) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab()) @slow def _lowerCamelCase ( self : Tuple) -> List[Any]: """simple docstring""" _UpperCAmelCase = RagTokenizer.from_pretrained('facebook/rag-token-nq') _UpperCAmelCase = [ 'who got the first nobel prize in physics', 'when is the next deadpool movie being released', 'which mode is used for short wave broadcast service', 'who is the owner of reading football club', 'when is the next scandal episode coming out', 'when is the last time the philadelphia won the superbowl', 'what is the most current adobe flash player version', 'how many episodes are there in dragon ball z', 'what is the first step in the evolution of the eye', 'where is gall bladder situated in human body', 'what is the main mineral in lithium batteries', 'who is the president of usa right now', 'where do the greasers live in the outsiders', 'panda is a national animal of which country', 'what is the name of manchester united stadium', ] _UpperCAmelCase = tokenizer(lowerCamelCase__) self.assertIsNotNone(lowerCamelCase__) @slow def _lowerCamelCase ( self : str) -> Optional[int]: """simple docstring""" _UpperCAmelCase = RagTokenizer.from_pretrained('facebook/rag-sequence-nq') _UpperCAmelCase = [ 'who got the first nobel prize in physics', 'when is the next deadpool movie being released', 'which mode is used for short wave broadcast service', 'who is the owner of reading football club', 'when is the next scandal episode coming out', 'when is the last time the philadelphia won the superbowl', 'what is the most current adobe flash player version', 'how many episodes are there in dragon ball z', 'what is the first step in the evolution of the eye', 'where is gall bladder situated in human body', 'what is the main mineral in lithium batteries', 'who is the president of usa right now', 'where do the greasers live in the outsiders', 'panda is a national animal of which country', 'what is the name of manchester united stadium', ] _UpperCAmelCase = tokenizer(lowerCamelCase__) self.assertIsNotNone(lowerCamelCase__)
339
import argparse import json import numpy import torch from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def A ( a_ ,a_ ) -> Optional[Any]: # Load checkpoint __UpperCamelCase : int =torch.load(a_ ,map_location='cpu' ) __UpperCamelCase : List[Any] =chkpt['model'] # We have the base model one level deeper than the original XLM repository __UpperCamelCase : str ={} for k, v in state_dict.items(): if "pred_layer" in k: __UpperCamelCase : Optional[Any] =v else: __UpperCamelCase : Optional[Any] =v __UpperCamelCase : List[Any] =chkpt['params'] __UpperCamelCase : str ={n: v for n, v in config.items() if not isinstance(a_ ,(torch.FloatTensor, numpy.ndarray) )} __UpperCamelCase : str =chkpt['dico_word2id'] __UpperCamelCase : Dict ={s + '</w>' if s.find('@@' ) == -1 and i > 13 else s.replace('@@' ,'' ): i for s, i in vocab.items()} # Save pytorch-model __UpperCamelCase : List[Any] =pytorch_dump_folder_path + '/' + WEIGHTS_NAME __UpperCamelCase : Tuple =pytorch_dump_folder_path + '/' + CONFIG_NAME __UpperCamelCase : Any =pytorch_dump_folder_path + '/' + VOCAB_FILES_NAMES['vocab_file'] print(F'Save PyTorch model to {pytorch_weights_dump_path}' ) torch.save(a_ ,a_ ) print(F'Save configuration file to {pytorch_config_dump_path}' ) with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(json.dumps(a_ ,indent=2 ) + '\n' ) print(F'Save vocab file to {pytorch_config_dump_path}' ) with open(a_ ,'w' ,encoding='utf-8' ) as f: f.write(json.dumps(a_ ,indent=2 ) + '\n' ) if __name__ == "__main__": A_ :str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--xlm_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.''' ) A_ :List[Any] = parser.parse_args() convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)
71
0
"""simple docstring""" import argparse import torch from transformers import OpenAIGPTConfig, OpenAIGPTModel, load_tf_weights_in_openai_gpt from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def __lowerCamelCase ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Tuple: """simple docstring""" if openai_config_file == "": lowerCAmelCase_ : int = OpenAIGPTConfig() else: lowerCAmelCase_ : Any = OpenAIGPTConfig.from_json_file(a_ ) lowerCAmelCase_ : Any = OpenAIGPTModel(a_ ) # Load weights from numpy load_tf_weights_in_openai_gpt(a_ , a_ , a_ ) # Save pytorch-model lowerCAmelCase_ : Optional[Any] = pytorch_dump_folder_path + '/' + WEIGHTS_NAME lowerCAmelCase_ : Optional[Any] = pytorch_dump_folder_path + '/' + CONFIG_NAME print(f'''Save PyTorch model to {pytorch_weights_dump_path}''' ) torch.save(model.state_dict() , a_ ) print(f'''Save configuration file to {pytorch_config_dump_path}''' ) with open(a_ , "w" , encoding="utf-8" ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": lowercase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--openai_checkpoint_folder_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) parser.add_argument( """--openai_config_file""", default="""""", type=str, help=( """An optional config json file corresponding to the pre-trained OpenAI model. \n""" """This specifies the model architecture.""" ), ) lowercase__ = parser.parse_args() convert_openai_checkpoint_to_pytorch( args.openai_checkpoint_folder_path, args.openai_config_file, args.pytorch_dump_folder_path )
241
import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class __A ( a ): """simple docstring""" def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str =tempfile.mkdtemp() __UpperCamelCase : Optional[int] =8 # DPR tok __UpperCamelCase : str =[ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] __UpperCamelCase : Optional[Any] =os.path.join(self.tmpdirname , 'dpr_tokenizer' ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) __UpperCamelCase : Dict =os.path.join(lowerCamelCase__ , DPR_VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) # BART tok __UpperCamelCase : Optional[int] =[ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] __UpperCamelCase : str =dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) __UpperCamelCase : Optional[int] =['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] __UpperCamelCase : Any ={'unk_token': '<unk>'} __UpperCamelCase : Any =os.path.join(self.tmpdirname , 'bart_tokenizer' ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) __UpperCamelCase : Any =os.path.join(lowerCamelCase__ , BART_VOCAB_FILES_NAMES['vocab_file'] ) __UpperCamelCase : Dict =os.path.join(lowerCamelCase__ , BART_VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(lowerCamelCase__ ) ) def __lowercase ( self ): """simple docstring""" return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'dpr_tokenizer' ) ) def __lowercase ( self ): """simple docstring""" return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'bart_tokenizer' ) ) def __lowercase ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) @require_tokenizers def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =os.path.join(self.tmpdirname , 'rag_tokenizer' ) __UpperCamelCase : Dict =RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) __UpperCamelCase : List[Any] =RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(lowerCamelCase__ ) rag_tokenizer.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : int =RagTokenizer.from_pretrained(lowerCamelCase__ , config=lowerCamelCase__ ) self.assertIsInstance(new_rag_tokenizer.question_encoder , lowerCamelCase__ ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , lowerCamelCase__ ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Dict =RagTokenizer.from_pretrained('facebook/rag-token-nq' ) __UpperCamelCase : Union[str, Any] =[ 'who got the first nobel prize in physics', 'when is the next deadpool movie being released', 'which mode is used for short wave broadcast service', 'who is the owner of reading football club', 'when is the next scandal episode coming out', 'when is the last time the philadelphia won the superbowl', 'what is the most current adobe flash player version', 'how many episodes are there in dragon ball z', 'what is the first step in the evolution of the eye', 'where is gall bladder situated in human body', 'what is the main mineral in lithium batteries', 'who is the president of usa right now', 'where do the greasers live in the outsiders', 'panda is a national animal of which country', 'what is the name of manchester united stadium', ] __UpperCamelCase : int =tokenizer(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =RagTokenizer.from_pretrained('facebook/rag-sequence-nq' ) __UpperCamelCase : Union[str, Any] =[ 'who got the first nobel prize in physics', 'when is the next deadpool movie being released', 'which mode is used for short wave broadcast service', 'who is the owner of reading football club', 'when is the next scandal episode coming out', 'when is the last time the philadelphia won the superbowl', 'what is the most current adobe flash player version', 'how many episodes are there in dragon ball z', 'what is the first step in the evolution of the eye', 'where is gall bladder situated in human body', 'what is the main mineral in lithium batteries', 'who is the president of usa right now', 'where do the greasers live in the outsiders', 'panda is a national animal of which country', 'what is the name of manchester united stadium', ] __UpperCamelCase : Any =tokenizer(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ )
71
0
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class _a ( UpperCamelCase__): """simple docstring""" UpperCamelCase__ = (DPMSolverSDEScheduler,) UpperCamelCase__ = 10 def UpperCAmelCase_ ( self: List[Any] , **__lowerCamelCase: int ): '''simple docstring''' UpperCamelCase__: Union[str, Any] = { 'num_train_timesteps': 1100, 'beta_start': 0.0_001, 'beta_end': 0.02, 'beta_schedule': 'linear', 'noise_sampler_seed': 0, } config.update(**lowerCamelCase__ ) return config def UpperCAmelCase_ ( self: List[Any] ): '''simple docstring''' for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=lowerCamelCase__ ) def UpperCAmelCase_ ( self: str ): '''simple docstring''' for beta_start, beta_end in zip([0.00_001, 0.0_001, 0.001] , [0.0_002, 0.002, 0.02] ): self.check_over_configs(beta_start=lowerCamelCase__ , beta_end=lowerCamelCase__ ) def UpperCAmelCase_ ( self: List[str] ): '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=lowerCamelCase__ ) def UpperCAmelCase_ ( self: Optional[int] ): '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowerCamelCase__ ) def UpperCAmelCase_ ( self: Tuple ): '''simple docstring''' UpperCamelCase__: str = self.scheduler_classes[0] UpperCamelCase__: Dict = self.get_scheduler_config() UpperCamelCase__: Union[str, Any] = scheduler_class(**lowerCamelCase__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCamelCase__: Dict = self.dummy_model() UpperCamelCase__: Optional[int] = self.dummy_sample_deter * scheduler.init_noise_sigma UpperCamelCase__: Tuple = sample.to(lowerCamelCase__ ) for i, t in enumerate(scheduler.timesteps ): UpperCamelCase__: Union[str, Any] = scheduler.scale_model_input(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__: Dict = model(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__: int = scheduler.step(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__: List[str] = output.prev_sample UpperCamelCase__: List[Any] = torch.sum(torch.abs(lowerCamelCase__ ) ) UpperCamelCase__: Optional[int] = torch.mean(torch.abs(lowerCamelCase__ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.47_821_044_921_875 ) < 1e-2 assert abs(result_mean.item() - 0.2_178_705_964_565_277 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59_352_111_816_406 ) < 1e-2 assert abs(result_mean.item() - 0.22_342_906_892_299_652 ) < 1e-3 else: assert abs(result_sum.item() - 162.52_383_422_851_562 ) < 1e-2 assert abs(result_mean.item() - 0.211_619_570_851_326 ) < 1e-3 def UpperCAmelCase_ ( self: Any ): '''simple docstring''' UpperCamelCase__: Dict = self.scheduler_classes[0] UpperCamelCase__: int = self.get_scheduler_config(prediction_type="v_prediction" ) UpperCamelCase__: Optional[Any] = scheduler_class(**lowerCamelCase__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCamelCase__: Optional[Any] = self.dummy_model() UpperCamelCase__: Tuple = self.dummy_sample_deter * scheduler.init_noise_sigma UpperCamelCase__: List[Any] = sample.to(lowerCamelCase__ ) for i, t in enumerate(scheduler.timesteps ): UpperCamelCase__: List[Any] = scheduler.scale_model_input(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__: List[Any] = model(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__: Optional[int] = scheduler.step(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__: Any = output.prev_sample UpperCamelCase__: int = torch.sum(torch.abs(lowerCamelCase__ ) ) UpperCamelCase__: Dict = torch.mean(torch.abs(lowerCamelCase__ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 124.77_149_200_439_453 ) < 1e-2 assert abs(result_mean.item() - 0.16_226_289_014_816_284 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 128.1_663_360_595_703 ) < 1e-2 assert abs(result_mean.item() - 0.16_688_326_001_167_297 ) < 1e-3 else: assert abs(result_sum.item() - 119.8_487_548_828_125 ) < 1e-2 assert abs(result_mean.item() - 0.1_560_530_662_536_621 ) < 1e-3 def UpperCAmelCase_ ( self: List[Any] ): '''simple docstring''' UpperCamelCase__: str = self.scheduler_classes[0] UpperCamelCase__: str = self.get_scheduler_config() UpperCamelCase__: Tuple = scheduler_class(**lowerCamelCase__ ) scheduler.set_timesteps(self.num_inference_steps , device=lowerCamelCase__ ) UpperCamelCase__: str = self.dummy_model() UpperCamelCase__: Any = self.dummy_sample_deter.to(lowerCamelCase__ ) * scheduler.init_noise_sigma for t in scheduler.timesteps: UpperCamelCase__: Tuple = scheduler.scale_model_input(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__: Optional[Any] = model(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__: int = scheduler.step(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__: List[str] = output.prev_sample UpperCamelCase__: List[Any] = torch.sum(torch.abs(lowerCamelCase__ ) ) UpperCamelCase__: Any = torch.mean(torch.abs(lowerCamelCase__ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.46_957_397_460_938 ) < 1e-2 assert abs(result_mean.item() - 0.21_805_934_607_982_635 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59_353_637_695_312 ) < 1e-2 assert abs(result_mean.item() - 0.22_342_908_382_415_771 ) < 1e-3 else: assert abs(result_sum.item() - 162.52_383_422_851_562 ) < 1e-2 assert abs(result_mean.item() - 0.211_619_570_851_326 ) < 1e-3 def UpperCAmelCase_ ( self: Optional[Any] ): '''simple docstring''' UpperCamelCase__: List[str] = self.scheduler_classes[0] UpperCamelCase__: Optional[int] = self.get_scheduler_config() UpperCamelCase__: Optional[Any] = scheduler_class(**lowerCamelCase__ , use_karras_sigmas=lowerCamelCase__ ) scheduler.set_timesteps(self.num_inference_steps , device=lowerCamelCase__ ) UpperCamelCase__: Tuple = self.dummy_model() UpperCamelCase__: Optional[Any] = self.dummy_sample_deter.to(lowerCamelCase__ ) * scheduler.init_noise_sigma UpperCamelCase__: int = sample.to(lowerCamelCase__ ) for t in scheduler.timesteps: UpperCamelCase__: List[str] = scheduler.scale_model_input(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__: Dict = model(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__: Any = scheduler.step(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__: Dict = output.prev_sample UpperCamelCase__: Optional[int] = torch.sum(torch.abs(lowerCamelCase__ ) ) UpperCamelCase__: Union[str, Any] = torch.mean(torch.abs(lowerCamelCase__ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 176.66_974_135_742_188 ) < 1e-2 assert abs(result_mean.item() - 0.23_003_872_730_981_811 ) < 1e-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 177.63_653_564_453_125 ) < 1e-2 assert abs(result_mean.item() - 0.23_003_872_730_981_811 ) < 1e-2 else: assert abs(result_sum.item() - 170.3_135_223_388_672 ) < 1e-2 assert abs(result_mean.item() - 0.23_003_872_730_981_811 ) < 1e-2
149
A_ :Optional[int] = ''' # Installazione di Transformers ! pip install transformers datasets # Per installare dalla fonte invece dell\'ultima versione rilasciata, commenta il comando sopra e # rimuovi la modalità commento al comando seguente. # ! pip install git+https://github.com/huggingface/transformers.git ''' A_ :Union[str, Any] = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] A_ :Optional[Any] = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
71
0
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase ) -> int: '''simple docstring''' return int((input_a, input_a).count(1 ) != 0 ) def __magic_name__ ( ) -> None: '''simple docstring''' assert or_gate(0, 0 ) == 0 assert or_gate(0, 1 ) == 1 assert or_gate(1, 0 ) == 1 assert or_gate(1, 1 ) == 1 if __name__ == "__main__": print(or_gate(0, 1)) print(or_gate(1, 0)) print(or_gate(0, 0)) print(or_gate(1, 1))
56
import argparse import hashlib import os import urllib import warnings import torch from torch import nn from tqdm import tqdm from transformers import WhisperConfig, WhisperForConditionalGeneration A_ :Optional[Any] = { '''tiny.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt''', '''tiny''': '''https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt''', '''base.en''': '''https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt''', '''base''': '''https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt''', '''small.en''': '''https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt''', '''small''': '''https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt''', '''medium.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt''', '''medium''': '''https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt''', '''large''': '''https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large.pt''', '''large-v2''': '''https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt''', } def A ( a_ ) -> List[Any]: __UpperCamelCase : Any =['layers', 'blocks'] for k in ignore_keys: state_dict.pop(a_ ,a_ ) A_ :int = { '''blocks''': '''layers''', '''mlp.0''': '''fc1''', '''mlp.2''': '''fc2''', '''mlp_ln''': '''final_layer_norm''', '''.attn.query''': '''.self_attn.q_proj''', '''.attn.key''': '''.self_attn.k_proj''', '''.attn.value''': '''.self_attn.v_proj''', '''.attn_ln''': '''.self_attn_layer_norm''', '''.attn.out''': '''.self_attn.out_proj''', '''.cross_attn.query''': '''.encoder_attn.q_proj''', '''.cross_attn.key''': '''.encoder_attn.k_proj''', '''.cross_attn.value''': '''.encoder_attn.v_proj''', '''.cross_attn_ln''': '''.encoder_attn_layer_norm''', '''.cross_attn.out''': '''.encoder_attn.out_proj''', '''decoder.ln.''': '''decoder.layer_norm.''', '''encoder.ln.''': '''encoder.layer_norm.''', '''token_embedding''': '''embed_tokens''', '''encoder.positional_embedding''': '''encoder.embed_positions.weight''', '''decoder.positional_embedding''': '''decoder.embed_positions.weight''', '''ln_post''': '''layer_norm''', } def A ( a_ ) -> Union[str, Any]: __UpperCamelCase : str =list(s_dict.keys() ) for key in keys: __UpperCamelCase : str =key for k, v in WHISPER_MAPPING.items(): if k in key: __UpperCamelCase : Optional[Any] =new_key.replace(a_ ,a_ ) print(F'{key} -> {new_key}' ) __UpperCamelCase : Dict =s_dict.pop(a_ ) return s_dict def A ( a_ ) -> Optional[Any]: __UpperCamelCase , __UpperCamelCase : Tuple =emb.weight.shape __UpperCamelCase : Tuple =nn.Linear(a_ ,a_ ,bias=a_ ) __UpperCamelCase : List[Any] =emb.weight.data return lin_layer def A ( a_ ,a_ ) -> bytes: os.makedirs(a_ ,exist_ok=a_ ) __UpperCamelCase : Optional[int] =os.path.basename(a_ ) __UpperCamelCase : Union[str, Any] =url.split('/' )[-2] __UpperCamelCase : Union[str, Any] =os.path.join(a_ ,a_ ) if os.path.exists(a_ ) and not os.path.isfile(a_ ): raise RuntimeError(F'{download_target} exists and is not a regular file' ) if os.path.isfile(a_ ): __UpperCamelCase : str =open(a_ ,'rb' ).read() if hashlib.shaaaa(a_ ).hexdigest() == expected_shaaaa: return model_bytes else: warnings.warn(F'{download_target} exists, but the SHA256 checksum does not match; re-downloading the file' ) with urllib.request.urlopen(a_ ) as source, open(a_ ,'wb' ) as output: with tqdm( total=int(source.info().get('Content-Length' ) ) ,ncols=80 ,unit='iB' ,unit_scale=a_ ,unit_divisor=1_024 ) as loop: while True: __UpperCamelCase : Optional[Any] =source.read(8_192 ) if not buffer: break output.write(a_ ) loop.update(len(a_ ) ) __UpperCamelCase : List[Any] =open(a_ ,'rb' ).read() if hashlib.shaaaa(a_ ).hexdigest() != expected_shaaaa: raise RuntimeError( 'Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model.' ) return model_bytes def A ( a_ ,a_ ) -> Optional[Any]: if ".pt" not in checkpoint_path: __UpperCamelCase : int =_download(_MODELS[checkpoint_path] ) else: __UpperCamelCase : List[str] =torch.load(a_ ,map_location='cpu' ) __UpperCamelCase : Union[str, Any] =original_checkpoint['dims'] __UpperCamelCase : List[Any] =original_checkpoint['model_state_dict'] __UpperCamelCase : Dict =state_dict['decoder.token_embedding.weight'] remove_ignore_keys_(a_ ) rename_keys(a_ ) __UpperCamelCase : List[str] =True __UpperCamelCase : str =state_dict['decoder.layers.0.fc1.weight'].shape[0] __UpperCamelCase : Optional[int] =WhisperConfig( vocab_size=dimensions['n_vocab'] ,encoder_ffn_dim=a_ ,decoder_ffn_dim=a_ ,num_mel_bins=dimensions['n_mels'] ,d_model=dimensions['n_audio_state'] ,max_target_positions=dimensions['n_text_ctx'] ,encoder_layers=dimensions['n_audio_layer'] ,encoder_attention_heads=dimensions['n_audio_head'] ,decoder_layers=dimensions['n_text_layer'] ,decoder_attention_heads=dimensions['n_text_state'] ,max_source_positions=dimensions['n_audio_ctx'] ,) __UpperCamelCase : List[str] =WhisperForConditionalGeneration(a_ ) __UpperCamelCase , __UpperCamelCase : Union[str, Any] =model.model.load_state_dict(a_ ,strict=a_ ) if len(a_ ) > 0 and not set(a_ ) <= { "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: __UpperCamelCase : Optional[int] =make_linear_from_emb(model.model.decoder.embed_tokens ) else: __UpperCamelCase : List[str] =proj_out_weights model.save_pretrained(a_ ) if __name__ == "__main__": A_ :List[Any] = argparse.ArgumentParser() # # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Patht to the downloaded checkpoints''') parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') A_ :List[Any] = parser.parse_args() convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)
71
0
'''simple docstring''' import os def _UpperCamelCase ( ): '''simple docstring''' with open(os.path.dirname(a_ ) + """/grid.txt""" ) as f: UpperCAmelCase__ = [] # noqa: E741 for _ in range(20 ): l.append([int(a_ ) for x in f.readline().split()] ) UpperCAmelCase__ = 0 # right for i in range(20 ): for j in range(17 ): UpperCAmelCase__ = l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3] if temp > maximum: UpperCAmelCase__ = temp # down for i in range(17 ): for j in range(20 ): UpperCAmelCase__ = l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j] if temp > maximum: UpperCAmelCase__ = temp # diagonal 1 for i in range(17 ): for j in range(17 ): UpperCAmelCase__ = l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3] if temp > maximum: UpperCAmelCase__ = temp # diagonal 2 for i in range(17 ): for j in range(3 , 20 ): UpperCAmelCase__ = l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3] if temp > maximum: UpperCAmelCase__ = temp return maximum if __name__ == "__main__": print(solution())
346
import os from datetime import datetime as dt from github import Github A_ :str = [ '''good first issue''', '''feature request''', '''wip''', ] def A ( ) -> Any: __UpperCamelCase : Any =Github(os.environ['GITHUB_TOKEN'] ) __UpperCamelCase : Union[str, Any] =g.get_repo('huggingface/accelerate' ) __UpperCamelCase : Tuple =repo.get_issues(state='open' ) for issue in open_issues: __UpperCamelCase : List[Any] =sorted([comment for comment in issue.get_comments()] ,key=lambda a_ : i.created_at ,reverse=a_ ) __UpperCamelCase : str =comments[0] if len(a_ ) > 0 else None __UpperCamelCase : Any =dt.utcnow() __UpperCamelCase : List[str] =(current_time - issue.updated_at).days __UpperCamelCase : Union[str, Any] =(current_time - issue.created_at).days if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and days_since_updated > 7 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Close issue since it has been 7 days of inactivity since bot mention. issue.edit(state='closed' ) elif ( days_since_updated > 23 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Add stale comment issue.create_comment( 'This issue has been automatically marked as stale because it has not had ' 'recent activity. If you think this still needs to be addressed ' 'please comment on this thread.\n\nPlease note that issues that do not follow the ' '[contributing guidelines](https://github.com/huggingface/accelerate/blob/main/CONTRIBUTING.md) ' 'are likely to be ignored.' ) if __name__ == "__main__": main()
71
0
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy a = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ ( _a ): def __init__( self : Dict , lowerCAmelCase : str , lowerCAmelCase : int , lowerCAmelCase : Tuple , **lowerCAmelCase : Any ): lowerCAmelCase = feature_size lowerCAmelCase = sampling_rate lowerCAmelCase = padding_value lowerCAmelCase = kwargs.pop("""padding_side""" , """right""" ) lowerCAmelCase = kwargs.pop("""return_attention_mask""" , lowerCamelCase__ ) super().__init__(**lowerCamelCase__ ) def __lowercase ( self : List[str] , lowerCAmelCase : Optional[Any] , lowerCAmelCase : int = True , lowerCAmelCase : List[str] = None , lowerCAmelCase : Tuple = False , lowerCAmelCase : Union[str, Any] = None , lowerCAmelCase : List[Any] = None , lowerCAmelCase : Optional[Any] = None , ): if isinstance(lowerCamelCase__ , (list, tuple) ) and isinstance(processed_features[0] , (dict, BatchFeature) ): lowerCAmelCase = { key: [example[key] for example in processed_features] for key in processed_features[0].keys() } # The model's main input name, usually `input_values`, has be passed for padding if self.model_input_names[0] not in processed_features: raise ValueError( """You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`""" f''' to this method that includes {self.model_input_names[0]}, but you provided''' f''' {list(processed_features.keys() )}''' ) lowerCAmelCase = processed_features[self.model_input_names[0]] lowerCAmelCase = ( return_attention_mask if return_attention_mask is not None else self.return_attention_mask ) if len(lowerCamelCase__ ) == 0: if return_attention_mask: lowerCAmelCase = [] return processed_features # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays # and rebuild them afterwards if no return_tensors is specified # Note that we lose the specific device the tensor may be on for PyTorch lowerCAmelCase = required_input[0] if isinstance(lowerCamelCase__ , (list, tuple) ): # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element. lowerCAmelCase = 0 while len(required_input[index] ) == 0: index += 1 if index < len(lowerCamelCase__ ): lowerCAmelCase = required_input[index][0] if return_tensors is None: if is_tf_tensor(lowerCamelCase__ ): lowerCAmelCase = 'tf' elif is_torch_tensor(lowerCamelCase__ ): lowerCAmelCase = 'pt' elif isinstance(lowerCamelCase__ , (int, float, list, tuple, np.ndarray) ): lowerCAmelCase = 'np' else: raise ValueError( f'''type of {first_element} unknown: {type(lowerCamelCase__ )}. ''' """Should be one of a python, numpy, pytorch or tensorflow object.""" ) for key, value in processed_features.items(): if isinstance(value[0] , (int, float) ): lowerCAmelCase = to_numpy(lowerCamelCase__ ) else: lowerCAmelCase = [to_numpy(lowerCamelCase__ ) for v in value] # Convert padding_strategy in PaddingStrategy lowerCAmelCase = self._get_padding_strategies(padding=lowerCamelCase__ , max_length=lowerCamelCase__ ) lowerCAmelCase = processed_features[self.model_input_names[0]] lowerCAmelCase = len(lowerCamelCase__ ) if not all(len(lowerCamelCase__ ) == batch_size for v in processed_features.values() ): raise ValueError("""Some items in the output dictionary have a different batch size than others.""" ) lowerCAmelCase = [] for i in range(lowerCamelCase__ ): lowerCAmelCase = {k: v[i] for k, v in processed_features.items()} # truncation lowerCAmelCase = self._truncate( lowerCamelCase__ , max_length=lowerCamelCase__ , pad_to_multiple_of=lowerCamelCase__ , truncation=lowerCamelCase__ , ) truncated_inputs.append(lowerCamelCase__ ) if padding_strategy == PaddingStrategy.LONGEST: # make sure that `max_length` cannot be longer than the longest truncated length lowerCAmelCase = max(len(input_slice[self.model_input_names[0]] ) for input_slice in truncated_inputs ) lowerCAmelCase = PaddingStrategy.MAX_LENGTH lowerCAmelCase = {} for i in range(lowerCamelCase__ ): # padding lowerCAmelCase = self._pad( truncated_inputs[i] , max_length=lowerCamelCase__ , padding_strategy=lowerCamelCase__ , pad_to_multiple_of=lowerCamelCase__ , return_attention_mask=lowerCamelCase__ , ) for key, value in outputs.items(): if key not in batch_outputs: lowerCAmelCase = [] if value.dtype is np.dtype(np.floataa ): lowerCAmelCase = value.astype(np.floataa ) batch_outputs[key].append(lowerCamelCase__ ) return BatchFeature(lowerCamelCase__ , tensor_type=lowerCamelCase__ ) def __lowercase ( self : Optional[int] , lowerCAmelCase : Union[str, Any] , lowerCAmelCase : Optional[Any] = None , lowerCAmelCase : Union[str, Any] = PaddingStrategy.DO_NOT_PAD , lowerCAmelCase : Dict = None , lowerCAmelCase : Any = None , ): lowerCAmelCase = processed_features[self.model_input_names[0]] if padding_strategy == PaddingStrategy.LONGEST: lowerCAmelCase = len(lowerCamelCase__ ) if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): lowerCAmelCase = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of lowerCAmelCase = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(lowerCamelCase__ ) < max_length if return_attention_mask and "attention_mask" not in processed_features: lowerCAmelCase = np.ones(len(lowerCamelCase__ ) , dtype=np.intaa ) if needs_to_be_padded: lowerCAmelCase = max_length - len(lowerCamelCase__ ) if self.padding_side == "right": if return_attention_mask: lowerCAmelCase = np.pad( processed_features["""attention_mask"""] , (0, difference) ) lowerCAmelCase = ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference) lowerCAmelCase = np.pad( lowerCamelCase__ , lowerCamelCase__ , """constant""" , constant_values=self.padding_value ) elif self.padding_side == "left": if return_attention_mask: lowerCAmelCase = np.pad( processed_features["""attention_mask"""] , (difference, 0) ) lowerCAmelCase = ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0) lowerCAmelCase = np.pad( lowerCamelCase__ , lowerCamelCase__ , """constant""" , constant_values=self.padding_value ) else: raise ValueError("""Invalid padding strategy:""" + str(self.padding_side ) ) return processed_features def __lowercase ( self : Union[str, Any] , lowerCAmelCase : Union[str, Any] , lowerCAmelCase : int = None , lowerCAmelCase : List[str] = None , lowerCAmelCase : List[str] = None , ): if not truncation: return processed_features elif truncation and max_length is None: raise ValueError("""When setting ``truncation=True``, make sure that ``max_length`` is defined.""" ) lowerCAmelCase = processed_features[self.model_input_names[0]] # find `max_length` that fits `pad_to_multiple_of` if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): lowerCAmelCase = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of lowerCAmelCase = len(lowerCamelCase__ ) > max_length if needs_to_be_truncated: lowerCAmelCase = processed_features[self.model_input_names[0]][:max_length] if "attention_mask" in processed_features: lowerCAmelCase = processed_features['attention_mask'][:max_length] return processed_features def __lowercase ( self : Any , lowerCAmelCase : Union[str, Any]=False , lowerCAmelCase : Any=None ): if padding is not False: if padding is True: lowerCAmelCase = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch elif not isinstance(lowerCamelCase__ , lowerCamelCase__ ): lowerCAmelCase = PaddingStrategy(lowerCamelCase__ ) elif isinstance(lowerCamelCase__ , lowerCamelCase__ ): lowerCAmelCase = padding else: lowerCAmelCase = PaddingStrategy.DO_NOT_PAD # Set max length if needed if max_length is None: if padding_strategy == PaddingStrategy.MAX_LENGTH: raise ValueError( f'''When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined''' ) # Test if we have a padding value if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None): raise ValueError( """Asking to pad but the feature_extractor does not have a padding value. Please select a value to use""" """ as `padding_value`. For example: `feature_extractor.padding_value = 0.0`.""" ) return padding_strategy
155
import re def A ( a_ ) -> bool: __UpperCamelCase : Any =re.compile( r'^(?:0|94|\+94|0{2}94)' r'7(0|1|2|4|5|6|7|8)' r'(-| |)' r'\d{7}$' ) return bool(re.search(a_ ,a_ ) ) if __name__ == "__main__": A_ :List[str] = '''0094702343221''' print(is_sri_lankan_phone_number(phone))
71
0