code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
from __future__ import annotations import typing from collections.abc import Iterable import numpy as np UpperCAmelCase__ = typing.Union[Iterable[float], Iterable[int], np.ndarray] # noqa: UP007 UpperCAmelCase__ = typing.Union[np.floataa, int, float] # noqa: UP007 def _a ( a :Vector , a :Vector ) -> VectorOut: return np.sqrt(np.sum((np.asarray(a ) - np.asarray(a )) ** 2 ) ) def _a ( a :Vector , a :Vector ) -> VectorOut: return sum((va - va) ** 2 for va, va in zip(a , a ) ) ** (1 / 2) if __name__ == "__main__": def _a ( ) -> None: from timeit import timeit print('''Without Numpy''' ) print( timeit( '''euclidean_distance_no_np([1, 2, 3], [4, 5, 6])''' , number=10_000 , globals=globals() , ) ) print('''With Numpy''' ) print( timeit( '''euclidean_distance([1, 2, 3], [4, 5, 6])''' , number=10_000 , globals=globals() , ) ) benchmark()
26
def _a ( a :list ) -> list: if len(a ) <= 1: return lst a = 1 while i < len(a ): if lst[i - 1] <= lst[i]: i += 1 else: a , a = lst[i], lst[i - 1] i -= 1 if i == 0: a = 1 return lst if __name__ == "__main__": UpperCAmelCase__ = input("Enter numbers separated by a comma:\n").strip() UpperCAmelCase__ = [int(item) for item in user_input.split(",")] print(gnome_sort(unsorted))
26
1
import argparse from collections import OrderedDict from pathlib import Path import torch from transformers import ( VisualBertConfig, VisualBertForMultipleChoice, VisualBertForPreTraining, VisualBertForQuestionAnswering, VisualBertForVisualReasoning, ) from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = [ ("bert.bert", "visual_bert"), ("bert.cls", "cls"), ("bert.classifier", "cls"), ("token_type_embeddings_visual", "visual_token_type_embeddings"), ("position_embeddings_visual", "visual_position_embeddings"), ("projection", "visual_projection"), ] UpperCAmelCase__ = [ "nlvr2_coco_pre_trained.th", "nlvr2_fine_tuned.th", "nlvr2_pre_trained.th", "vcr_coco_pre_train.th", "vcr_fine_tune.th", "vcr_pre_train.th", "vqa_coco_pre_trained.th", "vqa_fine_tuned.th", "vqa_pre_trained.th", ] def _a ( a :Any ) -> Optional[int]: a = torch.load(a , map_location='''cpu''' ) return sd def _a ( a :Tuple , a :Dict , a :Any=rename_keys_prefix ) -> Union[str, Any]: a = OrderedDict() a = torch.arange(config.max_position_embeddings ).expand((1, -1) ) # detector_d = OrderedDict() for key in d: if "detector" in key: # detector_d[key.replace('detector.','')] = d[key] continue a = key for name_pair in rename_keys_prefix: a = new_key.replace(name_pair[0] , name_pair[1] ) a = d[key] if key == "bert.cls.predictions.decoder.weight": # Old bert code didn't have `decoder.bias`, but was added separately a = new_d['''cls.predictions.bias'''] return new_d @torch.no_grad() def _a ( a :Optional[int] , a :int ) -> str: assert ( checkpoint_path.split('''/''' )[-1] in ACCEPTABLE_CHECKPOINTS ), F"""The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.""" # Get Config if "pre" in checkpoint_path: a = '''pretraining''' if "vcr" in checkpoint_path: a = {'''visual_embedding_dim''': 512} elif "vqa_advanced" in checkpoint_path: a = {'''visual_embedding_dim''': 2_048} elif "vqa" in checkpoint_path: a = {'''visual_embedding_dim''': 2_048} elif "nlvr" in checkpoint_path: a = {'''visual_embedding_dim''': 1_024} else: raise NotImplementedError(F"""No implementation found for `{checkpoint_path}`.""" ) else: if "vcr" in checkpoint_path: a = {'''visual_embedding_dim''': 512} a = '''multichoice''' elif "vqa_advanced" in checkpoint_path: a = {'''visual_embedding_dim''': 2_048} a = '''vqa_advanced''' elif "vqa" in checkpoint_path: a = {'''visual_embedding_dim''': 2_048, '''num_labels''': 3_129} a = '''vqa''' elif "nlvr" in checkpoint_path: a = { '''visual_embedding_dim''': 1_024, '''num_labels''': 2, } a = '''nlvr''' a = VisualBertConfig(**a ) # Load State Dict a = load_state_dict(a ) a = get_new_dict(a , a ) if model_type == "pretraining": a = VisualBertForPreTraining(a ) elif model_type == "vqa": a = VisualBertForQuestionAnswering(a ) elif model_type == "nlvr": a = VisualBertForVisualReasoning(a ) elif model_type == "multichoice": a = VisualBertForMultipleChoice(a ) model.load_state_dict(a ) # Save Checkpoints Path(a ).mkdir(exist_ok=a ) model.save_pretrained(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument("orig_checkpoint_path", type=str, help="A path to .th on local filesystem.") parser.add_argument("pytorch_dump_folder_path", type=str, help="Path to the output PyTorch model.") UpperCAmelCase__ = parser.parse_args() convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)
26
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCAmelCase__ = { "configuration_deberta": ["DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP", "DebertaConfig", "DebertaOnnxConfig"], "tokenization_deberta": ["DebertaTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ["DebertaTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "DebertaForMaskedLM", "DebertaForQuestionAnswering", "DebertaForSequenceClassification", "DebertaForTokenClassification", "DebertaModel", "DebertaPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "TFDebertaForMaskedLM", "TFDebertaForQuestionAnswering", "TFDebertaForSequenceClassification", "TFDebertaForTokenClassification", "TFDebertaModel", "TFDebertaPreTrainedModel", ] if TYPE_CHECKING: from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig, DebertaOnnxConfig from .tokenization_deberta import DebertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_deberta_fast import DebertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deberta import ( DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, DebertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deberta import ( TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFDebertaForMaskedLM, TFDebertaForQuestionAnswering, TFDebertaForSequenceClassification, TFDebertaForTokenClassification, TFDebertaModel, TFDebertaPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
1
from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, flip_channel_order, get_resize_output_image_size, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging if is_vision_available(): import PIL if is_torch_available(): import torch UpperCAmelCase__ = logging.get_logger(__name__) class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = ['''pixel_values'''] def __init__( self : Optional[Any] , __UpperCAmelCase : bool = True , __UpperCAmelCase : Dict[str, int] = None , __UpperCAmelCase : PILImageResampling = PILImageResampling.BILINEAR , __UpperCAmelCase : bool = True , __UpperCAmelCase : Union[int, float] = 1 / 255 , __UpperCAmelCase : bool = True , __UpperCAmelCase : Dict[str, int] = None , __UpperCAmelCase : bool = True , **__UpperCAmelCase : Tuple , ) ->None: """simple docstring""" super().__init__(**__UpperCAmelCase ) a = size if size is not None else {'''shortest_edge''': 224} a = get_size_dict(__UpperCAmelCase , default_to_square=__UpperCAmelCase ) a = crop_size if crop_size is not None else {'''height''': 256, '''width''': 256} a = get_size_dict(__UpperCAmelCase , param_name='''crop_size''' ) a = do_resize a = size a = resample a = do_rescale a = rescale_factor a = do_center_crop a = crop_size a = do_flip_channel_order def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : np.ndarray , __UpperCAmelCase : Dict[str, int] , __UpperCAmelCase : PILImageResampling = PIL.Image.BILINEAR , __UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__UpperCAmelCase : List[Any] , ) ->np.ndarray: """simple docstring""" a = get_size_dict(__UpperCAmelCase , default_to_square=__UpperCAmelCase ) if "shortest_edge" not in size: raise ValueError(F"""The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}""" ) a = get_resize_output_image_size(__UpperCAmelCase , size=size['''shortest_edge'''] , default_to_square=__UpperCAmelCase ) return resize(__UpperCAmelCase , size=__UpperCAmelCase , resample=__UpperCAmelCase , data_format=__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : np.ndarray , __UpperCAmelCase : Dict[str, int] , __UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__UpperCAmelCase : Any , ) ->np.ndarray: """simple docstring""" a = get_size_dict(__UpperCAmelCase ) if "height" not in size or "width" not in size: raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}""" ) return center_crop(__UpperCAmelCase , size=(size['''height'''], size['''width''']) , data_format=__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : str , __UpperCAmelCase : np.ndarray , __UpperCAmelCase : Union[int, float] , __UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__UpperCAmelCase : Optional[int] , ) ->str: """simple docstring""" return rescale(__UpperCAmelCase , scale=__UpperCAmelCase , data_format=__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : np.ndarray , __UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None ) ->np.ndarray: """simple docstring""" return flip_channel_order(__UpperCAmelCase , data_format=__UpperCAmelCase ) def __lowerCAmelCase ( self : Any , __UpperCAmelCase : ImageInput , __UpperCAmelCase : bool = None , __UpperCAmelCase : Dict[str, int] = None , __UpperCAmelCase : PILImageResampling = None , __UpperCAmelCase : bool = None , __UpperCAmelCase : float = None , __UpperCAmelCase : bool = None , __UpperCAmelCase : Dict[str, int] = None , __UpperCAmelCase : bool = None , __UpperCAmelCase : Optional[Union[str, TensorType]] = None , __UpperCAmelCase : ChannelDimension = ChannelDimension.FIRST , **__UpperCAmelCase : int , ) ->PIL.Image.Image: """simple docstring""" a = do_resize if do_resize is not None else self.do_resize a = resample if resample is not None else self.resample a = do_rescale if do_rescale is not None else self.do_rescale a = rescale_factor if rescale_factor is not None else self.rescale_factor a = do_center_crop if do_center_crop is not None else self.do_center_crop a = ( do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order ) a = size if size is not None else self.size a = get_size_dict(__UpperCAmelCase , default_to_square=__UpperCAmelCase ) a = crop_size if crop_size is not None else self.crop_size a = get_size_dict(__UpperCAmelCase , param_name='''crop_size''' ) a = make_list_of_images(__UpperCAmelCase ) if not valid_images(__UpperCAmelCase ): 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_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) # All transformations expect numpy arrays. a = [to_numpy_array(__UpperCAmelCase ) for image in images] if do_resize: a = [self.resize(image=__UpperCAmelCase , size=__UpperCAmelCase , resample=__UpperCAmelCase ) for image in images] if do_center_crop: a = [self.center_crop(image=__UpperCAmelCase , size=__UpperCAmelCase ) for image in images] if do_rescale: a = [self.rescale(image=__UpperCAmelCase , scale=__UpperCAmelCase ) for image in images] # the pretrained checkpoints assume images are BGR, not RGB if do_flip_channel_order: a = [self.flip_channel_order(image=__UpperCAmelCase ) for image in images] a = [to_channel_dimension_format(__UpperCAmelCase , __UpperCAmelCase ) for image in images] a = {'''pixel_values''': images} return BatchFeature(data=__UpperCAmelCase , tensor_type=__UpperCAmelCase ) def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : int , __UpperCAmelCase : List[Tuple] = None ) ->List[str]: """simple docstring""" a = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(__UpperCAmelCase ) != len(__UpperCAmelCase ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(__UpperCAmelCase ): a = target_sizes.numpy() a = [] for idx in range(len(__UpperCAmelCase ) ): a = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='''bilinear''' , align_corners=__UpperCAmelCase ) a = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(__UpperCAmelCase ) else: a = logits.argmax(dim=1 ) a = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
26
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all feature extractors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...feature_extraction_utils import FeatureExtractionMixin from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_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, ) UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = OrderedDict( [ ("audio-spectrogram-transformer", "ASTFeatureExtractor"), ("beit", "BeitFeatureExtractor"), ("chinese_clip", "ChineseCLIPFeatureExtractor"), ("clap", "ClapFeatureExtractor"), ("clip", "CLIPFeatureExtractor"), ("clipseg", "ViTFeatureExtractor"), ("conditional_detr", "ConditionalDetrFeatureExtractor"), ("convnext", "ConvNextFeatureExtractor"), ("cvt", "ConvNextFeatureExtractor"), ("data2vec-audio", "Wav2Vec2FeatureExtractor"), ("data2vec-vision", "BeitFeatureExtractor"), ("deformable_detr", "DeformableDetrFeatureExtractor"), ("deit", "DeiTFeatureExtractor"), ("detr", "DetrFeatureExtractor"), ("dinat", "ViTFeatureExtractor"), ("donut-swin", "DonutFeatureExtractor"), ("dpt", "DPTFeatureExtractor"), ("encodec", "EncodecFeatureExtractor"), ("flava", "FlavaFeatureExtractor"), ("glpn", "GLPNFeatureExtractor"), ("groupvit", "CLIPFeatureExtractor"), ("hubert", "Wav2Vec2FeatureExtractor"), ("imagegpt", "ImageGPTFeatureExtractor"), ("layoutlmv2", "LayoutLMv2FeatureExtractor"), ("layoutlmv3", "LayoutLMv3FeatureExtractor"), ("levit", "LevitFeatureExtractor"), ("maskformer", "MaskFormerFeatureExtractor"), ("mctct", "MCTCTFeatureExtractor"), ("mobilenet_v1", "MobileNetV1FeatureExtractor"), ("mobilenet_v2", "MobileNetV2FeatureExtractor"), ("mobilevit", "MobileViTFeatureExtractor"), ("nat", "ViTFeatureExtractor"), ("owlvit", "OwlViTFeatureExtractor"), ("perceiver", "PerceiverFeatureExtractor"), ("poolformer", "PoolFormerFeatureExtractor"), ("regnet", "ConvNextFeatureExtractor"), ("resnet", "ConvNextFeatureExtractor"), ("segformer", "SegformerFeatureExtractor"), ("sew", "Wav2Vec2FeatureExtractor"), ("sew-d", "Wav2Vec2FeatureExtractor"), ("speech_to_text", "Speech2TextFeatureExtractor"), ("speecht5", "SpeechT5FeatureExtractor"), ("swiftformer", "ViTFeatureExtractor"), ("swin", "ViTFeatureExtractor"), ("swinv2", "ViTFeatureExtractor"), ("table-transformer", "DetrFeatureExtractor"), ("timesformer", "VideoMAEFeatureExtractor"), ("tvlt", "TvltFeatureExtractor"), ("unispeech", "Wav2Vec2FeatureExtractor"), ("unispeech-sat", "Wav2Vec2FeatureExtractor"), ("van", "ConvNextFeatureExtractor"), ("videomae", "VideoMAEFeatureExtractor"), ("vilt", "ViltFeatureExtractor"), ("vit", "ViTFeatureExtractor"), ("vit_mae", "ViTFeatureExtractor"), ("vit_msn", "ViTFeatureExtractor"), ("wav2vec2", "Wav2Vec2FeatureExtractor"), ("wav2vec2-conformer", "Wav2Vec2FeatureExtractor"), ("wavlm", "Wav2Vec2FeatureExtractor"), ("whisper", "WhisperFeatureExtractor"), ("xclip", "CLIPFeatureExtractor"), ("yolos", "YolosFeatureExtractor"), ] ) UpperCAmelCase__ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES) def _a ( a :str ) -> Any: for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items(): if class_name in extractors: a = model_type_to_module_name(a ) a = importlib.import_module(F""".{module_name}""" , '''transformers.models''' ) try: return getattr(a , a ) except AttributeError: continue for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items(): if getattr(a , '''__name__''' , a ) == 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. a = importlib.import_module('''transformers''' ) if hasattr(a , a ): return getattr(a , a ) return None def _a ( a :Union[str, os.PathLike] , a :Optional[Union[str, os.PathLike]] = None , a :bool = False , a :bool = False , a :Optional[Dict[str, str]] = None , a :Optional[Union[bool, str]] = None , a :Optional[str] = None , a :bool = False , **a :int , ) -> Tuple: a = get_file_from_repo( a , a , cache_dir=a , force_download=a , resume_download=a , proxies=a , use_auth_token=a , revision=a , local_files_only=a , ) if resolved_config_file is None: logger.info( '''Could not locate the feature extractor configuration file, will try to use the model config instead.''' ) return {} with open(a , encoding='''utf-8''' ) as reader: return json.load(a ) class lowercase_ : '''simple docstring''' def __init__( self : Tuple ) ->int: """simple docstring""" raise EnvironmentError( '''AutoFeatureExtractor is designed to be instantiated ''' '''using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.''' ) @classmethod @replace_list_option_in_docstrings(__UpperCAmelCase ) def __lowerCAmelCase ( cls : int , __UpperCAmelCase : Optional[Any] , **__UpperCAmelCase : Dict ) ->List[Any]: """simple docstring""" a = kwargs.pop('''config''' , __UpperCAmelCase ) a = kwargs.pop('''trust_remote_code''' , __UpperCAmelCase ) a = True a , a = FeatureExtractionMixin.get_feature_extractor_dict(__UpperCAmelCase , **__UpperCAmelCase ) a = config_dict.get('''feature_extractor_type''' , __UpperCAmelCase ) a = None if "AutoFeatureExtractor" in config_dict.get('''auto_map''' , {} ): a = config_dict['''auto_map''']['''AutoFeatureExtractor'''] # If we don't find the feature extractor class in the feature extractor config, let's try the model config. if feature_extractor_class is None and feature_extractor_auto_map is None: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = AutoConfig.from_pretrained(__UpperCAmelCase , **__UpperCAmelCase ) # It could be in `config.feature_extractor_type`` a = getattr(__UpperCAmelCase , '''feature_extractor_type''' , __UpperCAmelCase ) if hasattr(__UpperCAmelCase , '''auto_map''' ) and "AutoFeatureExtractor" in config.auto_map: a = config.auto_map['''AutoFeatureExtractor'''] if feature_extractor_class is not None: a = feature_extractor_class_from_name(__UpperCAmelCase ) a = feature_extractor_auto_map is not None a = feature_extractor_class is not None or type(__UpperCAmelCase ) in FEATURE_EXTRACTOR_MAPPING a = resolve_trust_remote_code( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) if has_remote_code and trust_remote_code: a = get_class_from_dynamic_module( __UpperCAmelCase , __UpperCAmelCase , **__UpperCAmelCase ) a = kwargs.pop('''code_revision''' , __UpperCAmelCase ) if os.path.isdir(__UpperCAmelCase ): feature_extractor_class.register_for_auto_class() return feature_extractor_class.from_dict(__UpperCAmelCase , **__UpperCAmelCase ) elif feature_extractor_class is not None: return feature_extractor_class.from_dict(__UpperCAmelCase , **__UpperCAmelCase ) # Last try: we use the FEATURE_EXTRACTOR_MAPPING. elif type(__UpperCAmelCase ) in FEATURE_EXTRACTOR_MAPPING: a = FEATURE_EXTRACTOR_MAPPING[type(__UpperCAmelCase )] return feature_extractor_class.from_dict(__UpperCAmelCase , **__UpperCAmelCase ) raise ValueError( F"""Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a """ F"""`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following """ F"""`model_type` keys in its {CONFIG_NAME}: {', '.join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys() )}""" ) @staticmethod def __lowerCAmelCase ( __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Tuple ) ->Optional[int]: """simple docstring""" FEATURE_EXTRACTOR_MAPPING.register(__UpperCAmelCase , __UpperCAmelCase )
26
1
from collections.abc import Iterable from typing import Any class lowercase_ : '''simple docstring''' def __init__( self : List[Any] , __UpperCAmelCase : int | None = None ) ->Optional[Any]: """simple docstring""" a = value a = None # Added in order to delete a node easier a = None a = None def __repr__( self : int ) ->str: """simple docstring""" from pprint import pformat if self.left is None and self.right is None: return str(self.value ) return pformat({F"""{self.value}""": (self.left, self.right)} , indent=1 ) class lowercase_ : '''simple docstring''' def __init__( self : Optional[Any] , __UpperCAmelCase : Node | None = None ) ->str: """simple docstring""" a = root def __str__( self : Dict ) ->str: """simple docstring""" return str(self.root ) def __lowerCAmelCase ( self : str , __UpperCAmelCase : Node , __UpperCAmelCase : Node | None ) ->None: """simple docstring""" if new_children is not None: # reset its kids a = node.parent if node.parent is not None: # reset its parent if self.is_right(__UpperCAmelCase ): # If it is the right children a = new_children else: a = new_children else: a = new_children def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Node ) ->bool: """simple docstring""" if node.parent and node.parent.right: return node == node.parent.right return False def __lowerCAmelCase ( self : Optional[int] ) ->bool: """simple docstring""" return self.root is None def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Dict ) ->None: """simple docstring""" a = Node(__UpperCAmelCase ) # create a new Node if self.empty(): # if Tree is empty a = new_node # set its root else: # Tree is not empty a = self.root # from root if parent_node is None: return while True: # While we don't get to a leaf if value < parent_node.value: # We go left if parent_node.left is None: a = new_node # We insert the new node in a leaf break else: a = parent_node.left else: if parent_node.right is None: a = new_node break else: a = parent_node.right a = parent_node def __lowerCAmelCase ( self : List[Any] , *__UpperCAmelCase : str ) ->None: """simple docstring""" for value in values: self.__insert(__UpperCAmelCase ) def __lowerCAmelCase ( self : int , __UpperCAmelCase : List[str] ) ->Node | None: """simple docstring""" if self.empty(): raise IndexError('''Warning: Tree is empty! please use another.''' ) else: a = self.root # use lazy evaluation here to avoid NoneType Attribute error while node is not None and node.value is not value: a = node.left if value < node.value else node.right return node def __lowerCAmelCase ( self : Any , __UpperCAmelCase : Node | None = None ) ->Node | None: """simple docstring""" if node is None: if self.root is None: return None a = self.root if not self.empty(): while node.right is not None: a = node.right return node def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : Node | None = None ) ->Node | None: """simple docstring""" if node is None: a = self.root if self.root is None: return None if not self.empty(): a = self.root while node.left is not None: a = node.left return node def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : int ) ->None: """simple docstring""" a = self.search(__UpperCAmelCase ) # Look for the node with that label if node is not None: if node.left is None and node.right is None: # If it has no children self.__reassign_nodes(__UpperCAmelCase , __UpperCAmelCase ) elif node.left is None: # Has only right children self.__reassign_nodes(__UpperCAmelCase , node.right ) elif node.right is None: # Has only left children self.__reassign_nodes(__UpperCAmelCase , node.left ) else: a = self.get_max( node.left ) # Gets the max value of the left branch self.remove(tmp_node.value ) # type: ignore a = ( tmp_node.value # type: ignore ) # Assigns the value to the node to delete and keep tree structure def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Node | None ) ->Iterable: """simple docstring""" if node is not None: yield node # Preorder Traversal yield from self.preorder_traverse(node.left ) yield from self.preorder_traverse(node.right ) def __lowerCAmelCase ( self : Any , __UpperCAmelCase : str=None ) ->Any: """simple docstring""" if traversal_function is None: return self.preorder_traverse(self.root ) else: return traversal_function(self.root ) def __lowerCAmelCase ( self : str , __UpperCAmelCase : list , __UpperCAmelCase : Node | None ) ->None: """simple docstring""" if node: self.inorder(__UpperCAmelCase , node.left ) arr.append(node.value ) self.inorder(__UpperCAmelCase , node.right ) def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : int , __UpperCAmelCase : Node ) ->int: """simple docstring""" a = [] self.inorder(__UpperCAmelCase , __UpperCAmelCase ) # append all values to list using inorder traversal return arr[k - 1] def _a ( a :Node | None ) -> list[Node]: a = [] if curr_node is not None: a = postorder(curr_node.left ) + postorder(curr_node.right ) + [curr_node] return node_list def _a ( ) -> None: a = (8, 3, 6, 1, 10, 14, 13, 4, 7) a = BinarySearchTree() for i in testlist: t.insert(a ) # Prints all the elements of the list in order traversal print(a ) if t.search(6 ) is not None: print('''The value 6 exists''' ) else: print('''The value 6 doesn\'t exist''' ) if t.search(-1 ) is not None: print('''The value -1 exists''' ) else: print('''The value -1 doesn\'t exist''' ) if not t.empty(): print('''Max Value: ''' , t.get_max().value ) # type: ignore print('''Min Value: ''' , t.get_min().value ) # type: ignore for i in testlist: t.remove(a ) print(a ) if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
26
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import ( AutoProcessor, BertTokenizerFast, BlipImageProcessor, GPTaTokenizer, InstructBlipProcessor, PreTrainedTokenizerFast, ) @require_vision class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self : Optional[int] ) ->Tuple: """simple docstring""" a = tempfile.mkdtemp() a = BlipImageProcessor() a = GPTaTokenizer.from_pretrained('''hf-internal-testing/tiny-random-GPT2Model''' ) a = BertTokenizerFast.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) a = InstructBlipProcessor(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) processor.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self : Optional[Any] , **__UpperCAmelCase : Tuple ) ->List[str]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).tokenizer def __lowerCAmelCase ( self : int , **__UpperCAmelCase : str ) ->List[str]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).image_processor def __lowerCAmelCase ( self : Optional[Any] , **__UpperCAmelCase : Any ) ->Optional[Any]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).qformer_tokenizer def __lowerCAmelCase ( self : str ) ->Tuple: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __lowerCAmelCase ( self : Optional[int] ) ->str: """simple docstring""" a = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] a = [Image.fromarray(np.moveaxis(__UpperCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def __lowerCAmelCase ( self : Optional[Any] ) ->List[str]: """simple docstring""" a = InstructBlipProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() , qformer_tokenizer=self.get_qformer_tokenizer() , ) processor.save_pretrained(self.tmpdirname ) a = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) a = self.get_image_processor(do_normalize=__UpperCAmelCase , padding_value=1.0 ) a = InstructBlipProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=__UpperCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __UpperCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __UpperCAmelCase ) self.assertIsInstance(processor.qformer_tokenizer , __UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[Any] ) ->Any: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = self.prepare_image_inputs() a = image_processor(__UpperCAmelCase , return_tensors='''np''' ) a = processor(images=__UpperCAmelCase , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowerCAmelCase ( self : List[str] ) ->List[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = '''lower newer''' a = processor(text=__UpperCAmelCase ) a = tokenizer(__UpperCAmelCase , return_token_type_ids=__UpperCAmelCase ) a = qformer_tokenizer(__UpperCAmelCase , return_token_type_ids=__UpperCAmelCase ) for key in encoded_tokens.keys(): self.assertListEqual(encoded_tokens[key] , encoded_processor[key] ) for key in encoded_tokens_qformer.keys(): self.assertListEqual(encoded_tokens_qformer[key] , encoded_processor['''qformer_''' + key] ) def __lowerCAmelCase ( self : Dict ) ->Optional[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = '''lower newer''' a = self.prepare_image_inputs() a = processor(text=__UpperCAmelCase , images=__UpperCAmelCase ) self.assertListEqual( list(inputs.keys() ) , ['''input_ids''', '''attention_mask''', '''qformer_input_ids''', '''qformer_attention_mask''', '''pixel_values'''] , ) # test if it raises when no input is passed with pytest.raises(__UpperCAmelCase ): processor() def __lowerCAmelCase ( self : Dict ) ->List[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] a = processor.batch_decode(__UpperCAmelCase ) a = tokenizer.batch_decode(__UpperCAmelCase ) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] ) ->str: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = '''lower newer''' a = self.prepare_image_inputs() a = processor(text=__UpperCAmelCase , images=__UpperCAmelCase ) self.assertListEqual( list(inputs.keys() ) , ['''input_ids''', '''attention_mask''', '''qformer_input_ids''', '''qformer_attention_mask''', '''pixel_values'''] , )
26
1
# 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 lowercase_ ( lowercase ): '''simple docstring''' __snake_case = '''openai/whisper-base''' __snake_case = ( '''This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the ''' '''transcribed text.''' ) __snake_case = '''transcriber''' __snake_case = WhisperProcessor __snake_case = WhisperForConditionalGeneration __snake_case = ['''audio'''] __snake_case = ['''text'''] def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Optional[Any] ) ->List[str]: """simple docstring""" return self.pre_processor(__UpperCAmelCase , return_tensors='''pt''' ).input_features def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : int ) ->List[Any]: """simple docstring""" return self.model.generate(inputs=__UpperCAmelCase ) def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : Optional[Any] ) ->Any: """simple docstring""" return self.pre_processor.batch_decode(__UpperCAmelCase , skip_special_tokens=__UpperCAmelCase )[0]
26
import math def _a ( a :int = 100 ) -> int: a = sum(i * i for i in range(1 , n + 1 ) ) a = int(math.pow(sum(range(1 , n + 1 ) ) , 2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(f"""{solution() = }""")
26
1
from unittest.mock import Mock, patch from file_transfer.send_file import send_file @patch('''socket.socket''' ) @patch('''builtins.open''' ) def _a ( a :List[str] , a :Dict ) -> Any: # ===== initialization ===== a = Mock() a = conn, Mock() a = iter([1, None] ) a = lambda a : next(a ) # ===== invoke ===== send_file(filename='''mytext.txt''' , testing=a ) # ===== 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()
26
def _a ( a :int = 600_851_475_143 ) -> int: try: a = int(a ) 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.''' ) a = 2 a = 0 if n == 2: return 2 while n > 2: while n % i != 0: i += 1 a = i while n % i == 0: a = n // i i += 1 return int(a ) if __name__ == "__main__": print(f"""{solution() = }""")
26
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCAmelCase__ = { "configuration_roberta": ["ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP", "RobertaConfig", "RobertaOnnxConfig"], "tokenization_roberta": ["RobertaTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ["RobertaTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "RobertaForCausalLM", "RobertaForMaskedLM", "RobertaForMultipleChoice", "RobertaForQuestionAnswering", "RobertaForSequenceClassification", "RobertaForTokenClassification", "RobertaModel", "RobertaPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "TFRobertaForCausalLM", "TFRobertaForMaskedLM", "TFRobertaForMultipleChoice", "TFRobertaForQuestionAnswering", "TFRobertaForSequenceClassification", "TFRobertaForTokenClassification", "TFRobertaMainLayer", "TFRobertaModel", "TFRobertaPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "FlaxRobertaForCausalLM", "FlaxRobertaForMaskedLM", "FlaxRobertaForMultipleChoice", "FlaxRobertaForQuestionAnswering", "FlaxRobertaForSequenceClassification", "FlaxRobertaForTokenClassification", "FlaxRobertaModel", "FlaxRobertaPreTrainedModel", ] if TYPE_CHECKING: from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig, RobertaOnnxConfig from .tokenization_roberta import RobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roberta_fast import RobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roberta import ( ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, RobertaForCausalLM, RobertaForMaskedLM, RobertaForMultipleChoice, RobertaForQuestionAnswering, RobertaForSequenceClassification, RobertaForTokenClassification, RobertaModel, RobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roberta import ( TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForMultipleChoice, TFRobertaForQuestionAnswering, TFRobertaForSequenceClassification, TFRobertaForTokenClassification, TFRobertaMainLayer, TFRobertaModel, TFRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, FlaxRobertaPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer UpperCAmelCase__ = "bart" UpperCAmelCase__ = True @st.cache(allow_output_mutation=a ) def _a ( ) -> Tuple: if LOAD_DENSE_INDEX: a = AutoTokenizer.from_pretrained('''yjernite/retribert-base-uncased''' ) a = AutoModel.from_pretrained('''yjernite/retribert-base-uncased''' ).to('''cuda:0''' ) a = qar_model.eval() else: a , a = (None, None) if MODEL_TYPE == "bart": a = AutoTokenizer.from_pretrained('''yjernite/bart_eli5''' ) a = AutoModelForSeqaSeqLM.from_pretrained('''yjernite/bart_eli5''' ).to('''cuda:0''' ) a = torch.load('''seq2seq_models/eli5_bart_model_blm_2.pth''' ) sas_model.load_state_dict(save_dict['''model'''] ) a = sas_model.eval() else: a , a = make_qa_sas_model( model_name='''t5-small''' , from_file='''seq2seq_models/eli5_t5_model_1024_4.pth''' , device='''cuda:0''' ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=a ) def _a ( ) -> Dict: if LOAD_DENSE_INDEX: a = faiss.StandardGpuResources() a = datasets.load_dataset(path='''wiki_snippets''' , name='''wiki40b_en_100_0''' )['''train'''] a = np.memmap( '''wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat''' , dtype='''float32''' , mode='''r''' , shape=(wikiaab_passages.num_rows, 128) , ) a = faiss.IndexFlatIP(128 ) a = faiss.index_cpu_to_gpu(a , 1 , a ) wikiaab_gpu_index_flat.add(a ) # TODO fix for larger GPU else: a , a = (None, None) a = Elasticsearch([{'''host''': '''localhost''', '''port''': '''9200'''}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=a ) def _a ( ) -> Optional[int]: a = datasets.load_dataset('''eli5''' , name='''LFQA_reddit''' ) a = elia['''train_eli5'''] a = np.memmap( '''eli5_questions_reps.dat''' , dtype='''float32''' , mode='''r''' , shape=(elia_train.num_rows, 128) ) a = faiss.IndexFlatIP(128 ) eli5_train_q_index.add(a ) return (elia_train, eli5_train_q_index) UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = load_indexes() UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = load_models() UpperCAmelCase__ , UpperCAmelCase__ = load_train_data() def _a ( a :str , a :Tuple=10 ) -> List[str]: a = embed_questions_for_retrieval([question] , a , a ) a , a = eli5_train_q_index.search(a , a ) a = [elia_train[int(a )] for i in I[0]] return nn_examples def _a ( a :str , a :Any="wiki40b" , a :int="dense" , a :Union[str, Any]=10 ) -> List[str]: if source == "none": a , a = (''' <P> '''.join(['''''' for _ in range(11 )] ).strip(), []) else: if method == "dense": a , a = query_qa_dense_index( a , a , a , a , a , a ) else: a , a = query_es_index( a , a , index_name='''english_wiki40b_snippets_100w''' , n_results=a , ) a = [ (res['''article_title'''], res['''section_title'''].strip(), res['''score'''], res['''passage_text''']) for res in hit_lst ] a = '''question: {} context: {}'''.format(a , a ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda a : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda a : None), } ) def _a ( a :Tuple , a :int , a :int , a :Dict=64 , a :List[Any]=256 , a :List[Any]=False , a :List[Any]=2 , a :Tuple=0.95 , a :Optional[Any]=0.8 ) -> int: with torch.no_grad(): a = qa_sas_generate( a , a , a , num_answers=1 , num_beams=a , min_len=a , max_len=a , do_sample=a , temp=a , top_p=a , top_k=a , max_input_length=1_024 , device='''cuda:0''' , )[0] return (answer, support_list) st.title("Long Form Question Answering with ELI5") # Start sidebar UpperCAmelCase__ = "<img src='https://huggingface.co/front/assets/huggingface_logo.svg'>" UpperCAmelCase__ = "\n<html>\n <head>\n <style>\n .img-container {\n padding-left: 90px;\n padding-right: 90px;\n padding-top: 50px;\n padding-bottom: 50px;\n background-color: #f0f3f9;\n }\n </style>\n </head>\n <body>\n <span class=\"img-container\"> <!-- Inline parent element -->\n %s\n </span>\n </body>\n</html>\n" % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia UpperCAmelCase__ = "\nThis demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html).\nFirst, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset,\na pre-processed fixed snapshot of Wikipedia.\n" st.sidebar.markdown(description, unsafe_allow_html=True) UpperCAmelCase__ = [ "Answer the question", "View the retrieved document only", "View the most similar ELI5 question and answer", "Show me everything, please!", ] UpperCAmelCase__ = st.sidebar.checkbox("Demo options") if demo_options: UpperCAmelCase__ = st.sidebar.selectbox( "", action_list, index=3, ) UpperCAmelCase__ = action_list.index(action_st) UpperCAmelCase__ = st.sidebar.selectbox( "", ["Show full text of passages", "Show passage section titles"], index=0, ) UpperCAmelCase__ = show_type == "Show full text of passages" else: UpperCAmelCase__ = 3 UpperCAmelCase__ = True UpperCAmelCase__ = st.sidebar.checkbox("Retrieval options") if retrieval_options: UpperCAmelCase__ = "\n ### Information retriever options\n\n The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding\n trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs.\n The answer is then generated by sequence to sequence model which takes the question and retrieved document as input.\n " st.sidebar.markdown(retriever_info) UpperCAmelCase__ = st.sidebar.selectbox("Which Wikipedia format should the model use?", ["wiki40b", "none"]) UpperCAmelCase__ = st.sidebar.selectbox("Which Wikipedia indexer should the model use?", ["dense", "sparse", "mixed"]) else: UpperCAmelCase__ = "wiki40b" UpperCAmelCase__ = "dense" UpperCAmelCase__ = "beam" UpperCAmelCase__ = 2 UpperCAmelCase__ = 64 UpperCAmelCase__ = 256 UpperCAmelCase__ = None UpperCAmelCase__ = None UpperCAmelCase__ = st.sidebar.checkbox("Generation options") if generate_options: UpperCAmelCase__ = "\n ### Answer generation options\n\n The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large)\n weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with\n **beam** search, or **sample** from the decoder's output probabilities.\n " st.sidebar.markdown(generate_info) UpperCAmelCase__ = st.sidebar.selectbox("Would you like to use beam search or sample an answer?", ["beam", "sampled"]) UpperCAmelCase__ = st.sidebar.slider( "Minimum generation length", min_value=8, max_value=256, value=64, step=8, format=None, key=None ) UpperCAmelCase__ = st.sidebar.slider( "Maximum generation length", min_value=64, max_value=512, value=256, step=16, format=None, key=None ) if sampled == "beam": UpperCAmelCase__ = st.sidebar.slider("Beam size", min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: UpperCAmelCase__ = st.sidebar.slider( "Nucleus sampling p", min_value=0.1, max_value=1.0, value=0.95, step=0.01, format=None, key=None ) UpperCAmelCase__ = st.sidebar.slider( "Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.01, format=None, key=None ) UpperCAmelCase__ = None # start main text UpperCAmelCase__ = [ "<MY QUESTION>", "How do people make chocolate?", "Why do we get a fever when we are sick?", "How can different animals perceive different colors?", "What is natural language processing?", "What's the best way to treat a sunburn?", "What exactly are vitamins ?", "How does nuclear energy provide electricity?", "What's the difference between viruses and bacteria?", "Why are flutes classified as woodwinds when most of them are made out of metal ?", "Why do people like drinking coffee even though it tastes so bad?", "What happens when wine ages? How does it make the wine taste better?", "If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?", "How can we set a date to the beginning or end of an artistic period? Doesn't the change happen gradually?", "How does New Zealand have so many large bird predators?", ] UpperCAmelCase__ = st.selectbox( "What would you like to ask? ---- select <MY QUESTION> to enter a new query", questions_list, index=1, ) if question_s == "<MY QUESTION>": UpperCAmelCase__ = st.text_input("Enter your question here:", "") else: UpperCAmelCase__ = question_s if st.button("Show me!"): if action in [0, 1, 3]: if index_type == "mixed": UpperCAmelCase__ , UpperCAmelCase__ = make_support(question, source=wiki_source, method="dense", n_results=10) UpperCAmelCase__ , UpperCAmelCase__ = make_support(question, source=wiki_source, method="sparse", n_results=10) UpperCAmelCase__ = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] UpperCAmelCase__ = support_list[:10] UpperCAmelCase__ = "<P> " + " <P> ".join([res[-1] for res in support_list]) else: UpperCAmelCase__ , UpperCAmelCase__ = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: UpperCAmelCase__ , UpperCAmelCase__ = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == "sampled"), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown("### The model generated answer is:") st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown("--- \n ### The model is drawing information from the following Wikipedia passages:") for i, res in enumerate(support_list): UpperCAmelCase__ = "https://en.wikipedia.org/wiki/{}".format(res[0].replace(" ", "_")) UpperCAmelCase__ = res[1].strip() if sec_titles == "": UpperCAmelCase__ = "[{}]({})".format(res[0], wiki_url) else: UpperCAmelCase__ = sec_titles.split(" & ") UpperCAmelCase__ = " & ".join( ["[{}]({}#{})".format(sec.strip(), wiki_url, sec.strip().replace(" ", "_")) for sec in sec_list] ) st.markdown( "{0:02d} - **Article**: {1:<18} <br> _Section_: {2}".format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( "> <span style=\"font-family:arial; font-size:10pt;\">" + res[-1] + "</span>", unsafe_allow_html=True ) if action in [2, 3]: UpperCAmelCase__ = find_nearest_training(question) UpperCAmelCase__ = nn_train_list[0] st.markdown( "--- \n ### The most similar question in the ELI5 training set was: \n\n {}".format(train_exple["title"]) ) UpperCAmelCase__ = [ "{}. {}".format(i + 1, " \n".join([line.strip() for line in ans.split("\n") if line.strip() != ""])) for i, (ans, sc) in enumerate(zip(train_exple["answers"]["text"], train_exple["answers"]["score"])) if i == 0 or sc > 2 ] st.markdown("##### Its answers were: \n\n {}".format("\n".join(answers_st))) UpperCAmelCase__ = "\n---\n\n**Disclaimer**\n\n*The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system.\nEvaluating biases of such a model and ensuring factual generations are still very much open research problems.\nTherefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.*\n" st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
26
1
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow if is_tf_available(): import numpy as np import tensorflow as tf from transformers import TFCamembertModel @require_tf @require_sentencepiece @require_tokenizers class lowercase_ ( unittest.TestCase ): '''simple docstring''' @slow def __lowerCAmelCase ( self : List[Any] ) ->int: """simple docstring""" a = TFCamembertModel.from_pretrained('''jplu/tf-camembert-base''' ) a = tf.convert_to_tensor( [[5, 121, 11, 660, 16, 730, 25_543, 110, 83, 6]] , dtype=tf.intaa , ) # J'aime le camembert !" a = model(__UpperCAmelCase )['''last_hidden_state'''] a = tf.TensorShape((1, 10, 768) ) self.assertEqual(output.shape , __UpperCAmelCase ) # compare the actual values for a slice. a = tf.convert_to_tensor( [[[-0.0254, 0.0235, 0.1027], [0.0606, -0.1811, -0.0418], [-0.1561, -0.1127, 0.2687]]] , dtype=tf.floataa , ) # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0') # camembert.eval() # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach() self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
26
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin UpperCAmelCase__ = "▁" UpperCAmelCase__ = get_tests_dir("fixtures/test_sentencepiece.model") @require_sentencepiece class lowercase_ ( lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = BertGenerationTokenizer __snake_case = False __snake_case = True def __lowerCAmelCase ( self : str ) ->str: """simple docstring""" super().setUp() a = BertGenerationTokenizer(__UpperCAmelCase , keep_accents=__UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self : int ) ->Dict: """simple docstring""" a = '''<s>''' a = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCAmelCase ) , __UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCAmelCase ) , __UpperCAmelCase ) def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''<pad>''' ) self.assertEqual(len(__UpperCAmelCase ) , 1_002 ) def __lowerCAmelCase ( self : List[str] ) ->List[Any]: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1_000 ) def __lowerCAmelCase ( self : Tuple ) ->Optional[int]: """simple docstring""" a = BertGenerationTokenizer(__UpperCAmelCase , keep_accents=__UpperCAmelCase ) a = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(__UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) , [285, 46, 10, 170, 382] , ) a = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( __UpperCAmelCase , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) a = tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) self.assertListEqual( __UpperCAmelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) a = tokenizer.convert_ids_to_tokens(__UpperCAmelCase ) self.assertListEqual( __UpperCAmelCase , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) @cached_property def __lowerCAmelCase ( self : List[Any] ) ->List[str]: """simple docstring""" return BertGenerationTokenizer.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder''' ) @slow def __lowerCAmelCase ( self : Any ) ->str: """simple docstring""" a = '''Hello World!''' a = [18_536, 2_260, 101] self.assertListEqual(__UpperCAmelCase , self.big_tokenizer.encode(__UpperCAmelCase ) ) @slow def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = ( '''This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will''' ''' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth''' ) a = [ 871, 419, 358, 946, 991, 2_521, 452, 358, 1_357, 387, 7_751, 3_536, 112, 985, 456, 126, 865, 938, 5_400, 5_734, 458, 1_368, 467, 786, 2_462, 5_246, 1_159, 633, 865, 4_519, 457, 582, 852, 2_557, 427, 916, 508, 405, 34_324, 497, 391, 408, 11_342, 1_244, 385, 100, 938, 985, 456, 574, 362, 12_597, 3_200, 3_129, 1_172, ] self.assertListEqual(__UpperCAmelCase , self.big_tokenizer.encode(__UpperCAmelCase ) ) @require_torch @slow def __lowerCAmelCase ( self : Any ) ->Dict: """simple docstring""" import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence a = list(self.big_tokenizer.get_vocab().keys() )[:10] a = ''' '''.join(__UpperCAmelCase ) a = self.big_tokenizer.encode_plus(__UpperCAmelCase , return_tensors='''pt''' , return_token_type_ids=__UpperCAmelCase ) a = self.big_tokenizer.batch_encode_plus( [sequence + ''' ''' + sequence] , return_tensors='''pt''' , return_token_type_ids=__UpperCAmelCase ) a = BertGenerationConfig() a = BertGenerationEncoder(__UpperCAmelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**__UpperCAmelCase ) model(**__UpperCAmelCase ) @slow def __lowerCAmelCase ( self : str ) ->Optional[Any]: """simple docstring""" a = {'''input_ids''': [[39_286, 458, 36_335, 2_001, 456, 13_073, 13_266, 455, 113, 7_746, 1_741, 11_157, 391, 13_073, 13_266, 455, 113, 3_967, 35_412, 113, 4_936, 109, 3_870, 2_377, 113, 30_084, 45_720, 458, 134, 17_496, 112, 503, 11_672, 113, 118, 112, 5_665, 13_347, 38_687, 112, 1_496, 31_389, 112, 3_268, 47_264, 134, 962, 112, 16_377, 8_035, 23_130, 430, 12_169, 15_518, 28_592, 458, 146, 41_697, 109, 391, 12_169, 15_518, 16_689, 458, 146, 41_358, 109, 452, 726, 4_034, 111, 763, 35_412, 5_082, 388, 1_903, 111, 9_051, 391, 2_870, 48_918, 1_900, 1_123, 550, 998, 112, 9_586, 15_985, 455, 391, 410, 22_955, 37_636, 114], [448, 17_496, 419, 3_663, 385, 763, 113, 27_533, 2_870, 3_283, 13_043, 1_639, 24_713, 523, 656, 24_013, 18_550, 2_521, 517, 27_014, 21_244, 420, 1_212, 1_465, 391, 927, 4_833, 388, 578, 11_786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2_169, 7_687, 21_932, 18_146, 726, 363, 17_032, 3_391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__UpperCAmelCase , model_name='''google/bert_for_seq_generation_L-24_bbc_encoder''' , revision='''c817d1fd1be2ffa69431227a1fe320544943d4db''' , )
26
1
from manim import * class lowercase_ ( lowercase ): '''simple docstring''' def __lowerCAmelCase ( self : Tuple ) ->Union[str, Any]: """simple docstring""" a = Rectangle(height=0.5 , width=0.5 ) a = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) a = [mem.copy() for i in range(6 )] a = [mem.copy() for i in range(6 )] a = VGroup(*__UpperCAmelCase ).arrange(__UpperCAmelCase , buff=0 ) a = VGroup(*__UpperCAmelCase ).arrange(__UpperCAmelCase , buff=0 ) a = VGroup(__UpperCAmelCase , __UpperCAmelCase ).arrange(__UpperCAmelCase , buff=0 ) a = Text('''CPU''' , font_size=24 ) a = Group(__UpperCAmelCase , __UpperCAmelCase ).arrange(__UpperCAmelCase , buff=0.5 , aligned_edge=__UpperCAmelCase ) cpu.move_to([-2.5, -0.5, 0] ) self.add(__UpperCAmelCase ) a = [mem.copy() for i in range(4 )] a = VGroup(*__UpperCAmelCase ).arrange(__UpperCAmelCase , buff=0 ) a = Text('''GPU''' , font_size=24 ) a = Group(__UpperCAmelCase , __UpperCAmelCase ).arrange(__UpperCAmelCase , buff=0.5 , aligned_edge=__UpperCAmelCase ) gpu.move_to([-1, -1, 0] ) self.add(__UpperCAmelCase ) a = [mem.copy() for i in range(6 )] a = VGroup(*__UpperCAmelCase ).arrange(__UpperCAmelCase , buff=0 ) a = Text('''Model''' , font_size=24 ) a = Group(__UpperCAmelCase , __UpperCAmelCase ).arrange(__UpperCAmelCase , buff=0.5 , aligned_edge=__UpperCAmelCase ) model.move_to([3, -1.0, 0] ) self.add(__UpperCAmelCase ) a = [] for i, rect in enumerate(__UpperCAmelCase ): rect.set_stroke(__UpperCAmelCase ) # target = fill.copy().set_fill(YELLOW, opacity=0.7) # target.move_to(rect) # self.add(target) a = Rectangle(height=0.46 / 4 , width=0.46 / 3 ).set_stroke(width=0.0 ).set_fill(__UpperCAmelCase , opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.02 , direction=__UpperCAmelCase ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(cpu_targs[0] , direction=__UpperCAmelCase , buff=0.0 ) else: cpu_target.next_to(cpu_targs[i - 1] , direction=__UpperCAmelCase , buff=0.0 ) self.add(__UpperCAmelCase ) cpu_targs.append(__UpperCAmelCase ) a = [mem.copy() for i in range(6 )] a = VGroup(*__UpperCAmelCase ).arrange(__UpperCAmelCase , buff=0 ) a = Text('''Loaded Checkpoint''' , font_size=24 ) a = Group(__UpperCAmelCase , __UpperCAmelCase ).arrange(__UpperCAmelCase , aligned_edge=__UpperCAmelCase , buff=0.4 ) checkpoint.move_to([3, 0.5, 0] ) a = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) a = MarkupText( F"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model""" , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) self.add(__UpperCAmelCase , __UpperCAmelCase ) a = MarkupText( F"""<span fgcolor='{BLUE}'>●</span> Checkpoint""" , font_size=18 , ) blue_text.next_to(__UpperCAmelCase , DOWN * 2.4 , aligned_edge=key_text.get_left() ) a = MarkupText( F"""Next, a <i><span fgcolor=\"{BLUE}\">second</span></i> model is loaded into memory,\nwith the weights of a <span fgcolor=\"{BLUE}\">single shard</span>.""" , font_size=24 , ) step_a.move_to([2, 2, 0] ) self.play(Write(__UpperCAmelCase ) , Write(__UpperCAmelCase ) ) self.play(Write(__UpperCAmelCase , run_time=1 ) , Create(__UpperCAmelCase , run_time=1 ) ) a = [] a = [] for i, rect in enumerate(__UpperCAmelCase ): a = fill.copy().set_fill(__UpperCAmelCase , opacity=0.7 ) target.move_to(__UpperCAmelCase ) first_animations.append(GrowFromCenter(__UpperCAmelCase , run_time=1 ) ) a = target.copy() cpu_target.generate_target() if i < 5: cpu_target.target.move_to(cpu_left_col_base[i + 1] ) else: cpu_target.target.move_to(cpu_right_col_base[i - 5] ) second_animations.append(MoveToTarget(__UpperCAmelCase , run_time=1.5 ) ) self.play(*__UpperCAmelCase ) self.play(*__UpperCAmelCase ) self.wait()
26
import argparse import numpy as np import torch from transformers import SpeechTaHifiGan, SpeechTaHifiGanConfig, logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger("transformers.models.speecht5") def _a ( a :Optional[Any] , a :Tuple , a :Dict ) -> List[str]: hf_model.apply_weight_norm() a = checkpoint['''input_conv.weight_g'''] a = checkpoint['''input_conv.weight_v'''] a = checkpoint['''input_conv.bias'''] for i in range(len(config.upsample_rates ) ): a = checkpoint[F"""upsamples.{i}.1.weight_g"""] a = checkpoint[F"""upsamples.{i}.1.weight_v"""] a = checkpoint[F"""upsamples.{i}.1.bias"""] for i in range(len(config.upsample_rates ) * len(config.resblock_kernel_sizes ) ): for j in range(len(config.resblock_dilation_sizes ) ): a = checkpoint[F"""blocks.{i}.convs1.{j}.1.weight_g"""] a = checkpoint[F"""blocks.{i}.convs1.{j}.1.weight_v"""] a = checkpoint[F"""blocks.{i}.convs1.{j}.1.bias"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.weight_g"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.weight_v"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.bias"""] a = checkpoint['''output_conv.1.weight_g'''] a = checkpoint['''output_conv.1.weight_v'''] a = checkpoint['''output_conv.1.bias'''] hf_model.remove_weight_norm() @torch.no_grad() def _a ( a :List[str] , a :Union[str, Any] , a :Dict , a :Dict=None , a :List[Any]=None , ) -> int: if config_path is not None: a = SpeechTaHifiGanConfig.from_pretrained(a ) else: a = SpeechTaHifiGanConfig() a = SpeechTaHifiGan(a ) a = torch.load(a ) load_weights(orig_checkpoint['''model''']['''generator'''] , a , a ) a = np.load(a ) a = stats[0].reshape(-1 ) a = stats[1].reshape(-1 ) a = torch.from_numpy(a ).float() a = torch.from_numpy(a ).float() model.save_pretrained(a ) if repo_id: print('''Pushing to the hub...''' ) model.push_to_hub(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--checkpoint_path", required=True, default=None, type=str, help="Path to original checkpoint") parser.add_argument("--stats_path", required=True, default=None, type=str, help="Path to stats.npy file") 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." ) UpperCAmelCase__ = parser.parse_args() convert_hifigan_checkpoint( args.checkpoint_path, args.stats_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
26
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCAmelCase__ = { "configuration_ctrl": ["CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP", "CTRLConfig"], "tokenization_ctrl": ["CTRLTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "CTRL_PRETRAINED_MODEL_ARCHIVE_LIST", "CTRLForSequenceClassification", "CTRLLMHeadModel", "CTRLModel", "CTRLPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST", "TFCTRLForSequenceClassification", "TFCTRLLMHeadModel", "TFCTRLModel", "TFCTRLPreTrainedModel", ] if TYPE_CHECKING: from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig from .tokenization_ctrl import CTRLTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ctrl import ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, CTRLPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_ctrl import ( TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, TFCTRLForSequenceClassification, TFCTRLLMHeadModel, TFCTRLModel, TFCTRLPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) UpperCAmelCase__ = { "configuration_gpt_bigcode": ["GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP", "GPTBigCodeConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST", "GPTBigCodeForSequenceClassification", "GPTBigCodeForTokenClassification", "GPTBigCodeForCausalLM", "GPTBigCodeModel", "GPTBigCodePreTrainedModel", ] if TYPE_CHECKING: from .configuration_gpt_bigcode import GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTBigCodeConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_bigcode import ( GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTBigCodeForCausalLM, GPTBigCodeForSequenceClassification, GPTBigCodeForTokenClassification, GPTBigCodeModel, GPTBigCodePreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
1
# # This a `torch.distributed` diagnostics script that checks that all GPUs in the cluster (one or # many nodes) can talk to each other via nccl and allocate gpu memory. # # To run first adjust the number of processes and nodes: # # python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py # # You may need to add --master_addr $MASTER_ADDR --master_port $MASTER_PORT if using a custom addr:port # # You can also use the rdzv API: --rdzv_endpoint $MASTER_ADDR:$MASTER_PORT --rdzv_backend c10d # # use torch.distributed.launch instead of torch.distributed.run for torch < 1.9 # # If you get a hanging in `barrier` calls you have some network issues, you may try to debug this with: # # NCCL_DEBUG=INFO python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py # # which should tell you what's going on behind the scenes. # # # This script can be run via `srun` in the SLURM environment as well. Here is a SLURM script that # runs on 2 nodes of 4 gpus per node: # # #SBATCH --job-name=test-nodes # name # #SBATCH --nodes=2 # nodes # #SBATCH --ntasks-per-node=1 # crucial - only 1 task per dist per node! # #SBATCH --cpus-per-task=10 # number of cores per tasks # #SBATCH --gres=gpu:4 # number of gpus # #SBATCH --time 0:05:00 # maximum execution time (HH:MM:SS) # #SBATCH --output=%x-%j.out # output file name # # GPUS_PER_NODE=4 # MASTER_ADDR=$(scontrol show hostnames $SLURM_JOB_NODELIST | head -n 1) # MASTER_PORT=6000 # # srun --jobid $SLURM_JOBID bash -c 'python -m torch.distributed.run \ # --nproc_per_node $GPUS_PER_NODE --nnodes $SLURM_NNODES --node_rank $SLURM_PROCID \ # --master_addr $MASTER_ADDR --master_port $MASTER_PORT \ # torch-distributed-gpu-test.py' # import fcntl import os import socket import torch import torch.distributed as dist def _a ( *a :List[str] ) -> List[Any]: with open(a , '''r''' ) as fh: fcntl.flock(a , fcntl.LOCK_EX ) try: print(*a ) finally: fcntl.flock(a , fcntl.LOCK_UN ) UpperCAmelCase__ = int(os.environ["LOCAL_RANK"]) torch.cuda.set_device(local_rank) UpperCAmelCase__ = torch.device("cuda", local_rank) UpperCAmelCase__ = socket.gethostname() UpperCAmelCase__ = f"""[{hostname}-{local_rank}]""" try: # test distributed dist.init_process_group("nccl") dist.all_reduce(torch.ones(1).to(device), op=dist.ReduceOp.SUM) dist.barrier() # test cuda is available and can allocate memory torch.cuda.is_available() torch.ones(1).cuda(local_rank) # global rank UpperCAmelCase__ = dist.get_rank() UpperCAmelCase__ = dist.get_world_size() printflock(f"""{gpu} is OK (global rank: {rank}/{world_size})""") dist.barrier() if rank == 0: printflock(f"""pt={torch.__version__}, cuda={torch.version.cuda}, nccl={torch.cuda.nccl.version()}""") except Exception: printflock(f"""{gpu} is broken""") raise
26
import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def _a ( a :Tuple ) -> int: a = tmp_path / '''file.csv''' a = textwrap.dedent( '''\ header1,header2 1,2 10,20 ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :int ) -> List[str]: a = tmp_path / '''malformed_file.csv''' a = textwrap.dedent( '''\ header1,header2 1,2 10,20, ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :Dict , a :int ) -> List[str]: a = tmp_path / '''csv_with_image.csv''' a = textwrap.dedent( F"""\ image {image_file} """ ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :List[Any] ) -> Dict: a = tmp_path / '''csv_with_label.csv''' a = textwrap.dedent( '''\ label good bad good ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :Tuple ) -> Any: a = tmp_path / '''csv_with_int_list.csv''' a = textwrap.dedent( '''\ int_list 1 2 3 4 5 6 7 8 9 ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) def _a ( a :Dict , a :int , a :Union[str, Any] ) -> List[Any]: a = Csv() a = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(a , match='''Error tokenizing data''' ): for _ in generator: pass assert any( record.levelname == '''ERROR''' and '''Failed to read file''' in record.message and os.path.basename(a ) in record.message for record in caplog.records ) @require_pil def _a ( a :Dict ) -> Any: with open(a , encoding='''utf-8''' ) as f: a = f.read().splitlines()[1] a = Csv(encoding='''utf-8''' , features=Features({'''image''': Image()} ) ) a = csv._generate_tables([[csv_file_with_image]] ) a = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''image''' ).type == Image()() a = pa_table.to_pydict()['''image'''] assert generated_content == [{"path": image_file, "bytes": None}] def _a ( a :Any ) -> Tuple: with open(a , encoding='''utf-8''' ) as f: a = f.read().splitlines()[1:] a = Csv(encoding='''utf-8''' , features=Features({'''label''': ClassLabel(names=['''good''', '''bad'''] )} ) ) a = csv._generate_tables([[csv_file_with_label]] ) a = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''label''' ).type == ClassLabel(names=['''good''', '''bad'''] )() a = pa_table.to_pydict()['''label'''] assert generated_content == [ClassLabel(names=['''good''', '''bad'''] ).straint(a ) for label in labels] def _a ( a :Union[str, Any] ) -> Optional[Any]: a = Csv(encoding='''utf-8''' , sep=''',''' , converters={'''int_list''': lambda a : [int(a ) for i in x.split()]} ) a = csv._generate_tables([[csv_file_with_int_list]] ) a = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field('''int_list''' ).type ) a = pa_table.to_pydict()['''int_list'''] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
26
1
from math import ceil, sqrt def _a ( a :int = 1_000_000 ) -> int: a = 0 for outer_width in range(3 , (limit // 4) + 2 ): if outer_width**2 > limit: a = max(ceil(sqrt(outer_width**2 - limit ) ) , 1 ) else: a = 1 if (outer_width - hole_width_lower_bound) % 2: hole_width_lower_bound += 1 answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 return answer if __name__ == "__main__": print(f"""{solution() = }""")
26
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SwiftFormerConfig, SwiftFormerForImageClassification, ViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = torch.device("cpu") def _a ( ) -> Union[str, Any]: a = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a = Image.open(requests.get(a , stream=a ).raw ) return im def _a ( a :Dict ) -> Tuple: if swiftformer_name == "swiftformer_xs": return torch.tensor([-2.1703e00, 2.1107e00, -2.0811e00, 8.8685e-01, 2.4360e-01] ) elif swiftformer_name == "swiftformer_s": return torch.tensor([3.9636e-01, 2.3478e-01, -1.6963e00, -1.7381e00, -8.6337e-01] ) elif swiftformer_name == "swiftformer_l1": return torch.tensor([-4.2768e-01, -4.7429e-01, -1.0897e00, -1.0248e00, 3.5523e-02] ) elif swiftformer_name == "swiftformer_l3": return torch.tensor([-2.5330e-01, 2.4211e-01, -6.0185e-01, -8.2789e-01, -6.0446e-02] ) def _a ( a :int , a :Any , a :Union[str, Any] ) -> int: a = dct.pop(a ) a = val def _a ( a :Any ) -> Dict: a = [] for k in state_dict.keys(): a = k if ".pwconv" in k: a = k_new.replace('''.pwconv''' , '''.point_wise_conv''' ) if ".dwconv" in k: a = k_new.replace('''.dwconv''' , '''.depth_wise_conv''' ) if ".Proj." in k: a = k_new.replace('''.Proj.''' , '''.proj.''' ) if "patch_embed" in k_new: a = k_new.replace('''patch_embed''' , '''swiftformer.patch_embed.patch_embedding''' ) if "network" in k_new: a = k_new.split('''.''' ) if ls[2].isdigit(): a = '''swiftformer.encoder.network.''' + ls[1] + '''.blocks.''' + ls[2] + '''.''' + '''.'''.join(ls[3:] ) else: a = k_new.replace('''network''' , '''swiftformer.encoder.network''' ) rename_keys.append((k, k_new) ) return rename_keys @torch.no_grad() def _a ( a :List[Any] , a :Tuple , a :List[str] ) -> Union[str, Any]: a = SwiftFormerConfig() # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size a = 1_000 a = '''huggingface/label-files''' a = '''imagenet-1k-id2label.json''' a = json.load(open(hf_hub_download(a , a , repo_type='''dataset''' ) , '''r''' ) ) a = {int(a ): v for k, v in idalabel.items()} a = idalabel a = {v: k for k, v in idalabel.items()} # size of the architecture if swiftformer_name == "swiftformer_xs": a = [3, 3, 6, 4] a = [48, 56, 112, 220] elif swiftformer_name == "swiftformer_s": a = [3, 3, 9, 6] a = [48, 64, 168, 224] elif swiftformer_name == "swiftformer_l1": a = [4, 3, 10, 5] a = [48, 96, 192, 384] elif swiftformer_name == "swiftformer_l3": a = [4, 4, 12, 6] a = [64, 128, 320, 512] # load state_dict of original model, remove and rename some keys if original_ckpt: if original_ckpt.startswith('''https''' ): a = torch.hub.load_state_dict_from_url(a , map_location='''cpu''' , check_hash=a ) else: a = torch.load(a , map_location='''cpu''' ) a = checkpoint a = create_rename_keys(a ) for rename_key_src, rename_key_dest in rename_keys: rename_key(a , a , a ) # load HuggingFace model a = SwiftFormerForImageClassification(a ).eval() hf_model.load_state_dict(a ) # prepare test inputs a = prepare_img() a = ViTImageProcessor.from_pretrained('''preprocessor_config''' ) a = processor(images=a , return_tensors='''pt''' ) # compare outputs from both models a = get_expected_output(a ) a = hf_model(inputs['''pixel_values'''] ).logits assert hf_logits.shape == torch.Size([1, 1_000] ) assert torch.allclose(hf_logits[0, 0:5] , a , atol=1e-3 ) Path(a ).mkdir(exist_ok=a ) print(F"""Saving model {swiftformer_name} to {pytorch_dump_folder_path}""" ) hf_model.save_pretrained(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--swiftformer_name", default="swiftformer_xs", choices=["swiftformer_xs", "swiftformer_s", "swiftformer_l1", "swiftformer_l3"], type=str, help="Name of the SwiftFormer model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default="./converted_outputs/", type=str, help="Path to the output PyTorch model directory.", ) parser.add_argument("--original_ckpt", default=None, type=str, help="Path to the original model checkpoint.") UpperCAmelCase__ = parser.parse_args() convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)
26
1
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import SPIECE_UNDERLINE, logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {"vocab_file": "spiece.model"} UpperCAmelCase__ = { "vocab_file": { "TsinghuaAI/CPM-Generate": "https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model", } } class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Optional[Any] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Optional[Any]=False , __UpperCAmelCase : Any=True , __UpperCAmelCase : Optional[Any]=False , __UpperCAmelCase : List[str]="<s>" , __UpperCAmelCase : int="</s>" , __UpperCAmelCase : Any="<unk>" , __UpperCAmelCase : Optional[Any]="<sep>" , __UpperCAmelCase : int="<pad>" , __UpperCAmelCase : Any="<cls>" , __UpperCAmelCase : List[str]="<mask>" , __UpperCAmelCase : Optional[int]=["<eop>", "<eod>"] , __UpperCAmelCase : Optional[Dict[str, Any]] = None , **__UpperCAmelCase : Union[str, Any] , ) ->None: """simple docstring""" a = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else mask_token a = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__UpperCAmelCase , remove_space=__UpperCAmelCase , keep_accents=__UpperCAmelCase , bos_token=__UpperCAmelCase , eos_token=__UpperCAmelCase , unk_token=__UpperCAmelCase , sep_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , cls_token=__UpperCAmelCase , mask_token=__UpperCAmelCase , additional_special_tokens=__UpperCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **__UpperCAmelCase , ) a = 3 a = do_lower_case a = remove_space a = keep_accents a = vocab_file a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__UpperCAmelCase ) try: import jieba except ModuleNotFoundError as error: raise error.__class__( '''You need to install jieba to use CpmTokenizer or CpmTokenizerFast. ''' '''See https://pypi.org/project/jieba/ for installation.''' ) a = jieba a = str.maketrans(''' \n''' , '''\u2582\u2583''' ) @property # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size def __lowerCAmelCase ( self : Union[str, Any] ) ->Optional[Any]: """simple docstring""" return len(self.sp_model ) def __lowerCAmelCase ( self : Tuple ) ->List[str]: """simple docstring""" a = {self.convert_ids_to_tokens(__UpperCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) ->Optional[int]: """simple docstring""" a = self.__dict__.copy() a = None return state def __setstate__( self : List[str] , __UpperCAmelCase : Optional[int] ) ->str: """simple docstring""" a = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): a = {} a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : List[str] ) ->List[str]: """simple docstring""" if self.remove_space: a = ''' '''.join(inputs.strip().split() ) else: a = inputs a = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: a = unicodedata.normalize('''NFKD''' , __UpperCAmelCase ) a = ''''''.join([c for c in outputs if not unicodedata.combining(__UpperCAmelCase )] ) if self.do_lower_case: a = outputs.lower() return outputs def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : str ) ->List[str]: """simple docstring""" a = self.preprocess_text(__UpperCAmelCase ) a = self.sp_model.encode(__UpperCAmelCase , out_type=__UpperCAmelCase ) a = [] for piece in pieces: if len(__UpperCAmelCase ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): a = self.sp_model.EncodeAsPieces(piece[:-1].replace(__UpperCAmelCase , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: a = cur_pieces[1:] else: a = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(__UpperCAmelCase ) else: new_pieces.append(__UpperCAmelCase ) return new_pieces def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Any ) ->Any: """simple docstring""" return self.sp_model.PieceToId(__UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : Dict ) ->Union[str, Any]: """simple docstring""" return self.sp_model.IdToPiece(__UpperCAmelCase ) def __lowerCAmelCase ( self : Any , __UpperCAmelCase : str ) ->List[str]: """simple docstring""" a = ''''''.join(__UpperCAmelCase ).replace(__UpperCAmelCase , ''' ''' ).strip() return out_string def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None , __UpperCAmelCase : bool = False ) ->List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__UpperCAmelCase , token_ids_a=__UpperCAmelCase , already_has_special_tokens=__UpperCAmelCase ) if token_ids_a is not None: return ([0] * len(__UpperCAmelCase )) + [1] + ([0] * len(__UpperCAmelCase )) + [1, 1] return ([0] * len(__UpperCAmelCase )) + [1, 1] def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None ) ->Tuple[str]: """simple docstring""" if not os.path.isdir(__UpperCAmelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return a = os.path.join( __UpperCAmelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__UpperCAmelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __UpperCAmelCase ) elif not os.path.isfile(self.vocab_file ): with open(__UpperCAmelCase , '''wb''' ) as fi: a = self.sp_model.serialized_model_proto() fi.write(__UpperCAmelCase ) return (out_vocab_file,) def __lowerCAmelCase ( self : Any , *__UpperCAmelCase : List[str] , **__UpperCAmelCase : Optional[Any] ) ->Tuple: """simple docstring""" a = super()._decode(*__UpperCAmelCase , **__UpperCAmelCase ) a = text.replace(''' ''' , '''''' ).replace('''\u2582''' , ''' ''' ).replace('''\u2583''' , '''\n''' ) return text
26
import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Union[str, Any] , __UpperCAmelCase : UNetaDModel , __UpperCAmelCase : UNetaDModel , __UpperCAmelCase : DDPMScheduler , __UpperCAmelCase : Optional[int] , ) ->List[str]: """simple docstring""" super().__init__() a = value_function a = unet a = scheduler a = env a = env.get_dataset() a = {} for key in self.data.keys(): try: a = self.data[key].mean() except: # noqa: E722 pass a = {} for key in self.data.keys(): try: a = self.data[key].std() except: # noqa: E722 pass a = env.observation_space.shape[0] a = env.action_space.shape[0] def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[int] ) ->Dict: """simple docstring""" return (x_in - self.means[key]) / self.stds[key] def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Dict , __UpperCAmelCase : Dict ) ->List[str]: """simple docstring""" return x_in * self.stds[key] + self.means[key] def __lowerCAmelCase ( self : int , __UpperCAmelCase : int ) ->List[str]: """simple docstring""" if type(__UpperCAmelCase ) is dict: return {k: self.to_torch(__UpperCAmelCase ) for k, v in x_in.items()} elif torch.is_tensor(__UpperCAmelCase ): return x_in.to(self.unet.device ) return torch.tensor(__UpperCAmelCase , device=self.unet.device ) def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : int , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Tuple ) ->int: """simple docstring""" for key, val in cond.items(): a = val.clone() return x_in def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[Any] ) ->Tuple: """simple docstring""" a = x.shape[0] a = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model a = torch.full((batch_size,) , __UpperCAmelCase , device=self.unet.device , dtype=torch.long ) for _ in range(__UpperCAmelCase ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models a = self.value_function(x.permute(0 , 2 , 1 ) , __UpperCAmelCase ).sample a = torch.autograd.grad([y.sum()] , [x] )[0] a = self.scheduler._get_variance(__UpperCAmelCase ) a = torch.exp(0.5 * posterior_variance ) a = model_std * grad a = 0 a = x.detach() a = x + scale * grad a = self.reset_xa(__UpperCAmelCase , __UpperCAmelCase , self.action_dim ) a = self.unet(x.permute(0 , 2 , 1 ) , __UpperCAmelCase ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg a = self.scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , predict_epsilon=__UpperCAmelCase )['''prev_sample'''] # apply conditions to the trajectory (set the initial state) a = self.reset_xa(__UpperCAmelCase , __UpperCAmelCase , self.action_dim ) a = self.to_torch(__UpperCAmelCase ) return x, y def __call__( self : Union[str, Any] , __UpperCAmelCase : Dict , __UpperCAmelCase : Optional[int]=64 , __UpperCAmelCase : int=32 , __UpperCAmelCase : Optional[Any]=2 , __UpperCAmelCase : str=0.1 ) ->List[str]: """simple docstring""" a = self.normalize(__UpperCAmelCase , '''observations''' ) a = obs[None].repeat(__UpperCAmelCase , axis=0 ) a = {0: self.to_torch(__UpperCAmelCase )} a = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) a = randn_tensor(__UpperCAmelCase , device=self.unet.device ) a = self.reset_xa(__UpperCAmelCase , __UpperCAmelCase , self.action_dim ) a = self.to_torch(__UpperCAmelCase ) # run the diffusion process a , a = self.run_diffusion(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) # sort output trajectories by value a = y.argsort(0 , descending=__UpperCAmelCase ).squeeze() a = x[sorted_idx] a = sorted_values[:, :, : self.action_dim] a = actions.detach().cpu().numpy() a = self.de_normalize(__UpperCAmelCase , key='''actions''' ) # select the action with the highest value if y is not None: a = 0 else: # if we didn't run value guiding, select a random action a = np.random.randint(0 , __UpperCAmelCase ) a = denorm_actions[selected_index, 0] return denorm_actions
26
1
import collections import os from typing import List, Optional, Tuple from transformers.utils import is_jieba_available, requires_backends if is_jieba_available(): import jieba from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {"vocab_file": "vocab.txt"} UpperCAmelCase__ = { "vocab_file": { "openbmb/cpm-ant-10b": "https://huggingface.co/openbmb/cpm-ant-10b/blob/main/vocab.txt", }, } UpperCAmelCase__ = { "openbmb/cpm-ant-10b": 1024, } def _a ( a :Dict ) -> List[Any]: a = collections.OrderedDict() with open(a , '''r''' , encoding='''utf-8''' ) as reader: a = reader.readlines() for index, token in enumerate(a ): a = token.rstrip('''\n''' ) a = index return vocab class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Any , __UpperCAmelCase : Dict , __UpperCAmelCase : Dict="<unk>" , __UpperCAmelCase : str=200 ) ->Any: """simple docstring""" a = vocab a = unk_token a = max_input_chars_per_word def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : Optional[Any] ) ->str: """simple docstring""" a = list(__UpperCAmelCase ) if len(__UpperCAmelCase ) > self.max_input_chars_per_word: return [self.unk_token] a = 0 a = [] while start < len(__UpperCAmelCase ): a = len(__UpperCAmelCase ) a = None while start < end: a = ''''''.join(chars[start:end] ) if substr in self.vocab: a = substr break end -= 1 if cur_substr is None: sub_tokens.append(self.unk_token ) start += 1 else: sub_tokens.append(__UpperCAmelCase ) a = end return sub_tokens class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = VOCAB_FILES_NAMES __snake_case = PRETRAINED_VOCAB_FILES_MAP __snake_case = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __snake_case = ['''input_ids''', '''attention_mask'''] __snake_case = False def __init__( self : Tuple , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Optional[int]="<d>" , __UpperCAmelCase : int="</d>" , __UpperCAmelCase : Tuple="<s>" , __UpperCAmelCase : str="</s>" , __UpperCAmelCase : Dict="<pad>" , __UpperCAmelCase : int="<unk>" , __UpperCAmelCase : List[str]="</n>" , __UpperCAmelCase : Optional[int]="</_>" , __UpperCAmelCase : List[str]="left" , **__UpperCAmelCase : Optional[Any] , ) ->List[Any]: """simple docstring""" requires_backends(self , ['''jieba'''] ) super().__init__( bod_token=__UpperCAmelCase , eod_token=__UpperCAmelCase , bos_token=__UpperCAmelCase , eos_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , unk_token=__UpperCAmelCase , line_token=__UpperCAmelCase , space_token=__UpperCAmelCase , padding_side=__UpperCAmelCase , **__UpperCAmelCase , ) a = bod_token a = eod_token a = load_vocab(__UpperCAmelCase ) a = self.encoder[space_token] a = self.encoder[line_token] del self.encoder[space_token] del self.encoder[line_token] a = collections.OrderedDict(sorted(self.encoder.items() , key=lambda __UpperCAmelCase : x[1] ) ) a = {v: k for k, v in self.encoder.items()} a = WordpieceTokenizer(vocab=self.encoder , unk_token=self.unk_token ) @property def __lowerCAmelCase ( self : Any ) ->int: """simple docstring""" return self.encoder[self.bod_token] @property def __lowerCAmelCase ( self : Union[str, Any] ) ->Any: """simple docstring""" return self.encoder[self.eod_token] @property def __lowerCAmelCase ( self : Any ) ->Tuple: """simple docstring""" return self.encoder["\n"] @property def __lowerCAmelCase ( self : Optional[Any] ) ->int: """simple docstring""" return len(self.encoder ) def __lowerCAmelCase ( self : int ) ->Dict: """simple docstring""" return dict(self.encoder , **self.added_tokens_encoder ) def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Optional[int] ) ->str: """simple docstring""" a = [] for x in jieba.cut(__UpperCAmelCase , cut_all=__UpperCAmelCase ): output_tokens.extend(self.wordpiece_tokenizer.tokenize(__UpperCAmelCase ) ) return output_tokens def __lowerCAmelCase ( self : Any , __UpperCAmelCase : List[Any] , **__UpperCAmelCase : Optional[Any] ) ->Optional[Any]: """simple docstring""" a = [i for i in token_ids if i >= 0] a = [ x for x in token_ids if x != self.pad_token_id and x != self.eos_token_id and x != self.bos_token_id ] return super()._decode(__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : List[str] ) ->Optional[Any]: """simple docstring""" return token in self.encoder def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : List[str] ) ->str: """simple docstring""" return "".join(__UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : int ) ->Optional[Any]: """simple docstring""" return self.encoder.get(__UpperCAmelCase , self.encoder.get(self.unk_token ) ) def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Optional[int] ) ->List[str]: """simple docstring""" return self.decoder.get(__UpperCAmelCase , self.unk_token ) def __lowerCAmelCase ( self : str , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None ) ->Tuple[str]: """simple docstring""" if os.path.isdir(__UpperCAmelCase ): a = os.path.join( __UpperCAmelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) else: a = (filename_prefix + '''-''' if filename_prefix else '''''') + save_directory a = 0 if " " in self.encoder: a = self.encoder[''' '''] del self.encoder[" "] if "\n" in self.encoder: a = self.encoder['''\n'''] del self.encoder["\n"] a = collections.OrderedDict(sorted(self.encoder.items() , key=lambda __UpperCAmelCase : x[1] ) ) with open(__UpperCAmelCase , '''w''' , encoding='''utf-8''' ) as writer: for token, token_index in self.encoder.items(): if index != token_index: logger.warning( F"""Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.""" ''' Please check that the vocabulary is not corrupted!''' ) a = token_index writer.write(token + '''\n''' ) index += 1 return (vocab_file,) def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : List[int] = None ) ->List[int]: """simple docstring""" if token_ids_a is None: return [self.bos_token_id] + token_ids_a return [self.bos_token_id] + token_ids_a + [self.bos_token_id] + token_ids_a def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None , __UpperCAmelCase : bool = False ) ->List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__UpperCAmelCase , token_ids_a=__UpperCAmelCase , already_has_special_tokens=__UpperCAmelCase ) if token_ids_a is not None: return [1] + ([0] * len(__UpperCAmelCase )) + [1] + ([0] * len(__UpperCAmelCase )) return [1] + ([0] * len(__UpperCAmelCase ))
26
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import SPIECE_UNDERLINE, logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {"vocab_file": "spiece.model"} UpperCAmelCase__ = { "vocab_file": { "TsinghuaAI/CPM-Generate": "https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model", } } class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Optional[Any] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Optional[Any]=False , __UpperCAmelCase : Any=True , __UpperCAmelCase : Optional[Any]=False , __UpperCAmelCase : List[str]="<s>" , __UpperCAmelCase : int="</s>" , __UpperCAmelCase : Any="<unk>" , __UpperCAmelCase : Optional[Any]="<sep>" , __UpperCAmelCase : int="<pad>" , __UpperCAmelCase : Any="<cls>" , __UpperCAmelCase : List[str]="<mask>" , __UpperCAmelCase : Optional[int]=["<eop>", "<eod>"] , __UpperCAmelCase : Optional[Dict[str, Any]] = None , **__UpperCAmelCase : Union[str, Any] , ) ->None: """simple docstring""" a = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else mask_token a = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__UpperCAmelCase , remove_space=__UpperCAmelCase , keep_accents=__UpperCAmelCase , bos_token=__UpperCAmelCase , eos_token=__UpperCAmelCase , unk_token=__UpperCAmelCase , sep_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , cls_token=__UpperCAmelCase , mask_token=__UpperCAmelCase , additional_special_tokens=__UpperCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **__UpperCAmelCase , ) a = 3 a = do_lower_case a = remove_space a = keep_accents a = vocab_file a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__UpperCAmelCase ) try: import jieba except ModuleNotFoundError as error: raise error.__class__( '''You need to install jieba to use CpmTokenizer or CpmTokenizerFast. ''' '''See https://pypi.org/project/jieba/ for installation.''' ) a = jieba a = str.maketrans(''' \n''' , '''\u2582\u2583''' ) @property # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size def __lowerCAmelCase ( self : Union[str, Any] ) ->Optional[Any]: """simple docstring""" return len(self.sp_model ) def __lowerCAmelCase ( self : Tuple ) ->List[str]: """simple docstring""" a = {self.convert_ids_to_tokens(__UpperCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) ->Optional[int]: """simple docstring""" a = self.__dict__.copy() a = None return state def __setstate__( self : List[str] , __UpperCAmelCase : Optional[int] ) ->str: """simple docstring""" a = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): a = {} a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : List[str] ) ->List[str]: """simple docstring""" if self.remove_space: a = ''' '''.join(inputs.strip().split() ) else: a = inputs a = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: a = unicodedata.normalize('''NFKD''' , __UpperCAmelCase ) a = ''''''.join([c for c in outputs if not unicodedata.combining(__UpperCAmelCase )] ) if self.do_lower_case: a = outputs.lower() return outputs def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : str ) ->List[str]: """simple docstring""" a = self.preprocess_text(__UpperCAmelCase ) a = self.sp_model.encode(__UpperCAmelCase , out_type=__UpperCAmelCase ) a = [] for piece in pieces: if len(__UpperCAmelCase ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): a = self.sp_model.EncodeAsPieces(piece[:-1].replace(__UpperCAmelCase , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: a = cur_pieces[1:] else: a = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(__UpperCAmelCase ) else: new_pieces.append(__UpperCAmelCase ) return new_pieces def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Any ) ->Any: """simple docstring""" return self.sp_model.PieceToId(__UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : Dict ) ->Union[str, Any]: """simple docstring""" return self.sp_model.IdToPiece(__UpperCAmelCase ) def __lowerCAmelCase ( self : Any , __UpperCAmelCase : str ) ->List[str]: """simple docstring""" a = ''''''.join(__UpperCAmelCase ).replace(__UpperCAmelCase , ''' ''' ).strip() return out_string def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None , __UpperCAmelCase : bool = False ) ->List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__UpperCAmelCase , token_ids_a=__UpperCAmelCase , already_has_special_tokens=__UpperCAmelCase ) if token_ids_a is not None: return ([0] * len(__UpperCAmelCase )) + [1] + ([0] * len(__UpperCAmelCase )) + [1, 1] return ([0] * len(__UpperCAmelCase )) + [1, 1] def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None ) ->Tuple[str]: """simple docstring""" if not os.path.isdir(__UpperCAmelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return a = os.path.join( __UpperCAmelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__UpperCAmelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __UpperCAmelCase ) elif not os.path.isfile(self.vocab_file ): with open(__UpperCAmelCase , '''wb''' ) as fi: a = self.sp_model.serialized_model_proto() fi.write(__UpperCAmelCase ) return (out_vocab_file,) def __lowerCAmelCase ( self : Any , *__UpperCAmelCase : List[str] , **__UpperCAmelCase : Optional[Any] ) ->Tuple: """simple docstring""" a = super()._decode(*__UpperCAmelCase , **__UpperCAmelCase ) a = text.replace(''' ''' , '''''' ).replace('''\u2582''' , ''' ''' ).replace('''\u2583''' , '''\n''' ) return text
26
1
from math import ceil from typing import List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor from ...utils import TensorType, logging UpperCAmelCase__ = logging.get_logger(__name__) class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = ['''audio_values''', '''audio_mask'''] def __init__( self : Any , __UpperCAmelCase : Dict=2_048 , __UpperCAmelCase : Any=1 , __UpperCAmelCase : List[str]=[16, 16] , __UpperCAmelCase : str=128 , __UpperCAmelCase : Any=44_100 , __UpperCAmelCase : Any=86 , __UpperCAmelCase : Tuple=2_048 , __UpperCAmelCase : Optional[int]=0.0 , **__UpperCAmelCase : Tuple , ) ->Optional[Any]: """simple docstring""" super().__init__( feature_size=__UpperCAmelCase , sampling_rate=__UpperCAmelCase , padding_value=__UpperCAmelCase , **__UpperCAmelCase , ) a = spectrogram_length a = num_channels a = patch_size a = feature_size // self.patch_size[1] a = n_fft a = sampling_rate // hop_length_to_sampling_rate a = sampling_rate a = padding_value a = mel_filter_bank( num_frequency_bins=1 + n_fft // 2 , num_mel_filters=__UpperCAmelCase , min_frequency=0.0 , max_frequency=22050.0 , sampling_rate=__UpperCAmelCase , norm='''slaney''' , mel_scale='''slaney''' , ).T def __lowerCAmelCase ( self : int , __UpperCAmelCase : np.array ) ->np.ndarray: """simple docstring""" a = spectrogram( __UpperCAmelCase , window_function(self.n_fft , '''hann''' ) , frame_length=self.n_fft , hop_length=self.hop_length , power=2.0 , mel_filters=self.mel_filters.T , log_mel='''dB''' , db_range=80.0 , ) a = log_spec[:, :-1] a = log_spec - 20.0 a = np.clip(log_spec / 40.0 , -2.0 , 0.0 ) + 1.0 return log_spec def __call__( self : Dict , __UpperCAmelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , __UpperCAmelCase : Optional[Union[str, TensorType]] = None , __UpperCAmelCase : Optional[bool] = True , __UpperCAmelCase : Optional[int] = None , __UpperCAmelCase : bool = False , __UpperCAmelCase : bool = False , **__UpperCAmelCase : List[str] , ) ->BatchFeature: """simple docstring""" if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( '''This feature extractor is set to support sampling rate''' F""" of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled""" F""" with {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) a = isinstance(__UpperCAmelCase , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F"""Only mono-channel audio is supported for input to {self}""" ) a = is_batched_numpy or ( isinstance(__UpperCAmelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: a = [np.asarray([speech] , dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(__UpperCAmelCase , np.ndarray ): a = np.asarray(__UpperCAmelCase , dtype=np.floataa ) elif isinstance(__UpperCAmelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): a = raw_speech.astype(np.floataa ) # always return batch if not is_batched: a = [np.asarray([raw_speech] ).T] # Convert audio signals to log mel spectrograms, truncate by time axis a = [ self._np_extract_fbank_features(waveform.squeeze() ).T[: self.spectrogram_length] for waveform in raw_speech ] if isinstance(audio_features[0] , __UpperCAmelCase ): a = [np.asarray(__UpperCAmelCase , dtype=np.floataa ) for feature in audio_features] # Create audio attention mask a = max( [ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len for feature in audio_features] ) # The maximum number of audio patches in a batch if return_attention_mask: a = [ (ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [1] + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [0] for feature in audio_features ] a = np.array(__UpperCAmelCase ).astype(np.floataa ) # convert into correct format for padding a = max_patch_len // self.freq_len * self.patch_size[0] # The maximum audio size in a batch a = np.ones([len(__UpperCAmelCase ), 1, max_time_len, self.feature_size] ).astype(np.floataa ) a = padded_audio_features * self.padding_value for i in range(len(__UpperCAmelCase ) ): a = audio_features[i] a = feature # return as BatchFeature if return_attention_mask: a = {'''audio_values''': padded_audio_features, '''audio_mask''': audio_mask} else: a = {'''audio_values''': padded_audio_features} a = BatchFeature(data=__UpperCAmelCase , tensor_type=__UpperCAmelCase ) return encoded_inputs
26
import argparse import io import requests import torch from omegaconf import OmegaConf from diffusers import AutoencoderKL from diffusers.pipelines.stable_diffusion.convert_from_ckpt import ( assign_to_checkpoint, conv_attn_to_linear, create_vae_diffusers_config, renew_vae_attention_paths, renew_vae_resnet_paths, ) def _a ( a :Union[str, Any] , a :List[Any] ) -> List[Any]: a = checkpoint a = {} a = vae_state_dict['''encoder.conv_in.weight'''] a = vae_state_dict['''encoder.conv_in.bias'''] a = vae_state_dict['''encoder.conv_out.weight'''] a = vae_state_dict['''encoder.conv_out.bias'''] a = vae_state_dict['''encoder.norm_out.weight'''] a = vae_state_dict['''encoder.norm_out.bias'''] a = vae_state_dict['''decoder.conv_in.weight'''] a = vae_state_dict['''decoder.conv_in.bias'''] a = vae_state_dict['''decoder.conv_out.weight'''] a = vae_state_dict['''decoder.conv_out.bias'''] a = vae_state_dict['''decoder.norm_out.weight'''] a = vae_state_dict['''decoder.norm_out.bias'''] a = vae_state_dict['''quant_conv.weight'''] a = vae_state_dict['''quant_conv.bias'''] a = vae_state_dict['''post_quant_conv.weight'''] a = vae_state_dict['''post_quant_conv.bias'''] # Retrieves the keys for the encoder down blocks only a = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''encoder.down''' in layer} ) a = { layer_id: [key for key in vae_state_dict if F"""down.{layer_id}""" in key] for layer_id in range(a ) } # Retrieves the keys for the decoder up blocks only a = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''decoder.up''' in layer} ) a = { layer_id: [key for key in vae_state_dict if F"""up.{layer_id}""" in key] for layer_id in range(a ) } for i in range(a ): a = [key for key in down_blocks[i] if F"""down.{i}""" in key and F"""down.{i}.downsample""" not in key] if F"""encoder.down.{i}.downsample.conv.weight""" in vae_state_dict: a = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.weight""" ) a = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.bias""" ) a = renew_vae_resnet_paths(a ) a = {'''old''': F"""down.{i}.block""", '''new''': F"""down_blocks.{i}.resnets"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''encoder.mid.block''' in key] a = 2 for i in range(1 , num_mid_res_blocks + 1 ): a = [key for key in mid_resnets if F"""encoder.mid.block_{i}""" in key] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""mid.block_{i}""", '''new''': F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''encoder.mid.attn''' in key] a = renew_vae_attention_paths(a ) a = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) conv_attn_to_linear(a ) for i in range(a ): a = num_up_blocks - 1 - i a = [ key for key in up_blocks[block_id] if F"""up.{block_id}""" in key and F"""up.{block_id}.upsample""" not in key ] if F"""decoder.up.{block_id}.upsample.conv.weight""" in vae_state_dict: a = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.weight""" ] a = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.bias""" ] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""up.{block_id}.block""", '''new''': F"""up_blocks.{i}.resnets"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''decoder.mid.block''' in key] a = 2 for i in range(1 , num_mid_res_blocks + 1 ): a = [key for key in mid_resnets if F"""decoder.mid.block_{i}""" in key] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""mid.block_{i}""", '''new''': F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''decoder.mid.attn''' in key] a = renew_vae_attention_paths(a ) a = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) conv_attn_to_linear(a ) return new_checkpoint def _a ( a :str , a :str , ) -> List[str]: # Only support V1 a = requests.get( ''' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml''' ) a = io.BytesIO(r.content ) a = OmegaConf.load(a ) a = 512 a = '''cuda''' if torch.cuda.is_available() else '''cpu''' if checkpoint_path.endswith('''safetensors''' ): from safetensors import safe_open a = {} with safe_open(a , framework='''pt''' , device='''cpu''' ) as f: for key in f.keys(): a = f.get_tensor(a ) else: a = torch.load(a , map_location=a )['''state_dict'''] # Convert the VAE model. a = create_vae_diffusers_config(a , image_size=a ) a = custom_convert_ldm_vae_checkpoint(a , a ) a = AutoencoderKL(**a ) vae.load_state_dict(a ) vae.save_pretrained(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--vae_pt_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") UpperCAmelCase__ = parser.parse_args() vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)
26
1
import random import timeit from functools import wraps from typing import Callable, Optional from ..configuration_utils import PretrainedConfig from ..models.auto.modeling_tf_auto import TF_MODEL_MAPPING, TF_MODEL_WITH_LM_HEAD_MAPPING from ..utils import is_pyanvml_available, is_tf_available, logging from .benchmark_utils import ( Benchmark, Memory, MemorySummary, measure_peak_memory_cpu, start_memory_tracing, stop_memory_tracing, ) if is_tf_available(): import tensorflow as tf from tensorflow.python.framework.errors_impl import ResourceExhaustedError from .benchmark_args_tf import TensorFlowBenchmarkArguments if is_pyanvml_available(): import pyanvml.pyanvml as nvml UpperCAmelCase__ = logging.get_logger(__name__) def _a ( a :bool , a :bool ) -> int: def run_func(a :Union[str, Any] ): @wraps(a ) def run_in_eager_mode(*a :Tuple , **a :List[Any] ): return func(*a , **a ) @wraps(a ) @tf.function(experimental_compile=a ) def run_in_graph_mode(*a :List[str] , **a :Optional[Any] ): return func(*a , **a ) if do_eager_mode is True: if use_xla is not False: raise ValueError( '''Cannot run model in XLA, if `args.eager_mode` is set to `True`. Please set `args.eager_mode=False`.''' ) return run_in_eager_mode else: return run_in_graph_mode return run_func def _a ( a :int , a :int , a :int ) -> ["tf.Tensor"]: a = random.Random() a = [rng.randint(0 , vocab_size - 1 ) for i in range(batch_size * sequence_length )] return tf.constant(a , shape=(batch_size, sequence_length) , dtype=tf.intaa ) class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = 42 __snake_case = 42 __snake_case = "TensorFlow" @property def __lowerCAmelCase ( self : Any ) ->Dict: """simple docstring""" return tf.__version__ def __lowerCAmelCase ( self : int , __UpperCAmelCase : str , __UpperCAmelCase : int , __UpperCAmelCase : int ) ->float: """simple docstring""" a = self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''' ) a = self._prepare_inference_func(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) return self._measure_speed(_inference ) def __lowerCAmelCase ( self : str , __UpperCAmelCase : str , __UpperCAmelCase : int , __UpperCAmelCase : int ) ->float: """simple docstring""" a = self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''' ) a = self._prepare_train_func(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) return self._measure_speed(_train ) def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : str , __UpperCAmelCase : int , __UpperCAmelCase : int ) ->[Memory, Optional[MemorySummary]]: """simple docstring""" if self.args.is_gpu: tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx] , __UpperCAmelCase ) a = self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''' ) a = self._prepare_inference_func(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) return self._measure_memory(_inference ) def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : str , __UpperCAmelCase : int , __UpperCAmelCase : int ) ->[Memory, Optional[MemorySummary]]: """simple docstring""" if self.args.is_gpu: tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx] , __UpperCAmelCase ) a = self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''' ) a = self._prepare_train_func(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) return self._measure_memory(_train ) def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : str , __UpperCAmelCase : int , __UpperCAmelCase : int ) ->Callable[[], None]: """simple docstring""" a = self.config_dict[model_name] if self.args.fpaa: raise NotImplementedError('''Mixed precision is currently not supported.''' ) a = ( hasattr(__UpperCAmelCase , '''architectures''' ) and isinstance(config.architectures , __UpperCAmelCase ) and len(config.architectures ) > 0 ) if not self.args.only_pretrain_model and has_model_class_in_config: try: a = '''TF''' + config.architectures[0] # prepend 'TF' for tensorflow model a = __import__('''transformers''' , fromlist=[model_class] ) a = getattr(__UpperCAmelCase , __UpperCAmelCase ) a = model_cls(__UpperCAmelCase ) except ImportError: raise ImportError( F"""{model_class} does not exist. If you just want to test the pretrained model, you might want to""" ''' set `--only_pretrain_model` or `args.only_pretrain_model=True`.''' ) else: a = TF_MODEL_MAPPING[config.__class__](__UpperCAmelCase ) # encoder-decoder has vocab size saved differently a = config.vocab_size if hasattr(__UpperCAmelCase , '''vocab_size''' ) else config.encoder.vocab_size a = random_input_ids(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_decoder_forward(): return model(__UpperCAmelCase , decoder_input_ids=__UpperCAmelCase , training=__UpperCAmelCase ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_forward(): return model(__UpperCAmelCase , training=__UpperCAmelCase ) a = encoder_decoder_forward if config.is_encoder_decoder else encoder_forward return _inference def __lowerCAmelCase ( self : int , __UpperCAmelCase : str , __UpperCAmelCase : int , __UpperCAmelCase : int ) ->Callable[[], None]: """simple docstring""" a = self.config_dict[model_name] if self.args.eager_mode is not False: raise ValueError('''Training cannot be done in eager mode. Please make sure that `args.eager_mode = False`.''' ) if self.args.fpaa: raise NotImplementedError('''Mixed precision is currently not supported.''' ) a = ( hasattr(__UpperCAmelCase , '''architectures''' ) and isinstance(config.architectures , __UpperCAmelCase ) and len(config.architectures ) > 0 ) if not self.args.only_pretrain_model and has_model_class_in_config: try: a = '''TF''' + config.architectures[0] # prepend 'TF' for tensorflow model a = __import__('''transformers''' , fromlist=[model_class] ) a = getattr(__UpperCAmelCase , __UpperCAmelCase ) a = model_cls(__UpperCAmelCase ) except ImportError: raise ImportError( F"""{model_class} does not exist. If you just want to test the pretrained model, you might want to""" ''' set `--only_pretrain_model` or `args.only_pretrain_model=True`.''' ) else: a = TF_MODEL_WITH_LM_HEAD_MAPPING[config.__class__](__UpperCAmelCase ) # encoder-decoder has vocab size saved differently a = config.vocab_size if hasattr(__UpperCAmelCase , '''vocab_size''' ) else config.encoder.vocab_size a = random_input_ids(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_decoder_train(): a = model(__UpperCAmelCase , decoder_input_ids=__UpperCAmelCase , labels=__UpperCAmelCase , training=__UpperCAmelCase )[0] a = tf.gradients(__UpperCAmelCase , model.trainable_variables ) return gradients @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_train(): a = model(__UpperCAmelCase , labels=__UpperCAmelCase , training=__UpperCAmelCase )[0] a = tf.gradients(__UpperCAmelCase , model.trainable_variables ) return gradients a = encoder_decoder_train if config.is_encoder_decoder else encoder_train return _train def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Dict ) ->float: """simple docstring""" with self.args.strategy.scope(): try: if self.args.is_tpu or self.args.use_xla: # run additional 10 times to stabilize compilation for tpu logger.info('''Do inference on TPU. Running model 5 times to stabilize compilation''' ) timeit.repeat(__UpperCAmelCase , repeat=1 , number=5 ) # as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average a = timeit.repeat( __UpperCAmelCase , repeat=self.args.repeat , number=10 , ) return min(__UpperCAmelCase ) / 10.0 except ResourceExhaustedError as e: self.print_fn(F"""Doesn't fit on GPU. {e}""" ) def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : Callable[[], None] ) ->[Memory, MemorySummary]: """simple docstring""" logger.info( '''Note that TensorFlow allocates more memory than ''' '''it might need to speed up computation. ''' '''The memory reported here corresponds to the memory ''' '''reported by `nvidia-smi`, which can vary depending ''' '''on total available memory on the GPU that is used.''' ) with self.args.strategy.scope(): try: if self.args.trace_memory_line_by_line: if not self.args.eager_mode: raise ValueError( '''`args.eager_mode` is set to `False`. Make sure to run model in eager mode to measure memory''' ''' consumption line by line.''' ) a = start_memory_tracing('''transformers''' ) if self.args.is_tpu: # tpu raise NotImplementedError( '''Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking''' ''' with `args.memory=False`''' ) elif self.args.is_gpu: # gpu if not is_pyanvml_available(): logger.warning( '''py3nvml not installed, we won\'t log GPU memory usage. ''' '''Install py3nvml (pip install py3nvml) to log information about GPU.''' ) a = '''N/A''' else: logger.info( '''Measuring total GPU usage on GPU device. Make sure to not have additional processes''' ''' running on the same GPU.''' ) # init nvml nvml.nvmlInit() func() a = nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx ) a = nvml.nvmlDeviceGetMemoryInfo(__UpperCAmelCase ) a = meminfo.used a = Memory(__UpperCAmelCase ) # shutdown nvml nvml.nvmlShutdown() else: # cpu if self.args.trace_memory_line_by_line: logger.info( '''When enabling line by line tracing, the max peak memory for CPU is inaccurate in''' ''' TensorFlow.''' ) a = None else: a = measure_peak_memory_cpu(__UpperCAmelCase ) a = Memory(__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else memory_bytes if self.args.trace_memory_line_by_line: a = stop_memory_tracing(__UpperCAmelCase ) if memory is None: a = summary.total else: a = None return memory, summary except ResourceExhaustedError as e: self.print_fn(F"""Doesn't fit on GPU. {e}""" ) return "N/A", None
26
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = ['''image_processor''', '''tokenizer'''] __snake_case = '''CLIPImageProcessor''' __snake_case = ('''CLIPTokenizer''', '''CLIPTokenizerFast''') def __init__( self : Dict , __UpperCAmelCase : str=None , __UpperCAmelCase : Optional[Any]=None , **__UpperCAmelCase : Optional[Any] ) ->List[str]: """simple docstring""" a = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __UpperCAmelCase , ) a = kwargs.pop('''feature_extractor''' ) a = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(__UpperCAmelCase , __UpperCAmelCase ) def __call__( self : List[str] , __UpperCAmelCase : Any=None , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Any=None , **__UpperCAmelCase : str ) ->Optional[Any]: """simple docstring""" if text is None and images is None: raise ValueError('''You have to specify either text or images. Both cannot be none.''' ) if text is not None: a = self.tokenizer(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase ) if images is not None: a = self.image_processor(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase ) if text is not None and images is not None: a = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__UpperCAmelCase ) , tensor_type=__UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple , *__UpperCAmelCase : List[str] , **__UpperCAmelCase : Union[str, Any] ) ->Union[str, Any]: """simple docstring""" return self.tokenizer.batch_decode(*__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple , *__UpperCAmelCase : str , **__UpperCAmelCase : Tuple ) ->Any: """simple docstring""" return self.tokenizer.decode(*__UpperCAmelCase , **__UpperCAmelCase ) @property def __lowerCAmelCase ( self : int ) ->List[str]: """simple docstring""" a = self.tokenizer.model_input_names a = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def __lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __UpperCAmelCase , ) return self.image_processor_class @property def __lowerCAmelCase ( self : Union[str, Any] ) ->Optional[int]: """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __UpperCAmelCase , ) return self.image_processor
26
1
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { "transfo-xl-wt103": "https://huggingface.co/transfo-xl-wt103/resolve/main/config.json", } class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = '''transfo-xl''' __snake_case = ['''mems'''] __snake_case = { '''n_token''': '''vocab_size''', '''hidden_size''': '''d_model''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self : str , __UpperCAmelCase : Optional[Any]=267_735 , __UpperCAmelCase : str=[20_000, 40_000, 200_000] , __UpperCAmelCase : List[str]=1_024 , __UpperCAmelCase : int=1_024 , __UpperCAmelCase : int=16 , __UpperCAmelCase : List[str]=64 , __UpperCAmelCase : Tuple=4_096 , __UpperCAmelCase : Optional[Any]=4 , __UpperCAmelCase : Union[str, Any]=False , __UpperCAmelCase : Optional[int]=18 , __UpperCAmelCase : Optional[Any]=1_600 , __UpperCAmelCase : Tuple=1_000 , __UpperCAmelCase : Union[str, Any]=True , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Optional[int]=0 , __UpperCAmelCase : Any=-1 , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Dict=0.1 , __UpperCAmelCase : int=0.0 , __UpperCAmelCase : str=True , __UpperCAmelCase : Union[str, Any]="normal" , __UpperCAmelCase : Optional[int]=0.01 , __UpperCAmelCase : Optional[int]=0.01 , __UpperCAmelCase : Tuple=0.02 , __UpperCAmelCase : Dict=1e-5 , __UpperCAmelCase : List[Any]=0 , **__UpperCAmelCase : int , ) ->Tuple: """simple docstring""" a = vocab_size a = [] self.cutoffs.extend(__UpperCAmelCase ) if proj_share_all_but_first: a = [False] + [True] * len(self.cutoffs ) else: a = [False] + [False] * len(self.cutoffs ) a = d_model a = d_embed a = d_head a = d_inner a = div_val a = pre_lnorm a = n_layer a = n_head a = mem_len a = same_length a = attn_type a = clamp_len a = sample_softmax a = adaptive a = dropout a = dropatt a = untie_r a = init a = init_range a = proj_init_std a = init_std a = layer_norm_epsilon super().__init__(eos_token_id=__UpperCAmelCase , **__UpperCAmelCase ) @property def __lowerCAmelCase ( self : Optional[Any] ) ->Tuple: """simple docstring""" logger.info(F"""The model {self.model_type} is one of the few models that has no sequence length limit.""" ) return -1 @max_position_embeddings.setter def __lowerCAmelCase ( self : str , __UpperCAmelCase : Any ) ->Tuple: """simple docstring""" raise NotImplementedError( F"""The model {self.model_type} is one of the few models that has no sequence length limit.""" )
26
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_distilbert import DistilBertTokenizer UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} UpperCAmelCase__ = { "vocab_file": { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt" ), "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt", "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt" ), }, "tokenizer_file": { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json" ), "distilbert-base-german-cased": ( "https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json" ), "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json" ), }, } UpperCAmelCase__ = { "distilbert-base-uncased": 512, "distilbert-base-uncased-distilled-squad": 512, "distilbert-base-cased": 512, "distilbert-base-cased-distilled-squad": 512, "distilbert-base-german-cased": 512, "distilbert-base-multilingual-cased": 512, } UpperCAmelCase__ = { "distilbert-base-uncased": {"do_lower_case": True}, "distilbert-base-uncased-distilled-squad": {"do_lower_case": True}, "distilbert-base-cased": {"do_lower_case": False}, "distilbert-base-cased-distilled-squad": {"do_lower_case": False}, "distilbert-base-german-cased": {"do_lower_case": False}, "distilbert-base-multilingual-cased": {"do_lower_case": False}, } class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = VOCAB_FILES_NAMES __snake_case = PRETRAINED_VOCAB_FILES_MAP __snake_case = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __snake_case = PRETRAINED_INIT_CONFIGURATION __snake_case = ['''input_ids''', '''attention_mask'''] __snake_case = DistilBertTokenizer def __init__( self : Dict , __UpperCAmelCase : List[Any]=None , __UpperCAmelCase : Optional[int]=None , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Optional[int]="[UNK]" , __UpperCAmelCase : str="[SEP]" , __UpperCAmelCase : Tuple="[PAD]" , __UpperCAmelCase : Any="[CLS]" , __UpperCAmelCase : int="[MASK]" , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Optional[Any]=None , **__UpperCAmelCase : str , ) ->Optional[int]: """simple docstring""" super().__init__( __UpperCAmelCase , tokenizer_file=__UpperCAmelCase , do_lower_case=__UpperCAmelCase , unk_token=__UpperCAmelCase , sep_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , cls_token=__UpperCAmelCase , mask_token=__UpperCAmelCase , tokenize_chinese_chars=__UpperCAmelCase , strip_accents=__UpperCAmelCase , **__UpperCAmelCase , ) a = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , __UpperCAmelCase ) != do_lower_case or normalizer_state.get('''strip_accents''' , __UpperCAmelCase ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , __UpperCAmelCase ) != tokenize_chinese_chars ): a = getattr(__UpperCAmelCase , normalizer_state.pop('''type''' ) ) a = do_lower_case a = strip_accents a = tokenize_chinese_chars a = normalizer_class(**__UpperCAmelCase ) a = do_lower_case def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[int]=None ) ->Optional[Any]: """simple docstring""" a = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [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 __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None ) ->Tuple[str]: """simple docstring""" a = self._tokenizer.model.save(__UpperCAmelCase , name=__UpperCAmelCase ) return tuple(__UpperCAmelCase )
26
1
import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class lowercase_ ( lowercase , lowercase ): '''simple docstring''' @register_to_config def __init__( self : List[str] , *, __UpperCAmelCase : int = 4 , __UpperCAmelCase : int = 768 , __UpperCAmelCase : int , __UpperCAmelCase : List[Any] , ) ->List[Any]: """simple docstring""" super().__init__() a = nn.Parameter(torch.zeros(__UpperCAmelCase ) ) # parameters for additional clip time embeddings a = nn.Linear(__UpperCAmelCase , __UpperCAmelCase ) a = nn.Linear(__UpperCAmelCase , __UpperCAmelCase ) # parameters for encoder hidden states a = clip_extra_context_tokens a = nn.Linear( __UpperCAmelCase , self.clip_extra_context_tokens * cross_attention_dim ) a = nn.Linear(__UpperCAmelCase , __UpperCAmelCase ) a = nn.LayerNorm(__UpperCAmelCase ) def __lowerCAmelCase ( self : List[str] , *, __UpperCAmelCase : int , __UpperCAmelCase : Tuple , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[int] ) ->Union[str, Any]: """simple docstring""" if do_classifier_free_guidance: # Add the classifier free guidance embeddings to the image embeddings a = image_embeddings.shape[0] a = self.learned_classifier_free_guidance_embeddings.unsqueeze(0 ) a = classifier_free_guidance_embeddings.expand( __UpperCAmelCase , -1 ) a = torch.cat([classifier_free_guidance_embeddings, image_embeddings] , dim=0 ) # The image embeddings batch size and the text embeddings batch size are equal assert image_embeddings.shape[0] == prompt_embeds.shape[0] a = prompt_embeds.shape[0] # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and # adding CLIP embeddings to the existing timestep embedding, ... a = self.embedding_proj(__UpperCAmelCase ) a = self.clip_image_embeddings_project_to_time_embeddings(__UpperCAmelCase ) a = time_projected_image_embeddings + time_projected_prompt_embeds # ... and by projecting CLIP embeddings into four # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder" a = self.clip_extra_context_tokens_proj(__UpperCAmelCase ) a = clip_extra_context_tokens.reshape(__UpperCAmelCase , -1 , self.clip_extra_context_tokens ) a = clip_extra_context_tokens.permute(0 , 2 , 1 ) a = self.encoder_hidden_states_proj(__UpperCAmelCase ) a = self.text_encoder_hidden_states_norm(__UpperCAmelCase ) a = torch.cat([clip_extra_context_tokens, text_encoder_hidden_states] , dim=1 ) return text_encoder_hidden_states, additive_clip_time_embeddings
26
from __future__ import annotations import typing from collections import Counter def _a ( a :int ) -> typing.Counter[int]: a = Counter() for base in range(1 , max_perimeter + 1 ): for perpendicular in range(a , max_perimeter + 1 ): a = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(a ): a = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def _a ( a :int = 1_000 ) -> int: a = pythagorean_triple(a ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(f"""Perimeter {solution()} has maximum solutions""")
26
1
from math import pi, sqrt def _a ( a :float ) -> float: if num <= 0: raise ValueError('''math domain error''' ) if num > 171.5: raise OverflowError('''math range error''' ) elif num - int(a ) not in (0, 0.5): raise NotImplementedError('''num must be an integer or a half-integer''' ) elif num == 0.5: return sqrt(a ) else: return 1.0 if num == 1 else (num - 1) * gamma(num - 1 ) def _a ( ) -> None: assert gamma(0.5 ) == sqrt(a ) assert gamma(1 ) == 1.0 assert gamma(2 ) == 1.0 if __name__ == "__main__": from doctest import testmod testmod() UpperCAmelCase__ = 1.0 while num: UpperCAmelCase__ = float(input("Gamma of: ")) print(f"""gamma({num}) = {gamma(num)}""") print("\nEnter 0 to exit...")
26
from __future__ import annotations def _a ( a :dict , a :str ) -> set[str]: a , a = set(a ), [start] while stack: a = stack.pop() explored.add(a ) # 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(a ) return explored UpperCAmelCase__ = { "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"))
26
1
import unittest from transformers import load_tool from .test_tools_common import ToolTesterMixin class lowercase_ ( unittest.TestCase , lowercase ): '''simple docstring''' def __lowerCAmelCase ( self : str ) ->str: """simple docstring""" a = load_tool('''text-classification''' ) self.tool.setup() a = load_tool('''text-classification''' , remote=__UpperCAmelCase ) def __lowerCAmelCase ( self : int ) ->str: """simple docstring""" a = self.tool('''That\'s quite cool''' , ['''positive''', '''negative'''] ) self.assertEqual(__UpperCAmelCase , '''positive''' ) def __lowerCAmelCase ( self : str ) ->Optional[int]: """simple docstring""" a = self.remote_tool('''That\'s quite cool''' , ['''positive''', '''negative'''] ) self.assertEqual(__UpperCAmelCase , '''positive''' ) def __lowerCAmelCase ( self : Optional[int] ) ->Optional[int]: """simple docstring""" a = self.tool(text='''That\'s quite cool''' , labels=['''positive''', '''negative'''] ) self.assertEqual(__UpperCAmelCase , '''positive''' ) def __lowerCAmelCase ( self : str ) ->List[str]: """simple docstring""" a = self.remote_tool(text='''That\'s quite cool''' , labels=['''positive''', '''negative'''] ) self.assertEqual(__UpperCAmelCase , '''positive''' )
26
import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm UpperCAmelCase__ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex UpperCAmelCase__ = 10 UpperCAmelCase__ = 256 def _a ( a :List[str] ) -> Optional[MinHash]: if len(a ) < MIN_NUM_TOKENS: return None a = MinHash(num_perm=a ) for token in set(a ): min_hash.update(token.encode() ) return min_hash def _a ( a :str ) -> Set[str]: return {t for t in NON_ALPHA.split(a ) if len(t.strip() ) > 0} class lowercase_ : '''simple docstring''' def __init__( self : Any , *, __UpperCAmelCase : float = 0.85 , ) ->Dict: """simple docstring""" a = duplication_jaccard_threshold a = NUM_PERM a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) a = defaultdict(__UpperCAmelCase ) def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Tuple , __UpperCAmelCase : MinHash ) ->None: """simple docstring""" a = self._index.query(__UpperCAmelCase ) if code_key in self._index.keys: print(F"""Duplicate key {code_key}""" ) return self._index.insert(__UpperCAmelCase , __UpperCAmelCase ) if len(__UpperCAmelCase ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(__UpperCAmelCase ) break else: self._duplicate_clusters[close_duplicates[0]].add(__UpperCAmelCase ) def __lowerCAmelCase ( self : Dict ) ->List[List[Dict]]: """simple docstring""" a = [] for base, duplicates in self._duplicate_clusters.items(): a = [base] + list(__UpperCAmelCase ) # reformat the cluster to be a list of dict a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(__UpperCAmelCase ) return duplicate_clusters def __lowerCAmelCase ( self : Any , __UpperCAmelCase : Dict ) ->None: """simple docstring""" a = self.get_duplicate_clusters() with open(__UpperCAmelCase , '''w''' ) as f: json.dump(__UpperCAmelCase , __UpperCAmelCase ) def _a ( a :List[Any] ) -> List[Any]: a , a = element a = get_min_hash([t for t in NON_ALPHA.split(data['''content'''] ) if len(t.strip() ) > 0] ) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def _a ( a :Type[Dataset] ) -> List[Any]: with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(a , max_queue_size=10_000 ) , chunksize=100 , ): if data is not None: yield data def _a ( a :Type[Dataset] , a :float ) -> str: a = DuplicationIndex(duplication_jaccard_threshold=a ) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(a ) ) , max_queue_size=100 ) ): di.add(a , a ) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def _a ( a :str , a :str ) -> float: a = get_tokens(a ) a = get_tokens(a ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) UpperCAmelCase__ = None def _a ( a :Tuple , a :Tuple ) -> Any: a = [] for elementa in cluster: a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(a , a ) >= jaccard_threshold: elementa["copies"] += 1 break else: a = 1 extremes.append(a ) return extremes def _a ( a :List[Any] , a :Optional[Any] , a :Union[str, Any] ) -> Optional[int]: global _shared_dataset a = dataset a = [] a = partial(_find_cluster_extremes_shared , jaccard_threshold=a ) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( a , a , ) , total=len(a ) , ): extremes_list.append(a ) return extremes_list def _a ( a :Type[Dataset] , a :float = 0.85 ) -> Tuple[Type[Dataset], List[List[Dict]]]: a = make_duplicate_clusters(a , a ) a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} a = {} a = find_extremes(a , a , a ) for extremes in extremes_clusters: for element in extremes: a = element a = duplicate_indices - set(extreme_dict.keys() ) a = dataset.filter(lambda a , a : idx not in remove_indices , with_indices=a ) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: a = element['''base_index'''] in extreme_dict if element["is_extreme"]: a = extreme_dict[element['''base_index''']]['''copies'''] print(F"""Original dataset size: {len(a )}""" ) print(F"""Number of duplicate clusters: {len(a )}""" ) print(F"""Files in duplicate cluster: {len(a )}""" ) print(F"""Unique files in duplicate cluster: {len(a )}""" ) print(F"""Filtered dataset size: {len(a )}""" ) return ds_filter, duplicate_clusters
26
1
class lowercase_ : '''simple docstring''' def __init__( self : Tuple ) ->Optional[Any]: """simple docstring""" a = {} def __lowerCAmelCase ( self : str ) ->None: """simple docstring""" print(self.vertex ) for i in self.vertex: print(__UpperCAmelCase , ''' -> ''' , ''' -> '''.join([str(__UpperCAmelCase ) for j in self.vertex[i]] ) ) def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : int , __UpperCAmelCase : int ) ->None: """simple docstring""" if from_vertex in self.vertex: self.vertex[from_vertex].append(__UpperCAmelCase ) else: # else make a new vertex a = [to_vertex] def __lowerCAmelCase ( self : Dict ) ->None: """simple docstring""" a = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : int , __UpperCAmelCase : int , __UpperCAmelCase : list ) ->None: """simple docstring""" a = True print(__UpperCAmelCase , end=''' ''' ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(__UpperCAmelCase , __UpperCAmelCase ) if __name__ == "__main__": UpperCAmelCase__ = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print("DFS:") g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
26
from math import ceil, sqrt def _a ( a :int = 1_000_000 ) -> int: a = 0 for outer_width in range(3 , (limit // 4) + 2 ): if outer_width**2 > limit: a = max(ceil(sqrt(outer_width**2 - limit ) ) , 1 ) else: a = 1 if (outer_width - hole_width_lower_bound) % 2: hole_width_lower_bound += 1 answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 return answer if __name__ == "__main__": print(f"""{solution() = }""")
26
1
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, PNDMScheduler, StableDiffusionLDMaDPipeline, UNetaDConditionModel, ) from diffusers.utils import nightly, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS enable_full_determinism() class lowercase_ ( unittest.TestCase ): '''simple docstring''' __snake_case = StableDiffusionLDMaDPipeline __snake_case = TEXT_TO_IMAGE_PARAMS __snake_case = TEXT_TO_IMAGE_BATCH_PARAMS __snake_case = TEXT_TO_IMAGE_IMAGE_PARAMS def __lowerCAmelCase ( self : str ) ->int: """simple docstring""" torch.manual_seed(0 ) a = 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 , ) a = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=__UpperCAmelCase , set_alpha_to_one=__UpperCAmelCase , ) torch.manual_seed(0 ) a = AutoencoderKL( block_out_channels=[32, 64] , in_channels=6 , out_channels=6 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) a = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) a = CLIPTextModel(__UpperCAmelCase ) a = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) a = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : List[str]=0 ) ->Optional[int]: """simple docstring""" if str(__UpperCAmelCase ).startswith('''mps''' ): a = torch.manual_seed(__UpperCAmelCase ) else: a = torch.Generator(device=__UpperCAmelCase ).manual_seed(__UpperCAmelCase ) a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def __lowerCAmelCase ( self : Optional[Any] ) ->Union[str, Any]: """simple docstring""" a = '''cpu''' # ensure determinism for the device-dependent torch.Generator a = self.get_dummy_components() a = StableDiffusionLDMaDPipeline(**__UpperCAmelCase ) a = ldmad_pipe.to(__UpperCAmelCase ) ldmad_pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_dummy_inputs(__UpperCAmelCase ) a = ldmad_pipe(**__UpperCAmelCase ) a , a = output.rgb, output.depth a = rgb[0, -3:, -3:, -1] a = depth[0, -3:, -1] assert rgb.shape == (1, 64, 64, 3) assert depth.shape == (1, 64, 64) a = np.array( [0.37338176, 0.70247, 0.74203193, 0.51643604, 0.58256793, 0.60932136, 0.4181095, 0.48355877, 0.46535262] ) a = np.array([103.46727, 85.812004, 87.849236] ) assert np.abs(image_slice_rgb.flatten() - expected_slice_rgb ).max() < 1e-2 assert np.abs(image_slice_depth.flatten() - expected_slice_depth ).max() < 1e-2 def __lowerCAmelCase ( self : Optional[Any] ) ->List[str]: """simple docstring""" a = self.get_dummy_components() a = StableDiffusionLDMaDPipeline(**__UpperCAmelCase ) a = ldmad_pipe.to(__UpperCAmelCase ) ldmad_pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_dummy_inputs(__UpperCAmelCase ) a = 3 * [inputs['''prompt''']] # forward a = ldmad_pipe(**__UpperCAmelCase ) a , a = output.rgb, output.depth a = rgb_slice_a[0, -3:, -3:, -1] a = depth_slice_a[0, -3:, -1] a = self.get_dummy_inputs(__UpperCAmelCase ) a = 3 * [inputs.pop('''prompt''' )] a = ldmad_pipe.tokenizer( __UpperCAmelCase , padding='''max_length''' , max_length=ldmad_pipe.tokenizer.model_max_length , truncation=__UpperCAmelCase , return_tensors='''pt''' , ) a = text_inputs['''input_ids'''].to(__UpperCAmelCase ) a = ldmad_pipe.text_encoder(__UpperCAmelCase )[0] a = prompt_embeds # forward a = ldmad_pipe(**__UpperCAmelCase ) a , a = output.rgb, output.depth a = rgb_slice_a[0, -3:, -3:, -1] a = depth_slice_a[0, -3:, -1] assert np.abs(rgb_slice_a.flatten() - rgb_slice_a.flatten() ).max() < 1e-4 assert np.abs(depth_slice_a.flatten() - depth_slice_a.flatten() ).max() < 1e-4 def __lowerCAmelCase ( self : List[str] ) ->Tuple: """simple docstring""" a = '''cpu''' # ensure determinism for the device-dependent torch.Generator a = self.get_dummy_components() a = PNDMScheduler(skip_prk_steps=__UpperCAmelCase ) a = StableDiffusionLDMaDPipeline(**__UpperCAmelCase ) a = ldmad_pipe.to(__UpperCAmelCase ) ldmad_pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_dummy_inputs(__UpperCAmelCase ) a = '''french fries''' a = ldmad_pipe(**__UpperCAmelCase , negative_prompt=__UpperCAmelCase ) a , a = output.rgb, output.depth a = rgb[0, -3:, -3:, -1] a = depth[0, -3:, -1] assert rgb.shape == (1, 64, 64, 3) assert depth.shape == (1, 64, 64) a = np.array( [0.37044, 0.71811503, 0.7223251, 0.48603675, 0.5638391, 0.6364948, 0.42833704, 0.4901315, 0.47926217] ) a = np.array([107.84738, 84.62802, 89.962135] ) assert np.abs(rgb_slice.flatten() - expected_slice_rgb ).max() < 1e-2 assert np.abs(depth_slice.flatten() - expected_slice_depth ).max() < 1e-2 @slow @require_torch_gpu class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self : Tuple ) ->List[str]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : List[str]="cpu" , __UpperCAmelCase : Any=torch.floataa , __UpperCAmelCase : List[str]=0 ) ->int: """simple docstring""" a = torch.Generator(device=__UpperCAmelCase ).manual_seed(__UpperCAmelCase ) a = np.random.RandomState(__UpperCAmelCase ).standard_normal((1, 4, 64, 64) ) a = torch.from_numpy(__UpperCAmelCase ).to(device=__UpperCAmelCase , dtype=__UpperCAmelCase ) a = { '''prompt''': '''a photograph of an astronaut riding a horse''', '''latents''': latents, '''generator''': generator, '''num_inference_steps''': 3, '''guidance_scale''': 7.5, '''output_type''': '''numpy''', } return inputs def __lowerCAmelCase ( self : Optional[int] ) ->List[Any]: """simple docstring""" a = StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d''' ) a = ldmad_pipe.to(__UpperCAmelCase ) ldmad_pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_inputs(__UpperCAmelCase ) a = ldmad_pipe(**__UpperCAmelCase ) a , a = output.rgb, output.depth a = rgb[0, -3:, -3:, -1].flatten() a = rgb[0, -3:, -1].flatten() assert rgb.shape == (1, 512, 512, 3) assert depth.shape == (1, 512, 512) a = np.array( [0.53805465, 0.56707305, 0.5486515, 0.57012236, 0.5814511, 0.56253487, 0.54843014, 0.55092263, 0.6459706] ) a = np.array( [0.9263781, 0.6678672, 0.5486515, 0.92202145, 0.67831135, 0.56253487, 0.9241694, 0.7551478, 0.6459706] ) assert np.abs(rgb_slice - expected_slice_rgb ).max() < 3e-3 assert np.abs(depth_slice - expected_slice_depth ).max() < 3e-3 @nightly @require_torch_gpu class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self : str ) ->str: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : List[Any]="cpu" , __UpperCAmelCase : Optional[int]=torch.floataa , __UpperCAmelCase : Optional[int]=0 ) ->Optional[int]: """simple docstring""" a = torch.Generator(device=__UpperCAmelCase ).manual_seed(__UpperCAmelCase ) a = np.random.RandomState(__UpperCAmelCase ).standard_normal((1, 4, 64, 64) ) a = torch.from_numpy(__UpperCAmelCase ).to(device=__UpperCAmelCase , dtype=__UpperCAmelCase ) a = { '''prompt''': '''a photograph of an astronaut riding a horse''', '''latents''': latents, '''generator''': generator, '''num_inference_steps''': 50, '''guidance_scale''': 7.5, '''output_type''': '''numpy''', } return inputs def __lowerCAmelCase ( self : Optional[int] ) ->Any: """simple docstring""" a = StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d''' ).to(__UpperCAmelCase ) ldmad_pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_inputs(__UpperCAmelCase ) a = ldmad_pipe(**__UpperCAmelCase ) a , a = output.rgb, output.depth a = 0.495586 a = 0.33795515 a = 112.48518 a = 98.489746 assert np.abs(expected_rgb_mean - rgb.mean() ) < 1e-3 assert np.abs(expected_rgb_std - rgb.std() ) < 1e-3 assert np.abs(expected_depth_mean - depth.mean() ) < 1e-3 assert np.abs(expected_depth_std - depth.std() ) < 1e-3 def __lowerCAmelCase ( self : List[Any] ) ->Optional[Any]: """simple docstring""" a = StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d-4c''' ).to(__UpperCAmelCase ) ldmad_pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_inputs(__UpperCAmelCase ) a = ldmad_pipe(**__UpperCAmelCase ) a , a = output.rgb, output.depth a = 0.4194127 a = 0.35375586 a = 0.5638502 a = 0.34686103 assert rgb.shape == (1, 512, 512, 3) assert depth.shape == (1, 512, 512, 1) assert np.abs(expected_rgb_mean - rgb.mean() ) < 1e-3 assert np.abs(expected_rgb_std - rgb.std() ) < 1e-3 assert np.abs(expected_depth_mean - depth.mean() ) < 1e-3 assert np.abs(expected_depth_std - depth.std() ) < 1e-3
26
UpperCAmelCase__ = "0.21.0" from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
26
1
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar UpperCAmelCase__ = TypeVar("T") class lowercase_ ( Generic[T] ): '''simple docstring''' def __init__( self : Optional[Any] , __UpperCAmelCase : T ) ->Any: """simple docstring""" a = data a = None def __str__( self : Optional[int] ) ->str: """simple docstring""" return F"""{self.data}""" class lowercase_ ( Generic[T] ): '''simple docstring''' def __init__( self : List[str] ) ->None: """simple docstring""" a = None def __iter__( self : str ) ->Iterator[T]: """simple docstring""" a = self.top while node: yield node.data a = node.next def __str__( self : Optional[int] ) ->str: """simple docstring""" return "->".join([str(__UpperCAmelCase ) for item in self] ) def __len__( self : Any ) ->int: """simple docstring""" return len(tuple(iter(self ) ) ) def __lowerCAmelCase ( self : Optional[int] ) ->bool: """simple docstring""" return self.top is None def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : T ) ->None: """simple docstring""" a = Node(__UpperCAmelCase ) if not self.is_empty(): a = self.top a = node def __lowerCAmelCase ( self : str ) ->T: """simple docstring""" if self.is_empty(): raise IndexError('''pop from empty stack''' ) assert isinstance(self.top , __UpperCAmelCase ) a = self.top a = self.top.next return pop_node.data def __lowerCAmelCase ( self : Optional[int] ) ->T: """simple docstring""" if self.is_empty(): raise IndexError('''peek from empty stack''' ) assert self.top is not None return self.top.data def __lowerCAmelCase ( self : Tuple ) ->None: """simple docstring""" a = None if __name__ == "__main__": from doctest import testmod testmod()
26
def _a ( a :list ) -> list: if len(a ) <= 1: return lst a = 1 while i < len(a ): if lst[i - 1] <= lst[i]: i += 1 else: a , a = lst[i], lst[i - 1] i -= 1 if i == 0: a = 1 return lst if __name__ == "__main__": UpperCAmelCase__ = input("Enter numbers separated by a comma:\n").strip() UpperCAmelCase__ = [int(item) for item in user_input.split(",")] print(gnome_sort(unsorted))
26
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available UpperCAmelCase__ = { "configuration_roc_bert": ["ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "RoCBertConfig"], "tokenization_roc_bert": ["RoCBertTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: pass try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST", "RoCBertForCausalLM", "RoCBertForMaskedLM", "RoCBertForMultipleChoice", "RoCBertForPreTraining", "RoCBertForQuestionAnswering", "RoCBertForSequenceClassification", "RoCBertForTokenClassification", "RoCBertLayer", "RoCBertModel", "RoCBertPreTrainedModel", "load_tf_weights_in_roc_bert", ] if TYPE_CHECKING: from .configuration_roc_bert import ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RoCBertConfig from .tokenization_roc_bert import RoCBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: raise OptionalDependencyNotAvailable() try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roc_bert import ( ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, RoCBertForCausalLM, RoCBertForMaskedLM, RoCBertForMultipleChoice, RoCBertForPreTraining, RoCBertForQuestionAnswering, RoCBertForSequenceClassification, RoCBertForTokenClassification, RoCBertLayer, RoCBertModel, RoCBertPreTrainedModel, load_tf_weights_in_roc_bert, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCAmelCase__ = { "configuration_deberta": ["DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP", "DebertaConfig", "DebertaOnnxConfig"], "tokenization_deberta": ["DebertaTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ["DebertaTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "DebertaForMaskedLM", "DebertaForQuestionAnswering", "DebertaForSequenceClassification", "DebertaForTokenClassification", "DebertaModel", "DebertaPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "TFDebertaForMaskedLM", "TFDebertaForQuestionAnswering", "TFDebertaForSequenceClassification", "TFDebertaForTokenClassification", "TFDebertaModel", "TFDebertaPreTrainedModel", ] if TYPE_CHECKING: from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig, DebertaOnnxConfig from .tokenization_deberta import DebertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_deberta_fast import DebertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deberta import ( DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, DebertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deberta import ( TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFDebertaForMaskedLM, TFDebertaForQuestionAnswering, TFDebertaForSequenceClassification, TFDebertaForTokenClassification, TFDebertaModel, TFDebertaPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
1
import argparse import shutil import time from json import JSONDecodeError from logging import getLogger from pathlib import Path from typing import Dict, List import torch from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from utils import ( SeqaSeqDataset, calculate_bleu, calculate_rouge, chunks, lmap, load_json, parse_numeric_n_bool_cl_kwargs, save_json, use_task_specific_params, write_txt_file, ) UpperCAmelCase__ = getLogger(__name__) def _a ( a :Optional[Any] , a :str , a :str , a :int = 8 , a :int = 1_024 , a :Optional[Any]="val" , a :List[str]=None , a :List[str]=False , a :Optional[int]="summarization" , a :Any=None , a :List[Any]=1 , a :Dict = None , a :Tuple="" , **a :str , ) -> Dict: a = str(a ) assert local_rank is not None torch.distributed.init_process_group(backend='''nccl''' , rank=a ) a = Path(a ) a = save_dir.joinpath(F"""rank_{local_rank}_output.json""" ) torch.cuda.set_device(a ) a = AutoModelForSeqaSeqLM.from_pretrained(a ).cuda() if fpaa: a = model.half() # determine if we need to increase num_beams use_task_specific_params(a , a ) # update config with task specific params a = generate_kwargs.pop('''num_beams''' , model.config.num_beams ) # AttributeError risk? if num_return_sequences > num_beams: a = num_return_sequences a = AutoTokenizer.from_pretrained(a ) logger.info(F"""Inferred tokenizer type: {tokenizer.__class__}""" ) # if this is wrong, check config.model_type. if max_source_length is None: a = tokenizer.model_max_length if prefix is None: a = prefix or getattr(model.config , '''prefix''' , '''''' ) or '''''' a = SeqaSeqDataset( a , a , a , max_target_length=1_024 , type_path=a , n_obs=a , prefix=a , **a , ) # I set shuffle=True for a more accurate progress bar. # If all the longest samples are first, the prog bar estimate is too high at the beginning. a = ds.make_sortish_sampler(a , distributed=a , add_extra_examples=a , shuffle=a ) a = DataLoader(a , sampler=a , batch_size=a , collate_fn=ds.collate_fn ) a = [] for batch in tqdm(a ): a = model.generate( input_ids=batch['''input_ids'''].to(model.device ) , attention_mask=batch['''attention_mask'''].to(model.device ) , num_return_sequences=a , num_beams=a , **a , ) a = tokenizer.batch_decode(a , skip_special_tokens=a , clean_up_tokenization_spaces=a ) a = batch['''ids'''] if num_return_sequences > 1: a = chunks(a , a ) # batch size chunks, each of size num_return_seq for i, pred in enumerate(a ): results.append({'''pred''': pred, '''id''': ids[i].item()} ) save_json(a , a ) return results, sampler.num_replicas def _a ( ) -> Dict: a = argparse.ArgumentParser( epilog='''Unspecified args like --num_beams=2 --decoder_start_token_id=4 are passed to model.generate''' ) parser.add_argument('''--data_dir''' , type=a , help='''like cnn_dm/test.source''' ) parser.add_argument( '''--model_name''' , type=a , help='''like facebook/bart-large-cnn,t5-base, etc.''' , default='''sshleifer/distilbart-xsum-12-3''' , ) parser.add_argument('''--save_dir''' , type=a , help='''where to save''' , default='''tmp_gen''' ) parser.add_argument('''--max_source_length''' , type=a , default=a ) parser.add_argument( '''--type_path''' , type=a , default='''test''' , help='''which subset to evaluate typically train/val/test''' ) parser.add_argument('''--task''' , type=a , default='''summarization''' , help='''used for task_specific_params + metrics''' ) parser.add_argument('''--bs''' , type=a , default=8 , required=a , help='''batch size''' ) parser.add_argument( '''--local_rank''' , type=a , default=-1 , required=a , help='''should be passed by distributed.launch''' ) parser.add_argument( '''--n_obs''' , type=a , default=a , required=a , help='''How many observations. Defaults to all.''' ) parser.add_argument( '''--num_return_sequences''' , type=a , default=1 , required=a , help='''How many sequences to return''' ) parser.add_argument( '''--sync_timeout''' , type=a , default=600 , required=a , help='''How long should master process wait for other processes to finish.''' , ) parser.add_argument('''--src_lang''' , type=a , default=a , required=a ) parser.add_argument('''--tgt_lang''' , type=a , default=a , required=a ) parser.add_argument( '''--prefix''' , type=a , required=a , default=a , help='''will be added to the begininng of src examples''' ) parser.add_argument('''--fp16''' , action='''store_true''' ) parser.add_argument('''--debug''' , action='''store_true''' ) a = time.time() a , a = parser.parse_known_args() a = parse_numeric_n_bool_cl_kwargs(a ) if generate_kwargs and args.local_rank <= 0: print(F"""parsed the following generate kwargs: {generate_kwargs}""" ) a = Path(args.save_dir + '''_tmp''' ) Path(a ).mkdir(exist_ok=a ) # this handles locking. a = list(json_save_dir.glob('''rank_*.json''' ) ) if intermediate_files: raise ValueError(F"""Found files at {json_save_dir} please move or remove them.""" ) # In theory, a node could finish and save before another node hits this. If this happens, we can address later. a = {} if args.src_lang is not None: a = args.src_lang if args.tgt_lang is not None: a = args.tgt_lang Path(args.save_dir ).mkdir(exist_ok=a ) a , a = eval_data_dir( args.data_dir , a , args.model_name , type_path=args.type_path , bs=args.bs , fpaa=args.fpaa , task=args.task , local_rank=args.local_rank , n_obs=args.n_obs , max_source_length=args.max_source_length , num_return_sequences=args.num_return_sequences , prefix=args.prefix , dataset_kwargs=a , **a , ) if args.local_rank <= 0: a = Path(args.save_dir ) save_dir.mkdir(exist_ok=a ) a = gather_results_from_each_node(a , a , args.sync_timeout ) a = combine_partial_results(a ) if args.num_return_sequences > 1: a = save_dir.joinpath('''pseudolabel_results.json''' ) print(F"""Saving aggregated results at {save_path}, intermediate in {json_save_dir}/""" ) save_json(a , a ) return a = Path(args.data_dir ).joinpath(args.type_path + '''.target''' ) with open(a ) as f: a = [x.rstrip() for x in f.readlines()][: len(a )] # Calculate metrics, save metrics, and save _generations.txt a = '''translation''' in args.task a = calculate_bleu if calc_bleu else calculate_rouge a = '''bleu''' if calc_bleu else '''rouge''' a = score_fn(a , a ) a = len(a ) a = time.time() - start_time a = round(runtime / metrics['''n_obs'''] , 4 ) a = num_replicas # TODO(@stas00): add whatever metadata to metrics a = save_dir.joinpath(F"""{args.type_path}_{metric_name}.json""" ) save_json(a , a , indent=a ) print(a ) write_txt_file(a , save_dir.joinpath(F"""{args.type_path}_generations.txt""" ) ) if args.debug: write_txt_file(a , save_dir.joinpath(F"""{args.type_path}.target""" ) ) else: shutil.rmtree(a ) def _a ( a :str ) -> List: a = [] for partial_result in partial_results: records.extend(a ) a = sorted(a , key=lambda a : x["id"] ) a = [x['''pred'''] for x in records] return preds def _a ( a :int , a :Any , a :Optional[int] ) -> List[Dict[str, List]]: # WAIT FOR lots of .json files a = time.time() logger.info('''waiting for all nodes to finish''' ) a = None while (time.time() - start_wait) < timeout: a = list(save_dir.glob('''rank_*.json''' ) ) if len(a ) < num_replicas: continue try: # make sure all json files are fully saved a = lmap(a , a ) return json_data except JSONDecodeError: continue else: raise TimeoutError('''Rank 0 gave up on waiting for other processes''' ) # Unreachable if __name__ == "__main__": # Usage for MT: run_generate()
26
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all feature extractors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...feature_extraction_utils import FeatureExtractionMixin from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_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, ) UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = OrderedDict( [ ("audio-spectrogram-transformer", "ASTFeatureExtractor"), ("beit", "BeitFeatureExtractor"), ("chinese_clip", "ChineseCLIPFeatureExtractor"), ("clap", "ClapFeatureExtractor"), ("clip", "CLIPFeatureExtractor"), ("clipseg", "ViTFeatureExtractor"), ("conditional_detr", "ConditionalDetrFeatureExtractor"), ("convnext", "ConvNextFeatureExtractor"), ("cvt", "ConvNextFeatureExtractor"), ("data2vec-audio", "Wav2Vec2FeatureExtractor"), ("data2vec-vision", "BeitFeatureExtractor"), ("deformable_detr", "DeformableDetrFeatureExtractor"), ("deit", "DeiTFeatureExtractor"), ("detr", "DetrFeatureExtractor"), ("dinat", "ViTFeatureExtractor"), ("donut-swin", "DonutFeatureExtractor"), ("dpt", "DPTFeatureExtractor"), ("encodec", "EncodecFeatureExtractor"), ("flava", "FlavaFeatureExtractor"), ("glpn", "GLPNFeatureExtractor"), ("groupvit", "CLIPFeatureExtractor"), ("hubert", "Wav2Vec2FeatureExtractor"), ("imagegpt", "ImageGPTFeatureExtractor"), ("layoutlmv2", "LayoutLMv2FeatureExtractor"), ("layoutlmv3", "LayoutLMv3FeatureExtractor"), ("levit", "LevitFeatureExtractor"), ("maskformer", "MaskFormerFeatureExtractor"), ("mctct", "MCTCTFeatureExtractor"), ("mobilenet_v1", "MobileNetV1FeatureExtractor"), ("mobilenet_v2", "MobileNetV2FeatureExtractor"), ("mobilevit", "MobileViTFeatureExtractor"), ("nat", "ViTFeatureExtractor"), ("owlvit", "OwlViTFeatureExtractor"), ("perceiver", "PerceiverFeatureExtractor"), ("poolformer", "PoolFormerFeatureExtractor"), ("regnet", "ConvNextFeatureExtractor"), ("resnet", "ConvNextFeatureExtractor"), ("segformer", "SegformerFeatureExtractor"), ("sew", "Wav2Vec2FeatureExtractor"), ("sew-d", "Wav2Vec2FeatureExtractor"), ("speech_to_text", "Speech2TextFeatureExtractor"), ("speecht5", "SpeechT5FeatureExtractor"), ("swiftformer", "ViTFeatureExtractor"), ("swin", "ViTFeatureExtractor"), ("swinv2", "ViTFeatureExtractor"), ("table-transformer", "DetrFeatureExtractor"), ("timesformer", "VideoMAEFeatureExtractor"), ("tvlt", "TvltFeatureExtractor"), ("unispeech", "Wav2Vec2FeatureExtractor"), ("unispeech-sat", "Wav2Vec2FeatureExtractor"), ("van", "ConvNextFeatureExtractor"), ("videomae", "VideoMAEFeatureExtractor"), ("vilt", "ViltFeatureExtractor"), ("vit", "ViTFeatureExtractor"), ("vit_mae", "ViTFeatureExtractor"), ("vit_msn", "ViTFeatureExtractor"), ("wav2vec2", "Wav2Vec2FeatureExtractor"), ("wav2vec2-conformer", "Wav2Vec2FeatureExtractor"), ("wavlm", "Wav2Vec2FeatureExtractor"), ("whisper", "WhisperFeatureExtractor"), ("xclip", "CLIPFeatureExtractor"), ("yolos", "YolosFeatureExtractor"), ] ) UpperCAmelCase__ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES) def _a ( a :str ) -> Any: for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items(): if class_name in extractors: a = model_type_to_module_name(a ) a = importlib.import_module(F""".{module_name}""" , '''transformers.models''' ) try: return getattr(a , a ) except AttributeError: continue for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items(): if getattr(a , '''__name__''' , a ) == 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. a = importlib.import_module('''transformers''' ) if hasattr(a , a ): return getattr(a , a ) return None def _a ( a :Union[str, os.PathLike] , a :Optional[Union[str, os.PathLike]] = None , a :bool = False , a :bool = False , a :Optional[Dict[str, str]] = None , a :Optional[Union[bool, str]] = None , a :Optional[str] = None , a :bool = False , **a :int , ) -> Tuple: a = get_file_from_repo( a , a , cache_dir=a , force_download=a , resume_download=a , proxies=a , use_auth_token=a , revision=a , local_files_only=a , ) if resolved_config_file is None: logger.info( '''Could not locate the feature extractor configuration file, will try to use the model config instead.''' ) return {} with open(a , encoding='''utf-8''' ) as reader: return json.load(a ) class lowercase_ : '''simple docstring''' def __init__( self : Tuple ) ->int: """simple docstring""" raise EnvironmentError( '''AutoFeatureExtractor is designed to be instantiated ''' '''using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.''' ) @classmethod @replace_list_option_in_docstrings(__UpperCAmelCase ) def __lowerCAmelCase ( cls : int , __UpperCAmelCase : Optional[Any] , **__UpperCAmelCase : Dict ) ->List[Any]: """simple docstring""" a = kwargs.pop('''config''' , __UpperCAmelCase ) a = kwargs.pop('''trust_remote_code''' , __UpperCAmelCase ) a = True a , a = FeatureExtractionMixin.get_feature_extractor_dict(__UpperCAmelCase , **__UpperCAmelCase ) a = config_dict.get('''feature_extractor_type''' , __UpperCAmelCase ) a = None if "AutoFeatureExtractor" in config_dict.get('''auto_map''' , {} ): a = config_dict['''auto_map''']['''AutoFeatureExtractor'''] # If we don't find the feature extractor class in the feature extractor config, let's try the model config. if feature_extractor_class is None and feature_extractor_auto_map is None: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = AutoConfig.from_pretrained(__UpperCAmelCase , **__UpperCAmelCase ) # It could be in `config.feature_extractor_type`` a = getattr(__UpperCAmelCase , '''feature_extractor_type''' , __UpperCAmelCase ) if hasattr(__UpperCAmelCase , '''auto_map''' ) and "AutoFeatureExtractor" in config.auto_map: a = config.auto_map['''AutoFeatureExtractor'''] if feature_extractor_class is not None: a = feature_extractor_class_from_name(__UpperCAmelCase ) a = feature_extractor_auto_map is not None a = feature_extractor_class is not None or type(__UpperCAmelCase ) in FEATURE_EXTRACTOR_MAPPING a = resolve_trust_remote_code( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) if has_remote_code and trust_remote_code: a = get_class_from_dynamic_module( __UpperCAmelCase , __UpperCAmelCase , **__UpperCAmelCase ) a = kwargs.pop('''code_revision''' , __UpperCAmelCase ) if os.path.isdir(__UpperCAmelCase ): feature_extractor_class.register_for_auto_class() return feature_extractor_class.from_dict(__UpperCAmelCase , **__UpperCAmelCase ) elif feature_extractor_class is not None: return feature_extractor_class.from_dict(__UpperCAmelCase , **__UpperCAmelCase ) # Last try: we use the FEATURE_EXTRACTOR_MAPPING. elif type(__UpperCAmelCase ) in FEATURE_EXTRACTOR_MAPPING: a = FEATURE_EXTRACTOR_MAPPING[type(__UpperCAmelCase )] return feature_extractor_class.from_dict(__UpperCAmelCase , **__UpperCAmelCase ) raise ValueError( F"""Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a """ F"""`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following """ F"""`model_type` keys in its {CONFIG_NAME}: {', '.join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys() )}""" ) @staticmethod def __lowerCAmelCase ( __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Tuple ) ->Optional[int]: """simple docstring""" FEATURE_EXTRACTOR_MAPPING.register(__UpperCAmelCase , __UpperCAmelCase )
26
1
from __future__ import annotations import math def _a ( a :int ) -> list[int]: if num <= 0: a = F"""{num}: Invalid input, please enter a positive integer.""" raise ValueError(a ) a = [True] * (num + 1) a = [] a = 2 a = int(math.sqrt(a ) ) while start <= end: # If start is a prime if sieve[start] is True: prime.append(a ) # Set multiples of start be False for i in range(start * start , num + 1 , a ): if sieve[i] is True: a = False start += 1 for j in range(end + 1 , num + 1 ): if sieve[j] is True: prime.append(a ) return prime if __name__ == "__main__": print(prime_sieve(int(input("Enter a positive integer: ").strip())))
26
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import ( AutoProcessor, BertTokenizerFast, BlipImageProcessor, GPTaTokenizer, InstructBlipProcessor, PreTrainedTokenizerFast, ) @require_vision class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self : Optional[int] ) ->Tuple: """simple docstring""" a = tempfile.mkdtemp() a = BlipImageProcessor() a = GPTaTokenizer.from_pretrained('''hf-internal-testing/tiny-random-GPT2Model''' ) a = BertTokenizerFast.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) a = InstructBlipProcessor(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) processor.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self : Optional[Any] , **__UpperCAmelCase : Tuple ) ->List[str]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).tokenizer def __lowerCAmelCase ( self : int , **__UpperCAmelCase : str ) ->List[str]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).image_processor def __lowerCAmelCase ( self : Optional[Any] , **__UpperCAmelCase : Any ) ->Optional[Any]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).qformer_tokenizer def __lowerCAmelCase ( self : str ) ->Tuple: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __lowerCAmelCase ( self : Optional[int] ) ->str: """simple docstring""" a = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] a = [Image.fromarray(np.moveaxis(__UpperCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def __lowerCAmelCase ( self : Optional[Any] ) ->List[str]: """simple docstring""" a = InstructBlipProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() , qformer_tokenizer=self.get_qformer_tokenizer() , ) processor.save_pretrained(self.tmpdirname ) a = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) a = self.get_image_processor(do_normalize=__UpperCAmelCase , padding_value=1.0 ) a = InstructBlipProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=__UpperCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __UpperCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __UpperCAmelCase ) self.assertIsInstance(processor.qformer_tokenizer , __UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[Any] ) ->Any: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = self.prepare_image_inputs() a = image_processor(__UpperCAmelCase , return_tensors='''np''' ) a = processor(images=__UpperCAmelCase , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowerCAmelCase ( self : List[str] ) ->List[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = '''lower newer''' a = processor(text=__UpperCAmelCase ) a = tokenizer(__UpperCAmelCase , return_token_type_ids=__UpperCAmelCase ) a = qformer_tokenizer(__UpperCAmelCase , return_token_type_ids=__UpperCAmelCase ) for key in encoded_tokens.keys(): self.assertListEqual(encoded_tokens[key] , encoded_processor[key] ) for key in encoded_tokens_qformer.keys(): self.assertListEqual(encoded_tokens_qformer[key] , encoded_processor['''qformer_''' + key] ) def __lowerCAmelCase ( self : Dict ) ->Optional[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = '''lower newer''' a = self.prepare_image_inputs() a = processor(text=__UpperCAmelCase , images=__UpperCAmelCase ) self.assertListEqual( list(inputs.keys() ) , ['''input_ids''', '''attention_mask''', '''qformer_input_ids''', '''qformer_attention_mask''', '''pixel_values'''] , ) # test if it raises when no input is passed with pytest.raises(__UpperCAmelCase ): processor() def __lowerCAmelCase ( self : Dict ) ->List[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] a = processor.batch_decode(__UpperCAmelCase ) a = tokenizer.batch_decode(__UpperCAmelCase ) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] ) ->str: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = '''lower newer''' a = self.prepare_image_inputs() a = processor(text=__UpperCAmelCase , images=__UpperCAmelCase ) self.assertListEqual( list(inputs.keys() ) , ['''input_ids''', '''attention_mask''', '''qformer_input_ids''', '''qformer_attention_mask''', '''pixel_values'''] , )
26
1
from math import asin, atan, cos, radians, sin, sqrt, tan UpperCAmelCase__ = 637_8137.0 UpperCAmelCase__ = 635_6752.31_4245 UpperCAmelCase__ = 6378137 def _a ( a :float , a :float , a :float , a :float ) -> float: a = (AXIS_A - AXIS_B) / AXIS_A a = atan((1 - flattening) * tan(radians(a ) ) ) a = atan((1 - flattening) * tan(radians(a ) ) ) a = radians(a ) a = radians(a ) # Equation a = sin((phi_a - phi_a) / 2 ) a = sin((lambda_a - lambda_a) / 2 ) # Square both values sin_sq_phi *= sin_sq_phi sin_sq_lambda *= sin_sq_lambda a = sqrt(sin_sq_phi + (cos(a ) * cos(a ) * sin_sq_lambda) ) return 2 * RADIUS * asin(a ) if __name__ == "__main__": import doctest doctest.testmod()
26
import math def _a ( a :int = 100 ) -> int: a = sum(i * i for i in range(1 , n + 1 ) ) a = int(math.pow(sum(range(1 , n + 1 ) ) , 2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(f"""{solution() = }""")
26
1
def _a ( a :list ) -> list: if any(not isinstance(a , a ) or x < 0 for x in sequence ): raise TypeError('''Sequence must be list of non-negative integers''' ) for _ in range(len(a ) ): for i, (rod_upper, rod_lower) in enumerate(zip(a , sequence[1:] ) ): if rod_upper > rod_lower: sequence[i] -= rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower return sequence if __name__ == "__main__": assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9]
26
def _a ( a :int = 600_851_475_143 ) -> int: try: a = int(a ) 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.''' ) a = 2 a = 0 if n == 2: return 2 while n > 2: while n % i != 0: i += 1 a = i while n % i == 0: a = n // i i += 1 return int(a ) if __name__ == "__main__": print(f"""{solution() = }""")
26
1
from typing import Callable, Dict, Optional, Tuple import torch from torch import nn from torch.distributions import ( AffineTransform, Distribution, Independent, NegativeBinomial, Normal, StudentT, TransformedDistribution, ) class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Dict , __UpperCAmelCase : Distribution , __UpperCAmelCase : Union[str, Any]=None , __UpperCAmelCase : int=None , __UpperCAmelCase : Tuple=0 ) ->List[Any]: """simple docstring""" a = 1.0 if scale is None else scale a = 0.0 if loc is None else loc super().__init__(__UpperCAmelCase , [AffineTransform(loc=self.loc , scale=self.scale , event_dim=__UpperCAmelCase )] ) @property def __lowerCAmelCase ( self : Any ) ->Union[str, Any]: """simple docstring""" return self.base_dist.mean * self.scale + self.loc @property def __lowerCAmelCase ( self : Optional[Any] ) ->List[Any]: """simple docstring""" return self.base_dist.variance * self.scale**2 @property def __lowerCAmelCase ( self : Dict ) ->List[str]: """simple docstring""" return self.variance.sqrt() class lowercase_ ( nn.Module ): '''simple docstring''' def __init__( self : List[Any] , __UpperCAmelCase : int , __UpperCAmelCase : Dict[str, int] , __UpperCAmelCase : Callable[..., Tuple[torch.Tensor]] , **__UpperCAmelCase : Union[str, Any] ) ->None: """simple docstring""" super().__init__(**__UpperCAmelCase ) a = args_dim a = nn.ModuleList([nn.Linear(__UpperCAmelCase , __UpperCAmelCase ) for dim in args_dim.values()] ) a = domain_map def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : torch.Tensor ) ->Tuple[torch.Tensor]: """simple docstring""" a = [proj(__UpperCAmelCase ) for proj in self.proj] return self.domain_map(*__UpperCAmelCase ) class lowercase_ ( nn.Module ): '''simple docstring''' def __init__( self : Union[str, Any] , __UpperCAmelCase : Tuple ) ->Optional[int]: """simple docstring""" super().__init__() a = function def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : int , *__UpperCAmelCase : List[str] ) ->Optional[int]: """simple docstring""" return self.function(__UpperCAmelCase , *__UpperCAmelCase ) class lowercase_ : '''simple docstring''' __snake_case = 42 __snake_case = 42 __snake_case = 42 def __init__( self : Optional[Any] , __UpperCAmelCase : int = 1 ) ->None: """simple docstring""" a = dim a = {k: dim * self.args_dim[k] for k in self.args_dim} def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[Any] ) ->List[Any]: """simple docstring""" if self.dim == 1: return self.distribution_class(*__UpperCAmelCase ) else: return Independent(self.distribution_class(*__UpperCAmelCase ) , 1 ) def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Optional[torch.Tensor] = None , __UpperCAmelCase : Optional[torch.Tensor] = None , ) ->Distribution: """simple docstring""" a = self._base_distribution(__UpperCAmelCase ) if loc is None and scale is None: return distr else: return AffineTransformed(__UpperCAmelCase , loc=__UpperCAmelCase , scale=__UpperCAmelCase , event_dim=self.event_dim ) @property def __lowerCAmelCase ( self : List[Any] ) ->Tuple: """simple docstring""" return () if self.dim == 1 else (self.dim,) @property def __lowerCAmelCase ( self : str ) ->int: """simple docstring""" return len(self.event_shape ) @property def __lowerCAmelCase ( self : Optional[int] ) ->float: """simple docstring""" return 0.0 def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : int ) ->nn.Module: """simple docstring""" return ParameterProjection( in_features=__UpperCAmelCase , args_dim=self.args_dim , domain_map=LambdaLayer(self.domain_map ) , ) def __lowerCAmelCase ( self : List[str] , *__UpperCAmelCase : torch.Tensor ) ->int: """simple docstring""" raise NotImplementedError() @staticmethod def __lowerCAmelCase ( __UpperCAmelCase : torch.Tensor ) ->torch.Tensor: """simple docstring""" return (x + torch.sqrt(torch.square(__UpperCAmelCase ) + 4.0 )) / 2.0 class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = {"df": 1, "loc": 1, "scale": 1} __snake_case = StudentT @classmethod def __lowerCAmelCase ( cls : Union[str, Any] , __UpperCAmelCase : torch.Tensor , __UpperCAmelCase : torch.Tensor , __UpperCAmelCase : torch.Tensor ) ->int: """simple docstring""" a = cls.squareplus(__UpperCAmelCase ).clamp_min(torch.finfo(scale.dtype ).eps ) a = 2.0 + cls.squareplus(__UpperCAmelCase ) return df.squeeze(-1 ), loc.squeeze(-1 ), scale.squeeze(-1 ) class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = {"loc": 1, "scale": 1} __snake_case = Normal @classmethod def __lowerCAmelCase ( cls : Dict , __UpperCAmelCase : torch.Tensor , __UpperCAmelCase : torch.Tensor ) ->Union[str, Any]: """simple docstring""" a = cls.squareplus(__UpperCAmelCase ).clamp_min(torch.finfo(scale.dtype ).eps ) return loc.squeeze(-1 ), scale.squeeze(-1 ) class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = {"total_count": 1, "logits": 1} __snake_case = NegativeBinomial @classmethod def __lowerCAmelCase ( cls : Optional[int] , __UpperCAmelCase : torch.Tensor , __UpperCAmelCase : torch.Tensor ) ->int: """simple docstring""" a = cls.squareplus(__UpperCAmelCase ) return total_count.squeeze(-1 ), logits.squeeze(-1 ) def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : Union[str, Any] ) ->Distribution: """simple docstring""" a , a = distr_args if self.dim == 1: return self.distribution_class(total_count=__UpperCAmelCase , logits=__UpperCAmelCase ) else: return Independent(self.distribution_class(total_count=__UpperCAmelCase , logits=__UpperCAmelCase ) , 1 ) def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Optional[torch.Tensor] = None , __UpperCAmelCase : Optional[torch.Tensor] = None ) ->Distribution: """simple docstring""" a , a = distr_args if scale is not None: # See scaling property of Gamma. logits += scale.log() return self._base_distribution((total_count, logits) )
26
import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer UpperCAmelCase__ = "bart" UpperCAmelCase__ = True @st.cache(allow_output_mutation=a ) def _a ( ) -> Tuple: if LOAD_DENSE_INDEX: a = AutoTokenizer.from_pretrained('''yjernite/retribert-base-uncased''' ) a = AutoModel.from_pretrained('''yjernite/retribert-base-uncased''' ).to('''cuda:0''' ) a = qar_model.eval() else: a , a = (None, None) if MODEL_TYPE == "bart": a = AutoTokenizer.from_pretrained('''yjernite/bart_eli5''' ) a = AutoModelForSeqaSeqLM.from_pretrained('''yjernite/bart_eli5''' ).to('''cuda:0''' ) a = torch.load('''seq2seq_models/eli5_bart_model_blm_2.pth''' ) sas_model.load_state_dict(save_dict['''model'''] ) a = sas_model.eval() else: a , a = make_qa_sas_model( model_name='''t5-small''' , from_file='''seq2seq_models/eli5_t5_model_1024_4.pth''' , device='''cuda:0''' ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=a ) def _a ( ) -> Dict: if LOAD_DENSE_INDEX: a = faiss.StandardGpuResources() a = datasets.load_dataset(path='''wiki_snippets''' , name='''wiki40b_en_100_0''' )['''train'''] a = np.memmap( '''wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat''' , dtype='''float32''' , mode='''r''' , shape=(wikiaab_passages.num_rows, 128) , ) a = faiss.IndexFlatIP(128 ) a = faiss.index_cpu_to_gpu(a , 1 , a ) wikiaab_gpu_index_flat.add(a ) # TODO fix for larger GPU else: a , a = (None, None) a = Elasticsearch([{'''host''': '''localhost''', '''port''': '''9200'''}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=a ) def _a ( ) -> Optional[int]: a = datasets.load_dataset('''eli5''' , name='''LFQA_reddit''' ) a = elia['''train_eli5'''] a = np.memmap( '''eli5_questions_reps.dat''' , dtype='''float32''' , mode='''r''' , shape=(elia_train.num_rows, 128) ) a = faiss.IndexFlatIP(128 ) eli5_train_q_index.add(a ) return (elia_train, eli5_train_q_index) UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = load_indexes() UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = load_models() UpperCAmelCase__ , UpperCAmelCase__ = load_train_data() def _a ( a :str , a :Tuple=10 ) -> List[str]: a = embed_questions_for_retrieval([question] , a , a ) a , a = eli5_train_q_index.search(a , a ) a = [elia_train[int(a )] for i in I[0]] return nn_examples def _a ( a :str , a :Any="wiki40b" , a :int="dense" , a :Union[str, Any]=10 ) -> List[str]: if source == "none": a , a = (''' <P> '''.join(['''''' for _ in range(11 )] ).strip(), []) else: if method == "dense": a , a = query_qa_dense_index( a , a , a , a , a , a ) else: a , a = query_es_index( a , a , index_name='''english_wiki40b_snippets_100w''' , n_results=a , ) a = [ (res['''article_title'''], res['''section_title'''].strip(), res['''score'''], res['''passage_text''']) for res in hit_lst ] a = '''question: {} context: {}'''.format(a , a ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda a : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda a : None), } ) def _a ( a :Tuple , a :int , a :int , a :Dict=64 , a :List[Any]=256 , a :List[Any]=False , a :List[Any]=2 , a :Tuple=0.95 , a :Optional[Any]=0.8 ) -> int: with torch.no_grad(): a = qa_sas_generate( a , a , a , num_answers=1 , num_beams=a , min_len=a , max_len=a , do_sample=a , temp=a , top_p=a , top_k=a , max_input_length=1_024 , device='''cuda:0''' , )[0] return (answer, support_list) st.title("Long Form Question Answering with ELI5") # Start sidebar UpperCAmelCase__ = "<img src='https://huggingface.co/front/assets/huggingface_logo.svg'>" UpperCAmelCase__ = "\n<html>\n <head>\n <style>\n .img-container {\n padding-left: 90px;\n padding-right: 90px;\n padding-top: 50px;\n padding-bottom: 50px;\n background-color: #f0f3f9;\n }\n </style>\n </head>\n <body>\n <span class=\"img-container\"> <!-- Inline parent element -->\n %s\n </span>\n </body>\n</html>\n" % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia UpperCAmelCase__ = "\nThis demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html).\nFirst, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset,\na pre-processed fixed snapshot of Wikipedia.\n" st.sidebar.markdown(description, unsafe_allow_html=True) UpperCAmelCase__ = [ "Answer the question", "View the retrieved document only", "View the most similar ELI5 question and answer", "Show me everything, please!", ] UpperCAmelCase__ = st.sidebar.checkbox("Demo options") if demo_options: UpperCAmelCase__ = st.sidebar.selectbox( "", action_list, index=3, ) UpperCAmelCase__ = action_list.index(action_st) UpperCAmelCase__ = st.sidebar.selectbox( "", ["Show full text of passages", "Show passage section titles"], index=0, ) UpperCAmelCase__ = show_type == "Show full text of passages" else: UpperCAmelCase__ = 3 UpperCAmelCase__ = True UpperCAmelCase__ = st.sidebar.checkbox("Retrieval options") if retrieval_options: UpperCAmelCase__ = "\n ### Information retriever options\n\n The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding\n trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs.\n The answer is then generated by sequence to sequence model which takes the question and retrieved document as input.\n " st.sidebar.markdown(retriever_info) UpperCAmelCase__ = st.sidebar.selectbox("Which Wikipedia format should the model use?", ["wiki40b", "none"]) UpperCAmelCase__ = st.sidebar.selectbox("Which Wikipedia indexer should the model use?", ["dense", "sparse", "mixed"]) else: UpperCAmelCase__ = "wiki40b" UpperCAmelCase__ = "dense" UpperCAmelCase__ = "beam" UpperCAmelCase__ = 2 UpperCAmelCase__ = 64 UpperCAmelCase__ = 256 UpperCAmelCase__ = None UpperCAmelCase__ = None UpperCAmelCase__ = st.sidebar.checkbox("Generation options") if generate_options: UpperCAmelCase__ = "\n ### Answer generation options\n\n The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large)\n weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with\n **beam** search, or **sample** from the decoder's output probabilities.\n " st.sidebar.markdown(generate_info) UpperCAmelCase__ = st.sidebar.selectbox("Would you like to use beam search or sample an answer?", ["beam", "sampled"]) UpperCAmelCase__ = st.sidebar.slider( "Minimum generation length", min_value=8, max_value=256, value=64, step=8, format=None, key=None ) UpperCAmelCase__ = st.sidebar.slider( "Maximum generation length", min_value=64, max_value=512, value=256, step=16, format=None, key=None ) if sampled == "beam": UpperCAmelCase__ = st.sidebar.slider("Beam size", min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: UpperCAmelCase__ = st.sidebar.slider( "Nucleus sampling p", min_value=0.1, max_value=1.0, value=0.95, step=0.01, format=None, key=None ) UpperCAmelCase__ = st.sidebar.slider( "Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.01, format=None, key=None ) UpperCAmelCase__ = None # start main text UpperCAmelCase__ = [ "<MY QUESTION>", "How do people make chocolate?", "Why do we get a fever when we are sick?", "How can different animals perceive different colors?", "What is natural language processing?", "What's the best way to treat a sunburn?", "What exactly are vitamins ?", "How does nuclear energy provide electricity?", "What's the difference between viruses and bacteria?", "Why are flutes classified as woodwinds when most of them are made out of metal ?", "Why do people like drinking coffee even though it tastes so bad?", "What happens when wine ages? How does it make the wine taste better?", "If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?", "How can we set a date to the beginning or end of an artistic period? Doesn't the change happen gradually?", "How does New Zealand have so many large bird predators?", ] UpperCAmelCase__ = st.selectbox( "What would you like to ask? ---- select <MY QUESTION> to enter a new query", questions_list, index=1, ) if question_s == "<MY QUESTION>": UpperCAmelCase__ = st.text_input("Enter your question here:", "") else: UpperCAmelCase__ = question_s if st.button("Show me!"): if action in [0, 1, 3]: if index_type == "mixed": UpperCAmelCase__ , UpperCAmelCase__ = make_support(question, source=wiki_source, method="dense", n_results=10) UpperCAmelCase__ , UpperCAmelCase__ = make_support(question, source=wiki_source, method="sparse", n_results=10) UpperCAmelCase__ = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] UpperCAmelCase__ = support_list[:10] UpperCAmelCase__ = "<P> " + " <P> ".join([res[-1] for res in support_list]) else: UpperCAmelCase__ , UpperCAmelCase__ = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: UpperCAmelCase__ , UpperCAmelCase__ = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == "sampled"), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown("### The model generated answer is:") st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown("--- \n ### The model is drawing information from the following Wikipedia passages:") for i, res in enumerate(support_list): UpperCAmelCase__ = "https://en.wikipedia.org/wiki/{}".format(res[0].replace(" ", "_")) UpperCAmelCase__ = res[1].strip() if sec_titles == "": UpperCAmelCase__ = "[{}]({})".format(res[0], wiki_url) else: UpperCAmelCase__ = sec_titles.split(" & ") UpperCAmelCase__ = " & ".join( ["[{}]({}#{})".format(sec.strip(), wiki_url, sec.strip().replace(" ", "_")) for sec in sec_list] ) st.markdown( "{0:02d} - **Article**: {1:<18} <br> _Section_: {2}".format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( "> <span style=\"font-family:arial; font-size:10pt;\">" + res[-1] + "</span>", unsafe_allow_html=True ) if action in [2, 3]: UpperCAmelCase__ = find_nearest_training(question) UpperCAmelCase__ = nn_train_list[0] st.markdown( "--- \n ### The most similar question in the ELI5 training set was: \n\n {}".format(train_exple["title"]) ) UpperCAmelCase__ = [ "{}. {}".format(i + 1, " \n".join([line.strip() for line in ans.split("\n") if line.strip() != ""])) for i, (ans, sc) in enumerate(zip(train_exple["answers"]["text"], train_exple["answers"]["score"])) if i == 0 or sc > 2 ] st.markdown("##### Its answers were: \n\n {}".format("\n".join(answers_st))) UpperCAmelCase__ = "\n---\n\n**Disclaimer**\n\n*The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system.\nEvaluating biases of such a model and ensuring factual generations are still very much open research problems.\nTherefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.*\n" st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
26
1
import argparse import io import requests import torch from omegaconf import OmegaConf from diffusers import AutoencoderKL from diffusers.pipelines.stable_diffusion.convert_from_ckpt import ( assign_to_checkpoint, conv_attn_to_linear, create_vae_diffusers_config, renew_vae_attention_paths, renew_vae_resnet_paths, ) def _a ( a :Union[str, Any] , a :List[Any] ) -> List[Any]: a = checkpoint a = {} a = vae_state_dict['''encoder.conv_in.weight'''] a = vae_state_dict['''encoder.conv_in.bias'''] a = vae_state_dict['''encoder.conv_out.weight'''] a = vae_state_dict['''encoder.conv_out.bias'''] a = vae_state_dict['''encoder.norm_out.weight'''] a = vae_state_dict['''encoder.norm_out.bias'''] a = vae_state_dict['''decoder.conv_in.weight'''] a = vae_state_dict['''decoder.conv_in.bias'''] a = vae_state_dict['''decoder.conv_out.weight'''] a = vae_state_dict['''decoder.conv_out.bias'''] a = vae_state_dict['''decoder.norm_out.weight'''] a = vae_state_dict['''decoder.norm_out.bias'''] a = vae_state_dict['''quant_conv.weight'''] a = vae_state_dict['''quant_conv.bias'''] a = vae_state_dict['''post_quant_conv.weight'''] a = vae_state_dict['''post_quant_conv.bias'''] # Retrieves the keys for the encoder down blocks only a = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''encoder.down''' in layer} ) a = { layer_id: [key for key in vae_state_dict if F"""down.{layer_id}""" in key] for layer_id in range(a ) } # Retrieves the keys for the decoder up blocks only a = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''decoder.up''' in layer} ) a = { layer_id: [key for key in vae_state_dict if F"""up.{layer_id}""" in key] for layer_id in range(a ) } for i in range(a ): a = [key for key in down_blocks[i] if F"""down.{i}""" in key and F"""down.{i}.downsample""" not in key] if F"""encoder.down.{i}.downsample.conv.weight""" in vae_state_dict: a = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.weight""" ) a = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.bias""" ) a = renew_vae_resnet_paths(a ) a = {'''old''': F"""down.{i}.block""", '''new''': F"""down_blocks.{i}.resnets"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''encoder.mid.block''' in key] a = 2 for i in range(1 , num_mid_res_blocks + 1 ): a = [key for key in mid_resnets if F"""encoder.mid.block_{i}""" in key] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""mid.block_{i}""", '''new''': F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''encoder.mid.attn''' in key] a = renew_vae_attention_paths(a ) a = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) conv_attn_to_linear(a ) for i in range(a ): a = num_up_blocks - 1 - i a = [ key for key in up_blocks[block_id] if F"""up.{block_id}""" in key and F"""up.{block_id}.upsample""" not in key ] if F"""decoder.up.{block_id}.upsample.conv.weight""" in vae_state_dict: a = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.weight""" ] a = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.bias""" ] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""up.{block_id}.block""", '''new''': F"""up_blocks.{i}.resnets"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''decoder.mid.block''' in key] a = 2 for i in range(1 , num_mid_res_blocks + 1 ): a = [key for key in mid_resnets if F"""decoder.mid.block_{i}""" in key] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""mid.block_{i}""", '''new''': F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''decoder.mid.attn''' in key] a = renew_vae_attention_paths(a ) a = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) conv_attn_to_linear(a ) return new_checkpoint def _a ( a :str , a :str , ) -> List[str]: # Only support V1 a = requests.get( ''' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml''' ) a = io.BytesIO(r.content ) a = OmegaConf.load(a ) a = 512 a = '''cuda''' if torch.cuda.is_available() else '''cpu''' if checkpoint_path.endswith('''safetensors''' ): from safetensors import safe_open a = {} with safe_open(a , framework='''pt''' , device='''cpu''' ) as f: for key in f.keys(): a = f.get_tensor(a ) else: a = torch.load(a , map_location=a )['''state_dict'''] # Convert the VAE model. a = create_vae_diffusers_config(a , image_size=a ) a = custom_convert_ldm_vae_checkpoint(a , a ) a = AutoencoderKL(**a ) vae.load_state_dict(a ) vae.save_pretrained(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--vae_pt_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") UpperCAmelCase__ = parser.parse_args() vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)
26
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin UpperCAmelCase__ = "▁" UpperCAmelCase__ = get_tests_dir("fixtures/test_sentencepiece.model") @require_sentencepiece class lowercase_ ( lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = BertGenerationTokenizer __snake_case = False __snake_case = True def __lowerCAmelCase ( self : str ) ->str: """simple docstring""" super().setUp() a = BertGenerationTokenizer(__UpperCAmelCase , keep_accents=__UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self : int ) ->Dict: """simple docstring""" a = '''<s>''' a = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCAmelCase ) , __UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCAmelCase ) , __UpperCAmelCase ) def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''<pad>''' ) self.assertEqual(len(__UpperCAmelCase ) , 1_002 ) def __lowerCAmelCase ( self : List[str] ) ->List[Any]: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1_000 ) def __lowerCAmelCase ( self : Tuple ) ->Optional[int]: """simple docstring""" a = BertGenerationTokenizer(__UpperCAmelCase , keep_accents=__UpperCAmelCase ) a = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(__UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) , [285, 46, 10, 170, 382] , ) a = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( __UpperCAmelCase , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) a = tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) self.assertListEqual( __UpperCAmelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) a = tokenizer.convert_ids_to_tokens(__UpperCAmelCase ) self.assertListEqual( __UpperCAmelCase , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) @cached_property def __lowerCAmelCase ( self : List[Any] ) ->List[str]: """simple docstring""" return BertGenerationTokenizer.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder''' ) @slow def __lowerCAmelCase ( self : Any ) ->str: """simple docstring""" a = '''Hello World!''' a = [18_536, 2_260, 101] self.assertListEqual(__UpperCAmelCase , self.big_tokenizer.encode(__UpperCAmelCase ) ) @slow def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = ( '''This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will''' ''' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth''' ) a = [ 871, 419, 358, 946, 991, 2_521, 452, 358, 1_357, 387, 7_751, 3_536, 112, 985, 456, 126, 865, 938, 5_400, 5_734, 458, 1_368, 467, 786, 2_462, 5_246, 1_159, 633, 865, 4_519, 457, 582, 852, 2_557, 427, 916, 508, 405, 34_324, 497, 391, 408, 11_342, 1_244, 385, 100, 938, 985, 456, 574, 362, 12_597, 3_200, 3_129, 1_172, ] self.assertListEqual(__UpperCAmelCase , self.big_tokenizer.encode(__UpperCAmelCase ) ) @require_torch @slow def __lowerCAmelCase ( self : Any ) ->Dict: """simple docstring""" import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence a = list(self.big_tokenizer.get_vocab().keys() )[:10] a = ''' '''.join(__UpperCAmelCase ) a = self.big_tokenizer.encode_plus(__UpperCAmelCase , return_tensors='''pt''' , return_token_type_ids=__UpperCAmelCase ) a = self.big_tokenizer.batch_encode_plus( [sequence + ''' ''' + sequence] , return_tensors='''pt''' , return_token_type_ids=__UpperCAmelCase ) a = BertGenerationConfig() a = BertGenerationEncoder(__UpperCAmelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**__UpperCAmelCase ) model(**__UpperCAmelCase ) @slow def __lowerCAmelCase ( self : str ) ->Optional[Any]: """simple docstring""" a = {'''input_ids''': [[39_286, 458, 36_335, 2_001, 456, 13_073, 13_266, 455, 113, 7_746, 1_741, 11_157, 391, 13_073, 13_266, 455, 113, 3_967, 35_412, 113, 4_936, 109, 3_870, 2_377, 113, 30_084, 45_720, 458, 134, 17_496, 112, 503, 11_672, 113, 118, 112, 5_665, 13_347, 38_687, 112, 1_496, 31_389, 112, 3_268, 47_264, 134, 962, 112, 16_377, 8_035, 23_130, 430, 12_169, 15_518, 28_592, 458, 146, 41_697, 109, 391, 12_169, 15_518, 16_689, 458, 146, 41_358, 109, 452, 726, 4_034, 111, 763, 35_412, 5_082, 388, 1_903, 111, 9_051, 391, 2_870, 48_918, 1_900, 1_123, 550, 998, 112, 9_586, 15_985, 455, 391, 410, 22_955, 37_636, 114], [448, 17_496, 419, 3_663, 385, 763, 113, 27_533, 2_870, 3_283, 13_043, 1_639, 24_713, 523, 656, 24_013, 18_550, 2_521, 517, 27_014, 21_244, 420, 1_212, 1_465, 391, 927, 4_833, 388, 578, 11_786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2_169, 7_687, 21_932, 18_146, 726, 363, 17_032, 3_391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__UpperCAmelCase , model_name='''google/bert_for_seq_generation_L-24_bbc_encoder''' , revision='''c817d1fd1be2ffa69431227a1fe320544943d4db''' , )
26
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) UpperCAmelCase__ = { "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: UpperCAmelCase__ = ["BlipImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "BlipModel", "BlipPreTrainedModel", "BlipForConditionalGeneration", "BlipForQuestionAnswering", "BlipVisionModel", "BlipTextModel", "BlipForImageTextRetrieval", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "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 UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
import argparse import numpy as np import torch from transformers import SpeechTaHifiGan, SpeechTaHifiGanConfig, logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger("transformers.models.speecht5") def _a ( a :Optional[Any] , a :Tuple , a :Dict ) -> List[str]: hf_model.apply_weight_norm() a = checkpoint['''input_conv.weight_g'''] a = checkpoint['''input_conv.weight_v'''] a = checkpoint['''input_conv.bias'''] for i in range(len(config.upsample_rates ) ): a = checkpoint[F"""upsamples.{i}.1.weight_g"""] a = checkpoint[F"""upsamples.{i}.1.weight_v"""] a = checkpoint[F"""upsamples.{i}.1.bias"""] for i in range(len(config.upsample_rates ) * len(config.resblock_kernel_sizes ) ): for j in range(len(config.resblock_dilation_sizes ) ): a = checkpoint[F"""blocks.{i}.convs1.{j}.1.weight_g"""] a = checkpoint[F"""blocks.{i}.convs1.{j}.1.weight_v"""] a = checkpoint[F"""blocks.{i}.convs1.{j}.1.bias"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.weight_g"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.weight_v"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.bias"""] a = checkpoint['''output_conv.1.weight_g'''] a = checkpoint['''output_conv.1.weight_v'''] a = checkpoint['''output_conv.1.bias'''] hf_model.remove_weight_norm() @torch.no_grad() def _a ( a :List[str] , a :Union[str, Any] , a :Dict , a :Dict=None , a :List[Any]=None , ) -> int: if config_path is not None: a = SpeechTaHifiGanConfig.from_pretrained(a ) else: a = SpeechTaHifiGanConfig() a = SpeechTaHifiGan(a ) a = torch.load(a ) load_weights(orig_checkpoint['''model''']['''generator'''] , a , a ) a = np.load(a ) a = stats[0].reshape(-1 ) a = stats[1].reshape(-1 ) a = torch.from_numpy(a ).float() a = torch.from_numpy(a ).float() model.save_pretrained(a ) if repo_id: print('''Pushing to the hub...''' ) model.push_to_hub(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--checkpoint_path", required=True, default=None, type=str, help="Path to original checkpoint") parser.add_argument("--stats_path", required=True, default=None, type=str, help="Path to stats.npy file") 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." ) UpperCAmelCase__ = parser.parse_args() convert_hifigan_checkpoint( args.checkpoint_path, args.stats_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
26
1
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, BlipaProcessor, BlipImageProcessor, GPTaTokenizer, PreTrainedTokenizerFast @require_vision class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self : str ) ->Tuple: """simple docstring""" a = tempfile.mkdtemp() a = BlipImageProcessor() a = GPTaTokenizer.from_pretrained('''hf-internal-testing/tiny-random-GPT2Model''' ) a = BlipaProcessor(__UpperCAmelCase , __UpperCAmelCase ) processor.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self : List[str] , **__UpperCAmelCase : List[str] ) ->List[Any]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).tokenizer def __lowerCAmelCase ( self : Dict , **__UpperCAmelCase : Optional[int] ) ->Union[str, Any]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).image_processor def __lowerCAmelCase ( self : List[str] ) ->str: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __lowerCAmelCase ( self : str ) ->str: """simple docstring""" a = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] a = [Image.fromarray(np.moveaxis(__UpperCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def __lowerCAmelCase ( self : Union[str, Any] ) ->Any: """simple docstring""" a = BlipaProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) a = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) a = self.get_image_processor(do_normalize=__UpperCAmelCase , padding_value=1.0 ) a = BlipaProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=__UpperCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __UpperCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __UpperCAmelCase ) def __lowerCAmelCase ( self : Dict ) ->List[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = BlipaProcessor(tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase ) a = self.prepare_image_inputs() a = image_processor(__UpperCAmelCase , return_tensors='''np''' ) a = processor(images=__UpperCAmelCase , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowerCAmelCase ( self : Any ) ->Any: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = BlipaProcessor(tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase ) a = '''lower newer''' a = processor(text=__UpperCAmelCase ) a = tokenizer(__UpperCAmelCase , return_token_type_ids=__UpperCAmelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __lowerCAmelCase ( self : List[Any] ) ->int: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = BlipaProcessor(tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase ) a = '''lower newer''' a = self.prepare_image_inputs() a = processor(text=__UpperCAmelCase , images=__UpperCAmelCase ) self.assertListEqual(list(inputs.keys() ) , ['''pixel_values''', '''input_ids''', '''attention_mask'''] ) # test if it raises when no input is passed with pytest.raises(__UpperCAmelCase ): processor() def __lowerCAmelCase ( self : Any ) ->List[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = BlipaProcessor(tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase ) a = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] a = processor.batch_decode(__UpperCAmelCase ) a = tokenizer.batch_decode(__UpperCAmelCase ) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : str ) ->Optional[int]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = BlipaProcessor(tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase ) a = '''lower newer''' a = self.prepare_image_inputs() a = processor(text=__UpperCAmelCase , images=__UpperCAmelCase ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) , ['''pixel_values''', '''input_ids''', '''attention_mask'''] )
26
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) UpperCAmelCase__ = { "configuration_gpt_bigcode": ["GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP", "GPTBigCodeConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST", "GPTBigCodeForSequenceClassification", "GPTBigCodeForTokenClassification", "GPTBigCodeForCausalLM", "GPTBigCodeModel", "GPTBigCodePreTrainedModel", ] if TYPE_CHECKING: from .configuration_gpt_bigcode import GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTBigCodeConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_bigcode import ( GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTBigCodeForCausalLM, GPTBigCodeForSequenceClassification, GPTBigCodeForTokenClassification, GPTBigCodeModel, GPTBigCodePreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
1
from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( '''The RoBERTa Model transformer with early exiting (DeeRoBERTa). ''' , lowercase , ) class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = RobertaConfig __snake_case = '''roberta''' def __init__( self : List[Any] , __UpperCAmelCase : Any ) ->Union[str, Any]: """simple docstring""" super().__init__(__UpperCAmelCase ) a = RobertaEmbeddings(__UpperCAmelCase ) self.init_weights() @add_start_docstrings( '''RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top, also takes care of multi-layer training. ''' , lowercase , ) class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = RobertaConfig __snake_case = '''roberta''' def __init__( self : str , __UpperCAmelCase : str ) ->Any: """simple docstring""" super().__init__(__UpperCAmelCase ) a = config.num_labels a = config.num_hidden_layers a = DeeRobertaModel(__UpperCAmelCase ) a = nn.Dropout(config.hidden_dropout_prob ) a = nn.Linear(config.hidden_size , self.config.num_labels ) @add_start_docstrings_to_model_forward(__UpperCAmelCase ) def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : Tuple=None , __UpperCAmelCase : List[str]=None , __UpperCAmelCase : Any=None , __UpperCAmelCase : str=None , __UpperCAmelCase : int=None , __UpperCAmelCase : int=None , __UpperCAmelCase : List[Any]=None , __UpperCAmelCase : Optional[int]=-1 , __UpperCAmelCase : Optional[Any]=False , ) ->int: """simple docstring""" a = self.num_layers try: a = self.roberta( __UpperCAmelCase , attention_mask=__UpperCAmelCase , token_type_ids=__UpperCAmelCase , position_ids=__UpperCAmelCase , head_mask=__UpperCAmelCase , inputs_embeds=__UpperCAmelCase , ) a = outputs[1] a = self.dropout(__UpperCAmelCase ) a = self.classifier(__UpperCAmelCase ) a = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: a = e.message a = e.exit_layer a = outputs[0] if not self.training: a = entropy(__UpperCAmelCase ) a = [] a = [] if labels is not None: if self.num_labels == 1: # We are doing regression a = MSELoss() a = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: a = CrossEntropyLoss() a = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits a = [] for highway_exit in outputs[-1]: a = highway_exit[0] if not self.training: highway_logits_all.append(__UpperCAmelCase ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression a = MSELoss() a = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: a = CrossEntropyLoss() a = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(__UpperCAmelCase ) if train_highway: a = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: a = (loss,) + outputs if not self.training: a = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: a = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
26
import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def _a ( a :Tuple ) -> int: a = tmp_path / '''file.csv''' a = textwrap.dedent( '''\ header1,header2 1,2 10,20 ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :int ) -> List[str]: a = tmp_path / '''malformed_file.csv''' a = textwrap.dedent( '''\ header1,header2 1,2 10,20, ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :Dict , a :int ) -> List[str]: a = tmp_path / '''csv_with_image.csv''' a = textwrap.dedent( F"""\ image {image_file} """ ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :List[Any] ) -> Dict: a = tmp_path / '''csv_with_label.csv''' a = textwrap.dedent( '''\ label good bad good ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :Tuple ) -> Any: a = tmp_path / '''csv_with_int_list.csv''' a = textwrap.dedent( '''\ int_list 1 2 3 4 5 6 7 8 9 ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) def _a ( a :Dict , a :int , a :Union[str, Any] ) -> List[Any]: a = Csv() a = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(a , match='''Error tokenizing data''' ): for _ in generator: pass assert any( record.levelname == '''ERROR''' and '''Failed to read file''' in record.message and os.path.basename(a ) in record.message for record in caplog.records ) @require_pil def _a ( a :Dict ) -> Any: with open(a , encoding='''utf-8''' ) as f: a = f.read().splitlines()[1] a = Csv(encoding='''utf-8''' , features=Features({'''image''': Image()} ) ) a = csv._generate_tables([[csv_file_with_image]] ) a = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''image''' ).type == Image()() a = pa_table.to_pydict()['''image'''] assert generated_content == [{"path": image_file, "bytes": None}] def _a ( a :Any ) -> Tuple: with open(a , encoding='''utf-8''' ) as f: a = f.read().splitlines()[1:] a = Csv(encoding='''utf-8''' , features=Features({'''label''': ClassLabel(names=['''good''', '''bad'''] )} ) ) a = csv._generate_tables([[csv_file_with_label]] ) a = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''label''' ).type == ClassLabel(names=['''good''', '''bad'''] )() a = pa_table.to_pydict()['''label'''] assert generated_content == [ClassLabel(names=['''good''', '''bad'''] ).straint(a ) for label in labels] def _a ( a :Union[str, Any] ) -> Optional[Any]: a = Csv(encoding='''utf-8''' , sep=''',''' , converters={'''int_list''': lambda a : [int(a ) for i in x.split()]} ) a = csv._generate_tables([[csv_file_with_int_list]] ) a = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field('''int_list''' ).type ) a = pa_table.to_pydict()['''int_list'''] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
26
1
from typing import Any import numpy as np def _a ( a :np.ndarray ) -> bool: return np.array_equal(a , matrix.conjugate().T ) def _a ( a :np.ndarray , a :np.ndarray ) -> Any: a = v.conjugate().T a = v_star.dot(a ) assert isinstance(a , np.ndarray ) return (v_star_dot.dot(a )) / (v_star.dot(a )) def _a ( ) -> None: a = np.array([[2, 2 + 1j, 4], [2 - 1j, 3, 1j], [4, -1j, 1]] ) a = np.array([[1], [2], [3]] ) assert is_hermitian(a ), F"""{a} is not hermitian.""" print(rayleigh_quotient(a , a ) ) a = np.array([[1, 2, 4], [2, 3, -1], [4, -1, 1]] ) assert is_hermitian(a ), F"""{a} is not hermitian.""" assert rayleigh_quotient(a , a ) == float(3 ) if __name__ == "__main__": import doctest doctest.testmod() tests()
26
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SwiftFormerConfig, SwiftFormerForImageClassification, ViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = torch.device("cpu") def _a ( ) -> Union[str, Any]: a = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a = Image.open(requests.get(a , stream=a ).raw ) return im def _a ( a :Dict ) -> Tuple: if swiftformer_name == "swiftformer_xs": return torch.tensor([-2.1703e00, 2.1107e00, -2.0811e00, 8.8685e-01, 2.4360e-01] ) elif swiftformer_name == "swiftformer_s": return torch.tensor([3.9636e-01, 2.3478e-01, -1.6963e00, -1.7381e00, -8.6337e-01] ) elif swiftformer_name == "swiftformer_l1": return torch.tensor([-4.2768e-01, -4.7429e-01, -1.0897e00, -1.0248e00, 3.5523e-02] ) elif swiftformer_name == "swiftformer_l3": return torch.tensor([-2.5330e-01, 2.4211e-01, -6.0185e-01, -8.2789e-01, -6.0446e-02] ) def _a ( a :int , a :Any , a :Union[str, Any] ) -> int: a = dct.pop(a ) a = val def _a ( a :Any ) -> Dict: a = [] for k in state_dict.keys(): a = k if ".pwconv" in k: a = k_new.replace('''.pwconv''' , '''.point_wise_conv''' ) if ".dwconv" in k: a = k_new.replace('''.dwconv''' , '''.depth_wise_conv''' ) if ".Proj." in k: a = k_new.replace('''.Proj.''' , '''.proj.''' ) if "patch_embed" in k_new: a = k_new.replace('''patch_embed''' , '''swiftformer.patch_embed.patch_embedding''' ) if "network" in k_new: a = k_new.split('''.''' ) if ls[2].isdigit(): a = '''swiftformer.encoder.network.''' + ls[1] + '''.blocks.''' + ls[2] + '''.''' + '''.'''.join(ls[3:] ) else: a = k_new.replace('''network''' , '''swiftformer.encoder.network''' ) rename_keys.append((k, k_new) ) return rename_keys @torch.no_grad() def _a ( a :List[Any] , a :Tuple , a :List[str] ) -> Union[str, Any]: a = SwiftFormerConfig() # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size a = 1_000 a = '''huggingface/label-files''' a = '''imagenet-1k-id2label.json''' a = json.load(open(hf_hub_download(a , a , repo_type='''dataset''' ) , '''r''' ) ) a = {int(a ): v for k, v in idalabel.items()} a = idalabel a = {v: k for k, v in idalabel.items()} # size of the architecture if swiftformer_name == "swiftformer_xs": a = [3, 3, 6, 4] a = [48, 56, 112, 220] elif swiftformer_name == "swiftformer_s": a = [3, 3, 9, 6] a = [48, 64, 168, 224] elif swiftformer_name == "swiftformer_l1": a = [4, 3, 10, 5] a = [48, 96, 192, 384] elif swiftformer_name == "swiftformer_l3": a = [4, 4, 12, 6] a = [64, 128, 320, 512] # load state_dict of original model, remove and rename some keys if original_ckpt: if original_ckpt.startswith('''https''' ): a = torch.hub.load_state_dict_from_url(a , map_location='''cpu''' , check_hash=a ) else: a = torch.load(a , map_location='''cpu''' ) a = checkpoint a = create_rename_keys(a ) for rename_key_src, rename_key_dest in rename_keys: rename_key(a , a , a ) # load HuggingFace model a = SwiftFormerForImageClassification(a ).eval() hf_model.load_state_dict(a ) # prepare test inputs a = prepare_img() a = ViTImageProcessor.from_pretrained('''preprocessor_config''' ) a = processor(images=a , return_tensors='''pt''' ) # compare outputs from both models a = get_expected_output(a ) a = hf_model(inputs['''pixel_values'''] ).logits assert hf_logits.shape == torch.Size([1, 1_000] ) assert torch.allclose(hf_logits[0, 0:5] , a , atol=1e-3 ) Path(a ).mkdir(exist_ok=a ) print(F"""Saving model {swiftformer_name} to {pytorch_dump_folder_path}""" ) hf_model.save_pretrained(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--swiftformer_name", default="swiftformer_xs", choices=["swiftformer_xs", "swiftformer_s", "swiftformer_l1", "swiftformer_l3"], type=str, help="Name of the SwiftFormer model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default="./converted_outputs/", type=str, help="Path to the output PyTorch model directory.", ) parser.add_argument("--original_ckpt", default=None, type=str, help="Path to the original model checkpoint.") UpperCAmelCase__ = parser.parse_args() convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)
26
1
# 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. import argparse import os from accelerate.test_utils import execute_subprocess_async def _a ( a :str=None ) -> Tuple: if subparsers is not None: a = subparsers.add_parser('''test''' ) else: a = argparse.ArgumentParser('''Accelerate test command''' ) 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\'.''' ) , ) if subparsers is not None: parser.set_defaults(func=a ) return parser def _a ( a :Optional[Any] ) -> Dict: a = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ['''test_utils''', '''scripts''', '''test_script.py'''] ) if args.config_file is None: a = script_name else: a = F"""--config_file={args.config_file} {script_name}""" a = ['''accelerate-launch'''] + test_args.split() a = execute_subprocess_async(a , env=os.environ.copy() ) if result.returncode == 0: print('''Test is a success! You are ready for your distributed training!''' ) def _a ( ) -> Optional[int]: a = test_command_parser() a = parser.parse_args() test_command(a ) if __name__ == "__main__": main()
26
import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Union[str, Any] , __UpperCAmelCase : UNetaDModel , __UpperCAmelCase : UNetaDModel , __UpperCAmelCase : DDPMScheduler , __UpperCAmelCase : Optional[int] , ) ->List[str]: """simple docstring""" super().__init__() a = value_function a = unet a = scheduler a = env a = env.get_dataset() a = {} for key in self.data.keys(): try: a = self.data[key].mean() except: # noqa: E722 pass a = {} for key in self.data.keys(): try: a = self.data[key].std() except: # noqa: E722 pass a = env.observation_space.shape[0] a = env.action_space.shape[0] def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[int] ) ->Dict: """simple docstring""" return (x_in - self.means[key]) / self.stds[key] def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Dict , __UpperCAmelCase : Dict ) ->List[str]: """simple docstring""" return x_in * self.stds[key] + self.means[key] def __lowerCAmelCase ( self : int , __UpperCAmelCase : int ) ->List[str]: """simple docstring""" if type(__UpperCAmelCase ) is dict: return {k: self.to_torch(__UpperCAmelCase ) for k, v in x_in.items()} elif torch.is_tensor(__UpperCAmelCase ): return x_in.to(self.unet.device ) return torch.tensor(__UpperCAmelCase , device=self.unet.device ) def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : int , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Tuple ) ->int: """simple docstring""" for key, val in cond.items(): a = val.clone() return x_in def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[Any] ) ->Tuple: """simple docstring""" a = x.shape[0] a = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model a = torch.full((batch_size,) , __UpperCAmelCase , device=self.unet.device , dtype=torch.long ) for _ in range(__UpperCAmelCase ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models a = self.value_function(x.permute(0 , 2 , 1 ) , __UpperCAmelCase ).sample a = torch.autograd.grad([y.sum()] , [x] )[0] a = self.scheduler._get_variance(__UpperCAmelCase ) a = torch.exp(0.5 * posterior_variance ) a = model_std * grad a = 0 a = x.detach() a = x + scale * grad a = self.reset_xa(__UpperCAmelCase , __UpperCAmelCase , self.action_dim ) a = self.unet(x.permute(0 , 2 , 1 ) , __UpperCAmelCase ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg a = self.scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , predict_epsilon=__UpperCAmelCase )['''prev_sample'''] # apply conditions to the trajectory (set the initial state) a = self.reset_xa(__UpperCAmelCase , __UpperCAmelCase , self.action_dim ) a = self.to_torch(__UpperCAmelCase ) return x, y def __call__( self : Union[str, Any] , __UpperCAmelCase : Dict , __UpperCAmelCase : Optional[int]=64 , __UpperCAmelCase : int=32 , __UpperCAmelCase : Optional[Any]=2 , __UpperCAmelCase : str=0.1 ) ->List[str]: """simple docstring""" a = self.normalize(__UpperCAmelCase , '''observations''' ) a = obs[None].repeat(__UpperCAmelCase , axis=0 ) a = {0: self.to_torch(__UpperCAmelCase )} a = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) a = randn_tensor(__UpperCAmelCase , device=self.unet.device ) a = self.reset_xa(__UpperCAmelCase , __UpperCAmelCase , self.action_dim ) a = self.to_torch(__UpperCAmelCase ) # run the diffusion process a , a = self.run_diffusion(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) # sort output trajectories by value a = y.argsort(0 , descending=__UpperCAmelCase ).squeeze() a = x[sorted_idx] a = sorted_values[:, :, : self.action_dim] a = actions.detach().cpu().numpy() a = self.de_normalize(__UpperCAmelCase , key='''actions''' ) # select the action with the highest value if y is not None: a = 0 else: # if we didn't run value guiding, select a random action a = np.random.randint(0 , __UpperCAmelCase ) a = denorm_actions[selected_index, 0] return denorm_actions
26
1
UpperCAmelCase__ = "0.21.0" from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
26
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import SPIECE_UNDERLINE, logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {"vocab_file": "spiece.model"} UpperCAmelCase__ = { "vocab_file": { "TsinghuaAI/CPM-Generate": "https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model", } } class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Optional[Any] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Optional[Any]=False , __UpperCAmelCase : Any=True , __UpperCAmelCase : Optional[Any]=False , __UpperCAmelCase : List[str]="<s>" , __UpperCAmelCase : int="</s>" , __UpperCAmelCase : Any="<unk>" , __UpperCAmelCase : Optional[Any]="<sep>" , __UpperCAmelCase : int="<pad>" , __UpperCAmelCase : Any="<cls>" , __UpperCAmelCase : List[str]="<mask>" , __UpperCAmelCase : Optional[int]=["<eop>", "<eod>"] , __UpperCAmelCase : Optional[Dict[str, Any]] = None , **__UpperCAmelCase : Union[str, Any] , ) ->None: """simple docstring""" a = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else mask_token a = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__UpperCAmelCase , remove_space=__UpperCAmelCase , keep_accents=__UpperCAmelCase , bos_token=__UpperCAmelCase , eos_token=__UpperCAmelCase , unk_token=__UpperCAmelCase , sep_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , cls_token=__UpperCAmelCase , mask_token=__UpperCAmelCase , additional_special_tokens=__UpperCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **__UpperCAmelCase , ) a = 3 a = do_lower_case a = remove_space a = keep_accents a = vocab_file a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__UpperCAmelCase ) try: import jieba except ModuleNotFoundError as error: raise error.__class__( '''You need to install jieba to use CpmTokenizer or CpmTokenizerFast. ''' '''See https://pypi.org/project/jieba/ for installation.''' ) a = jieba a = str.maketrans(''' \n''' , '''\u2582\u2583''' ) @property # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size def __lowerCAmelCase ( self : Union[str, Any] ) ->Optional[Any]: """simple docstring""" return len(self.sp_model ) def __lowerCAmelCase ( self : Tuple ) ->List[str]: """simple docstring""" a = {self.convert_ids_to_tokens(__UpperCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) ->Optional[int]: """simple docstring""" a = self.__dict__.copy() a = None return state def __setstate__( self : List[str] , __UpperCAmelCase : Optional[int] ) ->str: """simple docstring""" a = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): a = {} a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : List[str] ) ->List[str]: """simple docstring""" if self.remove_space: a = ''' '''.join(inputs.strip().split() ) else: a = inputs a = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: a = unicodedata.normalize('''NFKD''' , __UpperCAmelCase ) a = ''''''.join([c for c in outputs if not unicodedata.combining(__UpperCAmelCase )] ) if self.do_lower_case: a = outputs.lower() return outputs def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : str ) ->List[str]: """simple docstring""" a = self.preprocess_text(__UpperCAmelCase ) a = self.sp_model.encode(__UpperCAmelCase , out_type=__UpperCAmelCase ) a = [] for piece in pieces: if len(__UpperCAmelCase ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): a = self.sp_model.EncodeAsPieces(piece[:-1].replace(__UpperCAmelCase , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: a = cur_pieces[1:] else: a = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(__UpperCAmelCase ) else: new_pieces.append(__UpperCAmelCase ) return new_pieces def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Any ) ->Any: """simple docstring""" return self.sp_model.PieceToId(__UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : Dict ) ->Union[str, Any]: """simple docstring""" return self.sp_model.IdToPiece(__UpperCAmelCase ) def __lowerCAmelCase ( self : Any , __UpperCAmelCase : str ) ->List[str]: """simple docstring""" a = ''''''.join(__UpperCAmelCase ).replace(__UpperCAmelCase , ''' ''' ).strip() return out_string def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None , __UpperCAmelCase : bool = False ) ->List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__UpperCAmelCase , token_ids_a=__UpperCAmelCase , already_has_special_tokens=__UpperCAmelCase ) if token_ids_a is not None: return ([0] * len(__UpperCAmelCase )) + [1] + ([0] * len(__UpperCAmelCase )) + [1, 1] return ([0] * len(__UpperCAmelCase )) + [1, 1] def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None ) ->Tuple[str]: """simple docstring""" if not os.path.isdir(__UpperCAmelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return a = os.path.join( __UpperCAmelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__UpperCAmelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __UpperCAmelCase ) elif not os.path.isfile(self.vocab_file ): with open(__UpperCAmelCase , '''wb''' ) as fi: a = self.sp_model.serialized_model_proto() fi.write(__UpperCAmelCase ) return (out_vocab_file,) def __lowerCAmelCase ( self : Any , *__UpperCAmelCase : List[str] , **__UpperCAmelCase : Optional[Any] ) ->Tuple: """simple docstring""" a = super()._decode(*__UpperCAmelCase , **__UpperCAmelCase ) a = text.replace(''' ''' , '''''' ).replace('''\u2582''' , ''' ''' ).replace('''\u2583''' , '''\n''' ) return text
26
1
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import YolosConfig, YolosForObjectDetection, YolosImageProcessor from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) def _a ( a :str ) -> YolosConfig: a = YolosConfig() # size of the architecture if "yolos_ti" in yolos_name: a = 192 a = 768 a = 12 a = 3 a = [800, 1_333] a = False elif yolos_name == "yolos_s_dWr": a = 330 a = 14 a = 6 a = 1_320 elif "yolos_s" in yolos_name: a = 384 a = 1_536 a = 12 a = 6 elif "yolos_b" in yolos_name: a = [800, 1_344] a = 91 a = '''huggingface/label-files''' a = '''coco-detection-id2label.json''' a = json.load(open(hf_hub_download(a , a , repo_type='''dataset''' ) , '''r''' ) ) a = {int(a ): v for k, v in idalabel.items()} a = idalabel a = {v: k for k, v in idalabel.items()} return config def _a ( a :dict , a :YolosConfig , a :bool = False ) -> int: for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) a = state_dict.pop(F"""blocks.{i}.attn.qkv.weight""" ) a = state_dict.pop(F"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict a = in_proj_weight[: config.hidden_size, :] a = in_proj_bias[: config.hidden_size] a = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] a = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] a = in_proj_weight[-config.hidden_size :, :] a = in_proj_bias[-config.hidden_size :] def _a ( a :str ) -> str: if "backbone" in name: a = name.replace('''backbone''' , '''vit''' ) if "cls_token" in name: a = name.replace('''cls_token''' , '''embeddings.cls_token''' ) if "det_token" in name: a = name.replace('''det_token''' , '''embeddings.detection_tokens''' ) if "mid_pos_embed" in name: a = name.replace('''mid_pos_embed''' , '''encoder.mid_position_embeddings''' ) if "pos_embed" in name: a = name.replace('''pos_embed''' , '''embeddings.position_embeddings''' ) if "patch_embed.proj" in name: a = name.replace('''patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) if "blocks" in name: a = name.replace('''blocks''' , '''encoder.layer''' ) if "attn.proj" in name: a = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name: a = name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: a = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: a = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: a = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: a = name.replace('''mlp.fc2''' , '''output.dense''' ) if "class_embed" in name: a = name.replace('''class_embed''' , '''class_labels_classifier''' ) if "bbox_embed" in name: a = name.replace('''bbox_embed''' , '''bbox_predictor''' ) if "vit.norm" in name: a = name.replace('''vit.norm''' , '''vit.layernorm''' ) return name def _a ( a :dict , a :YolosForObjectDetection ) -> dict: for key in orig_state_dict.copy().keys(): a = orig_state_dict.pop(a ) if "qkv" in key: a = key.split('''.''' ) a = int(key_split[2] ) a = model.vit.encoder.layer[layer_num].attention.attention.all_head_size if "weight" in key: a = val[:dim, :] a = val[ dim : dim * 2, : ] a = val[-dim:, :] else: a = val[:dim] a = val[dim : dim * 2] a = val[-dim:] else: a = val return orig_state_dict def _a ( ) -> torch.Tensor: a = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a = Image.open(requests.get(a , stream=a ).raw ) return im @torch.no_grad() def _a ( a :str , a :str , a :str , a :bool = False ) -> List[str]: a = get_yolos_config(a ) # load original state_dict a = torch.load(a , map_location='''cpu''' )['''model'''] # load 🤗 model a = YolosForObjectDetection(a ) model.eval() a = convert_state_dict(a , a ) model.load_state_dict(a ) # Check outputs on an image, prepared by YolosImageProcessor a = 800 if yolos_name != '''yolos_ti''' else 512 a = YolosImageProcessor(format='''coco_detection''' , size=a ) a = image_processor(images=prepare_img() , return_tensors='''pt''' ) a = model(**a ) a , a = outputs.logits, outputs.pred_boxes a , a = None, None if yolos_name == "yolos_ti": a = torch.tensor( [[-39.5_022, -11.9_820, -17.6_888], [-29.9_574, -9.9_769, -17.7_691], [-42.3_281, -20.7_200, -30.6_294]] ) a = torch.tensor( [[0.4_021, 0.0_836, 0.7_979], [0.0_184, 0.2_609, 0.0_364], [0.1_781, 0.2_004, 0.2_095]] ) elif yolos_name == "yolos_s_200_pre": a = torch.tensor( [[-24.0_248, -10.3_024, -14.8_290], [-42.0_392, -16.8_200, -27.4_334], [-27.2_743, -11.8_154, -18.7_148]] ) a = torch.tensor( [[0.2_559, 0.5_455, 0.4_706], [0.2_989, 0.7_279, 0.1_875], [0.7_732, 0.4_017, 0.4_462]] ) elif yolos_name == "yolos_s_300_pre": a = torch.tensor( [[-36.2_220, -14.4_385, -23.5_457], [-35.6_970, -14.7_583, -21.3_935], [-31.5_939, -13.6_042, -16.8_049]] ) a = torch.tensor( [[0.7_614, 0.2_316, 0.4_728], [0.7_168, 0.4_495, 0.3_855], [0.4_996, 0.1_466, 0.9_996]] ) elif yolos_name == "yolos_s_dWr": a = torch.tensor( [[-42.8_668, -24.1_049, -41.1_690], [-34.7_456, -14.1_274, -24.9_194], [-33.7_898, -12.1_946, -25.6_495]] ) a = torch.tensor( [[0.5_587, 0.2_773, 0.0_605], [0.5_004, 0.3_014, 0.9_994], [0.4_999, 0.1_548, 0.9_994]] ) elif yolos_name == "yolos_base": a = torch.tensor( [[-40.6_064, -24.3_084, -32.6_447], [-55.1_990, -30.7_719, -35.5_877], [-51.4_311, -33.3_507, -35.6_462]] ) a = torch.tensor( [[0.5_555, 0.2_794, 0.0_655], [0.9_049, 0.2_664, 0.1_894], [0.9_183, 0.1_984, 0.1_635]] ) else: raise ValueError(F"""Unknown yolos_name: {yolos_name}""" ) assert torch.allclose(logits[0, :3, :3] , a , atol=1e-4 ) assert torch.allclose(pred_boxes[0, :3, :3] , a , atol=1e-4 ) Path(a ).mkdir(exist_ok=a ) print(F"""Saving model {yolos_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(a ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(a ) if push_to_hub: a = { '''yolos_ti''': '''yolos-tiny''', '''yolos_s_200_pre''': '''yolos-small''', '''yolos_s_300_pre''': '''yolos-small-300''', '''yolos_s_dWr''': '''yolos-small-dwr''', '''yolos_base''': '''yolos-base''', } print('''Pushing to the hub...''' ) a = model_mapping[yolos_name] image_processor.push_to_hub(a , organization='''hustvl''' ) model.push_to_hub(a , organization='''hustvl''' ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--yolos_name", default="yolos_s_200_pre", type=str, help=( "Name of the YOLOS model you'd like to convert. Should be one of 'yolos_ti', 'yolos_s_200_pre'," " 'yolos_s_300_pre', 'yolos_s_dWr', 'yolos_base'." ), ) parser.add_argument( "--checkpoint_path", default=None, 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." ) UpperCAmelCase__ = parser.parse_args() convert_yolos_checkpoint(args.yolos_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
26
import argparse import io import requests import torch from omegaconf import OmegaConf from diffusers import AutoencoderKL from diffusers.pipelines.stable_diffusion.convert_from_ckpt import ( assign_to_checkpoint, conv_attn_to_linear, create_vae_diffusers_config, renew_vae_attention_paths, renew_vae_resnet_paths, ) def _a ( a :Union[str, Any] , a :List[Any] ) -> List[Any]: a = checkpoint a = {} a = vae_state_dict['''encoder.conv_in.weight'''] a = vae_state_dict['''encoder.conv_in.bias'''] a = vae_state_dict['''encoder.conv_out.weight'''] a = vae_state_dict['''encoder.conv_out.bias'''] a = vae_state_dict['''encoder.norm_out.weight'''] a = vae_state_dict['''encoder.norm_out.bias'''] a = vae_state_dict['''decoder.conv_in.weight'''] a = vae_state_dict['''decoder.conv_in.bias'''] a = vae_state_dict['''decoder.conv_out.weight'''] a = vae_state_dict['''decoder.conv_out.bias'''] a = vae_state_dict['''decoder.norm_out.weight'''] a = vae_state_dict['''decoder.norm_out.bias'''] a = vae_state_dict['''quant_conv.weight'''] a = vae_state_dict['''quant_conv.bias'''] a = vae_state_dict['''post_quant_conv.weight'''] a = vae_state_dict['''post_quant_conv.bias'''] # Retrieves the keys for the encoder down blocks only a = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''encoder.down''' in layer} ) a = { layer_id: [key for key in vae_state_dict if F"""down.{layer_id}""" in key] for layer_id in range(a ) } # Retrieves the keys for the decoder up blocks only a = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''decoder.up''' in layer} ) a = { layer_id: [key for key in vae_state_dict if F"""up.{layer_id}""" in key] for layer_id in range(a ) } for i in range(a ): a = [key for key in down_blocks[i] if F"""down.{i}""" in key and F"""down.{i}.downsample""" not in key] if F"""encoder.down.{i}.downsample.conv.weight""" in vae_state_dict: a = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.weight""" ) a = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.bias""" ) a = renew_vae_resnet_paths(a ) a = {'''old''': F"""down.{i}.block""", '''new''': F"""down_blocks.{i}.resnets"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''encoder.mid.block''' in key] a = 2 for i in range(1 , num_mid_res_blocks + 1 ): a = [key for key in mid_resnets if F"""encoder.mid.block_{i}""" in key] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""mid.block_{i}""", '''new''': F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''encoder.mid.attn''' in key] a = renew_vae_attention_paths(a ) a = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) conv_attn_to_linear(a ) for i in range(a ): a = num_up_blocks - 1 - i a = [ key for key in up_blocks[block_id] if F"""up.{block_id}""" in key and F"""up.{block_id}.upsample""" not in key ] if F"""decoder.up.{block_id}.upsample.conv.weight""" in vae_state_dict: a = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.weight""" ] a = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.bias""" ] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""up.{block_id}.block""", '''new''': F"""up_blocks.{i}.resnets"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''decoder.mid.block''' in key] a = 2 for i in range(1 , num_mid_res_blocks + 1 ): a = [key for key in mid_resnets if F"""decoder.mid.block_{i}""" in key] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""mid.block_{i}""", '''new''': F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''decoder.mid.attn''' in key] a = renew_vae_attention_paths(a ) a = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) conv_attn_to_linear(a ) return new_checkpoint def _a ( a :str , a :str , ) -> List[str]: # Only support V1 a = requests.get( ''' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml''' ) a = io.BytesIO(r.content ) a = OmegaConf.load(a ) a = 512 a = '''cuda''' if torch.cuda.is_available() else '''cpu''' if checkpoint_path.endswith('''safetensors''' ): from safetensors import safe_open a = {} with safe_open(a , framework='''pt''' , device='''cpu''' ) as f: for key in f.keys(): a = f.get_tensor(a ) else: a = torch.load(a , map_location=a )['''state_dict'''] # Convert the VAE model. a = create_vae_diffusers_config(a , image_size=a ) a = custom_convert_ldm_vae_checkpoint(a , a ) a = AutoencoderKL(**a ) vae.load_state_dict(a ) vae.save_pretrained(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--vae_pt_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") UpperCAmelCase__ = parser.parse_args() vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)
26
1
from __future__ import annotations import inspect import unittest from typing import List, Tuple from transformers import RegNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available 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 TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowercase_ : '''simple docstring''' def __init__( self : Tuple , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : int=3 , __UpperCAmelCase : int=32 , __UpperCAmelCase : str=3 , __UpperCAmelCase : List[str]=10 , __UpperCAmelCase : Optional[Any]=[10, 20, 30, 40] , __UpperCAmelCase : List[Any]=[1, 1, 2, 1] , __UpperCAmelCase : int=True , __UpperCAmelCase : List[str]=True , __UpperCAmelCase : Any="relu" , __UpperCAmelCase : int=3 , __UpperCAmelCase : Any=None , ) ->Union[str, Any]: """simple docstring""" a = parent a = batch_size a = image_size a = num_channels a = embeddings_size a = hidden_sizes a = depths a = is_training a = use_labels a = hidden_act a = num_labels a = scope a = len(__UpperCAmelCase ) def __lowerCAmelCase ( self : Dict ) ->Any: """simple docstring""" a = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) a = None if self.use_labels: a = ids_tensor([self.batch_size] , self.num_labels ) a = self.get_config() return config, pixel_values, labels def __lowerCAmelCase ( self : int ) ->Tuple: """simple docstring""" return RegNetConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , ) def __lowerCAmelCase ( self : str , __UpperCAmelCase : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : Optional[Any] ) ->str: """simple docstring""" a = TFRegNetModel(config=__UpperCAmelCase ) a = model(__UpperCAmelCase , training=__UpperCAmelCase ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[Any] ) ->Optional[Any]: """simple docstring""" a = self.num_labels a = TFRegNetForImageClassification(__UpperCAmelCase ) a = model(__UpperCAmelCase , labels=__UpperCAmelCase , training=__UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self : Dict ) ->str: """simple docstring""" a = self.prepare_config_and_inputs() a , a , a = config_and_inputs a = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class lowercase_ ( lowercase , lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = (TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else () __snake_case = ( {'''feature-extraction''': TFRegNetModel, '''image-classification''': TFRegNetForImageClassification} if is_tf_available() else {} ) __snake_case = False __snake_case = False __snake_case = False __snake_case = False __snake_case = False def __lowerCAmelCase ( self : Tuple ) ->int: """simple docstring""" a = TFRegNetModelTester(self ) a = ConfigTester(self , config_class=__UpperCAmelCase , has_text_modality=__UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[int] ) ->Dict: """simple docstring""" return @unittest.skip(reason='''RegNet does not use inputs_embeds''' ) def __lowerCAmelCase ( self : List[str] ) ->Union[str, Any]: """simple docstring""" pass @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices('''GPU''' ) ) == 0 , reason='''TF does not support backprop for grouped convolutions on CPU.''' , ) @slow def __lowerCAmelCase ( self : List[str] ) ->Optional[int]: """simple docstring""" super().test_keras_fit() @unittest.skip(reason='''RegNet does not support input and output embeddings''' ) def __lowerCAmelCase ( self : Tuple ) ->Optional[Any]: """simple docstring""" pass def __lowerCAmelCase ( self : Any ) ->List[Any]: """simple docstring""" a , a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a = model_class(__UpperCAmelCase ) a = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a = [*signature.parameters.keys()] a = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , __UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[Any] ) ->Optional[Any]: """simple docstring""" a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[int] ) ->Any: """simple docstring""" def check_hidden_states_output(__UpperCAmelCase : Optional[int] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Union[str, Any] ): a = model_class(__UpperCAmelCase ) a = model(**self._prepare_for_class(__UpperCAmelCase , __UpperCAmelCase ) , training=__UpperCAmelCase ) a = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states a = self.model_tester.num_stages self.assertEqual(len(__UpperCAmelCase ) , expected_num_stages + 1 ) # RegNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 2, self.model_tester.image_size // 2] , ) a , a = self.model_tester.prepare_config_and_inputs_for_common() a = ['''basic''', '''bottleneck'''] for model_class in self.all_model_classes: for layer_type in layers_type: a = layer_type a = True check_hidden_states_output(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] a = True check_hidden_states_output(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[Any] ) ->Dict: """simple docstring""" a , a = self.model_tester.prepare_config_and_inputs_for_common() def check_equivalence(__UpperCAmelCase : List[str] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Optional[Any]={} ): a = model(__UpperCAmelCase , return_dict=__UpperCAmelCase , **__UpperCAmelCase ) a = model(__UpperCAmelCase , return_dict=__UpperCAmelCase , **__UpperCAmelCase ).to_tuple() def recursive_check(__UpperCAmelCase : Optional[Any] , __UpperCAmelCase : int ): if isinstance(__UpperCAmelCase , (List, Tuple) ): for tuple_iterable_value, dict_iterable_value in zip(__UpperCAmelCase , __UpperCAmelCase ): recursive_check(__UpperCAmelCase , __UpperCAmelCase ) elif tuple_object is None: return else: self.assertTrue( all(tf.equal(__UpperCAmelCase , __UpperCAmelCase ) ) , msg=( '''Tuple and dict output are not equal. Difference:''' F""" {tf.math.reduce_max(tf.abs(tuple_object - dict_object ) )}""" ) , ) recursive_check(__UpperCAmelCase , __UpperCAmelCase ) for model_class in self.all_model_classes: a = model_class(__UpperCAmelCase ) a = self._prepare_for_class(__UpperCAmelCase , __UpperCAmelCase ) a = self._prepare_for_class(__UpperCAmelCase , __UpperCAmelCase ) check_equivalence(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) a = self._prepare_for_class(__UpperCAmelCase , __UpperCAmelCase , return_labels=__UpperCAmelCase ) a = self._prepare_for_class(__UpperCAmelCase , __UpperCAmelCase , return_labels=__UpperCAmelCase ) check_equivalence(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) a = self._prepare_for_class(__UpperCAmelCase , __UpperCAmelCase ) a = self._prepare_for_class(__UpperCAmelCase , __UpperCAmelCase ) check_equivalence(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , {'''output_hidden_states''': True} ) a = self._prepare_for_class(__UpperCAmelCase , __UpperCAmelCase , return_labels=__UpperCAmelCase ) a = self._prepare_for_class(__UpperCAmelCase , __UpperCAmelCase , return_labels=__UpperCAmelCase ) check_equivalence(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , {'''output_hidden_states''': True} ) def __lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase ) @slow def __lowerCAmelCase ( self : Dict ) ->Tuple: """simple docstring""" for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a = TFRegNetModel.from_pretrained(__UpperCAmelCase ) self.assertIsNotNone(__UpperCAmelCase ) def _a ( ) -> List[str]: a = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf @require_vision class lowercase_ ( unittest.TestCase ): '''simple docstring''' @cached_property def __lowerCAmelCase ( self : int ) ->Tuple: """simple docstring""" return ( AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __lowerCAmelCase ( self : Any ) ->Optional[int]: """simple docstring""" a = TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) a = self.default_image_processor a = prepare_img() a = image_processor(images=__UpperCAmelCase , return_tensors='''tf''' ) # forward pass a = model(**__UpperCAmelCase , training=__UpperCAmelCase ) # verify the logits a = tf.TensorShape((1, 1_000) ) self.assertEqual(outputs.logits.shape , __UpperCAmelCase ) a = tf.constant([-0.4180, -1.5051, -3.4836] ) tf.debugging.assert_near(outputs.logits[0, :3] , __UpperCAmelCase , atol=1e-4 )
26
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = ['''image_processor''', '''tokenizer'''] __snake_case = '''CLIPImageProcessor''' __snake_case = ('''CLIPTokenizer''', '''CLIPTokenizerFast''') def __init__( self : Dict , __UpperCAmelCase : str=None , __UpperCAmelCase : Optional[Any]=None , **__UpperCAmelCase : Optional[Any] ) ->List[str]: """simple docstring""" a = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __UpperCAmelCase , ) a = kwargs.pop('''feature_extractor''' ) a = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(__UpperCAmelCase , __UpperCAmelCase ) def __call__( self : List[str] , __UpperCAmelCase : Any=None , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Any=None , **__UpperCAmelCase : str ) ->Optional[Any]: """simple docstring""" if text is None and images is None: raise ValueError('''You have to specify either text or images. Both cannot be none.''' ) if text is not None: a = self.tokenizer(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase ) if images is not None: a = self.image_processor(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase ) if text is not None and images is not None: a = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__UpperCAmelCase ) , tensor_type=__UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple , *__UpperCAmelCase : List[str] , **__UpperCAmelCase : Union[str, Any] ) ->Union[str, Any]: """simple docstring""" return self.tokenizer.batch_decode(*__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple , *__UpperCAmelCase : str , **__UpperCAmelCase : Tuple ) ->Any: """simple docstring""" return self.tokenizer.decode(*__UpperCAmelCase , **__UpperCAmelCase ) @property def __lowerCAmelCase ( self : int ) ->List[str]: """simple docstring""" a = self.tokenizer.model_input_names a = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def __lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __UpperCAmelCase , ) return self.image_processor_class @property def __lowerCAmelCase ( self : Union[str, Any] ) ->Optional[int]: """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __UpperCAmelCase , ) return self.image_processor
26
1
def _a ( ) -> int: return [ a * b * (1_000 - a - b) for a in range(1 , 999 ) for b in range(a , 999 ) if (a * a + b * b == (1_000 - a - b) ** 2) ][0] if __name__ == "__main__": print(f"""{solution() = }""")
26
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_distilbert import DistilBertTokenizer UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} UpperCAmelCase__ = { "vocab_file": { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt" ), "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt", "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt" ), }, "tokenizer_file": { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json" ), "distilbert-base-german-cased": ( "https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json" ), "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json" ), }, } UpperCAmelCase__ = { "distilbert-base-uncased": 512, "distilbert-base-uncased-distilled-squad": 512, "distilbert-base-cased": 512, "distilbert-base-cased-distilled-squad": 512, "distilbert-base-german-cased": 512, "distilbert-base-multilingual-cased": 512, } UpperCAmelCase__ = { "distilbert-base-uncased": {"do_lower_case": True}, "distilbert-base-uncased-distilled-squad": {"do_lower_case": True}, "distilbert-base-cased": {"do_lower_case": False}, "distilbert-base-cased-distilled-squad": {"do_lower_case": False}, "distilbert-base-german-cased": {"do_lower_case": False}, "distilbert-base-multilingual-cased": {"do_lower_case": False}, } class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = VOCAB_FILES_NAMES __snake_case = PRETRAINED_VOCAB_FILES_MAP __snake_case = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __snake_case = PRETRAINED_INIT_CONFIGURATION __snake_case = ['''input_ids''', '''attention_mask'''] __snake_case = DistilBertTokenizer def __init__( self : Dict , __UpperCAmelCase : List[Any]=None , __UpperCAmelCase : Optional[int]=None , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Optional[int]="[UNK]" , __UpperCAmelCase : str="[SEP]" , __UpperCAmelCase : Tuple="[PAD]" , __UpperCAmelCase : Any="[CLS]" , __UpperCAmelCase : int="[MASK]" , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Optional[Any]=None , **__UpperCAmelCase : str , ) ->Optional[int]: """simple docstring""" super().__init__( __UpperCAmelCase , tokenizer_file=__UpperCAmelCase , do_lower_case=__UpperCAmelCase , unk_token=__UpperCAmelCase , sep_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , cls_token=__UpperCAmelCase , mask_token=__UpperCAmelCase , tokenize_chinese_chars=__UpperCAmelCase , strip_accents=__UpperCAmelCase , **__UpperCAmelCase , ) a = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , __UpperCAmelCase ) != do_lower_case or normalizer_state.get('''strip_accents''' , __UpperCAmelCase ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , __UpperCAmelCase ) != tokenize_chinese_chars ): a = getattr(__UpperCAmelCase , normalizer_state.pop('''type''' ) ) a = do_lower_case a = strip_accents a = tokenize_chinese_chars a = normalizer_class(**__UpperCAmelCase ) a = do_lower_case def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[int]=None ) ->Optional[Any]: """simple docstring""" a = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [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 __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None ) ->Tuple[str]: """simple docstring""" a = self._tokenizer.model.save(__UpperCAmelCase , name=__UpperCAmelCase ) return tuple(__UpperCAmelCase )
26
1
from __future__ import annotations import numpy as np def _a ( a :list[float] ) -> Optional[Any]: return np.maximum(0 , a ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
26
from __future__ import annotations import typing from collections import Counter def _a ( a :int ) -> typing.Counter[int]: a = Counter() for base in range(1 , max_perimeter + 1 ): for perpendicular in range(a , max_perimeter + 1 ): a = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(a ): a = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def _a ( a :int = 1_000 ) -> int: a = pythagorean_triple(a ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(f"""Perimeter {solution()} has maximum solutions""")
26
1
from typing import Dict, Iterable, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging UpperCAmelCase__ = logging.get_logger(__name__) class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = ['''pixel_values'''] def __init__( self : Optional[int] , __UpperCAmelCase : bool = True , __UpperCAmelCase : Dict[str, int] = None , __UpperCAmelCase : PILImageResampling = PILImageResampling.BICUBIC , __UpperCAmelCase : bool = True , __UpperCAmelCase : Dict[str, int] = None , __UpperCAmelCase : bool = True , __UpperCAmelCase : Union[int, float] = 1 / 255 , __UpperCAmelCase : bool = True , __UpperCAmelCase : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_MEAN , __UpperCAmelCase : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_STD , **__UpperCAmelCase : str , ) ->None: """simple docstring""" super().__init__(**__UpperCAmelCase ) a = size if size is not None else {'''shortest_edge''': 224} a = get_size_dict(__UpperCAmelCase , default_to_square=__UpperCAmelCase ) a = crop_size if crop_size is not None else {'''height''': 224, '''width''': 224} a = get_size_dict(__UpperCAmelCase , param_name='''crop_size''' ) a = do_resize a = size a = resample a = do_center_crop a = crop_size a = do_rescale a = rescale_factor a = do_normalize a = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN a = image_std if image_std is not None else IMAGENET_DEFAULT_STD def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : np.ndarray , __UpperCAmelCase : Dict[str, int] , __UpperCAmelCase : PILImageResampling = PILImageResampling.BICUBIC , __UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__UpperCAmelCase : Tuple , ) ->np.ndarray: """simple docstring""" a = get_size_dict(__UpperCAmelCase , default_to_square=__UpperCAmelCase ) # size_dict is a dict with either keys "height" and "width" or "shortest_edge" if "shortest_edge" in size: a = int((256 / 224) * size['''shortest_edge'''] ) a = get_resize_output_image_size(__UpperCAmelCase , size=__UpperCAmelCase , default_to_square=__UpperCAmelCase ) a = {'''height''': output_size[0], '''width''': output_size[1]} if "height" not in size_dict or "width" not in size_dict: raise ValueError( F"""Size dict must have keys 'height' and 'width' or 'shortest_edge'. Got {size_dict.keys()}""" ) return resize( __UpperCAmelCase , size=(size_dict['''height'''], size_dict['''width''']) , resample=__UpperCAmelCase , data_format=__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : np.ndarray , __UpperCAmelCase : Dict[str, int] , __UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__UpperCAmelCase : Optional[int] , ) ->np.ndarray: """simple docstring""" a = get_size_dict(__UpperCAmelCase ) if "height" not in size or "width" not in size: raise ValueError(F"""Size dict must have keys 'height' and 'width'. Got {size.keys()}""" ) return center_crop(__UpperCAmelCase , size=(size['''height'''], size['''width''']) , data_format=__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : np.ndarray , __UpperCAmelCase : Union[int, float] , __UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__UpperCAmelCase : Optional[Any] , ) ->np.ndarray: """simple docstring""" return rescale(__UpperCAmelCase , scale=__UpperCAmelCase , data_format=__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : str , __UpperCAmelCase : np.ndarray , __UpperCAmelCase : Union[float, List[float]] , __UpperCAmelCase : Union[float, List[float]] , __UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__UpperCAmelCase : Union[str, Any] , ) ->np.ndarray: """simple docstring""" return normalize(__UpperCAmelCase , mean=__UpperCAmelCase , std=__UpperCAmelCase , data_format=__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : ImageInput , __UpperCAmelCase : Optional[bool] = None , __UpperCAmelCase : Optional[Dict[str, int]] = None , __UpperCAmelCase : PILImageResampling = None , __UpperCAmelCase : Optional[bool] = None , __UpperCAmelCase : Optional[Dict[str, int]] = None , __UpperCAmelCase : Optional[bool] = None , __UpperCAmelCase : Optional[float] = None , __UpperCAmelCase : Optional[bool] = None , __UpperCAmelCase : Optional[Union[float, Iterable[float]]] = None , __UpperCAmelCase : Optional[Union[float, Iterable[float]]] = None , __UpperCAmelCase : Optional[TensorType] = None , __UpperCAmelCase : ChannelDimension = ChannelDimension.FIRST , **__UpperCAmelCase : List[Any] , ) ->BatchFeature: """simple docstring""" a = do_resize if do_resize is not None else self.do_resize a = resample if resample is not None else self.resample a = do_center_crop if do_center_crop is not None else self.do_center_crop a = do_rescale if do_rescale is not None else self.do_rescale a = rescale_factor if rescale_factor is not None else self.rescale_factor a = do_normalize if do_normalize is not None else self.do_normalize a = image_mean if image_mean is not None else self.image_mean a = image_std if image_std is not None else self.image_std a = size if size is not None else self.size a = get_size_dict(__UpperCAmelCase , default_to_square=__UpperCAmelCase ) a = crop_size if crop_size is not None else self.crop_size a = get_size_dict(__UpperCAmelCase , param_name='''crop_size''' ) a = make_list_of_images(__UpperCAmelCase ) if not valid_images(__UpperCAmelCase ): 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.''' ) # All transformations expect numpy arrays. a = [to_numpy_array(__UpperCAmelCase ) for image in images] if do_resize: a = [self.resize(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) for image in images] if do_center_crop: a = [self.center_crop(__UpperCAmelCase , __UpperCAmelCase ) for image in images] if do_rescale: a = [self.rescale(__UpperCAmelCase , __UpperCAmelCase ) for image in images] if do_normalize: a = [self.normalize(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) for image in images] a = [to_channel_dimension_format(__UpperCAmelCase , __UpperCAmelCase ) for image in images] a = {'''pixel_values''': images} return BatchFeature(data=__UpperCAmelCase , tensor_type=__UpperCAmelCase )
26
from __future__ import annotations def _a ( a :dict , a :str ) -> set[str]: a , a = set(a ), [start] while stack: a = stack.pop() explored.add(a ) # 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(a ) return explored UpperCAmelCase__ = { "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"))
26
1
def _a ( a :list , a :int , a :int = 0 , a :int = 0 ) -> int: a = right or len(a ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(a , a , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
26
import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm UpperCAmelCase__ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex UpperCAmelCase__ = 10 UpperCAmelCase__ = 256 def _a ( a :List[str] ) -> Optional[MinHash]: if len(a ) < MIN_NUM_TOKENS: return None a = MinHash(num_perm=a ) for token in set(a ): min_hash.update(token.encode() ) return min_hash def _a ( a :str ) -> Set[str]: return {t for t in NON_ALPHA.split(a ) if len(t.strip() ) > 0} class lowercase_ : '''simple docstring''' def __init__( self : Any , *, __UpperCAmelCase : float = 0.85 , ) ->Dict: """simple docstring""" a = duplication_jaccard_threshold a = NUM_PERM a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) a = defaultdict(__UpperCAmelCase ) def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Tuple , __UpperCAmelCase : MinHash ) ->None: """simple docstring""" a = self._index.query(__UpperCAmelCase ) if code_key in self._index.keys: print(F"""Duplicate key {code_key}""" ) return self._index.insert(__UpperCAmelCase , __UpperCAmelCase ) if len(__UpperCAmelCase ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(__UpperCAmelCase ) break else: self._duplicate_clusters[close_duplicates[0]].add(__UpperCAmelCase ) def __lowerCAmelCase ( self : Dict ) ->List[List[Dict]]: """simple docstring""" a = [] for base, duplicates in self._duplicate_clusters.items(): a = [base] + list(__UpperCAmelCase ) # reformat the cluster to be a list of dict a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(__UpperCAmelCase ) return duplicate_clusters def __lowerCAmelCase ( self : Any , __UpperCAmelCase : Dict ) ->None: """simple docstring""" a = self.get_duplicate_clusters() with open(__UpperCAmelCase , '''w''' ) as f: json.dump(__UpperCAmelCase , __UpperCAmelCase ) def _a ( a :List[Any] ) -> List[Any]: a , a = element a = get_min_hash([t for t in NON_ALPHA.split(data['''content'''] ) if len(t.strip() ) > 0] ) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def _a ( a :Type[Dataset] ) -> List[Any]: with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(a , max_queue_size=10_000 ) , chunksize=100 , ): if data is not None: yield data def _a ( a :Type[Dataset] , a :float ) -> str: a = DuplicationIndex(duplication_jaccard_threshold=a ) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(a ) ) , max_queue_size=100 ) ): di.add(a , a ) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def _a ( a :str , a :str ) -> float: a = get_tokens(a ) a = get_tokens(a ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) UpperCAmelCase__ = None def _a ( a :Tuple , a :Tuple ) -> Any: a = [] for elementa in cluster: a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(a , a ) >= jaccard_threshold: elementa["copies"] += 1 break else: a = 1 extremes.append(a ) return extremes def _a ( a :List[Any] , a :Optional[Any] , a :Union[str, Any] ) -> Optional[int]: global _shared_dataset a = dataset a = [] a = partial(_find_cluster_extremes_shared , jaccard_threshold=a ) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( a , a , ) , total=len(a ) , ): extremes_list.append(a ) return extremes_list def _a ( a :Type[Dataset] , a :float = 0.85 ) -> Tuple[Type[Dataset], List[List[Dict]]]: a = make_duplicate_clusters(a , a ) a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} a = {} a = find_extremes(a , a , a ) for extremes in extremes_clusters: for element in extremes: a = element a = duplicate_indices - set(extreme_dict.keys() ) a = dataset.filter(lambda a , a : idx not in remove_indices , with_indices=a ) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: a = element['''base_index'''] in extreme_dict if element["is_extreme"]: a = extreme_dict[element['''base_index''']]['''copies'''] print(F"""Original dataset size: {len(a )}""" ) print(F"""Number of duplicate clusters: {len(a )}""" ) print(F"""Files in duplicate cluster: {len(a )}""" ) print(F"""Unique files in duplicate cluster: {len(a )}""" ) print(F"""Filtered dataset size: {len(a )}""" ) return ds_filter, duplicate_clusters
26
1
import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = ['''image_processor''', '''tokenizer'''] __snake_case = '''LayoutLMv3ImageProcessor''' __snake_case = ('''LayoutLMv3Tokenizer''', '''LayoutLMv3TokenizerFast''') def __init__( self : Tuple , __UpperCAmelCase : int=None , __UpperCAmelCase : Dict=None , **__UpperCAmelCase : Optional[Any] ) ->Tuple: """simple docstring""" a = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __UpperCAmelCase , ) a = kwargs.pop('''feature_extractor''' ) a = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(__UpperCAmelCase , __UpperCAmelCase ) def __call__( self : Tuple , __UpperCAmelCase : List[str] , __UpperCAmelCase : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , __UpperCAmelCase : Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None , __UpperCAmelCase : Union[List[List[int]], List[List[List[int]]]] = None , __UpperCAmelCase : Optional[Union[List[int], List[List[int]]]] = None , __UpperCAmelCase : bool = True , __UpperCAmelCase : Union[bool, str, PaddingStrategy] = False , __UpperCAmelCase : Union[bool, str, TruncationStrategy] = None , __UpperCAmelCase : Optional[int] = None , __UpperCAmelCase : int = 0 , __UpperCAmelCase : Optional[int] = None , __UpperCAmelCase : Optional[bool] = None , __UpperCAmelCase : Optional[bool] = None , __UpperCAmelCase : bool = False , __UpperCAmelCase : bool = False , __UpperCAmelCase : bool = False , __UpperCAmelCase : bool = False , __UpperCAmelCase : bool = True , __UpperCAmelCase : Optional[Union[str, TensorType]] = None , **__UpperCAmelCase : Tuple , ) ->BatchEncoding: """simple docstring""" if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( '''You cannot provide bounding boxes if you initialized the image processor with apply_ocr set to True.''' ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.''' ) # first, apply the image processor a = self.image_processor(images=__UpperCAmelCase , return_tensors=__UpperCAmelCase ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = [text] # add batch dimension (as the image processor always adds a batch dimension) a = features['''words'''] a = self.tokenizer( text=text if text is not None else features['''words'''] , text_pair=text_pair if text_pair is not None else None , boxes=boxes if boxes is not None else features['''boxes'''] , word_labels=__UpperCAmelCase , add_special_tokens=__UpperCAmelCase , padding=__UpperCAmelCase , truncation=__UpperCAmelCase , max_length=__UpperCAmelCase , stride=__UpperCAmelCase , pad_to_multiple_of=__UpperCAmelCase , return_token_type_ids=__UpperCAmelCase , return_attention_mask=__UpperCAmelCase , return_overflowing_tokens=__UpperCAmelCase , return_special_tokens_mask=__UpperCAmelCase , return_offsets_mapping=__UpperCAmelCase , return_length=__UpperCAmelCase , verbose=__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase , ) # add pixel values a = features.pop('''pixel_values''' ) if return_overflowing_tokens is True: a = self.get_overflowing_images(__UpperCAmelCase , encoded_inputs['''overflow_to_sample_mapping'''] ) a = images return encoded_inputs def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : str ) ->Optional[int]: """simple docstring""" a = [] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(__UpperCAmelCase ) != len(__UpperCAmelCase ): raise ValueError( '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got''' F""" {len(__UpperCAmelCase )} and {len(__UpperCAmelCase )}""" ) return images_with_overflow def __lowerCAmelCase ( self : Union[str, Any] , *__UpperCAmelCase : int , **__UpperCAmelCase : str ) ->Any: """simple docstring""" return self.tokenizer.batch_decode(*__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[Any] , *__UpperCAmelCase : List[Any] , **__UpperCAmelCase : List[Any] ) ->Union[str, Any]: """simple docstring""" return self.tokenizer.decode(*__UpperCAmelCase , **__UpperCAmelCase ) @property def __lowerCAmelCase ( self : List[str] ) ->Optional[int]: """simple docstring""" return ["input_ids", "bbox", "attention_mask", "pixel_values"] @property def __lowerCAmelCase ( self : Tuple ) ->Dict: """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __UpperCAmelCase , ) return self.image_processor_class @property def __lowerCAmelCase ( self : List[str] ) ->Optional[int]: """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __UpperCAmelCase , ) return self.image_processor
26
from math import ceil, sqrt def _a ( a :int = 1_000_000 ) -> int: a = 0 for outer_width in range(3 , (limit // 4) + 2 ): if outer_width**2 > limit: a = max(ceil(sqrt(outer_width**2 - limit ) ) , 1 ) else: a = 1 if (outer_width - hole_width_lower_bound) % 2: hole_width_lower_bound += 1 answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 return answer if __name__ == "__main__": print(f"""{solution() = }""")
26
1
import torch from diffusers import UnCLIPScheduler from .test_schedulers import SchedulerCommonTest class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = (UnCLIPScheduler,) def __lowerCAmelCase ( self : Optional[int] , **__UpperCAmelCase : Union[str, Any] ) ->str: """simple docstring""" a = { '''num_train_timesteps''': 1_000, '''variance_type''': '''fixed_small_log''', '''clip_sample''': True, '''clip_sample_range''': 1.0, '''prediction_type''': '''epsilon''', } config.update(**__UpperCAmelCase ) return config def __lowerCAmelCase ( self : List[Any] ) ->Dict: """simple docstring""" for timesteps in [1, 5, 100, 1_000]: self.check_over_configs(num_train_timesteps=__UpperCAmelCase ) def __lowerCAmelCase ( self : Dict ) ->Dict: """simple docstring""" for variance in ["fixed_small_log", "learned_range"]: self.check_over_configs(variance_type=__UpperCAmelCase ) def __lowerCAmelCase ( self : List[str] ) ->int: """simple docstring""" for clip_sample in [True, False]: self.check_over_configs(clip_sample=__UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[int] ) ->str: """simple docstring""" for clip_sample_range in [1, 5, 10, 20]: self.check_over_configs(clip_sample_range=__UpperCAmelCase ) def __lowerCAmelCase ( self : List[str] ) ->Union[str, Any]: """simple docstring""" for prediction_type in ["epsilon", "sample"]: self.check_over_configs(prediction_type=__UpperCAmelCase ) def __lowerCAmelCase ( self : List[Any] ) ->List[Any]: """simple docstring""" for time_step in [0, 500, 999]: for prev_timestep in [None, 5, 100, 250, 500, 750]: if prev_timestep is not None and prev_timestep >= time_step: continue self.check_over_forward(time_step=__UpperCAmelCase , prev_timestep=__UpperCAmelCase ) def __lowerCAmelCase ( self : int ) ->List[Any]: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config(variance_type='''fixed_small_log''' ) a = scheduler_class(**__UpperCAmelCase ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 1.0_0_0_0e-1_0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.0549625 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.9994987 ) ) < 1e-5 def __lowerCAmelCase ( self : str ) ->List[Any]: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config(variance_type='''learned_range''' ) a = scheduler_class(**__UpperCAmelCase ) a = 0.5 assert scheduler._get_variance(1 , predicted_variance=__UpperCAmelCase ) - -10.1712790 < 1e-5 assert scheduler._get_variance(487 , predicted_variance=__UpperCAmelCase ) - -5.7998052 < 1e-5 assert scheduler._get_variance(999 , predicted_variance=__UpperCAmelCase ) - -0.0010011 < 1e-5 def __lowerCAmelCase ( self : Optional[Any] ) ->Union[str, Any]: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config() a = scheduler_class(**__UpperCAmelCase ) a = scheduler.timesteps a = self.dummy_model() a = self.dummy_sample_deter a = torch.manual_seed(0 ) for i, t in enumerate(__UpperCAmelCase ): # 1. predict noise residual a = model(__UpperCAmelCase , __UpperCAmelCase ) # 2. predict previous mean of sample x_t-1 a = scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , generator=__UpperCAmelCase ).prev_sample a = pred_prev_sample a = torch.sum(torch.abs(__UpperCAmelCase ) ) a = torch.mean(torch.abs(__UpperCAmelCase ) ) assert abs(result_sum.item() - 252.2682495 ) < 1e-2 assert abs(result_mean.item() - 0.3284743 ) < 1e-3 def __lowerCAmelCase ( self : int ) ->str: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config() a = scheduler_class(**__UpperCAmelCase ) scheduler.set_timesteps(25 ) a = scheduler.timesteps a = self.dummy_model() a = self.dummy_sample_deter a = torch.manual_seed(0 ) for i, t in enumerate(__UpperCAmelCase ): # 1. predict noise residual a = model(__UpperCAmelCase , __UpperCAmelCase ) if i + 1 == timesteps.shape[0]: a = None else: a = timesteps[i + 1] # 2. predict previous mean of sample x_t-1 a = scheduler.step( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , prev_timestep=__UpperCAmelCase , generator=__UpperCAmelCase ).prev_sample a = pred_prev_sample a = torch.sum(torch.abs(__UpperCAmelCase ) ) a = torch.mean(torch.abs(__UpperCAmelCase ) ) assert abs(result_sum.item() - 258.2044983 ) < 1e-2 assert abs(result_mean.item() - 0.3362038 ) < 1e-3 def __lowerCAmelCase ( self : Dict ) ->List[str]: """simple docstring""" pass def __lowerCAmelCase ( self : List[str] ) ->Optional[Any]: """simple docstring""" pass
26
UpperCAmelCase__ = "0.21.0" from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
26
1
import torch def _a ( ) -> str: if torch.cuda.is_available(): a = torch.cuda.device_count() else: a = 0 print(F"""Successfully ran on {num_gpus} GPUs""" ) if __name__ == "__main__": main()
26
def _a ( a :list ) -> list: if len(a ) <= 1: return lst a = 1 while i < len(a ): if lst[i - 1] <= lst[i]: i += 1 else: a , a = lst[i], lst[i - 1] i -= 1 if i == 0: a = 1 return lst if __name__ == "__main__": UpperCAmelCase__ = input("Enter numbers separated by a comma:\n").strip() UpperCAmelCase__ = [int(item) for item in user_input.split(",")] print(gnome_sort(unsorted))
26
1
from typing import List, Optional, Union import torch from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) UpperCAmelCase__ = logging.get_logger(__name__) # pylint: disable=invalid-name UpperCAmelCase__ = "\n Examples:\n ```py\n >>> import torch\n >>> import numpy as np\n\n >>> from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline\n >>> from transformers import pipeline\n >>> from diffusers.utils import load_image\n\n\n >>> def make_hint(image, depth_estimator):\n ... image = depth_estimator(image)[\"depth\"]\n ... image = np.array(image)\n ... image = image[:, :, None]\n ... image = np.concatenate([image, image, image], axis=2)\n ... detected_map = torch.from_numpy(image).float() / 255.0\n ... hint = detected_map.permute(2, 0, 1)\n ... return hint\n\n\n >>> depth_estimator = pipeline(\"depth-estimation\")\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\n ... \"kandinsky-community/kandinsky-2-2-prior\", torch_dtype=torch.float16\n ... )\n >>> pipe_prior = pipe_prior.to(\"cuda\")\n\n >>> pipe = KandinskyV22ControlnetPipeline.from_pretrained(\n ... \"kandinsky-community/kandinsky-2-2-controlnet-depth\", torch_dtype=torch.float16\n ... )\n >>> pipe = pipe.to(\"cuda\")\n\n\n >>> img = load_image(\n ... \"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main\"\n ... \"/kandinsky/cat.png\"\n ... ).resize((768, 768))\n\n >>> hint = make_hint(img, depth_estimator).unsqueeze(0).half().to(\"cuda\")\n\n >>> prompt = \"A robot, 4k photo\"\n >>> negative_prior_prompt = \"lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature\"\n\n >>> generator = torch.Generator(device=\"cuda\").manual_seed(43)\n\n >>> image_emb, zero_image_emb = pipe_prior(\n ... prompt=prompt, negative_prompt=negative_prior_prompt, generator=generator\n ... ).to_tuple()\n\n >>> images = pipe(\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... hint=hint,\n ... num_inference_steps=50,\n ... generator=generator,\n ... height=768,\n ... width=768,\n ... ).images\n\n >>> images[0].save(\"robot_cat.png\")\n ```\n" def _a ( a :Tuple , a :str , a :List[str]=8 ) -> Any: a = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 a = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Dict , __UpperCAmelCase : UNetaDConditionModel , __UpperCAmelCase : DDPMScheduler , __UpperCAmelCase : VQModel , ) ->Optional[int]: """simple docstring""" super().__init__() self.register_modules( unet=__UpperCAmelCase , scheduler=__UpperCAmelCase , movq=__UpperCAmelCase , ) a = 2 ** (len(self.movq.config.block_out_channels ) - 1) def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : List[Any] , __UpperCAmelCase : int , __UpperCAmelCase : Dict , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : str ) ->str: """simple docstring""" if latents is None: a = randn_tensor(__UpperCAmelCase , generator=__UpperCAmelCase , device=__UpperCAmelCase , dtype=__UpperCAmelCase ) else: if latents.shape != shape: raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {shape}""" ) a = latents.to(__UpperCAmelCase ) a = latents * scheduler.init_noise_sigma return latents def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : List[str]=0 ) ->Dict: """simple docstring""" if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) a = torch.device(F"""cuda:{gpu_id}""" ) a = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : int , __UpperCAmelCase : Union[str, Any]=0 ) ->Dict: """simple docstring""" if is_accelerate_available() and is_accelerate_version('''>=''' , '''0.17.0.dev0''' ): from accelerate import cpu_offload_with_hook else: raise ImportError('''`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.''' ) a = torch.device(F"""cuda:{gpu_id}""" ) if self.device.type != "cpu": self.to('''cpu''' , silence_dtype_warnings=__UpperCAmelCase ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) a = None for cpu_offloaded_model in [self.unet, self.movq]: a , a = cpu_offload_with_hook(__UpperCAmelCase , __UpperCAmelCase , prev_module_hook=__UpperCAmelCase ) # We'll offload the last model manually. a = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def __lowerCAmelCase ( self : Optional[Any] ) ->Optional[Any]: """simple docstring""" if not hasattr(self.unet , '''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(__UpperCAmelCase , '''_hf_hook''' ) and hasattr(module._hf_hook , '''execution_device''' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(__UpperCAmelCase ) def __call__( self : List[str] , __UpperCAmelCase : Union[torch.FloatTensor, List[torch.FloatTensor]] , __UpperCAmelCase : Union[torch.FloatTensor, List[torch.FloatTensor]] , __UpperCAmelCase : torch.FloatTensor , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 100 , __UpperCAmelCase : float = 4.0 , __UpperCAmelCase : int = 1 , __UpperCAmelCase : Optional[Union[torch.Generator, List[torch.Generator]]] = None , __UpperCAmelCase : Optional[torch.FloatTensor] = None , __UpperCAmelCase : Optional[str] = "pil" , __UpperCAmelCase : bool = True , ) ->List[Any]: """simple docstring""" a = self._execution_device a = guidance_scale > 1.0 if isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = torch.cat(__UpperCAmelCase , dim=0 ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = torch.cat(__UpperCAmelCase , dim=0 ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = torch.cat(__UpperCAmelCase , dim=0 ) a = image_embeds.shape[0] * num_images_per_prompt if do_classifier_free_guidance: a = image_embeds.repeat_interleave(__UpperCAmelCase , dim=0 ) a = negative_image_embeds.repeat_interleave(__UpperCAmelCase , dim=0 ) a = hint.repeat_interleave(__UpperCAmelCase , dim=0 ) a = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=__UpperCAmelCase ) a = torch.cat([hint, hint] , dim=0 ).to(dtype=self.unet.dtype , device=__UpperCAmelCase ) self.scheduler.set_timesteps(__UpperCAmelCase , device=__UpperCAmelCase ) a = self.scheduler.timesteps a = self.movq.config.latent_channels a , a = downscale_height_and_width(__UpperCAmelCase , __UpperCAmelCase , self.movq_scale_factor ) # create initial latent a = self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , self.scheduler , ) for i, t in enumerate(self.progress_bar(__UpperCAmelCase ) ): # expand the latents if we are doing classifier free guidance a = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents a = {'''image_embeds''': image_embeds, '''hint''': hint} a = self.unet( sample=__UpperCAmelCase , timestep=__UpperCAmelCase , encoder_hidden_states=__UpperCAmelCase , added_cond_kwargs=__UpperCAmelCase , return_dict=__UpperCAmelCase , )[0] if do_classifier_free_guidance: a , a = noise_pred.split(latents.shape[1] , dim=1 ) a , a = noise_pred.chunk(2 ) a , a = variance_pred.chunk(2 ) a = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) a = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , '''variance_type''' ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): a , a = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 a = self.scheduler.step( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , generator=__UpperCAmelCase , )[0] # post-processing a = self.movq.decode(__UpperCAmelCase , force_not_quantize=__UpperCAmelCase )['''sample'''] if output_type not in ["pt", "np", "pil"]: raise ValueError(F"""Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}""" ) if output_type in ["np", "pil"]: a = image * 0.5 + 0.5 a = image.clamp(0 , 1 ) a = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": a = self.numpy_to_pil(__UpperCAmelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=__UpperCAmelCase )
26
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCAmelCase__ = { "configuration_deberta": ["DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP", "DebertaConfig", "DebertaOnnxConfig"], "tokenization_deberta": ["DebertaTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ["DebertaTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "DebertaForMaskedLM", "DebertaForQuestionAnswering", "DebertaForSequenceClassification", "DebertaForTokenClassification", "DebertaModel", "DebertaPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "TFDebertaForMaskedLM", "TFDebertaForQuestionAnswering", "TFDebertaForSequenceClassification", "TFDebertaForTokenClassification", "TFDebertaModel", "TFDebertaPreTrainedModel", ] if TYPE_CHECKING: from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig, DebertaOnnxConfig from .tokenization_deberta import DebertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_deberta_fast import DebertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deberta import ( DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, DebertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deberta import ( TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFDebertaForMaskedLM, TFDebertaForQuestionAnswering, TFDebertaForSequenceClassification, TFDebertaForTokenClassification, TFDebertaModel, TFDebertaPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
1
import inspect import tempfile import unittest from huggingface_hub import hf_hub_download from transformers import is_torch_available from transformers.testing_utils import is_flaky, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin UpperCAmelCase__ = 1E-4 if is_torch_available(): import torch from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder @require_torch class lowercase_ : '''simple docstring''' def __init__( self : str , __UpperCAmelCase : List[str] , __UpperCAmelCase : Any=16 , __UpperCAmelCase : Any=13 , __UpperCAmelCase : Any=7 , __UpperCAmelCase : Optional[Any]=14 , __UpperCAmelCase : Union[str, Any]=10 , __UpperCAmelCase : Tuple=19 , __UpperCAmelCase : Any=5 , __UpperCAmelCase : Optional[int]=4 , __UpperCAmelCase : int=True , __UpperCAmelCase : List[str]=16 , __UpperCAmelCase : Tuple=2 , __UpperCAmelCase : Any=4 , __UpperCAmelCase : Tuple=4 , __UpperCAmelCase : Any="gelu" , __UpperCAmelCase : Optional[Any]=0.1 , __UpperCAmelCase : Tuple=0.1 , __UpperCAmelCase : Optional[int]=[1, 2, 3, 4, 5] , __UpperCAmelCase : List[Any]=25 , __UpperCAmelCase : Union[str, Any]=5 , ) ->Optional[Any]: """simple docstring""" a = d_model a = parent a = batch_size a = prediction_length a = context_length a = cardinality a = num_time_features a = lags_sequence a = embedding_dimension a = is_training a = hidden_size a = num_hidden_layers a = num_attention_heads a = intermediate_size a = hidden_act a = hidden_dropout_prob a = attention_probs_dropout_prob a = context_length a = prediction_length + label_length a = label_length a = moving_average a = autocorrelation_factor def __lowerCAmelCase ( self : List[Any] ) ->Optional[Any]: """simple docstring""" return AutoformerConfig( d_model=self.d_model , 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 , prediction_length=self.prediction_length , context_length=self.context_length , label_length=self.label_length , lags_sequence=self.lags_sequence , num_time_features=self.num_time_features , num_static_categorical_features=1 , cardinality=[self.cardinality] , embedding_dimension=[self.embedding_dimension] , moving_average=self.moving_average , ) def __lowerCAmelCase ( self : str , __UpperCAmelCase : Optional[int] ) ->Dict: """simple docstring""" a = config.context_length + max(config.lags_sequence ) a = ids_tensor([self.batch_size, 1] , config.cardinality[0] ) a = floats_tensor([self.batch_size, _past_length, config.num_time_features] ) a = floats_tensor([self.batch_size, _past_length] ) a = floats_tensor([self.batch_size, _past_length] ) > 0.5 # decoder inputs a = floats_tensor([self.batch_size, config.prediction_length, config.num_time_features] ) a = floats_tensor([self.batch_size, config.prediction_length] ) a = { '''past_values''': past_values, '''static_categorical_features''': static_categorical_features, '''past_time_features''': past_time_features, '''past_observed_mask''': past_observed_mask, '''future_time_features''': future_time_features, '''future_values''': future_values, } return inputs_dict def __lowerCAmelCase ( self : int ) ->int: """simple docstring""" a = self.get_config() a = self.prepare_autoformer_inputs_dict(__UpperCAmelCase ) return config, inputs_dict def __lowerCAmelCase ( self : str ) ->List[str]: """simple docstring""" a , a = self.prepare_config_and_inputs() return config, inputs_dict def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Tuple ) ->Union[str, Any]: """simple docstring""" a = AutoformerModel(config=__UpperCAmelCase ).to(__UpperCAmelCase ).eval() a = model(**__UpperCAmelCase ) a = outputs.encoder_last_hidden_state a = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: a = model.get_encoder() encoder.save_pretrained(__UpperCAmelCase ) a = AutoformerEncoder.from_pretrained(__UpperCAmelCase ).to(__UpperCAmelCase ) a , a , a , a , a = model.create_network_inputs(**__UpperCAmelCase ) a , a = model.decomposition_layer(transformer_inputs[:, : config.context_length, ...] ) a = torch.cat( (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...]) , dim=-1 , ) a = encoder(inputs_embeds=__UpperCAmelCase )[0] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1e-3 ) a = ( torch.mean(transformer_inputs[:, : config.context_length, ...] , dim=1 ) .unsqueeze(1 ) .repeat(1 , config.prediction_length , 1 ) ) a = torch.zeros( [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]] , device=enc_input.device , ) a = torch.cat( ( torch.cat((seasonal_input[:, -config.label_length :, ...], zeros) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) a = torch.cat( ( torch.cat((trend_input[:, -config.label_length :, ...], mean) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) with tempfile.TemporaryDirectory() as tmpdirname: a = model.get_decoder() decoder.save_pretrained(__UpperCAmelCase ) a = AutoformerDecoder.from_pretrained(__UpperCAmelCase ).to(__UpperCAmelCase ) a = decoder( trend=__UpperCAmelCase , inputs_embeds=__UpperCAmelCase , encoder_hidden_states=__UpperCAmelCase , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1e-3 ) @require_torch class lowercase_ ( lowercase , lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = (AutoformerModel, AutoformerForPrediction) if is_torch_available() else () __snake_case = (AutoformerForPrediction,) if is_torch_available() else () __snake_case = {'''feature-extraction''': AutoformerModel} if is_torch_available() else {} __snake_case = False __snake_case = False __snake_case = False __snake_case = False __snake_case = False __snake_case = False def __lowerCAmelCase ( self : Optional[Any] ) ->Any: """simple docstring""" a = AutoformerModelTester(self ) a = ConfigTester(self , config_class=__UpperCAmelCase , has_text_modality=__UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] ) ->int: """simple docstring""" self.config_tester.run_common_tests() def __lowerCAmelCase ( self : Union[str, Any] ) ->Union[str, Any]: """simple docstring""" a , a = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: a = model_class(__UpperCAmelCase ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(__UpperCAmelCase ) a , a = model_class.from_pretrained(__UpperCAmelCase , output_loading_info=__UpperCAmelCase ) self.assertEqual(info['''missing_keys'''] , [] ) def __lowerCAmelCase ( self : int ) ->List[Any]: """simple docstring""" a = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*__UpperCAmelCase ) @unittest.skip(reason='''Model has no tokens embeddings''' ) def __lowerCAmelCase ( self : Any ) ->Dict: """simple docstring""" pass def __lowerCAmelCase ( self : Optional[int] ) ->Dict: """simple docstring""" a = inspect.signature(getattr(__UpperCAmelCase , '''forward''' ) ) # The main input is the name of the argument after `self` a = list(model_signature.parameters.keys() )[1] self.assertEqual(AutoformerModel.main_input_name , __UpperCAmelCase ) def __lowerCAmelCase ( self : Dict ) ->int: """simple docstring""" a , a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a = model_class(__UpperCAmelCase ) a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a = [*signature.parameters.keys()] a = [ '''past_values''', '''past_time_features''', '''past_observed_mask''', '''static_categorical_features''', '''static_real_features''', '''future_values''', '''future_time_features''', ] if model.__class__.__name__ in ["AutoformerForPrediction"]: expected_arg_names.append('''future_observed_mask''' ) expected_arg_names.extend( [ '''decoder_attention_mask''', '''head_mask''', '''decoder_head_mask''', '''cross_attn_head_mask''', '''encoder_outputs''', '''past_key_values''', '''output_hidden_states''', '''output_attentions''', '''use_cache''', '''return_dict''', ] ) self.assertListEqual(arg_names[: len(__UpperCAmelCase )] , __UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[int] ) ->Tuple: """simple docstring""" a , a = self.model_tester.prepare_config_and_inputs_for_common() a = True a = getattr(self.model_tester , '''seq_length''' , __UpperCAmelCase ) a = getattr(self.model_tester , '''decoder_seq_length''' , __UpperCAmelCase ) a = getattr(self.model_tester , '''encoder_seq_length''' , __UpperCAmelCase ) a = getattr(self.model_tester , '''d_model''' , __UpperCAmelCase ) a = getattr(self.model_tester , '''num_attention_heads''' , __UpperCAmelCase ) a = d_model // num_attention_heads for model_class in self.all_model_classes: a = True a = False a = True a = model_class(__UpperCAmelCase ) model.to(__UpperCAmelCase ) model.eval() with torch.no_grad(): a = model(**self._prepare_for_class(__UpperCAmelCase , __UpperCAmelCase ) ) a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(__UpperCAmelCase ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] a = True a = model_class(__UpperCAmelCase ) model.to(__UpperCAmelCase ) model.eval() with torch.no_grad(): a = model(**self._prepare_for_class(__UpperCAmelCase , __UpperCAmelCase ) ) a = outputs.encoder_attentions self.assertEqual(len(__UpperCAmelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) a = len(__UpperCAmelCase ) a = 7 if "last_hidden_state" in outputs: correct_outlen += 1 if "trend" in outputs: correct_outlen += 1 if "past_key_values" in outputs: correct_outlen += 1 # past_key_values have been returned if "loss" in outputs: correct_outlen += 1 if "params" in outputs: correct_outlen += 1 self.assertEqual(__UpperCAmelCase , __UpperCAmelCase ) # decoder attentions a = outputs.decoder_attentions self.assertIsInstance(__UpperCAmelCase , (list, tuple) ) self.assertEqual(len(__UpperCAmelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # cross attentions a = outputs.cross_attentions self.assertIsInstance(__UpperCAmelCase , (list, tuple) ) self.assertEqual(len(__UpperCAmelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(cross_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # Check attention is always last and order is fine a = True a = True a = model_class(__UpperCAmelCase ) model.to(__UpperCAmelCase ) model.eval() with torch.no_grad(): a = model(**self._prepare_for_class(__UpperCAmelCase , __UpperCAmelCase ) ) self.assertEqual(out_len + 2 , len(__UpperCAmelCase ) ) a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(__UpperCAmelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) @is_flaky() def __lowerCAmelCase ( self : Any ) ->List[str]: """simple docstring""" super().test_retain_grad_hidden_states_attentions() def _a ( a :List[str]="train-batch.pt" ) -> List[Any]: a = hf_hub_download(repo_id='''hf-internal-testing/tourism-monthly-batch''' , filename=a , repo_type='''dataset''' ) a = torch.load(a , map_location=a ) return batch @require_torch @slow class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self : Any ) ->Any: """simple docstring""" a = AutoformerModel.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(__UpperCAmelCase ) a = prepare_batch() with torch.no_grad(): a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , future_values=batch['''future_values'''] , future_time_features=batch['''future_time_features'''] , )[0] a = torch.Size( (64, model.config.prediction_length + model.config.label_length, model.config.feature_size) ) self.assertEqual(output.shape , __UpperCAmelCase ) a = torch.tensor( [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]] , device=__UpperCAmelCase ) self.assertTrue(torch.allclose(output[0, :3, :3] , __UpperCAmelCase , atol=__UpperCAmelCase ) ) def __lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(__UpperCAmelCase ) a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , ).encoder_last_hidden_state a = torch.Size((64, model.config.context_length, model.config.d_model) ) self.assertEqual(output.shape , __UpperCAmelCase ) a = torch.tensor( [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]] , device=__UpperCAmelCase ) self.assertTrue(torch.allclose(output[0, :3, :3] , __UpperCAmelCase , atol=__UpperCAmelCase ) ) def __lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(__UpperCAmelCase ) a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): a = model.generate( static_categorical_features=batch['''static_categorical_features'''] , past_time_features=batch['''past_time_features'''] , past_values=batch['''past_values'''] , future_time_features=batch['''future_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , ) a = torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length) ) self.assertEqual(outputs.sequences.shape , __UpperCAmelCase ) a = torch.tensor([3130.6763, 4056.5293, 7053.0786] , device=__UpperCAmelCase ) a = outputs.sequences.mean(dim=1 ) self.assertTrue(torch.allclose(mean_prediction[0, -3:] , __UpperCAmelCase , rtol=1e-1 ) )
26
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all feature extractors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...feature_extraction_utils import FeatureExtractionMixin from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_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, ) UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = OrderedDict( [ ("audio-spectrogram-transformer", "ASTFeatureExtractor"), ("beit", "BeitFeatureExtractor"), ("chinese_clip", "ChineseCLIPFeatureExtractor"), ("clap", "ClapFeatureExtractor"), ("clip", "CLIPFeatureExtractor"), ("clipseg", "ViTFeatureExtractor"), ("conditional_detr", "ConditionalDetrFeatureExtractor"), ("convnext", "ConvNextFeatureExtractor"), ("cvt", "ConvNextFeatureExtractor"), ("data2vec-audio", "Wav2Vec2FeatureExtractor"), ("data2vec-vision", "BeitFeatureExtractor"), ("deformable_detr", "DeformableDetrFeatureExtractor"), ("deit", "DeiTFeatureExtractor"), ("detr", "DetrFeatureExtractor"), ("dinat", "ViTFeatureExtractor"), ("donut-swin", "DonutFeatureExtractor"), ("dpt", "DPTFeatureExtractor"), ("encodec", "EncodecFeatureExtractor"), ("flava", "FlavaFeatureExtractor"), ("glpn", "GLPNFeatureExtractor"), ("groupvit", "CLIPFeatureExtractor"), ("hubert", "Wav2Vec2FeatureExtractor"), ("imagegpt", "ImageGPTFeatureExtractor"), ("layoutlmv2", "LayoutLMv2FeatureExtractor"), ("layoutlmv3", "LayoutLMv3FeatureExtractor"), ("levit", "LevitFeatureExtractor"), ("maskformer", "MaskFormerFeatureExtractor"), ("mctct", "MCTCTFeatureExtractor"), ("mobilenet_v1", "MobileNetV1FeatureExtractor"), ("mobilenet_v2", "MobileNetV2FeatureExtractor"), ("mobilevit", "MobileViTFeatureExtractor"), ("nat", "ViTFeatureExtractor"), ("owlvit", "OwlViTFeatureExtractor"), ("perceiver", "PerceiverFeatureExtractor"), ("poolformer", "PoolFormerFeatureExtractor"), ("regnet", "ConvNextFeatureExtractor"), ("resnet", "ConvNextFeatureExtractor"), ("segformer", "SegformerFeatureExtractor"), ("sew", "Wav2Vec2FeatureExtractor"), ("sew-d", "Wav2Vec2FeatureExtractor"), ("speech_to_text", "Speech2TextFeatureExtractor"), ("speecht5", "SpeechT5FeatureExtractor"), ("swiftformer", "ViTFeatureExtractor"), ("swin", "ViTFeatureExtractor"), ("swinv2", "ViTFeatureExtractor"), ("table-transformer", "DetrFeatureExtractor"), ("timesformer", "VideoMAEFeatureExtractor"), ("tvlt", "TvltFeatureExtractor"), ("unispeech", "Wav2Vec2FeatureExtractor"), ("unispeech-sat", "Wav2Vec2FeatureExtractor"), ("van", "ConvNextFeatureExtractor"), ("videomae", "VideoMAEFeatureExtractor"), ("vilt", "ViltFeatureExtractor"), ("vit", "ViTFeatureExtractor"), ("vit_mae", "ViTFeatureExtractor"), ("vit_msn", "ViTFeatureExtractor"), ("wav2vec2", "Wav2Vec2FeatureExtractor"), ("wav2vec2-conformer", "Wav2Vec2FeatureExtractor"), ("wavlm", "Wav2Vec2FeatureExtractor"), ("whisper", "WhisperFeatureExtractor"), ("xclip", "CLIPFeatureExtractor"), ("yolos", "YolosFeatureExtractor"), ] ) UpperCAmelCase__ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES) def _a ( a :str ) -> Any: for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items(): if class_name in extractors: a = model_type_to_module_name(a ) a = importlib.import_module(F""".{module_name}""" , '''transformers.models''' ) try: return getattr(a , a ) except AttributeError: continue for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items(): if getattr(a , '''__name__''' , a ) == 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. a = importlib.import_module('''transformers''' ) if hasattr(a , a ): return getattr(a , a ) return None def _a ( a :Union[str, os.PathLike] , a :Optional[Union[str, os.PathLike]] = None , a :bool = False , a :bool = False , a :Optional[Dict[str, str]] = None , a :Optional[Union[bool, str]] = None , a :Optional[str] = None , a :bool = False , **a :int , ) -> Tuple: a = get_file_from_repo( a , a , cache_dir=a , force_download=a , resume_download=a , proxies=a , use_auth_token=a , revision=a , local_files_only=a , ) if resolved_config_file is None: logger.info( '''Could not locate the feature extractor configuration file, will try to use the model config instead.''' ) return {} with open(a , encoding='''utf-8''' ) as reader: return json.load(a ) class lowercase_ : '''simple docstring''' def __init__( self : Tuple ) ->int: """simple docstring""" raise EnvironmentError( '''AutoFeatureExtractor is designed to be instantiated ''' '''using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.''' ) @classmethod @replace_list_option_in_docstrings(__UpperCAmelCase ) def __lowerCAmelCase ( cls : int , __UpperCAmelCase : Optional[Any] , **__UpperCAmelCase : Dict ) ->List[Any]: """simple docstring""" a = kwargs.pop('''config''' , __UpperCAmelCase ) a = kwargs.pop('''trust_remote_code''' , __UpperCAmelCase ) a = True a , a = FeatureExtractionMixin.get_feature_extractor_dict(__UpperCAmelCase , **__UpperCAmelCase ) a = config_dict.get('''feature_extractor_type''' , __UpperCAmelCase ) a = None if "AutoFeatureExtractor" in config_dict.get('''auto_map''' , {} ): a = config_dict['''auto_map''']['''AutoFeatureExtractor'''] # If we don't find the feature extractor class in the feature extractor config, let's try the model config. if feature_extractor_class is None and feature_extractor_auto_map is None: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = AutoConfig.from_pretrained(__UpperCAmelCase , **__UpperCAmelCase ) # It could be in `config.feature_extractor_type`` a = getattr(__UpperCAmelCase , '''feature_extractor_type''' , __UpperCAmelCase ) if hasattr(__UpperCAmelCase , '''auto_map''' ) and "AutoFeatureExtractor" in config.auto_map: a = config.auto_map['''AutoFeatureExtractor'''] if feature_extractor_class is not None: a = feature_extractor_class_from_name(__UpperCAmelCase ) a = feature_extractor_auto_map is not None a = feature_extractor_class is not None or type(__UpperCAmelCase ) in FEATURE_EXTRACTOR_MAPPING a = resolve_trust_remote_code( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) if has_remote_code and trust_remote_code: a = get_class_from_dynamic_module( __UpperCAmelCase , __UpperCAmelCase , **__UpperCAmelCase ) a = kwargs.pop('''code_revision''' , __UpperCAmelCase ) if os.path.isdir(__UpperCAmelCase ): feature_extractor_class.register_for_auto_class() return feature_extractor_class.from_dict(__UpperCAmelCase , **__UpperCAmelCase ) elif feature_extractor_class is not None: return feature_extractor_class.from_dict(__UpperCAmelCase , **__UpperCAmelCase ) # Last try: we use the FEATURE_EXTRACTOR_MAPPING. elif type(__UpperCAmelCase ) in FEATURE_EXTRACTOR_MAPPING: a = FEATURE_EXTRACTOR_MAPPING[type(__UpperCAmelCase )] return feature_extractor_class.from_dict(__UpperCAmelCase , **__UpperCAmelCase ) raise ValueError( F"""Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a """ F"""`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following """ F"""`model_type` keys in its {CONFIG_NAME}: {', '.join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys() )}""" ) @staticmethod def __lowerCAmelCase ( __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Tuple ) ->Optional[int]: """simple docstring""" FEATURE_EXTRACTOR_MAPPING.register(__UpperCAmelCase , __UpperCAmelCase )
26
1
from math import atan, cos, radians, sin, tan from .haversine_distance import haversine_distance UpperCAmelCase__ = 637_8137.0 UpperCAmelCase__ = 635_6752.31_4245 UpperCAmelCase__ = 6378137 def _a ( a :float , a :float , a :float , a :float ) -> float: a = (AXIS_A - AXIS_B) / AXIS_A # Parametric latitudes # https://en.wikipedia.org/wiki/Latitude#Parametric_(or_reduced)_latitude a = atan((1 - flattening) * tan(radians(a ) ) ) a = atan((1 - flattening) * tan(radians(a ) ) ) # Compute central angle between two points # using haversine theta. sigma = haversine_distance / equatorial radius a = haversine_distance(a , a , a , a ) / EQUATORIAL_RADIUS # Intermediate P and Q values a = (b_lata + b_lata) / 2 a = (b_lata - b_lata) / 2 # Intermediate X value # X = (sigma - sin(sigma)) * sin^2Pcos^2Q / cos^2(sigma/2) a = (sin(a ) ** 2) * (cos(a ) ** 2) a = cos(sigma / 2 ) ** 2 a = (sigma - sin(a )) * (x_numerator / x_demonimator) # Intermediate Y value # Y = (sigma + sin(sigma)) * cos^2Psin^2Q / sin^2(sigma/2) a = (cos(a ) ** 2) * (sin(a ) ** 2) a = sin(sigma / 2 ) ** 2 a = (sigma + sin(a )) * (y_numerator / y_denominator) return EQUATORIAL_RADIUS * (sigma - ((flattening / 2) * (x_value + y_value))) if __name__ == "__main__": import doctest doctest.testmod()
26
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import ( AutoProcessor, BertTokenizerFast, BlipImageProcessor, GPTaTokenizer, InstructBlipProcessor, PreTrainedTokenizerFast, ) @require_vision class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self : Optional[int] ) ->Tuple: """simple docstring""" a = tempfile.mkdtemp() a = BlipImageProcessor() a = GPTaTokenizer.from_pretrained('''hf-internal-testing/tiny-random-GPT2Model''' ) a = BertTokenizerFast.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) a = InstructBlipProcessor(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) processor.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self : Optional[Any] , **__UpperCAmelCase : Tuple ) ->List[str]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).tokenizer def __lowerCAmelCase ( self : int , **__UpperCAmelCase : str ) ->List[str]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).image_processor def __lowerCAmelCase ( self : Optional[Any] , **__UpperCAmelCase : Any ) ->Optional[Any]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).qformer_tokenizer def __lowerCAmelCase ( self : str ) ->Tuple: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __lowerCAmelCase ( self : Optional[int] ) ->str: """simple docstring""" a = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] a = [Image.fromarray(np.moveaxis(__UpperCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def __lowerCAmelCase ( self : Optional[Any] ) ->List[str]: """simple docstring""" a = InstructBlipProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() , qformer_tokenizer=self.get_qformer_tokenizer() , ) processor.save_pretrained(self.tmpdirname ) a = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) a = self.get_image_processor(do_normalize=__UpperCAmelCase , padding_value=1.0 ) a = InstructBlipProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=__UpperCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __UpperCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __UpperCAmelCase ) self.assertIsInstance(processor.qformer_tokenizer , __UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[Any] ) ->Any: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = self.prepare_image_inputs() a = image_processor(__UpperCAmelCase , return_tensors='''np''' ) a = processor(images=__UpperCAmelCase , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowerCAmelCase ( self : List[str] ) ->List[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = '''lower newer''' a = processor(text=__UpperCAmelCase ) a = tokenizer(__UpperCAmelCase , return_token_type_ids=__UpperCAmelCase ) a = qformer_tokenizer(__UpperCAmelCase , return_token_type_ids=__UpperCAmelCase ) for key in encoded_tokens.keys(): self.assertListEqual(encoded_tokens[key] , encoded_processor[key] ) for key in encoded_tokens_qformer.keys(): self.assertListEqual(encoded_tokens_qformer[key] , encoded_processor['''qformer_''' + key] ) def __lowerCAmelCase ( self : Dict ) ->Optional[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = '''lower newer''' a = self.prepare_image_inputs() a = processor(text=__UpperCAmelCase , images=__UpperCAmelCase ) self.assertListEqual( list(inputs.keys() ) , ['''input_ids''', '''attention_mask''', '''qformer_input_ids''', '''qformer_attention_mask''', '''pixel_values'''] , ) # test if it raises when no input is passed with pytest.raises(__UpperCAmelCase ): processor() def __lowerCAmelCase ( self : Dict ) ->List[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] a = processor.batch_decode(__UpperCAmelCase ) a = tokenizer.batch_decode(__UpperCAmelCase ) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] ) ->str: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = '''lower newer''' a = self.prepare_image_inputs() a = processor(text=__UpperCAmelCase , images=__UpperCAmelCase ) self.assertListEqual( list(inputs.keys() ) , ['''input_ids''', '''attention_mask''', '''qformer_input_ids''', '''qformer_attention_mask''', '''pixel_values'''] , )
26
1
import unittest from huggingface_hub import hf_hub_download from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEFeatureExtractor from transformers.pipelines import VideoClassificationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_decord, require_tf, require_torch, require_torch_or_tf, require_vision, ) from .test_pipelines_common import ANY @is_pipeline_test @require_torch_or_tf @require_vision @require_decord class lowercase_ ( unittest.TestCase ): '''simple docstring''' __snake_case = MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING def __lowerCAmelCase ( self : str , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Dict , __UpperCAmelCase : Optional[int] ) ->Union[str, Any]: """simple docstring""" a = hf_hub_download( repo_id='''nateraw/video-demo''' , filename='''archery.mp4''' , repo_type='''dataset''' ) a = VideoClassificationPipeline(model=__UpperCAmelCase , image_processor=__UpperCAmelCase , top_k=2 ) a = [ example_video_filepath, '''https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4''', ] return video_classifier, examples def __lowerCAmelCase ( self : int , __UpperCAmelCase : List[Any] , __UpperCAmelCase : Dict ) ->int: """simple docstring""" for example in examples: a = video_classifier(__UpperCAmelCase ) self.assertEqual( __UpperCAmelCase , [ {'''score''': ANY(__UpperCAmelCase ), '''label''': ANY(__UpperCAmelCase )}, {'''score''': ANY(__UpperCAmelCase ), '''label''': ANY(__UpperCAmelCase )}, ] , ) @require_torch def __lowerCAmelCase ( self : str ) ->Dict: """simple docstring""" a = '''hf-internal-testing/tiny-random-VideoMAEForVideoClassification''' a = VideoMAEFeatureExtractor( size={'''shortest_edge''': 10} , crop_size={'''height''': 10, '''width''': 10} ) a = pipeline( '''video-classification''' , model=__UpperCAmelCase , feature_extractor=__UpperCAmelCase , frame_sampling_rate=4 ) a = hf_hub_download(repo_id='''nateraw/video-demo''' , filename='''archery.mp4''' , repo_type='''dataset''' ) a = video_classifier(__UpperCAmelCase , top_k=2 ) self.assertEqual( nested_simplify(__UpperCAmelCase , decimals=4 ) , [{'''score''': 0.5199, '''label''': '''LABEL_0'''}, {'''score''': 0.4801, '''label''': '''LABEL_1'''}] , ) a = video_classifier( [ video_file_path, video_file_path, ] , top_k=2 , ) self.assertEqual( nested_simplify(__UpperCAmelCase , decimals=4 ) , [ [{'''score''': 0.5199, '''label''': '''LABEL_0'''}, {'''score''': 0.4801, '''label''': '''LABEL_1'''}], [{'''score''': 0.5199, '''label''': '''LABEL_0'''}, {'''score''': 0.4801, '''label''': '''LABEL_1'''}], ] , ) @require_tf def __lowerCAmelCase ( self : Any ) ->str: """simple docstring""" pass
26
import math def _a ( a :int = 100 ) -> int: a = sum(i * i for i in range(1 , n + 1 ) ) a = int(math.pow(sum(range(1 , n + 1 ) ) , 2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(f"""{solution() = }""")
26
1
import argparse import torch from torch import nn from transformers import MBartConfig, MBartForConditionalGeneration def _a ( a :List[Any] ) -> int: a = [ '''encoder.version''', '''decoder.version''', '''model.encoder.version''', '''model.decoder.version''', '''_float_tensor''', '''decoder.output_projection.weight''', ] for k in ignore_keys: state_dict.pop(a , a ) def _a ( a :List[str] ) -> Tuple: a , a = emb.weight.shape a = nn.Linear(a , a , bias=a ) a = emb.weight.data return lin_layer def _a ( a :Optional[int] , a :Optional[int]="facebook/mbart-large-en-ro" , a :str=False , a :str=False ) -> List[Any]: a = torch.load(a , map_location='''cpu''' )['''model'''] remove_ignore_keys_(a ) a = state_dict['''encoder.embed_tokens.weight'''].shape[0] a = MBartConfig.from_pretrained(a , vocab_size=a ) if mbart_aa and finetuned: a = '''relu''' a = state_dict['''decoder.embed_tokens.weight'''] a = MBartForConditionalGeneration(a ) model.model.load_state_dict(a ) if finetuned: a = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( "fairseq_path", type=str, help="bart.large, bart.large.cnn or a path to a model.pt on local filesystem." ) parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument( "--hf_config", default="facebook/mbart-large-cc25", type=str, help="Which huggingface architecture to use: mbart-large", ) parser.add_argument("--mbart_50", action="store_true", help="whether the model is mMART-50 checkpoint") parser.add_argument("--finetuned", action="store_true", help="whether the model is a fine-tuned checkpoint") UpperCAmelCase__ = parser.parse_args() UpperCAmelCase__ = convert_fairseq_mbart_checkpoint_from_disk( args.fairseq_path, hf_config_path=args.hf_config, finetuned=args.finetuned, mbart_aa=args.mbart_aa ) model.save_pretrained(args.pytorch_dump_folder_path)
26
def _a ( a :int = 600_851_475_143 ) -> int: try: a = int(a ) 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.''' ) a = 2 a = 0 if n == 2: return 2 while n > 2: while n % i != 0: i += 1 a = i while n % i == 0: a = n // i i += 1 return int(a ) if __name__ == "__main__": print(f"""{solution() = }""")
26
1
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..bit import BitConfig UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { "Intel/dpt-large": "https://huggingface.co/Intel/dpt-large/resolve/main/config.json", # See all DPT models at https://huggingface.co/models?filter=dpt } class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = '''dpt''' def __init__( self : List[str] , __UpperCAmelCase : Optional[Any]=768 , __UpperCAmelCase : Tuple=12 , __UpperCAmelCase : Any=12 , __UpperCAmelCase : Union[str, Any]=3_072 , __UpperCAmelCase : int="gelu" , __UpperCAmelCase : Union[str, Any]=0.0 , __UpperCAmelCase : Optional[int]=0.0 , __UpperCAmelCase : Optional[int]=0.02 , __UpperCAmelCase : Dict=1e-1_2 , __UpperCAmelCase : int=384 , __UpperCAmelCase : Union[str, Any]=16 , __UpperCAmelCase : Dict=3 , __UpperCAmelCase : List[str]=False , __UpperCAmelCase : List[Any]=True , __UpperCAmelCase : List[str]=[2, 5, 8, 11] , __UpperCAmelCase : List[Any]="project" , __UpperCAmelCase : Optional[int]=[4, 2, 1, 0.5] , __UpperCAmelCase : Union[str, Any]=[96, 192, 384, 768] , __UpperCAmelCase : List[Any]=256 , __UpperCAmelCase : Dict=-1 , __UpperCAmelCase : str=False , __UpperCAmelCase : Tuple=True , __UpperCAmelCase : int=0.4 , __UpperCAmelCase : Union[str, Any]=255 , __UpperCAmelCase : Dict=0.1 , __UpperCAmelCase : Optional[int]=[1, 1_024, 24, 24] , __UpperCAmelCase : Dict=[0, 1] , __UpperCAmelCase : Optional[Any]=None , **__UpperCAmelCase : List[str] , ) ->Tuple: """simple docstring""" super().__init__(**__UpperCAmelCase ) a = hidden_size a = is_hybrid if self.is_hybrid: if backbone_config is None: logger.info('''Initializing the config with a `BiT` backbone.''' ) a = { '''global_padding''': '''same''', '''layer_type''': '''bottleneck''', '''depths''': [3, 4, 9], '''out_features''': ['''stage1''', '''stage2''', '''stage3'''], '''embedding_dynamic_padding''': True, } a = BitConfig(**__UpperCAmelCase ) elif isinstance(__UpperCAmelCase , __UpperCAmelCase ): logger.info('''Initializing the config with a `BiT` backbone.''' ) a = BitConfig(**__UpperCAmelCase ) elif isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = backbone_config else: raise ValueError( F"""backbone_config must be a dictionary or a `PretrainedConfig`, got {backbone_config.__class__}.""" ) a = backbone_featmap_shape a = neck_ignore_stages if readout_type != "project": raise ValueError('''Readout type must be \'project\' when using `DPT-hybrid` mode.''' ) else: a = None a = None a = [] a = num_hidden_layers a = num_attention_heads a = intermediate_size a = hidden_act a = hidden_dropout_prob a = attention_probs_dropout_prob a = initializer_range a = layer_norm_eps a = image_size a = patch_size a = num_channels a = qkv_bias a = backbone_out_indices if readout_type not in ["ignore", "add", "project"]: raise ValueError('''Readout_type must be one of [\'ignore\', \'add\', \'project\']''' ) a = readout_type a = reassemble_factors a = neck_hidden_sizes a = fusion_hidden_size a = head_in_index a = use_batch_norm_in_fusion_residual # auxiliary head attributes (semantic segmentation) a = use_auxiliary_head a = auxiliary_loss_weight a = semantic_loss_ignore_index a = semantic_classifier_dropout def __lowerCAmelCase ( self : Any ) ->Any: """simple docstring""" a = copy.deepcopy(self.__dict__ ) if output["backbone_config"] is not None: a = self.backbone_config.to_dict() a = self.__class__.model_type return output
26
import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer UpperCAmelCase__ = "bart" UpperCAmelCase__ = True @st.cache(allow_output_mutation=a ) def _a ( ) -> Tuple: if LOAD_DENSE_INDEX: a = AutoTokenizer.from_pretrained('''yjernite/retribert-base-uncased''' ) a = AutoModel.from_pretrained('''yjernite/retribert-base-uncased''' ).to('''cuda:0''' ) a = qar_model.eval() else: a , a = (None, None) if MODEL_TYPE == "bart": a = AutoTokenizer.from_pretrained('''yjernite/bart_eli5''' ) a = AutoModelForSeqaSeqLM.from_pretrained('''yjernite/bart_eli5''' ).to('''cuda:0''' ) a = torch.load('''seq2seq_models/eli5_bart_model_blm_2.pth''' ) sas_model.load_state_dict(save_dict['''model'''] ) a = sas_model.eval() else: a , a = make_qa_sas_model( model_name='''t5-small''' , from_file='''seq2seq_models/eli5_t5_model_1024_4.pth''' , device='''cuda:0''' ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=a ) def _a ( ) -> Dict: if LOAD_DENSE_INDEX: a = faiss.StandardGpuResources() a = datasets.load_dataset(path='''wiki_snippets''' , name='''wiki40b_en_100_0''' )['''train'''] a = np.memmap( '''wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat''' , dtype='''float32''' , mode='''r''' , shape=(wikiaab_passages.num_rows, 128) , ) a = faiss.IndexFlatIP(128 ) a = faiss.index_cpu_to_gpu(a , 1 , a ) wikiaab_gpu_index_flat.add(a ) # TODO fix for larger GPU else: a , a = (None, None) a = Elasticsearch([{'''host''': '''localhost''', '''port''': '''9200'''}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=a ) def _a ( ) -> Optional[int]: a = datasets.load_dataset('''eli5''' , name='''LFQA_reddit''' ) a = elia['''train_eli5'''] a = np.memmap( '''eli5_questions_reps.dat''' , dtype='''float32''' , mode='''r''' , shape=(elia_train.num_rows, 128) ) a = faiss.IndexFlatIP(128 ) eli5_train_q_index.add(a ) return (elia_train, eli5_train_q_index) UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = load_indexes() UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = load_models() UpperCAmelCase__ , UpperCAmelCase__ = load_train_data() def _a ( a :str , a :Tuple=10 ) -> List[str]: a = embed_questions_for_retrieval([question] , a , a ) a , a = eli5_train_q_index.search(a , a ) a = [elia_train[int(a )] for i in I[0]] return nn_examples def _a ( a :str , a :Any="wiki40b" , a :int="dense" , a :Union[str, Any]=10 ) -> List[str]: if source == "none": a , a = (''' <P> '''.join(['''''' for _ in range(11 )] ).strip(), []) else: if method == "dense": a , a = query_qa_dense_index( a , a , a , a , a , a ) else: a , a = query_es_index( a , a , index_name='''english_wiki40b_snippets_100w''' , n_results=a , ) a = [ (res['''article_title'''], res['''section_title'''].strip(), res['''score'''], res['''passage_text''']) for res in hit_lst ] a = '''question: {} context: {}'''.format(a , a ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda a : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda a : None), } ) def _a ( a :Tuple , a :int , a :int , a :Dict=64 , a :List[Any]=256 , a :List[Any]=False , a :List[Any]=2 , a :Tuple=0.95 , a :Optional[Any]=0.8 ) -> int: with torch.no_grad(): a = qa_sas_generate( a , a , a , num_answers=1 , num_beams=a , min_len=a , max_len=a , do_sample=a , temp=a , top_p=a , top_k=a , max_input_length=1_024 , device='''cuda:0''' , )[0] return (answer, support_list) st.title("Long Form Question Answering with ELI5") # Start sidebar UpperCAmelCase__ = "<img src='https://huggingface.co/front/assets/huggingface_logo.svg'>" UpperCAmelCase__ = "\n<html>\n <head>\n <style>\n .img-container {\n padding-left: 90px;\n padding-right: 90px;\n padding-top: 50px;\n padding-bottom: 50px;\n background-color: #f0f3f9;\n }\n </style>\n </head>\n <body>\n <span class=\"img-container\"> <!-- Inline parent element -->\n %s\n </span>\n </body>\n</html>\n" % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia UpperCAmelCase__ = "\nThis demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html).\nFirst, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset,\na pre-processed fixed snapshot of Wikipedia.\n" st.sidebar.markdown(description, unsafe_allow_html=True) UpperCAmelCase__ = [ "Answer the question", "View the retrieved document only", "View the most similar ELI5 question and answer", "Show me everything, please!", ] UpperCAmelCase__ = st.sidebar.checkbox("Demo options") if demo_options: UpperCAmelCase__ = st.sidebar.selectbox( "", action_list, index=3, ) UpperCAmelCase__ = action_list.index(action_st) UpperCAmelCase__ = st.sidebar.selectbox( "", ["Show full text of passages", "Show passage section titles"], index=0, ) UpperCAmelCase__ = show_type == "Show full text of passages" else: UpperCAmelCase__ = 3 UpperCAmelCase__ = True UpperCAmelCase__ = st.sidebar.checkbox("Retrieval options") if retrieval_options: UpperCAmelCase__ = "\n ### Information retriever options\n\n The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding\n trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs.\n The answer is then generated by sequence to sequence model which takes the question and retrieved document as input.\n " st.sidebar.markdown(retriever_info) UpperCAmelCase__ = st.sidebar.selectbox("Which Wikipedia format should the model use?", ["wiki40b", "none"]) UpperCAmelCase__ = st.sidebar.selectbox("Which Wikipedia indexer should the model use?", ["dense", "sparse", "mixed"]) else: UpperCAmelCase__ = "wiki40b" UpperCAmelCase__ = "dense" UpperCAmelCase__ = "beam" UpperCAmelCase__ = 2 UpperCAmelCase__ = 64 UpperCAmelCase__ = 256 UpperCAmelCase__ = None UpperCAmelCase__ = None UpperCAmelCase__ = st.sidebar.checkbox("Generation options") if generate_options: UpperCAmelCase__ = "\n ### Answer generation options\n\n The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large)\n weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with\n **beam** search, or **sample** from the decoder's output probabilities.\n " st.sidebar.markdown(generate_info) UpperCAmelCase__ = st.sidebar.selectbox("Would you like to use beam search or sample an answer?", ["beam", "sampled"]) UpperCAmelCase__ = st.sidebar.slider( "Minimum generation length", min_value=8, max_value=256, value=64, step=8, format=None, key=None ) UpperCAmelCase__ = st.sidebar.slider( "Maximum generation length", min_value=64, max_value=512, value=256, step=16, format=None, key=None ) if sampled == "beam": UpperCAmelCase__ = st.sidebar.slider("Beam size", min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: UpperCAmelCase__ = st.sidebar.slider( "Nucleus sampling p", min_value=0.1, max_value=1.0, value=0.95, step=0.01, format=None, key=None ) UpperCAmelCase__ = st.sidebar.slider( "Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.01, format=None, key=None ) UpperCAmelCase__ = None # start main text UpperCAmelCase__ = [ "<MY QUESTION>", "How do people make chocolate?", "Why do we get a fever when we are sick?", "How can different animals perceive different colors?", "What is natural language processing?", "What's the best way to treat a sunburn?", "What exactly are vitamins ?", "How does nuclear energy provide electricity?", "What's the difference between viruses and bacteria?", "Why are flutes classified as woodwinds when most of them are made out of metal ?", "Why do people like drinking coffee even though it tastes so bad?", "What happens when wine ages? How does it make the wine taste better?", "If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?", "How can we set a date to the beginning or end of an artistic period? Doesn't the change happen gradually?", "How does New Zealand have so many large bird predators?", ] UpperCAmelCase__ = st.selectbox( "What would you like to ask? ---- select <MY QUESTION> to enter a new query", questions_list, index=1, ) if question_s == "<MY QUESTION>": UpperCAmelCase__ = st.text_input("Enter your question here:", "") else: UpperCAmelCase__ = question_s if st.button("Show me!"): if action in [0, 1, 3]: if index_type == "mixed": UpperCAmelCase__ , UpperCAmelCase__ = make_support(question, source=wiki_source, method="dense", n_results=10) UpperCAmelCase__ , UpperCAmelCase__ = make_support(question, source=wiki_source, method="sparse", n_results=10) UpperCAmelCase__ = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] UpperCAmelCase__ = support_list[:10] UpperCAmelCase__ = "<P> " + " <P> ".join([res[-1] for res in support_list]) else: UpperCAmelCase__ , UpperCAmelCase__ = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: UpperCAmelCase__ , UpperCAmelCase__ = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == "sampled"), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown("### The model generated answer is:") st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown("--- \n ### The model is drawing information from the following Wikipedia passages:") for i, res in enumerate(support_list): UpperCAmelCase__ = "https://en.wikipedia.org/wiki/{}".format(res[0].replace(" ", "_")) UpperCAmelCase__ = res[1].strip() if sec_titles == "": UpperCAmelCase__ = "[{}]({})".format(res[0], wiki_url) else: UpperCAmelCase__ = sec_titles.split(" & ") UpperCAmelCase__ = " & ".join( ["[{}]({}#{})".format(sec.strip(), wiki_url, sec.strip().replace(" ", "_")) for sec in sec_list] ) st.markdown( "{0:02d} - **Article**: {1:<18} <br> _Section_: {2}".format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( "> <span style=\"font-family:arial; font-size:10pt;\">" + res[-1] + "</span>", unsafe_allow_html=True ) if action in [2, 3]: UpperCAmelCase__ = find_nearest_training(question) UpperCAmelCase__ = nn_train_list[0] st.markdown( "--- \n ### The most similar question in the ELI5 training set was: \n\n {}".format(train_exple["title"]) ) UpperCAmelCase__ = [ "{}. {}".format(i + 1, " \n".join([line.strip() for line in ans.split("\n") if line.strip() != ""])) for i, (ans, sc) in enumerate(zip(train_exple["answers"]["text"], train_exple["answers"]["score"])) if i == 0 or sc > 2 ] st.markdown("##### Its answers were: \n\n {}".format("\n".join(answers_st))) UpperCAmelCase__ = "\n---\n\n**Disclaimer**\n\n*The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system.\nEvaluating biases of such a model and ensuring factual generations are still very much open research problems.\nTherefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.*\n" st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
26
1
import csv from collections import defaultdict from dataclasses import dataclass, field from typing import List, Optional import matplotlib.pyplot as plt import numpy as np from matplotlib.ticker import ScalarFormatter from transformers import HfArgumentParser def _a ( a :str=None , a :str=None ) -> Optional[int]: return field(default_factory=lambda: default , metadata=a ) @dataclass class lowercase_ : '''simple docstring''' __snake_case = field( metadata={'''help''': '''The csv file to plot.'''} , ) __snake_case = field( default=lowercase , metadata={'''help''': '''Whether to plot along batch size or sequence length. Defaults to sequence length.'''} , ) __snake_case = field( default=lowercase , metadata={'''help''': '''Whether the csv file has time results or memory results. Defaults to memory results.'''} , ) __snake_case = field( default=lowercase , metadata={'''help''': '''Disable logarithmic scale when plotting'''} , ) __snake_case = field( default=lowercase , metadata={ '''help''': '''Whether the csv file has training results or inference results. Defaults to inference results.''' } , ) __snake_case = field( default=lowercase , metadata={'''help''': '''Filename under which the plot will be saved. If unused no plot is saved.'''} , ) __snake_case = list_field( default=lowercase , metadata={'''help''': '''List of model names that are used instead of the ones in the csv file.'''} ) def _a ( a :Optional[Any] ) -> Tuple: try: int(a ) return True except ValueError: return False def _a ( a :Any ) -> int: try: float(a ) return True except ValueError: return False class lowercase_ : '''simple docstring''' def __init__( self : Optional[Any] , __UpperCAmelCase : List[str] ) ->Tuple: """simple docstring""" a = args a = defaultdict(lambda: {"bsz": [], "seq_len": [], "result": {}} ) with open(self.args.csv_file , newline='''''' ) as csv_file: a = csv.DictReader(__UpperCAmelCase ) for row in reader: a = row['''model'''] self.result_dict[model_name]["bsz"].append(int(row['''batch_size'''] ) ) self.result_dict[model_name]["seq_len"].append(int(row['''sequence_length'''] ) ) if can_convert_to_int(row['''result'''] ): # value is not None a = int(row['''result'''] ) elif can_convert_to_float(row['''result'''] ): # value is not None a = float(row['''result'''] ) def __lowerCAmelCase ( self : Optional[Any] ) ->int: """simple docstring""" a , a = plt.subplots() a = '''Time usage''' if self.args.is_time else '''Memory usage''' a = title_str + ''' for training''' if self.args.is_train else title_str + ''' for inference''' if not self.args.no_log_scale: # set logarithm scales ax.set_xscale('''log''' ) ax.set_yscale('''log''' ) for axis in [ax.xaxis, ax.yaxis]: axis.set_major_formatter(ScalarFormatter() ) for model_name_idx, model_name in enumerate(self.result_dict.keys() ): a = sorted(set(self.result_dict[model_name]['''bsz'''] ) ) a = sorted(set(self.result_dict[model_name]['''seq_len'''] ) ) a = self.result_dict[model_name]['''result'''] ((a) , (a)) = ( (batch_sizes, sequence_lengths) if self.args.plot_along_batch else (sequence_lengths, batch_sizes) ) a = ( model_name if self.args.short_model_names is None else self.args.short_model_names[model_name_idx] ) for inner_loop_value in inner_loop_array: if self.args.plot_along_batch: a = np.asarray( [results[(x, inner_loop_value)] for x in x_axis_array if (x, inner_loop_value) in results] , dtype=__UpperCAmelCase , ) else: a = np.asarray( [results[(inner_loop_value, x)] for x in x_axis_array if (inner_loop_value, x) in results] , dtype=np.floataa , ) ((a) , (a)) = ( ('''batch_size''', '''len''') if self.args.plot_along_batch else ('''in #tokens''', '''bsz''') ) a = np.asarray(__UpperCAmelCase , __UpperCAmelCase )[: len(__UpperCAmelCase )] plt.scatter( __UpperCAmelCase , __UpperCAmelCase , label=F"""{label_model_name} - {inner_loop_label}: {inner_loop_value}""" ) plt.plot(__UpperCAmelCase , __UpperCAmelCase , '''--''' ) title_str += F""" {label_model_name} vs.""" a = title_str[:-4] a = '''Time in s''' if self.args.is_time else '''Memory in MB''' # plot plt.title(__UpperCAmelCase ) plt.xlabel(__UpperCAmelCase ) plt.ylabel(__UpperCAmelCase ) plt.legend() if self.args.figure_png_file is not None: plt.savefig(self.args.figure_png_file ) else: plt.show() def _a ( ) -> Any: a = HfArgumentParser(a ) a = parser.parse_args_into_dataclasses()[0] a = Plot(args=a ) plot.plot() if __name__ == "__main__": main()
26
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin UpperCAmelCase__ = "▁" UpperCAmelCase__ = get_tests_dir("fixtures/test_sentencepiece.model") @require_sentencepiece class lowercase_ ( lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = BertGenerationTokenizer __snake_case = False __snake_case = True def __lowerCAmelCase ( self : str ) ->str: """simple docstring""" super().setUp() a = BertGenerationTokenizer(__UpperCAmelCase , keep_accents=__UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self : int ) ->Dict: """simple docstring""" a = '''<s>''' a = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCAmelCase ) , __UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCAmelCase ) , __UpperCAmelCase ) def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''<pad>''' ) self.assertEqual(len(__UpperCAmelCase ) , 1_002 ) def __lowerCAmelCase ( self : List[str] ) ->List[Any]: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1_000 ) def __lowerCAmelCase ( self : Tuple ) ->Optional[int]: """simple docstring""" a = BertGenerationTokenizer(__UpperCAmelCase , keep_accents=__UpperCAmelCase ) a = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(__UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) , [285, 46, 10, 170, 382] , ) a = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( __UpperCAmelCase , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) a = tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) self.assertListEqual( __UpperCAmelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) a = tokenizer.convert_ids_to_tokens(__UpperCAmelCase ) self.assertListEqual( __UpperCAmelCase , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) @cached_property def __lowerCAmelCase ( self : List[Any] ) ->List[str]: """simple docstring""" return BertGenerationTokenizer.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder''' ) @slow def __lowerCAmelCase ( self : Any ) ->str: """simple docstring""" a = '''Hello World!''' a = [18_536, 2_260, 101] self.assertListEqual(__UpperCAmelCase , self.big_tokenizer.encode(__UpperCAmelCase ) ) @slow def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = ( '''This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will''' ''' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth''' ) a = [ 871, 419, 358, 946, 991, 2_521, 452, 358, 1_357, 387, 7_751, 3_536, 112, 985, 456, 126, 865, 938, 5_400, 5_734, 458, 1_368, 467, 786, 2_462, 5_246, 1_159, 633, 865, 4_519, 457, 582, 852, 2_557, 427, 916, 508, 405, 34_324, 497, 391, 408, 11_342, 1_244, 385, 100, 938, 985, 456, 574, 362, 12_597, 3_200, 3_129, 1_172, ] self.assertListEqual(__UpperCAmelCase , self.big_tokenizer.encode(__UpperCAmelCase ) ) @require_torch @slow def __lowerCAmelCase ( self : Any ) ->Dict: """simple docstring""" import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence a = list(self.big_tokenizer.get_vocab().keys() )[:10] a = ''' '''.join(__UpperCAmelCase ) a = self.big_tokenizer.encode_plus(__UpperCAmelCase , return_tensors='''pt''' , return_token_type_ids=__UpperCAmelCase ) a = self.big_tokenizer.batch_encode_plus( [sequence + ''' ''' + sequence] , return_tensors='''pt''' , return_token_type_ids=__UpperCAmelCase ) a = BertGenerationConfig() a = BertGenerationEncoder(__UpperCAmelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**__UpperCAmelCase ) model(**__UpperCAmelCase ) @slow def __lowerCAmelCase ( self : str ) ->Optional[Any]: """simple docstring""" a = {'''input_ids''': [[39_286, 458, 36_335, 2_001, 456, 13_073, 13_266, 455, 113, 7_746, 1_741, 11_157, 391, 13_073, 13_266, 455, 113, 3_967, 35_412, 113, 4_936, 109, 3_870, 2_377, 113, 30_084, 45_720, 458, 134, 17_496, 112, 503, 11_672, 113, 118, 112, 5_665, 13_347, 38_687, 112, 1_496, 31_389, 112, 3_268, 47_264, 134, 962, 112, 16_377, 8_035, 23_130, 430, 12_169, 15_518, 28_592, 458, 146, 41_697, 109, 391, 12_169, 15_518, 16_689, 458, 146, 41_358, 109, 452, 726, 4_034, 111, 763, 35_412, 5_082, 388, 1_903, 111, 9_051, 391, 2_870, 48_918, 1_900, 1_123, 550, 998, 112, 9_586, 15_985, 455, 391, 410, 22_955, 37_636, 114], [448, 17_496, 419, 3_663, 385, 763, 113, 27_533, 2_870, 3_283, 13_043, 1_639, 24_713, 523, 656, 24_013, 18_550, 2_521, 517, 27_014, 21_244, 420, 1_212, 1_465, 391, 927, 4_833, 388, 578, 11_786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2_169, 7_687, 21_932, 18_146, 726, 363, 17_032, 3_391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__UpperCAmelCase , model_name='''google/bert_for_seq_generation_L-24_bbc_encoder''' , revision='''c817d1fd1be2ffa69431227a1fe320544943d4db''' , )
26
1
from typing import Any, Callable, Dict, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker UpperCAmelCase__ = "CompVis/stable-diffusion-v1-1" UpperCAmelCase__ = "CompVis/stable-diffusion-v1-2" UpperCAmelCase__ = "CompVis/stable-diffusion-v1-3" UpperCAmelCase__ = "CompVis/stable-diffusion-v1-4" class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Union[str, Any] , __UpperCAmelCase : AutoencoderKL , __UpperCAmelCase : CLIPTextModel , __UpperCAmelCase : CLIPTokenizer , __UpperCAmelCase : UNetaDConditionModel , __UpperCAmelCase : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , __UpperCAmelCase : StableDiffusionSafetyChecker , __UpperCAmelCase : CLIPImageProcessor , __UpperCAmelCase : bool = True , ) ->Tuple: """simple docstring""" super()._init_() a = StableDiffusionPipeline.from_pretrained(__UpperCAmelCase ) a = StableDiffusionPipeline.from_pretrained(__UpperCAmelCase ) a = StableDiffusionPipeline.from_pretrained(__UpperCAmelCase ) a = StableDiffusionPipeline( vae=__UpperCAmelCase , text_encoder=__UpperCAmelCase , tokenizer=__UpperCAmelCase , unet=__UpperCAmelCase , scheduler=__UpperCAmelCase , safety_checker=__UpperCAmelCase , feature_extractor=__UpperCAmelCase , requires_safety_checker=__UpperCAmelCase , ) self.register_modules(pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea ) @property def __lowerCAmelCase ( self : Dict ) ->Dict[str, Any]: """simple docstring""" return {k: getattr(self , __UpperCAmelCase ) for k in self.config.keys() if not k.startswith('''_''' )} def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : Optional[Union[str, int]] = "auto" ) ->Dict: """simple docstring""" if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory a = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(__UpperCAmelCase ) def __lowerCAmelCase ( self : Any ) ->Tuple: """simple docstring""" self.enable_attention_slicing(__UpperCAmelCase ) @torch.no_grad() def __lowerCAmelCase ( self : Any , __UpperCAmelCase : Union[str, List[str]] , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 50 , __UpperCAmelCase : float = 7.5 , __UpperCAmelCase : Optional[Union[str, List[str]]] = None , __UpperCAmelCase : Optional[int] = 1 , __UpperCAmelCase : float = 0.0 , __UpperCAmelCase : Optional[torch.Generator] = None , __UpperCAmelCase : Optional[torch.FloatTensor] = None , __UpperCAmelCase : Optional[str] = "pil" , __UpperCAmelCase : bool = True , __UpperCAmelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , __UpperCAmelCase : int = 1 , **__UpperCAmelCase : int , ) ->str: """simple docstring""" return self.pipea( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) @torch.no_grad() def __lowerCAmelCase ( self : str , __UpperCAmelCase : Union[str, List[str]] , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 50 , __UpperCAmelCase : float = 7.5 , __UpperCAmelCase : Optional[Union[str, List[str]]] = None , __UpperCAmelCase : Optional[int] = 1 , __UpperCAmelCase : float = 0.0 , __UpperCAmelCase : Optional[torch.Generator] = None , __UpperCAmelCase : Optional[torch.FloatTensor] = None , __UpperCAmelCase : Optional[str] = "pil" , __UpperCAmelCase : bool = True , __UpperCAmelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , __UpperCAmelCase : int = 1 , **__UpperCAmelCase : List[Any] , ) ->Union[str, Any]: """simple docstring""" return self.pipea( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) @torch.no_grad() def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Union[str, List[str]] , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 50 , __UpperCAmelCase : float = 7.5 , __UpperCAmelCase : Optional[Union[str, List[str]]] = None , __UpperCAmelCase : Optional[int] = 1 , __UpperCAmelCase : float = 0.0 , __UpperCAmelCase : Optional[torch.Generator] = None , __UpperCAmelCase : Optional[torch.FloatTensor] = None , __UpperCAmelCase : Optional[str] = "pil" , __UpperCAmelCase : bool = True , __UpperCAmelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , __UpperCAmelCase : int = 1 , **__UpperCAmelCase : Optional[Any] , ) ->Optional[int]: """simple docstring""" return self.pipea( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) @torch.no_grad() def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Union[str, List[str]] , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 50 , __UpperCAmelCase : float = 7.5 , __UpperCAmelCase : Optional[Union[str, List[str]]] = None , __UpperCAmelCase : Optional[int] = 1 , __UpperCAmelCase : float = 0.0 , __UpperCAmelCase : Optional[torch.Generator] = None , __UpperCAmelCase : Optional[torch.FloatTensor] = None , __UpperCAmelCase : Optional[str] = "pil" , __UpperCAmelCase : bool = True , __UpperCAmelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , __UpperCAmelCase : int = 1 , **__UpperCAmelCase : List[Any] , ) ->Union[str, Any]: """simple docstring""" return self.pipea( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) @torch.no_grad() def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Union[str, List[str]] , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 50 , __UpperCAmelCase : float = 7.5 , __UpperCAmelCase : Optional[Union[str, List[str]]] = None , __UpperCAmelCase : Optional[int] = 1 , __UpperCAmelCase : float = 0.0 , __UpperCAmelCase : Optional[torch.Generator] = None , __UpperCAmelCase : Optional[torch.FloatTensor] = None , __UpperCAmelCase : Optional[str] = "pil" , __UpperCAmelCase : bool = True , __UpperCAmelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , __UpperCAmelCase : int = 1 , **__UpperCAmelCase : Any , ) ->Optional[int]: """simple docstring""" a = '''cuda''' if torch.cuda.is_available() else '''cpu''' self.to(__UpperCAmelCase ) # Checks if the height and width are divisible by 8 or not if height % 8 != 0 or width % 8 != 0: raise ValueError(F"""`height` and `width` must be divisible by 8 but are {height} and {width}.""" ) # Get first result from Stable Diffusion Checkpoint v1.1 a = self.textaimg_sda_a( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) # Get first result from Stable Diffusion Checkpoint v1.2 a = self.textaimg_sda_a( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) # Get first result from Stable Diffusion Checkpoint v1.3 a = self.textaimg_sda_a( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) # Get first result from Stable Diffusion Checkpoint v1.4 a = self.textaimg_sda_a( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) # Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]] )
26
import argparse import numpy as np import torch from transformers import SpeechTaHifiGan, SpeechTaHifiGanConfig, logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger("transformers.models.speecht5") def _a ( a :Optional[Any] , a :Tuple , a :Dict ) -> List[str]: hf_model.apply_weight_norm() a = checkpoint['''input_conv.weight_g'''] a = checkpoint['''input_conv.weight_v'''] a = checkpoint['''input_conv.bias'''] for i in range(len(config.upsample_rates ) ): a = checkpoint[F"""upsamples.{i}.1.weight_g"""] a = checkpoint[F"""upsamples.{i}.1.weight_v"""] a = checkpoint[F"""upsamples.{i}.1.bias"""] for i in range(len(config.upsample_rates ) * len(config.resblock_kernel_sizes ) ): for j in range(len(config.resblock_dilation_sizes ) ): a = checkpoint[F"""blocks.{i}.convs1.{j}.1.weight_g"""] a = checkpoint[F"""blocks.{i}.convs1.{j}.1.weight_v"""] a = checkpoint[F"""blocks.{i}.convs1.{j}.1.bias"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.weight_g"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.weight_v"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.bias"""] a = checkpoint['''output_conv.1.weight_g'''] a = checkpoint['''output_conv.1.weight_v'''] a = checkpoint['''output_conv.1.bias'''] hf_model.remove_weight_norm() @torch.no_grad() def _a ( a :List[str] , a :Union[str, Any] , a :Dict , a :Dict=None , a :List[Any]=None , ) -> int: if config_path is not None: a = SpeechTaHifiGanConfig.from_pretrained(a ) else: a = SpeechTaHifiGanConfig() a = SpeechTaHifiGan(a ) a = torch.load(a ) load_weights(orig_checkpoint['''model''']['''generator'''] , a , a ) a = np.load(a ) a = stats[0].reshape(-1 ) a = stats[1].reshape(-1 ) a = torch.from_numpy(a ).float() a = torch.from_numpy(a ).float() model.save_pretrained(a ) if repo_id: print('''Pushing to the hub...''' ) model.push_to_hub(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--checkpoint_path", required=True, default=None, type=str, help="Path to original checkpoint") parser.add_argument("--stats_path", required=True, default=None, type=str, help="Path to stats.npy file") 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." ) UpperCAmelCase__ = parser.parse_args() convert_hifigan_checkpoint( args.checkpoint_path, args.stats_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
26
1
def _a ( a :int ) -> bool: a = n ** (1 / 3) return (val * val * val) == n if __name__ == "__main__": print(perfect_cube(27)) print(perfect_cube(4))
26
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) UpperCAmelCase__ = { "configuration_gpt_bigcode": ["GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP", "GPTBigCodeConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST", "GPTBigCodeForSequenceClassification", "GPTBigCodeForTokenClassification", "GPTBigCodeForCausalLM", "GPTBigCodeModel", "GPTBigCodePreTrainedModel", ] if TYPE_CHECKING: from .configuration_gpt_bigcode import GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTBigCodeConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_bigcode import ( GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTBigCodeForCausalLM, GPTBigCodeForSequenceClassification, GPTBigCodeForTokenClassification, GPTBigCodeModel, GPTBigCodePreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
1
import collections import os import re from pathlib import Path UpperCAmelCase__ = "src/transformers" # Matches is_xxx_available() UpperCAmelCase__ = re.compile(R"is\_([a-z_]*)_available()") # Catches a one-line _import_struct = {xxx} UpperCAmelCase__ = re.compile(R"^_import_structure\s+=\s+\{([^\}]+)\}") # Catches a line with a key-values pattern: "bla": ["foo", "bar"] UpperCAmelCase__ = re.compile(R"\s+\"\S*\":\s+\[([^\]]*)\]") # Catches a line if not is_foo_available UpperCAmelCase__ = re.compile(R"^\s*if\s+not\s+is\_[a-z_]*\_available\(\)") # Catches a line _import_struct["bla"].append("foo") UpperCAmelCase__ = re.compile(R"^\s*_import_structure\[\"\S*\"\]\.append\(\"(\S*)\"\)") # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] UpperCAmelCase__ = re.compile(R"^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]") # Catches a line with an object between quotes and a comma: "MyModel", UpperCAmelCase__ = re.compile(R"^\s+\"([^\"]+)\",") # Catches a line with objects between brackets only: ["foo", "bar"], UpperCAmelCase__ = re.compile(R"^\s+\[([^\]]+)\]") # Catches a line with from foo import bar, bla, boo UpperCAmelCase__ = re.compile(R"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") # Catches a line with try: UpperCAmelCase__ = re.compile(R"^\s*try:") # Catches a line with else: UpperCAmelCase__ = re.compile(R"^\s*else:") def _a ( a :int ) -> int: if _re_test_backend.search(a ) is None: return None a = [b[0] for b in _re_backend.findall(a )] backends.sort() return "_and_".join(a ) def _a ( a :Dict ) -> int: with open(a , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: a = f.readlines() a = 0 while line_index < len(a ) and not lines[line_index].startswith('''_import_structure = {''' ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(a ): return None # First grab the objects without a specific backend in _import_structure a = [] while not lines[line_index].startswith('''if TYPE_CHECKING''' ) and find_backend(lines[line_index] ) is None: a = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(a ): a = _re_one_line_import_struct.search(a ).groups()[0] a = re.findall(r'''\[([^\]]+)\]''' , a ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(''', ''' )] ) line_index += 1 continue a = _re_import_struct_key_value.search(a ) if single_line_import_search is not None: a = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(''', ''' ) if len(a ) > 0] objects.extend(a ) elif line.startswith(''' ''' * 8 + '''"''' ): objects.append(line[9:-3] ) line_index += 1 a = {'''none''': objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith('''if TYPE_CHECKING''' ): # If the line is an if not is_backend_available, we grab all objects associated. a = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: a = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 a = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(''' ''' * 4 ): a = lines[line_index] if _re_import_struct_add_one.search(a ) is not None: objects.append(_re_import_struct_add_one.search(a ).groups()[0] ) elif _re_import_struct_add_many.search(a ) is not None: a = _re_import_struct_add_many.search(a ).groups()[0].split(''', ''' ) a = [obj[1:-1] for obj in imports if len(a ) > 0] objects.extend(a ) elif _re_between_brackets.search(a ) is not None: a = _re_between_brackets.search(a ).groups()[0].split(''', ''' ) a = [obj[1:-1] for obj in imports if len(a ) > 0] objects.extend(a ) elif _re_quote_object.search(a ) is not None: objects.append(_re_quote_object.search(a ).groups()[0] ) elif line.startswith(''' ''' * 8 + '''"''' ): objects.append(line[9:-3] ) elif line.startswith(''' ''' * 12 + '''"''' ): objects.append(line[13:-3] ) line_index += 1 a = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend a = [] while ( line_index < len(a ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith('''else''' ) ): a = lines[line_index] a = _re_import.search(a ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(''', ''' ) ) elif line.startswith(''' ''' * 8 ): objects.append(line[8:-2] ) line_index += 1 a = {'''none''': objects} # Let's continue with backend-specific objects while line_index < len(a ): # If the line is an if is_backend_available, we grab all objects associated. a = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: a = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 a = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(''' ''' * 8 ): a = lines[line_index] a = _re_import.search(a ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(''', ''' ) ) elif line.startswith(''' ''' * 12 ): objects.append(line[12:-2] ) line_index += 1 a = objects else: line_index += 1 return import_dict_objects, type_hint_objects def _a ( a :List[Any] , a :int ) -> int: def find_duplicates(a :List[str] ): return [k for k, v in collections.Counter(a ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] a = [] for key in import_dict_objects.keys(): a = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F"""Duplicate _import_structure definitions for: {duplicate_imports}""" ) a = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(F"""Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}""" ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): a = '''base imports''' if key == '''none''' else F"""{key} backend""" errors.append(F"""Differences for {name}:""" ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(F""" {a} in TYPE_HINT but not in _import_structure.""" ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(F""" {a} in _import_structure but not in TYPE_HINT.""" ) return errors def _a ( ) -> List[Any]: a = [] for root, _, files in os.walk(a ): if "__init__.py" in files: a = os.path.join(a , '''__init__.py''' ) a = parse_init(a ) if objects is not None: a = analyze_results(*a ) if len(a ) > 0: a = F"""Problem in {fname}, both halves do not define the same objects.\n{errors[0]}""" failures.append('''\n'''.join(a ) ) if len(a ) > 0: raise ValueError('''\n\n'''.join(a ) ) def _a ( ) -> List[Any]: a = [] for path, directories, files in os.walk(a ): for folder in directories: # Ignore private modules if folder.startswith('''_''' ): directories.remove(a ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(a ) / folder).glob('''*.py''' ) ) ) == 0: continue a = str((Path(a ) / folder).relative_to(a ) ) a = short_path.replace(os.path.sep , '''.''' ) submodules.append(a ) for fname in files: if fname == "__init__.py": continue a = str((Path(a ) / fname).relative_to(a ) ) a = short_path.replace('''.py''' , '''''' ).replace(os.path.sep , '''.''' ) if len(submodule.split('''.''' ) ) == 1: submodules.append(a ) return submodules UpperCAmelCase__ = [ "convert_pytorch_checkpoint_to_tf2", "modeling_flax_pytorch_utils", "models.esm.openfold_utils", ] def _a ( ) -> Optional[int]: # This is to make sure the transformers module imported is the one in the repo. from transformers.utils import direct_transformers_import a = direct_transformers_import(a ) a = set(transformers._import_structure.keys() ) # This contains all the base keys of the _import_structure object defined in the init, but if the user is missing # some optional dependencies, they may not have all of them. Thus we read the init to read all additions and # (potentiall re-) add them. with open(os.path.join(a , '''__init__.py''' ) , '''r''' ) as f: a = f.read() import_structure_keys.update(set(re.findall(r'''import_structure\[\"([^\"]*)\"\]''' , a ) ) ) a = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in import_structure_keys ] if len(a ) > 0: a = '''\n'''.join(F"""- {module}""" for module in module_not_registered ) raise ValueError( '''The following submodules are not properly registed in the main init of Transformers:\n''' F"""{list_of_modules}\n""" '''Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value.''' ) if __name__ == "__main__": check_all_inits() check_submodules()
26
import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def _a ( a :Tuple ) -> int: a = tmp_path / '''file.csv''' a = textwrap.dedent( '''\ header1,header2 1,2 10,20 ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :int ) -> List[str]: a = tmp_path / '''malformed_file.csv''' a = textwrap.dedent( '''\ header1,header2 1,2 10,20, ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :Dict , a :int ) -> List[str]: a = tmp_path / '''csv_with_image.csv''' a = textwrap.dedent( F"""\ image {image_file} """ ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :List[Any] ) -> Dict: a = tmp_path / '''csv_with_label.csv''' a = textwrap.dedent( '''\ label good bad good ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :Tuple ) -> Any: a = tmp_path / '''csv_with_int_list.csv''' a = textwrap.dedent( '''\ int_list 1 2 3 4 5 6 7 8 9 ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) def _a ( a :Dict , a :int , a :Union[str, Any] ) -> List[Any]: a = Csv() a = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(a , match='''Error tokenizing data''' ): for _ in generator: pass assert any( record.levelname == '''ERROR''' and '''Failed to read file''' in record.message and os.path.basename(a ) in record.message for record in caplog.records ) @require_pil def _a ( a :Dict ) -> Any: with open(a , encoding='''utf-8''' ) as f: a = f.read().splitlines()[1] a = Csv(encoding='''utf-8''' , features=Features({'''image''': Image()} ) ) a = csv._generate_tables([[csv_file_with_image]] ) a = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''image''' ).type == Image()() a = pa_table.to_pydict()['''image'''] assert generated_content == [{"path": image_file, "bytes": None}] def _a ( a :Any ) -> Tuple: with open(a , encoding='''utf-8''' ) as f: a = f.read().splitlines()[1:] a = Csv(encoding='''utf-8''' , features=Features({'''label''': ClassLabel(names=['''good''', '''bad'''] )} ) ) a = csv._generate_tables([[csv_file_with_label]] ) a = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''label''' ).type == ClassLabel(names=['''good''', '''bad'''] )() a = pa_table.to_pydict()['''label'''] assert generated_content == [ClassLabel(names=['''good''', '''bad'''] ).straint(a ) for label in labels] def _a ( a :Union[str, Any] ) -> Optional[Any]: a = Csv(encoding='''utf-8''' , sep=''',''' , converters={'''int_list''': lambda a : [int(a ) for i in x.split()]} ) a = csv._generate_tables([[csv_file_with_int_list]] ) a = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field('''int_list''' ).type ) a = pa_table.to_pydict()['''int_list'''] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
26
1
def _a ( a :str ) -> bool: a = 0 for ch in input_str: a = ord(a ) a = pow(2 , a ) # 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
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SwiftFormerConfig, SwiftFormerForImageClassification, ViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = torch.device("cpu") def _a ( ) -> Union[str, Any]: a = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a = Image.open(requests.get(a , stream=a ).raw ) return im def _a ( a :Dict ) -> Tuple: if swiftformer_name == "swiftformer_xs": return torch.tensor([-2.1703e00, 2.1107e00, -2.0811e00, 8.8685e-01, 2.4360e-01] ) elif swiftformer_name == "swiftformer_s": return torch.tensor([3.9636e-01, 2.3478e-01, -1.6963e00, -1.7381e00, -8.6337e-01] ) elif swiftformer_name == "swiftformer_l1": return torch.tensor([-4.2768e-01, -4.7429e-01, -1.0897e00, -1.0248e00, 3.5523e-02] ) elif swiftformer_name == "swiftformer_l3": return torch.tensor([-2.5330e-01, 2.4211e-01, -6.0185e-01, -8.2789e-01, -6.0446e-02] ) def _a ( a :int , a :Any , a :Union[str, Any] ) -> int: a = dct.pop(a ) a = val def _a ( a :Any ) -> Dict: a = [] for k in state_dict.keys(): a = k if ".pwconv" in k: a = k_new.replace('''.pwconv''' , '''.point_wise_conv''' ) if ".dwconv" in k: a = k_new.replace('''.dwconv''' , '''.depth_wise_conv''' ) if ".Proj." in k: a = k_new.replace('''.Proj.''' , '''.proj.''' ) if "patch_embed" in k_new: a = k_new.replace('''patch_embed''' , '''swiftformer.patch_embed.patch_embedding''' ) if "network" in k_new: a = k_new.split('''.''' ) if ls[2].isdigit(): a = '''swiftformer.encoder.network.''' + ls[1] + '''.blocks.''' + ls[2] + '''.''' + '''.'''.join(ls[3:] ) else: a = k_new.replace('''network''' , '''swiftformer.encoder.network''' ) rename_keys.append((k, k_new) ) return rename_keys @torch.no_grad() def _a ( a :List[Any] , a :Tuple , a :List[str] ) -> Union[str, Any]: a = SwiftFormerConfig() # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size a = 1_000 a = '''huggingface/label-files''' a = '''imagenet-1k-id2label.json''' a = json.load(open(hf_hub_download(a , a , repo_type='''dataset''' ) , '''r''' ) ) a = {int(a ): v for k, v in idalabel.items()} a = idalabel a = {v: k for k, v in idalabel.items()} # size of the architecture if swiftformer_name == "swiftformer_xs": a = [3, 3, 6, 4] a = [48, 56, 112, 220] elif swiftformer_name == "swiftformer_s": a = [3, 3, 9, 6] a = [48, 64, 168, 224] elif swiftformer_name == "swiftformer_l1": a = [4, 3, 10, 5] a = [48, 96, 192, 384] elif swiftformer_name == "swiftformer_l3": a = [4, 4, 12, 6] a = [64, 128, 320, 512] # load state_dict of original model, remove and rename some keys if original_ckpt: if original_ckpt.startswith('''https''' ): a = torch.hub.load_state_dict_from_url(a , map_location='''cpu''' , check_hash=a ) else: a = torch.load(a , map_location='''cpu''' ) a = checkpoint a = create_rename_keys(a ) for rename_key_src, rename_key_dest in rename_keys: rename_key(a , a , a ) # load HuggingFace model a = SwiftFormerForImageClassification(a ).eval() hf_model.load_state_dict(a ) # prepare test inputs a = prepare_img() a = ViTImageProcessor.from_pretrained('''preprocessor_config''' ) a = processor(images=a , return_tensors='''pt''' ) # compare outputs from both models a = get_expected_output(a ) a = hf_model(inputs['''pixel_values'''] ).logits assert hf_logits.shape == torch.Size([1, 1_000] ) assert torch.allclose(hf_logits[0, 0:5] , a , atol=1e-3 ) Path(a ).mkdir(exist_ok=a ) print(F"""Saving model {swiftformer_name} to {pytorch_dump_folder_path}""" ) hf_model.save_pretrained(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--swiftformer_name", default="swiftformer_xs", choices=["swiftformer_xs", "swiftformer_s", "swiftformer_l1", "swiftformer_l3"], type=str, help="Name of the SwiftFormer model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default="./converted_outputs/", type=str, help="Path to the output PyTorch model directory.", ) parser.add_argument("--original_ckpt", default=None, type=str, help="Path to the original model checkpoint.") UpperCAmelCase__ = parser.parse_args() convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)
26
1
import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device if is_torch_available(): from transformers import AutoModelForSeqaSeqLM, AutoTokenizer @require_torch @require_sentencepiece @require_tokenizers class lowercase_ ( unittest.TestCase ): '''simple docstring''' @slow def __lowerCAmelCase ( self : Optional[int] ) ->Tuple: """simple docstring""" a = AutoModelForSeqaSeqLM.from_pretrained('''google/mt5-small''' , return_dict=__UpperCAmelCase ).to(__UpperCAmelCase ) a = AutoTokenizer.from_pretrained('''google/mt5-small''' ) a = tokenizer('''Hello there''' , return_tensors='''pt''' ).input_ids a = tokenizer('''Hi I am''' , return_tensors='''pt''' ).input_ids a = model(input_ids.to(__UpperCAmelCase ) , labels=labels.to(__UpperCAmelCase ) ).loss a = -(labels.shape[-1] * loss.item()) a = -84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
26
import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Union[str, Any] , __UpperCAmelCase : UNetaDModel , __UpperCAmelCase : UNetaDModel , __UpperCAmelCase : DDPMScheduler , __UpperCAmelCase : Optional[int] , ) ->List[str]: """simple docstring""" super().__init__() a = value_function a = unet a = scheduler a = env a = env.get_dataset() a = {} for key in self.data.keys(): try: a = self.data[key].mean() except: # noqa: E722 pass a = {} for key in self.data.keys(): try: a = self.data[key].std() except: # noqa: E722 pass a = env.observation_space.shape[0] a = env.action_space.shape[0] def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[int] ) ->Dict: """simple docstring""" return (x_in - self.means[key]) / self.stds[key] def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Dict , __UpperCAmelCase : Dict ) ->List[str]: """simple docstring""" return x_in * self.stds[key] + self.means[key] def __lowerCAmelCase ( self : int , __UpperCAmelCase : int ) ->List[str]: """simple docstring""" if type(__UpperCAmelCase ) is dict: return {k: self.to_torch(__UpperCAmelCase ) for k, v in x_in.items()} elif torch.is_tensor(__UpperCAmelCase ): return x_in.to(self.unet.device ) return torch.tensor(__UpperCAmelCase , device=self.unet.device ) def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : int , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Tuple ) ->int: """simple docstring""" for key, val in cond.items(): a = val.clone() return x_in def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[Any] ) ->Tuple: """simple docstring""" a = x.shape[0] a = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model a = torch.full((batch_size,) , __UpperCAmelCase , device=self.unet.device , dtype=torch.long ) for _ in range(__UpperCAmelCase ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models a = self.value_function(x.permute(0 , 2 , 1 ) , __UpperCAmelCase ).sample a = torch.autograd.grad([y.sum()] , [x] )[0] a = self.scheduler._get_variance(__UpperCAmelCase ) a = torch.exp(0.5 * posterior_variance ) a = model_std * grad a = 0 a = x.detach() a = x + scale * grad a = self.reset_xa(__UpperCAmelCase , __UpperCAmelCase , self.action_dim ) a = self.unet(x.permute(0 , 2 , 1 ) , __UpperCAmelCase ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg a = self.scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , predict_epsilon=__UpperCAmelCase )['''prev_sample'''] # apply conditions to the trajectory (set the initial state) a = self.reset_xa(__UpperCAmelCase , __UpperCAmelCase , self.action_dim ) a = self.to_torch(__UpperCAmelCase ) return x, y def __call__( self : Union[str, Any] , __UpperCAmelCase : Dict , __UpperCAmelCase : Optional[int]=64 , __UpperCAmelCase : int=32 , __UpperCAmelCase : Optional[Any]=2 , __UpperCAmelCase : str=0.1 ) ->List[str]: """simple docstring""" a = self.normalize(__UpperCAmelCase , '''observations''' ) a = obs[None].repeat(__UpperCAmelCase , axis=0 ) a = {0: self.to_torch(__UpperCAmelCase )} a = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) a = randn_tensor(__UpperCAmelCase , device=self.unet.device ) a = self.reset_xa(__UpperCAmelCase , __UpperCAmelCase , self.action_dim ) a = self.to_torch(__UpperCAmelCase ) # run the diffusion process a , a = self.run_diffusion(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) # sort output trajectories by value a = y.argsort(0 , descending=__UpperCAmelCase ).squeeze() a = x[sorted_idx] a = sorted_values[:, :, : self.action_dim] a = actions.detach().cpu().numpy() a = self.de_normalize(__UpperCAmelCase , key='''actions''' ) # select the action with the highest value if y is not None: a = 0 else: # if we didn't run value guiding, select a random action a = np.random.randint(0 , __UpperCAmelCase ) a = denorm_actions[selected_index, 0] return denorm_actions
26
1
import inspect import tempfile from collections import OrderedDict, UserDict from collections.abc import MutableMapping from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from typing import Any, ContextManager, List, Tuple import numpy as np from .import_utils import is_flax_available, is_tf_available, is_torch_available, is_torch_fx_proxy if is_flax_available(): import jax.numpy as jnp class lowercase_ ( lowercase ): '''simple docstring''' def __get__( self : List[Any] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Dict=None ) ->Optional[int]: """simple docstring""" if obj is None: return self if self.fget is None: raise AttributeError('''unreadable attribute''' ) a = '''__cached_''' + self.fget.__name__ a = getattr(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) if cached is None: a = self.fget(__UpperCAmelCase ) setattr(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) return cached def _a ( a :Dict ) -> int: a = val.lower() if val in {"y", "yes", "t", "true", "on", "1"}: return 1 if val in {"n", "no", "f", "false", "off", "0"}: return 0 raise ValueError(F"""invalid truth value {val!r}""" ) def _a ( a :Union[str, Any] ) -> Optional[Any]: if is_torch_fx_proxy(a ): return True if is_torch_available(): import torch if isinstance(a , torch.Tensor ): return True if is_tf_available(): import tensorflow as tf if isinstance(a , tf.Tensor ): return True if is_flax_available(): import jax.numpy as jnp from jax.core import Tracer if isinstance(a , (jnp.ndarray, Tracer) ): return True return isinstance(a , np.ndarray ) def _a ( a :List[Any] ) -> Optional[Any]: return isinstance(a , np.ndarray ) def _a ( a :Optional[Any] ) -> Tuple: return _is_numpy(a ) def _a ( a :int ) -> Union[str, Any]: import torch return isinstance(a , torch.Tensor ) def _a ( a :List[str] ) -> str: return False if not is_torch_available() else _is_torch(a ) def _a ( a :List[str] ) -> List[Any]: import torch return isinstance(a , torch.device ) def _a ( a :Tuple ) -> Dict: return False if not is_torch_available() else _is_torch_device(a ) def _a ( a :Tuple ) -> Any: import torch if isinstance(a , a ): if hasattr(a , a ): a = getattr(a , a ) else: return False return isinstance(a , torch.dtype ) def _a ( a :List[str] ) -> str: return False if not is_torch_available() else _is_torch_dtype(a ) def _a ( a :List[Any] ) -> List[Any]: import tensorflow as tf return isinstance(a , tf.Tensor ) def _a ( a :int ) -> Optional[int]: return False if not is_tf_available() else _is_tensorflow(a ) def _a ( a :Dict ) -> str: import tensorflow as tf # the `is_symbolic_tensor` predicate is only available starting with TF 2.14 if hasattr(a , '''is_symbolic_tensor''' ): return tf.is_symbolic_tensor(a ) return type(a ) == tf.Tensor def _a ( a :int ) -> str: return False if not is_tf_available() else _is_tf_symbolic_tensor(a ) def _a ( a :List[str] ) -> List[Any]: import jax.numpy as jnp # noqa: F811 return isinstance(a , jnp.ndarray ) def _a ( a :List[Any] ) -> Dict: return False if not is_flax_available() else _is_jax(a ) def _a ( a :Any ) -> List[Any]: if isinstance(a , (dict, UserDict) ): return {k: to_py_obj(a ) for k, v in obj.items()} elif isinstance(a , (list, tuple) ): return [to_py_obj(a ) for o in obj] elif is_tf_tensor(a ): return obj.numpy().tolist() elif is_torch_tensor(a ): return obj.detach().cpu().tolist() elif is_jax_tensor(a ): return np.asarray(a ).tolist() elif isinstance(a , (np.ndarray, np.number) ): # tolist also works on 0d np arrays return obj.tolist() else: return obj def _a ( a :str ) -> Optional[Any]: if isinstance(a , (dict, UserDict) ): return {k: to_numpy(a ) for k, v in obj.items()} elif isinstance(a , (list, tuple) ): return np.array(a ) elif is_tf_tensor(a ): return obj.numpy() elif is_torch_tensor(a ): return obj.detach().cpu().numpy() elif is_jax_tensor(a ): return np.asarray(a ) else: return obj class lowercase_ ( lowercase ): '''simple docstring''' def __lowerCAmelCase ( self : Union[str, Any] ) ->List[str]: """simple docstring""" a = fields(self ) # Safety and consistency checks if not len(__UpperCAmelCase ): raise ValueError(F"""{self.__class__.__name__} has no fields.""" ) if not all(field.default is None for field in class_fields[1:] ): raise ValueError(F"""{self.__class__.__name__} should not have more than one required field.""" ) a = getattr(self , class_fields[0].name ) a = all(getattr(self , field.name ) is None for field in class_fields[1:] ) if other_fields_are_none and not is_tensor(__UpperCAmelCase ): if isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = first_field.items() a = True else: try: a = iter(__UpperCAmelCase ) a = True except TypeError: a = False # if we provided an iterator as first field and the iterator is a (key, value) iterator # set the associated fields if first_field_iterator: for idx, element in enumerate(__UpperCAmelCase ): if ( not isinstance(__UpperCAmelCase , (list, tuple) ) or not len(__UpperCAmelCase ) == 2 or not isinstance(element[0] , __UpperCAmelCase ) ): if idx == 0: # If we do not have an iterator of key/values, set it as attribute a = first_field else: # If we have a mixed iterator, raise an error raise ValueError( F"""Cannot set key/value for {element}. It needs to be a tuple (key, value).""" ) break setattr(self , element[0] , element[1] ) if element[1] is not None: a = element[1] elif first_field is not None: a = first_field else: for field in class_fields: a = getattr(self , field.name ) if v is not None: a = v def __delitem__( self : int , *__UpperCAmelCase : int , **__UpperCAmelCase : str ) ->Any: """simple docstring""" raise Exception(F"""You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.""" ) def __lowerCAmelCase ( self : List[Any] , *__UpperCAmelCase : Optional[int] , **__UpperCAmelCase : str ) ->Dict: """simple docstring""" raise Exception(F"""You cannot use ``setdefault`` on a {self.__class__.__name__} instance.""" ) def __lowerCAmelCase ( self : int , *__UpperCAmelCase : Optional[Any] , **__UpperCAmelCase : str ) ->List[Any]: """simple docstring""" raise Exception(F"""You cannot use ``pop`` on a {self.__class__.__name__} instance.""" ) def __lowerCAmelCase ( self : Dict , *__UpperCAmelCase : List[Any] , **__UpperCAmelCase : Optional[Any] ) ->Tuple: """simple docstring""" raise Exception(F"""You cannot use ``update`` on a {self.__class__.__name__} instance.""" ) def __getitem__( self : List[Any] , __UpperCAmelCase : List[str] ) ->Tuple: """simple docstring""" if isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = dict(self.items() ) return inner_dict[k] else: return self.to_tuple()[k] def __setattr__( self : Union[str, Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Union[str, Any] ) ->List[str]: """simple docstring""" if name in self.keys() and value is not None: # Don't call self.__setitem__ to avoid recursion errors super().__setitem__(__UpperCAmelCase , __UpperCAmelCase ) super().__setattr__(__UpperCAmelCase , __UpperCAmelCase ) def __setitem__( self : Union[str, Any] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Optional[Any] ) ->Any: """simple docstring""" super().__setitem__(__UpperCAmelCase , __UpperCAmelCase ) # Don't call self.__setattr__ to avoid recursion errors super().__setattr__(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : Dict ) ->Tuple[Any]: """simple docstring""" return tuple(self[k] for k in self.keys() ) class lowercase_ ( lowercase , lowercase ): '''simple docstring''' @classmethod def __lowerCAmelCase ( cls : Optional[Any] , __UpperCAmelCase : Union[str, Any] ) ->Any: """simple docstring""" raise ValueError( F"""{value} is not a valid {cls.__name__}, please select one of {list(cls._valueamember_map_.keys() )}""" ) class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = '''longest''' __snake_case = '''max_length''' __snake_case = '''do_not_pad''' class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = '''pt''' __snake_case = '''tf''' __snake_case = '''np''' __snake_case = '''jax''' class lowercase_ : '''simple docstring''' def __init__( self : Optional[int] , __UpperCAmelCase : List[ContextManager] ) ->Dict: """simple docstring""" a = context_managers a = ExitStack() def __enter__( self : List[Any] ) ->List[str]: """simple docstring""" for context_manager in self.context_managers: self.stack.enter_context(__UpperCAmelCase ) def __exit__( self : Any , *__UpperCAmelCase : int , **__UpperCAmelCase : int ) ->str: """simple docstring""" self.stack.__exit__(*__UpperCAmelCase , **__UpperCAmelCase ) def _a ( a :Dict ) -> List[Any]: a = infer_framework(a ) if framework == "tf": a = inspect.signature(model_class.call ) # TensorFlow models elif framework == "pt": a = inspect.signature(model_class.forward ) # PyTorch models else: a = inspect.signature(model_class.__call__ ) # Flax models for p in signature.parameters: if p == "return_loss" and signature.parameters[p].default is True: return True return False def _a ( a :Optional[Any] ) -> Tuple: a = model_class.__name__ a = infer_framework(a ) if framework == "tf": a = inspect.signature(model_class.call ) # TensorFlow models elif framework == "pt": a = inspect.signature(model_class.forward ) # PyTorch models else: a = inspect.signature(model_class.__call__ ) # Flax models if "QuestionAnswering" in model_name: return [p for p in signature.parameters if "label" in p or p in ("start_positions", "end_positions")] else: return [p for p in signature.parameters if "label" in p] def _a ( a :MutableMapping , a :str = "" , a :str = "." ) -> Any: def _flatten_dict(a :Dict , a :Any="" , a :Dict="." ): for k, v in d.items(): a = str(a ) + delimiter + str(a ) if parent_key else k if v and isinstance(a , a ): yield from flatten_dict(a , a , delimiter=a ).items() else: yield key, v return dict(_flatten_dict(a , a , a ) ) @contextmanager def _a ( a :Optional[int] , a :bool = False ) -> Any: if use_temp_dir: with tempfile.TemporaryDirectory() as tmp_dir: yield tmp_dir else: yield working_dir def _a ( a :List[str] , a :List[Any]=None ) -> Any: if is_numpy_array(a ): return np.transpose(a , axes=a ) elif is_torch_tensor(a ): return array.T if axes is None else array.permute(*a ) elif is_tf_tensor(a ): import tensorflow as tf return tf.transpose(a , perm=a ) elif is_jax_tensor(a ): return jnp.transpose(a , axes=a ) else: raise ValueError(F"""Type not supported for transpose: {type(a )}.""" ) def _a ( a :Optional[Any] , a :Union[str, Any] ) -> Optional[Any]: if is_numpy_array(a ): return np.reshape(a , a ) elif is_torch_tensor(a ): return array.reshape(*a ) elif is_tf_tensor(a ): import tensorflow as tf return tf.reshape(a , a ) elif is_jax_tensor(a ): return jnp.reshape(a , a ) else: raise ValueError(F"""Type not supported for reshape: {type(a )}.""" ) def _a ( a :Optional[Any] , a :Union[str, Any]=None ) -> Any: if is_numpy_array(a ): return np.squeeze(a , axis=a ) elif is_torch_tensor(a ): return array.squeeze() if axis is None else array.squeeze(dim=a ) elif is_tf_tensor(a ): import tensorflow as tf return tf.squeeze(a , axis=a ) elif is_jax_tensor(a ): return jnp.squeeze(a , axis=a ) else: raise ValueError(F"""Type not supported for squeeze: {type(a )}.""" ) def _a ( a :str , a :List[str] ) -> Optional[Any]: if is_numpy_array(a ): return np.expand_dims(a , a ) elif is_torch_tensor(a ): return array.unsqueeze(dim=a ) elif is_tf_tensor(a ): import tensorflow as tf return tf.expand_dims(a , axis=a ) elif is_jax_tensor(a ): return jnp.expand_dims(a , axis=a ) else: raise ValueError(F"""Type not supported for expand_dims: {type(a )}.""" ) def _a ( a :Optional[Any] ) -> Union[str, Any]: if is_numpy_array(a ): return np.size(a ) elif is_torch_tensor(a ): return array.numel() elif is_tf_tensor(a ): import tensorflow as tf return tf.size(a ) elif is_jax_tensor(a ): return array.size else: raise ValueError(F"""Type not supported for expand_dims: {type(a )}.""" ) def _a ( a :List[Any] , a :int ) -> Optional[int]: for key, value in auto_map.items(): if isinstance(a , (tuple, list) ): a = [F"""{repo_id}--{v}""" if (v is not None and '''--''' not in v) else v for v in value] elif value is not None and "--" not in value: a = F"""{repo_id}--{value}""" return auto_map def _a ( a :str ) -> Optional[int]: for base_class in inspect.getmro(a ): a = base_class.__module__ a = base_class.__name__ if module.startswith('''tensorflow''' ) or module.startswith('''keras''' ) or name == "TFPreTrainedModel": return "tf" elif module.startswith('''torch''' ) or name == "PreTrainedModel": return "pt" elif module.startswith('''flax''' ) or module.startswith('''jax''' ) or name == "FlaxPreTrainedModel": return "flax" else: raise TypeError(F"""Could not infer framework from class {model_class}.""" )
26
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import SPIECE_UNDERLINE, logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {"vocab_file": "spiece.model"} UpperCAmelCase__ = { "vocab_file": { "TsinghuaAI/CPM-Generate": "https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model", } } class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Optional[Any] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Optional[Any]=False , __UpperCAmelCase : Any=True , __UpperCAmelCase : Optional[Any]=False , __UpperCAmelCase : List[str]="<s>" , __UpperCAmelCase : int="</s>" , __UpperCAmelCase : Any="<unk>" , __UpperCAmelCase : Optional[Any]="<sep>" , __UpperCAmelCase : int="<pad>" , __UpperCAmelCase : Any="<cls>" , __UpperCAmelCase : List[str]="<mask>" , __UpperCAmelCase : Optional[int]=["<eop>", "<eod>"] , __UpperCAmelCase : Optional[Dict[str, Any]] = None , **__UpperCAmelCase : Union[str, Any] , ) ->None: """simple docstring""" a = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else mask_token a = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__UpperCAmelCase , remove_space=__UpperCAmelCase , keep_accents=__UpperCAmelCase , bos_token=__UpperCAmelCase , eos_token=__UpperCAmelCase , unk_token=__UpperCAmelCase , sep_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , cls_token=__UpperCAmelCase , mask_token=__UpperCAmelCase , additional_special_tokens=__UpperCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **__UpperCAmelCase , ) a = 3 a = do_lower_case a = remove_space a = keep_accents a = vocab_file a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__UpperCAmelCase ) try: import jieba except ModuleNotFoundError as error: raise error.__class__( '''You need to install jieba to use CpmTokenizer or CpmTokenizerFast. ''' '''See https://pypi.org/project/jieba/ for installation.''' ) a = jieba a = str.maketrans(''' \n''' , '''\u2582\u2583''' ) @property # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size def __lowerCAmelCase ( self : Union[str, Any] ) ->Optional[Any]: """simple docstring""" return len(self.sp_model ) def __lowerCAmelCase ( self : Tuple ) ->List[str]: """simple docstring""" a = {self.convert_ids_to_tokens(__UpperCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) ->Optional[int]: """simple docstring""" a = self.__dict__.copy() a = None return state def __setstate__( self : List[str] , __UpperCAmelCase : Optional[int] ) ->str: """simple docstring""" a = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): a = {} a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : List[str] ) ->List[str]: """simple docstring""" if self.remove_space: a = ''' '''.join(inputs.strip().split() ) else: a = inputs a = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: a = unicodedata.normalize('''NFKD''' , __UpperCAmelCase ) a = ''''''.join([c for c in outputs if not unicodedata.combining(__UpperCAmelCase )] ) if self.do_lower_case: a = outputs.lower() return outputs def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : str ) ->List[str]: """simple docstring""" a = self.preprocess_text(__UpperCAmelCase ) a = self.sp_model.encode(__UpperCAmelCase , out_type=__UpperCAmelCase ) a = [] for piece in pieces: if len(__UpperCAmelCase ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): a = self.sp_model.EncodeAsPieces(piece[:-1].replace(__UpperCAmelCase , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: a = cur_pieces[1:] else: a = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(__UpperCAmelCase ) else: new_pieces.append(__UpperCAmelCase ) return new_pieces def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Any ) ->Any: """simple docstring""" return self.sp_model.PieceToId(__UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : Dict ) ->Union[str, Any]: """simple docstring""" return self.sp_model.IdToPiece(__UpperCAmelCase ) def __lowerCAmelCase ( self : Any , __UpperCAmelCase : str ) ->List[str]: """simple docstring""" a = ''''''.join(__UpperCAmelCase ).replace(__UpperCAmelCase , ''' ''' ).strip() return out_string def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None , __UpperCAmelCase : bool = False ) ->List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__UpperCAmelCase , token_ids_a=__UpperCAmelCase , already_has_special_tokens=__UpperCAmelCase ) if token_ids_a is not None: return ([0] * len(__UpperCAmelCase )) + [1] + ([0] * len(__UpperCAmelCase )) + [1, 1] return ([0] * len(__UpperCAmelCase )) + [1, 1] def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None ) ->Tuple[str]: """simple docstring""" if not os.path.isdir(__UpperCAmelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return a = os.path.join( __UpperCAmelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__UpperCAmelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __UpperCAmelCase ) elif not os.path.isfile(self.vocab_file ): with open(__UpperCAmelCase , '''wb''' ) as fi: a = self.sp_model.serialized_model_proto() fi.write(__UpperCAmelCase ) return (out_vocab_file,) def __lowerCAmelCase ( self : Any , *__UpperCAmelCase : List[str] , **__UpperCAmelCase : Optional[Any] ) ->Tuple: """simple docstring""" a = super()._decode(*__UpperCAmelCase , **__UpperCAmelCase ) a = text.replace(''' ''' , '''''' ).replace('''\u2582''' , ''' ''' ).replace('''\u2583''' , '''\n''' ) return text
26
1
from dataclasses import dataclass from typing import Optional import numpy as np import torch import torch.nn as nn from ..utils import BaseOutput, is_torch_version, randn_tensor from .attention_processor import SpatialNorm from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block @dataclass class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = 42 class lowercase_ ( nn.Module ): '''simple docstring''' def __init__( self : str , __UpperCAmelCase : Any=3 , __UpperCAmelCase : Any=3 , __UpperCAmelCase : int=("DownEncoderBlock2D",) , __UpperCAmelCase : Tuple=(64,) , __UpperCAmelCase : Union[str, Any]=2 , __UpperCAmelCase : List[str]=32 , __UpperCAmelCase : Optional[Any]="silu" , __UpperCAmelCase : Union[str, Any]=True , ) ->str: """simple docstring""" super().__init__() a = layers_per_block a = torch.nn.Convad( __UpperCAmelCase , block_out_channels[0] , kernel_size=3 , stride=1 , padding=1 , ) a = None a = nn.ModuleList([] ) # down a = block_out_channels[0] for i, down_block_type in enumerate(__UpperCAmelCase ): a = output_channel a = block_out_channels[i] a = i == len(__UpperCAmelCase ) - 1 a = get_down_block( __UpperCAmelCase , num_layers=self.layers_per_block , in_channels=__UpperCAmelCase , out_channels=__UpperCAmelCase , add_downsample=not is_final_block , resnet_eps=1e-6 , downsample_padding=0 , resnet_act_fn=__UpperCAmelCase , resnet_groups=__UpperCAmelCase , attention_head_dim=__UpperCAmelCase , temb_channels=__UpperCAmelCase , ) self.down_blocks.append(__UpperCAmelCase ) # mid a = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1e-6 , resnet_act_fn=__UpperCAmelCase , output_scale_factor=1 , resnet_time_scale_shift='''default''' , attention_head_dim=block_out_channels[-1] , resnet_groups=__UpperCAmelCase , temb_channels=__UpperCAmelCase , ) # out a = nn.GroupNorm(num_channels=block_out_channels[-1] , num_groups=__UpperCAmelCase , eps=1e-6 ) a = nn.SiLU() a = 2 * out_channels if double_z else out_channels a = nn.Convad(block_out_channels[-1] , __UpperCAmelCase , 3 , padding=1 ) a = False def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : str ) ->Tuple: """simple docstring""" a = x a = self.conv_in(__UpperCAmelCase ) if self.training and self.gradient_checkpointing: def create_custom_forward(__UpperCAmelCase : Union[str, Any] ): def custom_forward(*__UpperCAmelCase : str ): return module(*__UpperCAmelCase ) return custom_forward # down if is_torch_version('''>=''' , '''1.11.0''' ): for down_block in self.down_blocks: a = torch.utils.checkpoint.checkpoint( create_custom_forward(__UpperCAmelCase ) , __UpperCAmelCase , use_reentrant=__UpperCAmelCase ) # middle a = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , __UpperCAmelCase , use_reentrant=__UpperCAmelCase ) else: for down_block in self.down_blocks: a = torch.utils.checkpoint.checkpoint(create_custom_forward(__UpperCAmelCase ) , __UpperCAmelCase ) # middle a = torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block ) , __UpperCAmelCase ) else: # down for down_block in self.down_blocks: a = down_block(__UpperCAmelCase ) # middle a = self.mid_block(__UpperCAmelCase ) # post-process a = self.conv_norm_out(__UpperCAmelCase ) a = self.conv_act(__UpperCAmelCase ) a = self.conv_out(__UpperCAmelCase ) return sample class lowercase_ ( nn.Module ): '''simple docstring''' def __init__( self : Optional[Any] , __UpperCAmelCase : Dict=3 , __UpperCAmelCase : int=3 , __UpperCAmelCase : Optional[int]=("UpDecoderBlock2D",) , __UpperCAmelCase : List[Any]=(64,) , __UpperCAmelCase : Optional[int]=2 , __UpperCAmelCase : List[Any]=32 , __UpperCAmelCase : Any="silu" , __UpperCAmelCase : Union[str, Any]="group" , ) ->List[str]: """simple docstring""" super().__init__() a = layers_per_block a = nn.Convad( __UpperCAmelCase , block_out_channels[-1] , kernel_size=3 , stride=1 , padding=1 , ) a = None a = nn.ModuleList([] ) a = in_channels if norm_type == '''spatial''' else None # mid a = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1e-6 , resnet_act_fn=__UpperCAmelCase , output_scale_factor=1 , resnet_time_scale_shift='''default''' if norm_type == '''group''' else norm_type , attention_head_dim=block_out_channels[-1] , resnet_groups=__UpperCAmelCase , temb_channels=__UpperCAmelCase , ) # up a = list(reversed(__UpperCAmelCase ) ) a = reversed_block_out_channels[0] for i, up_block_type in enumerate(__UpperCAmelCase ): a = output_channel a = reversed_block_out_channels[i] a = i == len(__UpperCAmelCase ) - 1 a = get_up_block( __UpperCAmelCase , num_layers=self.layers_per_block + 1 , in_channels=__UpperCAmelCase , out_channels=__UpperCAmelCase , prev_output_channel=__UpperCAmelCase , add_upsample=not is_final_block , resnet_eps=1e-6 , resnet_act_fn=__UpperCAmelCase , resnet_groups=__UpperCAmelCase , attention_head_dim=__UpperCAmelCase , temb_channels=__UpperCAmelCase , resnet_time_scale_shift=__UpperCAmelCase , ) self.up_blocks.append(__UpperCAmelCase ) a = output_channel # out if norm_type == "spatial": a = SpatialNorm(block_out_channels[0] , __UpperCAmelCase ) else: a = nn.GroupNorm(num_channels=block_out_channels[0] , num_groups=__UpperCAmelCase , eps=1e-6 ) a = nn.SiLU() a = nn.Convad(block_out_channels[0] , __UpperCAmelCase , 3 , padding=1 ) a = False def __lowerCAmelCase ( self : int , __UpperCAmelCase : Dict , __UpperCAmelCase : Tuple=None ) ->Optional[Any]: """simple docstring""" a = z a = self.conv_in(__UpperCAmelCase ) a = next(iter(self.up_blocks.parameters() ) ).dtype if self.training and self.gradient_checkpointing: def create_custom_forward(__UpperCAmelCase : str ): def custom_forward(*__UpperCAmelCase : Optional[int] ): return module(*__UpperCAmelCase ) return custom_forward if is_torch_version('''>=''' , '''1.11.0''' ): # middle a = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , __UpperCAmelCase , __UpperCAmelCase , use_reentrant=__UpperCAmelCase ) a = sample.to(__UpperCAmelCase ) # up for up_block in self.up_blocks: a = torch.utils.checkpoint.checkpoint( create_custom_forward(__UpperCAmelCase ) , __UpperCAmelCase , __UpperCAmelCase , use_reentrant=__UpperCAmelCase ) else: # middle a = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , __UpperCAmelCase , __UpperCAmelCase ) a = sample.to(__UpperCAmelCase ) # up for up_block in self.up_blocks: a = torch.utils.checkpoint.checkpoint(create_custom_forward(__UpperCAmelCase ) , __UpperCAmelCase , __UpperCAmelCase ) else: # middle a = self.mid_block(__UpperCAmelCase , __UpperCAmelCase ) a = sample.to(__UpperCAmelCase ) # up for up_block in self.up_blocks: a = up_block(__UpperCAmelCase , __UpperCAmelCase ) # post-process if latent_embeds is None: a = self.conv_norm_out(__UpperCAmelCase ) else: a = self.conv_norm_out(__UpperCAmelCase , __UpperCAmelCase ) a = self.conv_act(__UpperCAmelCase ) a = self.conv_out(__UpperCAmelCase ) return sample class lowercase_ ( nn.Module ): '''simple docstring''' def __init__( self : Any , __UpperCAmelCase : Dict , __UpperCAmelCase : Dict , __UpperCAmelCase : Dict , __UpperCAmelCase : str=None , __UpperCAmelCase : Optional[int]="random" , __UpperCAmelCase : List[Any]=False , __UpperCAmelCase : Optional[int]=True ) ->Optional[Any]: """simple docstring""" super().__init__() a = n_e a = vq_embed_dim a = beta a = legacy a = nn.Embedding(self.n_e , self.vq_embed_dim ) self.embedding.weight.data.uniform_(-1.0 / self.n_e , 1.0 / self.n_e ) a = remap if self.remap is not None: self.register_buffer('''used''' , torch.tensor(np.load(self.remap ) ) ) a = self.used.shape[0] a = unknown_index # "random" or "extra" or integer if self.unknown_index == "extra": a = self.re_embed a = self.re_embed + 1 print( F"""Remapping {self.n_e} indices to {self.re_embed} indices. """ F"""Using {self.unknown_index} for unknown indices.""" ) else: a = n_e a = sane_index_shape def __lowerCAmelCase ( self : str , __UpperCAmelCase : int ) ->Tuple: """simple docstring""" a = inds.shape assert len(__UpperCAmelCase ) > 1 a = inds.reshape(ishape[0] , -1 ) a = self.used.to(__UpperCAmelCase ) a = (inds[:, :, None] == used[None, None, ...]).long() a = match.argmax(-1 ) a = match.sum(2 ) < 1 if self.unknown_index == "random": a = torch.randint(0 , self.re_embed , size=new[unknown].shape ).to(device=new.device ) else: a = self.unknown_index return new.reshape(__UpperCAmelCase ) def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Optional[int] ) ->Optional[Any]: """simple docstring""" a = inds.shape assert len(__UpperCAmelCase ) > 1 a = inds.reshape(ishape[0] , -1 ) a = self.used.to(__UpperCAmelCase ) if self.re_embed > self.used.shape[0]: # extra token a = 0 # simply set to zero a = torch.gather(used[None, :][inds.shape[0] * [0], :] , 1 , __UpperCAmelCase ) return back.reshape(__UpperCAmelCase ) def __lowerCAmelCase ( self : int , __UpperCAmelCase : Any ) ->Any: """simple docstring""" a = z.permute(0 , 2 , 3 , 1 ).contiguous() a = z.view(-1 , self.vq_embed_dim ) # distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z a = torch.argmin(torch.cdist(__UpperCAmelCase , self.embedding.weight ) , dim=1 ) a = self.embedding(__UpperCAmelCase ).view(z.shape ) a = None a = None # compute loss for embedding if not self.legacy: a = self.beta * torch.mean((z_q.detach() - z) ** 2 ) + torch.mean((z_q - z.detach()) ** 2 ) else: a = torch.mean((z_q.detach() - z) ** 2 ) + self.beta * torch.mean((z_q - z.detach()) ** 2 ) # preserve gradients a = z + (z_q - z).detach() # reshape back to match original input shape a = z_q.permute(0 , 3 , 1 , 2 ).contiguous() if self.remap is not None: a = min_encoding_indices.reshape(z.shape[0] , -1 ) # add batch axis a = self.remap_to_used(__UpperCAmelCase ) a = min_encoding_indices.reshape(-1 , 1 ) # flatten if self.sane_index_shape: a = min_encoding_indices.reshape(z_q.shape[0] , z_q.shape[2] , z_q.shape[3] ) return z_q, loss, (perplexity, min_encodings, min_encoding_indices) def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : List[Any] ) ->List[str]: """simple docstring""" if self.remap is not None: a = indices.reshape(shape[0] , -1 ) # add batch axis a = self.unmap_to_all(__UpperCAmelCase ) a = indices.reshape(-1 ) # flatten again # get quantized latent vectors a = self.embedding(__UpperCAmelCase ) if shape is not None: a = z_q.view(__UpperCAmelCase ) # reshape back to match original input shape a = z_q.permute(0 , 3 , 1 , 2 ).contiguous() return z_q class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : str , __UpperCAmelCase : Any , __UpperCAmelCase : List[str]=False ) ->Optional[int]: """simple docstring""" a = parameters a , a = torch.chunk(__UpperCAmelCase , 2 , dim=1 ) a = torch.clamp(self.logvar , -30.0 , 20.0 ) a = deterministic a = torch.exp(0.5 * self.logvar ) a = torch.exp(self.logvar ) if self.deterministic: a = a = torch.zeros_like( self.mean , device=self.parameters.device , dtype=self.parameters.dtype ) def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : Optional[torch.Generator] = None ) ->torch.FloatTensor: """simple docstring""" a = randn_tensor( self.mean.shape , generator=__UpperCAmelCase , device=self.parameters.device , dtype=self.parameters.dtype ) a = self.mean + self.std * sample return x def __lowerCAmelCase ( self : Any , __UpperCAmelCase : Any=None ) ->int: """simple docstring""" if self.deterministic: return torch.Tensor([0.0] ) else: if other is None: return 0.5 * torch.sum(torch.pow(self.mean , 2 ) + self.var - 1.0 - self.logvar , dim=[1, 2, 3] ) else: return 0.5 * torch.sum( torch.pow(self.mean - other.mean , 2 ) / other.var + self.var / other.var - 1.0 - self.logvar + other.logvar , dim=[1, 2, 3] , ) def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Tuple=[1, 2, 3] ) ->List[Any]: """simple docstring""" if self.deterministic: return torch.Tensor([0.0] ) a = np.log(2.0 * np.pi ) return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean , 2 ) / self.var , dim=__UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple ) ->Optional[Any]: """simple docstring""" return self.mean
26
import argparse import io import requests import torch from omegaconf import OmegaConf from diffusers import AutoencoderKL from diffusers.pipelines.stable_diffusion.convert_from_ckpt import ( assign_to_checkpoint, conv_attn_to_linear, create_vae_diffusers_config, renew_vae_attention_paths, renew_vae_resnet_paths, ) def _a ( a :Union[str, Any] , a :List[Any] ) -> List[Any]: a = checkpoint a = {} a = vae_state_dict['''encoder.conv_in.weight'''] a = vae_state_dict['''encoder.conv_in.bias'''] a = vae_state_dict['''encoder.conv_out.weight'''] a = vae_state_dict['''encoder.conv_out.bias'''] a = vae_state_dict['''encoder.norm_out.weight'''] a = vae_state_dict['''encoder.norm_out.bias'''] a = vae_state_dict['''decoder.conv_in.weight'''] a = vae_state_dict['''decoder.conv_in.bias'''] a = vae_state_dict['''decoder.conv_out.weight'''] a = vae_state_dict['''decoder.conv_out.bias'''] a = vae_state_dict['''decoder.norm_out.weight'''] a = vae_state_dict['''decoder.norm_out.bias'''] a = vae_state_dict['''quant_conv.weight'''] a = vae_state_dict['''quant_conv.bias'''] a = vae_state_dict['''post_quant_conv.weight'''] a = vae_state_dict['''post_quant_conv.bias'''] # Retrieves the keys for the encoder down blocks only a = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''encoder.down''' in layer} ) a = { layer_id: [key for key in vae_state_dict if F"""down.{layer_id}""" in key] for layer_id in range(a ) } # Retrieves the keys for the decoder up blocks only a = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''decoder.up''' in layer} ) a = { layer_id: [key for key in vae_state_dict if F"""up.{layer_id}""" in key] for layer_id in range(a ) } for i in range(a ): a = [key for key in down_blocks[i] if F"""down.{i}""" in key and F"""down.{i}.downsample""" not in key] if F"""encoder.down.{i}.downsample.conv.weight""" in vae_state_dict: a = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.weight""" ) a = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.bias""" ) a = renew_vae_resnet_paths(a ) a = {'''old''': F"""down.{i}.block""", '''new''': F"""down_blocks.{i}.resnets"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''encoder.mid.block''' in key] a = 2 for i in range(1 , num_mid_res_blocks + 1 ): a = [key for key in mid_resnets if F"""encoder.mid.block_{i}""" in key] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""mid.block_{i}""", '''new''': F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''encoder.mid.attn''' in key] a = renew_vae_attention_paths(a ) a = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) conv_attn_to_linear(a ) for i in range(a ): a = num_up_blocks - 1 - i a = [ key for key in up_blocks[block_id] if F"""up.{block_id}""" in key and F"""up.{block_id}.upsample""" not in key ] if F"""decoder.up.{block_id}.upsample.conv.weight""" in vae_state_dict: a = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.weight""" ] a = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.bias""" ] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""up.{block_id}.block""", '''new''': F"""up_blocks.{i}.resnets"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''decoder.mid.block''' in key] a = 2 for i in range(1 , num_mid_res_blocks + 1 ): a = [key for key in mid_resnets if F"""decoder.mid.block_{i}""" in key] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""mid.block_{i}""", '''new''': F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''decoder.mid.attn''' in key] a = renew_vae_attention_paths(a ) a = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) conv_attn_to_linear(a ) return new_checkpoint def _a ( a :str , a :str , ) -> List[str]: # Only support V1 a = requests.get( ''' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml''' ) a = io.BytesIO(r.content ) a = OmegaConf.load(a ) a = 512 a = '''cuda''' if torch.cuda.is_available() else '''cpu''' if checkpoint_path.endswith('''safetensors''' ): from safetensors import safe_open a = {} with safe_open(a , framework='''pt''' , device='''cpu''' ) as f: for key in f.keys(): a = f.get_tensor(a ) else: a = torch.load(a , map_location=a )['''state_dict'''] # Convert the VAE model. a = create_vae_diffusers_config(a , image_size=a ) a = custom_convert_ldm_vae_checkpoint(a , a ) a = AutoencoderKL(**a ) vae.load_state_dict(a ) vae.save_pretrained(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--vae_pt_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") UpperCAmelCase__ = parser.parse_args() vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)
26
1
# Logistic Regression from scratch # In[62]: # In[63]: # importing all the required libraries import numpy as np from matplotlib import pyplot as plt from sklearn import datasets def _a ( a :str ) -> Tuple: return 1 / (1 + np.exp(-z )) def _a ( a :int , a :Any ) -> Tuple: return (-y * np.log(a ) - (1 - y) * np.log(1 - h )).mean() def _a ( a :List[str] , a :Optional[Any] , a :int ) -> List[Any]: a = np.dot(a , a ) return np.sum(y * scores - np.log(1 + np.exp(a ) ) ) def _a ( a :Union[str, Any] , a :int , a :Optional[Any] , a :Tuple=70_000 ) -> Any: a = np.zeros(x.shape[1] ) for iterations in range(a ): a = np.dot(a , a ) a = sigmoid_function(a ) a = np.dot(x.T , h - y ) / y.size a = theta - alpha * gradient # updating the weights a = np.dot(a , a ) a = sigmoid_function(a ) a = cost_function(a , a ) if iterations % 100 == 0: print(F"""loss: {j} \t""" ) # printing the loss after every 100 iterations return theta # In[68]: if __name__ == "__main__": UpperCAmelCase__ = datasets.load_iris() UpperCAmelCase__ = iris.data[:, :2] UpperCAmelCase__ = (iris.target != 0) * 1 UpperCAmelCase__ = 0.1 UpperCAmelCase__ = logistic_reg(alpha, x, y, max_iterations=70000) print("theta: ", theta) # printing the theta i.e our weights vector def _a ( a :Dict ) -> Tuple: return sigmoid_function( np.dot(a , a ) ) # predicting the value of probability from the logistic regression algorithm plt.figure(figsize=(10, 6)) plt.scatter(x[y == 0][:, 0], x[y == 0][:, 1], color="b", label="0") plt.scatter(x[y == 1][:, 0], x[y == 1][:, 1], color="r", label="1") ((UpperCAmelCase__) , (UpperCAmelCase__)) = (x[:, 0].min(), x[:, 0].max()) ((UpperCAmelCase__) , (UpperCAmelCase__)) = (x[:, 1].min(), x[:, 1].max()) ((UpperCAmelCase__) , (UpperCAmelCase__)) = np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max)) UpperCAmelCase__ = np.c_[xxa.ravel(), xxa.ravel()] UpperCAmelCase__ = predict_prob(grid).reshape(xxa.shape) plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors="black") plt.legend() plt.show()
26
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = ['''image_processor''', '''tokenizer'''] __snake_case = '''CLIPImageProcessor''' __snake_case = ('''CLIPTokenizer''', '''CLIPTokenizerFast''') def __init__( self : Dict , __UpperCAmelCase : str=None , __UpperCAmelCase : Optional[Any]=None , **__UpperCAmelCase : Optional[Any] ) ->List[str]: """simple docstring""" a = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __UpperCAmelCase , ) a = kwargs.pop('''feature_extractor''' ) a = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(__UpperCAmelCase , __UpperCAmelCase ) def __call__( self : List[str] , __UpperCAmelCase : Any=None , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Any=None , **__UpperCAmelCase : str ) ->Optional[Any]: """simple docstring""" if text is None and images is None: raise ValueError('''You have to specify either text or images. Both cannot be none.''' ) if text is not None: a = self.tokenizer(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase ) if images is not None: a = self.image_processor(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase ) if text is not None and images is not None: a = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__UpperCAmelCase ) , tensor_type=__UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple , *__UpperCAmelCase : List[str] , **__UpperCAmelCase : Union[str, Any] ) ->Union[str, Any]: """simple docstring""" return self.tokenizer.batch_decode(*__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple , *__UpperCAmelCase : str , **__UpperCAmelCase : Tuple ) ->Any: """simple docstring""" return self.tokenizer.decode(*__UpperCAmelCase , **__UpperCAmelCase ) @property def __lowerCAmelCase ( self : int ) ->List[str]: """simple docstring""" a = self.tokenizer.model_input_names a = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def __lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __UpperCAmelCase , ) return self.image_processor_class @property def __lowerCAmelCase ( self : Union[str, Any] ) ->Optional[int]: """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __UpperCAmelCase , ) return self.image_processor
26
1
def _a ( a :str ) -> list[int]: a = [0 for i in range(len(a ) )] # initialize interval's left pointer and right pointer a , a = 0, 0 for i in range(1 , len(a ) ): # case when current index is inside the interval if i <= right_pointer: a = min(right_pointer - i + 1 , z_result[i - left_pointer] ) a = min_edge while go_next(a , a , a ): z_result[i] += 1 # if new index's result gives us more right interval, # we've to update left_pointer and right_pointer if i + z_result[i] - 1 > right_pointer: a , a = i, i + z_result[i] - 1 return z_result def _a ( a :int , a :list[int] , a :str ) -> bool: return i + z_result[i] < len(a ) and s[z_result[i]] == s[i + z_result[i]] def _a ( a :str , a :str ) -> int: a = 0 # concatenate 'pattern' and 'input_str' and call z_function # with concatenated string a = z_function(pattern + input_str ) for val in z_result: # if value is greater then length of the pattern string # that means this index is starting position of substring # which is equal to pattern string if val >= len(a ): answer += 1 return answer if __name__ == "__main__": import doctest doctest.testmod()
26
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_distilbert import DistilBertTokenizer UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} UpperCAmelCase__ = { "vocab_file": { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt" ), "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt", "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt" ), }, "tokenizer_file": { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json" ), "distilbert-base-german-cased": ( "https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json" ), "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json" ), }, } UpperCAmelCase__ = { "distilbert-base-uncased": 512, "distilbert-base-uncased-distilled-squad": 512, "distilbert-base-cased": 512, "distilbert-base-cased-distilled-squad": 512, "distilbert-base-german-cased": 512, "distilbert-base-multilingual-cased": 512, } UpperCAmelCase__ = { "distilbert-base-uncased": {"do_lower_case": True}, "distilbert-base-uncased-distilled-squad": {"do_lower_case": True}, "distilbert-base-cased": {"do_lower_case": False}, "distilbert-base-cased-distilled-squad": {"do_lower_case": False}, "distilbert-base-german-cased": {"do_lower_case": False}, "distilbert-base-multilingual-cased": {"do_lower_case": False}, } class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = VOCAB_FILES_NAMES __snake_case = PRETRAINED_VOCAB_FILES_MAP __snake_case = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __snake_case = PRETRAINED_INIT_CONFIGURATION __snake_case = ['''input_ids''', '''attention_mask'''] __snake_case = DistilBertTokenizer def __init__( self : Dict , __UpperCAmelCase : List[Any]=None , __UpperCAmelCase : Optional[int]=None , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Optional[int]="[UNK]" , __UpperCAmelCase : str="[SEP]" , __UpperCAmelCase : Tuple="[PAD]" , __UpperCAmelCase : Any="[CLS]" , __UpperCAmelCase : int="[MASK]" , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Optional[Any]=None , **__UpperCAmelCase : str , ) ->Optional[int]: """simple docstring""" super().__init__( __UpperCAmelCase , tokenizer_file=__UpperCAmelCase , do_lower_case=__UpperCAmelCase , unk_token=__UpperCAmelCase , sep_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , cls_token=__UpperCAmelCase , mask_token=__UpperCAmelCase , tokenize_chinese_chars=__UpperCAmelCase , strip_accents=__UpperCAmelCase , **__UpperCAmelCase , ) a = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , __UpperCAmelCase ) != do_lower_case or normalizer_state.get('''strip_accents''' , __UpperCAmelCase ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , __UpperCAmelCase ) != tokenize_chinese_chars ): a = getattr(__UpperCAmelCase , normalizer_state.pop('''type''' ) ) a = do_lower_case a = strip_accents a = tokenize_chinese_chars a = normalizer_class(**__UpperCAmelCase ) a = do_lower_case def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[int]=None ) ->Optional[Any]: """simple docstring""" a = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [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 __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None ) ->Tuple[str]: """simple docstring""" a = self._tokenizer.model.save(__UpperCAmelCase , name=__UpperCAmelCase ) return tuple(__UpperCAmelCase )
26
1
import collections import json import math import os import re import time from fnmatch import fnmatch from typing import Dict import requests from slack_sdk import WebClient UpperCAmelCase__ = WebClient(token=os.environ["CI_SLACK_BOT_TOKEN"]) def _a ( a :Dict ) -> Optional[Any]: a = test_results.split(''' ''' ) a = 0 a = 0 # When the output is short enough, the output is surrounded by = signs: "== OUTPUT ==" # When it is too long, those signs are not present. a = expressions[-2] if '''=''' in expressions[-1] else expressions[-1] for i, expression in enumerate(a ): if "failed" in expression: failed += int(expressions[i - 1] ) if "passed" in expression: success += int(expressions[i - 1] ) return failed, success, time_spent def _a ( a :Optional[int] ) -> List[str]: a = {} a = None a = False for line in failures_short_lines.split('''\n''' ): if re.search(r'''_ \[doctest\]''' , a ): a = True a = line.split(''' ''' )[2] elif in_error and not line.split(''' ''' )[0].isdigit(): a = line a = False return failures class lowercase_ : '''simple docstring''' def __init__( self : Tuple , __UpperCAmelCase : str , __UpperCAmelCase : Dict ) ->List[Any]: """simple docstring""" a = title a = doc_test_results['''time_spent'''].split(''',''' )[0] a = doc_test_results['''success'''] a = doc_test_results['''failures'''] a = self.n_success + self.n_failures # Failures and success of the modeling tests a = doc_test_results @property def __lowerCAmelCase ( self : Dict ) ->str: """simple docstring""" a = [self._time_spent] a = 0 for time in time_spent: a = time.split(''':''' ) # Time can be formatted as xx:xx:xx, as .xx, or as x.xx if the time spent was less than a minute. if len(__UpperCAmelCase ) == 1: a = [0, 0, time_parts[0]] a , a , a = int(time_parts[0] ), int(time_parts[1] ), float(time_parts[2] ) total_secs += hours * 3_600 + minutes * 60 + seconds a , a , a = total_secs // 3_600, (total_secs % 3_600) // 60, total_secs % 60 return F"""{int(__UpperCAmelCase )}h{int(__UpperCAmelCase )}m{int(__UpperCAmelCase )}s""" @property def __lowerCAmelCase ( self : str ) ->Dict: """simple docstring""" return {"type": "header", "text": {"type": "plain_text", "text": self.title}} @property def __lowerCAmelCase ( self : Optional[int] ) ->Dict: """simple docstring""" return { "type": "section", "text": { "type": "plain_text", "text": F"""🌞 There were no failures: all {self.n_tests} tests passed. The suite ran in {self.time}.""", "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": F"""https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}""", }, } @property def __lowerCAmelCase ( self : Dict ) ->Dict: """simple docstring""" return { "type": "section", "text": { "type": "plain_text", "text": ( F"""There were {self.n_failures} failures, out of {self.n_tests} tests.\nThe suite ran in""" F""" {self.time}.""" ), "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": F"""https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}""", }, } @property def __lowerCAmelCase ( self : Union[str, Any] ) ->Dict: """simple docstring""" a = 40 a = {k: v['''failed'''] for k, v in doc_test_results.items() if isinstance(__UpperCAmelCase , __UpperCAmelCase )} a = '''''' for category, failures in category_failures.items(): if len(__UpperCAmelCase ) == 0: continue if report != "": report += "\n\n" report += F"""*{category} failures*:""".ljust(line_length // 2 ).rjust(line_length // 2 ) + "\n" report += "`" report += "`\n`".join(__UpperCAmelCase ) report += "`" return { "type": "section", "text": { "type": "mrkdwn", "text": F"""The following examples had failures:\n\n\n{report}\n""", }, } @property def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = [self.header] if self.n_failures > 0: blocks.append(self.failures ) if self.n_failures > 0: blocks.extend([self.category_failures] ) if self.n_failures == 0: blocks.append(self.no_failures ) return json.dumps(__UpperCAmelCase ) @staticmethod def __lowerCAmelCase ( ) ->Tuple: """simple docstring""" a = [ { '''type''': '''section''', '''text''': { '''type''': '''plain_text''', '''text''': '''There was an issue running the tests.''', }, '''accessory''': { '''type''': '''button''', '''text''': {'''type''': '''plain_text''', '''text''': '''Check Action results''', '''emoji''': True}, '''url''': F"""https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}""", }, } ] print('''Sending the following payload''' ) print(json.dumps({'''blocks''': json.loads(__UpperCAmelCase )} ) ) client.chat_postMessage( channel=os.environ['''CI_SLACK_CHANNEL_ID_DAILY'''] , text='''There was an issue running the tests.''' , blocks=__UpperCAmelCase , ) def __lowerCAmelCase ( self : Optional[Any] ) ->Any: """simple docstring""" print('''Sending the following payload''' ) print(json.dumps({'''blocks''': json.loads(self.payload )} ) ) a = F"""{self.n_failures} failures out of {self.n_tests} tests,""" if self.n_failures else '''All tests passed.''' a = client.chat_postMessage( channel=os.environ['''CI_SLACK_CHANNEL_ID_DAILY'''] , blocks=self.payload , text=__UpperCAmelCase , ) def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : str , __UpperCAmelCase : Tuple , __UpperCAmelCase : Any , __UpperCAmelCase : List[str] ) ->Optional[int]: """simple docstring""" a = '''''' for key, value in failures.items(): a = value[:200] + ''' [Truncated]''' if len(__UpperCAmelCase ) > 250 else value failures_text += F"""*{key}*\n_{value}_\n\n""" a = job_name a = {'''type''': '''section''', '''text''': {'''type''': '''mrkdwn''', '''text''': text}} if job_link is not None: a = { '''type''': '''button''', '''text''': {'''type''': '''plain_text''', '''text''': '''GitHub Action job''', '''emoji''': True}, '''url''': job_link, } return [ {"type": "header", "text": {"type": "plain_text", "text": title.upper(), "emoji": True}}, content, {"type": "section", "text": {"type": "mrkdwn", "text": failures_text}}, ] def __lowerCAmelCase ( self : int ) ->Dict: """simple docstring""" if self.thread_ts is None: raise ValueError('''Can only post reply if a post has been made.''' ) a = self.doc_test_results.pop('''job_link''' ) self.doc_test_results.pop('''failures''' ) self.doc_test_results.pop('''success''' ) self.doc_test_results.pop('''time_spent''' ) a = sorted(self.doc_test_results.items() , key=lambda __UpperCAmelCase : t[0] ) for job, job_result in sorted_dict: if len(job_result['''failures'''] ): a = F"""*Num failures* :{len(job_result['failed'] )} \n""" a = job_result['''failures'''] a = self.get_reply_blocks(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , text=__UpperCAmelCase ) print('''Sending the following reply''' ) print(json.dumps({'''blocks''': blocks} ) ) client.chat_postMessage( channel=os.environ['''CI_SLACK_CHANNEL_ID_DAILY'''] , text=F"""Results for {job}""" , blocks=__UpperCAmelCase , thread_ts=self.thread_ts['''ts'''] , ) time.sleep(1 ) def _a ( ) -> Tuple: a = os.environ['''GITHUB_RUN_ID'''] a = F"""https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100""" a = requests.get(a ).json() a = {} try: jobs.update({job['''name''']: job['''html_url'''] for job in result['''jobs''']} ) a = math.ceil((result['''total_count'''] - 100) / 100 ) for i in range(a ): a = requests.get(url + F"""&page={i + 2}""" ).json() jobs.update({job['''name''']: job['''html_url'''] for job in result['''jobs''']} ) return jobs except Exception as e: print('''Unknown error, could not fetch links.''' , a ) return {} def _a ( a :str ) -> Dict: a = {} if os.path.exists(a ): a = os.listdir(a ) for file in files: try: with open(os.path.join(a , a ) , encoding='''utf-8''' ) as f: a = f.read() except UnicodeDecodeError as e: raise ValueError(F"""Could not open {os.path.join(a , a )}.""" ) from e return _artifact def _a ( ) -> int: class lowercase_ : '''simple docstring''' def __init__( self : Optional[int] , __UpperCAmelCase : str ) ->List[Any]: """simple docstring""" a = name a = [] def __str__( self : int ) ->Optional[int]: """simple docstring""" return self.name def __lowerCAmelCase ( self : Any , __UpperCAmelCase : str ) ->int: """simple docstring""" self.paths.append({'''name''': self.name, '''path''': path} ) a = {} a = filter(os.path.isdir , os.listdir() ) for directory in directories: a = directory if artifact_name not in _available_artifacts: a = Artifact(a ) _available_artifacts[artifact_name].add_path(a ) return _available_artifacts if __name__ == "__main__": UpperCAmelCase__ = get_job_links() UpperCAmelCase__ = retrieve_available_artifacts() UpperCAmelCase__ = collections.OrderedDict( [ ("*.py", "API Examples"), ("*.md", "MD Examples"), ] ) # This dict will contain all the information relative to each doc test category: # - failed: list of failed tests # - failures: dict in the format 'test': 'error_message' UpperCAmelCase__ = { v: { "failed": [], "failures": {}, } for v in docs.values() } # Link to the GitHub Action job UpperCAmelCase__ = github_actions_job_links.get("run_doctests") UpperCAmelCase__ = available_artifacts["doc_tests_gpu_test_reports"].paths[0] UpperCAmelCase__ = retrieve_artifact(artifact_path["name"]) if "stats" in artifact: UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = handle_test_results(artifact["stats"]) UpperCAmelCase__ = failed UpperCAmelCase__ = success UpperCAmelCase__ = time_spent[1:-1] + ", " UpperCAmelCase__ = extract_first_line_failure(artifact["failures_short"]) for line in artifact["summary_short"].split("\n"): if re.search("FAILED", line): UpperCAmelCase__ = line.replace("FAILED ", "") UpperCAmelCase__ = line.split()[0].replace("\n", "") if "::" in line: UpperCAmelCase__ , UpperCAmelCase__ = line.split("::") else: UpperCAmelCase__ , UpperCAmelCase__ = line, line for file_regex in docs.keys(): if fnmatch(file_path, file_regex): UpperCAmelCase__ = docs[file_regex] doc_test_results[category]["failed"].append(test) UpperCAmelCase__ = all_failures[test] if test in all_failures else "N/A" UpperCAmelCase__ = failure break UpperCAmelCase__ = Message("🤗 Results of the doc tests.", doc_test_results) message.post() message.post_reply()
26
from __future__ import annotations import typing from collections import Counter def _a ( a :int ) -> typing.Counter[int]: a = Counter() for base in range(1 , max_perimeter + 1 ): for perpendicular in range(a , max_perimeter + 1 ): a = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(a ): a = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def _a ( a :int = 1_000 ) -> int: a = pythagorean_triple(a ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(f"""Perimeter {solution()} has maximum solutions""")
26
1
def _a ( a :int , a :list ) -> List[str]: _enforce_args(a , a ) if n == 0: return 0 a = float('''-inf''' ) for i in range(1 , n + 1 ): a = max( a , prices[i - 1] + naive_cut_rod_recursive(n - i , a ) ) return max_revue def _a ( a :int , a :list ) -> int: _enforce_args(a , a ) a = [float('''-inf''' ) for _ in range(n + 1 )] return _top_down_cut_rod_recursive(a , a , a ) def _a ( a :int , a :list , a :list ) -> int: if max_rev[n] >= 0: return max_rev[n] elif n == 0: return 0 else: a = float('''-inf''' ) for i in range(1 , n + 1 ): a = max( a , prices[i - 1] + _top_down_cut_rod_recursive(n - i , a , a ) , ) a = max_revenue return max_rev[n] def _a ( a :int , a :list ) -> str: _enforce_args(a , a ) # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of # length 0. a = [float('''-inf''' ) for _ in range(n + 1 )] a = 0 for i in range(1 , n + 1 ): a = max_rev[i] for j in range(1 , i + 1 ): a = max(a , prices[j - 1] + max_rev[i - j] ) a = max_revenue_i return max_rev[n] def _a ( a :int , a :list ) -> List[Any]: if n < 0: a = F"""n must be greater than or equal to 0. Got n = {n}""" raise ValueError(a ) if n > len(a ): a = ( '''Each integral piece of rod must have a corresponding price. ''' F"""Got n = {n} but length of prices = {len(a )}""" ) raise ValueError(a ) def _a ( ) -> int: a = [6, 10, 12, 15, 20, 23] a = len(a ) # the best revenue comes from cutting the rod into 6 pieces, each # of length 1 resulting in a revenue of 6 * 6 = 36. a = 36 a = top_down_cut_rod(a , a ) a = bottom_up_cut_rod(a , a ) a = naive_cut_rod_recursive(a , a ) assert expected_max_revenue == max_rev_top_down assert max_rev_top_down == max_rev_bottom_up assert max_rev_bottom_up == max_rev_naive if __name__ == "__main__": main()
26
from __future__ import annotations def _a ( a :dict , a :str ) -> set[str]: a , a = set(a ), [start] while stack: a = stack.pop() explored.add(a ) # 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(a ) return explored UpperCAmelCase__ = { "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"))
26
1
# Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position UpperCAmelCase__ = "2.13.1" import platform import pyarrow from packaging import version if version.parse(platform.python_version()) < version.parse("3.7"): raise ImportWarning( "To use `datasets`, Python>=3.7 is required, and the current version of Python doesn't match this condition." ) if version.parse(pyarrow.__version__).major < 8: raise ImportWarning( "To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn't match this condition.\n" "If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`." ) del platform del pyarrow del version from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip UpperCAmelCase__ = concatenate_datasets UpperCAmelCase__ = DownloadConfig UpperCAmelCase__ = DownloadManager UpperCAmelCase__ = DownloadMode UpperCAmelCase__ = DownloadConfig UpperCAmelCase__ = DownloadMode UpperCAmelCase__ = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
26
import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm UpperCAmelCase__ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex UpperCAmelCase__ = 10 UpperCAmelCase__ = 256 def _a ( a :List[str] ) -> Optional[MinHash]: if len(a ) < MIN_NUM_TOKENS: return None a = MinHash(num_perm=a ) for token in set(a ): min_hash.update(token.encode() ) return min_hash def _a ( a :str ) -> Set[str]: return {t for t in NON_ALPHA.split(a ) if len(t.strip() ) > 0} class lowercase_ : '''simple docstring''' def __init__( self : Any , *, __UpperCAmelCase : float = 0.85 , ) ->Dict: """simple docstring""" a = duplication_jaccard_threshold a = NUM_PERM a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) a = defaultdict(__UpperCAmelCase ) def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Tuple , __UpperCAmelCase : MinHash ) ->None: """simple docstring""" a = self._index.query(__UpperCAmelCase ) if code_key in self._index.keys: print(F"""Duplicate key {code_key}""" ) return self._index.insert(__UpperCAmelCase , __UpperCAmelCase ) if len(__UpperCAmelCase ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(__UpperCAmelCase ) break else: self._duplicate_clusters[close_duplicates[0]].add(__UpperCAmelCase ) def __lowerCAmelCase ( self : Dict ) ->List[List[Dict]]: """simple docstring""" a = [] for base, duplicates in self._duplicate_clusters.items(): a = [base] + list(__UpperCAmelCase ) # reformat the cluster to be a list of dict a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(__UpperCAmelCase ) return duplicate_clusters def __lowerCAmelCase ( self : Any , __UpperCAmelCase : Dict ) ->None: """simple docstring""" a = self.get_duplicate_clusters() with open(__UpperCAmelCase , '''w''' ) as f: json.dump(__UpperCAmelCase , __UpperCAmelCase ) def _a ( a :List[Any] ) -> List[Any]: a , a = element a = get_min_hash([t for t in NON_ALPHA.split(data['''content'''] ) if len(t.strip() ) > 0] ) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def _a ( a :Type[Dataset] ) -> List[Any]: with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(a , max_queue_size=10_000 ) , chunksize=100 , ): if data is not None: yield data def _a ( a :Type[Dataset] , a :float ) -> str: a = DuplicationIndex(duplication_jaccard_threshold=a ) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(a ) ) , max_queue_size=100 ) ): di.add(a , a ) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def _a ( a :str , a :str ) -> float: a = get_tokens(a ) a = get_tokens(a ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) UpperCAmelCase__ = None def _a ( a :Tuple , a :Tuple ) -> Any: a = [] for elementa in cluster: a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(a , a ) >= jaccard_threshold: elementa["copies"] += 1 break else: a = 1 extremes.append(a ) return extremes def _a ( a :List[Any] , a :Optional[Any] , a :Union[str, Any] ) -> Optional[int]: global _shared_dataset a = dataset a = [] a = partial(_find_cluster_extremes_shared , jaccard_threshold=a ) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( a , a , ) , total=len(a ) , ): extremes_list.append(a ) return extremes_list def _a ( a :Type[Dataset] , a :float = 0.85 ) -> Tuple[Type[Dataset], List[List[Dict]]]: a = make_duplicate_clusters(a , a ) a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} a = {} a = find_extremes(a , a , a ) for extremes in extremes_clusters: for element in extremes: a = element a = duplicate_indices - set(extreme_dict.keys() ) a = dataset.filter(lambda a , a : idx not in remove_indices , with_indices=a ) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: a = element['''base_index'''] in extreme_dict if element["is_extreme"]: a = extreme_dict[element['''base_index''']]['''copies'''] print(F"""Original dataset size: {len(a )}""" ) print(F"""Number of duplicate clusters: {len(a )}""" ) print(F"""Files in duplicate cluster: {len(a )}""" ) print(F"""Unique files in duplicate cluster: {len(a )}""" ) print(F"""Filtered dataset size: {len(a )}""" ) return ds_filter, duplicate_clusters
26
1
import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( "The `image_to_image.py` script is outdated. Please use directly `from diffusers import" " StableDiffusionImg2ImgPipeline` instead." )
26
from math import ceil, sqrt def _a ( a :int = 1_000_000 ) -> int: a = 0 for outer_width in range(3 , (limit // 4) + 2 ): if outer_width**2 > limit: a = max(ceil(sqrt(outer_width**2 - limit ) ) , 1 ) else: a = 1 if (outer_width - hole_width_lower_bound) % 2: hole_width_lower_bound += 1 answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 return answer if __name__ == "__main__": print(f"""{solution() = }""")
26
1
from __future__ import annotations import unittest from transformers import AutoTokenizer, PegasusConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, 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, TFPegasusForConditionalGeneration, TFPegasusModel @require_tf class lowercase_ : '''simple docstring''' __snake_case = PegasusConfig __snake_case = {} __snake_case = '''gelu''' def __init__( self : Optional[int] , __UpperCAmelCase : List[str] , __UpperCAmelCase : List[str]=13 , __UpperCAmelCase : List[Any]=7 , __UpperCAmelCase : Union[str, Any]=True , __UpperCAmelCase : List[Any]=False , __UpperCAmelCase : str=99 , __UpperCAmelCase : List[Any]=32 , __UpperCAmelCase : Optional[Any]=2 , __UpperCAmelCase : Any=4 , __UpperCAmelCase : Tuple=37 , __UpperCAmelCase : str=0.1 , __UpperCAmelCase : Optional[Any]=0.1 , __UpperCAmelCase : List[Any]=40 , __UpperCAmelCase : List[str]=2 , __UpperCAmelCase : int=1 , __UpperCAmelCase : List[Any]=0 , ) ->Union[str, Any]: """simple docstring""" a = parent a = batch_size a = seq_length a = is_training a = use_labels a = vocab_size a = hidden_size a = num_hidden_layers a = num_attention_heads a = intermediate_size a = hidden_dropout_prob a = attention_probs_dropout_prob a = max_position_embeddings a = eos_token_id a = pad_token_id a = bos_token_id def __lowerCAmelCase ( self : int ) ->Union[str, Any]: """simple docstring""" a = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) a = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) a = tf.concat([input_ids, eos_tensor] , axis=1 ) a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) a = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) a = prepare_pegasus_inputs_dict(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) return config, inputs_dict def __lowerCAmelCase ( self : int , __UpperCAmelCase : List[Any] , __UpperCAmelCase : Optional[int] ) ->List[Any]: """simple docstring""" a = TFPegasusModel(config=__UpperCAmelCase ).get_decoder() a = inputs_dict['''input_ids'''] a = input_ids[:1, :] a = inputs_dict['''attention_mask'''][:1, :] a = inputs_dict['''head_mask'''] a = 1 # first forward pass a = model(__UpperCAmelCase , attention_mask=__UpperCAmelCase , head_mask=__UpperCAmelCase , use_cache=__UpperCAmelCase ) a , a = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids a = ids_tensor((self.batch_size, 3) , config.vocab_size ) a = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and a = tf.concat([input_ids, next_tokens] , axis=-1 ) a = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) a = model(__UpperCAmelCase , attention_mask=__UpperCAmelCase )[0] a = model(__UpperCAmelCase , attention_mask=__UpperCAmelCase , past_key_values=__UpperCAmelCase )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice a = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) a = output_from_no_past[:, -3:, random_slice_idx] a = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(__UpperCAmelCase , __UpperCAmelCase , rtol=1e-3 ) def _a ( a :Any , a :Optional[Any] , a :int , a :Tuple=None , a :Optional[int]=None , a :List[Any]=None , a :int=None , a :Any=None , ) -> Union[str, Any]: if attention_mask is None: a = tf.cast(tf.math.not_equal(a , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: a = 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: a = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: a = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: a = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class lowercase_ ( lowercase , lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = (TFPegasusForConditionalGeneration, TFPegasusModel) if is_tf_available() else () __snake_case = (TFPegasusForConditionalGeneration,) if is_tf_available() else () __snake_case = ( { '''conversational''': TFPegasusForConditionalGeneration, '''feature-extraction''': TFPegasusModel, '''summarization''': TFPegasusForConditionalGeneration, '''text2text-generation''': TFPegasusForConditionalGeneration, '''translation''': TFPegasusForConditionalGeneration, } if is_tf_available() else {} ) __snake_case = True __snake_case = False __snake_case = False def __lowerCAmelCase ( self : List[str] ) ->Union[str, Any]: """simple docstring""" a = TFPegasusModelTester(self ) a = ConfigTester(self , config_class=__UpperCAmelCase ) def __lowerCAmelCase ( self : Any ) ->List[Any]: """simple docstring""" self.config_tester.run_common_tests() def __lowerCAmelCase ( self : str ) ->str: """simple docstring""" a = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*__UpperCAmelCase ) @require_sentencepiece @require_tokenizers @require_tf class lowercase_ ( unittest.TestCase ): '''simple docstring''' __snake_case = [ ''' PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow.''', ''' The London trio are up for best UK act and best album, as well as getting two nominations in the best song category."We got told like this morning \'Oh I think you\'re nominated\'", said Dappy."And I was like \'Oh yeah, which one?\' And now we\'ve got nominated for four awards. I mean, wow!"Bandmate Fazer added: "We thought it\'s best of us to come down and mingle with everyone and say hello to the cameras. And now we find we\'ve got four nominations."The band have two shots at the best song prize, getting the nod for their Tynchy Stryder collaboration Number One, and single Strong Again.Their album Uncle B will also go up against records by the likes of Beyonce and Kanye West.N-Dubz picked up the best newcomer Mobo in 2007, but female member Tulisa said they wouldn\'t be too disappointed if they didn\'t win this time around."At the end of the day we\'re grateful to be where we are in our careers."If it don\'t happen then it don\'t happen - live to fight another day and keep on making albums and hits for the fans."Dappy also revealed they could be performing live several times on the night.The group will be doing Number One and also a possible rendition of the War Child single, I Got Soul.The charity song is a re-working of The Killers\' All These Things That I\'ve Done and is set to feature artists like Chipmunk, Ironik and Pixie Lott.This year\'s Mobos will be held outside of London for the first time, in Glasgow on 30 September.N-Dubz said they were looking forward to performing for their Scottish fans and boasted about their recent shows north of the border."We just done Edinburgh the other day," said Dappy."We smashed up an N-Dubz show over there. We done Aberdeen about three or four months ago - we smashed up that show over there! Everywhere we go we smash it up!" ''', ] __snake_case = [ '''California\'s largest electricity provider has cut power to hundreds of thousands of customers in an effort to''' ''' reduce the risk of wildfires.''', '''N-Dubz have revealed they\'re "grateful" to have been nominated for four Mobo Awards.''', ] # differs slightly from pytorch, likely due to numerical differences in linear layers __snake_case = '''google/pegasus-xsum''' @cached_property def __lowerCAmelCase ( self : Optional[int] ) ->str: """simple docstring""" return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def __lowerCAmelCase ( self : Union[str, Any] ) ->Dict: """simple docstring""" a = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def __lowerCAmelCase ( self : Tuple , **__UpperCAmelCase : List[Any] ) ->Dict: """simple docstring""" a = self.translate_src_text(**__UpperCAmelCase ) assert self.expected_text == generated_words def __lowerCAmelCase ( self : Tuple , **__UpperCAmelCase : Dict ) ->Optional[int]: """simple docstring""" a = self.tokenizer(self.src_text , **__UpperCAmelCase , padding=__UpperCAmelCase , return_tensors='''tf''' ) a = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 , use_cache=__UpperCAmelCase , ) a = self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=__UpperCAmelCase ) return generated_words @slow def __lowerCAmelCase ( self : List[str] ) ->int: """simple docstring""" self._assert_generated_batch_equal_expected()
26
UpperCAmelCase__ = "0.21.0" from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
26
1
import gc import unittest import numpy as np import torch from torch.backends.cuda import sdp_kernel from diffusers import ( CMStochasticIterativeScheduler, ConsistencyModelPipeline, UNetaDModel, ) from diffusers.utils import randn_tensor, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_a, require_torch_gpu from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class lowercase_ ( lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = ConsistencyModelPipeline __snake_case = UNCONDITIONAL_IMAGE_GENERATION_PARAMS __snake_case = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS # Override required_optional_params to remove num_images_per_prompt __snake_case = frozenset( [ '''num_inference_steps''', '''generator''', '''latents''', '''output_type''', '''return_dict''', '''callback''', '''callback_steps''', ] ) @property def __lowerCAmelCase ( self : Dict ) ->Union[str, Any]: """simple docstring""" a = UNetaDModel.from_pretrained( '''diffusers/consistency-models-test''' , subfolder='''test_unet''' , ) return unet @property def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = UNetaDModel.from_pretrained( '''diffusers/consistency-models-test''' , subfolder='''test_unet_class_cond''' , ) return unet def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : Optional[Any]=False ) ->List[Any]: """simple docstring""" if class_cond: a = self.dummy_cond_unet else: a = self.dummy_uncond_unet # Default to CM multistep sampler a = CMStochasticIterativeScheduler( num_train_timesteps=40 , sigma_min=0.002 , sigma_max=80.0 , ) a = { '''unet''': unet, '''scheduler''': scheduler, } return components def __lowerCAmelCase ( self : str , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Optional[Any]=0 ) ->Union[str, Any]: """simple docstring""" if str(__UpperCAmelCase ).startswith('''mps''' ): a = torch.manual_seed(__UpperCAmelCase ) else: a = torch.Generator(device=__UpperCAmelCase ).manual_seed(__UpperCAmelCase ) a = { '''batch_size''': 1, '''num_inference_steps''': None, '''timesteps''': [22, 0], '''generator''': generator, '''output_type''': '''np''', } return inputs def __lowerCAmelCase ( self : Tuple ) ->Any: """simple docstring""" a = '''cpu''' # ensure determinism for the device-dependent torch.Generator a = self.get_dummy_components() a = ConsistencyModelPipeline(**__UpperCAmelCase ) a = pipe.to(__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_dummy_inputs(__UpperCAmelCase ) a = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 32, 32, 3) a = image[0, -3:, -3:, -1] a = np.array([0.3572, 0.6273, 0.4031, 0.3961, 0.4321, 0.5730, 0.5266, 0.4780, 0.5004] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def __lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" a = '''cpu''' # ensure determinism for the device-dependent torch.Generator a = self.get_dummy_components(class_cond=__UpperCAmelCase ) a = ConsistencyModelPipeline(**__UpperCAmelCase ) a = pipe.to(__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_dummy_inputs(__UpperCAmelCase ) a = 0 a = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 32, 32, 3) a = image[0, -3:, -3:, -1] a = np.array([0.3572, 0.6273, 0.4031, 0.3961, 0.4321, 0.5730, 0.5266, 0.4780, 0.5004] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = '''cpu''' # ensure determinism for the device-dependent torch.Generator a = self.get_dummy_components() a = ConsistencyModelPipeline(**__UpperCAmelCase ) a = pipe.to(__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_dummy_inputs(__UpperCAmelCase ) a = 1 a = None a = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 32, 32, 3) a = image[0, -3:, -3:, -1] a = np.array([0.5004, 0.5004, 0.4994, 0.5008, 0.4976, 0.5018, 0.4990, 0.4982, 0.4987] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def __lowerCAmelCase ( self : str ) ->int: """simple docstring""" a = '''cpu''' # ensure determinism for the device-dependent torch.Generator a = self.get_dummy_components(class_cond=__UpperCAmelCase ) a = ConsistencyModelPipeline(**__UpperCAmelCase ) a = pipe.to(__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_dummy_inputs(__UpperCAmelCase ) a = 1 a = None a = 0 a = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 32, 32, 3) a = image[0, -3:, -3:, -1] a = np.array([0.5004, 0.5004, 0.4994, 0.5008, 0.4976, 0.5018, 0.4990, 0.4982, 0.4987] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 @slow @require_torch_gpu class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self : Union[str, Any] ) ->Any: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Dict=0 , __UpperCAmelCase : Union[str, Any]=False , __UpperCAmelCase : Dict="cpu" , __UpperCAmelCase : str=torch.floataa , __UpperCAmelCase : List[Any]=(1, 3, 64, 64) ) ->List[str]: """simple docstring""" a = torch.manual_seed(__UpperCAmelCase ) a = { '''num_inference_steps''': None, '''timesteps''': [22, 0], '''class_labels''': 0, '''generator''': generator, '''output_type''': '''np''', } if get_fixed_latents: a = self.get_fixed_latents(seed=__UpperCAmelCase , device=__UpperCAmelCase , dtype=__UpperCAmelCase , shape=__UpperCAmelCase ) a = latents return inputs def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : List[str]=0 , __UpperCAmelCase : List[str]="cpu" , __UpperCAmelCase : Optional[int]=torch.floataa , __UpperCAmelCase : Tuple=(1, 3, 64, 64) ) ->List[Any]: """simple docstring""" if type(__UpperCAmelCase ) == str: a = torch.device(__UpperCAmelCase ) a = torch.Generator(device=__UpperCAmelCase ).manual_seed(__UpperCAmelCase ) a = randn_tensor(__UpperCAmelCase , generator=__UpperCAmelCase , device=__UpperCAmelCase , dtype=__UpperCAmelCase ) return latents def __lowerCAmelCase ( self : Dict ) ->Any: """simple docstring""" a = UNetaDModel.from_pretrained('''diffusers/consistency_models''' , subfolder='''diffusers_cd_imagenet64_l2''' ) a = CMStochasticIterativeScheduler( num_train_timesteps=40 , sigma_min=0.002 , sigma_max=80.0 , ) a = ConsistencyModelPipeline(unet=__UpperCAmelCase , scheduler=__UpperCAmelCase ) pipe.to(torch_device=__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_inputs() a = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 64, 64, 3) a = image[0, -3:, -3:, -1] a = np.array([0.0888, 0.0881, 0.0666, 0.0479, 0.0292, 0.0195, 0.0201, 0.0163, 0.0254] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 2e-2 def __lowerCAmelCase ( self : Dict ) ->List[Any]: """simple docstring""" a = UNetaDModel.from_pretrained('''diffusers/consistency_models''' , subfolder='''diffusers_cd_imagenet64_l2''' ) a = CMStochasticIterativeScheduler( num_train_timesteps=40 , sigma_min=0.002 , sigma_max=80.0 , ) a = ConsistencyModelPipeline(unet=__UpperCAmelCase , scheduler=__UpperCAmelCase ) pipe.to(torch_device=__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_inputs() a = 1 a = None a = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 64, 64, 3) a = image[0, -3:, -3:, -1] a = np.array([0.0340, 0.0152, 0.0063, 0.0267, 0.0221, 0.0107, 0.0416, 0.0186, 0.0217] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 2e-2 @require_torch_a def __lowerCAmelCase ( self : Dict ) ->Dict: """simple docstring""" a = UNetaDModel.from_pretrained('''diffusers/consistency_models''' , subfolder='''diffusers_cd_imagenet64_l2''' ) a = CMStochasticIterativeScheduler( num_train_timesteps=40 , sigma_min=0.002 , sigma_max=80.0 , ) a = ConsistencyModelPipeline(unet=__UpperCAmelCase , scheduler=__UpperCAmelCase ) pipe.to(torch_device=__UpperCAmelCase , torch_dtype=torch.floataa ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_inputs(get_fixed_latents=__UpperCAmelCase , device=__UpperCAmelCase ) # Ensure usage of flash attention in torch 2.0 with sdp_kernel(enable_flash=__UpperCAmelCase , enable_math=__UpperCAmelCase , enable_mem_efficient=__UpperCAmelCase ): a = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 64, 64, 3) a = image[0, -3:, -3:, -1] a = np.array([0.1875, 0.1428, 0.1289, 0.2151, 0.2092, 0.1477, 0.1877, 0.1641, 0.1353] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 @require_torch_a def __lowerCAmelCase ( self : Optional[Any] ) ->int: """simple docstring""" a = UNetaDModel.from_pretrained('''diffusers/consistency_models''' , subfolder='''diffusers_cd_imagenet64_l2''' ) a = CMStochasticIterativeScheduler( num_train_timesteps=40 , sigma_min=0.002 , sigma_max=80.0 , ) a = ConsistencyModelPipeline(unet=__UpperCAmelCase , scheduler=__UpperCAmelCase ) pipe.to(torch_device=__UpperCAmelCase , torch_dtype=torch.floataa ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_inputs(get_fixed_latents=__UpperCAmelCase , device=__UpperCAmelCase ) a = 1 a = None # Ensure usage of flash attention in torch 2.0 with sdp_kernel(enable_flash=__UpperCAmelCase , enable_math=__UpperCAmelCase , enable_mem_efficient=__UpperCAmelCase ): a = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 64, 64, 3) a = image[0, -3:, -3:, -1] a = np.array([0.1663, 0.1948, 0.2275, 0.1680, 0.1204, 0.1245, 0.1858, 0.1338, 0.2095] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3
26
def _a ( a :list ) -> list: if len(a ) <= 1: return lst a = 1 while i < len(a ): if lst[i - 1] <= lst[i]: i += 1 else: a , a = lst[i], lst[i - 1] i -= 1 if i == 0: a = 1 return lst if __name__ == "__main__": UpperCAmelCase__ = input("Enter numbers separated by a comma:\n").strip() UpperCAmelCase__ = [int(item) for item in user_input.split(",")] print(gnome_sort(unsorted))
26
1
import io import itertools import json from dataclasses import dataclass from typing import Optional import pyarrow as pa import pyarrow.json as paj import datasets from datasets.table import table_cast from datasets.utils.file_utils import readline UpperCAmelCase__ = datasets.utils.logging.get_logger(__name__) @dataclass class lowercase_ ( datasets.BuilderConfig ): '''simple docstring''' __snake_case = None __snake_case = "utf-8" __snake_case = None __snake_case = None __snake_case = True # deprecated __snake_case = None # deprecated __snake_case = 10 << 20 # 10MB __snake_case = None class lowercase_ ( datasets.ArrowBasedBuilder ): '''simple docstring''' __snake_case = JsonConfig def __lowerCAmelCase ( self : Optional[Any] ) ->Any: """simple docstring""" if self.config.block_size is not None: logger.warning('''The JSON loader parameter `block_size` is deprecated. Please use `chunksize` instead''' ) a = self.config.block_size if self.config.use_threads is not True: logger.warning( '''The JSON loader parameter `use_threads` is deprecated and doesn\'t have any effect anymore.''' ) if self.config.newlines_in_values is not None: raise ValueError('''The JSON loader parameter `newlines_in_values` is no longer supported''' ) return datasets.DatasetInfo(features=self.config.features ) def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : List[str] ) ->Union[str, Any]: """simple docstring""" if not self.config.data_files: raise ValueError(F"""At least one data file must be specified, but got data_files={self.config.data_files}""" ) a = dl_manager.download_and_extract(self.config.data_files ) if isinstance(__UpperCAmelCase , (str, list, tuple) ): a = data_files if isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = [files] a = [dl_manager.iter_files(__UpperCAmelCase ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''files''': files} )] a = [] for split_name, files in data_files.items(): if isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = [files] a = [dl_manager.iter_files(__UpperCAmelCase ) for file in files] splits.append(datasets.SplitGenerator(name=__UpperCAmelCase , gen_kwargs={'''files''': files} ) ) return splits def __lowerCAmelCase ( self : str , __UpperCAmelCase : pa.Table ) ->pa.Table: """simple docstring""" if self.config.features is not None: # adding missing columns for column_name in set(self.config.features ) - set(pa_table.column_names ): a = self.config.features.arrow_schema.field(__UpperCAmelCase ).type a = pa_table.append_column(__UpperCAmelCase , pa.array([None] * len(__UpperCAmelCase ) , type=__UpperCAmelCase ) ) # more expensive cast to support nested structures with keys in a different order # allows str <-> int/float or str to Audio for example a = table_cast(__UpperCAmelCase , self.config.features.arrow_schema ) return pa_table def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : List[str] ) ->Union[str, Any]: """simple docstring""" for file_idx, file in enumerate(itertools.chain.from_iterable(__UpperCAmelCase ) ): # If the file is one json object and if we need to look at the list of items in one specific field if self.config.field is not None: with open(__UpperCAmelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: a = json.load(__UpperCAmelCase ) # We keep only the field we are interested in a = dataset[self.config.field] # We accept two format: a list of dicts or a dict of lists if isinstance(__UpperCAmelCase , (list, tuple) ): a = set().union(*[row.keys() for row in dataset] ) a = {col: [row.get(__UpperCAmelCase ) for row in dataset] for col in keys} else: a = dataset a = pa.Table.from_pydict(__UpperCAmelCase ) yield file_idx, self._cast_table(__UpperCAmelCase ) # If the file has one json object per line else: with open(__UpperCAmelCase , '''rb''' ) as f: a = 0 # Use block_size equal to the chunk size divided by 32 to leverage multithreading # Set a default minimum value of 16kB if the chunk size is really small a = max(self.config.chunksize // 32 , 16 << 10 ) a = ( self.config.encoding_errors if self.config.encoding_errors is not None else '''strict''' ) while True: a = f.read(self.config.chunksize ) if not batch: break # Finish current line try: batch += f.readline() except (AttributeError, io.UnsupportedOperation): batch += readline(__UpperCAmelCase ) # PyArrow only accepts utf-8 encoded bytes if self.config.encoding != "utf-8": a = batch.decode(self.config.encoding , errors=__UpperCAmelCase ).encode('''utf-8''' ) try: while True: try: a = paj.read_json( io.BytesIO(__UpperCAmelCase ) , read_options=paj.ReadOptions(block_size=__UpperCAmelCase ) ) break except (pa.ArrowInvalid, pa.ArrowNotImplementedError) as e: if ( isinstance(__UpperCAmelCase , pa.ArrowInvalid ) and "straddling" not in str(__UpperCAmelCase ) or block_size > len(__UpperCAmelCase ) ): raise else: # Increase the block size in case it was too small. # The block size will be reset for the next file. logger.debug( F"""Batch of {len(__UpperCAmelCase )} bytes couldn't be parsed with block_size={block_size}. Retrying with block_size={block_size * 2}.""" ) block_size *= 2 except pa.ArrowInvalid as e: try: with open( __UpperCAmelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: a = json.load(__UpperCAmelCase ) except json.JSONDecodeError: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCAmelCase )}: {e}""" ) raise e # If possible, parse the file as a list of json objects and exit the loop if isinstance(__UpperCAmelCase , __UpperCAmelCase ): # list is the only sequence type supported in JSON try: a = set().union(*[row.keys() for row in dataset] ) a = {col: [row.get(__UpperCAmelCase ) for row in dataset] for col in keys} a = pa.Table.from_pydict(__UpperCAmelCase ) except (pa.ArrowInvalid, AttributeError) as e: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCAmelCase )}: {e}""" ) raise ValueError(F"""Not able to read records in the JSON file at {file}.""" ) from None yield file_idx, self._cast_table(__UpperCAmelCase ) break else: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCAmelCase )}: {e}""" ) raise ValueError( F"""Not able to read records in the JSON file at {file}. """ F"""You should probably indicate the field of the JSON file containing your records. """ F"""This JSON file contain the following fields: {str(list(dataset.keys() ) )}. """ F"""Select the correct one and provide it as `field='XXX'` to the dataset loading method. """ ) from None # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield (file_idx, batch_idx), self._cast_table(__UpperCAmelCase ) batch_idx += 1
26
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCAmelCase__ = { "configuration_deberta": ["DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP", "DebertaConfig", "DebertaOnnxConfig"], "tokenization_deberta": ["DebertaTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ["DebertaTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "DebertaForMaskedLM", "DebertaForQuestionAnswering", "DebertaForSequenceClassification", "DebertaForTokenClassification", "DebertaModel", "DebertaPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "TFDebertaForMaskedLM", "TFDebertaForQuestionAnswering", "TFDebertaForSequenceClassification", "TFDebertaForTokenClassification", "TFDebertaModel", "TFDebertaPreTrainedModel", ] if TYPE_CHECKING: from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig, DebertaOnnxConfig from .tokenization_deberta import DebertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_deberta_fast import DebertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deberta import ( DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, DebertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deberta import ( TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFDebertaForMaskedLM, TFDebertaForQuestionAnswering, TFDebertaForSequenceClassification, TFDebertaForTokenClassification, TFDebertaModel, TFDebertaPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
1
from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowercase_ : '''simple docstring''' def __init__( self : Any , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Optional[Any]=2 , __UpperCAmelCase : Optional[Any]=3 , __UpperCAmelCase : Optional[Any]=4 , __UpperCAmelCase : List[Any]=2 , __UpperCAmelCase : List[Any]=7 , __UpperCAmelCase : Tuple=True , __UpperCAmelCase : int=True , __UpperCAmelCase : Dict=True , __UpperCAmelCase : Dict=True , __UpperCAmelCase : Tuple=99 , __UpperCAmelCase : int=36 , __UpperCAmelCase : Dict=2 , __UpperCAmelCase : Dict=4 , __UpperCAmelCase : List[Any]=37 , __UpperCAmelCase : Dict="gelu" , __UpperCAmelCase : Dict=0.1 , __UpperCAmelCase : Dict=0.1 , __UpperCAmelCase : List[Any]=512 , __UpperCAmelCase : Tuple=16 , __UpperCAmelCase : List[Any]=2 , __UpperCAmelCase : List[Any]=0.02 , __UpperCAmelCase : Tuple=6 , __UpperCAmelCase : Dict=6 , __UpperCAmelCase : Dict=3 , __UpperCAmelCase : int=4 , __UpperCAmelCase : int=None , __UpperCAmelCase : List[str]=1_000 , ) ->List[str]: """simple docstring""" a = parent a = batch_size a = num_channels a = image_size a = patch_size a = is_training a = use_input_mask a = use_token_type_ids a = use_labels a = vocab_size a = hidden_size a = num_hidden_layers a = num_attention_heads a = intermediate_size a = hidden_act a = hidden_dropout_prob a = attention_probs_dropout_prob a = max_position_embeddings a = type_vocab_size a = type_sequence_label_size a = initializer_range a = coordinate_size a = shape_size a = num_labels a = num_choices a = scope a = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) a = text_seq_length a = (image_size // patch_size) ** 2 + 1 a = self.text_seq_length + self.image_seq_length def __lowerCAmelCase ( self : Any ) ->Dict: """simple docstring""" a = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) a = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) a = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: a = bbox[i, j, 3] a = bbox[i, j, 1] a = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: a = bbox[i, j, 2] a = bbox[i, j, 0] a = tmp_coordinate a = tf.constant(__UpperCAmelCase ) a = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) a = None if self.use_input_mask: a = random_attention_mask([self.batch_size, self.text_seq_length] ) a = None if self.use_token_type_ids: a = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) a = None a = None if self.use_labels: a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) a = LayoutLMvaConfig( 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 , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Any , __UpperCAmelCase : Dict ) ->Optional[int]: """simple docstring""" a = TFLayoutLMvaModel(config=__UpperCAmelCase ) # text + image a = model(__UpperCAmelCase , pixel_values=__UpperCAmelCase , training=__UpperCAmelCase ) a = model( __UpperCAmelCase , bbox=__UpperCAmelCase , pixel_values=__UpperCAmelCase , attention_mask=__UpperCAmelCase , token_type_ids=__UpperCAmelCase , training=__UpperCAmelCase , ) a = model(__UpperCAmelCase , bbox=__UpperCAmelCase , pixel_values=__UpperCAmelCase , training=__UpperCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only a = model(__UpperCAmelCase , training=__UpperCAmelCase ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only a = model({'''pixel_values''': pixel_values} , training=__UpperCAmelCase ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : List[Any] , __UpperCAmelCase : str , __UpperCAmelCase : List[str] , __UpperCAmelCase : int , __UpperCAmelCase : int , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Tuple ) ->Any: """simple docstring""" a = self.num_labels a = TFLayoutLMvaForSequenceClassification(config=__UpperCAmelCase ) a = model( __UpperCAmelCase , bbox=__UpperCAmelCase , pixel_values=__UpperCAmelCase , attention_mask=__UpperCAmelCase , token_type_ids=__UpperCAmelCase , labels=__UpperCAmelCase , training=__UpperCAmelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : str , __UpperCAmelCase : Any , __UpperCAmelCase : Dict , __UpperCAmelCase : Dict , __UpperCAmelCase : List[Any] , __UpperCAmelCase : int ) ->List[str]: """simple docstring""" a = self.num_labels a = TFLayoutLMvaForTokenClassification(config=__UpperCAmelCase ) a = model( __UpperCAmelCase , bbox=__UpperCAmelCase , pixel_values=__UpperCAmelCase , attention_mask=__UpperCAmelCase , token_type_ids=__UpperCAmelCase , labels=__UpperCAmelCase , training=__UpperCAmelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : str , __UpperCAmelCase : Tuple , __UpperCAmelCase : int , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Union[str, Any] ) ->str: """simple docstring""" a = 2 a = TFLayoutLMvaForQuestionAnswering(config=__UpperCAmelCase ) a = model( __UpperCAmelCase , bbox=__UpperCAmelCase , pixel_values=__UpperCAmelCase , attention_mask=__UpperCAmelCase , token_type_ids=__UpperCAmelCase , start_positions=__UpperCAmelCase , end_positions=__UpperCAmelCase , training=__UpperCAmelCase , ) 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 __lowerCAmelCase ( self : str ) ->Optional[int]: """simple docstring""" a = self.prepare_config_and_inputs() ((a) , (a) , (a) , (a) , (a) , (a) , (a) , (a)) = config_and_inputs a = { '''input_ids''': input_ids, '''bbox''': bbox, '''pixel_values''': pixel_values, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask, } return config, inputs_dict @require_tf class lowercase_ ( lowercase , lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) __snake_case = ( {'''document-question-answering''': TFLayoutLMvaForQuestionAnswering, '''feature-extraction''': TFLayoutLMvaModel} if is_tf_available() else {} ) __snake_case = False __snake_case = False __snake_case = False def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Dict , __UpperCAmelCase : Any , __UpperCAmelCase : List[Any] ) ->Dict: """simple docstring""" return True def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Union[str, Any]=False ) ->dict: """simple docstring""" a = copy.deepcopy(__UpperCAmelCase ) if model_class in get_values(__UpperCAmelCase ): a = { k: tf.tile(tf.expand_dims(__UpperCAmelCase , 1 ) , (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(__UpperCAmelCase , tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(__UpperCAmelCase ): a = tf.ones(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(__UpperCAmelCase ): a = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) a = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(__UpperCAmelCase ): a = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(__UpperCAmelCase ): a = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=tf.intaa ) return inputs_dict def __lowerCAmelCase ( self : int ) ->Dict: """simple docstring""" a = TFLayoutLMvaModelTester(self ) a = ConfigTester(self , config_class=__UpperCAmelCase , hidden_size=37 ) def __lowerCAmelCase ( self : Any ) ->Dict: """simple docstring""" self.config_tester.run_common_tests() def __lowerCAmelCase ( self : Dict ) ->Any: """simple docstring""" a , a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a = model_class(__UpperCAmelCase ) if getattr(__UpperCAmelCase , '''hf_compute_loss''' , __UpperCAmelCase ): # The number of elements in the loss should be the same as the number of elements in the label a = self._prepare_for_class(inputs_dict.copy() , __UpperCAmelCase , return_labels=__UpperCAmelCase ) a = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() , reverse=__UpperCAmelCase )[0] ] a = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs a = self._prepare_for_class(inputs_dict.copy() , __UpperCAmelCase , return_labels=__UpperCAmelCase ) a = prepared_for_class.pop('''input_ids''' ) a = model(__UpperCAmelCase , **__UpperCAmelCase )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions a = self._prepare_for_class(inputs_dict.copy() , __UpperCAmelCase , return_labels=__UpperCAmelCase ) a = prepared_for_class.pop('''input_ids''' ) if "labels" in prepared_for_class: a = prepared_for_class['''labels'''].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: a = -100 a = tf.convert_to_tensor(__UpperCAmelCase ) a = model(__UpperCAmelCase , **__UpperCAmelCase )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict a = self._prepare_for_class(inputs_dict.copy() , __UpperCAmelCase , return_labels=__UpperCAmelCase ) a = model(__UpperCAmelCase )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple a = self._prepare_for_class(inputs_dict.copy() , __UpperCAmelCase , return_labels=__UpperCAmelCase ) # Get keys that were added with the _prepare_for_class function a = prepared_for_class.keys() - inputs_dict.keys() a = inspect.signature(model.call ).parameters a = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple a = {0: '''input_ids'''} for label_key in label_keys: a = signature_names.index(__UpperCAmelCase ) a = label_key a = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple a = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: a = prepared_for_class[value] a = tuple(__UpperCAmelCase ) # Send to model a = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def __lowerCAmelCase ( self : List[Any] ) ->Tuple: """simple docstring""" ( ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : Any ) ->Tuple: """simple docstring""" ( ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ) = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: a = type self.model_tester.create_and_check_model(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[int] ) ->Optional[Any]: """simple docstring""" ( ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[Any] ) ->Optional[int]: """simple docstring""" ( ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] ) ->Union[str, Any]: """simple docstring""" ( ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) @slow def __lowerCAmelCase ( self : Dict ) ->Union[str, Any]: """simple docstring""" for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a = TFLayoutLMvaModel.from_pretrained(__UpperCAmelCase ) self.assertIsNotNone(__UpperCAmelCase ) def _a ( ) -> Optional[int]: a = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf class lowercase_ ( unittest.TestCase ): '''simple docstring''' @cached_property def __lowerCAmelCase ( self : List[str] ) ->List[str]: """simple docstring""" return LayoutLMvaImageProcessor(apply_ocr=__UpperCAmelCase ) if is_vision_available() else None @slow def __lowerCAmelCase ( self : str ) ->List[str]: """simple docstring""" a = TFLayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base''' ) a = self.default_image_processor a = prepare_img() a = image_processor(images=__UpperCAmelCase , return_tensors='''tf''' ).pixel_values a = tf.constant([[1, 2]] ) a = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) , axis=0 ) # forward pass a = model(input_ids=__UpperCAmelCase , bbox=__UpperCAmelCase , pixel_values=__UpperCAmelCase , training=__UpperCAmelCase ) # verify the logits a = (1, 199, 768) self.assertEqual(outputs.last_hidden_state.shape , __UpperCAmelCase ) a = tf.constant( [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , __UpperCAmelCase , atol=1e-4 ) )
26
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all feature extractors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...feature_extraction_utils import FeatureExtractionMixin from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_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, ) UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = OrderedDict( [ ("audio-spectrogram-transformer", "ASTFeatureExtractor"), ("beit", "BeitFeatureExtractor"), ("chinese_clip", "ChineseCLIPFeatureExtractor"), ("clap", "ClapFeatureExtractor"), ("clip", "CLIPFeatureExtractor"), ("clipseg", "ViTFeatureExtractor"), ("conditional_detr", "ConditionalDetrFeatureExtractor"), ("convnext", "ConvNextFeatureExtractor"), ("cvt", "ConvNextFeatureExtractor"), ("data2vec-audio", "Wav2Vec2FeatureExtractor"), ("data2vec-vision", "BeitFeatureExtractor"), ("deformable_detr", "DeformableDetrFeatureExtractor"), ("deit", "DeiTFeatureExtractor"), ("detr", "DetrFeatureExtractor"), ("dinat", "ViTFeatureExtractor"), ("donut-swin", "DonutFeatureExtractor"), ("dpt", "DPTFeatureExtractor"), ("encodec", "EncodecFeatureExtractor"), ("flava", "FlavaFeatureExtractor"), ("glpn", "GLPNFeatureExtractor"), ("groupvit", "CLIPFeatureExtractor"), ("hubert", "Wav2Vec2FeatureExtractor"), ("imagegpt", "ImageGPTFeatureExtractor"), ("layoutlmv2", "LayoutLMv2FeatureExtractor"), ("layoutlmv3", "LayoutLMv3FeatureExtractor"), ("levit", "LevitFeatureExtractor"), ("maskformer", "MaskFormerFeatureExtractor"), ("mctct", "MCTCTFeatureExtractor"), ("mobilenet_v1", "MobileNetV1FeatureExtractor"), ("mobilenet_v2", "MobileNetV2FeatureExtractor"), ("mobilevit", "MobileViTFeatureExtractor"), ("nat", "ViTFeatureExtractor"), ("owlvit", "OwlViTFeatureExtractor"), ("perceiver", "PerceiverFeatureExtractor"), ("poolformer", "PoolFormerFeatureExtractor"), ("regnet", "ConvNextFeatureExtractor"), ("resnet", "ConvNextFeatureExtractor"), ("segformer", "SegformerFeatureExtractor"), ("sew", "Wav2Vec2FeatureExtractor"), ("sew-d", "Wav2Vec2FeatureExtractor"), ("speech_to_text", "Speech2TextFeatureExtractor"), ("speecht5", "SpeechT5FeatureExtractor"), ("swiftformer", "ViTFeatureExtractor"), ("swin", "ViTFeatureExtractor"), ("swinv2", "ViTFeatureExtractor"), ("table-transformer", "DetrFeatureExtractor"), ("timesformer", "VideoMAEFeatureExtractor"), ("tvlt", "TvltFeatureExtractor"), ("unispeech", "Wav2Vec2FeatureExtractor"), ("unispeech-sat", "Wav2Vec2FeatureExtractor"), ("van", "ConvNextFeatureExtractor"), ("videomae", "VideoMAEFeatureExtractor"), ("vilt", "ViltFeatureExtractor"), ("vit", "ViTFeatureExtractor"), ("vit_mae", "ViTFeatureExtractor"), ("vit_msn", "ViTFeatureExtractor"), ("wav2vec2", "Wav2Vec2FeatureExtractor"), ("wav2vec2-conformer", "Wav2Vec2FeatureExtractor"), ("wavlm", "Wav2Vec2FeatureExtractor"), ("whisper", "WhisperFeatureExtractor"), ("xclip", "CLIPFeatureExtractor"), ("yolos", "YolosFeatureExtractor"), ] ) UpperCAmelCase__ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES) def _a ( a :str ) -> Any: for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items(): if class_name in extractors: a = model_type_to_module_name(a ) a = importlib.import_module(F""".{module_name}""" , '''transformers.models''' ) try: return getattr(a , a ) except AttributeError: continue for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items(): if getattr(a , '''__name__''' , a ) == 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. a = importlib.import_module('''transformers''' ) if hasattr(a , a ): return getattr(a , a ) return None def _a ( a :Union[str, os.PathLike] , a :Optional[Union[str, os.PathLike]] = None , a :bool = False , a :bool = False , a :Optional[Dict[str, str]] = None , a :Optional[Union[bool, str]] = None , a :Optional[str] = None , a :bool = False , **a :int , ) -> Tuple: a = get_file_from_repo( a , a , cache_dir=a , force_download=a , resume_download=a , proxies=a , use_auth_token=a , revision=a , local_files_only=a , ) if resolved_config_file is None: logger.info( '''Could not locate the feature extractor configuration file, will try to use the model config instead.''' ) return {} with open(a , encoding='''utf-8''' ) as reader: return json.load(a ) class lowercase_ : '''simple docstring''' def __init__( self : Tuple ) ->int: """simple docstring""" raise EnvironmentError( '''AutoFeatureExtractor is designed to be instantiated ''' '''using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.''' ) @classmethod @replace_list_option_in_docstrings(__UpperCAmelCase ) def __lowerCAmelCase ( cls : int , __UpperCAmelCase : Optional[Any] , **__UpperCAmelCase : Dict ) ->List[Any]: """simple docstring""" a = kwargs.pop('''config''' , __UpperCAmelCase ) a = kwargs.pop('''trust_remote_code''' , __UpperCAmelCase ) a = True a , a = FeatureExtractionMixin.get_feature_extractor_dict(__UpperCAmelCase , **__UpperCAmelCase ) a = config_dict.get('''feature_extractor_type''' , __UpperCAmelCase ) a = None if "AutoFeatureExtractor" in config_dict.get('''auto_map''' , {} ): a = config_dict['''auto_map''']['''AutoFeatureExtractor'''] # If we don't find the feature extractor class in the feature extractor config, let's try the model config. if feature_extractor_class is None and feature_extractor_auto_map is None: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = AutoConfig.from_pretrained(__UpperCAmelCase , **__UpperCAmelCase ) # It could be in `config.feature_extractor_type`` a = getattr(__UpperCAmelCase , '''feature_extractor_type''' , __UpperCAmelCase ) if hasattr(__UpperCAmelCase , '''auto_map''' ) and "AutoFeatureExtractor" in config.auto_map: a = config.auto_map['''AutoFeatureExtractor'''] if feature_extractor_class is not None: a = feature_extractor_class_from_name(__UpperCAmelCase ) a = feature_extractor_auto_map is not None a = feature_extractor_class is not None or type(__UpperCAmelCase ) in FEATURE_EXTRACTOR_MAPPING a = resolve_trust_remote_code( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) if has_remote_code and trust_remote_code: a = get_class_from_dynamic_module( __UpperCAmelCase , __UpperCAmelCase , **__UpperCAmelCase ) a = kwargs.pop('''code_revision''' , __UpperCAmelCase ) if os.path.isdir(__UpperCAmelCase ): feature_extractor_class.register_for_auto_class() return feature_extractor_class.from_dict(__UpperCAmelCase , **__UpperCAmelCase ) elif feature_extractor_class is not None: return feature_extractor_class.from_dict(__UpperCAmelCase , **__UpperCAmelCase ) # Last try: we use the FEATURE_EXTRACTOR_MAPPING. elif type(__UpperCAmelCase ) in FEATURE_EXTRACTOR_MAPPING: a = FEATURE_EXTRACTOR_MAPPING[type(__UpperCAmelCase )] return feature_extractor_class.from_dict(__UpperCAmelCase , **__UpperCAmelCase ) raise ValueError( F"""Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a """ F"""`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following """ F"""`model_type` keys in its {CONFIG_NAME}: {', '.join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys() )}""" ) @staticmethod def __lowerCAmelCase ( __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Tuple ) ->Optional[int]: """simple docstring""" FEATURE_EXTRACTOR_MAPPING.register(__UpperCAmelCase , __UpperCAmelCase )
26
1
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin UpperCAmelCase__ = "▁" UpperCAmelCase__ = get_tests_dir("fixtures/test_sentencepiece.model") @require_sentencepiece class lowercase_ ( lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = BertGenerationTokenizer __snake_case = False __snake_case = True def __lowerCAmelCase ( self : str ) ->str: """simple docstring""" super().setUp() a = BertGenerationTokenizer(__UpperCAmelCase , keep_accents=__UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self : int ) ->Dict: """simple docstring""" a = '''<s>''' a = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCAmelCase ) , __UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCAmelCase ) , __UpperCAmelCase ) def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''<pad>''' ) self.assertEqual(len(__UpperCAmelCase ) , 1_002 ) def __lowerCAmelCase ( self : List[str] ) ->List[Any]: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1_000 ) def __lowerCAmelCase ( self : Tuple ) ->Optional[int]: """simple docstring""" a = BertGenerationTokenizer(__UpperCAmelCase , keep_accents=__UpperCAmelCase ) a = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(__UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) , [285, 46, 10, 170, 382] , ) a = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( __UpperCAmelCase , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) a = tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) self.assertListEqual( __UpperCAmelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) a = tokenizer.convert_ids_to_tokens(__UpperCAmelCase ) self.assertListEqual( __UpperCAmelCase , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) @cached_property def __lowerCAmelCase ( self : List[Any] ) ->List[str]: """simple docstring""" return BertGenerationTokenizer.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder''' ) @slow def __lowerCAmelCase ( self : Any ) ->str: """simple docstring""" a = '''Hello World!''' a = [18_536, 2_260, 101] self.assertListEqual(__UpperCAmelCase , self.big_tokenizer.encode(__UpperCAmelCase ) ) @slow def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = ( '''This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will''' ''' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth''' ) a = [ 871, 419, 358, 946, 991, 2_521, 452, 358, 1_357, 387, 7_751, 3_536, 112, 985, 456, 126, 865, 938, 5_400, 5_734, 458, 1_368, 467, 786, 2_462, 5_246, 1_159, 633, 865, 4_519, 457, 582, 852, 2_557, 427, 916, 508, 405, 34_324, 497, 391, 408, 11_342, 1_244, 385, 100, 938, 985, 456, 574, 362, 12_597, 3_200, 3_129, 1_172, ] self.assertListEqual(__UpperCAmelCase , self.big_tokenizer.encode(__UpperCAmelCase ) ) @require_torch @slow def __lowerCAmelCase ( self : Any ) ->Dict: """simple docstring""" import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence a = list(self.big_tokenizer.get_vocab().keys() )[:10] a = ''' '''.join(__UpperCAmelCase ) a = self.big_tokenizer.encode_plus(__UpperCAmelCase , return_tensors='''pt''' , return_token_type_ids=__UpperCAmelCase ) a = self.big_tokenizer.batch_encode_plus( [sequence + ''' ''' + sequence] , return_tensors='''pt''' , return_token_type_ids=__UpperCAmelCase ) a = BertGenerationConfig() a = BertGenerationEncoder(__UpperCAmelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**__UpperCAmelCase ) model(**__UpperCAmelCase ) @slow def __lowerCAmelCase ( self : str ) ->Optional[Any]: """simple docstring""" a = {'''input_ids''': [[39_286, 458, 36_335, 2_001, 456, 13_073, 13_266, 455, 113, 7_746, 1_741, 11_157, 391, 13_073, 13_266, 455, 113, 3_967, 35_412, 113, 4_936, 109, 3_870, 2_377, 113, 30_084, 45_720, 458, 134, 17_496, 112, 503, 11_672, 113, 118, 112, 5_665, 13_347, 38_687, 112, 1_496, 31_389, 112, 3_268, 47_264, 134, 962, 112, 16_377, 8_035, 23_130, 430, 12_169, 15_518, 28_592, 458, 146, 41_697, 109, 391, 12_169, 15_518, 16_689, 458, 146, 41_358, 109, 452, 726, 4_034, 111, 763, 35_412, 5_082, 388, 1_903, 111, 9_051, 391, 2_870, 48_918, 1_900, 1_123, 550, 998, 112, 9_586, 15_985, 455, 391, 410, 22_955, 37_636, 114], [448, 17_496, 419, 3_663, 385, 763, 113, 27_533, 2_870, 3_283, 13_043, 1_639, 24_713, 523, 656, 24_013, 18_550, 2_521, 517, 27_014, 21_244, 420, 1_212, 1_465, 391, 927, 4_833, 388, 578, 11_786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2_169, 7_687, 21_932, 18_146, 726, 363, 17_032, 3_391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__UpperCAmelCase , model_name='''google/bert_for_seq_generation_L-24_bbc_encoder''' , revision='''c817d1fd1be2ffa69431227a1fe320544943d4db''' , )
26
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import ( AutoProcessor, BertTokenizerFast, BlipImageProcessor, GPTaTokenizer, InstructBlipProcessor, PreTrainedTokenizerFast, ) @require_vision class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self : Optional[int] ) ->Tuple: """simple docstring""" a = tempfile.mkdtemp() a = BlipImageProcessor() a = GPTaTokenizer.from_pretrained('''hf-internal-testing/tiny-random-GPT2Model''' ) a = BertTokenizerFast.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) a = InstructBlipProcessor(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) processor.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self : Optional[Any] , **__UpperCAmelCase : Tuple ) ->List[str]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).tokenizer def __lowerCAmelCase ( self : int , **__UpperCAmelCase : str ) ->List[str]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).image_processor def __lowerCAmelCase ( self : Optional[Any] , **__UpperCAmelCase : Any ) ->Optional[Any]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).qformer_tokenizer def __lowerCAmelCase ( self : str ) ->Tuple: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __lowerCAmelCase ( self : Optional[int] ) ->str: """simple docstring""" a = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] a = [Image.fromarray(np.moveaxis(__UpperCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def __lowerCAmelCase ( self : Optional[Any] ) ->List[str]: """simple docstring""" a = InstructBlipProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() , qformer_tokenizer=self.get_qformer_tokenizer() , ) processor.save_pretrained(self.tmpdirname ) a = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) a = self.get_image_processor(do_normalize=__UpperCAmelCase , padding_value=1.0 ) a = InstructBlipProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=__UpperCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __UpperCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __UpperCAmelCase ) self.assertIsInstance(processor.qformer_tokenizer , __UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[Any] ) ->Any: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = self.prepare_image_inputs() a = image_processor(__UpperCAmelCase , return_tensors='''np''' ) a = processor(images=__UpperCAmelCase , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowerCAmelCase ( self : List[str] ) ->List[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = '''lower newer''' a = processor(text=__UpperCAmelCase ) a = tokenizer(__UpperCAmelCase , return_token_type_ids=__UpperCAmelCase ) a = qformer_tokenizer(__UpperCAmelCase , return_token_type_ids=__UpperCAmelCase ) for key in encoded_tokens.keys(): self.assertListEqual(encoded_tokens[key] , encoded_processor[key] ) for key in encoded_tokens_qformer.keys(): self.assertListEqual(encoded_tokens_qformer[key] , encoded_processor['''qformer_''' + key] ) def __lowerCAmelCase ( self : Dict ) ->Optional[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = '''lower newer''' a = self.prepare_image_inputs() a = processor(text=__UpperCAmelCase , images=__UpperCAmelCase ) self.assertListEqual( list(inputs.keys() ) , ['''input_ids''', '''attention_mask''', '''qformer_input_ids''', '''qformer_attention_mask''', '''pixel_values'''] , ) # test if it raises when no input is passed with pytest.raises(__UpperCAmelCase ): processor() def __lowerCAmelCase ( self : Dict ) ->List[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] a = processor.batch_decode(__UpperCAmelCase ) a = tokenizer.batch_decode(__UpperCAmelCase ) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] ) ->str: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = '''lower newer''' a = self.prepare_image_inputs() a = processor(text=__UpperCAmelCase , images=__UpperCAmelCase ) self.assertListEqual( list(inputs.keys() ) , ['''input_ids''', '''attention_mask''', '''qformer_input_ids''', '''qformer_attention_mask''', '''pixel_values'''] , )
26
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCAmelCase__ = { "configuration_roformer": ["ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "RoFormerConfig", "RoFormerOnnxConfig"], "tokenization_roformer": ["RoFormerTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ["RoFormerTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "RoFormerForCausalLM", "RoFormerForMaskedLM", "RoFormerForMultipleChoice", "RoFormerForQuestionAnswering", "RoFormerForSequenceClassification", "RoFormerForTokenClassification", "RoFormerLayer", "RoFormerModel", "RoFormerPreTrainedModel", "load_tf_weights_in_roformer", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFRoFormerForCausalLM", "TFRoFormerForMaskedLM", "TFRoFormerForMultipleChoice", "TFRoFormerForQuestionAnswering", "TFRoFormerForSequenceClassification", "TFRoFormerForTokenClassification", "TFRoFormerLayer", "TFRoFormerModel", "TFRoFormerPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "FlaxRoFormerForMaskedLM", "FlaxRoFormerForMultipleChoice", "FlaxRoFormerForQuestionAnswering", "FlaxRoFormerForSequenceClassification", "FlaxRoFormerForTokenClassification", "FlaxRoFormerModel", "FlaxRoFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
import math def _a ( a :int = 100 ) -> int: a = sum(i * i for i in range(1 , n + 1 ) ) a = int(math.pow(sum(range(1 , n + 1 ) ) , 2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(f"""{solution() = }""")
26
1
def _a ( a :dict ) -> set: a = set() # edges = list of graph's edges a = get_edges(a ) # While there are still elements in edges list, take an arbitrary edge # (from_node, to_node) and add his extremity to chosen_vertices and then # remove all arcs adjacent to the from_node and to_node while edges: a , a = edges.pop() chosen_vertices.add(a ) chosen_vertices.add(a ) for edge in edges.copy(): if from_node in edge or to_node in edge: edges.discard(a ) return chosen_vertices def _a ( a :dict ) -> set: a = set() for from_node, to_nodes in graph.items(): for to_node in to_nodes: edges.add((from_node, to_node) ) return edges if __name__ == "__main__": import doctest doctest.testmod() # graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} # print(f"Matching vertex cover:\n{matching_min_vertex_cover(graph)}")
26
def _a ( a :int = 600_851_475_143 ) -> int: try: a = int(a ) 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.''' ) a = 2 a = 0 if n == 2: return 2 while n > 2: while n % i != 0: i += 1 a = i while n % i == 0: a = n // i i += 1 return int(a ) if __name__ == "__main__": print(f"""{solution() = }""")
26
1
import itertools import math def _a ( a :int ) -> bool: if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(a ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def _a ( ) -> Union[str, Any]: a = 2 while True: if is_prime(a ): yield num num += 1 def _a ( a :int = 10_001 ) -> int: return next(itertools.islice(prime_generator() , nth - 1 , a ) ) if __name__ == "__main__": print(f"""{solution() = }""")
26
import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer UpperCAmelCase__ = "bart" UpperCAmelCase__ = True @st.cache(allow_output_mutation=a ) def _a ( ) -> Tuple: if LOAD_DENSE_INDEX: a = AutoTokenizer.from_pretrained('''yjernite/retribert-base-uncased''' ) a = AutoModel.from_pretrained('''yjernite/retribert-base-uncased''' ).to('''cuda:0''' ) a = qar_model.eval() else: a , a = (None, None) if MODEL_TYPE == "bart": a = AutoTokenizer.from_pretrained('''yjernite/bart_eli5''' ) a = AutoModelForSeqaSeqLM.from_pretrained('''yjernite/bart_eli5''' ).to('''cuda:0''' ) a = torch.load('''seq2seq_models/eli5_bart_model_blm_2.pth''' ) sas_model.load_state_dict(save_dict['''model'''] ) a = sas_model.eval() else: a , a = make_qa_sas_model( model_name='''t5-small''' , from_file='''seq2seq_models/eli5_t5_model_1024_4.pth''' , device='''cuda:0''' ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=a ) def _a ( ) -> Dict: if LOAD_DENSE_INDEX: a = faiss.StandardGpuResources() a = datasets.load_dataset(path='''wiki_snippets''' , name='''wiki40b_en_100_0''' )['''train'''] a = np.memmap( '''wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat''' , dtype='''float32''' , mode='''r''' , shape=(wikiaab_passages.num_rows, 128) , ) a = faiss.IndexFlatIP(128 ) a = faiss.index_cpu_to_gpu(a , 1 , a ) wikiaab_gpu_index_flat.add(a ) # TODO fix for larger GPU else: a , a = (None, None) a = Elasticsearch([{'''host''': '''localhost''', '''port''': '''9200'''}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=a ) def _a ( ) -> Optional[int]: a = datasets.load_dataset('''eli5''' , name='''LFQA_reddit''' ) a = elia['''train_eli5'''] a = np.memmap( '''eli5_questions_reps.dat''' , dtype='''float32''' , mode='''r''' , shape=(elia_train.num_rows, 128) ) a = faiss.IndexFlatIP(128 ) eli5_train_q_index.add(a ) return (elia_train, eli5_train_q_index) UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = load_indexes() UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = load_models() UpperCAmelCase__ , UpperCAmelCase__ = load_train_data() def _a ( a :str , a :Tuple=10 ) -> List[str]: a = embed_questions_for_retrieval([question] , a , a ) a , a = eli5_train_q_index.search(a , a ) a = [elia_train[int(a )] for i in I[0]] return nn_examples def _a ( a :str , a :Any="wiki40b" , a :int="dense" , a :Union[str, Any]=10 ) -> List[str]: if source == "none": a , a = (''' <P> '''.join(['''''' for _ in range(11 )] ).strip(), []) else: if method == "dense": a , a = query_qa_dense_index( a , a , a , a , a , a ) else: a , a = query_es_index( a , a , index_name='''english_wiki40b_snippets_100w''' , n_results=a , ) a = [ (res['''article_title'''], res['''section_title'''].strip(), res['''score'''], res['''passage_text''']) for res in hit_lst ] a = '''question: {} context: {}'''.format(a , a ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda a : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda a : None), } ) def _a ( a :Tuple , a :int , a :int , a :Dict=64 , a :List[Any]=256 , a :List[Any]=False , a :List[Any]=2 , a :Tuple=0.95 , a :Optional[Any]=0.8 ) -> int: with torch.no_grad(): a = qa_sas_generate( a , a , a , num_answers=1 , num_beams=a , min_len=a , max_len=a , do_sample=a , temp=a , top_p=a , top_k=a , max_input_length=1_024 , device='''cuda:0''' , )[0] return (answer, support_list) st.title("Long Form Question Answering with ELI5") # Start sidebar UpperCAmelCase__ = "<img src='https://huggingface.co/front/assets/huggingface_logo.svg'>" UpperCAmelCase__ = "\n<html>\n <head>\n <style>\n .img-container {\n padding-left: 90px;\n padding-right: 90px;\n padding-top: 50px;\n padding-bottom: 50px;\n background-color: #f0f3f9;\n }\n </style>\n </head>\n <body>\n <span class=\"img-container\"> <!-- Inline parent element -->\n %s\n </span>\n </body>\n</html>\n" % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia UpperCAmelCase__ = "\nThis demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html).\nFirst, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset,\na pre-processed fixed snapshot of Wikipedia.\n" st.sidebar.markdown(description, unsafe_allow_html=True) UpperCAmelCase__ = [ "Answer the question", "View the retrieved document only", "View the most similar ELI5 question and answer", "Show me everything, please!", ] UpperCAmelCase__ = st.sidebar.checkbox("Demo options") if demo_options: UpperCAmelCase__ = st.sidebar.selectbox( "", action_list, index=3, ) UpperCAmelCase__ = action_list.index(action_st) UpperCAmelCase__ = st.sidebar.selectbox( "", ["Show full text of passages", "Show passage section titles"], index=0, ) UpperCAmelCase__ = show_type == "Show full text of passages" else: UpperCAmelCase__ = 3 UpperCAmelCase__ = True UpperCAmelCase__ = st.sidebar.checkbox("Retrieval options") if retrieval_options: UpperCAmelCase__ = "\n ### Information retriever options\n\n The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding\n trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs.\n The answer is then generated by sequence to sequence model which takes the question and retrieved document as input.\n " st.sidebar.markdown(retriever_info) UpperCAmelCase__ = st.sidebar.selectbox("Which Wikipedia format should the model use?", ["wiki40b", "none"]) UpperCAmelCase__ = st.sidebar.selectbox("Which Wikipedia indexer should the model use?", ["dense", "sparse", "mixed"]) else: UpperCAmelCase__ = "wiki40b" UpperCAmelCase__ = "dense" UpperCAmelCase__ = "beam" UpperCAmelCase__ = 2 UpperCAmelCase__ = 64 UpperCAmelCase__ = 256 UpperCAmelCase__ = None UpperCAmelCase__ = None UpperCAmelCase__ = st.sidebar.checkbox("Generation options") if generate_options: UpperCAmelCase__ = "\n ### Answer generation options\n\n The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large)\n weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with\n **beam** search, or **sample** from the decoder's output probabilities.\n " st.sidebar.markdown(generate_info) UpperCAmelCase__ = st.sidebar.selectbox("Would you like to use beam search or sample an answer?", ["beam", "sampled"]) UpperCAmelCase__ = st.sidebar.slider( "Minimum generation length", min_value=8, max_value=256, value=64, step=8, format=None, key=None ) UpperCAmelCase__ = st.sidebar.slider( "Maximum generation length", min_value=64, max_value=512, value=256, step=16, format=None, key=None ) if sampled == "beam": UpperCAmelCase__ = st.sidebar.slider("Beam size", min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: UpperCAmelCase__ = st.sidebar.slider( "Nucleus sampling p", min_value=0.1, max_value=1.0, value=0.95, step=0.01, format=None, key=None ) UpperCAmelCase__ = st.sidebar.slider( "Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.01, format=None, key=None ) UpperCAmelCase__ = None # start main text UpperCAmelCase__ = [ "<MY QUESTION>", "How do people make chocolate?", "Why do we get a fever when we are sick?", "How can different animals perceive different colors?", "What is natural language processing?", "What's the best way to treat a sunburn?", "What exactly are vitamins ?", "How does nuclear energy provide electricity?", "What's the difference between viruses and bacteria?", "Why are flutes classified as woodwinds when most of them are made out of metal ?", "Why do people like drinking coffee even though it tastes so bad?", "What happens when wine ages? How does it make the wine taste better?", "If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?", "How can we set a date to the beginning or end of an artistic period? Doesn't the change happen gradually?", "How does New Zealand have so many large bird predators?", ] UpperCAmelCase__ = st.selectbox( "What would you like to ask? ---- select <MY QUESTION> to enter a new query", questions_list, index=1, ) if question_s == "<MY QUESTION>": UpperCAmelCase__ = st.text_input("Enter your question here:", "") else: UpperCAmelCase__ = question_s if st.button("Show me!"): if action in [0, 1, 3]: if index_type == "mixed": UpperCAmelCase__ , UpperCAmelCase__ = make_support(question, source=wiki_source, method="dense", n_results=10) UpperCAmelCase__ , UpperCAmelCase__ = make_support(question, source=wiki_source, method="sparse", n_results=10) UpperCAmelCase__ = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] UpperCAmelCase__ = support_list[:10] UpperCAmelCase__ = "<P> " + " <P> ".join([res[-1] for res in support_list]) else: UpperCAmelCase__ , UpperCAmelCase__ = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: UpperCAmelCase__ , UpperCAmelCase__ = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == "sampled"), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown("### The model generated answer is:") st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown("--- \n ### The model is drawing information from the following Wikipedia passages:") for i, res in enumerate(support_list): UpperCAmelCase__ = "https://en.wikipedia.org/wiki/{}".format(res[0].replace(" ", "_")) UpperCAmelCase__ = res[1].strip() if sec_titles == "": UpperCAmelCase__ = "[{}]({})".format(res[0], wiki_url) else: UpperCAmelCase__ = sec_titles.split(" & ") UpperCAmelCase__ = " & ".join( ["[{}]({}#{})".format(sec.strip(), wiki_url, sec.strip().replace(" ", "_")) for sec in sec_list] ) st.markdown( "{0:02d} - **Article**: {1:<18} <br> _Section_: {2}".format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( "> <span style=\"font-family:arial; font-size:10pt;\">" + res[-1] + "</span>", unsafe_allow_html=True ) if action in [2, 3]: UpperCAmelCase__ = find_nearest_training(question) UpperCAmelCase__ = nn_train_list[0] st.markdown( "--- \n ### The most similar question in the ELI5 training set was: \n\n {}".format(train_exple["title"]) ) UpperCAmelCase__ = [ "{}. {}".format(i + 1, " \n".join([line.strip() for line in ans.split("\n") if line.strip() != ""])) for i, (ans, sc) in enumerate(zip(train_exple["answers"]["text"], train_exple["answers"]["score"])) if i == 0 or sc > 2 ] st.markdown("##### Its answers were: \n\n {}".format("\n".join(answers_st))) UpperCAmelCase__ = "\n---\n\n**Disclaimer**\n\n*The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system.\nEvaluating biases of such a model and ensuring factual generations are still very much open research problems.\nTherefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.*\n" st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
26
1
import random import unittest import torch from diffusers import IFImgaImgSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class lowercase_ ( lowercase , lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = IFImgaImgSuperResolutionPipeline __snake_case = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''width''', '''height'''} __snake_case = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({'''original_image'''} ) __snake_case = PipelineTesterMixin.required_optional_params - {'''latents'''} def __lowerCAmelCase ( self : Tuple ) ->str: """simple docstring""" return self._get_superresolution_dummy_components() def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : Dict , __UpperCAmelCase : Optional[int]=0 ) ->str: """simple docstring""" if str(__UpperCAmelCase ).startswith('''mps''' ): a = torch.manual_seed(__UpperCAmelCase ) else: a = torch.Generator(device=__UpperCAmelCase ).manual_seed(__UpperCAmelCase ) a = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCAmelCase ) ).to(__UpperCAmelCase ) a = floats_tensor((1, 3, 16, 16) , rng=random.Random(__UpperCAmelCase ) ).to(__UpperCAmelCase ) a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''original_image''': original_image, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def __lowerCAmelCase ( self : Tuple ) ->List[str]: """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def __lowerCAmelCase ( self : Optional[Any] ) ->Any: """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def __lowerCAmelCase ( self : Union[str, Any] ) ->Union[str, Any]: """simple docstring""" super().test_save_load_floataa(expected_max_diff=1e-1 ) def __lowerCAmelCase ( self : Optional[Any] ) ->Optional[Any]: """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def __lowerCAmelCase ( self : Optional[int] ) ->Union[str, Any]: """simple docstring""" self._test_save_load_local() def __lowerCAmelCase ( self : Union[str, Any] ) ->Tuple: """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1e-2 , )
26
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin UpperCAmelCase__ = "▁" UpperCAmelCase__ = get_tests_dir("fixtures/test_sentencepiece.model") @require_sentencepiece class lowercase_ ( lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = BertGenerationTokenizer __snake_case = False __snake_case = True def __lowerCAmelCase ( self : str ) ->str: """simple docstring""" super().setUp() a = BertGenerationTokenizer(__UpperCAmelCase , keep_accents=__UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self : int ) ->Dict: """simple docstring""" a = '''<s>''' a = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCAmelCase ) , __UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCAmelCase ) , __UpperCAmelCase ) def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''<pad>''' ) self.assertEqual(len(__UpperCAmelCase ) , 1_002 ) def __lowerCAmelCase ( self : List[str] ) ->List[Any]: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1_000 ) def __lowerCAmelCase ( self : Tuple ) ->Optional[int]: """simple docstring""" a = BertGenerationTokenizer(__UpperCAmelCase , keep_accents=__UpperCAmelCase ) a = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(__UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) , [285, 46, 10, 170, 382] , ) a = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( __UpperCAmelCase , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) a = tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) self.assertListEqual( __UpperCAmelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) a = tokenizer.convert_ids_to_tokens(__UpperCAmelCase ) self.assertListEqual( __UpperCAmelCase , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) @cached_property def __lowerCAmelCase ( self : List[Any] ) ->List[str]: """simple docstring""" return BertGenerationTokenizer.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder''' ) @slow def __lowerCAmelCase ( self : Any ) ->str: """simple docstring""" a = '''Hello World!''' a = [18_536, 2_260, 101] self.assertListEqual(__UpperCAmelCase , self.big_tokenizer.encode(__UpperCAmelCase ) ) @slow def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = ( '''This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will''' ''' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth''' ) a = [ 871, 419, 358, 946, 991, 2_521, 452, 358, 1_357, 387, 7_751, 3_536, 112, 985, 456, 126, 865, 938, 5_400, 5_734, 458, 1_368, 467, 786, 2_462, 5_246, 1_159, 633, 865, 4_519, 457, 582, 852, 2_557, 427, 916, 508, 405, 34_324, 497, 391, 408, 11_342, 1_244, 385, 100, 938, 985, 456, 574, 362, 12_597, 3_200, 3_129, 1_172, ] self.assertListEqual(__UpperCAmelCase , self.big_tokenizer.encode(__UpperCAmelCase ) ) @require_torch @slow def __lowerCAmelCase ( self : Any ) ->Dict: """simple docstring""" import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence a = list(self.big_tokenizer.get_vocab().keys() )[:10] a = ''' '''.join(__UpperCAmelCase ) a = self.big_tokenizer.encode_plus(__UpperCAmelCase , return_tensors='''pt''' , return_token_type_ids=__UpperCAmelCase ) a = self.big_tokenizer.batch_encode_plus( [sequence + ''' ''' + sequence] , return_tensors='''pt''' , return_token_type_ids=__UpperCAmelCase ) a = BertGenerationConfig() a = BertGenerationEncoder(__UpperCAmelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**__UpperCAmelCase ) model(**__UpperCAmelCase ) @slow def __lowerCAmelCase ( self : str ) ->Optional[Any]: """simple docstring""" a = {'''input_ids''': [[39_286, 458, 36_335, 2_001, 456, 13_073, 13_266, 455, 113, 7_746, 1_741, 11_157, 391, 13_073, 13_266, 455, 113, 3_967, 35_412, 113, 4_936, 109, 3_870, 2_377, 113, 30_084, 45_720, 458, 134, 17_496, 112, 503, 11_672, 113, 118, 112, 5_665, 13_347, 38_687, 112, 1_496, 31_389, 112, 3_268, 47_264, 134, 962, 112, 16_377, 8_035, 23_130, 430, 12_169, 15_518, 28_592, 458, 146, 41_697, 109, 391, 12_169, 15_518, 16_689, 458, 146, 41_358, 109, 452, 726, 4_034, 111, 763, 35_412, 5_082, 388, 1_903, 111, 9_051, 391, 2_870, 48_918, 1_900, 1_123, 550, 998, 112, 9_586, 15_985, 455, 391, 410, 22_955, 37_636, 114], [448, 17_496, 419, 3_663, 385, 763, 113, 27_533, 2_870, 3_283, 13_043, 1_639, 24_713, 523, 656, 24_013, 18_550, 2_521, 517, 27_014, 21_244, 420, 1_212, 1_465, 391, 927, 4_833, 388, 578, 11_786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2_169, 7_687, 21_932, 18_146, 726, 363, 17_032, 3_391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__UpperCAmelCase , model_name='''google/bert_for_seq_generation_L-24_bbc_encoder''' , revision='''c817d1fd1be2ffa69431227a1fe320544943d4db''' , )
26
1
from ...processing_utils import ProcessorMixin class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = '''WhisperFeatureExtractor''' __snake_case = '''WhisperTokenizer''' def __init__( self : int , __UpperCAmelCase : List[Any] , __UpperCAmelCase : str ) ->Tuple: """simple docstring""" super().__init__(__UpperCAmelCase , __UpperCAmelCase ) a = self.feature_extractor a = False def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : Any=None , __UpperCAmelCase : int=None , __UpperCAmelCase : Union[str, Any]=True ) ->List[str]: """simple docstring""" return self.tokenizer.get_decoder_prompt_ids(task=__UpperCAmelCase , language=__UpperCAmelCase , no_timestamps=__UpperCAmelCase ) def __call__( self : Optional[Any] , *__UpperCAmelCase : Optional[int] , **__UpperCAmelCase : Optional[Any] ) ->Tuple: """simple docstring""" if self._in_target_context_manager: return self.current_processor(*__UpperCAmelCase , **__UpperCAmelCase ) a = kwargs.pop('''audio''' , __UpperCAmelCase ) a = kwargs.pop('''sampling_rate''' , __UpperCAmelCase ) a = kwargs.pop('''text''' , __UpperCAmelCase ) if len(__UpperCAmelCase ) > 0: a = args[0] a = args[1:] if audio is None and text is None: raise ValueError('''You need to specify either an `audio` or `text` input to process.''' ) if audio is not None: a = self.feature_extractor(__UpperCAmelCase , *__UpperCAmelCase , sampling_rate=__UpperCAmelCase , **__UpperCAmelCase ) if text is not None: a = self.tokenizer(__UpperCAmelCase , **__UpperCAmelCase ) if text is None: return inputs elif audio is None: return encodings else: a = encodings['''input_ids'''] return inputs def __lowerCAmelCase ( self : str , *__UpperCAmelCase : Optional[Any] , **__UpperCAmelCase : Optional[Any] ) ->Tuple: """simple docstring""" return self.tokenizer.batch_decode(*__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : List[str] , *__UpperCAmelCase : List[str] , **__UpperCAmelCase : List[Any] ) ->Tuple: """simple docstring""" return self.tokenizer.decode(*__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : str , __UpperCAmelCase : Union[str, Any]="np" ) ->List[Any]: """simple docstring""" return self.tokenizer.get_prompt_ids(__UpperCAmelCase , return_tensors=__UpperCAmelCase )
26
import argparse import numpy as np import torch from transformers import SpeechTaHifiGan, SpeechTaHifiGanConfig, logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger("transformers.models.speecht5") def _a ( a :Optional[Any] , a :Tuple , a :Dict ) -> List[str]: hf_model.apply_weight_norm() a = checkpoint['''input_conv.weight_g'''] a = checkpoint['''input_conv.weight_v'''] a = checkpoint['''input_conv.bias'''] for i in range(len(config.upsample_rates ) ): a = checkpoint[F"""upsamples.{i}.1.weight_g"""] a = checkpoint[F"""upsamples.{i}.1.weight_v"""] a = checkpoint[F"""upsamples.{i}.1.bias"""] for i in range(len(config.upsample_rates ) * len(config.resblock_kernel_sizes ) ): for j in range(len(config.resblock_dilation_sizes ) ): a = checkpoint[F"""blocks.{i}.convs1.{j}.1.weight_g"""] a = checkpoint[F"""blocks.{i}.convs1.{j}.1.weight_v"""] a = checkpoint[F"""blocks.{i}.convs1.{j}.1.bias"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.weight_g"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.weight_v"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.bias"""] a = checkpoint['''output_conv.1.weight_g'''] a = checkpoint['''output_conv.1.weight_v'''] a = checkpoint['''output_conv.1.bias'''] hf_model.remove_weight_norm() @torch.no_grad() def _a ( a :List[str] , a :Union[str, Any] , a :Dict , a :Dict=None , a :List[Any]=None , ) -> int: if config_path is not None: a = SpeechTaHifiGanConfig.from_pretrained(a ) else: a = SpeechTaHifiGanConfig() a = SpeechTaHifiGan(a ) a = torch.load(a ) load_weights(orig_checkpoint['''model''']['''generator'''] , a , a ) a = np.load(a ) a = stats[0].reshape(-1 ) a = stats[1].reshape(-1 ) a = torch.from_numpy(a ).float() a = torch.from_numpy(a ).float() model.save_pretrained(a ) if repo_id: print('''Pushing to the hub...''' ) model.push_to_hub(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--checkpoint_path", required=True, default=None, type=str, help="Path to original checkpoint") parser.add_argument("--stats_path", required=True, default=None, type=str, help="Path to stats.npy file") 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." ) UpperCAmelCase__ = parser.parse_args() convert_hifigan_checkpoint( args.checkpoint_path, args.stats_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
26
1
import argparse from copy import deepcopy import numpy as np from datasets import ClassLabel, DatasetDict, load_dataset from evaluate import load from transformers import ( AutoModelForSequenceClassification, AutoTokenizer, DataCollatorWithPadding, Trainer, TrainerCallback, TrainingArguments, set_seed, ) def _a ( ) -> Optional[int]: a = argparse.ArgumentParser() parser.add_argument('''--model_ckpt''' , type=a , default='''microsoft/unixcoder-base-nine''' ) parser.add_argument('''--num_epochs''' , type=a , default=5 ) parser.add_argument('''--batch_size''' , type=a , default=6 ) parser.add_argument('''--gradient_accumulation_steps''' , type=a , default=1 ) parser.add_argument('''--freeze''' , type=a , default=a ) parser.add_argument('''--learning_rate''' , type=a , default=5e-4 ) parser.add_argument('''--seed''' , type=a , default=0 ) parser.add_argument('''--lr_scheduler_type''' , type=a , default='''cosine''' ) parser.add_argument('''--num_warmup_steps''' , type=a , default=10 ) parser.add_argument('''--weight_decay''' , type=a , default=0.01 ) parser.add_argument('''--output_dir''' , type=a , default='''./results''' ) return parser.parse_args() UpperCAmelCase__ = load("accuracy") def _a ( a :int ) -> Dict: a , a = eval_pred a = np.argmax(a , axis=1 ) return metric.compute(predictions=a , references=a ) class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Optional[int] , __UpperCAmelCase : List[str] ) ->None: """simple docstring""" super().__init__() a = trainer def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Optional[int] , **__UpperCAmelCase : List[Any] ) ->Optional[Any]: """simple docstring""" if control.should_evaluate: a = deepcopy(__UpperCAmelCase ) self._trainer.evaluate(eval_dataset=self._trainer.train_dataset , metric_key_prefix='''train''' ) return control_copy def _a ( ) -> Tuple: a = get_args() set_seed(args.seed ) a = load_dataset('''codeparrot/codecomplex''' , split='''train''' ) a = dataset.train_test_split(test_size=0.2 ) a = train_test['''test'''].train_test_split(test_size=0.5 ) a = DatasetDict( { '''train''': train_test['''train'''], '''test''': test_validation['''train'''], '''valid''': test_validation['''test'''], } ) print('''Loading tokenizer and model''' ) a = AutoTokenizer.from_pretrained(args.model_ckpt ) a = tokenizer.eos_token a = AutoModelForSequenceClassification.from_pretrained(args.model_ckpt , num_labels=7 ) a = model.config.eos_token_id if args.freeze: for param in model.roberta.parameters(): a = False a = ClassLabel(num_classes=7 , names=list(set(train_test_validation['''train''']['''complexity'''] ) ) ) def tokenize(a :Dict ): a = tokenizer(example['''src'''] , truncation=a , max_length=1_024 ) a = labels.straint(example['''complexity'''] ) return { "input_ids": inputs["input_ids"], "attention_mask": inputs["attention_mask"], "label": label, } a = train_test_validation.map( a , batched=a , remove_columns=train_test_validation['''train'''].column_names , ) a = DataCollatorWithPadding(tokenizer=a ) a = TrainingArguments( output_dir=args.output_dir , learning_rate=args.learning_rate , lr_scheduler_type=args.lr_scheduler_type , evaluation_strategy='''epoch''' , save_strategy='''epoch''' , logging_strategy='''epoch''' , per_device_train_batch_size=args.batch_size , per_device_eval_batch_size=args.batch_size , num_train_epochs=args.num_epochs , gradient_accumulation_steps=args.gradient_accumulation_steps , weight_decay=0.01 , metric_for_best_model='''accuracy''' , run_name='''complexity-java''' , report_to='''wandb''' , ) a = Trainer( model=a , args=a , train_dataset=tokenized_datasets['''train'''] , eval_dataset=tokenized_datasets['''valid'''] , tokenizer=a , data_collator=a , compute_metrics=a , ) print('''Training...''' ) trainer.add_callback(CustomCallback(a ) ) trainer.train() if __name__ == "__main__": main()
26
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) UpperCAmelCase__ = { "configuration_gpt_bigcode": ["GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP", "GPTBigCodeConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST", "GPTBigCodeForSequenceClassification", "GPTBigCodeForTokenClassification", "GPTBigCodeForCausalLM", "GPTBigCodeModel", "GPTBigCodePreTrainedModel", ] if TYPE_CHECKING: from .configuration_gpt_bigcode import GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTBigCodeConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_bigcode import ( GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTBigCodeForCausalLM, GPTBigCodeForSequenceClassification, GPTBigCodeForTokenClassification, GPTBigCodeModel, GPTBigCodePreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
1
import itertools import json import os import unittest from transformers import AddedToken, RobertaTokenizer, RobertaTokenizerFast from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase_ ( lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = RobertaTokenizer __snake_case = RobertaTokenizerFast __snake_case = True __snake_case = {'''cls_token''': '''<s>'''} def __lowerCAmelCase ( self : Tuple ) ->str: """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a = dict(zip(__UpperCAmelCase , range(len(__UpperCAmelCase ) ) ) ) a = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a = {'''unk_token''': '''<unk>'''} a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__UpperCAmelCase ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__UpperCAmelCase ) ) def __lowerCAmelCase ( self : Tuple , **__UpperCAmelCase : Dict ) ->Dict: """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__UpperCAmelCase ) def __lowerCAmelCase ( self : List[Any] , **__UpperCAmelCase : List[Any] ) ->Union[str, Any]: """simple docstring""" kwargs.update(self.special_tokens_map ) return RobertaTokenizerFast.from_pretrained(self.tmpdirname , **__UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Dict ) ->int: """simple docstring""" a = '''lower newer''' a = '''lower newer''' return input_text, output_text def __lowerCAmelCase ( self : str ) ->Tuple: """simple docstring""" a = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) a = '''lower newer''' a = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] a = tokenizer.tokenize(__UpperCAmelCase ) # , add_prefix_space=True) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) a = tokens + [tokenizer.unk_token] a = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) , __UpperCAmelCase ) def __lowerCAmelCase ( self : Any ) ->Optional[int]: """simple docstring""" a = self.get_tokenizer() self.assertListEqual(tokenizer.encode('''Hello world!''' , add_special_tokens=__UpperCAmelCase ) , [0, 31_414, 232, 328, 2] ) self.assertListEqual( tokenizer.encode('''Hello world! cécé herlolip 418''' , add_special_tokens=__UpperCAmelCase ) , [0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2] , ) @slow def __lowerCAmelCase ( self : Optional[Any] ) ->Optional[Any]: """simple docstring""" a = self.tokenizer_class.from_pretrained('''roberta-base''' ) a = tokenizer.encode('''sequence builders''' , add_special_tokens=__UpperCAmelCase ) a = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__UpperCAmelCase ) a = tokenizer.encode( '''sequence builders''' , add_special_tokens=__UpperCAmelCase , add_prefix_space=__UpperCAmelCase ) a = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__UpperCAmelCase , add_prefix_space=__UpperCAmelCase ) a = tokenizer.build_inputs_with_special_tokens(__UpperCAmelCase ) a = tokenizer.build_inputs_with_special_tokens(__UpperCAmelCase , __UpperCAmelCase ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def __lowerCAmelCase ( self : Optional[Any] ) ->Union[str, Any]: """simple docstring""" a = self.get_tokenizer() a = '''Encode this sequence.''' a = tokenizer.byte_encoder[''' '''.encode('''utf-8''' )[0]] # Testing encoder arguments a = tokenizer.encode(__UpperCAmelCase , add_special_tokens=__UpperCAmelCase , add_prefix_space=__UpperCAmelCase ) a = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(__UpperCAmelCase , __UpperCAmelCase ) a = tokenizer.encode(__UpperCAmelCase , add_special_tokens=__UpperCAmelCase , add_prefix_space=__UpperCAmelCase ) a = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(__UpperCAmelCase , __UpperCAmelCase ) tokenizer.add_special_tokens({'''bos_token''': '''<s>'''} ) a = tokenizer.encode(__UpperCAmelCase , add_special_tokens=__UpperCAmelCase ) a = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(__UpperCAmelCase , __UpperCAmelCase ) # Testing spaces after special tokens a = '''<mask>''' tokenizer.add_special_tokens( {'''mask_token''': AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase )} ) # mask token has a left space a = tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) a = '''Encode <mask> sequence''' a = '''Encode <mask>sequence''' a = tokenizer.encode(__UpperCAmelCase ) a = encoded.index(__UpperCAmelCase ) a = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(__UpperCAmelCase , __UpperCAmelCase ) a = tokenizer.encode(__UpperCAmelCase ) a = encoded.index(__UpperCAmelCase ) a = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" pass def __lowerCAmelCase ( self : Union[str, Any] ) ->List[Any]: """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): a = self.rust_tokenizer_class.from_pretrained(__UpperCAmelCase , **__UpperCAmelCase ) a = self.tokenizer_class.from_pretrained(__UpperCAmelCase , **__UpperCAmelCase ) a = '''A, <mask> AllenNLP sentence.''' a = tokenizer_r.encode_plus(__UpperCAmelCase , add_special_tokens=__UpperCAmelCase , return_token_type_ids=__UpperCAmelCase ) a = tokenizer_p.encode_plus(__UpperCAmelCase , add_special_tokens=__UpperCAmelCase , return_token_type_ids=__UpperCAmelCase ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['''token_type_ids'''] ) , sum(tokens_p['''token_type_ids'''] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['''attention_mask'''] ) / len(tokens_r['''attention_mask'''] ) , sum(tokens_p['''attention_mask'''] ) / len(tokens_p['''attention_mask'''] ) , ) a = tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids'''] ) a = tokenizer_p.convert_ids_to_tokens(tokens_p['''input_ids'''] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['''input_ids'''] , [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2] ) self.assertSequenceEqual(tokens_r['''input_ids'''] , [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2] ) self.assertSequenceEqual( __UpperCAmelCase , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) self.assertSequenceEqual( __UpperCAmelCase , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) def __lowerCAmelCase ( self : Any ) ->List[str]: """simple docstring""" for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): a = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=__UpperCAmelCase , add_prefix_space=__UpperCAmelCase , trim_offsets=__UpperCAmelCase ) a = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) a = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['''add_prefix_space'''] , __UpperCAmelCase ) self.assertEqual(post_processor_state['''add_prefix_space'''] , __UpperCAmelCase ) self.assertEqual(post_processor_state['''trim_offsets'''] , __UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[Any] ) ->int: """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): a = '''hello''' # `hello` is a token in the vocabulary of `pretrained_name` a = F"""{text_of_1_token} {text_of_1_token}""" a = self.rust_tokenizer_class.from_pretrained( __UpperCAmelCase , use_fast=__UpperCAmelCase , add_prefix_space=__UpperCAmelCase , trim_offsets=__UpperCAmelCase ) a = 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 )) , ) a = self.rust_tokenizer_class.from_pretrained( __UpperCAmelCase , use_fast=__UpperCAmelCase , add_prefix_space=__UpperCAmelCase , trim_offsets=__UpperCAmelCase ) a = 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 )) , ) a = self.rust_tokenizer_class.from_pretrained( __UpperCAmelCase , use_fast=__UpperCAmelCase , add_prefix_space=__UpperCAmelCase , trim_offsets=__UpperCAmelCase ) a = 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 ), len(__UpperCAmelCase ) + 1 + len(__UpperCAmelCase )) , ) a = self.rust_tokenizer_class.from_pretrained( __UpperCAmelCase , use_fast=__UpperCAmelCase , add_prefix_space=__UpperCAmelCase , trim_offsets=__UpperCAmelCase ) a = 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 ), len(__UpperCAmelCase ) + 1 + len(__UpperCAmelCase )) , ) a = F""" {text}""" # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) a = self.rust_tokenizer_class.from_pretrained( __UpperCAmelCase , use_fast=__UpperCAmelCase , add_prefix_space=__UpperCAmelCase , trim_offsets=__UpperCAmelCase ) a = 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 )) , ) a = self.rust_tokenizer_class.from_pretrained( __UpperCAmelCase , use_fast=__UpperCAmelCase , add_prefix_space=__UpperCAmelCase , trim_offsets=__UpperCAmelCase ) a = tokenizer_r(__UpperCAmelCase , return_offsets_mapping=__UpperCAmelCase , add_special_tokens=__UpperCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__UpperCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__UpperCAmelCase ), 1 + len(__UpperCAmelCase ) + 1 + len(__UpperCAmelCase )) , ) a = self.rust_tokenizer_class.from_pretrained( __UpperCAmelCase , use_fast=__UpperCAmelCase , add_prefix_space=__UpperCAmelCase , trim_offsets=__UpperCAmelCase ) a = tokenizer_r(__UpperCAmelCase , return_offsets_mapping=__UpperCAmelCase , add_special_tokens=__UpperCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__UpperCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__UpperCAmelCase ), 1 + len(__UpperCAmelCase ) + 1 + len(__UpperCAmelCase )) , )
26
import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def _a ( a :Tuple ) -> int: a = tmp_path / '''file.csv''' a = textwrap.dedent( '''\ header1,header2 1,2 10,20 ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :int ) -> List[str]: a = tmp_path / '''malformed_file.csv''' a = textwrap.dedent( '''\ header1,header2 1,2 10,20, ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :Dict , a :int ) -> List[str]: a = tmp_path / '''csv_with_image.csv''' a = textwrap.dedent( F"""\ image {image_file} """ ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :List[Any] ) -> Dict: a = tmp_path / '''csv_with_label.csv''' a = textwrap.dedent( '''\ label good bad good ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :Tuple ) -> Any: a = tmp_path / '''csv_with_int_list.csv''' a = textwrap.dedent( '''\ int_list 1 2 3 4 5 6 7 8 9 ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) def _a ( a :Dict , a :int , a :Union[str, Any] ) -> List[Any]: a = Csv() a = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(a , match='''Error tokenizing data''' ): for _ in generator: pass assert any( record.levelname == '''ERROR''' and '''Failed to read file''' in record.message and os.path.basename(a ) in record.message for record in caplog.records ) @require_pil def _a ( a :Dict ) -> Any: with open(a , encoding='''utf-8''' ) as f: a = f.read().splitlines()[1] a = Csv(encoding='''utf-8''' , features=Features({'''image''': Image()} ) ) a = csv._generate_tables([[csv_file_with_image]] ) a = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''image''' ).type == Image()() a = pa_table.to_pydict()['''image'''] assert generated_content == [{"path": image_file, "bytes": None}] def _a ( a :Any ) -> Tuple: with open(a , encoding='''utf-8''' ) as f: a = f.read().splitlines()[1:] a = Csv(encoding='''utf-8''' , features=Features({'''label''': ClassLabel(names=['''good''', '''bad'''] )} ) ) a = csv._generate_tables([[csv_file_with_label]] ) a = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''label''' ).type == ClassLabel(names=['''good''', '''bad'''] )() a = pa_table.to_pydict()['''label'''] assert generated_content == [ClassLabel(names=['''good''', '''bad'''] ).straint(a ) for label in labels] def _a ( a :Union[str, Any] ) -> Optional[Any]: a = Csv(encoding='''utf-8''' , sep=''',''' , converters={'''int_list''': lambda a : [int(a ) for i in x.split()]} ) a = csv._generate_tables([[csv_file_with_int_list]] ) a = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field('''int_list''' ).type ) a = pa_table.to_pydict()['''int_list'''] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
26
1
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = (DPMSolverSDEScheduler,) __snake_case = 10 def __lowerCAmelCase ( self : List[str] , **__UpperCAmelCase : int ) ->List[Any]: """simple docstring""" a = { '''num_train_timesteps''': 1_100, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', '''noise_sampler_seed''': 0, } config.update(**__UpperCAmelCase ) return config def __lowerCAmelCase ( self : Any ) ->List[str]: """simple docstring""" for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=__UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[Any] ) ->Union[str, Any]: """simple docstring""" for beta_start, beta_end in zip([0.00001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=__UpperCAmelCase , beta_end=__UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple ) ->Tuple: """simple docstring""" for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=__UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[Any] ) ->Tuple: """simple docstring""" for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[Any] ) ->Optional[Any]: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config() a = scheduler_class(**__UpperCAmelCase ) scheduler.set_timesteps(self.num_inference_steps ) a = self.dummy_model() a = self.dummy_sample_deter * scheduler.init_noise_sigma a = sample.to(__UpperCAmelCase ) for i, t in enumerate(scheduler.timesteps ): a = scheduler.scale_model_input(__UpperCAmelCase , __UpperCAmelCase ) a = model(__UpperCAmelCase , __UpperCAmelCase ) a = scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) a = output.prev_sample a = torch.sum(torch.abs(__UpperCAmelCase ) ) a = torch.mean(torch.abs(__UpperCAmelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.47821044921875 ) < 1e-2 assert abs(result_mean.item() - 0.2178705964565277 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59352111816406 ) < 1e-2 assert abs(result_mean.item() - 0.22342906892299652 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def __lowerCAmelCase ( self : Dict ) ->int: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config(prediction_type='''v_prediction''' ) a = scheduler_class(**__UpperCAmelCase ) scheduler.set_timesteps(self.num_inference_steps ) a = self.dummy_model() a = self.dummy_sample_deter * scheduler.init_noise_sigma a = sample.to(__UpperCAmelCase ) for i, t in enumerate(scheduler.timesteps ): a = scheduler.scale_model_input(__UpperCAmelCase , __UpperCAmelCase ) a = model(__UpperCAmelCase , __UpperCAmelCase ) a = scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) a = output.prev_sample a = torch.sum(torch.abs(__UpperCAmelCase ) ) a = torch.mean(torch.abs(__UpperCAmelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 124.77149200439453 ) < 1e-2 assert abs(result_mean.item() - 0.16226289014816284 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 128.1663360595703 ) < 1e-2 assert abs(result_mean.item() - 0.16688326001167297 ) < 1e-3 else: assert abs(result_sum.item() - 119.8487548828125 ) < 1e-2 assert abs(result_mean.item() - 0.1560530662536621 ) < 1e-3 def __lowerCAmelCase ( self : List[str] ) ->Optional[Any]: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config() a = scheduler_class(**__UpperCAmelCase ) scheduler.set_timesteps(self.num_inference_steps , device=__UpperCAmelCase ) a = self.dummy_model() a = self.dummy_sample_deter.to(__UpperCAmelCase ) * scheduler.init_noise_sigma for t in scheduler.timesteps: a = scheduler.scale_model_input(__UpperCAmelCase , __UpperCAmelCase ) a = model(__UpperCAmelCase , __UpperCAmelCase ) a = scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) a = output.prev_sample a = torch.sum(torch.abs(__UpperCAmelCase ) ) a = torch.mean(torch.abs(__UpperCAmelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.46957397460938 ) < 1e-2 assert abs(result_mean.item() - 0.21805934607982635 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59353637695312 ) < 1e-2 assert abs(result_mean.item() - 0.22342908382415771 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def __lowerCAmelCase ( self : int ) ->Union[str, Any]: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config() a = scheduler_class(**__UpperCAmelCase , use_karras_sigmas=__UpperCAmelCase ) scheduler.set_timesteps(self.num_inference_steps , device=__UpperCAmelCase ) a = self.dummy_model() a = self.dummy_sample_deter.to(__UpperCAmelCase ) * scheduler.init_noise_sigma a = sample.to(__UpperCAmelCase ) for t in scheduler.timesteps: a = scheduler.scale_model_input(__UpperCAmelCase , __UpperCAmelCase ) a = model(__UpperCAmelCase , __UpperCAmelCase ) a = scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) a = output.prev_sample a = torch.sum(torch.abs(__UpperCAmelCase ) ) a = torch.mean(torch.abs(__UpperCAmelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 176.66974135742188 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 177.63653564453125 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 else: assert abs(result_sum.item() - 170.3135223388672 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2
26
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SwiftFormerConfig, SwiftFormerForImageClassification, ViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = torch.device("cpu") def _a ( ) -> Union[str, Any]: a = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a = Image.open(requests.get(a , stream=a ).raw ) return im def _a ( a :Dict ) -> Tuple: if swiftformer_name == "swiftformer_xs": return torch.tensor([-2.1703e00, 2.1107e00, -2.0811e00, 8.8685e-01, 2.4360e-01] ) elif swiftformer_name == "swiftformer_s": return torch.tensor([3.9636e-01, 2.3478e-01, -1.6963e00, -1.7381e00, -8.6337e-01] ) elif swiftformer_name == "swiftformer_l1": return torch.tensor([-4.2768e-01, -4.7429e-01, -1.0897e00, -1.0248e00, 3.5523e-02] ) elif swiftformer_name == "swiftformer_l3": return torch.tensor([-2.5330e-01, 2.4211e-01, -6.0185e-01, -8.2789e-01, -6.0446e-02] ) def _a ( a :int , a :Any , a :Union[str, Any] ) -> int: a = dct.pop(a ) a = val def _a ( a :Any ) -> Dict: a = [] for k in state_dict.keys(): a = k if ".pwconv" in k: a = k_new.replace('''.pwconv''' , '''.point_wise_conv''' ) if ".dwconv" in k: a = k_new.replace('''.dwconv''' , '''.depth_wise_conv''' ) if ".Proj." in k: a = k_new.replace('''.Proj.''' , '''.proj.''' ) if "patch_embed" in k_new: a = k_new.replace('''patch_embed''' , '''swiftformer.patch_embed.patch_embedding''' ) if "network" in k_new: a = k_new.split('''.''' ) if ls[2].isdigit(): a = '''swiftformer.encoder.network.''' + ls[1] + '''.blocks.''' + ls[2] + '''.''' + '''.'''.join(ls[3:] ) else: a = k_new.replace('''network''' , '''swiftformer.encoder.network''' ) rename_keys.append((k, k_new) ) return rename_keys @torch.no_grad() def _a ( a :List[Any] , a :Tuple , a :List[str] ) -> Union[str, Any]: a = SwiftFormerConfig() # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size a = 1_000 a = '''huggingface/label-files''' a = '''imagenet-1k-id2label.json''' a = json.load(open(hf_hub_download(a , a , repo_type='''dataset''' ) , '''r''' ) ) a = {int(a ): v for k, v in idalabel.items()} a = idalabel a = {v: k for k, v in idalabel.items()} # size of the architecture if swiftformer_name == "swiftformer_xs": a = [3, 3, 6, 4] a = [48, 56, 112, 220] elif swiftformer_name == "swiftformer_s": a = [3, 3, 9, 6] a = [48, 64, 168, 224] elif swiftformer_name == "swiftformer_l1": a = [4, 3, 10, 5] a = [48, 96, 192, 384] elif swiftformer_name == "swiftformer_l3": a = [4, 4, 12, 6] a = [64, 128, 320, 512] # load state_dict of original model, remove and rename some keys if original_ckpt: if original_ckpt.startswith('''https''' ): a = torch.hub.load_state_dict_from_url(a , map_location='''cpu''' , check_hash=a ) else: a = torch.load(a , map_location='''cpu''' ) a = checkpoint a = create_rename_keys(a ) for rename_key_src, rename_key_dest in rename_keys: rename_key(a , a , a ) # load HuggingFace model a = SwiftFormerForImageClassification(a ).eval() hf_model.load_state_dict(a ) # prepare test inputs a = prepare_img() a = ViTImageProcessor.from_pretrained('''preprocessor_config''' ) a = processor(images=a , return_tensors='''pt''' ) # compare outputs from both models a = get_expected_output(a ) a = hf_model(inputs['''pixel_values'''] ).logits assert hf_logits.shape == torch.Size([1, 1_000] ) assert torch.allclose(hf_logits[0, 0:5] , a , atol=1e-3 ) Path(a ).mkdir(exist_ok=a ) print(F"""Saving model {swiftformer_name} to {pytorch_dump_folder_path}""" ) hf_model.save_pretrained(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--swiftformer_name", default="swiftformer_xs", choices=["swiftformer_xs", "swiftformer_s", "swiftformer_l1", "swiftformer_l3"], type=str, help="Name of the SwiftFormer model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default="./converted_outputs/", type=str, help="Path to the output PyTorch model directory.", ) parser.add_argument("--original_ckpt", default=None, type=str, help="Path to the original model checkpoint.") UpperCAmelCase__ = parser.parse_args() convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)
26
1
import json import os import shutil import tempfile import unittest from multiprocessing import get_context from pathlib import Path import datasets import numpy as np from datasets import load_dataset from parameterized import parameterized from transformers import AutoProcessor from transformers.models.wavaveca import WavaVecaCTCTokenizer, WavaVecaFeatureExtractor from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.testing_utils import require_pyctcdecode, require_torch, require_torchaudio, slow from transformers.utils import FEATURE_EXTRACTOR_NAME, is_pyctcdecode_available, is_torch_available from ..wavaveca.test_feature_extraction_wavaveca import floats_list if is_pyctcdecode_available(): from huggingface_hub import snapshot_download from pyctcdecode import BeamSearchDecoderCTC from transformers.models.wavaveca_with_lm import WavaVecaProcessorWithLM from transformers.models.wavaveca_with_lm.processing_wavaveca_with_lm import WavaVecaDecoderWithLMOutput if is_torch_available(): from transformers import WavaVecaForCTC @require_pyctcdecode class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self : Any ) ->Tuple: """simple docstring""" a = '''| <pad> <unk> <s> </s> a b c d e f g h i j k'''.split() a = dict(zip(__UpperCAmelCase , range(len(__UpperCAmelCase ) ) ) ) a = { '''unk_token''': '''<unk>''', '''bos_token''': '''<s>''', '''eos_token''': '''</s>''', } a = { '''feature_size''': 1, '''padding_value''': 0.0, '''sampling_rate''': 16_000, '''return_attention_mask''': False, '''do_normalize''': True, } a = tempfile.mkdtemp() a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) a = os.path.join(self.tmpdirname , __UpperCAmelCase ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__UpperCAmelCase ) + '''\n''' ) with open(self.feature_extraction_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__UpperCAmelCase ) + '''\n''' ) # load decoder from hub a = '''hf-internal-testing/ngram-beam-search-decoder''' def __lowerCAmelCase ( self : Optional[Any] , **__UpperCAmelCase : str ) ->Dict: """simple docstring""" a = self.add_kwargs_tokens_map.copy() kwargs.update(__UpperCAmelCase ) return WavaVecaCTCTokenizer.from_pretrained(self.tmpdirname , **__UpperCAmelCase ) def __lowerCAmelCase ( self : Any , **__UpperCAmelCase : List[str] ) ->Tuple: """simple docstring""" return WavaVecaFeatureExtractor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[int] , **__UpperCAmelCase : Union[str, Any] ) ->Any: """simple docstring""" return BeamSearchDecoderCTC.load_from_hf_hub(self.decoder_name , **__UpperCAmelCase ) def __lowerCAmelCase ( self : List[str] ) ->Optional[int]: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __lowerCAmelCase ( self : Any ) ->Tuple: """simple docstring""" a = self.get_tokenizer() a = self.get_feature_extractor() a = self.get_decoder() a = WavaVecaProcessorWithLM(tokenizer=__UpperCAmelCase , feature_extractor=__UpperCAmelCase , decoder=__UpperCAmelCase ) processor.save_pretrained(self.tmpdirname ) a = WavaVecaProcessorWithLM.from_pretrained(self.tmpdirname ) # tokenizer self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , __UpperCAmelCase ) # feature extractor self.assertEqual(processor.feature_extractor.to_json_string() , feature_extractor.to_json_string() ) self.assertIsInstance(processor.feature_extractor , __UpperCAmelCase ) # decoder self.assertEqual(processor.decoder._alphabet.labels , decoder._alphabet.labels ) self.assertEqual( processor.decoder.model_container[decoder._model_key]._unigram_set , decoder.model_container[decoder._model_key]._unigram_set , ) self.assertIsInstance(processor.decoder , __UpperCAmelCase ) def __lowerCAmelCase ( self : str ) ->List[Any]: """simple docstring""" a = WavaVecaProcessorWithLM( tokenizer=self.get_tokenizer() , feature_extractor=self.get_feature_extractor() , decoder=self.get_decoder() ) processor.save_pretrained(self.tmpdirname ) # make sure that error is thrown when decoder alphabet doesn't match a = WavaVecaProcessorWithLM.from_pretrained( self.tmpdirname , alpha=5.0 , beta=3.0 , score_boundary=-7.0 , unk_score_offset=3 ) # decoder self.assertEqual(processor.language_model.alpha , 5.0 ) self.assertEqual(processor.language_model.beta , 3.0 ) self.assertEqual(processor.language_model.score_boundary , -7.0 ) self.assertEqual(processor.language_model.unk_score_offset , 3 ) def __lowerCAmelCase ( self : Optional[Any] ) ->Tuple: """simple docstring""" a = self.get_tokenizer() # add token to trigger raise tokenizer.add_tokens(['''xx'''] ) with self.assertRaisesRegex(__UpperCAmelCase , '''include''' ): WavaVecaProcessorWithLM( tokenizer=__UpperCAmelCase , feature_extractor=self.get_feature_extractor() , decoder=self.get_decoder() ) def __lowerCAmelCase ( self : Tuple ) ->List[str]: """simple docstring""" a = self.get_feature_extractor() a = self.get_tokenizer() a = self.get_decoder() a = WavaVecaProcessorWithLM(tokenizer=__UpperCAmelCase , feature_extractor=__UpperCAmelCase , decoder=__UpperCAmelCase ) a = floats_list((3, 1_000) ) a = feature_extractor(__UpperCAmelCase , return_tensors='''np''' ) a = processor(__UpperCAmelCase , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowerCAmelCase ( self : Dict ) ->Union[str, Any]: """simple docstring""" a = self.get_feature_extractor() a = self.get_tokenizer() a = self.get_decoder() a = WavaVecaProcessorWithLM(tokenizer=__UpperCAmelCase , feature_extractor=__UpperCAmelCase , decoder=__UpperCAmelCase ) a = '''This is a test string''' a = processor(text=__UpperCAmelCase ) a = tokenizer(__UpperCAmelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Optional[int]=(2, 10, 16) , __UpperCAmelCase : Union[str, Any]=77 ) ->Tuple: """simple docstring""" np.random.seed(__UpperCAmelCase ) return np.random.rand(*__UpperCAmelCase ) def __lowerCAmelCase ( self : List[Any] ) ->Optional[Any]: """simple docstring""" a = self.get_feature_extractor() a = self.get_tokenizer() a = self.get_decoder() a = WavaVecaProcessorWithLM(tokenizer=__UpperCAmelCase , feature_extractor=__UpperCAmelCase , decoder=__UpperCAmelCase ) a = self._get_dummy_logits(shape=(10, 16) , seed=13 ) a = processor.decode(__UpperCAmelCase ) a = decoder.decode_beams(__UpperCAmelCase )[0] self.assertEqual(decoded_decoder[0] , decoded_processor.text ) self.assertEqual('''</s> <s> </s>''' , decoded_processor.text ) self.assertEqual(decoded_decoder[-2] , decoded_processor.logit_score ) self.assertEqual(decoded_decoder[-1] , decoded_processor.lm_score ) @parameterized.expand([[None], ['''fork'''], ['''spawn''']] ) def __lowerCAmelCase ( self : int , __UpperCAmelCase : int ) ->Dict: """simple docstring""" a = self.get_feature_extractor() a = self.get_tokenizer() a = self.get_decoder() a = WavaVecaProcessorWithLM(tokenizer=__UpperCAmelCase , feature_extractor=__UpperCAmelCase , decoder=__UpperCAmelCase ) a = self._get_dummy_logits() # note: pool should be instantiated *after* Wav2Vec2ProcessorWithLM. # otherwise, the LM won't be available to the pool's sub-processes. # manual logic used to allow parameterized test for both pool=None and pool=Pool(...) if pool_context is None: a = processor.batch_decode(__UpperCAmelCase ) else: with get_context(__UpperCAmelCase ).Pool() as pool: a = processor.batch_decode(__UpperCAmelCase , __UpperCAmelCase ) a = list(__UpperCAmelCase ) with get_context('''fork''' ).Pool() as p: a = decoder.decode_beams_batch(__UpperCAmelCase , __UpperCAmelCase ) a , a , a = [], [], [] for beams in decoded_beams: texts_decoder.append(beams[0][0] ) logit_scores_decoder.append(beams[0][-2] ) lm_scores_decoder.append(beams[0][-1] ) self.assertListEqual(__UpperCAmelCase , decoded_processor.text ) self.assertListEqual(['''<s> <s> </s>''', '''<s> <s> <s>'''] , decoded_processor.text ) self.assertListEqual(__UpperCAmelCase , decoded_processor.logit_score ) self.assertListEqual(__UpperCAmelCase , decoded_processor.lm_score ) def __lowerCAmelCase ( self : Union[str, Any] ) ->List[Any]: """simple docstring""" a = self.get_feature_extractor() a = self.get_tokenizer() a = self.get_decoder() a = WavaVecaProcessorWithLM(tokenizer=__UpperCAmelCase , feature_extractor=__UpperCAmelCase , decoder=__UpperCAmelCase ) a = self._get_dummy_logits() a = 15 a = -20.0 a = -4.0 a = processor.batch_decode( __UpperCAmelCase , beam_width=__UpperCAmelCase , beam_prune_logp=__UpperCAmelCase , token_min_logp=__UpperCAmelCase , ) a = decoded_processor_out.text a = list(__UpperCAmelCase ) with get_context('''fork''' ).Pool() as pool: a = decoder.decode_beams_batch( __UpperCAmelCase , __UpperCAmelCase , beam_width=__UpperCAmelCase , beam_prune_logp=__UpperCAmelCase , token_min_logp=__UpperCAmelCase , ) a = [d[0][0] for d in decoded_decoder_out] a = [d[0][2] for d in decoded_decoder_out] a = [d[0][3] for d in decoded_decoder_out] self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) self.assertListEqual(['''</s> <s> <s>''', '''<s> <s> <s>'''] , __UpperCAmelCase ) self.assertTrue(np.array_equal(__UpperCAmelCase , decoded_processor_out.logit_score ) ) self.assertTrue(np.allclose([-20.054, -18.447] , __UpperCAmelCase , atol=1e-3 ) ) self.assertTrue(np.array_equal(__UpperCAmelCase , decoded_processor_out.lm_score ) ) self.assertTrue(np.allclose([-15.554, -13.9474] , __UpperCAmelCase , atol=1e-3 ) ) def __lowerCAmelCase ( self : Any ) ->Optional[Any]: """simple docstring""" a = self.get_feature_extractor() a = self.get_tokenizer() a = self.get_decoder() a = WavaVecaProcessorWithLM(tokenizer=__UpperCAmelCase , feature_extractor=__UpperCAmelCase , decoder=__UpperCAmelCase ) a = self._get_dummy_logits() a = 2.0 a = 5.0 a = -20.0 a = True a = processor.batch_decode( __UpperCAmelCase , alpha=__UpperCAmelCase , beta=__UpperCAmelCase , unk_score_offset=__UpperCAmelCase , lm_score_boundary=__UpperCAmelCase , ) a = decoded_processor_out.text a = list(__UpperCAmelCase ) decoder.reset_params( alpha=__UpperCAmelCase , beta=__UpperCAmelCase , unk_score_offset=__UpperCAmelCase , lm_score_boundary=__UpperCAmelCase , ) with get_context('''fork''' ).Pool() as pool: a = decoder.decode_beams_batch( __UpperCAmelCase , __UpperCAmelCase , ) a = [d[0][0] for d in decoded_decoder_out] self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) self.assertListEqual(['''<s> </s> <s> </s> </s>''', '''</s> </s> <s> </s> </s>'''] , __UpperCAmelCase ) a = processor.decoder.model_container[processor.decoder._model_key] self.assertEqual(lm_model.alpha , 2.0 ) self.assertEqual(lm_model.beta , 5.0 ) self.assertEqual(lm_model.unk_score_offset , -20.0 ) self.assertEqual(lm_model.score_boundary , __UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] ) ->Tuple: """simple docstring""" a = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) a = processor.decoder.model_container[processor.decoder._model_key] a = Path(language_model._kenlm_model.path.decode('''utf-8''' ) ).parent.parent.absolute() a = os.listdir(__UpperCAmelCase ) a = ['''alphabet.json''', '''language_model'''] downloaded_decoder_files.sort() expected_decoder_files.sort() # test that only decoder relevant files from # https://huggingface.co/hf-internal-testing/processor_with_lm/tree/main # are downloaded and none of the rest (e.g. README.md, ...) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : int ) ->str: """simple docstring""" a = snapshot_download('''hf-internal-testing/processor_with_lm''' ) a = WavaVecaProcessorWithLM.from_pretrained(__UpperCAmelCase ) a = processor.decoder.model_container[processor.decoder._model_key] a = Path(language_model._kenlm_model.path.decode('''utf-8''' ) ).parent.parent.absolute() a = os.listdir(__UpperCAmelCase ) a = os.listdir(__UpperCAmelCase ) local_decoder_files.sort() expected_decoder_files.sort() # test that both decoder form hub and local files in cache are the same self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : int ) ->Any: """simple docstring""" a = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) a = AutoProcessor.from_pretrained('''hf-internal-testing/processor_with_lm''' ) a = floats_list((3, 1_000) ) a = processor_wavaveca(__UpperCAmelCase , return_tensors='''np''' ) a = processor_auto(__UpperCAmelCase , return_tensors='''np''' ) for key in input_wavaveca.keys(): self.assertAlmostEqual(input_wavaveca[key].sum() , input_auto[key].sum() , delta=1e-2 ) a = self._get_dummy_logits() a = processor_wavaveca.batch_decode(__UpperCAmelCase ) a = processor_auto.batch_decode(__UpperCAmelCase ) self.assertListEqual(decoded_wavaveca.text , decoded_auto.text ) def __lowerCAmelCase ( self : str ) ->Tuple: """simple docstring""" a = self.get_feature_extractor() a = self.get_tokenizer() a = self.get_decoder() a = WavaVecaProcessorWithLM(tokenizer=__UpperCAmelCase , feature_extractor=__UpperCAmelCase , decoder=__UpperCAmelCase ) self.assertListEqual( processor.model_input_names , feature_extractor.model_input_names , msg='''`processor` and `feature_extractor` model input names do not match''' , ) @staticmethod def __lowerCAmelCase ( __UpperCAmelCase : Tuple , __UpperCAmelCase : int ) ->Union[str, Any]: """simple docstring""" a = [d[key] for d in offsets] return retrieved_list def __lowerCAmelCase ( self : int ) ->str: """simple docstring""" a = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) a = self._get_dummy_logits()[0] a = processor.decode(__UpperCAmelCase , output_word_offsets=__UpperCAmelCase ) # check Wav2Vec2CTCTokenizerOutput keys for word self.assertEqual(len(outputs.keys() ) , 4 ) self.assertTrue('''text''' in outputs ) self.assertTrue('''word_offsets''' in outputs ) self.assertTrue(isinstance(__UpperCAmelCase , __UpperCAmelCase ) ) self.assertEqual(''' '''.join(self.get_from_offsets(outputs['''word_offsets'''] , '''word''' ) ) , outputs.text ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''] , '''word''' ) , ['''<s>''', '''<s>''', '''</s>'''] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''] , '''start_offset''' ) , [0, 2, 4] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''] , '''end_offset''' ) , [1, 3, 5] ) def __lowerCAmelCase ( self : Union[str, Any] ) ->List[str]: """simple docstring""" a = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) a = self._get_dummy_logits() a = processor.batch_decode(__UpperCAmelCase , output_word_offsets=__UpperCAmelCase ) # check Wav2Vec2CTCTokenizerOutput keys for word self.assertEqual(len(outputs.keys() ) , 4 ) self.assertTrue('''text''' in outputs ) self.assertTrue('''word_offsets''' in outputs ) self.assertTrue(isinstance(__UpperCAmelCase , __UpperCAmelCase ) ) self.assertListEqual( [''' '''.join(self.get_from_offsets(__UpperCAmelCase , '''word''' ) ) for o in outputs['''word_offsets''']] , outputs.text ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''][0] , '''word''' ) , ['''<s>''', '''<s>''', '''</s>'''] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''][0] , '''start_offset''' ) , [0, 2, 4] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''][0] , '''end_offset''' ) , [1, 3, 5] ) @slow @require_torch @require_torchaudio def __lowerCAmelCase ( self : List[Any] ) ->Union[str, Any]: """simple docstring""" import torch a = load_dataset('''common_voice''' , '''en''' , split='''train''' , streaming=__UpperCAmelCase ) a = ds.cast_column('''audio''' , datasets.Audio(sampling_rate=16_000 ) ) a = iter(__UpperCAmelCase ) a = next(__UpperCAmelCase ) a = AutoProcessor.from_pretrained('''patrickvonplaten/wav2vec2-base-100h-with-lm''' ) a = WavaVecaForCTC.from_pretrained('''patrickvonplaten/wav2vec2-base-100h-with-lm''' ) # compare to filename `common_voice_en_100038.mp3` of dataset viewer on https://huggingface.co/datasets/common_voice/viewer/en/train a = processor(sample['''audio''']['''array'''] , return_tensors='''pt''' ).input_values with torch.no_grad(): a = model(__UpperCAmelCase ).logits.cpu().numpy() a = processor.decode(logits[0] , output_word_offsets=__UpperCAmelCase ) a = model.config.inputs_to_logits_ratio / processor.feature_extractor.sampling_rate a = [ { '''start_time''': d['''start_offset'''] * time_offset, '''end_time''': d['''end_offset'''] * time_offset, '''word''': d['''word'''], } for d in output['''word_offsets'''] ] a = '''WHY DOES MILISANDRA LOOK LIKE SHE WANTS TO CONSUME JOHN SNOW ON THE RIVER AT THE WALL''' # output words self.assertEqual(''' '''.join(self.get_from_offsets(__UpperCAmelCase , '''word''' ) ) , __UpperCAmelCase ) self.assertEqual(''' '''.join(self.get_from_offsets(__UpperCAmelCase , '''word''' ) ) , output.text ) # output times a = torch.tensor(self.get_from_offsets(__UpperCAmelCase , '''start_time''' ) ) a = torch.tensor(self.get_from_offsets(__UpperCAmelCase , '''end_time''' ) ) # fmt: off a = torch.tensor([1.4199, 1.6599, 2.2599, 3.0, 3.24, 3.5999, 3.7999, 4.0999, 4.26, 4.94, 5.28, 5.6599, 5.78, 5.94, 6.32, 6.5399, 6.6599] ) a = torch.tensor([1.5399, 1.8999, 2.9, 3.16, 3.5399, 3.72, 4.0199, 4.1799, 4.76, 5.1599, 5.5599, 5.6999, 5.86, 6.1999, 6.38, 6.6199, 6.94] ) # fmt: on self.assertTrue(torch.allclose(__UpperCAmelCase , __UpperCAmelCase , atol=0.01 ) ) self.assertTrue(torch.allclose(__UpperCAmelCase , __UpperCAmelCase , atol=0.01 ) )
26
import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Union[str, Any] , __UpperCAmelCase : UNetaDModel , __UpperCAmelCase : UNetaDModel , __UpperCAmelCase : DDPMScheduler , __UpperCAmelCase : Optional[int] , ) ->List[str]: """simple docstring""" super().__init__() a = value_function a = unet a = scheduler a = env a = env.get_dataset() a = {} for key in self.data.keys(): try: a = self.data[key].mean() except: # noqa: E722 pass a = {} for key in self.data.keys(): try: a = self.data[key].std() except: # noqa: E722 pass a = env.observation_space.shape[0] a = env.action_space.shape[0] def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[int] ) ->Dict: """simple docstring""" return (x_in - self.means[key]) / self.stds[key] def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Dict , __UpperCAmelCase : Dict ) ->List[str]: """simple docstring""" return x_in * self.stds[key] + self.means[key] def __lowerCAmelCase ( self : int , __UpperCAmelCase : int ) ->List[str]: """simple docstring""" if type(__UpperCAmelCase ) is dict: return {k: self.to_torch(__UpperCAmelCase ) for k, v in x_in.items()} elif torch.is_tensor(__UpperCAmelCase ): return x_in.to(self.unet.device ) return torch.tensor(__UpperCAmelCase , device=self.unet.device ) def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : int , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Tuple ) ->int: """simple docstring""" for key, val in cond.items(): a = val.clone() return x_in def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[Any] ) ->Tuple: """simple docstring""" a = x.shape[0] a = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model a = torch.full((batch_size,) , __UpperCAmelCase , device=self.unet.device , dtype=torch.long ) for _ in range(__UpperCAmelCase ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models a = self.value_function(x.permute(0 , 2 , 1 ) , __UpperCAmelCase ).sample a = torch.autograd.grad([y.sum()] , [x] )[0] a = self.scheduler._get_variance(__UpperCAmelCase ) a = torch.exp(0.5 * posterior_variance ) a = model_std * grad a = 0 a = x.detach() a = x + scale * grad a = self.reset_xa(__UpperCAmelCase , __UpperCAmelCase , self.action_dim ) a = self.unet(x.permute(0 , 2 , 1 ) , __UpperCAmelCase ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg a = self.scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , predict_epsilon=__UpperCAmelCase )['''prev_sample'''] # apply conditions to the trajectory (set the initial state) a = self.reset_xa(__UpperCAmelCase , __UpperCAmelCase , self.action_dim ) a = self.to_torch(__UpperCAmelCase ) return x, y def __call__( self : Union[str, Any] , __UpperCAmelCase : Dict , __UpperCAmelCase : Optional[int]=64 , __UpperCAmelCase : int=32 , __UpperCAmelCase : Optional[Any]=2 , __UpperCAmelCase : str=0.1 ) ->List[str]: """simple docstring""" a = self.normalize(__UpperCAmelCase , '''observations''' ) a = obs[None].repeat(__UpperCAmelCase , axis=0 ) a = {0: self.to_torch(__UpperCAmelCase )} a = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) a = randn_tensor(__UpperCAmelCase , device=self.unet.device ) a = self.reset_xa(__UpperCAmelCase , __UpperCAmelCase , self.action_dim ) a = self.to_torch(__UpperCAmelCase ) # run the diffusion process a , a = self.run_diffusion(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) # sort output trajectories by value a = y.argsort(0 , descending=__UpperCAmelCase ).squeeze() a = x[sorted_idx] a = sorted_values[:, :, : self.action_dim] a = actions.detach().cpu().numpy() a = self.de_normalize(__UpperCAmelCase , key='''actions''' ) # select the action with the highest value if y is not None: a = 0 else: # if we didn't run value guiding, select a random action a = np.random.randint(0 , __UpperCAmelCase ) a = denorm_actions[selected_index, 0] return denorm_actions
26
1
import requests def _a ( a :str , a :str ) -> None: a = {'''Content-Type''': '''application/json'''} a = requests.post(a , json={'''text''': message_body} , headers=a ) if response.status_code != 200: a = ( '''Request to slack returned an error ''' F"""{response.status_code}, the response is:\n{response.text}""" ) raise ValueError(a ) if __name__ == "__main__": # Set the slack url to the one provided by Slack when you create the webhook at # https://my.slack.com/services/new/incoming-webhook/ send_slack_message("<YOUR MESSAGE BODY>", "<SLACK CHANNEL URL>")
26
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import SPIECE_UNDERLINE, logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {"vocab_file": "spiece.model"} UpperCAmelCase__ = { "vocab_file": { "TsinghuaAI/CPM-Generate": "https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model", } } class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Optional[Any] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Optional[Any]=False , __UpperCAmelCase : Any=True , __UpperCAmelCase : Optional[Any]=False , __UpperCAmelCase : List[str]="<s>" , __UpperCAmelCase : int="</s>" , __UpperCAmelCase : Any="<unk>" , __UpperCAmelCase : Optional[Any]="<sep>" , __UpperCAmelCase : int="<pad>" , __UpperCAmelCase : Any="<cls>" , __UpperCAmelCase : List[str]="<mask>" , __UpperCAmelCase : Optional[int]=["<eop>", "<eod>"] , __UpperCAmelCase : Optional[Dict[str, Any]] = None , **__UpperCAmelCase : Union[str, Any] , ) ->None: """simple docstring""" a = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else mask_token a = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__UpperCAmelCase , remove_space=__UpperCAmelCase , keep_accents=__UpperCAmelCase , bos_token=__UpperCAmelCase , eos_token=__UpperCAmelCase , unk_token=__UpperCAmelCase , sep_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , cls_token=__UpperCAmelCase , mask_token=__UpperCAmelCase , additional_special_tokens=__UpperCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **__UpperCAmelCase , ) a = 3 a = do_lower_case a = remove_space a = keep_accents a = vocab_file a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__UpperCAmelCase ) try: import jieba except ModuleNotFoundError as error: raise error.__class__( '''You need to install jieba to use CpmTokenizer or CpmTokenizerFast. ''' '''See https://pypi.org/project/jieba/ for installation.''' ) a = jieba a = str.maketrans(''' \n''' , '''\u2582\u2583''' ) @property # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size def __lowerCAmelCase ( self : Union[str, Any] ) ->Optional[Any]: """simple docstring""" return len(self.sp_model ) def __lowerCAmelCase ( self : Tuple ) ->List[str]: """simple docstring""" a = {self.convert_ids_to_tokens(__UpperCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) ->Optional[int]: """simple docstring""" a = self.__dict__.copy() a = None return state def __setstate__( self : List[str] , __UpperCAmelCase : Optional[int] ) ->str: """simple docstring""" a = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): a = {} a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : List[str] ) ->List[str]: """simple docstring""" if self.remove_space: a = ''' '''.join(inputs.strip().split() ) else: a = inputs a = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: a = unicodedata.normalize('''NFKD''' , __UpperCAmelCase ) a = ''''''.join([c for c in outputs if not unicodedata.combining(__UpperCAmelCase )] ) if self.do_lower_case: a = outputs.lower() return outputs def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : str ) ->List[str]: """simple docstring""" a = self.preprocess_text(__UpperCAmelCase ) a = self.sp_model.encode(__UpperCAmelCase , out_type=__UpperCAmelCase ) a = [] for piece in pieces: if len(__UpperCAmelCase ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): a = self.sp_model.EncodeAsPieces(piece[:-1].replace(__UpperCAmelCase , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: a = cur_pieces[1:] else: a = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(__UpperCAmelCase ) else: new_pieces.append(__UpperCAmelCase ) return new_pieces def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Any ) ->Any: """simple docstring""" return self.sp_model.PieceToId(__UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : Dict ) ->Union[str, Any]: """simple docstring""" return self.sp_model.IdToPiece(__UpperCAmelCase ) def __lowerCAmelCase ( self : Any , __UpperCAmelCase : str ) ->List[str]: """simple docstring""" a = ''''''.join(__UpperCAmelCase ).replace(__UpperCAmelCase , ''' ''' ).strip() return out_string def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None , __UpperCAmelCase : bool = False ) ->List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__UpperCAmelCase , token_ids_a=__UpperCAmelCase , already_has_special_tokens=__UpperCAmelCase ) if token_ids_a is not None: return ([0] * len(__UpperCAmelCase )) + [1] + ([0] * len(__UpperCAmelCase )) + [1, 1] return ([0] * len(__UpperCAmelCase )) + [1, 1] def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None ) ->Tuple[str]: """simple docstring""" if not os.path.isdir(__UpperCAmelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return a = os.path.join( __UpperCAmelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__UpperCAmelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __UpperCAmelCase ) elif not os.path.isfile(self.vocab_file ): with open(__UpperCAmelCase , '''wb''' ) as fi: a = self.sp_model.serialized_model_proto() fi.write(__UpperCAmelCase ) return (out_vocab_file,) def __lowerCAmelCase ( self : Any , *__UpperCAmelCase : List[str] , **__UpperCAmelCase : Optional[Any] ) ->Tuple: """simple docstring""" a = super()._decode(*__UpperCAmelCase , **__UpperCAmelCase ) a = text.replace(''' ''' , '''''' ).replace('''\u2582''' , ''' ''' ).replace('''\u2583''' , '''\n''' ) return text
26
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase__ = { "configuration_nllb_moe": [ "NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP", "NllbMoeConfig", ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST", "NllbMoeForConditionalGeneration", "NllbMoeModel", "NllbMoePreTrainedModel", "NllbMoeTop2Router", "NllbMoeSparseMLP", ] if TYPE_CHECKING: from .configuration_nllb_moe import ( NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP, NllbMoeConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_nllb_moe import ( NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST, NllbMoeForConditionalGeneration, NllbMoeModel, NllbMoePreTrainedModel, NllbMoeSparseMLP, NllbMoeTopaRouter, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
import argparse import io import requests import torch from omegaconf import OmegaConf from diffusers import AutoencoderKL from diffusers.pipelines.stable_diffusion.convert_from_ckpt import ( assign_to_checkpoint, conv_attn_to_linear, create_vae_diffusers_config, renew_vae_attention_paths, renew_vae_resnet_paths, ) def _a ( a :Union[str, Any] , a :List[Any] ) -> List[Any]: a = checkpoint a = {} a = vae_state_dict['''encoder.conv_in.weight'''] a = vae_state_dict['''encoder.conv_in.bias'''] a = vae_state_dict['''encoder.conv_out.weight'''] a = vae_state_dict['''encoder.conv_out.bias'''] a = vae_state_dict['''encoder.norm_out.weight'''] a = vae_state_dict['''encoder.norm_out.bias'''] a = vae_state_dict['''decoder.conv_in.weight'''] a = vae_state_dict['''decoder.conv_in.bias'''] a = vae_state_dict['''decoder.conv_out.weight'''] a = vae_state_dict['''decoder.conv_out.bias'''] a = vae_state_dict['''decoder.norm_out.weight'''] a = vae_state_dict['''decoder.norm_out.bias'''] a = vae_state_dict['''quant_conv.weight'''] a = vae_state_dict['''quant_conv.bias'''] a = vae_state_dict['''post_quant_conv.weight'''] a = vae_state_dict['''post_quant_conv.bias'''] # Retrieves the keys for the encoder down blocks only a = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''encoder.down''' in layer} ) a = { layer_id: [key for key in vae_state_dict if F"""down.{layer_id}""" in key] for layer_id in range(a ) } # Retrieves the keys for the decoder up blocks only a = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''decoder.up''' in layer} ) a = { layer_id: [key for key in vae_state_dict if F"""up.{layer_id}""" in key] for layer_id in range(a ) } for i in range(a ): a = [key for key in down_blocks[i] if F"""down.{i}""" in key and F"""down.{i}.downsample""" not in key] if F"""encoder.down.{i}.downsample.conv.weight""" in vae_state_dict: a = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.weight""" ) a = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.bias""" ) a = renew_vae_resnet_paths(a ) a = {'''old''': F"""down.{i}.block""", '''new''': F"""down_blocks.{i}.resnets"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''encoder.mid.block''' in key] a = 2 for i in range(1 , num_mid_res_blocks + 1 ): a = [key for key in mid_resnets if F"""encoder.mid.block_{i}""" in key] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""mid.block_{i}""", '''new''': F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''encoder.mid.attn''' in key] a = renew_vae_attention_paths(a ) a = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) conv_attn_to_linear(a ) for i in range(a ): a = num_up_blocks - 1 - i a = [ key for key in up_blocks[block_id] if F"""up.{block_id}""" in key and F"""up.{block_id}.upsample""" not in key ] if F"""decoder.up.{block_id}.upsample.conv.weight""" in vae_state_dict: a = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.weight""" ] a = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.bias""" ] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""up.{block_id}.block""", '''new''': F"""up_blocks.{i}.resnets"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''decoder.mid.block''' in key] a = 2 for i in range(1 , num_mid_res_blocks + 1 ): a = [key for key in mid_resnets if F"""decoder.mid.block_{i}""" in key] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""mid.block_{i}""", '''new''': F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''decoder.mid.attn''' in key] a = renew_vae_attention_paths(a ) a = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) conv_attn_to_linear(a ) return new_checkpoint def _a ( a :str , a :str , ) -> List[str]: # Only support V1 a = requests.get( ''' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml''' ) a = io.BytesIO(r.content ) a = OmegaConf.load(a ) a = 512 a = '''cuda''' if torch.cuda.is_available() else '''cpu''' if checkpoint_path.endswith('''safetensors''' ): from safetensors import safe_open a = {} with safe_open(a , framework='''pt''' , device='''cpu''' ) as f: for key in f.keys(): a = f.get_tensor(a ) else: a = torch.load(a , map_location=a )['''state_dict'''] # Convert the VAE model. a = create_vae_diffusers_config(a , image_size=a ) a = custom_convert_ldm_vae_checkpoint(a , a ) a = AutoencoderKL(**a ) vae.load_state_dict(a ) vae.save_pretrained(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--vae_pt_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") UpperCAmelCase__ = parser.parse_args() vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)
26
1
import logging import math from functools import partial from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union import torch from .tensor_utils import tensor_tree_map, tree_map def _a ( a :Union[dict, list, tuple, torch.Tensor] ) -> List[Tuple[int, ...]]: a = [] if isinstance(a , a ): for v in tree.values(): shapes.extend(_fetch_dims(a ) ) elif isinstance(a , (list, tuple) ): for t in tree: shapes.extend(_fetch_dims(a ) ) elif isinstance(a , torch.Tensor ): shapes.append(tree.shape ) else: raise ValueError('''Not supported''' ) return shapes @torch.jit.ignore def _a ( a :int , a :Tuple[int, ...] ) -> Tuple[int, ...]: a = [] for d in reversed(a ): idx.append(flat_idx % d ) a = flat_idx // d return tuple(reversed(a ) ) @torch.jit.ignore def _a ( a :Sequence[int] , a :Sequence[int] , a :Sequence[int] , a :Optional[Sequence[bool]] = None , a :Optional[Sequence[bool]] = None , ) -> List[Tuple[slice, ...]]: # start_edges and end_edges both indicate whether, starting from any given # dimension, the start/end index is at the top/bottom edge of the # corresponding tensor, modeled as a tree def reduce_edge_list(a :List[bool] ) -> None: a = True for i in range(len(a ) ): a = -1 * (i + 1) l[reversed_idx] &= tally a = l[reversed_idx] if start_edges is None: a = [s == 0 for s in start] reduce_edge_list(a ) if end_edges is None: a = [e == (d - 1) for e, d in zip(a , a )] reduce_edge_list(a ) # Base cases. Either start/end are empty and we're done, or the final, # one-dimensional tensor can be simply sliced if len(a ) == 0: return [()] elif len(a ) == 1: return [(slice(start[0] , end[0] + 1 ),)] a = [] a = [] # Dimensions common to start and end can be selected directly for s, e in zip(a , a ): if s == e: path_list.append(slice(a , s + 1 ) ) else: break a = tuple(a ) a = len(a ) # start == end, and we're done if divergence_idx == len(a ): return [path] def upper() -> Tuple[Tuple[slice, ...], ...]: assert start_edges is not None assert end_edges is not None a = start[divergence_idx] return tuple( path + (slice(a , sdi + 1 ),) + s for s in _get_minimal_slice_set( start[divergence_idx + 1 :] , [d - 1 for d in dims[divergence_idx + 1 :]] , dims[divergence_idx + 1 :] , start_edges=start_edges[divergence_idx + 1 :] , end_edges=[True for _ in end_edges[divergence_idx + 1 :]] , ) ) def lower() -> Tuple[Tuple[slice, ...], ...]: assert start_edges is not None assert end_edges is not None a = end[divergence_idx] return tuple( path + (slice(a , edi + 1 ),) + s for s in _get_minimal_slice_set( [0 for _ in start[divergence_idx + 1 :]] , end[divergence_idx + 1 :] , dims[divergence_idx + 1 :] , start_edges=[True for _ in start_edges[divergence_idx + 1 :]] , end_edges=end_edges[divergence_idx + 1 :] , ) ) # If both start and end are at the edges of the subtree rooted at # divergence_idx, we can just select the whole subtree at once if start_edges[divergence_idx] and end_edges[divergence_idx]: slices.append(path + (slice(start[divergence_idx] , end[divergence_idx] + 1 ),) ) # If just start is at the edge, we can grab almost all of the subtree, # treating only the ragged bottom edge as an edge case elif start_edges[divergence_idx]: slices.append(path + (slice(start[divergence_idx] , end[divergence_idx] ),) ) slices.extend(lower() ) # Analogous to the previous case, but the top is ragged this time elif end_edges[divergence_idx]: slices.extend(upper() ) slices.append(path + (slice(start[divergence_idx] + 1 , end[divergence_idx] + 1 ),) ) # If both sides of the range are ragged, we need to handle both sides # separately. If there's contiguous meat in between them, we can index it # in one big chunk else: slices.extend(upper() ) a = end[divergence_idx] - start[divergence_idx] if middle_ground > 1: slices.append(path + (slice(start[divergence_idx] + 1 , end[divergence_idx] ),) ) slices.extend(lower() ) return slices @torch.jit.ignore def _a ( a :torch.Tensor , a :int , a :int , a :int ) -> torch.Tensor: a = t.shape[:no_batch_dims] a = list(_flat_idx_to_idx(a , a ) ) # _get_minimal_slice_set is inclusive a = list(_flat_idx_to_idx(flat_end - 1 , a ) ) # Get an ordered list of slices to perform a = _get_minimal_slice_set( a , a , a , ) a = [t[s] for s in slices] return torch.cat([s.view((-1,) + t.shape[no_batch_dims:] ) for s in sliced_tensors] ) def _a ( a :Callable , a :Dict[str, Any] , a :int , a :int , a :bool = False , a :Any = None , a :bool = False , ) -> Any: if not (len(a ) > 0): raise ValueError('''Must provide at least one input''' ) a = [shape[:no_batch_dims] for shape in _fetch_dims(a )] a = tuple([max(a ) for s in zip(*a )] ) def _prep_inputs(a :torch.Tensor ) -> torch.Tensor: if not low_mem: if not sum(t.shape[:no_batch_dims] ) == no_batch_dims: a = t.expand(orig_batch_dims + t.shape[no_batch_dims:] ) a = t.reshape(-1 , *t.shape[no_batch_dims:] ) else: a = t.expand(orig_batch_dims + t.shape[no_batch_dims:] ) return t a = tensor_tree_map(_prep_inputs , a ) a = None if _out is not None: a = tensor_tree_map(lambda a : t.view([-1] + list(t.shape[no_batch_dims:] ) ) , _out ) a = 1 for d in orig_batch_dims: flat_batch_dim *= d a = flat_batch_dim // chunk_size + (flat_batch_dim % chunk_size != 0) def _select_chunk(a :torch.Tensor ) -> torch.Tensor: return t[i : i + chunk_size] if t.shape[0] != 1 else t a = 0 a = prepped_outputs for _ in range(a ): # Chunk the input if not low_mem: a = _select_chunk else: a = partial( _chunk_slice , flat_start=a , flat_end=min(a , i + chunk_size ) , no_batch_dims=len(a ) , ) a = tensor_tree_map(a , a ) # Run the layer on the chunk a = layer(**a ) # Allocate space for the output if out is None: a = tensor_tree_map(lambda a : t.new_zeros((flat_batch_dim,) + t.shape[1:] ) , a ) # Put the chunk in its pre-allocated space if isinstance(a , a ): def assign(a :dict , a :dict ) -> None: for k, v in da.items(): if isinstance(a , a ): assign(a , da[k] ) else: if _add_into_out: v[i : i + chunk_size] += da[k] else: a = da[k] assign(a , a ) elif isinstance(a , a ): for xa, xa in zip(a , a ): if _add_into_out: xa[i : i + chunk_size] += xa else: a = xa elif isinstance(a , torch.Tensor ): if _add_into_out: out[i : i + chunk_size] += output_chunk else: a = output_chunk else: raise ValueError('''Not supported''' ) i += chunk_size a = tensor_tree_map(lambda a : t.view(orig_batch_dims + t.shape[1:] ) , a ) return out class lowercase_ : '''simple docstring''' def __init__( self : Optional[Any] , __UpperCAmelCase : int = 512 , ) ->Union[str, Any]: """simple docstring""" a = max_chunk_size a = None a = None def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : Callable , __UpperCAmelCase : tuple , __UpperCAmelCase : int ) ->int: """simple docstring""" logging.info('''Tuning chunk size...''' ) if min_chunk_size >= self.max_chunk_size: return min_chunk_size a = [2**l for l in range(int(math.log(self.max_chunk_size , 2 ) ) + 1 )] a = [c for c in candidates if c > min_chunk_size] a = [min_chunk_size] + candidates candidates[-1] += 4 def test_chunk_size(__UpperCAmelCase : int ) -> bool: try: with torch.no_grad(): fn(*__UpperCAmelCase , chunk_size=__UpperCAmelCase ) return True except RuntimeError: return False a = 0 a = len(__UpperCAmelCase ) - 1 while i > min_viable_chunk_size_index: a = test_chunk_size(candidates[i] ) if not viable: a = (min_viable_chunk_size_index + i) // 2 else: a = i a = (i + len(__UpperCAmelCase ) - 1) // 2 return candidates[min_viable_chunk_size_index] def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : Iterable , __UpperCAmelCase : Iterable ) ->bool: """simple docstring""" a = True for aa, aa in zip(__UpperCAmelCase , __UpperCAmelCase ): assert type(__UpperCAmelCase ) == type(__UpperCAmelCase ) if isinstance(__UpperCAmelCase , (list, tuple) ): consistent &= self._compare_arg_caches(__UpperCAmelCase , __UpperCAmelCase ) elif isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = [v for _, v in sorted(aa.items() , key=lambda __UpperCAmelCase : x[0] )] a = [v for _, v in sorted(aa.items() , key=lambda __UpperCAmelCase : x[0] )] consistent &= self._compare_arg_caches(__UpperCAmelCase , __UpperCAmelCase ) else: consistent &= aa == aa return consistent def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Callable , __UpperCAmelCase : tuple , __UpperCAmelCase : int , ) ->int: """simple docstring""" a = True a = tree_map(lambda __UpperCAmelCase : a.shape if isinstance(__UpperCAmelCase , torch.Tensor ) else a , __UpperCAmelCase , __UpperCAmelCase ) if self.cached_arg_data is not None: # If args have changed shape/value, we need to re-tune assert len(self.cached_arg_data ) == len(__UpperCAmelCase ) a = self._compare_arg_caches(self.cached_arg_data , __UpperCAmelCase ) else: # Otherwise, we can reuse the precomputed value a = False if not consistent: a = self._determine_favorable_chunk_size( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , ) a = arg_data assert self.cached_chunk_size is not None return self.cached_chunk_size
26
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = ['''image_processor''', '''tokenizer'''] __snake_case = '''CLIPImageProcessor''' __snake_case = ('''CLIPTokenizer''', '''CLIPTokenizerFast''') def __init__( self : Dict , __UpperCAmelCase : str=None , __UpperCAmelCase : Optional[Any]=None , **__UpperCAmelCase : Optional[Any] ) ->List[str]: """simple docstring""" a = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __UpperCAmelCase , ) a = kwargs.pop('''feature_extractor''' ) a = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(__UpperCAmelCase , __UpperCAmelCase ) def __call__( self : List[str] , __UpperCAmelCase : Any=None , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Any=None , **__UpperCAmelCase : str ) ->Optional[Any]: """simple docstring""" if text is None and images is None: raise ValueError('''You have to specify either text or images. Both cannot be none.''' ) if text is not None: a = self.tokenizer(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase ) if images is not None: a = self.image_processor(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase ) if text is not None and images is not None: a = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__UpperCAmelCase ) , tensor_type=__UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple , *__UpperCAmelCase : List[str] , **__UpperCAmelCase : Union[str, Any] ) ->Union[str, Any]: """simple docstring""" return self.tokenizer.batch_decode(*__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple , *__UpperCAmelCase : str , **__UpperCAmelCase : Tuple ) ->Any: """simple docstring""" return self.tokenizer.decode(*__UpperCAmelCase , **__UpperCAmelCase ) @property def __lowerCAmelCase ( self : int ) ->List[str]: """simple docstring""" a = self.tokenizer.model_input_names a = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def __lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __UpperCAmelCase , ) return self.image_processor_class @property def __lowerCAmelCase ( self : Union[str, Any] ) ->Optional[int]: """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __UpperCAmelCase , ) return self.image_processor
26
1
import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Union[str, Any] , __UpperCAmelCase : UNetaDModel , __UpperCAmelCase : UNetaDModel , __UpperCAmelCase : DDPMScheduler , __UpperCAmelCase : Optional[int] , ) ->List[str]: """simple docstring""" super().__init__() a = value_function a = unet a = scheduler a = env a = env.get_dataset() a = {} for key in self.data.keys(): try: a = self.data[key].mean() except: # noqa: E722 pass a = {} for key in self.data.keys(): try: a = self.data[key].std() except: # noqa: E722 pass a = env.observation_space.shape[0] a = env.action_space.shape[0] def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[int] ) ->Dict: """simple docstring""" return (x_in - self.means[key]) / self.stds[key] def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Dict , __UpperCAmelCase : Dict ) ->List[str]: """simple docstring""" return x_in * self.stds[key] + self.means[key] def __lowerCAmelCase ( self : int , __UpperCAmelCase : int ) ->List[str]: """simple docstring""" if type(__UpperCAmelCase ) is dict: return {k: self.to_torch(__UpperCAmelCase ) for k, v in x_in.items()} elif torch.is_tensor(__UpperCAmelCase ): return x_in.to(self.unet.device ) return torch.tensor(__UpperCAmelCase , device=self.unet.device ) def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : int , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Tuple ) ->int: """simple docstring""" for key, val in cond.items(): a = val.clone() return x_in def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[Any] ) ->Tuple: """simple docstring""" a = x.shape[0] a = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model a = torch.full((batch_size,) , __UpperCAmelCase , device=self.unet.device , dtype=torch.long ) for _ in range(__UpperCAmelCase ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models a = self.value_function(x.permute(0 , 2 , 1 ) , __UpperCAmelCase ).sample a = torch.autograd.grad([y.sum()] , [x] )[0] a = self.scheduler._get_variance(__UpperCAmelCase ) a = torch.exp(0.5 * posterior_variance ) a = model_std * grad a = 0 a = x.detach() a = x + scale * grad a = self.reset_xa(__UpperCAmelCase , __UpperCAmelCase , self.action_dim ) a = self.unet(x.permute(0 , 2 , 1 ) , __UpperCAmelCase ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg a = self.scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , predict_epsilon=__UpperCAmelCase )['''prev_sample'''] # apply conditions to the trajectory (set the initial state) a = self.reset_xa(__UpperCAmelCase , __UpperCAmelCase , self.action_dim ) a = self.to_torch(__UpperCAmelCase ) return x, y def __call__( self : Union[str, Any] , __UpperCAmelCase : Dict , __UpperCAmelCase : Optional[int]=64 , __UpperCAmelCase : int=32 , __UpperCAmelCase : Optional[Any]=2 , __UpperCAmelCase : str=0.1 ) ->List[str]: """simple docstring""" a = self.normalize(__UpperCAmelCase , '''observations''' ) a = obs[None].repeat(__UpperCAmelCase , axis=0 ) a = {0: self.to_torch(__UpperCAmelCase )} a = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) a = randn_tensor(__UpperCAmelCase , device=self.unet.device ) a = self.reset_xa(__UpperCAmelCase , __UpperCAmelCase , self.action_dim ) a = self.to_torch(__UpperCAmelCase ) # run the diffusion process a , a = self.run_diffusion(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) # sort output trajectories by value a = y.argsort(0 , descending=__UpperCAmelCase ).squeeze() a = x[sorted_idx] a = sorted_values[:, :, : self.action_dim] a = actions.detach().cpu().numpy() a = self.de_normalize(__UpperCAmelCase , key='''actions''' ) # select the action with the highest value if y is not None: a = 0 else: # if we didn't run value guiding, select a random action a = np.random.randint(0 , __UpperCAmelCase ) a = denorm_actions[selected_index, 0] return denorm_actions
26
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_distilbert import DistilBertTokenizer UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} UpperCAmelCase__ = { "vocab_file": { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt" ), "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt", "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt" ), }, "tokenizer_file": { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json" ), "distilbert-base-german-cased": ( "https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json" ), "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json" ), }, } UpperCAmelCase__ = { "distilbert-base-uncased": 512, "distilbert-base-uncased-distilled-squad": 512, "distilbert-base-cased": 512, "distilbert-base-cased-distilled-squad": 512, "distilbert-base-german-cased": 512, "distilbert-base-multilingual-cased": 512, } UpperCAmelCase__ = { "distilbert-base-uncased": {"do_lower_case": True}, "distilbert-base-uncased-distilled-squad": {"do_lower_case": True}, "distilbert-base-cased": {"do_lower_case": False}, "distilbert-base-cased-distilled-squad": {"do_lower_case": False}, "distilbert-base-german-cased": {"do_lower_case": False}, "distilbert-base-multilingual-cased": {"do_lower_case": False}, } class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = VOCAB_FILES_NAMES __snake_case = PRETRAINED_VOCAB_FILES_MAP __snake_case = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __snake_case = PRETRAINED_INIT_CONFIGURATION __snake_case = ['''input_ids''', '''attention_mask'''] __snake_case = DistilBertTokenizer def __init__( self : Dict , __UpperCAmelCase : List[Any]=None , __UpperCAmelCase : Optional[int]=None , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Optional[int]="[UNK]" , __UpperCAmelCase : str="[SEP]" , __UpperCAmelCase : Tuple="[PAD]" , __UpperCAmelCase : Any="[CLS]" , __UpperCAmelCase : int="[MASK]" , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Optional[Any]=None , **__UpperCAmelCase : str , ) ->Optional[int]: """simple docstring""" super().__init__( __UpperCAmelCase , tokenizer_file=__UpperCAmelCase , do_lower_case=__UpperCAmelCase , unk_token=__UpperCAmelCase , sep_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , cls_token=__UpperCAmelCase , mask_token=__UpperCAmelCase , tokenize_chinese_chars=__UpperCAmelCase , strip_accents=__UpperCAmelCase , **__UpperCAmelCase , ) a = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , __UpperCAmelCase ) != do_lower_case or normalizer_state.get('''strip_accents''' , __UpperCAmelCase ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , __UpperCAmelCase ) != tokenize_chinese_chars ): a = getattr(__UpperCAmelCase , normalizer_state.pop('''type''' ) ) a = do_lower_case a = strip_accents a = tokenize_chinese_chars a = normalizer_class(**__UpperCAmelCase ) a = do_lower_case def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[int]=None ) ->Optional[Any]: """simple docstring""" a = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [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 __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None ) ->Tuple[str]: """simple docstring""" a = self._tokenizer.model.save(__UpperCAmelCase , name=__UpperCAmelCase ) return tuple(__UpperCAmelCase )
26
1
import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def _a ( a :Any , a :Optional[int] ) -> int: assert isinstance(a , a ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def _a ( a :Optional[Any] , a :Any , a :Union[str, Any] ) -> Optional[Any]: a = tmp_path / '''cache''' a = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): a = ParquetDatasetReader(a , cache_dir=a , keep_in_memory=a ).read() _check_parquet_dataset(a , a ) @pytest.mark.parametrize( '''features''' , [ None, {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}, {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''}, {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''}, {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''}, ] , ) def _a ( a :Optional[int] , a :Any , a :Optional[Any] ) -> Any: a = tmp_path / '''cache''' a = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} a = features.copy() if features else default_expected_features a = ( Features({feature: Value(a ) for feature, dtype in features.items()} ) if features is not None else None ) a = ParquetDatasetReader(a , features=a , cache_dir=a ).read() _check_parquet_dataset(a , a ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def _a ( a :Any , a :int , a :Tuple ) -> List[Any]: a = tmp_path / '''cache''' a = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} a = ParquetDatasetReader(a , cache_dir=a , split=a ).read() _check_parquet_dataset(a , a ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('''path_type''' , [str, list] ) def _a ( a :List[str] , a :List[str] , a :Optional[Any] ) -> List[Any]: if issubclass(a , a ): a = parquet_path elif issubclass(a , a ): a = [parquet_path] a = tmp_path / '''cache''' a = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} a = ParquetDatasetReader(a , cache_dir=a ).read() _check_parquet_dataset(a , a ) def _a ( a :str , a :Optional[Any] , a :int=("train",) ) -> Union[str, Any]: assert isinstance(a , a ) for split in splits: a = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def _a ( a :Union[str, Any] , a :Any , a :Union[str, Any] ) -> int: a = tmp_path / '''cache''' a = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): a = ParquetDatasetReader( {'''train''': parquet_path} , cache_dir=a , keep_in_memory=a ).read() _check_parquet_datasetdict(a , a ) @pytest.mark.parametrize( '''features''' , [ None, {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}, {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''}, {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''}, {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''}, ] , ) def _a ( a :Optional[Any] , a :Any , a :Dict ) -> Any: a = tmp_path / '''cache''' a = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} a = features.copy() if features else default_expected_features a = ( Features({feature: Value(a ) for feature, dtype in features.items()} ) if features is not None else None ) a = ParquetDatasetReader({'''train''': parquet_path} , features=a , cache_dir=a ).read() _check_parquet_datasetdict(a , a ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def _a ( a :Optional[int] , a :List[Any] , a :Tuple ) -> Tuple: if split: a = {split: parquet_path} else: a = '''train''' a = {'''train''': parquet_path, '''test''': parquet_path} a = tmp_path / '''cache''' a = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} a = ParquetDatasetReader(a , cache_dir=a ).read() _check_parquet_datasetdict(a , a , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def _a ( a :List[str] , a :Tuple ) -> List[Any]: a = ParquetDatasetWriter(a , tmp_path / '''foo.parquet''' ) assert writer.write() > 0 a = pq.ParquetFile(tmp_path / '''foo.parquet''' ) a = pf.read() assert dataset.data.table == output_table def _a ( a :List[Any] , a :int ) -> Optional[int]: a = str(shared_datadir / '''test_image_rgb.jpg''' ) a = {'''image''': [image_path]} a = Features({'''image''': Image()} ) a = Dataset.from_dict(a , features=a ) a = ParquetDatasetWriter(a , tmp_path / '''foo.parquet''' ) assert writer.write() > 0 a = Dataset.from_parquet(str(tmp_path / '''foo.parquet''' ) ) assert dataset.features == reloaded_dataset.features a = ParquetDatasetReader(str(tmp_path / '''foo.parquet''' ) , streaming=a ).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( '''feature, expected''' , [ (Features({'''foo''': Value('''int32''' )} ), None), (Features({'''image''': Image(), '''foo''': Value('''int32''' )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({'''nested''': Sequence(Audio() )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] , ) def _a ( a :str , a :Tuple ) -> List[str]: assert get_writer_batch_size(a ) == expected
26
from __future__ import annotations import typing from collections import Counter def _a ( a :int ) -> typing.Counter[int]: a = Counter() for base in range(1 , max_perimeter + 1 ): for perpendicular in range(a , max_perimeter + 1 ): a = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(a ): a = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def _a ( a :int = 1_000 ) -> int: a = pythagorean_triple(a ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(f"""Perimeter {solution()} has maximum solutions""")
26
1
import argparse import json import os import pickle import shutil import numpy as np import torch from distiller import Distiller from lm_seqs_dataset import LmSeqsDataset from transformers import ( BertConfig, BertForMaskedLM, BertTokenizer, DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer, GPTaConfig, GPTaLMHeadModel, GPTaTokenizer, RobertaConfig, RobertaForMaskedLM, RobertaTokenizer, ) from utils import git_log, init_gpu_params, logger, set_seed UpperCAmelCase__ = { "distilbert": (DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer), "roberta": (RobertaConfig, RobertaForMaskedLM, RobertaTokenizer), "bert": (BertConfig, BertForMaskedLM, BertTokenizer), "gpt2": (GPTaConfig, GPTaLMHeadModel, GPTaTokenizer), } def _a ( a :str ) -> str: assert (args.mlm and args.alpha_mlm > 0.0) or (not args.mlm and args.alpha_mlm == 0.0) assert (args.alpha_mlm > 0.0 and args.alpha_clm == 0.0) or (args.alpha_mlm == 0.0 and args.alpha_clm > 0.0) if args.mlm: assert os.path.isfile(args.token_counts ) assert (args.student_type in ["roberta", "distilbert"]) and (args.teacher_type in ["roberta", "bert"]) else: assert (args.student_type in ["gpt2"]) and (args.teacher_type in ["gpt2"]) assert args.teacher_type == args.student_type or ( args.student_type == "distilbert" and args.teacher_type == "bert" ) assert os.path.isfile(args.student_config ) if args.student_pretrained_weights is not None: assert os.path.isfile(args.student_pretrained_weights ) if args.freeze_token_type_embds: assert args.student_type in ["roberta"] assert args.alpha_ce >= 0.0 assert args.alpha_mlm >= 0.0 assert args.alpha_clm >= 0.0 assert args.alpha_mse >= 0.0 assert args.alpha_cos >= 0.0 assert args.alpha_ce + args.alpha_mlm + args.alpha_clm + args.alpha_mse + args.alpha_cos > 0.0 def _a ( a :Any , a :Tuple ) -> Tuple: if args.student_type == "roberta": a = False elif args.student_type == "gpt2": a = False def _a ( a :Optional[int] , a :str ) -> List[str]: if args.student_type == "roberta": a = False def _a ( ) -> int: a = argparse.ArgumentParser(description='''Training''' ) parser.add_argument('''--force''' , action='''store_true''' , help='''Overwrite dump_path if it already exists.''' ) parser.add_argument( '''--dump_path''' , type=a , required=a , help='''The output directory (log, checkpoints, parameters, etc.)''' ) parser.add_argument( '''--data_file''' , type=a , required=a , help='''The binarized file (tokenized + tokens_to_ids) and grouped by sequence.''' , ) parser.add_argument( '''--student_type''' , type=a , choices=['''distilbert''', '''roberta''', '''gpt2'''] , required=a , help='''The student type (DistilBERT, RoBERTa).''' , ) parser.add_argument('''--student_config''' , type=a , required=a , help='''Path to the student configuration.''' ) parser.add_argument( '''--student_pretrained_weights''' , default=a , type=a , help='''Load student initialization checkpoint.''' ) parser.add_argument( '''--teacher_type''' , choices=['''bert''', '''roberta''', '''gpt2'''] , required=a , help='''Teacher type (BERT, RoBERTa).''' ) parser.add_argument('''--teacher_name''' , type=a , required=a , help='''The teacher model.''' ) parser.add_argument('''--temperature''' , default=2.0 , type=a , help='''Temperature for the softmax temperature.''' ) parser.add_argument( '''--alpha_ce''' , default=0.5 , type=a , help='''Linear weight for the distillation loss. Must be >=0.''' ) parser.add_argument( '''--alpha_mlm''' , default=0.0 , type=a , help='''Linear weight for the MLM loss. Must be >=0. Should be used in conjunction with `mlm` flag.''' , ) parser.add_argument('''--alpha_clm''' , default=0.5 , type=a , help='''Linear weight for the CLM loss. Must be >=0.''' ) parser.add_argument('''--alpha_mse''' , default=0.0 , type=a , help='''Linear weight of the MSE loss. Must be >=0.''' ) parser.add_argument( '''--alpha_cos''' , default=0.0 , type=a , help='''Linear weight of the cosine embedding loss. Must be >=0.''' ) parser.add_argument( '''--mlm''' , action='''store_true''' , help='''The LM step: MLM or CLM. If `mlm` is True, the MLM is used over CLM.''' ) parser.add_argument( '''--mlm_mask_prop''' , default=0.15 , type=a , help='''Proportion of tokens for which we need to make a prediction.''' , ) parser.add_argument('''--word_mask''' , default=0.8 , type=a , help='''Proportion of tokens to mask out.''' ) parser.add_argument('''--word_keep''' , default=0.1 , type=a , help='''Proportion of tokens to keep.''' ) parser.add_argument('''--word_rand''' , default=0.1 , type=a , help='''Proportion of tokens to randomly replace.''' ) parser.add_argument( '''--mlm_smoothing''' , default=0.7 , type=a , help='''Smoothing parameter to emphasize more rare tokens (see XLM, similar to word2vec).''' , ) parser.add_argument('''--token_counts''' , type=a , help='''The token counts in the data_file for MLM.''' ) parser.add_argument( '''--restrict_ce_to_mask''' , action='''store_true''' , help='''If true, compute the distillation loss only the [MLM] prediction distribution.''' , ) parser.add_argument( '''--freeze_pos_embs''' , action='''store_true''' , help='''Freeze positional embeddings during distillation. For student_type in [\'roberta\', \'gpt2\'] only.''' , ) parser.add_argument( '''--freeze_token_type_embds''' , action='''store_true''' , help='''Freeze token type embeddings during distillation if existent. For student_type in [\'roberta\'] only.''' , ) parser.add_argument('''--n_epoch''' , type=a , default=3 , help='''Number of pass on the whole dataset.''' ) parser.add_argument('''--batch_size''' , type=a , default=5 , help='''Batch size (for each process).''' ) parser.add_argument( '''--group_by_size''' , action='''store_false''' , help='''If true, group sequences that have similar length into the same batch. Default is true.''' , ) parser.add_argument( '''--gradient_accumulation_steps''' , type=a , default=50 , help='''Gradient accumulation for larger training batches.''' , ) parser.add_argument('''--warmup_prop''' , default=0.05 , type=a , help='''Linear warmup proportion.''' ) parser.add_argument('''--weight_decay''' , default=0.0 , type=a , help='''Weight decay if we apply some.''' ) parser.add_argument('''--learning_rate''' , default=5e-4 , type=a , help='''The initial learning rate for Adam.''' ) parser.add_argument('''--adam_epsilon''' , default=1e-6 , type=a , help='''Epsilon for Adam optimizer.''' ) parser.add_argument('''--max_grad_norm''' , default=5.0 , type=a , help='''Max gradient norm.''' ) parser.add_argument('''--initializer_range''' , default=0.02 , type=a , help='''Random initialization range.''' ) parser.add_argument( '''--fp16''' , action='''store_true''' , help='''Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit''' , ) parser.add_argument( '''--fp16_opt_level''' , type=a , default='''O1''' , help=( '''For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\'].''' '''See details at https://nvidia.github.io/apex/amp.html''' ) , ) parser.add_argument('''--n_gpu''' , type=a , default=1 , help='''Number of GPUs in the node.''' ) parser.add_argument('''--local_rank''' , type=a , default=-1 , help='''Distributed training - Local rank''' ) parser.add_argument('''--seed''' , type=a , default=56 , help='''Random seed''' ) parser.add_argument('''--log_interval''' , type=a , default=500 , help='''Tensorboard logging interval.''' ) parser.add_argument('''--checkpoint_interval''' , type=a , default=4_000 , help='''Checkpoint interval.''' ) a = parser.parse_args() sanity_checks(a ) # ARGS # init_gpu_params(a ) set_seed(a ) if args.is_master: if os.path.exists(args.dump_path ): if not args.force: raise ValueError( F"""Serialization dir {args.dump_path} already exists, but you have not precised wheter to overwrite""" ''' itUse `--force` if you want to overwrite it''' ) else: shutil.rmtree(args.dump_path ) if not os.path.exists(args.dump_path ): os.makedirs(args.dump_path ) logger.info(F"""Experiment will be dumped and logged in {args.dump_path}""" ) # SAVE PARAMS # logger.info(F"""Param: {args}""" ) with open(os.path.join(args.dump_path , '''parameters.json''' ) , '''w''' ) as f: json.dump(vars(a ) , a , indent=4 ) git_log(args.dump_path ) a , a , a = MODEL_CLASSES[args.student_type] a , a , a = MODEL_CLASSES[args.teacher_type] # TOKENIZER # a = teacher_tokenizer_class.from_pretrained(args.teacher_name ) a = {} for tok_name, tok_symbol in tokenizer.special_tokens_map.items(): a = tokenizer.all_special_tokens.index(a ) a = tokenizer.all_special_ids[idx] logger.info(F"""Special tokens {special_tok_ids}""" ) a = special_tok_ids a = tokenizer.max_model_input_sizes[args.teacher_name] # DATA LOADER # logger.info(F"""Loading data from {args.data_file}""" ) with open(args.data_file , '''rb''' ) as fp: a = pickle.load(a ) if args.mlm: logger.info(F"""Loading token counts from {args.token_counts} (already pre-computed)""" ) with open(args.token_counts , '''rb''' ) as fp: a = pickle.load(a ) a = np.maximum(a , 1 ) ** -args.mlm_smoothing for idx in special_tok_ids.values(): a = 0.0 # do not predict special tokens a = torch.from_numpy(a ) else: a = None a = LmSeqsDataset(params=a , data=a ) logger.info('''Data loader created.''' ) # STUDENT # logger.info(F"""Loading student config from {args.student_config}""" ) a = student_config_class.from_pretrained(args.student_config ) a = True if args.student_pretrained_weights is not None: logger.info(F"""Loading pretrained weights from {args.student_pretrained_weights}""" ) a = student_model_class.from_pretrained(args.student_pretrained_weights , config=a ) else: a = student_model_class(a ) if args.n_gpu > 0: student.to(F"""cuda:{args.local_rank}""" ) logger.info('''Student loaded.''' ) # TEACHER # a = teacher_model_class.from_pretrained(args.teacher_name , output_hidden_states=a ) if args.n_gpu > 0: teacher.to(F"""cuda:{args.local_rank}""" ) logger.info(F"""Teacher loaded from {args.teacher_name}.""" ) # FREEZING # if args.freeze_pos_embs: freeze_pos_embeddings(a , a ) if args.freeze_token_type_embds: freeze_token_type_embeddings(a , a ) # SANITY CHECKS # assert student.config.vocab_size == teacher.config.vocab_size assert student.config.hidden_size == teacher.config.hidden_size assert student.config.max_position_embeddings == teacher.config.max_position_embeddings if args.mlm: assert token_probs.size(0 ) == stu_architecture_config.vocab_size # DISTILLER # torch.cuda.empty_cache() a = Distiller( params=a , dataset=a , token_probs=a , student=a , teacher=a ) distiller.train() logger.info('''Let\'s go get some drinks.''' ) if __name__ == "__main__": main()
26
from __future__ import annotations def _a ( a :dict , a :str ) -> set[str]: a , a = set(a ), [start] while stack: a = stack.pop() explored.add(a ) # 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(a ) return explored UpperCAmelCase__ = { "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"))
26
1
from __future__ import annotations UpperCAmelCase__ = 1.6_021E-19 # units = C def _a ( a :float , a :float , a :float , ) -> tuple[str, float]: if (conductivity, electron_conc, mobility).count(0 ) != 1: raise ValueError('''You cannot supply more or less than 2 values''' ) elif conductivity < 0: raise ValueError('''Conductivity cannot be negative''' ) elif electron_conc < 0: raise ValueError('''Electron concentration cannot be negative''' ) elif mobility < 0: raise ValueError('''mobility cannot be negative''' ) elif conductivity == 0: return ( "conductivity", mobility * electron_conc * ELECTRON_CHARGE, ) elif electron_conc == 0: return ( "electron_conc", conductivity / (mobility * ELECTRON_CHARGE), ) else: return ( "mobility", conductivity / (electron_conc * ELECTRON_CHARGE), ) if __name__ == "__main__": import doctest doctest.testmod()
26
import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm UpperCAmelCase__ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex UpperCAmelCase__ = 10 UpperCAmelCase__ = 256 def _a ( a :List[str] ) -> Optional[MinHash]: if len(a ) < MIN_NUM_TOKENS: return None a = MinHash(num_perm=a ) for token in set(a ): min_hash.update(token.encode() ) return min_hash def _a ( a :str ) -> Set[str]: return {t for t in NON_ALPHA.split(a ) if len(t.strip() ) > 0} class lowercase_ : '''simple docstring''' def __init__( self : Any , *, __UpperCAmelCase : float = 0.85 , ) ->Dict: """simple docstring""" a = duplication_jaccard_threshold a = NUM_PERM a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) a = defaultdict(__UpperCAmelCase ) def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Tuple , __UpperCAmelCase : MinHash ) ->None: """simple docstring""" a = self._index.query(__UpperCAmelCase ) if code_key in self._index.keys: print(F"""Duplicate key {code_key}""" ) return self._index.insert(__UpperCAmelCase , __UpperCAmelCase ) if len(__UpperCAmelCase ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(__UpperCAmelCase ) break else: self._duplicate_clusters[close_duplicates[0]].add(__UpperCAmelCase ) def __lowerCAmelCase ( self : Dict ) ->List[List[Dict]]: """simple docstring""" a = [] for base, duplicates in self._duplicate_clusters.items(): a = [base] + list(__UpperCAmelCase ) # reformat the cluster to be a list of dict a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(__UpperCAmelCase ) return duplicate_clusters def __lowerCAmelCase ( self : Any , __UpperCAmelCase : Dict ) ->None: """simple docstring""" a = self.get_duplicate_clusters() with open(__UpperCAmelCase , '''w''' ) as f: json.dump(__UpperCAmelCase , __UpperCAmelCase ) def _a ( a :List[Any] ) -> List[Any]: a , a = element a = get_min_hash([t for t in NON_ALPHA.split(data['''content'''] ) if len(t.strip() ) > 0] ) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def _a ( a :Type[Dataset] ) -> List[Any]: with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(a , max_queue_size=10_000 ) , chunksize=100 , ): if data is not None: yield data def _a ( a :Type[Dataset] , a :float ) -> str: a = DuplicationIndex(duplication_jaccard_threshold=a ) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(a ) ) , max_queue_size=100 ) ): di.add(a , a ) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def _a ( a :str , a :str ) -> float: a = get_tokens(a ) a = get_tokens(a ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) UpperCAmelCase__ = None def _a ( a :Tuple , a :Tuple ) -> Any: a = [] for elementa in cluster: a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(a , a ) >= jaccard_threshold: elementa["copies"] += 1 break else: a = 1 extremes.append(a ) return extremes def _a ( a :List[Any] , a :Optional[Any] , a :Union[str, Any] ) -> Optional[int]: global _shared_dataset a = dataset a = [] a = partial(_find_cluster_extremes_shared , jaccard_threshold=a ) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( a , a , ) , total=len(a ) , ): extremes_list.append(a ) return extremes_list def _a ( a :Type[Dataset] , a :float = 0.85 ) -> Tuple[Type[Dataset], List[List[Dict]]]: a = make_duplicate_clusters(a , a ) a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} a = {} a = find_extremes(a , a , a ) for extremes in extremes_clusters: for element in extremes: a = element a = duplicate_indices - set(extreme_dict.keys() ) a = dataset.filter(lambda a , a : idx not in remove_indices , with_indices=a ) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: a = element['''base_index'''] in extreme_dict if element["is_extreme"]: a = extreme_dict[element['''base_index''']]['''copies'''] print(F"""Original dataset size: {len(a )}""" ) print(F"""Number of duplicate clusters: {len(a )}""" ) print(F"""Files in duplicate cluster: {len(a )}""" ) print(F"""Unique files in duplicate cluster: {len(a )}""" ) print(F"""Filtered dataset size: {len(a )}""" ) return ds_filter, duplicate_clusters
26
1
import requests from bsa import BeautifulSoup def _a ( a :str = "AAPL" ) -> str: a = F"""https://in.finance.yahoo.com/quote/{symbol}?s={symbol}""" a = BeautifulSoup(requests.get(a ).text , '''html.parser''' ) a = '''My(6px) Pos(r) smartphone_Mt(6px)''' return soup.find('''div''' , class_=class_ ).find('''span''' ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(f"""Current {symbol:<4} stock price is {stock_price(symbol):>8}""")
26
from math import ceil, sqrt def _a ( a :int = 1_000_000 ) -> int: a = 0 for outer_width in range(3 , (limit // 4) + 2 ): if outer_width**2 > limit: a = max(ceil(sqrt(outer_width**2 - limit ) ) , 1 ) else: a = 1 if (outer_width - hole_width_lower_bound) % 2: hole_width_lower_bound += 1 answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 return answer if __name__ == "__main__": print(f"""{solution() = }""")
26
1
import argparse import numpy as np import torch from transformers import SpeechTaHifiGan, SpeechTaHifiGanConfig, logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger("transformers.models.speecht5") def _a ( a :Optional[Any] , a :Tuple , a :Dict ) -> List[str]: hf_model.apply_weight_norm() a = checkpoint['''input_conv.weight_g'''] a = checkpoint['''input_conv.weight_v'''] a = checkpoint['''input_conv.bias'''] for i in range(len(config.upsample_rates ) ): a = checkpoint[F"""upsamples.{i}.1.weight_g"""] a = checkpoint[F"""upsamples.{i}.1.weight_v"""] a = checkpoint[F"""upsamples.{i}.1.bias"""] for i in range(len(config.upsample_rates ) * len(config.resblock_kernel_sizes ) ): for j in range(len(config.resblock_dilation_sizes ) ): a = checkpoint[F"""blocks.{i}.convs1.{j}.1.weight_g"""] a = checkpoint[F"""blocks.{i}.convs1.{j}.1.weight_v"""] a = checkpoint[F"""blocks.{i}.convs1.{j}.1.bias"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.weight_g"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.weight_v"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.bias"""] a = checkpoint['''output_conv.1.weight_g'''] a = checkpoint['''output_conv.1.weight_v'''] a = checkpoint['''output_conv.1.bias'''] hf_model.remove_weight_norm() @torch.no_grad() def _a ( a :List[str] , a :Union[str, Any] , a :Dict , a :Dict=None , a :List[Any]=None , ) -> int: if config_path is not None: a = SpeechTaHifiGanConfig.from_pretrained(a ) else: a = SpeechTaHifiGanConfig() a = SpeechTaHifiGan(a ) a = torch.load(a ) load_weights(orig_checkpoint['''model''']['''generator'''] , a , a ) a = np.load(a ) a = stats[0].reshape(-1 ) a = stats[1].reshape(-1 ) a = torch.from_numpy(a ).float() a = torch.from_numpy(a ).float() model.save_pretrained(a ) if repo_id: print('''Pushing to the hub...''' ) model.push_to_hub(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--checkpoint_path", required=True, default=None, type=str, help="Path to original checkpoint") parser.add_argument("--stats_path", required=True, default=None, type=str, help="Path to stats.npy file") 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." ) UpperCAmelCase__ = parser.parse_args() convert_hifigan_checkpoint( args.checkpoint_path, args.stats_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
26
UpperCAmelCase__ = "0.21.0" from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
26
1
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_distilbert import DistilBertTokenizer UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} UpperCAmelCase__ = { "vocab_file": { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt" ), "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt", "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt" ), }, "tokenizer_file": { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json" ), "distilbert-base-german-cased": ( "https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json" ), "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json" ), }, } UpperCAmelCase__ = { "distilbert-base-uncased": 512, "distilbert-base-uncased-distilled-squad": 512, "distilbert-base-cased": 512, "distilbert-base-cased-distilled-squad": 512, "distilbert-base-german-cased": 512, "distilbert-base-multilingual-cased": 512, } UpperCAmelCase__ = { "distilbert-base-uncased": {"do_lower_case": True}, "distilbert-base-uncased-distilled-squad": {"do_lower_case": True}, "distilbert-base-cased": {"do_lower_case": False}, "distilbert-base-cased-distilled-squad": {"do_lower_case": False}, "distilbert-base-german-cased": {"do_lower_case": False}, "distilbert-base-multilingual-cased": {"do_lower_case": False}, } class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = VOCAB_FILES_NAMES __snake_case = PRETRAINED_VOCAB_FILES_MAP __snake_case = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __snake_case = PRETRAINED_INIT_CONFIGURATION __snake_case = ['''input_ids''', '''attention_mask'''] __snake_case = DistilBertTokenizer def __init__( self : Dict , __UpperCAmelCase : List[Any]=None , __UpperCAmelCase : Optional[int]=None , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Optional[int]="[UNK]" , __UpperCAmelCase : str="[SEP]" , __UpperCAmelCase : Tuple="[PAD]" , __UpperCAmelCase : Any="[CLS]" , __UpperCAmelCase : int="[MASK]" , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Optional[Any]=None , **__UpperCAmelCase : str , ) ->Optional[int]: """simple docstring""" super().__init__( __UpperCAmelCase , tokenizer_file=__UpperCAmelCase , do_lower_case=__UpperCAmelCase , unk_token=__UpperCAmelCase , sep_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , cls_token=__UpperCAmelCase , mask_token=__UpperCAmelCase , tokenize_chinese_chars=__UpperCAmelCase , strip_accents=__UpperCAmelCase , **__UpperCAmelCase , ) a = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , __UpperCAmelCase ) != do_lower_case or normalizer_state.get('''strip_accents''' , __UpperCAmelCase ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , __UpperCAmelCase ) != tokenize_chinese_chars ): a = getattr(__UpperCAmelCase , normalizer_state.pop('''type''' ) ) a = do_lower_case a = strip_accents a = tokenize_chinese_chars a = normalizer_class(**__UpperCAmelCase ) a = do_lower_case def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[int]=None ) ->Optional[Any]: """simple docstring""" a = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [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 __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None ) ->Tuple[str]: """simple docstring""" a = self._tokenizer.model.save(__UpperCAmelCase , name=__UpperCAmelCase ) return tuple(__UpperCAmelCase )
26
def _a ( a :list ) -> list: if len(a ) <= 1: return lst a = 1 while i < len(a ): if lst[i - 1] <= lst[i]: i += 1 else: a , a = lst[i], lst[i - 1] i -= 1 if i == 0: a = 1 return lst if __name__ == "__main__": UpperCAmelCase__ = input("Enter numbers separated by a comma:\n").strip() UpperCAmelCase__ = [int(item) for item in user_input.split(",")] print(gnome_sort(unsorted))
26
1
import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm UpperCAmelCase__ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex UpperCAmelCase__ = 10 UpperCAmelCase__ = 256 def _a ( a :List[str] ) -> Optional[MinHash]: if len(a ) < MIN_NUM_TOKENS: return None a = MinHash(num_perm=a ) for token in set(a ): min_hash.update(token.encode() ) return min_hash def _a ( a :str ) -> Set[str]: return {t for t in NON_ALPHA.split(a ) if len(t.strip() ) > 0} class lowercase_ : '''simple docstring''' def __init__( self : Any , *, __UpperCAmelCase : float = 0.85 , ) ->Dict: """simple docstring""" a = duplication_jaccard_threshold a = NUM_PERM a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) a = defaultdict(__UpperCAmelCase ) def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Tuple , __UpperCAmelCase : MinHash ) ->None: """simple docstring""" a = self._index.query(__UpperCAmelCase ) if code_key in self._index.keys: print(F"""Duplicate key {code_key}""" ) return self._index.insert(__UpperCAmelCase , __UpperCAmelCase ) if len(__UpperCAmelCase ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(__UpperCAmelCase ) break else: self._duplicate_clusters[close_duplicates[0]].add(__UpperCAmelCase ) def __lowerCAmelCase ( self : Dict ) ->List[List[Dict]]: """simple docstring""" a = [] for base, duplicates in self._duplicate_clusters.items(): a = [base] + list(__UpperCAmelCase ) # reformat the cluster to be a list of dict a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(__UpperCAmelCase ) return duplicate_clusters def __lowerCAmelCase ( self : Any , __UpperCAmelCase : Dict ) ->None: """simple docstring""" a = self.get_duplicate_clusters() with open(__UpperCAmelCase , '''w''' ) as f: json.dump(__UpperCAmelCase , __UpperCAmelCase ) def _a ( a :List[Any] ) -> List[Any]: a , a = element a = get_min_hash([t for t in NON_ALPHA.split(data['''content'''] ) if len(t.strip() ) > 0] ) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def _a ( a :Type[Dataset] ) -> List[Any]: with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(a , max_queue_size=10_000 ) , chunksize=100 , ): if data is not None: yield data def _a ( a :Type[Dataset] , a :float ) -> str: a = DuplicationIndex(duplication_jaccard_threshold=a ) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(a ) ) , max_queue_size=100 ) ): di.add(a , a ) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def _a ( a :str , a :str ) -> float: a = get_tokens(a ) a = get_tokens(a ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) UpperCAmelCase__ = None def _a ( a :Tuple , a :Tuple ) -> Any: a = [] for elementa in cluster: a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(a , a ) >= jaccard_threshold: elementa["copies"] += 1 break else: a = 1 extremes.append(a ) return extremes def _a ( a :List[Any] , a :Optional[Any] , a :Union[str, Any] ) -> Optional[int]: global _shared_dataset a = dataset a = [] a = partial(_find_cluster_extremes_shared , jaccard_threshold=a ) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( a , a , ) , total=len(a ) , ): extremes_list.append(a ) return extremes_list def _a ( a :Type[Dataset] , a :float = 0.85 ) -> Tuple[Type[Dataset], List[List[Dict]]]: a = make_duplicate_clusters(a , a ) a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} a = {} a = find_extremes(a , a , a ) for extremes in extremes_clusters: for element in extremes: a = element a = duplicate_indices - set(extreme_dict.keys() ) a = dataset.filter(lambda a , a : idx not in remove_indices , with_indices=a ) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: a = element['''base_index'''] in extreme_dict if element["is_extreme"]: a = extreme_dict[element['''base_index''']]['''copies'''] print(F"""Original dataset size: {len(a )}""" ) print(F"""Number of duplicate clusters: {len(a )}""" ) print(F"""Files in duplicate cluster: {len(a )}""" ) print(F"""Unique files in duplicate cluster: {len(a )}""" ) print(F"""Filtered dataset size: {len(a )}""" ) return ds_filter, duplicate_clusters
26
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCAmelCase__ = { "configuration_deberta": ["DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP", "DebertaConfig", "DebertaOnnxConfig"], "tokenization_deberta": ["DebertaTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ["DebertaTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "DebertaForMaskedLM", "DebertaForQuestionAnswering", "DebertaForSequenceClassification", "DebertaForTokenClassification", "DebertaModel", "DebertaPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "TFDebertaForMaskedLM", "TFDebertaForQuestionAnswering", "TFDebertaForSequenceClassification", "TFDebertaForTokenClassification", "TFDebertaModel", "TFDebertaPreTrainedModel", ] if TYPE_CHECKING: from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig, DebertaOnnxConfig from .tokenization_deberta import DebertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_deberta_fast import DebertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deberta import ( DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, DebertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deberta import ( TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFDebertaForMaskedLM, TFDebertaForQuestionAnswering, TFDebertaForSequenceClassification, TFDebertaForTokenClassification, TFDebertaModel, TFDebertaPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
1
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { "google/fnet-base": "https://huggingface.co/google/fnet-base/resolve/main/config.json", "google/fnet-large": "https://huggingface.co/google/fnet-large/resolve/main/config.json" # See all FNet models at https://huggingface.co/models?filter=fnet } class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = '''fnet''' def __init__( self : Tuple , __UpperCAmelCase : Optional[int]=32_000 , __UpperCAmelCase : Tuple=768 , __UpperCAmelCase : Any=12 , __UpperCAmelCase : str=3_072 , __UpperCAmelCase : Optional[int]="gelu_new" , __UpperCAmelCase : List[str]=0.1 , __UpperCAmelCase : List[Any]=512 , __UpperCAmelCase : List[str]=4 , __UpperCAmelCase : int=0.02 , __UpperCAmelCase : str=1e-1_2 , __UpperCAmelCase : str=False , __UpperCAmelCase : Optional[int]=512 , __UpperCAmelCase : Dict=3 , __UpperCAmelCase : Optional[int]=1 , __UpperCAmelCase : List[str]=2 , **__UpperCAmelCase : str , ) ->List[Any]: """simple docstring""" super().__init__(pad_token_id=__UpperCAmelCase , bos_token_id=__UpperCAmelCase , eos_token_id=__UpperCAmelCase , **__UpperCAmelCase ) a = vocab_size a = max_position_embeddings a = hidden_size a = num_hidden_layers a = intermediate_size a = hidden_act a = hidden_dropout_prob a = initializer_range a = type_vocab_size a = layer_norm_eps a = use_tpu_fourier_optimizations a = tpu_short_seq_length
26
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all feature extractors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...feature_extraction_utils import FeatureExtractionMixin from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_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, ) UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = OrderedDict( [ ("audio-spectrogram-transformer", "ASTFeatureExtractor"), ("beit", "BeitFeatureExtractor"), ("chinese_clip", "ChineseCLIPFeatureExtractor"), ("clap", "ClapFeatureExtractor"), ("clip", "CLIPFeatureExtractor"), ("clipseg", "ViTFeatureExtractor"), ("conditional_detr", "ConditionalDetrFeatureExtractor"), ("convnext", "ConvNextFeatureExtractor"), ("cvt", "ConvNextFeatureExtractor"), ("data2vec-audio", "Wav2Vec2FeatureExtractor"), ("data2vec-vision", "BeitFeatureExtractor"), ("deformable_detr", "DeformableDetrFeatureExtractor"), ("deit", "DeiTFeatureExtractor"), ("detr", "DetrFeatureExtractor"), ("dinat", "ViTFeatureExtractor"), ("donut-swin", "DonutFeatureExtractor"), ("dpt", "DPTFeatureExtractor"), ("encodec", "EncodecFeatureExtractor"), ("flava", "FlavaFeatureExtractor"), ("glpn", "GLPNFeatureExtractor"), ("groupvit", "CLIPFeatureExtractor"), ("hubert", "Wav2Vec2FeatureExtractor"), ("imagegpt", "ImageGPTFeatureExtractor"), ("layoutlmv2", "LayoutLMv2FeatureExtractor"), ("layoutlmv3", "LayoutLMv3FeatureExtractor"), ("levit", "LevitFeatureExtractor"), ("maskformer", "MaskFormerFeatureExtractor"), ("mctct", "MCTCTFeatureExtractor"), ("mobilenet_v1", "MobileNetV1FeatureExtractor"), ("mobilenet_v2", "MobileNetV2FeatureExtractor"), ("mobilevit", "MobileViTFeatureExtractor"), ("nat", "ViTFeatureExtractor"), ("owlvit", "OwlViTFeatureExtractor"), ("perceiver", "PerceiverFeatureExtractor"), ("poolformer", "PoolFormerFeatureExtractor"), ("regnet", "ConvNextFeatureExtractor"), ("resnet", "ConvNextFeatureExtractor"), ("segformer", "SegformerFeatureExtractor"), ("sew", "Wav2Vec2FeatureExtractor"), ("sew-d", "Wav2Vec2FeatureExtractor"), ("speech_to_text", "Speech2TextFeatureExtractor"), ("speecht5", "SpeechT5FeatureExtractor"), ("swiftformer", "ViTFeatureExtractor"), ("swin", "ViTFeatureExtractor"), ("swinv2", "ViTFeatureExtractor"), ("table-transformer", "DetrFeatureExtractor"), ("timesformer", "VideoMAEFeatureExtractor"), ("tvlt", "TvltFeatureExtractor"), ("unispeech", "Wav2Vec2FeatureExtractor"), ("unispeech-sat", "Wav2Vec2FeatureExtractor"), ("van", "ConvNextFeatureExtractor"), ("videomae", "VideoMAEFeatureExtractor"), ("vilt", "ViltFeatureExtractor"), ("vit", "ViTFeatureExtractor"), ("vit_mae", "ViTFeatureExtractor"), ("vit_msn", "ViTFeatureExtractor"), ("wav2vec2", "Wav2Vec2FeatureExtractor"), ("wav2vec2-conformer", "Wav2Vec2FeatureExtractor"), ("wavlm", "Wav2Vec2FeatureExtractor"), ("whisper", "WhisperFeatureExtractor"), ("xclip", "CLIPFeatureExtractor"), ("yolos", "YolosFeatureExtractor"), ] ) UpperCAmelCase__ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES) def _a ( a :str ) -> Any: for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items(): if class_name in extractors: a = model_type_to_module_name(a ) a = importlib.import_module(F""".{module_name}""" , '''transformers.models''' ) try: return getattr(a , a ) except AttributeError: continue for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items(): if getattr(a , '''__name__''' , a ) == 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. a = importlib.import_module('''transformers''' ) if hasattr(a , a ): return getattr(a , a ) return None def _a ( a :Union[str, os.PathLike] , a :Optional[Union[str, os.PathLike]] = None , a :bool = False , a :bool = False , a :Optional[Dict[str, str]] = None , a :Optional[Union[bool, str]] = None , a :Optional[str] = None , a :bool = False , **a :int , ) -> Tuple: a = get_file_from_repo( a , a , cache_dir=a , force_download=a , resume_download=a , proxies=a , use_auth_token=a , revision=a , local_files_only=a , ) if resolved_config_file is None: logger.info( '''Could not locate the feature extractor configuration file, will try to use the model config instead.''' ) return {} with open(a , encoding='''utf-8''' ) as reader: return json.load(a ) class lowercase_ : '''simple docstring''' def __init__( self : Tuple ) ->int: """simple docstring""" raise EnvironmentError( '''AutoFeatureExtractor is designed to be instantiated ''' '''using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.''' ) @classmethod @replace_list_option_in_docstrings(__UpperCAmelCase ) def __lowerCAmelCase ( cls : int , __UpperCAmelCase : Optional[Any] , **__UpperCAmelCase : Dict ) ->List[Any]: """simple docstring""" a = kwargs.pop('''config''' , __UpperCAmelCase ) a = kwargs.pop('''trust_remote_code''' , __UpperCAmelCase ) a = True a , a = FeatureExtractionMixin.get_feature_extractor_dict(__UpperCAmelCase , **__UpperCAmelCase ) a = config_dict.get('''feature_extractor_type''' , __UpperCAmelCase ) a = None if "AutoFeatureExtractor" in config_dict.get('''auto_map''' , {} ): a = config_dict['''auto_map''']['''AutoFeatureExtractor'''] # If we don't find the feature extractor class in the feature extractor config, let's try the model config. if feature_extractor_class is None and feature_extractor_auto_map is None: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): a = AutoConfig.from_pretrained(__UpperCAmelCase , **__UpperCAmelCase ) # It could be in `config.feature_extractor_type`` a = getattr(__UpperCAmelCase , '''feature_extractor_type''' , __UpperCAmelCase ) if hasattr(__UpperCAmelCase , '''auto_map''' ) and "AutoFeatureExtractor" in config.auto_map: a = config.auto_map['''AutoFeatureExtractor'''] if feature_extractor_class is not None: a = feature_extractor_class_from_name(__UpperCAmelCase ) a = feature_extractor_auto_map is not None a = feature_extractor_class is not None or type(__UpperCAmelCase ) in FEATURE_EXTRACTOR_MAPPING a = resolve_trust_remote_code( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) if has_remote_code and trust_remote_code: a = get_class_from_dynamic_module( __UpperCAmelCase , __UpperCAmelCase , **__UpperCAmelCase ) a = kwargs.pop('''code_revision''' , __UpperCAmelCase ) if os.path.isdir(__UpperCAmelCase ): feature_extractor_class.register_for_auto_class() return feature_extractor_class.from_dict(__UpperCAmelCase , **__UpperCAmelCase ) elif feature_extractor_class is not None: return feature_extractor_class.from_dict(__UpperCAmelCase , **__UpperCAmelCase ) # Last try: we use the FEATURE_EXTRACTOR_MAPPING. elif type(__UpperCAmelCase ) in FEATURE_EXTRACTOR_MAPPING: a = FEATURE_EXTRACTOR_MAPPING[type(__UpperCAmelCase )] return feature_extractor_class.from_dict(__UpperCAmelCase , **__UpperCAmelCase ) raise ValueError( F"""Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a """ F"""`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following """ F"""`model_type` keys in its {CONFIG_NAME}: {', '.join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys() )}""" ) @staticmethod def __lowerCAmelCase ( __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Tuple ) ->Optional[int]: """simple docstring""" FEATURE_EXTRACTOR_MAPPING.register(__UpperCAmelCase , __UpperCAmelCase )
26
1
import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, UNetaDConditionModel, VideoToVideoSDPipeline, ) from diffusers.utils import floats_tensor, is_xformers_available, skip_mps from diffusers.utils.testing_utils import enable_full_determinism, slow, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() @skip_mps class lowercase_ ( lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = VideoToVideoSDPipeline __snake_case = TEXT_GUIDED_IMAGE_VARIATION_PARAMS.union({'''video'''} ) - {'''image''', '''width''', '''height'''} __snake_case = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({'''video'''} ) - {'''image'''} __snake_case = PipelineTesterMixin.required_optional_params - {'''latents'''} __snake_case = False # No `output_type`. __snake_case = frozenset( [ '''num_inference_steps''', '''generator''', '''latents''', '''return_dict''', '''callback''', '''callback_steps''', ] ) def __lowerCAmelCase ( self : Tuple ) ->Dict: """simple docstring""" torch.manual_seed(0 ) a = UNetaDConditionModel( block_out_channels=(32, 64, 64, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''CrossAttnDownBlock3D''', '''CrossAttnDownBlock3D''', '''CrossAttnDownBlock3D''', '''DownBlock3D''') , up_block_types=('''UpBlock3D''', '''CrossAttnUpBlock3D''', '''CrossAttnUpBlock3D''', '''CrossAttnUpBlock3D''') , cross_attention_dim=32 , attention_head_dim=4 , ) a = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=__UpperCAmelCase , set_alpha_to_one=__UpperCAmelCase , ) torch.manual_seed(0 ) a = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) a = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , hidden_act='''gelu''' , projection_dim=512 , ) a = CLIPTextModel(__UpperCAmelCase ) a = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) a = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, } return components def __lowerCAmelCase ( self : str , __UpperCAmelCase : int , __UpperCAmelCase : Optional[Any]=0 ) ->Optional[Any]: """simple docstring""" a = floats_tensor((1, 3, 3, 32, 32) , rng=random.Random(__UpperCAmelCase ) ).to(__UpperCAmelCase ) if str(__UpperCAmelCase ).startswith('''mps''' ): a = torch.manual_seed(__UpperCAmelCase ) else: a = torch.Generator(device=__UpperCAmelCase ).manual_seed(__UpperCAmelCase ) a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''video''': video, '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''pt''', } return inputs def __lowerCAmelCase ( self : Optional[int] ) ->Union[str, Any]: """simple docstring""" a = '''cpu''' # ensure determinism for the device-dependent torch.Generator a = self.get_dummy_components() a = VideoToVideoSDPipeline(**__UpperCAmelCase ) a = sd_pipe.to(__UpperCAmelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_dummy_inputs(__UpperCAmelCase ) a = '''np''' a = sd_pipe(**__UpperCAmelCase ).frames a = frames[0][-3:, -3:, -1] assert frames[0].shape == (32, 32, 3) a = np.array([106, 117, 113, 174, 137, 112, 148, 151, 131] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def __lowerCAmelCase ( self : List[Any] ) ->Dict: """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=__UpperCAmelCase , expected_max_diff=5e-3 ) @unittest.skip(reason='''Batching needs to be properly figured out first for this pipeline.''' ) def __lowerCAmelCase ( self : List[str] ) ->Union[str, Any]: """simple docstring""" pass @unittest.skip(reason='''Batching needs to be properly figured out first for this pipeline.''' ) def __lowerCAmelCase ( self : List[str] ) ->Tuple: """simple docstring""" pass @unittest.skip(reason='''`num_images_per_prompt` argument is not supported for this pipeline.''' ) def __lowerCAmelCase ( self : int ) ->List[Any]: """simple docstring""" pass def __lowerCAmelCase ( self : Dict ) ->str: """simple docstring""" return super().test_progress_bar() @slow @skip_mps class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self : Union[str, Any] ) ->str: """simple docstring""" a = VideoToVideoSDPipeline.from_pretrained('''cerspense/zeroscope_v2_XL''' , torch_dtype=torch.floataa ) pipe.enable_model_cpu_offload() # 10 frames a = torch.Generator(device='''cpu''' ).manual_seed(0 ) a = torch.randn((1, 10, 3, 1_024, 576) , generator=__UpperCAmelCase ) a = video.to('''cuda''' ) a = '''Spiderman is surfing''' a = pipe(__UpperCAmelCase , video=__UpperCAmelCase , generator=__UpperCAmelCase , num_inference_steps=3 , output_type='''pt''' ).frames a = np.array([-1.0458984, -1.1279297, -0.9663086, -0.91503906, -0.75097656] ) assert np.abs(video_frames.cpu().numpy()[0, 0, 0, 0, -5:] - expected_array ).sum() < 1e-2
26
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import ( AutoProcessor, BertTokenizerFast, BlipImageProcessor, GPTaTokenizer, InstructBlipProcessor, PreTrainedTokenizerFast, ) @require_vision class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self : Optional[int] ) ->Tuple: """simple docstring""" a = tempfile.mkdtemp() a = BlipImageProcessor() a = GPTaTokenizer.from_pretrained('''hf-internal-testing/tiny-random-GPT2Model''' ) a = BertTokenizerFast.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) a = InstructBlipProcessor(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) processor.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self : Optional[Any] , **__UpperCAmelCase : Tuple ) ->List[str]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).tokenizer def __lowerCAmelCase ( self : int , **__UpperCAmelCase : str ) ->List[str]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).image_processor def __lowerCAmelCase ( self : Optional[Any] , **__UpperCAmelCase : Any ) ->Optional[Any]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ).qformer_tokenizer def __lowerCAmelCase ( self : str ) ->Tuple: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __lowerCAmelCase ( self : Optional[int] ) ->str: """simple docstring""" a = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] a = [Image.fromarray(np.moveaxis(__UpperCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def __lowerCAmelCase ( self : Optional[Any] ) ->List[str]: """simple docstring""" a = InstructBlipProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() , qformer_tokenizer=self.get_qformer_tokenizer() , ) processor.save_pretrained(self.tmpdirname ) a = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) a = self.get_image_processor(do_normalize=__UpperCAmelCase , padding_value=1.0 ) a = InstructBlipProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=__UpperCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __UpperCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __UpperCAmelCase ) self.assertIsInstance(processor.qformer_tokenizer , __UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[Any] ) ->Any: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = self.prepare_image_inputs() a = image_processor(__UpperCAmelCase , return_tensors='''np''' ) a = processor(images=__UpperCAmelCase , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowerCAmelCase ( self : List[str] ) ->List[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = '''lower newer''' a = processor(text=__UpperCAmelCase ) a = tokenizer(__UpperCAmelCase , return_token_type_ids=__UpperCAmelCase ) a = qformer_tokenizer(__UpperCAmelCase , return_token_type_ids=__UpperCAmelCase ) for key in encoded_tokens.keys(): self.assertListEqual(encoded_tokens[key] , encoded_processor[key] ) for key in encoded_tokens_qformer.keys(): self.assertListEqual(encoded_tokens_qformer[key] , encoded_processor['''qformer_''' + key] ) def __lowerCAmelCase ( self : Dict ) ->Optional[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = '''lower newer''' a = self.prepare_image_inputs() a = processor(text=__UpperCAmelCase , images=__UpperCAmelCase ) self.assertListEqual( list(inputs.keys() ) , ['''input_ids''', '''attention_mask''', '''qformer_input_ids''', '''qformer_attention_mask''', '''pixel_values'''] , ) # test if it raises when no input is passed with pytest.raises(__UpperCAmelCase ): processor() def __lowerCAmelCase ( self : Dict ) ->List[Any]: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] a = processor.batch_decode(__UpperCAmelCase ) a = tokenizer.batch_decode(__UpperCAmelCase ) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] ) ->str: """simple docstring""" a = self.get_image_processor() a = self.get_tokenizer() a = self.get_qformer_tokenizer() a = InstructBlipProcessor( tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase , qformer_tokenizer=__UpperCAmelCase ) a = '''lower newer''' a = self.prepare_image_inputs() a = processor(text=__UpperCAmelCase , images=__UpperCAmelCase ) self.assertListEqual( list(inputs.keys() ) , ['''input_ids''', '''attention_mask''', '''qformer_input_ids''', '''qformer_attention_mask''', '''pixel_values'''] , )
26
1
import argparse import logging from collections import namedtuple import torch from model_bertabs import BertAbsSummarizer from models.model_builder import AbsSummarizer # The authors' implementation from transformers import BertTokenizer logging.basicConfig(level=logging.INFO) UpperCAmelCase__ = logging.getLogger(__name__) UpperCAmelCase__ = "Hello world! cécé herlolip" UpperCAmelCase__ = namedtuple( "BertAbsConfig", [ "temp_dir", "large", "use_bert_emb", "finetune_bert", "encoder", "share_emb", "max_pos", "enc_layers", "enc_hidden_size", "enc_heads", "enc_ff_size", "enc_dropout", "dec_layers", "dec_hidden_size", "dec_heads", "dec_ff_size", "dec_dropout", ], ) def _a ( a :Any , a :List[str] ) -> int: a = BertAbsConfig( temp_dir='''.''' , finetune_bert=a , large=a , share_emb=a , use_bert_emb=a , encoder='''bert''' , max_pos=512 , enc_layers=6 , enc_hidden_size=512 , enc_heads=8 , enc_ff_size=512 , enc_dropout=0.2 , dec_layers=6 , dec_hidden_size=768 , dec_heads=8 , dec_ff_size=2_048 , dec_dropout=0.2 , ) a = torch.load(a , lambda a , a : storage ) a = AbsSummarizer(a , torch.device('''cpu''' ) , a ) original.eval() a = BertAbsSummarizer(a , torch.device('''cpu''' ) ) new_model.eval() # ------------------- # Convert the weights # ------------------- logging.info('''convert the model''' ) new_model.bert.load_state_dict(original.bert.state_dict() ) new_model.decoder.load_state_dict(original.decoder.state_dict() ) new_model.generator.load_state_dict(original.generator.state_dict() ) # ---------------------------------- # Make sure the outpus are identical # ---------------------------------- logging.info('''Make sure that the models\' outputs are identical''' ) a = BertTokenizer.from_pretrained('''bert-base-uncased''' ) # prepare the model inputs a = tokenizer.encode('''This is sample éàalj\'-.''' ) encoder_input_ids.extend([tokenizer.pad_token_id] * (512 - len(a )) ) a = torch.tensor(a ).unsqueeze(0 ) a = tokenizer.encode('''This is sample 3 éàalj\'-.''' ) decoder_input_ids.extend([tokenizer.pad_token_id] * (512 - len(a )) ) a = torch.tensor(a ).unsqueeze(0 ) # failsafe to make sure the weights reset does not affect the # loaded weights. assert torch.max(torch.abs(original.generator[0].weight - new_model.generator[0].weight ) ) == 0 # forward pass a = encoder_input_ids a = decoder_input_ids a = a = None a = None a = a = None a = a = None a = None # The original model does not apply the geneator layer immediatly but rather in # the beam search (where it combines softmax + linear layer). Since we already # apply the softmax in our generation process we only apply the linear layer here. # We make sure that the outputs of the full stack are identical a = original(a , a , a , a , a , a , a )[0] a = original.generator(a ) a = new_model( a , a , a , a , a )[0] a = new_model.generator(a ) a = torch.max(torch.abs(output_converted_model - output_original_model ) ).item() print('''Maximum absolute difference beween weights: {:.2f}'''.format(a ) ) a = torch.max(torch.abs(output_converted_generator - output_original_generator ) ).item() print('''Maximum absolute difference beween weights: {:.2f}'''.format(a ) ) a = torch.allclose(a , a , atol=1e-3 ) if are_identical: logging.info('''all weights are equal up to 1e-3''' ) else: raise ValueError('''the weights are different. The new model is likely different from the original one.''' ) # The model has been saved with torch.save(model) and this is bound to the exact # directory structure. We save the state_dict instead. logging.info('''saving the model\'s state dictionary''' ) torch.save( new_model.state_dict() , '''./bertabs-finetuned-cnndm-extractive-abstractive-summarization/pytorch_model.bin''' ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument( "--bertabs_checkpoint_path", default=None, type=str, required=True, help="Path the official PyTorch dump.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model.", ) UpperCAmelCase__ = parser.parse_args() convert_bertabs_checkpoints( args.bertabs_checkpoint_path, args.pytorch_dump_folder_path, )
26
import math def _a ( a :int = 100 ) -> int: a = sum(i * i for i in range(1 , n + 1 ) ) a = int(math.pow(sum(range(1 , n + 1 ) ) , 2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(f"""{solution() = }""")
26
1
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase__ = { "configuration_xmod": [ "XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP", "XmodConfig", "XmodOnnxConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "XMOD_PRETRAINED_MODEL_ARCHIVE_LIST", "XmodForCausalLM", "XmodForMaskedLM", "XmodForMultipleChoice", "XmodForQuestionAnswering", "XmodForSequenceClassification", "XmodForTokenClassification", "XmodModel", "XmodPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xmod import ( XMOD_PRETRAINED_MODEL_ARCHIVE_LIST, XmodForCausalLM, XmodForMaskedLM, XmodForMultipleChoice, XmodForQuestionAnswering, XmodForSequenceClassification, XmodForTokenClassification, XmodModel, XmodPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
def _a ( a :int = 600_851_475_143 ) -> int: try: a = int(a ) 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.''' ) a = 2 a = 0 if n == 2: return 2 while n > 2: while n % i != 0: i += 1 a = i while n % i == 0: a = n // i i += 1 return int(a ) if __name__ == "__main__": print(f"""{solution() = }""")
26
1
import unittest from typing import Dict, List, Optional, Union 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_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import BridgeTowerImageProcessor class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : Optional[Any] , __UpperCAmelCase : Dict , __UpperCAmelCase : bool = True , __UpperCAmelCase : Dict[str, int] = None , __UpperCAmelCase : int = 32 , __UpperCAmelCase : bool = True , __UpperCAmelCase : Union[int, float] = 1 / 255 , __UpperCAmelCase : bool = True , __UpperCAmelCase : bool = True , __UpperCAmelCase : Optional[Union[float, List[float]]] = [0.48145466, 0.4578275, 0.40821073] , __UpperCAmelCase : Optional[Union[float, List[float]]] = [0.26862954, 0.26130258, 0.27577711] , __UpperCAmelCase : bool = True , __UpperCAmelCase : Optional[int]=7 , __UpperCAmelCase : List[Any]=30 , __UpperCAmelCase : List[str]=400 , __UpperCAmelCase : Union[str, Any]=3 , ) ->List[str]: """simple docstring""" a = parent a = do_resize a = size if size is not None else {'''shortest_edge''': 288} a = size_divisor a = do_rescale a = rescale_factor a = do_normalize a = do_center_crop a = image_mean a = image_std a = do_pad a = batch_size a = num_channels a = min_resolution a = max_resolution def __lowerCAmelCase ( self : List[Any] ) ->List[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, "size_divisor": self.size_divisor, } def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : str , __UpperCAmelCase : Tuple=False ) ->Dict: """simple docstring""" if not batched: a = self.size['''shortest_edge'''] a = image_inputs[0] if isinstance(__UpperCAmelCase , Image.Image ): a , a = image.size else: a , a = image.shape[1], image.shape[2] a = size / min(__UpperCAmelCase , __UpperCAmelCase ) if h < w: a , a = size, scale * w else: a , a = scale * h, size a = int((1_333 / 800) * size ) if max(__UpperCAmelCase , __UpperCAmelCase ) > max_size: a = max_size / max(__UpperCAmelCase , __UpperCAmelCase ) a = newh * scale a = neww * scale a , a = int(newh + 0.5 ), int(neww + 0.5 ) a , a = ( newh // self.size_divisor * self.size_divisor, neww // self.size_divisor * self.size_divisor, ) else: a = [] for image in image_inputs: a , a = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) a = max(__UpperCAmelCase , key=lambda __UpperCAmelCase : item[0] )[0] a = max(__UpperCAmelCase , key=lambda __UpperCAmelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class lowercase_ ( lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = BridgeTowerImageProcessor if is_vision_available() else None def __lowerCAmelCase ( self : str ) ->List[str]: """simple docstring""" a = BridgeTowerImageProcessingTester(self ) @property def __lowerCAmelCase ( self : List[str] ) ->List[str]: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __lowerCAmelCase ( self : List[Any] ) ->Optional[int]: """simple docstring""" a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__UpperCAmelCase , '''image_mean''' ) ) self.assertTrue(hasattr(__UpperCAmelCase , '''image_std''' ) ) self.assertTrue(hasattr(__UpperCAmelCase , '''do_normalize''' ) ) self.assertTrue(hasattr(__UpperCAmelCase , '''do_resize''' ) ) self.assertTrue(hasattr(__UpperCAmelCase , '''size''' ) ) self.assertTrue(hasattr(__UpperCAmelCase , '''size_divisor''' ) ) def __lowerCAmelCase ( self : Dict ) ->Union[str, Any]: """simple docstring""" pass def __lowerCAmelCase ( self : Any ) ->str: """simple docstring""" a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images a = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , Image.Image ) # Test not batched input a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values a , a = self.image_processor_tester.get_expected_values(__UpperCAmelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched a = image_processing(__UpperCAmelCase , return_tensors='''pt''' ).pixel_values a , a = self.image_processor_tester.get_expected_values(__UpperCAmelCase , batched=__UpperCAmelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __lowerCAmelCase ( self : str ) ->Union[str, Any]: """simple docstring""" a = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors a = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCAmelCase , numpify=__UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , np.ndarray ) # Test not batched input a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values a , a = self.image_processor_tester.get_expected_values(__UpperCAmelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched a = image_processing(__UpperCAmelCase , return_tensors='''pt''' ).pixel_values a , a = self.image_processor_tester.get_expected_values(__UpperCAmelCase , batched=__UpperCAmelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __lowerCAmelCase ( self : Optional[Any] ) ->List[str]: """simple docstring""" a = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors a = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCAmelCase , torchify=__UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , torch.Tensor ) # Test not batched input a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values a , a = self.image_processor_tester.get_expected_values(__UpperCAmelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched a = image_processing(__UpperCAmelCase , return_tensors='''pt''' ).pixel_values a , a = self.image_processor_tester.get_expected_values(__UpperCAmelCase , batched=__UpperCAmelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , )
26
import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer UpperCAmelCase__ = "bart" UpperCAmelCase__ = True @st.cache(allow_output_mutation=a ) def _a ( ) -> Tuple: if LOAD_DENSE_INDEX: a = AutoTokenizer.from_pretrained('''yjernite/retribert-base-uncased''' ) a = AutoModel.from_pretrained('''yjernite/retribert-base-uncased''' ).to('''cuda:0''' ) a = qar_model.eval() else: a , a = (None, None) if MODEL_TYPE == "bart": a = AutoTokenizer.from_pretrained('''yjernite/bart_eli5''' ) a = AutoModelForSeqaSeqLM.from_pretrained('''yjernite/bart_eli5''' ).to('''cuda:0''' ) a = torch.load('''seq2seq_models/eli5_bart_model_blm_2.pth''' ) sas_model.load_state_dict(save_dict['''model'''] ) a = sas_model.eval() else: a , a = make_qa_sas_model( model_name='''t5-small''' , from_file='''seq2seq_models/eli5_t5_model_1024_4.pth''' , device='''cuda:0''' ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=a ) def _a ( ) -> Dict: if LOAD_DENSE_INDEX: a = faiss.StandardGpuResources() a = datasets.load_dataset(path='''wiki_snippets''' , name='''wiki40b_en_100_0''' )['''train'''] a = np.memmap( '''wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat''' , dtype='''float32''' , mode='''r''' , shape=(wikiaab_passages.num_rows, 128) , ) a = faiss.IndexFlatIP(128 ) a = faiss.index_cpu_to_gpu(a , 1 , a ) wikiaab_gpu_index_flat.add(a ) # TODO fix for larger GPU else: a , a = (None, None) a = Elasticsearch([{'''host''': '''localhost''', '''port''': '''9200'''}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=a ) def _a ( ) -> Optional[int]: a = datasets.load_dataset('''eli5''' , name='''LFQA_reddit''' ) a = elia['''train_eli5'''] a = np.memmap( '''eli5_questions_reps.dat''' , dtype='''float32''' , mode='''r''' , shape=(elia_train.num_rows, 128) ) a = faiss.IndexFlatIP(128 ) eli5_train_q_index.add(a ) return (elia_train, eli5_train_q_index) UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = load_indexes() UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = load_models() UpperCAmelCase__ , UpperCAmelCase__ = load_train_data() def _a ( a :str , a :Tuple=10 ) -> List[str]: a = embed_questions_for_retrieval([question] , a , a ) a , a = eli5_train_q_index.search(a , a ) a = [elia_train[int(a )] for i in I[0]] return nn_examples def _a ( a :str , a :Any="wiki40b" , a :int="dense" , a :Union[str, Any]=10 ) -> List[str]: if source == "none": a , a = (''' <P> '''.join(['''''' for _ in range(11 )] ).strip(), []) else: if method == "dense": a , a = query_qa_dense_index( a , a , a , a , a , a ) else: a , a = query_es_index( a , a , index_name='''english_wiki40b_snippets_100w''' , n_results=a , ) a = [ (res['''article_title'''], res['''section_title'''].strip(), res['''score'''], res['''passage_text''']) for res in hit_lst ] a = '''question: {} context: {}'''.format(a , a ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda a : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda a : None), } ) def _a ( a :Tuple , a :int , a :int , a :Dict=64 , a :List[Any]=256 , a :List[Any]=False , a :List[Any]=2 , a :Tuple=0.95 , a :Optional[Any]=0.8 ) -> int: with torch.no_grad(): a = qa_sas_generate( a , a , a , num_answers=1 , num_beams=a , min_len=a , max_len=a , do_sample=a , temp=a , top_p=a , top_k=a , max_input_length=1_024 , device='''cuda:0''' , )[0] return (answer, support_list) st.title("Long Form Question Answering with ELI5") # Start sidebar UpperCAmelCase__ = "<img src='https://huggingface.co/front/assets/huggingface_logo.svg'>" UpperCAmelCase__ = "\n<html>\n <head>\n <style>\n .img-container {\n padding-left: 90px;\n padding-right: 90px;\n padding-top: 50px;\n padding-bottom: 50px;\n background-color: #f0f3f9;\n }\n </style>\n </head>\n <body>\n <span class=\"img-container\"> <!-- Inline parent element -->\n %s\n </span>\n </body>\n</html>\n" % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia UpperCAmelCase__ = "\nThis demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html).\nFirst, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset,\na pre-processed fixed snapshot of Wikipedia.\n" st.sidebar.markdown(description, unsafe_allow_html=True) UpperCAmelCase__ = [ "Answer the question", "View the retrieved document only", "View the most similar ELI5 question and answer", "Show me everything, please!", ] UpperCAmelCase__ = st.sidebar.checkbox("Demo options") if demo_options: UpperCAmelCase__ = st.sidebar.selectbox( "", action_list, index=3, ) UpperCAmelCase__ = action_list.index(action_st) UpperCAmelCase__ = st.sidebar.selectbox( "", ["Show full text of passages", "Show passage section titles"], index=0, ) UpperCAmelCase__ = show_type == "Show full text of passages" else: UpperCAmelCase__ = 3 UpperCAmelCase__ = True UpperCAmelCase__ = st.sidebar.checkbox("Retrieval options") if retrieval_options: UpperCAmelCase__ = "\n ### Information retriever options\n\n The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding\n trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs.\n The answer is then generated by sequence to sequence model which takes the question and retrieved document as input.\n " st.sidebar.markdown(retriever_info) UpperCAmelCase__ = st.sidebar.selectbox("Which Wikipedia format should the model use?", ["wiki40b", "none"]) UpperCAmelCase__ = st.sidebar.selectbox("Which Wikipedia indexer should the model use?", ["dense", "sparse", "mixed"]) else: UpperCAmelCase__ = "wiki40b" UpperCAmelCase__ = "dense" UpperCAmelCase__ = "beam" UpperCAmelCase__ = 2 UpperCAmelCase__ = 64 UpperCAmelCase__ = 256 UpperCAmelCase__ = None UpperCAmelCase__ = None UpperCAmelCase__ = st.sidebar.checkbox("Generation options") if generate_options: UpperCAmelCase__ = "\n ### Answer generation options\n\n The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large)\n weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with\n **beam** search, or **sample** from the decoder's output probabilities.\n " st.sidebar.markdown(generate_info) UpperCAmelCase__ = st.sidebar.selectbox("Would you like to use beam search or sample an answer?", ["beam", "sampled"]) UpperCAmelCase__ = st.sidebar.slider( "Minimum generation length", min_value=8, max_value=256, value=64, step=8, format=None, key=None ) UpperCAmelCase__ = st.sidebar.slider( "Maximum generation length", min_value=64, max_value=512, value=256, step=16, format=None, key=None ) if sampled == "beam": UpperCAmelCase__ = st.sidebar.slider("Beam size", min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: UpperCAmelCase__ = st.sidebar.slider( "Nucleus sampling p", min_value=0.1, max_value=1.0, value=0.95, step=0.01, format=None, key=None ) UpperCAmelCase__ = st.sidebar.slider( "Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.01, format=None, key=None ) UpperCAmelCase__ = None # start main text UpperCAmelCase__ = [ "<MY QUESTION>", "How do people make chocolate?", "Why do we get a fever when we are sick?", "How can different animals perceive different colors?", "What is natural language processing?", "What's the best way to treat a sunburn?", "What exactly are vitamins ?", "How does nuclear energy provide electricity?", "What's the difference between viruses and bacteria?", "Why are flutes classified as woodwinds when most of them are made out of metal ?", "Why do people like drinking coffee even though it tastes so bad?", "What happens when wine ages? How does it make the wine taste better?", "If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?", "How can we set a date to the beginning or end of an artistic period? Doesn't the change happen gradually?", "How does New Zealand have so many large bird predators?", ] UpperCAmelCase__ = st.selectbox( "What would you like to ask? ---- select <MY QUESTION> to enter a new query", questions_list, index=1, ) if question_s == "<MY QUESTION>": UpperCAmelCase__ = st.text_input("Enter your question here:", "") else: UpperCAmelCase__ = question_s if st.button("Show me!"): if action in [0, 1, 3]: if index_type == "mixed": UpperCAmelCase__ , UpperCAmelCase__ = make_support(question, source=wiki_source, method="dense", n_results=10) UpperCAmelCase__ , UpperCAmelCase__ = make_support(question, source=wiki_source, method="sparse", n_results=10) UpperCAmelCase__ = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] UpperCAmelCase__ = support_list[:10] UpperCAmelCase__ = "<P> " + " <P> ".join([res[-1] for res in support_list]) else: UpperCAmelCase__ , UpperCAmelCase__ = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: UpperCAmelCase__ , UpperCAmelCase__ = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == "sampled"), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown("### The model generated answer is:") st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown("--- \n ### The model is drawing information from the following Wikipedia passages:") for i, res in enumerate(support_list): UpperCAmelCase__ = "https://en.wikipedia.org/wiki/{}".format(res[0].replace(" ", "_")) UpperCAmelCase__ = res[1].strip() if sec_titles == "": UpperCAmelCase__ = "[{}]({})".format(res[0], wiki_url) else: UpperCAmelCase__ = sec_titles.split(" & ") UpperCAmelCase__ = " & ".join( ["[{}]({}#{})".format(sec.strip(), wiki_url, sec.strip().replace(" ", "_")) for sec in sec_list] ) st.markdown( "{0:02d} - **Article**: {1:<18} <br> _Section_: {2}".format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( "> <span style=\"font-family:arial; font-size:10pt;\">" + res[-1] + "</span>", unsafe_allow_html=True ) if action in [2, 3]: UpperCAmelCase__ = find_nearest_training(question) UpperCAmelCase__ = nn_train_list[0] st.markdown( "--- \n ### The most similar question in the ELI5 training set was: \n\n {}".format(train_exple["title"]) ) UpperCAmelCase__ = [ "{}. {}".format(i + 1, " \n".join([line.strip() for line in ans.split("\n") if line.strip() != ""])) for i, (ans, sc) in enumerate(zip(train_exple["answers"]["text"], train_exple["answers"]["score"])) if i == 0 or sc > 2 ] st.markdown("##### Its answers were: \n\n {}".format("\n".join(answers_st))) UpperCAmelCase__ = "\n---\n\n**Disclaimer**\n\n*The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system.\nEvaluating biases of such a model and ensuring factual generations are still very much open research problems.\nTherefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.*\n" st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
26
1
import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate # and perform gradient accumulation # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## UpperCAmelCase__ = 16 UpperCAmelCase__ = 32 def _a ( a :Accelerator , a :int = 16 ) -> Optional[Any]: a = AutoTokenizer.from_pretrained('''bert-base-cased''' ) a = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(a :Optional[Any] ): # max_length=None => use the model max length (it's actually the default) a = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=a , max_length=a ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): a = datasets.map( a , batched=a , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library a = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(a :Tuple ): # On TPU it's best to pad everything to the same length or training will be very slow. a = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": a = 16 elif accelerator.mixed_precision != "no": a = 8 else: a = None return tokenizer.pad( a , padding='''longest''' , max_length=a , pad_to_multiple_of=a , return_tensors='''pt''' , ) # Instantiate dataloaders. a = DataLoader( tokenized_datasets['''train'''] , shuffle=a , collate_fn=a , batch_size=a ) a = DataLoader( tokenized_datasets['''validation'''] , shuffle=a , collate_fn=a , batch_size=a ) return train_dataloader, eval_dataloader # For testing only if os.environ.get("TESTING_MOCKED_DATALOADERS", None) == "1": from accelerate.test_utils.training import mocked_dataloaders UpperCAmelCase__ = mocked_dataloaders # noqa: F811 def _a ( a :Union[str, Any] , a :str ) -> str: # For testing only if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , a ) == "1": a = 2 # New Code # a = int(args.gradient_accumulation_steps ) # Initialize accelerator a = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , gradient_accumulation_steps=a ) if accelerator.distributed_type == DistributedType.TPU and gradient_accumulation_steps > 1: raise NotImplementedError( '''Gradient accumulation on TPUs is currently not supported. Pass `gradient_accumulation_steps=1`''' ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs a = config['''lr'''] a = int(config['''num_epochs'''] ) a = int(config['''seed'''] ) a = int(config['''batch_size'''] ) a = evaluate.load('''glue''' , '''mrpc''' ) set_seed(a ) a , a = get_dataloaders(a , a ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) a = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=a ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). a = model.to(accelerator.device ) # Instantiate optimizer a = AdamW(params=model.parameters() , lr=a ) # Instantiate scheduler a = get_linear_schedule_with_warmup( optimizer=a , num_warmup_steps=100 , num_training_steps=(len(a ) * num_epochs) , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. a , a , a , a , a = accelerator.prepare( a , a , a , a , a ) # Now we train the model for epoch in range(a ): model.train() for step, batch in enumerate(a ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) # New code # # We use the new `accumulate` context manager to perform gradient accumulation # We also currently do not support TPUs nor advise it as bugs were found on the XLA side when running our tests. with accelerator.accumulate(a ): a = model(**a ) a = output.loss accelerator.backward(a ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(a ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): a = model(**a ) a = outputs.logits.argmax(dim=-1 ) a , a = accelerator.gather_for_metrics((predictions, batch['''labels''']) ) metric.add_batch( predictions=a , references=a , ) a = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"""epoch {epoch}:""" , a ) def _a ( ) -> Optional[Any]: a = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=a , default=a , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) # New Code # parser.add_argument( '''--gradient_accumulation_steps''' , type=a , default=1 , help='''The number of minibatches to be ran before gradients are accumulated.''' , ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) a = parser.parse_args() a = {'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(a , a ) if __name__ == "__main__": main()
26
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin UpperCAmelCase__ = "▁" UpperCAmelCase__ = get_tests_dir("fixtures/test_sentencepiece.model") @require_sentencepiece class lowercase_ ( lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = BertGenerationTokenizer __snake_case = False __snake_case = True def __lowerCAmelCase ( self : str ) ->str: """simple docstring""" super().setUp() a = BertGenerationTokenizer(__UpperCAmelCase , keep_accents=__UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self : int ) ->Dict: """simple docstring""" a = '''<s>''' a = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCAmelCase ) , __UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCAmelCase ) , __UpperCAmelCase ) def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''<pad>''' ) self.assertEqual(len(__UpperCAmelCase ) , 1_002 ) def __lowerCAmelCase ( self : List[str] ) ->List[Any]: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1_000 ) def __lowerCAmelCase ( self : Tuple ) ->Optional[int]: """simple docstring""" a = BertGenerationTokenizer(__UpperCAmelCase , keep_accents=__UpperCAmelCase ) a = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(__UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) , [285, 46, 10, 170, 382] , ) a = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( __UpperCAmelCase , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) a = tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) self.assertListEqual( __UpperCAmelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) a = tokenizer.convert_ids_to_tokens(__UpperCAmelCase ) self.assertListEqual( __UpperCAmelCase , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) @cached_property def __lowerCAmelCase ( self : List[Any] ) ->List[str]: """simple docstring""" return BertGenerationTokenizer.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder''' ) @slow def __lowerCAmelCase ( self : Any ) ->str: """simple docstring""" a = '''Hello World!''' a = [18_536, 2_260, 101] self.assertListEqual(__UpperCAmelCase , self.big_tokenizer.encode(__UpperCAmelCase ) ) @slow def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = ( '''This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will''' ''' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth''' ) a = [ 871, 419, 358, 946, 991, 2_521, 452, 358, 1_357, 387, 7_751, 3_536, 112, 985, 456, 126, 865, 938, 5_400, 5_734, 458, 1_368, 467, 786, 2_462, 5_246, 1_159, 633, 865, 4_519, 457, 582, 852, 2_557, 427, 916, 508, 405, 34_324, 497, 391, 408, 11_342, 1_244, 385, 100, 938, 985, 456, 574, 362, 12_597, 3_200, 3_129, 1_172, ] self.assertListEqual(__UpperCAmelCase , self.big_tokenizer.encode(__UpperCAmelCase ) ) @require_torch @slow def __lowerCAmelCase ( self : Any ) ->Dict: """simple docstring""" import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence a = list(self.big_tokenizer.get_vocab().keys() )[:10] a = ''' '''.join(__UpperCAmelCase ) a = self.big_tokenizer.encode_plus(__UpperCAmelCase , return_tensors='''pt''' , return_token_type_ids=__UpperCAmelCase ) a = self.big_tokenizer.batch_encode_plus( [sequence + ''' ''' + sequence] , return_tensors='''pt''' , return_token_type_ids=__UpperCAmelCase ) a = BertGenerationConfig() a = BertGenerationEncoder(__UpperCAmelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**__UpperCAmelCase ) model(**__UpperCAmelCase ) @slow def __lowerCAmelCase ( self : str ) ->Optional[Any]: """simple docstring""" a = {'''input_ids''': [[39_286, 458, 36_335, 2_001, 456, 13_073, 13_266, 455, 113, 7_746, 1_741, 11_157, 391, 13_073, 13_266, 455, 113, 3_967, 35_412, 113, 4_936, 109, 3_870, 2_377, 113, 30_084, 45_720, 458, 134, 17_496, 112, 503, 11_672, 113, 118, 112, 5_665, 13_347, 38_687, 112, 1_496, 31_389, 112, 3_268, 47_264, 134, 962, 112, 16_377, 8_035, 23_130, 430, 12_169, 15_518, 28_592, 458, 146, 41_697, 109, 391, 12_169, 15_518, 16_689, 458, 146, 41_358, 109, 452, 726, 4_034, 111, 763, 35_412, 5_082, 388, 1_903, 111, 9_051, 391, 2_870, 48_918, 1_900, 1_123, 550, 998, 112, 9_586, 15_985, 455, 391, 410, 22_955, 37_636, 114], [448, 17_496, 419, 3_663, 385, 763, 113, 27_533, 2_870, 3_283, 13_043, 1_639, 24_713, 523, 656, 24_013, 18_550, 2_521, 517, 27_014, 21_244, 420, 1_212, 1_465, 391, 927, 4_833, 388, 578, 11_786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2_169, 7_687, 21_932, 18_146, 726, 363, 17_032, 3_391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__UpperCAmelCase , model_name='''google/bert_for_seq_generation_L-24_bbc_encoder''' , revision='''c817d1fd1be2ffa69431227a1fe320544943d4db''' , )
26
1
import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger UpperCAmelCase__ = get_logger(__name__) UpperCAmelCase__ = Path(__file__).parent / "model_card_template.md" UpperCAmelCase__ = uuida().hex UpperCAmelCase__ = os.getenv("HF_HUB_OFFLINE", "").upper() in ENV_VARS_TRUE_VALUES UpperCAmelCase__ = os.getenv("DISABLE_TELEMETRY", "").upper() in ENV_VARS_TRUE_VALUES UpperCAmelCase__ = HUGGINGFACE_CO_RESOLVE_ENDPOINT + "/api/telemetry/" def _a ( a :Union[Dict, str, None] = None ) -> str: a = F"""diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}""" if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += F"""; torch/{_torch_version}""" if is_flax_available(): ua += F"""; jax/{_jax_version}""" ua += F"""; flax/{_flax_version}""" if is_onnx_available(): ua += F"""; onnxruntime/{_onnxruntime_version}""" # CI will set this value to True if os.environ.get('''DIFFUSERS_IS_CI''' , '''''' ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(a , a ): ua += "; " + "; ".join(F"""{k}/{v}""" for k, v in user_agent.items() ) elif isinstance(a , a ): ua += "; " + user_agent return ua def _a ( a :str , a :Optional[str] = None , a :Optional[str] = None ) -> Optional[Any]: if token is None: a = HfFolder.get_token() if organization is None: a = whoami(a )['''name'''] return F"""{username}/{model_id}""" else: return F"""{organization}/{model_id}""" def _a ( a :Optional[Any] , a :str ) -> str: if not is_jinja_available(): raise ValueError( '''Modelcard rendering is based on Jinja templates.''' ''' Please make sure to have `jinja` installed before using `create_model_card`.''' ''' To install it, please run `pip install Jinja2`.''' ) if hasattr(a , '''local_rank''' ) and args.local_rank not in [-1, 0]: return a = args.hub_token if hasattr(a , '''hub_token''' ) else None a = get_full_repo_name(a , token=a ) a = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language='''en''' , license='''apache-2.0''' , library_name='''diffusers''' , tags=[] , datasets=args.dataset_name , metrics=[] , ) , template_path=a , model_name=a , repo_name=a , dataset_name=args.dataset_name if hasattr(a , '''dataset_name''' ) else None , learning_rate=args.learning_rate , train_batch_size=args.train_batch_size , eval_batch_size=args.eval_batch_size , gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(a , '''gradient_accumulation_steps''' ) else None ) , adam_betaa=args.adam_betaa if hasattr(a , '''adam_beta1''' ) else None , adam_betaa=args.adam_betaa if hasattr(a , '''adam_beta2''' ) else None , adam_weight_decay=args.adam_weight_decay if hasattr(a , '''adam_weight_decay''' ) else None , adam_epsilon=args.adam_epsilon if hasattr(a , '''adam_epsilon''' ) else None , lr_scheduler=args.lr_scheduler if hasattr(a , '''lr_scheduler''' ) else None , lr_warmup_steps=args.lr_warmup_steps if hasattr(a , '''lr_warmup_steps''' ) else None , ema_inv_gamma=args.ema_inv_gamma if hasattr(a , '''ema_inv_gamma''' ) else None , ema_power=args.ema_power if hasattr(a , '''ema_power''' ) else None , ema_max_decay=args.ema_max_decay if hasattr(a , '''ema_max_decay''' ) else None , mixed_precision=args.mixed_precision , ) a = os.path.join(args.output_dir , '''README.md''' ) model_card.save(a ) def _a ( a :Optional[str] , a :Optional[str] = None ) -> int: if resolved_file is None or commit_hash is not None: return commit_hash a = str(Path(a ).as_posix() ) a = re.search(r'''snapshots/([^/]+)/''' , a ) if search is None: return None a = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(a ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. UpperCAmelCase__ = os.path.expanduser( os.getenv("HF_HOME", os.path.join(os.getenv("XDG_CACHE_HOME", "~/.cache"), "huggingface")) ) UpperCAmelCase__ = os.path.join(hf_cache_home, "diffusers") def _a ( a :Optional[str] = None , a :Optional[str] = None ) -> None: if new_cache_dir is None: a = DIFFUSERS_CACHE if old_cache_dir is None: a = old_diffusers_cache a = Path(a ).expanduser() a = Path(a ).expanduser() for old_blob_path in old_cache_dir.glob('''**/blobs/*''' ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): a = new_cache_dir / old_blob_path.relative_to(a ) new_blob_path.parent.mkdir(parents=a , exist_ok=a ) os.replace(a , a ) try: os.symlink(a , a ) except OSError: logger.warning( '''Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.''' ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). UpperCAmelCase__ = os.path.join(DIFFUSERS_CACHE, "version_diffusers_cache.txt") if not os.path.isfile(cache_version_file): UpperCAmelCase__ = 0 else: with open(cache_version_file) as f: try: UpperCAmelCase__ = int(f.read()) except ValueError: UpperCAmelCase__ = 0 if cache_version < 1: UpperCAmelCase__ = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( "The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your " "existing cached models. This is a one-time operation, you can interrupt it or run it " "later by calling `diffusers.utils.hub_utils.move_cache()`." ) try: move_cache() except Exception as e: UpperCAmelCase__ = "\n".join(traceback.format_tb(e.__traceback__)) logger.error( f"""There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease """ "file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole " "message and we will do our best to help." ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, "w") as f: f.write("1") except Exception: logger.warning( f"""There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure """ "the directory exists and can be written to." ) def _a ( a :str , a :Optional[str] = None ) -> str: if variant is not None: a = weights_name.split('''.''' ) a = splits[:-1] + [variant] + splits[-1:] a = '''.'''.join(a ) return weights_name def _a ( a :Dict , *, a :Tuple , a :Any , a :Any , a :Dict , a :str , a :List[str] , a :List[str] , a :Optional[int] , a :Any , a :List[Any] , a :Optional[Any]=None , ) -> Any: a = str(a ) if os.path.isfile(a ): return pretrained_model_name_or_path elif os.path.isdir(a ): if os.path.isfile(os.path.join(a , a ) ): # Load from a PyTorch checkpoint a = os.path.join(a , a ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(a , a , a ) ): a = os.path.join(a , a , a ) return model_file else: raise EnvironmentError( F"""Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.""" ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(a ).base_version ) >= version.parse('''0.20.0''' ) ): try: a = hf_hub_download( a , filename=_add_variant(a , a ) , cache_dir=a , force_download=a , proxies=a , resume_download=a , local_files_only=a , use_auth_token=a , user_agent=a , subfolder=a , revision=revision or commit_hash , ) warnings.warn( F"""Loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'` is deprecated. Loading instead from `revision='main'` with `variant={revision}`. Loading model variants via `revision='{revision}'` will be removed in diffusers v1. Please use `variant='{revision}'` instead.""" , a , ) return model_file except: # noqa: E722 warnings.warn( F"""You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant='{revision}'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(a , a )} file in the 'main' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title '{pretrained_model_name_or_path} is missing {_add_variant(a , a )}' so that the correct variant file can be added.""" , a , ) try: # 2. Load model file as usual a = hf_hub_download( a , filename=a , cache_dir=a , force_download=a , proxies=a , resume_download=a , local_files_only=a , use_auth_token=a , user_agent=a , subfolder=a , revision=revision or commit_hash , ) return model_file except RepositoryNotFoundError: raise EnvironmentError( F"""{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier """ '''listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a ''' '''token having permission to this repo with `use_auth_token` or log in with `huggingface-cli ''' '''login`.''' ) except RevisionNotFoundError: raise EnvironmentError( F"""{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for """ '''this model name. Check the model page at ''' F"""'https://huggingface.co/{pretrained_model_name_or_path}' for available revisions.""" ) except EntryNotFoundError: raise EnvironmentError( F"""{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.""" ) except HTTPError as err: raise EnvironmentError( F"""There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}""" ) except ValueError: raise EnvironmentError( F"""We couldn't connect to '{HUGGINGFACE_CO_RESOLVE_ENDPOINT}' to load this model, couldn't find it""" F""" in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a""" F""" directory containing a file named {weights_name} or""" ''' \nCheckout your internet connection or see how to run the library in''' ''' offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.''' ) except EnvironmentError: raise EnvironmentError( F"""Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it from """ '''\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. ''' F"""Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory """ F"""containing a file named {weights_name}""" )
26
import argparse import numpy as np import torch from transformers import SpeechTaHifiGan, SpeechTaHifiGanConfig, logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger("transformers.models.speecht5") def _a ( a :Optional[Any] , a :Tuple , a :Dict ) -> List[str]: hf_model.apply_weight_norm() a = checkpoint['''input_conv.weight_g'''] a = checkpoint['''input_conv.weight_v'''] a = checkpoint['''input_conv.bias'''] for i in range(len(config.upsample_rates ) ): a = checkpoint[F"""upsamples.{i}.1.weight_g"""] a = checkpoint[F"""upsamples.{i}.1.weight_v"""] a = checkpoint[F"""upsamples.{i}.1.bias"""] for i in range(len(config.upsample_rates ) * len(config.resblock_kernel_sizes ) ): for j in range(len(config.resblock_dilation_sizes ) ): a = checkpoint[F"""blocks.{i}.convs1.{j}.1.weight_g"""] a = checkpoint[F"""blocks.{i}.convs1.{j}.1.weight_v"""] a = checkpoint[F"""blocks.{i}.convs1.{j}.1.bias"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.weight_g"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.weight_v"""] a = checkpoint[F"""blocks.{i}.convs2.{j}.1.bias"""] a = checkpoint['''output_conv.1.weight_g'''] a = checkpoint['''output_conv.1.weight_v'''] a = checkpoint['''output_conv.1.bias'''] hf_model.remove_weight_norm() @torch.no_grad() def _a ( a :List[str] , a :Union[str, Any] , a :Dict , a :Dict=None , a :List[Any]=None , ) -> int: if config_path is not None: a = SpeechTaHifiGanConfig.from_pretrained(a ) else: a = SpeechTaHifiGanConfig() a = SpeechTaHifiGan(a ) a = torch.load(a ) load_weights(orig_checkpoint['''model''']['''generator'''] , a , a ) a = np.load(a ) a = stats[0].reshape(-1 ) a = stats[1].reshape(-1 ) a = torch.from_numpy(a ).float() a = torch.from_numpy(a ).float() model.save_pretrained(a ) if repo_id: print('''Pushing to the hub...''' ) model.push_to_hub(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--checkpoint_path", required=True, default=None, type=str, help="Path to original checkpoint") parser.add_argument("--stats_path", required=True, default=None, type=str, help="Path to stats.npy file") 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." ) UpperCAmelCase__ = parser.parse_args() convert_hifigan_checkpoint( args.checkpoint_path, args.stats_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
26
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { "facebook/levit-128S": "https://huggingface.co/facebook/levit-128S/resolve/main/config.json", # See all LeViT models at https://huggingface.co/models?filter=levit } class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = '''levit''' def __init__( self : str , __UpperCAmelCase : Dict=224 , __UpperCAmelCase : int=3 , __UpperCAmelCase : Dict=3 , __UpperCAmelCase : Optional[Any]=2 , __UpperCAmelCase : Dict=1 , __UpperCAmelCase : Optional[Any]=16 , __UpperCAmelCase : Optional[int]=[128, 256, 384] , __UpperCAmelCase : Optional[int]=[4, 8, 12] , __UpperCAmelCase : Dict=[4, 4, 4] , __UpperCAmelCase : Dict=[16, 16, 16] , __UpperCAmelCase : Optional[int]=0 , __UpperCAmelCase : Optional[int]=[2, 2, 2] , __UpperCAmelCase : Optional[int]=[2, 2, 2] , __UpperCAmelCase : Dict=0.02 , **__UpperCAmelCase : Dict , ) ->int: """simple docstring""" super().__init__(**__UpperCAmelCase ) a = image_size a = num_channels a = kernel_size a = stride a = padding a = hidden_sizes a = num_attention_heads a = depths a = key_dim a = drop_path_rate a = patch_size a = attention_ratio a = mlp_ratio a = initializer_range a = [ ['''Subsample''', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2], ['''Subsample''', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2], ] class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = version.parse('''1.11''' ) @property def __lowerCAmelCase ( self : str ) ->Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __lowerCAmelCase ( self : int ) ->float: """simple docstring""" return 1e-4
26
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) UpperCAmelCase__ = { "configuration_gpt_bigcode": ["GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP", "GPTBigCodeConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST", "GPTBigCodeForSequenceClassification", "GPTBigCodeForTokenClassification", "GPTBigCodeForCausalLM", "GPTBigCodeModel", "GPTBigCodePreTrainedModel", ] if TYPE_CHECKING: from .configuration_gpt_bigcode import GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTBigCodeConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_bigcode import ( GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTBigCodeForCausalLM, GPTBigCodeForSequenceClassification, GPTBigCodeForTokenClassification, GPTBigCodeModel, GPTBigCodePreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
1
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { "xlm-mlm-en-2048": "https://huggingface.co/xlm-mlm-en-2048/resolve/main/config.json", "xlm-mlm-ende-1024": "https://huggingface.co/xlm-mlm-ende-1024/resolve/main/config.json", "xlm-mlm-enfr-1024": "https://huggingface.co/xlm-mlm-enfr-1024/resolve/main/config.json", "xlm-mlm-enro-1024": "https://huggingface.co/xlm-mlm-enro-1024/resolve/main/config.json", "xlm-mlm-tlm-xnli15-1024": "https://huggingface.co/xlm-mlm-tlm-xnli15-1024/resolve/main/config.json", "xlm-mlm-xnli15-1024": "https://huggingface.co/xlm-mlm-xnli15-1024/resolve/main/config.json", "xlm-clm-enfr-1024": "https://huggingface.co/xlm-clm-enfr-1024/resolve/main/config.json", "xlm-clm-ende-1024": "https://huggingface.co/xlm-clm-ende-1024/resolve/main/config.json", "xlm-mlm-17-1280": "https://huggingface.co/xlm-mlm-17-1280/resolve/main/config.json", "xlm-mlm-100-1280": "https://huggingface.co/xlm-mlm-100-1280/resolve/main/config.json", } class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = '''xlm''' __snake_case = { '''hidden_size''': '''emb_dim''', '''num_attention_heads''': '''n_heads''', '''num_hidden_layers''': '''n_layers''', '''n_words''': '''vocab_size''', # For backward compatibility } def __init__( self : Any , __UpperCAmelCase : Union[str, Any]=30_145 , __UpperCAmelCase : int=2_048 , __UpperCAmelCase : Union[str, Any]=12 , __UpperCAmelCase : List[str]=16 , __UpperCAmelCase : Optional[int]=0.1 , __UpperCAmelCase : Any=0.1 , __UpperCAmelCase : List[str]=True , __UpperCAmelCase : Tuple=False , __UpperCAmelCase : Optional[Any]=False , __UpperCAmelCase : Any=False , __UpperCAmelCase : List[str]=1 , __UpperCAmelCase : int=True , __UpperCAmelCase : str=512 , __UpperCAmelCase : int=2_048**-0.5 , __UpperCAmelCase : Optional[Any]=1e-1_2 , __UpperCAmelCase : Tuple=0.02 , __UpperCAmelCase : Optional[int]=0 , __UpperCAmelCase : Dict=1 , __UpperCAmelCase : str=2 , __UpperCAmelCase : int=3 , __UpperCAmelCase : Optional[int]=5 , __UpperCAmelCase : int=True , __UpperCAmelCase : str="first" , __UpperCAmelCase : Optional[Any]=True , __UpperCAmelCase : Tuple=None , __UpperCAmelCase : Any=True , __UpperCAmelCase : Union[str, Any]=0.1 , __UpperCAmelCase : Any=5 , __UpperCAmelCase : List[str]=5 , __UpperCAmelCase : List[str]=0 , __UpperCAmelCase : Union[str, Any]=0 , __UpperCAmelCase : str=2 , __UpperCAmelCase : Optional[int]=0 , **__UpperCAmelCase : Dict , ) ->int: """simple docstring""" a = vocab_size a = emb_dim a = n_layers a = n_heads a = dropout a = attention_dropout a = gelu_activation a = sinusoidal_embeddings a = causal a = asm a = n_langs a = use_lang_emb a = layer_norm_eps a = bos_index a = eos_index a = pad_index a = unk_index a = mask_index a = is_encoder a = max_position_embeddings a = embed_init_std a = init_std a = summary_type a = summary_use_proj a = summary_activation a = summary_proj_to_labels a = summary_first_dropout a = start_n_top a = end_n_top a = mask_token_id a = lang_id if "n_words" in kwargs: a = kwargs['''n_words'''] super().__init__(pad_token_id=__UpperCAmelCase , bos_token_id=__UpperCAmelCase , **__UpperCAmelCase ) class lowercase_ ( lowercase ): '''simple docstring''' @property def __lowerCAmelCase ( self : List[Any] ) ->Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": a = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: a = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis), ] )
26
import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def _a ( a :Tuple ) -> int: a = tmp_path / '''file.csv''' a = textwrap.dedent( '''\ header1,header2 1,2 10,20 ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :int ) -> List[str]: a = tmp_path / '''malformed_file.csv''' a = textwrap.dedent( '''\ header1,header2 1,2 10,20, ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :Dict , a :int ) -> List[str]: a = tmp_path / '''csv_with_image.csv''' a = textwrap.dedent( F"""\ image {image_file} """ ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :List[Any] ) -> Dict: a = tmp_path / '''csv_with_label.csv''' a = textwrap.dedent( '''\ label good bad good ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) @pytest.fixture def _a ( a :Tuple ) -> Any: a = tmp_path / '''csv_with_int_list.csv''' a = textwrap.dedent( '''\ int_list 1 2 3 4 5 6 7 8 9 ''' ) with open(a , '''w''' ) as f: f.write(a ) return str(a ) def _a ( a :Dict , a :int , a :Union[str, Any] ) -> List[Any]: a = Csv() a = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(a , match='''Error tokenizing data''' ): for _ in generator: pass assert any( record.levelname == '''ERROR''' and '''Failed to read file''' in record.message and os.path.basename(a ) in record.message for record in caplog.records ) @require_pil def _a ( a :Dict ) -> Any: with open(a , encoding='''utf-8''' ) as f: a = f.read().splitlines()[1] a = Csv(encoding='''utf-8''' , features=Features({'''image''': Image()} ) ) a = csv._generate_tables([[csv_file_with_image]] ) a = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''image''' ).type == Image()() a = pa_table.to_pydict()['''image'''] assert generated_content == [{"path": image_file, "bytes": None}] def _a ( a :Any ) -> Tuple: with open(a , encoding='''utf-8''' ) as f: a = f.read().splitlines()[1:] a = Csv(encoding='''utf-8''' , features=Features({'''label''': ClassLabel(names=['''good''', '''bad'''] )} ) ) a = csv._generate_tables([[csv_file_with_label]] ) a = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''label''' ).type == ClassLabel(names=['''good''', '''bad'''] )() a = pa_table.to_pydict()['''label'''] assert generated_content == [ClassLabel(names=['''good''', '''bad'''] ).straint(a ) for label in labels] def _a ( a :Union[str, Any] ) -> Optional[Any]: a = Csv(encoding='''utf-8''' , sep=''',''' , converters={'''int_list''': lambda a : [int(a ) for i in x.split()]} ) a = csv._generate_tables([[csv_file_with_int_list]] ) a = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field('''int_list''' ).type ) a = pa_table.to_pydict()['''int_list'''] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
26
1
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class lowercase_ ( lowercase , lowercase , lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = StableDiffusionInpaintPipeline __snake_case = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS __snake_case = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __snake_case = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess __snake_case = frozenset([] ) def __lowerCAmelCase ( self : Dict ) ->Optional[int]: """simple docstring""" torch.manual_seed(0 ) a = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=9 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=__UpperCAmelCase , ) a = PNDMScheduler(skip_prk_steps=__UpperCAmelCase ) torch.manual_seed(0 ) a = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) a = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , hidden_act='''gelu''' , projection_dim=512 , ) a = CLIPTextModel(__UpperCAmelCase ) a = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) a = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : Dict=0 ) ->List[Any]: """simple docstring""" a = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCAmelCase ) ).to(__UpperCAmelCase ) a = image.cpu().permute(0 , 2 , 3 , 1 )[0] a = Image.fromarray(np.uinta(__UpperCAmelCase ) ).convert('''RGB''' ).resize((64, 64) ) a = Image.fromarray(np.uinta(image + 4 ) ).convert('''RGB''' ).resize((64, 64) ) if str(__UpperCAmelCase ).startswith('''mps''' ): a = torch.manual_seed(__UpperCAmelCase ) else: a = torch.Generator(device=__UpperCAmelCase ).manual_seed(__UpperCAmelCase ) a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': init_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def __lowerCAmelCase ( self : Any ) ->Any: """simple docstring""" a = '''cpu''' # ensure determinism for the device-dependent torch.Generator a = self.get_dummy_components() a = StableDiffusionInpaintPipeline(**__UpperCAmelCase ) a = sd_pipe.to(__UpperCAmelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase ) a = self.get_dummy_inputs(__UpperCAmelCase ) a = sd_pipe(**__UpperCAmelCase ).images a = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) a = np.array([0.4727, 0.5735, 0.3941, 0.5446, 0.5926, 0.4394, 0.5062, 0.4654, 0.4476] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __lowerCAmelCase ( self : Union[str, Any] ) ->List[str]: """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCAmelCase ( self : str ) ->str: """simple docstring""" a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench.npy''' ) a = '''stabilityai/stable-diffusion-2-inpainting''' a = StableDiffusionInpaintPipeline.from_pretrained(__UpperCAmelCase , safety_checker=__UpperCAmelCase ) pipe.to(__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) pipe.enable_attention_slicing() a = '''Face of a yellow cat, high resolution, sitting on a park bench''' a = torch.manual_seed(0 ) a = pipe( prompt=__UpperCAmelCase , image=__UpperCAmelCase , mask_image=__UpperCAmelCase , generator=__UpperCAmelCase , output_type='''np''' , ) a = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 9e-3 def __lowerCAmelCase ( self : str ) ->Dict: """simple docstring""" a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench_fp16.npy''' ) a = '''stabilityai/stable-diffusion-2-inpainting''' a = StableDiffusionInpaintPipeline.from_pretrained( __UpperCAmelCase , torch_dtype=torch.floataa , safety_checker=__UpperCAmelCase , ) pipe.to(__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) pipe.enable_attention_slicing() a = '''Face of a yellow cat, high resolution, sitting on a park bench''' a = torch.manual_seed(0 ) a = pipe( prompt=__UpperCAmelCase , image=__UpperCAmelCase , mask_image=__UpperCAmelCase , generator=__UpperCAmelCase , output_type='''np''' , ) a = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 5e-1 def __lowerCAmelCase ( self : Union[str, Any] ) ->int: """simple docstring""" torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) a = '''stabilityai/stable-diffusion-2-inpainting''' a = PNDMScheduler.from_pretrained(__UpperCAmelCase , subfolder='''scheduler''' ) a = StableDiffusionInpaintPipeline.from_pretrained( __UpperCAmelCase , safety_checker=__UpperCAmelCase , scheduler=__UpperCAmelCase , torch_dtype=torch.floataa , ) pipe.to(__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() a = '''Face of a yellow cat, high resolution, sitting on a park bench''' a = torch.manual_seed(0 ) a = pipe( prompt=__UpperCAmelCase , image=__UpperCAmelCase , mask_image=__UpperCAmelCase , generator=__UpperCAmelCase , num_inference_steps=2 , output_type='''np''' , ) a = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 10**9
26
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SwiftFormerConfig, SwiftFormerForImageClassification, ViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = torch.device("cpu") def _a ( ) -> Union[str, Any]: a = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a = Image.open(requests.get(a , stream=a ).raw ) return im def _a ( a :Dict ) -> Tuple: if swiftformer_name == "swiftformer_xs": return torch.tensor([-2.1703e00, 2.1107e00, -2.0811e00, 8.8685e-01, 2.4360e-01] ) elif swiftformer_name == "swiftformer_s": return torch.tensor([3.9636e-01, 2.3478e-01, -1.6963e00, -1.7381e00, -8.6337e-01] ) elif swiftformer_name == "swiftformer_l1": return torch.tensor([-4.2768e-01, -4.7429e-01, -1.0897e00, -1.0248e00, 3.5523e-02] ) elif swiftformer_name == "swiftformer_l3": return torch.tensor([-2.5330e-01, 2.4211e-01, -6.0185e-01, -8.2789e-01, -6.0446e-02] ) def _a ( a :int , a :Any , a :Union[str, Any] ) -> int: a = dct.pop(a ) a = val def _a ( a :Any ) -> Dict: a = [] for k in state_dict.keys(): a = k if ".pwconv" in k: a = k_new.replace('''.pwconv''' , '''.point_wise_conv''' ) if ".dwconv" in k: a = k_new.replace('''.dwconv''' , '''.depth_wise_conv''' ) if ".Proj." in k: a = k_new.replace('''.Proj.''' , '''.proj.''' ) if "patch_embed" in k_new: a = k_new.replace('''patch_embed''' , '''swiftformer.patch_embed.patch_embedding''' ) if "network" in k_new: a = k_new.split('''.''' ) if ls[2].isdigit(): a = '''swiftformer.encoder.network.''' + ls[1] + '''.blocks.''' + ls[2] + '''.''' + '''.'''.join(ls[3:] ) else: a = k_new.replace('''network''' , '''swiftformer.encoder.network''' ) rename_keys.append((k, k_new) ) return rename_keys @torch.no_grad() def _a ( a :List[Any] , a :Tuple , a :List[str] ) -> Union[str, Any]: a = SwiftFormerConfig() # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size a = 1_000 a = '''huggingface/label-files''' a = '''imagenet-1k-id2label.json''' a = json.load(open(hf_hub_download(a , a , repo_type='''dataset''' ) , '''r''' ) ) a = {int(a ): v for k, v in idalabel.items()} a = idalabel a = {v: k for k, v in idalabel.items()} # size of the architecture if swiftformer_name == "swiftformer_xs": a = [3, 3, 6, 4] a = [48, 56, 112, 220] elif swiftformer_name == "swiftformer_s": a = [3, 3, 9, 6] a = [48, 64, 168, 224] elif swiftformer_name == "swiftformer_l1": a = [4, 3, 10, 5] a = [48, 96, 192, 384] elif swiftformer_name == "swiftformer_l3": a = [4, 4, 12, 6] a = [64, 128, 320, 512] # load state_dict of original model, remove and rename some keys if original_ckpt: if original_ckpt.startswith('''https''' ): a = torch.hub.load_state_dict_from_url(a , map_location='''cpu''' , check_hash=a ) else: a = torch.load(a , map_location='''cpu''' ) a = checkpoint a = create_rename_keys(a ) for rename_key_src, rename_key_dest in rename_keys: rename_key(a , a , a ) # load HuggingFace model a = SwiftFormerForImageClassification(a ).eval() hf_model.load_state_dict(a ) # prepare test inputs a = prepare_img() a = ViTImageProcessor.from_pretrained('''preprocessor_config''' ) a = processor(images=a , return_tensors='''pt''' ) # compare outputs from both models a = get_expected_output(a ) a = hf_model(inputs['''pixel_values'''] ).logits assert hf_logits.shape == torch.Size([1, 1_000] ) assert torch.allclose(hf_logits[0, 0:5] , a , atol=1e-3 ) Path(a ).mkdir(exist_ok=a ) print(F"""Saving model {swiftformer_name} to {pytorch_dump_folder_path}""" ) hf_model.save_pretrained(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--swiftformer_name", default="swiftformer_xs", choices=["swiftformer_xs", "swiftformer_s", "swiftformer_l1", "swiftformer_l3"], type=str, help="Name of the SwiftFormer model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default="./converted_outputs/", type=str, help="Path to the output PyTorch model directory.", ) parser.add_argument("--original_ckpt", default=None, type=str, help="Path to the original model checkpoint.") UpperCAmelCase__ = parser.parse_args() convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)
26
1