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
from __future__ import annotations import unittest from transformers import EsmConfig, 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 numpy import tensorflow as tf from transformers.models.esm.modeling_tf_esm import ( TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, TFEsmModel, ) class __snake_case : def __init__( self , snake_case__ , ) -> str: '''simple docstring''' UpperCAmelCase : Any =parent UpperCAmelCase : Optional[int] =13 UpperCAmelCase : Union[str, Any] =7 UpperCAmelCase : int =True UpperCAmelCase : int =True UpperCAmelCase : Dict =True UpperCAmelCase : Dict =99 UpperCAmelCase : Any =32 UpperCAmelCase : Optional[int] =2 UpperCAmelCase : Optional[Any] =4 UpperCAmelCase : Any =37 UpperCAmelCase : Tuple ='''gelu''' UpperCAmelCase : Optional[Any] =0.1 UpperCAmelCase : Optional[Any] =0.1 UpperCAmelCase : Optional[Any] =512 UpperCAmelCase : Optional[Any] =16 UpperCAmelCase : Any =2 UpperCAmelCase : int =0.02 UpperCAmelCase : List[Any] =3 UpperCAmelCase : Optional[int] =4 UpperCAmelCase : List[Any] =None def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[int] =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : List[str] =None if self.use_input_mask: UpperCAmelCase : List[Any] =random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : int =None UpperCAmelCase : Tuple =None UpperCAmelCase : int =None if self.use_labels: UpperCAmelCase : int =ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase : int =ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCAmelCase : List[str] =ids_tensor([self.batch_size] , self.num_choices ) UpperCAmelCase : Any =EsmConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , pad_token_id=1 , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' ( ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ) : Union[str, Any] =self.prepare_config_and_inputs() UpperCAmelCase : List[str] =True UpperCAmelCase : List[Any] =floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) UpperCAmelCase : List[Any] =ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : Tuple =TFEsmModel(config=snake_case__ ) UpperCAmelCase : Optional[int] ={'''input_ids''': input_ids, '''attention_mask''': input_mask} UpperCAmelCase : str =model(snake_case__ ) UpperCAmelCase : Tuple =[input_ids, input_mask] UpperCAmelCase : Any =model(snake_case__ ) UpperCAmelCase : List[str] =model(snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> str: '''simple docstring''' UpperCAmelCase : Optional[Any] =True UpperCAmelCase : Optional[Any] =TFEsmModel(config=snake_case__ ) UpperCAmelCase : int ={ '''input_ids''': input_ids, '''attention_mask''': input_mask, '''encoder_hidden_states''': encoder_hidden_states, '''encoder_attention_mask''': encoder_attention_mask, } UpperCAmelCase : Optional[Any] =model(snake_case__ ) UpperCAmelCase : Optional[Any] =[input_ids, input_mask] UpperCAmelCase : int =model(snake_case__ , encoder_hidden_states=snake_case__ ) # Also check the case where encoder outputs are not passed UpperCAmelCase : Optional[Any] =model(snake_case__ , attention_mask=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[int] =TFEsmForMaskedLM(config=snake_case__ ) UpperCAmelCase : Optional[int] =model([input_ids, input_mask] ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : int =self.num_labels UpperCAmelCase : Dict =TFEsmForTokenClassification(config=snake_case__ ) UpperCAmelCase : int ={'''input_ids''': input_ids, '''attention_mask''': input_mask} UpperCAmelCase : Optional[int] =model(snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : Tuple =self.prepare_config_and_inputs() ( ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ) : Optional[Any] =config_and_inputs UpperCAmelCase : Optional[Any] ={'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Dict = ( ( TFEsmModel, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, ) if is_tf_available() else () ) __lowerCamelCase : List[Any] = ( { """feature-extraction""": TFEsmModel, """fill-mask""": TFEsmForMaskedLM, """text-classification""": TFEsmForSequenceClassification, """token-classification""": TFEsmForTokenClassification, """zero-shot""": TFEsmForSequenceClassification, } if is_tf_available() else {} ) __lowerCamelCase : Optional[Any] = False __lowerCamelCase : Optional[int] = False def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Any =TFEsmModelTester(self ) UpperCAmelCase : Union[str, Any] =ConfigTester(self , config_class=snake_case__ , hidden_size=37 ) def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' UpperCAmelCase : Union[str, Any] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Optional[int] =self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*snake_case__ ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : str =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*snake_case__ ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Dict =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*snake_case__ ) @slow def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase : Optional[int] =TFEsmModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) @unittest.skip('''Protein models do not support embedding resizing.''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @unittest.skip('''Protein models do not support embedding resizing.''' ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Dict =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase : Union[str, Any] =model_class(snake_case__ ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class is TFEsmForMaskedLM: # Output embedding test differs from the main test because they're a matrix, not a layer UpperCAmelCase : Tuple =model.get_bias() assert isinstance(snake_case__ , snake_case__ ) for k, v in name.items(): assert isinstance(snake_case__ , tf.Variable ) else: UpperCAmelCase : List[Any] =model.get_output_embeddings() assert x is None UpperCAmelCase : Optional[int] =model.get_bias() assert name is None @require_tf class __snake_case ( unittest.TestCase ): @slow def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =TFEsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) UpperCAmelCase : Dict =tf.constant([[0, 1, 2, 3, 4, 5]] ) UpperCAmelCase : int =model(snake_case__ )[0] UpperCAmelCase : Union[str, Any] =[1, 6, 33] self.assertEqual(list(output.numpy().shape ) , snake_case__ ) # compare the actual values for a slice. UpperCAmelCase : List[Any] =tf.constant( [ [ [8.92_1518, -10.58_9814, -6.467_1307], [-6.396_7156, -13.91_1377, -1.121_1915], [-7.78_1247, -13.95_1557, -3.74_0592], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-2 ) ) @slow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : str =TFEsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) UpperCAmelCase : List[str] =tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) UpperCAmelCase : Dict =model(snake_case__ )[0] # compare the actual values for a slice. UpperCAmelCase : str =tf.constant( [ [ [0.1444_3092, 0.5412_5327, 0.324_7739], [0.3034_0484, 0.0052_6676, 0.3107_7722], [0.3227_8043, -0.2498_7096, 0.341_4628], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
348
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { '''asapp/sew-d-tiny-100k''': '''https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json''', # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[Any] = """sew-d""" def __init__( self , snake_case__=32 , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=2 , snake_case__=512 , snake_case__=256 , snake_case__=True , snake_case__=True , snake_case__=("p2c", "c2p") , snake_case__="layer_norm" , snake_case__="gelu_python" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.0 , snake_case__=0.1 , snake_case__=0.02 , snake_case__=1e-7 , snake_case__=1e-5 , snake_case__="group" , snake_case__="gelu" , snake_case__=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , snake_case__=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , snake_case__=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , snake_case__=False , snake_case__=128 , snake_case__=16 , snake_case__=True , snake_case__=0.05 , snake_case__=10 , snake_case__=2 , snake_case__=0.0 , snake_case__=10 , snake_case__=0 , snake_case__="mean" , snake_case__=False , snake_case__=False , snake_case__=256 , snake_case__=0 , snake_case__=1 , snake_case__=2 , **snake_case__ , ) -> int: '''simple docstring''' super().__init__(**snake_case__ , pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ ) UpperCAmelCase : Union[str, Any] =hidden_size UpperCAmelCase : Union[str, Any] =feat_extract_norm UpperCAmelCase : Optional[Any] =feat_extract_activation UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : int =list(snake_case__ ) UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : str =conv_bias UpperCAmelCase : Tuple =num_conv_pos_embeddings UpperCAmelCase : Dict =num_conv_pos_embedding_groups UpperCAmelCase : str =len(self.conv_dim ) UpperCAmelCase : Dict =num_hidden_layers UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : List[Any] =squeeze_factor UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : int =position_buckets UpperCAmelCase : Optional[int] =share_att_key UpperCAmelCase : Optional[int] =relative_attention UpperCAmelCase : Tuple =norm_rel_ebd UpperCAmelCase : List[Any] =list(snake_case__ ) UpperCAmelCase : Dict =hidden_act UpperCAmelCase : Optional[int] =num_attention_heads UpperCAmelCase : Any =hidden_dropout UpperCAmelCase : str =attention_dropout UpperCAmelCase : Union[str, Any] =activation_dropout UpperCAmelCase : str =feat_proj_dropout UpperCAmelCase : Union[str, Any] =final_dropout UpperCAmelCase : Optional[int] =layer_norm_eps UpperCAmelCase : str =feature_layer_norm_eps UpperCAmelCase : str =initializer_range UpperCAmelCase : Any =vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect.''' '''It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,''' f'''but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)''' f'''= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCAmelCase : Union[str, Any] =apply_spec_augment UpperCAmelCase : Optional[Any] =mask_time_prob UpperCAmelCase : Tuple =mask_time_length UpperCAmelCase : str =mask_time_min_masks UpperCAmelCase : Optional[int] =mask_feature_prob UpperCAmelCase : Optional[Any] =mask_feature_length UpperCAmelCase : List[Any] =mask_feature_min_masks # ctc loss UpperCAmelCase : str =ctc_loss_reduction UpperCAmelCase : Optional[int] =ctc_zero_infinity # sequence classification UpperCAmelCase : Union[str, Any] =use_weighted_layer_sum UpperCAmelCase : int =classifier_proj_size @property def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
348
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) __snake_case = { '''configuration_mega''': ['''MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MegaConfig''', '''MegaOnnxConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''MEGA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MegaForCausalLM''', '''MegaForMaskedLM''', '''MegaForMultipleChoice''', '''MegaForQuestionAnswering''', '''MegaForSequenceClassification''', '''MegaForTokenClassification''', '''MegaModel''', '''MegaPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mega import ( MEGA_PRETRAINED_MODEL_ARCHIVE_LIST, MegaForCausalLM, MegaForMaskedLM, MegaForMultipleChoice, MegaForQuestionAnswering, MegaForSequenceClassification, MegaForTokenClassification, MegaModel, MegaPreTrainedModel, ) else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node __snake_case = 4 __snake_case = 3 class __snake_case ( lowerCamelCase__ ): pass def lowerCAmelCase_ ( __lowerCAmelCase )-> List[str]: '''simple docstring''' for shard in shards: for i in range(__lowerCAmelCase ): yield {"i": i, "shard": shard} def lowerCAmelCase_ ( )-> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =int(os.environ['''RANK'''] ) UpperCAmelCase : Optional[Any] =int(os.environ['''WORLD_SIZE'''] ) UpperCAmelCase : List[Any] =ArgumentParser() parser.add_argument('''--streaming''' , type=__lowerCAmelCase ) parser.add_argument('''--local_rank''' , type=__lowerCAmelCase ) parser.add_argument('''--num_workers''' , type=__lowerCAmelCase , default=0 ) UpperCAmelCase : Any =parser.parse_args() UpperCAmelCase : List[str] =args.streaming UpperCAmelCase : Tuple =args.num_workers UpperCAmelCase : int ={'''shards''': [f'''shard_{shard_idx}''' for shard_idx in range(__lowerCAmelCase )]} UpperCAmelCase : Optional[int] =IterableDataset.from_generator(__lowerCAmelCase , gen_kwargs=__lowerCAmelCase ) if not streaming: UpperCAmelCase : List[Any] =Dataset.from_list(list(__lowerCAmelCase ) ) UpperCAmelCase : Dict =split_dataset_by_node(__lowerCAmelCase , rank=__lowerCAmelCase , world_size=__lowerCAmelCase ) UpperCAmelCase : List[Any] =torch.utils.data.DataLoader(__lowerCAmelCase , num_workers=__lowerCAmelCase ) UpperCAmelCase : Dict =NUM_SHARDS * NUM_ITEMS_PER_SHARD UpperCAmelCase : str =full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) UpperCAmelCase : List[Any] =sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f'''local_size {local_size} != expected_local_size {expected_local_size}''' ) if __name__ == "__main__": main()
348
1
import PIL.Image import PIL.ImageOps from packaging import version from PIL import Image if version.parse(version.parse(PIL.__version__).base_version) >= version.parse('''9.1.0'''): __snake_case = { '''linear''': PIL.Image.Resampling.BILINEAR, '''bilinear''': PIL.Image.Resampling.BILINEAR, '''bicubic''': PIL.Image.Resampling.BICUBIC, '''lanczos''': PIL.Image.Resampling.LANCZOS, '''nearest''': PIL.Image.Resampling.NEAREST, } else: __snake_case = { '''linear''': PIL.Image.LINEAR, '''bilinear''': PIL.Image.BILINEAR, '''bicubic''': PIL.Image.BICUBIC, '''lanczos''': PIL.Image.LANCZOS, '''nearest''': PIL.Image.NEAREST, } def lowerCAmelCase_ ( __lowerCAmelCase )-> Dict: '''simple docstring''' UpperCAmelCase : Dict =(images / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase : int =images.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() UpperCAmelCase : List[str] =numpy_to_pil(__lowerCAmelCase ) return images def lowerCAmelCase_ ( __lowerCAmelCase )-> int: '''simple docstring''' if images.ndim == 3: UpperCAmelCase : Any =images[None, ...] UpperCAmelCase : Optional[Any] =(images * 2_55).round().astype('''uint8''' ) if images.shape[-1] == 1: # special case for grayscale (single channel) images UpperCAmelCase : List[str] =[Image.fromarray(image.squeeze() , mode='''L''' ) for image in images] else: UpperCAmelCase : str =[Image.fromarray(__lowerCAmelCase ) for image in images] return pil_images
348
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __snake_case = {'''configuration_opt''': ['''OPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OPTConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''OPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OPTForCausalLM''', '''OPTModel''', '''OPTPreTrainedModel''', '''OPTForSequenceClassification''', '''OPTForQuestionAnswering''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''TFOPTForCausalLM''', '''TFOPTModel''', '''TFOPTPreTrainedModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''FlaxOPTForCausalLM''', '''FlaxOPTModel''', '''FlaxOPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
1
import argparse import torch from datasets import load_dataset from donut import DonutModel from transformers import ( DonutImageProcessor, DonutProcessor, DonutSwinConfig, DonutSwinModel, MBartConfig, MBartForCausalLM, VisionEncoderDecoderModel, XLMRobertaTokenizerFast, ) def lowerCAmelCase_ ( __lowerCAmelCase )-> Optional[int]: '''simple docstring''' UpperCAmelCase : List[Any] =model.config UpperCAmelCase : Optional[int] =DonutSwinConfig( image_size=original_config.input_size , patch_size=4 , depths=original_config.encoder_layer , num_heads=[4, 8, 16, 32] , window_size=original_config.window_size , embed_dim=1_28 , ) UpperCAmelCase : int =MBartConfig( is_decoder=__lowerCAmelCase , is_encoder_decoder=__lowerCAmelCase , add_cross_attention=__lowerCAmelCase , decoder_layers=original_config.decoder_layer , max_position_embeddings=original_config.max_position_embeddings , vocab_size=len( model.decoder.tokenizer ) , scale_embedding=__lowerCAmelCase , add_final_layer_norm=__lowerCAmelCase , ) return encoder_config, decoder_config def lowerCAmelCase_ ( __lowerCAmelCase )-> List[Any]: '''simple docstring''' if "encoder.model" in name: UpperCAmelCase : int =name.replace('''encoder.model''' , '''encoder''' ) if "decoder.model" in name: UpperCAmelCase : Any =name.replace('''decoder.model''' , '''decoder''' ) if "patch_embed.proj" in name: UpperCAmelCase : Dict =name.replace('''patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) if "patch_embed.norm" in name: UpperCAmelCase : Optional[Any] =name.replace('''patch_embed.norm''' , '''embeddings.norm''' ) if name.startswith('''encoder''' ): if "layers" in name: UpperCAmelCase : str ='''encoder.''' + name if "attn.proj" in name: UpperCAmelCase : Tuple =name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name and "mask" not in name: UpperCAmelCase : int =name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: UpperCAmelCase : int =name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: UpperCAmelCase : Any =name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: UpperCAmelCase : Tuple =name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: UpperCAmelCase : Union[str, Any] =name.replace('''mlp.fc2''' , '''output.dense''' ) if name == "encoder.norm.weight": UpperCAmelCase : Optional[int] ='''encoder.layernorm.weight''' if name == "encoder.norm.bias": UpperCAmelCase : List[Any] ='''encoder.layernorm.bias''' return name def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> Dict: '''simple docstring''' for key in orig_state_dict.copy().keys(): UpperCAmelCase : List[Any] =orig_state_dict.pop(__lowerCAmelCase ) if "qkv" in key: UpperCAmelCase : Union[str, Any] =key.split('''.''' ) UpperCAmelCase : Any =int(key_split[3] ) UpperCAmelCase : str =int(key_split[5] ) UpperCAmelCase : Optional[Any] =model.encoder.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: UpperCAmelCase : str =val[:dim, :] UpperCAmelCase : Any =val[dim : dim * 2, :] UpperCAmelCase : List[str] =val[-dim:, :] else: UpperCAmelCase : Optional[int] =val[:dim] UpperCAmelCase : List[Any] =val[dim : dim * 2] UpperCAmelCase : Any =val[-dim:] elif "attn_mask" in key or key in ["encoder.model.norm.weight", "encoder.model.norm.bias"]: # HuggingFace implementation doesn't use attn_mask buffer # and model doesn't use final LayerNorms for the encoder pass else: UpperCAmelCase : Any =val return orig_state_dict def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase=None , __lowerCAmelCase=False )-> str: '''simple docstring''' UpperCAmelCase : Tuple =DonutModel.from_pretrained(__lowerCAmelCase ).eval() # load HuggingFace model UpperCAmelCase , UpperCAmelCase : Any =get_configs(__lowerCAmelCase ) UpperCAmelCase : List[Any] =DonutSwinModel(__lowerCAmelCase ) UpperCAmelCase : Optional[Any] =MBartForCausalLM(__lowerCAmelCase ) UpperCAmelCase : Optional[Any] =VisionEncoderDecoderModel(encoder=__lowerCAmelCase , decoder=__lowerCAmelCase ) model.eval() UpperCAmelCase : List[str] =original_model.state_dict() UpperCAmelCase : List[Any] =convert_state_dict(__lowerCAmelCase , __lowerCAmelCase ) model.load_state_dict(__lowerCAmelCase ) # verify results on scanned document UpperCAmelCase : Tuple =load_dataset('''hf-internal-testing/example-documents''' ) UpperCAmelCase : List[Any] =dataset['''test'''][0]['''image'''].convert('''RGB''' ) UpperCAmelCase : Optional[int] =XLMRobertaTokenizerFast.from_pretrained(__lowerCAmelCase , from_slow=__lowerCAmelCase ) UpperCAmelCase : List[Any] =DonutImageProcessor( do_align_long_axis=original_model.config.align_long_axis , size=original_model.config.input_size[::-1] ) UpperCAmelCase : str =DonutProcessor(__lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase : Dict =processor(__lowerCAmelCase , return_tensors='''pt''' ).pixel_values if model_name == "naver-clova-ix/donut-base-finetuned-docvqa": UpperCAmelCase : Any ='''<s_docvqa><s_question>{user_input}</s_question><s_answer>''' UpperCAmelCase : Any ='''When is the coffee break?''' UpperCAmelCase : Dict =task_prompt.replace('''{user_input}''' , __lowerCAmelCase ) elif model_name == "naver-clova-ix/donut-base-finetuned-rvlcdip": UpperCAmelCase : List[str] ='''<s_rvlcdip>''' elif model_name in [ "naver-clova-ix/donut-base-finetuned-cord-v1", "naver-clova-ix/donut-base-finetuned-cord-v1-2560", ]: UpperCAmelCase : Dict ='''<s_cord>''' elif model_name == "naver-clova-ix/donut-base-finetuned-cord-v2": UpperCAmelCase : Dict ='''s_cord-v2>''' elif model_name == "naver-clova-ix/donut-base-finetuned-zhtrainticket": UpperCAmelCase : Dict ='''<s_zhtrainticket>''' elif model_name in ["naver-clova-ix/donut-proto", "naver-clova-ix/donut-base"]: # use a random prompt UpperCAmelCase : List[str] ='''hello world''' else: raise ValueError('''Model name not supported''' ) UpperCAmelCase : Tuple =original_model.decoder.tokenizer(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , return_tensors='''pt''' )[ '''input_ids''' ] UpperCAmelCase : Any =original_model.encoder.model.patch_embed(__lowerCAmelCase ) UpperCAmelCase , UpperCAmelCase : List[Any] =model.encoder.embeddings(__lowerCAmelCase ) assert torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-3 ) # verify encoder hidden states UpperCAmelCase : Optional[Any] =original_model.encoder(__lowerCAmelCase ) UpperCAmelCase : Tuple =model.encoder(__lowerCAmelCase ).last_hidden_state assert torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-2 ) # verify decoder hidden states UpperCAmelCase : List[Any] =original_model(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ).logits UpperCAmelCase : Optional[Any] =model(__lowerCAmelCase , decoder_input_ids=__lowerCAmelCase ).logits assert torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-3 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(f'''Saving model and processor to {pytorch_dump_folder_path}''' ) model.save_pretrained(__lowerCAmelCase ) processor.save_pretrained(__lowerCAmelCase ) if push_to_hub: model.push_to_hub('''nielsr/''' + model_name.split('''/''' )[-1] , commit_message='''Update model''' ) processor.push_to_hub('''nielsr/''' + model_name.split('''/''' )[-1] , commit_message='''Update model''' ) if __name__ == "__main__": __snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''naver-clova-ix/donut-base-finetuned-docvqa''', required=False, type=str, help='''Name of the original model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, required=False, 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 and processor to the 🤗 hub.''', ) __snake_case = parser.parse_args() convert_donut_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
348
import tempfile import unittest import numpy as np import transformers from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax import jax.numpy as jnp from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel if is_torch_available(): import torch class __snake_case : def __init__( self , snake_case__ , snake_case__=14 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=False , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=4 , snake_case__=4 , snake_case__=4 , snake_case__=37 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , ) -> str: '''simple docstring''' UpperCAmelCase : str =parent UpperCAmelCase : Tuple =batch_size UpperCAmelCase : Optional[int] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Tuple =use_input_mask UpperCAmelCase : List[Any] =use_token_type_ids UpperCAmelCase : Optional[Any] =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : List[Any] =hidden_size UpperCAmelCase : Optional[int] =rotary_dim UpperCAmelCase : Union[str, Any] =num_hidden_layers UpperCAmelCase : List[Any] =num_attention_heads UpperCAmelCase : Dict =intermediate_size UpperCAmelCase : Union[str, Any] =hidden_act UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : Dict =attention_probs_dropout_prob UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : str =initializer_range UpperCAmelCase : Optional[int] =None UpperCAmelCase : List[Any] =vocab_size - 1 UpperCAmelCase : Optional[Any] =vocab_size - 1 UpperCAmelCase : List[Any] =vocab_size - 1 def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : List[Any] =None if self.use_input_mask: UpperCAmelCase : Optional[Any] =random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : Dict =GPTJConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , use_cache=snake_case__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , rotary_dim=self.rotary_dim , ) return (config, input_ids, input_mask) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Union[str, Any] =config_and_inputs UpperCAmelCase : Tuple ={'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =20 UpperCAmelCase : Any =model_class_name(snake_case__ ) UpperCAmelCase : str =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : Any =jnp.ones((input_ids.shape[0], max_decoder_length) , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : List[str] =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =model( input_ids[:, -1:] , attention_mask=snake_case__ , past_key_values=outputs_cache.past_key_values , position_ids=snake_case__ , ) UpperCAmelCase : List[Any] =model(snake_case__ ) UpperCAmelCase : Any =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Dict =20 UpperCAmelCase : Dict =model_class_name(snake_case__ ) UpperCAmelCase : Tuple =jnp.concatenate( [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]) )] , axis=-1 , ) UpperCAmelCase : Dict =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : int =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : str =model( input_ids[:, -1:] , past_key_values=outputs_cache.past_key_values , attention_mask=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =model(snake_case__ , attention_mask=snake_case__ ) UpperCAmelCase : Dict =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) @require_flax class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Tuple = (FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else () __lowerCamelCase : Optional[Any] = (FlaxGPTJForCausalLM,) if is_flax_available() else () def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =FlaxGPTJModelTester(self ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Dict =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward_with_attn_mask( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) @tooslow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =GPTaTokenizer.from_pretrained('''gpt2''' , pad_token='''<|endoftext|>''' , padding_side='''left''' ) UpperCAmelCase : Optional[Any] =tokenizer(['''Hello this is a long string''', '''Hey'''] , return_tensors='''np''' , padding=snake_case__ , truncation=snake_case__ ) UpperCAmelCase : Optional[int] =FlaxGPTJForCausalLM.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : str =False UpperCAmelCase : Union[str, Any] =model.config.eos_token_id UpperCAmelCase : List[Any] =jax.jit(model.generate ) UpperCAmelCase : Dict =jit_generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , pad_token_id=tokenizer.pad_token_id ).sequences UpperCAmelCase : Any =tokenizer.batch_decode(snake_case__ , skip_special_tokens=snake_case__ ) UpperCAmelCase : Tuple =[ '''Hello this is a long string of text.\n\nI\'m trying to get the text of the''', '''Hey, I\'m a little late to the party. I\'m going to''', ] self.assertListEqual(snake_case__ , snake_case__ ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : List[str] =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : Any =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : Any =getattr(snake_case__ , snake_case__ ) UpperCAmelCase , UpperCAmelCase : Union[str, Any] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Tuple =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : int =0 UpperCAmelCase : Optional[int] =1 UpperCAmelCase : Optional[int] =0 UpperCAmelCase : Union[str, Any] =1 UpperCAmelCase : List[str] =pt_model_class(snake_case__ ).eval() UpperCAmelCase : Optional[int] =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Any =convert_pytorch_state_dict_to_flax(pt_model.state_dict() , snake_case__ ) UpperCAmelCase : Union[str, Any] =fx_state with torch.no_grad(): UpperCAmelCase : Any =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : Dict =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(snake_case__ ) UpperCAmelCase : str =model_class.from_pretrained(snake_case__ , from_pt=snake_case__ ) UpperCAmelCase : int =fx_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output_loaded, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output_loaded[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : int =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : int =getattr(snake_case__ , snake_case__ ) UpperCAmelCase : Dict =pt_model_class(snake_case__ ).eval() UpperCAmelCase : str =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Optional[Any] =load_flax_weights_in_pytorch_model(snake_case__ , fx_model.params ) UpperCAmelCase , UpperCAmelCase : Optional[int] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Optional[int] =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : str =0 UpperCAmelCase : Any =1 UpperCAmelCase : List[Any] =0 UpperCAmelCase : Tuple =1 # make sure weights are tied in PyTorch pt_model.tie_weights() with torch.no_grad(): UpperCAmelCase : Optional[Any] =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : List[Any] =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(snake_case__ ) UpperCAmelCase : Tuple =pt_model_class.from_pretrained(snake_case__ , from_flax=snake_case__ ) with torch.no_grad(): UpperCAmelCase : Any =pt_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @tooslow def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase : str =model_class_name.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : Tuple =model(np.ones((1, 1) ) ) self.assertIsNotNone(snake_case__ )
348
1
def lowerCAmelCase_ ( __lowerCAmelCase )-> Union[str, Any]: '''simple docstring''' UpperCAmelCase : str =[] UpperCAmelCase : int =[] UpperCAmelCase : Union[str, Any] ={ '''^''': 3, '''*''': 2, '''/''': 2, '''%''': 2, '''+''': 1, '''-''': 1, } # Priority of each operator UpperCAmelCase : Optional[Any] =len(__lowerCAmelCase ) if (len(__lowerCAmelCase ) > 7) else 7 # Print table header for output print( '''Symbol'''.center(8 ) , '''Stack'''.center(__lowerCAmelCase ) , '''Postfix'''.center(__lowerCAmelCase ) , sep=''' | ''' , ) print('''-''' * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(__lowerCAmelCase ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(__lowerCAmelCase ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(__lowerCAmelCase ) == 0: stack.append(__lowerCAmelCase ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(__lowerCAmelCase ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(__lowerCAmelCase ) # push x to stack print( x.center(8 ) , (''''''.join(__lowerCAmelCase )).ljust(__lowerCAmelCase ) , (''''''.join(__lowerCAmelCase )).ljust(__lowerCAmelCase ) , sep=''' | ''' , ) # Output in tabular format while len(__lowerCAmelCase ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( ''' '''.center(8 ) , (''''''.join(__lowerCAmelCase )).ljust(__lowerCAmelCase ) , (''''''.join(__lowerCAmelCase )).ljust(__lowerCAmelCase ) , sep=''' | ''' , ) # Output in tabular format return "".join(__lowerCAmelCase ) # return Postfix as str def lowerCAmelCase_ ( __lowerCAmelCase )-> Tuple: '''simple docstring''' UpperCAmelCase : Any =list(infix[::-1] ) # reverse the infix equation for i in range(len(__lowerCAmelCase ) ): if infix[i] == "(": UpperCAmelCase : List[str] =''')''' # change "(" to ")" elif infix[i] == ")": UpperCAmelCase : Optional[Any] ='''(''' # change ")" to "(" return (infix_2_postfix(''''''.join(__lowerCAmelCase ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": __snake_case = input('''\nEnter an Infix Equation = ''') # Input an Infix equation __snake_case = ''''''.join(Infix.split()) # Remove spaces from the input print('''\n\t''', Infix, '''(Infix) -> ''', infix_2_prefix(Infix), '''(Prefix)''')
348
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __snake_case = { '''configuration_bloom''': ['''BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BloomConfig''', '''BloomOnnxConfig'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''BloomTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BloomForCausalLM''', '''BloomModel''', '''BloomPreTrainedModel''', '''BloomForSequenceClassification''', '''BloomForTokenClassification''', '''BloomForQuestionAnswering''', ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) __snake_case = { '''configuration_mobilebert''': [ '''MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MobileBertConfig''', '''MobileBertOnnxConfig''', ], '''tokenization_mobilebert''': ['''MobileBertTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''MobileBertTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''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: __snake_case = [ '''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 __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
import os from typing import Dict, List, Tuple, TypeVar, Union __snake_case = TypeVar('''T''') __snake_case = Union[List[T], Tuple[T, ...]] __snake_case = Union[T, List[T], Dict[str, T]] __snake_case = Union[str, bytes, os.PathLike]
348
1
from __future__ import annotations import copy import inspect import json import math import os import tempfile import unittest from importlib import import_module import numpy as np from transformers import ViTMAEConfig from transformers.file_utils import cached_property, is_tf_available, is_vision_available from transformers.testing_utils import require_tf, require_vision, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFViTMAEForPreTraining, TFViTMAEModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __snake_case : def __init__( self , snake_case__ , snake_case__=13 , snake_case__=30 , snake_case__=2 , snake_case__=3 , snake_case__=True , snake_case__=True , snake_case__=32 , snake_case__=2 , 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 , snake_case__=0.6 , snake_case__=None , ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =parent UpperCAmelCase : Optional[Any] =batch_size UpperCAmelCase : Union[str, Any] =image_size UpperCAmelCase : Tuple =patch_size UpperCAmelCase : int =num_channels UpperCAmelCase : Dict =is_training UpperCAmelCase : Optional[int] =use_labels UpperCAmelCase : int =hidden_size UpperCAmelCase : List[Any] =num_hidden_layers UpperCAmelCase : Tuple =num_attention_heads UpperCAmelCase : Optional[Any] =intermediate_size UpperCAmelCase : Any =hidden_act UpperCAmelCase : Optional[int] =hidden_dropout_prob UpperCAmelCase : List[str] =attention_probs_dropout_prob UpperCAmelCase : List[str] =type_sequence_label_size UpperCAmelCase : int =initializer_range UpperCAmelCase : Optional[Any] =mask_ratio UpperCAmelCase : str =scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) UpperCAmelCase : Tuple =(image_size // patch_size) ** 2 UpperCAmelCase : Dict =int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' UpperCAmelCase : List[str] =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCAmelCase : Optional[int] =None if self.use_labels: UpperCAmelCase : str =ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase : Tuple =self.get_config() return config, pixel_values, labels def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' return ViTMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , decoder_hidden_size=self.hidden_size , decoder_num_hidden_layers=self.num_hidden_layers , decoder_num_attention_heads=self.num_attention_heads , decoder_intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=snake_case__ , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> int: '''simple docstring''' UpperCAmelCase : Any =TFViTMAEModel(config=snake_case__ ) UpperCAmelCase : Any =model(snake_case__ , training=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Any: '''simple docstring''' UpperCAmelCase : Union[str, Any] =TFViTMAEForPreTraining(snake_case__ ) UpperCAmelCase : Optional[Any] =model(snake_case__ , training=snake_case__ ) # expected sequence length = num_patches UpperCAmelCase : List[str] =(self.image_size // self.patch_size) ** 2 UpperCAmelCase : str =self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images UpperCAmelCase : Dict =1 UpperCAmelCase : int =TFViTMAEForPreTraining(snake_case__ ) UpperCAmelCase : List[Any] =floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCAmelCase : List[Any] =model(snake_case__ , training=snake_case__ ) UpperCAmelCase : List[str] =self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =self.prepare_config_and_inputs() ((UpperCAmelCase) , (UpperCAmelCase) , (UpperCAmelCase)) : Optional[int] =config_and_inputs UpperCAmelCase : Dict ={'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Union[str, Any] = (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else () __lowerCamelCase : Dict = {"""feature-extraction""": TFViTMAEModel} if is_tf_available() else {} __lowerCamelCase : Any = False __lowerCamelCase : Dict = False __lowerCamelCase : Optional[int] = False __lowerCamelCase : str = False def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Any =TFViTMAEModelTester(self ) UpperCAmelCase : List[str] =ConfigTester(self , config_class=snake_case__ , has_text_modality=snake_case__ , hidden_size=37 ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='''ViTMAE does not use inputs_embeds''' ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : List[Any] =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase : List[Any] =model_class(snake_case__ ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) UpperCAmelCase : List[str] =model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case__ , tf.keras.layers.Layer ) ) def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Optional[int] =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase : Optional[Any] =model_class(snake_case__ ) UpperCAmelCase : int =inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase : Optional[int] =[*signature.parameters.keys()] UpperCAmelCase : str =['''pixel_values'''] self.assertListEqual(arg_names[:1] , snake_case__ ) def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : List[str] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : List[Any] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*snake_case__ ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' np.random.seed(2 ) UpperCAmelCase , UpperCAmelCase : Union[str, Any] =self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : Tuple =int((config.image_size // config.patch_size) ** 2 ) UpperCAmelCase : Dict =np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: UpperCAmelCase : List[str] =model_class(snake_case__ ) UpperCAmelCase : List[str] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =model(snake_case__ , noise=snake_case__ ) UpperCAmelCase : Union[str, Any] =copy.deepcopy(self._prepare_for_class(snake_case__ , snake_case__ ) ) UpperCAmelCase : Tuple =model(**snake_case__ , noise=snake_case__ ) UpperCAmelCase : Optional[int] =outputs_dict[0].numpy() UpperCAmelCase : List[Any] =outputs_keywords[0].numpy() self.assertLess(np.sum(np.abs(output_dict - output_keywords ) ) , 1e-6 ) def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' np.random.seed(2 ) UpperCAmelCase , UpperCAmelCase : List[str] =self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : Tuple =int((config.image_size // config.patch_size) ** 2 ) UpperCAmelCase : Optional[int] =np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) def prepare_numpy_arrays(snake_case__ ): UpperCAmelCase : Union[str, Any] ={} for k, v in inputs_dict.items(): if tf.is_tensor(snake_case__ ): UpperCAmelCase : Tuple =v.numpy() else: UpperCAmelCase : int =np.array(snake_case__ ) return inputs_np_dict for model_class in self.all_model_classes: UpperCAmelCase : Optional[int] =model_class(snake_case__ ) UpperCAmelCase : str =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : Dict =prepare_numpy_arrays(snake_case__ ) UpperCAmelCase : Optional[int] =model(snake_case__ , noise=snake_case__ ) UpperCAmelCase : Optional[Any] =model(**snake_case__ , noise=snake_case__ ) self.assert_outputs_same(snake_case__ , snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> List[str]: '''simple docstring''' np.random.seed(2 ) UpperCAmelCase : Any =int((tf_model.config.image_size // tf_model.config.patch_size) ** 2 ) UpperCAmelCase : Optional[Any] =np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) UpperCAmelCase : Tuple =tf.constant(snake_case__ ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument UpperCAmelCase : Optional[Any] =tf_noise super().check_pt_tf_models(snake_case__ , snake_case__ , snake_case__ ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' np.random.seed(2 ) UpperCAmelCase , UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : List[str] ={ module_member for model_class in self.all_model_classes for module in (import_module(model_class.__module__ ),) for module_member_name in dir(snake_case__ ) if module_member_name.endswith('''MainLayer''' ) # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`. and module_member_name[: -len('''MainLayer''' )] == model_class.__name__[: -len('''Model''' )] for module_member in (getattr(snake_case__ , snake_case__ ),) if isinstance(snake_case__ , snake_case__ ) and tf.keras.layers.Layer in module_member.__bases__ and getattr(snake_case__ , '''_keras_serializable''' , snake_case__ ) } UpperCAmelCase : str =int((config.image_size // config.patch_size) ** 2 ) UpperCAmelCase : str =np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) UpperCAmelCase : str =tf.convert_to_tensor(snake_case__ ) inputs_dict.update({'''noise''': noise} ) for main_layer_class in tf_main_layer_classes: UpperCAmelCase : int =main_layer_class(snake_case__ ) UpperCAmelCase : List[str] ={ name: tf.keras.Input(tensor.shape[1:] , dtype=tensor.dtype ) for name, tensor in inputs_dict.items() } UpperCAmelCase : int =tf.keras.Model(snake_case__ , outputs=main_layer(snake_case__ ) ) UpperCAmelCase : Union[str, Any] =model(snake_case__ ) with tempfile.TemporaryDirectory() as tmpdirname: UpperCAmelCase : List[Any] =os.path.join(snake_case__ , '''keras_model.h5''' ) model.save(snake_case__ ) UpperCAmelCase : Union[str, Any] =tf.keras.models.load_model( snake_case__ , custom_objects={main_layer_class.__name__: main_layer_class} ) assert isinstance(snake_case__ , tf.keras.Model ) UpperCAmelCase : Optional[int] =model(snake_case__ ) self.assert_outputs_same(snake_case__ , snake_case__ ) @slow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' np.random.seed(2 ) UpperCAmelCase , UpperCAmelCase : Optional[int] =self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : Tuple =int((config.image_size // config.patch_size) ** 2 ) UpperCAmelCase : Optional[int] =np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: UpperCAmelCase : int =model_class(snake_case__ ) UpperCAmelCase : List[Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =model(snake_case__ , noise=snake_case__ ) if model_class.__name__ == "TFViTMAEModel": UpperCAmelCase : Union[str, Any] =outputs.last_hidden_state.numpy() UpperCAmelCase : str =0 else: UpperCAmelCase : str =outputs.logits.numpy() UpperCAmelCase : List[str] =0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(snake_case__ , saved_model=snake_case__ ) UpperCAmelCase : int =model_class.from_pretrained(snake_case__ ) UpperCAmelCase : Tuple =model(snake_case__ , noise=snake_case__ ) if model_class.__name__ == "TFViTMAEModel": UpperCAmelCase : List[str] =after_outputs['''last_hidden_state'''].numpy() UpperCAmelCase : Optional[int] =0 else: UpperCAmelCase : Optional[Any] =after_outputs['''logits'''].numpy() UpperCAmelCase : List[str] =0 UpperCAmelCase : Union[str, Any] =np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(snake_case__ , 1e-5 ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' np.random.seed(2 ) UpperCAmelCase , UpperCAmelCase : List[str] =self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : Optional[Any] =int((config.image_size // config.patch_size) ** 2 ) UpperCAmelCase : Union[str, Any] =np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: UpperCAmelCase : Union[str, Any] =model_class(snake_case__ ) UpperCAmelCase : Tuple =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =model(snake_case__ , noise=snake_case__ ) UpperCAmelCase : Dict =model.get_config() # make sure that returned config is jsonifiable, which is required by keras json.dumps(snake_case__ ) UpperCAmelCase : Optional[Any] =model_class.from_config(model.get_config() ) # make sure it also accepts a normal config UpperCAmelCase : Union[str, Any] =model_class.from_config(model.config ) UpperCAmelCase : Optional[int] =new_model(snake_case__ ) # Build model new_model.set_weights(model.get_weights() ) UpperCAmelCase : Union[str, Any] =new_model(snake_case__ , noise=snake_case__ ) self.assert_outputs_same(snake_case__ , snake_case__ ) @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' pass @unittest.skip(reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @slow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Dict =TFViTMAEModel.from_pretrained('''google/vit-base-patch16-224''' ) self.assertIsNotNone(snake_case__ ) def lowerCAmelCase_ ( )-> Any: '''simple docstring''' UpperCAmelCase : List[Any] =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf @require_vision class __snake_case ( unittest.TestCase ): @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' return ViTImageProcessor.from_pretrained('''facebook/vit-mae-base''' ) if is_vision_available() else None @slow def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' np.random.seed(2 ) UpperCAmelCase : int =TFViTMAEForPreTraining.from_pretrained('''facebook/vit-mae-base''' ) UpperCAmelCase : List[str] =self.default_image_processor UpperCAmelCase : List[str] =prepare_img() UpperCAmelCase : str =image_processor(images=snake_case__ , return_tensors='''tf''' ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) UpperCAmelCase : Optional[Any] =ViTMAEConfig() UpperCAmelCase : Tuple =int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) UpperCAmelCase : Tuple =np.random.uniform(size=(1, num_patches) ) # forward pass UpperCAmelCase : Union[str, Any] =model(**snake_case__ , noise=snake_case__ ) # verify the logits UpperCAmelCase : str =tf.convert_to_tensor([1, 196, 768] ) self.assertEqual(outputs.logits.shape , snake_case__ ) UpperCAmelCase : Optional[Any] =tf.convert_to_tensor( [[-0.0548, -1.7023, -0.9325], [0.3721, -0.5670, -0.2233], [0.8235, -1.3878, -0.3524]] ) tf.debugging.assert_near(outputs.logits[0, :3, :3] , snake_case__ , atol=1e-4 )
348
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_big_bird import BigBirdTokenizer else: __snake_case = None __snake_case = logging.get_logger(__name__) __snake_case = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} __snake_case = { '''vocab_file''': { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model''', '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model''' ), }, '''tokenizer_file''': { '''google/bigbird-roberta-base''': ( '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/tokenizer.json''' ), '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/tokenizer.json''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/tokenizer.json''' ), }, } __snake_case = { '''google/bigbird-roberta-base''': 40_96, '''google/bigbird-roberta-large''': 40_96, '''google/bigbird-base-trivia-itc''': 40_96, } __snake_case = '''▁''' class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Dict = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = BigBirdTokenizer __lowerCamelCase : Any = ["""input_ids""", """attention_mask"""] __lowerCamelCase : List[int] = [] def __init__( self , snake_case__=None , snake_case__=None , snake_case__="<unk>" , snake_case__="<s>" , snake_case__="</s>" , snake_case__="<pad>" , snake_case__="[SEP]" , snake_case__="[MASK]" , snake_case__="[CLS]" , **snake_case__ , ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else bos_token UpperCAmelCase : Optional[int] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else eos_token UpperCAmelCase : List[str] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else unk_token UpperCAmelCase : Union[str, Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else pad_token UpperCAmelCase : int =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else cls_token UpperCAmelCase : str =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else sep_token # Mask token behave like a normal word, i.e. include the space before it UpperCAmelCase : List[Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else mask_token super().__init__( snake_case__ , tokenizer_file=snake_case__ , bos_token=snake_case__ , eos_token=snake_case__ , unk_token=snake_case__ , sep_token=snake_case__ , pad_token=snake_case__ , cls_token=snake_case__ , mask_token=snake_case__ , **snake_case__ , ) UpperCAmelCase : Tuple =vocab_file UpperCAmelCase : Optional[int] =False if not self.vocab_file else True def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : int =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None , snake_case__ = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is None: return [1] + ([0] * len(snake_case__ )) + [1] return [1] + ([0] * len(snake_case__ )) + [1] + ([0] * len(snake_case__ )) + [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : Optional[Any] =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> Tuple[str]: '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(snake_case__ ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCAmelCase : Optional[int] =os.path.join( snake_case__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case__ ): copyfile(self.vocab_file , snake_case__ ) return (out_vocab_file,)
348
1
import argparse import torch from transformers import ( SpeechTaConfig, SpeechTaFeatureExtractor, SpeechTaForSpeechToSpeech, SpeechTaForSpeechToText, SpeechTaForTextToSpeech, SpeechTaProcessor, SpeechTaTokenizer, logging, ) from transformers.tokenization_utils import AddedToken logging.set_verbosity_info() __snake_case = logging.get_logger('''transformers.models.speecht5''') __snake_case = { '''speech_encoder_prenet.layer_norm''': '''speecht5.encoder.prenet.feature_projection.layer_norm''', '''speech_encoder_prenet.post_extract_proj''': '''speecht5.encoder.prenet.feature_projection.projection''', '''speech_encoder_prenet.pos_conv.0''': '''speecht5.encoder.prenet.pos_conv_embed.conv''', '''speech_encoder_prenet.mask_emb''': '''speecht5.encoder.prenet.masked_spec_embed''', } __snake_case = { '''text_encoder_prenet.encoder_prenet.0''': '''speecht5.encoder.prenet.embed_tokens''', '''text_encoder_prenet.encoder_prenet.1.alpha''': '''speecht5.encoder.prenet.encode_positions.alpha''', } __snake_case = { '''speech_decoder_prenet.decoder_prenet.0.0.prenet.0.0''': '''speecht5.decoder.prenet.layers.0''', '''speech_decoder_prenet.decoder_prenet.0.0.prenet.1.0''': '''speecht5.decoder.prenet.layers.1''', '''speech_decoder_prenet.decoder_prenet.0.1''': '''speecht5.decoder.prenet.final_layer''', '''speech_decoder_prenet.decoder_prenet.1.alpha''': '''speecht5.decoder.prenet.encode_positions.alpha''', '''speech_decoder_prenet.spkembs_layer.0''': '''speecht5.decoder.prenet.speaker_embeds_layer''', } __snake_case = { '''speech_decoder_postnet.feat_out''': '''speech_decoder_postnet.feat_out''', '''speech_decoder_postnet.prob_out''': '''speech_decoder_postnet.prob_out''', '''speech_decoder_postnet.postnet.postnet.0.0''': '''speech_decoder_postnet.layers.0.conv''', '''speech_decoder_postnet.postnet.postnet.0.1''': '''speech_decoder_postnet.layers.0.batch_norm''', '''speech_decoder_postnet.postnet.postnet.1.0''': '''speech_decoder_postnet.layers.1.conv''', '''speech_decoder_postnet.postnet.postnet.1.1''': '''speech_decoder_postnet.layers.1.batch_norm''', '''speech_decoder_postnet.postnet.postnet.2.0''': '''speech_decoder_postnet.layers.2.conv''', '''speech_decoder_postnet.postnet.postnet.2.1''': '''speech_decoder_postnet.layers.2.batch_norm''', '''speech_decoder_postnet.postnet.postnet.3.0''': '''speech_decoder_postnet.layers.3.conv''', '''speech_decoder_postnet.postnet.postnet.3.1''': '''speech_decoder_postnet.layers.3.batch_norm''', '''speech_decoder_postnet.postnet.postnet.4.0''': '''speech_decoder_postnet.layers.4.conv''', '''speech_decoder_postnet.postnet.postnet.4.1''': '''speech_decoder_postnet.layers.4.batch_norm''', } __snake_case = { '''text_decoder_prenet.embed_tokens''': '''speecht5.decoder.prenet.embed_tokens''', } __snake_case = { '''text_decoder_postnet.output_projection''': '''text_decoder_postnet.lm_head''', } __snake_case = { '''encoder.layers.*.self_attn.k_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.k_proj''', '''encoder.layers.*.self_attn.v_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.v_proj''', '''encoder.layers.*.self_attn.q_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.q_proj''', '''encoder.layers.*.self_attn.out_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.out_proj''', '''encoder.layers.*.self_attn_layer_norm''': '''speecht5.encoder.wrapped_encoder.layers.*.layer_norm''', '''encoder.layers.*.fc1''': '''speecht5.encoder.wrapped_encoder.layers.*.feed_forward.intermediate_dense''', '''encoder.layers.*.fc2''': '''speecht5.encoder.wrapped_encoder.layers.*.feed_forward.output_dense''', '''encoder.layers.*.final_layer_norm''': '''speecht5.encoder.wrapped_encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''speecht5.encoder.wrapped_encoder.layer_norm''', '''encoder.pos_emb.pe_k''': '''speecht5.encoder.wrapped_encoder.embed_positions.pe_k''', } __snake_case = { '''decoder.layers.*.self_attn.k_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.k_proj''', '''decoder.layers.*.self_attn.v_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.v_proj''', '''decoder.layers.*.self_attn.q_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.q_proj''', '''decoder.layers.*.self_attn.out_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.out_proj''', '''decoder.layers.*.self_attn_layer_norm''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn_layer_norm''', '''decoder.layers.*.encoder_attn.k_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.k_proj''', '''decoder.layers.*.encoder_attn.v_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.v_proj''', '''decoder.layers.*.encoder_attn.q_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.q_proj''', '''decoder.layers.*.encoder_attn.out_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.out_proj''', '''decoder.layers.*.encoder_attn_layer_norm''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn_layer_norm''', '''decoder.layers.*.fc1''': '''speecht5.decoder.wrapped_decoder.layers.*.feed_forward.intermediate_dense''', '''decoder.layers.*.fc2''': '''speecht5.decoder.wrapped_decoder.layers.*.feed_forward.output_dense''', '''decoder.layers.*.final_layer_norm''': '''speecht5.decoder.wrapped_decoder.layers.*.final_layer_norm''', } __snake_case = { **MAPPING_SPEECH_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_TEXT_DECODER_PRENET, **MAPPING_TEXT_DECODER_POSTNET, } __snake_case = { **MAPPING_TEXT_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_SPEECH_DECODER_PRENET, **MAPPING_SPEECH_DECODER_POSTNET, } __snake_case = { **MAPPING_SPEECH_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_SPEECH_DECODER_PRENET, **MAPPING_SPEECH_DECODER_POSTNET, } __snake_case = [] __snake_case = [ '''encoder.version''', '''encoder.layers.*.norm_k.weight''', '''encoder.layers.*.norm_k.bias''', '''decoder.version''', '''decoder.layers.*.norm_k.weight''', '''decoder.layers.*.norm_k.bias''', '''decoder.pos_emb.pe_k''', '''speech_encoder_prenet.embed_positions._float_tensor''', '''text_decoder_prenet.embed_positions._float_tensor''', ] __snake_case = IGNORE_KEYS + [ '''encoder.proj''', '''text_encoder_prenet.*''', '''speech_decoder_prenet.*''', '''speech_decoder_postnet.*''', ] __snake_case = IGNORE_KEYS + [ '''encoder.proj''', '''speech_encoder_prenet.*''', '''text_decoder_prenet.*''', '''text_decoder_postnet.*''', ] __snake_case = IGNORE_KEYS + [ '''encoder.proj''', '''text_encoder_prenet.*''', '''text_decoder_prenet.*''', '''text_decoder_postnet.*''', ] def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> Optional[int]: '''simple docstring''' for attribute in key.split('''.''' ): UpperCAmelCase : Optional[int] =getattr(__lowerCAmelCase , __lowerCAmelCase ) if weight_type is not None: UpperCAmelCase : Any =getattr(__lowerCAmelCase , __lowerCAmelCase ).shape else: UpperCAmelCase : Dict =hf_pointer.shape if hf_shape != value.shape: raise ValueError( f'''Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be''' f''' {value.shape} for {full_name}''' ) if weight_type == "weight": UpperCAmelCase : Optional[Any] =value elif weight_type == "weight_g": UpperCAmelCase : Union[str, Any] =value elif weight_type == "weight_v": UpperCAmelCase : List[Any] =value elif weight_type == "bias": UpperCAmelCase : Any =value elif weight_type == "running_mean": UpperCAmelCase : List[Any] =value elif weight_type == "running_var": UpperCAmelCase : Union[str, Any] =value elif weight_type == "num_batches_tracked": UpperCAmelCase : List[str] =value else: UpperCAmelCase : Tuple =value logger.info(f'''{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.''' ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> Optional[Any]: '''simple docstring''' for key in ignore_keys: if key.endswith('''.*''' ): if name.startswith(key[:-1] ): return True elif ".*." in key: UpperCAmelCase , UpperCAmelCase : Dict =key.split('''.*.''' ) if prefix in name and suffix in name: return True elif key in name: return True return False def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> Dict: '''simple docstring''' UpperCAmelCase : List[Any] =[] if task == "s2t": UpperCAmelCase : Any =hf_model.speechta.encoder.prenet.feature_encoder UpperCAmelCase : List[Any] =MAPPING_S2T UpperCAmelCase : List[str] =IGNORE_KEYS_S2T elif task == "t2s": UpperCAmelCase : str =None UpperCAmelCase : Union[str, Any] =MAPPING_T2S UpperCAmelCase : Tuple =IGNORE_KEYS_T2S elif task == "s2s": UpperCAmelCase : Optional[int] =hf_model.speechta.encoder.prenet.feature_encoder UpperCAmelCase : Tuple =MAPPING_S2S UpperCAmelCase : List[str] =IGNORE_KEYS_S2S else: raise ValueError(f'''Unsupported task: {task}''' ) for name, value in fairseq_dict.items(): if should_ignore(__lowerCAmelCase , __lowerCAmelCase ): logger.info(f'''{name} was ignored''' ) continue UpperCAmelCase : Dict =False if "conv_layers" in name: load_conv_layer( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , hf_model.config.feat_extract_norm == '''group''' , ) UpperCAmelCase : str =True else: for key, mapped_key in MAPPING.items(): # mapped_key = "speecht5." + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if "*" in key: UpperCAmelCase , UpperCAmelCase : Union[str, Any] =key.split('''.*.''' ) if prefix in name and suffix in name: UpperCAmelCase : Optional[Any] =suffix # if key in name or key.split("w2v_model.")[-1] == name.split(".")[0]: if key in name: UpperCAmelCase : List[Any] =True if "*" in mapped_key: UpperCAmelCase : Optional[int] =name.split(__lowerCAmelCase )[0].split('''.''' )[-2] UpperCAmelCase : Optional[Any] =mapped_key.replace('''*''' , __lowerCAmelCase ) if "weight_g" in name: UpperCAmelCase : List[str] ='''weight_g''' elif "weight_v" in name: UpperCAmelCase : Any ='''weight_v''' elif "bias" in name: UpperCAmelCase : int ='''bias''' elif "weight" in name: UpperCAmelCase : Tuple ='''weight''' elif "running_mean" in name: UpperCAmelCase : Any ='''running_mean''' elif "running_var" in name: UpperCAmelCase : Dict ='''running_var''' elif "num_batches_tracked" in name: UpperCAmelCase : List[str] ='''num_batches_tracked''' else: UpperCAmelCase : Optional[int] =None set_recursively(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) continue if not is_used: unused_weights.append(__lowerCAmelCase ) logger.warning(f'''Unused weights: {unused_weights}''' ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Any =full_name.split('''conv_layers.''' )[-1] UpperCAmelCase : List[str] =name.split('''.''' ) UpperCAmelCase : Optional[int] =int(items[0] ) UpperCAmelCase : List[str] =int(items[1] ) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) UpperCAmelCase : Union[str, Any] =value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) UpperCAmelCase : List[str] =value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found.''' ) UpperCAmelCase : Any =value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found.''' ) UpperCAmelCase : List[Any] =value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(__lowerCAmelCase ) @torch.no_grad() def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , )-> Tuple: '''simple docstring''' if config_path is not None: UpperCAmelCase : Union[str, Any] =SpeechTaConfig.from_pretrained(__lowerCAmelCase ) else: UpperCAmelCase : Optional[int] =SpeechTaConfig() if task == "s2t": UpperCAmelCase : Dict =config.max_text_positions UpperCAmelCase : List[Any] =SpeechTaForSpeechToText(__lowerCAmelCase ) elif task == "t2s": UpperCAmelCase : int =18_76 UpperCAmelCase : Optional[Any] =6_00 UpperCAmelCase : Union[str, Any] =config.max_speech_positions UpperCAmelCase : Any =SpeechTaForTextToSpeech(__lowerCAmelCase ) elif task == "s2s": UpperCAmelCase : Union[str, Any] =18_76 UpperCAmelCase : Tuple =config.max_speech_positions UpperCAmelCase : Union[str, Any] =SpeechTaForSpeechToSpeech(__lowerCAmelCase ) else: raise ValueError(f'''Unknown task name: {task}''' ) if vocab_path: UpperCAmelCase : int =SpeechTaTokenizer(__lowerCAmelCase , model_max_length=config.max_text_positions ) # Mask token behaves like a normal word, i.e. include the space before it UpperCAmelCase : Any =AddedToken('''<mask>''' , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase ) UpperCAmelCase : Union[str, Any] =mask_token tokenizer.add_special_tokens({'''mask_token''': mask_token} ) tokenizer.add_tokens(['''<ctc_blank>'''] ) UpperCAmelCase : Any =SpeechTaFeatureExtractor() UpperCAmelCase : Optional[Any] =SpeechTaProcessor(tokenizer=__lowerCAmelCase , feature_extractor=__lowerCAmelCase ) processor.save_pretrained(__lowerCAmelCase ) UpperCAmelCase : Union[str, Any] =torch.load(__lowerCAmelCase ) recursively_load_weights(fairseq_checkpoint['''model'''] , __lowerCAmelCase , __lowerCAmelCase ) model.save_pretrained(__lowerCAmelCase ) if repo_id: print('''Pushing to the hub...''' ) processor.push_to_hub(__lowerCAmelCase ) model.push_to_hub(__lowerCAmelCase ) if __name__ == "__main__": __snake_case = argparse.ArgumentParser() parser.add_argument( '''--task''', default='''s2t''', type=str, help='''Type of the SpeechT5 model you\'d like to convert. Should be one of \'s2t\', \'t2s\', \'s2s\'.''', ) parser.add_argument('''--checkpoint_path''', required=True, default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--vocab_path''', default=None, type=str, help='''Path to SentencePiece model''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--pytorch_dump_folder_path''', required=True, default=None, type=str, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--push_to_hub''', default=None, type=str, help='''Where to upload the converted model on the 🤗 hub.''' ) __snake_case = parser.parse_args() convert_speechta_checkpoint( args.task, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.vocab_path, args.push_to_hub, )
348
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def lowerCAmelCase_ ( __lowerCAmelCase )-> Optional[Any]: '''simple docstring''' def is_in_circle(__lowerCAmelCase , __lowerCAmelCase ) -> bool: UpperCAmelCase : List[Any] =sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle UpperCAmelCase : List[Any] =mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(__lowerCAmelCase ) ) # The ratio of the area for circle to square is pi/4. UpperCAmelCase : Dict =proportion * 4 print(f'''The estimated value of pi is {pi_estimate}''' ) print(f'''The numpy value of pi is {pi}''' ) print(f'''The total error is {abs(pi - pi_estimate )}''' ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 , )-> float: '''simple docstring''' return mean( function_to_integrate(uniform(__lowerCAmelCase , __lowerCAmelCase ) ) for _ in range(__lowerCAmelCase ) ) * (max_value - min_value) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 )-> None: '''simple docstring''' def identity_function(__lowerCAmelCase ) -> float: return x UpperCAmelCase : List[Any] =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase : Dict =(max_value * max_value - min_value * min_value) / 2 print('''******************''' ) print(f'''Estimating area under y=x where x varies from {min_value} to {max_value}''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {expected_value}''' ) print(f'''Total error is {abs(estimated_value - expected_value )}''' ) print('''******************''' ) def lowerCAmelCase_ ( __lowerCAmelCase )-> None: '''simple docstring''' def function_to_integrate(__lowerCAmelCase ) -> float: return sqrt(4.0 - x * x ) UpperCAmelCase : Dict =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , 0.0 , 2.0 ) print('''******************''' ) print('''Estimating pi using area_under_curve_estimator''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {pi}''' ) print(f'''Total error is {abs(estimated_value - pi )}''' ) print('''******************''' ) if __name__ == "__main__": import doctest doctest.testmod()
348
1
from typing import List, Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Any = ["""image_processor""", """tokenizer"""] __lowerCamelCase : Optional[int] = """BlipImageProcessor""" __lowerCamelCase : List[Any] = ("""BertTokenizer""", """BertTokenizerFast""") def __init__( self , snake_case__ , snake_case__ ) -> Dict: '''simple docstring''' UpperCAmelCase : str =False super().__init__(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =self.image_processor def __call__( self , snake_case__ = None , snake_case__ = None , snake_case__ = True , snake_case__ = False , snake_case__ = None , snake_case__ = None , snake_case__ = 0 , snake_case__ = None , snake_case__ = None , snake_case__ = False , snake_case__ = False , snake_case__ = False , snake_case__ = False , snake_case__ = False , snake_case__ = True , snake_case__ = None , **snake_case__ , ) -> BatchEncoding: '''simple docstring''' if images is None and text is None: raise ValueError('''You have to specify either images or text.''' ) # Get only text if images is None: UpperCAmelCase : Union[str, Any] =self.tokenizer UpperCAmelCase : Any =self.tokenizer( text=snake_case__ , add_special_tokens=snake_case__ , padding=snake_case__ , truncation=snake_case__ , max_length=snake_case__ , stride=snake_case__ , pad_to_multiple_of=snake_case__ , return_attention_mask=snake_case__ , return_overflowing_tokens=snake_case__ , return_special_tokens_mask=snake_case__ , return_offsets_mapping=snake_case__ , return_token_type_ids=snake_case__ , return_length=snake_case__ , verbose=snake_case__ , return_tensors=snake_case__ , **snake_case__ , ) return text_encoding # add pixel_values UpperCAmelCase : Union[str, Any] =self.image_processor(snake_case__ , return_tensors=snake_case__ ) if text is not None: UpperCAmelCase : str =self.tokenizer( text=snake_case__ , add_special_tokens=snake_case__ , padding=snake_case__ , truncation=snake_case__ , max_length=snake_case__ , stride=snake_case__ , pad_to_multiple_of=snake_case__ , return_attention_mask=snake_case__ , return_overflowing_tokens=snake_case__ , return_special_tokens_mask=snake_case__ , return_offsets_mapping=snake_case__ , return_token_type_ids=snake_case__ , return_length=snake_case__ , verbose=snake_case__ , return_tensors=snake_case__ , **snake_case__ , ) else: UpperCAmelCase : Any =None if text_encoding is not None: encoding_image_processor.update(snake_case__ ) return encoding_image_processor def UpperCAmelCase__ ( self , *snake_case__ , **snake_case__ ) -> Union[str, Any]: '''simple docstring''' return self.tokenizer.batch_decode(*snake_case__ , **snake_case__ ) def UpperCAmelCase__ ( self , *snake_case__ , **snake_case__ ) -> List[str]: '''simple docstring''' return self.tokenizer.decode(*snake_case__ , **snake_case__ ) @property def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : List[str] =self.tokenizer.model_input_names UpperCAmelCase : List[Any] =self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
348
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __snake_case : def __init__( self , snake_case__ , snake_case__=12 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , snake_case__=0 , snake_case__=None , ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : List[Any] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Union[str, Any] =use_input_mask UpperCAmelCase : Tuple =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : Tuple =hidden_size UpperCAmelCase : Dict =projection_dim UpperCAmelCase : Optional[int] =num_hidden_layers UpperCAmelCase : Dict =num_attention_heads UpperCAmelCase : int =intermediate_size UpperCAmelCase : Any =dropout UpperCAmelCase : Union[str, Any] =attention_dropout UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : str =scope UpperCAmelCase : str =bos_token_id def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : int =None if self.use_input_mask: UpperCAmelCase : Union[str, Any] =random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: UpperCAmelCase : Optional[int] =input_mask.numpy() UpperCAmelCase , UpperCAmelCase : List[Any] =input_mask.shape UpperCAmelCase : Optional[Any] =np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : List[Any] =1 UpperCAmelCase : Tuple =0 UpperCAmelCase : List[Any] =self.get_config() return config, input_ids, tf.convert_to_tensor(snake_case__ ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =TFBlipTextModel(config=snake_case__ ) UpperCAmelCase : List[Any] =model(snake_case__ , attention_mask=snake_case__ , training=snake_case__ ) UpperCAmelCase : str =model(snake_case__ , training=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[Any] =config_and_inputs UpperCAmelCase : Optional[int] ={'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __snake_case ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Optional[int] = (TFBlipTextModel,) if is_tf_available() else () __lowerCamelCase : Dict = False __lowerCamelCase : Optional[Any] = False __lowerCamelCase : Dict = False def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : str =BlipTextModelTester(self ) UpperCAmelCase : Optional[int] =ConfigTester(self , config_class=snake_case__ , hidden_size=37 ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip(reason='''Blip does not use inputs_embeds''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @slow def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase : Optional[Any] =TFBlipTextModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def UpperCAmelCase__ ( self , snake_case__=True ) -> Any: '''simple docstring''' super().test_pt_tf_model_equivalence(allow_missing_keys=snake_case__ )
348
1
from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Optional[int] =[] for part_id in partition_order: UpperCAmelCase : Optional[Any] =df.where(f'''SPARK_PARTITION_ID() = {part_id}''' ).collect() for row_idx, row in enumerate(__lowerCAmelCase ): expected_row_ids_and_row_dicts.append((f'''{part_id}_{row_idx}''', row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def lowerCAmelCase_ ( )-> List[Any]: '''simple docstring''' UpperCAmelCase : List[Any] =pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() UpperCAmelCase : str =spark.range(1_00 ).repartition(1 ) UpperCAmelCase : str =Spark(__lowerCAmelCase ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def lowerCAmelCase_ ( )-> List[Any]: '''simple docstring''' UpperCAmelCase : Optional[Any] =pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() UpperCAmelCase : List[str] =spark.range(10 ).repartition(2 ) UpperCAmelCase : str =[1, 0] UpperCAmelCase : List[Any] =_generate_iterable_examples(__lowerCAmelCase , __lowerCAmelCase ) # Reverse the partitions. UpperCAmelCase : Tuple =_get_expected_row_ids_and_row_dicts_for_partition_order(__lowerCAmelCase , __lowerCAmelCase ) for i, (row_id, row_dict) in enumerate(generate_fn() ): UpperCAmelCase , UpperCAmelCase : Optional[int] =expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def lowerCAmelCase_ ( )-> int: '''simple docstring''' UpperCAmelCase : Any =pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() UpperCAmelCase : Optional[Any] =spark.range(10 ).repartition(1 ) UpperCAmelCase : Optional[Any] =SparkExamplesIterable(__lowerCAmelCase ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(__lowerCAmelCase ): assert row_id == f'''0_{i}''' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def lowerCAmelCase_ ( )-> List[str]: '''simple docstring''' UpperCAmelCase : Dict =pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() UpperCAmelCase : Optional[Any] =spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''' ) as generator_mock: UpperCAmelCase : Optional[int] =lambda __lowerCAmelCase : x.reverse() UpperCAmelCase : Optional[Any] =_get_expected_row_ids_and_row_dicts_for_partition_order(__lowerCAmelCase , [2, 1, 0] ) UpperCAmelCase : str =SparkExamplesIterable(__lowerCAmelCase ).shuffle_data_sources(__lowerCAmelCase ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(__lowerCAmelCase ): UpperCAmelCase , UpperCAmelCase : Optional[int] =expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def lowerCAmelCase_ ( )-> Optional[Any]: '''simple docstring''' UpperCAmelCase : Tuple =pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() UpperCAmelCase : Tuple =spark.range(20 ).repartition(4 ) # Partitions 0 and 2 UpperCAmelCase : str =SparkExamplesIterable(__lowerCAmelCase ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 UpperCAmelCase : Any =_get_expected_row_ids_and_row_dicts_for_partition_order(__lowerCAmelCase , [0, 2] ) for i, (row_id, row_dict) in enumerate(__lowerCAmelCase ): UpperCAmelCase , UpperCAmelCase : Any =expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 UpperCAmelCase : Optional[int] =SparkExamplesIterable(__lowerCAmelCase ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 UpperCAmelCase : str =_get_expected_row_ids_and_row_dicts_for_partition_order(__lowerCAmelCase , [1, 3] ) for i, (row_id, row_dict) in enumerate(__lowerCAmelCase ): UpperCAmelCase , UpperCAmelCase : Union[str, Any] =expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def lowerCAmelCase_ ( )-> Optional[Any]: '''simple docstring''' UpperCAmelCase : int =pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() UpperCAmelCase : List[str] =spark.range(1_00 ).repartition(1 ) UpperCAmelCase : Any =Spark(__lowerCAmelCase ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 1_00
348
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging __snake_case = logging.get_logger(__name__) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' UpperCAmelCase : Dict =nn.functional.normalize(__lowerCAmelCase ) UpperCAmelCase : Tuple =nn.functional.normalize(__lowerCAmelCase ) return torch.mm(__lowerCAmelCase , normalized_text_embeds.t() ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : List[str] = CLIPConfig __lowerCamelCase : List[Any] = ["""CLIPEncoderLayer"""] def __init__( self , snake_case__ ) -> Dict: '''simple docstring''' super().__init__(snake_case__ ) UpperCAmelCase : Dict =CLIPVisionModel(config.vision_config ) UpperCAmelCase : Optional[Any] =nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=snake_case__ ) UpperCAmelCase : int =nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : List[str] =nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : str =nn.Parameter(torch.ones(17 ) , requires_grad=snake_case__ ) UpperCAmelCase : Optional[int] =nn.Parameter(torch.ones(3 ) , requires_grad=snake_case__ ) @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Union[str, Any] =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : Optional[Any] =self.visual_projection(snake_case__ ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 UpperCAmelCase : List[str] =cosine_distance(snake_case__ , self.special_care_embeds ).cpu().float().numpy() UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ).cpu().float().numpy() UpperCAmelCase : Tuple =[] UpperCAmelCase : Dict =image_embeds.shape[0] for i in range(snake_case__ ): UpperCAmelCase : str ={'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : str =0.0 for concept_idx in range(len(special_cos_dist[0] ) ): UpperCAmelCase : Optional[Any] =special_cos_dist[i][concept_idx] UpperCAmelCase : Union[str, Any] =self.special_care_embeds_weights[concept_idx].item() UpperCAmelCase : str =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} ) UpperCAmelCase : int =0.01 for concept_idx in range(len(cos_dist[0] ) ): UpperCAmelCase : Any =cos_dist[i][concept_idx] UpperCAmelCase : Optional[int] =self.concept_embeds_weights[concept_idx].item() UpperCAmelCase : int =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(snake_case__ ) result.append(snake_case__ ) UpperCAmelCase : Optional[int] =[len(res['''bad_concepts'''] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Any =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : List[str] =self.visual_projection(snake_case__ ) UpperCAmelCase : Any =cosine_distance(snake_case__ , self.special_care_embeds ) UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : Optional[Any] =0.0 UpperCAmelCase : Any =special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) UpperCAmelCase : str =torch.any(special_scores > 0 , dim=1 ) UpperCAmelCase : List[Any] =special_care * 0.01 UpperCAmelCase : Union[str, Any] =special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) UpperCAmelCase : List[Any] =(cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) UpperCAmelCase : str =torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
348
1
import argparse import pickle import numpy as np import torch from torch import nn from transformers import ReformerConfig, ReformerModelWithLMHead from transformers.utils import logging logging.set_verbosity_info() def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=None )-> Optional[int]: '''simple docstring''' assert torch_layer.weight.shape == weight.shape, f'''{torch_layer} layer.weight does not match''' UpperCAmelCase : Optional[Any] =nn.Parameter(__lowerCAmelCase ) if bias is not None: assert torch_layer.bias.shape == bias.shape, f'''{torch_layer} layer.bias does not match''' UpperCAmelCase : List[str] =nn.Parameter(__lowerCAmelCase ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> Optional[int]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =np.asarray(weights[0] ) UpperCAmelCase : Union[str, Any] =np.asarray(weights[1] ) UpperCAmelCase : int =np.asarray(weights[2] ) set_param( torch_layer.self_attention.query_key , torch.tensor(__lowerCAmelCase ).transpose(1 , 2 ).contiguous().view(-1 , __lowerCAmelCase ) , ) set_param( torch_layer.self_attention.value , torch.tensor(__lowerCAmelCase ).transpose(1 , 2 ).contiguous().view(-1 , __lowerCAmelCase ) , ) set_param( torch_layer.output.dense , torch.tensor(__lowerCAmelCase ).view(-1 , __lowerCAmelCase ).contiguous().transpose(0 , 1 ) , ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> Optional[int]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =np.asarray(weights[0] ) UpperCAmelCase : List[str] =np.asarray(weights[1] ) UpperCAmelCase : Any =np.asarray(weights[2] ) UpperCAmelCase : List[Any] =np.asarray(weights[3] ) set_param( torch_layer.self_attention.query , torch.tensor(__lowerCAmelCase ).transpose(1 , 2 ).contiguous().view(-1 , __lowerCAmelCase ) , ) set_param( torch_layer.self_attention.key , torch.tensor(__lowerCAmelCase ).transpose(1 , 2 ).contiguous().view(-1 , __lowerCAmelCase ) , ) set_param( torch_layer.self_attention.value , torch.tensor(__lowerCAmelCase ).transpose(1 , 2 ).contiguous().view(-1 , __lowerCAmelCase ) , ) set_param( torch_layer.output.dense , torch.tensor(__lowerCAmelCase ).view(-1 , __lowerCAmelCase ).contiguous().transpose(0 , 1 ) , ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> int: '''simple docstring''' UpperCAmelCase : Optional[int] =weights[0][0][0] UpperCAmelCase : str =np.asarray(layer_norm_a[0] ) UpperCAmelCase : Dict =np.asarray(layer_norm_a[1] ) set_param( torch_block.attention.layer_norm , torch.tensor(__lowerCAmelCase ) , torch.tensor(__lowerCAmelCase ) , ) # lsh weights + output UpperCAmelCase : List[Any] =weights[0][1] if len(__lowerCAmelCase ) < 4: set_layer_weights_in_torch_lsh(__lowerCAmelCase , torch_block.attention , __lowerCAmelCase ) else: set_layer_weights_in_torch_local(__lowerCAmelCase , torch_block.attention , __lowerCAmelCase ) # intermediate weighs UpperCAmelCase : Any =weights[2][0][1][2] # Chunked Feed Forward if len(__lowerCAmelCase ) == 4: UpperCAmelCase : Union[str, Any] =intermediate_weights[2] # layernorm 2 UpperCAmelCase : Any =np.asarray(intermediate_weights[0][0] ) UpperCAmelCase : List[str] =np.asarray(intermediate_weights[0][1] ) set_param( torch_block.feed_forward.layer_norm , torch.tensor(__lowerCAmelCase ) , torch.tensor(__lowerCAmelCase ) , ) # intermediate dense UpperCAmelCase : List[str] =np.asarray(intermediate_weights[1][0] ) UpperCAmelCase : str =np.asarray(intermediate_weights[1][1] ) set_param( torch_block.feed_forward.dense.dense , torch.tensor(__lowerCAmelCase ).transpose(0 , 1 ).contiguous() , torch.tensor(__lowerCAmelCase ) , ) # intermediate out UpperCAmelCase : str =np.asarray(intermediate_weights[4][0] ) UpperCAmelCase : Any =np.asarray(intermediate_weights[4][1] ) set_param( torch_block.feed_forward.output.dense , torch.tensor(__lowerCAmelCase ).transpose(0 , 1 ).contiguous() , torch.tensor(__lowerCAmelCase ) , ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> List[str]: '''simple docstring''' UpperCAmelCase : List[str] =torch_model.reformer # word embeds UpperCAmelCase : List[str] =np.asarray(weights[1] ) set_param( torch_model_reformer.embeddings.word_embeddings , torch.tensor(__lowerCAmelCase ) , ) if isinstance(weights[3] , __lowerCAmelCase ): UpperCAmelCase : Any =torch_model_reformer.embeddings.position_embeddings for emb_idx in range(len(position_embeddings.weights ) ): UpperCAmelCase : Union[str, Any] =np.asarray(weights[3][emb_idx][0] ) assert ( position_embeddings.weights[emb_idx].shape == emb_weights.shape ), f'''{position_embeddings[emb_idx]} emb does not match''' UpperCAmelCase : Optional[Any] =nn.Parameter(torch.tensor(__lowerCAmelCase ) ) UpperCAmelCase : List[str] =weights[5] assert len(torch_model_reformer.encoder.layers ) * 4 == len( __lowerCAmelCase ), "HF and trax model do not have the same number of layers" for layer_idx, layer in enumerate(torch_model_reformer.encoder.layers ): UpperCAmelCase : Tuple =trax_layer_weights[4 * layer_idx : 4 * (layer_idx + 1)] set_block_weights_in_torch(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) # output layer norm UpperCAmelCase : Union[str, Any] =np.asarray(weights[7][0] ) UpperCAmelCase : Optional[Any] =np.asarray(weights[7][1] ) set_param( torch_model_reformer.encoder.layer_norm , torch.tensor(__lowerCAmelCase ) , torch.tensor(__lowerCAmelCase ) , ) # output embeddings UpperCAmelCase : List[Any] =np.asarray(weights[9][0] ) UpperCAmelCase : Tuple =np.asarray(weights[9][1] ) set_param( torch_model.lm_head.decoder , torch.tensor(__lowerCAmelCase ).transpose(0 , 1 ).contiguous() , torch.tensor(__lowerCAmelCase ) , ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> Optional[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =ReformerConfig.from_json_file(__lowerCAmelCase ) print(f'''Building PyTorch model from configuration: {config}''' ) UpperCAmelCase : List[str] =ReformerModelWithLMHead(__lowerCAmelCase ) with open(__lowerCAmelCase , '''rb''' ) as f: UpperCAmelCase : Optional[Any] =pickle.load(__lowerCAmelCase )['''weights'''] set_model_weights_in_torch(__lowerCAmelCase , __lowerCAmelCase , config.hidden_size ) # Save pytorch-model print(f'''Save PyTorch model to {pytorch_dump_path}''' ) torch.save(model.state_dict() , __lowerCAmelCase ) if __name__ == "__main__": __snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--trax_model_pkl_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained Reformer model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __snake_case = parser.parse_args() convert_trax_checkpoint_to_pytorch(args.trax_model_pkl_path, args.config_file, args.pytorch_dump_path)
348
import argparse import intel_extension_for_pytorch as ipex import torch from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline __snake_case = argparse.ArgumentParser('''Stable Diffusion script with intel optimization''', add_help=False) parser.add_argument('''--dpm''', action='''store_true''', help='''Enable DPMSolver or not''') parser.add_argument('''--steps''', default=None, type=int, help='''Num inference steps''') __snake_case = parser.parse_args() __snake_case = '''cpu''' __snake_case = '''a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings''' __snake_case = '''path-to-your-trained-model''' __snake_case = StableDiffusionPipeline.from_pretrained(model_id) if args.dpm: __snake_case = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) __snake_case = pipe.to(device) # to channels last __snake_case = pipe.unet.to(memory_format=torch.channels_last) __snake_case = pipe.vae.to(memory_format=torch.channels_last) __snake_case = pipe.text_encoder.to(memory_format=torch.channels_last) if pipe.requires_safety_checker: __snake_case = pipe.safety_checker.to(memory_format=torch.channels_last) # optimize with ipex __snake_case = torch.randn(2, 4, 64, 64) __snake_case = torch.rand(1) * 9_99 __snake_case = torch.randn(2, 77, 7_68) __snake_case = (sample, timestep, encoder_hidden_status) try: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example) except Exception: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True) if pipe.requires_safety_checker: __snake_case = ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True) # compute __snake_case = 6_66 __snake_case = torch.Generator(device).manual_seed(seed) __snake_case = {'''generator''': generator} if args.steps is not None: __snake_case = args.steps with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa): __snake_case = pipe(prompt, **generate_kwargs).images[0] # save image image.save('''generated.png''')
348
1
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { '''asapp/sew-d-tiny-100k''': '''https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json''', # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[Any] = """sew-d""" def __init__( self , snake_case__=32 , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=2 , snake_case__=512 , snake_case__=256 , snake_case__=True , snake_case__=True , snake_case__=("p2c", "c2p") , snake_case__="layer_norm" , snake_case__="gelu_python" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.0 , snake_case__=0.1 , snake_case__=0.02 , snake_case__=1e-7 , snake_case__=1e-5 , snake_case__="group" , snake_case__="gelu" , snake_case__=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , snake_case__=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , snake_case__=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , snake_case__=False , snake_case__=128 , snake_case__=16 , snake_case__=True , snake_case__=0.05 , snake_case__=10 , snake_case__=2 , snake_case__=0.0 , snake_case__=10 , snake_case__=0 , snake_case__="mean" , snake_case__=False , snake_case__=False , snake_case__=256 , snake_case__=0 , snake_case__=1 , snake_case__=2 , **snake_case__ , ) -> int: '''simple docstring''' super().__init__(**snake_case__ , pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ ) UpperCAmelCase : Union[str, Any] =hidden_size UpperCAmelCase : Union[str, Any] =feat_extract_norm UpperCAmelCase : Optional[Any] =feat_extract_activation UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : int =list(snake_case__ ) UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : str =conv_bias UpperCAmelCase : Tuple =num_conv_pos_embeddings UpperCAmelCase : Dict =num_conv_pos_embedding_groups UpperCAmelCase : str =len(self.conv_dim ) UpperCAmelCase : Dict =num_hidden_layers UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : List[Any] =squeeze_factor UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : int =position_buckets UpperCAmelCase : Optional[int] =share_att_key UpperCAmelCase : Optional[int] =relative_attention UpperCAmelCase : Tuple =norm_rel_ebd UpperCAmelCase : List[Any] =list(snake_case__ ) UpperCAmelCase : Dict =hidden_act UpperCAmelCase : Optional[int] =num_attention_heads UpperCAmelCase : Any =hidden_dropout UpperCAmelCase : str =attention_dropout UpperCAmelCase : Union[str, Any] =activation_dropout UpperCAmelCase : str =feat_proj_dropout UpperCAmelCase : Union[str, Any] =final_dropout UpperCAmelCase : Optional[int] =layer_norm_eps UpperCAmelCase : str =feature_layer_norm_eps UpperCAmelCase : str =initializer_range UpperCAmelCase : Any =vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect.''' '''It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,''' f'''but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)''' f'''= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCAmelCase : Union[str, Any] =apply_spec_augment UpperCAmelCase : Optional[Any] =mask_time_prob UpperCAmelCase : Tuple =mask_time_length UpperCAmelCase : str =mask_time_min_masks UpperCAmelCase : Optional[int] =mask_feature_prob UpperCAmelCase : Optional[Any] =mask_feature_length UpperCAmelCase : List[Any] =mask_feature_min_masks # ctc loss UpperCAmelCase : str =ctc_loss_reduction UpperCAmelCase : Optional[int] =ctc_zero_infinity # sequence classification UpperCAmelCase : Union[str, Any] =use_weighted_layer_sum UpperCAmelCase : int =classifier_proj_size @property def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
348
__snake_case = '''Input must be a string of 8 numbers plus letter''' __snake_case = '''TRWAGMYFPDXBNJZSQVHLCKE''' def lowerCAmelCase_ ( __lowerCAmelCase )-> bool: '''simple docstring''' if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCAmelCase : Optional[Any] =f'''Expected string as input, found {type(__lowerCAmelCase ).__name__}''' raise TypeError(__lowerCAmelCase ) UpperCAmelCase : List[Any] =spanish_id.replace('''-''' , '''''' ).upper() if len(__lowerCAmelCase ) != 9: raise ValueError(__lowerCAmelCase ) try: UpperCAmelCase : int =int(spanish_id_clean[0:8] ) UpperCAmelCase : Optional[int] =spanish_id_clean[8] except ValueError as ex: raise ValueError(__lowerCAmelCase ) from ex if letter.isdigit(): raise ValueError(__lowerCAmelCase ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
348
1
import copy import inspect import unittest from transformers import AutoBackbone from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import require_timm, require_torch, torch_device from transformers.utils.import_utils import is_torch_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor if is_torch_available(): import torch from transformers import TimmBackbone, TimmBackboneConfig from ...test_pipeline_mixin import PipelineTesterMixin class __snake_case : def __init__( self , snake_case__ , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__="resnet50" , snake_case__=3 , snake_case__=32 , snake_case__=3 , snake_case__=True , snake_case__=True , ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =parent UpperCAmelCase : Any =out_indices if out_indices is not None else [4] UpperCAmelCase : Dict =stage_names UpperCAmelCase : Dict =out_features UpperCAmelCase : Optional[int] =backbone UpperCAmelCase : List[Any] =batch_size UpperCAmelCase : List[Any] =image_size UpperCAmelCase : List[str] =num_channels UpperCAmelCase : List[str] =use_pretrained_backbone UpperCAmelCase : Dict =is_training def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : int =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCAmelCase : Tuple =self.get_config() return config, pixel_values def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' return TimmBackboneConfig( image_size=self.image_size , num_channels=self.num_channels , out_features=self.out_features , out_indices=self.out_indices , stage_names=self.stage_names , use_pretrained_backbone=self.use_pretrained_backbone , backbone=self.backbone , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Dict: '''simple docstring''' UpperCAmelCase : int =TimmBackbone(config=snake_case__ ) model.to(snake_case__ ) model.eval() with torch.no_grad(): UpperCAmelCase : List[str] =model(snake_case__ ) self.parent.assertEqual( result.feature_map[-1].shape , (self.batch_size, model.channels[-1], 14, 14) , ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : int =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase : Any =config_and_inputs UpperCAmelCase : str ={'''pixel_values''': pixel_values} return config, inputs_dict @require_torch @require_timm class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Dict = (TimmBackbone,) if is_torch_available() else () __lowerCamelCase : Optional[Any] = {"""feature-extraction""": TimmBackbone} if is_torch_available() else {} __lowerCamelCase : Dict = False __lowerCamelCase : str = False __lowerCamelCase : Dict = False __lowerCamelCase : str = False def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : List[Any] =TimmBackboneModelTester(self ) UpperCAmelCase : Any =ConfigTester(self , config_class=snake_case__ , has_text_modality=snake_case__ ) def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : List[str] ='''resnet18''' UpperCAmelCase : Optional[Any] ='''microsoft/resnet-18''' UpperCAmelCase : Optional[int] =AutoBackbone.from_pretrained(snake_case__ , use_timm_backbone=snake_case__ ) UpperCAmelCase : Optional[int] =AutoBackbone.from_pretrained(snake_case__ ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(len(timm_model.stage_names ) , len(transformers_model.stage_names ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) # Out indices are set to the last layer by default. For timm models, we don't know # the number of layers in advance, so we set it to (-1,), whereas for transformers # models, we set it to [len(stage_names) - 1] (kept for backward compatibility). self.assertEqual(timm_model.out_indices , (-1,) ) self.assertEqual(transformers_model.out_indices , [len(timm_model.stage_names ) - 1] ) UpperCAmelCase : Dict =AutoBackbone.from_pretrained(snake_case__ , use_timm_backbone=snake_case__ , out_indices=[1, 2, 3] ) UpperCAmelCase : Tuple =AutoBackbone.from_pretrained(snake_case__ , out_indices=[1, 2, 3] ) self.assertEqual(timm_model.out_indices , transformers_model.out_indices ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) @unittest.skip('''TimmBackbone doesn\'t support feed forward chunking''' ) def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' pass @unittest.skip('''TimmBackbone doesn\'t have num_hidden_layers attribute''' ) def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' pass @unittest.skip('''TimmBackbone initialization is managed on the timm side''' ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip('''TimmBackbone models doesn\'t have inputs_embeds''' ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass @unittest.skip('''TimmBackbone models doesn\'t have inputs_embeds''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' pass @unittest.skip('''TimmBackbone model cannot be created without specifying a backbone checkpoint''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' pass @unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone''' ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' pass @unittest.skip('''model weights aren\'t tied in TimmBackbone.''' ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip('''model weights aren\'t tied in TimmBackbone.''' ) def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' pass @unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone''' ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone''' ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' pass @unittest.skip('''TimmBackbone doesn\'t have hidden size info in its configuration.''' ) def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' pass @unittest.skip('''TimmBackbone doesn\'t support output_attentions.''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' pass @unittest.skip('''Safetensors is not supported by timm.''' ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Tuple =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase : Optional[int] =model_class(snake_case__ ) UpperCAmelCase : str =inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase : Optional[int] =[*signature.parameters.keys()] UpperCAmelCase : Optional[int] =['''pixel_values'''] self.assertListEqual(arg_names[:1] , snake_case__ ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Optional[int] =self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : Optional[Any] =True UpperCAmelCase : Optional[int] =self.has_attentions # no need to test all models as different heads yield the same functionality UpperCAmelCase : List[Any] =self.all_model_classes[0] UpperCAmelCase : Union[str, Any] =model_class(snake_case__ ) model.to(snake_case__ ) UpperCAmelCase : int =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =model(**snake_case__ ) UpperCAmelCase : str =outputs[0][-1] # Encoder-/Decoder-only models UpperCAmelCase : str =outputs.hidden_states[0] hidden_states.retain_grad() if self.has_attentions: UpperCAmelCase : str =outputs.attentions[0] attentions.retain_grad() output.flatten()[0].backward(retain_graph=snake_case__ ) self.assertIsNotNone(hidden_states.grad ) if self.has_attentions: self.assertIsNotNone(attentions.grad ) def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Optional[int] =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase : Optional[Any] =model_class(snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase : List[str] =model(**snake_case__ ) self.assertEqual(len(result.feature_maps ) , len(config.out_indices ) ) self.assertEqual(len(model.channels ) , len(config.out_indices ) ) # Check output of last stage is taken if out_features=None, out_indices=None UpperCAmelCase : int =copy.deepcopy(snake_case__ ) UpperCAmelCase : Tuple =None UpperCAmelCase : List[Any] =model_class(snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase : Any =model(**snake_case__ ) self.assertEqual(len(result.feature_maps ) , 1 ) self.assertEqual(len(model.channels ) , 1 ) # Check backbone can be initialized with fresh weights UpperCAmelCase : int =copy.deepcopy(snake_case__ ) UpperCAmelCase : Optional[int] =False UpperCAmelCase : Union[str, Any] =model_class(snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase : Optional[Any] =model(**snake_case__ )
348
def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Dict =str(bin(__lowerCAmelCase ) ) binary_number += "0" * shift_amount return binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Any =str(bin(__lowerCAmelCase ) )[2:] if shift_amount >= len(__lowerCAmelCase ): return "0b0" UpperCAmelCase : Optional[Any] =binary_number[: len(__lowerCAmelCase ) - shift_amount] return "0b" + shifted_binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number >= 0: # Get binary representation of positive number UpperCAmelCase : Optional[Any] ='''0''' + str(bin(__lowerCAmelCase ) ).strip('''-''' )[2:] else: # Get binary (2's complement) representation of negative number UpperCAmelCase : int =len(bin(__lowerCAmelCase )[3:] ) # Find 2's complement of number UpperCAmelCase : Any =bin(abs(__lowerCAmelCase ) - (1 << binary_number_length) )[3:] UpperCAmelCase : Optional[Any] =( '''1''' + '''0''' * (binary_number_length - len(__lowerCAmelCase )) + binary_number ) if shift_amount >= len(__lowerCAmelCase ): return "0b" + binary_number[0] * len(__lowerCAmelCase ) return ( "0b" + binary_number[0] * shift_amount + binary_number[: len(__lowerCAmelCase ) - shift_amount] ) if __name__ == "__main__": import doctest doctest.testmod()
348
1
__snake_case = [0, 2, 4, 6, 8] __snake_case = [1, 3, 5, 7, 9] def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> int: '''simple docstring''' if remaining_length == 0: if digits[0] == 0 or digits[-1] == 0: return 0 for i in range(length // 2 - 1 , -1 , -1 ): remainder += digits[i] + digits[length - i - 1] if remainder % 2 == 0: return 0 remainder //= 10 return 1 if remaining_length == 1: if remainder % 2 == 0: return 0 UpperCAmelCase : int =0 for digit in range(10 ): UpperCAmelCase : str =digit result += reversible_numbers( 0 , (remainder + 2 * digit) // 10 , __lowerCAmelCase , __lowerCAmelCase ) return result UpperCAmelCase : Union[str, Any] =0 for digita in range(10 ): UpperCAmelCase : List[Any] =digita if (remainder + digita) % 2 == 0: UpperCAmelCase : Optional[int] =ODD_DIGITS else: UpperCAmelCase : Tuple =EVEN_DIGITS for digita in other_parity_digits: UpperCAmelCase : List[Any] =digita result += reversible_numbers( remaining_length - 2 , (remainder + digita + digita) // 10 , __lowerCAmelCase , __lowerCAmelCase , ) return result def lowerCAmelCase_ ( __lowerCAmelCase = 9 )-> int: '''simple docstring''' UpperCAmelCase : List[str] =0 for length in range(1 , max_power + 1 ): result += reversible_numbers(__lowerCAmelCase , 0 , [0] * length , __lowerCAmelCase ) return result if __name__ == "__main__": print(f'{solution() = }')
348
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) # TODO Update this __snake_case = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Tuple = """esm""" def __init__( self , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=1026 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__="absolute" , snake_case__=True , snake_case__=None , snake_case__=False , snake_case__=False , snake_case__=None , snake_case__=None , **snake_case__ , ) -> Union[str, Any]: '''simple docstring''' super().__init__(pad_token_id=snake_case__ , mask_token_id=snake_case__ , **snake_case__ ) UpperCAmelCase : List[str] =vocab_size UpperCAmelCase : str =hidden_size UpperCAmelCase : List[Any] =num_hidden_layers UpperCAmelCase : Optional[Any] =num_attention_heads UpperCAmelCase : str =intermediate_size UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : int =attention_probs_dropout_prob UpperCAmelCase : Dict =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : Union[str, Any] =layer_norm_eps UpperCAmelCase : Dict =position_embedding_type UpperCAmelCase : Optional[Any] =use_cache UpperCAmelCase : int =emb_layer_norm_before UpperCAmelCase : List[str] =token_dropout UpperCAmelCase : Optional[Any] =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) UpperCAmelCase : Optional[Any] =EsmFoldConfig() elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =EsmFoldConfig(**snake_case__ ) UpperCAmelCase : Tuple =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) UpperCAmelCase : Any =get_default_vocab_list() else: UpperCAmelCase : Tuple =vocab_list else: UpperCAmelCase : Optional[int] =None UpperCAmelCase : Union[str, Any] =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , snake_case__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =super().to_dict() if isinstance(self.esmfold_config , snake_case__ ): UpperCAmelCase : str =self.esmfold_config.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : str = None __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : float = 0 __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : int = 128 __lowerCamelCase : "TrunkConfig" = None def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' if self.trunk is None: UpperCAmelCase : str =TrunkConfig() elif isinstance(self.trunk , snake_case__ ): UpperCAmelCase : Optional[int] =TrunkConfig(**self.trunk ) def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[Any] =asdict(self ) UpperCAmelCase : Any =self.trunk.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 48 __lowerCamelCase : int = 1024 __lowerCamelCase : int = 128 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : float = 0 __lowerCamelCase : float = 0 __lowerCamelCase : bool = False __lowerCamelCase : int = 4 __lowerCamelCase : Optional[int] = 128 __lowerCamelCase : "StructureModuleConfig" = None def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' if self.structure_module is None: UpperCAmelCase : Any =StructureModuleConfig() elif isinstance(self.structure_module , snake_case__ ): UpperCAmelCase : str =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f'''`max_recycles` should be positive, got {self.max_recycles}.''' ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f''' {self.sequence_state_dim} and {self.sequence_state_dim}.''' ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f''' {self.pairwise_state_dim} and {self.pairwise_state_dim}.''' ) UpperCAmelCase : Optional[int] =self.sequence_state_dim // self.sequence_head_width UpperCAmelCase : Any =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f''' {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.''' ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f''' {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.''' ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f'''`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.''' ) if self.dropout >= 0.4: raise ValueError(f'''`dropout` should not be greater than 0.4, got {self.dropout}.''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =asdict(self ) UpperCAmelCase : Tuple =self.structure_module.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 384 __lowerCamelCase : int = 128 __lowerCamelCase : int = 16 __lowerCamelCase : int = 128 __lowerCamelCase : int = 12 __lowerCamelCase : int = 4 __lowerCamelCase : int = 8 __lowerCamelCase : float = 0.1 __lowerCamelCase : int = 8 __lowerCamelCase : int = 1 __lowerCamelCase : int = 2 __lowerCamelCase : int = 7 __lowerCamelCase : int = 10 __lowerCamelCase : float = 1E-8 __lowerCamelCase : float = 1E5 def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return asdict(self ) def lowerCAmelCase_ ( )-> Tuple: '''simple docstring''' return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
348
1
from __future__ import annotations def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> set[str]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Dict =set(__lowerCAmelCase ), [start] while stack: UpperCAmelCase : int =stack.pop() explored.add(__lowerCAmelCase ) # Differences from BFS: # 1) pop last element instead of first one # 2) add adjacent elements to stack without exploring them for adj in reversed(graph[v] ): if adj not in explored: stack.append(__lowerCAmelCase ) return explored __snake_case = { '''A''': ['''B''', '''C''', '''D'''], '''B''': ['''A''', '''D''', '''E'''], '''C''': ['''A''', '''F'''], '''D''': ['''B''', '''D'''], '''E''': ['''B''', '''F'''], '''F''': ['''C''', '''E''', '''G'''], '''G''': ['''F'''], } if __name__ == "__main__": import doctest doctest.testmod() print(depth_first_search(G, '''A'''))
348
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[int] = (KDPMaDiscreteScheduler,) __lowerCamelCase : List[str] = 10 def UpperCAmelCase__ ( self , **snake_case__ ) -> str: '''simple docstring''' UpperCAmelCase : int ={ '''num_train_timesteps''': 1100, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', } config.update(**snake_case__ ) return config def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=snake_case__ , beta_end=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' UpperCAmelCase : Optional[Any] =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config(prediction_type='''v_prediction''' ) UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : str =self.dummy_model() UpperCAmelCase : Optional[Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : Union[str, Any] =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : str =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Any =model(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : int =output.prev_sample UpperCAmelCase : Dict =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Optional[Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.69_34e-07 ) < 1e-2 assert abs(result_mean.item() - 6.11_12e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.6_93_42_86_50_17_09_72e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0002 ) < 1e-3 def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : Any =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config() UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : Optional[int] =self.dummy_model() UpperCAmelCase : Union[str, Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : str =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : Dict =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =model(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =output.prev_sample UpperCAmelCase : Any =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Union[str, Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : List[Any] =self.scheduler_classes[0] UpperCAmelCase : Dict =self.get_scheduler_config() UpperCAmelCase : List[str] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps , device=snake_case__ ) UpperCAmelCase : int =self.dummy_model() UpperCAmelCase : Tuple =self.dummy_sample_deter.to(snake_case__ ) * scheduler.init_noise_sigma for t in scheduler.timesteps: UpperCAmelCase : Optional[Any] =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : int =model(snake_case__ , snake_case__ ) UpperCAmelCase : str =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =output.prev_sample UpperCAmelCase : List[str] =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Dict =torch.mean(torch.abs(snake_case__ ) ) if str(snake_case__ ).startswith('''cpu''' ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3
348
1
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __snake_case : def __init__( self , snake_case__ , snake_case__=12 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , snake_case__=0 , snake_case__=None , ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : List[Any] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Union[str, Any] =use_input_mask UpperCAmelCase : Tuple =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : Tuple =hidden_size UpperCAmelCase : Dict =projection_dim UpperCAmelCase : Optional[int] =num_hidden_layers UpperCAmelCase : Dict =num_attention_heads UpperCAmelCase : int =intermediate_size UpperCAmelCase : Any =dropout UpperCAmelCase : Union[str, Any] =attention_dropout UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : str =scope UpperCAmelCase : str =bos_token_id def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : int =None if self.use_input_mask: UpperCAmelCase : Union[str, Any] =random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: UpperCAmelCase : Optional[int] =input_mask.numpy() UpperCAmelCase , UpperCAmelCase : List[Any] =input_mask.shape UpperCAmelCase : Optional[Any] =np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : List[Any] =1 UpperCAmelCase : Tuple =0 UpperCAmelCase : List[Any] =self.get_config() return config, input_ids, tf.convert_to_tensor(snake_case__ ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =TFBlipTextModel(config=snake_case__ ) UpperCAmelCase : List[Any] =model(snake_case__ , attention_mask=snake_case__ , training=snake_case__ ) UpperCAmelCase : str =model(snake_case__ , training=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[Any] =config_and_inputs UpperCAmelCase : Optional[int] ={'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __snake_case ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Optional[int] = (TFBlipTextModel,) if is_tf_available() else () __lowerCamelCase : Dict = False __lowerCamelCase : Optional[Any] = False __lowerCamelCase : Dict = False def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : str =BlipTextModelTester(self ) UpperCAmelCase : Optional[int] =ConfigTester(self , config_class=snake_case__ , hidden_size=37 ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip(reason='''Blip does not use inputs_embeds''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @slow def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase : Optional[Any] =TFBlipTextModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def UpperCAmelCase__ ( self , snake_case__=True ) -> Any: '''simple docstring''' super().test_pt_tf_model_equivalence(allow_missing_keys=snake_case__ )
348
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class __snake_case ( unittest.TestCase ): @slow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Any =FlaxMTaForConditionalGeneration.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : Tuple =AutoTokenizer.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : List[str] =tokenizer('''Hello there''' , return_tensors='''np''' ).input_ids UpperCAmelCase : List[Any] =tokenizer('''Hi I am''' , return_tensors='''np''' ).input_ids UpperCAmelCase : Union[str, Any] =shift_tokens_right(snake_case__ , model.config.pad_token_id , model.config.decoder_start_token_id ) UpperCAmelCase : List[str] =model(snake_case__ , decoder_input_ids=snake_case__ ).logits UpperCAmelCase : Any =optax.softmax_cross_entropy(snake_case__ , onehot(snake_case__ , logits.shape[-1] ) ).mean() UpperCAmelCase : Union[str, Any] =-(labels.shape[-1] * loss.item()) UpperCAmelCase : List[str] =-84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
348
1
import argparse import os import pickle import sys import torch from transformers import TransfoXLConfig, TransfoXLLMHeadModel, load_tf_weights_in_transfo_xl from transformers.models.transfo_xl import tokenization_transfo_xl as data_utils from transformers.models.transfo_xl.tokenization_transfo_xl import CORPUS_NAME, VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() # We do this to be able to load python 2 datasets pickles # See e.g. https://stackoverflow.com/questions/2121874/python-pickling-after-changing-a-modules-directory/2121918#2121918 __snake_case = data_utils.TransfoXLTokenizer __snake_case = data_utils.TransfoXLCorpus __snake_case = data_utils __snake_case = data_utils def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> List[str]: '''simple docstring''' if transfo_xl_dataset_file: # Convert a pre-processed corpus (see original TensorFlow repo) with open(__lowerCAmelCase , '''rb''' ) as fp: UpperCAmelCase : Optional[int] =pickle.load(__lowerCAmelCase , encoding='''latin1''' ) # Save vocabulary and dataset cache as Dictionaries (should be better than pickles for the long-term) UpperCAmelCase : str =pytorch_dump_folder_path + '''/''' + VOCAB_FILES_NAMES['''pretrained_vocab_file'''] print(f'''Save vocabulary to {pytorch_vocab_dump_path}''' ) UpperCAmelCase : int =corpus.vocab.__dict__ torch.save(__lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase : Any =corpus.__dict__ corpus_dict_no_vocab.pop('''vocab''' , __lowerCAmelCase ) UpperCAmelCase : int =pytorch_dump_folder_path + '''/''' + CORPUS_NAME print(f'''Save dataset to {pytorch_dataset_dump_path}''' ) torch.save(__lowerCAmelCase , __lowerCAmelCase ) if tf_checkpoint_path: # Convert a pre-trained TensorFlow model UpperCAmelCase : List[Any] =os.path.abspath(__lowerCAmelCase ) UpperCAmelCase : int =os.path.abspath(__lowerCAmelCase ) print(f'''Converting Transformer XL checkpoint from {tf_path} with config at {config_path}.''' ) # Initialise PyTorch model if transfo_xl_config_file == "": UpperCAmelCase : int =TransfoXLConfig() else: UpperCAmelCase : Dict =TransfoXLConfig.from_json_file(__lowerCAmelCase ) print(f'''Building PyTorch model from configuration: {config}''' ) UpperCAmelCase : Dict =TransfoXLLMHeadModel(__lowerCAmelCase ) UpperCAmelCase : Union[str, Any] =load_tf_weights_in_transfo_xl(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) # Save pytorch-model UpperCAmelCase : Dict =os.path.join(__lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase : Tuple =os.path.join(__lowerCAmelCase , __lowerCAmelCase ) print(f'''Save PyTorch model to {os.path.abspath(__lowerCAmelCase )}''' ) torch.save(model.state_dict() , __lowerCAmelCase ) print(f'''Save configuration file to {os.path.abspath(__lowerCAmelCase )}''' ) with open(__lowerCAmelCase , '''w''' , encoding='''utf-8''' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": __snake_case = argparse.ArgumentParser() parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the folder to store the PyTorch model or dataset/vocab.''', ) parser.add_argument( '''--tf_checkpoint_path''', default='''''', type=str, help='''An optional path to a TensorFlow checkpoint path to be converted.''', ) parser.add_argument( '''--transfo_xl_config_file''', default='''''', type=str, help=( '''An optional config json file corresponding to the pre-trained BERT model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--transfo_xl_dataset_file''', default='''''', type=str, help='''An optional dataset file to be converted in a vocabulary.''', ) __snake_case = parser.parse_args() convert_transfo_xl_checkpoint_to_pytorch( args.tf_checkpoint_path, args.transfo_xl_config_file, args.pytorch_dump_folder_path, args.transfo_xl_dataset_file, )
348
import unittest import numpy as np from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __snake_case ( lowerCamelCase__ , unittest.TestCase ): # FIXME: add fast tests pass @nightly @require_onnxruntime @require_torch_gpu class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : List[Any] =ort.SessionOptions() UpperCAmelCase : Optional[int] =False return options def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : int =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Optional[Any] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Dict ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : Any =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=10 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : Optional[int] =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Tuple =np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Tuple =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , subfolder='''scheduler''' , revision='''onnx''' ) UpperCAmelCase : int =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , scheduler=snake_case__ , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Union[str, Any] ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : str =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=20 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : int =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Union[str, Any] =np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3
348
1
from datasets.utils.patching import _PatchedModuleObj, patch_submodule from . import _test_patching def lowerCAmelCase_ ( )-> Dict: '''simple docstring''' import os as original_os from os import path as original_path from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join UpperCAmelCase : Any ='''__test_patch_submodule_mock__''' with patch_submodule(_test_patching , '''os.path.join''' , __lowerCAmelCase ): # Every way to access os.path.join must be patched, and the rest must stay untouched # check os.path.join assert isinstance(_test_patching.os , _PatchedModuleObj ) assert isinstance(_test_patching.os.path , _PatchedModuleObj ) assert _test_patching.os.path.join is mock # check path.join assert isinstance(_test_patching.path , _PatchedModuleObj ) assert _test_patching.path.join is mock # check join assert _test_patching.join is mock # check that the other attributes are untouched assert _test_patching.os.rename is original_rename assert _test_patching.path.dirname is original_dirname assert _test_patching.os.path.dirname is original_dirname # Even renamed modules or objects must be patched # check renamed_os.path.join assert isinstance(_test_patching.renamed_os , _PatchedModuleObj ) assert isinstance(_test_patching.renamed_os.path , _PatchedModuleObj ) assert _test_patching.renamed_os.path.join is mock # check renamed_path.join assert isinstance(_test_patching.renamed_path , _PatchedModuleObj ) assert _test_patching.renamed_path.join is mock # check renamed_join assert _test_patching.renamed_join is mock # check that the other attributes are untouched assert _test_patching.renamed_os.rename is original_rename assert _test_patching.renamed_path.dirname is original_dirname assert _test_patching.renamed_os.path.dirname is original_dirname # check that everthing is back to normal when the patch is over assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join def lowerCAmelCase_ ( )-> Any: '''simple docstring''' assert _test_patching.open is open UpperCAmelCase : Optional[Any] ='''__test_patch_submodule_builtin_mock__''' # _test_patching has "open" in its globals assert _test_patching.open is open with patch_submodule(_test_patching , '''open''' , __lowerCAmelCase ): assert _test_patching.open is mock # check that everthing is back to normal when the patch is over assert _test_patching.open is open def lowerCAmelCase_ ( )-> Dict: '''simple docstring''' UpperCAmelCase : Dict ='''__test_patch_submodule_missing_mock__''' with patch_submodule(_test_patching , '''pandas.read_csv''' , __lowerCAmelCase ): pass def lowerCAmelCase_ ( )-> str: '''simple docstring''' UpperCAmelCase : Tuple ='''__test_patch_submodule_missing_builtin_mock__''' # _test_patching doesn't have "len" in its globals assert getattr(_test_patching , '''len''' , __lowerCAmelCase ) is None with patch_submodule(_test_patching , '''len''' , __lowerCAmelCase ): assert _test_patching.len is mock assert _test_patching.len is len def lowerCAmelCase_ ( )-> str: '''simple docstring''' UpperCAmelCase : Optional[int] ='''__test_patch_submodule_start_and_stop_mock__''' UpperCAmelCase : str =patch_submodule(_test_patching , '''open''' , __lowerCAmelCase ) assert _test_patching.open is open patch.start() assert _test_patching.open is mock patch.stop() assert _test_patching.open is open def lowerCAmelCase_ ( )-> List[str]: '''simple docstring''' from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join UpperCAmelCase : Tuple ='''__test_patch_submodule_successive_join__''' UpperCAmelCase : str ='''__test_patch_submodule_successive_dirname__''' UpperCAmelCase : List[str] ='''__test_patch_submodule_successive_rename__''' assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename with patch_submodule(_test_patching , '''os.path.join''' , __lowerCAmelCase ): with patch_submodule(_test_patching , '''os.rename''' , __lowerCAmelCase ): with patch_submodule(_test_patching , '''os.path.dirname''' , __lowerCAmelCase ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename # try another order with patch_submodule(_test_patching , '''os.rename''' , __lowerCAmelCase ): with patch_submodule(_test_patching , '''os.path.join''' , __lowerCAmelCase ): with patch_submodule(_test_patching , '''os.path.dirname''' , __lowerCAmelCase ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename def lowerCAmelCase_ ( )-> Optional[Any]: '''simple docstring''' UpperCAmelCase : int ='''__test_patch_submodule_doesnt_exist_mock__''' with patch_submodule(_test_patching , '''__module_that_doesn_exist__.__attribute_that_doesn_exist__''' , __lowerCAmelCase ): pass with patch_submodule(_test_patching , '''os.__attribute_that_doesn_exist__''' , __lowerCAmelCase ): pass
348
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def lowerCAmelCase_ ( )-> int: '''simple docstring''' UpperCAmelCase : str ={ '''repo_name''': ['''test_repo1''', '''test_repo2''', '''test_repo3'''], '''path''': ['''test_1.py''', '''test_2.py''', '''unit_test.py'''], '''content''': ['''a ''' * 20, '''a ''' * 30, '''b ''' * 7], } UpperCAmelCase : Union[str, Any] =Dataset.from_dict(__lowerCAmelCase ) return dataset class __snake_case ( lowerCamelCase__ ): def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[str] =get_dataset() UpperCAmelCase : Optional[int] =make_duplicate_clusters(snake_case__ , 0.85 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : str =get_dataset() UpperCAmelCase , UpperCAmelCase : Tuple =deduplicate_dataset(snake_case__ ) self.assertEqual(len(snake_case__ ) , 2 ) print(snake_case__ ) self.assertEqual(duplicate_clusters[0][0]['''copies'''] , 2 ) self.assertEqual(duplicate_clusters[0][0]['''is_extreme'''] , snake_case__ )
348
1
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, convert_to_rgb, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging __snake_case = logging.get_logger(__name__) if is_vision_available(): import PIL class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[Any] = ["""pixel_values"""] def __init__( self , snake_case__ = True , snake_case__ = None , snake_case__ = PILImageResampling.BICUBIC , snake_case__ = True , snake_case__ = None , snake_case__ = True , snake_case__ = 1 / 255 , snake_case__ = True , snake_case__ = None , snake_case__ = None , snake_case__ = True , **snake_case__ , ) -> None: '''simple docstring''' super().__init__(**snake_case__ ) UpperCAmelCase : List[Any] =size if size is not None else {'''shortest_edge''': 224} UpperCAmelCase : Tuple =get_size_dict(snake_case__ , default_to_square=snake_case__ ) UpperCAmelCase : Any =crop_size if crop_size is not None else {'''height''': 224, '''width''': 224} UpperCAmelCase : Optional[Any] =get_size_dict(snake_case__ , default_to_square=snake_case__ , param_name='''crop_size''' ) UpperCAmelCase : Tuple =do_resize UpperCAmelCase : Optional[int] =size UpperCAmelCase : Dict =resample UpperCAmelCase : str =do_center_crop UpperCAmelCase : Union[str, Any] =crop_size UpperCAmelCase : Optional[int] =do_rescale UpperCAmelCase : List[Any] =rescale_factor UpperCAmelCase : Any =do_normalize UpperCAmelCase : Union[str, Any] =image_mean if image_mean is not None else OPENAI_CLIP_MEAN UpperCAmelCase : Any =image_std if image_std is not None else OPENAI_CLIP_STD UpperCAmelCase : Optional[int] =do_convert_rgb def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ = PILImageResampling.BICUBIC , snake_case__ = None , **snake_case__ , ) -> np.ndarray: '''simple docstring''' UpperCAmelCase : str =get_size_dict(snake_case__ , default_to_square=snake_case__ ) if "shortest_edge" not in size: raise ValueError(f'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}''' ) UpperCAmelCase : Optional[Any] =get_resize_output_image_size(snake_case__ , size=size['''shortest_edge'''] , default_to_square=snake_case__ ) return resize(snake_case__ , size=snake_case__ , resample=snake_case__ , data_format=snake_case__ , **snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ = None , **snake_case__ , ) -> np.ndarray: '''simple docstring''' UpperCAmelCase : List[Any] =get_size_dict(snake_case__ ) 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(snake_case__ , size=(size['''height'''], size['''width''']) , data_format=snake_case__ , **snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ = None , **snake_case__ , ) -> int: '''simple docstring''' return rescale(snake_case__ , scale=snake_case__ , data_format=snake_case__ , **snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ = None , **snake_case__ , ) -> np.ndarray: '''simple docstring''' return normalize(snake_case__ , mean=snake_case__ , std=snake_case__ , data_format=snake_case__ , **snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = ChannelDimension.FIRST , **snake_case__ , ) -> PIL.Image.Image: '''simple docstring''' UpperCAmelCase : str =do_resize if do_resize is not None else self.do_resize UpperCAmelCase : Optional[int] =size if size is not None else self.size UpperCAmelCase : int =get_size_dict(snake_case__ , param_name='''size''' , default_to_square=snake_case__ ) UpperCAmelCase : List[str] =resample if resample is not None else self.resample UpperCAmelCase : List[Any] =do_center_crop if do_center_crop is not None else self.do_center_crop UpperCAmelCase : Tuple =crop_size if crop_size is not None else self.crop_size UpperCAmelCase : Tuple =get_size_dict(snake_case__ , param_name='''crop_size''' , default_to_square=snake_case__ ) UpperCAmelCase : Optional[Any] =do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase : Optional[Any] =rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase : Any =do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase : int =image_mean if image_mean is not None else self.image_mean UpperCAmelCase : Tuple =image_std if image_std is not None else self.image_std UpperCAmelCase : str =do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb UpperCAmelCase : List[Any] =make_list_of_images(snake_case__ ) if not valid_images(snake_case__ ): 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.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # PIL RGBA images are converted to RGB if do_convert_rgb: UpperCAmelCase : Dict =[convert_to_rgb(snake_case__ ) for image in images] # All transformations expect numpy arrays. UpperCAmelCase : Union[str, Any] =[to_numpy_array(snake_case__ ) for image in images] if do_resize: UpperCAmelCase : Dict =[self.resize(image=snake_case__ , size=snake_case__ , resample=snake_case__ ) for image in images] if do_center_crop: UpperCAmelCase : int =[self.center_crop(image=snake_case__ , size=snake_case__ ) for image in images] if do_rescale: UpperCAmelCase : Tuple =[self.rescale(image=snake_case__ , scale=snake_case__ ) for image in images] if do_normalize: UpperCAmelCase : Optional[int] =[self.normalize(image=snake_case__ , mean=snake_case__ , std=snake_case__ ) for image in images] UpperCAmelCase : Dict =[to_channel_dimension_format(snake_case__ , snake_case__ ) for image in images] UpperCAmelCase : Dict ={'''pixel_values''': images} return BatchFeature(data=snake_case__ , tensor_type=snake_case__ )
348
from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput __snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name class __snake_case ( lowerCamelCase__ , lowerCamelCase__ ): @register_to_config def __init__( self , snake_case__ , snake_case__ = None , snake_case__ = None ) -> str: '''simple docstring''' super().__init__() UpperCAmelCase : Optional[Any] =learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" UpperCAmelCase : Any =torch.zeros(snake_case__ , snake_case__ ) else: UpperCAmelCase : Union[str, Any] =None UpperCAmelCase : Optional[int] =torch.nn.Parameter(snake_case__ ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : VQModel __lowerCamelCase : CLIPTextModel __lowerCamelCase : CLIPTokenizer __lowerCamelCase : TransformeraDModel __lowerCamelCase : LearnedClassifierFreeSamplingEmbeddings __lowerCamelCase : VQDiffusionScheduler def __init__( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> int: '''simple docstring''' super().__init__() self.register_modules( vqvae=snake_case__ , transformer=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , scheduler=snake_case__ , learned_classifier_free_sampling_embeddings=snake_case__ , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : int =len(snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else 1 # get prompt text embeddings UpperCAmelCase : Optional[int] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) UpperCAmelCase : int =text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCAmelCase : List[str] =self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' f''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) UpperCAmelCase : Optional[Any] =text_input_ids[:, : self.tokenizer.model_max_length] UpperCAmelCase : List[Any] =self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 UpperCAmelCase : int =prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate text embeddings for each generation per prompt UpperCAmelCase : int =prompt_embeds.repeat_interleave(snake_case__ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: UpperCAmelCase : Optional[int] =self.learned_classifier_free_sampling_embeddings.embeddings UpperCAmelCase : str =negative_prompt_embeds.unsqueeze(0 ).repeat(snake_case__ , 1 , 1 ) else: UpperCAmelCase : str =[''''''] * batch_size UpperCAmelCase : Tuple =text_input_ids.shape[-1] UpperCAmelCase : Optional[Any] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=snake_case__ , truncation=snake_case__ , return_tensors='''pt''' , ) UpperCAmelCase : Optional[Any] =self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings UpperCAmelCase : Optional[int] =negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCAmelCase : Optional[Any] =negative_prompt_embeds.shape[1] UpperCAmelCase : Union[str, Any] =negative_prompt_embeds.repeat(1 , snake_case__ , 1 ) UpperCAmelCase : Optional[Any] =negative_prompt_embeds.view(batch_size * num_images_per_prompt , snake_case__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCAmelCase : int =torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , snake_case__ , snake_case__ = 100 , snake_case__ = 5.0 , snake_case__ = 1.0 , snake_case__ = 1 , snake_case__ = None , snake_case__ = None , snake_case__ = "pil" , snake_case__ = True , snake_case__ = None , snake_case__ = 1 , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =1 elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Tuple =len(snake_case__ ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(snake_case__ )}''' ) UpperCAmelCase : Tuple =batch_size * num_images_per_prompt UpperCAmelCase : List[str] =guidance_scale > 1.0 UpperCAmelCase : List[Any] =self._encode_prompt(snake_case__ , snake_case__ , snake_case__ ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case__ , snake_case__ ) or callback_steps <= 0) ): raise ValueError( f'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' f''' {type(snake_case__ )}.''' ) # get the initial completely masked latents unless the user supplied it UpperCAmelCase : int =(batch_size, self.transformer.num_latent_pixels) if latents is None: UpperCAmelCase : Union[str, Any] =self.transformer.num_vector_embeds - 1 UpperCAmelCase : str =torch.full(snake_case__ , snake_case__ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( '''Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,''' f''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) UpperCAmelCase : Any =latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(snake_case__ , device=self.device ) UpperCAmelCase : Any =self.scheduler.timesteps.to(self.device ) UpperCAmelCase : Optional[int] =latents for i, t in enumerate(self.progress_bar(snake_case__ ) ): # expand the sample if we are doing classifier free guidance UpperCAmelCase : Optional[Any] =torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` UpperCAmelCase : Optional[int] =self.transformer(snake_case__ , encoder_hidden_states=snake_case__ , timestep=snake_case__ ).sample if do_classifier_free_guidance: UpperCAmelCase , UpperCAmelCase : str =model_output.chunk(2 ) UpperCAmelCase : Optional[int] =model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(snake_case__ , dim=1 , keepdim=snake_case__ ) UpperCAmelCase : Tuple =self.truncate(snake_case__ , snake_case__ ) # remove `log(0)`'s (`-inf`s) UpperCAmelCase : Optional[Any] =model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 UpperCAmelCase : int =self.scheduler.step(snake_case__ , timestep=snake_case__ , sample=snake_case__ , generator=snake_case__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =self.vqvae.config.vq_embed_dim UpperCAmelCase : Optional[Any] =(batch_size, self.transformer.height, self.transformer.width, embedding_channels) UpperCAmelCase : Dict =self.vqvae.quantize.get_codebook_entry(snake_case__ , shape=snake_case__ ) UpperCAmelCase : Tuple =self.vqvae.decode(snake_case__ , force_not_quantize=snake_case__ ).sample UpperCAmelCase : Union[str, Any] =(image / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase : Any =image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase : List[str] =self.numpy_to_pil(snake_case__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> torch.FloatTensor: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : int =torch.sort(snake_case__ , 1 , descending=snake_case__ ) UpperCAmelCase : Union[str, Any] =torch.exp(snake_case__ ) UpperCAmelCase : Union[str, Any] =sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out UpperCAmelCase : Optional[Any] =torch.full_like(keep_mask[:, 0:1, :] , snake_case__ ) UpperCAmelCase : Tuple =torch.cat((all_true, keep_mask) , dim=1 ) UpperCAmelCase : int =keep_mask[:, :-1, :] UpperCAmelCase : int =keep_mask.gather(1 , indices.argsort(1 ) ) UpperCAmelCase : Dict =log_p_x_0.clone() UpperCAmelCase : List[Any] =-torch.inf # -inf = log(0) return rv
348
1
import json import os import shutil import warnings from argparse import ArgumentParser, Namespace from pathlib import Path from typing import List from ..utils import logging from . import BaseTransformersCLICommand try: from cookiecutter.main import cookiecutter __snake_case = True except ImportError: __snake_case = False __snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name def lowerCAmelCase_ ( __lowerCAmelCase )-> Optional[Any]: '''simple docstring''' return AddNewModelCommand(args.testing , args.testing_file , path=args.path ) class __snake_case ( lowerCamelCase__ ): @staticmethod def UpperCAmelCase__ ( snake_case__ ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : str =parser.add_parser('''add-new-model''' ) add_new_model_parser.add_argument('''--testing''' , action='''store_true''' , help='''If in testing mode.''' ) add_new_model_parser.add_argument('''--testing_file''' , type=snake_case__ , help='''Configuration file on which to run.''' ) add_new_model_parser.add_argument( '''--path''' , type=snake_case__ , help='''Path to cookiecutter. Should only be used for testing purposes.''' ) add_new_model_parser.set_defaults(func=snake_case__ ) def __init__( self , snake_case__ , snake_case__ , snake_case__=None , *snake_case__ ) -> Dict: '''simple docstring''' UpperCAmelCase : Dict =testing UpperCAmelCase : List[Any] =testing_file UpperCAmelCase : str =path def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' warnings.warn( '''The command `transformers-cli add-new-model` is deprecated and will be removed in v5 of Transformers. ''' '''It is not actively maintained anymore, so might give a result that won\'t pass all tests and quality ''' '''checks, you should use `transformers-cli add-new-model-like` instead.''' ) if not _has_cookiecutter: raise ImportError( '''Model creation dependencies are required to use the `add_new_model` command. Install them by running ''' '''the following at the root of your `transformers` clone:\n\n\t$ pip install -e .[modelcreation]\n''' ) # Ensure that there is no other `cookiecutter-template-xxx` directory in the current working directory UpperCAmelCase : Dict =[directory for directory in os.listdir() if '''cookiecutter-template-''' == directory[:22]] if len(snake_case__ ) > 0: raise ValueError( '''Several directories starting with `cookiecutter-template-` in current working directory. ''' '''Please clean your directory by removing all folders starting with `cookiecutter-template-` or ''' '''change your working directory.''' ) UpperCAmelCase : Tuple =( Path(snake_case__ ).parent.parent.parent.parent if self._path is None else Path(self._path ).parent.parent ) UpperCAmelCase : Tuple =path_to_transformer_root / '''templates''' / '''adding_a_new_model''' # Execute cookiecutter if not self._testing: cookiecutter(str(snake_case__ ) ) else: with open(self._testing_file , '''r''' ) as configuration_file: UpperCAmelCase : Tuple =json.load(snake_case__ ) cookiecutter( str(path_to_cookiecutter if self._path is None else self._path ) , no_input=snake_case__ , extra_context=snake_case__ , ) UpperCAmelCase : List[Any] =[directory for directory in os.listdir() if '''cookiecutter-template-''' in directory[:22]][0] # Retrieve configuration with open(directory + '''/configuration.json''' , '''r''' ) as configuration_file: UpperCAmelCase : Any =json.load(snake_case__ ) UpperCAmelCase : Tuple =configuration['''lowercase_modelname'''] UpperCAmelCase : Optional[int] =configuration['''generate_tensorflow_pytorch_and_flax'''] os.remove(f'''{directory}/configuration.json''' ) UpperCAmelCase : Any ='''PyTorch''' in generate_tensorflow_pytorch_and_flax UpperCAmelCase : str ='''TensorFlow''' in generate_tensorflow_pytorch_and_flax UpperCAmelCase : Optional[Any] ='''Flax''' in generate_tensorflow_pytorch_and_flax UpperCAmelCase : str =f'''{path_to_transformer_root}/src/transformers/models/{lowercase_model_name}''' os.makedirs(snake_case__ , exist_ok=snake_case__ ) os.makedirs(f'''{path_to_transformer_root}/tests/models/{lowercase_model_name}''' , exist_ok=snake_case__ ) # Tests require submodules as they have parent imports with open(f'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/__init__.py''' , '''w''' ): pass shutil.move( f'''{directory}/__init__.py''' , f'''{model_dir}/__init__.py''' , ) shutil.move( f'''{directory}/configuration_{lowercase_model_name}.py''' , f'''{model_dir}/configuration_{lowercase_model_name}.py''' , ) def remove_copy_lines(snake_case__ ): with open(snake_case__ , '''r''' ) as f: UpperCAmelCase : Tuple =f.readlines() with open(snake_case__ , '''w''' ) as f: for line in lines: if "# Copied from transformers." not in line: f.write(snake_case__ ) if output_pytorch: if not self._testing: remove_copy_lines(f'''{directory}/modeling_{lowercase_model_name}.py''' ) shutil.move( f'''{directory}/modeling_{lowercase_model_name}.py''' , f'''{model_dir}/modeling_{lowercase_model_name}.py''' , ) shutil.move( f'''{directory}/test_modeling_{lowercase_model_name}.py''' , f'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_{lowercase_model_name}.py''' , ) else: os.remove(f'''{directory}/modeling_{lowercase_model_name}.py''' ) os.remove(f'''{directory}/test_modeling_{lowercase_model_name}.py''' ) if output_tensorflow: if not self._testing: remove_copy_lines(f'''{directory}/modeling_tf_{lowercase_model_name}.py''' ) shutil.move( f'''{directory}/modeling_tf_{lowercase_model_name}.py''' , f'''{model_dir}/modeling_tf_{lowercase_model_name}.py''' , ) shutil.move( f'''{directory}/test_modeling_tf_{lowercase_model_name}.py''' , f'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_tf_{lowercase_model_name}.py''' , ) else: os.remove(f'''{directory}/modeling_tf_{lowercase_model_name}.py''' ) os.remove(f'''{directory}/test_modeling_tf_{lowercase_model_name}.py''' ) if output_flax: if not self._testing: remove_copy_lines(f'''{directory}/modeling_flax_{lowercase_model_name}.py''' ) shutil.move( f'''{directory}/modeling_flax_{lowercase_model_name}.py''' , f'''{model_dir}/modeling_flax_{lowercase_model_name}.py''' , ) shutil.move( f'''{directory}/test_modeling_flax_{lowercase_model_name}.py''' , f'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_flax_{lowercase_model_name}.py''' , ) else: os.remove(f'''{directory}/modeling_flax_{lowercase_model_name}.py''' ) os.remove(f'''{directory}/test_modeling_flax_{lowercase_model_name}.py''' ) shutil.move( f'''{directory}/{lowercase_model_name}.md''' , f'''{path_to_transformer_root}/docs/source/en/model_doc/{lowercase_model_name}.md''' , ) shutil.move( f'''{directory}/tokenization_{lowercase_model_name}.py''' , f'''{model_dir}/tokenization_{lowercase_model_name}.py''' , ) shutil.move( f'''{directory}/tokenization_fast_{lowercase_model_name}.py''' , f'''{model_dir}/tokenization_{lowercase_model_name}_fast.py''' , ) from os import fdopen, remove from shutil import copymode, move from tempfile import mkstemp def replace(snake_case__ , snake_case__ , snake_case__ ): # Create temp file UpperCAmelCase , UpperCAmelCase : Optional[Any] =mkstemp() UpperCAmelCase : Dict =False with fdopen(snake_case__ , '''w''' ) as new_file: with open(snake_case__ ) as old_file: for line in old_file: new_file.write(snake_case__ ) if line_to_copy_below in line: UpperCAmelCase : List[str] =True for line_to_copy in lines_to_copy: new_file.write(snake_case__ ) if not line_found: raise ValueError(f'''Line {line_to_copy_below} was not found in file.''' ) # Copy the file permissions from the old file to the new file copymode(snake_case__ , snake_case__ ) # Remove original file remove(snake_case__ ) # Move new file move(snake_case__ , snake_case__ ) def skip_units(snake_case__ ): return ( ("generating PyTorch" in line and not output_pytorch) or ("generating TensorFlow" in line and not output_tensorflow) or ("generating Flax" in line and not output_flax) ) def replace_in_files(snake_case__ ): with open(snake_case__ ) as datafile: UpperCAmelCase : List[Any] =[] UpperCAmelCase : List[Any] =False UpperCAmelCase : List[str] =False for line in datafile: if "# To replace in: " in line and "##" not in line: UpperCAmelCase : Union[str, Any] =line.split('''"''' )[1] UpperCAmelCase : Any =skip_units(snake_case__ ) elif "# Below: " in line and "##" not in line: UpperCAmelCase : List[Any] =line.split('''"''' )[1] UpperCAmelCase : Optional[int] =skip_units(snake_case__ ) elif "# End." in line and "##" not in line: if not skip_file and not skip_snippet: replace(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[Any] =[] elif "# Replace with" in line and "##" not in line: UpperCAmelCase : List[str] =[] elif "##" not in line: lines_to_copy.append(snake_case__ ) remove(snake_case__ ) replace_in_files(f'''{directory}/to_replace_{lowercase_model_name}.py''' ) os.rmdir(snake_case__ )
348
import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase : Any =UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =self.dummy_uncond_unet UpperCAmelCase : Optional[int] =KarrasVeScheduler() UpperCAmelCase : List[Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : List[str] =torch.manual_seed(0 ) UpperCAmelCase : List[str] =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : str =torch.manual_seed(0 ) UpperCAmelCase : str =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' , return_dict=snake_case__ )[0] UpperCAmelCase : Any =image[0, -3:, -3:, -1] UpperCAmelCase : List[str] =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCAmelCase : int =np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class __snake_case ( unittest.TestCase ): def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Tuple ='''google/ncsnpp-celebahq-256''' UpperCAmelCase : int =UNetaDModel.from_pretrained(snake_case__ ) UpperCAmelCase : Dict =KarrasVeScheduler() UpperCAmelCase : Union[str, Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Any =torch.manual_seed(0 ) UpperCAmelCase : Tuple =pipe(num_inference_steps=20 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : Optional[int] =image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) UpperCAmelCase : Tuple =np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
348
1
def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' UpperCAmelCase : Union[str, Any] ='''''' for word_or_phrase in separated: if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): raise Exception('''join() accepts only strings to be joined''' ) joined += word_or_phrase + separator return joined.strip(__lowerCAmelCase ) if __name__ == "__main__": from doctest import testmod testmod()
348
import qiskit def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> qiskit.result.counts.Counts: '''simple docstring''' UpperCAmelCase : Union[str, Any] =qiskit.Aer.get_backend('''aer_simulator''' ) UpperCAmelCase : List[str] =qiskit.QuantumCircuit(4 , 2 ) # encode inputs in qubits 0 and 1 if bita == 1: qc_ha.x(0 ) if bita == 1: qc_ha.x(1 ) qc_ha.barrier() # use cnots to write XOR of the inputs on qubit2 qc_ha.cx(0 , 2 ) qc_ha.cx(1 , 2 ) # use ccx / toffoli gate to write AND of the inputs on qubit3 qc_ha.ccx(0 , 1 , 3 ) qc_ha.barrier() # extract outputs qc_ha.measure(2 , 0 ) # extract XOR value qc_ha.measure(3 , 1 ) # extract AND value # Execute the circuit on the qasm simulator UpperCAmelCase : Dict =qiskit.execute(__lowerCAmelCase , __lowerCAmelCase , shots=10_00 ) # Return the histogram data of the results of the experiment return job.result().get_counts(__lowerCAmelCase ) if __name__ == "__main__": __snake_case = half_adder(1, 1) print(f'Half Adder Output Qubit Counts: {counts}')
348
1
import numpy as np from PIL import Image def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> np.ndarray: '''simple docstring''' UpperCAmelCase : Any =np.array(__lowerCAmelCase ) if arr.shape[0] != arr.shape[1]: raise ValueError('''The input array is not a square matrix''' ) UpperCAmelCase : Union[str, Any] =0 UpperCAmelCase : int =0 UpperCAmelCase : Optional[Any] =0 UpperCAmelCase : Optional[Any] =0 # compute the shape of the output matrix UpperCAmelCase : Optional[Any] =(arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape maxpool_shape UpperCAmelCase : List[str] =np.zeros((maxpool_shape, maxpool_shape) ) while i < arr.shape[0]: if i + size > arr.shape[0]: # if the end of the matrix is reached, break break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the maximum of the pooling matrix UpperCAmelCase : int =np.max(arr[i : i + size, j : j + size] ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 UpperCAmelCase : Any =0 UpperCAmelCase : Dict =0 return updated_arr def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> np.ndarray: '''simple docstring''' UpperCAmelCase : Optional[int] =np.array(__lowerCAmelCase ) if arr.shape[0] != arr.shape[1]: raise ValueError('''The input array is not a square matrix''' ) UpperCAmelCase : List[str] =0 UpperCAmelCase : Optional[Any] =0 UpperCAmelCase : List[Any] =0 UpperCAmelCase : Dict =0 # compute the shape of the output matrix UpperCAmelCase : Any =(arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape avgpool_shape UpperCAmelCase : List[Any] =np.zeros((avgpool_shape, avgpool_shape) ) while i < arr.shape[0]: # if the end of the matrix is reached, break if i + size > arr.shape[0]: break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the average of the pooling matrix UpperCAmelCase : int =int(np.average(arr[i : i + size, j : j + size] ) ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 UpperCAmelCase : Optional[int] =0 UpperCAmelCase : Any =0 return updated_arr # Main Function if __name__ == "__main__": from doctest import testmod testmod(name='''avgpooling''', verbose=True) # Loading the image __snake_case = Image.open('''path_to_image''') # Converting the image to numpy array and maxpooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show() # Converting the image to numpy array and averagepooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()
348
from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class __snake_case : __lowerCamelCase : str = BlenderbotConfig __lowerCamelCase : Optional[Any] = {} __lowerCamelCase : Optional[int] = """gelu""" def __init__( self , snake_case__ , snake_case__=13 , snake_case__=7 , snake_case__=True , snake_case__=False , snake_case__=99 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=20 , snake_case__=2 , snake_case__=1 , snake_case__=0 , ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : Dict =seq_length UpperCAmelCase : Optional[Any] =is_training UpperCAmelCase : List[str] =use_labels UpperCAmelCase : List[Any] =vocab_size UpperCAmelCase : Optional[int] =hidden_size UpperCAmelCase : Tuple =num_hidden_layers UpperCAmelCase : Any =num_attention_heads UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : str =hidden_dropout_prob UpperCAmelCase : Optional[int] =attention_probs_dropout_prob UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : List[Any] =eos_token_id UpperCAmelCase : Optional[int] =pad_token_id UpperCAmelCase : Tuple =bos_token_id def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[Any] =ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) UpperCAmelCase : List[Any] =tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) UpperCAmelCase : Tuple =tf.concat([input_ids, eos_tensor] , axis=1 ) UpperCAmelCase : str =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : Optional[Any] =self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) UpperCAmelCase : List[str] =prepare_blenderbot_inputs_dict(snake_case__ , snake_case__ , snake_case__ ) return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =TFBlenderbotModel(config=snake_case__ ).get_decoder() UpperCAmelCase : Any =inputs_dict['''input_ids'''] UpperCAmelCase : str =input_ids[:1, :] UpperCAmelCase : Tuple =inputs_dict['''attention_mask'''][:1, :] UpperCAmelCase : Tuple =inputs_dict['''head_mask'''] UpperCAmelCase : List[Any] =1 # first forward pass UpperCAmelCase : List[str] =model(snake_case__ , attention_mask=snake_case__ , head_mask=snake_case__ , use_cache=snake_case__ ) UpperCAmelCase , UpperCAmelCase : str =outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase : Union[str, Any] =ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCAmelCase : List[Any] =tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and UpperCAmelCase : Tuple =tf.concat([input_ids, next_tokens] , axis=-1 ) UpperCAmelCase : int =tf.concat([attention_mask, next_attn_mask] , axis=-1 ) UpperCAmelCase : Optional[int] =model(snake_case__ , attention_mask=snake_case__ )[0] UpperCAmelCase : str =model(snake_case__ , attention_mask=snake_case__ , past_key_values=snake_case__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice UpperCAmelCase : List[Any] =int(ids_tensor((1,) , output_from_past.shape[-1] ) ) UpperCAmelCase : List[Any] =output_from_no_past[:, -3:, random_slice_idx] UpperCAmelCase : Dict =output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(snake_case__ , snake_case__ , rtol=1e-3 ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , )-> str: '''simple docstring''' if attention_mask is None: UpperCAmelCase : int =tf.cast(tf.math.not_equal(__lowerCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase : Tuple =tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase : str =tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase : Union[str, Any] =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase : int =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : List[str] = (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __lowerCamelCase : Dict = (TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __lowerCamelCase : Dict = ( { """conversational""": TFBlenderbotForConditionalGeneration, """feature-extraction""": TFBlenderbotModel, """summarization""": TFBlenderbotForConditionalGeneration, """text2text-generation""": TFBlenderbotForConditionalGeneration, """translation""": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __lowerCamelCase : Union[str, Any] = True __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Union[str, Any] = False def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : List[str] =TFBlenderbotModelTester(self ) UpperCAmelCase : List[Any] =ConfigTester(self , config_class=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*snake_case__ ) @require_tokenizers @require_tf class __snake_case ( unittest.TestCase ): __lowerCamelCase : List[str] = ["""My friends are cool but they eat too many carbs."""] __lowerCamelCase : Dict = """facebook/blenderbot-400M-distill""" @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[int] =self.tokenizer(self.src_text , return_tensors='''tf''' ) UpperCAmelCase : Optional[int] =self.model.generate( model_inputs.input_ids , ) UpperCAmelCase : str =self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=snake_case__ )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
348
1
import json import os import unittest from transformers import AutoTokenizer, GPTaTokenizer, GPTaTokenizerFast from transformers.models.gpta.tokenization_gpta import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __snake_case ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Union[str, Any] = GPTaTokenizer __lowerCamelCase : Dict = GPTaTokenizerFast __lowerCamelCase : List[Any] = True __lowerCamelCase : str = {"""add_prefix_space""": True} __lowerCamelCase : Dict = False def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt UpperCAmelCase : Tuple =[ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', '''<|endoftext|>''', ] UpperCAmelCase : str =dict(zip(snake_case__ , range(len(snake_case__ ) ) ) ) UpperCAmelCase : List[Any] =['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] UpperCAmelCase : Tuple ={'''unk_token''': '''<unk>'''} UpperCAmelCase : str =os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) UpperCAmelCase : Tuple =os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(snake_case__ ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(snake_case__ ) ) def UpperCAmelCase__ ( self , **snake_case__ ) -> List[str]: '''simple docstring''' kwargs.update(self.special_tokens_map ) return GPTaTokenizer.from_pretrained(self.tmpdirname , **snake_case__ ) def UpperCAmelCase__ ( self , **snake_case__ ) -> Optional[Any]: '''simple docstring''' kwargs.update(self.special_tokens_map ) return GPTaTokenizerFast.from_pretrained(self.tmpdirname , **snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ ) -> str: '''simple docstring''' UpperCAmelCase : Optional[Any] ='''lower newer''' UpperCAmelCase : Tuple ='''lower newer''' return input_text, output_text def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : Any =GPTaTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) UpperCAmelCase : Tuple ='''lower newer''' UpperCAmelCase : int =['''\u0120low''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] UpperCAmelCase : List[Any] =tokenizer.tokenize(snake_case__ , add_prefix_space=snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =tokens + [tokenizer.unk_token] UpperCAmelCase : int =[14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case__ ) , snake_case__ ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' if not self.test_rust_tokenizer: return UpperCAmelCase : Union[str, Any] =self.get_tokenizer() UpperCAmelCase : Union[str, Any] =self.get_rust_tokenizer(add_prefix_space=snake_case__ ) UpperCAmelCase : Dict ='''lower newer''' # Testing tokenization UpperCAmelCase : Optional[Any] =tokenizer.tokenize(snake_case__ , add_prefix_space=snake_case__ ) UpperCAmelCase : str =rust_tokenizer.tokenize(snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) # Testing conversion to ids without special tokens UpperCAmelCase : str =tokenizer.encode(snake_case__ , add_special_tokens=snake_case__ , add_prefix_space=snake_case__ ) UpperCAmelCase : List[str] =rust_tokenizer.encode(snake_case__ , add_special_tokens=snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) # Testing conversion to ids with special tokens UpperCAmelCase : Dict =self.get_rust_tokenizer(add_prefix_space=snake_case__ ) UpperCAmelCase : Tuple =tokenizer.encode(snake_case__ , add_prefix_space=snake_case__ ) UpperCAmelCase : Any =rust_tokenizer.encode(snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) # Testing the unknown token UpperCAmelCase : int =tokens + [rust_tokenizer.unk_token] UpperCAmelCase : Any =[14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(snake_case__ ) , snake_case__ ) def UpperCAmelCase__ ( self , *snake_case__ , **snake_case__ ) -> Optional[int]: '''simple docstring''' pass def UpperCAmelCase__ ( self , snake_case__=15 ) -> str: '''simple docstring''' for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): UpperCAmelCase : Dict =self.rust_tokenizer_class.from_pretrained(snake_case__ , **snake_case__ ) # Simple input UpperCAmelCase : Optional[int] ='''This is a simple input''' UpperCAmelCase : Tuple =['''This is a simple input 1''', '''This is a simple input 2'''] UpperCAmelCase : Optional[int] =('''This is a simple input''', '''This is a pair''') UpperCAmelCase : 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(snake_case__ , tokenizer_r.encode , snake_case__ , max_length=snake_case__ , padding='''max_length''' ) # Simple input self.assertRaises(snake_case__ , tokenizer_r.encode_plus , snake_case__ , max_length=snake_case__ , padding='''max_length''' ) # Simple input self.assertRaises( snake_case__ , tokenizer_r.batch_encode_plus , snake_case__ , max_length=snake_case__ , padding='''max_length''' , ) # Pair input self.assertRaises(snake_case__ , tokenizer_r.encode , snake_case__ , max_length=snake_case__ , padding='''max_length''' ) # Pair input self.assertRaises(snake_case__ , tokenizer_r.encode_plus , snake_case__ , max_length=snake_case__ , padding='''max_length''' ) # Pair input self.assertRaises( snake_case__ , tokenizer_r.batch_encode_plus , snake_case__ , max_length=snake_case__ , padding='''max_length''' , ) def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : List[Any] =GPTaTokenizer.from_pretrained(self.tmpdirname , pad_token='''<pad>''' ) # Simple input UpperCAmelCase : Any ='''This is a simple input''' UpperCAmelCase : Union[str, Any] =['''This is a simple input looooooooong''', '''This is a simple input'''] UpperCAmelCase : Union[str, Any] =('''This is a simple input''', '''This is a pair''') UpperCAmelCase : str =[ ('''This is a simple input loooooong''', '''This is a simple input'''), ('''This is a simple pair loooooong''', '''This is a simple pair'''), ] UpperCAmelCase : Union[str, Any] =tokenizer.pad_token_id UpperCAmelCase : Optional[Any] =tokenizer(snake_case__ , padding='''max_length''' , max_length=30 , return_tensors='''np''' ) UpperCAmelCase : int =tokenizer(snake_case__ , padding=snake_case__ , truncate=snake_case__ , return_tensors='''np''' ) UpperCAmelCase : Optional[Any] =tokenizer(*snake_case__ , padding='''max_length''' , max_length=60 , return_tensors='''np''' ) UpperCAmelCase : Union[str, Any] =tokenizer(snake_case__ , padding=snake_case__ , truncate=snake_case__ , return_tensors='''np''' ) # s # test single string max_length padding self.assertEqual(out_s['''input_ids'''].shape[-1] , 30 ) self.assertTrue(pad_token_id in out_s['''input_ids'''] ) self.assertTrue(0 in out_s['''attention_mask'''] ) # s2 # test automatic padding self.assertEqual(out_sa['''input_ids'''].shape[-1] , 33 ) # long slice doesn't have padding self.assertFalse(pad_token_id in out_sa['''input_ids'''][0] ) self.assertFalse(0 in out_sa['''attention_mask'''][0] ) # short slice does have padding self.assertTrue(pad_token_id in out_sa['''input_ids'''][1] ) self.assertTrue(0 in out_sa['''attention_mask'''][1] ) # p # test single pair max_length padding self.assertEqual(out_p['''input_ids'''].shape[-1] , 60 ) self.assertTrue(pad_token_id in out_p['''input_ids'''] ) self.assertTrue(0 in out_p['''attention_mask'''] ) # p2 # test automatic padding pair self.assertEqual(out_pa['''input_ids'''].shape[-1] , 52 ) # long slice pair doesn't have padding self.assertFalse(pad_token_id in out_pa['''input_ids'''][0] ) self.assertFalse(0 in out_pa['''attention_mask'''][0] ) # short slice pair does have padding self.assertTrue(pad_token_id in out_pa['''input_ids'''][1] ) self.assertTrue(0 in out_pa['''attention_mask'''][1] ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Dict ='''$$$''' UpperCAmelCase : List[str] =GPTaTokenizer.from_pretrained(self.tmpdirname , bos_token=snake_case__ , add_bos_token=snake_case__ ) UpperCAmelCase : Dict ='''This is a simple input''' UpperCAmelCase : Optional[Any] =['''This is a simple input 1''', '''This is a simple input 2'''] UpperCAmelCase : List[Any] =tokenizer.bos_token_id UpperCAmelCase : Optional[Any] =tokenizer(snake_case__ ) UpperCAmelCase : Union[str, Any] =tokenizer(snake_case__ ) self.assertEqual(out_s.input_ids[0] , snake_case__ ) self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids ) ) UpperCAmelCase : List[Any] =tokenizer.decode(out_s.input_ids ) UpperCAmelCase : Any =tokenizer.batch_decode(out_sa.input_ids ) self.assertEqual(decode_s.split()[0] , snake_case__ ) self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa ) ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : List[Any] =[self.get_tokenizer(do_lower_case=snake_case__ , add_bos_token=snake_case__ )] for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): UpperCAmelCase : Tuple ='''Encode this.''' UpperCAmelCase : Union[str, Any] ='''This one too please.''' UpperCAmelCase : Optional[Any] =tokenizer.encode(snake_case__ , add_special_tokens=snake_case__ ) encoded_sequence += tokenizer.encode(snake_case__ , add_special_tokens=snake_case__ ) UpperCAmelCase : List[str] =tokenizer.encode_plus( snake_case__ , snake_case__ , add_special_tokens=snake_case__ , return_special_tokens_mask=snake_case__ , ) UpperCAmelCase : List[Any] =encoded_sequence_dict['''input_ids'''] UpperCAmelCase : List[Any] =encoded_sequence_dict['''special_tokens_mask'''] self.assertEqual(len(snake_case__ ) , len(snake_case__ ) ) UpperCAmelCase : Optional[int] =[ (x if not special_tokens_mask[i] else None) for i, x in enumerate(snake_case__ ) ] UpperCAmelCase : Optional[Any] =[x for x in filtered_sequence if x is not None] self.assertEqual(snake_case__ , snake_case__ ) @require_tokenizers class __snake_case ( unittest.TestCase ): def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Dict =AutoTokenizer.from_pretrained('''facebook/opt-350m''' , from_slow=snake_case__ ) UpperCAmelCase : Any ='''A photo of a cat''' UpperCAmelCase : Union[str, Any] =tokenizer.encode( snake_case__ , ) self.assertEqual(snake_case__ , [2, 250, 1345, 9, 10, 4758] ) tokenizer.save_pretrained('''test_opt''' ) UpperCAmelCase : List[str] =AutoTokenizer.from_pretrained('''./test_opt''' ) UpperCAmelCase : Any =tokenizer.encode( snake_case__ , ) self.assertEqual(snake_case__ , [2, 250, 1345, 9, 10, 4758] ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : int =AutoTokenizer.from_pretrained('''facebook/opt-350m''' , use_slow=snake_case__ ) UpperCAmelCase : Optional[int] ='''A photo of a cat''' UpperCAmelCase : Any =tokenizer.encode( snake_case__ , ) # Same as above self.assertEqual(snake_case__ , [2, 250, 1345, 9, 10, 4758] ) @unittest.skip('''This test is failing because of a bug in the fast tokenizer''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Dict =AutoTokenizer.from_pretrained('''facebook/opt-350m''' , from_slow=snake_case__ ) UpperCAmelCase : Any ='''bos''' UpperCAmelCase : str =tokenizer.get_vocab()['''bos'''] UpperCAmelCase : Dict ='''A photo of a cat''' UpperCAmelCase : Optional[Any] =tokenizer.encode( snake_case__ , ) # We changed the bos token self.assertEqual(snake_case__ , [3_1957, 250, 1345, 9, 10, 4758] ) tokenizer.save_pretrained('''./tok''' ) UpperCAmelCase : Tuple =AutoTokenizer.from_pretrained('''./tok''' ) self.assertTrue(tokenizer.is_fast ) UpperCAmelCase : Dict =tokenizer.encode( snake_case__ , ) self.assertEqual(snake_case__ , [3_1957, 250, 1345, 9, 10, 4758] )
348
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { '''asapp/sew-d-tiny-100k''': '''https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json''', # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[Any] = """sew-d""" def __init__( self , snake_case__=32 , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=2 , snake_case__=512 , snake_case__=256 , snake_case__=True , snake_case__=True , snake_case__=("p2c", "c2p") , snake_case__="layer_norm" , snake_case__="gelu_python" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.0 , snake_case__=0.1 , snake_case__=0.02 , snake_case__=1e-7 , snake_case__=1e-5 , snake_case__="group" , snake_case__="gelu" , snake_case__=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , snake_case__=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , snake_case__=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , snake_case__=False , snake_case__=128 , snake_case__=16 , snake_case__=True , snake_case__=0.05 , snake_case__=10 , snake_case__=2 , snake_case__=0.0 , snake_case__=10 , snake_case__=0 , snake_case__="mean" , snake_case__=False , snake_case__=False , snake_case__=256 , snake_case__=0 , snake_case__=1 , snake_case__=2 , **snake_case__ , ) -> int: '''simple docstring''' super().__init__(**snake_case__ , pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ ) UpperCAmelCase : Union[str, Any] =hidden_size UpperCAmelCase : Union[str, Any] =feat_extract_norm UpperCAmelCase : Optional[Any] =feat_extract_activation UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : int =list(snake_case__ ) UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : str =conv_bias UpperCAmelCase : Tuple =num_conv_pos_embeddings UpperCAmelCase : Dict =num_conv_pos_embedding_groups UpperCAmelCase : str =len(self.conv_dim ) UpperCAmelCase : Dict =num_hidden_layers UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : List[Any] =squeeze_factor UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : int =position_buckets UpperCAmelCase : Optional[int] =share_att_key UpperCAmelCase : Optional[int] =relative_attention UpperCAmelCase : Tuple =norm_rel_ebd UpperCAmelCase : List[Any] =list(snake_case__ ) UpperCAmelCase : Dict =hidden_act UpperCAmelCase : Optional[int] =num_attention_heads UpperCAmelCase : Any =hidden_dropout UpperCAmelCase : str =attention_dropout UpperCAmelCase : Union[str, Any] =activation_dropout UpperCAmelCase : str =feat_proj_dropout UpperCAmelCase : Union[str, Any] =final_dropout UpperCAmelCase : Optional[int] =layer_norm_eps UpperCAmelCase : str =feature_layer_norm_eps UpperCAmelCase : str =initializer_range UpperCAmelCase : Any =vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect.''' '''It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,''' f'''but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)''' f'''= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCAmelCase : Union[str, Any] =apply_spec_augment UpperCAmelCase : Optional[Any] =mask_time_prob UpperCAmelCase : Tuple =mask_time_length UpperCAmelCase : str =mask_time_min_masks UpperCAmelCase : Optional[int] =mask_feature_prob UpperCAmelCase : Optional[Any] =mask_feature_length UpperCAmelCase : List[Any] =mask_feature_min_masks # ctc loss UpperCAmelCase : str =ctc_loss_reduction UpperCAmelCase : Optional[int] =ctc_zero_infinity # sequence classification UpperCAmelCase : Union[str, Any] =use_weighted_layer_sum UpperCAmelCase : int =classifier_proj_size @property def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
348
1
def lowerCAmelCase_ ( __lowerCAmelCase )-> int: '''simple docstring''' UpperCAmelCase : int =abs(__lowerCAmelCase ) UpperCAmelCase : Dict =0 while n > 0: res += n % 10 n //= 10 return res def lowerCAmelCase_ ( __lowerCAmelCase )-> int: '''simple docstring''' UpperCAmelCase : Any =abs(__lowerCAmelCase ) return n if n < 10 else n % 10 + sum_of_digits(n // 10 ) def lowerCAmelCase_ ( __lowerCAmelCase )-> int: '''simple docstring''' return sum(int(__lowerCAmelCase ) for c in str(abs(__lowerCAmelCase ) ) ) def lowerCAmelCase_ ( )-> None: '''simple docstring''' from collections.abc import Callable from timeit import timeit def benchmark_a_function(__lowerCAmelCase , __lowerCAmelCase ) -> None: UpperCAmelCase : Optional[Any] =f'''{func.__name__}({value})''' UpperCAmelCase : List[str] =timeit(f'''__main__.{call}''' , setup='''import __main__''' ) print(f'''{call:56} = {func(__lowerCAmelCase )} -- {timing:.4f} seconds''' ) for value in (26_21_44, 11_25_89_99_06_84_26_24, 1_26_76_50_60_02_28_22_94_01_49_67_03_20_53_76): for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact): benchmark_a_function(__lowerCAmelCase , __lowerCAmelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
348
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node __snake_case = 4 __snake_case = 3 class __snake_case ( lowerCamelCase__ ): pass def lowerCAmelCase_ ( __lowerCAmelCase )-> List[str]: '''simple docstring''' for shard in shards: for i in range(__lowerCAmelCase ): yield {"i": i, "shard": shard} def lowerCAmelCase_ ( )-> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =int(os.environ['''RANK'''] ) UpperCAmelCase : Optional[Any] =int(os.environ['''WORLD_SIZE'''] ) UpperCAmelCase : List[Any] =ArgumentParser() parser.add_argument('''--streaming''' , type=__lowerCAmelCase ) parser.add_argument('''--local_rank''' , type=__lowerCAmelCase ) parser.add_argument('''--num_workers''' , type=__lowerCAmelCase , default=0 ) UpperCAmelCase : Any =parser.parse_args() UpperCAmelCase : List[str] =args.streaming UpperCAmelCase : Tuple =args.num_workers UpperCAmelCase : int ={'''shards''': [f'''shard_{shard_idx}''' for shard_idx in range(__lowerCAmelCase )]} UpperCAmelCase : Optional[int] =IterableDataset.from_generator(__lowerCAmelCase , gen_kwargs=__lowerCAmelCase ) if not streaming: UpperCAmelCase : List[Any] =Dataset.from_list(list(__lowerCAmelCase ) ) UpperCAmelCase : Dict =split_dataset_by_node(__lowerCAmelCase , rank=__lowerCAmelCase , world_size=__lowerCAmelCase ) UpperCAmelCase : List[Any] =torch.utils.data.DataLoader(__lowerCAmelCase , num_workers=__lowerCAmelCase ) UpperCAmelCase : Dict =NUM_SHARDS * NUM_ITEMS_PER_SHARD UpperCAmelCase : str =full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) UpperCAmelCase : List[Any] =sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f'''local_size {local_size} != expected_local_size {expected_local_size}''' ) if __name__ == "__main__": main()
348
1
def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> List[str]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =0 UpperCAmelCase : Optional[Any] =len(__lowerCAmelCase ) - 1 while left <= right: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None UpperCAmelCase : Optional[Any] =left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(__lowerCAmelCase ): return None UpperCAmelCase : List[str] =sorted_collection[point] if current_item == item: return point else: if point < left: UpperCAmelCase : Tuple =left UpperCAmelCase : Optional[Any] =point elif point > right: UpperCAmelCase : Optional[Any] =right UpperCAmelCase : Union[str, Any] =point else: if item < current_item: UpperCAmelCase : str =point - 1 else: UpperCAmelCase : Any =point + 1 return None def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> Any: '''simple docstring''' if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None UpperCAmelCase : str =left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(__lowerCAmelCase ): return None if sorted_collection[point] == item: return point elif point < left: return interpolation_search_by_recursion(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) elif point > right: return interpolation_search_by_recursion(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) else: if sorted_collection[point] > item: return interpolation_search_by_recursion( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , point - 1 ) else: return interpolation_search_by_recursion( __lowerCAmelCase , __lowerCAmelCase , point + 1 , __lowerCAmelCase ) def lowerCAmelCase_ ( __lowerCAmelCase )-> Optional[int]: '''simple docstring''' if collection != sorted(__lowerCAmelCase ): raise ValueError('''Collection must be ascending sorted''' ) return True if __name__ == "__main__": import sys __snake_case = 0 if debug == 1: __snake_case = [10, 30, 40, 45, 50, 66, 77, 93] try: __assert_sorted(collection) except ValueError: sys.exit('''Sequence must be ascending sorted to apply interpolation search''') __snake_case = 67 __snake_case = interpolation_search(collection, target) if result is not None: print(f'{target} found at positions: {result}') else: print('''Not found''')
348
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __snake_case = {'''configuration_opt''': ['''OPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OPTConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''OPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OPTForCausalLM''', '''OPTModel''', '''OPTPreTrainedModel''', '''OPTForSequenceClassification''', '''OPTForQuestionAnswering''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''TFOPTForCausalLM''', '''TFOPTModel''', '''TFOPTPreTrainedModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''FlaxOPTForCausalLM''', '''FlaxOPTModel''', '''FlaxOPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
1
import argparse import os import gluonnlp as nlp import mxnet as mx import numpy as np import torch from gluonnlp.base import get_home_dir from gluonnlp.model.bert import BERTEncoder from gluonnlp.model.utils import _load_vocab from gluonnlp.vocab import Vocab from packaging import version from torch import nn from transformers import BertConfig, BertForMaskedLM, BertModel, RobertaTokenizer from transformers.models.bert.modeling_bert import ( BertIntermediate, BertLayer, BertOutput, BertSelfAttention, BertSelfOutput, ) from transformers.utils import logging if version.parse(nlp.__version__) != version.parse('''0.8.3'''): raise Exception('''requires gluonnlp == 0.8.3''') if version.parse(mx.__version__) != version.parse('''1.5.0'''): raise Exception('''requires mxnet == 1.5.0''') logging.set_verbosity_info() __snake_case = logging.get_logger(__name__) __snake_case = '''The Nymphenburg Palace is a beautiful palace in Munich!''' def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> Tuple: '''simple docstring''' UpperCAmelCase : List[str] ={ '''attention_cell''': '''multi_head''', '''num_layers''': 4, '''units''': 10_24, '''hidden_size''': 7_68, '''max_length''': 5_12, '''num_heads''': 8, '''scaled''': True, '''dropout''': 0.1, '''use_residual''': True, '''embed_size''': 10_24, '''embed_dropout''': 0.1, '''word_embed''': None, '''layer_norm_eps''': 1e-5, '''token_type_vocab_size''': 2, } UpperCAmelCase : Union[str, Any] =bort_4_8_768_1024_hparams # Let's construct the original Bort model here # Taken from official BERT implementation, see: # https://github.com/alexa/bort/blob/master/bort/bort.py UpperCAmelCase : Any =BERTEncoder( attention_cell=predefined_args['''attention_cell'''] , num_layers=predefined_args['''num_layers'''] , units=predefined_args['''units'''] , hidden_size=predefined_args['''hidden_size'''] , max_length=predefined_args['''max_length'''] , num_heads=predefined_args['''num_heads'''] , scaled=predefined_args['''scaled'''] , dropout=predefined_args['''dropout'''] , output_attention=__lowerCAmelCase , output_all_encodings=__lowerCAmelCase , use_residual=predefined_args['''use_residual'''] , activation=predefined_args.get('''activation''' , '''gelu''' ) , layer_norm_eps=predefined_args.get('''layer_norm_eps''' , __lowerCAmelCase ) , ) # Vocab information needs to be fetched first # It's the same as RoBERTa, so RobertaTokenizer can be used later UpperCAmelCase : Union[str, Any] ='''openwebtext_ccnews_stories_books_cased''' # Specify download folder to Gluonnlp's vocab UpperCAmelCase : Dict =os.path.join(get_home_dir() , '''models''' ) UpperCAmelCase : Optional[int] =_load_vocab(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , cls=__lowerCAmelCase ) UpperCAmelCase : Optional[int] =nlp.model.BERTModel( __lowerCAmelCase , len(__lowerCAmelCase ) , units=predefined_args['''units'''] , embed_size=predefined_args['''embed_size'''] , embed_dropout=predefined_args['''embed_dropout'''] , word_embed=predefined_args['''word_embed'''] , use_pooler=__lowerCAmelCase , use_token_type_embed=__lowerCAmelCase , token_type_vocab_size=predefined_args['''token_type_vocab_size'''] , use_classifier=__lowerCAmelCase , use_decoder=__lowerCAmelCase , ) original_bort.load_parameters(__lowerCAmelCase , cast_dtype=__lowerCAmelCase , ignore_extra=__lowerCAmelCase ) UpperCAmelCase : List[Any] =original_bort._collect_params_with_prefix() # Build our config 🤗 UpperCAmelCase : Optional[Any] ={ '''architectures''': ['''BertForMaskedLM'''], '''attention_probs_dropout_prob''': predefined_args['''dropout'''], '''hidden_act''': '''gelu''', '''hidden_dropout_prob''': predefined_args['''dropout'''], '''hidden_size''': predefined_args['''embed_size'''], '''initializer_range''': 0.02, '''intermediate_size''': predefined_args['''hidden_size'''], '''layer_norm_eps''': predefined_args['''layer_norm_eps'''], '''max_position_embeddings''': predefined_args['''max_length'''], '''model_type''': '''bort''', '''num_attention_heads''': predefined_args['''num_heads'''], '''num_hidden_layers''': predefined_args['''num_layers'''], '''pad_token_id''': 1, # 2 = BERT, 1 = RoBERTa '''type_vocab_size''': 1, # 2 = BERT, 1 = RoBERTa '''vocab_size''': len(__lowerCAmelCase ), } UpperCAmelCase : Optional[Any] =BertConfig.from_dict(__lowerCAmelCase ) UpperCAmelCase : List[Any] =BertForMaskedLM(__lowerCAmelCase ) hf_bort_model.eval() # Parameter mapping table (Gluonnlp to Transformers) # * denotes layer index # # | Gluon Parameter | Transformers Parameter # | -------------------------------------------------------------- | ---------------------- # | `encoder.layer_norm.beta` | `bert.embeddings.LayerNorm.bias` # | `encoder.layer_norm.gamma` | `bert.embeddings.LayerNorm.weight` # | `encoder.position_weight` | `bert.embeddings.position_embeddings.weight` # | `word_embed.0.weight` | `bert.embeddings.word_embeddings.weight` # | `encoder.transformer_cells.*.attention_cell.proj_key.bias` | `bert.encoder.layer.*.attention.self.key.bias` # | `encoder.transformer_cells.*.attention_cell.proj_key.weight` | `bert.encoder.layer.*.attention.self.key.weight` # | `encoder.transformer_cells.*.attention_cell.proj_query.bias` | `bert.encoder.layer.*.attention.self.query.bias` # | `encoder.transformer_cells.*.attention_cell.proj_query.weight` | `bert.encoder.layer.*.attention.self.query.weight` # | `encoder.transformer_cells.*.attention_cell.proj_value.bias` | `bert.encoder.layer.*.attention.self.value.bias` # | `encoder.transformer_cells.*.attention_cell.proj_value.weight` | `bert.encoder.layer.*.attention.self.value.weight` # | `encoder.transformer_cells.*.ffn.ffn_2.bias` | `bert.encoder.layer.*.attention.output.dense.bias` # | `encoder.transformer_cells.*.ffn.ffn_2.weight` | `bert.encoder.layer.*.attention.output.dense.weight` # | `encoder.transformer_cells.*.layer_norm.beta` | `bert.encoder.layer.*.attention.output.LayerNorm.bias` # | `encoder.transformer_cells.*.layer_norm.gamma` | `bert.encoder.layer.*.attention.output.LayerNorm.weight` # | `encoder.transformer_cells.*.ffn.ffn_1.bias` | `bert.encoder.layer.*.intermediate.dense.bias` # | `encoder.transformer_cells.*.ffn.ffn_1.weight` | `bert.encoder.layer.*.intermediate.dense.weight` # | `encoder.transformer_cells.*.ffn.layer_norm.beta` | `bert.encoder.layer.*.output.LayerNorm.bias` # | `encoder.transformer_cells.*.ffn.layer_norm.gamma` | `bert.encoder.layer.*.output.LayerNorm.weight` # | `encoder.transformer_cells.*.proj.bias` | `bert.encoder.layer.*.output.dense.bias` # | `encoder.transformer_cells.*.proj.weight` | `bert.encoder.layer.*.output.dense.weight` # Helper function to convert MXNET Arrays to PyTorch def to_torch(__lowerCAmelCase ) -> nn.Parameter: return nn.Parameter(torch.FloatTensor(mx_array.data().asnumpy() ) ) # Check param shapes and map new HF param back def check_and_map_params(__lowerCAmelCase , __lowerCAmelCase ): UpperCAmelCase : Dict =hf_param.shape UpperCAmelCase : Optional[int] =to_torch(params[gluon_param] ) UpperCAmelCase : List[Any] =gluon_param.shape assert ( shape_hf == shape_gluon ), f'''The gluon parameter {gluon_param} has shape {shape_gluon}, but expects shape {shape_hf} for Transformers''' return gluon_param UpperCAmelCase : Union[str, Any] =check_and_map_params( hf_bort_model.bert.embeddings.word_embeddings.weight , '''word_embed.0.weight''' ) UpperCAmelCase : Tuple =check_and_map_params( hf_bort_model.bert.embeddings.position_embeddings.weight , '''encoder.position_weight''' ) UpperCAmelCase : List[str] =check_and_map_params( hf_bort_model.bert.embeddings.LayerNorm.bias , '''encoder.layer_norm.beta''' ) UpperCAmelCase : Union[str, Any] =check_and_map_params( hf_bort_model.bert.embeddings.LayerNorm.weight , '''encoder.layer_norm.gamma''' ) # Inspired by RoBERTa conversion script, we just zero them out (Bort does not use them) UpperCAmelCase : Optional[Any] =torch.zeros_like( hf_bort_model.bert.embeddings.token_type_embeddings.weight.data ) for i in range(hf_bort_config.num_hidden_layers ): UpperCAmelCase : BertLayer =hf_bort_model.bert.encoder.layer[i] # self attention UpperCAmelCase : BertSelfAttention =layer.attention.self UpperCAmelCase : int =check_and_map_params( self_attn.key.bias.data , f'''encoder.transformer_cells.{i}.attention_cell.proj_key.bias''' ) UpperCAmelCase : Dict =check_and_map_params( self_attn.key.weight.data , f'''encoder.transformer_cells.{i}.attention_cell.proj_key.weight''' ) UpperCAmelCase : Dict =check_and_map_params( self_attn.query.bias.data , f'''encoder.transformer_cells.{i}.attention_cell.proj_query.bias''' ) UpperCAmelCase : Any =check_and_map_params( self_attn.query.weight.data , f'''encoder.transformer_cells.{i}.attention_cell.proj_query.weight''' ) UpperCAmelCase : str =check_and_map_params( self_attn.value.bias.data , f'''encoder.transformer_cells.{i}.attention_cell.proj_value.bias''' ) UpperCAmelCase : Tuple =check_and_map_params( self_attn.value.weight.data , f'''encoder.transformer_cells.{i}.attention_cell.proj_value.weight''' ) # self attention output UpperCAmelCase : BertSelfOutput =layer.attention.output UpperCAmelCase : Optional[Any] =check_and_map_params( self_output.dense.bias , f'''encoder.transformer_cells.{i}.proj.bias''' ) UpperCAmelCase : Union[str, Any] =check_and_map_params( self_output.dense.weight , f'''encoder.transformer_cells.{i}.proj.weight''' ) UpperCAmelCase : Dict =check_and_map_params( self_output.LayerNorm.bias , f'''encoder.transformer_cells.{i}.layer_norm.beta''' ) UpperCAmelCase : Union[str, Any] =check_and_map_params( self_output.LayerNorm.weight , f'''encoder.transformer_cells.{i}.layer_norm.gamma''' ) # intermediate UpperCAmelCase : BertIntermediate =layer.intermediate UpperCAmelCase : Optional[int] =check_and_map_params( intermediate.dense.bias , f'''encoder.transformer_cells.{i}.ffn.ffn_1.bias''' ) UpperCAmelCase : Dict =check_and_map_params( intermediate.dense.weight , f'''encoder.transformer_cells.{i}.ffn.ffn_1.weight''' ) # output UpperCAmelCase : BertOutput =layer.output UpperCAmelCase : Dict =check_and_map_params( bert_output.dense.bias , f'''encoder.transformer_cells.{i}.ffn.ffn_2.bias''' ) UpperCAmelCase : Optional[int] =check_and_map_params( bert_output.dense.weight , f'''encoder.transformer_cells.{i}.ffn.ffn_2.weight''' ) UpperCAmelCase : List[str] =check_and_map_params( bert_output.LayerNorm.bias , f'''encoder.transformer_cells.{i}.ffn.layer_norm.beta''' ) UpperCAmelCase : int =check_and_map_params( bert_output.LayerNorm.weight , f'''encoder.transformer_cells.{i}.ffn.layer_norm.gamma''' ) # Save space and energy 🎄 hf_bort_model.half() # Compare output of both models UpperCAmelCase : Dict =RobertaTokenizer.from_pretrained('''roberta-base''' ) UpperCAmelCase : Optional[int] =tokenizer.encode_plus(__lowerCAmelCase )['''input_ids'''] # Get gluon output UpperCAmelCase : Optional[Any] =mx.nd.array([input_ids] ) UpperCAmelCase : int =original_bort(inputs=__lowerCAmelCase , token_types=[] ) # Get Transformer output (save and reload model again) hf_bort_model.save_pretrained(__lowerCAmelCase ) UpperCAmelCase : List[str] =BertModel.from_pretrained(__lowerCAmelCase ) hf_bort_model.eval() UpperCAmelCase : Union[str, Any] =tokenizer.encode_plus(__lowerCAmelCase , return_tensors='''pt''' ) UpperCAmelCase : str =hf_bort_model(**__lowerCAmelCase )[0] UpperCAmelCase : Dict =output_gluon[0].asnumpy() UpperCAmelCase : Optional[int] =output_hf[0].detach().numpy() UpperCAmelCase : Optional[int] =np.max(np.abs(hf_layer - gluon_layer ) ).item() UpperCAmelCase : Optional[Any] =np.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-3 ) if success: print('''✔️ Both model do output the same tensors''' ) else: print('''❌ Both model do **NOT** output the same tensors''' ) print('''Absolute difference is:''' , __lowerCAmelCase ) if __name__ == "__main__": __snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--bort_checkpoint_path''', default=None, type=str, required=True, help='''Path the official Bort params file.''' ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __snake_case = parser.parse_args() convert_bort_checkpoint_to_pytorch(args.bort_checkpoint_path, args.pytorch_dump_folder_path)
348
import tempfile import unittest import numpy as np import transformers from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax import jax.numpy as jnp from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel if is_torch_available(): import torch class __snake_case : def __init__( self , snake_case__ , snake_case__=14 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=False , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=4 , snake_case__=4 , snake_case__=4 , snake_case__=37 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , ) -> str: '''simple docstring''' UpperCAmelCase : str =parent UpperCAmelCase : Tuple =batch_size UpperCAmelCase : Optional[int] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Tuple =use_input_mask UpperCAmelCase : List[Any] =use_token_type_ids UpperCAmelCase : Optional[Any] =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : List[Any] =hidden_size UpperCAmelCase : Optional[int] =rotary_dim UpperCAmelCase : Union[str, Any] =num_hidden_layers UpperCAmelCase : List[Any] =num_attention_heads UpperCAmelCase : Dict =intermediate_size UpperCAmelCase : Union[str, Any] =hidden_act UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : Dict =attention_probs_dropout_prob UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : str =initializer_range UpperCAmelCase : Optional[int] =None UpperCAmelCase : List[Any] =vocab_size - 1 UpperCAmelCase : Optional[Any] =vocab_size - 1 UpperCAmelCase : List[Any] =vocab_size - 1 def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : List[Any] =None if self.use_input_mask: UpperCAmelCase : Optional[Any] =random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : Dict =GPTJConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , use_cache=snake_case__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , rotary_dim=self.rotary_dim , ) return (config, input_ids, input_mask) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Union[str, Any] =config_and_inputs UpperCAmelCase : Tuple ={'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =20 UpperCAmelCase : Any =model_class_name(snake_case__ ) UpperCAmelCase : str =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : Any =jnp.ones((input_ids.shape[0], max_decoder_length) , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : List[str] =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =model( input_ids[:, -1:] , attention_mask=snake_case__ , past_key_values=outputs_cache.past_key_values , position_ids=snake_case__ , ) UpperCAmelCase : List[Any] =model(snake_case__ ) UpperCAmelCase : Any =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Dict =20 UpperCAmelCase : Dict =model_class_name(snake_case__ ) UpperCAmelCase : Tuple =jnp.concatenate( [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]) )] , axis=-1 , ) UpperCAmelCase : Dict =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : int =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : str =model( input_ids[:, -1:] , past_key_values=outputs_cache.past_key_values , attention_mask=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =model(snake_case__ , attention_mask=snake_case__ ) UpperCAmelCase : Dict =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) @require_flax class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Tuple = (FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else () __lowerCamelCase : Optional[Any] = (FlaxGPTJForCausalLM,) if is_flax_available() else () def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =FlaxGPTJModelTester(self ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Dict =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward_with_attn_mask( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) @tooslow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =GPTaTokenizer.from_pretrained('''gpt2''' , pad_token='''<|endoftext|>''' , padding_side='''left''' ) UpperCAmelCase : Optional[Any] =tokenizer(['''Hello this is a long string''', '''Hey'''] , return_tensors='''np''' , padding=snake_case__ , truncation=snake_case__ ) UpperCAmelCase : Optional[int] =FlaxGPTJForCausalLM.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : str =False UpperCAmelCase : Union[str, Any] =model.config.eos_token_id UpperCAmelCase : List[Any] =jax.jit(model.generate ) UpperCAmelCase : Dict =jit_generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , pad_token_id=tokenizer.pad_token_id ).sequences UpperCAmelCase : Any =tokenizer.batch_decode(snake_case__ , skip_special_tokens=snake_case__ ) UpperCAmelCase : Tuple =[ '''Hello this is a long string of text.\n\nI\'m trying to get the text of the''', '''Hey, I\'m a little late to the party. I\'m going to''', ] self.assertListEqual(snake_case__ , snake_case__ ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : List[str] =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : Any =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : Any =getattr(snake_case__ , snake_case__ ) UpperCAmelCase , UpperCAmelCase : Union[str, Any] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Tuple =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : int =0 UpperCAmelCase : Optional[int] =1 UpperCAmelCase : Optional[int] =0 UpperCAmelCase : Union[str, Any] =1 UpperCAmelCase : List[str] =pt_model_class(snake_case__ ).eval() UpperCAmelCase : Optional[int] =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Any =convert_pytorch_state_dict_to_flax(pt_model.state_dict() , snake_case__ ) UpperCAmelCase : Union[str, Any] =fx_state with torch.no_grad(): UpperCAmelCase : Any =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : Dict =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(snake_case__ ) UpperCAmelCase : str =model_class.from_pretrained(snake_case__ , from_pt=snake_case__ ) UpperCAmelCase : int =fx_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output_loaded, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output_loaded[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : int =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : int =getattr(snake_case__ , snake_case__ ) UpperCAmelCase : Dict =pt_model_class(snake_case__ ).eval() UpperCAmelCase : str =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Optional[Any] =load_flax_weights_in_pytorch_model(snake_case__ , fx_model.params ) UpperCAmelCase , UpperCAmelCase : Optional[int] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Optional[int] =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : str =0 UpperCAmelCase : Any =1 UpperCAmelCase : List[Any] =0 UpperCAmelCase : Tuple =1 # make sure weights are tied in PyTorch pt_model.tie_weights() with torch.no_grad(): UpperCAmelCase : Optional[Any] =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : List[Any] =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(snake_case__ ) UpperCAmelCase : Tuple =pt_model_class.from_pretrained(snake_case__ , from_flax=snake_case__ ) with torch.no_grad(): UpperCAmelCase : Any =pt_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @tooslow def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase : str =model_class_name.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : Tuple =model(np.ones((1, 1) ) ) self.assertIsNotNone(snake_case__ )
348
1
import copy from typing import Dict, List, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING __snake_case = { '''facebook/mask2former-swin-small-coco-instance''': ( '''https://huggingface.co/facebook/mask2former-swin-small-coco-instance/blob/main/config.json''' ) # See all Mask2Former models at https://huggingface.co/models?filter=mask2former } __snake_case = logging.get_logger(__name__) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : List[Any] = """mask2former""" __lowerCamelCase : List[Any] = ["""swin"""] __lowerCamelCase : Union[str, Any] = {"""hidden_size""": """hidden_dim"""} def __init__( self , snake_case__ = None , snake_case__ = 256 , snake_case__ = 256 , snake_case__ = 256 , snake_case__ = 1024 , snake_case__ = "relu" , snake_case__ = 6 , snake_case__ = 10 , snake_case__ = 8 , snake_case__ = 0.0 , snake_case__ = 2048 , snake_case__ = False , snake_case__ = False , snake_case__ = 4 , snake_case__ = 255 , snake_case__ = 100 , snake_case__ = 0.1 , snake_case__ = 2.0 , snake_case__ = 5.0 , snake_case__ = 5.0 , snake_case__ = 1_2544 , snake_case__ = 3.0 , snake_case__ = 0.75 , snake_case__ = 0.02 , snake_case__ = 1.0 , snake_case__ = True , snake_case__ = [4, 8, 16, 32] , snake_case__ = None , **snake_case__ , ) -> Tuple: '''simple docstring''' if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `Swin` backbone.''' ) UpperCAmelCase : Optional[Any] =CONFIG_MAPPING['''swin''']( image_size=224 , in_channels=3 , patch_size=4 , embed_dim=96 , depths=[2, 2, 18, 2] , num_heads=[3, 6, 12, 24] , window_size=7 , drop_path_rate=0.3 , use_absolute_embeddings=snake_case__ , out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] , ) if isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Union[str, Any] =backbone_config.pop('''model_type''' ) UpperCAmelCase : str =CONFIG_MAPPING[backbone_model_type] UpperCAmelCase : Any =config_class.from_dict(snake_case__ ) # verify that the backbone is supported if backbone_config.model_type not in self.backbones_supported: logger.warning_once( f'''Backbone {backbone_config.model_type} is not a supported model and may not be compatible with Mask2Former. ''' f'''Supported model types: {','.join(self.backbones_supported )}''' ) UpperCAmelCase : Tuple =backbone_config UpperCAmelCase : Dict =feature_size UpperCAmelCase : Dict =mask_feature_size UpperCAmelCase : Dict =hidden_dim UpperCAmelCase : Optional[int] =encoder_feedforward_dim UpperCAmelCase : str =activation_function UpperCAmelCase : int =encoder_layers UpperCAmelCase : Tuple =decoder_layers UpperCAmelCase : Tuple =num_attention_heads UpperCAmelCase : Optional[Any] =dropout UpperCAmelCase : Optional[int] =dim_feedforward UpperCAmelCase : Any =pre_norm UpperCAmelCase : Optional[int] =enforce_input_projection UpperCAmelCase : str =common_stride UpperCAmelCase : Any =ignore_value UpperCAmelCase : List[Any] =num_queries UpperCAmelCase : List[Any] =no_object_weight UpperCAmelCase : List[Any] =class_weight UpperCAmelCase : int =mask_weight UpperCAmelCase : int =dice_weight UpperCAmelCase : Tuple =train_num_points UpperCAmelCase : Any =oversample_ratio UpperCAmelCase : Any =importance_sample_ratio UpperCAmelCase : int =init_std UpperCAmelCase : Union[str, Any] =init_xavier_std UpperCAmelCase : Any =use_auxiliary_loss UpperCAmelCase : Optional[Any] =feature_strides UpperCAmelCase : Optional[int] =output_auxiliary_logits UpperCAmelCase : Any =decoder_layers super().__init__(**snake_case__ ) @classmethod def UpperCAmelCase__ ( cls , snake_case__ , **snake_case__ ) -> Tuple: '''simple docstring''' return cls( backbone_config=snake_case__ , **snake_case__ , ) def UpperCAmelCase__ ( self ) -> Dict[str, any]: '''simple docstring''' UpperCAmelCase : List[Any] =copy.deepcopy(self.__dict__ ) UpperCAmelCase : Any =self.backbone_config.to_dict() UpperCAmelCase : int =self.__class__.model_type return output
348
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __snake_case = { '''configuration_bloom''': ['''BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BloomConfig''', '''BloomOnnxConfig'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''BloomTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BloomForCausalLM''', '''BloomModel''', '''BloomPreTrainedModel''', '''BloomForSequenceClassification''', '''BloomForTokenClassification''', '''BloomForQuestionAnswering''', ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
1
import tempfile import unittest import numpy as np import transformers from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax import jax.numpy as jnp from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel if is_torch_available(): import torch class __snake_case : def __init__( self , snake_case__ , snake_case__=14 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=False , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=4 , snake_case__=4 , snake_case__=4 , snake_case__=37 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , ) -> str: '''simple docstring''' UpperCAmelCase : str =parent UpperCAmelCase : Tuple =batch_size UpperCAmelCase : Optional[int] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Tuple =use_input_mask UpperCAmelCase : List[Any] =use_token_type_ids UpperCAmelCase : Optional[Any] =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : List[Any] =hidden_size UpperCAmelCase : Optional[int] =rotary_dim UpperCAmelCase : Union[str, Any] =num_hidden_layers UpperCAmelCase : List[Any] =num_attention_heads UpperCAmelCase : Dict =intermediate_size UpperCAmelCase : Union[str, Any] =hidden_act UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : Dict =attention_probs_dropout_prob UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : str =initializer_range UpperCAmelCase : Optional[int] =None UpperCAmelCase : List[Any] =vocab_size - 1 UpperCAmelCase : Optional[Any] =vocab_size - 1 UpperCAmelCase : List[Any] =vocab_size - 1 def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : List[Any] =None if self.use_input_mask: UpperCAmelCase : Optional[Any] =random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : Dict =GPTJConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , use_cache=snake_case__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , rotary_dim=self.rotary_dim , ) return (config, input_ids, input_mask) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Union[str, Any] =config_and_inputs UpperCAmelCase : Tuple ={'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =20 UpperCAmelCase : Any =model_class_name(snake_case__ ) UpperCAmelCase : str =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : Any =jnp.ones((input_ids.shape[0], max_decoder_length) , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : List[str] =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =model( input_ids[:, -1:] , attention_mask=snake_case__ , past_key_values=outputs_cache.past_key_values , position_ids=snake_case__ , ) UpperCAmelCase : List[Any] =model(snake_case__ ) UpperCAmelCase : Any =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Dict =20 UpperCAmelCase : Dict =model_class_name(snake_case__ ) UpperCAmelCase : Tuple =jnp.concatenate( [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]) )] , axis=-1 , ) UpperCAmelCase : Dict =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : int =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : str =model( input_ids[:, -1:] , past_key_values=outputs_cache.past_key_values , attention_mask=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =model(snake_case__ , attention_mask=snake_case__ ) UpperCAmelCase : Dict =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) @require_flax class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Tuple = (FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else () __lowerCamelCase : Optional[Any] = (FlaxGPTJForCausalLM,) if is_flax_available() else () def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =FlaxGPTJModelTester(self ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Dict =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward_with_attn_mask( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) @tooslow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =GPTaTokenizer.from_pretrained('''gpt2''' , pad_token='''<|endoftext|>''' , padding_side='''left''' ) UpperCAmelCase : Optional[Any] =tokenizer(['''Hello this is a long string''', '''Hey'''] , return_tensors='''np''' , padding=snake_case__ , truncation=snake_case__ ) UpperCAmelCase : Optional[int] =FlaxGPTJForCausalLM.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : str =False UpperCAmelCase : Union[str, Any] =model.config.eos_token_id UpperCAmelCase : List[Any] =jax.jit(model.generate ) UpperCAmelCase : Dict =jit_generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , pad_token_id=tokenizer.pad_token_id ).sequences UpperCAmelCase : Any =tokenizer.batch_decode(snake_case__ , skip_special_tokens=snake_case__ ) UpperCAmelCase : Tuple =[ '''Hello this is a long string of text.\n\nI\'m trying to get the text of the''', '''Hey, I\'m a little late to the party. I\'m going to''', ] self.assertListEqual(snake_case__ , snake_case__ ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : List[str] =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : Any =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : Any =getattr(snake_case__ , snake_case__ ) UpperCAmelCase , UpperCAmelCase : Union[str, Any] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Tuple =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : int =0 UpperCAmelCase : Optional[int] =1 UpperCAmelCase : Optional[int] =0 UpperCAmelCase : Union[str, Any] =1 UpperCAmelCase : List[str] =pt_model_class(snake_case__ ).eval() UpperCAmelCase : Optional[int] =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Any =convert_pytorch_state_dict_to_flax(pt_model.state_dict() , snake_case__ ) UpperCAmelCase : Union[str, Any] =fx_state with torch.no_grad(): UpperCAmelCase : Any =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : Dict =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(snake_case__ ) UpperCAmelCase : str =model_class.from_pretrained(snake_case__ , from_pt=snake_case__ ) UpperCAmelCase : int =fx_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output_loaded, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output_loaded[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : int =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : int =getattr(snake_case__ , snake_case__ ) UpperCAmelCase : Dict =pt_model_class(snake_case__ ).eval() UpperCAmelCase : str =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Optional[Any] =load_flax_weights_in_pytorch_model(snake_case__ , fx_model.params ) UpperCAmelCase , UpperCAmelCase : Optional[int] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Optional[int] =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : str =0 UpperCAmelCase : Any =1 UpperCAmelCase : List[Any] =0 UpperCAmelCase : Tuple =1 # make sure weights are tied in PyTorch pt_model.tie_weights() with torch.no_grad(): UpperCAmelCase : Optional[Any] =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : List[Any] =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(snake_case__ ) UpperCAmelCase : Tuple =pt_model_class.from_pretrained(snake_case__ , from_flax=snake_case__ ) with torch.no_grad(): UpperCAmelCase : Any =pt_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @tooslow def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase : str =model_class_name.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : Tuple =model(np.ones((1, 1) ) ) self.assertIsNotNone(snake_case__ )
348
import os from typing import Dict, List, Tuple, TypeVar, Union __snake_case = TypeVar('''T''') __snake_case = Union[List[T], Tuple[T, ...]] __snake_case = Union[T, List[T], Dict[str, T]] __snake_case = Union[str, bytes, os.PathLike]
348
1
from typing import Tuple, Union from ...modeling_outputs import BackboneOutput from ...modeling_utils import PreTrainedModel from ...utils import is_timm_available, is_torch_available, requires_backends from ...utils.backbone_utils import BackboneMixin from .configuration_timm_backbone import TimmBackboneConfig if is_timm_available(): import timm if is_torch_available(): from torch import Tensor class __snake_case ( lowerCamelCase__ , lowerCamelCase__ ): __lowerCamelCase : str = """pixel_values""" __lowerCamelCase : Optional[Any] = False __lowerCamelCase : Optional[Any] = TimmBackboneConfig def __init__( self , snake_case__ , **snake_case__ ) -> int: '''simple docstring''' requires_backends(self , '''timm''' ) super().__init__(snake_case__ ) UpperCAmelCase : Union[str, Any] =config if config.backbone is None: raise ValueError('''backbone is not set in the config. Please set it to a timm model name.''' ) if config.backbone not in timm.list_models(): raise ValueError(f'''backbone {config.backbone} is not supported by timm.''' ) if hasattr(snake_case__ , '''out_features''' ) and config.out_features is not None: raise ValueError('''out_features is not supported by TimmBackbone. Please use out_indices instead.''' ) UpperCAmelCase : Optional[Any] =getattr(snake_case__ , '''use_pretrained_backbone''' , snake_case__ ) if pretrained is None: raise ValueError('''use_pretrained_backbone is not set in the config. Please set it to True or False.''' ) # We just take the final layer by default. This matches the default for the transformers models. UpperCAmelCase : Tuple =config.out_indices if getattr(snake_case__ , '''out_indices''' , snake_case__ ) is not None else (-1,) UpperCAmelCase : Optional[int] =timm.create_model( config.backbone , pretrained=snake_case__ , features_only=config.features_only , in_chans=config.num_channels , out_indices=snake_case__ , **snake_case__ , ) # These are used to control the output of the model when called. If output_hidden_states is True, then # return_layers is modified to include all layers. UpperCAmelCase : Any =self._backbone.return_layers UpperCAmelCase : Any ={layer['''module''']: str(snake_case__ ) for i, layer in enumerate(self._backbone.feature_info.info )} super()._init_backbone(snake_case__ ) @classmethod def UpperCAmelCase__ ( cls , snake_case__ , *snake_case__ , **snake_case__ ) -> int: '''simple docstring''' requires_backends(cls , ['''vision''', '''timm'''] ) from ...models.timm_backbone import TimmBackboneConfig UpperCAmelCase : int =kwargs.pop('''config''' , TimmBackboneConfig() ) UpperCAmelCase : Union[str, Any] =kwargs.pop('''use_timm_backbone''' , snake_case__ ) if not use_timm: raise ValueError('''use_timm_backbone must be True for timm backbones''' ) UpperCAmelCase : Optional[int] =kwargs.pop('''num_channels''' , config.num_channels ) UpperCAmelCase : Union[str, Any] =kwargs.pop('''features_only''' , config.features_only ) UpperCAmelCase : Union[str, Any] =kwargs.pop('''use_pretrained_backbone''' , config.use_pretrained_backbone ) UpperCAmelCase : Any =kwargs.pop('''out_indices''' , config.out_indices ) UpperCAmelCase : List[Any] =TimmBackboneConfig( backbone=snake_case__ , num_channels=snake_case__ , features_only=snake_case__ , use_pretrained_backbone=snake_case__ , out_indices=snake_case__ , ) return super()._from_config(snake_case__ , **snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ ) -> str: '''simple docstring''' pass def UpperCAmelCase__ ( self , snake_case__ , snake_case__=None , snake_case__=None , snake_case__=None , **snake_case__ ) -> Union[BackboneOutput, Tuple[Tensor, ...]]: '''simple docstring''' UpperCAmelCase : List[Any] =return_dict if return_dict is not None else self.config.use_return_dict UpperCAmelCase : List[str] =( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCAmelCase : str =output_attentions if output_attentions is not None else self.config.output_attentions if output_attentions: raise ValueError('''Cannot output attentions for timm backbones at the moment''' ) if output_hidden_states: # We modify the return layers to include all the stages of the backbone UpperCAmelCase : Tuple =self._all_layers UpperCAmelCase : int =self._backbone(snake_case__ , **snake_case__ ) UpperCAmelCase : Optional[Any] =self._return_layers UpperCAmelCase : Optional[Any] =tuple(hidden_states[i] for i in self.out_indices ) else: UpperCAmelCase : Tuple =self._backbone(snake_case__ , **snake_case__ ) UpperCAmelCase : Dict =None UpperCAmelCase : int =tuple(snake_case__ ) UpperCAmelCase : Tuple =tuple(snake_case__ ) if hidden_states is not None else None if not return_dict: UpperCAmelCase : Dict =(feature_maps,) if output_hidden_states: UpperCAmelCase : Dict =output + (hidden_states,) return output return BackboneOutput(feature_maps=snake_case__ , hidden_states=snake_case__ , attentions=snake_case__ )
348
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_big_bird import BigBirdTokenizer else: __snake_case = None __snake_case = logging.get_logger(__name__) __snake_case = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} __snake_case = { '''vocab_file''': { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model''', '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model''' ), }, '''tokenizer_file''': { '''google/bigbird-roberta-base''': ( '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/tokenizer.json''' ), '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/tokenizer.json''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/tokenizer.json''' ), }, } __snake_case = { '''google/bigbird-roberta-base''': 40_96, '''google/bigbird-roberta-large''': 40_96, '''google/bigbird-base-trivia-itc''': 40_96, } __snake_case = '''▁''' class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Dict = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = BigBirdTokenizer __lowerCamelCase : Any = ["""input_ids""", """attention_mask"""] __lowerCamelCase : List[int] = [] def __init__( self , snake_case__=None , snake_case__=None , snake_case__="<unk>" , snake_case__="<s>" , snake_case__="</s>" , snake_case__="<pad>" , snake_case__="[SEP]" , snake_case__="[MASK]" , snake_case__="[CLS]" , **snake_case__ , ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else bos_token UpperCAmelCase : Optional[int] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else eos_token UpperCAmelCase : List[str] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else unk_token UpperCAmelCase : Union[str, Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else pad_token UpperCAmelCase : int =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else cls_token UpperCAmelCase : str =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else sep_token # Mask token behave like a normal word, i.e. include the space before it UpperCAmelCase : List[Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else mask_token super().__init__( snake_case__ , tokenizer_file=snake_case__ , bos_token=snake_case__ , eos_token=snake_case__ , unk_token=snake_case__ , sep_token=snake_case__ , pad_token=snake_case__ , cls_token=snake_case__ , mask_token=snake_case__ , **snake_case__ , ) UpperCAmelCase : Tuple =vocab_file UpperCAmelCase : Optional[int] =False if not self.vocab_file else True def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : int =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None , snake_case__ = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is None: return [1] + ([0] * len(snake_case__ )) + [1] return [1] + ([0] * len(snake_case__ )) + [1] + ([0] * len(snake_case__ )) + [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : Optional[Any] =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> Tuple[str]: '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(snake_case__ ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCAmelCase : Optional[int] =os.path.join( snake_case__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case__ ): copyfile(self.vocab_file , snake_case__ ) return (out_vocab_file,)
348
1
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline else: from .pipeline_kandinsky import KandinskyPipeline from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput from .text_encoder import MultilingualCLIP
348
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def lowerCAmelCase_ ( __lowerCAmelCase )-> Optional[Any]: '''simple docstring''' def is_in_circle(__lowerCAmelCase , __lowerCAmelCase ) -> bool: UpperCAmelCase : List[Any] =sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle UpperCAmelCase : List[Any] =mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(__lowerCAmelCase ) ) # The ratio of the area for circle to square is pi/4. UpperCAmelCase : Dict =proportion * 4 print(f'''The estimated value of pi is {pi_estimate}''' ) print(f'''The numpy value of pi is {pi}''' ) print(f'''The total error is {abs(pi - pi_estimate )}''' ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 , )-> float: '''simple docstring''' return mean( function_to_integrate(uniform(__lowerCAmelCase , __lowerCAmelCase ) ) for _ in range(__lowerCAmelCase ) ) * (max_value - min_value) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 )-> None: '''simple docstring''' def identity_function(__lowerCAmelCase ) -> float: return x UpperCAmelCase : List[Any] =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase : Dict =(max_value * max_value - min_value * min_value) / 2 print('''******************''' ) print(f'''Estimating area under y=x where x varies from {min_value} to {max_value}''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {expected_value}''' ) print(f'''Total error is {abs(estimated_value - expected_value )}''' ) print('''******************''' ) def lowerCAmelCase_ ( __lowerCAmelCase )-> None: '''simple docstring''' def function_to_integrate(__lowerCAmelCase ) -> float: return sqrt(4.0 - x * x ) UpperCAmelCase : Dict =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , 0.0 , 2.0 ) print('''******************''' ) print('''Estimating pi using area_under_curve_estimator''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {pi}''' ) print(f'''Total error is {abs(estimated_value - pi )}''' ) print('''******************''' ) if __name__ == "__main__": import doctest doctest.testmod()
348
1
def lowerCAmelCase_ ( __lowerCAmelCase )-> list: '''simple docstring''' UpperCAmelCase : List[Any] =int(__lowerCAmelCase ) if n_element < 1: UpperCAmelCase : Optional[Any] =ValueError('''a should be a positive number''' ) raise my_error UpperCAmelCase : Optional[Any] =[1] UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[Any] =(0, 0, 0) UpperCAmelCase : Any =1 while index < n_element: while hamming_list[i] * 2 <= hamming_list[-1]: i += 1 while hamming_list[j] * 3 <= hamming_list[-1]: j += 1 while hamming_list[k] * 5 <= hamming_list[-1]: k += 1 hamming_list.append( min(hamming_list[i] * 2 , hamming_list[j] * 3 , hamming_list[k] * 5 ) ) index += 1 return hamming_list if __name__ == "__main__": __snake_case = input('''Enter the last number (nth term) of the Hamming Number Series: ''') print('''Formula of Hamming Number Series => 2^i * 3^j * 5^k''') __snake_case = hamming(int(n)) print('''-----------------------------------------------------''') print(f'The list with nth numbers is: {hamming_numbers}') print('''-----------------------------------------------------''')
348
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __snake_case : def __init__( self , snake_case__ , snake_case__=12 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , snake_case__=0 , snake_case__=None , ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : List[Any] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Union[str, Any] =use_input_mask UpperCAmelCase : Tuple =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : Tuple =hidden_size UpperCAmelCase : Dict =projection_dim UpperCAmelCase : Optional[int] =num_hidden_layers UpperCAmelCase : Dict =num_attention_heads UpperCAmelCase : int =intermediate_size UpperCAmelCase : Any =dropout UpperCAmelCase : Union[str, Any] =attention_dropout UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : str =scope UpperCAmelCase : str =bos_token_id def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : int =None if self.use_input_mask: UpperCAmelCase : Union[str, Any] =random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: UpperCAmelCase : Optional[int] =input_mask.numpy() UpperCAmelCase , UpperCAmelCase : List[Any] =input_mask.shape UpperCAmelCase : Optional[Any] =np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : List[Any] =1 UpperCAmelCase : Tuple =0 UpperCAmelCase : List[Any] =self.get_config() return config, input_ids, tf.convert_to_tensor(snake_case__ ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =TFBlipTextModel(config=snake_case__ ) UpperCAmelCase : List[Any] =model(snake_case__ , attention_mask=snake_case__ , training=snake_case__ ) UpperCAmelCase : str =model(snake_case__ , training=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[Any] =config_and_inputs UpperCAmelCase : Optional[int] ={'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __snake_case ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Optional[int] = (TFBlipTextModel,) if is_tf_available() else () __lowerCamelCase : Dict = False __lowerCamelCase : Optional[Any] = False __lowerCamelCase : Dict = False def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : str =BlipTextModelTester(self ) UpperCAmelCase : Optional[int] =ConfigTester(self , config_class=snake_case__ , hidden_size=37 ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip(reason='''Blip does not use inputs_embeds''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @slow def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase : Optional[Any] =TFBlipTextModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def UpperCAmelCase__ ( self , snake_case__=True ) -> Any: '''simple docstring''' super().test_pt_tf_model_equivalence(allow_missing_keys=snake_case__ )
348
1
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 __snake_case ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : int = """hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline""" def UpperCAmelCase__ ( self , snake_case__=0 ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =floats_tensor((1, 3, 128, 128) , rng=random.Random(snake_case__ ) ) UpperCAmelCase : Any =np.random.RandomState(snake_case__ ) UpperCAmelCase : 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 UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : List[str] =OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Dict =self.get_dummy_inputs() UpperCAmelCase : Any =pipe(**snake_case__ ).images UpperCAmelCase : Tuple =image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 128, 128, 3) UpperCAmelCase : 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 UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : Optional[Any] =OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) UpperCAmelCase : Any =PNDMScheduler.from_config(pipe.scheduler.config , skip_prk_steps=snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Optional[Any] =self.get_dummy_inputs() UpperCAmelCase : List[Any] =pipe(**snake_case__ ).images UpperCAmelCase : List[Any] =image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) UpperCAmelCase : 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 UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) UpperCAmelCase : Union[str, Any] =LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=snake_case__ ) # warmup pass to apply optimizations UpperCAmelCase : Tuple =pipe(**self.get_dummy_inputs() ) UpperCAmelCase : List[Any] =self.get_dummy_inputs() UpperCAmelCase : Union[str, Any] =pipe(**snake_case__ ).images UpperCAmelCase : str =image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) UpperCAmelCase : List[str] =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 UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' UpperCAmelCase : Any =OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) UpperCAmelCase : str =EulerDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : List[Any] =self.get_dummy_inputs() UpperCAmelCase : Dict =pipe(**snake_case__ ).images UpperCAmelCase : Optional[Any] =image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) UpperCAmelCase : 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 UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : str =OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) UpperCAmelCase : str =EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : int =self.get_dummy_inputs() UpperCAmelCase : List[Any] =pipe(**snake_case__ ).images UpperCAmelCase : Dict =image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) UpperCAmelCase : 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 UpperCAmelCase__ ( self ) -> str: '''simple docstring''' UpperCAmelCase : str =OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) UpperCAmelCase : Dict =DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : str =self.get_dummy_inputs() UpperCAmelCase : Any =pipe(**snake_case__ ).images UpperCAmelCase : Dict =image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) UpperCAmelCase : Tuple =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 __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : str =ort.SessionOptions() UpperCAmelCase : Any =False return options def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/img2img/sketch-mountains-input.jpg''' ) UpperCAmelCase : Tuple =init_image.resize((768, 512) ) # using the PNDM scheduler by default UpperCAmelCase : Any =OnnxStableDiffusionImgaImgPipeline.from_pretrained( '''CompVis/stable-diffusion-v1-4''' , revision='''onnx''' , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : str ='''A fantasy landscape, trending on artstation''' UpperCAmelCase : List[str] =np.random.RandomState(0 ) UpperCAmelCase : Optional[Any] =pipe( prompt=snake_case__ , image=snake_case__ , strength=0.75 , guidance_scale=7.5 , num_inference_steps=10 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Optional[Any] =output.images UpperCAmelCase : List[str] =images[0, 255:258, 383:386, -1] assert images.shape == (1, 512, 768, 3) UpperCAmelCase : Any =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 UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : int =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/img2img/sketch-mountains-input.jpg''' ) UpperCAmelCase : List[Any] =init_image.resize((768, 512) ) UpperCAmelCase : Any =LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-v1-5''' , subfolder='''scheduler''' , revision='''onnx''' ) UpperCAmelCase : Union[str, Any] =OnnxStableDiffusionImgaImgPipeline.from_pretrained( '''runwayml/stable-diffusion-v1-5''' , revision='''onnx''' , scheduler=snake_case__ , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Dict ='''A fantasy landscape, trending on artstation''' UpperCAmelCase : Tuple =np.random.RandomState(0 ) UpperCAmelCase : str =pipe( prompt=snake_case__ , image=snake_case__ , strength=0.75 , guidance_scale=7.5 , num_inference_steps=20 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Optional[Any] =output.images UpperCAmelCase : Optional[Any] =images[0, 255:258, 383:386, -1] assert images.shape == (1, 512, 768, 3) UpperCAmelCase : List[str] =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
348
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging __snake_case = logging.get_logger(__name__) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' UpperCAmelCase : Dict =nn.functional.normalize(__lowerCAmelCase ) UpperCAmelCase : Tuple =nn.functional.normalize(__lowerCAmelCase ) return torch.mm(__lowerCAmelCase , normalized_text_embeds.t() ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : List[str] = CLIPConfig __lowerCamelCase : List[Any] = ["""CLIPEncoderLayer"""] def __init__( self , snake_case__ ) -> Dict: '''simple docstring''' super().__init__(snake_case__ ) UpperCAmelCase : Dict =CLIPVisionModel(config.vision_config ) UpperCAmelCase : Optional[Any] =nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=snake_case__ ) UpperCAmelCase : int =nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : List[str] =nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : str =nn.Parameter(torch.ones(17 ) , requires_grad=snake_case__ ) UpperCAmelCase : Optional[int] =nn.Parameter(torch.ones(3 ) , requires_grad=snake_case__ ) @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Union[str, Any] =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : Optional[Any] =self.visual_projection(snake_case__ ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 UpperCAmelCase : List[str] =cosine_distance(snake_case__ , self.special_care_embeds ).cpu().float().numpy() UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ).cpu().float().numpy() UpperCAmelCase : Tuple =[] UpperCAmelCase : Dict =image_embeds.shape[0] for i in range(snake_case__ ): UpperCAmelCase : str ={'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : str =0.0 for concept_idx in range(len(special_cos_dist[0] ) ): UpperCAmelCase : Optional[Any] =special_cos_dist[i][concept_idx] UpperCAmelCase : Union[str, Any] =self.special_care_embeds_weights[concept_idx].item() UpperCAmelCase : str =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} ) UpperCAmelCase : int =0.01 for concept_idx in range(len(cos_dist[0] ) ): UpperCAmelCase : Any =cos_dist[i][concept_idx] UpperCAmelCase : Optional[int] =self.concept_embeds_weights[concept_idx].item() UpperCAmelCase : int =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(snake_case__ ) result.append(snake_case__ ) UpperCAmelCase : Optional[int] =[len(res['''bad_concepts'''] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Any =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : List[str] =self.visual_projection(snake_case__ ) UpperCAmelCase : Any =cosine_distance(snake_case__ , self.special_care_embeds ) UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : Optional[Any] =0.0 UpperCAmelCase : Any =special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) UpperCAmelCase : str =torch.any(special_scores > 0 , dim=1 ) UpperCAmelCase : List[Any] =special_care * 0.01 UpperCAmelCase : Union[str, Any] =special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) UpperCAmelCase : List[Any] =(cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) UpperCAmelCase : str =torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
348
1
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 __snake_case ( lowerCamelCase__ ): __lowerCamelCase : List[Any] = """char""" __lowerCamelCase : Union[str, Any] = """bpe""" __lowerCamelCase : Optional[int] = """wp""" __snake_case = (DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[Any] = ["""image_processor""", """char_tokenizer"""] __lowerCamelCase : Union[str, Any] = """ViTImageProcessor""" __lowerCamelCase : Optional[Any] = """MgpstrTokenizer""" def __init__( self , snake_case__=None , snake_case__=None , **snake_case__ ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Optional[Any] =None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , snake_case__ , ) UpperCAmelCase : List[str] =kwargs.pop('''feature_extractor''' ) UpperCAmelCase : List[str] =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`.''' ) UpperCAmelCase : List[str] =tokenizer UpperCAmelCase : Any =AutoTokenizer.from_pretrained('''gpt2''' ) UpperCAmelCase : Tuple =AutoTokenizer.from_pretrained('''bert-base-uncased''' ) super().__init__(snake_case__ , snake_case__ ) def __call__( self , snake_case__=None , snake_case__=None , snake_case__=None , **snake_case__ ) -> List[str]: '''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: UpperCAmelCase : Optional[int] =self.image_processor(snake_case__ , return_tensors=snake_case__ , **snake_case__ ) if text is not None: UpperCAmelCase : str =self.char_tokenizer(snake_case__ , return_tensors=snake_case__ , **snake_case__ ) if text is None: return inputs elif images is None: return encodings else: UpperCAmelCase : str =encodings['''input_ids'''] return inputs def UpperCAmelCase__ ( self , snake_case__ ) -> int: '''simple docstring''' UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Optional[int] =sequences UpperCAmelCase : Union[str, Any] =char_preds.size(0 ) UpperCAmelCase , UpperCAmelCase : Union[str, Any] =self._decode_helper(snake_case__ , '''char''' ) UpperCAmelCase , UpperCAmelCase : List[str] =self._decode_helper(snake_case__ , '''bpe''' ) UpperCAmelCase , UpperCAmelCase : Dict =self._decode_helper(snake_case__ , '''wp''' ) UpperCAmelCase : Tuple =[] UpperCAmelCase : Dict =[] for i in range(snake_case__ ): UpperCAmelCase : str =[char_scores[i], bpe_scores[i], wp_scores[i]] UpperCAmelCase : int =[char_strs[i], bpe_strs[i], wp_strs[i]] UpperCAmelCase : int =scores.index(max(snake_case__ ) ) final_strs.append(strs[max_score_index] ) final_scores.append(scores[max_score_index] ) UpperCAmelCase : Any ={} UpperCAmelCase : List[str] =final_strs UpperCAmelCase : List[str] =final_scores UpperCAmelCase : List[str] =char_strs UpperCAmelCase : str =bpe_strs UpperCAmelCase : int =wp_strs return out def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Any: '''simple docstring''' if format == DecodeType.CHARACTER: UpperCAmelCase : Dict =self.char_decode UpperCAmelCase : Optional[Any] =1 UpperCAmelCase : Union[str, Any] ='''[s]''' elif format == DecodeType.BPE: UpperCAmelCase : List[Any] =self.bpe_decode UpperCAmelCase : List[str] =2 UpperCAmelCase : Dict ='''#''' elif format == DecodeType.WORDPIECE: UpperCAmelCase : str =self.wp_decode UpperCAmelCase : Any =102 UpperCAmelCase : Optional[Any] ='''[SEP]''' else: raise ValueError(f'''Format {format} is not supported.''' ) UpperCAmelCase , UpperCAmelCase : Optional[Any] =[], [] UpperCAmelCase : Optional[Any] =pred_logits.size(0 ) UpperCAmelCase : Optional[int] =pred_logits.size(1 ) UpperCAmelCase , UpperCAmelCase : Tuple =pred_logits.topk(1 , dim=-1 , largest=snake_case__ , sorted=snake_case__ ) UpperCAmelCase : str =preds_index.view(-1 , snake_case__ )[:, 1:] UpperCAmelCase : Optional[Any] =decoder(snake_case__ ) UpperCAmelCase , UpperCAmelCase : List[Any] =torch.nn.functional.softmax(snake_case__ , dim=2 ).max(dim=2 ) UpperCAmelCase : Optional[int] =preds_max_prob[:, 1:] for index in range(snake_case__ ): UpperCAmelCase : List[str] =preds_str[index].find(snake_case__ ) UpperCAmelCase : Any =preds_str[index][:pred_eos] UpperCAmelCase : List[str] =preds_index[index].cpu().tolist() UpperCAmelCase : Optional[Any] =pred_index.index(snake_case__ ) if eos_token in pred_index else -1 UpperCAmelCase : Any =preds_max_prob[index][: pred_eos_index + 1] UpperCAmelCase : List[str] =pred_max_prob.cumprod(dim=0 )[-1] if pred_max_prob.nelement() != 0 else 0.0 dec_strs.append(snake_case__ ) conf_scores.append(snake_case__ ) return dec_strs, conf_scores def UpperCAmelCase__ ( self , snake_case__ ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Any =[seq.replace(''' ''' , '''''' ) for seq in self.char_tokenizer.batch_decode(snake_case__ )] return decode_strs def UpperCAmelCase__ ( self , snake_case__ ) -> List[str]: '''simple docstring''' return self.bpe_tokenizer.batch_decode(snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Tuple =[seq.replace(''' ''' , '''''' ) for seq in self.wp_tokenizer.batch_decode(snake_case__ )] return decode_strs
348
import argparse import intel_extension_for_pytorch as ipex import torch from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline __snake_case = argparse.ArgumentParser('''Stable Diffusion script with intel optimization''', add_help=False) parser.add_argument('''--dpm''', action='''store_true''', help='''Enable DPMSolver or not''') parser.add_argument('''--steps''', default=None, type=int, help='''Num inference steps''') __snake_case = parser.parse_args() __snake_case = '''cpu''' __snake_case = '''a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings''' __snake_case = '''path-to-your-trained-model''' __snake_case = StableDiffusionPipeline.from_pretrained(model_id) if args.dpm: __snake_case = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) __snake_case = pipe.to(device) # to channels last __snake_case = pipe.unet.to(memory_format=torch.channels_last) __snake_case = pipe.vae.to(memory_format=torch.channels_last) __snake_case = pipe.text_encoder.to(memory_format=torch.channels_last) if pipe.requires_safety_checker: __snake_case = pipe.safety_checker.to(memory_format=torch.channels_last) # optimize with ipex __snake_case = torch.randn(2, 4, 64, 64) __snake_case = torch.rand(1) * 9_99 __snake_case = torch.randn(2, 77, 7_68) __snake_case = (sample, timestep, encoder_hidden_status) try: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example) except Exception: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True) if pipe.requires_safety_checker: __snake_case = ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True) # compute __snake_case = 6_66 __snake_case = torch.Generator(device).manual_seed(seed) __snake_case = {'''generator''': generator} if args.steps is not None: __snake_case = args.steps with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa): __snake_case = pipe(prompt, **generate_kwargs).images[0] # save image image.save('''generated.png''')
348
1
import gc import random import tempfile import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.stable_diffusion_safe import StableDiffusionPipelineSafe as StableDiffusionPipeline from diffusers.utils import floats_tensor, nightly, torch_device from diffusers.utils.testing_utils import require_torch_gpu class __snake_case ( unittest.TestCase ): def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() @property def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : int =1 UpperCAmelCase : str =3 UpperCAmelCase : Tuple =(32, 32) UpperCAmelCase : List[str] =floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(snake_case__ ) return image @property def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase : Optional[Any] =UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) return model @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase : Optional[Any] =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 , ) return model @property def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase : List[Any] =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 , ) return CLIPTextModel(snake_case__ ) @property def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' def extract(*snake_case__ , **snake_case__ ): class __snake_case : def __init__( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : Dict =torch.ones([0] ) def UpperCAmelCase__ ( self , snake_case__ ) -> Any: '''simple docstring''' self.pixel_values.to(snake_case__ ) return self return Out() return extract def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : Tuple ='''cpu''' # ensure determinism for the device-dependent torch.Generator UpperCAmelCase : Union[str, Any] =self.dummy_cond_unet UpperCAmelCase : Optional[Any] =DDIMScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=snake_case__ , set_alpha_to_one=snake_case__ , ) UpperCAmelCase : List[Any] =self.dummy_vae UpperCAmelCase : Any =self.dummy_text_encoder UpperCAmelCase : Optional[int] =CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) # make sure here that pndm scheduler skips prk UpperCAmelCase : Union[str, Any] =StableDiffusionPipeline( unet=snake_case__ , scheduler=snake_case__ , vae=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , safety_checker=snake_case__ , feature_extractor=self.dummy_extractor , ) UpperCAmelCase : int =sd_pipe.to(snake_case__ ) sd_pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Dict ='''A painting of a squirrel eating a burger''' UpperCAmelCase : Optional[Any] =torch.Generator(device=snake_case__ ).manual_seed(0 ) UpperCAmelCase : Optional[Any] =sd_pipe([prompt] , generator=snake_case__ , guidance_scale=6.0 , num_inference_steps=2 , output_type='''np''' ) UpperCAmelCase : Dict =output.images UpperCAmelCase : int =torch.Generator(device=snake_case__ ).manual_seed(0 ) UpperCAmelCase : Any =sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=6.0 , num_inference_steps=2 , output_type='''np''' , return_dict=snake_case__ , )[0] UpperCAmelCase : Optional[Any] =image[0, -3:, -3:, -1] UpperCAmelCase : Any =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) UpperCAmelCase : Optional[Any] =np.array([0.5756, 0.6118, 0.5005, 0.5041, 0.5471, 0.4726, 0.4976, 0.4865, 0.4864] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] ='''cpu''' # ensure determinism for the device-dependent torch.Generator UpperCAmelCase : Tuple =self.dummy_cond_unet UpperCAmelCase : Any =PNDMScheduler(skip_prk_steps=snake_case__ ) UpperCAmelCase : List[str] =self.dummy_vae UpperCAmelCase : Dict =self.dummy_text_encoder UpperCAmelCase : Any =CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) # make sure here that pndm scheduler skips prk UpperCAmelCase : Optional[Any] =StableDiffusionPipeline( unet=snake_case__ , scheduler=snake_case__ , vae=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , safety_checker=snake_case__ , feature_extractor=self.dummy_extractor , ) UpperCAmelCase : List[str] =sd_pipe.to(snake_case__ ) sd_pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Union[str, Any] ='''A painting of a squirrel eating a burger''' UpperCAmelCase : List[Any] =torch.Generator(device=snake_case__ ).manual_seed(0 ) UpperCAmelCase : Dict =sd_pipe([prompt] , generator=snake_case__ , guidance_scale=6.0 , num_inference_steps=2 , output_type='''np''' ) UpperCAmelCase : List[str] =output.images UpperCAmelCase : Dict =torch.Generator(device=snake_case__ ).manual_seed(0 ) UpperCAmelCase : Optional[int] =sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=6.0 , num_inference_steps=2 , output_type='''np''' , return_dict=snake_case__ , )[0] UpperCAmelCase : Dict =image[0, -3:, -3:, -1] UpperCAmelCase : str =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) UpperCAmelCase : Tuple =np.array([0.5125, 0.5716, 0.4828, 0.5060, 0.5650, 0.4768, 0.5185, 0.4895, 0.4993] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : int =StableDiffusionPipeline.from_pretrained( '''hf-internal-testing/tiny-stable-diffusion-lms-pipe''' , safety_checker=snake_case__ ) assert isinstance(snake_case__ , snake_case__ ) assert isinstance(pipe.scheduler , snake_case__ ) assert pipe.safety_checker is None UpperCAmelCase : Any =pipe('''example prompt''' , num_inference_steps=2 ).images[0] assert image is not None # check that there's no error when saving a pipeline with one of the models being None with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(snake_case__ ) UpperCAmelCase : List[Any] =StableDiffusionPipeline.from_pretrained(snake_case__ ) # sanity check that the pipeline still works assert pipe.safety_checker is None UpperCAmelCase : int =pipe('''example prompt''' , num_inference_steps=2 ).images[0] assert image is not None @unittest.skipIf(torch_device != '''cuda''' , '''This test requires a GPU''' ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Optional[Any] =self.dummy_cond_unet UpperCAmelCase : str =PNDMScheduler(skip_prk_steps=snake_case__ ) UpperCAmelCase : Tuple =self.dummy_vae UpperCAmelCase : Dict =self.dummy_text_encoder UpperCAmelCase : List[Any] =CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) # put models in fp16 UpperCAmelCase : Union[str, Any] =unet.half() UpperCAmelCase : Any =vae.half() UpperCAmelCase : List[str] =bert.half() # make sure here that pndm scheduler skips prk UpperCAmelCase : List[Any] =StableDiffusionPipeline( unet=snake_case__ , scheduler=snake_case__ , vae=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , safety_checker=snake_case__ , feature_extractor=self.dummy_extractor , ) UpperCAmelCase : Optional[int] =sd_pipe.to(snake_case__ ) sd_pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Tuple ='''A painting of a squirrel eating a burger''' UpperCAmelCase : Any =sd_pipe([prompt] , num_inference_steps=2 , output_type='''np''' ).images assert image.shape == (1, 64, 64, 3) @nightly @require_torch_gpu class __snake_case ( unittest.TestCase ): def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' UpperCAmelCase : List[Any] =StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5''' , safety_checker=snake_case__ ) UpperCAmelCase : Tuple =LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config ) UpperCAmelCase : int =sd_pipe.to(snake_case__ ) sd_pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Dict =( '''portrait of girl with smokey eyes makeup in abandoned hotel, grange clothes, redshift, wide high angle''' ''' coloured polaroid photograph with flash, kodak film, hyper real, stunning moody cinematography, with''' ''' anamorphic lenses, by maripol, fallen angels by wong kar - wai, style of suspiria and neon demon and''' ''' children from bahnhof zoo, detailed ''' ) UpperCAmelCase : str =40_0366_0346 UpperCAmelCase : Optional[Any] =7 # without safety guidance (sld_guidance_scale = 0) UpperCAmelCase : Tuple =torch.manual_seed(snake_case__ ) UpperCAmelCase : str =sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=snake_case__ , num_inference_steps=50 , output_type='''np''' , width=512 , height=512 , sld_guidance_scale=0 , ) UpperCAmelCase : Any =output.images UpperCAmelCase : Tuple =image[0, -3:, -3:, -1] UpperCAmelCase : List[str] =[0.2278, 0.2231, 0.2249, 0.2333, 0.2303, 0.1885, 0.2273, 0.2144, 0.2176] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 # without safety guidance (strong configuration) UpperCAmelCase : Optional[int] =torch.manual_seed(snake_case__ ) UpperCAmelCase : Union[str, Any] =sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=snake_case__ , num_inference_steps=50 , output_type='''np''' , width=512 , height=512 , sld_guidance_scale=2000 , sld_warmup_steps=7 , sld_threshold=0.025 , sld_momentum_scale=0.5 , sld_mom_beta=0.7 , ) UpperCAmelCase : Tuple =output.images UpperCAmelCase : List[Any] =image[0, -3:, -3:, -1] UpperCAmelCase : List[Any] =[0.2383, 0.2276, 0.236, 0.2192, 0.2186, 0.2053, 0.1971, 0.1901, 0.1719] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5''' , safety_checker=snake_case__ ) UpperCAmelCase : List[Any] =LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config ) UpperCAmelCase : Dict =sd_pipe.to(snake_case__ ) sd_pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Dict ='''padme amidala taking a bath artwork, safe for work, no nudity''' UpperCAmelCase : Any =27_3497_1755 UpperCAmelCase : List[str] =7 UpperCAmelCase : Union[str, Any] =torch.manual_seed(snake_case__ ) UpperCAmelCase : Any =sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=snake_case__ , num_inference_steps=50 , output_type='''np''' , width=512 , height=512 , sld_guidance_scale=0 , ) UpperCAmelCase : List[str] =output.images UpperCAmelCase : Optional[int] =image[0, -3:, -3:, -1] UpperCAmelCase : str =[0.3502, 0.3622, 0.3396, 0.3642, 0.3478, 0.3318, 0.35, 0.3348, 0.3297] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 UpperCAmelCase : Any =torch.manual_seed(snake_case__ ) UpperCAmelCase : str =sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=snake_case__ , num_inference_steps=50 , output_type='''np''' , width=512 , height=512 , sld_guidance_scale=2000 , sld_warmup_steps=7 , sld_threshold=0.025 , sld_momentum_scale=0.5 , sld_mom_beta=0.7 , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : Tuple =image[0, -3:, -3:, -1] UpperCAmelCase : List[Any] =[0.5531, 0.5206, 0.4895, 0.5156, 0.5182, 0.4751, 0.4802, 0.4803, 0.4443] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5''' ) UpperCAmelCase : str =sd_pipe.to(snake_case__ ) sd_pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Tuple =( '''the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c.''' ''' leyendecker''' ) UpperCAmelCase : Any =10_4435_5234 UpperCAmelCase : Union[str, Any] =12 UpperCAmelCase : Optional[int] =torch.manual_seed(snake_case__ ) UpperCAmelCase : List[str] =sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=snake_case__ , num_inference_steps=50 , output_type='''np''' , width=512 , height=512 , sld_guidance_scale=0 , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : List[str] =image[0, -3:, -3:, -1] UpperCAmelCase : Dict =np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ) assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-7 UpperCAmelCase : Tuple =torch.manual_seed(snake_case__ ) UpperCAmelCase : List[str] =sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=snake_case__ , num_inference_steps=50 , output_type='''np''' , width=512 , height=512 , sld_guidance_scale=2000 , sld_warmup_steps=7 , sld_threshold=0.025 , sld_momentum_scale=0.5 , sld_mom_beta=0.7 , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : Union[str, Any] =image[0, -3:, -3:, -1] UpperCAmelCase : Dict =np.array([0.5818, 0.6285, 0.6835, 0.6019, 0.625, 0.6754, 0.6096, 0.6334, 0.6561] ) assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
348
__snake_case = '''Input must be a string of 8 numbers plus letter''' __snake_case = '''TRWAGMYFPDXBNJZSQVHLCKE''' def lowerCAmelCase_ ( __lowerCAmelCase )-> bool: '''simple docstring''' if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCAmelCase : Optional[Any] =f'''Expected string as input, found {type(__lowerCAmelCase ).__name__}''' raise TypeError(__lowerCAmelCase ) UpperCAmelCase : List[Any] =spanish_id.replace('''-''' , '''''' ).upper() if len(__lowerCAmelCase ) != 9: raise ValueError(__lowerCAmelCase ) try: UpperCAmelCase : int =int(spanish_id_clean[0:8] ) UpperCAmelCase : Optional[int] =spanish_id_clean[8] except ValueError as ex: raise ValueError(__lowerCAmelCase ) from ex if letter.isdigit(): raise ValueError(__lowerCAmelCase ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
348
1
import tempfile import unittest from transformers import TaConfig, is_torch_available from transformers.testing_utils import ( require_sentencepiece, require_tokenizers, require_torch, slow, torch_device, ) from ...generation.test_utils import GenerationTesterMixin from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import AutoTokenizer, UMTaForConditionalGeneration, UMTaForQuestionAnswering, UMTaModel class __snake_case : def __init__( self , snake_case__ , snake_case__=99 , snake_case__=13 , snake_case__=7 , snake_case__=9 , snake_case__=True , snake_case__=True , snake_case__=False , snake_case__=32 , snake_case__=5 , snake_case__=4 , snake_case__=37 , snake_case__=8 , snake_case__=0.1 , snake_case__=0.002 , snake_case__=1 , snake_case__=0 , snake_case__=0 , snake_case__=None , snake_case__=None , ) -> List[str]: '''simple docstring''' UpperCAmelCase : int =parent UpperCAmelCase : Optional[Any] =batch_size UpperCAmelCase : Any =encoder_seq_length UpperCAmelCase : str =decoder_seq_length # For common tests UpperCAmelCase : Any =self.decoder_seq_length UpperCAmelCase : List[str] =is_training UpperCAmelCase : Union[str, Any] =use_attention_mask UpperCAmelCase : Optional[Any] =use_labels UpperCAmelCase : str =vocab_size UpperCAmelCase : List[str] =hidden_size UpperCAmelCase : List[Any] =num_hidden_layers UpperCAmelCase : Union[str, Any] =num_attention_heads UpperCAmelCase : Dict =d_ff UpperCAmelCase : Any =relative_attention_num_buckets UpperCAmelCase : Any =dropout_rate UpperCAmelCase : List[Any] =initializer_factor UpperCAmelCase : int =eos_token_id UpperCAmelCase : Optional[Any] =pad_token_id UpperCAmelCase : Union[str, Any] =decoder_start_token_id UpperCAmelCase : Optional[Any] =None UpperCAmelCase : List[str] =decoder_layers def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' return TaConfig.from_pretrained('''google/umt5-base''' ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__=None , ) -> List[str]: '''simple docstring''' if attention_mask is None: UpperCAmelCase : Optional[int] =input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: UpperCAmelCase : Union[str, Any] =decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: UpperCAmelCase : Any =torch.ones(config.num_hidden_layers , config.num_attention_heads , device=snake_case__ ) if decoder_head_mask is None: UpperCAmelCase : Any =torch.ones(config.num_decoder_layers , config.num_attention_heads , device=snake_case__ ) if cross_attn_head_mask is None: UpperCAmelCase : Tuple =torch.ones( config.num_decoder_layers , config.num_attention_heads , device=snake_case__ ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' UpperCAmelCase : Tuple =ids_tensor([self.batch_size, self.encoder_seq_length] , self.vocab_size ) UpperCAmelCase : List[Any] =ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for NllbMoe the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input UpperCAmelCase : Any =input_ids.clamp(self.pad_token_id + 1 ) UpperCAmelCase : List[Any] =decoder_input_ids.clamp(self.pad_token_id + 1 ) UpperCAmelCase : Tuple =self.get_config() UpperCAmelCase : Tuple =config.num_attention_heads UpperCAmelCase : List[Any] =self.prepare_inputs_dict(snake_case__ , snake_case__ , snake_case__ ) return config, input_dict def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Union[str, Any] =self.prepare_config_and_inputs() return config, inputs_dict def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' return TaConfig( vocab_size=166 , d_model=self.hidden_size , d_ff=self.d_ff , d_kv=self.hidden_size // self.num_attention_heads , num_layers=self.num_hidden_layers , num_decoder_layers=self.decoder_layers , num_heads=self.num_attention_heads , relative_attention_num_buckets=self.relative_attention_num_buckets , dropout_rate=self.dropout_rate , initializer_factor=self.initializer_factor , eos_token_id=self.eos_token_id , bos_token_id=self.pad_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' return TaConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , d_ff=self.d_ff , d_kv=self.hidden_size // self.num_attention_heads , num_layers=self.num_hidden_layers , num_decoder_layers=self.decoder_layers , num_heads=self.num_attention_heads , relative_attention_num_buckets=self.relative_attention_num_buckets , dropout_rate=self.dropout_rate , initializer_factor=self.initializer_factor , eos_token_id=self.eos_token_id , bos_token_id=self.pad_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> Tuple: '''simple docstring''' UpperCAmelCase : str =UMTaModel(config=snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase : str =model( input_ids=snake_case__ , decoder_input_ids=snake_case__ , attention_mask=snake_case__ , decoder_attention_mask=snake_case__ , ) UpperCAmelCase : int =model(input_ids=snake_case__ , decoder_input_ids=snake_case__ ) UpperCAmelCase : Optional[int] =result.last_hidden_state UpperCAmelCase : List[Any] =result.past_key_values UpperCAmelCase : int =result.encoder_last_hidden_state self.parent.assertEqual(encoder_output.size() , (self.batch_size, self.encoder_seq_length, self.hidden_size) ) self.parent.assertEqual(decoder_output.size() , (self.batch_size, self.decoder_seq_length, self.hidden_size) ) # There should be `num_layers` key value embeddings stored in decoder_past self.parent.assertEqual(len(snake_case__ ) , config.num_layers ) # There should be a self attn key, a self attn value, a cross attn key and a cross attn value stored in each decoder_past tuple self.parent.assertEqual(len(decoder_past[0] ) , 4 ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Tuple =UMTaModel(config=snake_case__ ).get_decoder().to(snake_case__ ).eval() # first forward pass UpperCAmelCase : Any =model(snake_case__ , use_cache=snake_case__ ) UpperCAmelCase : int =model(snake_case__ ) UpperCAmelCase : str =model(snake_case__ , use_cache=snake_case__ ) self.parent.assertTrue(len(snake_case__ ) == len(snake_case__ ) ) self.parent.assertTrue(len(snake_case__ ) == len(snake_case__ ) + 1 ) UpperCAmelCase , UpperCAmelCase : List[str] =outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase : str =ids_tensor((self.batch_size, 1) , config.vocab_size ) # append to next input_ids and UpperCAmelCase : Union[str, Any] =torch.cat([input_ids, next_tokens] , dim=-1 ) UpperCAmelCase : Any =model(snake_case__ )['''last_hidden_state'''] UpperCAmelCase : Any =model(snake_case__ , past_key_values=snake_case__ )['''last_hidden_state'''] # select random slice UpperCAmelCase : int =ids_tensor((1,) , output_from_past.shape[-1] ).item() UpperCAmelCase : Tuple =output_from_no_past[:, -1, random_slice_idx].detach() UpperCAmelCase : List[Any] =output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(snake_case__ , snake_case__ , atol=1e-3 ) ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , ) -> Tuple: '''simple docstring''' UpperCAmelCase : Union[str, Any] =UMTaModel(config=snake_case__ ).to(snake_case__ ).half().eval() UpperCAmelCase : Optional[Any] =model(**snake_case__ )['''last_hidden_state'''] self.parent.assertFalse(torch.isnan(snake_case__ ).any().item() ) @require_torch class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Dict = ( (UMTaModel, UMTaForConditionalGeneration, UMTaForQuestionAnswering) if is_torch_available() else () ) __lowerCamelCase : Any = (UMTaForConditionalGeneration,) if is_torch_available() else () __lowerCamelCase : Optional[Any] = ( { """conversational""": UMTaForConditionalGeneration, """feature-extraction""": UMTaModel, """summarization""": UMTaForConditionalGeneration, """text2text-generation""": UMTaForConditionalGeneration, """translation""": UMTaForConditionalGeneration, """question-answering""": UMTaForQuestionAnswering, } if is_torch_available() else {} ) __lowerCamelCase : Tuple = True __lowerCamelCase : str = False __lowerCamelCase : Dict = False __lowerCamelCase : str = True __lowerCamelCase : int = True # The small UMT5 model needs higher percentages for CPU/MP tests __lowerCamelCase : List[str] = [0.8, 0.9] def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : str =UMTaModelTester(self ) @unittest.skip('''Test has a segmentation fault on torch 1.8.0''' ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : str =self.model_tester.prepare_config_and_inputs() UpperCAmelCase : Union[str, Any] =UMTaModel(config_and_inputs[0] ).to(snake_case__ ) with tempfile.TemporaryDirectory() as tmpdirname: torch.onnx.export( snake_case__ , (config_and_inputs[1], config_and_inputs[3], config_and_inputs[2]) , f'''{tmpdirname}/t5_test.onnx''' , export_params=snake_case__ , opset_version=9 , input_names=['''input_ids''', '''decoder_input_ids'''] , ) @unittest.skipIf(torch_device == '''cpu''' , '''Cant do half precision''' ) def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Tuple =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model_fpaa_forward(*snake_case__ ) def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' UpperCAmelCase : Any =['''encoder_attentions''', '''decoder_attentions''', '''cross_attentions'''] UpperCAmelCase : List[Any] =self.model_tester.prepare_config_and_inputs() UpperCAmelCase : Any =config_and_inputs[0] UpperCAmelCase : Union[str, Any] =UMTaForConditionalGeneration(snake_case__ ).eval() model.to(snake_case__ ) UpperCAmelCase : List[Any] ={ '''head_mask''': torch.zeros(config.num_layers , config.num_heads , device=snake_case__ ), '''decoder_head_mask''': torch.zeros(config.num_decoder_layers , config.num_heads , device=snake_case__ ), '''cross_attn_head_mask''': torch.zeros(config.num_decoder_layers , config.num_heads , device=snake_case__ ), } for attn_name, (name, mask) in zip(snake_case__ , head_masking.items() ): UpperCAmelCase : Tuple ={name: mask} # Explicitly pass decoder_head_mask as it is required from T5 model when head_mask specified if name == "head_mask": UpperCAmelCase : Optional[int] =torch.ones( config.num_decoder_layers , config.num_heads , device=snake_case__ ) UpperCAmelCase : Optional[Any] =model.generate( config_and_inputs[1]['''input_ids'''] , num_beams=1 , max_length=3 , output_attentions=snake_case__ , return_dict_in_generate=snake_case__ , **snake_case__ , ) # We check the state of decoder_attentions and cross_attentions just from the last step UpperCAmelCase : List[Any] =out[attn_name] if attn_name == attention_names[0] else out[attn_name][-1] self.assertEqual(sum([w.sum().item() for w in attn_weights] ) , 0.0 ) @unittest.skip('''Does not work on the tiny model as we keep hitting edge cases.''' ) def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' pass @require_torch @require_sentencepiece @require_tokenizers class __snake_case ( unittest.TestCase ): @slow @unittest.skip( '''Unless we stop stripping left and right by default for all special tokens, the expected ids obtained here will not match the original ones. Wait for https://github.com/huggingface/transformers/pull/23909 to be merged''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : int =UMTaForConditionalGeneration.from_pretrained('''google/umt5-small''' , return_dict=snake_case__ ).to(snake_case__ ) UpperCAmelCase : int =AutoTokenizer.from_pretrained('''google/umt5-small''' , use_fast=snake_case__ , legacy=snake_case__ ) UpperCAmelCase : Tuple =[ '''Bonjour monsieur <extra_id_0> bien <extra_id_1>.''', '''No se como puedo <extra_id_0>.''', '''This is the reason why we <extra_id_0> them.''', '''The <extra_id_0> walks in <extra_id_1>, seats''', '''A <extra_id_0> walks into a bar and orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.''', ] UpperCAmelCase : Union[str, Any] =tokenizer(snake_case__ , return_tensors='''pt''' , padding=snake_case__ ).input_ids # fmt: off UpperCAmelCase : List[Any] =torch.tensor( [ [ 3_8530, 21_0703, 25_6299, 1410, 25_6298, 274, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 826, 321, 671, 2_5922, 25_6299, 274, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 1460, 339, 312, 1_9014, 1_0620, 758, 25_6299, 2355,274, 1, 0, 0, 0, 0, 0, 0,0, 0], [ 517, 25_6299, 1_4869, 281, 301, 25_6298, 275, 11_9983,1, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 320, 25_6299, 1_4869, 281, 2234, 289, 2275, 333,6_1391, 289, 25_6298, 543, 25_6297, 16_8714, 329, 25_6296,274, 1], ] ) # fmt: on torch.testing.assert_allclose(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =model.generate(input_ids.to(snake_case__ ) ) UpperCAmelCase : Any =[ '''<pad><extra_id_0> et<extra_id_1> [eod] <extra_id_2><extra_id_55>.. [eod] 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 <extra_id_56>ajšietosto<extra_id_56>lleux<extra_id_19><extra_id_6>ajšie</s>''', '''<pad><extra_id_0>.<extra_id_1>.,<0x0A>...spech <0x0A><extra_id_20> <extra_id_21></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', '''<pad><extra_id_0> are not going to be a part of the world. We are not going to be a part of<extra_id_1> and<extra_id_2><0x0A><extra_id_48>.<extra_id_48></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', '''<pad><extra_id_0> door<extra_id_1>, the door<extra_id_2> 피해[/</s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', '''<pad><extra_id_0>nyone who<extra_id_1> drink<extra_id_2> a<extra_id_3> alcohol<extra_id_4> A<extra_id_5> A. This<extra_id_6> I<extra_id_7><extra_id_52><extra_id_53></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', ] UpperCAmelCase : str =tokenizer.batch_decode(snake_case__ ) self.assertEqual(snake_case__ , snake_case__ )
348
def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Dict =str(bin(__lowerCAmelCase ) ) binary_number += "0" * shift_amount return binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Any =str(bin(__lowerCAmelCase ) )[2:] if shift_amount >= len(__lowerCAmelCase ): return "0b0" UpperCAmelCase : Optional[Any] =binary_number[: len(__lowerCAmelCase ) - shift_amount] return "0b" + shifted_binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number >= 0: # Get binary representation of positive number UpperCAmelCase : Optional[Any] ='''0''' + str(bin(__lowerCAmelCase ) ).strip('''-''' )[2:] else: # Get binary (2's complement) representation of negative number UpperCAmelCase : int =len(bin(__lowerCAmelCase )[3:] ) # Find 2's complement of number UpperCAmelCase : Any =bin(abs(__lowerCAmelCase ) - (1 << binary_number_length) )[3:] UpperCAmelCase : Optional[Any] =( '''1''' + '''0''' * (binary_number_length - len(__lowerCAmelCase )) + binary_number ) if shift_amount >= len(__lowerCAmelCase ): return "0b" + binary_number[0] * len(__lowerCAmelCase ) return ( "0b" + binary_number[0] * shift_amount + binary_number[: len(__lowerCAmelCase ) - shift_amount] ) if __name__ == "__main__": import doctest doctest.testmod()
348
1
from __future__ import annotations def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> list[str]: '''simple docstring''' if partitions <= 0: raise ValueError('''partitions must be a positive number!''' ) if partitions > number_of_bytes: raise ValueError('''partitions can not > number_of_bytes!''' ) UpperCAmelCase : Any =number_of_bytes // partitions UpperCAmelCase : List[Any] =[] for i in range(__lowerCAmelCase ): UpperCAmelCase : Dict =i * bytes_per_partition + 1 UpperCAmelCase : Tuple =( number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition ) allocation_list.append(f'''{start_bytes}-{end_bytes}''' ) return allocation_list if __name__ == "__main__": import doctest doctest.testmod()
348
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) # TODO Update this __snake_case = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Tuple = """esm""" def __init__( self , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=1026 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__="absolute" , snake_case__=True , snake_case__=None , snake_case__=False , snake_case__=False , snake_case__=None , snake_case__=None , **snake_case__ , ) -> Union[str, Any]: '''simple docstring''' super().__init__(pad_token_id=snake_case__ , mask_token_id=snake_case__ , **snake_case__ ) UpperCAmelCase : List[str] =vocab_size UpperCAmelCase : str =hidden_size UpperCAmelCase : List[Any] =num_hidden_layers UpperCAmelCase : Optional[Any] =num_attention_heads UpperCAmelCase : str =intermediate_size UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : int =attention_probs_dropout_prob UpperCAmelCase : Dict =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : Union[str, Any] =layer_norm_eps UpperCAmelCase : Dict =position_embedding_type UpperCAmelCase : Optional[Any] =use_cache UpperCAmelCase : int =emb_layer_norm_before UpperCAmelCase : List[str] =token_dropout UpperCAmelCase : Optional[Any] =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) UpperCAmelCase : Optional[Any] =EsmFoldConfig() elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =EsmFoldConfig(**snake_case__ ) UpperCAmelCase : Tuple =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) UpperCAmelCase : Any =get_default_vocab_list() else: UpperCAmelCase : Tuple =vocab_list else: UpperCAmelCase : Optional[int] =None UpperCAmelCase : Union[str, Any] =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , snake_case__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =super().to_dict() if isinstance(self.esmfold_config , snake_case__ ): UpperCAmelCase : str =self.esmfold_config.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : str = None __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : float = 0 __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : int = 128 __lowerCamelCase : "TrunkConfig" = None def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' if self.trunk is None: UpperCAmelCase : str =TrunkConfig() elif isinstance(self.trunk , snake_case__ ): UpperCAmelCase : Optional[int] =TrunkConfig(**self.trunk ) def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[Any] =asdict(self ) UpperCAmelCase : Any =self.trunk.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 48 __lowerCamelCase : int = 1024 __lowerCamelCase : int = 128 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : float = 0 __lowerCamelCase : float = 0 __lowerCamelCase : bool = False __lowerCamelCase : int = 4 __lowerCamelCase : Optional[int] = 128 __lowerCamelCase : "StructureModuleConfig" = None def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' if self.structure_module is None: UpperCAmelCase : Any =StructureModuleConfig() elif isinstance(self.structure_module , snake_case__ ): UpperCAmelCase : str =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f'''`max_recycles` should be positive, got {self.max_recycles}.''' ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f''' {self.sequence_state_dim} and {self.sequence_state_dim}.''' ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f''' {self.pairwise_state_dim} and {self.pairwise_state_dim}.''' ) UpperCAmelCase : Optional[int] =self.sequence_state_dim // self.sequence_head_width UpperCAmelCase : Any =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f''' {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.''' ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f''' {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.''' ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f'''`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.''' ) if self.dropout >= 0.4: raise ValueError(f'''`dropout` should not be greater than 0.4, got {self.dropout}.''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =asdict(self ) UpperCAmelCase : Tuple =self.structure_module.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 384 __lowerCamelCase : int = 128 __lowerCamelCase : int = 16 __lowerCamelCase : int = 128 __lowerCamelCase : int = 12 __lowerCamelCase : int = 4 __lowerCamelCase : int = 8 __lowerCamelCase : float = 0.1 __lowerCamelCase : int = 8 __lowerCamelCase : int = 1 __lowerCamelCase : int = 2 __lowerCamelCase : int = 7 __lowerCamelCase : int = 10 __lowerCamelCase : float = 1E-8 __lowerCamelCase : float = 1E5 def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return asdict(self ) def lowerCAmelCase_ ( )-> Tuple: '''simple docstring''' return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
348
1
import baseaa import io import json import os from copy import deepcopy from ..optimizer import AcceleratedOptimizer from ..scheduler import AcceleratedScheduler class __snake_case : def __init__( self , snake_case__ ) -> Union[str, Any]: '''simple docstring''' if isinstance(snake_case__ , snake_case__ ): # Don't modify user's data should they want to reuse it (e.g. in tests), because once we # modified it, it will not be accepted here again, since `auto` values would have been overridden UpperCAmelCase : Optional[Any] =deepcopy(snake_case__ ) elif os.path.exists(snake_case__ ): with io.open(snake_case__ , '''r''' , encoding='''utf-8''' ) as f: UpperCAmelCase : List[Any] =json.load(snake_case__ ) else: try: UpperCAmelCase : Tuple =baseaa.urlsafe_baadecode(snake_case__ ).decode('''utf-8''' ) UpperCAmelCase : Tuple =json.loads(snake_case__ ) except (UnicodeDecodeError, AttributeError, ValueError): raise ValueError( f'''Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}''' ) UpperCAmelCase : Union[str, Any] =config self.set_stage_and_offload() def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : str =self.get_value('''zero_optimization.stage''' , -1 ) # offload UpperCAmelCase : str =False if self.is_zeroa() or self.is_zeroa(): UpperCAmelCase : Dict =set(['''cpu''', '''nvme'''] ) UpperCAmelCase : str =set( [ self.get_value('''zero_optimization.offload_optimizer.device''' ), self.get_value('''zero_optimization.offload_param.device''' ), ] ) if len(offload_devices & offload_devices_valid ) > 0: UpperCAmelCase : List[Any] =True def UpperCAmelCase__ ( self , snake_case__ ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =self.config # find the config node of interest if it exists UpperCAmelCase : int =ds_key_long.split('''.''' ) UpperCAmelCase : int =nodes.pop() for node in nodes: UpperCAmelCase : Optional[int] =config.get(snake_case__ ) if config is None: return None, ds_key return config, ds_key def UpperCAmelCase__ ( self , snake_case__ , snake_case__=None ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : List[str] =self.find_config_node(snake_case__ ) if config is None: return default return config.get(snake_case__ , snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__=False ) -> List[Any]: '''simple docstring''' UpperCAmelCase : List[Any] =self.config # find the config node of interest if it exists UpperCAmelCase : Tuple =ds_key_long.split('''.''' ) for node in nodes: UpperCAmelCase : Dict =config UpperCAmelCase : Any =config.get(snake_case__ ) if config is None: if must_exist: raise ValueError(f'''Can\'t find {ds_key_long} entry in the config: {self.config}''' ) else: return # if found remove it if parent_config is not None: parent_config.pop(snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ ) -> str: '''simple docstring''' UpperCAmelCase : str =self.get_value(snake_case__ ) return False if value is None else bool(snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : List[str] =self.get_value(snake_case__ ) return False if value is None else not bool(snake_case__ ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' return self._stage == 2 def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return self._stage == 3 def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' return self._offload class __snake_case : def __init__( self , snake_case__ ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =engine def UpperCAmelCase__ ( self , snake_case__ , **snake_case__ ) -> Any: '''simple docstring''' self.engine.backward(snake_case__ , **snake_case__ ) # Deepspeed's `engine.step` performs the following operations: # - gradient accumulation check # - gradient clipping # - optimizer step # - zero grad # - checking overflow # - lr_scheduler step (only if engine.lr_scheduler is not None) self.engine.step() # and this plugin overrides the above calls with no-ops when Accelerate runs under # Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple # training loop that works transparently under many training regimes. class __snake_case ( lowerCamelCase__ ): def __init__( self , snake_case__ ) -> Union[str, Any]: '''simple docstring''' super().__init__(snake_case__ , device_placement=snake_case__ , scaler=snake_case__ ) UpperCAmelCase : Any =hasattr(self.optimizer , '''overflow''' ) def UpperCAmelCase__ ( self , snake_case__=None ) -> Union[str, Any]: '''simple docstring''' pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed @property def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' if self.__has_overflow__: return self.optimizer.overflow return False class __snake_case ( lowerCamelCase__ ): def __init__( self , snake_case__ , snake_case__ ) -> List[str]: '''simple docstring''' super().__init__(snake_case__ , snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed class __snake_case : def __init__( self , snake_case__ , snake_case__=0.001 , snake_case__=0 , **snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : List[Any] =params UpperCAmelCase : int =lr UpperCAmelCase : Dict =weight_decay UpperCAmelCase : int =kwargs class __snake_case : def __init__( self , snake_case__ , snake_case__=None , snake_case__=0 , **snake_case__ ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : Optional[Any] =optimizer UpperCAmelCase : Union[str, Any] =total_num_steps UpperCAmelCase : Optional[Any] =warmup_num_steps UpperCAmelCase : List[str] =kwargs
348
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[int] = (KDPMaDiscreteScheduler,) __lowerCamelCase : List[str] = 10 def UpperCAmelCase__ ( self , **snake_case__ ) -> str: '''simple docstring''' UpperCAmelCase : int ={ '''num_train_timesteps''': 1100, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', } config.update(**snake_case__ ) return config def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=snake_case__ , beta_end=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' UpperCAmelCase : Optional[Any] =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config(prediction_type='''v_prediction''' ) UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : str =self.dummy_model() UpperCAmelCase : Optional[Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : Union[str, Any] =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : str =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Any =model(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : int =output.prev_sample UpperCAmelCase : Dict =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Optional[Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.69_34e-07 ) < 1e-2 assert abs(result_mean.item() - 6.11_12e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.6_93_42_86_50_17_09_72e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0002 ) < 1e-3 def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : Any =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config() UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : Optional[int] =self.dummy_model() UpperCAmelCase : Union[str, Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : str =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : Dict =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =model(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =output.prev_sample UpperCAmelCase : Any =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Union[str, Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : List[Any] =self.scheduler_classes[0] UpperCAmelCase : Dict =self.get_scheduler_config() UpperCAmelCase : List[str] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps , device=snake_case__ ) UpperCAmelCase : int =self.dummy_model() UpperCAmelCase : Tuple =self.dummy_sample_deter.to(snake_case__ ) * scheduler.init_noise_sigma for t in scheduler.timesteps: UpperCAmelCase : Optional[Any] =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : int =model(snake_case__ , snake_case__ ) UpperCAmelCase : str =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =output.prev_sample UpperCAmelCase : List[str] =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Dict =torch.mean(torch.abs(snake_case__ ) ) if str(snake_case__ ).startswith('''cpu''' ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3
348
1
import os import tempfile import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from torch import nn from transformers import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_inverse_sqrt_schedule, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase=10 )-> List[Any]: '''simple docstring''' UpperCAmelCase : Optional[int] =[] for _ in range(__lowerCAmelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() return lrs def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase=10 )-> Tuple: '''simple docstring''' UpperCAmelCase : Union[str, Any] =[] for step in range(__lowerCAmelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() if step == num_steps // 2: with tempfile.TemporaryDirectory() as tmpdirname: UpperCAmelCase : List[str] =os.path.join(__lowerCAmelCase , '''schedule.bin''' ) torch.save(scheduler.state_dict() , __lowerCAmelCase ) UpperCAmelCase : Dict =torch.load(__lowerCAmelCase ) scheduler.load_state_dict(__lowerCAmelCase ) return lrs @require_torch class __snake_case ( unittest.TestCase ): def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> str: '''simple docstring''' self.assertEqual(len(snake_case__ ) , len(snake_case__ ) ) for a, b in zip(snake_case__ , snake_case__ ): self.assertAlmostEqual(snake_case__ , snake_case__ , delta=snake_case__ ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Optional[int] =torch.tensor([0.1, -0.2, -0.1] , requires_grad=snake_case__ ) UpperCAmelCase : Tuple =torch.tensor([0.4, 0.2, -0.5] ) UpperCAmelCase : str =nn.MSELoss() # No warmup, constant schedule, no gradient clipping UpperCAmelCase : Union[str, Any] =AdamW(params=[w] , lr=2e-1 , weight_decay=0.0 ) for _ in range(100 ): UpperCAmelCase : str =criterion(snake_case__ , snake_case__ ) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1e-2 ) def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : Any =torch.tensor([0.1, -0.2, -0.1] , requires_grad=snake_case__ ) UpperCAmelCase : Union[str, Any] =torch.tensor([0.4, 0.2, -0.5] ) UpperCAmelCase : Dict =nn.MSELoss() # No warmup, constant schedule, no gradient clipping UpperCAmelCase : Union[str, Any] =Adafactor( params=[w] , lr=1e-2 , eps=(1e-30, 1e-3) , clip_threshold=1.0 , decay_rate=-0.8 , betaa=snake_case__ , weight_decay=0.0 , relative_step=snake_case__ , scale_parameter=snake_case__ , warmup_init=snake_case__ , ) for _ in range(1000 ): UpperCAmelCase : Dict =criterion(snake_case__ , snake_case__ ) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1e-2 ) @require_torch class __snake_case ( unittest.TestCase ): __lowerCamelCase : int = nn.Linear(50 , 50 ) if is_torch_available() else None __lowerCamelCase : int = AdamW(m.parameters() , lr=10.0 ) if is_torch_available() else None __lowerCamelCase : Any = 10 def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__=None ) -> List[Any]: '''simple docstring''' self.assertEqual(len(snake_case__ ) , len(snake_case__ ) ) for a, b in zip(snake_case__ , snake_case__ ): self.assertAlmostEqual(snake_case__ , snake_case__ , delta=snake_case__ , msg=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Tuple ={'''num_warmup_steps''': 2, '''num_training_steps''': 10} # schedulers doct format # function: (sched_args_dict, expected_learning_rates) UpperCAmelCase : Union[str, Any] ={ get_constant_schedule: ({}, [10.0] * self.num_steps), get_constant_schedule_with_warmup: ( {'''num_warmup_steps''': 4}, [0.0, 2.5, 5.0, 7.5, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0], ), get_linear_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25], ), get_cosine_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 9.61, 8.53, 6.91, 5.0, 3.08, 1.46, 0.38], ), get_cosine_with_hard_restarts_schedule_with_warmup: ( {**common_kwargs, '''num_cycles''': 2}, [0.0, 5.0, 10.0, 8.53, 5.0, 1.46, 10.0, 8.53, 5.0, 1.46], ), get_polynomial_decay_schedule_with_warmup: ( {**common_kwargs, '''power''': 2.0, '''lr_end''': 1e-7}, [0.0, 5.0, 10.0, 7.656, 5.625, 3.906, 2.5, 1.406, 0.625, 0.156], ), get_inverse_sqrt_schedule: ( {'''num_warmup_steps''': 2}, [0.0, 5.0, 10.0, 8.165, 7.071, 6.325, 5.774, 5.345, 5.0, 4.714], ), } for scheduler_func, data in scheds.items(): UpperCAmelCase , UpperCAmelCase : List[Any] =data UpperCAmelCase : List[str] =scheduler_func(self.optimizer , **snake_case__ ) self.assertEqual(len([scheduler.get_lr()[0]] ) , 1 ) UpperCAmelCase : Optional[Any] =unwrap_schedule(snake_case__ , self.num_steps ) self.assertListAlmostEqual( snake_case__ , snake_case__ , tol=1e-2 , msg=f'''failed for {scheduler_func} in normal scheduler''' , ) UpperCAmelCase : Union[str, Any] =scheduler_func(self.optimizer , **snake_case__ ) if scheduler_func.__name__ != "get_constant_schedule": LambdaScheduleWrapper.wrap_scheduler(snake_case__ ) # wrap to test picklability of the schedule UpperCAmelCase : Optional[Any] =unwrap_and_save_reload_schedule(snake_case__ , self.num_steps ) self.assertListEqual(snake_case__ , snake_case__ , msg=f'''failed for {scheduler_func} in save and reload''' ) class __snake_case : def __init__( self , snake_case__ ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : int =fn def __call__( self , *snake_case__ , **snake_case__ ) -> Optional[Any]: '''simple docstring''' return self.fn(*snake_case__ , **snake_case__ ) @classmethod def UpperCAmelCase__ ( self , snake_case__ ) -> int: '''simple docstring''' UpperCAmelCase : str =list(map(self , scheduler.lr_lambdas ) )
348
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class __snake_case ( unittest.TestCase ): @slow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Any =FlaxMTaForConditionalGeneration.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : Tuple =AutoTokenizer.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : List[str] =tokenizer('''Hello there''' , return_tensors='''np''' ).input_ids UpperCAmelCase : List[Any] =tokenizer('''Hi I am''' , return_tensors='''np''' ).input_ids UpperCAmelCase : Union[str, Any] =shift_tokens_right(snake_case__ , model.config.pad_token_id , model.config.decoder_start_token_id ) UpperCAmelCase : List[str] =model(snake_case__ , decoder_input_ids=snake_case__ ).logits UpperCAmelCase : Any =optax.softmax_cross_entropy(snake_case__ , onehot(snake_case__ , logits.shape[-1] ) ).mean() UpperCAmelCase : Union[str, Any] =-(labels.shape[-1] * loss.item()) UpperCAmelCase : List[str] =-84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
348
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) __snake_case = { '''configuration_clip''': [ '''CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CLIPConfig''', '''CLIPOnnxConfig''', '''CLIPTextConfig''', '''CLIPVisionConfig''', ], '''processing_clip''': ['''CLIPProcessor'''], '''tokenization_clip''': ['''CLIPTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''CLIPTokenizerFast'''] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''CLIPFeatureExtractor'''] __snake_case = ['''CLIPImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''CLIPModel''', '''CLIPPreTrainedModel''', '''CLIPTextModel''', '''CLIPTextModelWithProjection''', '''CLIPVisionModel''', '''CLIPVisionModelWithProjection''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFCLIPModel''', '''TFCLIPPreTrainedModel''', '''TFCLIPTextModel''', '''TFCLIPVisionModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''FlaxCLIPModel''', '''FlaxCLIPPreTrainedModel''', '''FlaxCLIPTextModel''', '''FlaxCLIPTextPreTrainedModel''', '''FlaxCLIPVisionModel''', '''FlaxCLIPVisionPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_clip import ( CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPConfig, CLIPOnnxConfig, CLIPTextConfig, CLIPVisionConfig, ) from .processing_clip import CLIPProcessor from .tokenization_clip import CLIPTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_clip_fast import CLIPTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clip import CLIPFeatureExtractor from .image_processing_clip import CLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clip import ( CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPModel, CLIPPreTrainedModel, CLIPTextModel, CLIPTextModelWithProjection, CLIPVisionModel, CLIPVisionModelWithProjection, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_clip import ( TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFCLIPModel, TFCLIPPreTrainedModel, TFCLIPTextModel, TFCLIPVisionModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_clip import ( FlaxCLIPModel, FlaxCLIPPreTrainedModel, FlaxCLIPTextModel, FlaxCLIPTextPreTrainedModel, FlaxCLIPVisionModel, FlaxCLIPVisionPreTrainedModel, ) else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
import unittest import numpy as np from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __snake_case ( lowerCamelCase__ , unittest.TestCase ): # FIXME: add fast tests pass @nightly @require_onnxruntime @require_torch_gpu class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : List[Any] =ort.SessionOptions() UpperCAmelCase : Optional[int] =False return options def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : int =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Optional[Any] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Dict ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : Any =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=10 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : Optional[int] =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Tuple =np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Tuple =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , subfolder='''scheduler''' , revision='''onnx''' ) UpperCAmelCase : int =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , scheduler=snake_case__ , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Union[str, Any] ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : str =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=20 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : int =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Union[str, Any] =np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3
348
1
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 lowercase_ ( lowercase , lowercase ): '''simple docstring''' __snake_case = '''convnextv2''' def __init__( self : Any , __UpperCAmelCase : Tuple=3 , __UpperCAmelCase : Dict=4 , __UpperCAmelCase : Optional[Any]=4 , __UpperCAmelCase : Optional[int]=None , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Union[str, Any]="gelu" , __UpperCAmelCase : Optional[Any]=0.02 , __UpperCAmelCase : List[str]=1e-1_2 , __UpperCAmelCase : Tuple=0.0 , __UpperCAmelCase : Any=224 , __UpperCAmelCase : Any=None , __UpperCAmelCase : List[Any]=None , **__UpperCAmelCase : str , ) ->Union[str, Any]: """simple docstring""" super().__init__(**__UpperCAmelCase ) a = num_channels a = patch_size a = num_stages a = [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes a = [3, 3, 9, 3] if depths is None else depths a = hidden_act a = initializer_range a = layer_norm_eps a = drop_path_rate a = image_size a = ['''stem'''] + [F"""stage{idx}""" for idx in range(1 , len(self.depths ) + 1 )] a , a = get_aligned_output_features_output_indices( out_features=__UpperCAmelCase , out_indices=__UpperCAmelCase , stage_names=self.stage_names )
0
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def lowerCAmelCase_ ( )-> int: '''simple docstring''' UpperCAmelCase : str ={ '''repo_name''': ['''test_repo1''', '''test_repo2''', '''test_repo3'''], '''path''': ['''test_1.py''', '''test_2.py''', '''unit_test.py'''], '''content''': ['''a ''' * 20, '''a ''' * 30, '''b ''' * 7], } UpperCAmelCase : Union[str, Any] =Dataset.from_dict(__lowerCAmelCase ) return dataset class __snake_case ( lowerCamelCase__ ): def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[str] =get_dataset() UpperCAmelCase : Optional[int] =make_duplicate_clusters(snake_case__ , 0.85 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : str =get_dataset() UpperCAmelCase , UpperCAmelCase : Tuple =deduplicate_dataset(snake_case__ ) self.assertEqual(len(snake_case__ ) , 2 ) print(snake_case__ ) self.assertEqual(duplicate_clusters[0][0]['''copies'''] , 2 ) self.assertEqual(duplicate_clusters[0][0]['''is_extreme'''] , snake_case__ )
348
0
'''simple docstring''' import json import logging import os import sys from pathlib import Path import finetune_rag from transformers.file_utils import is_apex_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, require_ray, require_torch_gpu, require_torch_multi_gpu, ) logging.basicConfig(level=logging.DEBUG) SCREAMING_SNAKE_CASE_: Optional[int] =logging.getLogger() SCREAMING_SNAKE_CASE_: Union[str, Any] =logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class __A ( UpperCamelCase__ ): def _lowercase (self : Optional[Any] , __a : str ): os.makedirs(__a , exist_ok=__a ) UpperCAmelCase_ = {"source": "What is love ?", "target": "life"} UpperCAmelCase_ = {"train": 12, "val": 2, "test": 2} for split in ["train", "test", "val"]: for field in ["source", "target"]: UpperCAmelCase_ = "\n".join([contents[field]] * n_lines[split] ) with open(os.path.join(__a , f"""{split}.{field}""" ) , "w" ) as f: f.write(__a ) def _lowercase (self : Optional[int] , __a : int , __a : str = "pytorch" ): UpperCAmelCase_ = self.get_auto_remove_tmp_dir() UpperCAmelCase_ = os.path.join(__a , "output" ) UpperCAmelCase_ = os.path.join(__a , "data" ) self._create_dummy_data(data_dir=__a ) UpperCAmelCase_ = f""" --data_dir {data_dir} \ --output_dir {output_dir} \ --model_name_or_path facebook/rag-sequence-base \ --model_type rag_sequence \ --do_train \ --do_predict \ --n_val -1 \ --val_check_interval 1.0 \ --train_batch_size 2 \ --eval_batch_size 1 \ --max_source_length 25 \ --max_target_length 25 \ --val_max_target_length 25 \ --test_max_target_length 25 \ --label_smoothing 0.1 \ --dropout 0.1 \ --attention_dropout 0.1 \ --weight_decay 0.001 \ --adam_epsilon 1e-08 \ --max_grad_norm 0.1 \ --lr_scheduler polynomial \ --learning_rate 3e-04 \ --num_train_epochs 1 \ --warmup_steps 4 \ --gradient_accumulation_steps 1 \ --distributed-port 8787 \ --use_dummy_dataset 1 \ --distributed_retriever {distributed_retriever} \ """.split() if gpus > 0: testargs.append(f"""--gpus={gpus}""" ) if is_apex_available(): testargs.append("--fp16" ) else: testargs.append("--gpus=0" ) testargs.append("--distributed_backend=ddp_cpu" ) testargs.append("--num_processes=2" ) UpperCAmelCase_ = [sys.executable, str(Path(finetune_rag.__file__ ).resolve() )] + testargs execute_subprocess_async(__a , env=self.get_env() ) UpperCAmelCase_ = os.path.join(__a , "metrics.json" ) with open(__a ) as f: UpperCAmelCase_ = json.load(__a ) return result @require_torch_gpu def _lowercase (self : Optional[int] ): UpperCAmelCase_ = self._run_finetune(gpus=1 ) self.assertGreaterEqual(result["test"][0]["test_avg_em"] , 0.2 ) @require_torch_multi_gpu def _lowercase (self : Dict ): UpperCAmelCase_ = self._run_finetune(gpus=2 ) self.assertGreaterEqual(result["test"][0]["test_avg_em"] , 0.2 ) @require_torch_gpu @require_ray def _lowercase (self : Optional[int] ): UpperCAmelCase_ = self._run_finetune(gpus=1 , distributed_retriever="ray" ) self.assertGreaterEqual(result["test"][0]["test_avg_em"] , 0.2 ) @require_torch_multi_gpu @require_ray def _lowercase (self : Any ): UpperCAmelCase_ = self._run_finetune(gpus=1 , distributed_retriever="ray" ) self.assertGreaterEqual(result["test"][0]["test_avg_em"] , 0.2 )
1
from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput __snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name class __snake_case ( lowerCamelCase__ , lowerCamelCase__ ): @register_to_config def __init__( self , snake_case__ , snake_case__ = None , snake_case__ = None ) -> str: '''simple docstring''' super().__init__() UpperCAmelCase : Optional[Any] =learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" UpperCAmelCase : Any =torch.zeros(snake_case__ , snake_case__ ) else: UpperCAmelCase : Union[str, Any] =None UpperCAmelCase : Optional[int] =torch.nn.Parameter(snake_case__ ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : VQModel __lowerCamelCase : CLIPTextModel __lowerCamelCase : CLIPTokenizer __lowerCamelCase : TransformeraDModel __lowerCamelCase : LearnedClassifierFreeSamplingEmbeddings __lowerCamelCase : VQDiffusionScheduler def __init__( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> int: '''simple docstring''' super().__init__() self.register_modules( vqvae=snake_case__ , transformer=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , scheduler=snake_case__ , learned_classifier_free_sampling_embeddings=snake_case__ , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : int =len(snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else 1 # get prompt text embeddings UpperCAmelCase : Optional[int] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) UpperCAmelCase : int =text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCAmelCase : List[str] =self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' f''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) UpperCAmelCase : Optional[Any] =text_input_ids[:, : self.tokenizer.model_max_length] UpperCAmelCase : List[Any] =self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 UpperCAmelCase : int =prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate text embeddings for each generation per prompt UpperCAmelCase : int =prompt_embeds.repeat_interleave(snake_case__ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: UpperCAmelCase : Optional[int] =self.learned_classifier_free_sampling_embeddings.embeddings UpperCAmelCase : str =negative_prompt_embeds.unsqueeze(0 ).repeat(snake_case__ , 1 , 1 ) else: UpperCAmelCase : str =[''''''] * batch_size UpperCAmelCase : Tuple =text_input_ids.shape[-1] UpperCAmelCase : Optional[Any] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=snake_case__ , truncation=snake_case__ , return_tensors='''pt''' , ) UpperCAmelCase : Optional[Any] =self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings UpperCAmelCase : Optional[int] =negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCAmelCase : Optional[Any] =negative_prompt_embeds.shape[1] UpperCAmelCase : Union[str, Any] =negative_prompt_embeds.repeat(1 , snake_case__ , 1 ) UpperCAmelCase : Optional[Any] =negative_prompt_embeds.view(batch_size * num_images_per_prompt , snake_case__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCAmelCase : int =torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , snake_case__ , snake_case__ = 100 , snake_case__ = 5.0 , snake_case__ = 1.0 , snake_case__ = 1 , snake_case__ = None , snake_case__ = None , snake_case__ = "pil" , snake_case__ = True , snake_case__ = None , snake_case__ = 1 , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =1 elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Tuple =len(snake_case__ ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(snake_case__ )}''' ) UpperCAmelCase : Tuple =batch_size * num_images_per_prompt UpperCAmelCase : List[str] =guidance_scale > 1.0 UpperCAmelCase : List[Any] =self._encode_prompt(snake_case__ , snake_case__ , snake_case__ ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case__ , snake_case__ ) or callback_steps <= 0) ): raise ValueError( f'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' f''' {type(snake_case__ )}.''' ) # get the initial completely masked latents unless the user supplied it UpperCAmelCase : int =(batch_size, self.transformer.num_latent_pixels) if latents is None: UpperCAmelCase : Union[str, Any] =self.transformer.num_vector_embeds - 1 UpperCAmelCase : str =torch.full(snake_case__ , snake_case__ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( '''Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,''' f''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) UpperCAmelCase : Any =latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(snake_case__ , device=self.device ) UpperCAmelCase : Any =self.scheduler.timesteps.to(self.device ) UpperCAmelCase : Optional[int] =latents for i, t in enumerate(self.progress_bar(snake_case__ ) ): # expand the sample if we are doing classifier free guidance UpperCAmelCase : Optional[Any] =torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` UpperCAmelCase : Optional[int] =self.transformer(snake_case__ , encoder_hidden_states=snake_case__ , timestep=snake_case__ ).sample if do_classifier_free_guidance: UpperCAmelCase , UpperCAmelCase : str =model_output.chunk(2 ) UpperCAmelCase : Optional[int] =model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(snake_case__ , dim=1 , keepdim=snake_case__ ) UpperCAmelCase : Tuple =self.truncate(snake_case__ , snake_case__ ) # remove `log(0)`'s (`-inf`s) UpperCAmelCase : Optional[Any] =model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 UpperCAmelCase : int =self.scheduler.step(snake_case__ , timestep=snake_case__ , sample=snake_case__ , generator=snake_case__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =self.vqvae.config.vq_embed_dim UpperCAmelCase : Optional[Any] =(batch_size, self.transformer.height, self.transformer.width, embedding_channels) UpperCAmelCase : Dict =self.vqvae.quantize.get_codebook_entry(snake_case__ , shape=snake_case__ ) UpperCAmelCase : Tuple =self.vqvae.decode(snake_case__ , force_not_quantize=snake_case__ ).sample UpperCAmelCase : Union[str, Any] =(image / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase : Any =image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase : List[str] =self.numpy_to_pil(snake_case__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> torch.FloatTensor: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : int =torch.sort(snake_case__ , 1 , descending=snake_case__ ) UpperCAmelCase : Union[str, Any] =torch.exp(snake_case__ ) UpperCAmelCase : Union[str, Any] =sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out UpperCAmelCase : Optional[Any] =torch.full_like(keep_mask[:, 0:1, :] , snake_case__ ) UpperCAmelCase : Tuple =torch.cat((all_true, keep_mask) , dim=1 ) UpperCAmelCase : int =keep_mask[:, :-1, :] UpperCAmelCase : int =keep_mask.gather(1 , indices.argsort(1 ) ) UpperCAmelCase : Dict =log_p_x_0.clone() UpperCAmelCase : List[Any] =-torch.inf # -inf = log(0) return rv
348
0
'''simple docstring''' import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def _SCREAMING_SNAKE_CASE (A , A=0.999 , A="cosine" , ) -> Optional[Any]: """simple docstring""" if alpha_transform_type == "cosine": def alpha_bar_fn(A ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(A ): return math.exp(t * -12.0 ) else: raise ValueError(f"Unsupported alpha_tranform_type: {alpha_transform_type}" ) lowercase__ = [] for i in range(A ): lowercase__ = i / num_diffusion_timesteps lowercase__ = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(A ) / alpha_bar_fn(A ) , A ) ) return torch.tensor(A , dtype=torch.floataa ) class __lowerCAmelCase (lowercase_ , lowercase_ ): '''simple docstring''' lowerCAmelCase__ : Dict = [e.name for e in KarrasDiffusionSchedulers] lowerCAmelCase__ : Optional[int] = 2 @register_to_config def __init__(self : int , UpperCamelCase : int = 1000 , UpperCamelCase : float = 0.0_00_85 , UpperCamelCase : float = 0.0_12 , UpperCamelCase : str = "linear" , UpperCamelCase : Optional[Union[np.ndarray, List[float]]] = None , UpperCamelCase : str = "epsilon" , UpperCamelCase : str = "linspace" , UpperCamelCase : int = 0 , ): '''simple docstring''' if trained_betas is not None: lowercase__ = torch.tensor(UpperCamelCase , dtype=torch.floataa ) elif beta_schedule == "linear": lowercase__ = torch.linspace(UpperCamelCase , UpperCamelCase , UpperCamelCase , dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. lowercase__ = ( torch.linspace(beta_start**0.5 , beta_end**0.5 , UpperCamelCase , dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule lowercase__ = betas_for_alpha_bar(UpperCamelCase ) else: raise NotImplementedError(f"{beta_schedule} does is not implemented for {self.__class__}" ) lowercase__ = 1.0 - self.betas lowercase__ = torch.cumprod(self.alphas , dim=0 ) # set all values self.set_timesteps(UpperCamelCase , UpperCamelCase , UpperCamelCase ) def UpperCamelCase__ (self : Any , UpperCamelCase : Optional[int] , UpperCamelCase : int=None ): '''simple docstring''' if schedule_timesteps is None: lowercase__ = self.timesteps lowercase__ = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: lowercase__ = 1 if len(UpperCamelCase ) > 1 else 0 else: lowercase__ = timestep.cpu().item() if torch.is_tensor(UpperCamelCase ) else timestep lowercase__ = self._index_counter[timestep_int] return indices[pos].item() @property def UpperCamelCase__ (self : List[str] ): '''simple docstring''' if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def UpperCamelCase__ (self : str , UpperCamelCase : torch.FloatTensor , UpperCamelCase : Union[float, torch.FloatTensor] , ): '''simple docstring''' lowercase__ = self.index_for_timestep(UpperCamelCase ) if self.state_in_first_order: lowercase__ = self.sigmas[step_index] else: lowercase__ = self.sigmas_interpol[step_index] lowercase__ = sample / ((sigma**2 + 1) ** 0.5) return sample def UpperCamelCase__ (self : Optional[Any] , UpperCamelCase : int , UpperCamelCase : Union[str, torch.device] = None , UpperCamelCase : Optional[int] = None , ): '''simple docstring''' lowercase__ = num_inference_steps lowercase__ = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": lowercase__ = np.linspace(0 , num_train_timesteps - 1 , UpperCamelCase , dtype=UpperCamelCase )[::-1].copy() elif self.config.timestep_spacing == "leading": lowercase__ = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowercase__ = (np.arange(0 , UpperCamelCase ) * step_ratio).round()[::-1].copy().astype(UpperCamelCase ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": lowercase__ = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowercase__ = (np.arange(UpperCamelCase , 0 , -step_ratio )).round().copy().astype(UpperCamelCase ) timesteps -= 1 else: raise ValueError( f"{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'." ) lowercase__ = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) lowercase__ = torch.from_numpy(np.log(UpperCamelCase ) ).to(UpperCamelCase ) lowercase__ = np.interp(UpperCamelCase , np.arange(0 , len(UpperCamelCase ) ) , UpperCamelCase ) lowercase__ = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) lowercase__ = torch.from_numpy(UpperCamelCase ).to(device=UpperCamelCase ) # interpolate sigmas lowercase__ = sigmas.log().lerp(sigmas.roll(1 ).log() , 0.5 ).exp() lowercase__ = torch.cat([sigmas[:1], sigmas[1:].repeat_interleave(2 ), sigmas[-1:]] ) lowercase__ = torch.cat( [sigmas_interpol[:1], sigmas_interpol[1:].repeat_interleave(2 ), sigmas_interpol[-1:]] ) if str(UpperCamelCase ).startswith('''mps''' ): # mps does not support float64 lowercase__ = torch.from_numpy(UpperCamelCase ).to(UpperCamelCase , dtype=torch.floataa ) else: lowercase__ = torch.from_numpy(UpperCamelCase ).to(UpperCamelCase ) # interpolate timesteps lowercase__ = self.sigma_to_t(UpperCamelCase ).to(UpperCamelCase , dtype=timesteps.dtype ) lowercase__ = torch.stack((timesteps_interpol[1:-1, None], timesteps[1:, None]) , dim=-1 ).flatten() lowercase__ = torch.cat([timesteps[:1], interleaved_timesteps] ) lowercase__ = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter lowercase__ = defaultdict(UpperCamelCase ) def UpperCamelCase__ (self : List[str] , UpperCamelCase : List[Any] ): '''simple docstring''' lowercase__ = sigma.log() # get distribution lowercase__ = log_sigma - self.log_sigmas[:, None] # get sigmas range lowercase__ = dists.ge(0 ).cumsum(dim=0 ).argmax(dim=0 ).clamp(max=self.log_sigmas.shape[0] - 2 ) lowercase__ = low_idx + 1 lowercase__ = self.log_sigmas[low_idx] lowercase__ = self.log_sigmas[high_idx] # interpolate sigmas lowercase__ = (low - log_sigma) / (low - high) lowercase__ = w.clamp(0 , 1 ) # transform interpolation to time range lowercase__ = (1 - w) * low_idx + w * high_idx lowercase__ = t.view(sigma.shape ) return t @property def UpperCamelCase__ (self : List[Any] ): '''simple docstring''' return self.sample is None def UpperCamelCase__ (self : str , UpperCamelCase : Union[torch.FloatTensor, np.ndarray] , UpperCamelCase : Union[float, torch.FloatTensor] , UpperCamelCase : Union[torch.FloatTensor, np.ndarray] , UpperCamelCase : bool = True , ): '''simple docstring''' lowercase__ = self.index_for_timestep(UpperCamelCase ) # advance index counter by 1 lowercase__ = timestep.cpu().item() if torch.is_tensor(UpperCamelCase ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: lowercase__ = self.sigmas[step_index] lowercase__ = self.sigmas_interpol[step_index + 1] lowercase__ = self.sigmas[step_index + 1] else: # 2nd order / KDPM2's method lowercase__ = self.sigmas[step_index - 1] lowercase__ = self.sigmas_interpol[step_index] lowercase__ = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API lowercase__ = 0 lowercase__ = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": lowercase__ = sigma_hat if self.state_in_first_order else sigma_interpol lowercase__ = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": lowercase__ = sigma_hat if self.state_in_first_order else sigma_interpol lowercase__ = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": raise NotImplementedError('''prediction_type not implemented yet: sample''' ) else: raise ValueError( f"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`" ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order lowercase__ = (sample - pred_original_sample) / sigma_hat # 3. delta timestep lowercase__ = sigma_interpol - sigma_hat # store for 2nd order step lowercase__ = sample else: # DPM-Solver-2 # 2. Convert to an ODE derivative for 2nd order lowercase__ = (sample - pred_original_sample) / sigma_interpol # 3. delta timestep lowercase__ = sigma_next - sigma_hat lowercase__ = self.sample lowercase__ = None lowercase__ = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=UpperCamelCase ) def UpperCamelCase__ (self : Optional[Any] , UpperCamelCase : torch.FloatTensor , UpperCamelCase : torch.FloatTensor , UpperCamelCase : torch.FloatTensor , ): '''simple docstring''' lowercase__ = self.sigmas.to(device=original_samples.device , dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(UpperCamelCase ): # mps does not support float64 lowercase__ = self.timesteps.to(original_samples.device , dtype=torch.floataa ) lowercase__ = timesteps.to(original_samples.device , dtype=torch.floataa ) else: lowercase__ = self.timesteps.to(original_samples.device ) lowercase__ = timesteps.to(original_samples.device ) lowercase__ = [self.index_for_timestep(UpperCamelCase , UpperCamelCase ) for t in timesteps] lowercase__ = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): lowercase__ = sigma.unsqueeze(-1 ) lowercase__ = original_samples + noise * sigma return noisy_samples def __len__(self : List[Any] ): '''simple docstring''' return self.config.num_train_timesteps
2
import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase : Any =UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =self.dummy_uncond_unet UpperCAmelCase : Optional[int] =KarrasVeScheduler() UpperCAmelCase : List[Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : List[str] =torch.manual_seed(0 ) UpperCAmelCase : List[str] =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : str =torch.manual_seed(0 ) UpperCAmelCase : str =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' , return_dict=snake_case__ )[0] UpperCAmelCase : Any =image[0, -3:, -3:, -1] UpperCAmelCase : List[str] =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCAmelCase : int =np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class __snake_case ( unittest.TestCase ): def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Tuple ='''google/ncsnpp-celebahq-256''' UpperCAmelCase : int =UNetaDModel.from_pretrained(snake_case__ ) UpperCAmelCase : Dict =KarrasVeScheduler() UpperCAmelCase : Union[str, Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Any =torch.manual_seed(0 ) UpperCAmelCase : Tuple =pipe(num_inference_steps=20 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : Optional[int] =image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) UpperCAmelCase : Tuple =np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
348
0
'''simple docstring''' from __future__ import annotations from collections.abc import Iterator class A : def __init__( self , SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" A : Optional[Any] = value A : Node | None = None A : Node | None = None class A : def __init__( self , SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" A : str = tree def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" if node is None: return 0 return node.value + ( self.depth_first_search(node.left ) + self.depth_first_search(node.right ) ) def __iter__( self ) -> Iterator[int]: """simple docstring""" yield self.depth_first_search(self.tree ) if __name__ == "__main__": import doctest doctest.testmod()
3
import qiskit def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> qiskit.result.counts.Counts: '''simple docstring''' UpperCAmelCase : Union[str, Any] =qiskit.Aer.get_backend('''aer_simulator''' ) UpperCAmelCase : List[str] =qiskit.QuantumCircuit(4 , 2 ) # encode inputs in qubits 0 and 1 if bita == 1: qc_ha.x(0 ) if bita == 1: qc_ha.x(1 ) qc_ha.barrier() # use cnots to write XOR of the inputs on qubit2 qc_ha.cx(0 , 2 ) qc_ha.cx(1 , 2 ) # use ccx / toffoli gate to write AND of the inputs on qubit3 qc_ha.ccx(0 , 1 , 3 ) qc_ha.barrier() # extract outputs qc_ha.measure(2 , 0 ) # extract XOR value qc_ha.measure(3 , 1 ) # extract AND value # Execute the circuit on the qasm simulator UpperCAmelCase : Dict =qiskit.execute(__lowerCAmelCase , __lowerCAmelCase , shots=10_00 ) # Return the histogram data of the results of the experiment return job.result().get_counts(__lowerCAmelCase ) if __name__ == "__main__": __snake_case = half_adder(1, 1) print(f'Half Adder Output Qubit Counts: {counts}')
348
0
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class UpperCAmelCase_ ( unittest.TestCase ): @slow def __UpperCAmelCase ( self : str ) -> List[str]: lowerCAmelCase = XLMRobertaModel.from_pretrained('xlm-roberta-base' ) lowerCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house lowerCAmelCase = torch.Size((1, 1_2, 7_6_8) ) # batch_size, sequence_length, embedding_vector_dim lowerCAmelCase = torch.tensor( [[-0.0_101, 0.1_218, -0.0_803, 0.0_801, 0.1_327, 0.0_776, -0.1_215, 0.2_383, 0.3_338, 0.3_106, 0.0_300, 0.0_252]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): lowerCAmelCase = model(UpperCAmelCase__ )['last_hidden_state'].detach() self.assertEqual(output.shape , UpperCAmelCase__ ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , UpperCAmelCase__ , atol=1E-3 ) ) @slow def __UpperCAmelCase ( self : List[Any] ) -> Tuple: lowerCAmelCase = XLMRobertaModel.from_pretrained('xlm-roberta-large' ) lowerCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house lowerCAmelCase = torch.Size((1, 1_2, 1_0_2_4) ) # batch_size, sequence_length, embedding_vector_dim lowerCAmelCase = torch.tensor( [[-0.0_699, -0.0_318, 0.0_705, -0.1_241, 0.0_999, -0.0_520, 0.1_004, -0.1_838, -0.4_704, 0.1_437, 0.0_821, 0.0_126]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): lowerCAmelCase = model(UpperCAmelCase__ )['last_hidden_state'].detach() self.assertEqual(output.shape , UpperCAmelCase__ ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , UpperCAmelCase__ , atol=1E-3 ) )
4
from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class __snake_case : __lowerCamelCase : str = BlenderbotConfig __lowerCamelCase : Optional[Any] = {} __lowerCamelCase : Optional[int] = """gelu""" def __init__( self , snake_case__ , snake_case__=13 , snake_case__=7 , snake_case__=True , snake_case__=False , snake_case__=99 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=20 , snake_case__=2 , snake_case__=1 , snake_case__=0 , ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : Dict =seq_length UpperCAmelCase : Optional[Any] =is_training UpperCAmelCase : List[str] =use_labels UpperCAmelCase : List[Any] =vocab_size UpperCAmelCase : Optional[int] =hidden_size UpperCAmelCase : Tuple =num_hidden_layers UpperCAmelCase : Any =num_attention_heads UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : str =hidden_dropout_prob UpperCAmelCase : Optional[int] =attention_probs_dropout_prob UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : List[Any] =eos_token_id UpperCAmelCase : Optional[int] =pad_token_id UpperCAmelCase : Tuple =bos_token_id def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[Any] =ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) UpperCAmelCase : List[Any] =tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) UpperCAmelCase : Tuple =tf.concat([input_ids, eos_tensor] , axis=1 ) UpperCAmelCase : str =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : Optional[Any] =self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) UpperCAmelCase : List[str] =prepare_blenderbot_inputs_dict(snake_case__ , snake_case__ , snake_case__ ) return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =TFBlenderbotModel(config=snake_case__ ).get_decoder() UpperCAmelCase : Any =inputs_dict['''input_ids'''] UpperCAmelCase : str =input_ids[:1, :] UpperCAmelCase : Tuple =inputs_dict['''attention_mask'''][:1, :] UpperCAmelCase : Tuple =inputs_dict['''head_mask'''] UpperCAmelCase : List[Any] =1 # first forward pass UpperCAmelCase : List[str] =model(snake_case__ , attention_mask=snake_case__ , head_mask=snake_case__ , use_cache=snake_case__ ) UpperCAmelCase , UpperCAmelCase : str =outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase : Union[str, Any] =ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCAmelCase : List[Any] =tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and UpperCAmelCase : Tuple =tf.concat([input_ids, next_tokens] , axis=-1 ) UpperCAmelCase : int =tf.concat([attention_mask, next_attn_mask] , axis=-1 ) UpperCAmelCase : Optional[int] =model(snake_case__ , attention_mask=snake_case__ )[0] UpperCAmelCase : str =model(snake_case__ , attention_mask=snake_case__ , past_key_values=snake_case__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice UpperCAmelCase : List[Any] =int(ids_tensor((1,) , output_from_past.shape[-1] ) ) UpperCAmelCase : List[Any] =output_from_no_past[:, -3:, random_slice_idx] UpperCAmelCase : Dict =output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(snake_case__ , snake_case__ , rtol=1e-3 ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , )-> str: '''simple docstring''' if attention_mask is None: UpperCAmelCase : int =tf.cast(tf.math.not_equal(__lowerCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase : Tuple =tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase : str =tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase : Union[str, Any] =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase : int =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : List[str] = (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __lowerCamelCase : Dict = (TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __lowerCamelCase : Dict = ( { """conversational""": TFBlenderbotForConditionalGeneration, """feature-extraction""": TFBlenderbotModel, """summarization""": TFBlenderbotForConditionalGeneration, """text2text-generation""": TFBlenderbotForConditionalGeneration, """translation""": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __lowerCamelCase : Union[str, Any] = True __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Union[str, Any] = False def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : List[str] =TFBlenderbotModelTester(self ) UpperCAmelCase : List[Any] =ConfigTester(self , config_class=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*snake_case__ ) @require_tokenizers @require_tf class __snake_case ( unittest.TestCase ): __lowerCamelCase : List[str] = ["""My friends are cool but they eat too many carbs."""] __lowerCamelCase : Dict = """facebook/blenderbot-400M-distill""" @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[int] =self.tokenizer(self.src_text , return_tensors='''tf''' ) UpperCAmelCase : Optional[int] =self.model.generate( model_inputs.input_ids , ) UpperCAmelCase : str =self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=snake_case__ )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
348
0
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('''>=''', '''4.25.0''')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
5
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { '''asapp/sew-d-tiny-100k''': '''https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json''', # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[Any] = """sew-d""" def __init__( self , snake_case__=32 , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=2 , snake_case__=512 , snake_case__=256 , snake_case__=True , snake_case__=True , snake_case__=("p2c", "c2p") , snake_case__="layer_norm" , snake_case__="gelu_python" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.0 , snake_case__=0.1 , snake_case__=0.02 , snake_case__=1e-7 , snake_case__=1e-5 , snake_case__="group" , snake_case__="gelu" , snake_case__=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , snake_case__=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , snake_case__=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , snake_case__=False , snake_case__=128 , snake_case__=16 , snake_case__=True , snake_case__=0.05 , snake_case__=10 , snake_case__=2 , snake_case__=0.0 , snake_case__=10 , snake_case__=0 , snake_case__="mean" , snake_case__=False , snake_case__=False , snake_case__=256 , snake_case__=0 , snake_case__=1 , snake_case__=2 , **snake_case__ , ) -> int: '''simple docstring''' super().__init__(**snake_case__ , pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ ) UpperCAmelCase : Union[str, Any] =hidden_size UpperCAmelCase : Union[str, Any] =feat_extract_norm UpperCAmelCase : Optional[Any] =feat_extract_activation UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : int =list(snake_case__ ) UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : str =conv_bias UpperCAmelCase : Tuple =num_conv_pos_embeddings UpperCAmelCase : Dict =num_conv_pos_embedding_groups UpperCAmelCase : str =len(self.conv_dim ) UpperCAmelCase : Dict =num_hidden_layers UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : List[Any] =squeeze_factor UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : int =position_buckets UpperCAmelCase : Optional[int] =share_att_key UpperCAmelCase : Optional[int] =relative_attention UpperCAmelCase : Tuple =norm_rel_ebd UpperCAmelCase : List[Any] =list(snake_case__ ) UpperCAmelCase : Dict =hidden_act UpperCAmelCase : Optional[int] =num_attention_heads UpperCAmelCase : Any =hidden_dropout UpperCAmelCase : str =attention_dropout UpperCAmelCase : Union[str, Any] =activation_dropout UpperCAmelCase : str =feat_proj_dropout UpperCAmelCase : Union[str, Any] =final_dropout UpperCAmelCase : Optional[int] =layer_norm_eps UpperCAmelCase : str =feature_layer_norm_eps UpperCAmelCase : str =initializer_range UpperCAmelCase : Any =vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect.''' '''It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,''' f'''but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)''' f'''= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCAmelCase : Union[str, Any] =apply_spec_augment UpperCAmelCase : Optional[Any] =mask_time_prob UpperCAmelCase : Tuple =mask_time_length UpperCAmelCase : str =mask_time_min_masks UpperCAmelCase : Optional[int] =mask_feature_prob UpperCAmelCase : Optional[Any] =mask_feature_length UpperCAmelCase : List[Any] =mask_feature_min_masks # ctc loss UpperCAmelCase : str =ctc_loss_reduction UpperCAmelCase : Optional[int] =ctc_zero_infinity # sequence classification UpperCAmelCase : Union[str, Any] =use_weighted_layer_sum UpperCAmelCase : int =classifier_proj_size @property def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
348
0
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import TensorType, is_torch_available, logging A : str = logging.get_logger(__name__) A : Tuple = { 'Helsinki-NLP/opus-mt-en-de': 'https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json', # See all Marian models at https://huggingface.co/models?filter=marian } class __A( a ): snake_case_ = '''marian''' snake_case_ = ['''past_key_values'''] snake_case_ = {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self , _snake_case=58_101 , _snake_case=None , _snake_case=1_024 , _snake_case=12 , _snake_case=4_096 , _snake_case=16 , _snake_case=12 , _snake_case=4_096 , _snake_case=16 , _snake_case=0.0 , _snake_case=0.0 , _snake_case=True , _snake_case=True , _snake_case="gelu" , _snake_case=1_024 , _snake_case=0.1 , _snake_case=0.0 , _snake_case=0.0 , _snake_case=0.02 , _snake_case=58_100 , _snake_case=False , _snake_case=58_100 , _snake_case=0 , _snake_case=0 , _snake_case=True , **_snake_case , ) -> List[str]: '''simple docstring''' __a = vocab_size __a = decoder_vocab_size or vocab_size __a = max_position_embeddings __a = d_model __a = encoder_ffn_dim __a = encoder_layers __a = encoder_attention_heads __a = decoder_ffn_dim __a = decoder_layers __a = decoder_attention_heads __a = dropout __a = attention_dropout __a = activation_dropout __a = activation_function __a = init_std __a = encoder_layerdrop __a = decoder_layerdrop __a = use_cache __a = encoder_layers __a = scale_embedding # scale factor will be sqrt(d_model) if True __a = share_encoder_decoder_embeddings super().__init__( pad_token_id=_snake_case , eos_token_id=_snake_case , is_encoder_decoder=_snake_case , decoder_start_token_id=_snake_case , forced_eos_token_id=_snake_case , **_snake_case , ) class __A( a ): @property # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs def SCREAMING_SNAKE_CASE_ ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: __a = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: __a = {0: '''batch'''} __a = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: __a = {0: '''batch''', 1: '''decoder_sequence'''} __a = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(_snake_case , direction='''inputs''' ) elif self.task == "causal-lm": # TODO: figure this case out. __a = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: __a , __a = self.num_layers for i in range(_snake_case ): __a = {0: '''batch''', 2: '''past_sequence + sequence'''} __a = {0: '''batch''', 2: '''past_sequence + sequence'''} else: __a = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}), ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}), ] ) return common_inputs @property # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs def SCREAMING_SNAKE_CASE_ ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: __a = super().outputs else: __a = super(_snake_case , self ).outputs if self.use_past: __a , __a = self.num_layers for i in range(_snake_case ): __a = {0: '''batch''', 2: '''past_sequence + sequence'''} __a = {0: '''batch''', 2: '''past_sequence + sequence'''} return common_outputs def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case = -1 , _snake_case = -1 , _snake_case = False , _snake_case = None , ) -> Mapping[str, Any]: '''simple docstring''' __a = self._generate_dummy_inputs_for_encoder_and_decoder( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ) # Generate decoder inputs __a = seq_length if not self.use_past else 1 __a = self._generate_dummy_inputs_for_encoder_and_decoder( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ) __a = {F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()} __a = dict(**_snake_case , **_snake_case ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch __a , __a = common_inputs['''input_ids'''].shape __a = common_inputs['''decoder_input_ids'''].shape[1] __a , __a = self.num_attention_heads __a = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) __a = decoder_seq_length + 3 __a = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) __a = torch.cat( [common_inputs['''decoder_attention_mask'''], torch.ones(_snake_case , _snake_case )] , dim=1 ) __a = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered __a , __a = self.num_layers __a = min(_snake_case , _snake_case ) __a = max(_snake_case , _snake_case ) - min_num_layers __a = '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder''' for _ in range(_snake_case ): common_inputs["past_key_values"].append( ( torch.zeros(_snake_case ), torch.zeros(_snake_case ), torch.zeros(_snake_case ), torch.zeros(_snake_case ), ) ) # TODO: test this. __a = encoder_shape if remaining_side_name == '''encoder''' else decoder_shape for _ in range(_snake_case , _snake_case ): common_inputs["past_key_values"].append((torch.zeros(_snake_case ), torch.zeros(_snake_case )) ) return common_inputs def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case = -1 , _snake_case = -1 , _snake_case = False , _snake_case = None , ) -> Mapping[str, Any]: '''simple docstring''' __a = self._generate_dummy_inputs_for_encoder_and_decoder( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch __a , __a = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values __a = seqlen + 2 __a , __a = self.num_layers __a , __a = self.num_attention_heads __a = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) __a = common_inputs['''attention_mask'''].dtype __a = torch.cat( [common_inputs['''attention_mask'''], torch.ones(_snake_case , _snake_case , dtype=_snake_case )] , dim=1 ) __a = [ (torch.zeros(_snake_case ), torch.zeros(_snake_case )) for _ in range(_snake_case ) ] return common_inputs def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case = -1 , _snake_case = -1 , _snake_case = False , _snake_case = None , ) -> Mapping[str, Any]: '''simple docstring''' __a = compute_effective_axis_dimension( _snake_case , 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 __a = tokenizer.num_special_tokens_to_add(_snake_case ) __a = compute_effective_axis_dimension( _snake_case , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=_snake_case ) # Generate dummy inputs according to compute batch and sequence __a = [''' '''.join([tokenizer.unk_token] ) * seq_length] * batch_size __a = dict(tokenizer(_snake_case , return_tensors=_snake_case ) ) return common_inputs def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case = -1 , _snake_case = -1 , _snake_case = False , _snake_case = None , ) -> Mapping[str, Any]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: __a = self._generate_dummy_inputs_for_default_and_seqaseq_lm( _snake_case , batch_size=_snake_case , seq_length=_snake_case , is_pair=_snake_case , framework=_snake_case ) else: __a = self._generate_dummy_inputs_for_causal_lm( _snake_case , batch_size=_snake_case , seq_length=_snake_case , is_pair=_snake_case , framework=_snake_case ) return common_inputs def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case , _snake_case ) -> Union[str, Any]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: __a = super()._flatten_past_key_values_(_snake_case , _snake_case , _snake_case , _snake_case ) else: __a = super(_snake_case , self )._flatten_past_key_values_( _snake_case , _snake_case , _snake_case , _snake_case ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> float: '''simple docstring''' return 1E-4
6
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node __snake_case = 4 __snake_case = 3 class __snake_case ( lowerCamelCase__ ): pass def lowerCAmelCase_ ( __lowerCAmelCase )-> List[str]: '''simple docstring''' for shard in shards: for i in range(__lowerCAmelCase ): yield {"i": i, "shard": shard} def lowerCAmelCase_ ( )-> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =int(os.environ['''RANK'''] ) UpperCAmelCase : Optional[Any] =int(os.environ['''WORLD_SIZE'''] ) UpperCAmelCase : List[Any] =ArgumentParser() parser.add_argument('''--streaming''' , type=__lowerCAmelCase ) parser.add_argument('''--local_rank''' , type=__lowerCAmelCase ) parser.add_argument('''--num_workers''' , type=__lowerCAmelCase , default=0 ) UpperCAmelCase : Any =parser.parse_args() UpperCAmelCase : List[str] =args.streaming UpperCAmelCase : Tuple =args.num_workers UpperCAmelCase : int ={'''shards''': [f'''shard_{shard_idx}''' for shard_idx in range(__lowerCAmelCase )]} UpperCAmelCase : Optional[int] =IterableDataset.from_generator(__lowerCAmelCase , gen_kwargs=__lowerCAmelCase ) if not streaming: UpperCAmelCase : List[Any] =Dataset.from_list(list(__lowerCAmelCase ) ) UpperCAmelCase : Dict =split_dataset_by_node(__lowerCAmelCase , rank=__lowerCAmelCase , world_size=__lowerCAmelCase ) UpperCAmelCase : List[Any] =torch.utils.data.DataLoader(__lowerCAmelCase , num_workers=__lowerCAmelCase ) UpperCAmelCase : Dict =NUM_SHARDS * NUM_ITEMS_PER_SHARD UpperCAmelCase : str =full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) UpperCAmelCase : List[Any] =sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f'''local_size {local_size} != expected_local_size {expected_local_size}''' ) if __name__ == "__main__": main()
348
0
import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _snake_case( *SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : Optional[Union[Dict, Any]] = None , SCREAMING_SNAKE_CASE__ : Union[str, Any]=True , SCREAMING_SNAKE_CASE__ : int=2 ) -> Optional[int]: '''simple docstring''' from .. import __version__ A__ = take_from A__ = () if not isinstance(args[0] , SCREAMING_SNAKE_CASE__ ): A__ = (args,) for attribute, version_name, message in args: if version.parse(version.parse(SCREAMING_SNAKE_CASE__ ).base_version ) >= version.parse(SCREAMING_SNAKE_CASE__ ): raise ValueError( f'The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\'' f' version {__version__} is >= {version_name}' ) A__ = None if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(SCREAMING_SNAKE_CASE__ ),) A__ = f'The `{attribute}` argument is deprecated and will be removed in version {version_name}.' elif hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): values += (getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ),) A__ = f'The `{attribute}` attribute is deprecated and will be removed in version {version_name}.' elif deprecated_kwargs is None: A__ = f'`{attribute}` is deprecated and will be removed in version {version_name}.' if warning is not None: A__ = warning + ' ' if standard_warn else '' warnings.warn(warning + message , SCREAMING_SNAKE_CASE__ , stacklevel=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and len(SCREAMING_SNAKE_CASE__ ) > 0: A__ = inspect.getouterframes(inspect.currentframe() )[1] A__ = call_frame.filename A__ = call_frame.lineno A__ = call_frame.function A__ , A__ = next(iter(deprecated_kwargs.items() ) ) raise TypeError(f'{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`' ) if len(SCREAMING_SNAKE_CASE__ ) == 0: return elif len(SCREAMING_SNAKE_CASE__ ) == 1: return values[0] return values
7
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __snake_case = {'''configuration_opt''': ['''OPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OPTConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''OPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OPTForCausalLM''', '''OPTModel''', '''OPTPreTrainedModel''', '''OPTForSequenceClassification''', '''OPTForQuestionAnswering''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''TFOPTForCausalLM''', '''TFOPTModel''', '''TFOPTPreTrainedModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''FlaxOPTForCausalLM''', '''FlaxOPTModel''', '''FlaxOPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
0
from typing import Any, Dict, List, Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from ..image_utils import load_image if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = Dict[str, Any] lowerCAmelCase_ = List[Prediction] @add_end_docstrings(__A ) class snake_case_ ( __A ): '''simple docstring''' def __init__( self : str , *_UpperCamelCase : Tuple , **_UpperCamelCase : Optional[Any] ) ->int: super().__init__(*_UpperCamelCase , **_UpperCamelCase ) if self.framework == "tf": raise ValueError(f'''The {self.__class__} is only available in PyTorch.''' ) requires_backends(self , '''vision''' ) self.check_model_type( dict(MODEL_FOR_OBJECT_DETECTION_MAPPING.items() + MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.items() ) ) def snake_case__( self : Optional[Any] , **_UpperCamelCase : Any ) ->Optional[int]: snake_case_ = {} if "threshold" in kwargs: snake_case_ = kwargs['''threshold'''] return {}, {}, postprocess_kwargs def __call__( self : str , *_UpperCamelCase : List[Any] , **_UpperCamelCase : Tuple ) ->Union[Predictions, List[Prediction]]: return super().__call__(*_UpperCamelCase , **_UpperCamelCase ) def snake_case__( self : Any , _UpperCamelCase : str ) ->Union[str, Any]: snake_case_ = load_image(_UpperCamelCase ) snake_case_ = torch.IntTensor([[image.height, image.width]] ) snake_case_ = self.image_processor(images=[image] , return_tensors='''pt''' ) if self.tokenizer is not None: snake_case_ = self.tokenizer(text=inputs['''words'''] , boxes=inputs['''boxes'''] , return_tensors='''pt''' ) snake_case_ = target_size return inputs def snake_case__( self : int , _UpperCamelCase : List[str] ) ->List[Any]: snake_case_ = model_inputs.pop('''target_size''' ) snake_case_ = self.model(**_UpperCamelCase ) snake_case_ = outputs.__class__({'''target_size''': target_size, **outputs} ) if self.tokenizer is not None: snake_case_ = model_inputs['''bbox'''] return model_outputs def snake_case__( self : List[Any] , _UpperCamelCase : str , _UpperCamelCase : List[str]=0.9 ) ->List[Any]: snake_case_ = model_outputs['''target_size'''] if self.tokenizer is not None: # This is a LayoutLMForTokenClassification variant. # The OCR got the boxes and the model classified the words. snake_case_, snake_case_ = target_size[0].tolist() def unnormalize(_UpperCamelCase : int ): return self._get_bounding_box( torch.Tensor( [ (width * bbox[0] / 1_0_0_0), (height * bbox[1] / 1_0_0_0), (width * bbox[2] / 1_0_0_0), (height * bbox[3] / 1_0_0_0), ] ) ) snake_case_, snake_case_ = model_outputs['''logits'''].squeeze(0 ).softmax(dim=-1 ).max(dim=-1 ) snake_case_ = [self.model.config.idalabel[prediction] for prediction in classes.tolist()] snake_case_ = [unnormalize(_UpperCamelCase ) for bbox in model_outputs['''bbox'''].squeeze(0 )] snake_case_ = ['''score''', '''label''', '''box'''] snake_case_ = [dict(zip(_UpperCamelCase , _UpperCamelCase ) ) for vals in zip(scores.tolist() , _UpperCamelCase , _UpperCamelCase ) if vals[0] > threshold] else: # This is a regular ForObjectDetectionModel snake_case_ = self.image_processor.post_process_object_detection(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) snake_case_ = raw_annotations[0] snake_case_ = raw_annotation['''scores'''] snake_case_ = raw_annotation['''labels'''] snake_case_ = raw_annotation['''boxes'''] snake_case_ = scores.tolist() snake_case_ = [self.model.config.idalabel[label.item()] for label in labels] snake_case_ = [self._get_bounding_box(_UpperCamelCase ) for box in boxes] # {"scores": [...], ...} --> [{"score":x, ...}, ...] snake_case_ = ['''score''', '''label''', '''box'''] snake_case_ = [ dict(zip(_UpperCamelCase , _UpperCamelCase ) ) for vals in zip(raw_annotation['''scores'''] , raw_annotation['''labels'''] , raw_annotation['''boxes'''] ) ] return annotation def snake_case__( self : List[Any] , _UpperCamelCase : "torch.Tensor" ) ->Dict[str, int]: if self.framework != "pt": raise ValueError('''The ObjectDetectionPipeline is only available in PyTorch.''' ) snake_case_, snake_case_, snake_case_, snake_case_ = box.int().tolist() snake_case_ = { '''xmin''': xmin, '''ymin''': ymin, '''xmax''': xmax, '''ymax''': ymax, } return bbox
8
import tempfile import unittest import numpy as np import transformers from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax import jax.numpy as jnp from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel if is_torch_available(): import torch class __snake_case : def __init__( self , snake_case__ , snake_case__=14 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=False , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=4 , snake_case__=4 , snake_case__=4 , snake_case__=37 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , ) -> str: '''simple docstring''' UpperCAmelCase : str =parent UpperCAmelCase : Tuple =batch_size UpperCAmelCase : Optional[int] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Tuple =use_input_mask UpperCAmelCase : List[Any] =use_token_type_ids UpperCAmelCase : Optional[Any] =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : List[Any] =hidden_size UpperCAmelCase : Optional[int] =rotary_dim UpperCAmelCase : Union[str, Any] =num_hidden_layers UpperCAmelCase : List[Any] =num_attention_heads UpperCAmelCase : Dict =intermediate_size UpperCAmelCase : Union[str, Any] =hidden_act UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : Dict =attention_probs_dropout_prob UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : str =initializer_range UpperCAmelCase : Optional[int] =None UpperCAmelCase : List[Any] =vocab_size - 1 UpperCAmelCase : Optional[Any] =vocab_size - 1 UpperCAmelCase : List[Any] =vocab_size - 1 def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : List[Any] =None if self.use_input_mask: UpperCAmelCase : Optional[Any] =random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : Dict =GPTJConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , use_cache=snake_case__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , rotary_dim=self.rotary_dim , ) return (config, input_ids, input_mask) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Union[str, Any] =config_and_inputs UpperCAmelCase : Tuple ={'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =20 UpperCAmelCase : Any =model_class_name(snake_case__ ) UpperCAmelCase : str =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : Any =jnp.ones((input_ids.shape[0], max_decoder_length) , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : List[str] =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =model( input_ids[:, -1:] , attention_mask=snake_case__ , past_key_values=outputs_cache.past_key_values , position_ids=snake_case__ , ) UpperCAmelCase : List[Any] =model(snake_case__ ) UpperCAmelCase : Any =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Dict =20 UpperCAmelCase : Dict =model_class_name(snake_case__ ) UpperCAmelCase : Tuple =jnp.concatenate( [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]) )] , axis=-1 , ) UpperCAmelCase : Dict =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : int =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : str =model( input_ids[:, -1:] , past_key_values=outputs_cache.past_key_values , attention_mask=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =model(snake_case__ , attention_mask=snake_case__ ) UpperCAmelCase : Dict =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) @require_flax class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Tuple = (FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else () __lowerCamelCase : Optional[Any] = (FlaxGPTJForCausalLM,) if is_flax_available() else () def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =FlaxGPTJModelTester(self ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Dict =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward_with_attn_mask( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) @tooslow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =GPTaTokenizer.from_pretrained('''gpt2''' , pad_token='''<|endoftext|>''' , padding_side='''left''' ) UpperCAmelCase : Optional[Any] =tokenizer(['''Hello this is a long string''', '''Hey'''] , return_tensors='''np''' , padding=snake_case__ , truncation=snake_case__ ) UpperCAmelCase : Optional[int] =FlaxGPTJForCausalLM.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : str =False UpperCAmelCase : Union[str, Any] =model.config.eos_token_id UpperCAmelCase : List[Any] =jax.jit(model.generate ) UpperCAmelCase : Dict =jit_generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , pad_token_id=tokenizer.pad_token_id ).sequences UpperCAmelCase : Any =tokenizer.batch_decode(snake_case__ , skip_special_tokens=snake_case__ ) UpperCAmelCase : Tuple =[ '''Hello this is a long string of text.\n\nI\'m trying to get the text of the''', '''Hey, I\'m a little late to the party. I\'m going to''', ] self.assertListEqual(snake_case__ , snake_case__ ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : List[str] =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : Any =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : Any =getattr(snake_case__ , snake_case__ ) UpperCAmelCase , UpperCAmelCase : Union[str, Any] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Tuple =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : int =0 UpperCAmelCase : Optional[int] =1 UpperCAmelCase : Optional[int] =0 UpperCAmelCase : Union[str, Any] =1 UpperCAmelCase : List[str] =pt_model_class(snake_case__ ).eval() UpperCAmelCase : Optional[int] =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Any =convert_pytorch_state_dict_to_flax(pt_model.state_dict() , snake_case__ ) UpperCAmelCase : Union[str, Any] =fx_state with torch.no_grad(): UpperCAmelCase : Any =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : Dict =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(snake_case__ ) UpperCAmelCase : str =model_class.from_pretrained(snake_case__ , from_pt=snake_case__ ) UpperCAmelCase : int =fx_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output_loaded, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output_loaded[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : int =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : int =getattr(snake_case__ , snake_case__ ) UpperCAmelCase : Dict =pt_model_class(snake_case__ ).eval() UpperCAmelCase : str =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Optional[Any] =load_flax_weights_in_pytorch_model(snake_case__ , fx_model.params ) UpperCAmelCase , UpperCAmelCase : Optional[int] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Optional[int] =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : str =0 UpperCAmelCase : Any =1 UpperCAmelCase : List[Any] =0 UpperCAmelCase : Tuple =1 # make sure weights are tied in PyTorch pt_model.tie_weights() with torch.no_grad(): UpperCAmelCase : Optional[Any] =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : List[Any] =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(snake_case__ ) UpperCAmelCase : Tuple =pt_model_class.from_pretrained(snake_case__ , from_flax=snake_case__ ) with torch.no_grad(): UpperCAmelCase : Any =pt_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @tooslow def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase : str =model_class_name.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : Tuple =model(np.ones((1, 1) ) ) self.assertIsNotNone(snake_case__ )
348
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_speech_available, is_torch_available __lowerCAmelCase : Any ={ 'configuration_audio_spectrogram_transformer': [ 'AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ASTConfig', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Tuple =[ 'AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'ASTForAudioClassification', 'ASTModel', 'ASTPreTrainedModel', ] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Dict =['ASTFeatureExtractor'] if TYPE_CHECKING: from .configuration_audio_spectrogram_transformer import ( AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, ASTConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_audio_spectrogram_transformer import ( AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ASTForAudioClassification, ASTModel, ASTPreTrainedModel, ) try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_audio_spectrogram_transformer import ASTFeatureExtractor else: import sys __lowerCAmelCase : Tuple =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
9
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __snake_case = { '''configuration_bloom''': ['''BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BloomConfig''', '''BloomOnnxConfig'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''BloomTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BloomForCausalLM''', '''BloomModel''', '''BloomPreTrainedModel''', '''BloomForSequenceClassification''', '''BloomForTokenClassification''', '''BloomForQuestionAnswering''', ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
0
from queue import Queue from typing import TYPE_CHECKING, Optional if TYPE_CHECKING: from ..models.auto import AutoTokenizer class _SCREAMING_SNAKE_CASE : '''simple docstring''' def SCREAMING_SNAKE_CASE_ (self : Optional[Any] , UpperCAmelCase_ : Tuple) ->Dict: '''simple docstring''' raise NotImplementedError() def SCREAMING_SNAKE_CASE_ (self : Tuple) ->Any: '''simple docstring''' raise NotImplementedError() class _SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__(self : Dict , UpperCAmelCase_ : "AutoTokenizer" , UpperCAmelCase_ : bool = False , **UpperCAmelCase_ : int) ->List[Any]: '''simple docstring''' lowerCamelCase__: Optional[int] =tokenizer lowerCamelCase__: List[Any] =skip_prompt lowerCamelCase__: Dict =decode_kwargs # variables used in the streaming process lowerCamelCase__: Optional[int] =[] lowerCamelCase__: List[str] =0 lowerCamelCase__: List[Any] =True def SCREAMING_SNAKE_CASE_ (self : Any , UpperCAmelCase_ : List[Any]) ->Any: '''simple docstring''' if len(value.shape) > 1 and value.shape[0] > 1: raise ValueError("TextStreamer only supports batch size 1") elif len(value.shape) > 1: lowerCamelCase__: Optional[int] =value[0] if self.skip_prompt and self.next_tokens_are_prompt: lowerCamelCase__: Any =False return # Add the new token to the cache and decodes the entire thing. self.token_cache.extend(value.tolist()) lowerCamelCase__: int =self.tokenizer.decode(self.token_cache , **self.decode_kwargs) # After the symbol for a new line, we flush the cache. if text.endswith("\n"): lowerCamelCase__: Any =text[self.print_len :] lowerCamelCase__: Union[str, Any] =[] lowerCamelCase__: str =0 # If the last token is a CJK character, we print the characters. elif len(UpperCAmelCase_) > 0 and self._is_chinese_char(ord(text[-1])): lowerCamelCase__: Any =text[self.print_len :] self.print_len += len(UpperCAmelCase_) # Otherwise, prints until the last space char (simple heuristic to avoid printing incomplete words, # which may change with the subsequent token -- there are probably smarter ways to do this!) else: lowerCamelCase__: str =text[self.print_len : text.rfind(" ") + 1] self.print_len += len(UpperCAmelCase_) self.on_finalized_text(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ (self : Optional[Any]) ->List[Any]: '''simple docstring''' if len(self.token_cache) > 0: lowerCamelCase__: Tuple =self.tokenizer.decode(self.token_cache , **self.decode_kwargs) lowerCamelCase__: Optional[int] =text[self.print_len :] lowerCamelCase__: List[str] =[] lowerCamelCase__: Tuple =0 else: lowerCamelCase__: str ="" lowerCamelCase__: List[str] =True self.on_finalized_text(UpperCAmelCase_ , stream_end=UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ (self : List[Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : bool = False) ->int: '''simple docstring''' print(UpperCAmelCase_ , flush=UpperCAmelCase_ , end="" if not stream_end else None) def SCREAMING_SNAKE_CASE_ (self : Tuple , UpperCAmelCase_ : Optional[int]) ->int: '''simple docstring''' if ( (cp >= 0x4E00 and cp <= 0x9FFF) or (cp >= 0x3400 and cp <= 0x4DBF) # or (cp >= 0x2_0000 and cp <= 0x2_A6DF) # or (cp >= 0x2_A700 and cp <= 0x2_B73F) # or (cp >= 0x2_B740 and cp <= 0x2_B81F) # or (cp >= 0x2_B820 and cp <= 0x2_CEAF) # or (cp >= 0xF900 and cp <= 0xFAFF) or (cp >= 0x2_F800 and cp <= 0x2_FA1F) # ): # return True return False class _SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__(self : Union[str, Any] , UpperCAmelCase_ : "AutoTokenizer" , UpperCAmelCase_ : bool = False , UpperCAmelCase_ : Optional[float] = None , **UpperCAmelCase_ : Optional[int]) ->Optional[Any]: '''simple docstring''' super().__init__(UpperCAmelCase_ , UpperCAmelCase_ , **UpperCAmelCase_) lowerCamelCase__: Any =Queue() lowerCamelCase__: Any =None lowerCamelCase__: int =timeout def SCREAMING_SNAKE_CASE_ (self : Optional[int] , UpperCAmelCase_ : str , UpperCAmelCase_ : bool = False) ->Union[str, Any]: '''simple docstring''' self.text_queue.put(UpperCAmelCase_ , timeout=self.timeout) if stream_end: self.text_queue.put(self.stop_signal , timeout=self.timeout) def __iter__(self : List[str]) ->Dict: '''simple docstring''' return self def SCREAMING_SNAKE_CASE_ (self : Any) ->List[Any]: '''simple docstring''' lowerCamelCase__: List[Any] =self.text_queue.get(timeout=self.timeout) if value == self.stop_signal: raise StopIteration() else: return value
10
import os from typing import Dict, List, Tuple, TypeVar, Union __snake_case = TypeVar('''T''') __snake_case = Union[List[T], Tuple[T, ...]] __snake_case = Union[T, List[T], Dict[str, T]] __snake_case = Union[str, bytes, os.PathLike]
348
0
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, Features, Value from .base import TaskTemplate @dataclass(frozen=a) class lowerCAmelCase__ ( a): '''simple docstring''' __SCREAMING_SNAKE_CASE = field(default="automatic-speech-recognition" , metadata={"include_in_asdict_even_if_is_default": True}) __SCREAMING_SNAKE_CASE = Features({"audio": Audio()}) __SCREAMING_SNAKE_CASE = Features({"transcription": Value("string")}) __SCREAMING_SNAKE_CASE = "audio" __SCREAMING_SNAKE_CASE = "transcription" def _lowerCamelCase ( self , __lowerCamelCase) -> Any: 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.") _A : Optional[int] = copy.deepcopy(self) _A : List[str] = self.input_schema.copy() _A : Any = features[self.audio_column] _A : Tuple = input_schema return task_template @property def _lowerCamelCase ( self) -> Dict[str, str]: return {self.audio_column: "audio", self.transcription_column: "transcription"}
11
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_big_bird import BigBirdTokenizer else: __snake_case = None __snake_case = logging.get_logger(__name__) __snake_case = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} __snake_case = { '''vocab_file''': { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model''', '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model''' ), }, '''tokenizer_file''': { '''google/bigbird-roberta-base''': ( '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/tokenizer.json''' ), '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/tokenizer.json''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/tokenizer.json''' ), }, } __snake_case = { '''google/bigbird-roberta-base''': 40_96, '''google/bigbird-roberta-large''': 40_96, '''google/bigbird-base-trivia-itc''': 40_96, } __snake_case = '''▁''' class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Dict = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = BigBirdTokenizer __lowerCamelCase : Any = ["""input_ids""", """attention_mask"""] __lowerCamelCase : List[int] = [] def __init__( self , snake_case__=None , snake_case__=None , snake_case__="<unk>" , snake_case__="<s>" , snake_case__="</s>" , snake_case__="<pad>" , snake_case__="[SEP]" , snake_case__="[MASK]" , snake_case__="[CLS]" , **snake_case__ , ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else bos_token UpperCAmelCase : Optional[int] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else eos_token UpperCAmelCase : List[str] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else unk_token UpperCAmelCase : Union[str, Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else pad_token UpperCAmelCase : int =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else cls_token UpperCAmelCase : str =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else sep_token # Mask token behave like a normal word, i.e. include the space before it UpperCAmelCase : List[Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else mask_token super().__init__( snake_case__ , tokenizer_file=snake_case__ , bos_token=snake_case__ , eos_token=snake_case__ , unk_token=snake_case__ , sep_token=snake_case__ , pad_token=snake_case__ , cls_token=snake_case__ , mask_token=snake_case__ , **snake_case__ , ) UpperCAmelCase : Tuple =vocab_file UpperCAmelCase : Optional[int] =False if not self.vocab_file else True def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : int =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None , snake_case__ = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is None: return [1] + ([0] * len(snake_case__ )) + [1] return [1] + ([0] * len(snake_case__ )) + [1] + ([0] * len(snake_case__ )) + [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : Optional[Any] =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> Tuple[str]: '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(snake_case__ ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCAmelCase : Optional[int] =os.path.join( snake_case__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case__ ): copyfile(self.vocab_file , snake_case__ ) return (out_vocab_file,)
348
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available UpperCAmelCase_ = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ = ['MLukeTokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mluke import MLukeTokenizer else: import sys UpperCAmelCase_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
12
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def lowerCAmelCase_ ( __lowerCAmelCase )-> Optional[Any]: '''simple docstring''' def is_in_circle(__lowerCAmelCase , __lowerCAmelCase ) -> bool: UpperCAmelCase : List[Any] =sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle UpperCAmelCase : List[Any] =mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(__lowerCAmelCase ) ) # The ratio of the area for circle to square is pi/4. UpperCAmelCase : Dict =proportion * 4 print(f'''The estimated value of pi is {pi_estimate}''' ) print(f'''The numpy value of pi is {pi}''' ) print(f'''The total error is {abs(pi - pi_estimate )}''' ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 , )-> float: '''simple docstring''' return mean( function_to_integrate(uniform(__lowerCAmelCase , __lowerCAmelCase ) ) for _ in range(__lowerCAmelCase ) ) * (max_value - min_value) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 )-> None: '''simple docstring''' def identity_function(__lowerCAmelCase ) -> float: return x UpperCAmelCase : List[Any] =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase : Dict =(max_value * max_value - min_value * min_value) / 2 print('''******************''' ) print(f'''Estimating area under y=x where x varies from {min_value} to {max_value}''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {expected_value}''' ) print(f'''Total error is {abs(estimated_value - expected_value )}''' ) print('''******************''' ) def lowerCAmelCase_ ( __lowerCAmelCase )-> None: '''simple docstring''' def function_to_integrate(__lowerCAmelCase ) -> float: return sqrt(4.0 - x * x ) UpperCAmelCase : Dict =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , 0.0 , 2.0 ) print('''******************''' ) print('''Estimating pi using area_under_curve_estimator''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {pi}''' ) print(f'''Total error is {abs(estimated_value - pi )}''' ) print('''******************''' ) if __name__ == "__main__": import doctest doctest.testmod()
348
0
def A_ ( _UpperCAmelCase = 10_00 ): return sum(e for e in range(3 , _UpperCAmelCase ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(f'''{solution() = }''')
13
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __snake_case : def __init__( self , snake_case__ , snake_case__=12 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , snake_case__=0 , snake_case__=None , ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : List[Any] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Union[str, Any] =use_input_mask UpperCAmelCase : Tuple =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : Tuple =hidden_size UpperCAmelCase : Dict =projection_dim UpperCAmelCase : Optional[int] =num_hidden_layers UpperCAmelCase : Dict =num_attention_heads UpperCAmelCase : int =intermediate_size UpperCAmelCase : Any =dropout UpperCAmelCase : Union[str, Any] =attention_dropout UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : str =scope UpperCAmelCase : str =bos_token_id def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : int =None if self.use_input_mask: UpperCAmelCase : Union[str, Any] =random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: UpperCAmelCase : Optional[int] =input_mask.numpy() UpperCAmelCase , UpperCAmelCase : List[Any] =input_mask.shape UpperCAmelCase : Optional[Any] =np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : List[Any] =1 UpperCAmelCase : Tuple =0 UpperCAmelCase : List[Any] =self.get_config() return config, input_ids, tf.convert_to_tensor(snake_case__ ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =TFBlipTextModel(config=snake_case__ ) UpperCAmelCase : List[Any] =model(snake_case__ , attention_mask=snake_case__ , training=snake_case__ ) UpperCAmelCase : str =model(snake_case__ , training=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[Any] =config_and_inputs UpperCAmelCase : Optional[int] ={'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __snake_case ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Optional[int] = (TFBlipTextModel,) if is_tf_available() else () __lowerCamelCase : Dict = False __lowerCamelCase : Optional[Any] = False __lowerCamelCase : Dict = False def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : str =BlipTextModelTester(self ) UpperCAmelCase : Optional[int] =ConfigTester(self , config_class=snake_case__ , hidden_size=37 ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip(reason='''Blip does not use inputs_embeds''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @slow def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase : Optional[Any] =TFBlipTextModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def UpperCAmelCase__ ( self , snake_case__=True ) -> Any: '''simple docstring''' super().test_pt_tf_model_equivalence(allow_missing_keys=snake_case__ )
348
0
from .glue import GlueDataset, GlueDataTrainingArguments from .language_modeling import ( LineByLineTextDataset, LineByLineWithRefDataset, LineByLineWithSOPTextDataset, TextDataset, TextDatasetForNextSentencePrediction, ) from .squad import SquadDataset, SquadDataTrainingArguments
14
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging __snake_case = logging.get_logger(__name__) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' UpperCAmelCase : Dict =nn.functional.normalize(__lowerCAmelCase ) UpperCAmelCase : Tuple =nn.functional.normalize(__lowerCAmelCase ) return torch.mm(__lowerCAmelCase , normalized_text_embeds.t() ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : List[str] = CLIPConfig __lowerCamelCase : List[Any] = ["""CLIPEncoderLayer"""] def __init__( self , snake_case__ ) -> Dict: '''simple docstring''' super().__init__(snake_case__ ) UpperCAmelCase : Dict =CLIPVisionModel(config.vision_config ) UpperCAmelCase : Optional[Any] =nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=snake_case__ ) UpperCAmelCase : int =nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : List[str] =nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : str =nn.Parameter(torch.ones(17 ) , requires_grad=snake_case__ ) UpperCAmelCase : Optional[int] =nn.Parameter(torch.ones(3 ) , requires_grad=snake_case__ ) @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Union[str, Any] =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : Optional[Any] =self.visual_projection(snake_case__ ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 UpperCAmelCase : List[str] =cosine_distance(snake_case__ , self.special_care_embeds ).cpu().float().numpy() UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ).cpu().float().numpy() UpperCAmelCase : Tuple =[] UpperCAmelCase : Dict =image_embeds.shape[0] for i in range(snake_case__ ): UpperCAmelCase : str ={'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : str =0.0 for concept_idx in range(len(special_cos_dist[0] ) ): UpperCAmelCase : Optional[Any] =special_cos_dist[i][concept_idx] UpperCAmelCase : Union[str, Any] =self.special_care_embeds_weights[concept_idx].item() UpperCAmelCase : str =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} ) UpperCAmelCase : int =0.01 for concept_idx in range(len(cos_dist[0] ) ): UpperCAmelCase : Any =cos_dist[i][concept_idx] UpperCAmelCase : Optional[int] =self.concept_embeds_weights[concept_idx].item() UpperCAmelCase : int =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(snake_case__ ) result.append(snake_case__ ) UpperCAmelCase : Optional[int] =[len(res['''bad_concepts'''] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Any =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : List[str] =self.visual_projection(snake_case__ ) UpperCAmelCase : Any =cosine_distance(snake_case__ , self.special_care_embeds ) UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : Optional[Any] =0.0 UpperCAmelCase : Any =special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) UpperCAmelCase : str =torch.any(special_scores > 0 , dim=1 ) UpperCAmelCase : List[Any] =special_care * 0.01 UpperCAmelCase : Union[str, Any] =special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) UpperCAmelCase : List[Any] =(cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) UpperCAmelCase : str =torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
348
0
# 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 SCREAMING_SNAKE_CASE :Union[str, Any] = 'Create a default config file for Accelerate with only a few flags set.' def UpperCAmelCase ( a_="no" , a_ = default_json_config_file , a_ = False ) -> Optional[int]: """simple docstring""" __A = Path(a_ ) path.parent.mkdir(parents=a_ , exist_ok=a_ ) if path.exists(): print( F'''Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.''' ) return False __A = mixed_precision.lower() if mixed_precision not in ["no", "fp16", "bf16", "fp8"]: raise ValueError( F'''`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}''' ) __A = { "compute_environment": "LOCAL_MACHINE", "mixed_precision": mixed_precision, } if torch.cuda.is_available(): __A = torch.cuda.device_count() __A = num_gpus __A = False if num_gpus > 1: __A = "MULTI_GPU" else: __A = "NO" elif is_xpu_available() and use_xpu: __A = torch.xpu.device_count() __A = num_xpus __A = False if num_xpus > 1: __A = "MULTI_XPU" else: __A = "NO" elif is_npu_available(): __A = torch.npu.device_count() __A = num_npus __A = False if num_npus > 1: __A = "MULTI_NPU" else: __A = "NO" else: __A = 0 __A = True __A = 1 __A = "NO" __A = ClusterConfig(**a_ ) config.to_json_file(a_ ) return path def UpperCAmelCase ( a_ , a_ ) -> List[Any]: """simple docstring""" __A = parser.add_parser("default" , parents=a_ , help=a_ , formatter_class=a_ ) parser.add_argument( "--config_file" , default=a_ , 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=a_ , 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=a_ ) return parser def UpperCAmelCase ( a_ ) -> Dict: """simple docstring""" __A = write_basic_config(args.mixed_precision , args.save_location ) if config_file: print(F'''accelerate configuration saved at {config_file}''' )
15
import argparse import intel_extension_for_pytorch as ipex import torch from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline __snake_case = argparse.ArgumentParser('''Stable Diffusion script with intel optimization''', add_help=False) parser.add_argument('''--dpm''', action='''store_true''', help='''Enable DPMSolver or not''') parser.add_argument('''--steps''', default=None, type=int, help='''Num inference steps''') __snake_case = parser.parse_args() __snake_case = '''cpu''' __snake_case = '''a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings''' __snake_case = '''path-to-your-trained-model''' __snake_case = StableDiffusionPipeline.from_pretrained(model_id) if args.dpm: __snake_case = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) __snake_case = pipe.to(device) # to channels last __snake_case = pipe.unet.to(memory_format=torch.channels_last) __snake_case = pipe.vae.to(memory_format=torch.channels_last) __snake_case = pipe.text_encoder.to(memory_format=torch.channels_last) if pipe.requires_safety_checker: __snake_case = pipe.safety_checker.to(memory_format=torch.channels_last) # optimize with ipex __snake_case = torch.randn(2, 4, 64, 64) __snake_case = torch.rand(1) * 9_99 __snake_case = torch.randn(2, 77, 7_68) __snake_case = (sample, timestep, encoder_hidden_status) try: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example) except Exception: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True) if pipe.requires_safety_checker: __snake_case = ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True) # compute __snake_case = 6_66 __snake_case = torch.Generator(device).manual_seed(seed) __snake_case = {'''generator''': generator} if args.steps is not None: __snake_case = args.steps with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa): __snake_case = pipe(prompt, **generate_kwargs).images[0] # save image image.save('''generated.png''')
348
0
"""simple docstring""" import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = '▁' lowerCAmelCase_ = { 'vocab_file': 'vocab.json', 'spm_file': 'sentencepiece.bpe.model', } lowerCAmelCase_ = { 'vocab_file': { 'facebook/s2t-small-librispeech-asr': ( 'https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/vocab.json' ), }, 'spm_file': { 'facebook/s2t-small-librispeech-asr': ( 'https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/sentencepiece.bpe.model' ) }, } lowerCAmelCase_ = { 'facebook/s2t-small-librispeech-asr': 1_024, } lowerCAmelCase_ = ['pt', 'fr', 'ru', 'nl', 'ro', 'it', 'es', 'de'] lowerCAmelCase_ = {'mustc': MUSTC_LANGS} class __A ( A_ ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = VOCAB_FILES_NAMES lowerCAmelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase : int = MAX_MODEL_INPUT_SIZES lowerCAmelCase : Optional[int] = ["input_ids", "attention_mask"] lowerCAmelCase : List[int] = [] def __init__( self : Optional[Any] ,_snake_case : int ,_snake_case : Optional[int] ,_snake_case : Optional[int]="<s>" ,_snake_case : Union[str, Any]="</s>" ,_snake_case : Any="<pad>" ,_snake_case : Dict="<unk>" ,_snake_case : Optional[Any]=False ,_snake_case : int=False ,_snake_case : Optional[int]=None ,_snake_case : Dict=None ,_snake_case : Optional[Dict[str, Any]] = None ,**_snake_case : int ,) -> None: """simple docstring""" lowercase__ : Union[str, Any] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_snake_case ,eos_token=_snake_case ,unk_token=_snake_case ,pad_token=_snake_case ,do_upper_case=_snake_case ,do_lower_case=_snake_case ,tgt_lang=_snake_case ,lang_codes=_snake_case ,sp_model_kwargs=self.sp_model_kwargs ,**_snake_case ,) lowercase__ : str = do_upper_case lowercase__ : List[str] = do_lower_case lowercase__ : Any = load_json(_snake_case ) lowercase__ : Dict = {v: k for k, v in self.encoder.items()} lowercase__ : Dict = spm_file lowercase__ : List[str] = load_spm(_snake_case ,self.sp_model_kwargs ) if lang_codes is not None: lowercase__ : Optional[Any] = lang_codes lowercase__ : Any = LANGUAGES[lang_codes] lowercase__ : Optional[int] = [f"""<lang:{lang}>""" for lang in self.langs] lowercase__ : Union[str, Any] = {lang: self.sp_model.PieceToId(f"""<lang:{lang}>""" ) for lang in self.langs} lowercase__ : str = self.lang_tokens lowercase__ : Optional[int] = tgt_lang if tgt_lang is not None else self.langs[0] self.set_tgt_lang_special_tokens(self._tgt_lang ) else: lowercase__ : Dict = {} @property def UpperCAmelCase ( self : Tuple ) -> int: """simple docstring""" return len(self.encoder ) @property def UpperCAmelCase ( self : str ) -> str: """simple docstring""" return self._tgt_lang @tgt_lang.setter def UpperCAmelCase ( self : Optional[int] ,_snake_case : List[Any] ) -> None: """simple docstring""" lowercase__ : Optional[Any] = new_tgt_lang self.set_tgt_lang_special_tokens(_snake_case ) def UpperCAmelCase ( self : Optional[int] ,_snake_case : str ) -> None: """simple docstring""" lowercase__ : List[Any] = self.lang_code_to_id[tgt_lang] lowercase__ : str = [lang_code_id] def UpperCAmelCase ( self : Optional[Any] ,_snake_case : str ) -> List[str]: """simple docstring""" return self.sp_model.encode(_snake_case ,out_type=_snake_case ) def UpperCAmelCase ( self : Dict ,_snake_case : Union[str, Any] ) -> Tuple: """simple docstring""" return self.encoder.get(_snake_case ,self.encoder[self.unk_token] ) def UpperCAmelCase ( self : List[str] ,_snake_case : int ) -> str: """simple docstring""" return self.decoder.get(_snake_case ,self.unk_token ) def UpperCAmelCase ( self : List[Any] ,_snake_case : List[str] ) -> str: """simple docstring""" lowercase__ : str = [] lowercase__ : List[str] = '''''' for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: lowercase__ : int = self.sp_model.decode(_snake_case ) out_string += (decoded.upper() if self.do_upper_case else decoded) + token + " " lowercase__ : str = [] else: current_sub_tokens.append(_snake_case ) lowercase__ : Tuple = self.sp_model.decode(_snake_case ) out_string += decoded.upper() if self.do_upper_case else decoded return out_string.strip() def UpperCAmelCase ( self : str ,_snake_case : Tuple ,_snake_case : Any=None ) -> List[int]: """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + [self.eos_token_id] def UpperCAmelCase ( self : Dict ,_snake_case : List[int] ,_snake_case : Optional[List[int]] = None ,_snake_case : bool = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_snake_case ,token_ids_a=_snake_case ,already_has_special_tokens=_snake_case ) lowercase__ : List[Any] = [1] * len(self.prefix_tokens ) lowercase__ : Tuple = [1] if token_ids_a is None: return prefix_ones + ([0] * len(_snake_case )) + suffix_ones return prefix_ones + ([0] * len(_snake_case )) + ([0] * len(_snake_case )) + suffix_ones def UpperCAmelCase ( self : Dict ) -> Dict: """simple docstring""" lowercase__ : Any = self.encoder.copy() vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : int ) -> Dict: """simple docstring""" lowercase__ : int = self.__dict__.copy() lowercase__ : Dict = None return state def __setstate__( self : Union[str, Any] ,_snake_case : Dict ) -> None: """simple docstring""" lowercase__ : Optional[int] = d # for backward compatibility if not hasattr(self ,'''sp_model_kwargs''' ): lowercase__ : int = {} lowercase__ : List[Any] = load_spm(self.spm_file ,self.sp_model_kwargs ) def UpperCAmelCase ( self : Optional[int] ,_snake_case : str ,_snake_case : Optional[str] = None ) -> Tuple[str]: """simple docstring""" lowercase__ : Tuple = Path(_snake_case ) assert save_dir.is_dir(), f"""{save_directory} should be a directory""" lowercase__ : List[Any] = save_dir / ( (filename_prefix + '''-''' if filename_prefix else '''''') + self.vocab_files_names['''vocab_file'''] ) lowercase__ : Optional[Any] = save_dir / ( (filename_prefix + '''-''' if filename_prefix else '''''') + self.vocab_files_names['''spm_file'''] ) save_json(self.encoder ,_snake_case ) if os.path.abspath(self.spm_file ) != os.path.abspath(_snake_case ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file ,_snake_case ) elif not os.path.isfile(self.spm_file ): with open(_snake_case ,'''wb''' ) as fi: lowercase__ : Dict = self.sp_model.serialized_model_proto() fi.write(_snake_case ) return (str(_snake_case ), str(_snake_case )) def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase ) -> sentencepiece.SentencePieceProcessor: lowercase__ : Any = sentencepiece.SentencePieceProcessor(**__lowerCamelCase ) spm.Load(str(__lowerCamelCase ) ) return spm def __UpperCAmelCase ( __lowerCamelCase ) -> Union[Dict, List]: with open(__lowerCamelCase , '''r''' ) as f: return json.load(__lowerCamelCase ) def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase ) -> None: with open(__lowerCamelCase , '''w''' ) as f: json.dump(__lowerCamelCase , __lowerCamelCase , indent=2 )
16
__snake_case = '''Input must be a string of 8 numbers plus letter''' __snake_case = '''TRWAGMYFPDXBNJZSQVHLCKE''' def lowerCAmelCase_ ( __lowerCAmelCase )-> bool: '''simple docstring''' if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCAmelCase : Optional[Any] =f'''Expected string as input, found {type(__lowerCAmelCase ).__name__}''' raise TypeError(__lowerCAmelCase ) UpperCAmelCase : List[Any] =spanish_id.replace('''-''' , '''''' ).upper() if len(__lowerCAmelCase ) != 9: raise ValueError(__lowerCAmelCase ) try: UpperCAmelCase : int =int(spanish_id_clean[0:8] ) UpperCAmelCase : Optional[int] =spanish_id_clean[8] except ValueError as ex: raise ValueError(__lowerCAmelCase ) from ex if letter.isdigit(): raise ValueError(__lowerCAmelCase ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
348
0
"""simple docstring""" from __future__ import annotations def _A ( UpperCamelCase_ : list) -> float: '''simple docstring''' if not nums: raise ValueError("List is empty") return sum(UpperCamelCase_) / len(UpperCamelCase_) if __name__ == "__main__": import doctest doctest.testmod()
17
def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Dict =str(bin(__lowerCAmelCase ) ) binary_number += "0" * shift_amount return binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Any =str(bin(__lowerCAmelCase ) )[2:] if shift_amount >= len(__lowerCAmelCase ): return "0b0" UpperCAmelCase : Optional[Any] =binary_number[: len(__lowerCAmelCase ) - shift_amount] return "0b" + shifted_binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number >= 0: # Get binary representation of positive number UpperCAmelCase : Optional[Any] ='''0''' + str(bin(__lowerCAmelCase ) ).strip('''-''' )[2:] else: # Get binary (2's complement) representation of negative number UpperCAmelCase : int =len(bin(__lowerCAmelCase )[3:] ) # Find 2's complement of number UpperCAmelCase : Any =bin(abs(__lowerCAmelCase ) - (1 << binary_number_length) )[3:] UpperCAmelCase : Optional[Any] =( '''1''' + '''0''' * (binary_number_length - len(__lowerCAmelCase )) + binary_number ) if shift_amount >= len(__lowerCAmelCase ): return "0b" + binary_number[0] * len(__lowerCAmelCase ) return ( "0b" + binary_number[0] * shift_amount + binary_number[: len(__lowerCAmelCase ) - shift_amount] ) if __name__ == "__main__": import doctest doctest.testmod()
348
0
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import VivitImageProcessor class a__ ( unittest.TestCase ): def __init__( self : List[Any],_A : Any,_A : Union[str, Any]=7,_A : Optional[Any]=3,_A : Optional[int]=10,_A : Optional[int]=18,_A : Optional[Any]=30,_A : Optional[Any]=400,_A : int=True,_A : Dict=None,_A : Any=True,_A : List[Any]=[0.5, 0.5, 0.5],_A : Any=[0.5, 0.5, 0.5],_A : str=None,): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = size if size is not None else {"shortest_edge": 18} SCREAMING_SNAKE_CASE_ : str = crop_size if crop_size is not None else {"height": 18, "width": 18} SCREAMING_SNAKE_CASE_ : List[Any] = parent SCREAMING_SNAKE_CASE_ : Union[str, Any] = batch_size SCREAMING_SNAKE_CASE_ : Any = num_channels SCREAMING_SNAKE_CASE_ : Any = num_frames SCREAMING_SNAKE_CASE_ : Tuple = image_size SCREAMING_SNAKE_CASE_ : Any = min_resolution SCREAMING_SNAKE_CASE_ : Dict = max_resolution SCREAMING_SNAKE_CASE_ : Union[str, Any] = do_resize SCREAMING_SNAKE_CASE_ : int = size SCREAMING_SNAKE_CASE_ : Dict = do_normalize SCREAMING_SNAKE_CASE_ : Optional[Any] = image_mean SCREAMING_SNAKE_CASE_ : Tuple = image_std SCREAMING_SNAKE_CASE_ : List[str] = crop_size def __UpperCamelCase ( self : Optional[Any] ): """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class a__ ( A__ , unittest.TestCase ): A = VivitImageProcessor if is_vision_available() else None def __UpperCamelCase ( self : Union[str, Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : str = VivitImageProcessingTester(self ) @property def __UpperCamelCase ( self : Union[str, Any] ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __UpperCamelCase ( self : Union[str, Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_A,"image_mean" ) ) self.assertTrue(hasattr(_A,"image_std" ) ) self.assertTrue(hasattr(_A,"do_normalize" ) ) self.assertTrue(hasattr(_A,"do_resize" ) ) self.assertTrue(hasattr(_A,"do_center_crop" ) ) self.assertTrue(hasattr(_A,"size" ) ) def __UpperCamelCase ( self : str ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size,{"shortest_edge": 18} ) self.assertEqual(image_processor.crop_size,{"height": 18, "width": 18} ) SCREAMING_SNAKE_CASE_ : List[Any] = self.image_processing_class.from_dict(self.image_processor_dict,size=42,crop_size=84 ) self.assertEqual(image_processor.size,{"shortest_edge": 42} ) self.assertEqual(image_processor.crop_size,{"height": 84, "width": 84} ) def __UpperCamelCase ( self : Dict ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PIL videos SCREAMING_SNAKE_CASE_ : List[str] = prepare_video_inputs(self.image_processor_tester,equal_resolution=_A ) for video in video_inputs: self.assertIsInstance(_A,_A ) self.assertIsInstance(video[0],Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE_ : int = image_processing(video_inputs[0],return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape,( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ),) # Test batched SCREAMING_SNAKE_CASE_ : Tuple = image_processing(_A,return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape,( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ),) def __UpperCamelCase ( self : Optional[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE_ : str = prepare_video_inputs(self.image_processor_tester,equal_resolution=_A,numpify=_A ) for video in video_inputs: self.assertIsInstance(_A,_A ) self.assertIsInstance(video[0],np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(video_inputs[0],return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape,( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ),) # Test batched SCREAMING_SNAKE_CASE_ : List[str] = image_processing(_A,return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape,( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ),) def __UpperCamelCase ( self : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE_ : Dict = prepare_video_inputs(self.image_processor_tester,equal_resolution=_A,torchify=_A ) for video in video_inputs: self.assertIsInstance(_A,_A ) self.assertIsInstance(video[0],torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE_ : List[str] = image_processing(video_inputs[0],return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape,( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ),) # Test batched SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(_A,return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape,( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ),)
18
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) # TODO Update this __snake_case = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Tuple = """esm""" def __init__( self , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=1026 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__="absolute" , snake_case__=True , snake_case__=None , snake_case__=False , snake_case__=False , snake_case__=None , snake_case__=None , **snake_case__ , ) -> Union[str, Any]: '''simple docstring''' super().__init__(pad_token_id=snake_case__ , mask_token_id=snake_case__ , **snake_case__ ) UpperCAmelCase : List[str] =vocab_size UpperCAmelCase : str =hidden_size UpperCAmelCase : List[Any] =num_hidden_layers UpperCAmelCase : Optional[Any] =num_attention_heads UpperCAmelCase : str =intermediate_size UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : int =attention_probs_dropout_prob UpperCAmelCase : Dict =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : Union[str, Any] =layer_norm_eps UpperCAmelCase : Dict =position_embedding_type UpperCAmelCase : Optional[Any] =use_cache UpperCAmelCase : int =emb_layer_norm_before UpperCAmelCase : List[str] =token_dropout UpperCAmelCase : Optional[Any] =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) UpperCAmelCase : Optional[Any] =EsmFoldConfig() elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =EsmFoldConfig(**snake_case__ ) UpperCAmelCase : Tuple =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) UpperCAmelCase : Any =get_default_vocab_list() else: UpperCAmelCase : Tuple =vocab_list else: UpperCAmelCase : Optional[int] =None UpperCAmelCase : Union[str, Any] =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , snake_case__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =super().to_dict() if isinstance(self.esmfold_config , snake_case__ ): UpperCAmelCase : str =self.esmfold_config.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : str = None __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : float = 0 __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : int = 128 __lowerCamelCase : "TrunkConfig" = None def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' if self.trunk is None: UpperCAmelCase : str =TrunkConfig() elif isinstance(self.trunk , snake_case__ ): UpperCAmelCase : Optional[int] =TrunkConfig(**self.trunk ) def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[Any] =asdict(self ) UpperCAmelCase : Any =self.trunk.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 48 __lowerCamelCase : int = 1024 __lowerCamelCase : int = 128 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : float = 0 __lowerCamelCase : float = 0 __lowerCamelCase : bool = False __lowerCamelCase : int = 4 __lowerCamelCase : Optional[int] = 128 __lowerCamelCase : "StructureModuleConfig" = None def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' if self.structure_module is None: UpperCAmelCase : Any =StructureModuleConfig() elif isinstance(self.structure_module , snake_case__ ): UpperCAmelCase : str =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f'''`max_recycles` should be positive, got {self.max_recycles}.''' ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f''' {self.sequence_state_dim} and {self.sequence_state_dim}.''' ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f''' {self.pairwise_state_dim} and {self.pairwise_state_dim}.''' ) UpperCAmelCase : Optional[int] =self.sequence_state_dim // self.sequence_head_width UpperCAmelCase : Any =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f''' {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.''' ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f''' {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.''' ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f'''`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.''' ) if self.dropout >= 0.4: raise ValueError(f'''`dropout` should not be greater than 0.4, got {self.dropout}.''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =asdict(self ) UpperCAmelCase : Tuple =self.structure_module.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 384 __lowerCamelCase : int = 128 __lowerCamelCase : int = 16 __lowerCamelCase : int = 128 __lowerCamelCase : int = 12 __lowerCamelCase : int = 4 __lowerCamelCase : int = 8 __lowerCamelCase : float = 0.1 __lowerCamelCase : int = 8 __lowerCamelCase : int = 1 __lowerCamelCase : int = 2 __lowerCamelCase : int = 7 __lowerCamelCase : int = 10 __lowerCamelCase : float = 1E-8 __lowerCamelCase : float = 1E5 def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return asdict(self ) def lowerCAmelCase_ ( )-> Tuple: '''simple docstring''' return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
348
0
import unittest from transformers import EsmConfig, is_torch_available from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.esm.modeling_esmfold import EsmForProteinFolding class _SCREAMING_SNAKE_CASE : def __init__( self , lowercase , lowercase=13 , lowercase=7 , lowercase=False , lowercase=True , lowercase=False , lowercase=False , lowercase=19 , lowercase=32 , lowercase=5 , lowercase=4 , lowercase=37 , lowercase="gelu" , lowercase=0.1 , lowercase=0.1 , lowercase=512 , lowercase=16 , lowercase=2 , lowercase=0.0_2 , lowercase=3 , lowercase=4 , lowercase=None , ) -> List[Any]: lowerCamelCase_ = parent lowerCamelCase_ = batch_size lowerCamelCase_ = seq_length lowerCamelCase_ = is_training lowerCamelCase_ = use_input_mask lowerCamelCase_ = use_token_type_ids lowerCamelCase_ = use_labels lowerCamelCase_ = vocab_size 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_ = max_position_embeddings lowerCamelCase_ = type_vocab_size lowerCamelCase_ = type_sequence_label_size lowerCamelCase_ = initializer_range lowerCamelCase_ = num_labels lowerCamelCase_ = num_choices lowerCamelCase_ = scope def SCREAMING_SNAKE_CASE_( self ) -> Tuple: lowerCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase_ = None if self.use_input_mask: lowerCamelCase_ = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase_ = None lowerCamelCase_ = None lowerCamelCase_ = None if self.use_labels: lowerCamelCase_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowerCamelCase_ = ids_tensor([self.batch_size] , self.num_choices ) lowerCamelCase_ = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE_( self ) -> Optional[Any]: lowerCamelCase_ = EsmConfig( vocab_size=33 , hidden_size=self.hidden_size , pad_token_id=1 , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , is_folding_model=lowercase , esmfold_config={"trunk": {"num_blocks": 2}, "fp16_esm": False} , ) return config def SCREAMING_SNAKE_CASE_( self , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase ) -> int: lowerCamelCase_ = EsmForProteinFolding(config=lowercase ).float() model.to(lowercase ) model.eval() lowerCamelCase_ = model(lowercase , attention_mask=lowercase ) lowerCamelCase_ = model(lowercase ) lowerCamelCase_ = model(lowercase ) self.parent.assertEqual(result.positions.shape , (8, self.batch_size, self.seq_length, 14, 3) ) self.parent.assertEqual(result.angles.shape , (8, self.batch_size, self.seq_length, 7, 2) ) def SCREAMING_SNAKE_CASE_( self ) -> Dict: lowerCamelCase_ = self.prepare_config_and_inputs() ( ( lowerCamelCase_ ) , ( lowerCamelCase_ ) , ( lowerCamelCase_ ) , ( lowerCamelCase_ ) , ( lowerCamelCase_ ) , ( lowerCamelCase_ ) , ) = config_and_inputs lowerCamelCase_ = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class _SCREAMING_SNAKE_CASE ( snake_case_ , snake_case_ , unittest.TestCase ): lowerCAmelCase__ = False lowerCAmelCase__ = (EsmForProteinFolding,) if is_torch_available() else () lowerCAmelCase__ = () lowerCAmelCase__ = {} if is_torch_available() else {} lowerCAmelCase__ = False def SCREAMING_SNAKE_CASE_( self ) -> Optional[Any]: lowerCamelCase_ = EsmFoldModelTester(self ) lowerCamelCase_ = ConfigTester(self , config_class=lowercase , hidden_size=37 ) def SCREAMING_SNAKE_CASE_( self ) -> Tuple: self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_( self ) -> Optional[int]: lowerCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowercase ) @unittest.skip("Does not support attention outputs" ) def SCREAMING_SNAKE_CASE_( self ) -> List[str]: pass @unittest.skip def SCREAMING_SNAKE_CASE_( self ) -> Dict: pass @unittest.skip("Esm does not support embedding resizing" ) def SCREAMING_SNAKE_CASE_( self ) -> Union[str, Any]: pass @unittest.skip("Esm does not support embedding resizing" ) def SCREAMING_SNAKE_CASE_( self ) -> List[Any]: pass @unittest.skip("ESMFold does not support passing input embeds!" ) def SCREAMING_SNAKE_CASE_( self ) -> List[Any]: pass @unittest.skip("ESMFold does not support head pruning." ) def SCREAMING_SNAKE_CASE_( self ) -> Union[str, Any]: pass @unittest.skip("ESMFold does not support head pruning." ) def SCREAMING_SNAKE_CASE_( self ) -> Any: pass @unittest.skip("ESMFold does not support head pruning." ) def SCREAMING_SNAKE_CASE_( self ) -> Any: pass @unittest.skip("ESMFold does not support head pruning." ) def SCREAMING_SNAKE_CASE_( self ) -> Tuple: pass @unittest.skip("ESMFold does not support head pruning." ) def SCREAMING_SNAKE_CASE_( self ) -> int: pass @unittest.skip("ESMFold does not output hidden states in the normal way." ) def SCREAMING_SNAKE_CASE_( self ) -> Any: pass @unittest.skip("ESMfold does not output hidden states in the normal way." ) def SCREAMING_SNAKE_CASE_( self ) -> int: pass @unittest.skip("ESMFold only has one output format." ) def SCREAMING_SNAKE_CASE_( self ) -> Union[str, Any]: pass @unittest.skip("This test doesn't work for ESMFold and doesn't test core functionality" ) def SCREAMING_SNAKE_CASE_( self ) -> Dict: pass @unittest.skip("ESMFold does not support input chunking." ) def SCREAMING_SNAKE_CASE_( self ) -> Optional[Any]: pass @unittest.skip("ESMFold doesn't respect you and it certainly doesn't respect your initialization arguments." ) def SCREAMING_SNAKE_CASE_( self ) -> Any: pass @unittest.skip("ESMFold doesn't support torchscript compilation." ) def SCREAMING_SNAKE_CASE_( self ) -> Tuple: pass @unittest.skip("ESMFold doesn't support torchscript compilation." ) def SCREAMING_SNAKE_CASE_( self ) -> List[Any]: pass @unittest.skip("ESMFold doesn't support torchscript compilation." ) def SCREAMING_SNAKE_CASE_( self ) -> Optional[Any]: pass @unittest.skip("ESMFold doesn't support data parallel." ) def SCREAMING_SNAKE_CASE_( self ) -> Tuple: pass @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def SCREAMING_SNAKE_CASE_( self ) -> Tuple: pass @require_torch class _SCREAMING_SNAKE_CASE ( snake_case_ ): @slow def SCREAMING_SNAKE_CASE_( self ) -> Optional[Any]: lowerCamelCase_ = EsmForProteinFolding.from_pretrained("facebook/esmfold_v1" ).float() model.eval() lowerCamelCase_ = torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) lowerCamelCase_ = model(lowercase )["positions"] lowerCamelCase_ = torch.tensor([2.5_8_2_8, 0.7_9_9_3, -1_0.9_3_3_4] , dtype=torch.floataa ) self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0] , lowercase , atol=1e-4 ) )
19
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[int] = (KDPMaDiscreteScheduler,) __lowerCamelCase : List[str] = 10 def UpperCAmelCase__ ( self , **snake_case__ ) -> str: '''simple docstring''' UpperCAmelCase : int ={ '''num_train_timesteps''': 1100, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', } config.update(**snake_case__ ) return config def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=snake_case__ , beta_end=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' UpperCAmelCase : Optional[Any] =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config(prediction_type='''v_prediction''' ) UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : str =self.dummy_model() UpperCAmelCase : Optional[Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : Union[str, Any] =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : str =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Any =model(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : int =output.prev_sample UpperCAmelCase : Dict =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Optional[Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.69_34e-07 ) < 1e-2 assert abs(result_mean.item() - 6.11_12e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.6_93_42_86_50_17_09_72e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0002 ) < 1e-3 def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : Any =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config() UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : Optional[int] =self.dummy_model() UpperCAmelCase : Union[str, Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : str =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : Dict =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =model(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =output.prev_sample UpperCAmelCase : Any =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Union[str, Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : List[Any] =self.scheduler_classes[0] UpperCAmelCase : Dict =self.get_scheduler_config() UpperCAmelCase : List[str] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps , device=snake_case__ ) UpperCAmelCase : int =self.dummy_model() UpperCAmelCase : Tuple =self.dummy_sample_deter.to(snake_case__ ) * scheduler.init_noise_sigma for t in scheduler.timesteps: UpperCAmelCase : Optional[Any] =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : int =model(snake_case__ , snake_case__ ) UpperCAmelCase : str =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =output.prev_sample UpperCAmelCase : List[str] =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Dict =torch.mean(torch.abs(snake_case__ ) ) if str(snake_case__ ).startswith('''cpu''' ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3
348
0
import argparse from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection from diffusers import UnCLIPImageVariationPipeline, UnCLIPPipeline if __name__ == "__main__": lowercase : Any = argparse.ArgumentParser() parser.add_argument("""--dump_path""", default=None, type=str, required=True, help="""Path to the output model.""") parser.add_argument( """--txt2img_unclip""", default="""kakaobrain/karlo-v1-alpha""", type=str, required=False, help="""The pretrained txt2img unclip.""", ) lowercase : List[Any] = parser.parse_args() lowercase : List[Any] = UnCLIPPipeline.from_pretrained(args.txtaimg_unclip) lowercase : Any = CLIPImageProcessor() lowercase : Any = CLIPVisionModelWithProjection.from_pretrained("""openai/clip-vit-large-patch14""") lowercase : Tuple = UnCLIPImageVariationPipeline( decoder=txtaimg.decoder, text_encoder=txtaimg.text_encoder, tokenizer=txtaimg.tokenizer, text_proj=txtaimg.text_proj, feature_extractor=feature_extractor, image_encoder=image_encoder, super_res_first=txtaimg.super_res_first, super_res_last=txtaimg.super_res_last, decoder_scheduler=txtaimg.decoder_scheduler, super_res_scheduler=txtaimg.super_res_scheduler, ) imgaimg.save_pretrained(args.dump_path)
20
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class __snake_case ( unittest.TestCase ): @slow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Any =FlaxMTaForConditionalGeneration.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : Tuple =AutoTokenizer.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : List[str] =tokenizer('''Hello there''' , return_tensors='''np''' ).input_ids UpperCAmelCase : List[Any] =tokenizer('''Hi I am''' , return_tensors='''np''' ).input_ids UpperCAmelCase : Union[str, Any] =shift_tokens_right(snake_case__ , model.config.pad_token_id , model.config.decoder_start_token_id ) UpperCAmelCase : List[str] =model(snake_case__ , decoder_input_ids=snake_case__ ).logits UpperCAmelCase : Any =optax.softmax_cross_entropy(snake_case__ , onehot(snake_case__ , logits.shape[-1] ) ).mean() UpperCAmelCase : Union[str, Any] =-(labels.shape[-1] * loss.item()) UpperCAmelCase : List[str] =-84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
348
0
from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer SCREAMING_SNAKE_CASE : Any = logging.get_logger(__name__) SCREAMING_SNAKE_CASE : Tuple = { "vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_config_file": "tokenizer_config.json", } SCREAMING_SNAKE_CASE : List[str] = { "vocab_file": { "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json" }, "merges_file": { "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt" }, "tokenizer_config_file": { "facebook/blenderbot_small-90M": ( "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json" ) }, } SCREAMING_SNAKE_CASE : int = { "facebook/blenderbot_small-90M": 512, } class _lowerCamelCase( _a ): lowercase_ : Tuple = VOCAB_FILES_NAMES lowercase_ : int = PRETRAINED_VOCAB_FILES_MAP lowercase_ : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ : List[Any] = BlenderbotSmallTokenizer def __init__( self, lowerCamelCase=None, lowerCamelCase=None, lowerCamelCase="<|endoftext|>", lowerCamelCase="<|endoftext|>", lowerCamelCase="<|endoftext|>", lowerCamelCase=False, lowerCamelCase=True, **lowerCamelCase, ) -> Tuple: """simple docstring""" super().__init__( ByteLevelBPETokenizer( vocab=lowerCamelCase, merges=lowerCamelCase, add_prefix_space=lowerCamelCase, trim_offsets=lowerCamelCase, ), bos_token=lowerCamelCase, eos_token=lowerCamelCase, unk_token=lowerCamelCase, **lowerCamelCase, ) _lowercase : Tuple = add_prefix_space def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase=None) -> int: """simple docstring""" _lowercase : List[Any] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase = None) -> List[int]: """simple docstring""" _lowercase : Tuple = [self.sep_token_id] _lowercase : Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep) * [0]
21
import unittest import numpy as np from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __snake_case ( lowerCamelCase__ , unittest.TestCase ): # FIXME: add fast tests pass @nightly @require_onnxruntime @require_torch_gpu class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : List[Any] =ort.SessionOptions() UpperCAmelCase : Optional[int] =False return options def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : int =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Optional[Any] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Dict ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : Any =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=10 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : Optional[int] =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Tuple =np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Tuple =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , subfolder='''scheduler''' , revision='''onnx''' ) UpperCAmelCase : int =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , scheduler=snake_case__ , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Union[str, Any] ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : str =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=20 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : int =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Union[str, Any] =np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3
348
0
'''simple docstring''' import sys from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers __SCREAMING_SNAKE_CASE :int = '''python tqdm regex requests packaging filelock numpy tokenizers'''.split() if sys.version_info < (3, 7): pkgs_to_check_at_runtime.append('''dataclasses''') if sys.version_info < (3, 8): pkgs_to_check_at_runtime.append('''importlib_metadata''') for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(F"can't find {pkg} in {deps.keys()}, check dependency_versions_table.py") def UpperCAmelCase_ ( __lowercase : Union[str, Any] , __lowercase : str=None ) -> int: '''simple docstring''' require_version(deps[pkg] , __lowercase )
22
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def lowerCAmelCase_ ( )-> int: '''simple docstring''' UpperCAmelCase : str ={ '''repo_name''': ['''test_repo1''', '''test_repo2''', '''test_repo3'''], '''path''': ['''test_1.py''', '''test_2.py''', '''unit_test.py'''], '''content''': ['''a ''' * 20, '''a ''' * 30, '''b ''' * 7], } UpperCAmelCase : Union[str, Any] =Dataset.from_dict(__lowerCAmelCase ) return dataset class __snake_case ( lowerCamelCase__ ): def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[str] =get_dataset() UpperCAmelCase : Optional[int] =make_duplicate_clusters(snake_case__ , 0.85 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : str =get_dataset() UpperCAmelCase , UpperCAmelCase : Tuple =deduplicate_dataset(snake_case__ ) self.assertEqual(len(snake_case__ ) , 2 ) print(snake_case__ ) self.assertEqual(duplicate_clusters[0][0]['''copies'''] , 2 ) self.assertEqual(duplicate_clusters[0][0]['''is_extreme'''] , snake_case__ )
348
0
'''simple docstring''' import os import sys import unittest UpperCamelCase__: Tuple = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_dummies # noqa: E402 from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init # noqa: E402 # Align TRANSFORMERS_PATH in check_dummies with the current path UpperCamelCase__: int = os.path.join(git_repo_path, "src", "diffusers") class SCREAMING_SNAKE_CASE( unittest.TestCase ): """simple docstring""" def A ( self : str ) -> List[Any]: UpperCAmelCase : Tuple = find_backend(''' if not is_torch_available():''' ) self.assertEqual(__snake_case , '''torch''' ) # backend_with_underscore = find_backend(" if not is_tensorflow_text_available():") # self.assertEqual(backend_with_underscore, "tensorflow_text") UpperCAmelCase : Optional[Any] = find_backend(''' if not (is_torch_available() and is_transformers_available()):''' ) self.assertEqual(__snake_case , '''torch_and_transformers''' ) # double_backend_with_underscore = find_backend( # " if not (is_sentencepiece_available() and is_tensorflow_text_available()):" # ) # self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text") UpperCAmelCase : int = find_backend( ''' if not (is_torch_available() and is_transformers_available() and is_onnx_available()):''' ) self.assertEqual(__snake_case , '''torch_and_transformers_and_onnx''' ) def A ( self : Optional[int] ) -> Dict: UpperCAmelCase : Any = read_init() # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects self.assertIn('''torch''' , __snake_case ) self.assertIn('''torch_and_transformers''' , __snake_case ) self.assertIn('''flax_and_transformers''' , __snake_case ) self.assertIn('''torch_and_transformers_and_onnx''' , __snake_case ) # Likewise, we can't assert on the exact content of a key self.assertIn('''UNet2DModel''' , objects['''torch'''] ) self.assertIn('''FlaxUNet2DConditionModel''' , objects['''flax'''] ) self.assertIn('''StableDiffusionPipeline''' , objects['''torch_and_transformers'''] ) self.assertIn('''FlaxStableDiffusionPipeline''' , objects['''flax_and_transformers'''] ) self.assertIn('''LMSDiscreteScheduler''' , objects['''torch_and_scipy'''] ) self.assertIn('''OnnxStableDiffusionPipeline''' , objects['''torch_and_transformers_and_onnx'''] ) def A ( self : List[str] ) -> Dict: UpperCAmelCase : Optional[int] = create_dummy_object('''CONSTANT''' , '''\'torch\'''' ) self.assertEqual(__snake_case , '''\nCONSTANT = None\n''' ) UpperCAmelCase : Optional[int] = create_dummy_object('''function''' , '''\'torch\'''' ) self.assertEqual( __snake_case , '''\ndef function(*args, **kwargs):\n requires_backends(function, \'torch\')\n''' ) UpperCAmelCase : Optional[int] = ''' class FakeClass(metaclass=DummyObject): _backends = \'torch\' def __init__(self, *args, **kwargs): requires_backends(self, \'torch\') @classmethod def from_config(cls, *args, **kwargs): requires_backends(cls, \'torch\') @classmethod def from_pretrained(cls, *args, **kwargs): requires_backends(cls, \'torch\') ''' UpperCAmelCase : List[Any] = create_dummy_object('''FakeClass''' , '''\'torch\'''' ) self.assertEqual(__snake_case , __snake_case ) def A ( self : str ) -> Optional[int]: UpperCAmelCase : Tuple = '''# This file is autogenerated by the command `make fix-copies`, do not edit. from ..utils import DummyObject, requires_backends CONSTANT = None def function(*args, **kwargs): requires_backends(function, ["torch"]) class FakeClass(metaclass=DummyObject): _backends = ["torch"] def __init__(self, *args, **kwargs): requires_backends(self, ["torch"]) @classmethod def from_config(cls, *args, **kwargs): requires_backends(cls, ["torch"]) @classmethod def from_pretrained(cls, *args, **kwargs): requires_backends(cls, ["torch"]) ''' UpperCAmelCase : Tuple = create_dummy_files({'''torch''': ['''CONSTANT''', '''function''', '''FakeClass''']} ) self.assertEqual(dummy_files['''torch'''] , __snake_case )
23
from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput __snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name class __snake_case ( lowerCamelCase__ , lowerCamelCase__ ): @register_to_config def __init__( self , snake_case__ , snake_case__ = None , snake_case__ = None ) -> str: '''simple docstring''' super().__init__() UpperCAmelCase : Optional[Any] =learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" UpperCAmelCase : Any =torch.zeros(snake_case__ , snake_case__ ) else: UpperCAmelCase : Union[str, Any] =None UpperCAmelCase : Optional[int] =torch.nn.Parameter(snake_case__ ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : VQModel __lowerCamelCase : CLIPTextModel __lowerCamelCase : CLIPTokenizer __lowerCamelCase : TransformeraDModel __lowerCamelCase : LearnedClassifierFreeSamplingEmbeddings __lowerCamelCase : VQDiffusionScheduler def __init__( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> int: '''simple docstring''' super().__init__() self.register_modules( vqvae=snake_case__ , transformer=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , scheduler=snake_case__ , learned_classifier_free_sampling_embeddings=snake_case__ , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : int =len(snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else 1 # get prompt text embeddings UpperCAmelCase : Optional[int] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) UpperCAmelCase : int =text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCAmelCase : List[str] =self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' f''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) UpperCAmelCase : Optional[Any] =text_input_ids[:, : self.tokenizer.model_max_length] UpperCAmelCase : List[Any] =self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 UpperCAmelCase : int =prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate text embeddings for each generation per prompt UpperCAmelCase : int =prompt_embeds.repeat_interleave(snake_case__ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: UpperCAmelCase : Optional[int] =self.learned_classifier_free_sampling_embeddings.embeddings UpperCAmelCase : str =negative_prompt_embeds.unsqueeze(0 ).repeat(snake_case__ , 1 , 1 ) else: UpperCAmelCase : str =[''''''] * batch_size UpperCAmelCase : Tuple =text_input_ids.shape[-1] UpperCAmelCase : Optional[Any] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=snake_case__ , truncation=snake_case__ , return_tensors='''pt''' , ) UpperCAmelCase : Optional[Any] =self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings UpperCAmelCase : Optional[int] =negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCAmelCase : Optional[Any] =negative_prompt_embeds.shape[1] UpperCAmelCase : Union[str, Any] =negative_prompt_embeds.repeat(1 , snake_case__ , 1 ) UpperCAmelCase : Optional[Any] =negative_prompt_embeds.view(batch_size * num_images_per_prompt , snake_case__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCAmelCase : int =torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , snake_case__ , snake_case__ = 100 , snake_case__ = 5.0 , snake_case__ = 1.0 , snake_case__ = 1 , snake_case__ = None , snake_case__ = None , snake_case__ = "pil" , snake_case__ = True , snake_case__ = None , snake_case__ = 1 , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =1 elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Tuple =len(snake_case__ ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(snake_case__ )}''' ) UpperCAmelCase : Tuple =batch_size * num_images_per_prompt UpperCAmelCase : List[str] =guidance_scale > 1.0 UpperCAmelCase : List[Any] =self._encode_prompt(snake_case__ , snake_case__ , snake_case__ ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case__ , snake_case__ ) or callback_steps <= 0) ): raise ValueError( f'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' f''' {type(snake_case__ )}.''' ) # get the initial completely masked latents unless the user supplied it UpperCAmelCase : int =(batch_size, self.transformer.num_latent_pixels) if latents is None: UpperCAmelCase : Union[str, Any] =self.transformer.num_vector_embeds - 1 UpperCAmelCase : str =torch.full(snake_case__ , snake_case__ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( '''Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,''' f''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) UpperCAmelCase : Any =latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(snake_case__ , device=self.device ) UpperCAmelCase : Any =self.scheduler.timesteps.to(self.device ) UpperCAmelCase : Optional[int] =latents for i, t in enumerate(self.progress_bar(snake_case__ ) ): # expand the sample if we are doing classifier free guidance UpperCAmelCase : Optional[Any] =torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` UpperCAmelCase : Optional[int] =self.transformer(snake_case__ , encoder_hidden_states=snake_case__ , timestep=snake_case__ ).sample if do_classifier_free_guidance: UpperCAmelCase , UpperCAmelCase : str =model_output.chunk(2 ) UpperCAmelCase : Optional[int] =model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(snake_case__ , dim=1 , keepdim=snake_case__ ) UpperCAmelCase : Tuple =self.truncate(snake_case__ , snake_case__ ) # remove `log(0)`'s (`-inf`s) UpperCAmelCase : Optional[Any] =model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 UpperCAmelCase : int =self.scheduler.step(snake_case__ , timestep=snake_case__ , sample=snake_case__ , generator=snake_case__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =self.vqvae.config.vq_embed_dim UpperCAmelCase : Optional[Any] =(batch_size, self.transformer.height, self.transformer.width, embedding_channels) UpperCAmelCase : Dict =self.vqvae.quantize.get_codebook_entry(snake_case__ , shape=snake_case__ ) UpperCAmelCase : Tuple =self.vqvae.decode(snake_case__ , force_not_quantize=snake_case__ ).sample UpperCAmelCase : Union[str, Any] =(image / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase : Any =image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase : List[str] =self.numpy_to_pil(snake_case__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> torch.FloatTensor: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : int =torch.sort(snake_case__ , 1 , descending=snake_case__ ) UpperCAmelCase : Union[str, Any] =torch.exp(snake_case__ ) UpperCAmelCase : Union[str, Any] =sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out UpperCAmelCase : Optional[Any] =torch.full_like(keep_mask[:, 0:1, :] , snake_case__ ) UpperCAmelCase : Tuple =torch.cat((all_true, keep_mask) , dim=1 ) UpperCAmelCase : int =keep_mask[:, :-1, :] UpperCAmelCase : int =keep_mask.gather(1 , indices.argsort(1 ) ) UpperCAmelCase : Dict =log_p_x_0.clone() UpperCAmelCase : List[Any] =-torch.inf # -inf = log(0) return rv
348
0
import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def lowerCamelCase__ ( ) -> Optional[int]: __snake_case = ArgumentParser( description=( '''PyTorch TPU distributed training launch helper utility that will spawn up multiple distributed processes''' ) ) # Optional arguments for the launch helper parser.add_argument('''--num_cores''' , type=snake_case_ , default=1 , help='''Number of TPU cores to use (1 or 8).''' ) # positional parser.add_argument( '''training_script''' , type=snake_case_ , help=( '''The full path to the single TPU training ''' '''program/script to be launched in parallel, ''' '''followed by all the arguments for the ''' '''training script''' ) , ) # rest from the training program parser.add_argument('''training_script_args''' , nargs=snake_case_ ) return parser.parse_args() def lowerCamelCase__ ( ) -> List[Any]: __snake_case = parse_args() # Import training_script as a module. __snake_case = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) __snake_case = script_fpath.stem __snake_case = importlib.import_module(snake_case_ ) # Patch sys.argv __snake_case = [args.training_script] + args.training_script_args + ['''--tpu_num_cores''', str(args.num_cores )] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores ) if __name__ == "__main__": main()
24
import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase : Any =UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =self.dummy_uncond_unet UpperCAmelCase : Optional[int] =KarrasVeScheduler() UpperCAmelCase : List[Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : List[str] =torch.manual_seed(0 ) UpperCAmelCase : List[str] =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : str =torch.manual_seed(0 ) UpperCAmelCase : str =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' , return_dict=snake_case__ )[0] UpperCAmelCase : Any =image[0, -3:, -3:, -1] UpperCAmelCase : List[str] =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCAmelCase : int =np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class __snake_case ( unittest.TestCase ): def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Tuple ='''google/ncsnpp-celebahq-256''' UpperCAmelCase : int =UNetaDModel.from_pretrained(snake_case__ ) UpperCAmelCase : Dict =KarrasVeScheduler() UpperCAmelCase : Union[str, Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Any =torch.manual_seed(0 ) UpperCAmelCase : Tuple =pipe(num_inference_steps=20 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : Optional[int] =image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) UpperCAmelCase : Tuple =np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
348
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCAmelCase__ : Tuple = { 'configuration_resnet': ['RESNET_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ResNetConfig', 'ResNetOnnxConfig'] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ : Tuple = [ 'RESNET_PRETRAINED_MODEL_ARCHIVE_LIST', 'ResNetForImageClassification', 'ResNetModel', 'ResNetPreTrainedModel', 'ResNetBackbone', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ : Dict = [ 'TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFResNetForImageClassification', 'TFResNetModel', 'TFResNetPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ : List[str] = [ 'FlaxResNetForImageClassification', 'FlaxResNetModel', 'FlaxResNetPreTrainedModel', ] if TYPE_CHECKING: from .configuration_resnet import RESNET_PRETRAINED_CONFIG_ARCHIVE_MAP, ResNetConfig, ResNetOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_resnet import ( RESNET_PRETRAINED_MODEL_ARCHIVE_LIST, ResNetBackbone, ResNetForImageClassification, ResNetModel, ResNetPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_resnet import ( TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFResNetForImageClassification, TFResNetModel, TFResNetPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel, FlaxResNetPreTrainedModel else: import sys UpperCAmelCase__ : str = _LazyModule(__name__, globals()['__file__'], _import_structure)
25
import qiskit def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> qiskit.result.counts.Counts: '''simple docstring''' UpperCAmelCase : Union[str, Any] =qiskit.Aer.get_backend('''aer_simulator''' ) UpperCAmelCase : List[str] =qiskit.QuantumCircuit(4 , 2 ) # encode inputs in qubits 0 and 1 if bita == 1: qc_ha.x(0 ) if bita == 1: qc_ha.x(1 ) qc_ha.barrier() # use cnots to write XOR of the inputs on qubit2 qc_ha.cx(0 , 2 ) qc_ha.cx(1 , 2 ) # use ccx / toffoli gate to write AND of the inputs on qubit3 qc_ha.ccx(0 , 1 , 3 ) qc_ha.barrier() # extract outputs qc_ha.measure(2 , 0 ) # extract XOR value qc_ha.measure(3 , 1 ) # extract AND value # Execute the circuit on the qasm simulator UpperCAmelCase : Dict =qiskit.execute(__lowerCAmelCase , __lowerCAmelCase , shots=10_00 ) # Return the histogram data of the results of the experiment return job.result().get_counts(__lowerCAmelCase ) if __name__ == "__main__": __snake_case = half_adder(1, 1) print(f'Half Adder Output Qubit Counts: {counts}')
348
0
def lowerCAmelCase_ ( snake_case_ ): _A : Optional[Any] = 0 for ch in input_str: _A : List[str] = ord(snake_case_ ) _A : List[str] = pow(2,snake_case_ ) # 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()
26
from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class __snake_case : __lowerCamelCase : str = BlenderbotConfig __lowerCamelCase : Optional[Any] = {} __lowerCamelCase : Optional[int] = """gelu""" def __init__( self , snake_case__ , snake_case__=13 , snake_case__=7 , snake_case__=True , snake_case__=False , snake_case__=99 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=20 , snake_case__=2 , snake_case__=1 , snake_case__=0 , ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : Dict =seq_length UpperCAmelCase : Optional[Any] =is_training UpperCAmelCase : List[str] =use_labels UpperCAmelCase : List[Any] =vocab_size UpperCAmelCase : Optional[int] =hidden_size UpperCAmelCase : Tuple =num_hidden_layers UpperCAmelCase : Any =num_attention_heads UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : str =hidden_dropout_prob UpperCAmelCase : Optional[int] =attention_probs_dropout_prob UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : List[Any] =eos_token_id UpperCAmelCase : Optional[int] =pad_token_id UpperCAmelCase : Tuple =bos_token_id def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[Any] =ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) UpperCAmelCase : List[Any] =tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) UpperCAmelCase : Tuple =tf.concat([input_ids, eos_tensor] , axis=1 ) UpperCAmelCase : str =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : Optional[Any] =self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) UpperCAmelCase : List[str] =prepare_blenderbot_inputs_dict(snake_case__ , snake_case__ , snake_case__ ) return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =TFBlenderbotModel(config=snake_case__ ).get_decoder() UpperCAmelCase : Any =inputs_dict['''input_ids'''] UpperCAmelCase : str =input_ids[:1, :] UpperCAmelCase : Tuple =inputs_dict['''attention_mask'''][:1, :] UpperCAmelCase : Tuple =inputs_dict['''head_mask'''] UpperCAmelCase : List[Any] =1 # first forward pass UpperCAmelCase : List[str] =model(snake_case__ , attention_mask=snake_case__ , head_mask=snake_case__ , use_cache=snake_case__ ) UpperCAmelCase , UpperCAmelCase : str =outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase : Union[str, Any] =ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCAmelCase : List[Any] =tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and UpperCAmelCase : Tuple =tf.concat([input_ids, next_tokens] , axis=-1 ) UpperCAmelCase : int =tf.concat([attention_mask, next_attn_mask] , axis=-1 ) UpperCAmelCase : Optional[int] =model(snake_case__ , attention_mask=snake_case__ )[0] UpperCAmelCase : str =model(snake_case__ , attention_mask=snake_case__ , past_key_values=snake_case__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice UpperCAmelCase : List[Any] =int(ids_tensor((1,) , output_from_past.shape[-1] ) ) UpperCAmelCase : List[Any] =output_from_no_past[:, -3:, random_slice_idx] UpperCAmelCase : Dict =output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(snake_case__ , snake_case__ , rtol=1e-3 ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , )-> str: '''simple docstring''' if attention_mask is None: UpperCAmelCase : int =tf.cast(tf.math.not_equal(__lowerCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase : Tuple =tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase : str =tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase : Union[str, Any] =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase : int =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : List[str] = (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __lowerCamelCase : Dict = (TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __lowerCamelCase : Dict = ( { """conversational""": TFBlenderbotForConditionalGeneration, """feature-extraction""": TFBlenderbotModel, """summarization""": TFBlenderbotForConditionalGeneration, """text2text-generation""": TFBlenderbotForConditionalGeneration, """translation""": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __lowerCamelCase : Union[str, Any] = True __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Union[str, Any] = False def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : List[str] =TFBlenderbotModelTester(self ) UpperCAmelCase : List[Any] =ConfigTester(self , config_class=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*snake_case__ ) @require_tokenizers @require_tf class __snake_case ( unittest.TestCase ): __lowerCamelCase : List[str] = ["""My friends are cool but they eat too many carbs."""] __lowerCamelCase : Dict = """facebook/blenderbot-400M-distill""" @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[int] =self.tokenizer(self.src_text , return_tensors='''tf''' ) UpperCAmelCase : Optional[int] =self.model.generate( model_inputs.input_ids , ) UpperCAmelCase : str =self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=snake_case__ )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
348
0
'''simple docstring''' # using dfs for finding eulerian path traversal def lowerCamelCase (_SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : Optional[int]=None ): __a : Union[str, Any] = (path or []) + [u] for v in graph[u]: if visited_edge[u][v] is False: __a , __a : Tuple = True, True __a : Dict = dfs(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return path def lowerCamelCase (_SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : int ): __a : Any = 0 __a : str = -1 for i in range(_SCREAMING_SNAKE_CASE ): if i not in graph.keys(): continue if len(graph[i] ) % 2 == 1: odd_degree_nodes += 1 __a : List[str] = i if odd_degree_nodes == 0: return 1, odd_node if odd_degree_nodes == 2: return 2, odd_node return 3, odd_node def lowerCamelCase (_SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : List[str] ): __a : List[Any] = [[False for _ in range(max_node + 1 )] for _ in range(max_node + 1 )] __a , __a : Union[str, Any] = check_circuit_or_path(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if check == 3: print('graph is not Eulerian' ) print('no path' ) return __a : List[Any] = 1 if check == 2: __a : str = odd_node print('graph has a Euler path' ) if check == 1: print('graph has a Euler cycle' ) __a : Any = dfs(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) print(_SCREAMING_SNAKE_CASE ) def lowerCamelCase (): __a : Union[str, Any] = {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]} __a : Any = {1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]} __a : Optional[Any] = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]} __a : Dict = {1: [2, 3], 2: [1, 3], 3: [1, 2]} __a : List[Any] = { 1: [], 2: [] # all degree is zero } __a : Dict = 10 check_euler(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) check_euler(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) check_euler(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) check_euler(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) check_euler(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if __name__ == "__main__": main()
27
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { '''asapp/sew-d-tiny-100k''': '''https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json''', # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[Any] = """sew-d""" def __init__( self , snake_case__=32 , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=2 , snake_case__=512 , snake_case__=256 , snake_case__=True , snake_case__=True , snake_case__=("p2c", "c2p") , snake_case__="layer_norm" , snake_case__="gelu_python" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.0 , snake_case__=0.1 , snake_case__=0.02 , snake_case__=1e-7 , snake_case__=1e-5 , snake_case__="group" , snake_case__="gelu" , snake_case__=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , snake_case__=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , snake_case__=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , snake_case__=False , snake_case__=128 , snake_case__=16 , snake_case__=True , snake_case__=0.05 , snake_case__=10 , snake_case__=2 , snake_case__=0.0 , snake_case__=10 , snake_case__=0 , snake_case__="mean" , snake_case__=False , snake_case__=False , snake_case__=256 , snake_case__=0 , snake_case__=1 , snake_case__=2 , **snake_case__ , ) -> int: '''simple docstring''' super().__init__(**snake_case__ , pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ ) UpperCAmelCase : Union[str, Any] =hidden_size UpperCAmelCase : Union[str, Any] =feat_extract_norm UpperCAmelCase : Optional[Any] =feat_extract_activation UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : int =list(snake_case__ ) UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : str =conv_bias UpperCAmelCase : Tuple =num_conv_pos_embeddings UpperCAmelCase : Dict =num_conv_pos_embedding_groups UpperCAmelCase : str =len(self.conv_dim ) UpperCAmelCase : Dict =num_hidden_layers UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : List[Any] =squeeze_factor UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : int =position_buckets UpperCAmelCase : Optional[int] =share_att_key UpperCAmelCase : Optional[int] =relative_attention UpperCAmelCase : Tuple =norm_rel_ebd UpperCAmelCase : List[Any] =list(snake_case__ ) UpperCAmelCase : Dict =hidden_act UpperCAmelCase : Optional[int] =num_attention_heads UpperCAmelCase : Any =hidden_dropout UpperCAmelCase : str =attention_dropout UpperCAmelCase : Union[str, Any] =activation_dropout UpperCAmelCase : str =feat_proj_dropout UpperCAmelCase : Union[str, Any] =final_dropout UpperCAmelCase : Optional[int] =layer_norm_eps UpperCAmelCase : str =feature_layer_norm_eps UpperCAmelCase : str =initializer_range UpperCAmelCase : Any =vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect.''' '''It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,''' f'''but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)''' f'''= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCAmelCase : Union[str, Any] =apply_spec_augment UpperCAmelCase : Optional[Any] =mask_time_prob UpperCAmelCase : Tuple =mask_time_length UpperCAmelCase : str =mask_time_min_masks UpperCAmelCase : Optional[int] =mask_feature_prob UpperCAmelCase : Optional[Any] =mask_feature_length UpperCAmelCase : List[Any] =mask_feature_min_masks # ctc loss UpperCAmelCase : str =ctc_loss_reduction UpperCAmelCase : Optional[int] =ctc_zero_infinity # sequence classification UpperCAmelCase : Union[str, Any] =use_weighted_layer_sum UpperCAmelCase : int =classifier_proj_size @property def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
348
0
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING _lowerCamelCase : Optional[int] = logging.get_logger(__name__) _lowerCamelCase : Optional[Any] = { "ut/deta": "https://huggingface.co/ut/deta/resolve/main/config.json", } class SCREAMING_SNAKE_CASE ( _a ): """simple docstring""" _SCREAMING_SNAKE_CASE = """deta""" _SCREAMING_SNAKE_CASE = { """hidden_size""": """d_model""", """num_attention_heads""": """encoder_attention_heads""", } def __init__( self : str , UpperCamelCase__ : Union[str, Any]=None , UpperCamelCase__ : List[Any]=9_0_0 , UpperCamelCase__ : str=2_0_4_8 , UpperCamelCase__ : int=6 , UpperCamelCase__ : List[Any]=2_0_4_8 , UpperCamelCase__ : Union[str, Any]=8 , UpperCamelCase__ : Optional[int]=6 , UpperCamelCase__ : Optional[int]=1_0_2_4 , UpperCamelCase__ : Union[str, Any]=8 , UpperCamelCase__ : Dict=0.0 , UpperCamelCase__ : Union[str, Any]=True , UpperCamelCase__ : Any="relu" , UpperCamelCase__ : Tuple=2_5_6 , UpperCamelCase__ : int=0.1 , UpperCamelCase__ : Any=0.0 , UpperCamelCase__ : Optional[Any]=0.0 , UpperCamelCase__ : Tuple=0.0_2 , UpperCamelCase__ : Optional[Any]=1.0 , UpperCamelCase__ : Union[str, Any]=True , UpperCamelCase__ : List[Any]=False , UpperCamelCase__ : int="sine" , UpperCamelCase__ : Tuple=5 , UpperCamelCase__ : List[Any]=4 , UpperCamelCase__ : Any=4 , UpperCamelCase__ : List[str]=True , UpperCamelCase__ : str=3_0_0 , UpperCamelCase__ : Optional[Any]=True , UpperCamelCase__ : str=True , UpperCamelCase__ : Tuple=1 , UpperCamelCase__ : str=5 , UpperCamelCase__ : List[str]=2 , UpperCamelCase__ : Any=1 , UpperCamelCase__ : int=1 , UpperCamelCase__ : List[Any]=5 , UpperCamelCase__ : Any=2 , UpperCamelCase__ : Any=0.1 , UpperCamelCase__ : Union[str, Any]=0.2_5 , **UpperCamelCase__ : int , ): """simple docstring""" if backbone_config is None: logger.info('`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.' ) UpperCamelCase = CONFIG_MAPPING['resnet'](out_features=['stage2', 'stage3', 'stage4'] ) else: if isinstance(UpperCamelCase__ , UpperCamelCase__ ): UpperCamelCase = backbone_config.pop('model_type' ) UpperCamelCase = CONFIG_MAPPING[backbone_model_type] UpperCamelCase = config_class.from_dict(UpperCamelCase__ ) UpperCamelCase = backbone_config UpperCamelCase = num_queries UpperCamelCase = max_position_embeddings UpperCamelCase = d_model UpperCamelCase = encoder_ffn_dim UpperCamelCase = encoder_layers UpperCamelCase = encoder_attention_heads UpperCamelCase = decoder_ffn_dim UpperCamelCase = decoder_layers UpperCamelCase = decoder_attention_heads UpperCamelCase = dropout UpperCamelCase = attention_dropout UpperCamelCase = activation_dropout UpperCamelCase = activation_function UpperCamelCase = init_std UpperCamelCase = init_xavier_std UpperCamelCase = encoder_layerdrop UpperCamelCase = auxiliary_loss UpperCamelCase = position_embedding_type # deformable attributes UpperCamelCase = num_feature_levels UpperCamelCase = encoder_n_points UpperCamelCase = decoder_n_points UpperCamelCase = two_stage UpperCamelCase = two_stage_num_proposals UpperCamelCase = with_box_refine UpperCamelCase = assign_first_stage if two_stage is True and with_box_refine is False: raise ValueError('If two_stage is True, with_box_refine must be True.' ) # Hungarian matcher UpperCamelCase = class_cost UpperCamelCase = bbox_cost UpperCamelCase = giou_cost # Loss coefficients UpperCamelCase = mask_loss_coefficient UpperCamelCase = dice_loss_coefficient UpperCamelCase = bbox_loss_coefficient UpperCamelCase = giou_loss_coefficient UpperCamelCase = eos_coefficient UpperCamelCase = focal_alpha super().__init__(is_encoder_decoder=UpperCamelCase__ , **UpperCamelCase__ ) @property def A ( self : List[str] ): """simple docstring""" return self.encoder_attention_heads @property def A ( self : Tuple ): """simple docstring""" return self.d_model def A ( self : Union[str, Any] ): """simple docstring""" UpperCamelCase = copy.deepcopy(self.__dict__ ) UpperCamelCase = self.backbone_config.to_dict() UpperCamelCase = self.__class__.model_type return output
28
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node __snake_case = 4 __snake_case = 3 class __snake_case ( lowerCamelCase__ ): pass def lowerCAmelCase_ ( __lowerCAmelCase )-> List[str]: '''simple docstring''' for shard in shards: for i in range(__lowerCAmelCase ): yield {"i": i, "shard": shard} def lowerCAmelCase_ ( )-> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =int(os.environ['''RANK'''] ) UpperCAmelCase : Optional[Any] =int(os.environ['''WORLD_SIZE'''] ) UpperCAmelCase : List[Any] =ArgumentParser() parser.add_argument('''--streaming''' , type=__lowerCAmelCase ) parser.add_argument('''--local_rank''' , type=__lowerCAmelCase ) parser.add_argument('''--num_workers''' , type=__lowerCAmelCase , default=0 ) UpperCAmelCase : Any =parser.parse_args() UpperCAmelCase : List[str] =args.streaming UpperCAmelCase : Tuple =args.num_workers UpperCAmelCase : int ={'''shards''': [f'''shard_{shard_idx}''' for shard_idx in range(__lowerCAmelCase )]} UpperCAmelCase : Optional[int] =IterableDataset.from_generator(__lowerCAmelCase , gen_kwargs=__lowerCAmelCase ) if not streaming: UpperCAmelCase : List[Any] =Dataset.from_list(list(__lowerCAmelCase ) ) UpperCAmelCase : Dict =split_dataset_by_node(__lowerCAmelCase , rank=__lowerCAmelCase , world_size=__lowerCAmelCase ) UpperCAmelCase : List[Any] =torch.utils.data.DataLoader(__lowerCAmelCase , num_workers=__lowerCAmelCase ) UpperCAmelCase : Dict =NUM_SHARDS * NUM_ITEMS_PER_SHARD UpperCAmelCase : str =full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) UpperCAmelCase : List[Any] =sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f'''local_size {local_size} != expected_local_size {expected_local_size}''' ) if __name__ == "__main__": main()
348
0
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCamelCase (_snake_case , unittest.TestCase ): '''simple docstring''' _snake_case : int = CLIPTokenizer _snake_case : Tuple = CLIPTokenizerFast _snake_case : List[Any] = True _snake_case : Dict = {} _snake_case : Optional[int] = False def __UpperCAmelCase ( self ) -> Tuple: super().setUp() # fmt: off UpperCAmelCase_ : str = ['l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>'] # fmt: on UpperCAmelCase_ : Optional[int] = dict(zip(_UpperCamelCase , range(len(_UpperCamelCase ) ) ) ) UpperCAmelCase_ : Tuple = ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>'] UpperCAmelCase_ : Any = {'unk_token': '<unk>'} UpperCAmelCase_ : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) UpperCAmelCase_ : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(_UpperCamelCase ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(_UpperCamelCase ) ) def __UpperCAmelCase ( self , **_UpperCamelCase ) -> Optional[Any]: kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname , **_UpperCamelCase ) def __UpperCAmelCase ( self , **_UpperCamelCase ) -> Any: kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **_UpperCamelCase ) def __UpperCAmelCase ( self , _UpperCamelCase ) -> Any: UpperCAmelCase_ : List[str] = 'lower newer' UpperCAmelCase_ : Any = 'lower newer' return input_text, output_text def __UpperCAmelCase ( self ) -> List[str]: UpperCAmelCase_ : List[Any] = CLIPTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) UpperCAmelCase_ : Tuple = 'lower newer' UpperCAmelCase_ : Any = ['lo', 'w', 'er</w>', 'n', 'e', 'w', 'er</w>'] UpperCAmelCase_ : Optional[int] = tokenizer.tokenize(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) UpperCAmelCase_ : List[Any] = tokens + [tokenizer.unk_token] UpperCAmelCase_ : List[Any] = [1_0, 2, 1_6, 9, 3, 2, 1_6, 2_0] self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCamelCase ) , _UpperCamelCase ) @require_ftfy def __UpperCAmelCase ( self ) -> Optional[Any]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): UpperCAmelCase_ : List[Any] = self.tokenizer_class.from_pretrained(_UpperCamelCase , **_UpperCamelCase ) UpperCAmelCase_ : int = self.rust_tokenizer_class.from_pretrained(_UpperCamelCase , **_UpperCamelCase ) UpperCAmelCase_ : Optional[int] = 'A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.' UpperCAmelCase_ : str = tokenizer_s.tokenize(_UpperCamelCase ) UpperCAmelCase_ : Optional[Any] = tokenizer_r.tokenize(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways UpperCAmelCase_ : Any = 'xa\u0303y' + ' ' + 'x\xe3y' UpperCAmelCase_ : Optional[int] = tokenizer_s.tokenize(_UpperCamelCase ) UpperCAmelCase_ : Any = tokenizer_r.tokenize(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) # Test that the tokenization is identical on unicode of space type UpperCAmelCase_ : int = [ '\u0009', # (horizontal tab, '\t') '\u000B', # (vertical tab) '\u000C', # (form feed) '\u0020', # (space, ' ') '\u200E', # (left-to-right mark):w '\u200F', # (right-to-left mark) ] for unicode_seq in spaces_unicodes: UpperCAmelCase_ : Optional[Any] = tokenizer_s.tokenize(_UpperCamelCase ) UpperCAmelCase_ : Union[str, Any] = tokenizer_r.tokenize(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) # Test that the tokenization is identical on unicode of line break type UpperCAmelCase_ : str = [ '\u000A', # (line feed, '\n') '\r\n', # (carriage return and line feed, '\r\n') '\u000D', # (carriage return, '\r') '\r', # (carriage return, '\r') '\u000D', # (carriage return, '\r') '\u2028', # (line separator) '\u2029', # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: UpperCAmelCase_ : Any = tokenizer_s.tokenize(_UpperCamelCase ) UpperCAmelCase_ : int = tokenizer_r.tokenize(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) def __UpperCAmelCase ( self ) -> Dict: # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): UpperCAmelCase_ : str = 'hello' # `hello` is a token in the vocabulary of `pretrained_name` UpperCAmelCase_ : Optional[int] = f"{text_of_1_token} {text_of_1_token}" UpperCAmelCase_ : Union[str, Any] = self.rust_tokenizer_class.from_pretrained( _UpperCamelCase , use_fast=_UpperCamelCase , ) UpperCAmelCase_ : int = tokenizer_r(_UpperCamelCase , return_offsets_mapping=_UpperCamelCase , add_special_tokens=_UpperCamelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_UpperCamelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_UpperCamelCase ) + 1, len(_UpperCamelCase ) + 1 + len(_UpperCamelCase )) , ) UpperCAmelCase_ : Optional[int] = f" {text}" UpperCAmelCase_ : Tuple = self.rust_tokenizer_class.from_pretrained( _UpperCamelCase , use_fast=_UpperCamelCase , ) UpperCAmelCase_ : int = tokenizer_r(_UpperCamelCase , return_offsets_mapping=_UpperCamelCase , add_special_tokens=_UpperCamelCase ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_UpperCamelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_UpperCamelCase ) + 1, 1 + len(_UpperCamelCase ) + 1 + len(_UpperCamelCase )) , ) def __UpperCAmelCase ( self ) -> Any: # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(_UpperCamelCase ) as context: self.rust_tokenizer_class.from_pretrained('robot-test/old-clip-tokenizer' ) self.assertTrue( context.exception.args[0].startswith( 'The `backend_tokenizer` provided does not match the expected format.' ) ) @require_ftfy def __UpperCAmelCase ( self ) -> Optional[Any]: super().test_tokenization_python_rust_equals() def __UpperCAmelCase ( self ) -> List[Any]: # CLIP always lower cases letters pass
29
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __snake_case = {'''configuration_opt''': ['''OPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OPTConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''OPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OPTForCausalLM''', '''OPTModel''', '''OPTPreTrainedModel''', '''OPTForSequenceClassification''', '''OPTForQuestionAnswering''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''TFOPTForCausalLM''', '''TFOPTModel''', '''TFOPTPreTrainedModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''FlaxOPTForCausalLM''', '''FlaxOPTModel''', '''FlaxOPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
0
import os def a ( snake_case__: str = "input.txt" ): '''simple docstring''' with open(os.path.join(os.path.dirname(snake_case__ ) , snake_case__ ) ) as input_file: lowercase_ = [ [int(snake_case__ ) for element in line.split(''',''' )] for line in input_file.readlines() ] lowercase_ = len(snake_case__ ) lowercase_ = len(matrix[0] ) lowercase_ = [[-1 for _ in range(snake_case__ )] for _ in range(snake_case__ )] for i in range(snake_case__ ): lowercase_ = matrix[i][0] for j in range(1 , snake_case__ ): for i in range(snake_case__ ): lowercase_ = minimal_path_sums[i][j - 1] + matrix[i][j] for i in range(1 , snake_case__ ): lowercase_ = min( minimal_path_sums[i][j] , minimal_path_sums[i - 1][j] + matrix[i][j] ) for i in range(rows - 2 , -1 , -1 ): lowercase_ = min( minimal_path_sums[i][j] , minimal_path_sums[i + 1][j] + matrix[i][j] ) return min(minimal_path_sums_row[-1] for minimal_path_sums_row in minimal_path_sums ) if __name__ == "__main__": print(f"{solution() = }")
30
import tempfile import unittest import numpy as np import transformers from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax import jax.numpy as jnp from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel if is_torch_available(): import torch class __snake_case : def __init__( self , snake_case__ , snake_case__=14 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=False , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=4 , snake_case__=4 , snake_case__=4 , snake_case__=37 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , ) -> str: '''simple docstring''' UpperCAmelCase : str =parent UpperCAmelCase : Tuple =batch_size UpperCAmelCase : Optional[int] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Tuple =use_input_mask UpperCAmelCase : List[Any] =use_token_type_ids UpperCAmelCase : Optional[Any] =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : List[Any] =hidden_size UpperCAmelCase : Optional[int] =rotary_dim UpperCAmelCase : Union[str, Any] =num_hidden_layers UpperCAmelCase : List[Any] =num_attention_heads UpperCAmelCase : Dict =intermediate_size UpperCAmelCase : Union[str, Any] =hidden_act UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : Dict =attention_probs_dropout_prob UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : str =initializer_range UpperCAmelCase : Optional[int] =None UpperCAmelCase : List[Any] =vocab_size - 1 UpperCAmelCase : Optional[Any] =vocab_size - 1 UpperCAmelCase : List[Any] =vocab_size - 1 def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : List[Any] =None if self.use_input_mask: UpperCAmelCase : Optional[Any] =random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : Dict =GPTJConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , use_cache=snake_case__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , rotary_dim=self.rotary_dim , ) return (config, input_ids, input_mask) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Union[str, Any] =config_and_inputs UpperCAmelCase : Tuple ={'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =20 UpperCAmelCase : Any =model_class_name(snake_case__ ) UpperCAmelCase : str =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : Any =jnp.ones((input_ids.shape[0], max_decoder_length) , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : List[str] =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =model( input_ids[:, -1:] , attention_mask=snake_case__ , past_key_values=outputs_cache.past_key_values , position_ids=snake_case__ , ) UpperCAmelCase : List[Any] =model(snake_case__ ) UpperCAmelCase : Any =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Dict =20 UpperCAmelCase : Dict =model_class_name(snake_case__ ) UpperCAmelCase : Tuple =jnp.concatenate( [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]) )] , axis=-1 , ) UpperCAmelCase : Dict =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : int =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : str =model( input_ids[:, -1:] , past_key_values=outputs_cache.past_key_values , attention_mask=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =model(snake_case__ , attention_mask=snake_case__ ) UpperCAmelCase : Dict =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) @require_flax class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Tuple = (FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else () __lowerCamelCase : Optional[Any] = (FlaxGPTJForCausalLM,) if is_flax_available() else () def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =FlaxGPTJModelTester(self ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Dict =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward_with_attn_mask( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) @tooslow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =GPTaTokenizer.from_pretrained('''gpt2''' , pad_token='''<|endoftext|>''' , padding_side='''left''' ) UpperCAmelCase : Optional[Any] =tokenizer(['''Hello this is a long string''', '''Hey'''] , return_tensors='''np''' , padding=snake_case__ , truncation=snake_case__ ) UpperCAmelCase : Optional[int] =FlaxGPTJForCausalLM.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : str =False UpperCAmelCase : Union[str, Any] =model.config.eos_token_id UpperCAmelCase : List[Any] =jax.jit(model.generate ) UpperCAmelCase : Dict =jit_generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , pad_token_id=tokenizer.pad_token_id ).sequences UpperCAmelCase : Any =tokenizer.batch_decode(snake_case__ , skip_special_tokens=snake_case__ ) UpperCAmelCase : Tuple =[ '''Hello this is a long string of text.\n\nI\'m trying to get the text of the''', '''Hey, I\'m a little late to the party. I\'m going to''', ] self.assertListEqual(snake_case__ , snake_case__ ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : List[str] =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : Any =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : Any =getattr(snake_case__ , snake_case__ ) UpperCAmelCase , UpperCAmelCase : Union[str, Any] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Tuple =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : int =0 UpperCAmelCase : Optional[int] =1 UpperCAmelCase : Optional[int] =0 UpperCAmelCase : Union[str, Any] =1 UpperCAmelCase : List[str] =pt_model_class(snake_case__ ).eval() UpperCAmelCase : Optional[int] =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Any =convert_pytorch_state_dict_to_flax(pt_model.state_dict() , snake_case__ ) UpperCAmelCase : Union[str, Any] =fx_state with torch.no_grad(): UpperCAmelCase : Any =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : Dict =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(snake_case__ ) UpperCAmelCase : str =model_class.from_pretrained(snake_case__ , from_pt=snake_case__ ) UpperCAmelCase : int =fx_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output_loaded, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output_loaded[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : int =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : int =getattr(snake_case__ , snake_case__ ) UpperCAmelCase : Dict =pt_model_class(snake_case__ ).eval() UpperCAmelCase : str =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Optional[Any] =load_flax_weights_in_pytorch_model(snake_case__ , fx_model.params ) UpperCAmelCase , UpperCAmelCase : Optional[int] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Optional[int] =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : str =0 UpperCAmelCase : Any =1 UpperCAmelCase : List[Any] =0 UpperCAmelCase : Tuple =1 # make sure weights are tied in PyTorch pt_model.tie_weights() with torch.no_grad(): UpperCAmelCase : Optional[Any] =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : List[Any] =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(snake_case__ ) UpperCAmelCase : Tuple =pt_model_class.from_pretrained(snake_case__ , from_flax=snake_case__ ) with torch.no_grad(): UpperCAmelCase : Any =pt_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @tooslow def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase : str =model_class_name.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : Tuple =model(np.ones((1, 1) ) ) self.assertIsNotNone(snake_case__ )
348
0
'''simple docstring''' import numpy as np import pandas as pd from sklearn.preprocessing import Normalizer from sklearn.svm import SVR from statsmodels.tsa.statespace.sarimax import SARIMAX def UpperCamelCase_ ( _UpperCAmelCase : list , _UpperCAmelCase : list , _UpperCAmelCase : list , _UpperCAmelCase : list , _UpperCAmelCase : list ) -> float: """simple docstring""" _UpperCAmelCase : Dict = np.array([[1, item, train_mtch[i]] for i, item in enumerate(_UpperCAmelCase )] ) _UpperCAmelCase : List[str] = np.array(_UpperCAmelCase ) _UpperCAmelCase : str = np.dot(np.dot(np.linalg.inv(np.dot(x.transpose() , _UpperCAmelCase ) ) , x.transpose() ) , _UpperCAmelCase ) return abs(beta[0] + test_dt[0] * beta[1] + test_mtch[0] + beta[2] ) def UpperCamelCase_ ( _UpperCAmelCase : list , _UpperCAmelCase : list , _UpperCAmelCase : list ) -> float: """simple docstring""" _UpperCAmelCase : Any = (1, 2, 1) _UpperCAmelCase : List[str] = (1, 1, 0, 7) _UpperCAmelCase : Optional[Any] = SARIMAX( _UpperCAmelCase , exog=_UpperCAmelCase , order=_UpperCAmelCase , seasonal_order=_UpperCAmelCase ) _UpperCAmelCase : Tuple = model.fit(disp=_UpperCAmelCase , maxiter=600 , method="nm" ) _UpperCAmelCase : int = model_fit.predict(1 , len(_UpperCAmelCase ) , exog=[test_match] ) return result[0] def UpperCamelCase_ ( _UpperCAmelCase : list , _UpperCAmelCase : list , _UpperCAmelCase : list ) -> float: """simple docstring""" _UpperCAmelCase : int = SVR(kernel="rbf" , C=1 , gamma=0.1 , epsilon=0.1 ) regressor.fit(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase : Optional[Any] = regressor.predict(_UpperCAmelCase ) return y_pred[0] def UpperCamelCase_ ( _UpperCAmelCase : list ) -> float: """simple docstring""" train_user.sort() _UpperCAmelCase : str = np.percentile(_UpperCAmelCase , 25 ) _UpperCAmelCase : Tuple = np.percentile(_UpperCAmelCase , 75 ) _UpperCAmelCase : Tuple = qa - qa _UpperCAmelCase : str = qa - (iqr * 0.1) return low_lim def UpperCamelCase_ ( _UpperCAmelCase : list , _UpperCAmelCase : float ) -> bool: """simple docstring""" _UpperCAmelCase : int = 0 _UpperCAmelCase : Any = 0 for i in list_vote: if i > actual_result: _UpperCAmelCase : int = not_safe + 1 else: if abs(abs(_UpperCAmelCase ) - abs(_UpperCAmelCase ) ) <= 0.1: safe += 1 else: not_safe += 1 return safe > not_safe if __name__ == "__main__": # data_input_df = pd.read_csv("ex_data.csv", header=None) __SCREAMING_SNAKE_CASE : Union[str, Any] = [[18_231, 0.0, 1], [22_621, 1.0, 2], [15_675, 0.0, 3], [23_583, 1.0, 4]] __SCREAMING_SNAKE_CASE : Dict = pd.DataFrame( data_input, columns=["""total_user""", """total_even""", """days"""] ) __SCREAMING_SNAKE_CASE : Optional[int] = Normalizer().fit_transform(data_input_df.values) # split data __SCREAMING_SNAKE_CASE : List[Any] = normalize_df[:, 2].tolist() __SCREAMING_SNAKE_CASE : int = normalize_df[:, 0].tolist() __SCREAMING_SNAKE_CASE : str = normalize_df[:, 1].tolist() # for svr (input variable = total date and total match) __SCREAMING_SNAKE_CASE : str = normalize_df[:, [1, 2]].tolist() __SCREAMING_SNAKE_CASE : str = x[: len(x) - 1] __SCREAMING_SNAKE_CASE : Optional[Any] = x[len(x) - 1 :] # for linear regression & sarimax __SCREAMING_SNAKE_CASE : Any = total_date[: len(total_date) - 1] __SCREAMING_SNAKE_CASE : str = total_user[: len(total_user) - 1] __SCREAMING_SNAKE_CASE : str = total_match[: len(total_match) - 1] __SCREAMING_SNAKE_CASE : int = total_date[len(total_date) - 1 :] __SCREAMING_SNAKE_CASE : Optional[int] = total_user[len(total_user) - 1 :] __SCREAMING_SNAKE_CASE : List[Any] = total_match[len(total_match) - 1 :] # voting system with forecasting __SCREAMING_SNAKE_CASE : Optional[Any] = [ linear_regression_prediction( trn_date, trn_user, trn_match, tst_date, tst_match ), sarimax_predictor(trn_user, trn_match, tst_match), support_vector_regressor(x_train, x_test, trn_user), ] # check the safety of today's data __SCREAMING_SNAKE_CASE : List[str] = """""" if data_safety_checker(res_vote, tst_user) else """not """ print("""Today's data is {not_str}safe.""")
31
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __snake_case = { '''configuration_bloom''': ['''BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BloomConfig''', '''BloomOnnxConfig'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''BloomTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BloomForCausalLM''', '''BloomModel''', '''BloomPreTrainedModel''', '''BloomForSequenceClassification''', '''BloomForTokenClassification''', '''BloomForQuestionAnswering''', ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
0
UpperCAmelCase_ : Optional[Any] = 6_5521 def SCREAMING_SNAKE_CASE_ ( __A : str ) -> int: """simple docstring""" a_ : Dict = 1 a_ : Dict = 0 for plain_chr in plain_text: a_ : List[Any] = (a + ord(__A )) % MOD_ADLER a_ : int = (b + a) % MOD_ADLER return (b << 16) | a
32
import os from typing import Dict, List, Tuple, TypeVar, Union __snake_case = TypeVar('''T''') __snake_case = Union[List[T], Tuple[T, ...]] __snake_case = Union[T, List[T], Dict[str, T]] __snake_case = Union[str, bytes, os.PathLike]
348
0
"""simple docstring""" import gc import random import unittest import numpy as np import torch from diffusers import DDIMScheduler, KandinskyVaaPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel from diffusers.utils import floats_tensor, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class _UpperCAmelCase ( _A , unittest.TestCase ): SCREAMING_SNAKE_CASE_ : List[str] = KandinskyVaaPipeline SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ "image_embeds", "negative_image_embeds", ] SCREAMING_SNAKE_CASE_ : int = ["image_embeds", "negative_image_embeds"] SCREAMING_SNAKE_CASE_ : Tuple = [ "generator", "height", "width", "latents", "guidance_scale", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] SCREAMING_SNAKE_CASE_ : List[str] = False @property def A ( self : int ) -> Any: return 32 @property def A ( self : Dict ) -> Tuple: return 32 @property def A ( self : Union[str, Any] ) -> List[Any]: return self.time_input_dim @property def A ( self : List[Any] ) -> Dict: return self.time_input_dim * 4 @property def A ( self : List[Any] ) -> Optional[int]: return 1_00 @property def A ( self : Tuple ) -> int: torch.manual_seed(0 ) lowercase_ : Optional[Any] = { '''in_channels''': 4, # Out channels is double in channels because predicts mean and variance '''out_channels''': 8, '''addition_embed_type''': '''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''': '''image_proj''', '''cross_attention_dim''': self.cross_attention_dim, '''attention_head_dim''': 4, '''resnet_time_scale_shift''': '''scale_shift''', '''class_embed_type''': None, } lowercase_ : Optional[int] = UNetaDConditionModel(**A ) return model @property def A ( self : str ) -> Optional[Any]: 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 A ( self : Dict ) -> Any: torch.manual_seed(0 ) lowercase_ : Optional[Any] = VQModel(**self.dummy_movq_kwargs ) return model def A ( self : Dict ) -> Dict: lowercase_ : Union[str, Any] = self.dummy_unet lowercase_ : Optional[Any] = self.dummy_movq lowercase_ : Dict = DDIMScheduler( num_train_timesteps=10_00 , beta_schedule='''linear''' , beta_start=0.00085 , beta_end=0.012 , clip_sample=A , set_alpha_to_one=A , steps_offset=1 , prediction_type='''epsilon''' , thresholding=A , ) lowercase_ : List[str] = { '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def A ( self : List[Any] , A : List[Any] , A : List[Any]=0 ) -> List[Any]: lowercase_ : Any = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(A ) ).to(A ) lowercase_ : Dict = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( A ) if str(A ).startswith('''mps''' ): lowercase_ : Dict = torch.manual_seed(A ) else: lowercase_ : List[Any] = torch.Generator(device=A ).manual_seed(A ) lowercase_ : List[str] = { '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 64, '''width''': 64, '''guidance_scale''': 4.0, '''num_inference_steps''': 2, '''output_type''': '''np''', } return inputs def A ( self : Tuple ) -> List[Any]: lowercase_ : Union[str, Any] = '''cpu''' lowercase_ : Optional[int] = self.get_dummy_components() lowercase_ : Optional[int] = self.pipeline_class(**A ) lowercase_ : List[Any] = pipe.to(A ) pipe.set_progress_bar_config(disable=A ) lowercase_ : str = pipe(**self.get_dummy_inputs(A ) ) lowercase_ : Union[str, Any] = output.images lowercase_ : List[str] = pipe( **self.get_dummy_inputs(A ) , return_dict=A , )[0] lowercase_ : Tuple = image[0, -3:, -3:, -1] lowercase_ : str = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowercase_ : Union[str, Any] = np.array( [0.6237976, 1.0, 0.36441332, 1.0, 0.70639634, 0.29877186, 0.85652125, 0.5216843, 0.54454046] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), F''' expected_slice {expected_slice}, but got {image_slice.flatten()}''' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), F''' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}''' @slow @require_torch_gpu class _UpperCAmelCase ( unittest.TestCase ): def A ( self : List[Any] ) -> Tuple: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def A ( self : Optional[int] ) -> List[str]: lowercase_ : Dict = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinskyv22/kandinskyv22_text2img_cat_fp16.npy''' ) lowercase_ : List[str] = KandinskyVaaPriorPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-2-prior''' , torch_dtype=torch.floataa ) pipe_prior.to(A ) lowercase_ : int = KandinskyVaaPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-2-decoder''' , torch_dtype=torch.floataa ) lowercase_ : Any = pipeline.to(A ) pipeline.set_progress_bar_config(disable=A ) lowercase_ : int = '''red cat, 4k photo''' lowercase_ : Tuple = torch.Generator(device='''cuda''' ).manual_seed(0 ) lowercase_ , lowercase_ : Dict = pipe_prior( A , generator=A , num_inference_steps=5 , negative_prompt='''''' , ).to_tuple() lowercase_ : Any = torch.Generator(device='''cuda''' ).manual_seed(0 ) lowercase_ : List[Any] = pipeline( image_embeds=A , negative_image_embeds=A , generator=A , num_inference_steps=1_00 , output_type='''np''' , ) lowercase_ : List[str] = output.images[0] assert image.shape == (5_12, 5_12, 3) assert_mean_pixel_difference(A , A )
33
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_big_bird import BigBirdTokenizer else: __snake_case = None __snake_case = logging.get_logger(__name__) __snake_case = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} __snake_case = { '''vocab_file''': { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model''', '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model''' ), }, '''tokenizer_file''': { '''google/bigbird-roberta-base''': ( '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/tokenizer.json''' ), '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/tokenizer.json''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/tokenizer.json''' ), }, } __snake_case = { '''google/bigbird-roberta-base''': 40_96, '''google/bigbird-roberta-large''': 40_96, '''google/bigbird-base-trivia-itc''': 40_96, } __snake_case = '''▁''' class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Dict = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = BigBirdTokenizer __lowerCamelCase : Any = ["""input_ids""", """attention_mask"""] __lowerCamelCase : List[int] = [] def __init__( self , snake_case__=None , snake_case__=None , snake_case__="<unk>" , snake_case__="<s>" , snake_case__="</s>" , snake_case__="<pad>" , snake_case__="[SEP]" , snake_case__="[MASK]" , snake_case__="[CLS]" , **snake_case__ , ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else bos_token UpperCAmelCase : Optional[int] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else eos_token UpperCAmelCase : List[str] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else unk_token UpperCAmelCase : Union[str, Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else pad_token UpperCAmelCase : int =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else cls_token UpperCAmelCase : str =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else sep_token # Mask token behave like a normal word, i.e. include the space before it UpperCAmelCase : List[Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else mask_token super().__init__( snake_case__ , tokenizer_file=snake_case__ , bos_token=snake_case__ , eos_token=snake_case__ , unk_token=snake_case__ , sep_token=snake_case__ , pad_token=snake_case__ , cls_token=snake_case__ , mask_token=snake_case__ , **snake_case__ , ) UpperCAmelCase : Tuple =vocab_file UpperCAmelCase : Optional[int] =False if not self.vocab_file else True def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : int =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None , snake_case__ = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is None: return [1] + ([0] * len(snake_case__ )) + [1] return [1] + ([0] * len(snake_case__ )) + [1] + ([0] * len(snake_case__ )) + [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : Optional[Any] =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> Tuple[str]: '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(snake_case__ ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCAmelCase : Optional[int] =os.path.join( snake_case__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case__ ): copyfile(self.vocab_file , snake_case__ ) return (out_vocab_file,)
348
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available A ={ 'configuration_groupvit': [ 'GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GroupViTConfig', 'GroupViTOnnxConfig', 'GroupViTTextConfig', 'GroupViTVisionConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A =[ 'GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GroupViTModel', 'GroupViTPreTrainedModel', 'GroupViTTextModel', 'GroupViTVisionModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A =[ 'TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFGroupViTModel', 'TFGroupViTPreTrainedModel', 'TFGroupViTTextModel', 'TFGroupViTVisionModel', ] if TYPE_CHECKING: from .configuration_groupvit import ( GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GroupViTConfig, GroupViTOnnxConfig, GroupViTTextConfig, GroupViTVisionConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_groupvit import ( GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, GroupViTModel, GroupViTPreTrainedModel, GroupViTTextModel, GroupViTVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_groupvit import ( TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFGroupViTModel, TFGroupViTPreTrainedModel, TFGroupViTTextModel, TFGroupViTVisionModel, ) else: import sys A =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
34
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def lowerCAmelCase_ ( __lowerCAmelCase )-> Optional[Any]: '''simple docstring''' def is_in_circle(__lowerCAmelCase , __lowerCAmelCase ) -> bool: UpperCAmelCase : List[Any] =sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle UpperCAmelCase : List[Any] =mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(__lowerCAmelCase ) ) # The ratio of the area for circle to square is pi/4. UpperCAmelCase : Dict =proportion * 4 print(f'''The estimated value of pi is {pi_estimate}''' ) print(f'''The numpy value of pi is {pi}''' ) print(f'''The total error is {abs(pi - pi_estimate )}''' ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 , )-> float: '''simple docstring''' return mean( function_to_integrate(uniform(__lowerCAmelCase , __lowerCAmelCase ) ) for _ in range(__lowerCAmelCase ) ) * (max_value - min_value) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 )-> None: '''simple docstring''' def identity_function(__lowerCAmelCase ) -> float: return x UpperCAmelCase : List[Any] =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase : Dict =(max_value * max_value - min_value * min_value) / 2 print('''******************''' ) print(f'''Estimating area under y=x where x varies from {min_value} to {max_value}''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {expected_value}''' ) print(f'''Total error is {abs(estimated_value - expected_value )}''' ) print('''******************''' ) def lowerCAmelCase_ ( __lowerCAmelCase )-> None: '''simple docstring''' def function_to_integrate(__lowerCAmelCase ) -> float: return sqrt(4.0 - x * x ) UpperCAmelCase : Dict =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , 0.0 , 2.0 ) print('''******************''' ) print('''Estimating pi using area_under_curve_estimator''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {pi}''' ) print(f'''Total error is {abs(estimated_value - pi )}''' ) print('''******************''' ) if __name__ == "__main__": import doctest doctest.testmod()
348
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) __a = { "configuration_blip": [ "BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlipConfig", "BlipTextConfig", "BlipVisionConfig", ], "processing_blip": ["BlipProcessor"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ["BlipImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ "BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "BlipModel", "BlipPreTrainedModel", "BlipForConditionalGeneration", "BlipForQuestionAnswering", "BlipVisionModel", "BlipTextModel", "BlipForImageTextRetrieval", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ "TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "TFBlipModel", "TFBlipPreTrainedModel", "TFBlipForConditionalGeneration", "TFBlipForQuestionAnswering", "TFBlipVisionModel", "TFBlipTextModel", "TFBlipForImageTextRetrieval", ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys __a = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
35
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __snake_case : def __init__( self , snake_case__ , snake_case__=12 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , snake_case__=0 , snake_case__=None , ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : List[Any] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Union[str, Any] =use_input_mask UpperCAmelCase : Tuple =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : Tuple =hidden_size UpperCAmelCase : Dict =projection_dim UpperCAmelCase : Optional[int] =num_hidden_layers UpperCAmelCase : Dict =num_attention_heads UpperCAmelCase : int =intermediate_size UpperCAmelCase : Any =dropout UpperCAmelCase : Union[str, Any] =attention_dropout UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : str =scope UpperCAmelCase : str =bos_token_id def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : int =None if self.use_input_mask: UpperCAmelCase : Union[str, Any] =random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: UpperCAmelCase : Optional[int] =input_mask.numpy() UpperCAmelCase , UpperCAmelCase : List[Any] =input_mask.shape UpperCAmelCase : Optional[Any] =np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : List[Any] =1 UpperCAmelCase : Tuple =0 UpperCAmelCase : List[Any] =self.get_config() return config, input_ids, tf.convert_to_tensor(snake_case__ ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =TFBlipTextModel(config=snake_case__ ) UpperCAmelCase : List[Any] =model(snake_case__ , attention_mask=snake_case__ , training=snake_case__ ) UpperCAmelCase : str =model(snake_case__ , training=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[Any] =config_and_inputs UpperCAmelCase : Optional[int] ={'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __snake_case ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Optional[int] = (TFBlipTextModel,) if is_tf_available() else () __lowerCamelCase : Dict = False __lowerCamelCase : Optional[Any] = False __lowerCamelCase : Dict = False def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : str =BlipTextModelTester(self ) UpperCAmelCase : Optional[int] =ConfigTester(self , config_class=snake_case__ , hidden_size=37 ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip(reason='''Blip does not use inputs_embeds''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @slow def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase : Optional[Any] =TFBlipTextModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def UpperCAmelCase__ ( self , snake_case__=True ) -> Any: '''simple docstring''' super().test_pt_tf_model_equivalence(allow_missing_keys=snake_case__ )
348
0
import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem _snake_case = importlib.util.find_spec("s3fs") is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 _snake_case = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(f'''A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.''') fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def A ( _lowerCamelCase ): '''simple docstring''' if "://" in dataset_path: _lowerCAmelCase : Any = dataset_path.split("://" )[1] return dataset_path def A ( _lowerCamelCase ): '''simple docstring''' if fs is not None and fs.protocol != "file": return True else: return False def A ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' _lowerCAmelCase : Tuple = not is_remote_filesystem(_lowerCamelCase ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(_lowerCamelCase ) , fs._strip_protocol(_lowerCamelCase ) ) else: fs.mv(_lowerCamelCase , _lowerCamelCase , recursive=_lowerCamelCase ) def A ( ): '''simple docstring''' if hasattr(fsspec.asyn , "reset_lock" ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: _lowerCAmelCase : Any = None _lowerCAmelCase : int = None _lowerCAmelCase : List[Any] = threading.Lock()
36
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging __snake_case = logging.get_logger(__name__) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' UpperCAmelCase : Dict =nn.functional.normalize(__lowerCAmelCase ) UpperCAmelCase : Tuple =nn.functional.normalize(__lowerCAmelCase ) return torch.mm(__lowerCAmelCase , normalized_text_embeds.t() ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : List[str] = CLIPConfig __lowerCamelCase : List[Any] = ["""CLIPEncoderLayer"""] def __init__( self , snake_case__ ) -> Dict: '''simple docstring''' super().__init__(snake_case__ ) UpperCAmelCase : Dict =CLIPVisionModel(config.vision_config ) UpperCAmelCase : Optional[Any] =nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=snake_case__ ) UpperCAmelCase : int =nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : List[str] =nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : str =nn.Parameter(torch.ones(17 ) , requires_grad=snake_case__ ) UpperCAmelCase : Optional[int] =nn.Parameter(torch.ones(3 ) , requires_grad=snake_case__ ) @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Union[str, Any] =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : Optional[Any] =self.visual_projection(snake_case__ ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 UpperCAmelCase : List[str] =cosine_distance(snake_case__ , self.special_care_embeds ).cpu().float().numpy() UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ).cpu().float().numpy() UpperCAmelCase : Tuple =[] UpperCAmelCase : Dict =image_embeds.shape[0] for i in range(snake_case__ ): UpperCAmelCase : str ={'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : str =0.0 for concept_idx in range(len(special_cos_dist[0] ) ): UpperCAmelCase : Optional[Any] =special_cos_dist[i][concept_idx] UpperCAmelCase : Union[str, Any] =self.special_care_embeds_weights[concept_idx].item() UpperCAmelCase : str =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} ) UpperCAmelCase : int =0.01 for concept_idx in range(len(cos_dist[0] ) ): UpperCAmelCase : Any =cos_dist[i][concept_idx] UpperCAmelCase : Optional[int] =self.concept_embeds_weights[concept_idx].item() UpperCAmelCase : int =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(snake_case__ ) result.append(snake_case__ ) UpperCAmelCase : Optional[int] =[len(res['''bad_concepts'''] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Any =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : List[str] =self.visual_projection(snake_case__ ) UpperCAmelCase : Any =cosine_distance(snake_case__ , self.special_care_embeds ) UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : Optional[Any] =0.0 UpperCAmelCase : Any =special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) UpperCAmelCase : str =torch.any(special_scores > 0 , dim=1 ) UpperCAmelCase : List[Any] =special_care * 0.01 UpperCAmelCase : Union[str, Any] =special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) UpperCAmelCase : List[Any] =(cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) UpperCAmelCase : str =torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
348
0
'''simple docstring''' _lowerCAmelCase = frozenset( [ '''prompt''', '''height''', '''width''', '''guidance_scale''', '''negative_prompt''', '''prompt_embeds''', '''negative_prompt_embeds''', '''cross_attention_kwargs''', ] ) _lowerCAmelCase = frozenset(['''prompt''', '''negative_prompt''']) _lowerCAmelCase = frozenset([]) _lowerCAmelCase = frozenset(['''image''']) _lowerCAmelCase = frozenset( [ '''image''', '''height''', '''width''', '''guidance_scale''', ] ) _lowerCAmelCase = frozenset(['''image''']) _lowerCAmelCase = frozenset( [ '''prompt''', '''image''', '''height''', '''width''', '''guidance_scale''', '''negative_prompt''', '''prompt_embeds''', '''negative_prompt_embeds''', ] ) _lowerCAmelCase = frozenset(['''prompt''', '''image''', '''negative_prompt''']) _lowerCAmelCase = frozenset( [ # Text guided image variation with an image mask '''prompt''', '''image''', '''mask_image''', '''height''', '''width''', '''guidance_scale''', '''negative_prompt''', '''prompt_embeds''', '''negative_prompt_embeds''', ] ) _lowerCAmelCase = frozenset(['''prompt''', '''image''', '''mask_image''', '''negative_prompt''']) _lowerCAmelCase = frozenset( [ # image variation with an image mask '''image''', '''mask_image''', '''height''', '''width''', '''guidance_scale''', ] ) _lowerCAmelCase = frozenset(['''image''', '''mask_image''']) _lowerCAmelCase = frozenset( [ '''example_image''', '''image''', '''mask_image''', '''height''', '''width''', '''guidance_scale''', ] ) _lowerCAmelCase = frozenset(['''example_image''', '''image''', '''mask_image''']) _lowerCAmelCase = frozenset(['''class_labels''']) _lowerCAmelCase = frozenset(['''class_labels''']) _lowerCAmelCase = frozenset(['''batch_size''']) _lowerCAmelCase = frozenset([]) _lowerCAmelCase = frozenset(['''batch_size''']) _lowerCAmelCase = frozenset([]) _lowerCAmelCase = frozenset( [ '''prompt''', '''audio_length_in_s''', '''guidance_scale''', '''negative_prompt''', '''prompt_embeds''', '''negative_prompt_embeds''', '''cross_attention_kwargs''', ] ) _lowerCAmelCase = frozenset(['''prompt''', '''negative_prompt''']) _lowerCAmelCase = frozenset(['''input_tokens''']) _lowerCAmelCase = frozenset(['''input_tokens'''])
37
import argparse import intel_extension_for_pytorch as ipex import torch from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline __snake_case = argparse.ArgumentParser('''Stable Diffusion script with intel optimization''', add_help=False) parser.add_argument('''--dpm''', action='''store_true''', help='''Enable DPMSolver or not''') parser.add_argument('''--steps''', default=None, type=int, help='''Num inference steps''') __snake_case = parser.parse_args() __snake_case = '''cpu''' __snake_case = '''a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings''' __snake_case = '''path-to-your-trained-model''' __snake_case = StableDiffusionPipeline.from_pretrained(model_id) if args.dpm: __snake_case = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) __snake_case = pipe.to(device) # to channels last __snake_case = pipe.unet.to(memory_format=torch.channels_last) __snake_case = pipe.vae.to(memory_format=torch.channels_last) __snake_case = pipe.text_encoder.to(memory_format=torch.channels_last) if pipe.requires_safety_checker: __snake_case = pipe.safety_checker.to(memory_format=torch.channels_last) # optimize with ipex __snake_case = torch.randn(2, 4, 64, 64) __snake_case = torch.rand(1) * 9_99 __snake_case = torch.randn(2, 77, 7_68) __snake_case = (sample, timestep, encoder_hidden_status) try: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example) except Exception: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True) if pipe.requires_safety_checker: __snake_case = ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True) # compute __snake_case = 6_66 __snake_case = torch.Generator(device).manual_seed(seed) __snake_case = {'''generator''': generator} if args.steps is not None: __snake_case = args.steps with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa): __snake_case = pipe(prompt, **generate_kwargs).images[0] # save image image.save('''generated.png''')
348
0
print((lambda quine: quine % quine)('''print((lambda quine: quine %% quine)(%r))'''))
38
__snake_case = '''Input must be a string of 8 numbers plus letter''' __snake_case = '''TRWAGMYFPDXBNJZSQVHLCKE''' def lowerCAmelCase_ ( __lowerCAmelCase )-> bool: '''simple docstring''' if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCAmelCase : Optional[Any] =f'''Expected string as input, found {type(__lowerCAmelCase ).__name__}''' raise TypeError(__lowerCAmelCase ) UpperCAmelCase : List[Any] =spanish_id.replace('''-''' , '''''' ).upper() if len(__lowerCAmelCase ) != 9: raise ValueError(__lowerCAmelCase ) try: UpperCAmelCase : int =int(spanish_id_clean[0:8] ) UpperCAmelCase : Optional[int] =spanish_id_clean[8] except ValueError as ex: raise ValueError(__lowerCAmelCase ) from ex if letter.isdigit(): raise ValueError(__lowerCAmelCase ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
348
0
from sympy import diff, lambdify, symbols from sympy.functions import * # noqa: F403 def __A ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = "x" , __lowerCAmelCase = 10**-10 , __lowerCAmelCase = 1 , )-> complex: """simple docstring""" _UpperCAmelCase = symbols(__lowerCAmelCase ) _UpperCAmelCase = lambdify(__lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = lambdify(__lowerCAmelCase , diff(__lowerCAmelCase , __lowerCAmelCase ) ) _UpperCAmelCase = starting_point while True: if diff_function(__lowerCAmelCase ) != 0: _UpperCAmelCase = prev_guess - multiplicity * func(__lowerCAmelCase ) / diff_function( __lowerCAmelCase ) else: raise ZeroDivisionError('Could not find root' ) from None # Precision is checked by comparing the difference of consecutive guesses if abs(next_guess - prev_guess ) < precision: return next_guess _UpperCAmelCase = next_guess # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(F'''The root of sin(x) = 0 is {newton_raphson('sin(x)', 2)}''') # Find root of polynomial # Find fourth Root of 5 print(F'''The root of x**4 - 5 = 0 is {newton_raphson('x**4 -5', 0.4 +5J)}''') # Find value of e print( '''The root of log(y) - 1 = 0 is ''', F'''{newton_raphson('log(y) - 1', 2, variable='y')}''', ) # Exponential Roots print( '''The root of exp(x) - 1 = 0 is''', F'''{newton_raphson('exp(x) - 1', 10, precision=0.0_05)}''', ) # Find root of cos(x) print(F'''The root of cos(x) = 0 is {newton_raphson('cos(x)', 0)}''')
39
def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Dict =str(bin(__lowerCAmelCase ) ) binary_number += "0" * shift_amount return binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number < 0 or shift_amount < 0: raise ValueError('''both inputs must be positive integers''' ) UpperCAmelCase : Any =str(bin(__lowerCAmelCase ) )[2:] if shift_amount >= len(__lowerCAmelCase ): return "0b0" UpperCAmelCase : Optional[Any] =binary_number[: len(__lowerCAmelCase ) - shift_amount] return "0b" + shifted_binary_number def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' if number >= 0: # Get binary representation of positive number UpperCAmelCase : Optional[Any] ='''0''' + str(bin(__lowerCAmelCase ) ).strip('''-''' )[2:] else: # Get binary (2's complement) representation of negative number UpperCAmelCase : int =len(bin(__lowerCAmelCase )[3:] ) # Find 2's complement of number UpperCAmelCase : Any =bin(abs(__lowerCAmelCase ) - (1 << binary_number_length) )[3:] UpperCAmelCase : Optional[Any] =( '''1''' + '''0''' * (binary_number_length - len(__lowerCAmelCase )) + binary_number ) if shift_amount >= len(__lowerCAmelCase ): return "0b" + binary_number[0] * len(__lowerCAmelCase ) return ( "0b" + binary_number[0] * shift_amount + binary_number[: len(__lowerCAmelCase ) - shift_amount] ) if __name__ == "__main__": import doctest doctest.testmod()
348
0
"""simple docstring""" import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def lowercase ( A_ )-> List[str]: '''simple docstring''' a : List[str] = {} a : Optional[Any] = tokenizer(example["content"] , truncation=A_ )["input_ids"] a : Optional[Any] = len(example["content"] ) / len(output["input_ids"] ) return output __lowercase = HfArgumentParser(PretokenizationArguments) __lowercase = parser.parse_args() if args.num_workers is None: __lowercase = multiprocessing.cpu_count() __lowercase = AutoTokenizer.from_pretrained(args.tokenizer_dir) __lowercase = time.time() __lowercase = load_dataset(args.dataset_name, split="""train""") print(f'''Dataset loaded in {time.time()-t_start:.2f}s''') __lowercase = time.time() __lowercase = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ """repo_name""", """path""", """copies""", """size""", """content""", """license""", """hash""", """line_mean""", """line_max""", """alpha_frac""", """autogenerated""", ], ) print(f'''Dataset tokenized in {time.time()-t_start:.2f}s''') __lowercase = time.time() ds.push_to_hub(args.tokenized_data_repo) print(f'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
40
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) # TODO Update this __snake_case = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Tuple = """esm""" def __init__( self , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=1026 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__="absolute" , snake_case__=True , snake_case__=None , snake_case__=False , snake_case__=False , snake_case__=None , snake_case__=None , **snake_case__ , ) -> Union[str, Any]: '''simple docstring''' super().__init__(pad_token_id=snake_case__ , mask_token_id=snake_case__ , **snake_case__ ) UpperCAmelCase : List[str] =vocab_size UpperCAmelCase : str =hidden_size UpperCAmelCase : List[Any] =num_hidden_layers UpperCAmelCase : Optional[Any] =num_attention_heads UpperCAmelCase : str =intermediate_size UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : int =attention_probs_dropout_prob UpperCAmelCase : Dict =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : Union[str, Any] =layer_norm_eps UpperCAmelCase : Dict =position_embedding_type UpperCAmelCase : Optional[Any] =use_cache UpperCAmelCase : int =emb_layer_norm_before UpperCAmelCase : List[str] =token_dropout UpperCAmelCase : Optional[Any] =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) UpperCAmelCase : Optional[Any] =EsmFoldConfig() elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =EsmFoldConfig(**snake_case__ ) UpperCAmelCase : Tuple =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) UpperCAmelCase : Any =get_default_vocab_list() else: UpperCAmelCase : Tuple =vocab_list else: UpperCAmelCase : Optional[int] =None UpperCAmelCase : Union[str, Any] =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , snake_case__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =super().to_dict() if isinstance(self.esmfold_config , snake_case__ ): UpperCAmelCase : str =self.esmfold_config.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : str = None __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : float = 0 __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : int = 128 __lowerCamelCase : "TrunkConfig" = None def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' if self.trunk is None: UpperCAmelCase : str =TrunkConfig() elif isinstance(self.trunk , snake_case__ ): UpperCAmelCase : Optional[int] =TrunkConfig(**self.trunk ) def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[Any] =asdict(self ) UpperCAmelCase : Any =self.trunk.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 48 __lowerCamelCase : int = 1024 __lowerCamelCase : int = 128 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : int = 32 __lowerCamelCase : float = 0 __lowerCamelCase : float = 0 __lowerCamelCase : bool = False __lowerCamelCase : int = 4 __lowerCamelCase : Optional[int] = 128 __lowerCamelCase : "StructureModuleConfig" = None def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' if self.structure_module is None: UpperCAmelCase : Any =StructureModuleConfig() elif isinstance(self.structure_module , snake_case__ ): UpperCAmelCase : str =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f'''`max_recycles` should be positive, got {self.max_recycles}.''' ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f''' {self.sequence_state_dim} and {self.sequence_state_dim}.''' ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f''' {self.pairwise_state_dim} and {self.pairwise_state_dim}.''' ) UpperCAmelCase : Optional[int] =self.sequence_state_dim // self.sequence_head_width UpperCAmelCase : Any =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f''' {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.''' ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f''' {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.''' ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f'''`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.''' ) if self.dropout >= 0.4: raise ValueError(f'''`dropout` should not be greater than 0.4, got {self.dropout}.''' ) def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =asdict(self ) UpperCAmelCase : Tuple =self.structure_module.to_dict() return output @dataclass class __snake_case : __lowerCamelCase : int = 384 __lowerCamelCase : int = 128 __lowerCamelCase : int = 16 __lowerCamelCase : int = 128 __lowerCamelCase : int = 12 __lowerCamelCase : int = 4 __lowerCamelCase : int = 8 __lowerCamelCase : float = 0.1 __lowerCamelCase : int = 8 __lowerCamelCase : int = 1 __lowerCamelCase : int = 2 __lowerCamelCase : int = 7 __lowerCamelCase : int = 10 __lowerCamelCase : float = 1E-8 __lowerCamelCase : float = 1E5 def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return asdict(self ) def lowerCAmelCase_ ( )-> Tuple: '''simple docstring''' return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
348
0
'''simple docstring''' import unittest import torch from diffusers import DDIMScheduler, DDPMScheduler, UNetaDModel from diffusers.training_utils import set_seed from diffusers.utils.testing_utils import slow _A : Union[str, Any] =False class _lowercase ( unittest.TestCase ): def lowerCamelCase_ ( self: int , UpperCamelCase__: Dict=32 ): set_seed(0 ) lowerCamelCase__ : str = UNetaDModel(sample_size=UpperCamelCase__ , in_channels=3 , out_channels=3 ) lowerCamelCase__ : Dict = torch.optim.SGD(model.parameters() , lr=0.0_001 ) return model, optimizer @slow def lowerCamelCase_ ( self: Dict ): lowerCamelCase__ : Any = """cpu""" # ensure full determinism without setting the CUBLAS_WORKSPACE_CONFIG env variable lowerCamelCase__ : int = DDPMScheduler( num_train_timesteps=1_000 , beta_start=0.0_001 , beta_end=0.02 , beta_schedule="""linear""" , clip_sample=UpperCamelCase__ , ) lowerCamelCase__ : int = DDIMScheduler( num_train_timesteps=1_000 , beta_start=0.0_001 , beta_end=0.02 , beta_schedule="""linear""" , clip_sample=UpperCamelCase__ , ) assert ddpm_scheduler.config.num_train_timesteps == ddim_scheduler.config.num_train_timesteps # shared batches for DDPM and DDIM set_seed(0 ) lowerCamelCase__ : Tuple = [torch.randn((4, 3, 32, 32) ).clip(-1 , 1 ).to(UpperCamelCase__ ) for _ in range(4 )] lowerCamelCase__ : str = [torch.randn((4, 3, 32, 32) ).to(UpperCamelCase__ ) for _ in range(4 )] lowerCamelCase__ : Tuple = [torch.randint(0 , 1_000 , (4,) ).long().to(UpperCamelCase__ ) for _ in range(4 )] # train with a DDPM scheduler lowerCamelCase__ , lowerCamelCase__ : int = self.get_model_optimizer(resolution=32 ) model.train().to(UpperCamelCase__ ) for i in range(4 ): optimizer.zero_grad() lowerCamelCase__ : Any = ddpm_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i] ) lowerCamelCase__ : List[Any] = model(UpperCamelCase__ , timesteps[i] ).sample lowerCamelCase__ : Union[str, Any] = torch.nn.functional.mse_loss(UpperCamelCase__ , noise[i] ) loss.backward() optimizer.step() del model, optimizer # recreate the model and optimizer, and retry with DDIM lowerCamelCase__ , lowerCamelCase__ : Any = self.get_model_optimizer(resolution=32 ) model.train().to(UpperCamelCase__ ) for i in range(4 ): optimizer.zero_grad() lowerCamelCase__ : Union[str, Any] = ddim_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i] ) lowerCamelCase__ : List[Any] = model(UpperCamelCase__ , timesteps[i] ).sample lowerCamelCase__ : int = torch.nn.functional.mse_loss(UpperCamelCase__ , noise[i] ) loss.backward() optimizer.step() del model, optimizer self.assertTrue(torch.allclose(UpperCamelCase__ , UpperCamelCase__ , atol=1e-5 ) ) self.assertTrue(torch.allclose(UpperCamelCase__ , UpperCamelCase__ , atol=1e-5 ) )
41
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[int] = (KDPMaDiscreteScheduler,) __lowerCamelCase : List[str] = 10 def UpperCAmelCase__ ( self , **snake_case__ ) -> str: '''simple docstring''' UpperCAmelCase : int ={ '''num_train_timesteps''': 1100, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', } config.update(**snake_case__ ) return config def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=snake_case__ , beta_end=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=snake_case__ ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' UpperCAmelCase : Optional[Any] =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config(prediction_type='''v_prediction''' ) UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : str =self.dummy_model() UpperCAmelCase : Optional[Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : Union[str, Any] =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : str =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Any =model(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : int =output.prev_sample UpperCAmelCase : Dict =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Optional[Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.69_34e-07 ) < 1e-2 assert abs(result_mean.item() - 6.11_12e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.6_93_42_86_50_17_09_72e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0002 ) < 1e-3 def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : Any =self.scheduler_classes[0] UpperCAmelCase : Optional[int] =self.get_scheduler_config() UpperCAmelCase : Optional[Any] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps ) UpperCAmelCase : Optional[int] =self.dummy_model() UpperCAmelCase : Union[str, Any] =self.dummy_sample_deter * scheduler.init_noise_sigma UpperCAmelCase : str =sample.to(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : Dict =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] =model(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =output.prev_sample UpperCAmelCase : Any =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Union[str, Any] =torch.mean(torch.abs(snake_case__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' if torch_device == "mps": return UpperCAmelCase : List[Any] =self.scheduler_classes[0] UpperCAmelCase : Dict =self.get_scheduler_config() UpperCAmelCase : List[str] =scheduler_class(**snake_case__ ) scheduler.set_timesteps(self.num_inference_steps , device=snake_case__ ) UpperCAmelCase : int =self.dummy_model() UpperCAmelCase : Tuple =self.dummy_sample_deter.to(snake_case__ ) * scheduler.init_noise_sigma for t in scheduler.timesteps: UpperCAmelCase : Optional[Any] =scheduler.scale_model_input(snake_case__ , snake_case__ ) UpperCAmelCase : int =model(snake_case__ , snake_case__ ) UpperCAmelCase : str =scheduler.step(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : List[str] =output.prev_sample UpperCAmelCase : List[str] =torch.sum(torch.abs(snake_case__ ) ) UpperCAmelCase : Dict =torch.mean(torch.abs(snake_case__ ) ) if str(snake_case__ ).startswith('''cpu''' ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3
348
0
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class __UpperCAmelCase ( unittest.TestCase ): def lowerCamelCase ( self ): """simple docstring""" _snake_case = [[1, 2, 4], [1, 2, 3, 4]] _snake_case = DisjunctiveConstraint(lowerCAmelCase_ ) self.assertTrue(isinstance(dc.token_ids , lowerCAmelCase_ ) ) with self.assertRaises(lowerCAmelCase_ ): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]] ) ) with self.assertRaises(lowerCAmelCase_ ): DisjunctiveConstraint([torch.LongTensor([1, 2, 4] ), torch.LongTensor([1, 2, 3, 4, 5] )] ) def lowerCamelCase ( self ): """simple docstring""" _snake_case = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(lowerCAmelCase_ ): DisjunctiveConstraint(lowerCAmelCase_ ) # fails here def lowerCamelCase ( self ): """simple docstring""" _snake_case = [[1, 2, 3], [1, 2, 4]] _snake_case = DisjunctiveConstraint(lowerCAmelCase_ ) _snake_case , _snake_case , _snake_case = dc.update(1 ) _snake_case = stepped is True and completed is False and reset is False self.assertTrue(lowerCAmelCase_ ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) _snake_case , _snake_case , _snake_case = dc.update(2 ) _snake_case = stepped is True and completed is False and reset is False self.assertTrue(lowerCAmelCase_ ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) _snake_case , _snake_case , _snake_case = dc.update(3 ) _snake_case = stepped is True and completed is True and reset is False self.assertTrue(lowerCAmelCase_ ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3] ) def lowerCamelCase ( self ): """simple docstring""" _snake_case = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] _snake_case = DisjunctiveConstraint(lowerCAmelCase_ ) _snake_case , _snake_case , _snake_case = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) _snake_case , _snake_case , _snake_case = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) _snake_case , _snake_case , _snake_case = dc.update(4 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2, 4] ) _snake_case , _snake_case , _snake_case = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5] ) dc.reset() _snake_case , _snake_case , _snake_case = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 3 ) self.assertTrue(dc.current_seq == [1] ) _snake_case , _snake_case , _snake_case = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 2 ) self.assertTrue(dc.current_seq == [1, 2] ) _snake_case , _snake_case , _snake_case = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.remaining() == 0 ) self.assertTrue(dc.current_seq == [1, 2, 5] )
42
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class __snake_case ( unittest.TestCase ): @slow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Any =FlaxMTaForConditionalGeneration.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : Tuple =AutoTokenizer.from_pretrained('''google/mt5-small''' ) UpperCAmelCase : List[str] =tokenizer('''Hello there''' , return_tensors='''np''' ).input_ids UpperCAmelCase : List[Any] =tokenizer('''Hi I am''' , return_tensors='''np''' ).input_ids UpperCAmelCase : Union[str, Any] =shift_tokens_right(snake_case__ , model.config.pad_token_id , model.config.decoder_start_token_id ) UpperCAmelCase : List[str] =model(snake_case__ , decoder_input_ids=snake_case__ ).logits UpperCAmelCase : Any =optax.softmax_cross_entropy(snake_case__ , onehot(snake_case__ , logits.shape[-1] ) ).mean() UpperCAmelCase : Union[str, Any] =-(labels.shape[-1] * loss.item()) UpperCAmelCase : List[str] =-84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
348
0
import argparse import json import pickle from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import MaskFormerConfig, MaskFormerForInstanceSegmentation, MaskFormerImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() __lowercase = logging.get_logger(__name__) def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :List[str] = SwinConfig.from_pretrained( '''microsoft/swin-tiny-patch4-window7-224''' , out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] ) __UpperCamelCase :str = MaskFormerConfig(backbone_config=SCREAMING_SNAKE_CASE ) __UpperCamelCase :List[str] = '''huggingface/label-files''' if "ade20k-full" in model_name: # this should be ok __UpperCamelCase :Union[str, Any] = 847 __UpperCamelCase :str = '''maskformer-ade20k-full-id2label.json''' elif "ade" in model_name: # this should be ok __UpperCamelCase :List[Any] = 150 __UpperCamelCase :Union[str, Any] = '''ade20k-id2label.json''' elif "coco-stuff" in model_name: # this should be ok __UpperCamelCase :str = 171 __UpperCamelCase :Optional[int] = '''maskformer-coco-stuff-id2label.json''' elif "coco" in model_name: # TODO __UpperCamelCase :Optional[Any] = 133 __UpperCamelCase :Optional[Any] = '''coco-panoptic-id2label.json''' elif "cityscapes" in model_name: # this should be ok __UpperCamelCase :Dict = 19 __UpperCamelCase :Tuple = '''cityscapes-id2label.json''' elif "vistas" in model_name: # this should be ok __UpperCamelCase :Any = 65 __UpperCamelCase :Union[str, Any] = '''mapillary-vistas-id2label.json''' __UpperCamelCase :List[Any] = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , repo_type='''dataset''' ) , '''r''' ) ) __UpperCamelCase :List[Any] = {int(SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} return config def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :int = [] # stem # fmt: off rename_keys.append(('''backbone.patch_embed.proj.weight''', '''model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.patch_embed.proj.bias''', '''model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.patch_embed.norm.weight''', '''model.pixel_level_module.encoder.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.patch_embed.norm.bias''', '''model.pixel_level_module.encoder.model.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.norm1.weight""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.norm1.bias""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.attn.relative_position_bias_table""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.attn.relative_position_index""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.attn.proj.weight""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.attn.proj.bias""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.norm2.weight""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.norm2.bias""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.mlp.fc1.weight""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.mlp.fc1.bias""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.mlp.fc2.weight""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.weight""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.mlp.fc2.bias""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.bias""") ) if i < 3: rename_keys.append((f"""backbone.layers.{i}.downsample.reduction.weight""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.reduction.weight""") ) rename_keys.append((f"""backbone.layers.{i}.downsample.norm.weight""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.weight""") ) rename_keys.append((f"""backbone.layers.{i}.downsample.norm.bias""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.bias""") ) rename_keys.append((f"""backbone.norm{i}.weight""", f"""model.pixel_level_module.encoder.hidden_states_norms.{i}.weight""") ) rename_keys.append((f"""backbone.norm{i}.bias""", f"""model.pixel_level_module.encoder.hidden_states_norms.{i}.bias""") ) # FPN rename_keys.append(('''sem_seg_head.layer_4.weight''', '''model.pixel_level_module.decoder.fpn.stem.0.weight''') ) rename_keys.append(('''sem_seg_head.layer_4.norm.weight''', '''model.pixel_level_module.decoder.fpn.stem.1.weight''') ) rename_keys.append(('''sem_seg_head.layer_4.norm.bias''', '''model.pixel_level_module.decoder.fpn.stem.1.bias''') ) for source_index, target_index in zip(range(3 , 0 , -1 ) , range(0 , 3 ) ): rename_keys.append((f"""sem_seg_head.adapter_{source_index}.weight""", f"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.0.weight""") ) rename_keys.append((f"""sem_seg_head.adapter_{source_index}.norm.weight""", f"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.weight""") ) rename_keys.append((f"""sem_seg_head.adapter_{source_index}.norm.bias""", f"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.bias""") ) rename_keys.append((f"""sem_seg_head.layer_{source_index}.weight""", f"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.0.weight""") ) rename_keys.append((f"""sem_seg_head.layer_{source_index}.norm.weight""", f"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.weight""") ) rename_keys.append((f"""sem_seg_head.layer_{source_index}.norm.bias""", f"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.bias""") ) rename_keys.append(('''sem_seg_head.mask_features.weight''', '''model.pixel_level_module.decoder.mask_projection.weight''') ) rename_keys.append(('''sem_seg_head.mask_features.bias''', '''model.pixel_level_module.decoder.mask_projection.bias''') ) # Transformer decoder for idx in range(config.decoder_config.decoder_layers ): # self-attention out projection rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.weight""", f"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.bias""", f"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.bias""") ) # cross-attention out projection rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.weight""", f"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.bias""", f"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.bias""") ) # MLP 1 rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.weight""", f"""model.transformer_module.decoder.layers.{idx}.fc1.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.bias""", f"""model.transformer_module.decoder.layers.{idx}.fc1.bias""") ) # MLP 2 rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.weight""", f"""model.transformer_module.decoder.layers.{idx}.fc2.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.bias""", f"""model.transformer_module.decoder.layers.{idx}.fc2.bias""") ) # layernorm 1 (self-attention layernorm) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.weight""", f"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.bias""", f"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.bias""") ) # layernorm 2 (cross-attention layernorm) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.weight""", f"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.bias""", f"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.bias""") ) # layernorm 3 (final layernorm) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.weight""", f"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.bias""", f"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.bias""") ) rename_keys.append(('''sem_seg_head.predictor.transformer.decoder.norm.weight''', '''model.transformer_module.decoder.layernorm.weight''') ) rename_keys.append(('''sem_seg_head.predictor.transformer.decoder.norm.bias''', '''model.transformer_module.decoder.layernorm.bias''') ) # heads on top rename_keys.append(('''sem_seg_head.predictor.query_embed.weight''', '''model.transformer_module.queries_embedder.weight''') ) rename_keys.append(('''sem_seg_head.predictor.input_proj.weight''', '''model.transformer_module.input_projection.weight''') ) rename_keys.append(('''sem_seg_head.predictor.input_proj.bias''', '''model.transformer_module.input_projection.bias''') ) rename_keys.append(('''sem_seg_head.predictor.class_embed.weight''', '''class_predictor.weight''') ) rename_keys.append(('''sem_seg_head.predictor.class_embed.bias''', '''class_predictor.bias''') ) for i in range(3 ): rename_keys.append((f"""sem_seg_head.predictor.mask_embed.layers.{i}.weight""", f"""mask_embedder.{i}.0.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.mask_embed.layers.{i}.bias""", f"""mask_embedder.{i}.0.bias""") ) # fmt: on return rename_keys def lowerCamelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :List[str] = dct.pop(SCREAMING_SNAKE_CASE ) __UpperCamelCase :Dict = val def lowerCamelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :int = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): __UpperCamelCase :int = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) __UpperCamelCase :Any = state_dict.pop(f"""backbone.layers.{i}.blocks.{j}.attn.qkv.weight""" ) __UpperCamelCase :List[Any] = state_dict.pop(f"""backbone.layers.{i}.blocks.{j}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict __UpperCamelCase :int = in_proj_weight[:dim, :] __UpperCamelCase :Union[str, Any] = in_proj_bias[: dim] __UpperCamelCase :List[Any] = in_proj_weight[ dim : dim * 2, : ] __UpperCamelCase :Union[str, Any] = in_proj_bias[ dim : dim * 2 ] __UpperCamelCase :Optional[Any] = in_proj_weight[ -dim :, : ] __UpperCamelCase :Any = in_proj_bias[-dim :] # fmt: on def lowerCamelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :Dict = config.decoder_config.hidden_size for idx in range(config.decoder_config.decoder_layers ): # read in weights + bias of self-attention input projection layer (in the original implementation, this is a single matrix + bias) __UpperCamelCase :Union[str, Any] = state_dict.pop(f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_weight""" ) __UpperCamelCase :Optional[int] = state_dict.pop(f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict __UpperCamelCase :Optional[Any] = in_proj_weight[: hidden_size, :] __UpperCamelCase :Tuple = in_proj_bias[:config.hidden_size] __UpperCamelCase :Any = in_proj_weight[hidden_size : hidden_size * 2, :] __UpperCamelCase :Dict = in_proj_bias[hidden_size : hidden_size * 2] __UpperCamelCase :List[str] = in_proj_weight[-hidden_size :, :] __UpperCamelCase :Optional[Any] = in_proj_bias[-hidden_size :] # read in weights + bias of cross-attention input projection layer (in the original implementation, this is a single matrix + bias) __UpperCamelCase :Dict = state_dict.pop(f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_weight""" ) __UpperCamelCase :Union[str, Any] = state_dict.pop(f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict __UpperCamelCase :List[str] = in_proj_weight[: hidden_size, :] __UpperCamelCase :str = in_proj_bias[:config.hidden_size] __UpperCamelCase :Optional[int] = in_proj_weight[hidden_size : hidden_size * 2, :] __UpperCamelCase :Union[str, Any] = in_proj_bias[hidden_size : hidden_size * 2] __UpperCamelCase :Optional[Any] = in_proj_weight[-hidden_size :, :] __UpperCamelCase :Optional[int] = in_proj_bias[-hidden_size :] # fmt: on def lowerCamelCase ( ): '''simple docstring''' __UpperCamelCase :Union[str, Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' __UpperCamelCase :List[str] = Image.open(requests.get(SCREAMING_SNAKE_CASE , stream=SCREAMING_SNAKE_CASE ).raw ) return im @torch.no_grad() def lowerCamelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = False ): '''simple docstring''' __UpperCamelCase :str = get_maskformer_config(SCREAMING_SNAKE_CASE ) # load original state_dict with open(SCREAMING_SNAKE_CASE , '''rb''' ) as f: __UpperCamelCase :str = pickle.load(SCREAMING_SNAKE_CASE ) __UpperCamelCase :int = data['''model'''] # for name, param in state_dict.items(): # print(name, param.shape) # rename keys __UpperCamelCase :Optional[Any] = create_rename_keys(SCREAMING_SNAKE_CASE ) for src, dest in rename_keys: rename_key(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) read_in_swin_q_k_v(SCREAMING_SNAKE_CASE , config.backbone_config ) read_in_decoder_q_k_v(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # update to torch tensors for key, value in state_dict.items(): __UpperCamelCase :Dict = torch.from_numpy(SCREAMING_SNAKE_CASE ) # load 🤗 model __UpperCamelCase :Optional[int] = MaskFormerForInstanceSegmentation(SCREAMING_SNAKE_CASE ) model.eval() for name, param in model.named_parameters(): print(SCREAMING_SNAKE_CASE , param.shape ) __UpperCamelCase , __UpperCamelCase :Optional[int] = model.load_state_dict(SCREAMING_SNAKE_CASE , strict=SCREAMING_SNAKE_CASE ) assert missing_keys == [ "model.pixel_level_module.encoder.model.layernorm.weight", "model.pixel_level_module.encoder.model.layernorm.bias", ] assert len(SCREAMING_SNAKE_CASE ) == 0, f"""Unexpected keys: {unexpected_keys}""" # verify results __UpperCamelCase :Any = prepare_img() if "vistas" in model_name: __UpperCamelCase :Dict = 65 elif "cityscapes" in model_name: __UpperCamelCase :int = 65_535 else: __UpperCamelCase :Dict = 255 __UpperCamelCase :Union[str, Any] = True if '''ade''' in model_name else False __UpperCamelCase :Tuple = MaskFormerImageProcessor(ignore_index=SCREAMING_SNAKE_CASE , reduce_labels=SCREAMING_SNAKE_CASE ) __UpperCamelCase :Union[str, Any] = image_processor(SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) __UpperCamelCase :str = model(**SCREAMING_SNAKE_CASE ) print('''Logits:''' , outputs.class_queries_logits[0, :3, :3] ) if model_name == "maskformer-swin-tiny-ade": __UpperCamelCase :List[Any] = torch.tensor( [[3.6_353, -4.4_770, -2.6_065], [0.5_081, -4.2_394, -3.5_343], [2.1_909, -5.0_353, -1.9_323]] ) assert torch.allclose(outputs.class_queries_logits[0, :3, :3] , SCREAMING_SNAKE_CASE , atol=1e-4 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(f"""Saving model and image processor to {pytorch_dump_folder_path}""" ) Path(SCREAMING_SNAKE_CASE ).mkdir(exist_ok=SCREAMING_SNAKE_CASE ) model.save_pretrained(SCREAMING_SNAKE_CASE ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE ) if push_to_hub: print('''Pushing model and image processor to the hub...''' ) model.push_to_hub(f"""nielsr/{model_name}""" ) image_processor.push_to_hub(f"""nielsr/{model_name}""" ) if __name__ == "__main__": __lowercase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''maskformer-swin-tiny-ade''', type=str, help=('''Name of the MaskFormer model you\'d like to convert''',), ) parser.add_argument( '''--checkpoint_path''', default='''/Users/nielsrogge/Documents/MaskFormer_checkpoints/MaskFormer-Swin-tiny-ADE20k/model.pkl''', type=str, help='''Path to the original state dict (.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_maskformer_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
43
import unittest import numpy as np from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __snake_case ( lowerCamelCase__ , unittest.TestCase ): # FIXME: add fast tests pass @nightly @require_onnxruntime @require_torch_gpu class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : List[Any] =ort.SessionOptions() UpperCAmelCase : Optional[int] =False return options def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : int =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Optional[Any] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Dict ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : Any =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=10 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : Optional[int] =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Tuple =np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) UpperCAmelCase : Tuple =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) UpperCAmelCase : List[str] =LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , subfolder='''scheduler''' , revision='''onnx''' ) UpperCAmelCase : int =OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , scheduler=snake_case__ , safety_checker=snake_case__ , feature_extractor=snake_case__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Union[str, Any] ='''A red cat sitting on a park bench''' UpperCAmelCase : int =np.random.RandomState(0 ) UpperCAmelCase : str =pipe( prompt=snake_case__ , image=snake_case__ , mask_image=snake_case__ , guidance_scale=7.5 , num_inference_steps=20 , generator=snake_case__ , output_type='''np''' , ) UpperCAmelCase : Dict =output.images UpperCAmelCase : int =images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) UpperCAmelCase : Union[str, Any] =np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3
348
0
"""simple docstring""" import json import os import torch from diffusers import UNetaDModel os.makedirs('hub/hopper-medium-v2/unet/hor32', exist_ok=True) os.makedirs('hub/hopper-medium-v2/unet/hor128', exist_ok=True) os.makedirs('hub/hopper-medium-v2/value_function', exist_ok=True) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Dict ) -> Dict: if hor == 128: _lowerCAmelCase : Any = ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""") _lowerCAmelCase : Optional[int] = (32, 128, 256) _lowerCAmelCase : Any = ("""UpResnetBlock1D""", """UpResnetBlock1D""") elif hor == 32: _lowerCAmelCase : Tuple = ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""") _lowerCAmelCase : int = (32, 64, 128, 256) _lowerCAmelCase : List[Any] = ("""UpResnetBlock1D""", """UpResnetBlock1D""", """UpResnetBlock1D""") _lowerCAmelCase : Tuple = torch.load(f"/Users/bglickenhaus/Documents/diffuser/temporal_unet-hopper-mediumv2-hor{hor}.torch" ) _lowerCAmelCase : Union[str, Any] = model.state_dict() _lowerCAmelCase : Dict = { """down_block_types""": down_block_types, """block_out_channels""": block_out_channels, """up_block_types""": up_block_types, """layers_per_block""": 1, """use_timestep_embedding""": True, """out_block_type""": """OutConv1DBlock""", """norm_num_groups""": 8, """downsample_each_block""": False, """in_channels""": 14, """out_channels""": 14, """extra_in_channels""": 0, """time_embedding_type""": """positional""", """flip_sin_to_cos""": False, """freq_shift""": 1, """sample_size""": 65536, """mid_block_type""": """MidResTemporalBlock1D""", """act_fn""": """mish""", } _lowerCAmelCase : Optional[int] = UNetaDModel(**_lowerCamelCase ) print(f"length of state dict: {len(state_dict.keys() )}" ) print(f"length of value function dict: {len(hf_value_function.state_dict().keys() )}" ) _lowerCAmelCase : int = dict(zip(model.state_dict().keys() ,hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): _lowerCAmelCase : Optional[int] = state_dict.pop(_lowerCamelCase ) hf_value_function.load_state_dict(_lowerCamelCase ) torch.save(hf_value_function.state_dict() ,f"hub/hopper-medium-v2/unet/hor{hor}/diffusion_pytorch_model.bin" ) with open(f"hub/hopper-medium-v2/unet/hor{hor}/config.json" ,"""w""" ) as f: json.dump(_lowerCamelCase ,_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( ) -> List[str]: _lowerCAmelCase : List[str] = { """in_channels""": 14, """down_block_types""": ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D"""), """up_block_types""": (), """out_block_type""": """ValueFunction""", """mid_block_type""": """ValueFunctionMidBlock1D""", """block_out_channels""": (32, 64, 128, 256), """layers_per_block""": 1, """downsample_each_block""": True, """sample_size""": 65536, """out_channels""": 14, """extra_in_channels""": 0, """time_embedding_type""": """positional""", """use_timestep_embedding""": True, """flip_sin_to_cos""": False, """freq_shift""": 1, """norm_num_groups""": 8, """act_fn""": """mish""", } _lowerCAmelCase : Union[str, Any] = torch.load("""/Users/bglickenhaus/Documents/diffuser/value_function-hopper-mediumv2-hor32.torch""" ) _lowerCAmelCase : Any = model _lowerCAmelCase : int = UNetaDModel(**_lowerCamelCase ) print(f"length of state dict: {len(state_dict.keys() )}" ) print(f"length of value function dict: {len(hf_value_function.state_dict().keys() )}" ) _lowerCAmelCase : Optional[Any] = dict(zip(state_dict.keys() ,hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): _lowerCAmelCase : Optional[int] = state_dict.pop(_lowerCamelCase ) hf_value_function.load_state_dict(_lowerCamelCase ) torch.save(hf_value_function.state_dict() ,"""hub/hopper-medium-v2/value_function/diffusion_pytorch_model.bin""" ) with open("""hub/hopper-medium-v2/value_function/config.json""" ,"""w""" ) as f: json.dump(_lowerCamelCase ,_lowerCamelCase ) if __name__ == "__main__": unet(32) # unet(128) value_function()
44
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def lowerCAmelCase_ ( )-> int: '''simple docstring''' UpperCAmelCase : str ={ '''repo_name''': ['''test_repo1''', '''test_repo2''', '''test_repo3'''], '''path''': ['''test_1.py''', '''test_2.py''', '''unit_test.py'''], '''content''': ['''a ''' * 20, '''a ''' * 30, '''b ''' * 7], } UpperCAmelCase : Union[str, Any] =Dataset.from_dict(__lowerCAmelCase ) return dataset class __snake_case ( lowerCamelCase__ ): def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[str] =get_dataset() UpperCAmelCase : Optional[int] =make_duplicate_clusters(snake_case__ , 0.85 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : str =get_dataset() UpperCAmelCase , UpperCAmelCase : Tuple =deduplicate_dataset(snake_case__ ) self.assertEqual(len(snake_case__ ) , 2 ) print(snake_case__ ) self.assertEqual(duplicate_clusters[0][0]['''copies'''] , 2 ) self.assertEqual(duplicate_clusters[0][0]['''is_extreme'''] , snake_case__ )
348
0
"""simple docstring""" import os import pickle import unittest from transformers import AutoTokenizer from transformers.models.bert.tokenization_bert import BertTokenizer from transformers.models.bert_japanese.tokenization_bert_japanese import ( VOCAB_FILES_NAMES, BertJapaneseTokenizer, CharacterTokenizer, JumanppTokenizer, MecabTokenizer, SudachiTokenizer, WordpieceTokenizer, ) from transformers.testing_utils import custom_tokenizers, require_jumanpp, require_sudachi from ...test_tokenization_common import TokenizerTesterMixin @custom_tokenizers class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = BertJapaneseTokenizer __UpperCAmelCase : Optional[int] = False __UpperCAmelCase : Any = True def __UpperCAmelCase ( self ): super().setUp() __a = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''こんにちは''', '''こん''', '''にちは''', '''ばんは''', '''##こん''', '''##にちは''', '''##ばんは''', '''世界''', '''##世界''', '''、''', '''##、''', '''。''', '''##。''', ] __a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) def __UpperCAmelCase ( self , _a ): __a = '''こんにちは、世界。 \nこんばんは、世界。''' __a = '''こんにちは 、 世界 。 こんばんは 、 世界 。''' return input_text, output_text def __UpperCAmelCase ( self , _a ): __a , __a = self.get_input_output_texts(_a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = tokenizer.decode(_a , clean_up_tokenization_spaces=_a ) return text, ids def __UpperCAmelCase ( self ): pass # TODO add if relevant def __UpperCAmelCase ( self ): pass # TODO add if relevant def __UpperCAmelCase ( self ): pass # TODO add if relevant def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file ) __a = tokenizer.tokenize('''こんにちは、世界。\nこんばんは、世界。''' ) self.assertListEqual(_a , ['''こんにちは''', '''、''', '''世界''', '''。''', '''こん''', '''##ばんは''', '''、''', '''世界''', '''。'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file , word_tokenizer_type='''mecab''' ) self.assertIsNotNone(_a ) __a = '''こんにちは、世界。\nこんばんは、世界。''' __a = tokenizer.tokenize(_a ) self.assertListEqual(_a , ['''こんにちは''', '''、''', '''世界''', '''。''', '''こん''', '''##ばんは''', '''、''', '''世界''', '''。'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) __a = os.path.join(self.tmpdirname , '''tokenizer.bin''' ) with open(_a , '''wb''' ) as handle: pickle.dump(_a , _a ) with open(_a , '''rb''' ) as handle: __a = pickle.load(_a ) __a = tokenizer_new.tokenize(_a ) self.assertListEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = MecabTokenizer(mecab_dic='''ipadic''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップルストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', '''。'''] , ) def __UpperCAmelCase ( self ): try: __a = MecabTokenizer(mecab_dic='''unidic_lite''' ) except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', '''。'''] , ) def __UpperCAmelCase ( self ): try: __a = MecabTokenizer(mecab_dic='''unidic''' ) except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', '''。'''] , ) def __UpperCAmelCase ( self ): __a = MecabTokenizer(do_lower_case=_a , mecab_dic='''ipadic''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップルストア''', '''で''', '''iphone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', '''。'''] , ) def __UpperCAmelCase ( self ): try: __a = MecabTokenizer( do_lower_case=_a , normalize_text=_a , mecab_option='''-d /usr/local/lib/mecab/dic/jumandic''' ) except RuntimeError: # if dict doesn't exist in the system, previous code raises this error. return self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップルストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れた''', '''\u3000''', '''。'''] , ) def __UpperCAmelCase ( self ): __a = MecabTokenizer(normalize_text=_a , mecab_dic='''ipadic''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップルストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', ''' ''', '''。'''] , ) @require_sudachi def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file , word_tokenizer_type='''sudachi''' ) self.assertIsNotNone(_a ) __a = '''こんにちは、世界。\nこんばんは、世界。''' __a = tokenizer.tokenize(_a ) self.assertListEqual(_a , ['''こんにちは''', '''、''', '''世界''', '''。''', '''こん''', '''##ばんは''', '''、''', '''世界''', '''。'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) __a = os.path.join(self.tmpdirname , '''tokenizer.bin''' ) with open(_a , '''wb''' ) as handle: pickle.dump(_a , _a ) with open(_a , '''rb''' ) as handle: __a = pickle.load(_a ) __a = tokenizer_new.tokenize(_a ) self.assertListEqual(_a , _a ) @require_sudachi def __UpperCAmelCase ( self ): __a = SudachiTokenizer(sudachi_dict_type='''core''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , [''' ''', '''\t''', '''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', ''' ''', '''が''', ''' ''', ''' ''', '''\n ''', '''発売''', '''さ''', '''れ''', '''た''', ''' ''', '''。''', ''' ''', ''' '''] , ) @require_sudachi def __UpperCAmelCase ( self ): __a = SudachiTokenizer(sudachi_dict_type='''core''' , sudachi_split_mode='''A''' ) self.assertListEqual(tokenizer.tokenize('''外国人参政権''' ) , ['''外国''', '''人''', '''参政''', '''権'''] ) @require_sudachi def __UpperCAmelCase ( self ): __a = SudachiTokenizer(sudachi_dict_type='''core''' , sudachi_split_mode='''B''' ) self.assertListEqual(tokenizer.tokenize('''外国人参政権''' ) , ['''外国人''', '''参政権'''] ) @require_sudachi def __UpperCAmelCase ( self ): __a = SudachiTokenizer(sudachi_dict_type='''core''' , sudachi_split_mode='''C''' ) self.assertListEqual(tokenizer.tokenize('''外国人参政権''' ) , ['''外国人参政権'''] ) @require_sudachi def __UpperCAmelCase ( self ): __a = SudachiTokenizer(do_lower_case=_a , sudachi_dict_type='''core''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , [''' ''', '''\t''', '''アップル''', '''ストア''', '''で''', '''iphone''', '''8''', ''' ''', '''が''', ''' ''', ''' ''', '''\n ''', '''発売''', '''さ''', '''れ''', '''た''', ''' ''', '''。''', ''' ''', ''' '''] , ) @require_sudachi def __UpperCAmelCase ( self ): __a = SudachiTokenizer(normalize_text=_a , sudachi_dict_type='''core''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , [''' ''', '''\t''', '''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', ''' ''', '''が''', ''' ''', ''' ''', '''\n ''', '''発売''', '''さ''', '''れ''', '''た''', '''\u3000''', '''。''', ''' ''', ''' '''] , ) @require_sudachi def __UpperCAmelCase ( self ): __a = SudachiTokenizer(trim_whitespace=_a , sudachi_dict_type='''core''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', '''。'''] , ) @require_jumanpp def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file , word_tokenizer_type='''jumanpp''' ) self.assertIsNotNone(_a ) __a = '''こんにちは、世界。\nこんばんは、世界。''' __a = tokenizer.tokenize(_a ) self.assertListEqual(_a , ['''こんにちは''', '''、''', '''世界''', '''。''', '''こん''', '''##ばんは''', '''、''', '''世界''', '''。'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) __a = os.path.join(self.tmpdirname , '''tokenizer.bin''' ) with open(_a , '''wb''' ) as handle: pickle.dump(_a , _a ) with open(_a , '''rb''' ) as handle: __a = pickle.load(_a ) __a = tokenizer_new.tokenize(_a ) self.assertListEqual(_a , _a ) @require_jumanpp def __UpperCAmelCase ( self ): __a = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''\u3000''', '''が''', '''\u3000''', '''\u3000''', '''\u3000''', '''発売''', '''さ''', '''れた''', '''\u3000''', '''。'''] , ) @require_jumanpp def __UpperCAmelCase ( self ): __a = JumanppTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iphone''', '''8''', '''\u3000''', '''が''', '''\u3000''', '''\u3000''', '''\u3000''', '''発売''', '''さ''', '''れた''', '''\u3000''', '''。'''] , ) @require_jumanpp def __UpperCAmelCase ( self ): __a = JumanppTokenizer(normalize_text=_a ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''ア''', '''ッ''', '''フ''', '''゚''', '''ル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''\u3000''', '''が''', '''\u3000''', '''\u3000''', '''\u3000''', '''発売''', '''さ''', '''れた''', '''\u3000''', '''。'''] , ) @require_jumanpp def __UpperCAmelCase ( self ): __a = JumanppTokenizer(trim_whitespace=_a ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れた''', '''。'''] , ) @require_jumanpp def __UpperCAmelCase ( self ): __a = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize('''ありがとうございますm(_ _)m見つけるのが大変です。''' ) , ['''ありがとう''', '''ございます''', '''m(_ _)m''', '''見つける''', '''の''', '''が''', '''大変です''', '''。'''] , ) def __UpperCAmelCase ( self ): __a = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''こんにちは''', '''こん''', '''にちは''', '''ばんは''', '''##こん''', '''##にちは''', '''##ばんは'''] __a = {} for i, token in enumerate(_a ): __a = i __a = WordpieceTokenizer(vocab=_a , unk_token='''[UNK]''' ) self.assertListEqual(tokenizer.tokenize('''''' ) , [] ) self.assertListEqual(tokenizer.tokenize('''こんにちは''' ) , ['''こんにちは'''] ) self.assertListEqual(tokenizer.tokenize('''こんばんは''' ) , ['''こん''', '''##ばんは'''] ) self.assertListEqual(tokenizer.tokenize('''こんばんは こんばんにちは こんにちは''' ) , ['''こん''', '''##ばんは''', '''[UNK]''', '''こんにちは'''] ) def __UpperCAmelCase ( self ): __a = BertJapaneseTokenizer.from_pretrained('''nlp-waseda/roberta-base-japanese-with-auto-jumanpp''' ) __a = tokenizer.subword_tokenizer __a = subword_tokenizer.tokenize('''国境 の 長い トンネル を 抜ける と 雪国 であった 。''' ) self.assertListEqual(_a , ['''▁国境''', '''▁の''', '''▁長い''', '''▁トンネル''', '''▁を''', '''▁抜ける''', '''▁と''', '''▁雪''', '''国''', '''▁であった''', '''▁。'''] ) __a = subword_tokenizer.tokenize('''こんばんは こんばん にち は こんにちは''' ) self.assertListEqual(_a , ['''▁こん''', '''ばん''', '''は''', '''▁こん''', '''ばん''', '''▁に''', '''ち''', '''▁は''', '''▁こんにちは'''] ) def __UpperCAmelCase ( self ): __a = self.tokenizer_class.from_pretrained('''cl-tohoku/bert-base-japanese''' ) __a = tokenizer.encode('''ありがとう。''' , add_special_tokens=_a ) __a = tokenizer.encode('''どういたしまして。''' , add_special_tokens=_a ) __a = tokenizer.build_inputs_with_special_tokens(_a ) __a = tokenizer.build_inputs_with_special_tokens(_a , _a ) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Dict = BertJapaneseTokenizer __UpperCAmelCase : Any = False def __UpperCAmelCase ( self ): super().setUp() __a = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''こ''', '''ん''', '''に''', '''ち''', '''は''', '''ば''', '''世''', '''界''', '''、''', '''。'''] __a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) def __UpperCAmelCase ( self , **_a ): return BertJapaneseTokenizer.from_pretrained(self.tmpdirname , subword_tokenizer_type='''character''' , **_a ) def __UpperCAmelCase ( self , _a ): __a = '''こんにちは、世界。 \nこんばんは、世界。''' __a = '''こ ん に ち は 、 世 界 。 こ ん ば ん は 、 世 界 。''' return input_text, output_text def __UpperCAmelCase ( self ): pass # TODO add if relevant def __UpperCAmelCase ( self ): pass # TODO add if relevant def __UpperCAmelCase ( self ): pass # TODO add if relevant def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file , subword_tokenizer_type='''character''' ) __a = tokenizer.tokenize('''こんにちは、世界。 \nこんばんは、世界。''' ) self.assertListEqual( _a , ['''こ''', '''ん''', '''に''', '''ち''', '''は''', '''、''', '''世''', '''界''', '''。''', '''こ''', '''ん''', '''ば''', '''ん''', '''は''', '''、''', '''世''', '''界''', '''。'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_a ) , [3, 4, 5, 6, 7, 11, 9, 10, 12, 3, 4, 8, 4, 7, 11, 9, 10, 12] ) def __UpperCAmelCase ( self ): __a = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''こ''', '''ん''', '''に''', '''ち''', '''は''', '''ば''', '''世''', '''界''', '''、''', '''。'''] __a = {} for i, token in enumerate(_a ): __a = i __a = CharacterTokenizer(vocab=_a , unk_token='''[UNK]''' ) self.assertListEqual(tokenizer.tokenize('''''' ) , [] ) self.assertListEqual(tokenizer.tokenize('''こんにちは''' ) , ['''こ''', '''ん''', '''に''', '''ち''', '''は'''] ) self.assertListEqual(tokenizer.tokenize('''こんにちほ''' ) , ['''こ''', '''ん''', '''に''', '''ち''', '''[UNK]'''] ) def __UpperCAmelCase ( self ): __a = self.tokenizer_class.from_pretrained('''cl-tohoku/bert-base-japanese-char''' ) __a = tokenizer.encode('''ありがとう。''' , add_special_tokens=_a ) __a = tokenizer.encode('''どういたしまして。''' , add_special_tokens=_a ) __a = tokenizer.build_inputs_with_special_tokens(_a ) __a = tokenizer.build_inputs_with_special_tokens(_a , _a ) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = '''cl-tohoku/bert-base-japanese''' __a = AutoTokenizer.from_pretrained(_a ) self.assertIsInstance(_a , _a ) class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = '''cl-tohoku/bert-base-japanese''' with self.assertLogs('''transformers''' , level='''WARNING''' ) as cm: BertTokenizer.from_pretrained(_a ) self.assertTrue( cm.records[0].message.startswith( '''The tokenizer class you load from this checkpoint is not the same type as the class this function''' ''' is called from.''' ) ) __a = '''bert-base-cased''' with self.assertLogs('''transformers''' , level='''WARNING''' ) as cm: BertJapaneseTokenizer.from_pretrained(_a ) self.assertTrue( cm.records[0].message.startswith( '''The tokenizer class you load from this checkpoint is not the same type as the class this function''' ''' is called from.''' ) )
45
from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput __snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name class __snake_case ( lowerCamelCase__ , lowerCamelCase__ ): @register_to_config def __init__( self , snake_case__ , snake_case__ = None , snake_case__ = None ) -> str: '''simple docstring''' super().__init__() UpperCAmelCase : Optional[Any] =learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" UpperCAmelCase : Any =torch.zeros(snake_case__ , snake_case__ ) else: UpperCAmelCase : Union[str, Any] =None UpperCAmelCase : Optional[int] =torch.nn.Parameter(snake_case__ ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : VQModel __lowerCamelCase : CLIPTextModel __lowerCamelCase : CLIPTokenizer __lowerCamelCase : TransformeraDModel __lowerCamelCase : LearnedClassifierFreeSamplingEmbeddings __lowerCamelCase : VQDiffusionScheduler def __init__( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> int: '''simple docstring''' super().__init__() self.register_modules( vqvae=snake_case__ , transformer=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , scheduler=snake_case__ , learned_classifier_free_sampling_embeddings=snake_case__ , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : int =len(snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else 1 # get prompt text embeddings UpperCAmelCase : Optional[int] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) UpperCAmelCase : int =text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCAmelCase : List[str] =self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' f''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) UpperCAmelCase : Optional[Any] =text_input_ids[:, : self.tokenizer.model_max_length] UpperCAmelCase : List[Any] =self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 UpperCAmelCase : int =prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate text embeddings for each generation per prompt UpperCAmelCase : int =prompt_embeds.repeat_interleave(snake_case__ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: UpperCAmelCase : Optional[int] =self.learned_classifier_free_sampling_embeddings.embeddings UpperCAmelCase : str =negative_prompt_embeds.unsqueeze(0 ).repeat(snake_case__ , 1 , 1 ) else: UpperCAmelCase : str =[''''''] * batch_size UpperCAmelCase : Tuple =text_input_ids.shape[-1] UpperCAmelCase : Optional[Any] =self.tokenizer( snake_case__ , padding='''max_length''' , max_length=snake_case__ , truncation=snake_case__ , return_tensors='''pt''' , ) UpperCAmelCase : Optional[Any] =self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings UpperCAmelCase : Optional[int] =negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=snake_case__ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCAmelCase : Optional[Any] =negative_prompt_embeds.shape[1] UpperCAmelCase : Union[str, Any] =negative_prompt_embeds.repeat(1 , snake_case__ , 1 ) UpperCAmelCase : Optional[Any] =negative_prompt_embeds.view(batch_size * num_images_per_prompt , snake_case__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCAmelCase : int =torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , snake_case__ , snake_case__ = 100 , snake_case__ = 5.0 , snake_case__ = 1.0 , snake_case__ = 1 , snake_case__ = None , snake_case__ = None , snake_case__ = "pil" , snake_case__ = True , snake_case__ = None , snake_case__ = 1 , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Optional[int] =1 elif isinstance(snake_case__ , snake_case__ ): UpperCAmelCase : Tuple =len(snake_case__ ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(snake_case__ )}''' ) UpperCAmelCase : Tuple =batch_size * num_images_per_prompt UpperCAmelCase : List[str] =guidance_scale > 1.0 UpperCAmelCase : List[Any] =self._encode_prompt(snake_case__ , snake_case__ , snake_case__ ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case__ , snake_case__ ) or callback_steps <= 0) ): raise ValueError( f'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' f''' {type(snake_case__ )}.''' ) # get the initial completely masked latents unless the user supplied it UpperCAmelCase : int =(batch_size, self.transformer.num_latent_pixels) if latents is None: UpperCAmelCase : Union[str, Any] =self.transformer.num_vector_embeds - 1 UpperCAmelCase : str =torch.full(snake_case__ , snake_case__ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( '''Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,''' f''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) UpperCAmelCase : Any =latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(snake_case__ , device=self.device ) UpperCAmelCase : Any =self.scheduler.timesteps.to(self.device ) UpperCAmelCase : Optional[int] =latents for i, t in enumerate(self.progress_bar(snake_case__ ) ): # expand the sample if we are doing classifier free guidance UpperCAmelCase : Optional[Any] =torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` UpperCAmelCase : Optional[int] =self.transformer(snake_case__ , encoder_hidden_states=snake_case__ , timestep=snake_case__ ).sample if do_classifier_free_guidance: UpperCAmelCase , UpperCAmelCase : str =model_output.chunk(2 ) UpperCAmelCase : Optional[int] =model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(snake_case__ , dim=1 , keepdim=snake_case__ ) UpperCAmelCase : Tuple =self.truncate(snake_case__ , snake_case__ ) # remove `log(0)`'s (`-inf`s) UpperCAmelCase : Optional[Any] =model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 UpperCAmelCase : int =self.scheduler.step(snake_case__ , timestep=snake_case__ , sample=snake_case__ , generator=snake_case__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase : Optional[int] =self.vqvae.config.vq_embed_dim UpperCAmelCase : Optional[Any] =(batch_size, self.transformer.height, self.transformer.width, embedding_channels) UpperCAmelCase : Dict =self.vqvae.quantize.get_codebook_entry(snake_case__ , shape=snake_case__ ) UpperCAmelCase : Tuple =self.vqvae.decode(snake_case__ , force_not_quantize=snake_case__ ).sample UpperCAmelCase : Union[str, Any] =(image / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase : Any =image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase : List[str] =self.numpy_to_pil(snake_case__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=snake_case__ ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> torch.FloatTensor: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : int =torch.sort(snake_case__ , 1 , descending=snake_case__ ) UpperCAmelCase : Union[str, Any] =torch.exp(snake_case__ ) UpperCAmelCase : Union[str, Any] =sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out UpperCAmelCase : Optional[Any] =torch.full_like(keep_mask[:, 0:1, :] , snake_case__ ) UpperCAmelCase : Tuple =torch.cat((all_true, keep_mask) , dim=1 ) UpperCAmelCase : int =keep_mask[:, :-1, :] UpperCAmelCase : int =keep_mask.gather(1 , indices.argsort(1 ) ) UpperCAmelCase : Dict =log_p_x_0.clone() UpperCAmelCase : List[Any] =-torch.inf # -inf = log(0) return rv
348
0
"""simple docstring""" import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all image processors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...image_processing_utils import ImageProcessingMixin from ...utils import CONFIG_NAME, IMAGE_PROCESSOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = OrderedDict( [ ("align", "EfficientNetImageProcessor"), ("beit", "BeitImageProcessor"), ("bit", "BitImageProcessor"), ("blip", "BlipImageProcessor"), ("blip-2", "BlipImageProcessor"), ("bridgetower", "BridgeTowerImageProcessor"), ("chinese_clip", "ChineseCLIPImageProcessor"), ("clip", "CLIPImageProcessor"), ("clipseg", "ViTImageProcessor"), ("conditional_detr", "ConditionalDetrImageProcessor"), ("convnext", "ConvNextImageProcessor"), ("convnextv2", "ConvNextImageProcessor"), ("cvt", "ConvNextImageProcessor"), ("data2vec-vision", "BeitImageProcessor"), ("deformable_detr", "DeformableDetrImageProcessor"), ("deit", "DeiTImageProcessor"), ("deta", "DetaImageProcessor"), ("detr", "DetrImageProcessor"), ("dinat", "ViTImageProcessor"), ("donut-swin", "DonutImageProcessor"), ("dpt", "DPTImageProcessor"), ("efficientformer", "EfficientFormerImageProcessor"), ("efficientnet", "EfficientNetImageProcessor"), ("flava", "FlavaImageProcessor"), ("focalnet", "BitImageProcessor"), ("git", "CLIPImageProcessor"), ("glpn", "GLPNImageProcessor"), ("groupvit", "CLIPImageProcessor"), ("imagegpt", "ImageGPTImageProcessor"), ("instructblip", "BlipImageProcessor"), ("layoutlmv2", "LayoutLMv2ImageProcessor"), ("layoutlmv3", "LayoutLMv3ImageProcessor"), ("levit", "LevitImageProcessor"), ("mask2former", "Mask2FormerImageProcessor"), ("maskformer", "MaskFormerImageProcessor"), ("mgp-str", "ViTImageProcessor"), ("mobilenet_v1", "MobileNetV1ImageProcessor"), ("mobilenet_v2", "MobileNetV2ImageProcessor"), ("mobilevit", "MobileViTImageProcessor"), ("mobilevit", "MobileViTImageProcessor"), ("mobilevitv2", "MobileViTImageProcessor"), ("nat", "ViTImageProcessor"), ("oneformer", "OneFormerImageProcessor"), ("owlvit", "OwlViTImageProcessor"), ("perceiver", "PerceiverImageProcessor"), ("pix2struct", "Pix2StructImageProcessor"), ("poolformer", "PoolFormerImageProcessor"), ("regnet", "ConvNextImageProcessor"), ("resnet", "ConvNextImageProcessor"), ("sam", "SamImageProcessor"), ("segformer", "SegformerImageProcessor"), ("swiftformer", "ViTImageProcessor"), ("swin", "ViTImageProcessor"), ("swin2sr", "Swin2SRImageProcessor"), ("swinv2", "ViTImageProcessor"), ("table-transformer", "DetrImageProcessor"), ("timesformer", "VideoMAEImageProcessor"), ("tvlt", "TvltImageProcessor"), ("upernet", "SegformerImageProcessor"), ("van", "ConvNextImageProcessor"), ("videomae", "VideoMAEImageProcessor"), ("vilt", "ViltImageProcessor"), ("vit", "ViTImageProcessor"), ("vit_hybrid", "ViTHybridImageProcessor"), ("vit_mae", "ViTImageProcessor"), ("vit_msn", "ViTImageProcessor"), ("xclip", "CLIPImageProcessor"), ("yolos", "YolosImageProcessor"), ] ) SCREAMING_SNAKE_CASE__ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, IMAGE_PROCESSOR_MAPPING_NAMES) def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : str ): '''simple docstring''' for module_name, extractors in IMAGE_PROCESSOR_MAPPING_NAMES.items(): if class_name in extractors: lowerCAmelCase = model_type_to_module_name(SCREAMING_SNAKE_CASE ) lowerCAmelCase = importlib.import_module(F'.{module_name}' , """transformers.models""" ) try: return getattr(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) except AttributeError: continue for _, extractor in IMAGE_PROCESSOR_MAPPING._extra_content.items(): if getattr(SCREAMING_SNAKE_CASE , """__name__""" , SCREAMING_SNAKE_CASE ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. lowerCAmelCase = importlib.import_module("""transformers""" ) if hasattr(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return getattr(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) return None def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : Union[str, os.PathLike] , SCREAMING_SNAKE_CASE : Optional[Union[str, os.PathLike]] = None , SCREAMING_SNAKE_CASE : bool = False , SCREAMING_SNAKE_CASE : bool = False , SCREAMING_SNAKE_CASE : Optional[Dict[str, str]] = None , SCREAMING_SNAKE_CASE : Optional[Union[bool, str]] = None , SCREAMING_SNAKE_CASE : Optional[str] = None , SCREAMING_SNAKE_CASE : bool = False , **SCREAMING_SNAKE_CASE : List[str] , ): '''simple docstring''' lowerCAmelCase = get_file_from_repo( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , cache_dir=SCREAMING_SNAKE_CASE , force_download=SCREAMING_SNAKE_CASE , resume_download=SCREAMING_SNAKE_CASE , proxies=SCREAMING_SNAKE_CASE , use_auth_token=SCREAMING_SNAKE_CASE , revision=SCREAMING_SNAKE_CASE , local_files_only=SCREAMING_SNAKE_CASE , ) if resolved_config_file is None: logger.info( """Could not locate the image processor configuration file, will try to use the model config instead.""" ) return {} with open(SCREAMING_SNAKE_CASE , encoding="""utf-8""" ) as reader: return json.load(SCREAMING_SNAKE_CASE ) class lowercase : def __init__( self ) -> str: raise EnvironmentError( """AutoImageProcessor is designed to be instantiated """ """using the `AutoImageProcessor.from_pretrained(pretrained_model_name_or_path)` method.""" ) @classmethod @replace_list_option_in_docstrings(lowercase ) def _snake_case ( cls , lowercase , **lowercase ) -> Union[str, Any]: lowerCAmelCase = kwargs.pop("""config""" , lowercase ) lowerCAmelCase = kwargs.pop("""trust_remote_code""" , lowercase ) lowerCAmelCase = True lowerCAmelCase , lowerCAmelCase = ImageProcessingMixin.get_image_processor_dict(lowercase , **lowercase ) lowerCAmelCase = config_dict.get("""image_processor_type""" , lowercase ) lowerCAmelCase = None if "AutoImageProcessor" in config_dict.get("""auto_map""" , {} ): lowerCAmelCase = config_dict["""auto_map"""]["""AutoImageProcessor"""] # If we still don't have the image processor class, check if we're loading from a previous feature extractor config # and if so, infer the image processor class from there. if image_processor_class is None and image_processor_auto_map is None: lowerCAmelCase = config_dict.pop("""feature_extractor_type""" , lowercase ) if feature_extractor_class is not None: logger.warning( """Could not find image processor class in the image processor config or the model config. Loading""" """ based on pattern matching with the model's feature extractor configuration.""" ) lowerCAmelCase = feature_extractor_class.replace("""FeatureExtractor""" , """ImageProcessor""" ) if "AutoFeatureExtractor" in config_dict.get("""auto_map""" , {} ): lowerCAmelCase = config_dict["""auto_map"""]["""AutoFeatureExtractor"""] lowerCAmelCase = feature_extractor_auto_map.replace("""FeatureExtractor""" , """ImageProcessor""" ) logger.warning( """Could not find image processor auto map in the image processor config or the model config.""" """ Loading based on pattern matching with the model's feature extractor configuration.""" ) # If we don't find the image processor class in the image processor config, let's try the model config. if image_processor_class is None and image_processor_auto_map is None: if not isinstance(lowercase , lowercase ): lowerCAmelCase = AutoConfig.from_pretrained(lowercase , **lowercase ) # It could be in `config.image_processor_type`` lowerCAmelCase = getattr(lowercase , """image_processor_type""" , lowercase ) if hasattr(lowercase , """auto_map""" ) and "AutoImageProcessor" in config.auto_map: lowerCAmelCase = config.auto_map["""AutoImageProcessor"""] if image_processor_class is not None: lowerCAmelCase = image_processor_class_from_name(lowercase ) lowerCAmelCase = image_processor_auto_map is not None lowerCAmelCase = image_processor_class is not None or type(lowercase ) in IMAGE_PROCESSOR_MAPPING lowerCAmelCase = resolve_trust_remote_code( lowercase , lowercase , lowercase , lowercase ) if has_remote_code and trust_remote_code: lowerCAmelCase = get_class_from_dynamic_module( lowercase , lowercase , **lowercase ) lowerCAmelCase = kwargs.pop("""code_revision""" , lowercase ) if os.path.isdir(lowercase ): image_processor_class.register_for_auto_class() return image_processor_class.from_dict(lowercase , **lowercase ) elif image_processor_class is not None: return image_processor_class.from_dict(lowercase , **lowercase ) # Last try: we use the IMAGE_PROCESSOR_MAPPING. elif type(lowercase ) in IMAGE_PROCESSOR_MAPPING: lowerCAmelCase = IMAGE_PROCESSOR_MAPPING[type(lowercase )] return image_processor_class.from_dict(lowercase , **lowercase ) raise ValueError( f'Unrecognized image processor in {pretrained_model_name_or_path}. Should have a ' f'`image_processor_type` key in its {IMAGE_PROCESSOR_NAME} of {CONFIG_NAME}, or one of the following ' f'`model_type` keys in its {CONFIG_NAME}: {", ".join(c for c in IMAGE_PROCESSOR_MAPPING_NAMES.keys() )}' ) @staticmethod def _snake_case ( lowercase , lowercase ) -> Optional[Any]: IMAGE_PROCESSOR_MAPPING.register(lowercase , lowercase )
46
import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class __snake_case ( unittest.TestCase ): @property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase : Any =UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =self.dummy_uncond_unet UpperCAmelCase : Optional[int] =KarrasVeScheduler() UpperCAmelCase : List[Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : List[str] =torch.manual_seed(0 ) UpperCAmelCase : List[str] =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : str =torch.manual_seed(0 ) UpperCAmelCase : str =pipe(num_inference_steps=2 , generator=snake_case__ , output_type='''numpy''' , return_dict=snake_case__ )[0] UpperCAmelCase : Any =image[0, -3:, -3:, -1] UpperCAmelCase : List[str] =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCAmelCase : int =np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class __snake_case ( unittest.TestCase ): def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Tuple ='''google/ncsnpp-celebahq-256''' UpperCAmelCase : int =UNetaDModel.from_pretrained(snake_case__ ) UpperCAmelCase : Dict =KarrasVeScheduler() UpperCAmelCase : Union[str, Any] =KarrasVePipeline(unet=snake_case__ , scheduler=snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase : Any =torch.manual_seed(0 ) UpperCAmelCase : Tuple =pipe(num_inference_steps=20 , generator=snake_case__ , output_type='''numpy''' ).images UpperCAmelCase : Optional[int] =image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) UpperCAmelCase : Tuple =np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
348
0
'''simple docstring''' import unittest from transformers import LiltConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( LiltForQuestionAnswering, LiltForSequenceClassification, LiltForTokenClassification, LiltModel, ) from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST class A__ : def __init__( self : str , _a : Any , _a : List[str]=13 , _a : Union[str, Any]=7 , _a : List[Any]=True , _a : Optional[int]=True , _a : Tuple=True , _a : Optional[Any]=True , _a : Optional[int]=99 , _a : str=24 , _a : str=2 , _a : Union[str, Any]=6 , _a : Union[str, Any]=37 , _a : Any="gelu" , _a : Any=0.1 , _a : List[Any]=0.1 , _a : List[str]=512 , _a : Tuple=16 , _a : List[str]=2 , _a : int=0.02 , _a : Optional[int]=3 , _a : Any=None , _a : int=1000 , ) -> Optional[int]: '''simple docstring''' _SCREAMING_SNAKE_CASE =parent _SCREAMING_SNAKE_CASE =batch_size _SCREAMING_SNAKE_CASE =seq_length _SCREAMING_SNAKE_CASE =is_training _SCREAMING_SNAKE_CASE =use_input_mask _SCREAMING_SNAKE_CASE =use_token_type_ids _SCREAMING_SNAKE_CASE =use_labels _SCREAMING_SNAKE_CASE =vocab_size _SCREAMING_SNAKE_CASE =hidden_size _SCREAMING_SNAKE_CASE =num_hidden_layers _SCREAMING_SNAKE_CASE =num_attention_heads _SCREAMING_SNAKE_CASE =intermediate_size _SCREAMING_SNAKE_CASE =hidden_act _SCREAMING_SNAKE_CASE =hidden_dropout_prob _SCREAMING_SNAKE_CASE =attention_probs_dropout_prob _SCREAMING_SNAKE_CASE =max_position_embeddings _SCREAMING_SNAKE_CASE =type_vocab_size _SCREAMING_SNAKE_CASE =type_sequence_label_size _SCREAMING_SNAKE_CASE =initializer_range _SCREAMING_SNAKE_CASE =num_labels _SCREAMING_SNAKE_CASE =scope _SCREAMING_SNAKE_CASE =range_bbox def A ( self : int ) -> Dict: '''simple docstring''' _SCREAMING_SNAKE_CASE =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _SCREAMING_SNAKE_CASE =ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox ) # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: _SCREAMING_SNAKE_CASE =bbox[i, j, 3] _SCREAMING_SNAKE_CASE =bbox[i, j, 1] _SCREAMING_SNAKE_CASE =t if bbox[i, j, 2] < bbox[i, j, 0]: _SCREAMING_SNAKE_CASE =bbox[i, j, 2] _SCREAMING_SNAKE_CASE =bbox[i, j, 0] _SCREAMING_SNAKE_CASE =t _SCREAMING_SNAKE_CASE =None if self.use_input_mask: _SCREAMING_SNAKE_CASE =ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) _SCREAMING_SNAKE_CASE =None if self.use_token_type_ids: _SCREAMING_SNAKE_CASE =ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =None if self.use_labels: _SCREAMING_SNAKE_CASE =ids_tensor([self.batch_size] , self.type_sequence_label_size ) _SCREAMING_SNAKE_CASE =ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _SCREAMING_SNAKE_CASE =self.get_config() return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels def A ( self : Optional[int] ) -> List[Any]: '''simple docstring''' return LiltConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) def A ( self : int , _a : Any , _a : str , _a : Union[str, Any] , _a : Any , _a : Tuple , _a : List[Any] , _a : Dict , ) -> Optional[Any]: '''simple docstring''' _SCREAMING_SNAKE_CASE =LiltModel(config=_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a , bbox=_a , attention_mask=_a , token_type_ids=_a ) _SCREAMING_SNAKE_CASE =model(_a , bbox=_a , token_type_ids=_a ) _SCREAMING_SNAKE_CASE =model(_a , bbox=_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def A ( self : Optional[int] , _a : Tuple , _a : List[str] , _a : Optional[Any] , _a : Any , _a : Tuple , _a : Any , _a : Optional[int] , ) -> Union[str, Any]: '''simple docstring''' _SCREAMING_SNAKE_CASE =self.num_labels _SCREAMING_SNAKE_CASE =LiltForTokenClassification(config=_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model( _a , bbox=_a , attention_mask=_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def A ( self : Tuple , _a : str , _a : List[str] , _a : Optional[Any] , _a : Union[str, Any] , _a : Dict , _a : List[Any] , _a : str , ) -> List[Any]: '''simple docstring''' _SCREAMING_SNAKE_CASE =LiltForQuestionAnswering(config=_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model( _a , bbox=_a , attention_mask=_a , token_type_ids=_a , start_positions=_a , end_positions=_a , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def A ( self : Dict ) -> List[str]: '''simple docstring''' _SCREAMING_SNAKE_CASE =self.prepare_config_and_inputs() ( ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ) =config_and_inputs _SCREAMING_SNAKE_CASE ={ 'input_ids': input_ids, 'bbox': bbox, 'token_type_ids': token_type_ids, 'attention_mask': input_mask, } return config, inputs_dict @require_torch class A__ ( A__ , A__ , A__ , unittest.TestCase ): A__ = ( ( LiltModel, LiltForSequenceClassification, LiltForTokenClassification, LiltForQuestionAnswering, ) if is_torch_available() else () ) A__ = ( { 'feature-extraction': LiltModel, 'question-answering': LiltForQuestionAnswering, 'text-classification': LiltForSequenceClassification, 'token-classification': LiltForTokenClassification, 'zero-shot': LiltForSequenceClassification, } if is_torch_available() else {} ) A__ = False A__ = False def A ( self : Tuple , _a : Union[str, Any] , _a : int , _a : List[Any] , _a : Optional[int] , _a : int ) -> Optional[Any]: '''simple docstring''' return True def A ( self : List[str] ) -> Union[str, Any]: '''simple docstring''' _SCREAMING_SNAKE_CASE =LiltModelTester(self ) _SCREAMING_SNAKE_CASE =ConfigTester(self , config_class=_a , hidden_size=37 ) def A ( self : Dict ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def A ( self : Union[str, Any] ) -> List[str]: '''simple docstring''' _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def A ( self : Optional[int] ) -> List[Any]: '''simple docstring''' _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _SCREAMING_SNAKE_CASE =type self.model_tester.create_and_check_model(*_a ) def A ( self : int ) -> Union[str, Any]: '''simple docstring''' _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_a ) def A ( self : List[Any] ) -> int: '''simple docstring''' _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_a ) @slow def A ( self : Optional[int] ) -> Dict: '''simple docstring''' for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _SCREAMING_SNAKE_CASE =LiltModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @require_torch @slow class A__ ( unittest.TestCase ): def A ( self : Dict ) -> str: '''simple docstring''' _SCREAMING_SNAKE_CASE =LiltModel.from_pretrained('SCUT-DLVCLab/lilt-roberta-en-base' ).to(_a ) _SCREAMING_SNAKE_CASE =torch.tensor([[1, 2]] , device=_a ) _SCREAMING_SNAKE_CASE =torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]] , device=_a ) # forward pass with torch.no_grad(): _SCREAMING_SNAKE_CASE =model(input_ids=_a , bbox=_a ) _SCREAMING_SNAKE_CASE =torch.Size([1, 2, 768] ) _SCREAMING_SNAKE_CASE =torch.tensor( [[-0.06_53, 0.09_50, -0.00_61], [-0.05_45, 0.09_26, -0.03_24]] , device=_a , ) self.assertTrue(outputs.last_hidden_state.shape , _a ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :, :3] , _a , atol=1e-3 ) )
47
import qiskit def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> qiskit.result.counts.Counts: '''simple docstring''' UpperCAmelCase : Union[str, Any] =qiskit.Aer.get_backend('''aer_simulator''' ) UpperCAmelCase : List[str] =qiskit.QuantumCircuit(4 , 2 ) # encode inputs in qubits 0 and 1 if bita == 1: qc_ha.x(0 ) if bita == 1: qc_ha.x(1 ) qc_ha.barrier() # use cnots to write XOR of the inputs on qubit2 qc_ha.cx(0 , 2 ) qc_ha.cx(1 , 2 ) # use ccx / toffoli gate to write AND of the inputs on qubit3 qc_ha.ccx(0 , 1 , 3 ) qc_ha.barrier() # extract outputs qc_ha.measure(2 , 0 ) # extract XOR value qc_ha.measure(3 , 1 ) # extract AND value # Execute the circuit on the qasm simulator UpperCAmelCase : Dict =qiskit.execute(__lowerCAmelCase , __lowerCAmelCase , shots=10_00 ) # Return the histogram data of the results of the experiment return job.result().get_counts(__lowerCAmelCase ) if __name__ == "__main__": __snake_case = half_adder(1, 1) print(f'Half Adder Output Qubit Counts: {counts}')
348
0
from collections import OrderedDict from typing import Any, List, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import logging SCREAMING_SNAKE_CASE__ : Any = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ : Union[str, Any] = { 'Salesforce/codegen-350M-nl': 'https://huggingface.co/Salesforce/codegen-350M-nl/resolve/main/config.json', 'Salesforce/codegen-350M-multi': 'https://huggingface.co/Salesforce/codegen-350M-multi/resolve/main/config.json', 'Salesforce/codegen-350M-mono': 'https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/config.json', 'Salesforce/codegen-2B-nl': 'https://huggingface.co/Salesforce/codegen-2B-nl/resolve/main/config.json', 'Salesforce/codegen-2B-multi': 'https://huggingface.co/Salesforce/codegen-2B-multi/resolve/main/config.json', 'Salesforce/codegen-2B-mono': 'https://huggingface.co/Salesforce/codegen-2B-mono/resolve/main/config.json', 'Salesforce/codegen-6B-nl': 'https://huggingface.co/Salesforce/codegen-6B-nl/resolve/main/config.json', 'Salesforce/codegen-6B-multi': 'https://huggingface.co/Salesforce/codegen-6B-multi/resolve/main/config.json', 'Salesforce/codegen-6B-mono': 'https://huggingface.co/Salesforce/codegen-6B-mono/resolve/main/config.json', 'Salesforce/codegen-16B-nl': 'https://huggingface.co/Salesforce/codegen-16B-nl/resolve/main/config.json', 'Salesforce/codegen-16B-multi': 'https://huggingface.co/Salesforce/codegen-16B-multi/resolve/main/config.json', 'Salesforce/codegen-16B-mono': 'https://huggingface.co/Salesforce/codegen-16B-mono/resolve/main/config.json', } class UpperCamelCase__ (lowerCAmelCase__ ): '''simple docstring''' lowerCamelCase_ : Tuple = """codegen""" lowerCamelCase_ : Optional[int] = { """max_position_embeddings""": """n_positions""", """hidden_size""": """n_embd""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self , UpperCamelCase__=5_0400 , UpperCamelCase__=2048 , UpperCamelCase__=2048 , UpperCamelCase__=4096 , UpperCamelCase__=28 , UpperCamelCase__=16 , UpperCamelCase__=64 , UpperCamelCase__=None , UpperCamelCase__="gelu_new" , UpperCamelCase__=0.0 , UpperCamelCase__=0.0 , UpperCamelCase__=0.0 , UpperCamelCase__=1e-5 , UpperCamelCase__=0.02 , UpperCamelCase__=True , UpperCamelCase__=5_0256 , UpperCamelCase__=5_0256 , UpperCamelCase__=False , **UpperCamelCase__ , ) -> List[str]: lowerCamelCase : Union[str, Any] = vocab_size lowerCamelCase : Optional[Any] = n_ctx lowerCamelCase : Optional[int] = n_positions lowerCamelCase : int = n_embd lowerCamelCase : Optional[Any] = n_layer lowerCamelCase : List[Any] = n_head lowerCamelCase : Optional[int] = n_inner lowerCamelCase : Optional[int] = rotary_dim lowerCamelCase : int = activation_function lowerCamelCase : List[str] = resid_pdrop lowerCamelCase : Optional[int] = embd_pdrop lowerCamelCase : Tuple = attn_pdrop lowerCamelCase : int = layer_norm_epsilon lowerCamelCase : Optional[Any] = initializer_range lowerCamelCase : str = use_cache lowerCamelCase : List[Any] = bos_token_id lowerCamelCase : Tuple = eos_token_id super().__init__( bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , tie_word_embeddings=UpperCamelCase__ , **UpperCamelCase__ ) class UpperCamelCase__ (lowerCAmelCase__ ): '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__ = "default" , UpperCamelCase__ = None , UpperCamelCase__ = False , ) -> List[Any]: super().__init__(UpperCamelCase__ , task=UpperCamelCase__ , patching_specs=UpperCamelCase__ , use_past=UpperCamelCase__ ) if not getattr(self._config , "pad_token_id" , UpperCamelCase__ ): # TODO: how to do that better? lowerCamelCase : Union[str, Any] = 0 @property def _lowercase ( self ) -> Mapping[str, Mapping[int, str]]: lowerCamelCase : List[Any] = OrderedDict({"input_ids": {0: "batch", 1: "sequence"}} ) if self.use_past: self.fill_with_past_key_values_(UpperCamelCase__ , direction="inputs" ) lowerCamelCase : Optional[Any] = {0: "batch", 1: "past_sequence + sequence"} else: lowerCamelCase : Dict = {0: "batch", 1: "sequence"} return common_inputs @property def _lowercase ( self ) -> int: return self._config.n_layer @property def _lowercase ( self ) -> int: return self._config.n_head def _lowercase ( self , UpperCamelCase__ , UpperCamelCase__ = -1 , UpperCamelCase__ = -1 , UpperCamelCase__ = False , UpperCamelCase__ = None , ) -> Mapping[str, Any]: lowerCamelCase : int = super(UpperCamelCase__ , self ).generate_dummy_inputs( UpperCamelCase__ , batch_size=UpperCamelCase__ , seq_length=UpperCamelCase__ , is_pair=UpperCamelCase__ , framework=UpperCamelCase__ ) # We need to order the input in the way they appears in the forward() lowerCamelCase : int = OrderedDict({"input_ids": common_inputs["input_ids"]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch lowerCamelCase , lowerCamelCase : Optional[int] = common_inputs["input_ids"].shape # Not using the same length for past_key_values lowerCamelCase : List[Any] = seqlen + 2 lowerCamelCase : Any = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) lowerCamelCase : Union[str, Any] = [ (torch.zeros(UpperCamelCase__ ), torch.zeros(UpperCamelCase__ )) for _ in range(self.num_layers ) ] lowerCamelCase : str = common_inputs["attention_mask"] if self.use_past: lowerCamelCase : Optional[Any] = ordered_inputs["attention_mask"].dtype lowerCamelCase : Union[str, Any] = torch.cat( [ordered_inputs["attention_mask"], torch.ones(UpperCamelCase__ , UpperCamelCase__ , dtype=UpperCamelCase__ )] , dim=1 ) return ordered_inputs @property def _lowercase ( self ) -> int: return 13
48
from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class __snake_case : __lowerCamelCase : str = BlenderbotConfig __lowerCamelCase : Optional[Any] = {} __lowerCamelCase : Optional[int] = """gelu""" def __init__( self , snake_case__ , snake_case__=13 , snake_case__=7 , snake_case__=True , snake_case__=False , snake_case__=99 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=20 , snake_case__=2 , snake_case__=1 , snake_case__=0 , ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : Dict =seq_length UpperCAmelCase : Optional[Any] =is_training UpperCAmelCase : List[str] =use_labels UpperCAmelCase : List[Any] =vocab_size UpperCAmelCase : Optional[int] =hidden_size UpperCAmelCase : Tuple =num_hidden_layers UpperCAmelCase : Any =num_attention_heads UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : str =hidden_dropout_prob UpperCAmelCase : Optional[int] =attention_probs_dropout_prob UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : List[Any] =eos_token_id UpperCAmelCase : Optional[int] =pad_token_id UpperCAmelCase : Tuple =bos_token_id def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[Any] =ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) UpperCAmelCase : List[Any] =tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) UpperCAmelCase : Tuple =tf.concat([input_ids, eos_tensor] , axis=1 ) UpperCAmelCase : str =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : Optional[Any] =self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) UpperCAmelCase : List[str] =prepare_blenderbot_inputs_dict(snake_case__ , snake_case__ , snake_case__ ) return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =TFBlenderbotModel(config=snake_case__ ).get_decoder() UpperCAmelCase : Any =inputs_dict['''input_ids'''] UpperCAmelCase : str =input_ids[:1, :] UpperCAmelCase : Tuple =inputs_dict['''attention_mask'''][:1, :] UpperCAmelCase : Tuple =inputs_dict['''head_mask'''] UpperCAmelCase : List[Any] =1 # first forward pass UpperCAmelCase : List[str] =model(snake_case__ , attention_mask=snake_case__ , head_mask=snake_case__ , use_cache=snake_case__ ) UpperCAmelCase , UpperCAmelCase : str =outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase : Union[str, Any] =ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCAmelCase : List[Any] =tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and UpperCAmelCase : Tuple =tf.concat([input_ids, next_tokens] , axis=-1 ) UpperCAmelCase : int =tf.concat([attention_mask, next_attn_mask] , axis=-1 ) UpperCAmelCase : Optional[int] =model(snake_case__ , attention_mask=snake_case__ )[0] UpperCAmelCase : str =model(snake_case__ , attention_mask=snake_case__ , past_key_values=snake_case__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice UpperCAmelCase : List[Any] =int(ids_tensor((1,) , output_from_past.shape[-1] ) ) UpperCAmelCase : List[Any] =output_from_no_past[:, -3:, random_slice_idx] UpperCAmelCase : Dict =output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(snake_case__ , snake_case__ , rtol=1e-3 ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , )-> str: '''simple docstring''' if attention_mask is None: UpperCAmelCase : int =tf.cast(tf.math.not_equal(__lowerCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase : Tuple =tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase : str =tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase : Union[str, Any] =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase : int =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : List[str] = (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __lowerCamelCase : Dict = (TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __lowerCamelCase : Dict = ( { """conversational""": TFBlenderbotForConditionalGeneration, """feature-extraction""": TFBlenderbotModel, """summarization""": TFBlenderbotForConditionalGeneration, """text2text-generation""": TFBlenderbotForConditionalGeneration, """translation""": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __lowerCamelCase : Union[str, Any] = True __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Union[str, Any] = False def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : List[str] =TFBlenderbotModelTester(self ) UpperCAmelCase : List[Any] =ConfigTester(self , config_class=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*snake_case__ ) @require_tokenizers @require_tf class __snake_case ( unittest.TestCase ): __lowerCamelCase : List[str] = ["""My friends are cool but they eat too many carbs."""] __lowerCamelCase : Dict = """facebook/blenderbot-400M-distill""" @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[int] =self.tokenizer(self.src_text , return_tensors='''tf''' ) UpperCAmelCase : Optional[int] =self.model.generate( model_inputs.input_ids , ) UpperCAmelCase : str =self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=snake_case__ )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
348
0
import argparse import glob import importlib.util import os import re import black from doc_builder.style_doc import style_docstrings_in_code # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py __snake_case :Optional[int] = '''src/diffusers''' __snake_case :Any = '''.''' # This is to make sure the diffusers module imported is the one in the repo. __snake_case :List[Any] = importlib.util.spec_from_file_location( '''diffusers''', os.path.join(DIFFUSERS_PATH, '''__init__.py'''), submodule_search_locations=[DIFFUSERS_PATH], ) __snake_case :int = spec.loader.load_module() def __snake_case ( _UpperCAmelCase , _UpperCAmelCase ): return line.startswith(_UpperCAmelCase ) or len(_UpperCAmelCase ) <= 1 or re.search(R'''^\s*\)(\s*->.*:|:)\s*$''' , _UpperCAmelCase ) is not None def __snake_case ( _UpperCAmelCase ): __a = object_name.split('''.''' ) __a = 0 # First let's find the module where our object lives. __a = parts[i] while i < len(_UpperCAmelCase ) and not os.path.isfile(os.path.join(_UpperCAmelCase , f'{module}.py' ) ): i += 1 if i < len(_UpperCAmelCase ): __a = os.path.join(_UpperCAmelCase , parts[i] ) if i >= len(_UpperCAmelCase ): raise ValueError(f'`object_name` should begin with the name of a module of diffusers but got {object_name}.' ) with open(os.path.join(_UpperCAmelCase , f'{module}.py' ) , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: __a = f.readlines() # Now let's find the class / func in the code! __a = '''''' __a = 0 for name in parts[i + 1 :]: while ( line_index < len(_UpperCAmelCase ) and re.search(Rf'^{indent}(class|def)\s+{name}(\(|\:)' , lines[line_index] ) is None ): line_index += 1 indent += " " line_index += 1 if line_index >= len(_UpperCAmelCase ): raise ValueError(f' {object_name} does not match any function or class in {module}.' ) # We found the beginning of the class / func, now let's find the end (when the indent diminishes). __a = line_index while line_index < len(_UpperCAmelCase ) and _should_continue(lines[line_index] , _UpperCAmelCase ): line_index += 1 # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 __a = lines[start_index:line_index] return "".join(_UpperCAmelCase ) __snake_case :List[Any] = re.compile(r'''^(\s*)#\s*Copied from\s+diffusers\.(\S+\.\S+)\s*($|\S.*$)''') __snake_case :Tuple = re.compile(r'''^\s*(\S+)->(\S+)(\s+.*|$)''') __snake_case :Union[str, Any] = re.compile(r'''<FILL\s+[^>]*>''') def __snake_case ( _UpperCAmelCase ): __a = code.split('''\n''' ) __a = 0 while idx < len(_UpperCAmelCase ) and len(lines[idx] ) == 0: idx += 1 if idx < len(_UpperCAmelCase ): return re.search(R'''^(\s*)\S''' , lines[idx] ).groups()[0] return "" def __snake_case ( _UpperCAmelCase ): __a = len(get_indent(_UpperCAmelCase ) ) > 0 if has_indent: __a = f'class Bla:\n{code}' __a = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 , preview=_UpperCAmelCase ) __a = black.format_str(_UpperCAmelCase , mode=_UpperCAmelCase ) __a , __a = style_docstrings_in_code(_UpperCAmelCase ) return result[len('''class Bla:\n''' ) :] if has_indent else result def __snake_case ( _UpperCAmelCase , _UpperCAmelCase=False ): with open(_UpperCAmelCase , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: __a = f.readlines() __a = [] __a = 0 # Not a for loop cause `lines` is going to change (if `overwrite=True`). while line_index < len(_UpperCAmelCase ): __a = _re_copy_warning.search(lines[line_index] ) if search is None: line_index += 1 continue # There is some copied code here, let's retrieve the original. __a , __a , __a = search.groups() __a = find_code_in_diffusers(_UpperCAmelCase ) __a = get_indent(_UpperCAmelCase ) __a = line_index + 1 if indent == theoretical_indent else line_index + 2 __a = theoretical_indent __a = start_index # Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment. __a = True while line_index < len(_UpperCAmelCase ) and should_continue: line_index += 1 if line_index >= len(_UpperCAmelCase ): break __a = lines[line_index] __a = _should_continue(_UpperCAmelCase , _UpperCAmelCase ) and re.search(f'^{indent}# End copy' , _UpperCAmelCase ) is None # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 __a = lines[start_index:line_index] __a = ''''''.join(_UpperCAmelCase ) # Remove any nested `Copied from` comments to avoid circular copies __a = [line for line in theoretical_code.split('''\n''' ) if _re_copy_warning.search(_UpperCAmelCase ) is None] __a = '''\n'''.join(_UpperCAmelCase ) # Before comparing, use the `replace_pattern` on the original code. if len(_UpperCAmelCase ) > 0: __a = replace_pattern.replace('''with''' , '''''' ).split(''',''' ) __a = [_re_replace_pattern.search(_UpperCAmelCase ) for p in patterns] for pattern in patterns: if pattern is None: continue __a , __a , __a = pattern.groups() __a = re.sub(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) if option.strip() == "all-casing": __a = re.sub(obja.lower() , obja.lower() , _UpperCAmelCase ) __a = re.sub(obja.upper() , obja.upper() , _UpperCAmelCase ) # Blackify after replacement. To be able to do that, we need the header (class or function definition) # from the previous line __a = blackify(lines[start_index - 1] + theoretical_code ) __a = theoretical_code[len(lines[start_index - 1] ) :] # Test for a diff and act accordingly. if observed_code != theoretical_code: diffs.append([object_name, start_index] ) if overwrite: __a = lines[:start_index] + [theoretical_code] + lines[line_index:] __a = start_index + 1 if overwrite and len(_UpperCAmelCase ) > 0: # Warn the user a file has been modified. print(f'Detected changes, rewriting {filename}.' ) with open(_UpperCAmelCase , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.writelines(_UpperCAmelCase ) return diffs def __snake_case ( _UpperCAmelCase = False ): __a = glob.glob(os.path.join(_UpperCAmelCase , '''**/*.py''' ) , recursive=_UpperCAmelCase ) __a = [] for filename in all_files: __a = is_copy_consistent(_UpperCAmelCase , _UpperCAmelCase ) diffs += [f'- {filename}: copy does not match {d[0]} at line {d[1]}' for d in new_diffs] if not overwrite and len(_UpperCAmelCase ) > 0: __a = '''\n'''.join(_UpperCAmelCase ) raise Exception( '''Found the following copy inconsistencies:\n''' + diff + '''\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them.''' ) if __name__ == "__main__": __snake_case :List[str] = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') __snake_case :int = parser.parse_args() check_copies(args.fix_and_overwrite)
49
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { '''asapp/sew-d-tiny-100k''': '''https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json''', # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Optional[Any] = """sew-d""" def __init__( self , snake_case__=32 , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3072 , snake_case__=2 , snake_case__=512 , snake_case__=256 , snake_case__=True , snake_case__=True , snake_case__=("p2c", "c2p") , snake_case__="layer_norm" , snake_case__="gelu_python" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.0 , snake_case__=0.1 , snake_case__=0.02 , snake_case__=1e-7 , snake_case__=1e-5 , snake_case__="group" , snake_case__="gelu" , snake_case__=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , snake_case__=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , snake_case__=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , snake_case__=False , snake_case__=128 , snake_case__=16 , snake_case__=True , snake_case__=0.05 , snake_case__=10 , snake_case__=2 , snake_case__=0.0 , snake_case__=10 , snake_case__=0 , snake_case__="mean" , snake_case__=False , snake_case__=False , snake_case__=256 , snake_case__=0 , snake_case__=1 , snake_case__=2 , **snake_case__ , ) -> int: '''simple docstring''' super().__init__(**snake_case__ , pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ ) UpperCAmelCase : Union[str, Any] =hidden_size UpperCAmelCase : Union[str, Any] =feat_extract_norm UpperCAmelCase : Optional[Any] =feat_extract_activation UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : int =list(snake_case__ ) UpperCAmelCase : List[str] =list(snake_case__ ) UpperCAmelCase : str =conv_bias UpperCAmelCase : Tuple =num_conv_pos_embeddings UpperCAmelCase : Dict =num_conv_pos_embedding_groups UpperCAmelCase : str =len(self.conv_dim ) UpperCAmelCase : Dict =num_hidden_layers UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : List[Any] =squeeze_factor UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : int =position_buckets UpperCAmelCase : Optional[int] =share_att_key UpperCAmelCase : Optional[int] =relative_attention UpperCAmelCase : Tuple =norm_rel_ebd UpperCAmelCase : List[Any] =list(snake_case__ ) UpperCAmelCase : Dict =hidden_act UpperCAmelCase : Optional[int] =num_attention_heads UpperCAmelCase : Any =hidden_dropout UpperCAmelCase : str =attention_dropout UpperCAmelCase : Union[str, Any] =activation_dropout UpperCAmelCase : str =feat_proj_dropout UpperCAmelCase : Union[str, Any] =final_dropout UpperCAmelCase : Optional[int] =layer_norm_eps UpperCAmelCase : str =feature_layer_norm_eps UpperCAmelCase : str =initializer_range UpperCAmelCase : Any =vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect.''' '''It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,''' f'''but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)''' f'''= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCAmelCase : Union[str, Any] =apply_spec_augment UpperCAmelCase : Optional[Any] =mask_time_prob UpperCAmelCase : Tuple =mask_time_length UpperCAmelCase : str =mask_time_min_masks UpperCAmelCase : Optional[int] =mask_feature_prob UpperCAmelCase : Optional[Any] =mask_feature_length UpperCAmelCase : List[Any] =mask_feature_min_masks # ctc loss UpperCAmelCase : str =ctc_loss_reduction UpperCAmelCase : Optional[int] =ctc_zero_infinity # sequence classification UpperCAmelCase : Union[str, Any] =use_weighted_layer_sum UpperCAmelCase : int =classifier_proj_size @property def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
348
0
from unittest.mock import Mock, patch from file_transfer.send_file import send_file @patch('socket.socket' ) @patch('builtins.open' ) def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase ) -> Tuple: # ===== initialization ===== lowerCamelCase__ : Union[str, Any] = Mock() lowerCamelCase__ : Any = conn, Mock() lowerCamelCase__ : int = iter([1, None] ) lowerCamelCase__ : Optional[int] = lambda _UpperCAmelCase : next(_UpperCAmelCase ) # ===== invoke ===== send_file(filename='mytext.txt' , testing=_UpperCAmelCase ) # ===== ensurance ===== sock.assert_called_once() sock.return_value.bind.assert_called_once() sock.return_value.listen.assert_called_once() sock.return_value.accept.assert_called_once() conn.recv.assert_called_once() file.return_value.__enter__.assert_called_once() file.return_value.__enter__.return_value.read.assert_called() conn.send.assert_called_once() conn.close.assert_called_once() sock.return_value.shutdown.assert_called_once() sock.return_value.close.assert_called_once()
50
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node __snake_case = 4 __snake_case = 3 class __snake_case ( lowerCamelCase__ ): pass def lowerCAmelCase_ ( __lowerCAmelCase )-> List[str]: '''simple docstring''' for shard in shards: for i in range(__lowerCAmelCase ): yield {"i": i, "shard": shard} def lowerCAmelCase_ ( )-> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =int(os.environ['''RANK'''] ) UpperCAmelCase : Optional[Any] =int(os.environ['''WORLD_SIZE'''] ) UpperCAmelCase : List[Any] =ArgumentParser() parser.add_argument('''--streaming''' , type=__lowerCAmelCase ) parser.add_argument('''--local_rank''' , type=__lowerCAmelCase ) parser.add_argument('''--num_workers''' , type=__lowerCAmelCase , default=0 ) UpperCAmelCase : Any =parser.parse_args() UpperCAmelCase : List[str] =args.streaming UpperCAmelCase : Tuple =args.num_workers UpperCAmelCase : int ={'''shards''': [f'''shard_{shard_idx}''' for shard_idx in range(__lowerCAmelCase )]} UpperCAmelCase : Optional[int] =IterableDataset.from_generator(__lowerCAmelCase , gen_kwargs=__lowerCAmelCase ) if not streaming: UpperCAmelCase : List[Any] =Dataset.from_list(list(__lowerCAmelCase ) ) UpperCAmelCase : Dict =split_dataset_by_node(__lowerCAmelCase , rank=__lowerCAmelCase , world_size=__lowerCAmelCase ) UpperCAmelCase : List[Any] =torch.utils.data.DataLoader(__lowerCAmelCase , num_workers=__lowerCAmelCase ) UpperCAmelCase : Dict =NUM_SHARDS * NUM_ITEMS_PER_SHARD UpperCAmelCase : str =full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) UpperCAmelCase : List[Any] =sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f'''local_size {local_size} != expected_local_size {expected_local_size}''' ) if __name__ == "__main__": main()
348
0
import sys from typing import Tuple import numpy as np import torch from PIL import Image from torch import nn from transformers.image_utils import PILImageResampling from utils import img_tensorize class __snake_case : def __init__( self : int , _snake_case : Tuple , _snake_case : Union[str, Any]=sys.maxsize): """simple docstring""" UpperCAmelCase_ = '''bilinear''' UpperCAmelCase_ = max_size UpperCAmelCase_ = short_edge_length def __call__( self : Tuple , _snake_case : int): """simple docstring""" UpperCAmelCase_ = [] for img in imgs: UpperCAmelCase_ , UpperCAmelCase_ = img.shape[:2] # later: provide list and randomly choose index for resize UpperCAmelCase_ = np.random.randint(self.short_edge_length[0] , self.short_edge_length[1] + 1) if size == 0: return img UpperCAmelCase_ = size * 1.0 / min(_snake_case , _snake_case) if h < w: UpperCAmelCase_ , UpperCAmelCase_ = size, scale * w else: UpperCAmelCase_ , UpperCAmelCase_ = scale * h, size if max(_snake_case , _snake_case) > self.max_size: UpperCAmelCase_ = self.max_size * 1.0 / max(_snake_case , _snake_case) UpperCAmelCase_ = newh * scale UpperCAmelCase_ = neww * scale UpperCAmelCase_ = int(neww + 0.5) UpperCAmelCase_ = int(newh + 0.5) if img.dtype == np.uinta: UpperCAmelCase_ = Image.fromarray(_snake_case) UpperCAmelCase_ = pil_image.resize((neww, newh) , PILImageResampling.BILINEAR) UpperCAmelCase_ = np.asarray(_snake_case) else: UpperCAmelCase_ = img.permute(2 , 0 , 1).unsqueeze(0) # 3, 0, 1) # hw(c) -> nchw UpperCAmelCase_ = nn.functional.interpolate( _snake_case , (newh, neww) , mode=self.interp_method , align_corners=_snake_case).squeeze(0) img_augs.append(_snake_case) return img_augs class __snake_case : def __init__( self : Tuple , _snake_case : int): """simple docstring""" UpperCAmelCase_ = ResizeShortestEdge([cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST] , cfg.INPUT.MAX_SIZE_TEST) UpperCAmelCase_ = cfg.INPUT.FORMAT UpperCAmelCase_ = cfg.SIZE_DIVISIBILITY UpperCAmelCase_ = cfg.PAD_VALUE UpperCAmelCase_ = cfg.INPUT.MAX_SIZE_TEST UpperCAmelCase_ = cfg.MODEL.DEVICE UpperCAmelCase_ = torch.tensor(cfg.MODEL.PIXEL_STD).to(self.device).view(len(cfg.MODEL.PIXEL_STD) , 1 , 1) UpperCAmelCase_ = torch.tensor(cfg.MODEL.PIXEL_MEAN).to(self.device).view(len(cfg.MODEL.PIXEL_STD) , 1 , 1) UpperCAmelCase_ = lambda _snake_case: (x - self.pixel_mean) / self.pixel_std def lowerCamelCase ( self : str , _snake_case : List[str]): """simple docstring""" UpperCAmelCase_ = tuple(max(_snake_case) for s in zip(*[img.shape for img in images])) UpperCAmelCase_ = [im.shape[-2:] for im in images] UpperCAmelCase_ = [ nn.functional.pad( _snake_case , [0, max_size[-1] - size[1], 0, max_size[-2] - size[0]] , value=self.pad_value , ) for size, im in zip(_snake_case , _snake_case) ] return torch.stack(_snake_case), torch.tensor(_snake_case) def __call__( self : str , _snake_case : List[str] , _snake_case : int=False): """simple docstring""" with torch.no_grad(): if not isinstance(_snake_case , _snake_case): UpperCAmelCase_ = [images] if single_image: assert len(_snake_case) == 1 for i in range(len(_snake_case)): if isinstance(images[i] , torch.Tensor): images.insert(_snake_case , images.pop(_snake_case).to(self.device).float()) elif not isinstance(images[i] , torch.Tensor): images.insert( _snake_case , torch.as_tensor(img_tensorize(images.pop(_snake_case) , input_format=self.input_format)) .to(self.device) .float() , ) # resize smallest edge UpperCAmelCase_ = torch.tensor([im.shape[:2] for im in images]) UpperCAmelCase_ = self.aug(_snake_case) # transpose images and convert to torch tensors # images = [torch.as_tensor(i.astype("float32")).permute(2, 0, 1).to(self.device) for i in images] # now normalize before pad to avoid useless arithmetic UpperCAmelCase_ = [self.normalizer(_snake_case) for x in images] # now pad them to do the following operations UpperCAmelCase_ , UpperCAmelCase_ = self.pad(_snake_case) # Normalize if self.size_divisibility > 0: raise NotImplementedError() # pad UpperCAmelCase_ = torch.true_divide(_snake_case , _snake_case) if single_image: return images[0], sizes[0], scales_yx[0] else: return images, sizes, scales_yx def A (__A : Optional[Any] , __A : Optional[int] ) -> List[Any]: """simple docstring""" boxes[:, 0::2] *= scale_yx[:, 1] boxes[:, 1::2] *= scale_yx[:, 0] return boxes def A (__A : int , __A : Tuple[int, int] ) -> Tuple: """simple docstring""" assert torch.isfinite(__A ).all(), "Box tensor contains infinite or NaN!" UpperCAmelCase_ , UpperCAmelCase_ = box_size tensor[:, 0].clamp_(min=0 , max=__A ) tensor[:, 1].clamp_(min=0 , max=__A ) tensor[:, 2].clamp_(min=0 , max=__A ) tensor[:, 3].clamp_(min=0 , max=__A )
51
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __snake_case = {'''configuration_opt''': ['''OPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OPTConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''OPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OPTForCausalLM''', '''OPTModel''', '''OPTPreTrainedModel''', '''OPTForSequenceClassification''', '''OPTForQuestionAnswering''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''TFOPTForCausalLM''', '''TFOPTModel''', '''TFOPTPreTrainedModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''FlaxOPTForCausalLM''', '''FlaxOPTModel''', '''FlaxOPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
0
import json import os import shutil import tempfile import unittest from transformers import BatchEncoding, CanineTokenizer from transformers.testing_utils import require_tokenizers, require_torch from transformers.tokenization_utils import AddedToken from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin class A__ ( __snake_case , unittest.TestCase ): _UpperCAmelCase :Union[str, Any] = CanineTokenizer _UpperCAmelCase :Optional[Any] = False def __UpperCamelCase( self ): '''simple docstring''' super().setUp() UpperCamelCase : Optional[int] = CanineTokenizer() tokenizer.save_pretrained(self.tmpdirname ) @cached_property def __UpperCamelCase( self ): '''simple docstring''' return CanineTokenizer.from_pretrained("google/canine-s" ) def __UpperCamelCase( self , **A_ ): '''simple docstring''' UpperCamelCase : Any = self.tokenizer_class.from_pretrained(self.tmpdirname , **A_ ) UpperCamelCase : Tuple = 1024 return tokenizer @require_torch def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Union[str, Any] = self.canine_tokenizer UpperCamelCase : str = ["Life is like a box of chocolates.", "You never know what you're gonna get."] # fmt: off UpperCamelCase : List[Any] = [5_7344, 76, 105, 102, 101, 32, 105, 115, 32, 108, 105, 107, 101, 32, 97, 32, 98, 111, 120, 32, 111, 102, 32, 99, 104, 111, 99, 111, 108, 97, 116, 101, 115, 46, 5_7345, 0, 0, 0, 0] # fmt: on UpperCamelCase : Optional[Any] = tokenizer(A_ , padding=A_ , return_tensors="pt" ) self.assertIsInstance(A_ , A_ ) UpperCamelCase : Dict = list(batch.input_ids.numpy()[0] ) self.assertListEqual(A_ , A_ ) self.assertEqual((2, 39) , batch.input_ids.shape ) self.assertEqual((2, 39) , batch.attention_mask.shape ) @require_torch def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Any = self.canine_tokenizer UpperCamelCase : str = ["Once there was a man.", "He wrote a test in HuggingFace Tranformers."] UpperCamelCase : Optional[Any] = tokenizer(A_ , padding=A_ , return_tensors="pt" ) # check if input_ids, attention_mask and token_type_ids are returned self.assertIn("input_ids" , A_ ) self.assertIn("attention_mask" , A_ ) self.assertIn("token_type_ids" , A_ ) @require_torch def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : str = self.canine_tokenizer UpperCamelCase : List[Any] = [ "What's the weater?", "It's about 25 degrees.", ] UpperCamelCase : Any = tokenizer( text_target=A_ , max_length=32 , padding="max_length" , truncation=A_ , return_tensors="pt" ) self.assertEqual(32 , targets["input_ids"].shape[1] ) def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Any = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): self.assertNotEqual(tokenizer.model_max_length , 42 ) # Now let's start the test UpperCamelCase : int = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): # Isolate this from the other tests because we save additional tokens/etc UpperCamelCase : Dict = tempfile.mkdtemp() UpperCamelCase : Optional[Any] = " He is very happy, UNwant\u00E9d,running" UpperCamelCase : Union[str, Any] = tokenizer.encode(A_ , add_special_tokens=A_ ) tokenizer.save_pretrained(A_ ) UpperCamelCase : int = tokenizer.__class__.from_pretrained(A_ ) UpperCamelCase : Any = after_tokenizer.encode(A_ , add_special_tokens=A_ ) self.assertListEqual(A_ , A_ ) shutil.rmtree(A_ ) UpperCamelCase : List[str] = self.get_tokenizers(model_max_length=42 ) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): # Isolate this from the other tests because we save additional tokens/etc UpperCamelCase : Union[str, Any] = tempfile.mkdtemp() UpperCamelCase : List[str] = " He is very happy, UNwant\u00E9d,running" UpperCamelCase : List[Any] = tokenizer.additional_special_tokens # We can add a new special token for Canine as follows: UpperCamelCase : Dict = chr(0xE_0_0_7 ) additional_special_tokens.append(A_ ) tokenizer.add_special_tokens({"additional_special_tokens": additional_special_tokens} ) UpperCamelCase : int = tokenizer.encode(A_ , add_special_tokens=A_ ) tokenizer.save_pretrained(A_ ) UpperCamelCase : List[str] = tokenizer.__class__.from_pretrained(A_ ) UpperCamelCase : List[str] = after_tokenizer.encode(A_ , add_special_tokens=A_ ) self.assertListEqual(A_ , A_ ) self.assertIn(A_ , after_tokenizer.additional_special_tokens ) self.assertEqual(after_tokenizer.model_max_length , 42 ) UpperCamelCase : str = tokenizer.__class__.from_pretrained(A_ , model_max_length=43 ) self.assertEqual(tokenizer.model_max_length , 43 ) shutil.rmtree(A_ ) def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : List[Any] = self.get_tokenizers(do_lower_case=A_ ) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): UpperCamelCase , UpperCamelCase : Dict = self.get_clean_sequence(A_ ) # a special token for Canine can be defined as follows: UpperCamelCase : Dict = 0xE_0_0_5 UpperCamelCase : List[Any] = chr(A_ ) tokenizer.add_special_tokens({"cls_token": special_token} ) UpperCamelCase : Any = tokenizer.encode(A_ , add_special_tokens=A_ ) self.assertEqual(len(A_ ) , 1 ) UpperCamelCase : Union[str, Any] = tokenizer.decode(ids + encoded_special_token , clean_up_tokenization_spaces=A_ ) UpperCamelCase : Optional[int] = tokenizer.encode(A_ , add_special_tokens=A_ ) UpperCamelCase : Tuple = tokenizer.encode(A_ , add_special_tokens=A_ ) UpperCamelCase : Optional[Any] = tokenizer.encode(A_ , add_special_tokens=A_ ) self.assertEqual(A_ , input_encoded + special_token_id ) UpperCamelCase : List[str] = tokenizer.decode(A_ , skip_special_tokens=A_ ) self.assertTrue(special_token not in decoded ) def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : str = self.get_tokenizers(do_lower_case=A_ ) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): UpperCamelCase : str = chr(0xE_0_0_5 ) UpperCamelCase : Optional[int] = chr(0xE_0_0_6 ) # `add_tokens` method stores special tokens only in `tokenizer.unique_no_split_tokens`. (in tokenization_utils.py) tokenizer.add_tokens([SPECIAL_TOKEN_1] , special_tokens=A_ ) # `add_special_tokens` method stores special tokens in `tokenizer.additional_special_tokens`, # which also occur in `tokenizer.all_special_tokens`. (in tokenization_utils_base.py) tokenizer.add_special_tokens({"additional_special_tokens": [SPECIAL_TOKEN_2]} ) UpperCamelCase : Any = tokenizer.tokenize(A_ ) UpperCamelCase : List[Any] = tokenizer.tokenize(A_ ) self.assertEqual(len(A_ ) , 1 ) self.assertEqual(len(A_ ) , 1 ) self.assertEqual(token_a[0] , A_ ) self.assertEqual(token_a[0] , A_ ) @require_tokenizers def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : int = self.get_tokenizers(do_lower_case=A_ ) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): # a special token for Canine can be defined as follows: UpperCamelCase : Union[str, Any] = 0xE_0_0_6 UpperCamelCase : Tuple = chr(A_ ) UpperCamelCase : Optional[Any] = AddedToken(A_ , lstrip=A_ ) tokenizer.add_special_tokens({"additional_special_tokens": [new_token]} ) with tempfile.TemporaryDirectory() as tmp_dir_name: tokenizer.save_pretrained(A_ ) tokenizer.from_pretrained(A_ ) def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Union[str, Any] = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(A_ ) with open(os.path.join(A_ , "special_tokens_map.json" ) , encoding="utf-8" ) as json_file: UpperCamelCase : List[Any] = json.load(A_ ) with open(os.path.join(A_ , "tokenizer_config.json" ) , encoding="utf-8" ) as json_file: UpperCamelCase : Optional[int] = json.load(A_ ) # a special token for Canine can be defined as follows: UpperCamelCase : Dict = 0xE_0_0_6 UpperCamelCase : Any = chr(A_ ) UpperCamelCase : Tuple = [new_token_a] UpperCamelCase : Union[str, Any] = [new_token_a] with open(os.path.join(A_ , "special_tokens_map.json" ) , "w" , encoding="utf-8" ) as outfile: json.dump(A_ , A_ ) with open(os.path.join(A_ , "tokenizer_config.json" ) , "w" , encoding="utf-8" ) as outfile: json.dump(A_ , A_ ) # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and # "special_tokens_map.json" files UpperCamelCase : Optional[Any] = tokenizer_class.from_pretrained(A_ , extra_ids=0 ) self.assertIn(A_ , tokenizer_without_change_in_init.additional_special_tokens ) # self.assertIn("an_additional_special_token",tokenizer_without_change_in_init.get_vocab()) # ByT5Tokenization no vocab self.assertEqual( [new_token_a] , tokenizer_without_change_in_init.convert_ids_to_tokens( tokenizer_without_change_in_init.convert_tokens_to_ids([new_token_a] ) ) , ) UpperCamelCase : Optional[int] = 0xE_0_0_7 UpperCamelCase : Union[str, Any] = chr(A_ ) # Now we test that we can change the value of additional_special_tokens in the from_pretrained UpperCamelCase : List[str] = [AddedToken(A_ , lstrip=A_ )] UpperCamelCase : Optional[int] = tokenizer_class.from_pretrained( A_ , additional_special_tokens=A_ , extra_ids=0 ) self.assertIn(A_ , tokenizer.additional_special_tokens ) # self.assertIn(new_token_2,tokenizer.get_vocab()) # ByT5Tokenization no vocab self.assertEqual( [new_token_a] , tokenizer.convert_ids_to_tokens(tokenizer.convert_tokens_to_ids([new_token_a] ) ) ) @require_tokenizers def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Optional[Any] = self.get_tokenizers(do_lower_case=A_ ) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): UpperCamelCase : Optional[Any] = "hello world" if self.space_between_special_tokens: UpperCamelCase : Optional[Any] = "[CLS] hello world [SEP]" else: UpperCamelCase : Dict = input UpperCamelCase : Optional[int] = tokenizer.encode(A_ , add_special_tokens=A_ ) UpperCamelCase : Tuple = tokenizer.decode(A_ , spaces_between_special_tokens=self.space_between_special_tokens ) self.assertIn(A_ , [output, output.lower()] ) def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : int = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): UpperCamelCase : List[str] = [ "bos_token", "eos_token", "unk_token", "sep_token", "pad_token", "cls_token", "mask_token", ] UpperCamelCase : Optional[int] = "a" UpperCamelCase : Tuple = ord(A_ ) for attr in attributes_list: setattr(A_ , attr + "_id" , A_ ) self.assertEqual(getattr(A_ , A_ ) , A_ ) self.assertEqual(getattr(A_ , attr + "_id" ) , A_ ) setattr(A_ , attr + "_id" , A_ ) self.assertEqual(getattr(A_ , A_ ) , A_ ) self.assertEqual(getattr(A_ , attr + "_id" ) , A_ ) setattr(A_ , "additional_special_tokens_ids" , [] ) self.assertListEqual(getattr(A_ , "additional_special_tokens" ) , [] ) self.assertListEqual(getattr(A_ , "additional_special_tokens_ids" ) , [] ) UpperCamelCase : Union[str, Any] = 0xE_0_0_6 UpperCamelCase : List[Any] = chr(A_ ) setattr(A_ , "additional_special_tokens_ids" , [additional_special_token_id] ) self.assertListEqual(getattr(A_ , "additional_special_tokens" ) , [additional_special_token] ) self.assertListEqual(getattr(A_ , "additional_special_tokens_ids" ) , [additional_special_token_id] ) def __UpperCamelCase( self ): '''simple docstring''' pass def __UpperCamelCase( self ): '''simple docstring''' pass def __UpperCamelCase( self ): '''simple docstring''' pass def __UpperCamelCase( self ): '''simple docstring''' pass def __UpperCamelCase( self ): '''simple docstring''' pass def __UpperCamelCase( self ): '''simple docstring''' pass def __UpperCamelCase( self ): '''simple docstring''' pass def __UpperCamelCase( self ): '''simple docstring''' pass
52
import tempfile import unittest import numpy as np import transformers from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax import jax.numpy as jnp from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel if is_torch_available(): import torch class __snake_case : def __init__( self , snake_case__ , snake_case__=14 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=False , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=4 , snake_case__=4 , snake_case__=4 , snake_case__=37 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , ) -> str: '''simple docstring''' UpperCAmelCase : str =parent UpperCAmelCase : Tuple =batch_size UpperCAmelCase : Optional[int] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Tuple =use_input_mask UpperCAmelCase : List[Any] =use_token_type_ids UpperCAmelCase : Optional[Any] =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : List[Any] =hidden_size UpperCAmelCase : Optional[int] =rotary_dim UpperCAmelCase : Union[str, Any] =num_hidden_layers UpperCAmelCase : List[Any] =num_attention_heads UpperCAmelCase : Dict =intermediate_size UpperCAmelCase : Union[str, Any] =hidden_act UpperCAmelCase : Any =hidden_dropout_prob UpperCAmelCase : Dict =attention_probs_dropout_prob UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : str =initializer_range UpperCAmelCase : Optional[int] =None UpperCAmelCase : List[Any] =vocab_size - 1 UpperCAmelCase : Optional[Any] =vocab_size - 1 UpperCAmelCase : List[Any] =vocab_size - 1 def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : List[str] =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : List[Any] =None if self.use_input_mask: UpperCAmelCase : Optional[Any] =random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : Dict =GPTJConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , use_cache=snake_case__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , rotary_dim=self.rotary_dim , ) return (config, input_ids, input_mask) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Union[str, Any] =config_and_inputs UpperCAmelCase : Tuple ={'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =20 UpperCAmelCase : Any =model_class_name(snake_case__ ) UpperCAmelCase : str =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : Any =jnp.ones((input_ids.shape[0], max_decoder_length) , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : List[str] =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : Optional[Any] =model( input_ids[:, -1:] , attention_mask=snake_case__ , past_key_values=outputs_cache.past_key_values , position_ids=snake_case__ , ) UpperCAmelCase : List[Any] =model(snake_case__ ) UpperCAmelCase : Any =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Dict =20 UpperCAmelCase : Dict =model_class_name(snake_case__ ) UpperCAmelCase : Tuple =jnp.concatenate( [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]) )] , axis=-1 , ) UpperCAmelCase : Dict =model.init_cache(input_ids.shape[0] , snake_case__ ) UpperCAmelCase : int =jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) UpperCAmelCase : Optional[Any] =model( input_ids[:, :-1] , attention_mask=snake_case__ , past_key_values=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) UpperCAmelCase : str =model( input_ids[:, -1:] , past_key_values=outputs_cache.past_key_values , attention_mask=snake_case__ , position_ids=snake_case__ , ) UpperCAmelCase : Any =model(snake_case__ , attention_mask=snake_case__ ) UpperCAmelCase : Dict =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) @require_flax class __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Tuple = (FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else () __lowerCamelCase : Optional[Any] = (FlaxGPTJForCausalLM,) if is_flax_available() else () def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =FlaxGPTJModelTester(self ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Dict =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward_with_attn_mask( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) @tooslow def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Tuple =GPTaTokenizer.from_pretrained('''gpt2''' , pad_token='''<|endoftext|>''' , padding_side='''left''' ) UpperCAmelCase : Optional[Any] =tokenizer(['''Hello this is a long string''', '''Hey'''] , return_tensors='''np''' , padding=snake_case__ , truncation=snake_case__ ) UpperCAmelCase : Optional[int] =FlaxGPTJForCausalLM.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : str =False UpperCAmelCase : Union[str, Any] =model.config.eos_token_id UpperCAmelCase : List[Any] =jax.jit(model.generate ) UpperCAmelCase : Dict =jit_generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , pad_token_id=tokenizer.pad_token_id ).sequences UpperCAmelCase : Any =tokenizer.batch_decode(snake_case__ , skip_special_tokens=snake_case__ ) UpperCAmelCase : Tuple =[ '''Hello this is a long string of text.\n\nI\'m trying to get the text of the''', '''Hey, I\'m a little late to the party. I\'m going to''', ] self.assertListEqual(snake_case__ , snake_case__ ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : List[str] =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : List[str] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : Any =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : Any =getattr(snake_case__ , snake_case__ ) UpperCAmelCase , UpperCAmelCase : Union[str, Any] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Tuple =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : int =0 UpperCAmelCase : Optional[int] =1 UpperCAmelCase : Optional[int] =0 UpperCAmelCase : Union[str, Any] =1 UpperCAmelCase : List[str] =pt_model_class(snake_case__ ).eval() UpperCAmelCase : Optional[int] =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Any =convert_pytorch_state_dict_to_flax(pt_model.state_dict() , snake_case__ ) UpperCAmelCase : Union[str, Any] =fx_state with torch.no_grad(): UpperCAmelCase : Any =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : Dict =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(snake_case__ ) UpperCAmelCase : str =model_class.from_pretrained(snake_case__ , from_pt=snake_case__ ) UpperCAmelCase : int =fx_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output_loaded, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output_loaded[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @is_pt_flax_cross_test def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs UpperCAmelCase : Union[str, Any] =self._prepare_for_class(snake_case__ , snake_case__ ) UpperCAmelCase : Union[str, Any] ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class UpperCAmelCase : int =model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCAmelCase : int =getattr(snake_case__ , snake_case__ ) UpperCAmelCase : Dict =pt_model_class(snake_case__ ).eval() UpperCAmelCase : str =model_class(snake_case__ , dtype=jnp.floataa ) UpperCAmelCase : Optional[Any] =load_flax_weights_in_pytorch_model(snake_case__ , fx_model.params ) UpperCAmelCase , UpperCAmelCase : Optional[int] =pt_inputs['''input_ids'''].shape UpperCAmelCase : Optional[int] =np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : str =0 UpperCAmelCase : Any =1 UpperCAmelCase : List[Any] =0 UpperCAmelCase : Tuple =1 # make sure weights are tied in PyTorch pt_model.tie_weights() with torch.no_grad(): UpperCAmelCase : Optional[Any] =pt_model(**snake_case__ ).to_tuple() UpperCAmelCase : List[Any] =fx_model(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(snake_case__ ) UpperCAmelCase : Tuple =pt_model_class.from_pretrained(snake_case__ , from_flax=snake_case__ ) with torch.no_grad(): UpperCAmelCase : Any =pt_model_loaded(**snake_case__ ).to_tuple() self.assertEqual( len(snake_case__ ) , len(snake_case__ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(snake_case__ , snake_case__ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @tooslow def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase : str =model_class_name.from_pretrained('''EleutherAI/gpt-j-6B''' ) UpperCAmelCase : Tuple =model(np.ones((1, 1) ) ) self.assertIsNotNone(snake_case__ )
348
0
'''simple docstring''' import unittest import numpy as np import timeout_decorator # noqa from transformers import BlenderbotSmallConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html a__ : int ='''platform''' import jax import jax.numpy as jnp from transformers.models.blenderbot_small.modeling_flax_blenderbot_small import ( FlaxBlenderbotSmallForConditionalGeneration, FlaxBlenderbotSmallModel, shift_tokens_right, ) def lowercase__ ( __lowercase : int , __lowercase : Optional[int] , __lowercase : str=None , __lowercase : List[Any]=None , __lowercase : Dict=None , __lowercase : int=None , __lowercase : Union[str, Any]=None , __lowercase : Dict=None , ) -> List[Any]: """simple docstring""" if attention_mask is None: __UpperCamelCase = np.where(input_ids != config.pad_token_id , 1 , 0 ) if decoder_attention_mask is None: __UpperCamelCase = np.where(decoder_input_ids != config.pad_token_id , 1 , 0 ) if head_mask is None: __UpperCamelCase = np.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: __UpperCamelCase = np.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: __UpperCamelCase = np.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, } class snake_case : """simple docstring""" def __init__( self : Optional[int] , __A : List[Any] , __A : List[str]=1_3 , __A : str=7 , __A : Any=True , __A : Optional[Any]=False , __A : Optional[int]=9_9 , __A : str=1_6 , __A : List[str]=2 , __A : str=4 , __A : str=4 , __A : Optional[Any]="gelu" , __A : Dict=0.1 , __A : Optional[Any]=0.1 , __A : Tuple=3_2 , __A : Dict=2 , __A : Tuple=1 , __A : List[str]=0 , __A : Optional[int]=0.02 , ): __UpperCamelCase = parent __UpperCamelCase = batch_size __UpperCamelCase = seq_length __UpperCamelCase = is_training __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 = eos_token_id __UpperCamelCase = pad_token_id __UpperCamelCase = bos_token_id __UpperCamelCase = initializer_range def _lowerCamelCase ( self : Dict ): __UpperCamelCase = np.clip(ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) , 3 , self.vocab_size ) __UpperCamelCase = np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1) , dtype=np.intaa )) , -1 ) __UpperCamelCase = shift_tokens_right(__A , 1 , 2 ) __UpperCamelCase = BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , initializer_range=self.initializer_range , use_cache=__A , ) __UpperCamelCase = prepare_blenderbot_inputs_dict(__A , __A , __A ) return config, inputs_dict def _lowerCamelCase ( self : Optional[Any] ): __UpperCamelCase , __UpperCamelCase = self.prepare_config_and_inputs() return config, inputs_dict def _lowerCamelCase ( self : List[str] , __A : Dict , __A : Tuple , __A : Optional[int] ): __UpperCamelCase = 2_0 __UpperCamelCase = model_class_name(__A ) __UpperCamelCase = model.encode(inputs_dict['input_ids'] ) __UpperCamelCase , __UpperCamelCase = ( inputs_dict['decoder_input_ids'], inputs_dict['decoder_attention_mask'], ) __UpperCamelCase = model.init_cache(decoder_input_ids.shape[0] , __A , __A ) __UpperCamelCase = jnp.ones((decoder_input_ids.shape[0], max_decoder_length) , dtype='i4' ) __UpperCamelCase = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) __UpperCamelCase = model.decode( decoder_input_ids[:, :-1] , __A , decoder_attention_mask=__A , past_key_values=__A , decoder_position_ids=__A , ) __UpperCamelCase = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype='i4' ) __UpperCamelCase = model.decode( decoder_input_ids[:, -1:] , __A , decoder_attention_mask=__A , past_key_values=outputs_cache.past_key_values , decoder_position_ids=__A , ) __UpperCamelCase = model.decode(__A , __A ) __UpperCamelCase = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) def _lowerCamelCase ( self : Optional[int] , __A : List[Any] , __A : Union[str, Any] , __A : Any ): __UpperCamelCase = 2_0 __UpperCamelCase = model_class_name(__A ) __UpperCamelCase = model.encode(inputs_dict['input_ids'] ) __UpperCamelCase , __UpperCamelCase = ( inputs_dict['decoder_input_ids'], inputs_dict['decoder_attention_mask'], ) __UpperCamelCase = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ] , axis=-1 , ) __UpperCamelCase = model.init_cache(decoder_input_ids.shape[0] , __A , __A ) __UpperCamelCase = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) __UpperCamelCase = model.decode( decoder_input_ids[:, :-1] , __A , decoder_attention_mask=__A , past_key_values=__A , decoder_position_ids=__A , ) __UpperCamelCase = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype='i4' ) __UpperCamelCase = model.decode( decoder_input_ids[:, -1:] , __A , past_key_values=outputs_cache.past_key_values , decoder_attention_mask=__A , decoder_position_ids=__A , ) __UpperCamelCase = model.decode(__A , __A , decoder_attention_mask=__A ) __UpperCamelCase = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'''Max diff is {diff}''' ) @require_flax class snake_case ( unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] =99 def _lowerCamelCase ( self : List[Any] ): __UpperCamelCase = np.array( [ [7_1, 8_2, 1_8, 3_3, 4_6, 9_1, 2], [6_8, 3_4, 2_6, 5_8, 3_0, 8_2, 2], [5, 9_7, 1_7, 3_9, 9_4, 4_0, 2], [7_6, 8_3, 9_4, 2_5, 7_0, 7_8, 2], [8_7, 5_9, 4_1, 3_5, 4_8, 6_6, 2], [5_5, 1_3, 1_6, 5_8, 5, 2, 1], # note padding [6_4, 2_7, 3_1, 5_1, 1_2, 7_5, 2], [5_2, 6_4, 8_6, 1_7, 8_3, 3_9, 2], [4_8, 6_1, 9, 2_4, 7_1, 8_2, 2], [2_6, 1, 6_0, 4_8, 2_2, 1_3, 2], [2_1, 5, 6_2, 2_8, 1_4, 7_6, 2], [4_5, 9_8, 3_7, 8_6, 5_9, 4_8, 2], [7_0, 7_0, 5_0, 9, 2_8, 0, 2], ] , dtype=np.intaa , ) __UpperCamelCase = input_ids.shape[0] __UpperCamelCase = BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=2_4 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=3_2 , decoder_ffn_dim=3_2 , max_position_embeddings=4_8 , eos_token_id=2 , pad_token_id=1 , bos_token_id=0 , ) return config, input_ids, batch_size def _lowerCamelCase ( self : List[str] ): __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = self._get_config_and_data() __UpperCamelCase = FlaxBlenderbotSmallForConditionalGeneration(__A ) __UpperCamelCase = lm_model(input_ids=__A ) __UpperCamelCase = (batch_size, input_ids.shape[1], config.vocab_size) self.assertEqual(outputs['logits'].shape , __A ) def _lowerCamelCase ( self : Union[str, Any] ): __UpperCamelCase = BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=1_4 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=8 , decoder_ffn_dim=8 , max_position_embeddings=4_8 , ) __UpperCamelCase = FlaxBlenderbotSmallForConditionalGeneration(__A ) __UpperCamelCase = np.array([[7_1, 8_2, 1_8, 3_3, 4_6, 9_1, 2], [6_8, 3_4, 2_6, 5_8, 3_0, 2, 1]] , dtype=np.intaa ) __UpperCamelCase = np.array([[8_2, 7_1, 8_2, 1_8, 2], [5_8, 6_8, 2, 1, 1]] , dtype=np.intaa ) __UpperCamelCase = lm_model(input_ids=__A , decoder_input_ids=__A ) __UpperCamelCase = (*summary.shape, config.vocab_size) self.assertEqual(outputs['logits'].shape , __A ) def _lowerCamelCase ( self : Optional[Any] ): __UpperCamelCase = np.array([[7_1, 8_2, 1_8, 3_3, 2, 1, 1], [6_8, 3_4, 2_6, 5_8, 3_0, 8_2, 2]] , dtype=np.intaa ) __UpperCamelCase = shift_tokens_right(__A , 1 , 2 ) __UpperCamelCase = np.equal(__A , 1 ).astype(np.floataa ).sum() __UpperCamelCase = np.equal(__A , 1 ).astype(np.floataa ).sum() self.assertEqual(shifted.shape , input_ids.shape ) self.assertEqual(__A , n_pad_before - 1 ) self.assertTrue(np.equal(shifted[:, 0] , 2 ).all() ) @require_flax class snake_case ( __lowerCamelCase , unittest.TestCase , __lowerCamelCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict =True SCREAMING_SNAKE_CASE_ : Tuple =( ( FlaxBlenderbotSmallModel, FlaxBlenderbotSmallForConditionalGeneration, ) if is_flax_available() else () ) SCREAMING_SNAKE_CASE_ : Optional[Any] =(FlaxBlenderbotSmallForConditionalGeneration,) if is_flax_available() else () def _lowerCamelCase ( self : List[Any] ): __UpperCamelCase = FlaxBlenderbotSmallModelTester(self ) def _lowerCamelCase ( self : int ): __UpperCamelCase , __UpperCamelCase = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(__A , __A , __A ) def _lowerCamelCase ( self : Optional[Any] ): __UpperCamelCase , __UpperCamelCase = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(__A , __A , __A ) def _lowerCamelCase ( self : Optional[Any] ): __UpperCamelCase , __UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __UpperCamelCase = self._prepare_for_class(__A , __A ) __UpperCamelCase = model_class(__A ) @jax.jit def encode_jitted(__A : List[str] , __A : List[str]=None , **__A : Dict ): return model.encode(input_ids=__A , attention_mask=__A ) with self.subTest('JIT Enabled' ): __UpperCamelCase = encode_jitted(**__A ).to_tuple() with self.subTest('JIT Disabled' ): with jax.disable_jit(): __UpperCamelCase = encode_jitted(**__A ).to_tuple() self.assertEqual(len(__A ) , len(__A ) ) for jitted_output, output in zip(__A , __A ): self.assertEqual(jitted_output.shape , output.shape ) def _lowerCamelCase ( self : str ): __UpperCamelCase , __UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __UpperCamelCase = model_class(__A ) __UpperCamelCase = model.encode(inputs_dict['input_ids'] , inputs_dict['attention_mask'] ) __UpperCamelCase = { 'decoder_input_ids': inputs_dict['decoder_input_ids'], 'decoder_attention_mask': inputs_dict['decoder_attention_mask'], 'encoder_outputs': encoder_outputs, } @jax.jit def decode_jitted(__A : List[Any] , __A : Tuple , __A : List[Any] ): return model.decode( decoder_input_ids=__A , decoder_attention_mask=__A , encoder_outputs=__A , ) with self.subTest('JIT Enabled' ): __UpperCamelCase = decode_jitted(**__A ).to_tuple() with self.subTest('JIT Disabled' ): with jax.disable_jit(): __UpperCamelCase = decode_jitted(**__A ).to_tuple() self.assertEqual(len(__A ) , len(__A ) ) for jitted_output, output in zip(__A , __A ): self.assertEqual(jitted_output.shape , output.shape ) @slow def _lowerCamelCase ( self : str ): for model_class_name in self.all_model_classes: __UpperCamelCase = model_class_name.from_pretrained('facebook/blenderbot_small-90M' ) # FlaxBlenderbotForSequenceClassification expects eos token in input_ids __UpperCamelCase = np.ones((1, 1) ) * model.config.eos_token_id __UpperCamelCase = model(__A ) self.assertIsNotNone(__A )
53
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __snake_case = { '''configuration_bloom''': ['''BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BloomConfig''', '''BloomOnnxConfig'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''BloomTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BloomForCausalLM''', '''BloomModel''', '''BloomPreTrainedModel''', '''BloomForSequenceClassification''', '''BloomForTokenClassification''', '''BloomForQuestionAnswering''', ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
0
"""simple docstring""" import string from math import logaa def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ ): '''simple docstring''' __SCREAMING_SNAKE_CASE = document.translate( str.maketrans("" , "" , string.punctuation ) ).replace("\n" , "" ) __SCREAMING_SNAKE_CASE = document_without_punctuation.split(" " ) # word tokenization return len([word for word in tokenize_document if word.lower() == term.lower()] ) def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ ): '''simple docstring''' __SCREAMING_SNAKE_CASE = corpus.lower().translate( str.maketrans("" , "" , string.punctuation ) ) # strip all punctuation and replace it with '' __SCREAMING_SNAKE_CASE = corpus_without_punctuation.split("\n" ) __SCREAMING_SNAKE_CASE = term.lower() return (len([doc for doc in docs if term in doc] ), len(lowerCAmelCase_ )) def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_=False ): '''simple docstring''' if smoothing: if n == 0: raise ValueError("log10(0) is undefined." ) return round(1 + logaa(n / (1 + df) ) , 3 ) if df == 0: raise ZeroDivisionError("df must be > 0" ) elif n == 0: raise ValueError("log10(0) is undefined." ) return round(logaa(n / df ) , 3 ) def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ ): '''simple docstring''' return round(tf * idf , 3 )
54
import os from typing import Dict, List, Tuple, TypeVar, Union __snake_case = TypeVar('''T''') __snake_case = Union[List[T], Tuple[T, ...]] __snake_case = Union[T, List[T], Dict[str, T]] __snake_case = Union[str, bytes, os.PathLike]
348
0
'''simple docstring''' # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor from .base import PipelineTool class snake_case ( lowercase ): """simple docstring""" _lowerCamelCase = "openai/whisper-base" _lowerCamelCase = ( "This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the " "transcribed text." ) _lowerCamelCase = "transcriber" _lowerCamelCase = WhisperProcessor _lowerCamelCase = WhisperForConditionalGeneration _lowerCamelCase = ["audio"] _lowerCamelCase = ["text"] def snake_case ( self , UpperCamelCase ): """simple docstring""" return self.pre_processor(UpperCamelCase , return_tensors="pt" ).input_features def snake_case ( self , UpperCamelCase ): """simple docstring""" return self.model.generate(inputs=UpperCamelCase ) def snake_case ( self , UpperCamelCase ): """simple docstring""" return self.pre_processor.batch_decode(UpperCamelCase , skip_special_tokens=UpperCamelCase )[0]
55
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_big_bird import BigBirdTokenizer else: __snake_case = None __snake_case = logging.get_logger(__name__) __snake_case = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} __snake_case = { '''vocab_file''': { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model''', '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model''' ), }, '''tokenizer_file''': { '''google/bigbird-roberta-base''': ( '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/tokenizer.json''' ), '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/tokenizer.json''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/tokenizer.json''' ), }, } __snake_case = { '''google/bigbird-roberta-base''': 40_96, '''google/bigbird-roberta-large''': 40_96, '''google/bigbird-base-trivia-itc''': 40_96, } __snake_case = '''▁''' class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Dict = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = BigBirdTokenizer __lowerCamelCase : Any = ["""input_ids""", """attention_mask"""] __lowerCamelCase : List[int] = [] def __init__( self , snake_case__=None , snake_case__=None , snake_case__="<unk>" , snake_case__="<s>" , snake_case__="</s>" , snake_case__="<pad>" , snake_case__="[SEP]" , snake_case__="[MASK]" , snake_case__="[CLS]" , **snake_case__ , ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else bos_token UpperCAmelCase : Optional[int] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else eos_token UpperCAmelCase : List[str] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else unk_token UpperCAmelCase : Union[str, Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else pad_token UpperCAmelCase : int =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else cls_token UpperCAmelCase : str =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else sep_token # Mask token behave like a normal word, i.e. include the space before it UpperCAmelCase : List[Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else mask_token super().__init__( snake_case__ , tokenizer_file=snake_case__ , bos_token=snake_case__ , eos_token=snake_case__ , unk_token=snake_case__ , sep_token=snake_case__ , pad_token=snake_case__ , cls_token=snake_case__ , mask_token=snake_case__ , **snake_case__ , ) UpperCAmelCase : Tuple =vocab_file UpperCAmelCase : Optional[int] =False if not self.vocab_file else True def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : int =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None , snake_case__ = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is None: return [1] + ([0] * len(snake_case__ )) + [1] return [1] + ([0] * len(snake_case__ )) + [1] + ([0] * len(snake_case__ )) + [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : Optional[Any] =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> Tuple[str]: '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(snake_case__ ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCAmelCase : Optional[int] =os.path.join( snake_case__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case__ ): copyfile(self.vocab_file , snake_case__ ) return (out_vocab_file,)
348
0
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase = 200 ) -> int: '''simple docstring''' snake_case_ = [1, 2, 5, 10, 20, 50, 100, 200] snake_case_ = [0] * (pence + 1) snake_case_ = 1 # base case: 1 way to make 0 pence for coin in coins: for i in range(__UpperCAmelCase, pence + 1, 1 ): number_of_ways[i] += number_of_ways[i - coin] return number_of_ways[pence] if __name__ == "__main__": assert solution(200) == 7_3682
56
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def lowerCAmelCase_ ( __lowerCAmelCase )-> Optional[Any]: '''simple docstring''' def is_in_circle(__lowerCAmelCase , __lowerCAmelCase ) -> bool: UpperCAmelCase : List[Any] =sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle UpperCAmelCase : List[Any] =mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(__lowerCAmelCase ) ) # The ratio of the area for circle to square is pi/4. UpperCAmelCase : Dict =proportion * 4 print(f'''The estimated value of pi is {pi_estimate}''' ) print(f'''The numpy value of pi is {pi}''' ) print(f'''The total error is {abs(pi - pi_estimate )}''' ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 , )-> float: '''simple docstring''' return mean( function_to_integrate(uniform(__lowerCAmelCase , __lowerCAmelCase ) ) for _ in range(__lowerCAmelCase ) ) * (max_value - min_value) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase = 0.0 , __lowerCAmelCase = 1.0 )-> None: '''simple docstring''' def identity_function(__lowerCAmelCase ) -> float: return x UpperCAmelCase : List[Any] =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase : Dict =(max_value * max_value - min_value * min_value) / 2 print('''******************''' ) print(f'''Estimating area under y=x where x varies from {min_value} to {max_value}''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {expected_value}''' ) print(f'''Total error is {abs(estimated_value - expected_value )}''' ) print('''******************''' ) def lowerCAmelCase_ ( __lowerCAmelCase )-> None: '''simple docstring''' def function_to_integrate(__lowerCAmelCase ) -> float: return sqrt(4.0 - x * x ) UpperCAmelCase : Dict =area_under_curve_estimator( __lowerCAmelCase , __lowerCAmelCase , 0.0 , 2.0 ) print('''******************''' ) print('''Estimating pi using area_under_curve_estimator''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {pi}''' ) print(f'''Total error is {abs(estimated_value - pi )}''' ) print('''******************''' ) if __name__ == "__main__": import doctest doctest.testmod()
348
0
"""simple docstring""" def _lowerCamelCase ( _UpperCamelCase = 6008_5147_5143 ): '''simple docstring''' try: __lowerCAmelCase = int(_UpperCamelCase ) except (TypeError, ValueError): raise TypeError("Parameter n must be int or castable to int." ) if n <= 0: raise ValueError("Parameter n must be greater than or equal to one." ) __lowerCAmelCase = 2 __lowerCAmelCase = 0 if n == 2: return 2 while n > 2: while n % i != 0: i += 1 __lowerCAmelCase = i while n % i == 0: __lowerCAmelCase = n // i i += 1 return int(_UpperCamelCase ) if __name__ == "__main__": print(f'''{solution() = }''')
57
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __snake_case : def __init__( self , snake_case__ , snake_case__=12 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=0.02 , snake_case__=0 , snake_case__=None , ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : List[Any] =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : Union[str, Any] =use_input_mask UpperCAmelCase : Tuple =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : Tuple =hidden_size UpperCAmelCase : Dict =projection_dim UpperCAmelCase : Optional[int] =num_hidden_layers UpperCAmelCase : Dict =num_attention_heads UpperCAmelCase : int =intermediate_size UpperCAmelCase : Any =dropout UpperCAmelCase : Union[str, Any] =attention_dropout UpperCAmelCase : Union[str, Any] =max_position_embeddings UpperCAmelCase : List[str] =initializer_range UpperCAmelCase : str =scope UpperCAmelCase : str =bos_token_id def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : int =None if self.use_input_mask: UpperCAmelCase : Union[str, Any] =random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: UpperCAmelCase : Optional[int] =input_mask.numpy() UpperCAmelCase , UpperCAmelCase : List[Any] =input_mask.shape UpperCAmelCase : Optional[Any] =np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): UpperCAmelCase : List[Any] =1 UpperCAmelCase : Tuple =0 UpperCAmelCase : List[Any] =self.get_config() return config, input_ids, tf.convert_to_tensor(snake_case__ ) def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def UpperCAmelCase__ ( self , snake_case__ , snake_case__ , snake_case__ ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =TFBlipTextModel(config=snake_case__ ) UpperCAmelCase : List[Any] =model(snake_case__ , attention_mask=snake_case__ , training=snake_case__ ) UpperCAmelCase : str =model(snake_case__ , training=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[str] =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[Any] =config_and_inputs UpperCAmelCase : Optional[int] ={'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __snake_case ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Optional[int] = (TFBlipTextModel,) if is_tf_available() else () __lowerCamelCase : Dict = False __lowerCamelCase : Optional[Any] = False __lowerCamelCase : Dict = False def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : str =BlipTextModelTester(self ) UpperCAmelCase : Optional[int] =ConfigTester(self , config_class=snake_case__ , hidden_size=37 ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass def UpperCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip(reason='''Blip does not use inputs_embeds''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @slow def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase : Optional[Any] =TFBlipTextModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def UpperCAmelCase__ ( self , snake_case__=True ) -> Any: '''simple docstring''' super().test_pt_tf_model_equivalence(allow_missing_keys=snake_case__ )
348
0
'''simple docstring''' import random import unittest import numpy as np import transformers from transformers import is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax if is_flax_available(): import os import jax.numpy as jnp from jax import jit from transformers import AutoTokenizer, FlaxAutoModelForCausalLM from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model lowercase_ = """0.12""" # assumed parallelism: 8 if is_torch_available(): import torch def lowerCamelCase ( __lowerCamelCase : Tuple , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Optional[int]=None ) ->Tuple: if rng is None: _SCREAMING_SNAKE_CASE = random.Random() _SCREAMING_SNAKE_CASE = 1 for dim in shape: total_dims *= dim _SCREAMING_SNAKE_CASE = [] for _ in range(__lowerCamelCase ): values.append(rng.randint(0 , vocab_size - 1 ) ) _SCREAMING_SNAKE_CASE = np.array(__lowerCamelCase , dtype=jnp.intaa ).reshape(__lowerCamelCase ) return output def lowerCamelCase ( __lowerCamelCase : Dict , __lowerCamelCase : Optional[Any]=None ) ->List[Any]: _SCREAMING_SNAKE_CASE = ids_tensor(__lowerCamelCase , vocab_size=2 , rng=__lowerCamelCase ) # make sure that at least one token is attended to for each batch _SCREAMING_SNAKE_CASE = 1 return attn_mask @require_flax class a_ : '''simple docstring''' UpperCamelCase = None UpperCamelCase = () def snake_case_( self ) -> List[Any]: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() # cut to half length & take max batch_size 3 _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = inputs["""input_ids"""].shape[-1] // 2 _SCREAMING_SNAKE_CASE = inputs["""input_ids"""][:max_batch_size, :sequence_length] _SCREAMING_SNAKE_CASE = jnp.ones_like(A ) _SCREAMING_SNAKE_CASE = attention_mask[:max_batch_size, :sequence_length] # generate max 5 tokens _SCREAMING_SNAKE_CASE = input_ids.shape[-1] + 5 if config.eos_token_id is not None and config.pad_token_id is None: # hack to allow generate for models such as GPT2 as is done in `generate()` _SCREAMING_SNAKE_CASE = config.eos_token_id return config, input_ids, attention_mask, max_length @is_pt_flax_cross_test def snake_case_( self ) -> Tuple: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self._get_input_ids_and_config() _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = max_length _SCREAMING_SNAKE_CASE = 0 for model_class in self.all_generative_model_classes: _SCREAMING_SNAKE_CASE = model_class(A ) _SCREAMING_SNAKE_CASE = model_class.__name__[4:] # Skip the "Flax" at the beginning _SCREAMING_SNAKE_CASE = getattr(A , A ) _SCREAMING_SNAKE_CASE = pt_model_class(A ).eval() _SCREAMING_SNAKE_CASE = load_flax_weights_in_pytorch_model(A , flax_model.params ) _SCREAMING_SNAKE_CASE = flax_model.generate(A ).sequences _SCREAMING_SNAKE_CASE = pt_model.generate(torch.tensor(A , dtype=torch.long ) ) if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]: _SCREAMING_SNAKE_CASE = flax_generation_outputs[:, : pt_generation_outputs.shape[-1]] self.assertListEqual(pt_generation_outputs.numpy().tolist() , flax_generation_outputs.tolist() ) def snake_case_( self ) -> str: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self._get_input_ids_and_config() _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = max_length for model_class in self.all_generative_model_classes: _SCREAMING_SNAKE_CASE = model_class(A ) _SCREAMING_SNAKE_CASE = model.generate(A ).sequences self.assertEqual(generation_outputs.shape[-1] , A ) _SCREAMING_SNAKE_CASE = jit(model.generate ) _SCREAMING_SNAKE_CASE = jit_generate(A ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case_( self ) -> List[Any]: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self._get_input_ids_and_config() _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = max_length for model_class in self.all_generative_model_classes: _SCREAMING_SNAKE_CASE = model_class(A ) _SCREAMING_SNAKE_CASE = model.generate(A ).sequences self.assertEqual(generation_outputs.shape[-1] , A ) _SCREAMING_SNAKE_CASE = jit(model.generate ) _SCREAMING_SNAKE_CASE = jit_generate(A ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case_( self ) -> List[str]: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self._get_input_ids_and_config() _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = max_length _SCREAMING_SNAKE_CASE = 2 for model_class in self.all_generative_model_classes: _SCREAMING_SNAKE_CASE = model_class(A ) _SCREAMING_SNAKE_CASE = model.generate(A ).sequences self.assertEqual(generation_outputs.shape[-1] , A ) _SCREAMING_SNAKE_CASE = jit(model.generate ) _SCREAMING_SNAKE_CASE = jit_generate(A ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case_( self ) -> str: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self._get_input_ids_and_config() _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = max_length _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = 2 for model_class in self.all_generative_model_classes: _SCREAMING_SNAKE_CASE = model_class(A ) _SCREAMING_SNAKE_CASE = model.generate(A ).sequences self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences ) def snake_case_( self ) -> Tuple: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self._get_input_ids_and_config() _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = max_length _SCREAMING_SNAKE_CASE = 0.8 _SCREAMING_SNAKE_CASE = 10 _SCREAMING_SNAKE_CASE = 0.3 _SCREAMING_SNAKE_CASE = 1 _SCREAMING_SNAKE_CASE = 8 _SCREAMING_SNAKE_CASE = 9 for model_class in self.all_generative_model_classes: _SCREAMING_SNAKE_CASE = model_class(A ) _SCREAMING_SNAKE_CASE = model.generate(A ).sequences self.assertEqual(generation_outputs.shape[-1] , A ) _SCREAMING_SNAKE_CASE = jit(model.generate ) _SCREAMING_SNAKE_CASE = jit_generate(A ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case_( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self._get_input_ids_and_config() _SCREAMING_SNAKE_CASE = max_length _SCREAMING_SNAKE_CASE = 1 _SCREAMING_SNAKE_CASE = 8 _SCREAMING_SNAKE_CASE = 9 for model_class in self.all_generative_model_classes: _SCREAMING_SNAKE_CASE = model_class(A ) _SCREAMING_SNAKE_CASE = model.generate(A ).sequences self.assertEqual(generation_outputs.shape[-1] , A ) _SCREAMING_SNAKE_CASE = jit(model.generate ) _SCREAMING_SNAKE_CASE = jit_generate(A ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case_( self ) -> Any: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self._get_input_ids_and_config() _SCREAMING_SNAKE_CASE = max_length _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = 1 _SCREAMING_SNAKE_CASE = 8 _SCREAMING_SNAKE_CASE = 9 for model_class in self.all_generative_model_classes: _SCREAMING_SNAKE_CASE = model_class(A ) _SCREAMING_SNAKE_CASE = model.generate(A ).sequences self.assertEqual(generation_outputs.shape[-1] , A ) _SCREAMING_SNAKE_CASE = jit(model.generate ) _SCREAMING_SNAKE_CASE = jit_generate(A ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case_( self ) -> str: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self._get_input_ids_and_config() # pad attention mask on the left _SCREAMING_SNAKE_CASE = attention_mask.at[(0, 0)].set(0 ) _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = max_length for model_class in self.all_generative_model_classes: _SCREAMING_SNAKE_CASE = model_class(A ) _SCREAMING_SNAKE_CASE = model.generate(A , attention_mask=A ).sequences self.assertEqual(generation_outputs.shape[-1] , A ) _SCREAMING_SNAKE_CASE = jit(model.generate ) _SCREAMING_SNAKE_CASE = jit_generate(A , attention_mask=A ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case_( self ) -> int: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self._get_input_ids_and_config() # pad attention mask on the left _SCREAMING_SNAKE_CASE = attention_mask.at[(0, 0)].set(0 ) _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = max_length for model_class in self.all_generative_model_classes: _SCREAMING_SNAKE_CASE = model_class(A ) _SCREAMING_SNAKE_CASE = model.generate(A , attention_mask=A ).sequences self.assertEqual(generation_outputs.shape[-1] , A ) _SCREAMING_SNAKE_CASE = jit(model.generate ) _SCREAMING_SNAKE_CASE = jit_generate(A , attention_mask=A ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case_( self ) -> Tuple: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self._get_input_ids_and_config() # pad attention mask on the left _SCREAMING_SNAKE_CASE = attention_mask.at[(0, 0)].set(0 ) _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = max_length for model_class in self.all_generative_model_classes: _SCREAMING_SNAKE_CASE = model_class(A ) _SCREAMING_SNAKE_CASE = model.generate(A , attention_mask=A ).sequences self.assertEqual(generation_outputs.shape[-1] , A ) _SCREAMING_SNAKE_CASE = jit(model.generate ) _SCREAMING_SNAKE_CASE = jit_generate(A , attention_mask=A ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) @require_flax class a_ ( unittest.TestCase ): '''simple docstring''' def snake_case_( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-bert""" ) _SCREAMING_SNAKE_CASE = FlaxAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-bert-flax-only""" ) _SCREAMING_SNAKE_CASE = """Hello world""" _SCREAMING_SNAKE_CASE = tokenizer(A , return_tensors="""np""" ).input_ids # typos are quickly detected (the correct argument is `do_sample`) with self.assertRaisesRegex(A , """do_samples""" ): model.generate(A , do_samples=A ) # arbitrary arguments that will not be used anywhere are also not accepted with self.assertRaisesRegex(A , """foo""" ): _SCREAMING_SNAKE_CASE = {"""foo""": """bar"""} model.generate(A , **A )
58
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging __snake_case = logging.get_logger(__name__) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> str: '''simple docstring''' UpperCAmelCase : Dict =nn.functional.normalize(__lowerCAmelCase ) UpperCAmelCase : Tuple =nn.functional.normalize(__lowerCAmelCase ) return torch.mm(__lowerCAmelCase , normalized_text_embeds.t() ) class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : List[str] = CLIPConfig __lowerCamelCase : List[Any] = ["""CLIPEncoderLayer"""] def __init__( self , snake_case__ ) -> Dict: '''simple docstring''' super().__init__(snake_case__ ) UpperCAmelCase : Dict =CLIPVisionModel(config.vision_config ) UpperCAmelCase : Optional[Any] =nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=snake_case__ ) UpperCAmelCase : int =nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : List[str] =nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=snake_case__ ) UpperCAmelCase : str =nn.Parameter(torch.ones(17 ) , requires_grad=snake_case__ ) UpperCAmelCase : Optional[int] =nn.Parameter(torch.ones(3 ) , requires_grad=snake_case__ ) @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Union[str, Any] =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : Optional[Any] =self.visual_projection(snake_case__ ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 UpperCAmelCase : List[str] =cosine_distance(snake_case__ , self.special_care_embeds ).cpu().float().numpy() UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ).cpu().float().numpy() UpperCAmelCase : Tuple =[] UpperCAmelCase : Dict =image_embeds.shape[0] for i in range(snake_case__ ): UpperCAmelCase : str ={'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : str =0.0 for concept_idx in range(len(special_cos_dist[0] ) ): UpperCAmelCase : Optional[Any] =special_cos_dist[i][concept_idx] UpperCAmelCase : Union[str, Any] =self.special_care_embeds_weights[concept_idx].item() UpperCAmelCase : str =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} ) UpperCAmelCase : int =0.01 for concept_idx in range(len(cos_dist[0] ) ): UpperCAmelCase : Any =cos_dist[i][concept_idx] UpperCAmelCase : Optional[int] =self.concept_embeds_weights[concept_idx].item() UpperCAmelCase : int =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(snake_case__ ) result.append(snake_case__ ) UpperCAmelCase : Optional[int] =[len(res['''bad_concepts'''] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> Tuple: '''simple docstring''' UpperCAmelCase : Any =self.vision_model(snake_case__ )[1] # pooled_output UpperCAmelCase : List[str] =self.visual_projection(snake_case__ ) UpperCAmelCase : Any =cosine_distance(snake_case__ , self.special_care_embeds ) UpperCAmelCase : Optional[Any] =cosine_distance(snake_case__ , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images UpperCAmelCase : Optional[Any] =0.0 UpperCAmelCase : Any =special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) UpperCAmelCase : str =torch.any(special_scores > 0 , dim=1 ) UpperCAmelCase : List[Any] =special_care * 0.01 UpperCAmelCase : Union[str, Any] =special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) UpperCAmelCase : List[Any] =(cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) UpperCAmelCase : str =torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
348
0
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 UpperCamelCase ( __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Dict ): snake_case : int = k_size // 2 snake_case , snake_case : Tuple = mgrid[0 - center : k_size - center, 0 - center : k_size - center] snake_case : Optional[int] = 1 / (2 * pi * sigma) * exp(-(square(__lowerCamelCase ) + square(__lowerCamelCase )) / (2 * square(__lowerCamelCase )) ) return g def UpperCamelCase ( __lowerCamelCase : Any , __lowerCamelCase : Any , __lowerCamelCase : Dict ): snake_case , snake_case : List[str] = image.shape[0], image.shape[1] # dst image height and width snake_case : Union[str, Any] = height - k_size + 1 snake_case : Any = width - k_size + 1 # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows snake_case : Optional[int] = zeros((dst_height * dst_width, k_size * k_size) ) snake_case : List[str] = 0 for i, j in product(range(__lowerCamelCase ) , range(__lowerCamelCase ) ): snake_case : Tuple = ravel(image[i : i + k_size, j : j + k_size] ) snake_case : List[Any] = window row += 1 # turn the kernel into shape(k*k, 1) snake_case : Tuple = gen_gaussian_kernel(__lowerCamelCase , __lowerCamelCase ) snake_case : List[str] = ravel(__lowerCamelCase ) # reshape and get the dst image snake_case : int = dot(__lowerCamelCase , __lowerCamelCase ).reshape(__lowerCamelCase , __lowerCamelCase ).astype(__lowerCamelCase ) return dst if __name__ == "__main__": # read original image __lowerCamelCase = imread(R"""../image_data/lena.jpg""") # turn image in gray scale value __lowerCamelCase = cvtColor(img, COLOR_BGR2GRAY) # get values with two different mask size __lowerCamelCase = gaussian_filter(gray, 3, sigma=1) __lowerCamelCase = 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()
59
import argparse import intel_extension_for_pytorch as ipex import torch from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline __snake_case = argparse.ArgumentParser('''Stable Diffusion script with intel optimization''', add_help=False) parser.add_argument('''--dpm''', action='''store_true''', help='''Enable DPMSolver or not''') parser.add_argument('''--steps''', default=None, type=int, help='''Num inference steps''') __snake_case = parser.parse_args() __snake_case = '''cpu''' __snake_case = '''a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings''' __snake_case = '''path-to-your-trained-model''' __snake_case = StableDiffusionPipeline.from_pretrained(model_id) if args.dpm: __snake_case = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) __snake_case = pipe.to(device) # to channels last __snake_case = pipe.unet.to(memory_format=torch.channels_last) __snake_case = pipe.vae.to(memory_format=torch.channels_last) __snake_case = pipe.text_encoder.to(memory_format=torch.channels_last) if pipe.requires_safety_checker: __snake_case = pipe.safety_checker.to(memory_format=torch.channels_last) # optimize with ipex __snake_case = torch.randn(2, 4, 64, 64) __snake_case = torch.rand(1) * 9_99 __snake_case = torch.randn(2, 77, 7_68) __snake_case = (sample, timestep, encoder_hidden_status) try: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example) except Exception: __snake_case = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True) __snake_case = ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True) if pipe.requires_safety_checker: __snake_case = ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True) # compute __snake_case = 6_66 __snake_case = torch.Generator(device).manual_seed(seed) __snake_case = {'''generator''': generator} if args.steps is not None: __snake_case = args.steps with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa): __snake_case = pipe(prompt, **generate_kwargs).images[0] # save image image.save('''generated.png''')
348
0
"""simple docstring""" # limitations under the License. from typing import Optional, Tuple, Union import torch from diffusers import DiffusionPipeline, ImagePipelineOutput class snake_case_( a__ ): def __init__( self : List[str] , UpperCamelCase_ : List[Any] , UpperCamelCase_ : List[Any] ): super().__init__() self.register_modules(unet=UpperCamelCase_ , scheduler=UpperCamelCase_ ) @torch.no_grad() def __call__( self : Tuple , UpperCamelCase_ : int = 1 , UpperCamelCase_ : Optional[torch.Generator] = None , UpperCamelCase_ : int = 5_0 , UpperCamelCase_ : Optional[str] = "pil" , UpperCamelCase_ : bool = True , **UpperCamelCase_ : str , ): lowerCAmelCase : Dict = torch.randn( (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size) , generator=UpperCamelCase_ , ) lowerCAmelCase : int = image.to(self.device ) # set step values self.scheduler.set_timesteps(UpperCamelCase_ ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output lowerCAmelCase : int = self.unet(UpperCamelCase_ , UpperCamelCase_ ).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 lowerCAmelCase : List[Any] = self.scheduler.step(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ).prev_sample lowerCAmelCase : List[str] = (image / 2 + 0.5).clamp(0 , 1 ) lowerCAmelCase : Any = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": lowerCAmelCase : Union[str, Any] = self.numpy_to_pil(UpperCamelCase_ ) if not return_dict: return (image,), "This is a local test" return ImagePipelineOutput(images=UpperCamelCase_ ), "This is a local test"
60
__snake_case = '''Input must be a string of 8 numbers plus letter''' __snake_case = '''TRWAGMYFPDXBNJZSQVHLCKE''' def lowerCAmelCase_ ( __lowerCAmelCase )-> bool: '''simple docstring''' if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCAmelCase : Optional[Any] =f'''Expected string as input, found {type(__lowerCAmelCase ).__name__}''' raise TypeError(__lowerCAmelCase ) UpperCAmelCase : List[Any] =spanish_id.replace('''-''' , '''''' ).upper() if len(__lowerCAmelCase ) != 9: raise ValueError(__lowerCAmelCase ) try: UpperCAmelCase : int =int(spanish_id_clean[0:8] ) UpperCAmelCase : Optional[int] =spanish_id_clean[8] except ValueError as ex: raise ValueError(__lowerCAmelCase ) from ex if letter.isdigit(): raise ValueError(__lowerCAmelCase ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
348
0