code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation lowercase : List[str] = logging.get_logger(__name__) lowercase : str = { """vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_config_file""": """tokenizer_config.json""", } lowercase : Optional[Any] = { """vocab_file""": {"""facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json"""}, """merges_file""": {"""facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt"""}, """tokenizer_config_file""": { """facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json""" }, } lowercase : List[Any] = {"""facebook/blenderbot-3B""": 128} class __snake_case ( lowerCAmelCase ): _a : Tuple= VOCAB_FILES_NAMES _a : Union[str, Any]= PRETRAINED_VOCAB_FILES_MAP _a : Optional[int]= PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _a : List[Any]= ["input_ids", "attention_mask"] _a : Tuple= BlenderbotTokenizer def __init__( self ,snake_case=None ,snake_case=None ,snake_case=None ,snake_case="replace" ,snake_case="<s>" ,snake_case="</s>" ,snake_case="</s>" ,snake_case="<s>" ,snake_case="<unk>" ,snake_case="<pad>" ,snake_case="<mask>" ,snake_case=False ,snake_case=True ,**snake_case ,): '''simple docstring''' super().__init__( snake_case ,snake_case ,tokenizer_file=snake_case ,errors=snake_case ,bos_token=snake_case ,eos_token=snake_case ,sep_token=snake_case ,cls_token=snake_case ,unk_token=snake_case ,pad_token=snake_case ,mask_token=snake_case ,add_prefix_space=snake_case ,trim_offsets=snake_case ,**snake_case ,) lowercase : Tuple = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("""add_prefix_space""" ,snake_case ) != add_prefix_space: lowercase : Union[str, Any] = getattr(snake_case ,pre_tok_state.pop("""type""" ) ) lowercase : str = add_prefix_space lowercase : Any = pre_tok_class(**snake_case ) lowercase : Tuple = add_prefix_space lowercase : int = """post_processor""" lowercase : Dict = getattr(self.backend_tokenizer ,snake_case ,snake_case ) if tokenizer_component_instance: lowercase : int = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: lowercase : Any = tuple(state["""sep"""] ) if "cls" in state: lowercase : Union[str, Any] = tuple(state["""cls"""] ) lowercase : int = False if state.get("""add_prefix_space""" ,snake_case ) != add_prefix_space: lowercase : List[str] = add_prefix_space lowercase : Union[str, Any] = True if state.get("""trim_offsets""" ,snake_case ) != trim_offsets: lowercase : str = trim_offsets lowercase : str = True if changes_to_apply: lowercase : Union[str, Any] = getattr(snake_case ,state.pop("""type""" ) ) lowercase : int = component_class(**snake_case ) setattr(self.backend_tokenizer ,snake_case ,snake_case ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self._mask_token is None: if self.verbose: logger.error("""Using mask_token, but it is not set yet.""" ) return None return str(self._mask_token ) @mask_token.setter def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : str = AddedToken(snake_case ,lstrip=snake_case ,rstrip=snake_case ) if isinstance(snake_case ,snake_case ) else value lowercase : List[Any] = value def _SCREAMING_SNAKE_CASE ( self ,*snake_case ,**snake_case ): '''simple docstring''' lowercase : Optional[Any] = kwargs.get("""is_split_into_words""" ,snake_case ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*snake_case ,**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,*snake_case ,**snake_case ): '''simple docstring''' lowercase : List[str] = kwargs.get("""is_split_into_words""" ,snake_case ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._encode_plus(*snake_case ,**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = None ): '''simple docstring''' lowercase : int = self._tokenizer.model.save(snake_case ,name=snake_case ) return tuple(snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = None ): '''simple docstring''' lowercase : Optional[int] = [self.sep_token_id] lowercase : List[str] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = None ): '''simple docstring''' return token_ids_a + [self.eos_token_id] def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Dict = [] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(""" """ + text ) else: # Generated responses should contain them already. inputs.append(snake_case ) lowercase : List[Any] = """ """.join(snake_case ) lowercase : Optional[Any] = self.encode(snake_case ) if len(snake_case ) > self.model_max_length: lowercase : Any = input_ids[-self.model_max_length :] logger.warning(f"Trimmed input from conversation as it was longer than {self.model_max_length} tokens." ) return input_ids
20
from collections import UserDict from typing import Union import numpy as np import requests from ..utils import ( add_end_docstrings, logging, ) from .audio_classification import ffmpeg_read from .base import PIPELINE_INIT_ARGS, Pipeline lowercase : Optional[Any] = logging.get_logger(__name__) @add_end_docstrings(lowerCAmelCase ) class __snake_case ( lowerCAmelCase ): def __init__( self ,**snake_case ): '''simple docstring''' super().__init__(**snake_case ) if self.framework != "pt": raise ValueError(f"The {self.__class__} is only available in PyTorch." ) # No specific FOR_XXX available yet def __call__( self ,snake_case ,**snake_case ): '''simple docstring''' return super().__call__(snake_case ,**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,**snake_case ): '''simple docstring''' lowercase : Union[str, Any] = {} if "candidate_labels" in kwargs: lowercase : List[str] = kwargs["""candidate_labels"""] if "hypothesis_template" in kwargs: lowercase : Dict = kwargs["""hypothesis_template"""] return preprocess_params, {}, {} def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ,snake_case="This is a sound of {}." ): '''simple docstring''' if isinstance(snake_case ,snake_case ): if audio.startswith("""http://""" ) or audio.startswith("""https://""" ): # We need to actually check for a real protocol, otherwise it's impossible to use a local file # like http_huggingface_co.png lowercase : Optional[Any] = requests.get(snake_case ).content else: with open(snake_case ,"""rb""" ) as f: lowercase : Union[str, Any] = f.read() if isinstance(snake_case ,snake_case ): lowercase : int = ffmpeg_read(snake_case ,self.feature_extractor.sampling_rate ) if not isinstance(snake_case ,np.ndarray ): raise ValueError("""We expect a numpy ndarray as input""" ) if len(audio.shape ) != 1: raise ValueError("""We expect a single channel audio input for ZeroShotAudioClassificationPipeline""" ) lowercase : Dict = self.feature_extractor( [audio] ,sampling_rate=self.feature_extractor.sampling_rate ,return_tensors="""pt""" ) lowercase : Tuple = candidate_labels lowercase : Tuple = [hypothesis_template.format(snake_case ) for x in candidate_labels] lowercase : Optional[Any] = self.tokenizer(snake_case ,return_tensors=self.framework ,padding=snake_case ) lowercase : Optional[Any] = [text_inputs] return inputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : List[str] = model_inputs.pop("""candidate_labels""" ) lowercase : Dict = model_inputs.pop("""text_inputs""" ) if isinstance(text_inputs[0] ,snake_case ): lowercase : List[Any] = text_inputs[0] else: # Batching case. lowercase : Dict = text_inputs[0][0] lowercase : Optional[Any] = self.model(**snake_case ,**snake_case ) lowercase : Any = { """candidate_labels""": candidate_labels, """logits""": outputs.logits_per_audio, } return model_outputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : List[Any] = model_outputs.pop("""candidate_labels""" ) lowercase : Any = model_outputs["""logits"""][0] if self.framework == "pt": lowercase : Any = logits.softmax(dim=0 ) lowercase : Tuple = probs.tolist() else: raise ValueError("""`tf` framework not supported.""" ) lowercase : Tuple = [ {"""score""": score, """label""": candidate_label} for score, candidate_label in sorted(zip(snake_case ,snake_case ) ,key=lambda snake_case : -x[0] ) ] return result
20
1
from typing import Dict, Optional import numpy as np import datasets lowercase : List[str] = """ IoU is the area of overlap between the predicted segmentation and the ground truth divided by the area of union between the predicted segmentation and the ground truth. For binary (two classes) or multi-class segmentation, the mean IoU of the image is calculated by taking the IoU of each class and averaging them. """ lowercase : Tuple = """ Args: predictions (`List[ndarray]`): List of predicted segmentation maps, each of shape (height, width). Each segmentation map can be of a different size. references (`List[ndarray]`): List of ground truth segmentation maps, each of shape (height, width). Each segmentation map can be of a different size. num_labels (`int`): Number of classes (categories). ignore_index (`int`): Index that will be ignored during evaluation. nan_to_num (`int`, *optional*): If specified, NaN values will be replaced by the number defined by the user. label_map (`dict`, *optional*): If specified, dictionary mapping old label indices to new label indices. reduce_labels (`bool`, *optional*, defaults to `False`): Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is used for background, and background itself is not included in all classes of a dataset (e.g. ADE20k). The background label will be replaced by 255. Returns: `Dict[str, float | ndarray]` comprising various elements: - *mean_iou* (`float`): Mean Intersection-over-Union (IoU averaged over all categories). - *mean_accuracy* (`float`): Mean accuracy (averaged over all categories). - *overall_accuracy* (`float`): Overall accuracy on all images. - *per_category_accuracy* (`ndarray` of shape `(num_labels,)`): Per category accuracy. - *per_category_iou* (`ndarray` of shape `(num_labels,)`): Per category IoU. Examples: >>> import numpy as np >>> mean_iou = datasets.load_metric(\"mean_iou\") >>> # suppose one has 3 different segmentation maps predicted >>> predicted_1 = np.array([[1, 2], [3, 4], [5, 255]]) >>> actual_1 = np.array([[0, 3], [5, 4], [6, 255]]) >>> predicted_2 = np.array([[2, 7], [9, 2], [3, 6]]) >>> actual_2 = np.array([[1, 7], [9, 2], [3, 6]]) >>> predicted_3 = np.array([[2, 2, 3], [8, 2, 4], [3, 255, 2]]) >>> actual_3 = np.array([[1, 2, 2], [8, 2, 1], [3, 255, 1]]) >>> predicted = [predicted_1, predicted_2, predicted_3] >>> ground_truth = [actual_1, actual_2, actual_3] >>> results = mean_iou.compute(predictions=predicted, references=ground_truth, num_labels=10, ignore_index=255, reduce_labels=False) >>> print(results) # doctest: +NORMALIZE_WHITESPACE {'mean_iou': 0.47750000000000004, 'mean_accuracy': 0.5916666666666666, 'overall_accuracy': 0.5263157894736842, 'per_category_iou': array([0. , 0. , 0.375, 0.4 , 0.5 , 0. , 0.5 , 1. , 1. , 1. ]), 'per_category_accuracy': array([0. , 0. , 0.75 , 0.66666667, 1. , 0. , 0.5 , 1. , 1. , 1. ])} """ lowercase : List[Any] = """\ @software{MMSegmentation_Contributors_OpenMMLab_Semantic_Segmentation_2020, author = {{MMSegmentation Contributors}}, license = {Apache-2.0}, month = {7}, title = {{OpenMMLab Semantic Segmentation Toolbox and Benchmark}}, url = {https://github.com/open-mmlab/mmsegmentation}, year = {2020} }""" def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = False , ) -> Union[str, Any]: if label_map is not None: for old_id, new_id in label_map.items(): lowercase : Union[str, Any] = new_id # turn into Numpy arrays lowercase : Optional[int] = np.array(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[int] = np.array(SCREAMING_SNAKE_CASE__ ) if reduce_labels: lowercase : Optional[Any] = 255 lowercase : int = label - 1 lowercase : int = 255 lowercase : Tuple = label != ignore_index lowercase : Optional[int] = np.not_equal(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : int = pred_label[mask] lowercase : Dict = np.array(SCREAMING_SNAKE_CASE__ )[mask] lowercase : Union[str, Any] = pred_label[pred_label == label] lowercase : List[Any] = np.histogram(SCREAMING_SNAKE_CASE__ , bins=SCREAMING_SNAKE_CASE__ , range=(0, num_labels - 1) )[0] lowercase : Any = np.histogram(SCREAMING_SNAKE_CASE__ , bins=SCREAMING_SNAKE_CASE__ , range=(0, num_labels - 1) )[0] lowercase : Tuple = np.histogram(SCREAMING_SNAKE_CASE__ , bins=SCREAMING_SNAKE_CASE__ , range=(0, num_labels - 1) )[0] lowercase : str = area_pred_label + area_label - area_intersect return area_intersect, area_union, area_pred_label, area_label def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = False , ) -> List[Any]: lowercase : Dict = np.zeros((num_labels,) , dtype=np.floataa ) lowercase : Dict = np.zeros((num_labels,) , dtype=np.floataa ) lowercase : Optional[Any] = np.zeros((num_labels,) , dtype=np.floataa ) lowercase : Optional[int] = np.zeros((num_labels,) , dtype=np.floataa ) for result, gt_seg_map in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowercase , lowercase , lowercase , lowercase : List[str] = intersect_and_union( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) total_area_intersect += area_intersect total_area_union += area_union total_area_pred_label += area_pred_label total_area_label += area_label return total_area_intersect, total_area_union, total_area_pred_label, total_area_label def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = False , ) -> Optional[Any]: lowercase , lowercase , lowercase , lowercase : Tuple = total_intersect_and_union( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # compute metrics lowercase : str = {} lowercase : List[Any] = total_area_intersect.sum() / total_area_label.sum() lowercase : List[Any] = total_area_intersect / total_area_union lowercase : Union[str, Any] = total_area_intersect / total_area_label lowercase : Union[str, Any] = np.nanmean(SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = np.nanmean(SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = all_acc lowercase : str = iou lowercase : int = acc if nan_to_num is not None: lowercase : Tuple = {metric: np.nan_to_num(SCREAMING_SNAKE_CASE__ , nan=SCREAMING_SNAKE_CASE__ ) for metric, metric_value in metrics.items()} return metrics @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __snake_case ( datasets.Metric ): def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( # 1st Seq - height dim, 2nd - width dim { """predictions""": datasets.Sequence(datasets.Sequence(datasets.Value("""uint16""" ) ) ), """references""": datasets.Sequence(datasets.Sequence(datasets.Value("""uint16""" ) ) ), } ) ,reference_urls=[ """https://github.com/open-mmlab/mmsegmentation/blob/71c201b1813267d78764f306a297ca717827c4bf/mmseg/core/evaluation/metrics.py""" ] ,) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case = None ,snake_case = None ,snake_case = False ,): '''simple docstring''' lowercase : Union[str, Any] = mean_iou( results=snake_case ,gt_seg_maps=snake_case ,num_labels=snake_case ,ignore_index=snake_case ,nan_to_num=snake_case ,label_map=snake_case ,reduce_labels=snake_case ,) return iou_result
20
import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _snake_case( *SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__=True , SCREAMING_SNAKE_CASE__=2 ) -> Optional[Any]: from .. import __version__ lowercase : int = take_from lowercase : Tuple = () if not isinstance(args[0] , SCREAMING_SNAKE_CASE__ ): lowercase : Dict = (args,) for attribute, version_name, message in args: if version.parse(version.parse(SCREAMING_SNAKE_CASE__ ).base_version ) >= version.parse(SCREAMING_SNAKE_CASE__ ): raise ValueError( f"The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers'" f" version {__version__} is >= {version_name}" ) lowercase : int = None if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(SCREAMING_SNAKE_CASE__ ),) lowercase : Union[str, Any] = f"The `{attribute}` argument is deprecated and will be removed in version {version_name}." elif hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): values += (getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ),) lowercase : int = f"The `{attribute}` attribute is deprecated and will be removed in version {version_name}." elif deprecated_kwargs is None: lowercase : Dict = f"`{attribute}` is deprecated and will be removed in version {version_name}." if warning is not None: lowercase : Dict = warning + """ """ if standard_warn else """""" warnings.warn(warning + message , SCREAMING_SNAKE_CASE__ , stacklevel=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and len(SCREAMING_SNAKE_CASE__ ) > 0: lowercase : str = inspect.getouterframes(inspect.currentframe() )[1] lowercase : List[str] = call_frame.filename lowercase : Tuple = call_frame.lineno lowercase : List[str] = call_frame.function lowercase , lowercase : Optional[Any] = next(iter(deprecated_kwargs.items() ) ) raise TypeError(f"{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`" ) if len(SCREAMING_SNAKE_CASE__ ) == 0: return elif len(SCREAMING_SNAKE_CASE__ ) == 1: return values[0] return values
20
1
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowercase : Optional[int] = logging.get_logger(__name__) lowercase : str = """▁""" lowercase : List[str] = {"""vocab_file""": """sentencepiece.bpe.model"""} lowercase : str = { """vocab_file""": { """facebook/xglm-564M""": """https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model""", } } lowercase : Dict = { """facebook/xglm-564M""": 2048, } class __snake_case ( lowerCAmelCase ): _a : Optional[Any]= VOCAB_FILES_NAMES _a : str= PRETRAINED_VOCAB_FILES_MAP _a : str= PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _a : Dict= ["input_ids", "attention_mask"] def __init__( self ,snake_case ,snake_case="<s>" ,snake_case="</s>" ,snake_case="</s>" ,snake_case="<s>" ,snake_case="<unk>" ,snake_case="<pad>" ,snake_case = None ,**snake_case ,): '''simple docstring''' lowercase : int = {} if sp_model_kwargs is None else sp_model_kwargs # Compatibility with the original tokenizer lowercase : Optional[int] = 7 lowercase : Union[str, Any] = [f"<madeupword{i}>" for i in range(self.num_madeup_words )] lowercase : Optional[Any] = kwargs.get("""additional_special_tokens""" ,[] ) kwargs["additional_special_tokens"] += [ word for word in madeup_words if word not in kwargs["additional_special_tokens"] ] super().__init__( bos_token=snake_case ,eos_token=snake_case ,unk_token=snake_case ,sep_token=snake_case ,cls_token=snake_case ,pad_token=snake_case ,sp_model_kwargs=self.sp_model_kwargs ,**snake_case ,) lowercase : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(snake_case ) ) lowercase : List[str] = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab lowercase : Any = 1 # Mimic fairseq token-to-id alignment for the first 4 token lowercase : Tuple = {"""<s>""": 0, """<pad>""": 1, """</s>""": 2, """<unk>""": 3} lowercase : Optional[Any] = len(self.sp_model ) lowercase : Tuple = {f"<madeupword{i}>": sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words )} self.fairseq_tokens_to_ids.update(snake_case ) lowercase : Union[str, Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self ): '''simple docstring''' lowercase : Optional[Any] = self.__dict__.copy() lowercase : Union[str, Any] = None lowercase : List[str] = self.sp_model.serialized_model_proto() return state def __setstate__( self ,snake_case ): '''simple docstring''' lowercase : Union[str, Any] = d # for backward compatibility if not hasattr(self ,"""sp_model_kwargs""" ): lowercase : List[str] = {} lowercase : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = None ): '''simple docstring''' if token_ids_a is None: return [self.sep_token_id] + token_ids_a lowercase : Any = [self.sep_token_id] return sep + token_ids_a + sep + sep + token_ids_a def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = None ,snake_case = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=snake_case ,token_ids_a=snake_case ,already_has_special_tokens=snake_case ) if token_ids_a is None: return [1] + ([0] * len(snake_case )) return [1] + ([0] * len(snake_case )) + [1, 1] + ([0] * len(snake_case )) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = None ): '''simple docstring''' lowercase : List[str] = [self.sep_token_id] if token_ids_a is None: return len(sep + token_ids_a ) * [0] return len(sep + token_ids_a + sep + sep + token_ids_a ) * [0] @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset + self.num_madeup_words def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = {self.convert_ids_to_tokens(snake_case ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' return self.sp_model.encode(snake_case ,out_type=snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] lowercase : str = self.sp_model.PieceToId(snake_case ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Optional[int] = """""".join(snake_case ).replace(snake_case ,""" """ ).strip() return out_string def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = None ): '''simple docstring''' if not os.path.isdir(snake_case ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return lowercase : List[Any] = os.path.join( snake_case ,(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,snake_case ) elif not os.path.isfile(self.vocab_file ): with open(snake_case ,"""wb""" ) as fi: lowercase : int = self.sp_model.serialized_model_proto() fi.write(snake_case ) return (out_vocab_file,)
20
def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: if index == r: for j in range(SCREAMING_SNAKE_CASE__ ): print(data[j] , end=""" """ ) print(""" """ ) return # When no more elements are there to put in data[] if i >= n: return # current is included, put next at next location lowercase : Tuple = arr[i] combination_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , index + 1 , SCREAMING_SNAKE_CASE__ , i + 1 ) # current is excluded, replace it with # next (Note that i+1 is passed, but # index is not changed) combination_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , i + 1 ) # The main function that prints all combinations # of size r in arr[] of size n. This function # mainly uses combinationUtil() def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: # A temporary array to store all combination one by one lowercase : Optional[int] = [0] * r # Print all combination using temporary array 'data[]' combination_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , 0 , SCREAMING_SNAKE_CASE__ , 0 ) if __name__ == "__main__": # Driver code to check the function above lowercase : int = [10, 20, 30, 40, 50] print_combination(arr, len(arr), 3) # This code is contributed by Ambuj sahu
20
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 __snake_case ( lowerCAmelCase ): _a : List[str]= "openai/whisper-base" _a : Union[str, Any]= ( "This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the " "transcribed text." ) _a : List[Any]= "transcriber" _a : Optional[Any]= WhisperProcessor _a : List[Any]= WhisperForConditionalGeneration _a : int= ["audio"] _a : Tuple= ["text"] def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' return self.pre_processor(snake_case ,return_tensors="""pt""" ).input_features def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' return self.model.generate(inputs=snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' return self.pre_processor.batch_decode(snake_case ,skip_special_tokens=snake_case )[0]
20
import contextlib import os import sqlitea import pytest from datasets import Dataset, Features, Value from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) 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 @require_sqlalchemy @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: lowercase : Union[str, Any] = tmp_path / """cache""" lowercase : Optional[Any] = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowercase : Any = SqlDatasetReader( """dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ , keep_in_memory=SCREAMING_SNAKE_CASE__ ).read() _check_sql_dataset(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @require_sqlalchemy @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 _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: lowercase : Union[str, Any] = tmp_path / """cache""" lowercase : int = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} lowercase : str = features.copy() if features else default_expected_features lowercase : Optional[Any] = ( Features({feature: Value(SCREAMING_SNAKE_CASE__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowercase : Optional[int] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , features=SCREAMING_SNAKE_CASE__ , cache_dir=SCREAMING_SNAKE_CASE__ ).read() _check_sql_dataset(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[Any]: with contextlib.closing(sqlitea.connect(SCREAMING_SNAKE_CASE__ ) ) as con: lowercase : Optional[int] = con.cursor() cur.execute("""SELECT * FROM dataset""" ) for row in cur: yield row @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> int: lowercase : Any = tmp_path / """cache""" lowercase : int = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : List[str] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=1 ).write() lowercase : List[str] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = iter_sql_file(SCREAMING_SNAKE_CASE__ ) for rowa, rowa in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): assert rowa == rowa @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: lowercase : Dict = tmp_path / """cache""" lowercase : List[str] = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : List[str] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=2 ).write() lowercase : Optional[int] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) for rowa, rowa in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): assert rowa == rowa @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : str = tmp_path / """cache""" lowercase : Optional[int] = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : Optional[Any] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() with pytest.raises(SCREAMING_SNAKE_CASE__ ): SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=0 ).write()
20
1
import json import os from dataclasses import dataclass from functools import partial from typing import Callable import flax.linen as nn import jax import jax.numpy as jnp import joblib import optax import wandb from flax import jax_utils, struct, traverse_util from flax.serialization import from_bytes, to_bytes from flax.training import train_state from flax.training.common_utils import shard from tqdm.auto import tqdm from transformers import BigBirdConfig, FlaxBigBirdForQuestionAnswering from transformers.models.big_bird.modeling_flax_big_bird import FlaxBigBirdForQuestionAnsweringModule class __snake_case ( lowerCAmelCase ): _a : BigBirdConfig _a : jnp.dtype= jnp.floataa _a : bool= True def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' super().setup() lowercase : List[str] = nn.Dense(5 ,dtype=self.dtype ) def __call__( self ,*snake_case ,**snake_case ): '''simple docstring''' lowercase : int = super().__call__(*snake_case ,**snake_case ) lowercase : Any = self.cls(outputs[2] ) return outputs[:2] + (cls_out,) class __snake_case ( lowerCAmelCase ): _a : List[Any]= FlaxBigBirdForNaturalQuestionsModule def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: def cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None ): lowercase : int = logits.shape[-1] lowercase : Dict = (labels[..., None] == jnp.arange(SCREAMING_SNAKE_CASE__ )[None]).astype("""f4""" ) lowercase : Any = jax.nn.log_softmax(SCREAMING_SNAKE_CASE__ , axis=-1 ) lowercase : Optional[Any] = -jnp.sum(labels * logits , axis=-1 ) if reduction is not None: lowercase : Any = reduction(SCREAMING_SNAKE_CASE__ ) return loss lowercase : Optional[Any] = partial(SCREAMING_SNAKE_CASE__ , reduction=jnp.mean ) lowercase : Optional[int] = cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Dict = cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : int = cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return (start_loss + end_loss + pooled_loss) / 3 @dataclass class __snake_case : _a : str= "google/bigbird-roberta-base" _a : int= 3000 _a : int= 1_0500 _a : int= 128 _a : int= 3 _a : int= 1 _a : int= 5 # tx_args _a : float= 3E-5 _a : float= 0.0 _a : int= 2_0000 _a : float= 0.00_95 _a : str= "bigbird-roberta-natural-questions" _a : str= "training-expt" _a : str= "data/nq-training.jsonl" _a : str= "data/nq-validation.jsonl" def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' os.makedirs(self.base_dir ,exist_ok=snake_case ) lowercase : Optional[int] = os.path.join(self.base_dir ,self.save_dir ) lowercase : Optional[int] = self.batch_size_per_device * jax.device_count() @dataclass class __snake_case : _a : int _a : int= 4096 # no dynamic padding on TPUs def __call__( self ,snake_case ): '''simple docstring''' lowercase : int = self.collate_fn(snake_case ) lowercase : Union[str, Any] = jax.tree_util.tree_map(snake_case ,snake_case ) return batch def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase , lowercase : Union[str, Any] = self.fetch_inputs(features["""input_ids"""] ) lowercase : Tuple = { """input_ids""": jnp.array(snake_case ,dtype=jnp.intaa ), """attention_mask""": jnp.array(snake_case ,dtype=jnp.intaa ), """start_labels""": jnp.array(features["""start_token"""] ,dtype=jnp.intaa ), """end_labels""": jnp.array(features["""end_token"""] ,dtype=jnp.intaa ), """pooled_labels""": jnp.array(features["""category"""] ,dtype=jnp.intaa ), } return batch def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Tuple = [self._fetch_inputs(snake_case ) for ids in input_ids] return zip(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Union[str, Any] = [1 for _ in range(len(snake_case ) )] while len(snake_case ) < self.max_length: input_ids.append(self.pad_id ) attention_mask.append(0 ) return input_ids, attention_mask def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None ) -> Any: if seed is not None: lowercase : Optional[int] = dataset.shuffle(seed=SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) // batch_size ): lowercase : Optional[Any] = dataset[i * batch_size : (i + 1) * batch_size] yield dict(SCREAMING_SNAKE_CASE__ ) @partial(jax.pmap , axis_name="""batch""" ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) -> List[Any]: def loss_fn(SCREAMING_SNAKE_CASE__ ): lowercase : List[str] = model_inputs.pop("""start_labels""" ) lowercase : Optional[int] = model_inputs.pop("""end_labels""" ) lowercase : str = model_inputs.pop("""pooled_labels""" ) lowercase : Union[str, Any] = state.apply_fn(**SCREAMING_SNAKE_CASE__ , params=SCREAMING_SNAKE_CASE__ , dropout_rng=SCREAMING_SNAKE_CASE__ , train=SCREAMING_SNAKE_CASE__ ) lowercase , lowercase , lowercase : List[str] = outputs return state.loss_fn( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) lowercase , lowercase : int = jax.random.split(SCREAMING_SNAKE_CASE__ ) lowercase : Dict = jax.value_and_grad(SCREAMING_SNAKE_CASE__ ) lowercase , lowercase : Union[str, Any] = grad_fn(state.params ) lowercase : List[Any] = jax.lax.pmean({"""loss""": loss} , axis_name="""batch""" ) lowercase : List[Any] = jax.lax.pmean(SCREAMING_SNAKE_CASE__ , """batch""" ) lowercase : str = state.apply_gradients(grads=SCREAMING_SNAKE_CASE__ ) return state, metrics, new_drp_rng @partial(jax.pmap , axis_name="""batch""" ) def _snake_case( SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : int = model_inputs.pop("""start_labels""" ) lowercase : Dict = model_inputs.pop("""end_labels""" ) lowercase : Optional[Any] = model_inputs.pop("""pooled_labels""" ) lowercase : Optional[int] = state.apply_fn(**SCREAMING_SNAKE_CASE__ , params=state.params , train=SCREAMING_SNAKE_CASE__ ) lowercase , lowercase , lowercase : List[Any] = outputs lowercase : Dict = state.loss_fn(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : str = jax.lax.pmean({"""loss""": loss} , axis_name="""batch""" ) return metrics class __snake_case ( train_state.TrainState ): _a : Callable= struct.field(pytree_node=lowerCAmelCase ) @dataclass class __snake_case : _a : Args _a : Callable _a : Callable _a : Callable _a : Callable _a : wandb _a : Callable= None def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case=None ): '''simple docstring''' lowercase : Tuple = model.params lowercase : Any = TrainState.create( apply_fn=model.__call__ ,params=snake_case ,tx=snake_case ,loss_fn=snake_case ,) if ckpt_dir is not None: lowercase , lowercase , lowercase , lowercase , lowercase : Tuple = restore_checkpoint(snake_case ,snake_case ) lowercase : List[str] = { """lr""": args.lr, """init_lr""": args.init_lr, """warmup_steps""": args.warmup_steps, """num_train_steps""": num_train_steps, """weight_decay""": args.weight_decay, } lowercase , lowercase : Tuple = build_tx(**snake_case ) lowercase : str = train_state.TrainState( step=snake_case ,apply_fn=model.__call__ ,params=snake_case ,tx=snake_case ,opt_state=snake_case ,) lowercase : Any = args lowercase : Optional[Any] = data_collator lowercase : List[str] = lr lowercase : str = params lowercase : Tuple = jax_utils.replicate(snake_case ) return state def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ): '''simple docstring''' lowercase : Dict = self.args lowercase : Optional[Any] = len(snake_case ) // args.batch_size lowercase : int = jax.random.PRNGKey(0 ) lowercase : List[str] = jax.random.split(snake_case ,jax.device_count() ) for epoch in range(args.max_epochs ): lowercase : List[Any] = jnp.array(0 ,dtype=jnp.floataa ) lowercase : List[str] = get_batched_dataset(snake_case ,args.batch_size ,seed=snake_case ) lowercase : int = 0 for batch in tqdm(snake_case ,total=snake_case ,desc=f"Running EPOCH-{epoch}" ): lowercase : Dict = self.data_collator(snake_case ) lowercase , lowercase , lowercase : Optional[int] = self.train_step_fn(snake_case ,snake_case ,**snake_case ) running_loss += jax_utils.unreplicate(metrics["""loss"""] ) i += 1 if i % args.logging_steps == 0: lowercase : Optional[Any] = jax_utils.unreplicate(state.step ) lowercase : List[str] = running_loss.item() / i lowercase : List[str] = self.scheduler_fn(state_step - 1 ) lowercase : int = self.evaluate(snake_case ,snake_case ) lowercase : Tuple = { """step""": state_step.item(), """eval_loss""": eval_loss.item(), """tr_loss""": tr_loss, """lr""": lr.item(), } tqdm.write(str(snake_case ) ) self.logger.log(snake_case ,commit=snake_case ) if i % args.save_steps == 0: self.save_checkpoint(args.save_dir + f"-e{epoch}-s{i}" ,state=snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : List[str] = get_batched_dataset(snake_case ,self.args.batch_size ) lowercase : Any = len(snake_case ) // self.args.batch_size lowercase : List[Any] = jnp.array(0 ,dtype=jnp.floataa ) lowercase : Optional[int] = 0 for batch in tqdm(snake_case ,total=snake_case ,desc="""Evaluating ... """ ): lowercase : Tuple = self.data_collator(snake_case ) lowercase : Optional[int] = self.val_step_fn(snake_case ,**snake_case ) running_loss += jax_utils.unreplicate(metrics["""loss"""] ) i += 1 return running_loss / i def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : str = jax_utils.unreplicate(snake_case ) print(f"SAVING CHECKPOINT IN {save_dir}" ,end=""" ... """ ) self.model_save_fn(snake_case ,params=state.params ) with open(os.path.join(snake_case ,"""opt_state.msgpack""" ) ,"""wb""" ) as f: f.write(to_bytes(state.opt_state ) ) joblib.dump(self.args ,os.path.join(snake_case ,"""args.joblib""" ) ) joblib.dump(self.data_collator ,os.path.join(snake_case ,"""data_collator.joblib""" ) ) with open(os.path.join(snake_case ,"""training_state.json""" ) ,"""w""" ) as f: json.dump({"""step""": state.step.item()} ,snake_case ) print("""DONE""" ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: print(f"RESTORING CHECKPOINT FROM {save_dir}" , end=""" ... """ ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , """flax_model.msgpack""" ) , """rb""" ) as f: lowercase : str = from_bytes(state.params , f.read() ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , """opt_state.msgpack""" ) , """rb""" ) as f: lowercase : Optional[int] = from_bytes(state.opt_state , f.read() ) lowercase : Optional[Any] = joblib.load(os.path.join(SCREAMING_SNAKE_CASE__ , """args.joblib""" ) ) lowercase : int = joblib.load(os.path.join(SCREAMING_SNAKE_CASE__ , """data_collator.joblib""" ) ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , """training_state.json""" ) , """r""" ) as f: lowercase : Tuple = json.load(SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = training_state["""step"""] print("""DONE""" ) return params, opt_state, step, args, data_collator def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: lowercase : List[str] = num_train_steps - warmup_steps lowercase : Dict = optax.linear_schedule(init_value=SCREAMING_SNAKE_CASE__ , end_value=SCREAMING_SNAKE_CASE__ , transition_steps=SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = optax.linear_schedule(init_value=SCREAMING_SNAKE_CASE__ , end_value=1e-7 , transition_steps=SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = optax.join_schedules(schedules=[warmup_fn, decay_fn] , boundaries=[warmup_steps] ) return lr def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: def weight_decay_mask(SCREAMING_SNAKE_CASE__ ): lowercase : List[Any] = traverse_util.flatten_dict(SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = {k: (v[-1] != """bias""" and v[-2:] != ("""LayerNorm""", """scale""")) for k, v in params.items()} return traverse_util.unflatten_dict(SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = scheduler_fn(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = optax.adamw(learning_rate=SCREAMING_SNAKE_CASE__ , weight_decay=SCREAMING_SNAKE_CASE__ , mask=SCREAMING_SNAKE_CASE__ ) return tx, lr
20
import os import numpy import onnx def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: lowercase : int = a.name lowercase : Any = b.name lowercase : Optional[Any] = """""" lowercase : Dict = """""" lowercase : int = a == b lowercase : int = name_a lowercase : List[str] = name_b return res def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: for i, input_name in enumerate(node_proto.input ): if input_name == name: node_proto.input.insert(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) node_proto.input.pop(i + 1 ) if node_proto.op_type == "If": _graph_replace_input_with(node_proto.attribute[0].g , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) _graph_replace_input_with(node_proto.attribute[1].g , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if node_proto.op_type == "Loop": _graph_replace_input_with(node_proto.attribute[0].g , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: for n in graph_proto.node: _node_replace_input_with(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: lowercase : Any = list(model.graph.initializer ) lowercase : Dict = list(model_without_ext.graph.initializer ) for i, ref_i in ind_to_replace: assert inits_with_data[i].name == inits[i].name assert inits_with_data[ref_i].name == inits[ref_i].name assert i > ref_i lowercase : Union[str, Any] = inits[i].name lowercase : Dict = inits[ref_i].name model_without_ext.graph.initializer.remove(inits[i] ) # for n in model.graph.node: _graph_replace_input_with(model_without_ext.graph , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : Union[str, Any] = os.path.dirname(SCREAMING_SNAKE_CASE__ ) lowercase : Dict = os.path.basename(SCREAMING_SNAKE_CASE__ ) lowercase : str = onnx.load(os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) lowercase : List[str] = list(model.graph.initializer ) lowercase : Tuple = set() lowercase : int = {} lowercase : Optional[Any] = [] lowercase : Dict = 0 for i in range(len(SCREAMING_SNAKE_CASE__ ) ): if i in dup_set: continue for j in range(i + 1 , len(SCREAMING_SNAKE_CASE__ ) ): if j in dup_set: continue if _is_equal_tensor_proto(inits[i] , inits[j] ): dup_set.add(SCREAMING_SNAKE_CASE__ ) dup_set.add(SCREAMING_SNAKE_CASE__ ) lowercase : int = inits[j].data_type lowercase : Optional[int] = numpy.prod(inits[j].dims ) if dtype == 1: mem_size *= 4 elif dtype == 6: mem_size *= 4 elif dtype == 7 or dtype == 11: mem_size *= 8 else: print("""unexpected data type: """ , SCREAMING_SNAKE_CASE__ ) total_reduced_size += mem_size lowercase : Tuple = inits[i].name lowercase : int = inits[j].name if name_i in dup_map: dup_map[name_i].append(SCREAMING_SNAKE_CASE__ ) else: lowercase : List[str] = [name_j] ind_to_replace.append((j, i) ) print("""total reduced size: """ , total_reduced_size / 1_024 / 1_024 / 1_024 , """GB""" ) lowercase : str = sorted(SCREAMING_SNAKE_CASE__ ) _remove_dup_initializers_from_model(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = """optimized_""" + model_file_name lowercase : Dict = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) onnx.save(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return new_model
20
1
import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline 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_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __snake_case ( lowerCAmelCase , lowerCAmelCase , unittest.TestCase ): _a : Union[str, Any]= IFInpaintingSuperResolutionPipeline _a : Tuple= TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} _a : str= TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"original_image"} ) _a : Tuple= PipelineTesterMixin.required_optional_params - {"latents"} def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return self._get_superresolution_dummy_components() def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=0 ): '''simple docstring''' if str(snake_case ).startswith("""mps""" ): lowercase : int = torch.manual_seed(snake_case ) else: lowercase : List[str] = torch.Generator(device=snake_case ).manual_seed(snake_case ) lowercase : str = floats_tensor((1, 3, 16, 16) ,rng=random.Random(snake_case ) ).to(snake_case ) lowercase : Union[str, Any] = floats_tensor((1, 3, 32, 32) ,rng=random.Random(snake_case ) ).to(snake_case ) lowercase : str = floats_tensor((1, 3, 32, 32) ,rng=random.Random(snake_case ) ).to(snake_case ) lowercase : int = { """prompt""": """A painting of a squirrel eating a burger""", """image""": image, """original_image""": original_image, """mask_image""": mask_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 _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != """cuda""" ,reason="""float16 requires CUDA""" ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1e-1 ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' self._test_save_load_local() def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1e-2 ,)
20
import argparse import json from collections import OrderedDict import torch from huggingface_hub import cached_download, hf_hub_url from transformers import AutoImageProcessor, CvtConfig, CvtForImageClassification def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Tuple: lowercase : Union[str, Any] = [] embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.weight", f"stage{idx}.patch_embed.proj.weight", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.bias", f"stage{idx}.patch_embed.proj.bias", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.weight", f"stage{idx}.patch_embed.norm.weight", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.bias", f"stage{idx}.patch_embed.norm.bias", ) ) return embed def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: lowercase : Optional[Any] = [] attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.weight", f"stage{idx}.blocks.{cnt}.attn.proj_q.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.bias", f"stage{idx}.blocks.{cnt}.attn.proj_q.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.weight", f"stage{idx}.blocks.{cnt}.attn.proj_k.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.bias", f"stage{idx}.blocks.{cnt}.attn.proj_k.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.weight", f"stage{idx}.blocks.{cnt}.attn.proj_v.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.bias", f"stage{idx}.blocks.{cnt}.attn.proj_v.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.weight", f"stage{idx}.blocks.{cnt}.attn.proj.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.bias", f"stage{idx}.blocks.{cnt}.attn.proj.bias", ) ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.weight", f"stage{idx}.blocks.{cnt}.mlp.fc1.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.bias", f"stage{idx}.blocks.{cnt}.mlp.fc1.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.weight", f"stage{idx}.blocks.{cnt}.mlp.fc2.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.bias", f"stage{idx}.blocks.{cnt}.mlp.fc2.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.weight", f"stage{idx}.blocks.{cnt}.norm1.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.bias", f"stage{idx}.blocks.{cnt}.norm1.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.weight", f"stage{idx}.blocks.{cnt}.norm2.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.bias", f"stage{idx}.blocks.{cnt}.norm2.bias") ) return attention_weights def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: lowercase : Optional[Any] = [] token.append((f"cvt.encoder.stages.{idx}.cls_token", """stage2.cls_token""") ) return token def _snake_case( ) -> Dict: lowercase : Optional[Any] = [] head.append(("""layernorm.weight""", """norm.weight""") ) head.append(("""layernorm.bias""", """norm.bias""") ) head.append(("""classifier.weight""", """head.weight""") ) head.append(("""classifier.bias""", """head.bias""") ) return head def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: lowercase : Any = """imagenet-1k-id2label.json""" lowercase : List[str] = 1_000 lowercase : int = """huggingface/label-files""" lowercase : Union[str, Any] = num_labels lowercase : Optional[Any] = json.load(open(cached_download(hf_hub_url(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , repo_type="""dataset""" ) ) , """r""" ) ) lowercase : List[Any] = {int(SCREAMING_SNAKE_CASE__ ): v for k, v in idalabel.items()} lowercase : Dict = idalabel lowercase : List[str] = {v: k for k, v in idalabel.items()} lowercase : List[str] = CvtConfig(num_labels=SCREAMING_SNAKE_CASE__ , idalabel=SCREAMING_SNAKE_CASE__ , labelaid=SCREAMING_SNAKE_CASE__ ) # For depth size 13 (13 = 1+2+10) if cvt_model.rsplit("""/""" , 1 )[-1][4:6] == "13": lowercase : Tuple = [1, 2, 10] # For depth size 21 (21 = 1+4+16) elif cvt_model.rsplit("""/""" , 1 )[-1][4:6] == "21": lowercase : Dict = [1, 4, 16] # For wide cvt (similar to wide-resnet) depth size 24 (w24 = 2 + 2 20) else: lowercase : int = [2, 2, 20] lowercase : Optional[int] = [3, 12, 16] lowercase : str = [192, 768, 1_024] lowercase : Union[str, Any] = CvtForImageClassification(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = AutoImageProcessor.from_pretrained("""facebook/convnext-base-224-22k-1k""" ) lowercase : Optional[Any] = image_size lowercase : Union[str, Any] = torch.load(SCREAMING_SNAKE_CASE__ , map_location=torch.device("""cpu""" ) ) lowercase : Optional[Any] = OrderedDict() lowercase : Tuple = [] for idx in range(len(config.depth ) ): if config.cls_token[idx]: lowercase : Optional[Any] = list_of_state_dict + cls_token(SCREAMING_SNAKE_CASE__ ) lowercase : str = list_of_state_dict + embeddings(SCREAMING_SNAKE_CASE__ ) for cnt in range(config.depth[idx] ): lowercase : List[str] = list_of_state_dict + attention(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = list_of_state_dict + final() for gg in list_of_state_dict: print(SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) ): lowercase : Optional[Any] = original_weights[list_of_state_dict[i][1]] model.load_state_dict(SCREAMING_SNAKE_CASE__ ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) # Download the weights from zoo: https://1drv.ms/u/s!AhIXJn_J-blW9RzF3rMW7SsLHa8h?e=blQ0Al if __name__ == "__main__": lowercase : Tuple = argparse.ArgumentParser() parser.add_argument( """--cvt_model""", default="""cvt-w24""", type=str, help="""Name of the cvt model you'd like to convert.""", ) parser.add_argument( """--image_size""", default=384, type=int, help="""Input Image Size""", ) parser.add_argument( """--cvt_file_name""", default=R"""cvtmodels\CvT-w24-384x384-IN-22k.pth""", type=str, help="""Input Image Size""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) lowercase : Optional[int] = parser.parse_args() convert_cvt_checkpoint(args.cvt_model, args.image_size, args.cvt_file_name, args.pytorch_dump_folder_path)
20
1
from arguments import InitializationArguments from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer, HfArgumentParser # Configuration lowercase : str = HfArgumentParser(InitializationArguments) lowercase : int = parser.parse_args() # Load codeparrot tokenizer trained for Python code tokenization lowercase : Optional[int] = AutoTokenizer.from_pretrained(args.tokenizer_name) # Config: "scale_attn_by_layer_idx" and "reorder_and_upcast_attn" are Mistral stability tweaks lowercase : Optional[Any] = { """vocab_size""": len(tokenizer), """scale_attn_by_inverse_layer_idx""": True, """reorder_and_upcast_attn""": True, } # Load model config (GPT-2 large in this case) lowercase : str = AutoConfig.from_pretrained(args.config_name, **config_kwargs) # Initialize new model with config lowercase : Union[str, Any] = AutoModelForCausalLM.from_config(config) # Save model to the hub model.save_pretrained(args.model_name, push_to_hub=args.push_to_hub)
20
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor from ..utils import is_datasets_available from .base import PipelineTool if is_datasets_available(): from datasets import load_dataset class __snake_case ( lowerCAmelCase ): _a : Union[str, Any]= "microsoft/speecht5_tts" _a : Tuple= ( "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the " "text to read (in English) and returns a waveform object containing the sound." ) _a : Dict= "text_reader" _a : Optional[Any]= SpeechTaProcessor _a : Tuple= SpeechTaForTextToSpeech _a : Optional[int]= SpeechTaHifiGan _a : Union[str, Any]= ["text"] _a : Optional[int]= ["audio"] def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self.post_processor is None: lowercase : Any = """microsoft/speecht5_hifigan""" super().setup() def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ): '''simple docstring''' lowercase : int = self.pre_processor(text=snake_case ,return_tensors="""pt""" ,truncation=snake_case ) if speaker_embeddings is None: if not is_datasets_available(): raise ImportError("""Datasets needs to be installed if not passing speaker embeddings.""" ) lowercase : Tuple = load_dataset("""Matthijs/cmu-arctic-xvectors""" ,split="""validation""" ) lowercase : List[str] = torch.tensor(embeddings_dataset[7305]["""xvector"""] ).unsqueeze(0 ) return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings} def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' with torch.no_grad(): return self.model.generate_speech(**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' with torch.no_grad(): return self.post_processor(snake_case ).cpu().detach()
20
1
import mpmath # for roots of unity import numpy as np class __snake_case : def __init__( self ,snake_case=None ,snake_case=None ): '''simple docstring''' lowercase : List[str] = list(poly_a or [0] )[:] lowercase : List[str] = list(poly_b or [0] )[:] # Remove leading zero coefficients while self.polyA[-1] == 0: self.polyA.pop() lowercase : Any = len(self.polyA ) while self.polyB[-1] == 0: self.polyB.pop() lowercase : List[str] = len(self.polyB ) # Add 0 to make lengths equal a power of 2 lowercase : str = int( 2 ** np.ceil(np.loga(len(self.polyA ) + len(self.polyB ) - 1 ) ) ) while len(self.polyA ) < self.c_max_length: self.polyA.append(0 ) while len(self.polyB ) < self.c_max_length: self.polyB.append(0 ) # A complex root used for the fourier transform lowercase : Union[str, Any] = complex(mpmath.root(x=1 ,n=self.c_max_length ,k=1 ) ) # The product lowercase : Any = self.__multiply() def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Union[str, Any] = [[x] for x in self.polyA] if which == """A""" else [[x] for x in self.polyB] # Corner case if len(snake_case ) <= 1: return dft[0] # lowercase : Optional[Any] = self.c_max_length // 2 while next_ncol > 0: lowercase : Tuple = [[] for i in range(snake_case )] lowercase : int = self.root**next_ncol # First half of next step lowercase : Tuple = 1 for j in range(self.c_max_length // (next_ncol * 2) ): for i in range(snake_case ): new_dft[i].append(dft[i][j] + current_root * dft[i + next_ncol][j] ) current_root *= root # Second half of next step lowercase : Any = 1 for j in range(self.c_max_length // (next_ncol * 2) ): for i in range(snake_case ): new_dft[i].append(dft[i][j] - current_root * dft[i + next_ncol][j] ) current_root *= root # Update lowercase : Dict = new_dft lowercase : Optional[int] = next_ncol // 2 return dft[0] def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[Any] = self.__dft("""A""" ) lowercase : List[str] = self.__dft("""B""" ) lowercase : Optional[int] = [[dft_a[i] * dft_b[i] for i in range(self.c_max_length )]] del dft_a del dft_b # Corner Case if len(inverce_c[0] ) <= 1: return inverce_c[0] # Inverse DFT lowercase : Dict = 2 while next_ncol <= self.c_max_length: lowercase : Any = [[] for i in range(snake_case )] lowercase : Optional[Any] = self.root ** (next_ncol // 2) lowercase : Optional[int] = 1 # First half of next step for j in range(self.c_max_length // next_ncol ): for i in range(next_ncol // 2 ): # Even positions new_inverse_c[i].append( ( inverce_c[i][j] + inverce_c[i][j + self.c_max_length // next_ncol] ) / 2 ) # Odd positions new_inverse_c[i + next_ncol // 2].append( ( inverce_c[i][j] - inverce_c[i][j + self.c_max_length // next_ncol] ) / (2 * current_root) ) current_root *= root # Update lowercase : str = new_inverse_c next_ncol *= 2 # Unpack lowercase : List[str] = [round(x[0].real ,8 ) + round(x[0].imag ,8 ) * 1j for x in inverce_c] # Remove leading 0's while inverce_c[-1] == 0: inverce_c.pop() return inverce_c def __str__( self ): '''simple docstring''' lowercase : Optional[int] = """A = """ + """ + """.join( f"{coef}*x^{i}" for coef, i in enumerate(self.polyA[: self.len_A] ) ) lowercase : List[Any] = """B = """ + """ + """.join( f"{coef}*x^{i}" for coef, i in enumerate(self.polyB[: self.len_B] ) ) lowercase : Optional[Any] = """A*B = """ + """ + """.join( f"{coef}*x^{i}" for coef, i in enumerate(self.product ) ) return f"{a}\n{b}\n{c}" # Unit tests if __name__ == "__main__": import doctest doctest.testmod()
20
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase : str = { """configuration_funnel""": ["""FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FunnelConfig"""], """convert_funnel_original_tf_checkpoint_to_pytorch""": [], """tokenization_funnel""": ["""FunnelTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Tuple = ["""FunnelTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : List[str] = [ """FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""", """FunnelBaseModel""", """FunnelForMaskedLM""", """FunnelForMultipleChoice""", """FunnelForPreTraining""", """FunnelForQuestionAnswering""", """FunnelForSequenceClassification""", """FunnelForTokenClassification""", """FunnelModel""", """FunnelPreTrainedModel""", """load_tf_weights_in_funnel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Optional[Any] = [ """TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFFunnelBaseModel""", """TFFunnelForMaskedLM""", """TFFunnelForMultipleChoice""", """TFFunnelForPreTraining""", """TFFunnelForQuestionAnswering""", """TFFunnelForSequenceClassification""", """TFFunnelForTokenClassification""", """TFFunnelModel""", """TFFunnelPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig from .tokenization_funnel import FunnelTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_funnel_fast import FunnelTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_funnel import ( FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, FunnelBaseModel, FunnelForMaskedLM, FunnelForMultipleChoice, FunnelForPreTraining, FunnelForQuestionAnswering, FunnelForSequenceClassification, FunnelForTokenClassification, FunnelModel, FunnelPreTrainedModel, load_tf_weights_in_funnel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_funnel import ( TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, TFFunnelPreTrainedModel, ) else: import sys lowercase : Any = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
20
1
import random from typing import Any def _snake_case( SCREAMING_SNAKE_CASE__ ) -> list[Any]: for _ in range(len(SCREAMING_SNAKE_CASE__ ) ): lowercase : List[Any] = random.randint(0 , len(SCREAMING_SNAKE_CASE__ ) - 1 ) lowercase : Any = random.randint(0 , len(SCREAMING_SNAKE_CASE__ ) - 1 ) lowercase , lowercase : int = data[b], data[a] return data if __name__ == "__main__": lowercase : str = [0, 1, 2, 3, 4, 5, 6, 7] lowercase : List[str] = ["""python""", """says""", """hello""", """!"""] print("""Fisher-Yates Shuffle:""") print("""List""", integers, strings) print("""FY Shuffle""", fisher_yates_shuffle(integers), fisher_yates_shuffle(strings))
20
import os import zipfile import pytest from datasets.utils.extract import ( BzipaExtractor, Extractor, GzipExtractor, LzaExtractor, SevenZipExtractor, TarExtractor, XzExtractor, ZipExtractor, ZstdExtractor, ) from .utils import require_lza, require_pyazr, require_zstandard @pytest.mark.parametrize( """compression_format, is_archive""" , [ ("""7z""", True), ("""bz2""", False), ("""gzip""", False), ("""lz4""", False), ("""tar""", True), ("""xz""", False), ("""zip""", True), ("""zstd""", False), ] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) -> Any: lowercase : Dict = { """7z""": (seven_zip_file, SevenZipExtractor), """bz2""": (bza_file, BzipaExtractor), """gzip""": (gz_file, GzipExtractor), """lz4""": (lza_file, LzaExtractor), """tar""": (tar_file, TarExtractor), """xz""": (xz_file, XzExtractor), """zip""": (zip_file, ZipExtractor), """zstd""": (zstd_file, ZstdExtractor), } lowercase , lowercase : Optional[Any] = input_paths_and_base_extractors[compression_format] if input_path is None: lowercase : Dict = f"for '{compression_format}' compression_format, " if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(SCREAMING_SNAKE_CASE__ ) assert base_extractor.is_extractable(SCREAMING_SNAKE_CASE__ ) lowercase : Any = tmp_path / ("""extracted""" if is_archive else """extracted.txt""") base_extractor.extract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name lowercase : str = file_path.read_text(encoding="""utf-8""" ) else: lowercase : Optional[Any] = output_path.read_text(encoding="""utf-8""" ) lowercase : Tuple = text_file.read_text(encoding="""utf-8""" ) assert extracted_file_content == expected_file_content @pytest.mark.parametrize( """compression_format, is_archive""" , [ ("""7z""", True), ("""bz2""", False), ("""gzip""", False), ("""lz4""", False), ("""tar""", True), ("""xz""", False), ("""zip""", True), ("""zstd""", False), ] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) -> Dict: lowercase : str = { """7z""": seven_zip_file, """bz2""": bza_file, """gzip""": gz_file, """lz4""": lza_file, """tar""": tar_file, """xz""": xz_file, """zip""": zip_file, """zstd""": zstd_file, } lowercase : Optional[Any] = input_paths[compression_format] if input_path is None: lowercase : int = f"for '{compression_format}' compression_format, " if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = Extractor.infer_extractor_format(SCREAMING_SNAKE_CASE__ ) assert extractor_format is not None lowercase : Any = tmp_path / ("""extracted""" if is_archive else """extracted.txt""") Extractor.extract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name lowercase : Dict = file_path.read_text(encoding="""utf-8""" ) else: lowercase : int = output_path.read_text(encoding="""utf-8""" ) lowercase : Optional[Any] = text_file.read_text(encoding="""utf-8""" ) assert extracted_file_content == expected_file_content @pytest.fixture def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: import tarfile lowercase : Tuple = tmp_path / """data_dot_dot""" directory.mkdir() lowercase : str = directory / """tar_file_with_dot_dot.tar""" with tarfile.TarFile(SCREAMING_SNAKE_CASE__ , """w""" ) as f: f.add(SCREAMING_SNAKE_CASE__ , arcname=os.path.join("""..""" , text_file.name ) ) return path @pytest.fixture def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[str]: import tarfile lowercase : Tuple = tmp_path / """data_sym_link""" directory.mkdir() lowercase : int = directory / """tar_file_with_sym_link.tar""" os.symlink("""..""" , directory / """subdir""" , target_is_directory=SCREAMING_SNAKE_CASE__ ) with tarfile.TarFile(SCREAMING_SNAKE_CASE__ , """w""" ) as f: f.add(str(directory / """subdir""" ) , arcname="""subdir""" ) # str required by os.readlink on Windows and Python < 3.8 return path @pytest.mark.parametrize( """insecure_tar_file, error_log""" , [("""tar_file_with_dot_dot""", """illegal path"""), ("""tar_file_with_sym_link""", """Symlink""")] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: lowercase : List[Any] = { """tar_file_with_dot_dot""": tar_file_with_dot_dot, """tar_file_with_sym_link""": tar_file_with_sym_link, } lowercase : Optional[int] = insecure_tar_files[insecure_tar_file] lowercase : List[str] = tmp_path / """extracted""" TarExtractor.extract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) assert caplog.text for record in caplog.records: assert record.levelname == "ERROR" assert error_log in record.msg def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[int]: # We should have less false positives than zipfile.is_zipfile # We do that by checking only the magic number lowercase : Any = tmpdir / """not_a_zip_file""" # From: https://github.com/python/cpython/pull/5053 lowercase : str = ( B"""\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00""" B"""\x00\x02\x08\x06\x00\x00\x00\x99\x81\xb6'\x00\x00\x00\x15I""" B"""DATx\x01\x01\n\x00\xf5\xff\x00PK\x05\x06\x00PK\x06\x06\x07""" B"""\xac\x01N\xc6|a\r\x00\x00\x00\x00IEND\xaeB`\x82""" ) with not_a_zip_file.open("""wb""" ) as f: f.write(SCREAMING_SNAKE_CASE__ ) assert zipfile.is_zipfile(str(SCREAMING_SNAKE_CASE__ ) ) # is a false positive for `zipfile` assert not ZipExtractor.is_extractable(SCREAMING_SNAKE_CASE__ ) # but we're right
20
1
import unittest from pathlib import Path from tempfile import TemporaryDirectory from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available from transformers.models.bert.tokenization_bert import BertTokenizer from transformers.testing_utils import require_tensorflow_text, require_tf, slow if is_tf_available(): import tensorflow as tf if is_tensorflow_text_available(): from transformers.models.bert import TFBertTokenizer lowercase : Optional[Any] = ["""bert-base-uncased""", """bert-base-cased"""] lowercase : Union[str, Any] = """hf-internal-testing/tiny-bert-tf-only""" if is_tf_available(): class __snake_case ( tf.keras.Model ): def __init__( self ,snake_case ): '''simple docstring''' super().__init__() lowercase : Tuple = tokenizer lowercase : Any = AutoConfig.from_pretrained(snake_case ) lowercase : Optional[int] = TFAutoModel.from_config(snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Any = self.tokenizer(snake_case ) lowercase : Any = self.bert(**snake_case ) return out["pooler_output"] @require_tf @require_tensorflow_text class __snake_case ( unittest.TestCase ): def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' super().setUp() lowercase : Any = [ BertTokenizer.from_pretrained(snake_case ) for checkpoint in (TOKENIZER_CHECKPOINTS * 2) ] # repeat for when fast_bert_tokenizer=false lowercase : str = [TFBertTokenizer.from_pretrained(snake_case ) for checkpoint in TOKENIZER_CHECKPOINTS] + [ TFBertTokenizer.from_pretrained(snake_case ,use_fast_bert_tokenizer=snake_case ) for checkpoint in TOKENIZER_CHECKPOINTS ] assert len(self.tokenizers ) == len(self.tf_tokenizers ) lowercase : str = [ """This is a straightforward English test sentence.""", """This one has some weird characters\rto\nsee\r\nif those\u00E9break things.""", """Now we're going to add some Chinese: 一 二 三 一二三""", """And some much more rare Chinese: 齉 堃 齉堃""", """Je vais aussi écrire en français pour tester les accents""", """Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ""", ] lowercase : Any = list(zip(self.test_sentences ,self.test_sentences[::-1] ) ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' for tokenizer, tf_tokenizer in zip(self.tokenizers ,self.tf_tokenizers ): for test_inputs in (self.test_sentences, self.paired_sentences): lowercase : str = tokenizer(snake_case ,return_tensors="""tf""" ,padding="""longest""" ) lowercase : Union[str, Any] = tf_tokenizer(snake_case ) for key in python_outputs.keys(): self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape ) ) self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key] ,tf.intaa ) == tf_outputs[key] ) ) @slow def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' for tf_tokenizer in self.tf_tokenizers: lowercase : Optional[Any] = tf_tokenizer(self.paired_sentences ) lowercase : str = tf_tokenizer( text=[sentence[0] for sentence in self.paired_sentences] ,text_pair=[sentence[1] for sentence in self.paired_sentences] ,) for key in merged_outputs.keys(): self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key] ,tf.intaa ) == separated_outputs[key] ) ) @slow def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' for tf_tokenizer in self.tf_tokenizers: lowercase : Any = tf.function(snake_case ) for test_inputs in (self.test_sentences, self.paired_sentences): lowercase : Tuple = tf.constant(snake_case ) lowercase : Any = compiled_tokenizer(snake_case ) lowercase : Union[str, Any] = tf_tokenizer(snake_case ) for key in eager_outputs.keys(): self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key] ) ) @slow def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' for tf_tokenizer in self.tf_tokenizers: lowercase : Tuple = ModelToSave(tokenizer=snake_case ) lowercase : Dict = tf.convert_to_tensor(self.test_sentences ) lowercase : Optional[int] = model(snake_case ) # Build model with some sample inputs with TemporaryDirectory() as tempdir: lowercase : Union[str, Any] = Path(snake_case ) / """saved.model""" model.save(snake_case ) lowercase : List[Any] = tf.keras.models.load_model(snake_case ) lowercase : List[str] = loaded_model(snake_case ) # We may see small differences because the loaded model is compiled, so we need an epsilon for the test self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output ) ) ,1e-5 )
20
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast from ...utils import logging lowercase : List[str] = logging.get_logger(__name__) lowercase : Optional[Any] = { """EleutherAI/gpt-neo-1.3B""": """https://huggingface.co/EleutherAI/gpt-neo-1.3B/resolve/main/config.json""", # See all GPTNeo models at https://huggingface.co/models?filter=gpt_neo } class __snake_case ( lowerCAmelCase ): _a : str= "gpt_neo" _a : Optional[int]= ["past_key_values"] _a : Dict= {"num_attention_heads": "num_heads", "num_hidden_layers": "num_layers"} def __init__( self ,snake_case=50257 ,snake_case=2048 ,snake_case=2048 ,snake_case=24 ,snake_case=[[["global", "local"], 12]] ,snake_case=16 ,snake_case=None ,snake_case=256 ,snake_case="gelu_new" ,snake_case=0.0 ,snake_case=0.0 ,snake_case=0.0 ,snake_case=0.1 ,snake_case=1e-5 ,snake_case=0.02 ,snake_case=True ,snake_case=50256 ,snake_case=50256 ,**snake_case ,): '''simple docstring''' lowercase : int = vocab_size lowercase : Union[str, Any] = max_position_embeddings lowercase : Dict = hidden_size lowercase : Union[str, Any] = num_layers lowercase : Union[str, Any] = num_heads lowercase : Optional[int] = intermediate_size lowercase : List[str] = window_size lowercase : Optional[int] = activation_function lowercase : List[str] = resid_dropout lowercase : int = embed_dropout lowercase : Optional[int] = attention_dropout lowercase : Tuple = classifier_dropout lowercase : Optional[int] = layer_norm_epsilon lowercase : Dict = initializer_range lowercase : List[str] = use_cache lowercase : Optional[int] = bos_token_id lowercase : int = eos_token_id lowercase : Union[str, Any] = attention_types lowercase : Dict = self.expand_attention_types_params(snake_case ) if len(self.attention_layers ) != self.num_layers: raise ValueError( """Configuration for convolutional module is incorrect. """ """It is required that `len(config.attention_layers)` == `config.num_layers` """ f"but is `len(config.attention_layers) = {len(self.attention_layers )}`, " f"`config.num_layers = {self.num_layers}`. " """`config.attention_layers` is prepared using `config.attention_types`. """ """Please verify the value of `config.attention_types` argument.""" ) super().__init__(bos_token_id=snake_case ,eos_token_id=snake_case ,**snake_case ) @staticmethod def _SCREAMING_SNAKE_CASE ( snake_case ): '''simple docstring''' lowercase : List[Any] = [] for item in attention_types: for _ in range(item[1] ): attentions.extend(item[0] ) return attentions def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: import torch lowercase : Tuple = input.size() lowercase : int = len(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = shape[dimension] lowercase : int = torch.arange(0 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = torch.div(sizedim - size , SCREAMING_SNAKE_CASE__ , rounding_mode="""floor""" ) + 1 lowercase : Dict = torch.arange(SCREAMING_SNAKE_CASE__ ) + low_indices[:min_length][:, None] lowercase : Union[str, Any] = [slice(SCREAMING_SNAKE_CASE__ )] * rank lowercase : Optional[Any] = indices lowercase : List[str] = input[s] lowercase : Optional[int] = list(range(0 , rank + 1 ) ) perm.append(perm.pop(dimension + 1 ) ) return sliced.permute(SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: import torch lowercase : Union[str, Any] = torch.arange(1 , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = torch.remainder(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = remainders == 0 lowercase : Optional[int] = candidates[divisor_indices] lowercase : List[Any] = torch.max(SCREAMING_SNAKE_CASE__ ) return largest_divisor, torch.div(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , rounding_mode="""floor""" ) class __snake_case ( lowerCAmelCase ): @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Dict = OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}} ) if self.use_past: self.fill_with_past_key_values_(snake_case ,direction="""inputs""" ) lowercase : Dict = {0: """batch""", 1: """past_sequence + sequence"""} else: lowercase : List[str] = {0: """batch""", 1: """sequence"""} return common_inputs @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return self._config.num_heads def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = -1 ,snake_case = -1 ,snake_case = False ,snake_case = None ,): '''simple docstring''' lowercase : Any = super(snake_case ,self ).generate_dummy_inputs( snake_case ,batch_size=snake_case ,seq_length=snake_case ,is_pair=snake_case ,framework=snake_case ) # We need to order the input in the way they appears in the forward() lowercase : List[str] = OrderedDict({"""input_ids""": common_inputs["""input_ids"""]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed.""" ) else: import torch lowercase , lowercase : List[Any] = common_inputs["""input_ids"""].shape # Not using the same length for past_key_values lowercase : Optional[int] = seqlen + 2 lowercase : int = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) lowercase : Optional[Any] = [ (torch.zeros(snake_case ), torch.zeros(snake_case )) for _ in range(self.num_layers ) ] lowercase : Optional[Any] = common_inputs["""attention_mask"""] if self.use_past: lowercase : Any = ordered_inputs["""attention_mask"""].dtype lowercase : Union[str, Any] = torch.cat( [ordered_inputs["""attention_mask"""], torch.ones(snake_case ,snake_case ,dtype=snake_case )] ,dim=1 ) return ordered_inputs @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return 13
20
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) lowercase : Optional[int] = {"""configuration_fnet""": ["""FNET_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FNetConfig"""]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Union[str, Any] = ["""FNetTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : List[str] = ["""FNetTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : str = [ """FNET_PRETRAINED_MODEL_ARCHIVE_LIST""", """FNetForMaskedLM""", """FNetForMultipleChoice""", """FNetForNextSentencePrediction""", """FNetForPreTraining""", """FNetForQuestionAnswering""", """FNetForSequenceClassification""", """FNetForTokenClassification""", """FNetLayer""", """FNetModel""", """FNetPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_fnet import FNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet import FNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet_fast import FNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_fnet import ( FNET_PRETRAINED_MODEL_ARCHIVE_LIST, FNetForMaskedLM, FNetForMultipleChoice, FNetForNextSentencePrediction, FNetForPreTraining, FNetForQuestionAnswering, FNetForSequenceClassification, FNetForTokenClassification, FNetLayer, FNetModel, FNetPreTrainedModel, ) else: import sys lowercase : Dict = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
20
from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING lowercase : Any = logging.get_logger(__name__) @add_end_docstrings(lowerCAmelCase ) class __snake_case ( lowerCAmelCase ): def __init__( self ,*snake_case ,**snake_case ): '''simple docstring''' super().__init__(*snake_case ,**snake_case ) requires_backends(self ,"""vision""" ) self.check_model_type( TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING if self.framework == """tf""" else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING ) def _SCREAMING_SNAKE_CASE ( self ,snake_case=None ): '''simple docstring''' lowercase : List[Any] = {} if top_k is not None: lowercase : int = top_k return {}, {}, postprocess_params def __call__( self ,snake_case ,**snake_case ): '''simple docstring''' return super().__call__(snake_case ,**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Any = load_image(snake_case ) lowercase : List[Any] = self.image_processor(images=snake_case ,return_tensors=self.framework ) return model_inputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : int = self.model(**snake_case ) return model_outputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=5 ): '''simple docstring''' if top_k > self.model.config.num_labels: lowercase : Tuple = self.model.config.num_labels if self.framework == "pt": lowercase : str = model_outputs.logits.softmax(-1 )[0] lowercase , lowercase : Dict = probs.topk(snake_case ) elif self.framework == "tf": lowercase : Optional[int] = stable_softmax(model_outputs.logits ,axis=-1 )[0] lowercase : Union[str, Any] = tf.math.top_k(snake_case ,k=snake_case ) lowercase , lowercase : List[str] = topk.values.numpy(), topk.indices.numpy() else: raise ValueError(f"Unsupported framework: {self.framework}" ) lowercase : Tuple = scores.tolist() lowercase : Dict = ids.tolist() return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(snake_case ,snake_case )]
20
1
# DISCLAIMER: This code is strongly influenced by https://github.com/pesser/pytorch_diffusion # and https://github.com/hojonathanho/diffusion import math from dataclasses import dataclass from typing import List, Optional, Tuple, Union import numpy as np import torch from diffusers.configuration_utils import ConfigMixin, register_to_config from diffusers.schedulers.scheduling_utils import SchedulerMixin from diffusers.utils import BaseOutput, deprecate @dataclass # Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->DDIM class __snake_case ( lowerCAmelCase ): _a : torch.FloatTensor _a : Optional[torch.FloatTensor]= None def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=0.999 , SCREAMING_SNAKE_CASE__="cosine" , ) -> Optional[Any]: if alpha_transform_type == "cosine": def alpha_bar_fn(SCREAMING_SNAKE_CASE__ ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(SCREAMING_SNAKE_CASE__ ): return math.exp(t * -12.0 ) else: raise ValueError(f"Unsupported alpha_tranform_type: {alpha_transform_type}" ) lowercase : Tuple = [] for i in range(SCREAMING_SNAKE_CASE__ ): lowercase : List[str] = i / num_diffusion_timesteps lowercase : List[str] = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(SCREAMING_SNAKE_CASE__ ) / alpha_bar_fn(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) ) return torch.tensor(SCREAMING_SNAKE_CASE__ , dtype=torch.floataa ) class __snake_case ( lowerCAmelCase , lowerCAmelCase ): _a : Union[str, Any]= 1 @register_to_config def __init__( self ,snake_case = 1000 ,snake_case = 0.0_001 ,snake_case = 0.02 ,snake_case = "linear" ,snake_case = None ,snake_case = True ,snake_case = True ,snake_case = 0 ,snake_case = "epsilon" ,snake_case = 1.0 ,**snake_case ,): '''simple docstring''' if kwargs.get("""set_alpha_to_one""" ,snake_case ) is not None: lowercase : Any = ( """The `set_alpha_to_one` argument is deprecated. Please use `set_alpha_to_zero` instead.""" ) deprecate("""set_alpha_to_one""" ,"""1.0.0""" ,snake_case ,standard_warn=snake_case ) lowercase : List[str] = kwargs["""set_alpha_to_one"""] if trained_betas is not None: lowercase : str = torch.tensor(snake_case ,dtype=torch.floataa ) elif beta_schedule == "linear": lowercase : Union[str, Any] = torch.linspace(snake_case ,snake_case ,snake_case ,dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. lowercase : int = ( torch.linspace(beta_start**0.5 ,beta_end**0.5 ,snake_case ,dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule lowercase : List[Any] = betas_for_alpha_bar(snake_case ) else: raise NotImplementedError(f"{beta_schedule} does is not implemented for {self.__class__}" ) lowercase : Optional[Any] = 1.0 - self.betas lowercase : str = torch.cumprod(self.alphas ,dim=0 ) # At every step in inverted ddim, we are looking into the next alphas_cumprod # For the final step, there is no next alphas_cumprod, and the index is out of bounds # `set_alpha_to_zero` decides whether we set this parameter simply to zero # in this case, self.step() just output the predicted noise # or whether we use the final alpha of the "non-previous" one. lowercase : Optional[int] = torch.tensor(0.0 ) if set_alpha_to_zero else self.alphas_cumprod[-1] # standard deviation of the initial noise distribution lowercase : Dict = 1.0 # setable values lowercase : List[Any] = None lowercase : Dict = torch.from_numpy(np.arange(0 ,snake_case ).copy().astype(np.intaa ) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = None ): '''simple docstring''' return sample def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = None ): '''simple docstring''' if num_inference_steps > self.config.num_train_timesteps: raise ValueError( f"`num_inference_steps`: {num_inference_steps} cannot be larger than `self.config.train_timesteps`:" f" {self.config.num_train_timesteps} as the unet model trained with this scheduler can only handle" f" maximal {self.config.num_train_timesteps} timesteps." ) lowercase : Any = num_inference_steps lowercase : Any = self.config.num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowercase : Union[str, Any] = (np.arange(0 ,snake_case ) * step_ratio).round().copy().astype(np.intaa ) lowercase : Dict = torch.from_numpy(snake_case ).to(snake_case ) self.timesteps += self.config.steps_offset def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case = 0.0 ,snake_case = False ,snake_case = None ,snake_case = True ,): '''simple docstring''' lowercase : List[str] = timestep + self.config.num_train_timesteps // self.num_inference_steps # 2. compute alphas, betas # change original implementation to exactly match noise levels for analogous forward process lowercase : Optional[Any] = self.alphas_cumprod[timestep] lowercase : str = ( self.alphas_cumprod[prev_timestep] if prev_timestep < self.config.num_train_timesteps else self.final_alpha_cumprod ) lowercase : Any = 1 - alpha_prod_t # 3. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf if self.config.prediction_type == "epsilon": lowercase : Union[str, Any] = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 lowercase : str = model_output elif self.config.prediction_type == "sample": lowercase : str = model_output lowercase : str = (sample - alpha_prod_t ** 0.5 * pred_original_sample) / beta_prod_t ** 0.5 elif self.config.prediction_type == "v_prediction": lowercase : str = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output lowercase : int = (alpha_prod_t**0.5) * model_output + (beta_prod_t**0.5) * sample else: raise ValueError( f"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample`, or" """ `v_prediction`""" ) # 4. Clip or threshold "predicted x_0" if self.config.clip_sample: lowercase : int = pred_original_sample.clamp( -self.config.clip_sample_range ,self.config.clip_sample_range ) # 5. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf lowercase : Dict = (1 - alpha_prod_t_prev) ** 0.5 * pred_epsilon # 6. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf lowercase : int = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction if not return_dict: return (prev_sample, pred_original_sample) return DDIMSchedulerOutput(prev_sample=snake_case ,pred_original_sample=snake_case ) def __len__( self ): '''simple docstring''' return self.config.num_train_timesteps
20
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class __snake_case : def __init__( self ,snake_case ,): '''simple docstring''' lowercase : Any = parent lowercase : Tuple = 13 lowercase : str = 7 lowercase : Dict = True lowercase : Dict = True lowercase : str = True lowercase : List[str] = True lowercase : int = True lowercase : Union[str, Any] = False lowercase : Dict = False lowercase : List[Any] = False lowercase : List[Any] = 2 lowercase : Optional[Any] = 99 lowercase : int = 0 lowercase : Tuple = 32 lowercase : int = 2 lowercase : Tuple = 4 lowercase : List[Any] = 0.1 lowercase : Tuple = 0.1 lowercase : List[Any] = 512 lowercase : int = 16 lowercase : Dict = 2 lowercase : int = 0.02 lowercase : Union[str, Any] = 3 lowercase : Any = 4 lowercase : List[Any] = """last""" lowercase : Tuple = True lowercase : List[Any] = None lowercase : Any = 0 def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) lowercase : List[str] = random_attention_mask([self.batch_size, self.seq_length] ,dtype=tf.floataa ) lowercase : Tuple = None if self.use_input_lengths: lowercase : List[str] = ( ids_tensor([self.batch_size] ,vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length lowercase : Tuple = None if self.use_token_type_ids: lowercase : Optional[int] = ids_tensor([self.batch_size, self.seq_length] ,self.n_langs ) lowercase : List[str] = None lowercase : List[str] = None lowercase : Optional[Any] = None if self.use_labels: lowercase : List[str] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) lowercase : List[str] = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) lowercase : str = ids_tensor([self.batch_size] ,2 ,dtype=tf.floataa ) lowercase : Optional[Any] = ids_tensor([self.batch_size] ,self.num_choices ) lowercase : str = FlaubertConfig( vocab_size=self.vocab_size ,n_special=self.n_special ,emb_dim=self.hidden_size ,n_layers=self.num_hidden_layers ,n_heads=self.num_attention_heads ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,gelu_activation=self.gelu_activation ,sinusoidal_embeddings=self.sinusoidal_embeddings ,asm=self.asm ,causal=self.causal ,n_langs=self.n_langs ,max_position_embeddings=self.max_position_embeddings ,initializer_range=self.initializer_range ,summary_type=self.summary_type ,use_proj=self.use_proj ,bos_token_id=self.bos_token_id ,) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Tuple = TFFlaubertModel(config=snake_case ) lowercase : str = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} lowercase : Optional[Any] = model(snake_case ) lowercase : List[Any] = [input_ids, input_mask] lowercase : int = model(snake_case ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : List[Any] = TFFlaubertWithLMHeadModel(snake_case ) lowercase : Optional[Any] = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} lowercase : int = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Tuple = TFFlaubertForQuestionAnsweringSimple(snake_case ) lowercase : Union[str, Any] = {"""input_ids""": input_ids, """lengths""": input_lengths} lowercase : Tuple = model(snake_case ) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Union[str, Any] = TFFlaubertForSequenceClassification(snake_case ) lowercase : str = {"""input_ids""": input_ids, """lengths""": input_lengths} lowercase : str = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Any = self.num_labels lowercase : List[str] = TFFlaubertForTokenClassification(config=snake_case ) lowercase : Dict = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} lowercase : int = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Any = self.num_choices lowercase : Dict = TFFlaubertForMultipleChoice(config=snake_case ) lowercase : Any = tf.tile(tf.expand_dims(snake_case ,1 ) ,(1, self.num_choices, 1) ) lowercase : Optional[Any] = tf.tile(tf.expand_dims(snake_case ,1 ) ,(1, self.num_choices, 1) ) lowercase : Dict = tf.tile(tf.expand_dims(snake_case ,1 ) ,(1, self.num_choices, 1) ) lowercase : Union[str, Any] = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } lowercase : int = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_choices) ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Any = self.prepare_config_and_inputs() ( ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ) : int = config_and_inputs lowercase : List[str] = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """langs""": token_type_ids, """lengths""": input_lengths, } return config, inputs_dict @require_tf class __snake_case ( lowerCAmelCase , lowerCAmelCase , unittest.TestCase ): _a : Dict= ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) _a : Optional[Any]= ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable _a : Any= ( { "feature-extraction": TFFlaubertModel, "fill-mask": TFFlaubertWithLMHeadModel, "question-answering": TFFlaubertForQuestionAnsweringSimple, "text-classification": TFFlaubertForSequenceClassification, "token-classification": TFFlaubertForTokenClassification, "zero-shot": TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) _a : Tuple= False _a : int= False def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ): '''simple docstring''' if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("""Fast""" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : str = TFFlaubertModelTester(self ) lowercase : List[Any] = ConfigTester(self ,config_class=snake_case ,emb_dim=37 ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' self.config_tester.run_common_tests() def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*snake_case ) @slow def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase : Dict = TFFlaubertModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) @require_tf @require_sentencepiece @require_tokenizers class __snake_case ( unittest.TestCase ): @slow def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased""" ) lowercase : int = tf.convert_to_tensor( [[0, 158, 735, 2592, 1424, 6727, 82, 1]] ,dtype=tf.intaa ,) # "J'aime flaubert !" lowercase : Dict = model(snake_case )[0] lowercase : Union[str, Any] = tf.TensorShape((1, 8, 512) ) self.assertEqual(output.shape ,snake_case ) # compare the actual values for a slice. lowercase : Tuple = tf.convert_to_tensor( [ [ [-1.8_768_773, -1.566_555, 0.27_072_418], [-1.6_920_038, -0.5_873_505, 1.9_329_599], [-2.9_563_985, -1.6_993_835, 1.7_972_052], ] ] ,dtype=tf.floataa ,) self.assertTrue(np.allclose(output[:, :3, :3].numpy() ,expected_slice.numpy() ,atol=1e-4 ) )
20
1
import math from typing import Optional import numpy as np from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase : Optional[Any] = logging.get_logger(__name__) lowercase : List[Any] = { """facebook/encodec_24khz""": """https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json""", """facebook/encodec_48khz""": """https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json""", } class __snake_case ( lowerCAmelCase ): _a : int= "encodec" def __init__( self ,snake_case=[1.5, 3.0, 6.0, 12.0, 24.0] ,snake_case=24000 ,snake_case=1 ,snake_case=False ,snake_case=None ,snake_case=None ,snake_case=128 ,snake_case=32 ,snake_case=1 ,snake_case=[8, 5, 4, 2] ,snake_case="weight_norm" ,snake_case=7 ,snake_case=7 ,snake_case=3 ,snake_case=2 ,snake_case=True ,snake_case="reflect" ,snake_case=2 ,snake_case=2 ,snake_case=1.0 ,snake_case=1024 ,snake_case=None ,snake_case=True ,**snake_case ,): '''simple docstring''' lowercase : Tuple = target_bandwidths lowercase : int = sampling_rate lowercase : List[str] = audio_channels lowercase : Tuple = normalize lowercase : Optional[Any] = chunk_length_s lowercase : List[str] = overlap lowercase : List[Any] = hidden_size lowercase : Tuple = num_filters lowercase : Dict = num_residual_layers lowercase : str = upsampling_ratios lowercase : str = norm_type lowercase : List[Any] = kernel_size lowercase : Tuple = last_kernel_size lowercase : Any = residual_kernel_size lowercase : Union[str, Any] = dilation_growth_rate lowercase : Union[str, Any] = use_causal_conv lowercase : int = pad_mode lowercase : List[str] = compress lowercase : List[str] = num_lstm_layers lowercase : Union[str, Any] = trim_right_ratio lowercase : List[Any] = codebook_size lowercase : List[Any] = codebook_dim if codebook_dim is not None else hidden_size lowercase : Optional[int] = use_conv_shortcut if self.norm_type not in ["weight_norm", "time_group_norm"]: raise ValueError( f"self.norm_type must be one of `\"weight_norm\"`, `\"time_group_norm\"`), got {self.norm_type}" ) super().__init__(**snake_case ) @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 ,int((1.0 - self.overlap) * self.chunk_length ) ) @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[Any] = np.prod(self.upsampling_ratios ) return math.ceil(self.sampling_rate / hop_length ) @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return int(1000 * self.target_bandwidths[-1] // (self.frame_rate * 10) )
20
import json import os from dataclasses import dataclass from functools import partial from typing import Callable import flax.linen as nn import jax import jax.numpy as jnp import joblib import optax import wandb from flax import jax_utils, struct, traverse_util from flax.serialization import from_bytes, to_bytes from flax.training import train_state from flax.training.common_utils import shard from tqdm.auto import tqdm from transformers import BigBirdConfig, FlaxBigBirdForQuestionAnswering from transformers.models.big_bird.modeling_flax_big_bird import FlaxBigBirdForQuestionAnsweringModule class __snake_case ( lowerCAmelCase ): _a : BigBirdConfig _a : jnp.dtype= jnp.floataa _a : bool= True def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' super().setup() lowercase : List[str] = nn.Dense(5 ,dtype=self.dtype ) def __call__( self ,*snake_case ,**snake_case ): '''simple docstring''' lowercase : int = super().__call__(*snake_case ,**snake_case ) lowercase : Any = self.cls(outputs[2] ) return outputs[:2] + (cls_out,) class __snake_case ( lowerCAmelCase ): _a : List[Any]= FlaxBigBirdForNaturalQuestionsModule def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: def cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None ): lowercase : int = logits.shape[-1] lowercase : Dict = (labels[..., None] == jnp.arange(SCREAMING_SNAKE_CASE__ )[None]).astype("""f4""" ) lowercase : Any = jax.nn.log_softmax(SCREAMING_SNAKE_CASE__ , axis=-1 ) lowercase : Optional[Any] = -jnp.sum(labels * logits , axis=-1 ) if reduction is not None: lowercase : Any = reduction(SCREAMING_SNAKE_CASE__ ) return loss lowercase : Optional[Any] = partial(SCREAMING_SNAKE_CASE__ , reduction=jnp.mean ) lowercase : Optional[int] = cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Dict = cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : int = cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return (start_loss + end_loss + pooled_loss) / 3 @dataclass class __snake_case : _a : str= "google/bigbird-roberta-base" _a : int= 3000 _a : int= 1_0500 _a : int= 128 _a : int= 3 _a : int= 1 _a : int= 5 # tx_args _a : float= 3E-5 _a : float= 0.0 _a : int= 2_0000 _a : float= 0.00_95 _a : str= "bigbird-roberta-natural-questions" _a : str= "training-expt" _a : str= "data/nq-training.jsonl" _a : str= "data/nq-validation.jsonl" def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' os.makedirs(self.base_dir ,exist_ok=snake_case ) lowercase : Optional[int] = os.path.join(self.base_dir ,self.save_dir ) lowercase : Optional[int] = self.batch_size_per_device * jax.device_count() @dataclass class __snake_case : _a : int _a : int= 4096 # no dynamic padding on TPUs def __call__( self ,snake_case ): '''simple docstring''' lowercase : int = self.collate_fn(snake_case ) lowercase : Union[str, Any] = jax.tree_util.tree_map(snake_case ,snake_case ) return batch def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase , lowercase : Union[str, Any] = self.fetch_inputs(features["""input_ids"""] ) lowercase : Tuple = { """input_ids""": jnp.array(snake_case ,dtype=jnp.intaa ), """attention_mask""": jnp.array(snake_case ,dtype=jnp.intaa ), """start_labels""": jnp.array(features["""start_token"""] ,dtype=jnp.intaa ), """end_labels""": jnp.array(features["""end_token"""] ,dtype=jnp.intaa ), """pooled_labels""": jnp.array(features["""category"""] ,dtype=jnp.intaa ), } return batch def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Tuple = [self._fetch_inputs(snake_case ) for ids in input_ids] return zip(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Union[str, Any] = [1 for _ in range(len(snake_case ) )] while len(snake_case ) < self.max_length: input_ids.append(self.pad_id ) attention_mask.append(0 ) return input_ids, attention_mask def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None ) -> Any: if seed is not None: lowercase : Optional[int] = dataset.shuffle(seed=SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) // batch_size ): lowercase : Optional[Any] = dataset[i * batch_size : (i + 1) * batch_size] yield dict(SCREAMING_SNAKE_CASE__ ) @partial(jax.pmap , axis_name="""batch""" ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) -> List[Any]: def loss_fn(SCREAMING_SNAKE_CASE__ ): lowercase : List[str] = model_inputs.pop("""start_labels""" ) lowercase : Optional[int] = model_inputs.pop("""end_labels""" ) lowercase : str = model_inputs.pop("""pooled_labels""" ) lowercase : Union[str, Any] = state.apply_fn(**SCREAMING_SNAKE_CASE__ , params=SCREAMING_SNAKE_CASE__ , dropout_rng=SCREAMING_SNAKE_CASE__ , train=SCREAMING_SNAKE_CASE__ ) lowercase , lowercase , lowercase : List[str] = outputs return state.loss_fn( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) lowercase , lowercase : int = jax.random.split(SCREAMING_SNAKE_CASE__ ) lowercase : Dict = jax.value_and_grad(SCREAMING_SNAKE_CASE__ ) lowercase , lowercase : Union[str, Any] = grad_fn(state.params ) lowercase : List[Any] = jax.lax.pmean({"""loss""": loss} , axis_name="""batch""" ) lowercase : List[Any] = jax.lax.pmean(SCREAMING_SNAKE_CASE__ , """batch""" ) lowercase : str = state.apply_gradients(grads=SCREAMING_SNAKE_CASE__ ) return state, metrics, new_drp_rng @partial(jax.pmap , axis_name="""batch""" ) def _snake_case( SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : int = model_inputs.pop("""start_labels""" ) lowercase : Dict = model_inputs.pop("""end_labels""" ) lowercase : Optional[Any] = model_inputs.pop("""pooled_labels""" ) lowercase : Optional[int] = state.apply_fn(**SCREAMING_SNAKE_CASE__ , params=state.params , train=SCREAMING_SNAKE_CASE__ ) lowercase , lowercase , lowercase : List[Any] = outputs lowercase : Dict = state.loss_fn(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : str = jax.lax.pmean({"""loss""": loss} , axis_name="""batch""" ) return metrics class __snake_case ( train_state.TrainState ): _a : Callable= struct.field(pytree_node=lowerCAmelCase ) @dataclass class __snake_case : _a : Args _a : Callable _a : Callable _a : Callable _a : Callable _a : wandb _a : Callable= None def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case=None ): '''simple docstring''' lowercase : Tuple = model.params lowercase : Any = TrainState.create( apply_fn=model.__call__ ,params=snake_case ,tx=snake_case ,loss_fn=snake_case ,) if ckpt_dir is not None: lowercase , lowercase , lowercase , lowercase , lowercase : Tuple = restore_checkpoint(snake_case ,snake_case ) lowercase : List[str] = { """lr""": args.lr, """init_lr""": args.init_lr, """warmup_steps""": args.warmup_steps, """num_train_steps""": num_train_steps, """weight_decay""": args.weight_decay, } lowercase , lowercase : Tuple = build_tx(**snake_case ) lowercase : str = train_state.TrainState( step=snake_case ,apply_fn=model.__call__ ,params=snake_case ,tx=snake_case ,opt_state=snake_case ,) lowercase : Any = args lowercase : Optional[Any] = data_collator lowercase : List[str] = lr lowercase : str = params lowercase : Tuple = jax_utils.replicate(snake_case ) return state def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ): '''simple docstring''' lowercase : Dict = self.args lowercase : Optional[Any] = len(snake_case ) // args.batch_size lowercase : int = jax.random.PRNGKey(0 ) lowercase : List[str] = jax.random.split(snake_case ,jax.device_count() ) for epoch in range(args.max_epochs ): lowercase : List[Any] = jnp.array(0 ,dtype=jnp.floataa ) lowercase : List[str] = get_batched_dataset(snake_case ,args.batch_size ,seed=snake_case ) lowercase : int = 0 for batch in tqdm(snake_case ,total=snake_case ,desc=f"Running EPOCH-{epoch}" ): lowercase : Dict = self.data_collator(snake_case ) lowercase , lowercase , lowercase : Optional[int] = self.train_step_fn(snake_case ,snake_case ,**snake_case ) running_loss += jax_utils.unreplicate(metrics["""loss"""] ) i += 1 if i % args.logging_steps == 0: lowercase : Optional[Any] = jax_utils.unreplicate(state.step ) lowercase : List[str] = running_loss.item() / i lowercase : List[str] = self.scheduler_fn(state_step - 1 ) lowercase : int = self.evaluate(snake_case ,snake_case ) lowercase : Tuple = { """step""": state_step.item(), """eval_loss""": eval_loss.item(), """tr_loss""": tr_loss, """lr""": lr.item(), } tqdm.write(str(snake_case ) ) self.logger.log(snake_case ,commit=snake_case ) if i % args.save_steps == 0: self.save_checkpoint(args.save_dir + f"-e{epoch}-s{i}" ,state=snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : List[str] = get_batched_dataset(snake_case ,self.args.batch_size ) lowercase : Any = len(snake_case ) // self.args.batch_size lowercase : List[Any] = jnp.array(0 ,dtype=jnp.floataa ) lowercase : Optional[int] = 0 for batch in tqdm(snake_case ,total=snake_case ,desc="""Evaluating ... """ ): lowercase : Tuple = self.data_collator(snake_case ) lowercase : Optional[int] = self.val_step_fn(snake_case ,**snake_case ) running_loss += jax_utils.unreplicate(metrics["""loss"""] ) i += 1 return running_loss / i def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : str = jax_utils.unreplicate(snake_case ) print(f"SAVING CHECKPOINT IN {save_dir}" ,end=""" ... """ ) self.model_save_fn(snake_case ,params=state.params ) with open(os.path.join(snake_case ,"""opt_state.msgpack""" ) ,"""wb""" ) as f: f.write(to_bytes(state.opt_state ) ) joblib.dump(self.args ,os.path.join(snake_case ,"""args.joblib""" ) ) joblib.dump(self.data_collator ,os.path.join(snake_case ,"""data_collator.joblib""" ) ) with open(os.path.join(snake_case ,"""training_state.json""" ) ,"""w""" ) as f: json.dump({"""step""": state.step.item()} ,snake_case ) print("""DONE""" ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: print(f"RESTORING CHECKPOINT FROM {save_dir}" , end=""" ... """ ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , """flax_model.msgpack""" ) , """rb""" ) as f: lowercase : str = from_bytes(state.params , f.read() ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , """opt_state.msgpack""" ) , """rb""" ) as f: lowercase : Optional[int] = from_bytes(state.opt_state , f.read() ) lowercase : Optional[Any] = joblib.load(os.path.join(SCREAMING_SNAKE_CASE__ , """args.joblib""" ) ) lowercase : int = joblib.load(os.path.join(SCREAMING_SNAKE_CASE__ , """data_collator.joblib""" ) ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , """training_state.json""" ) , """r""" ) as f: lowercase : Tuple = json.load(SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = training_state["""step"""] print("""DONE""" ) return params, opt_state, step, args, data_collator def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: lowercase : List[str] = num_train_steps - warmup_steps lowercase : Dict = optax.linear_schedule(init_value=SCREAMING_SNAKE_CASE__ , end_value=SCREAMING_SNAKE_CASE__ , transition_steps=SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = optax.linear_schedule(init_value=SCREAMING_SNAKE_CASE__ , end_value=1e-7 , transition_steps=SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = optax.join_schedules(schedules=[warmup_fn, decay_fn] , boundaries=[warmup_steps] ) return lr def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: def weight_decay_mask(SCREAMING_SNAKE_CASE__ ): lowercase : List[Any] = traverse_util.flatten_dict(SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = {k: (v[-1] != """bias""" and v[-2:] != ("""LayerNorm""", """scale""")) for k, v in params.items()} return traverse_util.unflatten_dict(SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = scheduler_fn(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = optax.adamw(learning_rate=SCREAMING_SNAKE_CASE__ , weight_decay=SCREAMING_SNAKE_CASE__ , mask=SCREAMING_SNAKE_CASE__ ) return tx, lr
20
1
lowercase : List[str] = 8.3_1_4_4_6_2 # Unit - J mol-1 K-1 def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> float: if moles < 0 or kelvin < 0 or volume < 0: raise ValueError("""Invalid inputs. Enter positive value.""" ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> float: if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError("""Invalid inputs. Enter positive value.""" ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
20
from math import sqrt def _snake_case( SCREAMING_SNAKE_CASE__ ) -> bool: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number >= 0 ), "'number' must been an int and positive" lowercase : Union[str, Any] = True # 0 and 1 are none primes. if number <= 1: lowercase : str = False for divisor in range(2 , int(round(sqrt(SCREAMING_SNAKE_CASE__ ) ) ) + 1 ): # if 'number' divisible by 'divisor' then sets 'status' # of false and break up the loop. if number % divisor == 0: lowercase : Any = False break # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'status' must been from type bool" return status def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n > 2), "'N' must been an int and > 2" # beginList: contains all natural numbers from 2 up to N lowercase : str = list(range(2 , n + 1 ) ) lowercase : Tuple = [] # this list will be returns. # actual sieve of erathostenes for i in range(len(SCREAMING_SNAKE_CASE__ ) ): for j in range(i + 1 , len(SCREAMING_SNAKE_CASE__ ) ): if (begin_list[i] != 0) and (begin_list[j] % begin_list[i] == 0): lowercase : Tuple = 0 # filters actual prime numbers. lowercase : int = [x for x in begin_list if x != 0] # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type list" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n > 2), "'N' must been an int and > 2" lowercase : Dict = [] # iterates over all numbers between 2 up to N+1 # if a number is prime then appends to list 'ans' for number in range(2 , n + 1 ): if is_prime(SCREAMING_SNAKE_CASE__ ): ans.append(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type list" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Tuple: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and number >= 0, "'number' must been an int and >= 0" lowercase : Tuple = [] # this list will be returns of the function. # potential prime number factors. lowercase : Optional[Any] = 2 lowercase : Any = number if number == 0 or number == 1: ans.append(SCREAMING_SNAKE_CASE__ ) # if 'number' not prime then builds the prime factorization of 'number' elif not is_prime(SCREAMING_SNAKE_CASE__ ): while quotient != 1: if is_prime(SCREAMING_SNAKE_CASE__ ) and (quotient % factor == 0): ans.append(SCREAMING_SNAKE_CASE__ ) quotient /= factor else: factor += 1 else: ans.append(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type list" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number >= 0 ), "'number' bust been an int and >= 0" lowercase : Tuple = 0 # prime factorization of 'number' lowercase : Optional[int] = prime_factorization(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = max(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type int" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> str: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number >= 0 ), "'number' bust been an int and >= 0" lowercase : Union[str, Any] = 0 # prime factorization of 'number' lowercase : Tuple = prime_factorization(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = min(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type int" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'number' must been an int" assert isinstance(number % 2 == 0 , SCREAMING_SNAKE_CASE__ ), "compare bust been from type bool" return number % 2 == 0 def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'number' must been an int" assert isinstance(number % 2 != 0 , SCREAMING_SNAKE_CASE__ ), "compare bust been from type bool" return number % 2 != 0 def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (number > 2) and is_even(SCREAMING_SNAKE_CASE__ ) ), "'number' must been an int, even and > 2" lowercase : Union[str, Any] = [] # this list will returned # creates a list of prime numbers between 2 up to 'number' lowercase : str = get_prime_numbers(SCREAMING_SNAKE_CASE__ ) lowercase : Any = len(SCREAMING_SNAKE_CASE__ ) # run variable for while-loops. lowercase : Optional[Any] = 0 lowercase : List[Any] = None # exit variable. for break up the loops lowercase : Any = True while i < len_pn and loop: lowercase : str = i + 1 while j < len_pn and loop: if prime_numbers[i] + prime_numbers[j] == number: lowercase : Union[str, Any] = False ans.append(prime_numbers[i] ) ans.append(prime_numbers[j] ) j += 1 i += 1 # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (len(SCREAMING_SNAKE_CASE__ ) == 2) and (ans[0] + ans[1] == number) and is_prime(ans[0] ) and is_prime(ans[1] ) ), "'ans' must contains two primes. And sum of elements must been eq 'number'" return ans def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (numbera >= 0) and (numbera >= 0) ), "'number1' and 'number2' must been positive integer." lowercase : Union[str, Any] = 0 while numbera != 0: lowercase : Optional[int] = numbera % numbera lowercase : Optional[int] = numbera lowercase : Dict = rest # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( numbera >= 0 ), "'number' must been from type int and positive" return numbera def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (numbera >= 1) and (numbera >= 1) ), "'number1' and 'number2' must been positive integer." lowercase : Dict = 1 # actual answer that will be return. # for kgV (x,1) if numbera > 1 and numbera > 1: # builds the prime factorization of 'number1' and 'number2' lowercase : Optional[Any] = prime_factorization(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[int] = prime_factorization(SCREAMING_SNAKE_CASE__ ) elif numbera == 1 or numbera == 1: lowercase : Union[str, Any] = [] lowercase : List[str] = [] lowercase : Dict = max(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = 0 lowercase : Optional[Any] = 0 lowercase : List[str] = [] # captured numbers int both 'primeFac1' and 'primeFac2' # iterates through primeFac1 for n in prime_fac_a: if n not in done: if n in prime_fac_a: lowercase : Dict = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) for _ in range(max(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ): ans *= n else: lowercase : List[Any] = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) for _ in range(SCREAMING_SNAKE_CASE__ ): ans *= n done.append(SCREAMING_SNAKE_CASE__ ) # iterates through primeFac2 for n in prime_fac_a: if n not in done: lowercase : Optional[int] = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) for _ in range(SCREAMING_SNAKE_CASE__ ): ans *= n done.append(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( ans >= 0 ), "'ans' must been from type int and positive" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 0), "'number' must been a positive int" lowercase : Dict = 0 lowercase : List[str] = 2 # this variable holds the answer while index < n: index += 1 ans += 1 # counts to the next number # if ans not prime then # runs to the next prime number. while not is_prime(SCREAMING_SNAKE_CASE__ ): ans += 1 # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and is_prime( SCREAMING_SNAKE_CASE__ ), "'ans' must been a prime number and from type int" return ans def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: assert ( is_prime(SCREAMING_SNAKE_CASE__ ) and is_prime(SCREAMING_SNAKE_CASE__ ) and (p_number_a < p_number_a) ), "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" lowercase : List[str] = p_number_a + 1 # jump to the next number lowercase : List[Any] = [] # this list will be returns. # if number is not prime then # fetch the next prime number. while not is_prime(SCREAMING_SNAKE_CASE__ ): number += 1 while number < p_number_a: ans.append(SCREAMING_SNAKE_CASE__ ) number += 1 # fetch the next prime number. while not is_prime(SCREAMING_SNAKE_CASE__ ): number += 1 # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ans[0] != p_number_a and ans[len(SCREAMING_SNAKE_CASE__ ) - 1] != p_number_a ), "'ans' must been a list without the arguments" # 'ans' contains not 'pNumber1' and 'pNumber2' ! return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 1), "'n' must been int and >= 1" lowercase : Optional[Any] = [] # will be returned. for divisor in range(1 , n + 1 ): if n % divisor == 0: ans.append(SCREAMING_SNAKE_CASE__ ) # precondition assert ans[0] == 1 and ans[len(SCREAMING_SNAKE_CASE__ ) - 1] == n, "Error in function getDivisiors(...)" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number > 1 ), "'number' must been an int and >= 1" lowercase : str = get_divisors(SCREAMING_SNAKE_CASE__ ) # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (divisors[0] == 1) and (divisors[len(SCREAMING_SNAKE_CASE__ ) - 1] == number) ), "Error in help-function getDivisiors(...)" # summed all divisors up to 'number' (exclusive), hence [:-1] return sum(divisors[:-1] ) == number def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (denominator != 0) ), "The arguments must been from type int and 'denominator' != 0" # build the greatest common divisor of numerator and denominator. lowercase : Tuple = gcd(abs(SCREAMING_SNAKE_CASE__ ) , abs(SCREAMING_SNAKE_CASE__ ) ) # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (numerator % gcd_of_fraction == 0) and (denominator % gcd_of_fraction == 0) ), "Error in function gcd(...,...)" return (numerator // gcd_of_fraction, denominator // gcd_of_fraction) def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 0), "'n' must been a int and >= 0" lowercase : List[str] = 1 # this will be return. for factor in range(1 , n + 1 ): ans *= factor return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 0), "'n' must been an int and >= 0" lowercase : int = 0 lowercase : Union[str, Any] = 1 lowercase : int = 1 # this will be return for _ in range(n - 1 ): lowercase : Optional[int] = ans ans += fiba lowercase : Optional[int] = tmp return ans
20
1
from __future__ import annotations def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> list[str]: if partitions <= 0: raise ValueError("""partitions must be a positive number!""" ) if partitions > number_of_bytes: raise ValueError("""partitions can not > number_of_bytes!""" ) lowercase : Optional[Any] = number_of_bytes // partitions lowercase : List[str] = [] for i in range(SCREAMING_SNAKE_CASE__ ): lowercase : Dict = i * bytes_per_partition + 1 lowercase : Any = ( number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition ) allocation_list.append(f"{start_bytes}-{end_bytes}" ) return allocation_list if __name__ == "__main__": import doctest doctest.testmod()
20
from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase : List[str] = logging.get_logger(__name__) lowercase : Any = { """uclanlp/visualbert-vqa""": """https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json""", """uclanlp/visualbert-vqa-pre""": """https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json""", """uclanlp/visualbert-vqa-coco-pre""": ( """https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json""" ), """uclanlp/visualbert-vcr""": """https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json""", """uclanlp/visualbert-vcr-pre""": """https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json""", """uclanlp/visualbert-vcr-coco-pre""": ( """https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json""" ), """uclanlp/visualbert-nlvr2""": """https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json""", """uclanlp/visualbert-nlvr2-pre""": """https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json""", """uclanlp/visualbert-nlvr2-coco-pre""": ( """https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json""" ) # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert } class __snake_case ( lowerCAmelCase ): _a : Union[str, Any]= "visual_bert" def __init__( self ,snake_case=30522 ,snake_case=768 ,snake_case=512 ,snake_case=12 ,snake_case=12 ,snake_case=3072 ,snake_case="gelu" ,snake_case=0.1 ,snake_case=0.1 ,snake_case=512 ,snake_case=2 ,snake_case=0.02 ,snake_case=1e-12 ,snake_case=False ,snake_case=True ,snake_case=1 ,snake_case=0 ,snake_case=2 ,**snake_case ,): '''simple docstring''' super().__init__(pad_token_id=snake_case ,bos_token_id=snake_case ,eos_token_id=snake_case ,**snake_case ) lowercase : Tuple = vocab_size lowercase : int = max_position_embeddings lowercase : Optional[Any] = hidden_size lowercase : int = visual_embedding_dim lowercase : Tuple = num_hidden_layers lowercase : str = num_attention_heads lowercase : Optional[Any] = intermediate_size lowercase : str = hidden_act lowercase : Tuple = hidden_dropout_prob lowercase : List[Any] = attention_probs_dropout_prob lowercase : Union[str, Any] = initializer_range lowercase : int = type_vocab_size lowercase : Union[str, Any] = layer_norm_eps lowercase : Union[str, Any] = bypass_transformer lowercase : int = special_visual_initialize
20
1
import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel from diffusers import DDIMScheduler, LDMPipeline, UNetaDModel, VQModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class __snake_case ( unittest.TestCase ): @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' torch.manual_seed(0 ) lowercase : Dict = UNetaDModel( block_out_channels=(32, 64) ,layers_per_block=2 ,sample_size=32 ,in_channels=3 ,out_channels=3 ,down_block_types=("""DownBlock2D""", """AttnDownBlock2D""") ,up_block_types=("""AttnUpBlock2D""", """UpBlock2D""") ,) return model @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' torch.manual_seed(0 ) lowercase : Optional[int] = VQModel( block_out_channels=[32, 64] ,in_channels=3 ,out_channels=3 ,down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] ,up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] ,latent_channels=3 ,) return model @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' torch.manual_seed(0 ) lowercase : Union[str, Any] = CLIPTextConfig( bos_token_id=0 ,eos_token_id=2 ,hidden_size=32 ,intermediate_size=37 ,layer_norm_eps=1e-05 ,num_attention_heads=4 ,num_hidden_layers=5 ,pad_token_id=1 ,vocab_size=1000 ,) return CLIPTextModel(snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Union[str, Any] = self.dummy_uncond_unet lowercase : Optional[Any] = DDIMScheduler() lowercase : Optional[Any] = self.dummy_vq_model lowercase : Any = LDMPipeline(unet=snake_case ,vqvae=snake_case ,scheduler=snake_case ) ldm.to(snake_case ) ldm.set_progress_bar_config(disable=snake_case ) lowercase : str = torch.manual_seed(0 ) lowercase : List[Any] = ldm(generator=snake_case ,num_inference_steps=2 ,output_type="""numpy""" ).images lowercase : int = torch.manual_seed(0 ) lowercase : Any = ldm(generator=snake_case ,num_inference_steps=2 ,output_type="""numpy""" ,return_dict=snake_case )[0] lowercase : Optional[int] = image[0, -3:, -3:, -1] lowercase : Optional[Any] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowercase : str = np.array([0.8_512, 0.818, 0.6_411, 0.6_808, 0.4_465, 0.5_618, 0.46, 0.6_231, 0.5_172] ) lowercase : List[Any] = 1e-2 if torch_device != """mps""" else 3e-2 assert np.abs(image_slice.flatten() - expected_slice ).max() < tolerance assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < tolerance @slow @require_torch class __snake_case ( unittest.TestCase ): def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Union[str, Any] = LDMPipeline.from_pretrained("""CompVis/ldm-celebahq-256""" ) ldm.to(snake_case ) ldm.set_progress_bar_config(disable=snake_case ) lowercase : List[Any] = torch.manual_seed(0 ) lowercase : Optional[int] = ldm(generator=snake_case ,num_inference_steps=5 ,output_type="""numpy""" ).images lowercase : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) lowercase : str = np.array([0.4_399, 0.44_975, 0.46_825, 0.474, 0.4_359, 0.4_581, 0.45_095, 0.4_341, 0.4_447] ) lowercase : str = 1e-2 if torch_device != """mps""" else 3e-2 assert np.abs(image_slice.flatten() - expected_slice ).max() < tolerance
20
import argparse import requests import torch from PIL import Image from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: if "cls_token" in name: lowercase : List[Any] = name.replace("""cls_token""" , """vit.embeddings.cls_token""" ) if "mask_token" in name: lowercase : Any = name.replace("""mask_token""" , """decoder.mask_token""" ) if "decoder_pos_embed" in name: lowercase : str = name.replace("""decoder_pos_embed""" , """decoder.decoder_pos_embed""" ) if "pos_embed" in name and "decoder" not in name: lowercase : List[str] = name.replace("""pos_embed""" , """vit.embeddings.position_embeddings""" ) if "patch_embed.proj" in name: lowercase : Tuple = name.replace("""patch_embed.proj""" , """vit.embeddings.patch_embeddings.projection""" ) if "patch_embed.norm" in name: lowercase : int = name.replace("""patch_embed.norm""" , """vit.embeddings.norm""" ) if "decoder_blocks" in name: lowercase : Tuple = name.replace("""decoder_blocks""" , """decoder.decoder_layers""" ) if "blocks" in name: lowercase : List[Any] = name.replace("""blocks""" , """vit.encoder.layer""" ) if "attn.proj" in name: lowercase : List[str] = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name: lowercase : Union[str, Any] = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: lowercase : Optional[Any] = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: lowercase : Union[str, Any] = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: lowercase : Dict = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: lowercase : Dict = name.replace("""mlp.fc2""" , """output.dense""" ) if "decoder_embed" in name: lowercase : List[str] = name.replace("""decoder_embed""" , """decoder.decoder_embed""" ) if "decoder_norm" in name: lowercase : Dict = name.replace("""decoder_norm""" , """decoder.decoder_norm""" ) if "decoder_pred" in name: lowercase : List[str] = name.replace("""decoder_pred""" , """decoder.decoder_pred""" ) if "norm.weight" in name and "decoder" not in name: lowercase : Tuple = name.replace("""norm.weight""" , """vit.layernorm.weight""" ) if "norm.bias" in name and "decoder" not in name: lowercase : int = name.replace("""norm.bias""" , """vit.layernorm.bias""" ) return name def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: for key in orig_state_dict.copy().keys(): lowercase : List[Any] = orig_state_dict.pop(SCREAMING_SNAKE_CASE__ ) if "qkv" in key: lowercase : int = key.split(""".""" ) lowercase : List[str] = int(key_split[1] ) if "decoder_blocks" in key: lowercase : Tuple = config.decoder_hidden_size lowercase : int = """decoder.decoder_layers.""" if "weight" in key: lowercase : List[Any] = val[:dim, :] lowercase : Tuple = val[dim : dim * 2, :] lowercase : List[Any] = val[-dim:, :] elif "bias" in key: lowercase : str = val[:dim] lowercase : Dict = val[dim : dim * 2] lowercase : Union[str, Any] = val[-dim:] else: lowercase : Tuple = config.hidden_size lowercase : Union[str, Any] = """vit.encoder.layer.""" if "weight" in key: lowercase : Tuple = val[:dim, :] lowercase : List[str] = val[dim : dim * 2, :] lowercase : Dict = val[-dim:, :] elif "bias" in key: lowercase : Any = val[:dim] lowercase : str = val[dim : dim * 2] lowercase : Union[str, Any] = val[-dim:] else: lowercase : Union[str, Any] = val return orig_state_dict def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> int: lowercase : int = ViTMAEConfig() if "large" in checkpoint_url: lowercase : Dict = 1_024 lowercase : str = 4_096 lowercase : Optional[Any] = 24 lowercase : Optional[Any] = 16 elif "huge" in checkpoint_url: lowercase : int = 14 lowercase : List[Any] = 1_280 lowercase : int = 5_120 lowercase : List[Any] = 32 lowercase : Any = 16 lowercase : List[str] = ViTMAEForPreTraining(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE__ , map_location="""cpu""" )["""model"""] lowercase : Tuple = ViTMAEImageProcessor(size=config.image_size ) lowercase : Optional[int] = convert_state_dict(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) model.load_state_dict(SCREAMING_SNAKE_CASE__ ) model.eval() lowercase : Union[str, Any] = """https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg""" lowercase : Union[str, Any] = Image.open(requests.get(SCREAMING_SNAKE_CASE__ , stream=SCREAMING_SNAKE_CASE__ ).raw ) lowercase : Optional[Any] = ViTMAEImageProcessor(size=config.image_size ) lowercase : List[Any] = image_processor(images=SCREAMING_SNAKE_CASE__ , return_tensors="""pt""" ) # forward pass torch.manual_seed(2 ) lowercase : int = model(**SCREAMING_SNAKE_CASE__ ) lowercase : str = outputs.logits if "large" in checkpoint_url: lowercase : List[Any] = torch.tensor( [[-0.7309, -0.7128, -1.0169], [-1.0161, -0.9058, -1.1878], [-1.0478, -0.9411, -1.1911]] ) elif "huge" in checkpoint_url: lowercase : Tuple = torch.tensor( [[-1.1599, -0.9199, -1.2221], [-1.1952, -0.9269, -1.2307], [-1.2143, -0.9337, -1.2262]] ) else: lowercase : List[str] = torch.tensor( [[-0.9192, -0.8481, -1.1259], [-1.1349, -1.0034, -1.2599], [-1.1757, -1.0429, -1.2726]] ) # verify logits assert torch.allclose(logits[0, :3, :3] , SCREAMING_SNAKE_CASE__ , atol=1e-4 ) print(f"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) print(f"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": lowercase : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--checkpoint_url""", default="""https://dl.fbaipublicfiles.com/mae/visualize/mae_visualize_vit_base.pth""", type=str, help="""URL of the checkpoint you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) lowercase : List[Any] = parser.parse_args() convert_vit_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
20
1
def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> int: return abs(SCREAMING_SNAKE_CASE__ ) if a == 0 else greatest_common_divisor(b % a , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> int: while y: # --> when y=0 then loop will terminate and return x as final GCD. lowercase , lowercase : int = y, x % y return abs(SCREAMING_SNAKE_CASE__ ) def _snake_case( ) -> List[str]: try: lowercase : List[Any] = input("""Enter two integers separated by comma (,): """ ).split(""",""" ) lowercase : Dict = int(nums[0] ) lowercase : Tuple = int(nums[1] ) print( f"greatest_common_divisor({num_a}, {num_a}) = " f"{greatest_common_divisor(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )}" ) print(f"By iterative gcd({num_a}, {num_a}) = {gcd_by_iterative(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )}" ) except (IndexError, UnboundLocalError, ValueError): print("""Wrong input""" ) if __name__ == "__main__": main()
20
import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=0.999 , SCREAMING_SNAKE_CASE__="cosine" , ) -> List[Any]: if alpha_transform_type == "cosine": def alpha_bar_fn(SCREAMING_SNAKE_CASE__ ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(SCREAMING_SNAKE_CASE__ ): return math.exp(t * -12.0 ) else: raise ValueError(f"Unsupported alpha_tranform_type: {alpha_transform_type}" ) lowercase : int = [] for i in range(SCREAMING_SNAKE_CASE__ ): lowercase : Dict = i / num_diffusion_timesteps lowercase : Optional[int] = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(SCREAMING_SNAKE_CASE__ ) / alpha_bar_fn(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) ) return torch.tensor(SCREAMING_SNAKE_CASE__ , dtype=torch.floataa ) class __snake_case ( lowerCAmelCase , lowerCAmelCase ): _a : Tuple= [e.name for e in KarrasDiffusionSchedulers] _a : int= 2 @register_to_config def __init__( self ,snake_case = 1000 ,snake_case = 0.00_085 ,snake_case = 0.012 ,snake_case = "linear" ,snake_case = None ,snake_case = "epsilon" ,snake_case = False ,snake_case = False ,snake_case = 1.0 ,snake_case = "linspace" ,snake_case = 0 ,): '''simple docstring''' if trained_betas is not None: lowercase : List[str] = torch.tensor(snake_case ,dtype=torch.floataa ) elif beta_schedule == "linear": lowercase : Optional[Any] = torch.linspace(snake_case ,snake_case ,snake_case ,dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. lowercase : int = ( torch.linspace(beta_start**0.5 ,beta_end**0.5 ,snake_case ,dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule lowercase : Union[str, Any] = betas_for_alpha_bar(snake_case ,alpha_transform_type="""cosine""" ) elif beta_schedule == "exp": lowercase : int = betas_for_alpha_bar(snake_case ,alpha_transform_type="""exp""" ) else: raise NotImplementedError(f"{beta_schedule} does is not implemented for {self.__class__}" ) lowercase : Any = 1.0 - self.betas lowercase : Dict = torch.cumprod(self.alphas ,dim=0 ) # set all values self.set_timesteps(snake_case ,snake_case ,snake_case ) lowercase : Tuple = use_karras_sigmas def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ): '''simple docstring''' if schedule_timesteps is None: lowercase : Union[str, Any] = self.timesteps lowercase : Dict = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: lowercase : Dict = 1 if len(snake_case ) > 1 else 0 else: lowercase : Union[str, Any] = timestep.cpu().item() if torch.is_tensor(snake_case ) else timestep lowercase : str = self._index_counter[timestep_int] return indices[pos].item() @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Optional[Any] = self.index_for_timestep(snake_case ) lowercase : Dict = self.sigmas[step_index] lowercase : List[str] = sample / ((sigma**2 + 1) ** 0.5) return sample def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = None ,snake_case = None ,): '''simple docstring''' lowercase : Any = num_inference_steps lowercase : List[Any] = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": lowercase : Optional[int] = np.linspace(0 ,num_train_timesteps - 1 ,snake_case ,dtype=snake_case )[::-1].copy() elif self.config.timestep_spacing == "leading": lowercase : int = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowercase : List[str] = (np.arange(0 ,snake_case ) * step_ratio).round()[::-1].copy().astype(snake_case ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": lowercase : List[str] = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowercase : Optional[int] = (np.arange(snake_case ,0 ,-step_ratio )).round().copy().astype(snake_case ) timesteps -= 1 else: raise ValueError( f"{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'." ) lowercase : Optional[int] = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) lowercase : Dict = np.log(snake_case ) lowercase : Union[str, Any] = np.interp(snake_case ,np.arange(0 ,len(snake_case ) ) ,snake_case ) if self.config.use_karras_sigmas: lowercase : List[Any] = self._convert_to_karras(in_sigmas=snake_case ,num_inference_steps=self.num_inference_steps ) lowercase : Tuple = np.array([self._sigma_to_t(snake_case ,snake_case ) for sigma in sigmas] ) lowercase : Any = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) lowercase : List[Any] = torch.from_numpy(snake_case ).to(device=snake_case ) lowercase : List[Any] = torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2 ), sigmas[-1:]] ) lowercase : Dict = torch.from_numpy(snake_case ) lowercase : List[Any] = torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2 )] ) if str(snake_case ).startswith("""mps""" ): # mps does not support float64 lowercase : Any = timesteps.to(snake_case ,dtype=torch.floataa ) else: lowercase : str = timesteps.to(device=snake_case ) # empty dt and derivative lowercase : Union[str, Any] = None lowercase : Any = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter lowercase : str = defaultdict(snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : Optional[int] = np.log(snake_case ) # get distribution lowercase : Union[str, Any] = log_sigma - log_sigmas[:, np.newaxis] # get sigmas range lowercase : Optional[int] = np.cumsum((dists >= 0) ,axis=0 ).argmax(axis=0 ).clip(max=log_sigmas.shape[0] - 2 ) lowercase : Any = low_idx + 1 lowercase : str = log_sigmas[low_idx] lowercase : Dict = log_sigmas[high_idx] # interpolate sigmas lowercase : int = (low - log_sigma) / (low - high) lowercase : Dict = np.clip(snake_case ,0 ,1 ) # transform interpolation to time range lowercase : Optional[Any] = (1 - w) * low_idx + w * high_idx lowercase : Tuple = t.reshape(sigma.shape ) return t def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : float = in_sigmas[-1].item() lowercase : float = in_sigmas[0].item() lowercase : Dict = 7.0 # 7.0 is the value used in the paper lowercase : Optional[int] = np.linspace(0 ,1 ,snake_case ) lowercase : int = sigma_min ** (1 / rho) lowercase : Any = sigma_max ** (1 / rho) lowercase : Tuple = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho return sigmas @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return self.dt is None def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case = True ,): '''simple docstring''' lowercase : Union[str, Any] = self.index_for_timestep(snake_case ) # advance index counter by 1 lowercase : Optional[int] = timestep.cpu().item() if torch.is_tensor(snake_case ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: lowercase : str = self.sigmas[step_index] lowercase : Optional[int] = self.sigmas[step_index + 1] else: # 2nd order / Heun's method lowercase : Dict = self.sigmas[step_index - 1] lowercase : Optional[Any] = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API lowercase : Union[str, Any] = 0 lowercase : Any = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": lowercase : Any = sigma_hat if self.state_in_first_order else sigma_next lowercase : int = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": lowercase : Union[str, Any] = sigma_hat if self.state_in_first_order else sigma_next lowercase : Optional[Any] = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": lowercase : Optional[Any] = model_output else: raise ValueError( f"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`" ) if self.config.clip_sample: lowercase : str = pred_original_sample.clamp( -self.config.clip_sample_range ,self.config.clip_sample_range ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order lowercase : Optional[int] = (sample - pred_original_sample) / sigma_hat # 3. delta timestep lowercase : Union[str, Any] = sigma_next - sigma_hat # store for 2nd order step lowercase : Optional[int] = derivative lowercase : Union[str, Any] = dt lowercase : Union[str, Any] = sample else: # 2. 2nd order / Heun's method lowercase : Tuple = (sample - pred_original_sample) / sigma_next lowercase : Dict = (self.prev_derivative + derivative) / 2 # 3. take prev timestep & sample lowercase : Tuple = self.dt lowercase : Optional[Any] = self.sample # free dt and derivative # Note, this puts the scheduler in "first order mode" lowercase : List[str] = None lowercase : Tuple = None lowercase : Dict = None lowercase : List[Any] = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Optional[int] = self.sigmas.to(device=original_samples.device ,dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(snake_case ): # mps does not support float64 lowercase : List[Any] = self.timesteps.to(original_samples.device ,dtype=torch.floataa ) lowercase : List[str] = timesteps.to(original_samples.device ,dtype=torch.floataa ) else: lowercase : List[str] = self.timesteps.to(original_samples.device ) lowercase : Tuple = timesteps.to(original_samples.device ) lowercase : Any = [self.index_for_timestep(snake_case ,snake_case ) for t in timesteps] lowercase : int = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): lowercase : Any = sigma.unsqueeze(-1 ) lowercase : Optional[int] = original_samples + noise * sigma return noisy_samples def __len__( self ): '''simple docstring''' return self.config.num_train_timesteps
20
1
from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase : Dict = logging.get_logger(__name__) lowercase : Optional[int] = { """alibaba-damo/mgp-str-base""": """https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json""", } class __snake_case ( lowerCAmelCase ): _a : str= "mgp-str" def __init__( self ,snake_case=[32, 128] ,snake_case=4 ,snake_case=3 ,snake_case=27 ,snake_case=38 ,snake_case=50257 ,snake_case=30522 ,snake_case=768 ,snake_case=12 ,snake_case=12 ,snake_case=4.0 ,snake_case=True ,snake_case=False ,snake_case=1e-5 ,snake_case=0.0 ,snake_case=0.0 ,snake_case=0.0 ,snake_case=False ,snake_case=0.02 ,**snake_case ,): '''simple docstring''' super().__init__(**snake_case ) lowercase : Optional[int] = image_size lowercase : Any = patch_size lowercase : Any = num_channels lowercase : Tuple = max_token_length lowercase : Any = num_character_labels lowercase : int = num_bpe_labels lowercase : Union[str, Any] = num_wordpiece_labels lowercase : Any = hidden_size lowercase : Union[str, Any] = num_hidden_layers lowercase : Union[str, Any] = num_attention_heads lowercase : Tuple = mlp_ratio lowercase : Tuple = distilled lowercase : Any = layer_norm_eps lowercase : Any = drop_rate lowercase : Union[str, Any] = qkv_bias lowercase : Dict = attn_drop_rate lowercase : Union[str, Any] = drop_path_rate lowercase : Optional[Any] = output_aa_attentions lowercase : str = initializer_range
20
from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch lowercase : List[str] = logging.get_logger(__name__) @add_end_docstrings( lowerCAmelCase , R"\n top_k (`int`, defaults to 5):\n The number of predictions to return.\n targets (`str` or `List[str]`, *optional*):\n When passed, the model will limit the scores to the passed targets instead of looking up in the whole\n vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting\n token will be used (with a warning, and that might be slower).\n\n " , ) class __snake_case ( lowerCAmelCase ): def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' if self.framework == "tf": lowercase : str = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": lowercase : Optional[int] = torch.nonzero(input_ids == self.tokenizer.mask_token_id ,as_tuple=snake_case ) else: raise ValueError("""Unsupported framework""" ) return masked_index def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Tuple = self.get_masked_index(snake_case ) lowercase : Dict = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( """fill-mask""" ,self.model.base_model_prefix ,f"No mask_token ({self.tokenizer.mask_token}) found on the input" ,) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' if isinstance(snake_case ,snake_case ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input["""input_ids"""][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ,**snake_case ): '''simple docstring''' if return_tensors is None: lowercase : int = self.framework lowercase : Optional[Any] = self.tokenizer(snake_case ,return_tensors=snake_case ) self.ensure_exactly_one_mask_token(snake_case ) return model_inputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Optional[int] = self.model(**snake_case ) lowercase : Tuple = model_inputs["""input_ids"""] return model_outputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=5 ,snake_case=None ): '''simple docstring''' if target_ids is not None and target_ids.shape[0] < top_k: lowercase : str = target_ids.shape[0] lowercase : Optional[Any] = model_outputs["""input_ids"""][0] lowercase : List[str] = model_outputs["""logits"""] if self.framework == "tf": lowercase : List[str] = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] lowercase : Tuple = outputs.numpy() lowercase : Tuple = outputs[0, masked_index, :] lowercase : Any = stable_softmax(snake_case ,axis=-1 ) if target_ids is not None: lowercase : Union[str, Any] = tf.gather_nd(tf.squeeze(snake_case ,0 ) ,target_ids.reshape(-1 ,1 ) ) lowercase : int = tf.expand_dims(snake_case ,0 ) lowercase : Tuple = tf.math.top_k(snake_case ,k=snake_case ) lowercase , lowercase : int = topk.values.numpy(), topk.indices.numpy() else: lowercase : Optional[Any] = torch.nonzero(input_ids == self.tokenizer.mask_token_id ,as_tuple=snake_case ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample lowercase : Union[str, Any] = outputs[0, masked_index, :] lowercase : Tuple = logits.softmax(dim=-1 ) if target_ids is not None: lowercase : List[str] = probs[..., target_ids] lowercase , lowercase : Union[str, Any] = probs.topk(snake_case ) lowercase : Any = [] lowercase : List[Any] = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() ,predictions.tolist() ) ): lowercase : Dict = [] for v, p in zip(_values ,_predictions ): # Copy is important since we're going to modify this array in place lowercase : Dict = input_ids.numpy().copy() if target_ids is not None: lowercase : Union[str, Any] = target_ids[p].tolist() lowercase : Tuple = p # Filter padding out: lowercase : List[str] = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back lowercase : Tuple = self.tokenizer.decode(snake_case ,skip_special_tokens=snake_case ) lowercase : Optional[Any] = {"""score""": v, """token""": p, """token_str""": self.tokenizer.decode([p] ), """sequence""": sequence} row.append(snake_case ) result.append(snake_case ) if single_mask: return result[0] return result def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ): '''simple docstring''' if isinstance(snake_case ,snake_case ): lowercase : List[Any] = [targets] try: lowercase : List[str] = self.tokenizer.get_vocab() except Exception: lowercase : Any = {} lowercase : Dict = [] for target in targets: lowercase : Dict = vocab.get(snake_case ,snake_case ) if id_ is None: lowercase : Optional[int] = self.tokenizer( snake_case ,add_special_tokens=snake_case ,return_attention_mask=snake_case ,return_token_type_ids=snake_case ,max_length=1 ,truncation=snake_case ,)["""input_ids"""] if len(snake_case ) == 0: logger.warning( f"The specified target token `{target}` does not exist in the model vocabulary. " """We cannot replace it with anything meaningful, ignoring it""" ) continue lowercase : Union[str, Any] = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( f"The specified target token `{target}` does not exist in the model vocabulary. " f"Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`." ) target_ids.append(id_ ) lowercase : Optional[Any] = list(set(snake_case ) ) if len(snake_case ) == 0: raise ValueError("""At least one target must be provided when passed.""" ) lowercase : Optional[Any] = np.array(snake_case ) return target_ids def _SCREAMING_SNAKE_CASE ( self ,snake_case=None ,snake_case=None ): '''simple docstring''' lowercase : Dict = {} if targets is not None: lowercase : str = self.get_target_ids(snake_case ,snake_case ) lowercase : List[Any] = target_ids if top_k is not None: lowercase : List[str] = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( """fill-mask""" ,self.model.base_model_prefix ,"""The tokenizer does not define a `mask_token`.""" ) return {}, {}, postprocess_params def __call__( self ,snake_case ,*snake_case ,**snake_case ): '''simple docstring''' lowercase : Tuple = super().__call__(snake_case ,**snake_case ) if isinstance(snake_case ,snake_case ) and len(snake_case ) == 1: return outputs[0] return outputs
20
1
from __future__ import annotations def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: # noqa: E741 while r - l > 1: lowercase : Dict = (l + r) // 2 if v[m] >= key: lowercase : Any = m else: lowercase : Optional[Any] = m # noqa: E741 return r def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: if len(SCREAMING_SNAKE_CASE__ ) == 0: return 0 lowercase : str = [0] * len(SCREAMING_SNAKE_CASE__ ) lowercase : Dict = 1 lowercase : int = v[0] for i in range(1 , len(SCREAMING_SNAKE_CASE__ ) ): if v[i] < tail[0]: lowercase : List[Any] = v[i] elif v[i] > tail[length - 1]: lowercase : str = v[i] length += 1 else: lowercase : List[Any] = v[i] return length if __name__ == "__main__": import doctest doctest.testmod()
20
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class __snake_case ( unittest.TestCase ): def __init__( self ,snake_case ,snake_case=7 ,snake_case=3 ,snake_case=18 ,snake_case=30 ,snake_case=400 ,snake_case=True ,snake_case=None ,snake_case=True ,snake_case=None ,): '''simple docstring''' lowercase : Dict = size if size is not None else {"""shortest_edge""": 20} lowercase : Union[str, Any] = crop_size if crop_size is not None else {"""height""": 18, """width""": 18} lowercase : str = parent lowercase : int = batch_size lowercase : str = num_channels lowercase : int = image_size lowercase : List[str] = min_resolution lowercase : str = max_resolution lowercase : Dict = do_resize lowercase : Dict = size lowercase : Dict = do_center_crop lowercase : str = crop_size def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, } @require_torch @require_vision class __snake_case ( lowerCAmelCase , unittest.TestCase ): _a : Any= MobileNetVaImageProcessor if is_vision_available() else None def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = MobileNetVaImageProcessingTester(self ) @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[int] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case ,"""do_resize""" ) ) self.assertTrue(hasattr(snake_case ,"""size""" ) ) self.assertTrue(hasattr(snake_case ,"""do_center_crop""" ) ) self.assertTrue(hasattr(snake_case ,"""crop_size""" ) ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : int = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{"""shortest_edge""": 20} ) self.assertEqual(image_processor.crop_size ,{"""height""": 18, """width""": 18} ) lowercase : int = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ,crop_size=84 ) self.assertEqual(image_processor.size ,{"""shortest_edge""": 42} ) self.assertEqual(image_processor.crop_size ,{"""height""": 84, """width""": 84} ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' pass def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowercase : str = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case ,Image.Image ) # Test not batched input lowercase : Dict = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) # Test batched lowercase : Tuple = image_processing(snake_case ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowercase : Union[str, Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case ,numpify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case ,np.ndarray ) # Test not batched input lowercase : Optional[Any] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) # Test batched lowercase : List[str] = image_processing(snake_case ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowercase : Tuple = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case ,torchify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case ,torch.Tensor ) # Test not batched input lowercase : Optional[int] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) # Test batched lowercase : List[str] = image_processing(snake_case ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,)
20
1
import inspect from typing import Optional, Union import numpy as np import PIL import torch from torch.nn import functional as F from torchvision import transforms from transformers import CLIPFeatureExtractor, CLIPModel, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, DPMSolverMultistepScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.utils import ( PIL_INTERPOLATION, randn_tensor, ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: if isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): return image elif isinstance(SCREAMING_SNAKE_CASE__ , PIL.Image.Image ): lowercase : Tuple = [image] if isinstance(image[0] , PIL.Image.Image ): lowercase : List[str] = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION["""lanczos"""] ) )[None, :] for i in image] lowercase : Optional[Any] = np.concatenate(SCREAMING_SNAKE_CASE__ , axis=0 ) lowercase : Optional[int] = np.array(SCREAMING_SNAKE_CASE__ ).astype(np.floataa ) / 255.0 lowercase : Tuple = image.transpose(0 , 3 , 1 , 2 ) lowercase : Dict = 2.0 * image - 1.0 lowercase : Optional[Any] = torch.from_numpy(SCREAMING_SNAKE_CASE__ ) elif isinstance(image[0] , torch.Tensor ): lowercase : Any = torch.cat(SCREAMING_SNAKE_CASE__ , dim=0 ) return image def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=0.9995 ) -> Any: if not isinstance(SCREAMING_SNAKE_CASE__ , np.ndarray ): lowercase : Optional[Any] = True lowercase : Any = va.device lowercase : Tuple = va.cpu().numpy() lowercase : Dict = va.cpu().numpy() lowercase : Any = np.sum(va * va / (np.linalg.norm(SCREAMING_SNAKE_CASE__ ) * np.linalg.norm(SCREAMING_SNAKE_CASE__ )) ) if np.abs(SCREAMING_SNAKE_CASE__ ) > DOT_THRESHOLD: lowercase : Any = (1 - t) * va + t * va else: lowercase : int = np.arccos(SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = np.sin(SCREAMING_SNAKE_CASE__ ) lowercase : str = theta_a * t lowercase : List[Any] = np.sin(SCREAMING_SNAKE_CASE__ ) lowercase : int = np.sin(theta_a - theta_t ) / sin_theta_a lowercase : int = sin_theta_t / sin_theta_a lowercase : Dict = sa * va + sa * va if inputs_are_torch: lowercase : Optional[int] = torch.from_numpy(SCREAMING_SNAKE_CASE__ ).to(SCREAMING_SNAKE_CASE__ ) return va def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> int: lowercase : Union[str, Any] = F.normalize(SCREAMING_SNAKE_CASE__ , dim=-1 ) lowercase : List[Any] = F.normalize(SCREAMING_SNAKE_CASE__ , dim=-1 ) return (x - y).norm(dim=-1 ).div(2 ).arcsin().pow(2 ).mul(2 ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: for param in model.parameters(): lowercase : List[str] = value class __snake_case ( lowerCAmelCase ): def __init__( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case=None ,snake_case=None ,snake_case=None ,): '''simple docstring''' super().__init__() self.register_modules( vae=snake_case ,text_encoder=snake_case ,clip_model=snake_case ,tokenizer=snake_case ,unet=snake_case ,scheduler=snake_case ,feature_extractor=snake_case ,coca_model=snake_case ,coca_tokenizer=snake_case ,coca_transform=snake_case ,) lowercase : Optional[int] = ( feature_extractor.size if isinstance(feature_extractor.size ,snake_case ) else feature_extractor.size["""shortest_edge"""] ) lowercase : Dict = transforms.Normalize(mean=feature_extractor.image_mean ,std=feature_extractor.image_std ) set_requires_grad(self.text_encoder ,snake_case ) set_requires_grad(self.clip_model ,snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case = "auto" ): '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory lowercase : List[Any] = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' self.enable_attention_slicing(snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' set_requires_grad(self.vae ,snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' set_requires_grad(self.vae ,snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' set_requires_grad(self.unet ,snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' set_requires_grad(self.unet ,snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ): '''simple docstring''' lowercase : Optional[int] = min(int(num_inference_steps * strength ) ,snake_case ) lowercase : List[Any] = max(num_inference_steps - init_timestep ,0 ) lowercase : int = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case=None ): '''simple docstring''' if not isinstance(snake_case ,torch.Tensor ): raise ValueError(f"`image` has to be of type `torch.Tensor` but is {type(snake_case )}" ) lowercase : List[str] = image.to(device=snake_case ,dtype=snake_case ) if isinstance(snake_case ,snake_case ): lowercase : Optional[int] = [ self.vae.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(snake_case ) ] lowercase : Tuple = torch.cat(snake_case ,dim=0 ) else: lowercase : List[str] = self.vae.encode(snake_case ).latent_dist.sample(snake_case ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor lowercase : Any = 0.18_215 * init_latents lowercase : Dict = init_latents.repeat_interleave(snake_case ,dim=0 ) lowercase : List[str] = randn_tensor(init_latents.shape ,generator=snake_case ,device=snake_case ,dtype=snake_case ) # get latents lowercase : Optional[int] = self.scheduler.add_noise(snake_case ,snake_case ,snake_case ) lowercase : Optional[Any] = init_latents return latents def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Dict = self.coca_transform(snake_case ).unsqueeze(0 ) with torch.no_grad(), torch.cuda.amp.autocast(): lowercase : List[Any] = self.coca_model.generate(transformed_image.to(device=self.device ,dtype=self.coca_model.dtype ) ) lowercase : int = self.coca_tokenizer.decode(generated[0].cpu().numpy() ) return generated.split("""<end_of_text>""" )[0].replace("""<start_of_text>""" ,"""""" ).rstrip(""" .,""" ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : Optional[int] = self.feature_extractor.preprocess(snake_case ) lowercase : Optional[int] = torch.from_numpy(clip_image_input["""pixel_values"""][0] ).unsqueeze(0 ).to(self.device ).half() lowercase : List[Any] = self.clip_model.get_image_features(snake_case ) lowercase : List[str] = image_embeddings_clip / image_embeddings_clip.norm(p=2 ,dim=-1 ,keepdim=snake_case ) lowercase : Tuple = image_embeddings_clip.repeat_interleave(snake_case ,dim=0 ) return image_embeddings_clip @torch.enable_grad() def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Optional[int] = latents.detach().requires_grad_() lowercase : Optional[int] = self.scheduler.scale_model_input(snake_case ,snake_case ) # predict the noise residual lowercase : Optional[int] = self.unet(snake_case ,snake_case ,encoder_hidden_states=snake_case ).sample if isinstance(self.scheduler ,(PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler) ): lowercase : Optional[int] = self.scheduler.alphas_cumprod[timestep] lowercase : Union[str, Any] = 1 - alpha_prod_t # compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf lowercase : Tuple = (latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5 lowercase : int = torch.sqrt(snake_case ) lowercase : Any = pred_original_sample * (fac) + latents * (1 - fac) elif isinstance(self.scheduler ,snake_case ): lowercase : Dict = self.scheduler.sigmas[index] lowercase : Tuple = latents - sigma * noise_pred else: raise ValueError(f"scheduler type {type(self.scheduler )} not supported" ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor lowercase : Optional[Any] = 1 / 0.18_215 * sample lowercase : Union[str, Any] = self.vae.decode(snake_case ).sample lowercase : str = (image / 2 + 0.5).clamp(0 ,1 ) lowercase : int = transforms.Resize(self.feature_extractor_size )(snake_case ) lowercase : Tuple = self.normalize(snake_case ).to(latents.dtype ) lowercase : Tuple = self.clip_model.get_image_features(snake_case ) lowercase : List[str] = image_embeddings_clip / image_embeddings_clip.norm(p=2 ,dim=-1 ,keepdim=snake_case ) lowercase : str = spherical_dist_loss(snake_case ,snake_case ).mean() * clip_guidance_scale lowercase : List[Any] = -torch.autograd.grad(snake_case ,snake_case )[0] if isinstance(self.scheduler ,snake_case ): lowercase : Any = latents.detach() + grads * (sigma**2) lowercase : Optional[Any] = noise_pred_original else: lowercase : int = noise_pred_original - torch.sqrt(snake_case ) * grads return noise_pred, latents @torch.no_grad() def __call__( self ,snake_case ,snake_case ,snake_case = None ,snake_case = None ,snake_case = 512 ,snake_case = 512 ,snake_case = 0.6 ,snake_case = 50 ,snake_case = 7.5 ,snake_case = 1 ,snake_case = 0.0 ,snake_case = 100 ,snake_case = None ,snake_case = "pil" ,snake_case = True ,snake_case = 0.8 ,snake_case = 0.1 ,snake_case = 0.1 ,): '''simple docstring''' if isinstance(snake_case ,snake_case ) and len(snake_case ) != batch_size: raise ValueError(f"You have passed {batch_size} batch_size, but only {len(snake_case )} generators." ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f"`height` and `width` have to be divisible by 8 but are {height} and {width}." ) if isinstance(snake_case ,torch.Generator ) and batch_size > 1: lowercase : str = [generator] + [None] * (batch_size - 1) lowercase : Union[str, Any] = [ ("""model""", self.coca_model is None), ("""tokenizer""", self.coca_tokenizer is None), ("""transform""", self.coca_transform is None), ] lowercase : Optional[int] = [x[0] for x in coca_is_none if x[1]] lowercase : int = """, """.join(snake_case ) # generate prompts with coca model if prompt is None if content_prompt is None: if len(snake_case ): raise ValueError( f"Content prompt is None and CoCa [{coca_is_none_str}] is None." f"Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline." ) lowercase : Optional[int] = self.get_image_description(snake_case ) if style_prompt is None: if len(snake_case ): raise ValueError( f"Style prompt is None and CoCa [{coca_is_none_str}] is None." f" Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline." ) lowercase : str = self.get_image_description(snake_case ) # get prompt text embeddings for content and style lowercase : List[Any] = self.tokenizer( snake_case ,padding="""max_length""" ,max_length=self.tokenizer.model_max_length ,truncation=snake_case ,return_tensors="""pt""" ,) lowercase : List[Any] = self.text_encoder(content_text_input.input_ids.to(self.device ) )[0] lowercase : Optional[int] = self.tokenizer( snake_case ,padding="""max_length""" ,max_length=self.tokenizer.model_max_length ,truncation=snake_case ,return_tensors="""pt""" ,) lowercase : Tuple = self.text_encoder(style_text_input.input_ids.to(self.device ) )[0] lowercase : Optional[Any] = slerp(snake_case ,snake_case ,snake_case ) # duplicate text embeddings for each generation per prompt lowercase : str = text_embeddings.repeat_interleave(snake_case ,dim=0 ) # set timesteps lowercase : str = """offset""" in set(inspect.signature(self.scheduler.set_timesteps ).parameters.keys() ) lowercase : Tuple = {} if accepts_offset: lowercase : int = 1 self.scheduler.set_timesteps(snake_case ,**snake_case ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand self.scheduler.timesteps.to(self.device ) lowercase , lowercase : Optional[int] = self.get_timesteps(snake_case ,snake_case ,self.device ) lowercase : Tuple = timesteps[:1].repeat(snake_case ) # Preprocess image lowercase : str = preprocess(snake_case ,snake_case ,snake_case ) lowercase : int = self.prepare_latents( snake_case ,snake_case ,snake_case ,text_embeddings.dtype ,self.device ,snake_case ) lowercase : List[Any] = preprocess(snake_case ,snake_case ,snake_case ) lowercase : Tuple = self.prepare_latents( snake_case ,snake_case ,snake_case ,text_embeddings.dtype ,self.device ,snake_case ) lowercase : List[str] = slerp(snake_case ,snake_case ,snake_case ) if clip_guidance_scale > 0: lowercase : Union[str, Any] = self.get_clip_image_embeddings(snake_case ,snake_case ) lowercase : Optional[int] = self.get_clip_image_embeddings(snake_case ,snake_case ) lowercase : Optional[int] = slerp( snake_case ,snake_case ,snake_case ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. lowercase : str = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: lowercase : List[str] = content_text_input.input_ids.shape[-1] lowercase : Optional[Any] = self.tokenizer([""""""] ,padding="""max_length""" ,max_length=snake_case ,return_tensors="""pt""" ) lowercase : Any = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt lowercase : Any = uncond_embeddings.repeat_interleave(snake_case ,dim=0 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes lowercase : List[str] = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. lowercase : Tuple = (batch_size, self.unet.config.in_channels, height // 8, width // 8) lowercase : Tuple = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not work reproducibly on mps lowercase : str = torch.randn(snake_case ,generator=snake_case ,device="""cpu""" ,dtype=snake_case ).to( self.device ) else: lowercase : Optional[int] = torch.randn(snake_case ,generator=snake_case ,device=self.device ,dtype=snake_case ) else: if latents.shape != latents_shape: raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {latents_shape}" ) lowercase : Any = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler lowercase : Dict = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] lowercase : Union[str, Any] = """eta""" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) lowercase : str = {} if accepts_eta: lowercase : str = eta # check if the scheduler accepts generator lowercase : Optional[Any] = """generator""" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) if accepts_generator: lowercase : Tuple = generator with self.progress_bar(total=snake_case ): for i, t in enumerate(snake_case ): # expand the latents if we are doing classifier free guidance lowercase : Optional[Any] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowercase : Optional[int] = self.scheduler.scale_model_input(snake_case ,snake_case ) # predict the noise residual lowercase : Dict = self.unet(snake_case ,snake_case ,encoder_hidden_states=snake_case ).sample # perform classifier free guidance if do_classifier_free_guidance: lowercase , lowercase : str = noise_pred.chunk(2 ) lowercase : Optional[int] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # perform clip guidance if clip_guidance_scale > 0: lowercase : int = ( text_embeddings.chunk(2 )[1] if do_classifier_free_guidance else text_embeddings ) lowercase , lowercase : Union[str, Any] = self.cond_fn( snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,) # compute the previous noisy sample x_t -> x_t-1 lowercase : Any = self.scheduler.step(snake_case ,snake_case ,snake_case ,**snake_case ).prev_sample # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor lowercase : Optional[Any] = 1 / 0.18_215 * latents lowercase : Any = self.vae.decode(snake_case ).sample lowercase : Optional[Any] = (image / 2 + 0.5).clamp(0 ,1 ) lowercase : Optional[Any] = image.cpu().permute(0 ,2 ,3 ,1 ).numpy() if output_type == "pil": lowercase : List[str] = self.numpy_to_pil(snake_case ) if not return_dict: return (image, None) return StableDiffusionPipelineOutput(images=snake_case ,nsfw_content_detected=snake_case )
20
from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef import datasets lowercase : str = """\ @inproceedings{wang2019glue, title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding}, author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.}, note={In the Proceedings of ICLR.}, year={2019} } """ lowercase : Dict = """\ GLUE, the General Language Understanding Evaluation benchmark (https://gluebenchmark.com/) is a collection of resources for training, evaluating, and analyzing natural language understanding systems. """ lowercase : int = """ Compute GLUE evaluation metric associated to each GLUE dataset. Args: predictions: list of predictions to score. Each translation should be tokenized into a list of tokens. references: list of lists of references for each translation. Each reference should be tokenized into a list of tokens. Returns: depending on the GLUE subset, one or several of: \"accuracy\": Accuracy \"f1\": F1 score \"pearson\": Pearson Correlation \"spearmanr\": Spearman Correlation \"matthews_correlation\": Matthew Correlation Examples: >>> glue_metric = datasets.load_metric('glue', 'sst2') # 'sst2' or any of [\"mnli\", \"mnli_mismatched\", \"mnli_matched\", \"qnli\", \"rte\", \"wnli\", \"hans\"] >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0} >>> glue_metric = datasets.load_metric('glue', 'mrpc') # 'mrpc' or 'qqp' >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0, 'f1': 1.0} >>> glue_metric = datasets.load_metric('glue', 'stsb') >>> references = [0., 1., 2., 3., 4., 5.] >>> predictions = [0., 1., 2., 3., 4., 5.] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print({\"pearson\": round(results[\"pearson\"], 2), \"spearmanr\": round(results[\"spearmanr\"], 2)}) {'pearson': 1.0, 'spearmanr': 1.0} >>> glue_metric = datasets.load_metric('glue', 'cola') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'matthews_correlation': 1.0} """ def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[int]: return float((preds == labels).mean() ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: lowercase : Any = simple_accuracy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = float(fa_score(y_true=SCREAMING_SNAKE_CASE__ , y_pred=SCREAMING_SNAKE_CASE__ ) ) return { "accuracy": acc, "f1": fa, } def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: lowercase : Union[str, Any] = float(pearsonr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )[0] ) lowercase : Dict = float(spearmanr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )[0] ) return { "pearson": pearson_corr, "spearmanr": spearman_corr, } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __snake_case ( datasets.Metric ): def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self.config_name not in [ "sst2", "mnli", "mnli_mismatched", "mnli_matched", "cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans", ]: raise KeyError( """You should supply a configuration name selected in """ """[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", """ """\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]""" ) return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { """predictions""": datasets.Value("""int64""" if self.config_name != """stsb""" else """float32""" ), """references""": datasets.Value("""int64""" if self.config_name != """stsb""" else """float32""" ), } ) ,codebase_urls=[] ,reference_urls=[] ,format="""numpy""" ,) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' if self.config_name == "cola": return {"matthews_correlation": matthews_corrcoef(snake_case ,snake_case )} elif self.config_name == "stsb": return pearson_and_spearman(snake_case ,snake_case ) elif self.config_name in ["mrpc", "qqp"]: return acc_and_fa(snake_case ,snake_case ) elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]: return {"accuracy": simple_accuracy(snake_case ,snake_case )} else: raise KeyError( """You should supply a configuration name selected in """ """[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", """ """\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]""" )
20
1
from __future__ import annotations from cmath import sqrt def _a ( a :int , a :int , a :int ) -> tuple[complex, complex]: if a == 0: raise ValueError('''Coefficient \'a\' must not be zero.''' ) a = b * b - 4 * a * c a = (-b + sqrt(a )) / (2 * a) a = (-b - sqrt(a )) / (2 * a) return ( root_a.real if not root_a.imag else root_a, root_a.real if not root_a.imag else root_a, ) def _a ( ) -> Optional[Any]: a , a = quadratic_roots(a=5 , b=6 , c=1 ) print(F"""The solutions are: {solutiona} and {solutiona}""" ) if __name__ == "__main__": main()
0
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 __snake_case ( unittest.TestCase ): _a : Optional[int]= MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ): '''simple docstring''' lowercase : Any = hf_hub_download( repo_id="""nateraw/video-demo""" ,filename="""archery.mp4""" ,repo_type="""dataset""" ) lowercase : List[str] = VideoClassificationPipeline(model=snake_case ,image_processor=snake_case ,top_k=2 ) lowercase : Dict = [ example_video_filepath, """https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4""", ] return video_classifier, examples def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' for example in examples: lowercase : int = video_classifier(snake_case ) self.assertEqual( snake_case ,[ {"""score""": ANY(snake_case ), """label""": ANY(snake_case )}, {"""score""": ANY(snake_case ), """label""": ANY(snake_case )}, ] ,) @require_torch def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Any = """hf-internal-testing/tiny-random-VideoMAEForVideoClassification""" lowercase : str = VideoMAEFeatureExtractor( size={"""shortest_edge""": 10} ,crop_size={"""height""": 10, """width""": 10} ) lowercase : List[Any] = pipeline( """video-classification""" ,model=snake_case ,feature_extractor=snake_case ,frame_sampling_rate=4 ) lowercase : Dict = hf_hub_download(repo_id="""nateraw/video-demo""" ,filename="""archery.mp4""" ,repo_type="""dataset""" ) lowercase : Any = video_classifier(snake_case ,top_k=2 ) self.assertEqual( nested_simplify(snake_case ,decimals=4 ) ,[{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}] ,) lowercase : str = video_classifier( [ video_file_path, video_file_path, ] ,top_k=2 ,) self.assertEqual( nested_simplify(snake_case ,decimals=4 ) ,[ [{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}], [{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}], ] ,) @require_tf def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' pass
20
0
'''simple docstring''' import warnings from ...utils import logging from .image_processing_owlvit import OwlViTImageProcessor SCREAMING_SNAKE_CASE_: int =logging.get_logger(__name__) class __A ( UpperCamelCase__ ): def __init__(self : Any , *__a : Optional[Any] , **__a : List[str] ): warnings.warn( "The class OwlViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use OwlViTImageProcessor instead." , __a , ) super().__init__(*__a , **__a )
1
from __future__ import annotations from collections import namedtuple from dataclasses import dataclass @dataclass class __snake_case : _a : int _a : TreeNode | None= None _a : TreeNode | None= None lowercase : Dict = namedtuple("""CoinsDistribResult""", """moves excess""") def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: if root is None: return 0 # Validation def count_nodes(SCREAMING_SNAKE_CASE__ ) -> int: if node is None: return 0 return count_nodes(node.left ) + count_nodes(node.right ) + 1 def count_coins(SCREAMING_SNAKE_CASE__ ) -> int: if node is None: return 0 return count_coins(node.left ) + count_coins(node.right ) + node.data if count_nodes(SCREAMING_SNAKE_CASE__ ) != count_coins(SCREAMING_SNAKE_CASE__ ): raise ValueError("""The nodes number should be same as the number of coins""" ) # Main calculation def get_distrib(SCREAMING_SNAKE_CASE__ ) -> CoinsDistribResult: if node is None: return CoinsDistribResult(0 , 1 ) lowercase , lowercase : int = get_distrib(node.left ) lowercase , lowercase : List[Any] = get_distrib(node.right ) lowercase : Optional[Any] = 1 - left_distrib_excess lowercase : Union[str, Any] = 1 - right_distrib_excess lowercase : List[Any] = ( left_distrib_moves + right_distrib_moves + abs(SCREAMING_SNAKE_CASE__ ) + abs(SCREAMING_SNAKE_CASE__ ) ) lowercase : Any = node.data - coins_to_left - coins_to_right return CoinsDistribResult(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return get_distrib(SCREAMING_SNAKE_CASE__ )[0] if __name__ == "__main__": import doctest doctest.testmod()
20
0
'''simple docstring''' import argparse import OmegaConf import torch from diffusers import DDIMScheduler, LDMPipeline, UNetLDMModel, VQModel def _SCREAMING_SNAKE_CASE (A , A , A ) -> List[Any]: """simple docstring""" lowercase__ = OmegaConf.load(A ) lowercase__ = torch.load(A , map_location='''cpu''' )['''model'''] lowercase__ = list(state_dict.keys() ) # extract state_dict for VQVAE lowercase__ = {} lowercase__ = '''first_stage_model.''' for key in keys: if key.startswith(A ): lowercase__ = state_dict[key] # extract state_dict for UNetLDM lowercase__ = {} lowercase__ = '''model.diffusion_model.''' for key in keys: if key.startswith(A ): lowercase__ = state_dict[key] lowercase__ = config.model.params.first_stage_config.params lowercase__ = config.model.params.unet_config.params lowercase__ = VQModel(**A ).eval() vqvae.load_state_dict(A ) lowercase__ = UNetLDMModel(**A ).eval() unet.load_state_dict(A ) lowercase__ = DDIMScheduler( timesteps=config.model.params.timesteps , beta_schedule='''scaled_linear''' , beta_start=config.model.params.linear_start , beta_end=config.model.params.linear_end , clip_sample=A , ) lowercase__ = LDMPipeline(A , A , A ) pipeline.save_pretrained(A ) if __name__ == "__main__": lowerCamelCase : int = argparse.ArgumentParser() parser.add_argument('--checkpoint_path', type=str, required=True) parser.add_argument('--config_path', type=str, required=True) parser.add_argument('--output_path', type=str, required=True) lowerCamelCase : Dict = parser.parse_args() convert_ldm_original(args.checkpoint_path, args.config_path, args.output_path)
2
from collections import UserDict from typing import Union import numpy as np import requests from ..utils import ( add_end_docstrings, logging, ) from .audio_classification import ffmpeg_read from .base import PIPELINE_INIT_ARGS, Pipeline lowercase : Optional[Any] = logging.get_logger(__name__) @add_end_docstrings(lowerCAmelCase ) class __snake_case ( lowerCAmelCase ): def __init__( self ,**snake_case ): '''simple docstring''' super().__init__(**snake_case ) if self.framework != "pt": raise ValueError(f"The {self.__class__} is only available in PyTorch." ) # No specific FOR_XXX available yet def __call__( self ,snake_case ,**snake_case ): '''simple docstring''' return super().__call__(snake_case ,**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,**snake_case ): '''simple docstring''' lowercase : Union[str, Any] = {} if "candidate_labels" in kwargs: lowercase : List[str] = kwargs["""candidate_labels"""] if "hypothesis_template" in kwargs: lowercase : Dict = kwargs["""hypothesis_template"""] return preprocess_params, {}, {} def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ,snake_case="This is a sound of {}." ): '''simple docstring''' if isinstance(snake_case ,snake_case ): if audio.startswith("""http://""" ) or audio.startswith("""https://""" ): # We need to actually check for a real protocol, otherwise it's impossible to use a local file # like http_huggingface_co.png lowercase : Optional[Any] = requests.get(snake_case ).content else: with open(snake_case ,"""rb""" ) as f: lowercase : Union[str, Any] = f.read() if isinstance(snake_case ,snake_case ): lowercase : int = ffmpeg_read(snake_case ,self.feature_extractor.sampling_rate ) if not isinstance(snake_case ,np.ndarray ): raise ValueError("""We expect a numpy ndarray as input""" ) if len(audio.shape ) != 1: raise ValueError("""We expect a single channel audio input for ZeroShotAudioClassificationPipeline""" ) lowercase : Dict = self.feature_extractor( [audio] ,sampling_rate=self.feature_extractor.sampling_rate ,return_tensors="""pt""" ) lowercase : Tuple = candidate_labels lowercase : Tuple = [hypothesis_template.format(snake_case ) for x in candidate_labels] lowercase : Optional[Any] = self.tokenizer(snake_case ,return_tensors=self.framework ,padding=snake_case ) lowercase : Optional[Any] = [text_inputs] return inputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : List[str] = model_inputs.pop("""candidate_labels""" ) lowercase : Dict = model_inputs.pop("""text_inputs""" ) if isinstance(text_inputs[0] ,snake_case ): lowercase : List[Any] = text_inputs[0] else: # Batching case. lowercase : Dict = text_inputs[0][0] lowercase : Optional[Any] = self.model(**snake_case ,**snake_case ) lowercase : Any = { """candidate_labels""": candidate_labels, """logits""": outputs.logits_per_audio, } return model_outputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : List[Any] = model_outputs.pop("""candidate_labels""" ) lowercase : Any = model_outputs["""logits"""][0] if self.framework == "pt": lowercase : Any = logits.softmax(dim=0 ) lowercase : Tuple = probs.tolist() else: raise ValueError("""`tf` framework not supported.""" ) lowercase : Tuple = [ {"""score""": score, """label""": candidate_label} for score, candidate_label in sorted(zip(snake_case ,snake_case ) ,key=lambda snake_case : -x[0] ) ] return result
20
0
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase : List[Any] = logging.get_logger(__name__) lowercase : List[Any] = { 'microsoft/unispeech-large-1500h-cv': ( 'https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json' ), # See all UniSpeech models at https://huggingface.co/models?filter=unispeech } class A ( __snake_case ): __magic_name__ = '''unispeech''' def __init__( self , SCREAMING_SNAKE_CASE=32 , SCREAMING_SNAKE_CASE=768 , SCREAMING_SNAKE_CASE=12 , SCREAMING_SNAKE_CASE=12 , SCREAMING_SNAKE_CASE=3072 , SCREAMING_SNAKE_CASE="gelu" , SCREAMING_SNAKE_CASE=0.1 , SCREAMING_SNAKE_CASE=0.1 , SCREAMING_SNAKE_CASE=0.1 , SCREAMING_SNAKE_CASE=0.0 , SCREAMING_SNAKE_CASE=0.0 , SCREAMING_SNAKE_CASE=0.1 , SCREAMING_SNAKE_CASE=0.1 , SCREAMING_SNAKE_CASE=0.02 , SCREAMING_SNAKE_CASE=1e-5 , SCREAMING_SNAKE_CASE="group" , SCREAMING_SNAKE_CASE="gelu" , SCREAMING_SNAKE_CASE=(512, 512, 512, 512, 512, 512, 512) , SCREAMING_SNAKE_CASE=(5, 2, 2, 2, 2, 2, 2) , SCREAMING_SNAKE_CASE=(10, 3, 3, 3, 3, 2, 2) , SCREAMING_SNAKE_CASE=False , SCREAMING_SNAKE_CASE=128 , SCREAMING_SNAKE_CASE=16 , SCREAMING_SNAKE_CASE=False , SCREAMING_SNAKE_CASE=True , SCREAMING_SNAKE_CASE=0.05 , SCREAMING_SNAKE_CASE=10 , SCREAMING_SNAKE_CASE=2 , SCREAMING_SNAKE_CASE=0.0 , SCREAMING_SNAKE_CASE=10 , SCREAMING_SNAKE_CASE=0 , SCREAMING_SNAKE_CASE=320 , SCREAMING_SNAKE_CASE=2 , SCREAMING_SNAKE_CASE=0.1 , SCREAMING_SNAKE_CASE=100 , SCREAMING_SNAKE_CASE=256 , SCREAMING_SNAKE_CASE=256 , SCREAMING_SNAKE_CASE=0.1 , SCREAMING_SNAKE_CASE="mean" , SCREAMING_SNAKE_CASE=False , SCREAMING_SNAKE_CASE=False , SCREAMING_SNAKE_CASE=256 , SCREAMING_SNAKE_CASE=80 , SCREAMING_SNAKE_CASE=0 , SCREAMING_SNAKE_CASE=1 , SCREAMING_SNAKE_CASE=2 , SCREAMING_SNAKE_CASE=0.5 , **SCREAMING_SNAKE_CASE , ) -> Any: """simple docstring""" super().__init__(**SCREAMING_SNAKE_CASE , pad_token_id=SCREAMING_SNAKE_CASE , bos_token_id=SCREAMING_SNAKE_CASE , eos_token_id=SCREAMING_SNAKE_CASE ) A : Any = hidden_size A : Union[str, Any] = feat_extract_norm A : int = feat_extract_activation A : Tuple = list(SCREAMING_SNAKE_CASE ) A : Dict = list(SCREAMING_SNAKE_CASE ) A : Any = list(SCREAMING_SNAKE_CASE ) A : Any = conv_bias A : Dict = num_conv_pos_embeddings A : str = num_conv_pos_embedding_groups A : Optional[Any] = len(self.conv_dim ) A : str = num_hidden_layers A : Optional[Any] = intermediate_size A : Any = hidden_act A : List[str] = num_attention_heads A : int = hidden_dropout A : Optional[Any] = attention_dropout A : Union[str, Any] = activation_dropout A : str = feat_proj_dropout A : Tuple = final_dropout A : str = layerdrop A : Optional[int] = layer_norm_eps A : Optional[Any] = initializer_range A : Dict = num_ctc_classes A : int = vocab_size A : Dict = do_stable_layer_norm A : Dict = use_weighted_layer_sum A : Any = classifier_proj_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' F' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,' F' `len(config.conv_kernel) = {len(self.conv_kernel )}`.' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 A : Dict = apply_spec_augment A : Tuple = mask_time_prob A : Dict = mask_time_length A : List[Any] = mask_time_min_masks A : List[Any] = mask_feature_prob A : List[str] = mask_feature_length A : Any = mask_feature_min_masks # parameters for pretraining with codevector quantized representations A : Dict = num_codevectors_per_group A : str = num_codevector_groups A : List[Any] = contrastive_logits_temperature A : Optional[Any] = feat_quantizer_dropout A : Union[str, Any] = num_negatives A : str = codevector_dim A : Tuple = proj_codevector_dim A : Optional[int] = diversity_loss_weight # ctc loss A : List[Any] = ctc_loss_reduction A : int = ctc_zero_infinity # pretraining loss A : Union[str, Any] = replace_prob @property def __lowerCAmelCase ( self ) -> Optional[int]: """simple docstring""" return functools.reduce(operator.mul , self.conv_stride , 1 )
3
import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _snake_case( *SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__=True , SCREAMING_SNAKE_CASE__=2 ) -> Optional[Any]: from .. import __version__ lowercase : int = take_from lowercase : Tuple = () if not isinstance(args[0] , SCREAMING_SNAKE_CASE__ ): lowercase : Dict = (args,) for attribute, version_name, message in args: if version.parse(version.parse(SCREAMING_SNAKE_CASE__ ).base_version ) >= version.parse(SCREAMING_SNAKE_CASE__ ): raise ValueError( f"The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers'" f" version {__version__} is >= {version_name}" ) lowercase : int = None if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(SCREAMING_SNAKE_CASE__ ),) lowercase : Union[str, Any] = f"The `{attribute}` argument is deprecated and will be removed in version {version_name}." elif hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): values += (getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ),) lowercase : int = f"The `{attribute}` attribute is deprecated and will be removed in version {version_name}." elif deprecated_kwargs is None: lowercase : Dict = f"`{attribute}` is deprecated and will be removed in version {version_name}." if warning is not None: lowercase : Dict = warning + """ """ if standard_warn else """""" warnings.warn(warning + message , SCREAMING_SNAKE_CASE__ , stacklevel=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and len(SCREAMING_SNAKE_CASE__ ) > 0: lowercase : str = inspect.getouterframes(inspect.currentframe() )[1] lowercase : List[str] = call_frame.filename lowercase : Tuple = call_frame.lineno lowercase : List[str] = call_frame.function lowercase , lowercase : Optional[Any] = next(iter(deprecated_kwargs.items() ) ) raise TypeError(f"{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`" ) if len(SCREAMING_SNAKE_CASE__ ) == 0: return elif len(SCREAMING_SNAKE_CASE__ ) == 1: return values[0] return values
20
0
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __snake_case =logging.get_logger(__name__) __snake_case ={ """microsoft/swin-tiny-patch4-window7-224""": ( """https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json""" ), # See all Swin models at https://huggingface.co/models?filter=swin } class UpperCAmelCase_ ( __lowercase , __lowercase ): lowerCamelCase : Union[str, Any] = '''swin''' lowerCamelCase : Optional[Any] = { '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self : int , UpperCAmelCase__ : List[Any]=2_2_4 , UpperCAmelCase__ : Any=4 , UpperCAmelCase__ : Dict=3 , UpperCAmelCase__ : Union[str, Any]=9_6 , UpperCAmelCase__ : Any=[2, 2, 6, 2] , UpperCAmelCase__ : List[str]=[3, 6, 1_2, 2_4] , UpperCAmelCase__ : List[Any]=7 , UpperCAmelCase__ : Dict=4.0 , UpperCAmelCase__ : Any=True , UpperCAmelCase__ : Any=0.0 , UpperCAmelCase__ : Any=0.0 , UpperCAmelCase__ : Union[str, Any]=0.1 , UpperCAmelCase__ : Dict="gelu" , UpperCAmelCase__ : Optional[Any]=False , UpperCAmelCase__ : int=0.02 , UpperCAmelCase__ : Optional[Any]=1E-5 , UpperCAmelCase__ : List[str]=3_2 , UpperCAmelCase__ : Dict=None , UpperCAmelCase__ : Dict=None , **UpperCAmelCase__ : Dict , ) -> int: super().__init__(**UpperCAmelCase__ ) lowerCAmelCase = image_size lowerCAmelCase = patch_size lowerCAmelCase = num_channels lowerCAmelCase = embed_dim lowerCAmelCase = depths lowerCAmelCase = len(UpperCAmelCase__ ) lowerCAmelCase = num_heads lowerCAmelCase = window_size lowerCAmelCase = mlp_ratio lowerCAmelCase = qkv_bias lowerCAmelCase = hidden_dropout_prob lowerCAmelCase = attention_probs_dropout_prob lowerCAmelCase = drop_path_rate lowerCAmelCase = hidden_act lowerCAmelCase = use_absolute_embeddings lowerCAmelCase = layer_norm_eps lowerCAmelCase = initializer_range lowerCAmelCase = encoder_stride # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowerCAmelCase = int(embed_dim * 2 ** (len(UpperCAmelCase__ ) - 1) ) lowerCAmelCase = ['stem'] + [F'''stage{idx}''' for idx in range(1 , len(UpperCAmelCase__ ) + 1 )] lowerCAmelCase , lowerCAmelCase = get_aligned_output_features_output_indices( out_features=UpperCAmelCase__ , out_indices=UpperCAmelCase__ , stage_names=self.stage_names ) class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Dict = version.parse('''1.11''' ) @property def __UpperCAmelCase ( self : Dict ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def __UpperCAmelCase ( self : Dict ) -> float: return 1E-4
4
def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: if index == r: for j in range(SCREAMING_SNAKE_CASE__ ): print(data[j] , end=""" """ ) print(""" """ ) return # When no more elements are there to put in data[] if i >= n: return # current is included, put next at next location lowercase : Tuple = arr[i] combination_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , index + 1 , SCREAMING_SNAKE_CASE__ , i + 1 ) # current is excluded, replace it with # next (Note that i+1 is passed, but # index is not changed) combination_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , i + 1 ) # The main function that prints all combinations # of size r in arr[] of size n. This function # mainly uses combinationUtil() def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: # A temporary array to store all combination one by one lowercase : Optional[int] = [0] * r # Print all combination using temporary array 'data[]' combination_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , 0 , SCREAMING_SNAKE_CASE__ , 0 ) if __name__ == "__main__": # Driver code to check the function above lowercase : int = [10, 20, 30, 40, 50] print_combination(arr, len(arr), 3) # This code is contributed by Ambuj sahu
20
0
# flake8: noqa # Lint as: python3 UpperCAmelCase__ = [ '''VerificationMode''', '''Version''', '''disable_progress_bar''', '''enable_progress_bar''', '''is_progress_bar_enabled''', '''experimental''', ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
5
import contextlib import os import sqlitea import pytest from datasets import Dataset, Features, Value from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) 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 @require_sqlalchemy @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: lowercase : Union[str, Any] = tmp_path / """cache""" lowercase : Optional[Any] = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowercase : Any = SqlDatasetReader( """dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ , keep_in_memory=SCREAMING_SNAKE_CASE__ ).read() _check_sql_dataset(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @require_sqlalchemy @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 _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: lowercase : Union[str, Any] = tmp_path / """cache""" lowercase : int = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} lowercase : str = features.copy() if features else default_expected_features lowercase : Optional[Any] = ( Features({feature: Value(SCREAMING_SNAKE_CASE__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowercase : Optional[int] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , features=SCREAMING_SNAKE_CASE__ , cache_dir=SCREAMING_SNAKE_CASE__ ).read() _check_sql_dataset(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[Any]: with contextlib.closing(sqlitea.connect(SCREAMING_SNAKE_CASE__ ) ) as con: lowercase : Optional[int] = con.cursor() cur.execute("""SELECT * FROM dataset""" ) for row in cur: yield row @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> int: lowercase : Any = tmp_path / """cache""" lowercase : int = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : List[str] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=1 ).write() lowercase : List[str] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = iter_sql_file(SCREAMING_SNAKE_CASE__ ) for rowa, rowa in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): assert rowa == rowa @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: lowercase : Dict = tmp_path / """cache""" lowercase : List[str] = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : List[str] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=2 ).write() lowercase : Optional[int] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) for rowa, rowa in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): assert rowa == rowa @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : str = tmp_path / """cache""" lowercase : Optional[int] = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : Optional[Any] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() with pytest.raises(SCREAMING_SNAKE_CASE__ ): SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=0 ).write()
20
0
from typing import List, Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __A( a ): snake_case_ = ['''image_processor''', '''tokenizer'''] snake_case_ = '''BlipImageProcessor''' snake_case_ = ('''BertTokenizer''', '''BertTokenizerFast''') def __init__( self , _snake_case , _snake_case ) -> Dict: '''simple docstring''' __a = False super().__init__(_snake_case , _snake_case ) __a = self.image_processor def __call__( self , _snake_case = None , _snake_case = None , _snake_case = True , _snake_case = False , _snake_case = None , _snake_case = None , _snake_case = 0 , _snake_case = None , _snake_case = None , _snake_case = False , _snake_case = False , _snake_case = False , _snake_case = False , _snake_case = False , _snake_case = True , _snake_case = None , **_snake_case , ) -> BatchEncoding: '''simple docstring''' if images is None and text is None: raise ValueError('''You have to specify either images or text.''' ) # Get only text if images is None: __a = self.tokenizer __a = self.tokenizer( text=_snake_case , add_special_tokens=_snake_case , padding=_snake_case , truncation=_snake_case , max_length=_snake_case , stride=_snake_case , pad_to_multiple_of=_snake_case , return_attention_mask=_snake_case , return_overflowing_tokens=_snake_case , return_special_tokens_mask=_snake_case , return_offsets_mapping=_snake_case , return_token_type_ids=_snake_case , return_length=_snake_case , verbose=_snake_case , return_tensors=_snake_case , **_snake_case , ) return text_encoding # add pixel_values __a = self.image_processor(_snake_case , return_tensors=_snake_case ) if text is not None: __a = self.tokenizer( text=_snake_case , add_special_tokens=_snake_case , padding=_snake_case , truncation=_snake_case , max_length=_snake_case , stride=_snake_case , pad_to_multiple_of=_snake_case , return_attention_mask=_snake_case , return_overflowing_tokens=_snake_case , return_special_tokens_mask=_snake_case , return_offsets_mapping=_snake_case , return_token_type_ids=_snake_case , return_length=_snake_case , verbose=_snake_case , return_tensors=_snake_case , **_snake_case , ) else: __a = None if text_encoding is not None: encoding_image_processor.update(_snake_case ) return encoding_image_processor def SCREAMING_SNAKE_CASE_ ( self , *_snake_case , **_snake_case ) -> int: '''simple docstring''' return self.tokenizer.batch_decode(*_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , *_snake_case , **_snake_case ) -> Optional[int]: '''simple docstring''' return self.tokenizer.decode(*_snake_case , **_snake_case ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> List[Any]: '''simple docstring''' __a = self.tokenizer.model_input_names __a = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
6
import os import numpy import onnx def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: lowercase : int = a.name lowercase : Any = b.name lowercase : Optional[Any] = """""" lowercase : Dict = """""" lowercase : int = a == b lowercase : int = name_a lowercase : List[str] = name_b return res def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: for i, input_name in enumerate(node_proto.input ): if input_name == name: node_proto.input.insert(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) node_proto.input.pop(i + 1 ) if node_proto.op_type == "If": _graph_replace_input_with(node_proto.attribute[0].g , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) _graph_replace_input_with(node_proto.attribute[1].g , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if node_proto.op_type == "Loop": _graph_replace_input_with(node_proto.attribute[0].g , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: for n in graph_proto.node: _node_replace_input_with(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: lowercase : Any = list(model.graph.initializer ) lowercase : Dict = list(model_without_ext.graph.initializer ) for i, ref_i in ind_to_replace: assert inits_with_data[i].name == inits[i].name assert inits_with_data[ref_i].name == inits[ref_i].name assert i > ref_i lowercase : Union[str, Any] = inits[i].name lowercase : Dict = inits[ref_i].name model_without_ext.graph.initializer.remove(inits[i] ) # for n in model.graph.node: _graph_replace_input_with(model_without_ext.graph , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : Union[str, Any] = os.path.dirname(SCREAMING_SNAKE_CASE__ ) lowercase : Dict = os.path.basename(SCREAMING_SNAKE_CASE__ ) lowercase : str = onnx.load(os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) lowercase : List[str] = list(model.graph.initializer ) lowercase : Tuple = set() lowercase : int = {} lowercase : Optional[Any] = [] lowercase : Dict = 0 for i in range(len(SCREAMING_SNAKE_CASE__ ) ): if i in dup_set: continue for j in range(i + 1 , len(SCREAMING_SNAKE_CASE__ ) ): if j in dup_set: continue if _is_equal_tensor_proto(inits[i] , inits[j] ): dup_set.add(SCREAMING_SNAKE_CASE__ ) dup_set.add(SCREAMING_SNAKE_CASE__ ) lowercase : int = inits[j].data_type lowercase : Optional[int] = numpy.prod(inits[j].dims ) if dtype == 1: mem_size *= 4 elif dtype == 6: mem_size *= 4 elif dtype == 7 or dtype == 11: mem_size *= 8 else: print("""unexpected data type: """ , SCREAMING_SNAKE_CASE__ ) total_reduced_size += mem_size lowercase : Tuple = inits[i].name lowercase : int = inits[j].name if name_i in dup_map: dup_map[name_i].append(SCREAMING_SNAKE_CASE__ ) else: lowercase : List[str] = [name_j] ind_to_replace.append((j, i) ) print("""total reduced size: """ , total_reduced_size / 1_024 / 1_024 / 1_024 , """GB""" ) lowercase : str = sorted(SCREAMING_SNAKE_CASE__ ) _remove_dup_initializers_from_model(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = """optimized_""" + model_file_name lowercase : Dict = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) onnx.save(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return new_model
20
0
import inspect import unittest from transformers import BitConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import BitBackbone, BitForImageClassification, BitImageProcessor, BitModel from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class A : """simple docstring""" def __init__( self : Union[str, Any],lowercase_ : int,lowercase_ : List[Any]=3,lowercase_ : Dict=3_2,lowercase_ : List[str]=3,lowercase_ : List[str]=1_0,lowercase_ : List[Any]=[8, 1_6, 3_2, 6_4],lowercase_ : Any=[1, 1, 2, 1],lowercase_ : Any=True,lowercase_ : List[Any]=True,lowercase_ : Union[str, Any]="relu",lowercase_ : int=3,lowercase_ : Tuple=None,lowercase_ : Union[str, Any]=["stage2", "stage3", "stage4"],lowercase_ : Tuple=[2, 3, 4],lowercase_ : List[Any]=1,)-> int: '''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(lowercase_ ) A__ = out_features A__ = out_indices A__ = num_groups def snake_case__ ( self : Any )-> Optional[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 snake_case__ ( self : List[str] )-> Tuple: '''simple docstring''' return BitConfig( 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,out_features=self.out_features,out_indices=self.out_indices,num_groups=self.num_groups,) def snake_case__ ( self : Dict,lowercase_ : int,lowercase_ : Optional[int],lowercase_ : Optional[Any] )-> Dict: '''simple docstring''' A__ = BitModel(config=lowercase_ ) model.to(lowercase_ ) model.eval() A__ = model(lowercase_ ) self.parent.assertEqual( result.last_hidden_state.shape,(self.batch_size, self.hidden_sizes[-1], self.image_size // 3_2, self.image_size // 3_2),) def snake_case__ ( self : Union[str, Any],lowercase_ : List[Any],lowercase_ : Tuple,lowercase_ : Tuple )-> Optional[int]: '''simple docstring''' A__ = self.num_labels A__ = BitForImageClassification(lowercase_ ) model.to(lowercase_ ) model.eval() A__ = model(lowercase_,labels=lowercase_ ) self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_labels) ) def snake_case__ ( self : int,lowercase_ : str,lowercase_ : Dict,lowercase_ : List[str] )-> Union[str, Any]: '''simple docstring''' A__ = BitBackbone(config=lowercase_ ) model.to(lowercase_ ) model.eval() A__ = model(lowercase_ ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ),len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ),[self.batch_size, self.hidden_sizes[1], 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ),len(config.out_features ) ) self.parent.assertListEqual(model.channels,config.hidden_sizes[1:] ) # verify backbone works with out_features=None A__ = None A__ = BitBackbone(config=lowercase_ ) model.to(lowercase_ ) model.eval() A__ = model(lowercase_ ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ),1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ),[self.batch_size, self.hidden_sizes[-1], 1, 1] ) # verify channels self.parent.assertEqual(len(model.channels ),1 ) self.parent.assertListEqual(model.channels,[config.hidden_sizes[-1]] ) def snake_case__ ( self : Tuple )-> Optional[int]: '''simple docstring''' A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class A ( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): """simple docstring""" lowerCamelCase = (BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else () lowerCamelCase = ( {'feature-extraction': BitModel, 'image-classification': BitForImageClassification} if is_torch_available() else {} ) lowerCamelCase = False lowerCamelCase = False lowerCamelCase = False lowerCamelCase = False lowerCamelCase = False def snake_case__ ( self : Union[str, Any] )-> int: '''simple docstring''' A__ = BitModelTester(self ) A__ = ConfigTester(self,config_class=lowercase_,has_text_modality=lowercase_ ) def snake_case__ ( self : Dict )-> Tuple: '''simple docstring''' self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def snake_case__ ( self : Union[str, Any] )-> Tuple: '''simple docstring''' return @unittest.skip(reason='Bit does not output attentions' ) def snake_case__ ( self : Tuple )-> Tuple: '''simple docstring''' pass @unittest.skip(reason='Bit does not use inputs_embeds' ) def snake_case__ ( self : Dict )-> Optional[Any]: '''simple docstring''' pass @unittest.skip(reason='Bit does not support input and output embeddings' ) def snake_case__ ( self : Optional[Any] )-> Optional[Any]: '''simple docstring''' pass def snake_case__ ( self : List[str] )-> 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(lowercase_ ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1],lowercase_ ) def snake_case__ ( self : List[Any] )-> int: '''simple docstring''' A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowercase_ ) def snake_case__ ( self : List[str] )-> Optional[Any]: '''simple docstring''' A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*lowercase_ ) def snake_case__ ( self : Any )-> 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(config=lowercase_ ) for name, module in model.named_modules(): if isinstance(lowercase_,(nn.BatchNormad, nn.GroupNorm) ): self.assertTrue( torch.all(module.weight == 1 ),msg=F'Parameter {name} of model {model_class} seems not properly initialized',) self.assertTrue( torch.all(module.bias == 0 ),msg=F'Parameter {name} of model {model_class} seems not properly initialized',) def snake_case__ ( self : Dict )-> Tuple: '''simple docstring''' def check_hidden_states_output(lowercase_ : Any,lowercase_ : Union[str, Any],lowercase_ : Dict ): A__ = model_class(lowercase_ ) model.to(lowercase_ ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(lowercase_,lowercase_ ) ) A__ = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states A__ = self.model_tester.num_stages self.assertEqual(len(lowercase_ ),expected_num_stages + 1 ) # Bit'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 // 4, self.model_tester.image_size // 4],) A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = ['preactivation', 'bottleneck'] for model_class in self.all_model_classes: for layer_type in layers_type: A__ = layer_type A__ = True check_hidden_states_output(lowercase_,lowercase_,lowercase_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ = True check_hidden_states_output(lowercase_,lowercase_,lowercase_ ) @unittest.skip(reason='Bit does not use feedforward chunking' ) def snake_case__ ( self : int )-> str: '''simple docstring''' pass def snake_case__ ( self : Any )-> Union[str, Any]: '''simple docstring''' A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowercase_ ) @slow def snake_case__ ( self : str )-> str: '''simple docstring''' for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = BitModel.from_pretrained(lowercase_ ) self.assertIsNotNone(lowercase_ ) def _snake_case( ) -> str: '''simple docstring''' A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class A ( unittest.TestCase ): """simple docstring""" @cached_property def snake_case__ ( self : Tuple )-> Optional[int]: '''simple docstring''' return ( BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def snake_case__ ( self : Optional[int] )-> Dict: '''simple docstring''' A__ = BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(lowercase_ ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(images=lowercase_,return_tensors='pt' ).to(lowercase_ ) # forward pass with torch.no_grad(): A__ = model(**lowercase_ ) # verify the logits A__ = torch.Size((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape,lowercase_ ) A__ = torch.tensor([[-0.6_526, -0.5_263, -1.4_398]] ).to(lowercase_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3],lowercase_,atol=1E-4 ) ) @require_torch class A ( _UpperCAmelCase , unittest.TestCase ): """simple docstring""" lowerCamelCase = (BitBackbone,) if is_torch_available() else () lowerCamelCase = BitConfig lowerCamelCase = False def snake_case__ ( self : Tuple )-> Optional[int]: '''simple docstring''' A__ = BitModelTester(self )
7
import argparse import json from collections import OrderedDict import torch from huggingface_hub import cached_download, hf_hub_url from transformers import AutoImageProcessor, CvtConfig, CvtForImageClassification def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Tuple: lowercase : Union[str, Any] = [] embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.weight", f"stage{idx}.patch_embed.proj.weight", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.bias", f"stage{idx}.patch_embed.proj.bias", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.weight", f"stage{idx}.patch_embed.norm.weight", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.bias", f"stage{idx}.patch_embed.norm.bias", ) ) return embed def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: lowercase : Optional[Any] = [] attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.weight", f"stage{idx}.blocks.{cnt}.attn.proj_q.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.bias", f"stage{idx}.blocks.{cnt}.attn.proj_q.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.weight", f"stage{idx}.blocks.{cnt}.attn.proj_k.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.bias", f"stage{idx}.blocks.{cnt}.attn.proj_k.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.weight", f"stage{idx}.blocks.{cnt}.attn.proj_v.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.bias", f"stage{idx}.blocks.{cnt}.attn.proj_v.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.weight", f"stage{idx}.blocks.{cnt}.attn.proj.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.bias", f"stage{idx}.blocks.{cnt}.attn.proj.bias", ) ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.weight", f"stage{idx}.blocks.{cnt}.mlp.fc1.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.bias", f"stage{idx}.blocks.{cnt}.mlp.fc1.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.weight", f"stage{idx}.blocks.{cnt}.mlp.fc2.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.bias", f"stage{idx}.blocks.{cnt}.mlp.fc2.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.weight", f"stage{idx}.blocks.{cnt}.norm1.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.bias", f"stage{idx}.blocks.{cnt}.norm1.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.weight", f"stage{idx}.blocks.{cnt}.norm2.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.bias", f"stage{idx}.blocks.{cnt}.norm2.bias") ) return attention_weights def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: lowercase : Optional[Any] = [] token.append((f"cvt.encoder.stages.{idx}.cls_token", """stage2.cls_token""") ) return token def _snake_case( ) -> Dict: lowercase : Optional[Any] = [] head.append(("""layernorm.weight""", """norm.weight""") ) head.append(("""layernorm.bias""", """norm.bias""") ) head.append(("""classifier.weight""", """head.weight""") ) head.append(("""classifier.bias""", """head.bias""") ) return head def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: lowercase : Any = """imagenet-1k-id2label.json""" lowercase : List[str] = 1_000 lowercase : int = """huggingface/label-files""" lowercase : Union[str, Any] = num_labels lowercase : Optional[Any] = json.load(open(cached_download(hf_hub_url(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , repo_type="""dataset""" ) ) , """r""" ) ) lowercase : List[Any] = {int(SCREAMING_SNAKE_CASE__ ): v for k, v in idalabel.items()} lowercase : Dict = idalabel lowercase : List[str] = {v: k for k, v in idalabel.items()} lowercase : List[str] = CvtConfig(num_labels=SCREAMING_SNAKE_CASE__ , idalabel=SCREAMING_SNAKE_CASE__ , labelaid=SCREAMING_SNAKE_CASE__ ) # For depth size 13 (13 = 1+2+10) if cvt_model.rsplit("""/""" , 1 )[-1][4:6] == "13": lowercase : Tuple = [1, 2, 10] # For depth size 21 (21 = 1+4+16) elif cvt_model.rsplit("""/""" , 1 )[-1][4:6] == "21": lowercase : Dict = [1, 4, 16] # For wide cvt (similar to wide-resnet) depth size 24 (w24 = 2 + 2 20) else: lowercase : int = [2, 2, 20] lowercase : Optional[int] = [3, 12, 16] lowercase : str = [192, 768, 1_024] lowercase : Union[str, Any] = CvtForImageClassification(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = AutoImageProcessor.from_pretrained("""facebook/convnext-base-224-22k-1k""" ) lowercase : Optional[Any] = image_size lowercase : Union[str, Any] = torch.load(SCREAMING_SNAKE_CASE__ , map_location=torch.device("""cpu""" ) ) lowercase : Optional[Any] = OrderedDict() lowercase : Tuple = [] for idx in range(len(config.depth ) ): if config.cls_token[idx]: lowercase : Optional[Any] = list_of_state_dict + cls_token(SCREAMING_SNAKE_CASE__ ) lowercase : str = list_of_state_dict + embeddings(SCREAMING_SNAKE_CASE__ ) for cnt in range(config.depth[idx] ): lowercase : List[str] = list_of_state_dict + attention(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = list_of_state_dict + final() for gg in list_of_state_dict: print(SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) ): lowercase : Optional[Any] = original_weights[list_of_state_dict[i][1]] model.load_state_dict(SCREAMING_SNAKE_CASE__ ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) # Download the weights from zoo: https://1drv.ms/u/s!AhIXJn_J-blW9RzF3rMW7SsLHa8h?e=blQ0Al if __name__ == "__main__": lowercase : Tuple = argparse.ArgumentParser() parser.add_argument( """--cvt_model""", default="""cvt-w24""", type=str, help="""Name of the cvt model you'd like to convert.""", ) parser.add_argument( """--image_size""", default=384, type=int, help="""Input Image Size""", ) parser.add_argument( """--cvt_file_name""", default=R"""cvtmodels\CvT-w24-384x384-IN-22k.pth""", type=str, help="""Input Image Size""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) lowercase : Optional[int] = parser.parse_args() convert_cvt_checkpoint(args.cvt_model, args.image_size, args.cvt_file_name, args.pytorch_dump_folder_path)
20
0
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, convert_to_rgb, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging lowerCAmelCase_ = logging.get_logger(__name__) if is_vision_available(): import PIL class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = ["pixel_values"] def __init__( self : List[str] , _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 / 2_5_5 , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[Union[float, List[float]]] = None , _UpperCamelCase : Optional[Union[float, List[float]]] = None , _UpperCamelCase : bool = True , **_UpperCamelCase : Union[str, Any] , ) ->None: super().__init__(**_UpperCamelCase ) snake_case_ = size if size is not None else {'''shortest_edge''': 2_2_4} snake_case_ = get_size_dict(_UpperCamelCase , default_to_square=_UpperCamelCase ) snake_case_ = crop_size if crop_size is not None else {'''height''': 2_2_4, '''width''': 2_2_4} snake_case_ = get_size_dict(_UpperCamelCase , default_to_square=_UpperCamelCase , param_name='''crop_size''' ) snake_case_ = do_resize snake_case_ = size snake_case_ = resample snake_case_ = do_center_crop snake_case_ = crop_size snake_case_ = do_rescale snake_case_ = rescale_factor snake_case_ = do_normalize snake_case_ = image_mean if image_mean is not None else OPENAI_CLIP_MEAN snake_case_ = image_std if image_std is not None else OPENAI_CLIP_STD snake_case_ = do_convert_rgb def snake_case__( self : List[str] , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, int] , _UpperCamelCase : PILImageResampling = PILImageResampling.BICUBIC , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : Optional[Any] , ) ->np.ndarray: snake_case_ = get_size_dict(_UpperCamelCase , default_to_square=_UpperCamelCase ) if "shortest_edge" not in size: raise ValueError(f'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}''' ) snake_case_ = 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 snake_case__( self : int , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, int] , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : int , ) ->np.ndarray: snake_case_ = get_size_dict(_UpperCamelCase ) if "height" not in size or "width" not in size: raise ValueError(f'''The `size` parameter must contain the keys (height, width). Got {size.keys()}''' ) return center_crop(_UpperCamelCase , size=(size['''height'''], size['''width''']) , data_format=_UpperCamelCase , **_UpperCamelCase ) def snake_case__( self : Tuple , _UpperCamelCase : np.ndarray , _UpperCamelCase : Union[int, float] , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : str , ) ->Optional[Any]: return rescale(_UpperCamelCase , scale=_UpperCamelCase , data_format=_UpperCamelCase , **_UpperCamelCase ) def snake_case__( self : Any , _UpperCamelCase : np.ndarray , _UpperCamelCase : Union[float, List[float]] , _UpperCamelCase : Union[float, List[float]] , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : Optional[Any] , ) ->np.ndarray: return normalize(_UpperCamelCase , mean=_UpperCamelCase , std=_UpperCamelCase , data_format=_UpperCamelCase , **_UpperCamelCase ) def snake_case__( self : Union[str, Any] , _UpperCamelCase : ImageInput , _UpperCamelCase : bool = None , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = None , _UpperCamelCase : bool = None , _UpperCamelCase : int = None , _UpperCamelCase : bool = None , _UpperCamelCase : float = None , _UpperCamelCase : bool = None , _UpperCamelCase : Optional[Union[float, List[float]]] = None , _UpperCamelCase : Optional[Union[float, List[float]]] = None , _UpperCamelCase : bool = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , _UpperCamelCase : Optional[ChannelDimension] = ChannelDimension.FIRST , **_UpperCamelCase : Tuple , ) ->PIL.Image.Image: snake_case_ = do_resize if do_resize is not None else self.do_resize snake_case_ = size if size is not None else self.size snake_case_ = get_size_dict(_UpperCamelCase , param_name='''size''' , default_to_square=_UpperCamelCase ) snake_case_ = resample if resample is not None else self.resample snake_case_ = do_center_crop if do_center_crop is not None else self.do_center_crop snake_case_ = crop_size if crop_size is not None else self.crop_size snake_case_ = get_size_dict(_UpperCamelCase , param_name='''crop_size''' , default_to_square=_UpperCamelCase ) snake_case_ = do_rescale if do_rescale is not None else self.do_rescale snake_case_ = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case_ = do_normalize if do_normalize is not None else self.do_normalize snake_case_ = image_mean if image_mean is not None else self.image_mean snake_case_ = image_std if image_std is not None else self.image_std snake_case_ = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb snake_case_ = 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.''' ) # PIL RGBA images are converted to RGB if do_convert_rgb: snake_case_ = [convert_to_rgb(_UpperCamelCase ) for image in images] # All transformations expect numpy arrays. snake_case_ = [to_numpy_array(_UpperCamelCase ) for image in images] if do_resize: snake_case_ = [self.resize(image=_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase ) for image in images] if do_center_crop: snake_case_ = [self.center_crop(image=_UpperCamelCase , size=_UpperCamelCase ) for image in images] if do_rescale: snake_case_ = [self.rescale(image=_UpperCamelCase , scale=_UpperCamelCase ) for image in images] if do_normalize: snake_case_ = [self.normalize(image=_UpperCamelCase , mean=_UpperCamelCase , std=_UpperCamelCase ) for image in images] snake_case_ = [to_channel_dimension_format(_UpperCamelCase , _UpperCamelCase ) for image in images] snake_case_ = {'''pixel_values''': images} return BatchFeature(data=_UpperCamelCase , tensor_type=_UpperCamelCase )
8
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor from ..utils import is_datasets_available from .base import PipelineTool if is_datasets_available(): from datasets import load_dataset class __snake_case ( lowerCAmelCase ): _a : Union[str, Any]= "microsoft/speecht5_tts" _a : Tuple= ( "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the " "text to read (in English) and returns a waveform object containing the sound." ) _a : Dict= "text_reader" _a : Optional[Any]= SpeechTaProcessor _a : Tuple= SpeechTaForTextToSpeech _a : Optional[int]= SpeechTaHifiGan _a : Union[str, Any]= ["text"] _a : Optional[int]= ["audio"] def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self.post_processor is None: lowercase : Any = """microsoft/speecht5_hifigan""" super().setup() def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ): '''simple docstring''' lowercase : int = self.pre_processor(text=snake_case ,return_tensors="""pt""" ,truncation=snake_case ) if speaker_embeddings is None: if not is_datasets_available(): raise ImportError("""Datasets needs to be installed if not passing speaker embeddings.""" ) lowercase : Tuple = load_dataset("""Matthijs/cmu-arctic-xvectors""" ,split="""validation""" ) lowercase : List[str] = torch.tensor(embeddings_dataset[7305]["""xvector"""] ).unsqueeze(0 ) return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings} def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' with torch.no_grad(): return self.model.generate_speech(**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' with torch.no_grad(): return self.post_processor(snake_case ).cpu().detach()
20
0
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 :Any , lowerCAmelCase__ :Tuple , lowerCAmelCase__ :bool = True , lowerCAmelCase__ :Dict[str, int] = None , lowerCAmelCase__ :int = 32 , lowerCAmelCase__ :bool = True , lowerCAmelCase__ :Union[int, float] = 1 / 255 , lowerCAmelCase__ :bool = True , lowerCAmelCase__ :bool = True , lowerCAmelCase__ :Optional[Union[float, List[float]]] = [0.4814_5466, 0.457_8275, 0.4082_1073] , lowerCAmelCase__ :Optional[Union[float, List[float]]] = [0.2686_2954, 0.2613_0258, 0.2757_7711] , lowerCAmelCase__ :bool = True , lowerCAmelCase__ :Tuple=7 , lowerCAmelCase__ :Any=30 , lowerCAmelCase__ :Optional[Any]=400 , lowerCAmelCase__ :List[str]=3 , ) -> List[Any]: __SCREAMING_SNAKE_CASE : Tuple = parent __SCREAMING_SNAKE_CASE : List[str] = do_resize __SCREAMING_SNAKE_CASE : List[str] = size if size is not None else {'''shortest_edge''': 288} __SCREAMING_SNAKE_CASE : str = size_divisor __SCREAMING_SNAKE_CASE : Optional[int] = do_rescale __SCREAMING_SNAKE_CASE : Dict = rescale_factor __SCREAMING_SNAKE_CASE : int = do_normalize __SCREAMING_SNAKE_CASE : Union[str, Any] = do_center_crop __SCREAMING_SNAKE_CASE : int = image_mean __SCREAMING_SNAKE_CASE : List[Any] = image_std __SCREAMING_SNAKE_CASE : Any = do_pad __SCREAMING_SNAKE_CASE : str = batch_size __SCREAMING_SNAKE_CASE : Dict = num_channels __SCREAMING_SNAKE_CASE : List[Any] = min_resolution __SCREAMING_SNAKE_CASE : Tuple = max_resolution def __magic_name__( self :str ) -> Any: 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 __magic_name__( self :Optional[Any] , lowerCAmelCase__ :List[str] , lowerCAmelCase__ :Tuple=False ) -> List[Any]: if not batched: __SCREAMING_SNAKE_CASE : Dict = self.size['''shortest_edge'''] __SCREAMING_SNAKE_CASE : int = image_inputs[0] if isinstance(lowerCAmelCase__ , Image.Image ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : List[str] = image.size else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : int = image.shape[1], image.shape[2] __SCREAMING_SNAKE_CASE : Optional[int] = size / min(lowerCAmelCase__ , lowerCAmelCase__ ) if h < w: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : int = size, scale * w else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : int = scale * h, size __SCREAMING_SNAKE_CASE : Tuple = int((1_333 / 800) * size ) if max(lowerCAmelCase__ , lowerCAmelCase__ ) > max_size: __SCREAMING_SNAKE_CASE : Tuple = max_size / max(lowerCAmelCase__ , lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Any = newh * scale __SCREAMING_SNAKE_CASE : List[Any] = neww * scale __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Any = int(newh + 0.5 ), int(neww + 0.5 ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : int = ( newh // self.size_divisor * self.size_divisor, neww // self.size_divisor * self.size_divisor, ) else: __SCREAMING_SNAKE_CASE : Optional[Any] = [] for image in image_inputs: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : str = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) __SCREAMING_SNAKE_CASE : int = max(lowerCAmelCase__ , key=lambda lowerCAmelCase__ : item[0] )[0] __SCREAMING_SNAKE_CASE : str = max(lowerCAmelCase__ , key=lambda lowerCAmelCase__ : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class _lowercase ( A__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[Any] = BridgeTowerImageProcessor if is_vision_available() else None def __magic_name__( self :Optional[int] ) -> Any: __SCREAMING_SNAKE_CASE : Union[str, Any] = BridgeTowerImageProcessingTester(self ) @property def __magic_name__( self :Dict ) -> Optional[int]: return self.image_processor_tester.prepare_image_processor_dict() def __magic_name__( self :List[str] ) -> List[str]: __SCREAMING_SNAKE_CASE : str = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCAmelCase__ , '''image_mean''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''image_std''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_normalize''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_resize''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''size''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''size_divisor''' ) ) def __magic_name__( self :int ) -> List[str]: pass def __magic_name__( self :Optional[int] ) -> int: # Initialize image processor __SCREAMING_SNAKE_CASE : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __SCREAMING_SNAKE_CASE : Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , Image.Image ) # Test not batched input __SCREAMING_SNAKE_CASE : Dict = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Dict = self.image_processor_tester.get_expected_values(lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched __SCREAMING_SNAKE_CASE : Tuple = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : str = self.image_processor_tester.get_expected_values(lowerCAmelCase__ , batched=lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __magic_name__( self :str ) -> Any: # Initialize image processor __SCREAMING_SNAKE_CASE : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __SCREAMING_SNAKE_CASE : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , numpify=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , np.ndarray ) # Test not batched input __SCREAMING_SNAKE_CASE : Any = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : List[str] = self.image_processor_tester.get_expected_values(lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched __SCREAMING_SNAKE_CASE : int = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Union[str, Any] = self.image_processor_tester.get_expected_values(lowerCAmelCase__ , batched=lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __magic_name__( self :List[str] ) -> Any: # Initialize image processor __SCREAMING_SNAKE_CASE : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __SCREAMING_SNAKE_CASE : Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , torchify=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , torch.Tensor ) # Test not batched input __SCREAMING_SNAKE_CASE : Tuple = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Union[str, Any] = self.image_processor_tester.get_expected_values(lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched __SCREAMING_SNAKE_CASE : Dict = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : int = self.image_processor_tester.get_expected_values(lowerCAmelCase__ , batched=lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , )
9
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase : str = { """configuration_funnel""": ["""FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FunnelConfig"""], """convert_funnel_original_tf_checkpoint_to_pytorch""": [], """tokenization_funnel""": ["""FunnelTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Tuple = ["""FunnelTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : List[str] = [ """FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""", """FunnelBaseModel""", """FunnelForMaskedLM""", """FunnelForMultipleChoice""", """FunnelForPreTraining""", """FunnelForQuestionAnswering""", """FunnelForSequenceClassification""", """FunnelForTokenClassification""", """FunnelModel""", """FunnelPreTrainedModel""", """load_tf_weights_in_funnel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Optional[Any] = [ """TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFFunnelBaseModel""", """TFFunnelForMaskedLM""", """TFFunnelForMultipleChoice""", """TFFunnelForPreTraining""", """TFFunnelForQuestionAnswering""", """TFFunnelForSequenceClassification""", """TFFunnelForTokenClassification""", """TFFunnelModel""", """TFFunnelPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig from .tokenization_funnel import FunnelTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_funnel_fast import FunnelTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_funnel import ( FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, FunnelBaseModel, FunnelForMaskedLM, FunnelForMultipleChoice, FunnelForPreTraining, FunnelForQuestionAnswering, FunnelForSequenceClassification, FunnelForTokenClassification, FunnelModel, FunnelPreTrainedModel, load_tf_weights_in_funnel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_funnel import ( TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, TFFunnelPreTrainedModel, ) else: import sys lowercase : Any = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
20
0
def lowerCAmelCase_ ( __a ) -> List[Any]: """simple docstring""" if collection == []: return [] # get some information about the collection lowerCamelCase__: List[Any] =len(__a ) lowerCamelCase__: List[str] =max(__a ) lowerCamelCase__: Dict =min(__a ) # create the counting array lowerCamelCase__: Tuple =coll_max + 1 - coll_min lowerCamelCase__: Optional[int] =[0] * counting_arr_length # count how much a number appears in the collection for number in collection: counting_arr[number - coll_min] += 1 # sum each position with it's predecessors. now, counting_arr[i] tells # us how many elements <= i has in the collection for i in range(1 , __a ): lowerCamelCase__: int =counting_arr[i] + counting_arr[i - 1] # create the output collection lowerCamelCase__: Dict =[0] * coll_len # place the elements in the output, respecting the original order (stable # sort) from end to begin, updating counting_arr for i in reversed(range(0 , __a ) ): lowerCamelCase__: int =collection[i] counting_arr[collection[i] - coll_min] -= 1 return ordered def lowerCAmelCase_ ( __a ) -> str: """simple docstring""" return "".join([chr(__a ) for i in counting_sort([ord(__a ) for c in string] )] ) if __name__ == "__main__": # Test string sort assert counting_sort_string("thisisthestring") == "eghhiiinrsssttt" __A = input("Enter numbers separated by a comma:\n").strip() __A = [int(item) for item in user_input.split(",")] print(counting_sort(unsorted))
10
import os import zipfile import pytest from datasets.utils.extract import ( BzipaExtractor, Extractor, GzipExtractor, LzaExtractor, SevenZipExtractor, TarExtractor, XzExtractor, ZipExtractor, ZstdExtractor, ) from .utils import require_lza, require_pyazr, require_zstandard @pytest.mark.parametrize( """compression_format, is_archive""" , [ ("""7z""", True), ("""bz2""", False), ("""gzip""", False), ("""lz4""", False), ("""tar""", True), ("""xz""", False), ("""zip""", True), ("""zstd""", False), ] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) -> Any: lowercase : Dict = { """7z""": (seven_zip_file, SevenZipExtractor), """bz2""": (bza_file, BzipaExtractor), """gzip""": (gz_file, GzipExtractor), """lz4""": (lza_file, LzaExtractor), """tar""": (tar_file, TarExtractor), """xz""": (xz_file, XzExtractor), """zip""": (zip_file, ZipExtractor), """zstd""": (zstd_file, ZstdExtractor), } lowercase , lowercase : Optional[Any] = input_paths_and_base_extractors[compression_format] if input_path is None: lowercase : Dict = f"for '{compression_format}' compression_format, " if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(SCREAMING_SNAKE_CASE__ ) assert base_extractor.is_extractable(SCREAMING_SNAKE_CASE__ ) lowercase : Any = tmp_path / ("""extracted""" if is_archive else """extracted.txt""") base_extractor.extract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name lowercase : str = file_path.read_text(encoding="""utf-8""" ) else: lowercase : Optional[Any] = output_path.read_text(encoding="""utf-8""" ) lowercase : Tuple = text_file.read_text(encoding="""utf-8""" ) assert extracted_file_content == expected_file_content @pytest.mark.parametrize( """compression_format, is_archive""" , [ ("""7z""", True), ("""bz2""", False), ("""gzip""", False), ("""lz4""", False), ("""tar""", True), ("""xz""", False), ("""zip""", True), ("""zstd""", False), ] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) -> Dict: lowercase : str = { """7z""": seven_zip_file, """bz2""": bza_file, """gzip""": gz_file, """lz4""": lza_file, """tar""": tar_file, """xz""": xz_file, """zip""": zip_file, """zstd""": zstd_file, } lowercase : Optional[Any] = input_paths[compression_format] if input_path is None: lowercase : int = f"for '{compression_format}' compression_format, " if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = Extractor.infer_extractor_format(SCREAMING_SNAKE_CASE__ ) assert extractor_format is not None lowercase : Any = tmp_path / ("""extracted""" if is_archive else """extracted.txt""") Extractor.extract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name lowercase : Dict = file_path.read_text(encoding="""utf-8""" ) else: lowercase : int = output_path.read_text(encoding="""utf-8""" ) lowercase : Optional[Any] = text_file.read_text(encoding="""utf-8""" ) assert extracted_file_content == expected_file_content @pytest.fixture def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: import tarfile lowercase : Tuple = tmp_path / """data_dot_dot""" directory.mkdir() lowercase : str = directory / """tar_file_with_dot_dot.tar""" with tarfile.TarFile(SCREAMING_SNAKE_CASE__ , """w""" ) as f: f.add(SCREAMING_SNAKE_CASE__ , arcname=os.path.join("""..""" , text_file.name ) ) return path @pytest.fixture def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[str]: import tarfile lowercase : Tuple = tmp_path / """data_sym_link""" directory.mkdir() lowercase : int = directory / """tar_file_with_sym_link.tar""" os.symlink("""..""" , directory / """subdir""" , target_is_directory=SCREAMING_SNAKE_CASE__ ) with tarfile.TarFile(SCREAMING_SNAKE_CASE__ , """w""" ) as f: f.add(str(directory / """subdir""" ) , arcname="""subdir""" ) # str required by os.readlink on Windows and Python < 3.8 return path @pytest.mark.parametrize( """insecure_tar_file, error_log""" , [("""tar_file_with_dot_dot""", """illegal path"""), ("""tar_file_with_sym_link""", """Symlink""")] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: lowercase : List[Any] = { """tar_file_with_dot_dot""": tar_file_with_dot_dot, """tar_file_with_sym_link""": tar_file_with_sym_link, } lowercase : Optional[int] = insecure_tar_files[insecure_tar_file] lowercase : List[str] = tmp_path / """extracted""" TarExtractor.extract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) assert caplog.text for record in caplog.records: assert record.levelname == "ERROR" assert error_log in record.msg def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[int]: # We should have less false positives than zipfile.is_zipfile # We do that by checking only the magic number lowercase : Any = tmpdir / """not_a_zip_file""" # From: https://github.com/python/cpython/pull/5053 lowercase : str = ( B"""\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00""" B"""\x00\x02\x08\x06\x00\x00\x00\x99\x81\xb6'\x00\x00\x00\x15I""" B"""DATx\x01\x01\n\x00\xf5\xff\x00PK\x05\x06\x00PK\x06\x06\x07""" B"""\xac\x01N\xc6|a\r\x00\x00\x00\x00IEND\xaeB`\x82""" ) with not_a_zip_file.open("""wb""" ) as f: f.write(SCREAMING_SNAKE_CASE__ ) assert zipfile.is_zipfile(str(SCREAMING_SNAKE_CASE__ ) ) # is a false positive for `zipfile` assert not ZipExtractor.is_extractable(SCREAMING_SNAKE_CASE__ ) # but we're right
20
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { 'RWKV/rwkv-4-169m-pile': 'https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json', 'RWKV/rwkv-4-430m-pile': 'https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json', 'RWKV/rwkv-4-1b5-pile': 'https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json', 'RWKV/rwkv-4-3b-pile': 'https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json', 'RWKV/rwkv-4-7b-pile': 'https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json', 'RWKV/rwkv-4-14b-pile': 'https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json', 'RWKV/rwkv-raven-1b5': 'https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json', 'RWKV/rwkv-raven-3b': 'https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json', 'RWKV/rwkv-raven-7b': 'https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json', 'RWKV/rwkv-raven-14b': 'https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json', } class lowerCAmelCase__ ( a): '''simple docstring''' __SCREAMING_SNAKE_CASE = "rwkv" __SCREAMING_SNAKE_CASE = {"max_position_embeddings": "context_length"} def __init__( self , __lowerCamelCase=5_0_2_7_7 , __lowerCamelCase=1_0_2_4 , __lowerCamelCase=4_0_9_6 , __lowerCamelCase=3_2 , __lowerCamelCase=None , __lowerCamelCase=None , __lowerCamelCase=1e-5 , __lowerCamelCase=0 , __lowerCamelCase=0 , __lowerCamelCase=6 , __lowerCamelCase=False , __lowerCamelCase=True , **__lowerCamelCase , ) -> Any: _A : Tuple = vocab_size _A : Union[str, Any] = context_length _A : Union[str, Any] = hidden_size _A : str = num_hidden_layers _A : str = attention_hidden_size if attention_hidden_size is not None else hidden_size _A : Dict = intermediate_size if intermediate_size is not None else 4 * hidden_size _A : Any = layer_norm_epsilon _A : int = rescale_every _A : int = use_cache _A : List[Any] = bos_token_id _A : List[Any] = eos_token_id super().__init__( tie_word_embeddings=__lowerCamelCase , bos_token_id=__lowerCamelCase , eos_token_id=__lowerCamelCase , **__lowerCamelCase)
11
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast from ...utils import logging lowercase : List[str] = logging.get_logger(__name__) lowercase : Optional[Any] = { """EleutherAI/gpt-neo-1.3B""": """https://huggingface.co/EleutherAI/gpt-neo-1.3B/resolve/main/config.json""", # See all GPTNeo models at https://huggingface.co/models?filter=gpt_neo } class __snake_case ( lowerCAmelCase ): _a : str= "gpt_neo" _a : Optional[int]= ["past_key_values"] _a : Dict= {"num_attention_heads": "num_heads", "num_hidden_layers": "num_layers"} def __init__( self ,snake_case=50257 ,snake_case=2048 ,snake_case=2048 ,snake_case=24 ,snake_case=[[["global", "local"], 12]] ,snake_case=16 ,snake_case=None ,snake_case=256 ,snake_case="gelu_new" ,snake_case=0.0 ,snake_case=0.0 ,snake_case=0.0 ,snake_case=0.1 ,snake_case=1e-5 ,snake_case=0.02 ,snake_case=True ,snake_case=50256 ,snake_case=50256 ,**snake_case ,): '''simple docstring''' lowercase : int = vocab_size lowercase : Union[str, Any] = max_position_embeddings lowercase : Dict = hidden_size lowercase : Union[str, Any] = num_layers lowercase : Union[str, Any] = num_heads lowercase : Optional[int] = intermediate_size lowercase : List[str] = window_size lowercase : Optional[int] = activation_function lowercase : List[str] = resid_dropout lowercase : int = embed_dropout lowercase : Optional[int] = attention_dropout lowercase : Tuple = classifier_dropout lowercase : Optional[int] = layer_norm_epsilon lowercase : Dict = initializer_range lowercase : List[str] = use_cache lowercase : Optional[int] = bos_token_id lowercase : int = eos_token_id lowercase : Union[str, Any] = attention_types lowercase : Dict = self.expand_attention_types_params(snake_case ) if len(self.attention_layers ) != self.num_layers: raise ValueError( """Configuration for convolutional module is incorrect. """ """It is required that `len(config.attention_layers)` == `config.num_layers` """ f"but is `len(config.attention_layers) = {len(self.attention_layers )}`, " f"`config.num_layers = {self.num_layers}`. " """`config.attention_layers` is prepared using `config.attention_types`. """ """Please verify the value of `config.attention_types` argument.""" ) super().__init__(bos_token_id=snake_case ,eos_token_id=snake_case ,**snake_case ) @staticmethod def _SCREAMING_SNAKE_CASE ( snake_case ): '''simple docstring''' lowercase : List[Any] = [] for item in attention_types: for _ in range(item[1] ): attentions.extend(item[0] ) return attentions def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: import torch lowercase : Tuple = input.size() lowercase : int = len(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = shape[dimension] lowercase : int = torch.arange(0 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = torch.div(sizedim - size , SCREAMING_SNAKE_CASE__ , rounding_mode="""floor""" ) + 1 lowercase : Dict = torch.arange(SCREAMING_SNAKE_CASE__ ) + low_indices[:min_length][:, None] lowercase : Union[str, Any] = [slice(SCREAMING_SNAKE_CASE__ )] * rank lowercase : Optional[Any] = indices lowercase : List[str] = input[s] lowercase : Optional[int] = list(range(0 , rank + 1 ) ) perm.append(perm.pop(dimension + 1 ) ) return sliced.permute(SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: import torch lowercase : Union[str, Any] = torch.arange(1 , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = torch.remainder(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = remainders == 0 lowercase : Optional[int] = candidates[divisor_indices] lowercase : List[Any] = torch.max(SCREAMING_SNAKE_CASE__ ) return largest_divisor, torch.div(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , rounding_mode="""floor""" ) class __snake_case ( lowerCAmelCase ): @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Dict = OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}} ) if self.use_past: self.fill_with_past_key_values_(snake_case ,direction="""inputs""" ) lowercase : Dict = {0: """batch""", 1: """past_sequence + sequence"""} else: lowercase : List[str] = {0: """batch""", 1: """sequence"""} return common_inputs @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return self._config.num_heads def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = -1 ,snake_case = -1 ,snake_case = False ,snake_case = None ,): '''simple docstring''' lowercase : Any = super(snake_case ,self ).generate_dummy_inputs( snake_case ,batch_size=snake_case ,seq_length=snake_case ,is_pair=snake_case ,framework=snake_case ) # We need to order the input in the way they appears in the forward() lowercase : List[str] = OrderedDict({"""input_ids""": common_inputs["""input_ids"""]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed.""" ) else: import torch lowercase , lowercase : List[Any] = common_inputs["""input_ids"""].shape # Not using the same length for past_key_values lowercase : Optional[int] = seqlen + 2 lowercase : int = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) lowercase : Optional[Any] = [ (torch.zeros(snake_case ), torch.zeros(snake_case )) for _ in range(self.num_layers ) ] lowercase : Optional[Any] = common_inputs["""attention_mask"""] if self.use_past: lowercase : Any = ordered_inputs["""attention_mask"""].dtype lowercase : Union[str, Any] = torch.cat( [ordered_inputs["""attention_mask"""], torch.ones(snake_case ,snake_case ,dtype=snake_case )] ,dim=1 ) return ordered_inputs @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return 13
20
0
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__)
12
from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING lowercase : Any = logging.get_logger(__name__) @add_end_docstrings(lowerCAmelCase ) class __snake_case ( lowerCAmelCase ): def __init__( self ,*snake_case ,**snake_case ): '''simple docstring''' super().__init__(*snake_case ,**snake_case ) requires_backends(self ,"""vision""" ) self.check_model_type( TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING if self.framework == """tf""" else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING ) def _SCREAMING_SNAKE_CASE ( self ,snake_case=None ): '''simple docstring''' lowercase : List[Any] = {} if top_k is not None: lowercase : int = top_k return {}, {}, postprocess_params def __call__( self ,snake_case ,**snake_case ): '''simple docstring''' return super().__call__(snake_case ,**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Any = load_image(snake_case ) lowercase : List[Any] = self.image_processor(images=snake_case ,return_tensors=self.framework ) return model_inputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : int = self.model(**snake_case ) return model_outputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=5 ): '''simple docstring''' if top_k > self.model.config.num_labels: lowercase : Tuple = self.model.config.num_labels if self.framework == "pt": lowercase : str = model_outputs.logits.softmax(-1 )[0] lowercase , lowercase : Dict = probs.topk(snake_case ) elif self.framework == "tf": lowercase : Optional[int] = stable_softmax(model_outputs.logits ,axis=-1 )[0] lowercase : Union[str, Any] = tf.math.top_k(snake_case ,k=snake_case ) lowercase , lowercase : List[str] = topk.values.numpy(), topk.indices.numpy() else: raise ValueError(f"Unsupported framework: {self.framework}" ) lowercase : Tuple = scores.tolist() lowercase : Dict = ids.tolist() return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(snake_case ,snake_case )]
20
0
from sklearn.metrics import recall_score import datasets lowerCAmelCase : Optional[int] = """ Recall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation: Recall = TP / (TP + FN) Where TP is the true positives and FN is the false negatives. """ lowerCAmelCase : int = """ Args: - **predictions** (`list` of `int`): The predicted labels. - **references** (`list` of `int`): The ground truth labels. - **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None. - **pos_label** (`int`): The class label to use as the 'positive class' when calculating the recall. Defaults to `1`. - **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`. - `'binary'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary. - `'micro'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives. - `'macro'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - `'weighted'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall. - `'samples'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). - **sample_weight** (`list` of `float`): Sample weights Defaults to `None`. - **zero_division** (): Sets the value to return when there is a zero division. Defaults to . - `'warn'`: If there is a zero division, the return value is `0`, but warnings are also raised. - `0`: If there is a zero division, the return value is `0`. - `1`: If there is a zero division, the return value is `1`. Returns: - **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better. Examples: Example 1-A simple example with some errors >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1]) >>> print(results) {'recall': 0.6666666666666666} Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`. >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0) >>> print(results) {'recall': 0.5} Example 3-The same example as Example 1, but with `sample_weight` included. >>> recall_metric = datasets.load_metric('recall') >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8] >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight) >>> print(results) {'recall': 0.55} Example 4-A multiclass example, using different averages. >>> recall_metric = datasets.load_metric('recall') >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = recall_metric.compute(predictions=predictions, references=references, average='macro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='micro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='weighted') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {'recall': array([1., 0., 0.])} """ lowerCAmelCase : Tuple = """ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __lowercase ( datasets.Metric ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[str]): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("int32")), "references": datasets.Sequence(datasets.Value("int32")), } if self.config_name == "multilabel" else { "predictions": datasets.Value("int32"), "references": datasets.Value("int32"), }) , reference_urls=["https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html"] , ) def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : str=1 , lowerCAmelCase__ : List[str]="binary" , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : str="warn" , ): SCREAMING_SNAKE_CASE_: List[str] = recall_score( lowerCAmelCase__ , lowerCAmelCase__ , labels=lowerCAmelCase__ , pos_label=lowerCAmelCase__ , average=lowerCAmelCase__ , sample_weight=lowerCAmelCase__ , zero_division=lowerCAmelCase__ , ) return {"recall": float(lowerCAmelCase__) if score.size == 1 else score}
13
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class __snake_case : def __init__( self ,snake_case ,): '''simple docstring''' lowercase : Any = parent lowercase : Tuple = 13 lowercase : str = 7 lowercase : Dict = True lowercase : Dict = True lowercase : str = True lowercase : List[str] = True lowercase : int = True lowercase : Union[str, Any] = False lowercase : Dict = False lowercase : List[Any] = False lowercase : List[Any] = 2 lowercase : Optional[Any] = 99 lowercase : int = 0 lowercase : Tuple = 32 lowercase : int = 2 lowercase : Tuple = 4 lowercase : List[Any] = 0.1 lowercase : Tuple = 0.1 lowercase : List[Any] = 512 lowercase : int = 16 lowercase : Dict = 2 lowercase : int = 0.02 lowercase : Union[str, Any] = 3 lowercase : Any = 4 lowercase : List[Any] = """last""" lowercase : Tuple = True lowercase : List[Any] = None lowercase : Any = 0 def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) lowercase : List[str] = random_attention_mask([self.batch_size, self.seq_length] ,dtype=tf.floataa ) lowercase : Tuple = None if self.use_input_lengths: lowercase : List[str] = ( ids_tensor([self.batch_size] ,vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length lowercase : Tuple = None if self.use_token_type_ids: lowercase : Optional[int] = ids_tensor([self.batch_size, self.seq_length] ,self.n_langs ) lowercase : List[str] = None lowercase : List[str] = None lowercase : Optional[Any] = None if self.use_labels: lowercase : List[str] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) lowercase : List[str] = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) lowercase : str = ids_tensor([self.batch_size] ,2 ,dtype=tf.floataa ) lowercase : Optional[Any] = ids_tensor([self.batch_size] ,self.num_choices ) lowercase : str = FlaubertConfig( vocab_size=self.vocab_size ,n_special=self.n_special ,emb_dim=self.hidden_size ,n_layers=self.num_hidden_layers ,n_heads=self.num_attention_heads ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,gelu_activation=self.gelu_activation ,sinusoidal_embeddings=self.sinusoidal_embeddings ,asm=self.asm ,causal=self.causal ,n_langs=self.n_langs ,max_position_embeddings=self.max_position_embeddings ,initializer_range=self.initializer_range ,summary_type=self.summary_type ,use_proj=self.use_proj ,bos_token_id=self.bos_token_id ,) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Tuple = TFFlaubertModel(config=snake_case ) lowercase : str = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} lowercase : Optional[Any] = model(snake_case ) lowercase : List[Any] = [input_ids, input_mask] lowercase : int = model(snake_case ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : List[Any] = TFFlaubertWithLMHeadModel(snake_case ) lowercase : Optional[Any] = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} lowercase : int = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Tuple = TFFlaubertForQuestionAnsweringSimple(snake_case ) lowercase : Union[str, Any] = {"""input_ids""": input_ids, """lengths""": input_lengths} lowercase : Tuple = model(snake_case ) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Union[str, Any] = TFFlaubertForSequenceClassification(snake_case ) lowercase : str = {"""input_ids""": input_ids, """lengths""": input_lengths} lowercase : str = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Any = self.num_labels lowercase : List[str] = TFFlaubertForTokenClassification(config=snake_case ) lowercase : Dict = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} lowercase : int = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Any = self.num_choices lowercase : Dict = TFFlaubertForMultipleChoice(config=snake_case ) lowercase : Any = tf.tile(tf.expand_dims(snake_case ,1 ) ,(1, self.num_choices, 1) ) lowercase : Optional[Any] = tf.tile(tf.expand_dims(snake_case ,1 ) ,(1, self.num_choices, 1) ) lowercase : Dict = tf.tile(tf.expand_dims(snake_case ,1 ) ,(1, self.num_choices, 1) ) lowercase : Union[str, Any] = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } lowercase : int = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_choices) ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Any = self.prepare_config_and_inputs() ( ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ) : int = config_and_inputs lowercase : List[str] = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """langs""": token_type_ids, """lengths""": input_lengths, } return config, inputs_dict @require_tf class __snake_case ( lowerCAmelCase , lowerCAmelCase , unittest.TestCase ): _a : Dict= ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) _a : Optional[Any]= ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable _a : Any= ( { "feature-extraction": TFFlaubertModel, "fill-mask": TFFlaubertWithLMHeadModel, "question-answering": TFFlaubertForQuestionAnsweringSimple, "text-classification": TFFlaubertForSequenceClassification, "token-classification": TFFlaubertForTokenClassification, "zero-shot": TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) _a : Tuple= False _a : int= False def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ): '''simple docstring''' if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("""Fast""" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : str = TFFlaubertModelTester(self ) lowercase : List[Any] = ConfigTester(self ,config_class=snake_case ,emb_dim=37 ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' self.config_tester.run_common_tests() def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*snake_case ) @slow def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase : Dict = TFFlaubertModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) @require_tf @require_sentencepiece @require_tokenizers class __snake_case ( unittest.TestCase ): @slow def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased""" ) lowercase : int = tf.convert_to_tensor( [[0, 158, 735, 2592, 1424, 6727, 82, 1]] ,dtype=tf.intaa ,) # "J'aime flaubert !" lowercase : Dict = model(snake_case )[0] lowercase : Union[str, Any] = tf.TensorShape((1, 8, 512) ) self.assertEqual(output.shape ,snake_case ) # compare the actual values for a slice. lowercase : Tuple = tf.convert_to_tensor( [ [ [-1.8_768_773, -1.566_555, 0.27_072_418], [-1.6_920_038, -0.5_873_505, 1.9_329_599], [-2.9_563_985, -1.6_993_835, 1.7_972_052], ] ] ,dtype=tf.floataa ,) self.assertTrue(np.allclose(output[:, :3, :3].numpy() ,expected_slice.numpy() ,atol=1e-4 ) )
20
0
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 _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) _lowerCamelCase : List[Any] = {"""vocab_file""": """vocab.txt"""} _lowerCamelCase : List[Any] = { """vocab_file""": { """openbmb/cpm-ant-10b""": """https://huggingface.co/openbmb/cpm-ant-10b/blob/main/vocab.txt""", }, } _lowerCamelCase : List[str] = { """openbmb/cpm-ant-10b""": 1024, } def SCREAMING_SNAKE_CASE ( lowercase_ ) -> Any: """simple docstring""" A__ = collections.OrderedDict() with open(lowercase_ , '''r''' , encoding='''utf-8''' ) as reader: A__ = reader.readlines() for index, token in enumerate(lowercase_ ): A__ = token.rstrip('''\n''' ) A__ = index return vocab class UpperCamelCase_ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self : Dict , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : List[str]="<unk>" , UpperCAmelCase__ : Union[str, Any]=200) ->List[str]: '''simple docstring''' A__ = vocab A__ = unk_token A__ = max_input_chars_per_word def SCREAMING_SNAKE_CASE ( self : Optional[Any] , UpperCAmelCase__ : Optional[int]) ->Optional[int]: '''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 UpperCamelCase_ ( UpperCAmelCase__ ): '''simple docstring''' UpperCAmelCase__ = VOCAB_FILES_NAMES UpperCAmelCase__ = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ = ['''input_ids''', '''attention_mask'''] UpperCAmelCase__ = False def __init__( self : Union[str, Any] , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Optional[int]="<d>" , UpperCAmelCase__ : Union[str, Any]="</d>" , UpperCAmelCase__ : Dict="<s>" , UpperCAmelCase__ : Optional[int]="</s>" , UpperCAmelCase__ : Optional[int]="<pad>" , UpperCAmelCase__ : Dict="<unk>" , UpperCAmelCase__ : Optional[Any]="</n>" , UpperCAmelCase__ : List[Any]="</_>" , UpperCAmelCase__ : Optional[Any]="left" , **UpperCAmelCase__ : Union[str, Any] , ) ->Dict: '''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 SCREAMING_SNAKE_CASE ( self : List[str]) ->Dict: '''simple docstring''' return self.encoder[self.bod_token] @property def SCREAMING_SNAKE_CASE ( self : Optional[Any]) ->List[Any]: '''simple docstring''' return self.encoder[self.eod_token] @property def SCREAMING_SNAKE_CASE ( self : int) ->List[Any]: '''simple docstring''' return self.encoder["\n"] @property def SCREAMING_SNAKE_CASE ( self : int) ->int: '''simple docstring''' return len(self.encoder) def SCREAMING_SNAKE_CASE ( self : Dict) ->str: '''simple docstring''' return dict(self.encoder , **self.added_tokens_encoder) def SCREAMING_SNAKE_CASE ( self : Any , UpperCAmelCase__ : Any) ->Union[str, Any]: '''simple docstring''' A__ = [] for x in jieba.cut(UpperCAmelCase__ , cut_all=UpperCAmelCase__): output_tokens.extend(self.wordpiece_tokenizer.tokenize(UpperCAmelCase__)) return output_tokens def SCREAMING_SNAKE_CASE ( self : Optional[Any] , UpperCAmelCase__ : Optional[Any] , **UpperCAmelCase__ : Dict) ->List[str]: '''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 SCREAMING_SNAKE_CASE ( self : str , UpperCAmelCase__ : Union[str, Any]) ->Tuple: '''simple docstring''' return token in self.encoder def SCREAMING_SNAKE_CASE ( self : Any , UpperCAmelCase__ : List[str]) ->str: '''simple docstring''' return "".join(UpperCAmelCase__) def SCREAMING_SNAKE_CASE ( self : int , UpperCAmelCase__ : int) ->Union[str, Any]: '''simple docstring''' return self.encoder.get(UpperCAmelCase__ , self.encoder.get(self.unk_token)) def SCREAMING_SNAKE_CASE ( self : int , UpperCAmelCase__ : List[str]) ->Any: '''simple docstring''' return self.decoder.get(UpperCAmelCase__ , self.unk_token) def SCREAMING_SNAKE_CASE ( self : List[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 SCREAMING_SNAKE_CASE ( self : int , 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 SCREAMING_SNAKE_CASE ( self : Optional[int] , 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__))
14
import json import os from dataclasses import dataclass from functools import partial from typing import Callable import flax.linen as nn import jax import jax.numpy as jnp import joblib import optax import wandb from flax import jax_utils, struct, traverse_util from flax.serialization import from_bytes, to_bytes from flax.training import train_state from flax.training.common_utils import shard from tqdm.auto import tqdm from transformers import BigBirdConfig, FlaxBigBirdForQuestionAnswering from transformers.models.big_bird.modeling_flax_big_bird import FlaxBigBirdForQuestionAnsweringModule class __snake_case ( lowerCAmelCase ): _a : BigBirdConfig _a : jnp.dtype= jnp.floataa _a : bool= True def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' super().setup() lowercase : List[str] = nn.Dense(5 ,dtype=self.dtype ) def __call__( self ,*snake_case ,**snake_case ): '''simple docstring''' lowercase : int = super().__call__(*snake_case ,**snake_case ) lowercase : Any = self.cls(outputs[2] ) return outputs[:2] + (cls_out,) class __snake_case ( lowerCAmelCase ): _a : List[Any]= FlaxBigBirdForNaturalQuestionsModule def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: def cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None ): lowercase : int = logits.shape[-1] lowercase : Dict = (labels[..., None] == jnp.arange(SCREAMING_SNAKE_CASE__ )[None]).astype("""f4""" ) lowercase : Any = jax.nn.log_softmax(SCREAMING_SNAKE_CASE__ , axis=-1 ) lowercase : Optional[Any] = -jnp.sum(labels * logits , axis=-1 ) if reduction is not None: lowercase : Any = reduction(SCREAMING_SNAKE_CASE__ ) return loss lowercase : Optional[Any] = partial(SCREAMING_SNAKE_CASE__ , reduction=jnp.mean ) lowercase : Optional[int] = cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Dict = cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : int = cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return (start_loss + end_loss + pooled_loss) / 3 @dataclass class __snake_case : _a : str= "google/bigbird-roberta-base" _a : int= 3000 _a : int= 1_0500 _a : int= 128 _a : int= 3 _a : int= 1 _a : int= 5 # tx_args _a : float= 3E-5 _a : float= 0.0 _a : int= 2_0000 _a : float= 0.00_95 _a : str= "bigbird-roberta-natural-questions" _a : str= "training-expt" _a : str= "data/nq-training.jsonl" _a : str= "data/nq-validation.jsonl" def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' os.makedirs(self.base_dir ,exist_ok=snake_case ) lowercase : Optional[int] = os.path.join(self.base_dir ,self.save_dir ) lowercase : Optional[int] = self.batch_size_per_device * jax.device_count() @dataclass class __snake_case : _a : int _a : int= 4096 # no dynamic padding on TPUs def __call__( self ,snake_case ): '''simple docstring''' lowercase : int = self.collate_fn(snake_case ) lowercase : Union[str, Any] = jax.tree_util.tree_map(snake_case ,snake_case ) return batch def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase , lowercase : Union[str, Any] = self.fetch_inputs(features["""input_ids"""] ) lowercase : Tuple = { """input_ids""": jnp.array(snake_case ,dtype=jnp.intaa ), """attention_mask""": jnp.array(snake_case ,dtype=jnp.intaa ), """start_labels""": jnp.array(features["""start_token"""] ,dtype=jnp.intaa ), """end_labels""": jnp.array(features["""end_token"""] ,dtype=jnp.intaa ), """pooled_labels""": jnp.array(features["""category"""] ,dtype=jnp.intaa ), } return batch def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Tuple = [self._fetch_inputs(snake_case ) for ids in input_ids] return zip(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Union[str, Any] = [1 for _ in range(len(snake_case ) )] while len(snake_case ) < self.max_length: input_ids.append(self.pad_id ) attention_mask.append(0 ) return input_ids, attention_mask def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None ) -> Any: if seed is not None: lowercase : Optional[int] = dataset.shuffle(seed=SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) // batch_size ): lowercase : Optional[Any] = dataset[i * batch_size : (i + 1) * batch_size] yield dict(SCREAMING_SNAKE_CASE__ ) @partial(jax.pmap , axis_name="""batch""" ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) -> List[Any]: def loss_fn(SCREAMING_SNAKE_CASE__ ): lowercase : List[str] = model_inputs.pop("""start_labels""" ) lowercase : Optional[int] = model_inputs.pop("""end_labels""" ) lowercase : str = model_inputs.pop("""pooled_labels""" ) lowercase : Union[str, Any] = state.apply_fn(**SCREAMING_SNAKE_CASE__ , params=SCREAMING_SNAKE_CASE__ , dropout_rng=SCREAMING_SNAKE_CASE__ , train=SCREAMING_SNAKE_CASE__ ) lowercase , lowercase , lowercase : List[str] = outputs return state.loss_fn( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) lowercase , lowercase : int = jax.random.split(SCREAMING_SNAKE_CASE__ ) lowercase : Dict = jax.value_and_grad(SCREAMING_SNAKE_CASE__ ) lowercase , lowercase : Union[str, Any] = grad_fn(state.params ) lowercase : List[Any] = jax.lax.pmean({"""loss""": loss} , axis_name="""batch""" ) lowercase : List[Any] = jax.lax.pmean(SCREAMING_SNAKE_CASE__ , """batch""" ) lowercase : str = state.apply_gradients(grads=SCREAMING_SNAKE_CASE__ ) return state, metrics, new_drp_rng @partial(jax.pmap , axis_name="""batch""" ) def _snake_case( SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : int = model_inputs.pop("""start_labels""" ) lowercase : Dict = model_inputs.pop("""end_labels""" ) lowercase : Optional[Any] = model_inputs.pop("""pooled_labels""" ) lowercase : Optional[int] = state.apply_fn(**SCREAMING_SNAKE_CASE__ , params=state.params , train=SCREAMING_SNAKE_CASE__ ) lowercase , lowercase , lowercase : List[Any] = outputs lowercase : Dict = state.loss_fn(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : str = jax.lax.pmean({"""loss""": loss} , axis_name="""batch""" ) return metrics class __snake_case ( train_state.TrainState ): _a : Callable= struct.field(pytree_node=lowerCAmelCase ) @dataclass class __snake_case : _a : Args _a : Callable _a : Callable _a : Callable _a : Callable _a : wandb _a : Callable= None def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case=None ): '''simple docstring''' lowercase : Tuple = model.params lowercase : Any = TrainState.create( apply_fn=model.__call__ ,params=snake_case ,tx=snake_case ,loss_fn=snake_case ,) if ckpt_dir is not None: lowercase , lowercase , lowercase , lowercase , lowercase : Tuple = restore_checkpoint(snake_case ,snake_case ) lowercase : List[str] = { """lr""": args.lr, """init_lr""": args.init_lr, """warmup_steps""": args.warmup_steps, """num_train_steps""": num_train_steps, """weight_decay""": args.weight_decay, } lowercase , lowercase : Tuple = build_tx(**snake_case ) lowercase : str = train_state.TrainState( step=snake_case ,apply_fn=model.__call__ ,params=snake_case ,tx=snake_case ,opt_state=snake_case ,) lowercase : Any = args lowercase : Optional[Any] = data_collator lowercase : List[str] = lr lowercase : str = params lowercase : Tuple = jax_utils.replicate(snake_case ) return state def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ): '''simple docstring''' lowercase : Dict = self.args lowercase : Optional[Any] = len(snake_case ) // args.batch_size lowercase : int = jax.random.PRNGKey(0 ) lowercase : List[str] = jax.random.split(snake_case ,jax.device_count() ) for epoch in range(args.max_epochs ): lowercase : List[Any] = jnp.array(0 ,dtype=jnp.floataa ) lowercase : List[str] = get_batched_dataset(snake_case ,args.batch_size ,seed=snake_case ) lowercase : int = 0 for batch in tqdm(snake_case ,total=snake_case ,desc=f"Running EPOCH-{epoch}" ): lowercase : Dict = self.data_collator(snake_case ) lowercase , lowercase , lowercase : Optional[int] = self.train_step_fn(snake_case ,snake_case ,**snake_case ) running_loss += jax_utils.unreplicate(metrics["""loss"""] ) i += 1 if i % args.logging_steps == 0: lowercase : Optional[Any] = jax_utils.unreplicate(state.step ) lowercase : List[str] = running_loss.item() / i lowercase : List[str] = self.scheduler_fn(state_step - 1 ) lowercase : int = self.evaluate(snake_case ,snake_case ) lowercase : Tuple = { """step""": state_step.item(), """eval_loss""": eval_loss.item(), """tr_loss""": tr_loss, """lr""": lr.item(), } tqdm.write(str(snake_case ) ) self.logger.log(snake_case ,commit=snake_case ) if i % args.save_steps == 0: self.save_checkpoint(args.save_dir + f"-e{epoch}-s{i}" ,state=snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : List[str] = get_batched_dataset(snake_case ,self.args.batch_size ) lowercase : Any = len(snake_case ) // self.args.batch_size lowercase : List[Any] = jnp.array(0 ,dtype=jnp.floataa ) lowercase : Optional[int] = 0 for batch in tqdm(snake_case ,total=snake_case ,desc="""Evaluating ... """ ): lowercase : Tuple = self.data_collator(snake_case ) lowercase : Optional[int] = self.val_step_fn(snake_case ,**snake_case ) running_loss += jax_utils.unreplicate(metrics["""loss"""] ) i += 1 return running_loss / i def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : str = jax_utils.unreplicate(snake_case ) print(f"SAVING CHECKPOINT IN {save_dir}" ,end=""" ... """ ) self.model_save_fn(snake_case ,params=state.params ) with open(os.path.join(snake_case ,"""opt_state.msgpack""" ) ,"""wb""" ) as f: f.write(to_bytes(state.opt_state ) ) joblib.dump(self.args ,os.path.join(snake_case ,"""args.joblib""" ) ) joblib.dump(self.data_collator ,os.path.join(snake_case ,"""data_collator.joblib""" ) ) with open(os.path.join(snake_case ,"""training_state.json""" ) ,"""w""" ) as f: json.dump({"""step""": state.step.item()} ,snake_case ) print("""DONE""" ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: print(f"RESTORING CHECKPOINT FROM {save_dir}" , end=""" ... """ ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , """flax_model.msgpack""" ) , """rb""" ) as f: lowercase : str = from_bytes(state.params , f.read() ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , """opt_state.msgpack""" ) , """rb""" ) as f: lowercase : Optional[int] = from_bytes(state.opt_state , f.read() ) lowercase : Optional[Any] = joblib.load(os.path.join(SCREAMING_SNAKE_CASE__ , """args.joblib""" ) ) lowercase : int = joblib.load(os.path.join(SCREAMING_SNAKE_CASE__ , """data_collator.joblib""" ) ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , """training_state.json""" ) , """r""" ) as f: lowercase : Tuple = json.load(SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = training_state["""step"""] print("""DONE""" ) return params, opt_state, step, args, data_collator def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: lowercase : List[str] = num_train_steps - warmup_steps lowercase : Dict = optax.linear_schedule(init_value=SCREAMING_SNAKE_CASE__ , end_value=SCREAMING_SNAKE_CASE__ , transition_steps=SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = optax.linear_schedule(init_value=SCREAMING_SNAKE_CASE__ , end_value=1e-7 , transition_steps=SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = optax.join_schedules(schedules=[warmup_fn, decay_fn] , boundaries=[warmup_steps] ) return lr def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: def weight_decay_mask(SCREAMING_SNAKE_CASE__ ): lowercase : List[Any] = traverse_util.flatten_dict(SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = {k: (v[-1] != """bias""" and v[-2:] != ("""LayerNorm""", """scale""")) for k, v in params.items()} return traverse_util.unflatten_dict(SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = scheduler_fn(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = optax.adamw(learning_rate=SCREAMING_SNAKE_CASE__ , weight_decay=SCREAMING_SNAKE_CASE__ , mask=SCREAMING_SNAKE_CASE__ ) return tx, lr
20
0
import math class UpperCAmelCase : '''simple docstring''' def __init__( self : Optional[int] ,A : Dict=0 ): # a graph with Node 0,1,...,N-1 __A = n __A = [ [math.inf for j in range(0 ,A )] for i in range(0 ,A ) ] # adjacency matrix for weight __A = [ [math.inf for j in range(0 ,A )] for i in range(0 ,A ) ] # dp[i][j] stores minimum distance from i to j def UpperCamelCase_ ( self : List[str] ,A : Tuple ,A : Union[str, Any] ,A : Optional[int] ): __A = w def UpperCamelCase_ ( self : int ): for k in range(0 ,self.n ): for i in range(0 ,self.n ): for j in range(0 ,self.n ): __A = min(self.dp[i][j] ,self.dp[i][k] + self.dp[k][j] ) def UpperCamelCase_ ( self : Optional[int] ,A : str ,A : Union[str, Any] ): return self.dp[u][v] if __name__ == "__main__": SCREAMING_SNAKE_CASE :Optional[int] = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 10) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 10) graph.add_edge(3, 1, 2) graph.add_edge(3, 2, 1) graph.add_edge(3, 4, 6) graph.add_edge(4, 1, 3) graph.add_edge(4, 2, 4) graph.add_edge(4, 3, 9) graph.floyd_warshall() graph.show_min(1, 4) graph.show_min(0, 3)
15
from math import sqrt def _snake_case( SCREAMING_SNAKE_CASE__ ) -> bool: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number >= 0 ), "'number' must been an int and positive" lowercase : Union[str, Any] = True # 0 and 1 are none primes. if number <= 1: lowercase : str = False for divisor in range(2 , int(round(sqrt(SCREAMING_SNAKE_CASE__ ) ) ) + 1 ): # if 'number' divisible by 'divisor' then sets 'status' # of false and break up the loop. if number % divisor == 0: lowercase : Any = False break # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'status' must been from type bool" return status def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n > 2), "'N' must been an int and > 2" # beginList: contains all natural numbers from 2 up to N lowercase : str = list(range(2 , n + 1 ) ) lowercase : Tuple = [] # this list will be returns. # actual sieve of erathostenes for i in range(len(SCREAMING_SNAKE_CASE__ ) ): for j in range(i + 1 , len(SCREAMING_SNAKE_CASE__ ) ): if (begin_list[i] != 0) and (begin_list[j] % begin_list[i] == 0): lowercase : Tuple = 0 # filters actual prime numbers. lowercase : int = [x for x in begin_list if x != 0] # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type list" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n > 2), "'N' must been an int and > 2" lowercase : Dict = [] # iterates over all numbers between 2 up to N+1 # if a number is prime then appends to list 'ans' for number in range(2 , n + 1 ): if is_prime(SCREAMING_SNAKE_CASE__ ): ans.append(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type list" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Tuple: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and number >= 0, "'number' must been an int and >= 0" lowercase : Tuple = [] # this list will be returns of the function. # potential prime number factors. lowercase : Optional[Any] = 2 lowercase : Any = number if number == 0 or number == 1: ans.append(SCREAMING_SNAKE_CASE__ ) # if 'number' not prime then builds the prime factorization of 'number' elif not is_prime(SCREAMING_SNAKE_CASE__ ): while quotient != 1: if is_prime(SCREAMING_SNAKE_CASE__ ) and (quotient % factor == 0): ans.append(SCREAMING_SNAKE_CASE__ ) quotient /= factor else: factor += 1 else: ans.append(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type list" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number >= 0 ), "'number' bust been an int and >= 0" lowercase : Tuple = 0 # prime factorization of 'number' lowercase : Optional[int] = prime_factorization(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = max(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type int" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> str: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number >= 0 ), "'number' bust been an int and >= 0" lowercase : Union[str, Any] = 0 # prime factorization of 'number' lowercase : Tuple = prime_factorization(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = min(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type int" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'number' must been an int" assert isinstance(number % 2 == 0 , SCREAMING_SNAKE_CASE__ ), "compare bust been from type bool" return number % 2 == 0 def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'number' must been an int" assert isinstance(number % 2 != 0 , SCREAMING_SNAKE_CASE__ ), "compare bust been from type bool" return number % 2 != 0 def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (number > 2) and is_even(SCREAMING_SNAKE_CASE__ ) ), "'number' must been an int, even and > 2" lowercase : Union[str, Any] = [] # this list will returned # creates a list of prime numbers between 2 up to 'number' lowercase : str = get_prime_numbers(SCREAMING_SNAKE_CASE__ ) lowercase : Any = len(SCREAMING_SNAKE_CASE__ ) # run variable for while-loops. lowercase : Optional[Any] = 0 lowercase : List[Any] = None # exit variable. for break up the loops lowercase : Any = True while i < len_pn and loop: lowercase : str = i + 1 while j < len_pn and loop: if prime_numbers[i] + prime_numbers[j] == number: lowercase : Union[str, Any] = False ans.append(prime_numbers[i] ) ans.append(prime_numbers[j] ) j += 1 i += 1 # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (len(SCREAMING_SNAKE_CASE__ ) == 2) and (ans[0] + ans[1] == number) and is_prime(ans[0] ) and is_prime(ans[1] ) ), "'ans' must contains two primes. And sum of elements must been eq 'number'" return ans def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (numbera >= 0) and (numbera >= 0) ), "'number1' and 'number2' must been positive integer." lowercase : Union[str, Any] = 0 while numbera != 0: lowercase : Optional[int] = numbera % numbera lowercase : Optional[int] = numbera lowercase : Dict = rest # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( numbera >= 0 ), "'number' must been from type int and positive" return numbera def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (numbera >= 1) and (numbera >= 1) ), "'number1' and 'number2' must been positive integer." lowercase : Dict = 1 # actual answer that will be return. # for kgV (x,1) if numbera > 1 and numbera > 1: # builds the prime factorization of 'number1' and 'number2' lowercase : Optional[Any] = prime_factorization(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[int] = prime_factorization(SCREAMING_SNAKE_CASE__ ) elif numbera == 1 or numbera == 1: lowercase : Union[str, Any] = [] lowercase : List[str] = [] lowercase : Dict = max(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = 0 lowercase : Optional[Any] = 0 lowercase : List[str] = [] # captured numbers int both 'primeFac1' and 'primeFac2' # iterates through primeFac1 for n in prime_fac_a: if n not in done: if n in prime_fac_a: lowercase : Dict = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) for _ in range(max(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ): ans *= n else: lowercase : List[Any] = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) for _ in range(SCREAMING_SNAKE_CASE__ ): ans *= n done.append(SCREAMING_SNAKE_CASE__ ) # iterates through primeFac2 for n in prime_fac_a: if n not in done: lowercase : Optional[int] = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) for _ in range(SCREAMING_SNAKE_CASE__ ): ans *= n done.append(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( ans >= 0 ), "'ans' must been from type int and positive" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 0), "'number' must been a positive int" lowercase : Dict = 0 lowercase : List[str] = 2 # this variable holds the answer while index < n: index += 1 ans += 1 # counts to the next number # if ans not prime then # runs to the next prime number. while not is_prime(SCREAMING_SNAKE_CASE__ ): ans += 1 # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and is_prime( SCREAMING_SNAKE_CASE__ ), "'ans' must been a prime number and from type int" return ans def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: assert ( is_prime(SCREAMING_SNAKE_CASE__ ) and is_prime(SCREAMING_SNAKE_CASE__ ) and (p_number_a < p_number_a) ), "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" lowercase : List[str] = p_number_a + 1 # jump to the next number lowercase : List[Any] = [] # this list will be returns. # if number is not prime then # fetch the next prime number. while not is_prime(SCREAMING_SNAKE_CASE__ ): number += 1 while number < p_number_a: ans.append(SCREAMING_SNAKE_CASE__ ) number += 1 # fetch the next prime number. while not is_prime(SCREAMING_SNAKE_CASE__ ): number += 1 # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ans[0] != p_number_a and ans[len(SCREAMING_SNAKE_CASE__ ) - 1] != p_number_a ), "'ans' must been a list without the arguments" # 'ans' contains not 'pNumber1' and 'pNumber2' ! return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 1), "'n' must been int and >= 1" lowercase : Optional[Any] = [] # will be returned. for divisor in range(1 , n + 1 ): if n % divisor == 0: ans.append(SCREAMING_SNAKE_CASE__ ) # precondition assert ans[0] == 1 and ans[len(SCREAMING_SNAKE_CASE__ ) - 1] == n, "Error in function getDivisiors(...)" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number > 1 ), "'number' must been an int and >= 1" lowercase : str = get_divisors(SCREAMING_SNAKE_CASE__ ) # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (divisors[0] == 1) and (divisors[len(SCREAMING_SNAKE_CASE__ ) - 1] == number) ), "Error in help-function getDivisiors(...)" # summed all divisors up to 'number' (exclusive), hence [:-1] return sum(divisors[:-1] ) == number def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (denominator != 0) ), "The arguments must been from type int and 'denominator' != 0" # build the greatest common divisor of numerator and denominator. lowercase : Tuple = gcd(abs(SCREAMING_SNAKE_CASE__ ) , abs(SCREAMING_SNAKE_CASE__ ) ) # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (numerator % gcd_of_fraction == 0) and (denominator % gcd_of_fraction == 0) ), "Error in function gcd(...,...)" return (numerator // gcd_of_fraction, denominator // gcd_of_fraction) def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 0), "'n' must been a int and >= 0" lowercase : List[str] = 1 # this will be return. for factor in range(1 , n + 1 ): ans *= factor return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 0), "'n' must been an int and >= 0" lowercase : int = 0 lowercase : Union[str, Any] = 1 lowercase : int = 1 # this will be return for _ in range(n - 1 ): lowercase : Optional[int] = ans ans += fiba lowercase : Optional[int] = tmp return ans
20
0
"""simple docstring""" from __future__ import annotations import unittest from transformers import is_tf_available, is_torch_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow if is_tf_available(): from transformers import ( AutoConfig, BertConfig, GPTaConfig, TaConfig, TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, ) from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST if is_torch_available(): from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForPreTraining, AutoModelForQuestionAnswering, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoModelWithLMHead, BertForMaskedLM, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, BertModel, GPTaLMHeadModel, RobertaForMaskedLM, TaForConditionalGeneration, ) @is_pt_tf_cross_test class __A ( unittest.TestCase ): '''simple docstring''' @slow def UpperCAmelCase ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" for model_name in ["bert-base-uncased"]: lowercase__ : Tuple = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Dict = TFAutoModel.from_pretrained(_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : List[str] = AutoModel.from_pretrained(_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCAmelCase ( self : Union[str, Any] ) -> Tuple: """simple docstring""" for model_name in ["bert-base-uncased"]: lowercase__ : Dict = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : str = TFAutoModelForPreTraining.from_pretrained(_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Optional[Any] = AutoModelForPreTraining.from_pretrained(_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCAmelCase ( self : Tuple ) -> Dict: """simple docstring""" for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ : Any = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : List[str] = TFAutoModelForCausalLM.from_pretrained(_snake_case ,from_pt=_snake_case ) lowercase__ , lowercase__ : Optional[Any] = TFAutoModelForCausalLM.from_pretrained( _snake_case ,output_loading_info=_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Union[str, Any] = AutoModelForCausalLM.from_pretrained(_snake_case ,from_tf=_snake_case ) lowercase__ , lowercase__ : Optional[Any] = AutoModelForCausalLM.from_pretrained( _snake_case ,output_loading_info=_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCAmelCase ( self : Any ) -> Tuple: """simple docstring""" for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ : Any = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Optional[Any] = TFAutoModelWithLMHead.from_pretrained(_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Any = AutoModelWithLMHead.from_pretrained(_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCAmelCase ( self : List[str] ) -> Optional[Any]: """simple docstring""" for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ : str = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Union[str, Any] = TFAutoModelForMaskedLM.from_pretrained(_snake_case ,from_pt=_snake_case ) lowercase__ , lowercase__ : str = TFAutoModelForMaskedLM.from_pretrained( _snake_case ,output_loading_info=_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : List[str] = AutoModelForMaskedLM.from_pretrained(_snake_case ,from_tf=_snake_case ) lowercase__ , lowercase__ : Any = AutoModelForMaskedLM.from_pretrained( _snake_case ,output_loading_info=_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCAmelCase ( self : List[Any] ) -> Dict: """simple docstring""" for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ : Union[str, Any] = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : List[str] = TFAutoModelForSeqaSeqLM.from_pretrained(_snake_case ,from_pt=_snake_case ) lowercase__ , lowercase__ : List[str] = TFAutoModelForSeqaSeqLM.from_pretrained( _snake_case ,output_loading_info=_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Any = AutoModelForSeqaSeqLM.from_pretrained(_snake_case ,from_tf=_snake_case ) lowercase__ , lowercase__ : Optional[int] = AutoModelForSeqaSeqLM.from_pretrained( _snake_case ,output_loading_info=_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCAmelCase ( self : List[str] ) -> Union[str, Any]: """simple docstring""" for model_name in ["bert-base-uncased"]: lowercase__ : Tuple = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Any = TFAutoModelForSequenceClassification.from_pretrained(_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Union[str, Any] = AutoModelForSequenceClassification.from_pretrained(_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCAmelCase ( self : Tuple ) -> Optional[Any]: """simple docstring""" for model_name in ["bert-base-uncased"]: lowercase__ : List[Any] = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : str = TFAutoModelForQuestionAnswering.from_pretrained(_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Any = AutoModelForQuestionAnswering.from_pretrained(_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) def UpperCAmelCase ( self : Dict ) -> Any: """simple docstring""" lowercase__ : Optional[Any] = TFAutoModelWithLMHead.from_pretrained(_snake_case ,from_pt=_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) self.assertEqual(model.num_parameters() ,14_410 ) self.assertEqual(model.num_parameters(only_trainable=_snake_case ) ,14_410 ) lowercase__ : Union[str, Any] = AutoModelWithLMHead.from_pretrained(_snake_case ,from_tf=_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) self.assertEqual(model.num_parameters() ,14_410 ) self.assertEqual(model.num_parameters(only_trainable=_snake_case ) ,14_410 ) def UpperCAmelCase ( self : int ) -> List[Any]: """simple docstring""" lowercase__ : List[Any] = TFAutoModelWithLMHead.from_pretrained(_snake_case ,from_pt=_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) self.assertEqual(model.num_parameters() ,14_410 ) self.assertEqual(model.num_parameters(only_trainable=_snake_case ) ,14_410 ) lowercase__ : int = AutoModelWithLMHead.from_pretrained(_snake_case ,from_tf=_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) self.assertEqual(model.num_parameters() ,14_410 ) self.assertEqual(model.num_parameters(only_trainable=_snake_case ) ,14_410 )
16
from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase : List[str] = logging.get_logger(__name__) lowercase : Any = { """uclanlp/visualbert-vqa""": """https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json""", """uclanlp/visualbert-vqa-pre""": """https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json""", """uclanlp/visualbert-vqa-coco-pre""": ( """https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json""" ), """uclanlp/visualbert-vcr""": """https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json""", """uclanlp/visualbert-vcr-pre""": """https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json""", """uclanlp/visualbert-vcr-coco-pre""": ( """https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json""" ), """uclanlp/visualbert-nlvr2""": """https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json""", """uclanlp/visualbert-nlvr2-pre""": """https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json""", """uclanlp/visualbert-nlvr2-coco-pre""": ( """https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json""" ) # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert } class __snake_case ( lowerCAmelCase ): _a : Union[str, Any]= "visual_bert" def __init__( self ,snake_case=30522 ,snake_case=768 ,snake_case=512 ,snake_case=12 ,snake_case=12 ,snake_case=3072 ,snake_case="gelu" ,snake_case=0.1 ,snake_case=0.1 ,snake_case=512 ,snake_case=2 ,snake_case=0.02 ,snake_case=1e-12 ,snake_case=False ,snake_case=True ,snake_case=1 ,snake_case=0 ,snake_case=2 ,**snake_case ,): '''simple docstring''' super().__init__(pad_token_id=snake_case ,bos_token_id=snake_case ,eos_token_id=snake_case ,**snake_case ) lowercase : Tuple = vocab_size lowercase : int = max_position_embeddings lowercase : Optional[Any] = hidden_size lowercase : int = visual_embedding_dim lowercase : Tuple = num_hidden_layers lowercase : str = num_attention_heads lowercase : Optional[Any] = intermediate_size lowercase : str = hidden_act lowercase : Tuple = hidden_dropout_prob lowercase : List[Any] = attention_probs_dropout_prob lowercase : Union[str, Any] = initializer_range lowercase : int = type_vocab_size lowercase : Union[str, Any] = layer_norm_eps lowercase : Union[str, Any] = bypass_transformer lowercase : int = special_visual_initialize
20
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) _a = { 'configuration_layoutlmv2': ['LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LayoutLMv2Config'], 'processing_layoutlmv2': ['LayoutLMv2Processor'], 'tokenization_layoutlmv2': ['LayoutLMv2Tokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a = ['LayoutLMv2TokenizerFast'] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a = ['LayoutLMv2FeatureExtractor'] _a = ['LayoutLMv2ImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a = [ 'LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST', 'LayoutLMv2ForQuestionAnswering', 'LayoutLMv2ForSequenceClassification', 'LayoutLMv2ForTokenClassification', 'LayoutLMv2Layer', 'LayoutLMv2Model', 'LayoutLMv2PreTrainedModel', ] if TYPE_CHECKING: from .configuration_layoutlmva import LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig from .processing_layoutlmva import LayoutLMvaProcessor from .tokenization_layoutlmva import LayoutLMvaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor, LayoutLMvaImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_layoutlmva import ( LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaLayer, LayoutLMvaModel, LayoutLMvaPreTrainedModel, ) else: import sys _a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
17
import argparse import requests import torch from PIL import Image from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: if "cls_token" in name: lowercase : List[Any] = name.replace("""cls_token""" , """vit.embeddings.cls_token""" ) if "mask_token" in name: lowercase : Any = name.replace("""mask_token""" , """decoder.mask_token""" ) if "decoder_pos_embed" in name: lowercase : str = name.replace("""decoder_pos_embed""" , """decoder.decoder_pos_embed""" ) if "pos_embed" in name and "decoder" not in name: lowercase : List[str] = name.replace("""pos_embed""" , """vit.embeddings.position_embeddings""" ) if "patch_embed.proj" in name: lowercase : Tuple = name.replace("""patch_embed.proj""" , """vit.embeddings.patch_embeddings.projection""" ) if "patch_embed.norm" in name: lowercase : int = name.replace("""patch_embed.norm""" , """vit.embeddings.norm""" ) if "decoder_blocks" in name: lowercase : Tuple = name.replace("""decoder_blocks""" , """decoder.decoder_layers""" ) if "blocks" in name: lowercase : List[Any] = name.replace("""blocks""" , """vit.encoder.layer""" ) if "attn.proj" in name: lowercase : List[str] = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name: lowercase : Union[str, Any] = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: lowercase : Optional[Any] = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: lowercase : Union[str, Any] = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: lowercase : Dict = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: lowercase : Dict = name.replace("""mlp.fc2""" , """output.dense""" ) if "decoder_embed" in name: lowercase : List[str] = name.replace("""decoder_embed""" , """decoder.decoder_embed""" ) if "decoder_norm" in name: lowercase : Dict = name.replace("""decoder_norm""" , """decoder.decoder_norm""" ) if "decoder_pred" in name: lowercase : List[str] = name.replace("""decoder_pred""" , """decoder.decoder_pred""" ) if "norm.weight" in name and "decoder" not in name: lowercase : Tuple = name.replace("""norm.weight""" , """vit.layernorm.weight""" ) if "norm.bias" in name and "decoder" not in name: lowercase : int = name.replace("""norm.bias""" , """vit.layernorm.bias""" ) return name def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: for key in orig_state_dict.copy().keys(): lowercase : List[Any] = orig_state_dict.pop(SCREAMING_SNAKE_CASE__ ) if "qkv" in key: lowercase : int = key.split(""".""" ) lowercase : List[str] = int(key_split[1] ) if "decoder_blocks" in key: lowercase : Tuple = config.decoder_hidden_size lowercase : int = """decoder.decoder_layers.""" if "weight" in key: lowercase : List[Any] = val[:dim, :] lowercase : Tuple = val[dim : dim * 2, :] lowercase : List[Any] = val[-dim:, :] elif "bias" in key: lowercase : str = val[:dim] lowercase : Dict = val[dim : dim * 2] lowercase : Union[str, Any] = val[-dim:] else: lowercase : Tuple = config.hidden_size lowercase : Union[str, Any] = """vit.encoder.layer.""" if "weight" in key: lowercase : Tuple = val[:dim, :] lowercase : List[str] = val[dim : dim * 2, :] lowercase : Dict = val[-dim:, :] elif "bias" in key: lowercase : Any = val[:dim] lowercase : str = val[dim : dim * 2] lowercase : Union[str, Any] = val[-dim:] else: lowercase : Union[str, Any] = val return orig_state_dict def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> int: lowercase : int = ViTMAEConfig() if "large" in checkpoint_url: lowercase : Dict = 1_024 lowercase : str = 4_096 lowercase : Optional[Any] = 24 lowercase : Optional[Any] = 16 elif "huge" in checkpoint_url: lowercase : int = 14 lowercase : List[Any] = 1_280 lowercase : int = 5_120 lowercase : List[Any] = 32 lowercase : Any = 16 lowercase : List[str] = ViTMAEForPreTraining(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE__ , map_location="""cpu""" )["""model"""] lowercase : Tuple = ViTMAEImageProcessor(size=config.image_size ) lowercase : Optional[int] = convert_state_dict(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) model.load_state_dict(SCREAMING_SNAKE_CASE__ ) model.eval() lowercase : Union[str, Any] = """https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg""" lowercase : Union[str, Any] = Image.open(requests.get(SCREAMING_SNAKE_CASE__ , stream=SCREAMING_SNAKE_CASE__ ).raw ) lowercase : Optional[Any] = ViTMAEImageProcessor(size=config.image_size ) lowercase : List[Any] = image_processor(images=SCREAMING_SNAKE_CASE__ , return_tensors="""pt""" ) # forward pass torch.manual_seed(2 ) lowercase : int = model(**SCREAMING_SNAKE_CASE__ ) lowercase : str = outputs.logits if "large" in checkpoint_url: lowercase : List[Any] = torch.tensor( [[-0.7309, -0.7128, -1.0169], [-1.0161, -0.9058, -1.1878], [-1.0478, -0.9411, -1.1911]] ) elif "huge" in checkpoint_url: lowercase : Tuple = torch.tensor( [[-1.1599, -0.9199, -1.2221], [-1.1952, -0.9269, -1.2307], [-1.2143, -0.9337, -1.2262]] ) else: lowercase : List[str] = torch.tensor( [[-0.9192, -0.8481, -1.1259], [-1.1349, -1.0034, -1.2599], [-1.1757, -1.0429, -1.2726]] ) # verify logits assert torch.allclose(logits[0, :3, :3] , SCREAMING_SNAKE_CASE__ , atol=1e-4 ) print(f"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) print(f"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": lowercase : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--checkpoint_url""", default="""https://dl.fbaipublicfiles.com/mae/visualize/mae_visualize_vit_base.pth""", type=str, help="""URL of the checkpoint you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) lowercase : List[Any] = parser.parse_args() convert_vit_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
20
0
import json import os import unittest from transformers import BatchEncoding, LEDTokenizer, LEDTokenizerFast from transformers.models.led.tokenization_led import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, require_torch from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class a__ ( A__ , unittest.TestCase ): A = LEDTokenizer A = LEDTokenizerFast A = True def __UpperCamelCase ( self : List[str] ): """simple docstring""" super().setUp() SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "\u0120", "\u0120l", "\u0120n", "\u0120lo", "\u0120low", "er", "\u0120lowest", "\u0120newer", "\u0120wider", "<unk>", ] SCREAMING_SNAKE_CASE_ : Optional[Any] = dict(zip(_A,range(len(_A ) ) ) ) SCREAMING_SNAKE_CASE_ : Dict = ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""] SCREAMING_SNAKE_CASE_ : Tuple = {"unk_token": "<unk>"} SCREAMING_SNAKE_CASE_ : List[str] = os.path.join(self.tmpdirname,VOCAB_FILES_NAMES["vocab_file"] ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = 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(_A ) + "\n" ) with open(self.merges_file,"w",encoding="utf-8" ) as fp: fp.write("\n".join(_A ) ) def __UpperCamelCase ( self : Tuple,**_A : List[str] ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname,**_A ) def __UpperCamelCase ( self : str,**_A : Union[str, Any] ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname,**_A ) def __UpperCamelCase ( self : Tuple,_A : Any ): """simple docstring""" return "lower newer", "lower newer" @cached_property def __UpperCamelCase ( self : Tuple ): """simple docstring""" return LEDTokenizer.from_pretrained("allenai/led-base-16384" ) @cached_property def __UpperCamelCase ( self : List[Any] ): """simple docstring""" return LEDTokenizerFast.from_pretrained("allenai/led-base-16384" ) @require_torch def __UpperCamelCase ( self : Optional[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = ["A long paragraph for summarization.", "Another paragraph for summarization."] SCREAMING_SNAKE_CASE_ : List[Any] = [0, 250, 251, 1_7818, 13, 3_9186, 1938, 4, 2] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer(_A,max_length=len(_A ),padding=_A,return_tensors="pt" ) self.assertIsInstance(_A,_A ) self.assertEqual((2, 9),batch.input_ids.shape ) self.assertEqual((2, 9),batch.attention_mask.shape ) SCREAMING_SNAKE_CASE_ : List[str] = batch.input_ids.tolist()[0] self.assertListEqual(_A,_A ) @require_torch def __UpperCamelCase ( self : List[str] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : str = ["A long paragraph for summarization.", "Another paragraph for summarization."] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: SCREAMING_SNAKE_CASE_ : int = tokenizer(_A,padding=_A,return_tensors="pt" ) self.assertIn("input_ids",_A ) self.assertIn("attention_mask",_A ) self.assertNotIn("labels",_A ) self.assertNotIn("decoder_attention_mask",_A ) @require_torch def __UpperCamelCase ( self : Optional[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = [ "Summary of the text.", "Another summary.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer(text_target=_A,max_length=32,padding="max_length",return_tensors="pt" ) self.assertEqual(32,targets["input_ids"].shape[1] ) @require_torch def __UpperCamelCase ( self : Optional[Any] ): """simple docstring""" for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: SCREAMING_SNAKE_CASE_ : List[str] = tokenizer( ["I am a small frog" * 1024, "I am a small frog"],padding=_A,truncation=_A,return_tensors="pt" ) self.assertIsInstance(_A,_A ) self.assertEqual(batch.input_ids.shape,(2, 5122) ) @require_torch def __UpperCamelCase ( self : Optional[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = ["A long paragraph for summarization."] SCREAMING_SNAKE_CASE_ : str = [ "Summary of the text.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer(_A,return_tensors="pt" ) SCREAMING_SNAKE_CASE_ : Optional[int] = tokenizer(text_target=_A,return_tensors="pt" ) SCREAMING_SNAKE_CASE_ : Optional[int] = inputs["input_ids"] SCREAMING_SNAKE_CASE_ : Any = targets["input_ids"] self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item() ) self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item() ) @require_torch def __UpperCamelCase ( self : Optional[Any] ): """simple docstring""" for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: SCREAMING_SNAKE_CASE_ : str = ["Summary of the text.", "Another summary."] SCREAMING_SNAKE_CASE_ : Optional[int] = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1, -1]] SCREAMING_SNAKE_CASE_ : Optional[int] = tokenizer(_A,padding=_A ) SCREAMING_SNAKE_CASE_ : List[Any] = [[0] * len(_A ) for x in encoded_output["input_ids"]] SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer.pad(_A ) self.assertSequenceEqual(outputs["global_attention_mask"],_A ) def __UpperCamelCase ( self : List[Any] ): """simple docstring""" pass def __UpperCamelCase ( self : str ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})' ): SCREAMING_SNAKE_CASE_ : Optional[int] = self.rust_tokenizer_class.from_pretrained(_A,**_A ) SCREAMING_SNAKE_CASE_ : Dict = self.tokenizer_class.from_pretrained(_A,**_A ) SCREAMING_SNAKE_CASE_ : Any = "A, <mask> AllenNLP sentence." SCREAMING_SNAKE_CASE_ : Tuple = tokenizer_r.encode_plus(_A,add_special_tokens=_A,return_token_type_ids=_A ) SCREAMING_SNAKE_CASE_ : Any = tokenizer_p.encode_plus(_A,add_special_tokens=_A,return_token_type_ids=_A ) self.assertEqual(sum(tokens_r["token_type_ids"] ),sum(tokens_p["token_type_ids"] ) ) self.assertEqual( sum(tokens_r["attention_mask"] ) / len(tokens_r["attention_mask"] ),sum(tokens_p["attention_mask"] ) / len(tokens_p["attention_mask"] ),) SCREAMING_SNAKE_CASE_ : Dict = tokenizer_r.convert_ids_to_tokens(tokens_r["input_ids"] ) SCREAMING_SNAKE_CASE_ : str = tokenizer_p.convert_ids_to_tokens(tokens_p["input_ids"] ) self.assertSequenceEqual(tokens_p["input_ids"],[0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual(tokens_r["input_ids"],[0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual( _A,["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] ) self.assertSequenceEqual( _A,["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] )
18
import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=0.999 , SCREAMING_SNAKE_CASE__="cosine" , ) -> List[Any]: if alpha_transform_type == "cosine": def alpha_bar_fn(SCREAMING_SNAKE_CASE__ ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(SCREAMING_SNAKE_CASE__ ): return math.exp(t * -12.0 ) else: raise ValueError(f"Unsupported alpha_tranform_type: {alpha_transform_type}" ) lowercase : int = [] for i in range(SCREAMING_SNAKE_CASE__ ): lowercase : Dict = i / num_diffusion_timesteps lowercase : Optional[int] = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(SCREAMING_SNAKE_CASE__ ) / alpha_bar_fn(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) ) return torch.tensor(SCREAMING_SNAKE_CASE__ , dtype=torch.floataa ) class __snake_case ( lowerCAmelCase , lowerCAmelCase ): _a : Tuple= [e.name for e in KarrasDiffusionSchedulers] _a : int= 2 @register_to_config def __init__( self ,snake_case = 1000 ,snake_case = 0.00_085 ,snake_case = 0.012 ,snake_case = "linear" ,snake_case = None ,snake_case = "epsilon" ,snake_case = False ,snake_case = False ,snake_case = 1.0 ,snake_case = "linspace" ,snake_case = 0 ,): '''simple docstring''' if trained_betas is not None: lowercase : List[str] = torch.tensor(snake_case ,dtype=torch.floataa ) elif beta_schedule == "linear": lowercase : Optional[Any] = torch.linspace(snake_case ,snake_case ,snake_case ,dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. lowercase : int = ( torch.linspace(beta_start**0.5 ,beta_end**0.5 ,snake_case ,dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule lowercase : Union[str, Any] = betas_for_alpha_bar(snake_case ,alpha_transform_type="""cosine""" ) elif beta_schedule == "exp": lowercase : int = betas_for_alpha_bar(snake_case ,alpha_transform_type="""exp""" ) else: raise NotImplementedError(f"{beta_schedule} does is not implemented for {self.__class__}" ) lowercase : Any = 1.0 - self.betas lowercase : Dict = torch.cumprod(self.alphas ,dim=0 ) # set all values self.set_timesteps(snake_case ,snake_case ,snake_case ) lowercase : Tuple = use_karras_sigmas def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ): '''simple docstring''' if schedule_timesteps is None: lowercase : Union[str, Any] = self.timesteps lowercase : Dict = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: lowercase : Dict = 1 if len(snake_case ) > 1 else 0 else: lowercase : Union[str, Any] = timestep.cpu().item() if torch.is_tensor(snake_case ) else timestep lowercase : str = self._index_counter[timestep_int] return indices[pos].item() @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Optional[Any] = self.index_for_timestep(snake_case ) lowercase : Dict = self.sigmas[step_index] lowercase : List[str] = sample / ((sigma**2 + 1) ** 0.5) return sample def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = None ,snake_case = None ,): '''simple docstring''' lowercase : Any = num_inference_steps lowercase : List[Any] = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": lowercase : Optional[int] = np.linspace(0 ,num_train_timesteps - 1 ,snake_case ,dtype=snake_case )[::-1].copy() elif self.config.timestep_spacing == "leading": lowercase : int = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowercase : List[str] = (np.arange(0 ,snake_case ) * step_ratio).round()[::-1].copy().astype(snake_case ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": lowercase : List[str] = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowercase : Optional[int] = (np.arange(snake_case ,0 ,-step_ratio )).round().copy().astype(snake_case ) timesteps -= 1 else: raise ValueError( f"{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'." ) lowercase : Optional[int] = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) lowercase : Dict = np.log(snake_case ) lowercase : Union[str, Any] = np.interp(snake_case ,np.arange(0 ,len(snake_case ) ) ,snake_case ) if self.config.use_karras_sigmas: lowercase : List[Any] = self._convert_to_karras(in_sigmas=snake_case ,num_inference_steps=self.num_inference_steps ) lowercase : Tuple = np.array([self._sigma_to_t(snake_case ,snake_case ) for sigma in sigmas] ) lowercase : Any = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) lowercase : List[Any] = torch.from_numpy(snake_case ).to(device=snake_case ) lowercase : List[Any] = torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2 ), sigmas[-1:]] ) lowercase : Dict = torch.from_numpy(snake_case ) lowercase : List[Any] = torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2 )] ) if str(snake_case ).startswith("""mps""" ): # mps does not support float64 lowercase : Any = timesteps.to(snake_case ,dtype=torch.floataa ) else: lowercase : str = timesteps.to(device=snake_case ) # empty dt and derivative lowercase : Union[str, Any] = None lowercase : Any = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter lowercase : str = defaultdict(snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : Optional[int] = np.log(snake_case ) # get distribution lowercase : Union[str, Any] = log_sigma - log_sigmas[:, np.newaxis] # get sigmas range lowercase : Optional[int] = np.cumsum((dists >= 0) ,axis=0 ).argmax(axis=0 ).clip(max=log_sigmas.shape[0] - 2 ) lowercase : Any = low_idx + 1 lowercase : str = log_sigmas[low_idx] lowercase : Dict = log_sigmas[high_idx] # interpolate sigmas lowercase : int = (low - log_sigma) / (low - high) lowercase : Dict = np.clip(snake_case ,0 ,1 ) # transform interpolation to time range lowercase : Optional[Any] = (1 - w) * low_idx + w * high_idx lowercase : Tuple = t.reshape(sigma.shape ) return t def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : float = in_sigmas[-1].item() lowercase : float = in_sigmas[0].item() lowercase : Dict = 7.0 # 7.0 is the value used in the paper lowercase : Optional[int] = np.linspace(0 ,1 ,snake_case ) lowercase : int = sigma_min ** (1 / rho) lowercase : Any = sigma_max ** (1 / rho) lowercase : Tuple = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho return sigmas @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return self.dt is None def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case = True ,): '''simple docstring''' lowercase : Union[str, Any] = self.index_for_timestep(snake_case ) # advance index counter by 1 lowercase : Optional[int] = timestep.cpu().item() if torch.is_tensor(snake_case ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: lowercase : str = self.sigmas[step_index] lowercase : Optional[int] = self.sigmas[step_index + 1] else: # 2nd order / Heun's method lowercase : Dict = self.sigmas[step_index - 1] lowercase : Optional[Any] = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API lowercase : Union[str, Any] = 0 lowercase : Any = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": lowercase : Any = sigma_hat if self.state_in_first_order else sigma_next lowercase : int = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": lowercase : Union[str, Any] = sigma_hat if self.state_in_first_order else sigma_next lowercase : Optional[Any] = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": lowercase : Optional[Any] = model_output else: raise ValueError( f"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`" ) if self.config.clip_sample: lowercase : str = pred_original_sample.clamp( -self.config.clip_sample_range ,self.config.clip_sample_range ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order lowercase : Optional[int] = (sample - pred_original_sample) / sigma_hat # 3. delta timestep lowercase : Union[str, Any] = sigma_next - sigma_hat # store for 2nd order step lowercase : Optional[int] = derivative lowercase : Union[str, Any] = dt lowercase : Union[str, Any] = sample else: # 2. 2nd order / Heun's method lowercase : Tuple = (sample - pred_original_sample) / sigma_next lowercase : Dict = (self.prev_derivative + derivative) / 2 # 3. take prev timestep & sample lowercase : Tuple = self.dt lowercase : Optional[Any] = self.sample # free dt and derivative # Note, this puts the scheduler in "first order mode" lowercase : List[str] = None lowercase : Tuple = None lowercase : Dict = None lowercase : List[Any] = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Optional[int] = self.sigmas.to(device=original_samples.device ,dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(snake_case ): # mps does not support float64 lowercase : List[Any] = self.timesteps.to(original_samples.device ,dtype=torch.floataa ) lowercase : List[str] = timesteps.to(original_samples.device ,dtype=torch.floataa ) else: lowercase : List[str] = self.timesteps.to(original_samples.device ) lowercase : Tuple = timesteps.to(original_samples.device ) lowercase : Any = [self.index_for_timestep(snake_case ,snake_case ) for t in timesteps] lowercase : int = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): lowercase : Any = sigma.unsqueeze(-1 ) lowercase : Optional[int] = original_samples + noise * sigma return noisy_samples def __len__( self ): '''simple docstring''' return self.config.num_train_timesteps
20
0
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow if is_tf_available(): import numpy as np import tensorflow as tf from transformers import TFXLMRobertaModel @require_tf @require_sentencepiece @require_tokenizers class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE_( self ) -> Dict: lowerCamelCase_ = TFXLMRobertaModel.from_pretrained("jplu/tf-xlm-roberta-base" ) lowerCamelCase_ = { "input_ids": tf.convert_to_tensor([[0, 2646, 10269, 83, 99942, 2]] , dtype=tf.intaa ), # "My dog is cute" "attention_mask": tf.convert_to_tensor([[1, 1, 1, 1, 1, 1]] , dtype=tf.intaa ), } lowerCamelCase_ = model(lowercase )["last_hidden_state"] lowerCamelCase_ = tf.TensorShape((1, 6, 768) ) self.assertEqual(output.shape , lowercase ) # compare the actual values for a slice. lowerCamelCase_ = tf.convert_to_tensor( [ [ [0.0_6_8_1_7_6_2, 0.1_0_8_9_4_4_5_1, 0.0_6_7_7_2_5_0_4], [-0.0_6_4_2_3_6_6_8, 0.0_2_3_6_6_6_1_5, 0.0_4_3_2_9_3_4_4], [-0.0_6_0_5_7_2_9_5, 0.0_9_9_7_4_1_3_5, -0.0_0_0_7_0_5_8_4], ] ] , dtype=tf.floataa , ) self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
19
from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch lowercase : List[str] = logging.get_logger(__name__) @add_end_docstrings( lowerCAmelCase , R"\n top_k (`int`, defaults to 5):\n The number of predictions to return.\n targets (`str` or `List[str]`, *optional*):\n When passed, the model will limit the scores to the passed targets instead of looking up in the whole\n vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting\n token will be used (with a warning, and that might be slower).\n\n " , ) class __snake_case ( lowerCAmelCase ): def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' if self.framework == "tf": lowercase : str = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": lowercase : Optional[int] = torch.nonzero(input_ids == self.tokenizer.mask_token_id ,as_tuple=snake_case ) else: raise ValueError("""Unsupported framework""" ) return masked_index def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Tuple = self.get_masked_index(snake_case ) lowercase : Dict = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( """fill-mask""" ,self.model.base_model_prefix ,f"No mask_token ({self.tokenizer.mask_token}) found on the input" ,) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' if isinstance(snake_case ,snake_case ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input["""input_ids"""][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ,**snake_case ): '''simple docstring''' if return_tensors is None: lowercase : int = self.framework lowercase : Optional[Any] = self.tokenizer(snake_case ,return_tensors=snake_case ) self.ensure_exactly_one_mask_token(snake_case ) return model_inputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Optional[int] = self.model(**snake_case ) lowercase : Tuple = model_inputs["""input_ids"""] return model_outputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=5 ,snake_case=None ): '''simple docstring''' if target_ids is not None and target_ids.shape[0] < top_k: lowercase : str = target_ids.shape[0] lowercase : Optional[Any] = model_outputs["""input_ids"""][0] lowercase : List[str] = model_outputs["""logits"""] if self.framework == "tf": lowercase : List[str] = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] lowercase : Tuple = outputs.numpy() lowercase : Tuple = outputs[0, masked_index, :] lowercase : Any = stable_softmax(snake_case ,axis=-1 ) if target_ids is not None: lowercase : Union[str, Any] = tf.gather_nd(tf.squeeze(snake_case ,0 ) ,target_ids.reshape(-1 ,1 ) ) lowercase : int = tf.expand_dims(snake_case ,0 ) lowercase : Tuple = tf.math.top_k(snake_case ,k=snake_case ) lowercase , lowercase : int = topk.values.numpy(), topk.indices.numpy() else: lowercase : Optional[Any] = torch.nonzero(input_ids == self.tokenizer.mask_token_id ,as_tuple=snake_case ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample lowercase : Union[str, Any] = outputs[0, masked_index, :] lowercase : Tuple = logits.softmax(dim=-1 ) if target_ids is not None: lowercase : List[str] = probs[..., target_ids] lowercase , lowercase : Union[str, Any] = probs.topk(snake_case ) lowercase : Any = [] lowercase : List[Any] = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() ,predictions.tolist() ) ): lowercase : Dict = [] for v, p in zip(_values ,_predictions ): # Copy is important since we're going to modify this array in place lowercase : Dict = input_ids.numpy().copy() if target_ids is not None: lowercase : Union[str, Any] = target_ids[p].tolist() lowercase : Tuple = p # Filter padding out: lowercase : List[str] = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back lowercase : Tuple = self.tokenizer.decode(snake_case ,skip_special_tokens=snake_case ) lowercase : Optional[Any] = {"""score""": v, """token""": p, """token_str""": self.tokenizer.decode([p] ), """sequence""": sequence} row.append(snake_case ) result.append(snake_case ) if single_mask: return result[0] return result def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ): '''simple docstring''' if isinstance(snake_case ,snake_case ): lowercase : List[Any] = [targets] try: lowercase : List[str] = self.tokenizer.get_vocab() except Exception: lowercase : Any = {} lowercase : Dict = [] for target in targets: lowercase : Dict = vocab.get(snake_case ,snake_case ) if id_ is None: lowercase : Optional[int] = self.tokenizer( snake_case ,add_special_tokens=snake_case ,return_attention_mask=snake_case ,return_token_type_ids=snake_case ,max_length=1 ,truncation=snake_case ,)["""input_ids"""] if len(snake_case ) == 0: logger.warning( f"The specified target token `{target}` does not exist in the model vocabulary. " """We cannot replace it with anything meaningful, ignoring it""" ) continue lowercase : Union[str, Any] = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( f"The specified target token `{target}` does not exist in the model vocabulary. " f"Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`." ) target_ids.append(id_ ) lowercase : Optional[Any] = list(set(snake_case ) ) if len(snake_case ) == 0: raise ValueError("""At least one target must be provided when passed.""" ) lowercase : Optional[Any] = np.array(snake_case ) return target_ids def _SCREAMING_SNAKE_CASE ( self ,snake_case=None ,snake_case=None ): '''simple docstring''' lowercase : Dict = {} if targets is not None: lowercase : str = self.get_target_ids(snake_case ,snake_case ) lowercase : List[Any] = target_ids if top_k is not None: lowercase : List[str] = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( """fill-mask""" ,self.model.base_model_prefix ,"""The tokenizer does not define a `mask_token`.""" ) return {}, {}, postprocess_params def __call__( self ,snake_case ,*snake_case ,**snake_case ): '''simple docstring''' lowercase : Tuple = super().__call__(snake_case ,**snake_case ) if isinstance(snake_case ,snake_case ) and len(snake_case ) == 1: return outputs[0] return outputs
20
0
from __future__ import annotations from math import ceil, floor, sqrt def UpperCamelCase_( lowerCamelCase_ = 200_0000 ) -> int: _lowercase : list[int] = [0] _lowercase : int for idx in range(1 , ceil(sqrt(target * 2 ) * 1.1 ) ): triangle_numbers.append(triangle_numbers[-1] + idx ) # we want this to be as close as possible to target _lowercase : int = 0 # the area corresponding to the grid that gives the product closest to target _lowercase : int = 0 # an estimate of b, using the quadratic formula _lowercase : float # the largest integer less than b_estimate _lowercase : int # the largest integer less than b_estimate _lowercase : int # the triangle number corresponding to b_floor _lowercase : int # the triangle number corresponding to b_ceil _lowercase : int for idx_a, triangle_a in enumerate(triangle_numbers[1:] , 1 ): _lowercase : Optional[int] = (-1 + sqrt(1 + 8 * target / triangle_a )) / 2 _lowercase : List[str] = floor(lowerCamelCase_ ) _lowercase : Dict = ceil(lowerCamelCase_ ) _lowercase : List[str] = triangle_numbers[b_floor] _lowercase : List[str] = triangle_numbers[b_ceil] if abs(target - triangle_b_first_guess * triangle_a ) < abs( target - best_product ): _lowercase : Union[str, Any] = triangle_b_first_guess * triangle_a _lowercase : Union[str, Any] = idx_a * b_floor if abs(target - triangle_b_second_guess * triangle_a ) < abs( target - best_product ): _lowercase : Any = triangle_b_second_guess * triangle_a _lowercase : Optional[Any] = idx_a * b_ceil return area if __name__ == "__main__": print(F"{solution() = }")
21
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class __snake_case ( unittest.TestCase ): def __init__( self ,snake_case ,snake_case=7 ,snake_case=3 ,snake_case=18 ,snake_case=30 ,snake_case=400 ,snake_case=True ,snake_case=None ,snake_case=True ,snake_case=None ,): '''simple docstring''' lowercase : Dict = size if size is not None else {"""shortest_edge""": 20} lowercase : Union[str, Any] = crop_size if crop_size is not None else {"""height""": 18, """width""": 18} lowercase : str = parent lowercase : int = batch_size lowercase : str = num_channels lowercase : int = image_size lowercase : List[str] = min_resolution lowercase : str = max_resolution lowercase : Dict = do_resize lowercase : Dict = size lowercase : Dict = do_center_crop lowercase : str = crop_size def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, } @require_torch @require_vision class __snake_case ( lowerCAmelCase , unittest.TestCase ): _a : Any= MobileNetVaImageProcessor if is_vision_available() else None def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = MobileNetVaImageProcessingTester(self ) @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[int] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case ,"""do_resize""" ) ) self.assertTrue(hasattr(snake_case ,"""size""" ) ) self.assertTrue(hasattr(snake_case ,"""do_center_crop""" ) ) self.assertTrue(hasattr(snake_case ,"""crop_size""" ) ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : int = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{"""shortest_edge""": 20} ) self.assertEqual(image_processor.crop_size ,{"""height""": 18, """width""": 18} ) lowercase : int = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ,crop_size=84 ) self.assertEqual(image_processor.size ,{"""shortest_edge""": 42} ) self.assertEqual(image_processor.crop_size ,{"""height""": 84, """width""": 84} ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' pass def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowercase : str = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case ,Image.Image ) # Test not batched input lowercase : Dict = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) # Test batched lowercase : Tuple = image_processing(snake_case ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowercase : Union[str, Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case ,numpify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case ,np.ndarray ) # Test not batched input lowercase : Optional[Any] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) # Test batched lowercase : List[str] = image_processing(snake_case ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowercase : Tuple = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case ,torchify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case ,torch.Tensor ) # Test not batched input lowercase : Optional[int] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) # Test batched lowercase : List[str] = image_processing(snake_case ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,)
20
0
'''simple docstring''' def UpperCAmelCase_ ( __lowercase : list[list] ) -> list[list]: '''simple docstring''' _UpperCAmelCase = current_set.copy() for row_index, row in enumerate(__lowercase ): _UpperCAmelCase = row[0] for column_index, column in enumerate(__lowercase ): if magnitude == 0: _UpperCAmelCase = column continue _UpperCAmelCase = column / magnitude # Subtract to cancel term _UpperCAmelCase = current_set[0] _UpperCAmelCase = [first_row] _UpperCAmelCase = current_set[1::] for row in current_set: _UpperCAmelCase = [] # If first term is 0, it is already in form we want, so we preserve it if row[0] == 0: final_set.append(__lowercase ) continue for column_index in range(len(__lowercase ) ): temp_row.append(first_row[column_index] - row[column_index] ) final_set.append(__lowercase ) # Create next recursion iteration set if len(final_set[0] ) != 3: _UpperCAmelCase = final_set[0] _UpperCAmelCase = [] _UpperCAmelCase = [] for row in final_set[1::]: current_first_column.append(row[0] ) next_iteration.append(row[1::] ) _UpperCAmelCase = simplify(__lowercase ) for i in range(len(__lowercase ) ): resultant[i].insert(0 , current_first_column[i] ) resultant.insert(0 , __lowercase ) _UpperCAmelCase = resultant return final_set def UpperCAmelCase_ ( __lowercase : list[list] ) -> list: '''simple docstring''' if len(__lowercase ) == 0: raise IndexError("solve_simultaneous() requires n lists of length n+1" ) _UpperCAmelCase = len(__lowercase ) + 1 if any(len(__lowercase ) != _length for item in equations ): raise IndexError("solve_simultaneous() requires n lists of length n+1" ) for row in equations: if any(not isinstance(__lowercase , (int, float) ) for column in row ): raise ValueError("solve_simultaneous() requires lists of integers" ) if len(__lowercase ) == 1: return [equations[0][-1] / equations[0][0]] _UpperCAmelCase = equations.copy() if any(0 in row for row in data_set ): _UpperCAmelCase = data_set.copy() _UpperCAmelCase = [] for row_index, row in enumerate(__lowercase ): if 0 not in row: _UpperCAmelCase = data_set.pop(__lowercase ) break if not full_row: raise ValueError("solve_simultaneous() requires at least 1 full equation" ) data_set.insert(0 , __lowercase ) _UpperCAmelCase = data_set.copy() _UpperCAmelCase = simplify(__lowercase ) _UpperCAmelCase = simplified[::-1] _UpperCAmelCase = [] for row in simplified: _UpperCAmelCase = row[-1] if not solutions: if row[-2] == 0: solutions.append(0 ) continue solutions.append(current_solution / row[-2] ) continue _UpperCAmelCase = row.copy()[: len(__lowercase ) - 1 :] while temp_row[0] == 0: temp_row.pop(0 ) if len(__lowercase ) == 0: solutions.append(0 ) continue _UpperCAmelCase = temp_row[1::] _UpperCAmelCase = temp_row[::-1] for column_index, column in enumerate(__lowercase ): current_solution -= column * solutions[column_index] solutions.append(__lowercase ) _UpperCAmelCase = [] for item in solutions: final.append(float(round(__lowercase , 5 ) ) ) return final[::-1] if __name__ == "__main__": import doctest doctest.testmod() __SCREAMING_SNAKE_CASE :Tuple = [ [2, 1, 1, 1, 1, 4], [1, 2, 1, 1, 1, 5], [1, 1, 2, 1, 1, 6], [1, 1, 1, 2, 1, 7], [1, 1, 1, 1, 2, 8], ] print(solve_simultaneous(eq)) print(solve_simultaneous([[4, 2]]))
22
from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef import datasets lowercase : str = """\ @inproceedings{wang2019glue, title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding}, author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.}, note={In the Proceedings of ICLR.}, year={2019} } """ lowercase : Dict = """\ GLUE, the General Language Understanding Evaluation benchmark (https://gluebenchmark.com/) is a collection of resources for training, evaluating, and analyzing natural language understanding systems. """ lowercase : int = """ Compute GLUE evaluation metric associated to each GLUE dataset. Args: predictions: list of predictions to score. Each translation should be tokenized into a list of tokens. references: list of lists of references for each translation. Each reference should be tokenized into a list of tokens. Returns: depending on the GLUE subset, one or several of: \"accuracy\": Accuracy \"f1\": F1 score \"pearson\": Pearson Correlation \"spearmanr\": Spearman Correlation \"matthews_correlation\": Matthew Correlation Examples: >>> glue_metric = datasets.load_metric('glue', 'sst2') # 'sst2' or any of [\"mnli\", \"mnli_mismatched\", \"mnli_matched\", \"qnli\", \"rte\", \"wnli\", \"hans\"] >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0} >>> glue_metric = datasets.load_metric('glue', 'mrpc') # 'mrpc' or 'qqp' >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0, 'f1': 1.0} >>> glue_metric = datasets.load_metric('glue', 'stsb') >>> references = [0., 1., 2., 3., 4., 5.] >>> predictions = [0., 1., 2., 3., 4., 5.] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print({\"pearson\": round(results[\"pearson\"], 2), \"spearmanr\": round(results[\"spearmanr\"], 2)}) {'pearson': 1.0, 'spearmanr': 1.0} >>> glue_metric = datasets.load_metric('glue', 'cola') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'matthews_correlation': 1.0} """ def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[int]: return float((preds == labels).mean() ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: lowercase : Any = simple_accuracy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = float(fa_score(y_true=SCREAMING_SNAKE_CASE__ , y_pred=SCREAMING_SNAKE_CASE__ ) ) return { "accuracy": acc, "f1": fa, } def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: lowercase : Union[str, Any] = float(pearsonr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )[0] ) lowercase : Dict = float(spearmanr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )[0] ) return { "pearson": pearson_corr, "spearmanr": spearman_corr, } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __snake_case ( datasets.Metric ): def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self.config_name not in [ "sst2", "mnli", "mnli_mismatched", "mnli_matched", "cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans", ]: raise KeyError( """You should supply a configuration name selected in """ """[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", """ """\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]""" ) return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { """predictions""": datasets.Value("""int64""" if self.config_name != """stsb""" else """float32""" ), """references""": datasets.Value("""int64""" if self.config_name != """stsb""" else """float32""" ), } ) ,codebase_urls=[] ,reference_urls=[] ,format="""numpy""" ,) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' if self.config_name == "cola": return {"matthews_correlation": matthews_corrcoef(snake_case ,snake_case )} elif self.config_name == "stsb": return pearson_and_spearman(snake_case ,snake_case ) elif self.config_name in ["mrpc", "qqp"]: return acc_and_fa(snake_case ,snake_case ) elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]: return {"accuracy": simple_accuracy(snake_case ,snake_case )} else: raise KeyError( """You should supply a configuration name selected in """ """[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", """ """\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]""" )
20
0
'''simple docstring''' 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__: int = logging.get_logger(__name__) UpperCamelCase__: Tuple = {"vocab_file": "spiece.model"} UpperCamelCase__: List[Any] = { "vocab_file": { "TsinghuaAI/CPM-Generate": "https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model", } } class SCREAMING_SNAKE_CASE( A__ ): """simple docstring""" def __init__( self : Dict , __snake_case : Optional[Any] , __snake_case : Optional[Any]=False , __snake_case : str=True , __snake_case : List[Any]=False , __snake_case : Union[str, Any]="<s>" , __snake_case : Tuple="</s>" , __snake_case : str="<unk>" , __snake_case : int="<sep>" , __snake_case : str="<pad>" , __snake_case : Optional[int]="<cls>" , __snake_case : Dict="<mask>" , __snake_case : str=["<eop>", "<eod>"] , __snake_case : Optional[Dict[str, Any]] = None , **__snake_case : Any , ) -> None: UpperCAmelCase : Any = AddedToken(__snake_case , lstrip=__snake_case , rstrip=__snake_case ) if isinstance(__snake_case , __snake_case ) else mask_token UpperCAmelCase : Tuple = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__snake_case , remove_space=__snake_case , keep_accents=__snake_case , bos_token=__snake_case , eos_token=__snake_case , unk_token=__snake_case , sep_token=__snake_case , pad_token=__snake_case , cls_token=__snake_case , mask_token=__snake_case , additional_special_tokens=__snake_case , sp_model_kwargs=self.sp_model_kwargs , **__snake_case , ) UpperCAmelCase : Union[str, Any] = 3 UpperCAmelCase : str = do_lower_case UpperCAmelCase : Optional[Any] = remove_space UpperCAmelCase : str = keep_accents UpperCAmelCase : Optional[Any] = vocab_file UpperCAmelCase : List[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__snake_case ) 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.''' ) UpperCAmelCase : List[str] = jieba UpperCAmelCase : Any = str.maketrans(''' \n''' , '''\u2582\u2583''' ) @property # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size def A ( self : Tuple ) -> Dict: return len(self.sp_model ) def A ( self : Tuple ) -> int: UpperCAmelCase : Dict = {self.convert_ids_to_tokens(__snake_case ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Optional[int] ) -> Any: UpperCAmelCase : int = self.__dict__.copy() UpperCAmelCase : Optional[Any] = None return state def __setstate__( self : Any , __snake_case : Union[str, Any] ) -> Optional[Any]: UpperCAmelCase : List[str] = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): UpperCAmelCase : Dict = {} UpperCAmelCase : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def A ( self : int , __snake_case : int ) -> Tuple: if self.remove_space: UpperCAmelCase : Dict = ''' '''.join(inputs.strip().split() ) else: UpperCAmelCase : Any = inputs UpperCAmelCase : List[str] = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: UpperCAmelCase : int = unicodedata.normalize('''NFKD''' , __snake_case ) UpperCAmelCase : List[str] = ''''''.join([c for c in outputs if not unicodedata.combining(__snake_case )] ) if self.do_lower_case: UpperCAmelCase : Union[str, Any] = outputs.lower() return outputs def A ( self : Any , __snake_case : str ) -> List[str]: UpperCAmelCase : Dict = self.preprocess_text(__snake_case ) UpperCAmelCase : Union[str, Any] = self.sp_model.encode(__snake_case , out_type=__snake_case ) UpperCAmelCase : List[Any] = [] for piece in pieces: if len(__snake_case ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): UpperCAmelCase : List[str] = self.sp_model.EncodeAsPieces(piece[:-1].replace(__snake_case , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: UpperCAmelCase : Union[str, Any] = cur_pieces[1:] else: UpperCAmelCase : Dict = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(__snake_case ) else: new_pieces.append(__snake_case ) return new_pieces def A ( self : Dict , __snake_case : Tuple ) -> List[Any]: return self.sp_model.PieceToId(__snake_case ) def A ( self : Any , __snake_case : str ) -> List[Any]: return self.sp_model.IdToPiece(__snake_case ) def A ( self : str , __snake_case : Tuple ) -> Dict: UpperCAmelCase : List[Any] = ''''''.join(__snake_case ).replace(__snake_case , ''' ''' ).strip() return out_string def A ( self : Union[str, Any] , __snake_case : List[int] , __snake_case : Optional[List[int]] = None ) -> List[int]: UpperCAmelCase : Dict = [self.sep_token_id] UpperCAmelCase : int = [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 A ( self : Union[str, Any] , __snake_case : List[int] , __snake_case : Optional[List[int]] = None , __snake_case : bool = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__snake_case , token_ids_a=__snake_case , already_has_special_tokens=__snake_case ) if token_ids_a is not None: return ([0] * len(__snake_case )) + [1] + ([0] * len(__snake_case )) + [1, 1] return ([0] * len(__snake_case )) + [1, 1] def A ( self : Any , __snake_case : List[int] , __snake_case : Optional[List[int]] = None ) -> List[int]: UpperCAmelCase : Any = [self.sep_token_id] UpperCAmelCase : Union[str, Any] = [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 A ( self : Tuple , __snake_case : str , __snake_case : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(__snake_case ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return UpperCAmelCase : Any = os.path.join( __snake_case , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__snake_case ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __snake_case ) elif not os.path.isfile(self.vocab_file ): with open(__snake_case , '''wb''' ) as fi: UpperCAmelCase : Optional[Any] = self.sp_model.serialized_model_proto() fi.write(__snake_case ) return (out_vocab_file,) def A ( self : Optional[Any] , *__snake_case : Any , **__snake_case : Optional[Any] ) -> int: UpperCAmelCase : Any = super()._decode(*__snake_case , **__snake_case ) UpperCAmelCase : Any = text.replace(''' ''' , '''''' ).replace('''\u2582''' , ''' ''' ).replace('''\u2583''' , '''\n''' ) return text
23
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 __snake_case ( unittest.TestCase ): _a : Optional[int]= MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ): '''simple docstring''' lowercase : Any = hf_hub_download( repo_id="""nateraw/video-demo""" ,filename="""archery.mp4""" ,repo_type="""dataset""" ) lowercase : List[str] = VideoClassificationPipeline(model=snake_case ,image_processor=snake_case ,top_k=2 ) lowercase : Dict = [ example_video_filepath, """https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4""", ] return video_classifier, examples def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' for example in examples: lowercase : int = video_classifier(snake_case ) self.assertEqual( snake_case ,[ {"""score""": ANY(snake_case ), """label""": ANY(snake_case )}, {"""score""": ANY(snake_case ), """label""": ANY(snake_case )}, ] ,) @require_torch def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Any = """hf-internal-testing/tiny-random-VideoMAEForVideoClassification""" lowercase : str = VideoMAEFeatureExtractor( size={"""shortest_edge""": 10} ,crop_size={"""height""": 10, """width""": 10} ) lowercase : List[Any] = pipeline( """video-classification""" ,model=snake_case ,feature_extractor=snake_case ,frame_sampling_rate=4 ) lowercase : Dict = hf_hub_download(repo_id="""nateraw/video-demo""" ,filename="""archery.mp4""" ,repo_type="""dataset""" ) lowercase : Any = video_classifier(snake_case ,top_k=2 ) self.assertEqual( nested_simplify(snake_case ,decimals=4 ) ,[{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}] ,) lowercase : str = video_classifier( [ video_file_path, video_file_path, ] ,top_k=2 ,) self.assertEqual( nested_simplify(snake_case ,decimals=4 ) ,[ [{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}], [{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}], ] ,) @require_tf def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' pass
20
0
# Lint as: python3 import sys from collections.abc import Mapping from typing import TYPE_CHECKING import numpy as np import pyarrow as pa from .. import config from ..utils.py_utils import map_nested from .formatting import TensorFormatter if TYPE_CHECKING: import torch class SCREAMING_SNAKE_CASE__ ( TensorFormatter[Mapping, 'torch.Tensor', Mapping] ): def __init__(self : Dict , a__ : Union[str, Any]=None , **a__ : Tuple ): """simple docstring""" super().__init__(features=a__ ) __snake_case = torch_tensor_kwargs import torch # noqa import torch at initialization def a (self : Any , a__ : Tuple ): """simple docstring""" import torch if isinstance(a__ , a__ ) and column: if all( isinstance(a__ , torch.Tensor ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column ): return torch.stack(a__ ) return column def a (self : Union[str, Any] , a__ : Dict ): """simple docstring""" import torch if isinstance(a__ , (str, bytes, type(a__ )) ): return value elif isinstance(a__ , (np.character, np.ndarray) ) and np.issubdtype(value.dtype , np.character ): return value.tolist() __snake_case = {} if isinstance(a__ , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.integer ): __snake_case = {'''dtype''': torch.intaa} elif isinstance(a__ , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.floating ): __snake_case = {'''dtype''': torch.floataa} elif config.PIL_AVAILABLE and "PIL" in sys.modules: import PIL.Image if isinstance(a__ , PIL.Image.Image ): __snake_case = np.asarray(a__ ) return torch.tensor(a__ , **{**default_dtype, **self.torch_tensor_kwargs} ) def a (self : Any , a__ : Union[str, Any] ): """simple docstring""" import torch # support for torch, tf, jax etc. if hasattr(a__ , '''__array__''' ) and not isinstance(a__ , torch.Tensor ): __snake_case = data_struct.__array__() # support for nested types like struct of list of struct if isinstance(a__ , np.ndarray ): if data_struct.dtype == object: # torch tensors cannot be instantied from an array of objects return self._consolidate([self.recursive_tensorize(a__ ) for substruct in data_struct] ) elif isinstance(a__ , (list, tuple) ): return self._consolidate([self.recursive_tensorize(a__ ) for substruct in data_struct] ) return self._tensorize(a__ ) def a (self : str , a__ : dict ): """simple docstring""" return map_nested(self._recursive_tensorize , a__ , map_list=a__ ) def a (self : List[Any] , a__ : pa.Table ): """simple docstring""" __snake_case = self.numpy_arrow_extractor().extract_row(a__ ) __snake_case = self.python_features_decoder.decode_row(a__ ) return self.recursive_tensorize(a__ ) def a (self : str , a__ : pa.Table ): """simple docstring""" __snake_case = self.numpy_arrow_extractor().extract_column(a__ ) __snake_case = self.python_features_decoder.decode_column(a__ , pa_table.column_names[0] ) __snake_case = self.recursive_tensorize(a__ ) __snake_case = self._consolidate(a__ ) return column def a (self : List[Any] , a__ : pa.Table ): """simple docstring""" __snake_case = self.numpy_arrow_extractor().extract_batch(a__ ) __snake_case = self.python_features_decoder.decode_batch(a__ ) __snake_case = self.recursive_tensorize(a__ ) for column_name in batch: __snake_case = self._consolidate(batch[column_name] ) return batch
24
from __future__ import annotations from collections import namedtuple from dataclasses import dataclass @dataclass class __snake_case : _a : int _a : TreeNode | None= None _a : TreeNode | None= None lowercase : Dict = namedtuple("""CoinsDistribResult""", """moves excess""") def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: if root is None: return 0 # Validation def count_nodes(SCREAMING_SNAKE_CASE__ ) -> int: if node is None: return 0 return count_nodes(node.left ) + count_nodes(node.right ) + 1 def count_coins(SCREAMING_SNAKE_CASE__ ) -> int: if node is None: return 0 return count_coins(node.left ) + count_coins(node.right ) + node.data if count_nodes(SCREAMING_SNAKE_CASE__ ) != count_coins(SCREAMING_SNAKE_CASE__ ): raise ValueError("""The nodes number should be same as the number of coins""" ) # Main calculation def get_distrib(SCREAMING_SNAKE_CASE__ ) -> CoinsDistribResult: if node is None: return CoinsDistribResult(0 , 1 ) lowercase , lowercase : int = get_distrib(node.left ) lowercase , lowercase : List[Any] = get_distrib(node.right ) lowercase : Optional[Any] = 1 - left_distrib_excess lowercase : Union[str, Any] = 1 - right_distrib_excess lowercase : List[Any] = ( left_distrib_moves + right_distrib_moves + abs(SCREAMING_SNAKE_CASE__ ) + abs(SCREAMING_SNAKE_CASE__ ) ) lowercase : Any = node.data - coins_to_left - coins_to_right return CoinsDistribResult(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return get_distrib(SCREAMING_SNAKE_CASE__ )[0] if __name__ == "__main__": import doctest doctest.testmod()
20
0
"""simple docstring""" import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class lowerCAmelCase_ (a__ ): """simple docstring""" __UpperCamelCase : int = 0 __UpperCamelCase : bool = False __UpperCamelCase : float = 3.0 class lowerCAmelCase_ (unittest.TestCase ): """simple docstring""" def __magic_name__ (self ) -> List[str]: """simple docstring""" self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {"""a""": 2} ) self.assertDictEqual(MockClass(a=2 , b=SCREAMING_SNAKE_CASE__ ).to_kwargs() , {"""a""": 2, """b""": True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {"""a""": 2, """c""": 2.25} ) @require_cuda def __magic_name__ (self ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ : Tuple = GradScalerKwargs(init_scale=10_24 , growth_factor=2 ) AcceleratorState._reset_state() SCREAMING_SNAKE_CASE__ : Tuple = Accelerator(mixed_precision="""fp16""" , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) SCREAMING_SNAKE_CASE__ : str = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 20_00 ) self.assertEqual(scaler._enabled , SCREAMING_SNAKE_CASE__ ) @require_multi_gpu def __magic_name__ (self ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : int = ["""torchrun""", F'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(SCREAMING_SNAKE_CASE__ , env=os.environ.copy() ) if __name__ == "__main__": UpperCAmelCase__ : Optional[Any] = DistributedDataParallelKwargs(bucket_cap_mb=1_5, find_unused_parameters=True) UpperCAmelCase__ : Union[str, Any] = Accelerator(kwargs_handlers=[ddp_scaler]) UpperCAmelCase__ : Any = torch.nn.Linear(1_0_0, 2_0_0) UpperCAmelCase__ : Optional[Any] = accelerator.prepare(model) # Check the values changed in kwargs UpperCAmelCase__ : Tuple = '' UpperCAmelCase__ : List[str] = model.bucket_bytes_cap // (1_0_2_4 * 1_0_2_4) if observed_bucket_cap_map != 1_5: error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
25
from collections import UserDict from typing import Union import numpy as np import requests from ..utils import ( add_end_docstrings, logging, ) from .audio_classification import ffmpeg_read from .base import PIPELINE_INIT_ARGS, Pipeline lowercase : Optional[Any] = logging.get_logger(__name__) @add_end_docstrings(lowerCAmelCase ) class __snake_case ( lowerCAmelCase ): def __init__( self ,**snake_case ): '''simple docstring''' super().__init__(**snake_case ) if self.framework != "pt": raise ValueError(f"The {self.__class__} is only available in PyTorch." ) # No specific FOR_XXX available yet def __call__( self ,snake_case ,**snake_case ): '''simple docstring''' return super().__call__(snake_case ,**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,**snake_case ): '''simple docstring''' lowercase : Union[str, Any] = {} if "candidate_labels" in kwargs: lowercase : List[str] = kwargs["""candidate_labels"""] if "hypothesis_template" in kwargs: lowercase : Dict = kwargs["""hypothesis_template"""] return preprocess_params, {}, {} def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ,snake_case="This is a sound of {}." ): '''simple docstring''' if isinstance(snake_case ,snake_case ): if audio.startswith("""http://""" ) or audio.startswith("""https://""" ): # We need to actually check for a real protocol, otherwise it's impossible to use a local file # like http_huggingface_co.png lowercase : Optional[Any] = requests.get(snake_case ).content else: with open(snake_case ,"""rb""" ) as f: lowercase : Union[str, Any] = f.read() if isinstance(snake_case ,snake_case ): lowercase : int = ffmpeg_read(snake_case ,self.feature_extractor.sampling_rate ) if not isinstance(snake_case ,np.ndarray ): raise ValueError("""We expect a numpy ndarray as input""" ) if len(audio.shape ) != 1: raise ValueError("""We expect a single channel audio input for ZeroShotAudioClassificationPipeline""" ) lowercase : Dict = self.feature_extractor( [audio] ,sampling_rate=self.feature_extractor.sampling_rate ,return_tensors="""pt""" ) lowercase : Tuple = candidate_labels lowercase : Tuple = [hypothesis_template.format(snake_case ) for x in candidate_labels] lowercase : Optional[Any] = self.tokenizer(snake_case ,return_tensors=self.framework ,padding=snake_case ) lowercase : Optional[Any] = [text_inputs] return inputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : List[str] = model_inputs.pop("""candidate_labels""" ) lowercase : Dict = model_inputs.pop("""text_inputs""" ) if isinstance(text_inputs[0] ,snake_case ): lowercase : List[Any] = text_inputs[0] else: # Batching case. lowercase : Dict = text_inputs[0][0] lowercase : Optional[Any] = self.model(**snake_case ,**snake_case ) lowercase : Any = { """candidate_labels""": candidate_labels, """logits""": outputs.logits_per_audio, } return model_outputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : List[Any] = model_outputs.pop("""candidate_labels""" ) lowercase : Any = model_outputs["""logits"""][0] if self.framework == "pt": lowercase : Any = logits.softmax(dim=0 ) lowercase : Tuple = probs.tolist() else: raise ValueError("""`tf` framework not supported.""" ) lowercase : Tuple = [ {"""score""": score, """label""": candidate_label} for score, candidate_label in sorted(zip(snake_case ,snake_case ) ,key=lambda snake_case : -x[0] ) ] return result
20
0
_snake_case = "\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n" _snake_case = [{"type": "code", "content": INSTALL_CONTENT}] _snake_case = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
26
import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _snake_case( *SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__=True , SCREAMING_SNAKE_CASE__=2 ) -> Optional[Any]: from .. import __version__ lowercase : int = take_from lowercase : Tuple = () if not isinstance(args[0] , SCREAMING_SNAKE_CASE__ ): lowercase : Dict = (args,) for attribute, version_name, message in args: if version.parse(version.parse(SCREAMING_SNAKE_CASE__ ).base_version ) >= version.parse(SCREAMING_SNAKE_CASE__ ): raise ValueError( f"The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers'" f" version {__version__} is >= {version_name}" ) lowercase : int = None if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(SCREAMING_SNAKE_CASE__ ),) lowercase : Union[str, Any] = f"The `{attribute}` argument is deprecated and will be removed in version {version_name}." elif hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): values += (getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ),) lowercase : int = f"The `{attribute}` attribute is deprecated and will be removed in version {version_name}." elif deprecated_kwargs is None: lowercase : Dict = f"`{attribute}` is deprecated and will be removed in version {version_name}." if warning is not None: lowercase : Dict = warning + """ """ if standard_warn else """""" warnings.warn(warning + message , SCREAMING_SNAKE_CASE__ , stacklevel=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and len(SCREAMING_SNAKE_CASE__ ) > 0: lowercase : str = inspect.getouterframes(inspect.currentframe() )[1] lowercase : List[str] = call_frame.filename lowercase : Tuple = call_frame.lineno lowercase : List[str] = call_frame.function lowercase , lowercase : Optional[Any] = next(iter(deprecated_kwargs.items() ) ) raise TypeError(f"{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`" ) if len(SCREAMING_SNAKE_CASE__ ) == 0: return elif len(SCREAMING_SNAKE_CASE__ ) == 1: return values[0] return values
20
0
'''simple docstring''' # # 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 lowerCamelCase (*_SCREAMING_SNAKE_CASE : int ): with open(_SCREAMING_SNAKE_CASE , 'r' ) as fh: fcntl.flock(_SCREAMING_SNAKE_CASE , fcntl.LOCK_EX ) try: print(*_SCREAMING_SNAKE_CASE ) finally: fcntl.flock(_SCREAMING_SNAKE_CASE , fcntl.LOCK_UN ) __lowercase : Dict = int(os.environ['LOCAL_RANK']) torch.cuda.set_device(local_rank) __lowercase : Tuple = torch.device('cuda', local_rank) __lowercase : Optional[int] = socket.gethostname() __lowercase : List[str] = 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 __lowercase : str = dist.get_rank() __lowercase : Union[str, Any] = 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
27
def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: if index == r: for j in range(SCREAMING_SNAKE_CASE__ ): print(data[j] , end=""" """ ) print(""" """ ) return # When no more elements are there to put in data[] if i >= n: return # current is included, put next at next location lowercase : Tuple = arr[i] combination_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , index + 1 , SCREAMING_SNAKE_CASE__ , i + 1 ) # current is excluded, replace it with # next (Note that i+1 is passed, but # index is not changed) combination_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , i + 1 ) # The main function that prints all combinations # of size r in arr[] of size n. This function # mainly uses combinationUtil() def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: # A temporary array to store all combination one by one lowercase : Optional[int] = [0] * r # Print all combination using temporary array 'data[]' combination_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , 0 , SCREAMING_SNAKE_CASE__ , 0 ) if __name__ == "__main__": # Driver code to check the function above lowercase : int = [10, 20, 30, 40, 50] print_combination(arr, len(arr), 3) # This code is contributed by Ambuj sahu
20
0
'''simple docstring''' from math import sqrt def __lowerCamelCase ( A__ ) -> int: """simple docstring""" UpperCamelCase = 0 for i in range(1 , int(sqrt(A__ ) + 1 ) ): if n % i == 0 and i != sqrt(A__ ): total += i + n // i elif i == sqrt(A__ ): total += i return total - n def __lowerCamelCase ( A__ = 10_000 ) -> int: """simple docstring""" UpperCamelCase = sum( i for i in range(1 , A__ ) if sum_of_divisors(sum_of_divisors(A__ ) ) == i and sum_of_divisors(A__ ) != i ) return total if __name__ == "__main__": print(solution(int(str(input()).strip())))
28
import contextlib import os import sqlitea import pytest from datasets import Dataset, Features, Value from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) 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 @require_sqlalchemy @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: lowercase : Union[str, Any] = tmp_path / """cache""" lowercase : Optional[Any] = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowercase : Any = SqlDatasetReader( """dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ , keep_in_memory=SCREAMING_SNAKE_CASE__ ).read() _check_sql_dataset(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @require_sqlalchemy @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 _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: lowercase : Union[str, Any] = tmp_path / """cache""" lowercase : int = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} lowercase : str = features.copy() if features else default_expected_features lowercase : Optional[Any] = ( Features({feature: Value(SCREAMING_SNAKE_CASE__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowercase : Optional[int] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , features=SCREAMING_SNAKE_CASE__ , cache_dir=SCREAMING_SNAKE_CASE__ ).read() _check_sql_dataset(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[Any]: with contextlib.closing(sqlitea.connect(SCREAMING_SNAKE_CASE__ ) ) as con: lowercase : Optional[int] = con.cursor() cur.execute("""SELECT * FROM dataset""" ) for row in cur: yield row @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> int: lowercase : Any = tmp_path / """cache""" lowercase : int = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : List[str] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=1 ).write() lowercase : List[str] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = iter_sql_file(SCREAMING_SNAKE_CASE__ ) for rowa, rowa in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): assert rowa == rowa @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: lowercase : Dict = tmp_path / """cache""" lowercase : List[str] = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : List[str] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=2 ).write() lowercase : Optional[int] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) for rowa, rowa in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): assert rowa == rowa @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : str = tmp_path / """cache""" lowercase : Optional[int] = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : Optional[Any] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() with pytest.raises(SCREAMING_SNAKE_CASE__ ): SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=0 ).write()
20
0
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': { 'xlnet-base-cased': 'https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model', 'xlnet-large-cased': 'https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model', } } __UpperCAmelCase = { 'xlnet-base-cased': None, 'xlnet-large-cased': None, } # Segments (not really needed) __UpperCAmelCase = 0 __UpperCAmelCase = 1 __UpperCAmelCase = 2 __UpperCAmelCase = 3 __UpperCAmelCase = 4 class lowerCamelCase (_snake_case ): '''simple docstring''' _snake_case : Union[str, Any] = VOCAB_FILES_NAMES _snake_case : int = PRETRAINED_VOCAB_FILES_MAP _snake_case : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _snake_case : Union[str, Any] = '''left''' def __init__( self , _UpperCamelCase , _UpperCamelCase=False , _UpperCamelCase=True , _UpperCamelCase=False , _UpperCamelCase="<s>" , _UpperCamelCase="</s>" , _UpperCamelCase="<unk>" , _UpperCamelCase="<sep>" , _UpperCamelCase="<pad>" , _UpperCamelCase="<cls>" , _UpperCamelCase="<mask>" , _UpperCamelCase=["<eop>", "<eod>"] , _UpperCamelCase = None , **_UpperCamelCase , ) -> None: # Mask token behave like a normal word, i.e. include the space before it UpperCAmelCase_ : int = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else mask_token UpperCAmelCase_ : Any = {} 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 , ) UpperCAmelCase_ : List[str] = 3 UpperCAmelCase_ : int = do_lower_case UpperCAmelCase_ : int = remove_space UpperCAmelCase_ : Dict = keep_accents UpperCAmelCase_ : List[str] = vocab_file UpperCAmelCase_ : Any = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_UpperCamelCase ) @property def __UpperCAmelCase ( self ) -> List[Any]: return len(self.sp_model ) def __UpperCAmelCase ( self ) -> Optional[int]: UpperCAmelCase_ : Optional[int] = {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 ) -> List[Any]: UpperCAmelCase_ : Tuple = self.__dict__.copy() UpperCAmelCase_ : Union[str, Any] = None return state def __setstate__( self , _UpperCamelCase ) -> Tuple: UpperCAmelCase_ : Tuple = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): UpperCAmelCase_ : Union[str, Any] = {} UpperCAmelCase_ : Tuple = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __UpperCAmelCase ( self , _UpperCamelCase ) -> Dict: if self.remove_space: UpperCAmelCase_ : Optional[int] = ' '.join(inputs.strip().split() ) else: UpperCAmelCase_ : Dict = inputs UpperCAmelCase_ : str = outputs.replace('``' , '"' ).replace('\'\'' , '"' ) if not self.keep_accents: UpperCAmelCase_ : List[str] = unicodedata.normalize('NFKD' , _UpperCamelCase ) UpperCAmelCase_ : List[Any] = ''.join([c for c in outputs if not unicodedata.combining(_UpperCamelCase )] ) if self.do_lower_case: UpperCAmelCase_ : List[Any] = outputs.lower() return outputs def __UpperCAmelCase ( self , _UpperCamelCase ) -> List[str]: UpperCAmelCase_ : Union[str, Any] = self.preprocess_text(_UpperCamelCase ) UpperCAmelCase_ : List[Any] = self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) UpperCAmelCase_ : str = [] for piece in pieces: if len(_UpperCamelCase ) > 1 and piece[-1] == str(',' ) and piece[-2].isdigit(): UpperCAmelCase_ : Any = 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: UpperCAmelCase_ : str = cur_pieces[1:] else: UpperCAmelCase_ : int = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(_UpperCamelCase ) else: new_pieces.append(_UpperCamelCase ) return new_pieces def __UpperCAmelCase ( self , _UpperCamelCase ) -> List[Any]: return self.sp_model.PieceToId(_UpperCamelCase ) def __UpperCAmelCase ( self , _UpperCamelCase ) -> Tuple: return self.sp_model.IdToPiece(_UpperCamelCase ) def __UpperCAmelCase ( self , _UpperCamelCase ) -> List[str]: UpperCAmelCase_ : List[str] = ''.join(_UpperCamelCase ).replace(_UpperCamelCase , ' ' ).strip() return out_string def __UpperCAmelCase ( self , _UpperCamelCase , _UpperCamelCase = False , _UpperCamelCase = None , _UpperCamelCase = True , **_UpperCamelCase , ) -> str: UpperCAmelCase_ : Optional[int] = kwargs.pop('use_source_tokenizer' , _UpperCamelCase ) UpperCAmelCase_ : Tuple = self.convert_ids_to_tokens(_UpperCamelCase , skip_special_tokens=_UpperCamelCase ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 UpperCAmelCase_ : List[Any] = [] UpperCAmelCase_ : Any = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_UpperCamelCase ) ) UpperCAmelCase_ : List[str] = [] sub_texts.append(_UpperCamelCase ) else: current_sub_text.append(_UpperCamelCase ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_UpperCamelCase ) ) # Mimic the behavior of the Rust tokenizer: # By default, there are no spaces between special tokens UpperCAmelCase_ : Dict = ''.join(_UpperCamelCase ) UpperCAmelCase_ : Dict = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: UpperCAmelCase_ : int = self.clean_up_tokenization(_UpperCamelCase ) return clean_text else: return text def __UpperCAmelCase ( self , _UpperCamelCase , _UpperCamelCase = None ) -> List[int]: UpperCAmelCase_ : List[Any] = [self.sep_token_id] UpperCAmelCase_ : Tuple = [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 __UpperCAmelCase ( self , _UpperCamelCase , _UpperCamelCase = None , _UpperCamelCase = False ) -> List[int]: 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 __UpperCAmelCase ( self , _UpperCamelCase , _UpperCamelCase = None ) -> List[int]: UpperCAmelCase_ : Any = [self.sep_token_id] UpperCAmelCase_ : Any = [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 __UpperCAmelCase ( self , _UpperCamelCase , _UpperCamelCase = None ) -> Tuple[str]: if not os.path.isdir(_UpperCamelCase ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return UpperCAmelCase_ : List[str] = 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: UpperCAmelCase_ : Optional[Any] = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,)
29
import os import numpy import onnx def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: lowercase : int = a.name lowercase : Any = b.name lowercase : Optional[Any] = """""" lowercase : Dict = """""" lowercase : int = a == b lowercase : int = name_a lowercase : List[str] = name_b return res def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: for i, input_name in enumerate(node_proto.input ): if input_name == name: node_proto.input.insert(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) node_proto.input.pop(i + 1 ) if node_proto.op_type == "If": _graph_replace_input_with(node_proto.attribute[0].g , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) _graph_replace_input_with(node_proto.attribute[1].g , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if node_proto.op_type == "Loop": _graph_replace_input_with(node_proto.attribute[0].g , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: for n in graph_proto.node: _node_replace_input_with(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: lowercase : Any = list(model.graph.initializer ) lowercase : Dict = list(model_without_ext.graph.initializer ) for i, ref_i in ind_to_replace: assert inits_with_data[i].name == inits[i].name assert inits_with_data[ref_i].name == inits[ref_i].name assert i > ref_i lowercase : Union[str, Any] = inits[i].name lowercase : Dict = inits[ref_i].name model_without_ext.graph.initializer.remove(inits[i] ) # for n in model.graph.node: _graph_replace_input_with(model_without_ext.graph , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : Union[str, Any] = os.path.dirname(SCREAMING_SNAKE_CASE__ ) lowercase : Dict = os.path.basename(SCREAMING_SNAKE_CASE__ ) lowercase : str = onnx.load(os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) lowercase : List[str] = list(model.graph.initializer ) lowercase : Tuple = set() lowercase : int = {} lowercase : Optional[Any] = [] lowercase : Dict = 0 for i in range(len(SCREAMING_SNAKE_CASE__ ) ): if i in dup_set: continue for j in range(i + 1 , len(SCREAMING_SNAKE_CASE__ ) ): if j in dup_set: continue if _is_equal_tensor_proto(inits[i] , inits[j] ): dup_set.add(SCREAMING_SNAKE_CASE__ ) dup_set.add(SCREAMING_SNAKE_CASE__ ) lowercase : int = inits[j].data_type lowercase : Optional[int] = numpy.prod(inits[j].dims ) if dtype == 1: mem_size *= 4 elif dtype == 6: mem_size *= 4 elif dtype == 7 or dtype == 11: mem_size *= 8 else: print("""unexpected data type: """ , SCREAMING_SNAKE_CASE__ ) total_reduced_size += mem_size lowercase : Tuple = inits[i].name lowercase : int = inits[j].name if name_i in dup_map: dup_map[name_i].append(SCREAMING_SNAKE_CASE__ ) else: lowercase : List[str] = [name_j] ind_to_replace.append((j, i) ) print("""total reduced size: """ , total_reduced_size / 1_024 / 1_024 / 1_024 , """GB""" ) lowercase : str = sorted(SCREAMING_SNAKE_CASE__ ) _remove_dup_initializers_from_model(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = """optimized_""" + model_file_name lowercase : Dict = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) onnx.save(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return new_model
20
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __a = { 'configuration_whisper': ['WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WhisperConfig', 'WhisperOnnxConfig'], 'feature_extraction_whisper': ['WhisperFeatureExtractor'], 'processing_whisper': ['WhisperProcessor'], 'tokenization_whisper': ['WhisperTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ['WhisperTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'WhisperForConditionalGeneration', 'WhisperModel', 'WhisperPreTrainedModel', 'WhisperForAudioClassification', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFWhisperForConditionalGeneration', 'TFWhisperModel', 'TFWhisperPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'FlaxWhisperForConditionalGeneration', 'FlaxWhisperModel', 'FlaxWhisperPreTrainedModel', 'FlaxWhisperForAudioClassification', ] if TYPE_CHECKING: from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig from .feature_extraction_whisper import WhisperFeatureExtractor from .processing_whisper import WhisperProcessor from .tokenization_whisper import WhisperTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_whisper_fast import WhisperTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_whisper import ( WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, WhisperForAudioClassification, WhisperForConditionalGeneration, WhisperModel, WhisperPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_whisper import ( TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, TFWhisperForConditionalGeneration, TFWhisperModel, TFWhisperPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_whisper import ( FlaxWhisperForAudioClassification, FlaxWhisperForConditionalGeneration, FlaxWhisperModel, FlaxWhisperPreTrainedModel, ) else: import sys __a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
30
import argparse import json from collections import OrderedDict import torch from huggingface_hub import cached_download, hf_hub_url from transformers import AutoImageProcessor, CvtConfig, CvtForImageClassification def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Tuple: lowercase : Union[str, Any] = [] embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.weight", f"stage{idx}.patch_embed.proj.weight", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.bias", f"stage{idx}.patch_embed.proj.bias", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.weight", f"stage{idx}.patch_embed.norm.weight", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.bias", f"stage{idx}.patch_embed.norm.bias", ) ) return embed def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: lowercase : Optional[Any] = [] attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.weight", f"stage{idx}.blocks.{cnt}.attn.proj_q.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.bias", f"stage{idx}.blocks.{cnt}.attn.proj_q.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.weight", f"stage{idx}.blocks.{cnt}.attn.proj_k.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.bias", f"stage{idx}.blocks.{cnt}.attn.proj_k.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.weight", f"stage{idx}.blocks.{cnt}.attn.proj_v.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.bias", f"stage{idx}.blocks.{cnt}.attn.proj_v.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.weight", f"stage{idx}.blocks.{cnt}.attn.proj.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.bias", f"stage{idx}.blocks.{cnt}.attn.proj.bias", ) ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.weight", f"stage{idx}.blocks.{cnt}.mlp.fc1.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.bias", f"stage{idx}.blocks.{cnt}.mlp.fc1.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.weight", f"stage{idx}.blocks.{cnt}.mlp.fc2.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.bias", f"stage{idx}.blocks.{cnt}.mlp.fc2.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.weight", f"stage{idx}.blocks.{cnt}.norm1.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.bias", f"stage{idx}.blocks.{cnt}.norm1.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.weight", f"stage{idx}.blocks.{cnt}.norm2.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.bias", f"stage{idx}.blocks.{cnt}.norm2.bias") ) return attention_weights def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: lowercase : Optional[Any] = [] token.append((f"cvt.encoder.stages.{idx}.cls_token", """stage2.cls_token""") ) return token def _snake_case( ) -> Dict: lowercase : Optional[Any] = [] head.append(("""layernorm.weight""", """norm.weight""") ) head.append(("""layernorm.bias""", """norm.bias""") ) head.append(("""classifier.weight""", """head.weight""") ) head.append(("""classifier.bias""", """head.bias""") ) return head def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: lowercase : Any = """imagenet-1k-id2label.json""" lowercase : List[str] = 1_000 lowercase : int = """huggingface/label-files""" lowercase : Union[str, Any] = num_labels lowercase : Optional[Any] = json.load(open(cached_download(hf_hub_url(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , repo_type="""dataset""" ) ) , """r""" ) ) lowercase : List[Any] = {int(SCREAMING_SNAKE_CASE__ ): v for k, v in idalabel.items()} lowercase : Dict = idalabel lowercase : List[str] = {v: k for k, v in idalabel.items()} lowercase : List[str] = CvtConfig(num_labels=SCREAMING_SNAKE_CASE__ , idalabel=SCREAMING_SNAKE_CASE__ , labelaid=SCREAMING_SNAKE_CASE__ ) # For depth size 13 (13 = 1+2+10) if cvt_model.rsplit("""/""" , 1 )[-1][4:6] == "13": lowercase : Tuple = [1, 2, 10] # For depth size 21 (21 = 1+4+16) elif cvt_model.rsplit("""/""" , 1 )[-1][4:6] == "21": lowercase : Dict = [1, 4, 16] # For wide cvt (similar to wide-resnet) depth size 24 (w24 = 2 + 2 20) else: lowercase : int = [2, 2, 20] lowercase : Optional[int] = [3, 12, 16] lowercase : str = [192, 768, 1_024] lowercase : Union[str, Any] = CvtForImageClassification(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = AutoImageProcessor.from_pretrained("""facebook/convnext-base-224-22k-1k""" ) lowercase : Optional[Any] = image_size lowercase : Union[str, Any] = torch.load(SCREAMING_SNAKE_CASE__ , map_location=torch.device("""cpu""" ) ) lowercase : Optional[Any] = OrderedDict() lowercase : Tuple = [] for idx in range(len(config.depth ) ): if config.cls_token[idx]: lowercase : Optional[Any] = list_of_state_dict + cls_token(SCREAMING_SNAKE_CASE__ ) lowercase : str = list_of_state_dict + embeddings(SCREAMING_SNAKE_CASE__ ) for cnt in range(config.depth[idx] ): lowercase : List[str] = list_of_state_dict + attention(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = list_of_state_dict + final() for gg in list_of_state_dict: print(SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) ): lowercase : Optional[Any] = original_weights[list_of_state_dict[i][1]] model.load_state_dict(SCREAMING_SNAKE_CASE__ ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) # Download the weights from zoo: https://1drv.ms/u/s!AhIXJn_J-blW9RzF3rMW7SsLHa8h?e=blQ0Al if __name__ == "__main__": lowercase : Tuple = argparse.ArgumentParser() parser.add_argument( """--cvt_model""", default="""cvt-w24""", type=str, help="""Name of the cvt model you'd like to convert.""", ) parser.add_argument( """--image_size""", default=384, type=int, help="""Input Image Size""", ) parser.add_argument( """--cvt_file_name""", default=R"""cvtmodels\CvT-w24-384x384-IN-22k.pth""", type=str, help="""Input Image Size""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) lowercase : Optional[int] = parser.parse_args() convert_cvt_checkpoint(args.cvt_model, args.image_size, args.cvt_file_name, args.pytorch_dump_folder_path)
20
0
'''simple docstring''' import math import numpy as np import qiskit from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute def UpperCamelCase_ ( _UpperCAmelCase : int = 3 ) -> qiskit.result.counts.Counts: """simple docstring""" if isinstance(_UpperCAmelCase , _UpperCAmelCase ): raise TypeError("number of qubits must be a integer." ) if number_of_qubits <= 0: raise ValueError("number of qubits must be > 0." ) if math.floor(_UpperCAmelCase ) != number_of_qubits: raise ValueError("number of qubits must be exact integer." ) if number_of_qubits > 10: raise ValueError("number of qubits too large to simulate(>10)." ) _UpperCAmelCase : Dict = QuantumRegister(_UpperCAmelCase , "qr" ) _UpperCAmelCase : Any = ClassicalRegister(_UpperCAmelCase , "cr" ) _UpperCAmelCase : int = QuantumCircuit(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase : List[Any] = number_of_qubits for i in range(_UpperCAmelCase ): quantum_circuit.h(number_of_qubits - i - 1 ) counter -= 1 for j in range(_UpperCAmelCase ): quantum_circuit.cp(np.pi / 2 ** (counter - j) , _UpperCAmelCase , _UpperCAmelCase ) for k in range(number_of_qubits // 2 ): quantum_circuit.swap(_UpperCAmelCase , number_of_qubits - k - 1 ) # measure all the qubits quantum_circuit.measure(_UpperCAmelCase , _UpperCAmelCase ) # simulate with 10000 shots _UpperCAmelCase : str = Aer.get_backend("qasm_simulator" ) _UpperCAmelCase : Dict = execute(_UpperCAmelCase , _UpperCAmelCase , shots=10_000 ) return job.result().get_counts(_UpperCAmelCase ) if __name__ == "__main__": print( F'Total count for quantum fourier transform state is: \ {quantum_fourier_transform(3)}' )
31
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor from ..utils import is_datasets_available from .base import PipelineTool if is_datasets_available(): from datasets import load_dataset class __snake_case ( lowerCAmelCase ): _a : Union[str, Any]= "microsoft/speecht5_tts" _a : Tuple= ( "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the " "text to read (in English) and returns a waveform object containing the sound." ) _a : Dict= "text_reader" _a : Optional[Any]= SpeechTaProcessor _a : Tuple= SpeechTaForTextToSpeech _a : Optional[int]= SpeechTaHifiGan _a : Union[str, Any]= ["text"] _a : Optional[int]= ["audio"] def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self.post_processor is None: lowercase : Any = """microsoft/speecht5_hifigan""" super().setup() def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ): '''simple docstring''' lowercase : int = self.pre_processor(text=snake_case ,return_tensors="""pt""" ,truncation=snake_case ) if speaker_embeddings is None: if not is_datasets_available(): raise ImportError("""Datasets needs to be installed if not passing speaker embeddings.""" ) lowercase : Tuple = load_dataset("""Matthijs/cmu-arctic-xvectors""" ,split="""validation""" ) lowercase : List[str] = torch.tensor(embeddings_dataset[7305]["""xvector"""] ).unsqueeze(0 ) return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings} def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' with torch.no_grad(): return self.model.generate_speech(**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' with torch.no_grad(): return self.post_processor(snake_case ).cpu().detach()
20
0
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_ : str = logging.get_logger(__name__) UpperCAmelCase_ : Any = { 'microsoft/beit-base-patch16-224-pt22k': ( 'https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json' ), # See all BEiT models at https://huggingface.co/models?filter=beit } class SCREAMING_SNAKE_CASE__ ( lowercase__ ): snake_case__ : Tuple = '''beit''' def __init__( self : Dict , SCREAMING_SNAKE_CASE__ : Any=8_1_9_2 , SCREAMING_SNAKE_CASE__ : Tuple=7_6_8 , SCREAMING_SNAKE_CASE__ : Any=1_2 , SCREAMING_SNAKE_CASE__ : str=1_2 , SCREAMING_SNAKE_CASE__ : Any=3_0_7_2 , SCREAMING_SNAKE_CASE__ : Optional[int]="gelu" , SCREAMING_SNAKE_CASE__ : str=0.0 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=0.0 , SCREAMING_SNAKE_CASE__ : List[str]=0.02 , SCREAMING_SNAKE_CASE__ : Optional[int]=1E-12 , SCREAMING_SNAKE_CASE__ : Any=2_2_4 , SCREAMING_SNAKE_CASE__ : List[Any]=1_6 , SCREAMING_SNAKE_CASE__ : Dict=3 , SCREAMING_SNAKE_CASE__ : Dict=False , SCREAMING_SNAKE_CASE__ : List[Any]=False , SCREAMING_SNAKE_CASE__ : str=False , SCREAMING_SNAKE_CASE__ : str=False , SCREAMING_SNAKE_CASE__ : Dict=0.1 , SCREAMING_SNAKE_CASE__ : Dict=0.1 , SCREAMING_SNAKE_CASE__ : int=True , SCREAMING_SNAKE_CASE__ : Optional[Any]=[3, 5, 7, 1_1] , SCREAMING_SNAKE_CASE__ : Union[str, Any]=[1, 2, 3, 6] , SCREAMING_SNAKE_CASE__ : List[str]=True , SCREAMING_SNAKE_CASE__ : Any=0.4 , SCREAMING_SNAKE_CASE__ : Any=2_5_6 , SCREAMING_SNAKE_CASE__ : Any=1 , SCREAMING_SNAKE_CASE__ : Optional[int]=False , SCREAMING_SNAKE_CASE__ : List[Any]=2_5_5 , **SCREAMING_SNAKE_CASE__ : Optional[Any] , ) -> str: super().__init__(**SCREAMING_SNAKE_CASE__ ) a_ : Union[str, Any] = vocab_size a_ : str = hidden_size a_ : int = num_hidden_layers a_ : Any = num_attention_heads a_ : Any = intermediate_size a_ : Optional[Any] = hidden_act a_ : str = hidden_dropout_prob a_ : List[Any] = attention_probs_dropout_prob a_ : Optional[Any] = initializer_range a_ : Any = layer_norm_eps a_ : Dict = image_size a_ : List[str] = patch_size a_ : Optional[int] = num_channels a_ : str = use_mask_token a_ : Optional[int] = use_absolute_position_embeddings a_ : Union[str, Any] = use_relative_position_bias a_ : Optional[Any] = use_shared_relative_position_bias a_ : Optional[int] = layer_scale_init_value a_ : str = drop_path_rate a_ : Tuple = use_mean_pooling # decode head attributes (semantic segmentation) a_ : str = out_indices a_ : List[str] = pool_scales # auxiliary head attributes (semantic segmentation) a_ : Optional[int] = use_auxiliary_head a_ : Optional[Any] = auxiliary_loss_weight a_ : List[str] = auxiliary_channels a_ : List[str] = auxiliary_num_convs a_ : Dict = auxiliary_concat_input a_ : str = semantic_loss_ignore_index class SCREAMING_SNAKE_CASE__ ( lowercase__ ): snake_case__ : Dict = version.parse('''1.11''' ) @property def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> float: return 1E-4
32
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase : str = { """configuration_funnel""": ["""FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FunnelConfig"""], """convert_funnel_original_tf_checkpoint_to_pytorch""": [], """tokenization_funnel""": ["""FunnelTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Tuple = ["""FunnelTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : List[str] = [ """FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""", """FunnelBaseModel""", """FunnelForMaskedLM""", """FunnelForMultipleChoice""", """FunnelForPreTraining""", """FunnelForQuestionAnswering""", """FunnelForSequenceClassification""", """FunnelForTokenClassification""", """FunnelModel""", """FunnelPreTrainedModel""", """load_tf_weights_in_funnel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Optional[Any] = [ """TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFFunnelBaseModel""", """TFFunnelForMaskedLM""", """TFFunnelForMultipleChoice""", """TFFunnelForPreTraining""", """TFFunnelForQuestionAnswering""", """TFFunnelForSequenceClassification""", """TFFunnelForTokenClassification""", """TFFunnelModel""", """TFFunnelPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig from .tokenization_funnel import FunnelTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_funnel_fast import FunnelTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_funnel import ( FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, FunnelBaseModel, FunnelForMaskedLM, FunnelForMultipleChoice, FunnelForPreTraining, FunnelForQuestionAnswering, FunnelForSequenceClassification, FunnelForTokenClassification, FunnelModel, FunnelPreTrainedModel, load_tf_weights_in_funnel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_funnel import ( TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, TFFunnelPreTrainedModel, ) else: import sys lowercase : Any = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
20
0
"""simple docstring""" from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef import datasets __A : List[Any] = '''\ @inproceedings{wang2019glue, title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding}, author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.}, note={In the Proceedings of ICLR.}, year={2019} } ''' __A : List[str] = '''\ GLUE, the General Language Understanding Evaluation benchmark (https://gluebenchmark.com/) is a collection of resources for training, evaluating, and analyzing natural language understanding systems. ''' __A : Any = ''' Compute GLUE evaluation metric associated to each GLUE dataset. Args: predictions: list of predictions to score. Each translation should be tokenized into a list of tokens. references: list of lists of references for each translation. Each reference should be tokenized into a list of tokens. Returns: depending on the GLUE subset, one or several of: "accuracy": Accuracy "f1": F1 score "pearson": Pearson Correlation "spearmanr": Spearman Correlation "matthews_correlation": Matthew Correlation Examples: >>> glue_metric = datasets.load_metric(\'glue\', \'sst2\') # \'sst2\' or any of ["mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"] >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0} >>> glue_metric = datasets.load_metric(\'glue\', \'mrpc\') # \'mrpc\' or \'qqp\' >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0, \'f1\': 1.0} >>> glue_metric = datasets.load_metric(\'glue\', \'stsb\') >>> references = [0., 1., 2., 3., 4., 5.] >>> predictions = [0., 1., 2., 3., 4., 5.] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print({"pearson": round(results["pearson"], 2), "spearmanr": round(results["spearmanr"], 2)}) {\'pearson\': 1.0, \'spearmanr\': 1.0} >>> glue_metric = datasets.load_metric(\'glue\', \'cola\') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'matthews_correlation\': 1.0} ''' def lowercase ( __snake_case : int , __snake_case : Optional[Any] ): return float((preds == labels).mean() ) def lowercase ( __snake_case : Dict , __snake_case : List[str] ): lowercase_ : Union[str, Any] = simple_accuracy(__snake_case , __snake_case ) lowercase_ : Union[str, Any] = float(fa_score(y_true=__snake_case , y_pred=__snake_case ) ) return { "accuracy": acc, "f1": fa, } def lowercase ( __snake_case : List[Any] , __snake_case : List[str] ): lowercase_ : Any = float(pearsonr(__snake_case , __snake_case )[0] ) lowercase_ : Union[str, Any] = float(spearmanr(__snake_case , __snake_case )[0] ) return { "pearson": pearson_corr, "spearmanr": spearman_corr, } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _UpperCAmelCase ( datasets.Metric ): def A ( self : Dict ) -> List[Any]: if self.config_name not in [ "sst2", "mnli", "mnli_mismatched", "mnli_matched", "cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans", ]: raise KeyError( '''You should supply a configuration name selected in ''' '''["sst2", "mnli", "mnli_mismatched", "mnli_matched", ''' '''"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]''' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''int64''' if self.config_name != '''stsb''' else '''float32''' ), '''references''': datasets.Value('''int64''' if self.config_name != '''stsb''' else '''float32''' ), } ) , codebase_urls=[] , reference_urls=[] , format='''numpy''' , ) def A ( self : Union[str, Any] , A : str , A : int ) -> int: if self.config_name == "cola": return {"matthews_correlation": matthews_corrcoef(A , A )} elif self.config_name == "stsb": return pearson_and_spearman(A , A ) elif self.config_name in ["mrpc", "qqp"]: return acc_and_fa(A , A ) elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]: return {"accuracy": simple_accuracy(A , A )} else: raise KeyError( '''You should supply a configuration name selected in ''' '''["sst2", "mnli", "mnli_mismatched", "mnli_matched", ''' '''"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]''' )
33
import os import zipfile import pytest from datasets.utils.extract import ( BzipaExtractor, Extractor, GzipExtractor, LzaExtractor, SevenZipExtractor, TarExtractor, XzExtractor, ZipExtractor, ZstdExtractor, ) from .utils import require_lza, require_pyazr, require_zstandard @pytest.mark.parametrize( """compression_format, is_archive""" , [ ("""7z""", True), ("""bz2""", False), ("""gzip""", False), ("""lz4""", False), ("""tar""", True), ("""xz""", False), ("""zip""", True), ("""zstd""", False), ] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) -> Any: lowercase : Dict = { """7z""": (seven_zip_file, SevenZipExtractor), """bz2""": (bza_file, BzipaExtractor), """gzip""": (gz_file, GzipExtractor), """lz4""": (lza_file, LzaExtractor), """tar""": (tar_file, TarExtractor), """xz""": (xz_file, XzExtractor), """zip""": (zip_file, ZipExtractor), """zstd""": (zstd_file, ZstdExtractor), } lowercase , lowercase : Optional[Any] = input_paths_and_base_extractors[compression_format] if input_path is None: lowercase : Dict = f"for '{compression_format}' compression_format, " if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(SCREAMING_SNAKE_CASE__ ) assert base_extractor.is_extractable(SCREAMING_SNAKE_CASE__ ) lowercase : Any = tmp_path / ("""extracted""" if is_archive else """extracted.txt""") base_extractor.extract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name lowercase : str = file_path.read_text(encoding="""utf-8""" ) else: lowercase : Optional[Any] = output_path.read_text(encoding="""utf-8""" ) lowercase : Tuple = text_file.read_text(encoding="""utf-8""" ) assert extracted_file_content == expected_file_content @pytest.mark.parametrize( """compression_format, is_archive""" , [ ("""7z""", True), ("""bz2""", False), ("""gzip""", False), ("""lz4""", False), ("""tar""", True), ("""xz""", False), ("""zip""", True), ("""zstd""", False), ] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) -> Dict: lowercase : str = { """7z""": seven_zip_file, """bz2""": bza_file, """gzip""": gz_file, """lz4""": lza_file, """tar""": tar_file, """xz""": xz_file, """zip""": zip_file, """zstd""": zstd_file, } lowercase : Optional[Any] = input_paths[compression_format] if input_path is None: lowercase : int = f"for '{compression_format}' compression_format, " if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = Extractor.infer_extractor_format(SCREAMING_SNAKE_CASE__ ) assert extractor_format is not None lowercase : Any = tmp_path / ("""extracted""" if is_archive else """extracted.txt""") Extractor.extract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name lowercase : Dict = file_path.read_text(encoding="""utf-8""" ) else: lowercase : int = output_path.read_text(encoding="""utf-8""" ) lowercase : Optional[Any] = text_file.read_text(encoding="""utf-8""" ) assert extracted_file_content == expected_file_content @pytest.fixture def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: import tarfile lowercase : Tuple = tmp_path / """data_dot_dot""" directory.mkdir() lowercase : str = directory / """tar_file_with_dot_dot.tar""" with tarfile.TarFile(SCREAMING_SNAKE_CASE__ , """w""" ) as f: f.add(SCREAMING_SNAKE_CASE__ , arcname=os.path.join("""..""" , text_file.name ) ) return path @pytest.fixture def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[str]: import tarfile lowercase : Tuple = tmp_path / """data_sym_link""" directory.mkdir() lowercase : int = directory / """tar_file_with_sym_link.tar""" os.symlink("""..""" , directory / """subdir""" , target_is_directory=SCREAMING_SNAKE_CASE__ ) with tarfile.TarFile(SCREAMING_SNAKE_CASE__ , """w""" ) as f: f.add(str(directory / """subdir""" ) , arcname="""subdir""" ) # str required by os.readlink on Windows and Python < 3.8 return path @pytest.mark.parametrize( """insecure_tar_file, error_log""" , [("""tar_file_with_dot_dot""", """illegal path"""), ("""tar_file_with_sym_link""", """Symlink""")] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: lowercase : List[Any] = { """tar_file_with_dot_dot""": tar_file_with_dot_dot, """tar_file_with_sym_link""": tar_file_with_sym_link, } lowercase : Optional[int] = insecure_tar_files[insecure_tar_file] lowercase : List[str] = tmp_path / """extracted""" TarExtractor.extract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) assert caplog.text for record in caplog.records: assert record.levelname == "ERROR" assert error_log in record.msg def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[int]: # We should have less false positives than zipfile.is_zipfile # We do that by checking only the magic number lowercase : Any = tmpdir / """not_a_zip_file""" # From: https://github.com/python/cpython/pull/5053 lowercase : str = ( B"""\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00""" B"""\x00\x02\x08\x06\x00\x00\x00\x99\x81\xb6'\x00\x00\x00\x15I""" B"""DATx\x01\x01\n\x00\xf5\xff\x00PK\x05\x06\x00PK\x06\x06\x07""" B"""\xac\x01N\xc6|a\r\x00\x00\x00\x00IEND\xaeB`\x82""" ) with not_a_zip_file.open("""wb""" ) as f: f.write(SCREAMING_SNAKE_CASE__ ) assert zipfile.is_zipfile(str(SCREAMING_SNAKE_CASE__ ) ) # is a false positive for `zipfile` assert not ZipExtractor.is_extractable(SCREAMING_SNAKE_CASE__ ) # but we're right
20
0
'''simple docstring''' import os import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers.models.realm.configuration_realm import RealmConfig from transformers.models.realm.retrieval_realm import _REALM_BLOCK_RECORDS_FILENAME, RealmRetriever from transformers.models.realm.tokenization_realm import VOCAB_FILES_NAMES, RealmTokenizer class _a ( __a ): def A ( self : List[Any] ): '''simple docstring''' UpperCAmelCase = tempfile.mkdtemp() UpperCAmelCase = 5 # Realm tok UpperCAmelCase = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''test''', '''question''', '''this''', '''is''', '''the''', '''first''', '''second''', '''third''', '''fourth''', '''fifth''', '''record''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] UpperCAmelCase = os.path.join(self.tmpdirname , '''realm_tokenizer''' ) os.makedirs(lowercase , exist_ok=lowercase ) UpperCAmelCase = os.path.join(lowercase , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) UpperCAmelCase = os.path.join(self.tmpdirname , '''realm_block_records''' ) os.makedirs(lowercase , exist_ok=lowercase ) def A ( self : List[Any] ): '''simple docstring''' return RealmTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''realm_tokenizer''' ) ) def A ( self : int ): '''simple docstring''' shutil.rmtree(self.tmpdirname ) def A ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase = RealmConfig(num_block_records=self.num_block_records ) return config def A ( self : List[str] ): '''simple docstring''' UpperCAmelCase = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''question''': ['''foo''', '''bar'''], '''answers''': [['''Foo''', '''Bar'''], ['''Bar''']], } ) return dataset def A ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase = np.array( [ B'''This is the first record''', B'''This is the second record''', B'''This is the third record''', B'''This is the fourth record''', B'''This is the fifth record''', B'''This is a longer longer longer record''', ] , dtype=lowercase , ) return block_records def A ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase = RealmRetriever( block_records=self.get_dummy_block_records() , tokenizer=self.get_tokenizer() , ) return retriever def A ( self : str ): '''simple docstring''' UpperCAmelCase = self.get_config() UpperCAmelCase = self.get_dummy_retriever() UpperCAmelCase = retriever.tokenizer UpperCAmelCase = np.array([0, 3] , dtype='''long''' ) UpperCAmelCase = tokenizer(['''Test question'''] ).input_ids UpperCAmelCase = tokenizer( ['''the fourth'''] , add_special_tokens=lowercase , return_token_type_ids=lowercase , return_attention_mask=lowercase , ).input_ids UpperCAmelCase = config.reader_seq_len UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = retriever( lowercase , lowercase , answer_ids=lowercase , max_length=lowercase , return_tensors='''np''' ) self.assertEqual(len(lowercase ) , 2 ) self.assertEqual(len(lowercase ) , 2 ) self.assertEqual(len(lowercase ) , 2 ) self.assertEqual(concat_inputs.input_ids.shape , (2, 10) ) self.assertEqual(concat_inputs.attention_mask.shape , (2, 10) ) self.assertEqual(concat_inputs.token_type_ids.shape , (2, 10) ) self.assertEqual(concat_inputs.special_tokens_mask.shape , (2, 10) ) self.assertEqual( tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[0] ) , ['''[CLS]''', '''test''', '''question''', '''[SEP]''', '''this''', '''is''', '''the''', '''first''', '''record''', '''[SEP]'''] , ) self.assertEqual( tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[1] ) , ['''[CLS]''', '''test''', '''question''', '''[SEP]''', '''this''', '''is''', '''the''', '''fourth''', '''record''', '''[SEP]'''] , ) def A ( self : List[str] ): '''simple docstring''' UpperCAmelCase = self.get_config() UpperCAmelCase = self.get_dummy_retriever() UpperCAmelCase = retriever.tokenizer UpperCAmelCase = np.array([0, 3, 5] , dtype='''long''' ) UpperCAmelCase = tokenizer(['''Test question'''] ).input_ids UpperCAmelCase = tokenizer( ['''the fourth''', '''longer longer'''] , add_special_tokens=lowercase , return_token_type_ids=lowercase , return_attention_mask=lowercase , ).input_ids UpperCAmelCase = config.reader_seq_len UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = retriever( lowercase , lowercase , answer_ids=lowercase , max_length=lowercase , return_tensors='''np''' ) self.assertEqual([False, True, True] , lowercase ) self.assertEqual([[-1, -1, -1], [6, -1, -1], [6, 7, 8]] , lowercase ) self.assertEqual([[-1, -1, -1], [7, -1, -1], [7, 8, 9]] , lowercase ) def A ( self : Dict ): '''simple docstring''' UpperCAmelCase = self.get_dummy_retriever() retriever.save_pretrained(os.path.join(self.tmpdirname , '''realm_block_records''' ) ) # Test local path UpperCAmelCase = retriever.from_pretrained(os.path.join(self.tmpdirname , '''realm_block_records''' ) ) self.assertEqual(retriever.block_records[0] , B'''This is the first record''' ) # Test mocked remote path with patch('''transformers.models.realm.retrieval_realm.hf_hub_download''' ) as mock_hf_hub_download: UpperCAmelCase = os.path.join( os.path.join(self.tmpdirname , '''realm_block_records''' ) , _REALM_BLOCK_RECORDS_FILENAME ) UpperCAmelCase = RealmRetriever.from_pretrained('''google/realm-cc-news-pretrained-openqa''' ) self.assertEqual(retriever.block_records[0] , B'''This is the first record''' )
34
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast from ...utils import logging lowercase : List[str] = logging.get_logger(__name__) lowercase : Optional[Any] = { """EleutherAI/gpt-neo-1.3B""": """https://huggingface.co/EleutherAI/gpt-neo-1.3B/resolve/main/config.json""", # See all GPTNeo models at https://huggingface.co/models?filter=gpt_neo } class __snake_case ( lowerCAmelCase ): _a : str= "gpt_neo" _a : Optional[int]= ["past_key_values"] _a : Dict= {"num_attention_heads": "num_heads", "num_hidden_layers": "num_layers"} def __init__( self ,snake_case=50257 ,snake_case=2048 ,snake_case=2048 ,snake_case=24 ,snake_case=[[["global", "local"], 12]] ,snake_case=16 ,snake_case=None ,snake_case=256 ,snake_case="gelu_new" ,snake_case=0.0 ,snake_case=0.0 ,snake_case=0.0 ,snake_case=0.1 ,snake_case=1e-5 ,snake_case=0.02 ,snake_case=True ,snake_case=50256 ,snake_case=50256 ,**snake_case ,): '''simple docstring''' lowercase : int = vocab_size lowercase : Union[str, Any] = max_position_embeddings lowercase : Dict = hidden_size lowercase : Union[str, Any] = num_layers lowercase : Union[str, Any] = num_heads lowercase : Optional[int] = intermediate_size lowercase : List[str] = window_size lowercase : Optional[int] = activation_function lowercase : List[str] = resid_dropout lowercase : int = embed_dropout lowercase : Optional[int] = attention_dropout lowercase : Tuple = classifier_dropout lowercase : Optional[int] = layer_norm_epsilon lowercase : Dict = initializer_range lowercase : List[str] = use_cache lowercase : Optional[int] = bos_token_id lowercase : int = eos_token_id lowercase : Union[str, Any] = attention_types lowercase : Dict = self.expand_attention_types_params(snake_case ) if len(self.attention_layers ) != self.num_layers: raise ValueError( """Configuration for convolutional module is incorrect. """ """It is required that `len(config.attention_layers)` == `config.num_layers` """ f"but is `len(config.attention_layers) = {len(self.attention_layers )}`, " f"`config.num_layers = {self.num_layers}`. " """`config.attention_layers` is prepared using `config.attention_types`. """ """Please verify the value of `config.attention_types` argument.""" ) super().__init__(bos_token_id=snake_case ,eos_token_id=snake_case ,**snake_case ) @staticmethod def _SCREAMING_SNAKE_CASE ( snake_case ): '''simple docstring''' lowercase : List[Any] = [] for item in attention_types: for _ in range(item[1] ): attentions.extend(item[0] ) return attentions def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: import torch lowercase : Tuple = input.size() lowercase : int = len(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = shape[dimension] lowercase : int = torch.arange(0 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = torch.div(sizedim - size , SCREAMING_SNAKE_CASE__ , rounding_mode="""floor""" ) + 1 lowercase : Dict = torch.arange(SCREAMING_SNAKE_CASE__ ) + low_indices[:min_length][:, None] lowercase : Union[str, Any] = [slice(SCREAMING_SNAKE_CASE__ )] * rank lowercase : Optional[Any] = indices lowercase : List[str] = input[s] lowercase : Optional[int] = list(range(0 , rank + 1 ) ) perm.append(perm.pop(dimension + 1 ) ) return sliced.permute(SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: import torch lowercase : Union[str, Any] = torch.arange(1 , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = torch.remainder(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = remainders == 0 lowercase : Optional[int] = candidates[divisor_indices] lowercase : List[Any] = torch.max(SCREAMING_SNAKE_CASE__ ) return largest_divisor, torch.div(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , rounding_mode="""floor""" ) class __snake_case ( lowerCAmelCase ): @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Dict = OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}} ) if self.use_past: self.fill_with_past_key_values_(snake_case ,direction="""inputs""" ) lowercase : Dict = {0: """batch""", 1: """past_sequence + sequence"""} else: lowercase : List[str] = {0: """batch""", 1: """sequence"""} return common_inputs @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return self._config.num_heads def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = -1 ,snake_case = -1 ,snake_case = False ,snake_case = None ,): '''simple docstring''' lowercase : Any = super(snake_case ,self ).generate_dummy_inputs( snake_case ,batch_size=snake_case ,seq_length=snake_case ,is_pair=snake_case ,framework=snake_case ) # We need to order the input in the way they appears in the forward() lowercase : List[str] = OrderedDict({"""input_ids""": common_inputs["""input_ids"""]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed.""" ) else: import torch lowercase , lowercase : List[Any] = common_inputs["""input_ids"""].shape # Not using the same length for past_key_values lowercase : Optional[int] = seqlen + 2 lowercase : int = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) lowercase : Optional[Any] = [ (torch.zeros(snake_case ), torch.zeros(snake_case )) for _ in range(self.num_layers ) ] lowercase : Optional[Any] = common_inputs["""attention_mask"""] if self.use_past: lowercase : Any = ordered_inputs["""attention_mask"""].dtype lowercase : Union[str, Any] = torch.cat( [ordered_inputs["""attention_mask"""], torch.ones(snake_case ,snake_case ,dtype=snake_case )] ,dim=1 ) return ordered_inputs @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return 13
20
0
'''simple docstring''' import argparse import os import re __a = "src/transformers" # Pattern that looks at the indentation in a line. __a = re.compile(R"^(\s*)\S") # Pattern that matches `"key":" and puts `key` in group 0. __a = re.compile(R"^\s*\"([^\"]+)\":") # Pattern that matches `_import_structure["key"]` and puts `key` in group 0. __a = re.compile(R"^\s*_import_structure\[\"([^\"]+)\"\]") # Pattern that matches `"key",` and puts `key` in group 0. __a = re.compile(R"^\s*\"([^\"]+)\",\s*$") # Pattern that matches any `[stuff]` and puts `stuff` in group 0. __a = re.compile(R"\[([^\]]+)\]") def __snake_case( _lowerCAmelCase ) -> List[Any]: snake_case__ : int = _re_indent.search(_lowerCAmelCase ) return "" if search is None else search.groups()[0] def __snake_case( _lowerCAmelCase , _lowerCAmelCase="" , _lowerCAmelCase=None , _lowerCAmelCase=None ) -> List[str]: snake_case__ : str = 0 snake_case__ : Union[str, Any] = code.split("""\n""" ) if start_prompt is not None: while not lines[index].startswith(_lowerCAmelCase ): index += 1 snake_case__ : Tuple = ["""\n""".join(lines[:index] )] else: snake_case__ : List[str] = [] # We split into blocks until we get to the `end_prompt` (or the end of the block). snake_case__ : Optional[int] = [lines[index]] index += 1 while index < len(_lowerCAmelCase ) and (end_prompt is None or not lines[index].startswith(_lowerCAmelCase )): if len(lines[index] ) > 0 and get_indent(lines[index] ) == indent_level: if len(_lowerCAmelCase ) > 0 and get_indent(current_block[-1] ).startswith(indent_level + """ """ ): current_block.append(lines[index] ) blocks.append("""\n""".join(_lowerCAmelCase ) ) if index < len(_lowerCAmelCase ) - 1: snake_case__ : str = [lines[index + 1]] index += 1 else: snake_case__ : int = [] else: blocks.append("""\n""".join(_lowerCAmelCase ) ) snake_case__ : Optional[Any] = [lines[index]] else: current_block.append(lines[index] ) index += 1 # Adds current block if it's nonempty. if len(_lowerCAmelCase ) > 0: blocks.append("""\n""".join(_lowerCAmelCase ) ) # Add final block after end_prompt if provided. if end_prompt is not None and index < len(_lowerCAmelCase ): blocks.append("""\n""".join(lines[index:] ) ) return blocks def __snake_case( _lowerCAmelCase ) -> Tuple: def _inner(_lowerCAmelCase ): return key(_lowerCAmelCase ).lower().replace("""_""" , """""" ) return _inner def __snake_case( _lowerCAmelCase , _lowerCAmelCase=None ) -> List[Any]: # If no key is provided, we use a noop. def noop(_lowerCAmelCase ): return x if key is None: snake_case__ : Optional[int] = noop # Constants are all uppercase, they go first. snake_case__ : Optional[int] = [obj for obj in objects if key(_lowerCAmelCase ).isupper()] # Classes are not all uppercase but start with a capital, they go second. snake_case__ : int = [obj for obj in objects if key(_lowerCAmelCase )[0].isupper() and not key(_lowerCAmelCase ).isupper()] # Functions begin with a lowercase, they go last. snake_case__ : str = [obj for obj in objects if not key(_lowerCAmelCase )[0].isupper()] snake_case__ : List[str] = ignore_underscore(_lowerCAmelCase ) return sorted(_lowerCAmelCase , key=_lowerCAmelCase ) + sorted(_lowerCAmelCase , key=_lowerCAmelCase ) + sorted(_lowerCAmelCase , key=_lowerCAmelCase ) def __snake_case( _lowerCAmelCase ) -> int: # This inner function sort imports between [ ]. def _replace(_lowerCAmelCase ): snake_case__ : Union[str, Any] = match.groups()[0] if "," not in imports: return f"[{imports}]" snake_case__ : int = [part.strip().replace("""\"""" , """""" ) for part in imports.split(""",""" )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: snake_case__ : List[str] = keys[:-1] return "[" + ", ".join([f"\"{k}\"" for k in sort_objects(_lowerCAmelCase )] ) + "]" snake_case__ : str = import_statement.split("""\n""" ) if len(_lowerCAmelCase ) > 3: # Here we have to sort internal imports that are on several lines (one per name): # key: [ # "object1", # "object2", # ... # ] # We may have to ignore one or two lines on each side. snake_case__ : Dict = 2 if lines[1].strip() == """[""" else 1 snake_case__ : str = [(i, _re_strip_line.search(_lowerCAmelCase ).groups()[0]) for i, line in enumerate(lines[idx:-idx] )] snake_case__ : str = sort_objects(_lowerCAmelCase , key=lambda _lowerCAmelCase : x[1] ) snake_case__ : Union[str, Any] = [lines[x[0] + idx] for x in sorted_indices] return "\n".join(lines[:idx] + sorted_lines + lines[-idx:] ) elif len(_lowerCAmelCase ) == 3: # Here we have to sort internal imports that are on one separate line: # key: [ # "object1", "object2", ... # ] if _re_bracket_content.search(lines[1] ) is not None: snake_case__ : Union[str, Any] = _re_bracket_content.sub(_replace , lines[1] ) else: snake_case__ : List[Any] = [part.strip().replace("""\"""" , """""" ) for part in lines[1].split(""",""" )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: snake_case__ : List[str] = keys[:-1] snake_case__ : int = get_indent(lines[1] ) + """, """.join([f"\"{k}\"" for k in sort_objects(_lowerCAmelCase )] ) return "\n".join(_lowerCAmelCase ) else: # Finally we have to deal with imports fitting on one line snake_case__ : Optional[Any] = _re_bracket_content.sub(_replace , _lowerCAmelCase ) return import_statement def __snake_case( _lowerCAmelCase , _lowerCAmelCase=True ) -> Dict: with open(_lowerCAmelCase , encoding="""utf-8""" ) as f: snake_case__ : Optional[int] = f.read() if "_import_structure" not in code: return # Blocks of indent level 0 snake_case__ : Optional[int] = split_code_in_indented_blocks( _lowerCAmelCase , start_prompt="""_import_structure = {""" , end_prompt="""if TYPE_CHECKING:""" ) # We ignore block 0 (everything untils start_prompt) and the last block (everything after end_prompt). for block_idx in range(1 , len(_lowerCAmelCase ) - 1 ): # Check if the block contains some `_import_structure`s thingy to sort. snake_case__ : Optional[Any] = main_blocks[block_idx] snake_case__ : Dict = block.split("""\n""" ) # Get to the start of the imports. snake_case__ : Dict = 0 while line_idx < len(_lowerCAmelCase ) and "_import_structure" not in block_lines[line_idx]: # Skip dummy import blocks if "import dummy" in block_lines[line_idx]: snake_case__ : Union[str, Any] = len(_lowerCAmelCase ) else: line_idx += 1 if line_idx >= len(_lowerCAmelCase ): continue # Ignore beginning and last line: they don't contain anything. snake_case__ : List[str] = """\n""".join(block_lines[line_idx:-1] ) snake_case__ : str = get_indent(block_lines[1] ) # Slit the internal block into blocks of indent level 1. snake_case__ : Optional[int] = split_code_in_indented_blocks(_lowerCAmelCase , indent_level=_lowerCAmelCase ) # We have two categories of import key: list or _import_structure[key].append/extend snake_case__ : Tuple = _re_direct_key if """_import_structure = {""" in block_lines[0] else _re_indirect_key # Grab the keys, but there is a trap: some lines are empty or just comments. snake_case__ : Optional[Any] = [(pattern.search(_lowerCAmelCase ).groups()[0] if pattern.search(_lowerCAmelCase ) is not None else None) for b in internal_blocks] # We only sort the lines with a key. snake_case__ : Dict = [(i, key) for i, key in enumerate(_lowerCAmelCase ) if key is not None] snake_case__ : Union[str, Any] = [x[0] for x in sorted(_lowerCAmelCase , key=lambda _lowerCAmelCase : x[1] )] # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. snake_case__ : List[Any] = 0 snake_case__ : Optional[Any] = [] for i in range(len(_lowerCAmelCase ) ): if keys[i] is None: reorderded_blocks.append(internal_blocks[i] ) else: snake_case__ : Optional[Any] = sort_objects_in_import(internal_blocks[sorted_indices[count]] ) reorderded_blocks.append(_lowerCAmelCase ) count += 1 # And we put our main block back together with its first and last line. snake_case__ : Dict = """\n""".join(block_lines[:line_idx] + reorderded_blocks + [block_lines[-1]] ) if code != "\n".join(_lowerCAmelCase ): if check_only: return True else: print(f"Overwriting {file}." ) with open(_lowerCAmelCase , """w""" , encoding="""utf-8""" ) as f: f.write("""\n""".join(_lowerCAmelCase ) ) def __snake_case( _lowerCAmelCase=True ) -> Tuple: snake_case__ : str = [] for root, _, files in os.walk(_lowerCAmelCase ): if "__init__.py" in files: snake_case__ : Union[str, Any] = sort_imports(os.path.join(_lowerCAmelCase , """__init__.py""" ) , check_only=_lowerCAmelCase ) if result: snake_case__ : Union[str, Any] = [os.path.join(_lowerCAmelCase , """__init__.py""" )] if len(_lowerCAmelCase ) > 0: raise ValueError(f"Would overwrite {len(_lowerCAmelCase )} files, run `make style`." ) if __name__ == "__main__": __a = argparse.ArgumentParser() parser.add_argument("--check_only", action="store_true", help="Whether to only check or fix style.") __a = parser.parse_args() sort_imports_in_all_inits(check_only=args.check_only)
35
from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING lowercase : Any = logging.get_logger(__name__) @add_end_docstrings(lowerCAmelCase ) class __snake_case ( lowerCAmelCase ): def __init__( self ,*snake_case ,**snake_case ): '''simple docstring''' super().__init__(*snake_case ,**snake_case ) requires_backends(self ,"""vision""" ) self.check_model_type( TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING if self.framework == """tf""" else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING ) def _SCREAMING_SNAKE_CASE ( self ,snake_case=None ): '''simple docstring''' lowercase : List[Any] = {} if top_k is not None: lowercase : int = top_k return {}, {}, postprocess_params def __call__( self ,snake_case ,**snake_case ): '''simple docstring''' return super().__call__(snake_case ,**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Any = load_image(snake_case ) lowercase : List[Any] = self.image_processor(images=snake_case ,return_tensors=self.framework ) return model_inputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : int = self.model(**snake_case ) return model_outputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=5 ): '''simple docstring''' if top_k > self.model.config.num_labels: lowercase : Tuple = self.model.config.num_labels if self.framework == "pt": lowercase : str = model_outputs.logits.softmax(-1 )[0] lowercase , lowercase : Dict = probs.topk(snake_case ) elif self.framework == "tf": lowercase : Optional[int] = stable_softmax(model_outputs.logits ,axis=-1 )[0] lowercase : Union[str, Any] = tf.math.top_k(snake_case ,k=snake_case ) lowercase , lowercase : List[str] = topk.values.numpy(), topk.indices.numpy() else: raise ValueError(f"Unsupported framework: {self.framework}" ) lowercase : Tuple = scores.tolist() lowercase : Dict = ids.tolist() return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(snake_case ,snake_case )]
20
0
import logging import os from typing import List, TextIO, Union from conllu import parse_incr from utils_ner import InputExample, Split, TokenClassificationTask _snake_case = logging.getLogger(__name__) class UpperCAmelCase_ ( a): def __init__( self, __a=-1): '''simple docstring''' _lowerCAmelCase : Optional[int] = label_idx def snake_case__ ( self, __a, __a): '''simple docstring''' if isinstance(__a, __a): _lowerCAmelCase : Dict = mode.value _lowerCAmelCase : Optional[int] = os.path.join(__a, f"{mode}.txt") _lowerCAmelCase : Optional[int] = 1 _lowerCAmelCase : str = [] with open(__a, encoding="utf-8") as f: _lowerCAmelCase : Any = [] _lowerCAmelCase : int = [] for line in f: if line.startswith("-DOCSTART-") or line == "" or line == "\n": if words: examples.append(InputExample(guid=f"{mode}-{guid_index}", words=__a, labels=__a)) guid_index += 1 _lowerCAmelCase : List[Any] = [] _lowerCAmelCase : Any = [] else: _lowerCAmelCase : int = line.split(" ") words.append(splits[0]) if len(__a) > 1: labels.append(splits[self.label_idx].replace("\n", "")) else: # Examples could have no label for mode = "test" labels.append("O") if words: examples.append(InputExample(guid=f"{mode}-{guid_index}", words=__a, labels=__a)) return examples def snake_case__ ( self, __a, __a, __a): '''simple docstring''' _lowerCAmelCase : Any = 0 for line in test_input_reader: if line.startswith("-DOCSTART-") or line == "" or line == "\n": writer.write(__a) if not preds_list[example_id]: example_id += 1 elif preds_list[example_id]: _lowerCAmelCase : int = line.split()[0] + " " + preds_list[example_id].pop(0) + "\n" writer.write(__a) else: logger.warning("Maximum sequence length exceeded: No prediction for '%s'.", line.split()[0]) def snake_case__ ( self, __a): '''simple docstring''' if path: with open(__a, "r") as f: _lowerCAmelCase : Dict = f.read().splitlines() if "O" not in labels: _lowerCAmelCase : List[Any] = ["O"] + labels return labels else: return ["O", "B-MISC", "I-MISC", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"] class UpperCAmelCase_ ( a): def __init__( self): '''simple docstring''' super().__init__(label_idx=-2) def snake_case__ ( self, __a): '''simple docstring''' if path: with open(__a, "r") as f: _lowerCAmelCase : Any = f.read().splitlines() if "O" not in labels: _lowerCAmelCase : Optional[Any] = ["O"] + labels return labels else: return [ "O", "B-ADVP", "B-INTJ", "B-LST", "B-PRT", "B-NP", "B-SBAR", "B-VP", "B-ADJP", "B-CONJP", "B-PP", "I-ADVP", "I-INTJ", "I-LST", "I-PRT", "I-NP", "I-SBAR", "I-VP", "I-ADJP", "I-CONJP", "I-PP", ] class UpperCAmelCase_ ( a): def snake_case__ ( self, __a, __a): '''simple docstring''' if isinstance(__a, __a): _lowerCAmelCase : int = mode.value _lowerCAmelCase : List[str] = os.path.join(__a, f"{mode}.txt") _lowerCAmelCase : Dict = 1 _lowerCAmelCase : Optional[int] = [] with open(__a, encoding="utf-8") as f: for sentence in parse_incr(__a): _lowerCAmelCase : List[str] = [] _lowerCAmelCase : Union[str, Any] = [] for token in sentence: words.append(token["form"]) labels.append(token["upos"]) assert len(__a) == len(__a) if words: examples.append(InputExample(guid=f"{mode}-{guid_index}", words=__a, labels=__a)) guid_index += 1 return examples def snake_case__ ( self, __a, __a, __a): '''simple docstring''' _lowerCAmelCase : List[Any] = 0 for sentence in parse_incr(__a): _lowerCAmelCase : List[Any] = preds_list[example_id] _lowerCAmelCase : Union[str, Any] = "" for token in sentence: out += f"{token['form']} ({token['upos']}|{s_p.pop(0)}) " out += "\n" writer.write(__a) example_id += 1 def snake_case__ ( self, __a): '''simple docstring''' if path: with open(__a, "r") as f: return f.read().splitlines() else: return [ "ADJ", "ADP", "ADV", "AUX", "CCONJ", "DET", "INTJ", "NOUN", "NUM", "PART", "PRON", "PROPN", "PUNCT", "SCONJ", "SYM", "VERB", "X", ]
36
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class __snake_case : def __init__( self ,snake_case ,): '''simple docstring''' lowercase : Any = parent lowercase : Tuple = 13 lowercase : str = 7 lowercase : Dict = True lowercase : Dict = True lowercase : str = True lowercase : List[str] = True lowercase : int = True lowercase : Union[str, Any] = False lowercase : Dict = False lowercase : List[Any] = False lowercase : List[Any] = 2 lowercase : Optional[Any] = 99 lowercase : int = 0 lowercase : Tuple = 32 lowercase : int = 2 lowercase : Tuple = 4 lowercase : List[Any] = 0.1 lowercase : Tuple = 0.1 lowercase : List[Any] = 512 lowercase : int = 16 lowercase : Dict = 2 lowercase : int = 0.02 lowercase : Union[str, Any] = 3 lowercase : Any = 4 lowercase : List[Any] = """last""" lowercase : Tuple = True lowercase : List[Any] = None lowercase : Any = 0 def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) lowercase : List[str] = random_attention_mask([self.batch_size, self.seq_length] ,dtype=tf.floataa ) lowercase : Tuple = None if self.use_input_lengths: lowercase : List[str] = ( ids_tensor([self.batch_size] ,vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length lowercase : Tuple = None if self.use_token_type_ids: lowercase : Optional[int] = ids_tensor([self.batch_size, self.seq_length] ,self.n_langs ) lowercase : List[str] = None lowercase : List[str] = None lowercase : Optional[Any] = None if self.use_labels: lowercase : List[str] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) lowercase : List[str] = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) lowercase : str = ids_tensor([self.batch_size] ,2 ,dtype=tf.floataa ) lowercase : Optional[Any] = ids_tensor([self.batch_size] ,self.num_choices ) lowercase : str = FlaubertConfig( vocab_size=self.vocab_size ,n_special=self.n_special ,emb_dim=self.hidden_size ,n_layers=self.num_hidden_layers ,n_heads=self.num_attention_heads ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,gelu_activation=self.gelu_activation ,sinusoidal_embeddings=self.sinusoidal_embeddings ,asm=self.asm ,causal=self.causal ,n_langs=self.n_langs ,max_position_embeddings=self.max_position_embeddings ,initializer_range=self.initializer_range ,summary_type=self.summary_type ,use_proj=self.use_proj ,bos_token_id=self.bos_token_id ,) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Tuple = TFFlaubertModel(config=snake_case ) lowercase : str = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} lowercase : Optional[Any] = model(snake_case ) lowercase : List[Any] = [input_ids, input_mask] lowercase : int = model(snake_case ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : List[Any] = TFFlaubertWithLMHeadModel(snake_case ) lowercase : Optional[Any] = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} lowercase : int = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Tuple = TFFlaubertForQuestionAnsweringSimple(snake_case ) lowercase : Union[str, Any] = {"""input_ids""": input_ids, """lengths""": input_lengths} lowercase : Tuple = model(snake_case ) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Union[str, Any] = TFFlaubertForSequenceClassification(snake_case ) lowercase : str = {"""input_ids""": input_ids, """lengths""": input_lengths} lowercase : str = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Any = self.num_labels lowercase : List[str] = TFFlaubertForTokenClassification(config=snake_case ) lowercase : Dict = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} lowercase : int = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Any = self.num_choices lowercase : Dict = TFFlaubertForMultipleChoice(config=snake_case ) lowercase : Any = tf.tile(tf.expand_dims(snake_case ,1 ) ,(1, self.num_choices, 1) ) lowercase : Optional[Any] = tf.tile(tf.expand_dims(snake_case ,1 ) ,(1, self.num_choices, 1) ) lowercase : Dict = tf.tile(tf.expand_dims(snake_case ,1 ) ,(1, self.num_choices, 1) ) lowercase : Union[str, Any] = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } lowercase : int = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_choices) ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Any = self.prepare_config_and_inputs() ( ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ) : int = config_and_inputs lowercase : List[str] = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """langs""": token_type_ids, """lengths""": input_lengths, } return config, inputs_dict @require_tf class __snake_case ( lowerCAmelCase , lowerCAmelCase , unittest.TestCase ): _a : Dict= ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) _a : Optional[Any]= ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable _a : Any= ( { "feature-extraction": TFFlaubertModel, "fill-mask": TFFlaubertWithLMHeadModel, "question-answering": TFFlaubertForQuestionAnsweringSimple, "text-classification": TFFlaubertForSequenceClassification, "token-classification": TFFlaubertForTokenClassification, "zero-shot": TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) _a : Tuple= False _a : int= False def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ): '''simple docstring''' if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("""Fast""" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : str = TFFlaubertModelTester(self ) lowercase : List[Any] = ConfigTester(self ,config_class=snake_case ,emb_dim=37 ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' self.config_tester.run_common_tests() def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*snake_case ) @slow def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase : Dict = TFFlaubertModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) @require_tf @require_sentencepiece @require_tokenizers class __snake_case ( unittest.TestCase ): @slow def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased""" ) lowercase : int = tf.convert_to_tensor( [[0, 158, 735, 2592, 1424, 6727, 82, 1]] ,dtype=tf.intaa ,) # "J'aime flaubert !" lowercase : Dict = model(snake_case )[0] lowercase : Union[str, Any] = tf.TensorShape((1, 8, 512) ) self.assertEqual(output.shape ,snake_case ) # compare the actual values for a slice. lowercase : Tuple = tf.convert_to_tensor( [ [ [-1.8_768_773, -1.566_555, 0.27_072_418], [-1.6_920_038, -0.5_873_505, 1.9_329_599], [-2.9_563_985, -1.6_993_835, 1.7_972_052], ] ] ,dtype=tf.floataa ,) self.assertTrue(np.allclose(output[:, :3, :3].numpy() ,expected_slice.numpy() ,atol=1e-4 ) )
20
0
'''simple docstring''' def _SCREAMING_SNAKE_CASE ( UpperCamelCase ): """simple docstring""" if num < 0: return False lowerCAmelCase__ : int = num lowerCAmelCase__ : int = 0 while num > 0: lowerCAmelCase__ : Union[str, Any] = rev_num * 10 + (num % 10) num //= 10 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
37
import json import os from dataclasses import dataclass from functools import partial from typing import Callable import flax.linen as nn import jax import jax.numpy as jnp import joblib import optax import wandb from flax import jax_utils, struct, traverse_util from flax.serialization import from_bytes, to_bytes from flax.training import train_state from flax.training.common_utils import shard from tqdm.auto import tqdm from transformers import BigBirdConfig, FlaxBigBirdForQuestionAnswering from transformers.models.big_bird.modeling_flax_big_bird import FlaxBigBirdForQuestionAnsweringModule class __snake_case ( lowerCAmelCase ): _a : BigBirdConfig _a : jnp.dtype= jnp.floataa _a : bool= True def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' super().setup() lowercase : List[str] = nn.Dense(5 ,dtype=self.dtype ) def __call__( self ,*snake_case ,**snake_case ): '''simple docstring''' lowercase : int = super().__call__(*snake_case ,**snake_case ) lowercase : Any = self.cls(outputs[2] ) return outputs[:2] + (cls_out,) class __snake_case ( lowerCAmelCase ): _a : List[Any]= FlaxBigBirdForNaturalQuestionsModule def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: def cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None ): lowercase : int = logits.shape[-1] lowercase : Dict = (labels[..., None] == jnp.arange(SCREAMING_SNAKE_CASE__ )[None]).astype("""f4""" ) lowercase : Any = jax.nn.log_softmax(SCREAMING_SNAKE_CASE__ , axis=-1 ) lowercase : Optional[Any] = -jnp.sum(labels * logits , axis=-1 ) if reduction is not None: lowercase : Any = reduction(SCREAMING_SNAKE_CASE__ ) return loss lowercase : Optional[Any] = partial(SCREAMING_SNAKE_CASE__ , reduction=jnp.mean ) lowercase : Optional[int] = cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Dict = cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : int = cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return (start_loss + end_loss + pooled_loss) / 3 @dataclass class __snake_case : _a : str= "google/bigbird-roberta-base" _a : int= 3000 _a : int= 1_0500 _a : int= 128 _a : int= 3 _a : int= 1 _a : int= 5 # tx_args _a : float= 3E-5 _a : float= 0.0 _a : int= 2_0000 _a : float= 0.00_95 _a : str= "bigbird-roberta-natural-questions" _a : str= "training-expt" _a : str= "data/nq-training.jsonl" _a : str= "data/nq-validation.jsonl" def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' os.makedirs(self.base_dir ,exist_ok=snake_case ) lowercase : Optional[int] = os.path.join(self.base_dir ,self.save_dir ) lowercase : Optional[int] = self.batch_size_per_device * jax.device_count() @dataclass class __snake_case : _a : int _a : int= 4096 # no dynamic padding on TPUs def __call__( self ,snake_case ): '''simple docstring''' lowercase : int = self.collate_fn(snake_case ) lowercase : Union[str, Any] = jax.tree_util.tree_map(snake_case ,snake_case ) return batch def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase , lowercase : Union[str, Any] = self.fetch_inputs(features["""input_ids"""] ) lowercase : Tuple = { """input_ids""": jnp.array(snake_case ,dtype=jnp.intaa ), """attention_mask""": jnp.array(snake_case ,dtype=jnp.intaa ), """start_labels""": jnp.array(features["""start_token"""] ,dtype=jnp.intaa ), """end_labels""": jnp.array(features["""end_token"""] ,dtype=jnp.intaa ), """pooled_labels""": jnp.array(features["""category"""] ,dtype=jnp.intaa ), } return batch def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Tuple = [self._fetch_inputs(snake_case ) for ids in input_ids] return zip(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Union[str, Any] = [1 for _ in range(len(snake_case ) )] while len(snake_case ) < self.max_length: input_ids.append(self.pad_id ) attention_mask.append(0 ) return input_ids, attention_mask def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None ) -> Any: if seed is not None: lowercase : Optional[int] = dataset.shuffle(seed=SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) // batch_size ): lowercase : Optional[Any] = dataset[i * batch_size : (i + 1) * batch_size] yield dict(SCREAMING_SNAKE_CASE__ ) @partial(jax.pmap , axis_name="""batch""" ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) -> List[Any]: def loss_fn(SCREAMING_SNAKE_CASE__ ): lowercase : List[str] = model_inputs.pop("""start_labels""" ) lowercase : Optional[int] = model_inputs.pop("""end_labels""" ) lowercase : str = model_inputs.pop("""pooled_labels""" ) lowercase : Union[str, Any] = state.apply_fn(**SCREAMING_SNAKE_CASE__ , params=SCREAMING_SNAKE_CASE__ , dropout_rng=SCREAMING_SNAKE_CASE__ , train=SCREAMING_SNAKE_CASE__ ) lowercase , lowercase , lowercase : List[str] = outputs return state.loss_fn( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) lowercase , lowercase : int = jax.random.split(SCREAMING_SNAKE_CASE__ ) lowercase : Dict = jax.value_and_grad(SCREAMING_SNAKE_CASE__ ) lowercase , lowercase : Union[str, Any] = grad_fn(state.params ) lowercase : List[Any] = jax.lax.pmean({"""loss""": loss} , axis_name="""batch""" ) lowercase : List[Any] = jax.lax.pmean(SCREAMING_SNAKE_CASE__ , """batch""" ) lowercase : str = state.apply_gradients(grads=SCREAMING_SNAKE_CASE__ ) return state, metrics, new_drp_rng @partial(jax.pmap , axis_name="""batch""" ) def _snake_case( SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : int = model_inputs.pop("""start_labels""" ) lowercase : Dict = model_inputs.pop("""end_labels""" ) lowercase : Optional[Any] = model_inputs.pop("""pooled_labels""" ) lowercase : Optional[int] = state.apply_fn(**SCREAMING_SNAKE_CASE__ , params=state.params , train=SCREAMING_SNAKE_CASE__ ) lowercase , lowercase , lowercase : List[Any] = outputs lowercase : Dict = state.loss_fn(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : str = jax.lax.pmean({"""loss""": loss} , axis_name="""batch""" ) return metrics class __snake_case ( train_state.TrainState ): _a : Callable= struct.field(pytree_node=lowerCAmelCase ) @dataclass class __snake_case : _a : Args _a : Callable _a : Callable _a : Callable _a : Callable _a : wandb _a : Callable= None def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case=None ): '''simple docstring''' lowercase : Tuple = model.params lowercase : Any = TrainState.create( apply_fn=model.__call__ ,params=snake_case ,tx=snake_case ,loss_fn=snake_case ,) if ckpt_dir is not None: lowercase , lowercase , lowercase , lowercase , lowercase : Tuple = restore_checkpoint(snake_case ,snake_case ) lowercase : List[str] = { """lr""": args.lr, """init_lr""": args.init_lr, """warmup_steps""": args.warmup_steps, """num_train_steps""": num_train_steps, """weight_decay""": args.weight_decay, } lowercase , lowercase : Tuple = build_tx(**snake_case ) lowercase : str = train_state.TrainState( step=snake_case ,apply_fn=model.__call__ ,params=snake_case ,tx=snake_case ,opt_state=snake_case ,) lowercase : Any = args lowercase : Optional[Any] = data_collator lowercase : List[str] = lr lowercase : str = params lowercase : Tuple = jax_utils.replicate(snake_case ) return state def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ): '''simple docstring''' lowercase : Dict = self.args lowercase : Optional[Any] = len(snake_case ) // args.batch_size lowercase : int = jax.random.PRNGKey(0 ) lowercase : List[str] = jax.random.split(snake_case ,jax.device_count() ) for epoch in range(args.max_epochs ): lowercase : List[Any] = jnp.array(0 ,dtype=jnp.floataa ) lowercase : List[str] = get_batched_dataset(snake_case ,args.batch_size ,seed=snake_case ) lowercase : int = 0 for batch in tqdm(snake_case ,total=snake_case ,desc=f"Running EPOCH-{epoch}" ): lowercase : Dict = self.data_collator(snake_case ) lowercase , lowercase , lowercase : Optional[int] = self.train_step_fn(snake_case ,snake_case ,**snake_case ) running_loss += jax_utils.unreplicate(metrics["""loss"""] ) i += 1 if i % args.logging_steps == 0: lowercase : Optional[Any] = jax_utils.unreplicate(state.step ) lowercase : List[str] = running_loss.item() / i lowercase : List[str] = self.scheduler_fn(state_step - 1 ) lowercase : int = self.evaluate(snake_case ,snake_case ) lowercase : Tuple = { """step""": state_step.item(), """eval_loss""": eval_loss.item(), """tr_loss""": tr_loss, """lr""": lr.item(), } tqdm.write(str(snake_case ) ) self.logger.log(snake_case ,commit=snake_case ) if i % args.save_steps == 0: self.save_checkpoint(args.save_dir + f"-e{epoch}-s{i}" ,state=snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : List[str] = get_batched_dataset(snake_case ,self.args.batch_size ) lowercase : Any = len(snake_case ) // self.args.batch_size lowercase : List[Any] = jnp.array(0 ,dtype=jnp.floataa ) lowercase : Optional[int] = 0 for batch in tqdm(snake_case ,total=snake_case ,desc="""Evaluating ... """ ): lowercase : Tuple = self.data_collator(snake_case ) lowercase : Optional[int] = self.val_step_fn(snake_case ,**snake_case ) running_loss += jax_utils.unreplicate(metrics["""loss"""] ) i += 1 return running_loss / i def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : str = jax_utils.unreplicate(snake_case ) print(f"SAVING CHECKPOINT IN {save_dir}" ,end=""" ... """ ) self.model_save_fn(snake_case ,params=state.params ) with open(os.path.join(snake_case ,"""opt_state.msgpack""" ) ,"""wb""" ) as f: f.write(to_bytes(state.opt_state ) ) joblib.dump(self.args ,os.path.join(snake_case ,"""args.joblib""" ) ) joblib.dump(self.data_collator ,os.path.join(snake_case ,"""data_collator.joblib""" ) ) with open(os.path.join(snake_case ,"""training_state.json""" ) ,"""w""" ) as f: json.dump({"""step""": state.step.item()} ,snake_case ) print("""DONE""" ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: print(f"RESTORING CHECKPOINT FROM {save_dir}" , end=""" ... """ ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , """flax_model.msgpack""" ) , """rb""" ) as f: lowercase : str = from_bytes(state.params , f.read() ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , """opt_state.msgpack""" ) , """rb""" ) as f: lowercase : Optional[int] = from_bytes(state.opt_state , f.read() ) lowercase : Optional[Any] = joblib.load(os.path.join(SCREAMING_SNAKE_CASE__ , """args.joblib""" ) ) lowercase : int = joblib.load(os.path.join(SCREAMING_SNAKE_CASE__ , """data_collator.joblib""" ) ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , """training_state.json""" ) , """r""" ) as f: lowercase : Tuple = json.load(SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = training_state["""step"""] print("""DONE""" ) return params, opt_state, step, args, data_collator def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: lowercase : List[str] = num_train_steps - warmup_steps lowercase : Dict = optax.linear_schedule(init_value=SCREAMING_SNAKE_CASE__ , end_value=SCREAMING_SNAKE_CASE__ , transition_steps=SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = optax.linear_schedule(init_value=SCREAMING_SNAKE_CASE__ , end_value=1e-7 , transition_steps=SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = optax.join_schedules(schedules=[warmup_fn, decay_fn] , boundaries=[warmup_steps] ) return lr def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: def weight_decay_mask(SCREAMING_SNAKE_CASE__ ): lowercase : List[Any] = traverse_util.flatten_dict(SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = {k: (v[-1] != """bias""" and v[-2:] != ("""LayerNorm""", """scale""")) for k, v in params.items()} return traverse_util.unflatten_dict(SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = scheduler_fn(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = optax.adamw(learning_rate=SCREAMING_SNAKE_CASE__ , weight_decay=SCREAMING_SNAKE_CASE__ , mask=SCREAMING_SNAKE_CASE__ ) return tx, lr
20
0
import asyncio import os import re import sys import tempfile import unittest from contextlib import contextmanager from copy import deepcopy from distutils.util import strtobool from enum import Enum from importlib.util import find_spec from pathlib import Path from unittest.mock import patch import pyarrow as pa import pytest import requests from packaging import version from datasets import config if config.PY_VERSION < version.parse('''3.8'''): import importlib_metadata else: import importlib.metadata as importlib_metadata def SCREAMING_SNAKE_CASE_ ( __magic_name__ : str , __magic_name__ : Optional[int]=False ) -> Tuple: """simple docstring""" try: UpperCamelCase :str = os.environ[key] except KeyError: # KEY isn't set, default to `default`. UpperCamelCase :str = default else: # KEY is set, convert it to True or False. try: UpperCamelCase :Union[str, Any] = strtobool(__magic_name__ ) except ValueError: # More values are supported, but let's keep the message simple. raise ValueError(f"""If set, {key} must be yes or no.""" ) return _value UpperCAmelCase_ : Any = parse_flag_from_env('''RUN_SLOW''', default=False) UpperCAmelCase_ : Optional[Any] = parse_flag_from_env('''RUN_REMOTE''', default=False) UpperCAmelCase_ : Dict = parse_flag_from_env('''RUN_LOCAL''', default=True) UpperCAmelCase_ : str = parse_flag_from_env('''RUN_PACKAGED''', default=True) # Compression UpperCAmelCase_ : str = pytest.mark.skipif(not config.LZ4_AVAILABLE, reason='''test requires lz4''') UpperCAmelCase_ : Optional[Any] = pytest.mark.skipif(not config.PY7ZR_AVAILABLE, reason='''test requires py7zr''') UpperCAmelCase_ : str = pytest.mark.skipif(not config.ZSTANDARD_AVAILABLE, reason='''test requires zstandard''') # Audio UpperCAmelCase_ : str = pytest.mark.skipif( # On Windows and OS X, soundfile installs sndfile find_spec('''soundfile''') is None or version.parse(importlib_metadata.version('''soundfile''')) < version.parse('''0.12.0'''), reason='''test requires sndfile>=0.12.1: \'pip install \"soundfile>=0.12.1\"\'; ''', ) # Beam UpperCAmelCase_ : Any = pytest.mark.skipif( not config.BEAM_AVAILABLE or config.DILL_VERSION >= version.parse('''0.3.2'''), reason='''test requires apache-beam and a compatible dill version''', ) # Dill-cloudpickle compatibility UpperCAmelCase_ : Optional[int] = pytest.mark.skipif( config.DILL_VERSION <= version.parse('''0.3.2'''), reason='''test requires dill>0.3.2 for cloudpickle compatibility''', ) # Windows UpperCAmelCase_ : Optional[Any] = pytest.mark.skipif( sys.platform == '''win32''', reason='''test should not be run on Windows''', ) def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Dict ) -> Any: """simple docstring""" try: import faiss # noqa except ImportError: UpperCamelCase :List[str] = unittest.skip("""test requires faiss""" )(__magic_name__ ) return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Tuple ) -> int: """simple docstring""" try: import regex # noqa except ImportError: UpperCamelCase :Dict = unittest.skip("""test requires regex""" )(__magic_name__ ) return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Any ) -> Dict: """simple docstring""" try: import elasticsearch # noqa except ImportError: UpperCamelCase :int = unittest.skip("""test requires elasticsearch""" )(__magic_name__ ) return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : str ) -> Dict: """simple docstring""" try: import sqlalchemy # noqa except ImportError: UpperCamelCase :Tuple = unittest.skip("""test requires sqlalchemy""" )(__magic_name__ ) return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Any ) -> List[Any]: """simple docstring""" if not config.TORCH_AVAILABLE: UpperCamelCase :int = unittest.skip("""test requires PyTorch""" )(__magic_name__ ) return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Optional[Any] ) -> List[Any]: """simple docstring""" if not config.TF_AVAILABLE: UpperCamelCase :str = unittest.skip("""test requires TensorFlow""" )(__magic_name__ ) return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : List[Any] ) -> Optional[Any]: """simple docstring""" if not config.JAX_AVAILABLE: UpperCamelCase :Optional[int] = unittest.skip("""test requires JAX""" )(__magic_name__ ) return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Optional[Any] ) -> Union[str, Any]: """simple docstring""" if not config.PIL_AVAILABLE: UpperCamelCase :int = unittest.skip("""test requires Pillow""" )(__magic_name__ ) return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Optional[int] ) -> Dict: """simple docstring""" try: import transformers # noqa F401 except ImportError: return unittest.skip("""test requires transformers""" )(__magic_name__ ) else: return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : str ) -> int: """simple docstring""" try: import tiktoken # noqa F401 except ImportError: return unittest.skip("""test requires tiktoken""" )(__magic_name__ ) else: return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : List[Any] ) -> List[str]: """simple docstring""" try: import spacy # noqa F401 except ImportError: return unittest.skip("""test requires spacy""" )(__magic_name__ ) else: return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Optional[Any] ) -> Tuple: """simple docstring""" def _require_spacy_model(__magic_name__ : List[str] ): try: import spacy # noqa F401 spacy.load(__magic_name__ ) except ImportError: return unittest.skip("""test requires spacy""" )(__magic_name__ ) except OSError: return unittest.skip("""test requires spacy model '{}'""".format(__magic_name__ ) )(__magic_name__ ) else: return test_case return _require_spacy_model def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Tuple ) -> List[Any]: """simple docstring""" try: import pyspark # noqa F401 except ImportError: return unittest.skip("""test requires pyspark""" )(__magic_name__ ) else: return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Tuple ) -> Optional[int]: """simple docstring""" try: import joblibspark # noqa F401 except ImportError: return unittest.skip("""test requires joblibspark""" )(__magic_name__ ) else: return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : str ) -> List[str]: """simple docstring""" if not _run_slow_tests or _run_slow_tests == 0: UpperCamelCase :Optional[int] = unittest.skip("""test is slow""" )(__magic_name__ ) return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Any ) -> Union[str, Any]: """simple docstring""" if not _run_local_tests or _run_local_tests == 0: UpperCamelCase :str = unittest.skip("""test is local""" )(__magic_name__ ) return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Any ) -> int: """simple docstring""" if not _run_packaged_tests or _run_packaged_tests == 0: UpperCamelCase :Optional[int] = unittest.skip("""test is packaged""" )(__magic_name__ ) return test_case def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Optional[Any] ) -> Union[str, Any]: """simple docstring""" if not _run_remote_tests or _run_remote_tests == 0: UpperCamelCase :Optional[int] = unittest.skip("""test requires remote""" )(__magic_name__ ) return test_case def SCREAMING_SNAKE_CASE_ ( *__magic_name__ : int ) -> List[str]: """simple docstring""" def decorate(cls : int ): for name, fn in cls.__dict__.items(): if callable(__magic_name__ ) and name.startswith("""test""" ): for decorator in decorators: UpperCamelCase :int = decorator(__magic_name__ ) setattr(cls , __magic_name__ , __magic_name__ ) return cls return decorate class _SCREAMING_SNAKE_CASE ( _a ): pass class _SCREAMING_SNAKE_CASE ( _a ): snake_case__ : Union[str, Any] = 0 snake_case__ : Union[str, Any] = 1 snake_case__ : List[str] = 2 @contextmanager def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Dict=OfflineSimulationMode.CONNECTION_FAILS , __magic_name__ : str=1E-16 ) -> Any: """simple docstring""" UpperCamelCase :Optional[int] = requests.Session().request def timeout_request(__magic_name__ : str , __magic_name__ : List[str] , __magic_name__ : Optional[int] , **__magic_name__ : Dict ): # Change the url to an invalid url so that the connection hangs UpperCamelCase :Dict = """https://10.255.255.1""" if kwargs.get("""timeout""" ) is None: raise RequestWouldHangIndefinitelyError( f"""Tried a call to {url} in offline mode with no timeout set. Please set a timeout.""" ) UpperCamelCase :List[str] = timeout try: return online_request(__magic_name__ , __magic_name__ , **__magic_name__ ) except Exception as e: # The following changes in the error are just here to make the offline timeout error prettier UpperCamelCase :Any = url UpperCamelCase :Any = e.args[0] UpperCamelCase :Tuple = (max_retry_error.args[0].replace("""10.255.255.1""" , f"""OfflineMock[{url}]""" ),) UpperCamelCase :List[str] = (max_retry_error,) raise def raise_connection_error(__magic_name__ : Tuple , __magic_name__ : int , **__magic_name__ : str ): raise requests.ConnectionError("""Offline mode is enabled.""" , request=__magic_name__ ) if mode is OfflineSimulationMode.CONNECTION_FAILS: with patch("""requests.Session.send""" , __magic_name__ ): yield elif mode is OfflineSimulationMode.CONNECTION_TIMES_OUT: # inspired from https://stackoverflow.com/a/904609 with patch("""requests.Session.request""" , __magic_name__ ): yield elif mode is OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1: with patch("""datasets.config.HF_DATASETS_OFFLINE""" , __magic_name__ ): yield else: raise ValueError("""Please use a value from the OfflineSimulationMode enum.""" ) @contextmanager def SCREAMING_SNAKE_CASE_ ( *__magic_name__ : str , **__magic_name__ : Dict ) -> List[Any]: """simple docstring""" UpperCamelCase :Optional[int] = str(Path().resolve() ) with tempfile.TemporaryDirectory(*__magic_name__ , **__magic_name__ ) as tmp_dir: try: os.chdir(__magic_name__ ) yield finally: os.chdir(__magic_name__ ) @contextmanager def SCREAMING_SNAKE_CASE_ ( ) -> Optional[int]: """simple docstring""" import gc gc.collect() UpperCamelCase :Tuple = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory > 0, "Arrow memory didn't increase." @contextmanager def SCREAMING_SNAKE_CASE_ ( ) -> Optional[Any]: """simple docstring""" import gc gc.collect() UpperCamelCase :Dict = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory <= 0, "Arrow memory wasn't expected to increase." def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Dict , __magic_name__ : Dict ) -> Tuple: """simple docstring""" return deepcopy(__magic_name__ ).integers(0 , 100 , 10 ).tolist() == deepcopy(__magic_name__ ).integers(0 , 100 , 10 ).tolist() def SCREAMING_SNAKE_CASE_ ( __magic_name__ : List[str] ) -> Union[str, Any]: """simple docstring""" import decorator from requests.exceptions import HTTPError def _wrapper(__magic_name__ : Tuple , *__magic_name__ : List[str] , **__magic_name__ : List[Any] ): try: return func(*__magic_name__ , **__magic_name__ ) except HTTPError as err: if str(__magic_name__ ).startswith("""500""" ) or str(__magic_name__ ).startswith("""502""" ): pytest.xfail(str(__magic_name__ ) ) raise err return decorator.decorator(_wrapper , __magic_name__ ) class _SCREAMING_SNAKE_CASE : def __init__( self : Tuple , __lowerCamelCase : Dict , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Any ): UpperCamelCase :int = returncode UpperCamelCase :Tuple = stdout UpperCamelCase :List[str] = stderr async def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Dict , __magic_name__ : List[str] ) -> Union[str, Any]: """simple docstring""" while True: UpperCamelCase :List[str] = await stream.readline() if line: callback(__magic_name__ ) else: break async def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Tuple , __magic_name__ : List[str]=None , __magic_name__ : Optional[int]=None , __magic_name__ : int=None , __magic_name__ : int=False , __magic_name__ : str=False ) -> _RunOutput: """simple docstring""" if echo: print("""\nRunning: """ , """ """.join(__magic_name__ ) ) UpperCamelCase :List[str] = await asyncio.create_subprocess_exec( cmd[0] , *cmd[1:] , stdin=__magic_name__ , stdout=asyncio.subprocess.PIPE , stderr=asyncio.subprocess.PIPE , env=__magic_name__ , ) # note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe # https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait # # If it starts hanging, will need to switch to the following code. The problem is that no data # will be seen until it's done and if it hangs for example there will be no debug info. # out, err = await p.communicate() # return _RunOutput(p.returncode, out, err) UpperCamelCase :List[Any] = [] UpperCamelCase :List[Any] = [] def tee(__magic_name__ : List[str] , __magic_name__ : List[Any] , __magic_name__ : int , __magic_name__ : Optional[Any]="" ): UpperCamelCase :List[Any] = line.decode("""utf-8""" ).rstrip() sink.append(__magic_name__ ) if not quiet: print(__magic_name__ , __magic_name__ , file=__magic_name__ ) # XXX: the timeout doesn't seem to make any difference here await asyncio.wait( [ _read_stream(p.stdout , lambda __magic_name__ : tee(__magic_name__ , __magic_name__ , sys.stdout , label="""stdout:""" ) ), _read_stream(p.stderr , lambda __magic_name__ : tee(__magic_name__ , __magic_name__ , sys.stderr , label="""stderr:""" ) ), ] , timeout=__magic_name__ , ) return _RunOutput(await p.wait() , __magic_name__ , __magic_name__ ) def SCREAMING_SNAKE_CASE_ ( __magic_name__ : int , __magic_name__ : List[Any]=None , __magic_name__ : Optional[Any]=None , __magic_name__ : List[str]=180 , __magic_name__ : Optional[Any]=False , __magic_name__ : Any=True ) -> _RunOutput: """simple docstring""" UpperCamelCase :str = asyncio.get_event_loop() UpperCamelCase :Optional[int] = loop.run_until_complete( _stream_subprocess(__magic_name__ , env=__magic_name__ , stdin=__magic_name__ , timeout=__magic_name__ , quiet=__magic_name__ , echo=__magic_name__ ) ) UpperCamelCase :Tuple = """ """.join(__magic_name__ ) if result.returncode > 0: UpperCamelCase :int = """\n""".join(result.stderr ) raise RuntimeError( f"""'{cmd_str}' failed with returncode {result.returncode}\n\n""" f"""The combined stderr from workers follows:\n{stderr}""" ) # check that the subprocess actually did run and produced some output, should the test rely on # the remote side to do the testing if not result.stdout and not result.stderr: raise RuntimeError(f"""'{cmd_str}' produced no output.""" ) return result def SCREAMING_SNAKE_CASE_ ( ) -> Union[str, Any]: """simple docstring""" UpperCamelCase :int = os.environ.get("""PYTEST_XDIST_WORKER""" , """gw0""" ) UpperCamelCase :str = re.sub(R"""^gw""" , """""" , __magic_name__ , 0 , re.M ) return int(__magic_name__ ) def SCREAMING_SNAKE_CASE_ ( ) -> Any: """simple docstring""" UpperCamelCase :Union[str, Any] = 2_9500 UpperCamelCase :Union[str, Any] = pytest_xdist_worker_id() return port + uniq_delta
38
from math import sqrt def _snake_case( SCREAMING_SNAKE_CASE__ ) -> bool: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number >= 0 ), "'number' must been an int and positive" lowercase : Union[str, Any] = True # 0 and 1 are none primes. if number <= 1: lowercase : str = False for divisor in range(2 , int(round(sqrt(SCREAMING_SNAKE_CASE__ ) ) ) + 1 ): # if 'number' divisible by 'divisor' then sets 'status' # of false and break up the loop. if number % divisor == 0: lowercase : Any = False break # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'status' must been from type bool" return status def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n > 2), "'N' must been an int and > 2" # beginList: contains all natural numbers from 2 up to N lowercase : str = list(range(2 , n + 1 ) ) lowercase : Tuple = [] # this list will be returns. # actual sieve of erathostenes for i in range(len(SCREAMING_SNAKE_CASE__ ) ): for j in range(i + 1 , len(SCREAMING_SNAKE_CASE__ ) ): if (begin_list[i] != 0) and (begin_list[j] % begin_list[i] == 0): lowercase : Tuple = 0 # filters actual prime numbers. lowercase : int = [x for x in begin_list if x != 0] # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type list" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n > 2), "'N' must been an int and > 2" lowercase : Dict = [] # iterates over all numbers between 2 up to N+1 # if a number is prime then appends to list 'ans' for number in range(2 , n + 1 ): if is_prime(SCREAMING_SNAKE_CASE__ ): ans.append(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type list" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Tuple: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and number >= 0, "'number' must been an int and >= 0" lowercase : Tuple = [] # this list will be returns of the function. # potential prime number factors. lowercase : Optional[Any] = 2 lowercase : Any = number if number == 0 or number == 1: ans.append(SCREAMING_SNAKE_CASE__ ) # if 'number' not prime then builds the prime factorization of 'number' elif not is_prime(SCREAMING_SNAKE_CASE__ ): while quotient != 1: if is_prime(SCREAMING_SNAKE_CASE__ ) and (quotient % factor == 0): ans.append(SCREAMING_SNAKE_CASE__ ) quotient /= factor else: factor += 1 else: ans.append(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type list" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number >= 0 ), "'number' bust been an int and >= 0" lowercase : Tuple = 0 # prime factorization of 'number' lowercase : Optional[int] = prime_factorization(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = max(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type int" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> str: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number >= 0 ), "'number' bust been an int and >= 0" lowercase : Union[str, Any] = 0 # prime factorization of 'number' lowercase : Tuple = prime_factorization(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = min(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type int" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'number' must been an int" assert isinstance(number % 2 == 0 , SCREAMING_SNAKE_CASE__ ), "compare bust been from type bool" return number % 2 == 0 def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'number' must been an int" assert isinstance(number % 2 != 0 , SCREAMING_SNAKE_CASE__ ), "compare bust been from type bool" return number % 2 != 0 def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (number > 2) and is_even(SCREAMING_SNAKE_CASE__ ) ), "'number' must been an int, even and > 2" lowercase : Union[str, Any] = [] # this list will returned # creates a list of prime numbers between 2 up to 'number' lowercase : str = get_prime_numbers(SCREAMING_SNAKE_CASE__ ) lowercase : Any = len(SCREAMING_SNAKE_CASE__ ) # run variable for while-loops. lowercase : Optional[Any] = 0 lowercase : List[Any] = None # exit variable. for break up the loops lowercase : Any = True while i < len_pn and loop: lowercase : str = i + 1 while j < len_pn and loop: if prime_numbers[i] + prime_numbers[j] == number: lowercase : Union[str, Any] = False ans.append(prime_numbers[i] ) ans.append(prime_numbers[j] ) j += 1 i += 1 # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (len(SCREAMING_SNAKE_CASE__ ) == 2) and (ans[0] + ans[1] == number) and is_prime(ans[0] ) and is_prime(ans[1] ) ), "'ans' must contains two primes. And sum of elements must been eq 'number'" return ans def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (numbera >= 0) and (numbera >= 0) ), "'number1' and 'number2' must been positive integer." lowercase : Union[str, Any] = 0 while numbera != 0: lowercase : Optional[int] = numbera % numbera lowercase : Optional[int] = numbera lowercase : Dict = rest # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( numbera >= 0 ), "'number' must been from type int and positive" return numbera def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (numbera >= 1) and (numbera >= 1) ), "'number1' and 'number2' must been positive integer." lowercase : Dict = 1 # actual answer that will be return. # for kgV (x,1) if numbera > 1 and numbera > 1: # builds the prime factorization of 'number1' and 'number2' lowercase : Optional[Any] = prime_factorization(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[int] = prime_factorization(SCREAMING_SNAKE_CASE__ ) elif numbera == 1 or numbera == 1: lowercase : Union[str, Any] = [] lowercase : List[str] = [] lowercase : Dict = max(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = 0 lowercase : Optional[Any] = 0 lowercase : List[str] = [] # captured numbers int both 'primeFac1' and 'primeFac2' # iterates through primeFac1 for n in prime_fac_a: if n not in done: if n in prime_fac_a: lowercase : Dict = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) for _ in range(max(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ): ans *= n else: lowercase : List[Any] = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) for _ in range(SCREAMING_SNAKE_CASE__ ): ans *= n done.append(SCREAMING_SNAKE_CASE__ ) # iterates through primeFac2 for n in prime_fac_a: if n not in done: lowercase : Optional[int] = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) for _ in range(SCREAMING_SNAKE_CASE__ ): ans *= n done.append(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( ans >= 0 ), "'ans' must been from type int and positive" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 0), "'number' must been a positive int" lowercase : Dict = 0 lowercase : List[str] = 2 # this variable holds the answer while index < n: index += 1 ans += 1 # counts to the next number # if ans not prime then # runs to the next prime number. while not is_prime(SCREAMING_SNAKE_CASE__ ): ans += 1 # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and is_prime( SCREAMING_SNAKE_CASE__ ), "'ans' must been a prime number and from type int" return ans def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: assert ( is_prime(SCREAMING_SNAKE_CASE__ ) and is_prime(SCREAMING_SNAKE_CASE__ ) and (p_number_a < p_number_a) ), "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" lowercase : List[str] = p_number_a + 1 # jump to the next number lowercase : List[Any] = [] # this list will be returns. # if number is not prime then # fetch the next prime number. while not is_prime(SCREAMING_SNAKE_CASE__ ): number += 1 while number < p_number_a: ans.append(SCREAMING_SNAKE_CASE__ ) number += 1 # fetch the next prime number. while not is_prime(SCREAMING_SNAKE_CASE__ ): number += 1 # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ans[0] != p_number_a and ans[len(SCREAMING_SNAKE_CASE__ ) - 1] != p_number_a ), "'ans' must been a list without the arguments" # 'ans' contains not 'pNumber1' and 'pNumber2' ! return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 1), "'n' must been int and >= 1" lowercase : Optional[Any] = [] # will be returned. for divisor in range(1 , n + 1 ): if n % divisor == 0: ans.append(SCREAMING_SNAKE_CASE__ ) # precondition assert ans[0] == 1 and ans[len(SCREAMING_SNAKE_CASE__ ) - 1] == n, "Error in function getDivisiors(...)" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number > 1 ), "'number' must been an int and >= 1" lowercase : str = get_divisors(SCREAMING_SNAKE_CASE__ ) # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (divisors[0] == 1) and (divisors[len(SCREAMING_SNAKE_CASE__ ) - 1] == number) ), "Error in help-function getDivisiors(...)" # summed all divisors up to 'number' (exclusive), hence [:-1] return sum(divisors[:-1] ) == number def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (denominator != 0) ), "The arguments must been from type int and 'denominator' != 0" # build the greatest common divisor of numerator and denominator. lowercase : Tuple = gcd(abs(SCREAMING_SNAKE_CASE__ ) , abs(SCREAMING_SNAKE_CASE__ ) ) # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (numerator % gcd_of_fraction == 0) and (denominator % gcd_of_fraction == 0) ), "Error in function gcd(...,...)" return (numerator // gcd_of_fraction, denominator // gcd_of_fraction) def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 0), "'n' must been a int and >= 0" lowercase : List[str] = 1 # this will be return. for factor in range(1 , n + 1 ): ans *= factor return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 0), "'n' must been an int and >= 0" lowercase : int = 0 lowercase : Union[str, Any] = 1 lowercase : int = 1 # this will be return for _ in range(n - 1 ): lowercase : Optional[int] = ans ans += fiba lowercase : Optional[int] = tmp return ans
20
0
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel from transformers.utils import logging logging.set_verbosity_info() _a = logging.get_logger(__name__) def __A ( __lowerCAmelCase , __lowerCAmelCase=False )-> Union[str, Any]: """simple docstring""" _UpperCAmelCase = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""blocks.{i}.norm1.weight""", F"""vit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""blocks.{i}.norm1.bias""", F"""vit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append((F"""blocks.{i}.attn.proj.weight""", F"""vit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.attn.proj.bias""", F"""vit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""blocks.{i}.norm2.weight""", F"""vit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""blocks.{i}.norm2.bias""", F"""vit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.weight""", F"""vit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.bias""", F"""vit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.weight""", F"""vit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.bias""", F"""vit.encoder.layer.{i}.output.dense.bias""") ) # projection layer + position embeddings rename_keys.extend( [ ('cls_token', 'vit.embeddings.cls_token'), ('patch_embed.proj.weight', 'vit.embeddings.patch_embeddings.projection.weight'), ('patch_embed.proj.bias', 'vit.embeddings.patch_embeddings.projection.bias'), ('pos_embed', 'vit.embeddings.position_embeddings'), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('norm.weight', 'layernorm.weight'), ('norm.bias', 'layernorm.bias'), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" _UpperCAmelCase = [(pair[0], pair[1][4:]) if pair[1].startswith('vit' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('norm.weight', 'vit.layernorm.weight'), ('norm.bias', 'vit.layernorm.bias'), ('head.weight', 'classifier.weight'), ('head.bias', 'classifier.bias'), ] ) return rename_keys def __A ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=False )-> List[str]: """simple docstring""" for i in range(config.num_hidden_layers ): if base_model: _UpperCAmelCase = '' else: _UpperCAmelCase = 'vit.' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) _UpperCAmelCase = state_dict.pop(F"""blocks.{i}.attn.qkv.weight""" ) _UpperCAmelCase = state_dict.pop(F"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict _UpperCAmelCase = in_proj_weight[ : config.hidden_size, : ] _UpperCAmelCase = in_proj_bias[: config.hidden_size] _UpperCAmelCase = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] _UpperCAmelCase = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] _UpperCAmelCase = in_proj_weight[ -config.hidden_size :, : ] _UpperCAmelCase = in_proj_bias[-config.hidden_size :] def __A ( __lowerCAmelCase )-> Optional[Any]: """simple docstring""" _UpperCAmelCase = ['head.weight', 'head.bias'] for k in ignore_keys: state_dict.pop(__lowerCAmelCase , __lowerCAmelCase ) def __A ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> int: """simple docstring""" _UpperCAmelCase = dct.pop(__lowerCAmelCase ) _UpperCAmelCase = val def __A ( )-> str: """simple docstring""" _UpperCAmelCase = 'http://images.cocodataset.org/val2017/000000039769.jpg' _UpperCAmelCase = Image.open(requests.get(__lowerCAmelCase , stream=__lowerCAmelCase ).raw ) return im @torch.no_grad() def __A ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=True )-> List[str]: """simple docstring""" _UpperCAmelCase = ViTConfig() # patch_size if model_name[-1] == "8": _UpperCAmelCase = 8 # set labels if required if not base_model: _UpperCAmelCase = 1_000 _UpperCAmelCase = 'huggingface/label-files' _UpperCAmelCase = 'imagenet-1k-id2label.json' _UpperCAmelCase = json.load(open(hf_hub_download(__lowerCAmelCase , __lowerCAmelCase , repo_type='dataset' ) , 'r' ) ) _UpperCAmelCase = {int(__lowerCAmelCase ): v for k, v in idalabel.items()} _UpperCAmelCase = idalabel _UpperCAmelCase = {v: k for k, v in idalabel.items()} # size of the architecture if model_name in ["dino_vits8", "dino_vits16"]: _UpperCAmelCase = 384 _UpperCAmelCase = 1_536 _UpperCAmelCase = 12 _UpperCAmelCase = 6 # load original model from torch hub _UpperCAmelCase = torch.hub.load('facebookresearch/dino:main' , __lowerCAmelCase ) original_model.eval() # load state_dict of original model, remove and rename some keys _UpperCAmelCase = original_model.state_dict() if base_model: remove_classification_head_(__lowerCAmelCase ) _UpperCAmelCase = create_rename_keys(__lowerCAmelCase , base_model=__lowerCAmelCase ) for src, dest in rename_keys: rename_key(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) read_in_q_k_v(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) # load HuggingFace model if base_model: _UpperCAmelCase = ViTModel(__lowerCAmelCase , add_pooling_layer=__lowerCAmelCase ).eval() else: _UpperCAmelCase = ViTForImageClassification(__lowerCAmelCase ).eval() model.load_state_dict(__lowerCAmelCase ) # Check outputs on an image, prepared by ViTImageProcessor _UpperCAmelCase = ViTImageProcessor() _UpperCAmelCase = image_processor(images=prepare_img() , return_tensors='pt' ) _UpperCAmelCase = encoding['pixel_values'] _UpperCAmelCase = model(__lowerCAmelCase ) if base_model: _UpperCAmelCase = original_model(__lowerCAmelCase ) assert torch.allclose(__lowerCAmelCase , outputs.last_hidden_state[:, 0, :] , atol=1E-1 ) else: _UpperCAmelCase = original_model(__lowerCAmelCase ) assert logits.shape == outputs.logits.shape assert torch.allclose(__lowerCAmelCase , outputs.logits , atol=1E-3 ) Path(__lowerCAmelCase ).mkdir(exist_ok=__lowerCAmelCase ) print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowerCAmelCase ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(__lowerCAmelCase ) if __name__ == "__main__": _a = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''dino_vitb16''', type=str, help='''Name of the model trained with DINO you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--base_model''', action='''store_true''', help='''Whether to only convert the base model (no projection head weights).''', ) parser.set_defaults(base_model=True) _a = parser.parse_args() convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)
39
from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase : List[str] = logging.get_logger(__name__) lowercase : Any = { """uclanlp/visualbert-vqa""": """https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json""", """uclanlp/visualbert-vqa-pre""": """https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json""", """uclanlp/visualbert-vqa-coco-pre""": ( """https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json""" ), """uclanlp/visualbert-vcr""": """https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json""", """uclanlp/visualbert-vcr-pre""": """https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json""", """uclanlp/visualbert-vcr-coco-pre""": ( """https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json""" ), """uclanlp/visualbert-nlvr2""": """https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json""", """uclanlp/visualbert-nlvr2-pre""": """https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json""", """uclanlp/visualbert-nlvr2-coco-pre""": ( """https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json""" ) # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert } class __snake_case ( lowerCAmelCase ): _a : Union[str, Any]= "visual_bert" def __init__( self ,snake_case=30522 ,snake_case=768 ,snake_case=512 ,snake_case=12 ,snake_case=12 ,snake_case=3072 ,snake_case="gelu" ,snake_case=0.1 ,snake_case=0.1 ,snake_case=512 ,snake_case=2 ,snake_case=0.02 ,snake_case=1e-12 ,snake_case=False ,snake_case=True ,snake_case=1 ,snake_case=0 ,snake_case=2 ,**snake_case ,): '''simple docstring''' super().__init__(pad_token_id=snake_case ,bos_token_id=snake_case ,eos_token_id=snake_case ,**snake_case ) lowercase : Tuple = vocab_size lowercase : int = max_position_embeddings lowercase : Optional[Any] = hidden_size lowercase : int = visual_embedding_dim lowercase : Tuple = num_hidden_layers lowercase : str = num_attention_heads lowercase : Optional[Any] = intermediate_size lowercase : str = hidden_act lowercase : Tuple = hidden_dropout_prob lowercase : List[Any] = attention_probs_dropout_prob lowercase : Union[str, Any] = initializer_range lowercase : int = type_vocab_size lowercase : Union[str, Any] = layer_norm_eps lowercase : Union[str, Any] = bypass_transformer lowercase : int = special_visual_initialize
20
0
"""simple docstring""" import argparse import torch from torch import nn from transformers import SpeechaTextConfig, SpeechaTextForConditionalGeneration def lowercase ( A_ )-> Optional[int]: '''simple docstring''' a : str = [ "encoder.version", "decoder.version", "model.encoder.version", "model.decoder.version", "decoder.output_projection.weight", "_float_tensor", "encoder.embed_positions._float_tensor", "decoder.embed_positions._float_tensor", ] for k in ignore_keys: state_dict.pop(A_ , A_ ) def lowercase ( A_ )-> Optional[Any]: '''simple docstring''' a : Tuple = list(s_dict.keys() ) for key in keys: if "transformer_layers" in key: a : Union[str, Any] = s_dict.pop(A_ ) elif "subsample" in key: a : Optional[Any] = s_dict.pop(A_ ) def lowercase ( A_ )-> Optional[int]: '''simple docstring''' a , a : int = emb.weight.shape a : List[str] = nn.Linear(A_ , A_ , bias=A_ ) a : List[Any] = emb.weight.data return lin_layer def lowercase ( A_ , A_ )-> Dict: '''simple docstring''' a : Optional[Any] = torch.load(A_ , map_location="cpu" ) a : Union[str, Any] = mam_aaa["args"] a : Tuple = mam_aaa["model"] a : Tuple = state_dict["decoder.output_projection.weight"] remove_ignore_keys_(A_ ) rename_keys(A_ ) a : str = state_dict["decoder.embed_tokens.weight"].shape[0] a : List[Any] = args.share_decoder_input_output_embed a : str = [int(A_ ) for i in args.conv_kernel_sizes.split("," )] a : Optional[int] = SpeechaTextConfig( vocab_size=A_ , max_source_positions=args.max_source_positions , max_target_positions=args.max_target_positions , encoder_layers=args.encoder_layers , decoder_layers=args.decoder_layers , encoder_attention_heads=args.encoder_attention_heads , decoder_attention_heads=args.decoder_attention_heads , encoder_ffn_dim=args.encoder_ffn_embed_dim , decoder_ffn_dim=args.decoder_ffn_embed_dim , d_model=args.encoder_embed_dim , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="relu" , num_conv_layers=len(A_ ) , conv_channels=args.conv_channels , conv_kernel_sizes=A_ , input_feat_per_channel=args.input_feat_per_channel , input_channels=args.input_channels , tie_word_embeddings=A_ , num_beams=5 , max_length=200 , use_cache=A_ , decoder_start_token_id=2 , early_stopping=A_ , ) a : Dict = SpeechaTextForConditionalGeneration(A_ ) a , a : List[Any] = model.model.load_state_dict(A_ , strict=A_ ) if len(A_ ) > 0 and not set(A_ ) <= { "encoder.embed_positions.weights", "decoder.embed_positions.weights", }: raise ValueError( "Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights` are allowed to be missing," F''' but all the following weights are missing {missing}''' ) if tie_embeds: a : Union[str, Any] = make_linear_from_emb(model.model.decoder.embed_tokens ) else: a : Union[str, Any] = lm_head_weights model.save_pretrained(A_ ) if __name__ == "__main__": __lowercase = argparse.ArgumentParser() # Required parameters parser.add_argument("""--fairseq_path""", type=str, help="""Path to the fairseq model (.pt) file.""") parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") __lowercase = parser.parse_args() convert_fairseq_sat_checkpoint_to_tfms(args.fairseq_path, args.pytorch_dump_folder_path)
40
import argparse import requests import torch from PIL import Image from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: if "cls_token" in name: lowercase : List[Any] = name.replace("""cls_token""" , """vit.embeddings.cls_token""" ) if "mask_token" in name: lowercase : Any = name.replace("""mask_token""" , """decoder.mask_token""" ) if "decoder_pos_embed" in name: lowercase : str = name.replace("""decoder_pos_embed""" , """decoder.decoder_pos_embed""" ) if "pos_embed" in name and "decoder" not in name: lowercase : List[str] = name.replace("""pos_embed""" , """vit.embeddings.position_embeddings""" ) if "patch_embed.proj" in name: lowercase : Tuple = name.replace("""patch_embed.proj""" , """vit.embeddings.patch_embeddings.projection""" ) if "patch_embed.norm" in name: lowercase : int = name.replace("""patch_embed.norm""" , """vit.embeddings.norm""" ) if "decoder_blocks" in name: lowercase : Tuple = name.replace("""decoder_blocks""" , """decoder.decoder_layers""" ) if "blocks" in name: lowercase : List[Any] = name.replace("""blocks""" , """vit.encoder.layer""" ) if "attn.proj" in name: lowercase : List[str] = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name: lowercase : Union[str, Any] = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: lowercase : Optional[Any] = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: lowercase : Union[str, Any] = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: lowercase : Dict = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: lowercase : Dict = name.replace("""mlp.fc2""" , """output.dense""" ) if "decoder_embed" in name: lowercase : List[str] = name.replace("""decoder_embed""" , """decoder.decoder_embed""" ) if "decoder_norm" in name: lowercase : Dict = name.replace("""decoder_norm""" , """decoder.decoder_norm""" ) if "decoder_pred" in name: lowercase : List[str] = name.replace("""decoder_pred""" , """decoder.decoder_pred""" ) if "norm.weight" in name and "decoder" not in name: lowercase : Tuple = name.replace("""norm.weight""" , """vit.layernorm.weight""" ) if "norm.bias" in name and "decoder" not in name: lowercase : int = name.replace("""norm.bias""" , """vit.layernorm.bias""" ) return name def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: for key in orig_state_dict.copy().keys(): lowercase : List[Any] = orig_state_dict.pop(SCREAMING_SNAKE_CASE__ ) if "qkv" in key: lowercase : int = key.split(""".""" ) lowercase : List[str] = int(key_split[1] ) if "decoder_blocks" in key: lowercase : Tuple = config.decoder_hidden_size lowercase : int = """decoder.decoder_layers.""" if "weight" in key: lowercase : List[Any] = val[:dim, :] lowercase : Tuple = val[dim : dim * 2, :] lowercase : List[Any] = val[-dim:, :] elif "bias" in key: lowercase : str = val[:dim] lowercase : Dict = val[dim : dim * 2] lowercase : Union[str, Any] = val[-dim:] else: lowercase : Tuple = config.hidden_size lowercase : Union[str, Any] = """vit.encoder.layer.""" if "weight" in key: lowercase : Tuple = val[:dim, :] lowercase : List[str] = val[dim : dim * 2, :] lowercase : Dict = val[-dim:, :] elif "bias" in key: lowercase : Any = val[:dim] lowercase : str = val[dim : dim * 2] lowercase : Union[str, Any] = val[-dim:] else: lowercase : Union[str, Any] = val return orig_state_dict def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> int: lowercase : int = ViTMAEConfig() if "large" in checkpoint_url: lowercase : Dict = 1_024 lowercase : str = 4_096 lowercase : Optional[Any] = 24 lowercase : Optional[Any] = 16 elif "huge" in checkpoint_url: lowercase : int = 14 lowercase : List[Any] = 1_280 lowercase : int = 5_120 lowercase : List[Any] = 32 lowercase : Any = 16 lowercase : List[str] = ViTMAEForPreTraining(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE__ , map_location="""cpu""" )["""model"""] lowercase : Tuple = ViTMAEImageProcessor(size=config.image_size ) lowercase : Optional[int] = convert_state_dict(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) model.load_state_dict(SCREAMING_SNAKE_CASE__ ) model.eval() lowercase : Union[str, Any] = """https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg""" lowercase : Union[str, Any] = Image.open(requests.get(SCREAMING_SNAKE_CASE__ , stream=SCREAMING_SNAKE_CASE__ ).raw ) lowercase : Optional[Any] = ViTMAEImageProcessor(size=config.image_size ) lowercase : List[Any] = image_processor(images=SCREAMING_SNAKE_CASE__ , return_tensors="""pt""" ) # forward pass torch.manual_seed(2 ) lowercase : int = model(**SCREAMING_SNAKE_CASE__ ) lowercase : str = outputs.logits if "large" in checkpoint_url: lowercase : List[Any] = torch.tensor( [[-0.7309, -0.7128, -1.0169], [-1.0161, -0.9058, -1.1878], [-1.0478, -0.9411, -1.1911]] ) elif "huge" in checkpoint_url: lowercase : Tuple = torch.tensor( [[-1.1599, -0.9199, -1.2221], [-1.1952, -0.9269, -1.2307], [-1.2143, -0.9337, -1.2262]] ) else: lowercase : List[str] = torch.tensor( [[-0.9192, -0.8481, -1.1259], [-1.1349, -1.0034, -1.2599], [-1.1757, -1.0429, -1.2726]] ) # verify logits assert torch.allclose(logits[0, :3, :3] , SCREAMING_SNAKE_CASE__ , atol=1e-4 ) print(f"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) print(f"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": lowercase : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--checkpoint_url""", default="""https://dl.fbaipublicfiles.com/mae/visualize/mae_visualize_vit_base.pth""", type=str, help="""URL of the checkpoint you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) lowercase : List[Any] = parser.parse_args() convert_vit_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
20
0
'''simple docstring''' import random from typing import Any def SCREAMING_SNAKE_CASE_ (UpperCamelCase ) -> list[Any]: for _ in range(len(UpperCamelCase ) ): lowerCamelCase__ : List[Any] = random.randint(0 , len(UpperCamelCase ) - 1 ) lowerCamelCase__ : Tuple = random.randint(0 , len(UpperCamelCase ) - 1 ) lowerCamelCase__ , lowerCamelCase__ : List[str] = data[b], data[a] return data if __name__ == "__main__": _A : int =[0, 1, 2, 3, 4, 5, 6, 7] _A : Optional[Any] =['''python''', '''says''', '''hello''', '''!'''] print('''Fisher-Yates Shuffle:''') print('''List''', integers, strings) print('''FY Shuffle''', fisher_yates_shuffle(integers), fisher_yates_shuffle(strings))
41
import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=0.999 , SCREAMING_SNAKE_CASE__="cosine" , ) -> List[Any]: if alpha_transform_type == "cosine": def alpha_bar_fn(SCREAMING_SNAKE_CASE__ ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(SCREAMING_SNAKE_CASE__ ): return math.exp(t * -12.0 ) else: raise ValueError(f"Unsupported alpha_tranform_type: {alpha_transform_type}" ) lowercase : int = [] for i in range(SCREAMING_SNAKE_CASE__ ): lowercase : Dict = i / num_diffusion_timesteps lowercase : Optional[int] = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(SCREAMING_SNAKE_CASE__ ) / alpha_bar_fn(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) ) return torch.tensor(SCREAMING_SNAKE_CASE__ , dtype=torch.floataa ) class __snake_case ( lowerCAmelCase , lowerCAmelCase ): _a : Tuple= [e.name for e in KarrasDiffusionSchedulers] _a : int= 2 @register_to_config def __init__( self ,snake_case = 1000 ,snake_case = 0.00_085 ,snake_case = 0.012 ,snake_case = "linear" ,snake_case = None ,snake_case = "epsilon" ,snake_case = False ,snake_case = False ,snake_case = 1.0 ,snake_case = "linspace" ,snake_case = 0 ,): '''simple docstring''' if trained_betas is not None: lowercase : List[str] = torch.tensor(snake_case ,dtype=torch.floataa ) elif beta_schedule == "linear": lowercase : Optional[Any] = torch.linspace(snake_case ,snake_case ,snake_case ,dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. lowercase : int = ( torch.linspace(beta_start**0.5 ,beta_end**0.5 ,snake_case ,dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule lowercase : Union[str, Any] = betas_for_alpha_bar(snake_case ,alpha_transform_type="""cosine""" ) elif beta_schedule == "exp": lowercase : int = betas_for_alpha_bar(snake_case ,alpha_transform_type="""exp""" ) else: raise NotImplementedError(f"{beta_schedule} does is not implemented for {self.__class__}" ) lowercase : Any = 1.0 - self.betas lowercase : Dict = torch.cumprod(self.alphas ,dim=0 ) # set all values self.set_timesteps(snake_case ,snake_case ,snake_case ) lowercase : Tuple = use_karras_sigmas def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ): '''simple docstring''' if schedule_timesteps is None: lowercase : Union[str, Any] = self.timesteps lowercase : Dict = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: lowercase : Dict = 1 if len(snake_case ) > 1 else 0 else: lowercase : Union[str, Any] = timestep.cpu().item() if torch.is_tensor(snake_case ) else timestep lowercase : str = self._index_counter[timestep_int] return indices[pos].item() @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Optional[Any] = self.index_for_timestep(snake_case ) lowercase : Dict = self.sigmas[step_index] lowercase : List[str] = sample / ((sigma**2 + 1) ** 0.5) return sample def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = None ,snake_case = None ,): '''simple docstring''' lowercase : Any = num_inference_steps lowercase : List[Any] = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": lowercase : Optional[int] = np.linspace(0 ,num_train_timesteps - 1 ,snake_case ,dtype=snake_case )[::-1].copy() elif self.config.timestep_spacing == "leading": lowercase : int = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowercase : List[str] = (np.arange(0 ,snake_case ) * step_ratio).round()[::-1].copy().astype(snake_case ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": lowercase : List[str] = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowercase : Optional[int] = (np.arange(snake_case ,0 ,-step_ratio )).round().copy().astype(snake_case ) timesteps -= 1 else: raise ValueError( f"{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'." ) lowercase : Optional[int] = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) lowercase : Dict = np.log(snake_case ) lowercase : Union[str, Any] = np.interp(snake_case ,np.arange(0 ,len(snake_case ) ) ,snake_case ) if self.config.use_karras_sigmas: lowercase : List[Any] = self._convert_to_karras(in_sigmas=snake_case ,num_inference_steps=self.num_inference_steps ) lowercase : Tuple = np.array([self._sigma_to_t(snake_case ,snake_case ) for sigma in sigmas] ) lowercase : Any = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) lowercase : List[Any] = torch.from_numpy(snake_case ).to(device=snake_case ) lowercase : List[Any] = torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2 ), sigmas[-1:]] ) lowercase : Dict = torch.from_numpy(snake_case ) lowercase : List[Any] = torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2 )] ) if str(snake_case ).startswith("""mps""" ): # mps does not support float64 lowercase : Any = timesteps.to(snake_case ,dtype=torch.floataa ) else: lowercase : str = timesteps.to(device=snake_case ) # empty dt and derivative lowercase : Union[str, Any] = None lowercase : Any = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter lowercase : str = defaultdict(snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : Optional[int] = np.log(snake_case ) # get distribution lowercase : Union[str, Any] = log_sigma - log_sigmas[:, np.newaxis] # get sigmas range lowercase : Optional[int] = np.cumsum((dists >= 0) ,axis=0 ).argmax(axis=0 ).clip(max=log_sigmas.shape[0] - 2 ) lowercase : Any = low_idx + 1 lowercase : str = log_sigmas[low_idx] lowercase : Dict = log_sigmas[high_idx] # interpolate sigmas lowercase : int = (low - log_sigma) / (low - high) lowercase : Dict = np.clip(snake_case ,0 ,1 ) # transform interpolation to time range lowercase : Optional[Any] = (1 - w) * low_idx + w * high_idx lowercase : Tuple = t.reshape(sigma.shape ) return t def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : float = in_sigmas[-1].item() lowercase : float = in_sigmas[0].item() lowercase : Dict = 7.0 # 7.0 is the value used in the paper lowercase : Optional[int] = np.linspace(0 ,1 ,snake_case ) lowercase : int = sigma_min ** (1 / rho) lowercase : Any = sigma_max ** (1 / rho) lowercase : Tuple = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho return sigmas @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return self.dt is None def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case = True ,): '''simple docstring''' lowercase : Union[str, Any] = self.index_for_timestep(snake_case ) # advance index counter by 1 lowercase : Optional[int] = timestep.cpu().item() if torch.is_tensor(snake_case ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: lowercase : str = self.sigmas[step_index] lowercase : Optional[int] = self.sigmas[step_index + 1] else: # 2nd order / Heun's method lowercase : Dict = self.sigmas[step_index - 1] lowercase : Optional[Any] = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API lowercase : Union[str, Any] = 0 lowercase : Any = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": lowercase : Any = sigma_hat if self.state_in_first_order else sigma_next lowercase : int = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": lowercase : Union[str, Any] = sigma_hat if self.state_in_first_order else sigma_next lowercase : Optional[Any] = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": lowercase : Optional[Any] = model_output else: raise ValueError( f"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`" ) if self.config.clip_sample: lowercase : str = pred_original_sample.clamp( -self.config.clip_sample_range ,self.config.clip_sample_range ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order lowercase : Optional[int] = (sample - pred_original_sample) / sigma_hat # 3. delta timestep lowercase : Union[str, Any] = sigma_next - sigma_hat # store for 2nd order step lowercase : Optional[int] = derivative lowercase : Union[str, Any] = dt lowercase : Union[str, Any] = sample else: # 2. 2nd order / Heun's method lowercase : Tuple = (sample - pred_original_sample) / sigma_next lowercase : Dict = (self.prev_derivative + derivative) / 2 # 3. take prev timestep & sample lowercase : Tuple = self.dt lowercase : Optional[Any] = self.sample # free dt and derivative # Note, this puts the scheduler in "first order mode" lowercase : List[str] = None lowercase : Tuple = None lowercase : Dict = None lowercase : List[Any] = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Optional[int] = self.sigmas.to(device=original_samples.device ,dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(snake_case ): # mps does not support float64 lowercase : List[Any] = self.timesteps.to(original_samples.device ,dtype=torch.floataa ) lowercase : List[str] = timesteps.to(original_samples.device ,dtype=torch.floataa ) else: lowercase : List[str] = self.timesteps.to(original_samples.device ) lowercase : Tuple = timesteps.to(original_samples.device ) lowercase : Any = [self.index_for_timestep(snake_case ,snake_case ) for t in timesteps] lowercase : int = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): lowercase : Any = sigma.unsqueeze(-1 ) lowercase : Optional[int] = original_samples + noise * sigma return noisy_samples def __len__( self ): '''simple docstring''' return self.config.num_train_timesteps
20
0
'''simple docstring''' from __future__ import annotations import string from itertools import cycle, product from pathlib import Path lowercase : str = ( string.ascii_letters + string.digits + string.punctuation + string.whitespace ) lowercase : list[int] = [ord(letter) for letter in string.ascii_lowercase] lowercase : set[int] = {ord(char) for char in VALID_CHARS} lowercase : list[str] = ["the", "be", "to", "of", "and", "in", "that", "have"] def SCREAMING_SNAKE_CASE__ ( __A , __A ) -> str | None: _snake_case = "" _snake_case = 42 _snake_case = 42 _snake_case = 42 for keychar, cipherchar in zip(cycle(__A ) , __A ): _snake_case = cipherchar ^ keychar if decodedchar not in VALID_INTS: return None decoded += chr(__A ) return decoded def SCREAMING_SNAKE_CASE__ ( __A ) -> list[str]: _snake_case = [] for key in product(__A , repeat=3 ): _snake_case = try_key(__A , __A ) if encoded is not None: possibles.append(__A ) return possibles def SCREAMING_SNAKE_CASE__ ( __A , __A ) -> list[str]: return [possible for possible in possibles if common_word in possible.lower()] def SCREAMING_SNAKE_CASE__ ( __A = "p059_cipher.txt" ) -> int: _snake_case = 42 _snake_case = 42 _snake_case = 42 _snake_case = 42 _snake_case = Path(__A ).parent.joinpath(__A ).read_text(encoding='utf-8' ) _snake_case = [int(__A ) for number in data.strip().split(',' )] _snake_case = filter_valid_chars(__A ) for common_word in COMMON_WORDS: _snake_case = filter_common_word(__A , __A ) if len(__A ) == 1: break _snake_case = possibles[0] return sum(ord(__A ) for char in decoded_text ) if __name__ == "__main__": print(F'''{solution() = }''')
42
from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch lowercase : List[str] = logging.get_logger(__name__) @add_end_docstrings( lowerCAmelCase , R"\n top_k (`int`, defaults to 5):\n The number of predictions to return.\n targets (`str` or `List[str]`, *optional*):\n When passed, the model will limit the scores to the passed targets instead of looking up in the whole\n vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting\n token will be used (with a warning, and that might be slower).\n\n " , ) class __snake_case ( lowerCAmelCase ): def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' if self.framework == "tf": lowercase : str = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": lowercase : Optional[int] = torch.nonzero(input_ids == self.tokenizer.mask_token_id ,as_tuple=snake_case ) else: raise ValueError("""Unsupported framework""" ) return masked_index def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Tuple = self.get_masked_index(snake_case ) lowercase : Dict = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( """fill-mask""" ,self.model.base_model_prefix ,f"No mask_token ({self.tokenizer.mask_token}) found on the input" ,) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' if isinstance(snake_case ,snake_case ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input["""input_ids"""][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ,**snake_case ): '''simple docstring''' if return_tensors is None: lowercase : int = self.framework lowercase : Optional[Any] = self.tokenizer(snake_case ,return_tensors=snake_case ) self.ensure_exactly_one_mask_token(snake_case ) return model_inputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Optional[int] = self.model(**snake_case ) lowercase : Tuple = model_inputs["""input_ids"""] return model_outputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=5 ,snake_case=None ): '''simple docstring''' if target_ids is not None and target_ids.shape[0] < top_k: lowercase : str = target_ids.shape[0] lowercase : Optional[Any] = model_outputs["""input_ids"""][0] lowercase : List[str] = model_outputs["""logits"""] if self.framework == "tf": lowercase : List[str] = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] lowercase : Tuple = outputs.numpy() lowercase : Tuple = outputs[0, masked_index, :] lowercase : Any = stable_softmax(snake_case ,axis=-1 ) if target_ids is not None: lowercase : Union[str, Any] = tf.gather_nd(tf.squeeze(snake_case ,0 ) ,target_ids.reshape(-1 ,1 ) ) lowercase : int = tf.expand_dims(snake_case ,0 ) lowercase : Tuple = tf.math.top_k(snake_case ,k=snake_case ) lowercase , lowercase : int = topk.values.numpy(), topk.indices.numpy() else: lowercase : Optional[Any] = torch.nonzero(input_ids == self.tokenizer.mask_token_id ,as_tuple=snake_case ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample lowercase : Union[str, Any] = outputs[0, masked_index, :] lowercase : Tuple = logits.softmax(dim=-1 ) if target_ids is not None: lowercase : List[str] = probs[..., target_ids] lowercase , lowercase : Union[str, Any] = probs.topk(snake_case ) lowercase : Any = [] lowercase : List[Any] = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() ,predictions.tolist() ) ): lowercase : Dict = [] for v, p in zip(_values ,_predictions ): # Copy is important since we're going to modify this array in place lowercase : Dict = input_ids.numpy().copy() if target_ids is not None: lowercase : Union[str, Any] = target_ids[p].tolist() lowercase : Tuple = p # Filter padding out: lowercase : List[str] = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back lowercase : Tuple = self.tokenizer.decode(snake_case ,skip_special_tokens=snake_case ) lowercase : Optional[Any] = {"""score""": v, """token""": p, """token_str""": self.tokenizer.decode([p] ), """sequence""": sequence} row.append(snake_case ) result.append(snake_case ) if single_mask: return result[0] return result def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ): '''simple docstring''' if isinstance(snake_case ,snake_case ): lowercase : List[Any] = [targets] try: lowercase : List[str] = self.tokenizer.get_vocab() except Exception: lowercase : Any = {} lowercase : Dict = [] for target in targets: lowercase : Dict = vocab.get(snake_case ,snake_case ) if id_ is None: lowercase : Optional[int] = self.tokenizer( snake_case ,add_special_tokens=snake_case ,return_attention_mask=snake_case ,return_token_type_ids=snake_case ,max_length=1 ,truncation=snake_case ,)["""input_ids"""] if len(snake_case ) == 0: logger.warning( f"The specified target token `{target}` does not exist in the model vocabulary. " """We cannot replace it with anything meaningful, ignoring it""" ) continue lowercase : Union[str, Any] = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( f"The specified target token `{target}` does not exist in the model vocabulary. " f"Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`." ) target_ids.append(id_ ) lowercase : Optional[Any] = list(set(snake_case ) ) if len(snake_case ) == 0: raise ValueError("""At least one target must be provided when passed.""" ) lowercase : Optional[Any] = np.array(snake_case ) return target_ids def _SCREAMING_SNAKE_CASE ( self ,snake_case=None ,snake_case=None ): '''simple docstring''' lowercase : Dict = {} if targets is not None: lowercase : str = self.get_target_ids(snake_case ,snake_case ) lowercase : List[Any] = target_ids if top_k is not None: lowercase : List[str] = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( """fill-mask""" ,self.model.base_model_prefix ,"""The tokenizer does not define a `mask_token`.""" ) return {}, {}, postprocess_params def __call__( self ,snake_case ,*snake_case ,**snake_case ): '''simple docstring''' lowercase : Tuple = super().__call__(snake_case ,**snake_case ) if isinstance(snake_case ,snake_case ) and len(snake_case ) == 1: return outputs[0] return outputs
20
0
import argparse import random import joblib import numpy as np import torch from igf.igf import ( SecondaryLearner, collect_objective_set, compute_perplexity, generate_datasets, load_gpta, recopy_gpta, set_seed, train_secondary_learner, ) from torch.utils.data import DataLoader, RandomSampler from transformers import GPTaLMHeadModel def lowerCamelCase ( SCREAMING_SNAKE_CASE=32 , SCREAMING_SNAKE_CASE=10 , SCREAMING_SNAKE_CASE=100 , SCREAMING_SNAKE_CASE=1_026 , SCREAMING_SNAKE_CASE=True , SCREAMING_SNAKE_CASE="data/tokenized_stories_train_wikitext103.jbl" , SCREAMING_SNAKE_CASE="igf_context_pairs.jbl" , ): '''simple docstring''' set_seed(3 ) # generate train_data and objective_set __UpperCamelCase , __UpperCamelCase :Optional[Any] = generate_datasets( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , number=SCREAMING_SNAKE_CASE , min_len=1_026 , trim=SCREAMING_SNAKE_CASE ) # keeps model same across runs set_seed(4 ) # model, lm_optimizer, lm_scheduler = recopy_gpt2(model, device, max_steps) # store original model weights # can we train on GPU? __UpperCamelCase :List[Any] = torch.device('''cuda:0''' if torch.cuda.is_available() else '''cpu''' ) # load pretrained model __UpperCamelCase :str = load_gpta('''gpt2''' ).to(SCREAMING_SNAKE_CASE ) print('''computing perplexity on objective set''' ) __UpperCamelCase :List[str] = compute_perplexity(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ).item() print('''perplexity on objective set:''' , SCREAMING_SNAKE_CASE ) # collect igf pairs and save to file demo.jbl collect_objective_set(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # clean up, delete model and data we don't need anymore del model, train_data, objective_set torch.cuda.empty_cache() def lowerCamelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=15 , SCREAMING_SNAKE_CASE=128 , SCREAMING_SNAKE_CASE=100 , SCREAMING_SNAKE_CASE="igf_model.pt" , ): '''simple docstring''' set_seed(42 ) # Load pre-trained model __UpperCamelCase :str = GPTaLMHeadModel.from_pretrained('''gpt2''' ) # Initialize secondary learner to use embedding weights of model __UpperCamelCase :List[str] = SecondaryLearner(SCREAMING_SNAKE_CASE ) # Train secondary learner __UpperCamelCase :Tuple = train_secondary_learner( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , max_epochs=SCREAMING_SNAKE_CASE , batch_size=SCREAMING_SNAKE_CASE , eval_freq=100 , igf_model_path=SCREAMING_SNAKE_CASE , ) del model, secondary_learner_train_data torch.cuda.empty_cache() return secondary_learner def lowerCamelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=32 , SCREAMING_SNAKE_CASE=1_000 , SCREAMING_SNAKE_CASE=16 , SCREAMING_SNAKE_CASE=1.0 , SCREAMING_SNAKE_CASE=recopy_gpta , SCREAMING_SNAKE_CASE=None , SCREAMING_SNAKE_CASE=10 , SCREAMING_SNAKE_CASE="gpt2_finetuned.pt" , ): '''simple docstring''' __UpperCamelCase :List[Any] = torch.device('''cuda:0''' if torch.cuda.is_available() else '''cpu''' ) __UpperCamelCase :Tuple = RandomSampler(SCREAMING_SNAKE_CASE ) __UpperCamelCase :Union[str, Any] = DataLoader(SCREAMING_SNAKE_CASE , sampler=SCREAMING_SNAKE_CASE ) __UpperCamelCase :List[Any] = max_steps // (len(SCREAMING_SNAKE_CASE )) + 1 __UpperCamelCase :Optional[int] = 0 __UpperCamelCase :int = torch.zeros((1, context_len) , dtype=torch.long , device=SCREAMING_SNAKE_CASE ) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase :List[str] = recopy_model(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) model.train() if secondary_learner is not None: secondary_learner.to(SCREAMING_SNAKE_CASE ) secondary_learner.eval() __UpperCamelCase :List[str] = [] __UpperCamelCase :str = 0 __UpperCamelCase :int = [] __UpperCamelCase :int = [] # Compute the performance of the transformer model at the beginning __UpperCamelCase :List[str] = compute_perplexity(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) test_perps.append(SCREAMING_SNAKE_CASE ) print('''Test perplexity, step''' , SCREAMING_SNAKE_CASE , ''':''' , SCREAMING_SNAKE_CASE ) for epoch in range(int(SCREAMING_SNAKE_CASE ) ): for step, example in enumerate(SCREAMING_SNAKE_CASE ): torch.cuda.empty_cache() __UpperCamelCase :Optional[Any] = random.randint(0 , example.size(2 ) - context_len - 1 ) __UpperCamelCase :Tuple = example[0, 0, start : start + context_len] lm_optimizer.zero_grad() __UpperCamelCase :List[str] = model(SCREAMING_SNAKE_CASE , labels=SCREAMING_SNAKE_CASE ) __UpperCamelCase :Any = True if secondary_learner is not None: __UpperCamelCase :List[Any] = secondary_learner.forward( torch.tensor(SCREAMING_SNAKE_CASE , dtype=torch.long , device=SCREAMING_SNAKE_CASE ).unsqueeze(0 ) )[0].item() observed_qs.append(float(SCREAMING_SNAKE_CASE ) ) # Here we implement the simple non-constant threshold for the predicted IG(X) value # We will decay the selectivity of our secondary learner filter from # 1 standard deviation above average to 1 below average after 10 batches. if global_step == 10: __UpperCamelCase :List[Any] = -1 if predicted_q < threshold: __UpperCamelCase :List[str] = False # If we passed the filter, add the context to the batch! if do_backprop: contexts.append(np.array(context.cpu() ) ) __UpperCamelCase :int = outputs[0] lm_loss.backward() examples += 1 del outputs # Once the batch is filled with enough contexts, backprop on the batch. if examples == batch_size: torch.cuda.empty_cache() __UpperCamelCase :Any = 0 # Do LM backprop torch.nn.utils.clip_grad_norm_(model.parameters() , 3.0 ) lm_optimizer.step() lm_scheduler.step() # Update learning rate schedule global_step += 1 # Compute the performance of the transformer model at this batch if global_step % eval_interval == 0: __UpperCamelCase :Tuple = compute_perplexity(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) test_perps.append(SCREAMING_SNAKE_CASE ) print('''Test perplexity, step''' , SCREAMING_SNAKE_CASE , ''':''' , SCREAMING_SNAKE_CASE ) # Break out of the loop after 60 batches if max_steps > 0 and global_step > 60: break if max_steps > 0 and global_step > 60: break # save finetuned transformer model torch.save(model.state_dict() , SCREAMING_SNAKE_CASE ) torch.cuda.empty_cache() # Do some cleaning up so we can reinitialize for the next run of this function del lm_optimizer del lm_scheduler return model def lowerCamelCase ( ): '''simple docstring''' __UpperCamelCase :List[str] = argparse.ArgumentParser(description='''Fine-tune a transformer model with IGF on a language modeling task''' ) # Required parameters parser.add_argument( '''--data_dir''' , default=SCREAMING_SNAKE_CASE , type=SCREAMING_SNAKE_CASE , required=SCREAMING_SNAKE_CASE , help='''The input data dir. Should contain data files for WikiText.''' , ) parser.add_argument( '''--model_name_or_path''' , default=SCREAMING_SNAKE_CASE , type=SCREAMING_SNAKE_CASE , required=SCREAMING_SNAKE_CASE , help='''Path to pretrained model or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--data_file''' , type=SCREAMING_SNAKE_CASE , default=SCREAMING_SNAKE_CASE , help=( '''A jbl file containing tokenized data which can be split as objective dataset, ''' '''train_dataset and test_dataset.''' ) , ) parser.add_argument( '''--igf_data_file''' , type=SCREAMING_SNAKE_CASE , default=SCREAMING_SNAKE_CASE , help='''A jbl file containing the context and information gain pairs to train secondary learner.''' , ) parser.add_argument( '''--output_dir''' , default=SCREAMING_SNAKE_CASE , type=SCREAMING_SNAKE_CASE , required=SCREAMING_SNAKE_CASE , help='''The output directory where the final fine-tuned model is stored.''' , ) parser.add_argument( '''--tokenizer_name''' , default=SCREAMING_SNAKE_CASE , type=SCREAMING_SNAKE_CASE , help='''Pretrained tokenizer name or path if not the same as model_name''' , ) parser.add_argument('''--seed''' , type=SCREAMING_SNAKE_CASE , default=SCREAMING_SNAKE_CASE , help='''A seed for reproducible training.''' ) parser.add_argument( '''--context_len''' , default=32 , type=SCREAMING_SNAKE_CASE , help=( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) , ) parser.add_argument( '''--size_objective_set''' , default=100 , type=SCREAMING_SNAKE_CASE , help='''number of articles that are long enough to be used as our objective set''' , ) parser.add_argument( '''--eval_freq''' , default=100 , type=SCREAMING_SNAKE_CASE , help='''secondary model evaluation is triggered at eval_freq''' ) parser.add_argument('''--max_steps''' , default=1_000 , type=SCREAMING_SNAKE_CASE , help='''To calculate training epochs''' ) parser.add_argument( '''--secondary_learner_batch_size''' , default=128 , type=SCREAMING_SNAKE_CASE , help='''batch size of training data for secondary learner''' , ) parser.add_argument( '''--batch_size''' , default=16 , type=SCREAMING_SNAKE_CASE , help='''batch size of training data of language model(gpt2) ''' ) parser.add_argument( '''--eval_interval''' , default=10 , type=SCREAMING_SNAKE_CASE , help=( '''decay the selectivity of our secondary learner filter from''' '''1 standard deviation above average to 1 below average after 10 batches''' ) , ) parser.add_argument( '''--number''' , default=100 , type=SCREAMING_SNAKE_CASE , help='''The number of examples split to be used as objective_set/test_data''' ) parser.add_argument( '''--min_len''' , default=1_026 , type=SCREAMING_SNAKE_CASE , help='''The minimum length of the article to be used as objective set''' ) parser.add_argument( '''--secondary_learner_max_epochs''' , default=15 , type=SCREAMING_SNAKE_CASE , help='''number of epochs to train secondary learner''' ) parser.add_argument('''--trim''' , default=SCREAMING_SNAKE_CASE , type=SCREAMING_SNAKE_CASE , help='''truncate the example if it exceeds context length''' ) parser.add_argument( '''--threshold''' , default=1.0 , type=SCREAMING_SNAKE_CASE , help=( '''The threshold value used by secondary learner to filter the train_data and allow only''' ''' informative data as input to the model''' ) , ) parser.add_argument('''--finetuned_model_name''' , default='''gpt2_finetuned.pt''' , type=SCREAMING_SNAKE_CASE , help='''finetuned_model_name''' ) parser.add_argument( '''--recopy_model''' , default=SCREAMING_SNAKE_CASE , type=SCREAMING_SNAKE_CASE , help='''Reset the model to the original pretrained GPT-2 weights after each iteration''' , ) # function calls # Collecting *n* pairs of context and information gain(X, IG(X)) for training the secondary learner generate_n_pairs( context_len=32 , max_steps=10 , size_objective_set=100 , min_len=1_026 , trim=SCREAMING_SNAKE_CASE , data_file='''data/tokenized_stories_train_wikitext103.jbl''' , igf_data_file='''igf_context_pairs.jbl''' , ) # Load train data for secondary learner __UpperCamelCase :Optional[Any] = joblib.load('''data/IGF_values.jbl''' ) # Train secondary learner __UpperCamelCase :str = training_secondary_learner( SCREAMING_SNAKE_CASE , secondary_learner_max_epochs=15 , secondary_learner_batch_size=128 , eval_freq=100 , igf_model_path='''igf_model.pt''' , ) # load pretrained gpt2 model __UpperCamelCase :Union[str, Any] = GPTaLMHeadModel.from_pretrained('''gpt2''' ) set_seed(42 ) # Generate train and test data to train and evaluate gpt2 model __UpperCamelCase , __UpperCamelCase :Dict = generate_datasets( context_len=32 , file='''data/tokenized_stories_train_wikitext103.jbl''' , number=100 , min_len=1_026 , trim=SCREAMING_SNAKE_CASE ) # fine-tuning of the gpt2 model using igf (Information Gain Filtration) finetune( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , context_len=32 , max_steps=1_000 , batch_size=16 , threshold=1.0 , recopy_model=SCREAMING_SNAKE_CASE , secondary_learner=SCREAMING_SNAKE_CASE , eval_interval=10 , finetuned_model_name='''gpt2_finetuned.pt''' , ) if __name__ == "__main__": main()
43
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class __snake_case ( unittest.TestCase ): def __init__( self ,snake_case ,snake_case=7 ,snake_case=3 ,snake_case=18 ,snake_case=30 ,snake_case=400 ,snake_case=True ,snake_case=None ,snake_case=True ,snake_case=None ,): '''simple docstring''' lowercase : Dict = size if size is not None else {"""shortest_edge""": 20} lowercase : Union[str, Any] = crop_size if crop_size is not None else {"""height""": 18, """width""": 18} lowercase : str = parent lowercase : int = batch_size lowercase : str = num_channels lowercase : int = image_size lowercase : List[str] = min_resolution lowercase : str = max_resolution lowercase : Dict = do_resize lowercase : Dict = size lowercase : Dict = do_center_crop lowercase : str = crop_size def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, } @require_torch @require_vision class __snake_case ( lowerCAmelCase , unittest.TestCase ): _a : Any= MobileNetVaImageProcessor if is_vision_available() else None def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = MobileNetVaImageProcessingTester(self ) @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[int] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case ,"""do_resize""" ) ) self.assertTrue(hasattr(snake_case ,"""size""" ) ) self.assertTrue(hasattr(snake_case ,"""do_center_crop""" ) ) self.assertTrue(hasattr(snake_case ,"""crop_size""" ) ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : int = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{"""shortest_edge""": 20} ) self.assertEqual(image_processor.crop_size ,{"""height""": 18, """width""": 18} ) lowercase : int = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ,crop_size=84 ) self.assertEqual(image_processor.size ,{"""shortest_edge""": 42} ) self.assertEqual(image_processor.crop_size ,{"""height""": 84, """width""": 84} ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' pass def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowercase : str = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case ,Image.Image ) # Test not batched input lowercase : Dict = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) # Test batched lowercase : Tuple = image_processing(snake_case ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowercase : Union[str, Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case ,numpify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case ,np.ndarray ) # Test not batched input lowercase : Optional[Any] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) # Test batched lowercase : List[str] = image_processing(snake_case ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowercase : Tuple = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case ,torchify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case ,torch.Tensor ) # Test not batched input lowercase : Optional[int] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) # Test batched lowercase : List[str] = image_processing(snake_case ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,)
20
0
"""simple docstring""" from argparse import ArgumentParser from . import BaseTransformersCLICommand def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ) -> int: return DownloadCommand(args.model ,args.cache_dir ,args.force ,args.trust_remote_code ) class __A ( SCREAMING_SNAKE_CASE_ ): @staticmethod def __A ( a__ ): _lowerCAmelCase : Optional[int] = parser.add_parser("""download""" ) download_parser.add_argument( """--cache-dir""" , type=a__ , default=a__ , help="""Path to location to store the models""" ) download_parser.add_argument( """--force""" , action="""store_true""" , help="""Force the model to be download even if already in cache-dir""" ) download_parser.add_argument( """--trust-remote-code""" , action="""store_true""" , help="""Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you've reviewed the code as it will execute on your local machine""" , ) download_parser.add_argument("""model""" , type=a__ , help="""Name of the model to download""" ) download_parser.set_defaults(func=a__ ) def __init__( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : str = model _lowerCAmelCase : List[Any] = cache _lowerCAmelCase : Tuple = force _lowerCAmelCase : List[Any] = trust_remote_code def __A ( self ): from ..models.auto import AutoModel, AutoTokenizer AutoModel.from_pretrained( self._model , cache_dir=self._cache , force_download=self._force , trust_remote_code=self._trust_remote_code ) AutoTokenizer.from_pretrained( self._model , cache_dir=self._cache , force_download=self._force , trust_remote_code=self._trust_remote_code )
44
from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef import datasets lowercase : str = """\ @inproceedings{wang2019glue, title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding}, author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.}, note={In the Proceedings of ICLR.}, year={2019} } """ lowercase : Dict = """\ GLUE, the General Language Understanding Evaluation benchmark (https://gluebenchmark.com/) is a collection of resources for training, evaluating, and analyzing natural language understanding systems. """ lowercase : int = """ Compute GLUE evaluation metric associated to each GLUE dataset. Args: predictions: list of predictions to score. Each translation should be tokenized into a list of tokens. references: list of lists of references for each translation. Each reference should be tokenized into a list of tokens. Returns: depending on the GLUE subset, one or several of: \"accuracy\": Accuracy \"f1\": F1 score \"pearson\": Pearson Correlation \"spearmanr\": Spearman Correlation \"matthews_correlation\": Matthew Correlation Examples: >>> glue_metric = datasets.load_metric('glue', 'sst2') # 'sst2' or any of [\"mnli\", \"mnli_mismatched\", \"mnli_matched\", \"qnli\", \"rte\", \"wnli\", \"hans\"] >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0} >>> glue_metric = datasets.load_metric('glue', 'mrpc') # 'mrpc' or 'qqp' >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0, 'f1': 1.0} >>> glue_metric = datasets.load_metric('glue', 'stsb') >>> references = [0., 1., 2., 3., 4., 5.] >>> predictions = [0., 1., 2., 3., 4., 5.] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print({\"pearson\": round(results[\"pearson\"], 2), \"spearmanr\": round(results[\"spearmanr\"], 2)}) {'pearson': 1.0, 'spearmanr': 1.0} >>> glue_metric = datasets.load_metric('glue', 'cola') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'matthews_correlation': 1.0} """ def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[int]: return float((preds == labels).mean() ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: lowercase : Any = simple_accuracy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = float(fa_score(y_true=SCREAMING_SNAKE_CASE__ , y_pred=SCREAMING_SNAKE_CASE__ ) ) return { "accuracy": acc, "f1": fa, } def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: lowercase : Union[str, Any] = float(pearsonr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )[0] ) lowercase : Dict = float(spearmanr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )[0] ) return { "pearson": pearson_corr, "spearmanr": spearman_corr, } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __snake_case ( datasets.Metric ): def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self.config_name not in [ "sst2", "mnli", "mnli_mismatched", "mnli_matched", "cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans", ]: raise KeyError( """You should supply a configuration name selected in """ """[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", """ """\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]""" ) return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { """predictions""": datasets.Value("""int64""" if self.config_name != """stsb""" else """float32""" ), """references""": datasets.Value("""int64""" if self.config_name != """stsb""" else """float32""" ), } ) ,codebase_urls=[] ,reference_urls=[] ,format="""numpy""" ,) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' if self.config_name == "cola": return {"matthews_correlation": matthews_corrcoef(snake_case ,snake_case )} elif self.config_name == "stsb": return pearson_and_spearman(snake_case ,snake_case ) elif self.config_name in ["mrpc", "qqp"]: return acc_and_fa(snake_case ,snake_case ) elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]: return {"accuracy": simple_accuracy(snake_case ,snake_case )} else: raise KeyError( """You should supply a configuration name selected in """ """[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", """ """\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]""" )
20
0
"""simple docstring""" import unittest from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __lowerCAmelCase : '''simple docstring''' @staticmethod def __UpperCAmelCase ( *_a , **_a ): pass @is_pipeline_test @require_vision @require_torch class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : List[str] = MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING def __UpperCAmelCase ( self , _a , _a , _a ): __a = pipeline( '''zero-shot-object-detection''' , model='''hf-internal-testing/tiny-random-owlvit-object-detection''' ) __a = [ { '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''', '''candidate_labels''': ['''cat''', '''remote''', '''couch'''], } ] return object_detector, examples def __UpperCAmelCase ( self , _a , _a ): __a = object_detector(examples[0] , threshold=0.0 ) __a = len(_a ) self.assertGreater(_a , 0 ) self.assertEqual( _a , [ { '''score''': ANY(_a ), '''label''': ANY(_a ), '''box''': {'''xmin''': ANY(_a ), '''ymin''': ANY(_a ), '''xmax''': ANY(_a ), '''ymax''': ANY(_a )}, } for i in range(_a ) ] , ) @require_tf @unittest.skip('''Zero Shot Object Detection not implemented in TF''' ) def __UpperCAmelCase ( self ): pass @require_torch def __UpperCAmelCase ( self ): __a = pipeline( '''zero-shot-object-detection''' , model='''hf-internal-testing/tiny-random-owlvit-object-detection''' ) __a = object_detector( '''./tests/fixtures/tests_samples/COCO/000000039769.png''' , candidate_labels=['''cat''', '''remote''', '''couch'''] , threshold=0.64 , ) self.assertEqual( nested_simplify(_a , decimals=4 ) , [ {'''score''': 0.7235, '''label''': '''cat''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}}, {'''score''': 0.7218, '''label''': '''remote''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}}, {'''score''': 0.7184, '''label''': '''couch''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}}, {'''score''': 0.6748, '''label''': '''remote''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}}, {'''score''': 0.6656, '''label''': '''cat''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}}, {'''score''': 0.6614, '''label''': '''couch''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}}, {'''score''': 0.6456, '''label''': '''remote''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}}, {'''score''': 0.642, '''label''': '''remote''', '''box''': {'''xmin''': 67, '''ymin''': 274, '''xmax''': 93, '''ymax''': 297}}, {'''score''': 0.6419, '''label''': '''cat''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}}, ] , ) __a = object_detector( [ { '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''', '''candidate_labels''': ['''cat''', '''remote''', '''couch'''], } ] , threshold=0.64 , ) self.assertEqual( nested_simplify(_a , decimals=4 ) , [ [ {'''score''': 0.7235, '''label''': '''cat''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}}, {'''score''': 0.7218, '''label''': '''remote''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}}, {'''score''': 0.7184, '''label''': '''couch''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}}, {'''score''': 0.6748, '''label''': '''remote''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}}, {'''score''': 0.6656, '''label''': '''cat''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}}, {'''score''': 0.6614, '''label''': '''couch''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}}, {'''score''': 0.6456, '''label''': '''remote''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}}, {'''score''': 0.642, '''label''': '''remote''', '''box''': {'''xmin''': 67, '''ymin''': 274, '''xmax''': 93, '''ymax''': 297}}, {'''score''': 0.6419, '''label''': '''cat''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}}, ] ] , ) @require_torch @slow def __UpperCAmelCase ( self ): __a = pipeline('''zero-shot-object-detection''' ) __a = object_detector( '''http://images.cocodataset.org/val2017/000000039769.jpg''' , candidate_labels=['''cat''', '''remote''', '''couch'''] , ) self.assertEqual( nested_simplify(_a , decimals=4 ) , [ {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}}, {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}}, {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}}, {'''score''': 0.1474, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}}, {'''score''': 0.1208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}}, ] , ) __a = object_detector( [ { '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''candidate_labels''': ['''cat''', '''remote''', '''couch'''], }, { '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''candidate_labels''': ['''cat''', '''remote''', '''couch'''], }, ] , ) self.assertEqual( nested_simplify(_a , decimals=4 ) , [ [ {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}}, {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}}, {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}}, {'''score''': 0.1474, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}}, {'''score''': 0.1208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}}, ], [ {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}}, {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}}, {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}}, {'''score''': 0.1474, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}}, {'''score''': 0.1208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}}, ], ] , ) @require_tf @unittest.skip('''Zero Shot Object Detection not implemented in TF''' ) def __UpperCAmelCase ( self ): pass @require_torch @slow def __UpperCAmelCase ( self ): __a = 0.2 __a = pipeline('''zero-shot-object-detection''' ) __a = object_detector( '''http://images.cocodataset.org/val2017/000000039769.jpg''' , candidate_labels=['''cat''', '''remote''', '''couch'''] , threshold=_a , ) self.assertEqual( nested_simplify(_a , decimals=4 ) , [ {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}}, {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}}, {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}}, ] , ) @require_torch @slow def __UpperCAmelCase ( self ): __a = 2 __a = pipeline('''zero-shot-object-detection''' ) __a = object_detector( '''http://images.cocodataset.org/val2017/000000039769.jpg''' , candidate_labels=['''cat''', '''remote''', '''couch'''] , top_k=_a , ) self.assertEqual( nested_simplify(_a , decimals=4 ) , [ {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}}, {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}}, ] , )
45
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 __snake_case ( unittest.TestCase ): _a : Optional[int]= MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ): '''simple docstring''' lowercase : Any = hf_hub_download( repo_id="""nateraw/video-demo""" ,filename="""archery.mp4""" ,repo_type="""dataset""" ) lowercase : List[str] = VideoClassificationPipeline(model=snake_case ,image_processor=snake_case ,top_k=2 ) lowercase : Dict = [ example_video_filepath, """https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4""", ] return video_classifier, examples def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' for example in examples: lowercase : int = video_classifier(snake_case ) self.assertEqual( snake_case ,[ {"""score""": ANY(snake_case ), """label""": ANY(snake_case )}, {"""score""": ANY(snake_case ), """label""": ANY(snake_case )}, ] ,) @require_torch def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Any = """hf-internal-testing/tiny-random-VideoMAEForVideoClassification""" lowercase : str = VideoMAEFeatureExtractor( size={"""shortest_edge""": 10} ,crop_size={"""height""": 10, """width""": 10} ) lowercase : List[Any] = pipeline( """video-classification""" ,model=snake_case ,feature_extractor=snake_case ,frame_sampling_rate=4 ) lowercase : Dict = hf_hub_download(repo_id="""nateraw/video-demo""" ,filename="""archery.mp4""" ,repo_type="""dataset""" ) lowercase : Any = video_classifier(snake_case ,top_k=2 ) self.assertEqual( nested_simplify(snake_case ,decimals=4 ) ,[{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}] ,) lowercase : str = video_classifier( [ video_file_path, video_file_path, ] ,top_k=2 ,) self.assertEqual( nested_simplify(snake_case ,decimals=4 ) ,[ [{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}], [{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}], ] ,) @require_tf def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' pass
20
0
"""simple docstring""" def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : list[int] ): '''simple docstring''' if not numbers: return 0 if not isinstance(SCREAMING_SNAKE_CASE , (list, tuple) ) or not all( isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) for number in numbers ): raise ValueError("""numbers must be an iterable of integers""" ) lowerCAmelCase = lowerCAmelCase = lowerCAmelCase = numbers[0] for i in range(1 , len(SCREAMING_SNAKE_CASE ) ): # update the maximum and minimum subarray products lowerCAmelCase = numbers[i] if number < 0: lowerCAmelCase , lowerCAmelCase = min_till_now, max_till_now lowerCAmelCase = max(SCREAMING_SNAKE_CASE , max_till_now * number ) lowerCAmelCase = min(SCREAMING_SNAKE_CASE , min_till_now * number ) # update the maximum product found till now lowerCAmelCase = max(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) return max_prod
46
from __future__ import annotations from collections import namedtuple from dataclasses import dataclass @dataclass class __snake_case : _a : int _a : TreeNode | None= None _a : TreeNode | None= None lowercase : Dict = namedtuple("""CoinsDistribResult""", """moves excess""") def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: if root is None: return 0 # Validation def count_nodes(SCREAMING_SNAKE_CASE__ ) -> int: if node is None: return 0 return count_nodes(node.left ) + count_nodes(node.right ) + 1 def count_coins(SCREAMING_SNAKE_CASE__ ) -> int: if node is None: return 0 return count_coins(node.left ) + count_coins(node.right ) + node.data if count_nodes(SCREAMING_SNAKE_CASE__ ) != count_coins(SCREAMING_SNAKE_CASE__ ): raise ValueError("""The nodes number should be same as the number of coins""" ) # Main calculation def get_distrib(SCREAMING_SNAKE_CASE__ ) -> CoinsDistribResult: if node is None: return CoinsDistribResult(0 , 1 ) lowercase , lowercase : int = get_distrib(node.left ) lowercase , lowercase : List[Any] = get_distrib(node.right ) lowercase : Optional[Any] = 1 - left_distrib_excess lowercase : Union[str, Any] = 1 - right_distrib_excess lowercase : List[Any] = ( left_distrib_moves + right_distrib_moves + abs(SCREAMING_SNAKE_CASE__ ) + abs(SCREAMING_SNAKE_CASE__ ) ) lowercase : Any = node.data - coins_to_left - coins_to_right return CoinsDistribResult(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return get_distrib(SCREAMING_SNAKE_CASE__ )[0] if __name__ == "__main__": import doctest doctest.testmod()
20
0
'''simple docstring''' import argparse import torch from transformers import OpenAIGPTConfig, OpenAIGPTModel, load_tf_weights_in_openai_gpt from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def _lowerCAmelCase ( _UpperCamelCase : Optional[int] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[int] ) -> Optional[Any]: """simple docstring""" if openai_config_file == "": _SCREAMING_SNAKE_CASE =OpenAIGPTConfig() else: _SCREAMING_SNAKE_CASE =OpenAIGPTConfig.from_json_file(_UpperCamelCase ) _SCREAMING_SNAKE_CASE =OpenAIGPTModel(_UpperCamelCase ) # Load weights from numpy load_tf_weights_in_openai_gpt(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # Save pytorch-model _SCREAMING_SNAKE_CASE =pytorch_dump_folder_path + '/' + WEIGHTS_NAME _SCREAMING_SNAKE_CASE =pytorch_dump_folder_path + '/' + CONFIG_NAME print(f"Save PyTorch model to {pytorch_weights_dump_path}" ) torch.save(model.state_dict() , _UpperCamelCase ) print(f"Save configuration file to {pytorch_config_dump_path}" ) with open(_UpperCamelCase , 'w' , encoding='utf-8' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": lowerCamelCase : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( "--openai_checkpoint_folder_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument( "--openai_config_file", default="", type=str, help=( "An optional config json file corresponding to the pre-trained OpenAI model. \n" "This specifies the model architecture." ), ) lowerCamelCase : str = parser.parse_args() convert_openai_checkpoint_to_pytorch( args.openai_checkpoint_folder_path, args.openai_config_file, args.pytorch_dump_folder_path )
47
from collections import UserDict from typing import Union import numpy as np import requests from ..utils import ( add_end_docstrings, logging, ) from .audio_classification import ffmpeg_read from .base import PIPELINE_INIT_ARGS, Pipeline lowercase : Optional[Any] = logging.get_logger(__name__) @add_end_docstrings(lowerCAmelCase ) class __snake_case ( lowerCAmelCase ): def __init__( self ,**snake_case ): '''simple docstring''' super().__init__(**snake_case ) if self.framework != "pt": raise ValueError(f"The {self.__class__} is only available in PyTorch." ) # No specific FOR_XXX available yet def __call__( self ,snake_case ,**snake_case ): '''simple docstring''' return super().__call__(snake_case ,**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,**snake_case ): '''simple docstring''' lowercase : Union[str, Any] = {} if "candidate_labels" in kwargs: lowercase : List[str] = kwargs["""candidate_labels"""] if "hypothesis_template" in kwargs: lowercase : Dict = kwargs["""hypothesis_template"""] return preprocess_params, {}, {} def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ,snake_case="This is a sound of {}." ): '''simple docstring''' if isinstance(snake_case ,snake_case ): if audio.startswith("""http://""" ) or audio.startswith("""https://""" ): # We need to actually check for a real protocol, otherwise it's impossible to use a local file # like http_huggingface_co.png lowercase : Optional[Any] = requests.get(snake_case ).content else: with open(snake_case ,"""rb""" ) as f: lowercase : Union[str, Any] = f.read() if isinstance(snake_case ,snake_case ): lowercase : int = ffmpeg_read(snake_case ,self.feature_extractor.sampling_rate ) if not isinstance(snake_case ,np.ndarray ): raise ValueError("""We expect a numpy ndarray as input""" ) if len(audio.shape ) != 1: raise ValueError("""We expect a single channel audio input for ZeroShotAudioClassificationPipeline""" ) lowercase : Dict = self.feature_extractor( [audio] ,sampling_rate=self.feature_extractor.sampling_rate ,return_tensors="""pt""" ) lowercase : Tuple = candidate_labels lowercase : Tuple = [hypothesis_template.format(snake_case ) for x in candidate_labels] lowercase : Optional[Any] = self.tokenizer(snake_case ,return_tensors=self.framework ,padding=snake_case ) lowercase : Optional[Any] = [text_inputs] return inputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : List[str] = model_inputs.pop("""candidate_labels""" ) lowercase : Dict = model_inputs.pop("""text_inputs""" ) if isinstance(text_inputs[0] ,snake_case ): lowercase : List[Any] = text_inputs[0] else: # Batching case. lowercase : Dict = text_inputs[0][0] lowercase : Optional[Any] = self.model(**snake_case ,**snake_case ) lowercase : Any = { """candidate_labels""": candidate_labels, """logits""": outputs.logits_per_audio, } return model_outputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : List[Any] = model_outputs.pop("""candidate_labels""" ) lowercase : Any = model_outputs["""logits"""][0] if self.framework == "pt": lowercase : Any = logits.softmax(dim=0 ) lowercase : Tuple = probs.tolist() else: raise ValueError("""`tf` framework not supported.""" ) lowercase : Tuple = [ {"""score""": score, """label""": candidate_label} for score, candidate_label in sorted(zip(snake_case ,snake_case ) ,key=lambda snake_case : -x[0] ) ] return result
20
0
import gc import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer import diffusers from diffusers import ( AutoencoderKL, EulerDiscreteScheduler, StableDiffusionLatentUpscalePipeline, StableDiffusionPipeline, UNetaDConditionModel, ) from diffusers.schedulers import KarrasDiffusionSchedulers from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() def A ( _SCREAMING_SNAKE_CASE ) -> Optional[Any]: lowerCamelCase : Tuple = [tensor.shape for tensor in tensor_list] return all(shape == shapes[0] for shape in shapes[1:] ) class UpperCamelCase__ (lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , unittest.TestCase ): '''simple docstring''' lowerCamelCase_ : Union[str, Any] = StableDiffusionLatentUpscalePipeline lowerCamelCase_ : Union[str, Any] = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - { """height""", """width""", """cross_attention_kwargs""", """negative_prompt_embeds""", """prompt_embeds""", } lowerCamelCase_ : Tuple = PipelineTesterMixin.required_optional_params - {"""num_images_per_prompt"""} lowerCamelCase_ : Optional[int] = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS lowerCamelCase_ : Tuple = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess lowerCamelCase_ : Dict = frozenset([] ) lowerCamelCase_ : Tuple = True @property def _lowercase ( self ) -> List[Any]: lowerCamelCase : Optional[int] = 1 lowerCamelCase : List[str] = 4 lowerCamelCase : List[str] = (16, 16) lowerCamelCase : str = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(UpperCamelCase__ ) return image def _lowercase ( self ) -> List[str]: torch.manual_seed(0 ) lowerCamelCase : Dict = UNetaDConditionModel( act_fn="gelu" , attention_head_dim=8 , norm_num_groups=UpperCamelCase__ , block_out_channels=[32, 32, 64, 64] , time_cond_proj_dim=160 , conv_in_kernel=1 , conv_out_kernel=1 , cross_attention_dim=32 , down_block_types=( "KDownBlock2D", "KCrossAttnDownBlock2D", "KCrossAttnDownBlock2D", "KCrossAttnDownBlock2D", ) , in_channels=8 , mid_block_type=UpperCamelCase__ , only_cross_attention=UpperCamelCase__ , out_channels=5 , resnet_time_scale_shift="scale_shift" , time_embedding_type="fourier" , timestep_post_act="gelu" , up_block_types=("KCrossAttnUpBlock2D", "KCrossAttnUpBlock2D", "KCrossAttnUpBlock2D", "KUpBlock2D") , ) lowerCamelCase : Tuple = AutoencoderKL( block_out_channels=[32, 32, 64, 64] , in_channels=3 , out_channels=3 , down_block_types=[ "DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D", ] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , ) lowerCamelCase : int = EulerDiscreteScheduler(prediction_type="sample" ) lowerCamelCase : List[str] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act="quick_gelu" , projection_dim=512 , ) lowerCamelCase : List[str] = CLIPTextModel(UpperCamelCase__ ) lowerCamelCase : List[str] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) lowerCamelCase : Optional[Any] = { "unet": model.eval(), "vae": vae.eval(), "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def _lowercase ( self , UpperCamelCase__ , UpperCamelCase__=0 ) -> Optional[Any]: if str(UpperCamelCase__ ).startswith("mps" ): lowerCamelCase : Any = torch.manual_seed(UpperCamelCase__ ) else: lowerCamelCase : Optional[Any] = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) lowerCamelCase : int = { "prompt": "A painting of a squirrel eating a burger", "image": self.dummy_image.cpu(), "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs def _lowercase ( self ) -> List[Any]: lowerCamelCase : List[str] = "cpu" lowerCamelCase : Any = self.get_dummy_components() lowerCamelCase : Union[str, Any] = self.pipeline_class(**UpperCamelCase__ ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) lowerCamelCase : str = self.get_dummy_inputs(UpperCamelCase__ ) lowerCamelCase : Any = pipe(**UpperCamelCase__ ).images lowerCamelCase : Optional[int] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 256, 256, 3) ) lowerCamelCase : str = np.array( [0.47222412, 0.41921633, 0.44717434, 0.46874192, 0.42588258, 0.46150726, 0.4677534, 0.45583832, 0.48579055] ) lowerCamelCase : List[Any] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(UpperCamelCase__ , 1e-3 ) def _lowercase ( self ) -> str: super().test_attention_slicing_forward_pass(expected_max_diff=7e-3 ) def _lowercase ( self ) -> Any: super().test_cpu_offload_forward_pass(expected_max_diff=3e-3 ) def _lowercase ( self ) -> Union[str, Any]: super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def _lowercase ( self ) -> Tuple: super().test_inference_batch_single_identical(expected_max_diff=7e-3 ) def _lowercase ( self ) -> Dict: super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3e-3 ) def _lowercase ( self ) -> Optional[int]: super().test_save_load_local(expected_max_difference=3e-3 ) def _lowercase ( self ) -> Any: super().test_save_load_optional_components(expected_max_difference=3e-3 ) def _lowercase ( self ) -> Dict: lowerCamelCase : List[str] = [ "DDIMScheduler", "DDPMScheduler", "PNDMScheduler", "HeunDiscreteScheduler", "EulerAncestralDiscreteScheduler", "KDPM2DiscreteScheduler", "KDPM2AncestralDiscreteScheduler", "DPMSolverSDEScheduler", ] lowerCamelCase : Optional[Any] = self.get_dummy_components() lowerCamelCase : Tuple = self.pipeline_class(**UpperCamelCase__ ) # make sure that PNDM does not need warm-up pipe.scheduler.register_to_config(skip_prk_steps=UpperCamelCase__ ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) lowerCamelCase : Optional[Any] = self.get_dummy_inputs(UpperCamelCase__ ) lowerCamelCase : Optional[int] = 2 lowerCamelCase : Any = [] for scheduler_enum in KarrasDiffusionSchedulers: if scheduler_enum.name in skip_schedulers: # no sigma schedulers are not supported # no schedulers continue lowerCamelCase : int = getattr(UpperCamelCase__ , scheduler_enum.name ) lowerCamelCase : Optional[Any] = scheduler_cls.from_config(pipe.scheduler.config ) lowerCamelCase : Optional[int] = pipe(**UpperCamelCase__ )[0] outputs.append(UpperCamelCase__ ) assert check_same_shape(UpperCamelCase__ ) @require_torch_gpu @slow class UpperCamelCase__ (unittest.TestCase ): '''simple docstring''' def _lowercase ( self ) -> List[Any]: super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowercase ( self ) -> str: lowerCamelCase : int = torch.manual_seed(33 ) lowerCamelCase : Tuple = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4" , torch_dtype=torch.floataa ) pipe.to("cuda" ) lowerCamelCase : List[Any] = StableDiffusionLatentUpscalePipeline.from_pretrained( "stabilityai/sd-x2-latent-upscaler" , torch_dtype=torch.floataa ) upscaler.to("cuda" ) lowerCamelCase : List[Any] = "a photo of an astronaut high resolution, unreal engine, ultra realistic" lowerCamelCase : int = pipe(UpperCamelCase__ , generator=UpperCamelCase__ , output_type="latent" ).images lowerCamelCase : str = upscaler( prompt=UpperCamelCase__ , image=UpperCamelCase__ , num_inference_steps=20 , guidance_scale=0 , generator=UpperCamelCase__ , output_type="np" , ).images[0] lowerCamelCase : Any = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy" ) assert np.abs((expected_image - image).mean() ) < 5e-2 def _lowercase ( self ) -> Optional[int]: lowerCamelCase : int = torch.manual_seed(33 ) lowerCamelCase : str = StableDiffusionLatentUpscalePipeline.from_pretrained( "stabilityai/sd-x2-latent-upscaler" , torch_dtype=torch.floataa ) upscaler.to("cuda" ) lowerCamelCase : Any = "the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas" lowerCamelCase : Any = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png" ) lowerCamelCase : str = upscaler( prompt=UpperCamelCase__ , image=UpperCamelCase__ , num_inference_steps=20 , guidance_scale=0 , generator=UpperCamelCase__ , output_type="np" , ).images[0] lowerCamelCase : List[Any] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy" ) assert np.abs((expected_image - image).max() ) < 5e-2
48
import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _snake_case( *SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__=True , SCREAMING_SNAKE_CASE__=2 ) -> Optional[Any]: from .. import __version__ lowercase : int = take_from lowercase : Tuple = () if not isinstance(args[0] , SCREAMING_SNAKE_CASE__ ): lowercase : Dict = (args,) for attribute, version_name, message in args: if version.parse(version.parse(SCREAMING_SNAKE_CASE__ ).base_version ) >= version.parse(SCREAMING_SNAKE_CASE__ ): raise ValueError( f"The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers'" f" version {__version__} is >= {version_name}" ) lowercase : int = None if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(SCREAMING_SNAKE_CASE__ ),) lowercase : Union[str, Any] = f"The `{attribute}` argument is deprecated and will be removed in version {version_name}." elif hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): values += (getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ),) lowercase : int = f"The `{attribute}` attribute is deprecated and will be removed in version {version_name}." elif deprecated_kwargs is None: lowercase : Dict = f"`{attribute}` is deprecated and will be removed in version {version_name}." if warning is not None: lowercase : Dict = warning + """ """ if standard_warn else """""" warnings.warn(warning + message , SCREAMING_SNAKE_CASE__ , stacklevel=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and len(SCREAMING_SNAKE_CASE__ ) > 0: lowercase : str = inspect.getouterframes(inspect.currentframe() )[1] lowercase : List[str] = call_frame.filename lowercase : Tuple = call_frame.lineno lowercase : List[str] = call_frame.function lowercase , lowercase : Optional[Any] = next(iter(deprecated_kwargs.items() ) ) raise TypeError(f"{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`" ) if len(SCREAMING_SNAKE_CASE__ ) == 0: return elif len(SCREAMING_SNAKE_CASE__ ) == 1: return values[0] return values
20
0
import unittest from transformers import AlbertTokenizer, AlbertTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin __snake_case :Union[str, Any] = get_tests_dir('''fixtures/spiece.model''') @require_sentencepiece @require_tokenizers class _A ( __UpperCAmelCase ,unittest.TestCase ): UpperCamelCase__ : Dict = AlbertTokenizer UpperCamelCase__ : List[str] = AlbertTokenizerFast UpperCamelCase__ : int = True UpperCamelCase__ : int = True UpperCamelCase__ : Any = True def _lowerCamelCase ( self : Dict): '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing __a = AlbertTokenizer(__SCREAMING_SNAKE_CASE) tokenizer.save_pretrained(self.tmpdirname) def _lowerCamelCase ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, Any]): '''simple docstring''' __a = '''this is a test''' __a = '''this is a test''' return input_text, output_text def _lowerCamelCase ( self : Optional[int]): '''simple docstring''' __a = '''<pad>''' __a = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__SCREAMING_SNAKE_CASE) , __SCREAMING_SNAKE_CASE) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__SCREAMING_SNAKE_CASE) , __SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : List[Any]): '''simple docstring''' __a = list(self.get_tokenizer().get_vocab().keys()) self.assertEqual(vocab_keys[0] , '''<pad>''') self.assertEqual(vocab_keys[1] , '''<unk>''') self.assertEqual(vocab_keys[-1] , '''▁eloquent''') self.assertEqual(len(__SCREAMING_SNAKE_CASE) , 30_000) def _lowerCamelCase ( self : Dict): '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 30_000) def _lowerCamelCase ( self : Optional[int]): '''simple docstring''' if not self.test_rust_tokenizer: return __a = self.get_tokenizer() __a = self.get_rust_tokenizer() __a = '''I was born in 92000, and this is falsé.''' __a = tokenizer.tokenize(__SCREAMING_SNAKE_CASE) __a = rust_tokenizer.tokenize(__SCREAMING_SNAKE_CASE) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE) __a = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a = self.get_rust_tokenizer() __a = tokenizer.encode(__SCREAMING_SNAKE_CASE) __a = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : str): '''simple docstring''' __a = AlbertTokenizer(__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE) __a = tokenizer.tokenize('''This is a test''') self.assertListEqual(__SCREAMING_SNAKE_CASE , ['''▁this''', '''▁is''', '''▁a''', '''▁test''']) self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE) , [48, 25, 21, 1_289]) __a = tokenizer.tokenize('''I was born in 92000, and this is falsé.''') self.assertListEqual( __SCREAMING_SNAKE_CASE , ['''▁i''', '''▁was''', '''▁born''', '''▁in''', '''▁9''', '''2000''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁fal''', '''s''', '''é''', '''.''']) __a = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE) self.assertListEqual(__SCREAMING_SNAKE_CASE , [31, 23, 386, 19, 561, 3_050, 15, 17, 48, 25, 8_256, 18, 1, 9]) __a = tokenizer.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE) self.assertListEqual( __SCREAMING_SNAKE_CASE , ['''▁i''', '''▁was''', '''▁born''', '''▁in''', '''▁9''', '''2000''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁fal''', '''s''', '''<unk>''', '''.'''] , ) def _lowerCamelCase ( self : Tuple): '''simple docstring''' __a = AlbertTokenizer(__SCREAMING_SNAKE_CASE) __a = tokenizer.encode('''sequence builders''') __a = tokenizer.encode('''multi-sequence build''') __a = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE) __a = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [ tokenizer.sep_token_id ] @slow def _lowerCamelCase ( self : Tuple): '''simple docstring''' __a = {'''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''input_ids''': [[2, 21_970, 13, 5, 6_092, 167, 28, 7_103, 2_153, 673, 8, 7_028, 12_051, 18, 17, 7_103, 2_153, 673, 8, 3_515, 18_684, 8, 4_461, 6, 1_927, 297, 8, 12_060, 2_607, 18, 13, 5, 4_461, 15, 10_538, 38, 8, 135, 15, 822, 58, 15, 993, 10_363, 15, 1_460, 8_005, 4_461, 15, 993, 255, 2_328, 9, 9, 9, 6, 26, 1_112, 816, 3_260, 13, 5, 103, 2_377, 6, 17, 1_112, 816, 2_782, 13, 5, 103, 10_641, 6, 29, 84, 2_512, 2_430, 782, 18_684, 2_761, 19, 808, 2_430, 2_556, 17, 855, 1_480, 9_477, 4_091, 128, 11_712, 15, 7_103, 2_153, 673, 17, 24_883, 9_990, 9, 3], [2, 11_502, 25, 1_006, 20, 782, 8, 11_809, 855, 1_732, 19_393, 18_667, 37, 367, 21_018, 69, 1_854, 34, 11_860, 19_124, 27, 156, 225, 17, 193, 4_141, 19, 65, 9_124, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 14, 2_231, 886, 2_385, 17_659, 84, 14, 16_792, 1_952, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''token_type_ids''': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__SCREAMING_SNAKE_CASE , model_name='''albert-base-v2''' , revision='''6b6560eaf5ff2e250b00c50f380c5389a9c2d82e''' , )
49
def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: if index == r: for j in range(SCREAMING_SNAKE_CASE__ ): print(data[j] , end=""" """ ) print(""" """ ) return # When no more elements are there to put in data[] if i >= n: return # current is included, put next at next location lowercase : Tuple = arr[i] combination_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , index + 1 , SCREAMING_SNAKE_CASE__ , i + 1 ) # current is excluded, replace it with # next (Note that i+1 is passed, but # index is not changed) combination_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , i + 1 ) # The main function that prints all combinations # of size r in arr[] of size n. This function # mainly uses combinationUtil() def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: # A temporary array to store all combination one by one lowercase : Optional[int] = [0] * r # Print all combination using temporary array 'data[]' combination_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , 0 , SCREAMING_SNAKE_CASE__ , 0 ) if __name__ == "__main__": # Driver code to check the function above lowercase : int = [10, 20, 30, 40, 50] print_combination(arr, len(arr), 3) # This code is contributed by Ambuj sahu
20
0
from __future__ import annotations from math import pow, sqrt def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) -> dict[str, float]: if (resistance, reactance, impedance).count(0 ) != 1: raise ValueError('One and only one argument must be 0' ) if resistance == 0: return {"resistance": sqrt(pow(_UpperCAmelCase , 2 ) - pow(_UpperCAmelCase , 2 ) )} elif reactance == 0: return {"reactance": sqrt(pow(_UpperCAmelCase , 2 ) - pow(_UpperCAmelCase , 2 ) )} elif impedance == 0: return {"impedance": sqrt(pow(_UpperCAmelCase , 2 ) + pow(_UpperCAmelCase , 2 ) )} else: raise ValueError('Exactly one argument must be 0' ) if __name__ == "__main__": import doctest doctest.testmod()
50
import contextlib import os import sqlitea import pytest from datasets import Dataset, Features, Value from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) 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 @require_sqlalchemy @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: lowercase : Union[str, Any] = tmp_path / """cache""" lowercase : Optional[Any] = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowercase : Any = SqlDatasetReader( """dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ , keep_in_memory=SCREAMING_SNAKE_CASE__ ).read() _check_sql_dataset(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @require_sqlalchemy @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 _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: lowercase : Union[str, Any] = tmp_path / """cache""" lowercase : int = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} lowercase : str = features.copy() if features else default_expected_features lowercase : Optional[Any] = ( Features({feature: Value(SCREAMING_SNAKE_CASE__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowercase : Optional[int] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , features=SCREAMING_SNAKE_CASE__ , cache_dir=SCREAMING_SNAKE_CASE__ ).read() _check_sql_dataset(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[Any]: with contextlib.closing(sqlitea.connect(SCREAMING_SNAKE_CASE__ ) ) as con: lowercase : Optional[int] = con.cursor() cur.execute("""SELECT * FROM dataset""" ) for row in cur: yield row @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> int: lowercase : Any = tmp_path / """cache""" lowercase : int = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : List[str] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=1 ).write() lowercase : List[str] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = iter_sql_file(SCREAMING_SNAKE_CASE__ ) for rowa, rowa in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): assert rowa == rowa @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: lowercase : Dict = tmp_path / """cache""" lowercase : List[str] = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : List[str] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=2 ).write() lowercase : Optional[int] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) for rowa, rowa in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): assert rowa == rowa @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : str = tmp_path / """cache""" lowercase : Optional[int] = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : Optional[Any] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() with pytest.raises(SCREAMING_SNAKE_CASE__ ): SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=0 ).write()
20
0
import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class __snake_case : def __init__( self : str , _snake_case : Tuple , _snake_case : List[str]=13 , _snake_case : Tuple=7 , _snake_case : Optional[Any]=True , _snake_case : List[str]=True , _snake_case : Dict=False , _snake_case : List[str]=True , _snake_case : Optional[int]=99 , _snake_case : Optional[int]=32 , _snake_case : Dict=5 , _snake_case : List[Any]=4 , _snake_case : Optional[int]=37 , _snake_case : Optional[Any]="gelu" , _snake_case : Tuple=0.1 , _snake_case : Tuple=0.1 , _snake_case : int=512 , _snake_case : int=16 , _snake_case : List[Any]=2 , _snake_case : Any=0.0_2 , _snake_case : str=3 , _snake_case : Union[str, Any]=4 , _snake_case : str=None , ): """simple docstring""" UpperCAmelCase_ = parent UpperCAmelCase_ = batch_size UpperCAmelCase_ = seq_length UpperCAmelCase_ = is_training UpperCAmelCase_ = use_input_mask UpperCAmelCase_ = use_token_type_ids UpperCAmelCase_ = use_labels UpperCAmelCase_ = vocab_size UpperCAmelCase_ = hidden_size UpperCAmelCase_ = num_hidden_layers UpperCAmelCase_ = num_attention_heads UpperCAmelCase_ = intermediate_size UpperCAmelCase_ = hidden_act UpperCAmelCase_ = hidden_dropout_prob UpperCAmelCase_ = attention_probs_dropout_prob UpperCAmelCase_ = max_position_embeddings UpperCAmelCase_ = type_vocab_size UpperCAmelCase_ = type_sequence_label_size UpperCAmelCase_ = initializer_range UpperCAmelCase_ = num_labels UpperCAmelCase_ = num_choices UpperCAmelCase_ = scope def lowerCamelCase ( self : Optional[Any]): """simple docstring""" UpperCAmelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) UpperCAmelCase_ = None if self.use_input_mask: UpperCAmelCase_ = random_attention_mask([self.batch_size, self.seq_length]) UpperCAmelCase_ = None if self.use_token_type_ids: UpperCAmelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size) UpperCAmelCase_ = None UpperCAmelCase_ = None UpperCAmelCase_ = None if self.use_labels: UpperCAmelCase_ = ids_tensor([self.batch_size] , self.type_sequence_label_size) UpperCAmelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels) UpperCAmelCase_ = ids_tensor([self.batch_size] , self.num_choices) UpperCAmelCase_ = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowerCamelCase ( self : str): """simple docstring""" return OpenLlamaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_snake_case , initializer_range=self.initializer_range , use_stable_embedding=_snake_case , ) def lowerCamelCase ( self : Optional[Any] , _snake_case : int , _snake_case : str , _snake_case : Union[str, Any] , _snake_case : Dict , _snake_case : Optional[Any] , _snake_case : Any , _snake_case : Optional[Any]): """simple docstring""" UpperCAmelCase_ = OpenLlamaModel(config=_snake_case) model.to(_snake_case) model.eval() UpperCAmelCase_ = model(_snake_case , attention_mask=_snake_case) UpperCAmelCase_ = model(_snake_case) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def lowerCamelCase ( self : Dict , _snake_case : Optional[int] , _snake_case : Dict , _snake_case : List[Any] , _snake_case : List[Any] , _snake_case : Any , _snake_case : List[Any] , _snake_case : str , _snake_case : Optional[int] , _snake_case : str , ): """simple docstring""" UpperCAmelCase_ = True UpperCAmelCase_ = OpenLlamaModel(_snake_case) model.to(_snake_case) model.eval() UpperCAmelCase_ = model( _snake_case , attention_mask=_snake_case , encoder_hidden_states=_snake_case , encoder_attention_mask=_snake_case , ) UpperCAmelCase_ = model( _snake_case , attention_mask=_snake_case , encoder_hidden_states=_snake_case , ) UpperCAmelCase_ = model(_snake_case , attention_mask=_snake_case) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def lowerCamelCase ( self : Union[str, Any] , _snake_case : List[Any] , _snake_case : Tuple , _snake_case : int , _snake_case : List[Any] , _snake_case : Any , _snake_case : str , _snake_case : int , _snake_case : Optional[int] , _snake_case : Union[str, Any] , ): """simple docstring""" UpperCAmelCase_ = OpenLlamaForCausalLM(config=_snake_case) model.to(_snake_case) model.eval() UpperCAmelCase_ = model(_snake_case , attention_mask=_snake_case , labels=_snake_case) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def lowerCamelCase ( self : List[str] , _snake_case : Union[str, Any] , _snake_case : Optional[int] , _snake_case : Any , _snake_case : Tuple , _snake_case : List[Any] , _snake_case : str , _snake_case : Tuple , _snake_case : Tuple , _snake_case : Dict , ): """simple docstring""" UpperCAmelCase_ = True UpperCAmelCase_ = True UpperCAmelCase_ = OpenLlamaForCausalLM(config=_snake_case) model.to(_snake_case) model.eval() # first forward pass UpperCAmelCase_ = model( _snake_case , attention_mask=_snake_case , encoder_hidden_states=_snake_case , encoder_attention_mask=_snake_case , use_cache=_snake_case , ) UpperCAmelCase_ = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids UpperCAmelCase_ = ids_tensor((self.batch_size, 3) , config.vocab_size) UpperCAmelCase_ = ids_tensor((self.batch_size, 3) , vocab_size=2) # append to next input_ids and UpperCAmelCase_ = torch.cat([input_ids, next_tokens] , dim=-1) UpperCAmelCase_ = torch.cat([input_mask, next_mask] , dim=-1) UpperCAmelCase_ = model( _snake_case , attention_mask=_snake_case , encoder_hidden_states=_snake_case , encoder_attention_mask=_snake_case , output_hidden_states=_snake_case , )['''hidden_states'''][0] UpperCAmelCase_ = model( _snake_case , attention_mask=_snake_case , encoder_hidden_states=_snake_case , encoder_attention_mask=_snake_case , past_key_values=_snake_case , output_hidden_states=_snake_case , )['''hidden_states'''][0] # select random slice UpperCAmelCase_ = ids_tensor((1,) , output_from_past.shape[-1]).item() UpperCAmelCase_ = output_from_no_past[:, -3:, random_slice_idx].detach() UpperCAmelCase_ = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(_snake_case , _snake_case , atol=1e-3)) def lowerCamelCase ( self : Any): """simple docstring""" UpperCAmelCase_ = self.prepare_config_and_inputs() ( ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ) = config_and_inputs UpperCAmelCase_ = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __snake_case ( a , a , a , unittest.TestCase ): UpperCAmelCase__ : Dict = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) UpperCAmelCase__ : int = (OpenLlamaForCausalLM,) if is_torch_available() else () UpperCAmelCase__ : Union[str, Any] = ( { '''feature-extraction''': OpenLlamaModel, '''text-classification''': OpenLlamaForSequenceClassification, '''text-generation''': OpenLlamaForCausalLM, '''zero-shot''': OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) UpperCAmelCase__ : Any = False UpperCAmelCase__ : Dict = False def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ = OpenLlamaModelTester(self) UpperCAmelCase_ = ConfigTester(self , config_class=_snake_case , hidden_size=37) def lowerCamelCase ( self : Tuple): """simple docstring""" self.config_tester.run_common_tests() def lowerCamelCase ( self : str): """simple docstring""" UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case) def lowerCamelCase ( self : Dict): """simple docstring""" UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: UpperCAmelCase_ = type self.model_tester.create_and_check_model(*_snake_case) def lowerCamelCase ( self : Union[str, Any]): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase_ = 3 UpperCAmelCase_ = input_dict['''input_ids'''] UpperCAmelCase_ = input_ids.ne(1).to(_snake_case) UpperCAmelCase_ = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size) UpperCAmelCase_ = OpenLlamaForSequenceClassification(_snake_case) model.to(_snake_case) model.eval() UpperCAmelCase_ = model(_snake_case , attention_mask=_snake_case , labels=_snake_case) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels)) def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase_ = 3 UpperCAmelCase_ = '''single_label_classification''' UpperCAmelCase_ = input_dict['''input_ids'''] UpperCAmelCase_ = input_ids.ne(1).to(_snake_case) UpperCAmelCase_ = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size) UpperCAmelCase_ = OpenLlamaForSequenceClassification(_snake_case) model.to(_snake_case) model.eval() UpperCAmelCase_ = model(_snake_case , attention_mask=_snake_case , labels=_snake_case) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels)) def lowerCamelCase ( self : int): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase_ = 3 UpperCAmelCase_ = '''multi_label_classification''' UpperCAmelCase_ = input_dict['''input_ids'''] UpperCAmelCase_ = input_ids.ne(1).to(_snake_case) UpperCAmelCase_ = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size).to(torch.float) UpperCAmelCase_ = OpenLlamaForSequenceClassification(_snake_case) model.to(_snake_case) model.eval() UpperCAmelCase_ = model(_snake_case , attention_mask=_snake_case , labels=_snake_case) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels)) @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test''') def lowerCamelCase ( self : List[Any]): """simple docstring""" pass @parameterized.expand([('''linear''',), ('''dynamic''',)]) def lowerCamelCase ( self : List[Any] , _snake_case : List[Any]): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase_ = ids_tensor([1, 10] , config.vocab_size) UpperCAmelCase_ = ids_tensor([1, int(config.max_position_embeddings * 1.5)] , config.vocab_size) set_seed(42) # Fixed seed at init time so the two models get the same random weights UpperCAmelCase_ = OpenLlamaModel(_snake_case) original_model.to(_snake_case) original_model.eval() UpperCAmelCase_ = original_model(_snake_case).last_hidden_state UpperCAmelCase_ = original_model(_snake_case).last_hidden_state set_seed(42) # Fixed seed at init time so the two models get the same random weights UpperCAmelCase_ = {'''type''': scaling_type, '''factor''': 1_0.0} UpperCAmelCase_ = OpenLlamaModel(_snake_case) scaled_model.to(_snake_case) scaled_model.eval() UpperCAmelCase_ = scaled_model(_snake_case).last_hidden_state UpperCAmelCase_ = scaled_model(_snake_case).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(_snake_case , _snake_case , atol=1e-5)) else: self.assertFalse(torch.allclose(_snake_case , _snake_case , atol=1e-5)) # The output should be different for long inputs self.assertFalse(torch.allclose(_snake_case , _snake_case , atol=1e-5))
51
import os import numpy import onnx def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: lowercase : int = a.name lowercase : Any = b.name lowercase : Optional[Any] = """""" lowercase : Dict = """""" lowercase : int = a == b lowercase : int = name_a lowercase : List[str] = name_b return res def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: for i, input_name in enumerate(node_proto.input ): if input_name == name: node_proto.input.insert(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) node_proto.input.pop(i + 1 ) if node_proto.op_type == "If": _graph_replace_input_with(node_proto.attribute[0].g , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) _graph_replace_input_with(node_proto.attribute[1].g , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if node_proto.op_type == "Loop": _graph_replace_input_with(node_proto.attribute[0].g , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: for n in graph_proto.node: _node_replace_input_with(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: lowercase : Any = list(model.graph.initializer ) lowercase : Dict = list(model_without_ext.graph.initializer ) for i, ref_i in ind_to_replace: assert inits_with_data[i].name == inits[i].name assert inits_with_data[ref_i].name == inits[ref_i].name assert i > ref_i lowercase : Union[str, Any] = inits[i].name lowercase : Dict = inits[ref_i].name model_without_ext.graph.initializer.remove(inits[i] ) # for n in model.graph.node: _graph_replace_input_with(model_without_ext.graph , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : Union[str, Any] = os.path.dirname(SCREAMING_SNAKE_CASE__ ) lowercase : Dict = os.path.basename(SCREAMING_SNAKE_CASE__ ) lowercase : str = onnx.load(os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) lowercase : List[str] = list(model.graph.initializer ) lowercase : Tuple = set() lowercase : int = {} lowercase : Optional[Any] = [] lowercase : Dict = 0 for i in range(len(SCREAMING_SNAKE_CASE__ ) ): if i in dup_set: continue for j in range(i + 1 , len(SCREAMING_SNAKE_CASE__ ) ): if j in dup_set: continue if _is_equal_tensor_proto(inits[i] , inits[j] ): dup_set.add(SCREAMING_SNAKE_CASE__ ) dup_set.add(SCREAMING_SNAKE_CASE__ ) lowercase : int = inits[j].data_type lowercase : Optional[int] = numpy.prod(inits[j].dims ) if dtype == 1: mem_size *= 4 elif dtype == 6: mem_size *= 4 elif dtype == 7 or dtype == 11: mem_size *= 8 else: print("""unexpected data type: """ , SCREAMING_SNAKE_CASE__ ) total_reduced_size += mem_size lowercase : Tuple = inits[i].name lowercase : int = inits[j].name if name_i in dup_map: dup_map[name_i].append(SCREAMING_SNAKE_CASE__ ) else: lowercase : List[str] = [name_j] ind_to_replace.append((j, i) ) print("""total reduced size: """ , total_reduced_size / 1_024 / 1_024 / 1_024 , """GB""" ) lowercase : str = sorted(SCREAMING_SNAKE_CASE__ ) _remove_dup_initializers_from_model(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = """optimized_""" + model_file_name lowercase : Dict = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) onnx.save(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return new_model
20
0
import unittest from knapsack import greedy_knapsack as kp class A__ ( unittest.TestCase ): def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : str = [10, 20, 30, 40, 50, 60] UpperCamelCase : Optional[int] = [2, 4, 6, 8, 10, 12] UpperCamelCase : str = 100 self.assertEqual(kp.calc_profit(A_ , A_ , A_ ) , 210 ) def __UpperCamelCase( self ): '''simple docstring''' self.assertRaisesRegex(A_ , "max_weight must greater than zero." ) def __UpperCamelCase( self ): '''simple docstring''' self.assertRaisesRegex(A_ , "Weight can not be negative." ) def __UpperCamelCase( self ): '''simple docstring''' self.assertRaisesRegex(A_ , "Profit can not be negative." ) def __UpperCamelCase( self ): '''simple docstring''' self.assertRaisesRegex(A_ , "max_weight must greater than zero." ) def __UpperCamelCase( self ): '''simple docstring''' self.assertRaisesRegex( A_ , "The length of profit and weight must be same." ) if __name__ == "__main__": unittest.main()
52
import argparse import json from collections import OrderedDict import torch from huggingface_hub import cached_download, hf_hub_url from transformers import AutoImageProcessor, CvtConfig, CvtForImageClassification def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Tuple: lowercase : Union[str, Any] = [] embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.weight", f"stage{idx}.patch_embed.proj.weight", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.bias", f"stage{idx}.patch_embed.proj.bias", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.weight", f"stage{idx}.patch_embed.norm.weight", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.bias", f"stage{idx}.patch_embed.norm.bias", ) ) return embed def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: lowercase : Optional[Any] = [] attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.weight", f"stage{idx}.blocks.{cnt}.attn.proj_q.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.bias", f"stage{idx}.blocks.{cnt}.attn.proj_q.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.weight", f"stage{idx}.blocks.{cnt}.attn.proj_k.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.bias", f"stage{idx}.blocks.{cnt}.attn.proj_k.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.weight", f"stage{idx}.blocks.{cnt}.attn.proj_v.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.bias", f"stage{idx}.blocks.{cnt}.attn.proj_v.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.weight", f"stage{idx}.blocks.{cnt}.attn.proj.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.bias", f"stage{idx}.blocks.{cnt}.attn.proj.bias", ) ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.weight", f"stage{idx}.blocks.{cnt}.mlp.fc1.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.bias", f"stage{idx}.blocks.{cnt}.mlp.fc1.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.weight", f"stage{idx}.blocks.{cnt}.mlp.fc2.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.bias", f"stage{idx}.blocks.{cnt}.mlp.fc2.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.weight", f"stage{idx}.blocks.{cnt}.norm1.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.bias", f"stage{idx}.blocks.{cnt}.norm1.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.weight", f"stage{idx}.blocks.{cnt}.norm2.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.bias", f"stage{idx}.blocks.{cnt}.norm2.bias") ) return attention_weights def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: lowercase : Optional[Any] = [] token.append((f"cvt.encoder.stages.{idx}.cls_token", """stage2.cls_token""") ) return token def _snake_case( ) -> Dict: lowercase : Optional[Any] = [] head.append(("""layernorm.weight""", """norm.weight""") ) head.append(("""layernorm.bias""", """norm.bias""") ) head.append(("""classifier.weight""", """head.weight""") ) head.append(("""classifier.bias""", """head.bias""") ) return head def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: lowercase : Any = """imagenet-1k-id2label.json""" lowercase : List[str] = 1_000 lowercase : int = """huggingface/label-files""" lowercase : Union[str, Any] = num_labels lowercase : Optional[Any] = json.load(open(cached_download(hf_hub_url(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , repo_type="""dataset""" ) ) , """r""" ) ) lowercase : List[Any] = {int(SCREAMING_SNAKE_CASE__ ): v for k, v in idalabel.items()} lowercase : Dict = idalabel lowercase : List[str] = {v: k for k, v in idalabel.items()} lowercase : List[str] = CvtConfig(num_labels=SCREAMING_SNAKE_CASE__ , idalabel=SCREAMING_SNAKE_CASE__ , labelaid=SCREAMING_SNAKE_CASE__ ) # For depth size 13 (13 = 1+2+10) if cvt_model.rsplit("""/""" , 1 )[-1][4:6] == "13": lowercase : Tuple = [1, 2, 10] # For depth size 21 (21 = 1+4+16) elif cvt_model.rsplit("""/""" , 1 )[-1][4:6] == "21": lowercase : Dict = [1, 4, 16] # For wide cvt (similar to wide-resnet) depth size 24 (w24 = 2 + 2 20) else: lowercase : int = [2, 2, 20] lowercase : Optional[int] = [3, 12, 16] lowercase : str = [192, 768, 1_024] lowercase : Union[str, Any] = CvtForImageClassification(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = AutoImageProcessor.from_pretrained("""facebook/convnext-base-224-22k-1k""" ) lowercase : Optional[Any] = image_size lowercase : Union[str, Any] = torch.load(SCREAMING_SNAKE_CASE__ , map_location=torch.device("""cpu""" ) ) lowercase : Optional[Any] = OrderedDict() lowercase : Tuple = [] for idx in range(len(config.depth ) ): if config.cls_token[idx]: lowercase : Optional[Any] = list_of_state_dict + cls_token(SCREAMING_SNAKE_CASE__ ) lowercase : str = list_of_state_dict + embeddings(SCREAMING_SNAKE_CASE__ ) for cnt in range(config.depth[idx] ): lowercase : List[str] = list_of_state_dict + attention(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = list_of_state_dict + final() for gg in list_of_state_dict: print(SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) ): lowercase : Optional[Any] = original_weights[list_of_state_dict[i][1]] model.load_state_dict(SCREAMING_SNAKE_CASE__ ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) # Download the weights from zoo: https://1drv.ms/u/s!AhIXJn_J-blW9RzF3rMW7SsLHa8h?e=blQ0Al if __name__ == "__main__": lowercase : Tuple = argparse.ArgumentParser() parser.add_argument( """--cvt_model""", default="""cvt-w24""", type=str, help="""Name of the cvt model you'd like to convert.""", ) parser.add_argument( """--image_size""", default=384, type=int, help="""Input Image Size""", ) parser.add_argument( """--cvt_file_name""", default=R"""cvtmodels\CvT-w24-384x384-IN-22k.pth""", type=str, help="""Input Image Size""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) lowercase : Optional[int] = parser.parse_args() convert_cvt_checkpoint(args.cvt_model, args.image_size, args.cvt_file_name, args.pytorch_dump_folder_path)
20
0
'''simple docstring''' 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.''' )
53
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor from ..utils import is_datasets_available from .base import PipelineTool if is_datasets_available(): from datasets import load_dataset class __snake_case ( lowerCAmelCase ): _a : Union[str, Any]= "microsoft/speecht5_tts" _a : Tuple= ( "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the " "text to read (in English) and returns a waveform object containing the sound." ) _a : Dict= "text_reader" _a : Optional[Any]= SpeechTaProcessor _a : Tuple= SpeechTaForTextToSpeech _a : Optional[int]= SpeechTaHifiGan _a : Union[str, Any]= ["text"] _a : Optional[int]= ["audio"] def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self.post_processor is None: lowercase : Any = """microsoft/speecht5_hifigan""" super().setup() def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ): '''simple docstring''' lowercase : int = self.pre_processor(text=snake_case ,return_tensors="""pt""" ,truncation=snake_case ) if speaker_embeddings is None: if not is_datasets_available(): raise ImportError("""Datasets needs to be installed if not passing speaker embeddings.""" ) lowercase : Tuple = load_dataset("""Matthijs/cmu-arctic-xvectors""" ,split="""validation""" ) lowercase : List[str] = torch.tensor(embeddings_dataset[7305]["""xvector"""] ).unsqueeze(0 ) return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings} def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' with torch.no_grad(): return self.model.generate_speech(**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' with torch.no_grad(): return self.post_processor(snake_case ).cpu().detach()
20
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging a__ : Any = logging.get_logger(__name__) a__ : Union[str, Any] = { '''transfo-xl-wt103''': '''https://huggingface.co/transfo-xl-wt103/resolve/main/config.json''', } class UpperCamelCase_ ( UpperCamelCase): """simple docstring""" snake_case__ : Optional[int] = "transfo-xl" snake_case__ : Optional[Any] = ["mems"] snake_case__ : List[Any] = { "n_token": "vocab_size", "hidden_size": "d_model", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : List[Any] , UpperCAmelCase__ : Union[str, Any]=2_6_7_7_3_5 , UpperCAmelCase__ : str=[2_0_0_0_0, 4_0_0_0_0, 2_0_0_0_0_0] , UpperCAmelCase__ : int=1_0_2_4 , UpperCAmelCase__ : Optional[int]=1_0_2_4 , UpperCAmelCase__ : Union[str, Any]=1_6 , UpperCAmelCase__ : Union[str, Any]=6_4 , UpperCAmelCase__ : List[Any]=4_0_9_6 , UpperCAmelCase__ : Any=4 , UpperCAmelCase__ : Any=False , UpperCAmelCase__ : List[str]=1_8 , UpperCAmelCase__ : Dict=1_6_0_0 , UpperCAmelCase__ : Optional[Any]=1_0_0_0 , UpperCAmelCase__ : List[str]=True , UpperCAmelCase__ : Dict=True , UpperCAmelCase__ : Union[str, Any]=0 , UpperCAmelCase__ : str=-1 , UpperCAmelCase__ : str=True , UpperCAmelCase__ : Optional[int]=0.1 , UpperCAmelCase__ : Any=0.0 , UpperCAmelCase__ : Any=True , UpperCAmelCase__ : List[Any]="normal" , UpperCAmelCase__ : Any=0.01 , UpperCAmelCase__ : str=0.01 , UpperCAmelCase__ : Any=0.02 , UpperCAmelCase__ : Tuple=1E-5 , UpperCAmelCase__ : Optional[int]=0 , **UpperCAmelCase__ : Optional[Any] , ) -> List[Any]: __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = [] self.cutoffs.extend(UpperCAmelCase__ ) if proj_share_all_but_first: __SCREAMING_SNAKE_CASE = [False] + [True] * len(self.cutoffs ) else: __SCREAMING_SNAKE_CASE = [False] + [False] * len(self.cutoffs ) __SCREAMING_SNAKE_CASE = d_model __SCREAMING_SNAKE_CASE = d_embed __SCREAMING_SNAKE_CASE = d_head __SCREAMING_SNAKE_CASE = d_inner __SCREAMING_SNAKE_CASE = div_val __SCREAMING_SNAKE_CASE = pre_lnorm __SCREAMING_SNAKE_CASE = n_layer __SCREAMING_SNAKE_CASE = n_head __SCREAMING_SNAKE_CASE = mem_len __SCREAMING_SNAKE_CASE = same_length __SCREAMING_SNAKE_CASE = attn_type __SCREAMING_SNAKE_CASE = clamp_len __SCREAMING_SNAKE_CASE = sample_softmax __SCREAMING_SNAKE_CASE = adaptive __SCREAMING_SNAKE_CASE = dropout __SCREAMING_SNAKE_CASE = dropatt __SCREAMING_SNAKE_CASE = untie_r __SCREAMING_SNAKE_CASE = init __SCREAMING_SNAKE_CASE = init_range __SCREAMING_SNAKE_CASE = proj_init_std __SCREAMING_SNAKE_CASE = init_std __SCREAMING_SNAKE_CASE = layer_norm_epsilon super().__init__(eos_token_id=UpperCAmelCase__ , **UpperCAmelCase__ ) @property def UpperCAmelCase_ ( self : Union[str, Any] ) -> str: # Message copied from Transformer-XL documentation 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 UpperCAmelCase_ ( self : str , UpperCAmelCase__ : str ) -> Optional[int]: # Message copied from Transformer-XL documentation raise NotImplementedError( F"""The model {self.model_type} is one of the few models that has no sequence length limit.""" )
54
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase : str = { """configuration_funnel""": ["""FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FunnelConfig"""], """convert_funnel_original_tf_checkpoint_to_pytorch""": [], """tokenization_funnel""": ["""FunnelTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Tuple = ["""FunnelTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : List[str] = [ """FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""", """FunnelBaseModel""", """FunnelForMaskedLM""", """FunnelForMultipleChoice""", """FunnelForPreTraining""", """FunnelForQuestionAnswering""", """FunnelForSequenceClassification""", """FunnelForTokenClassification""", """FunnelModel""", """FunnelPreTrainedModel""", """load_tf_weights_in_funnel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Optional[Any] = [ """TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFFunnelBaseModel""", """TFFunnelForMaskedLM""", """TFFunnelForMultipleChoice""", """TFFunnelForPreTraining""", """TFFunnelForQuestionAnswering""", """TFFunnelForSequenceClassification""", """TFFunnelForTokenClassification""", """TFFunnelModel""", """TFFunnelPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig from .tokenization_funnel import FunnelTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_funnel_fast import FunnelTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_funnel import ( FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, FunnelBaseModel, FunnelForMaskedLM, FunnelForMultipleChoice, FunnelForPreTraining, FunnelForQuestionAnswering, FunnelForSequenceClassification, FunnelForTokenClassification, FunnelModel, FunnelPreTrainedModel, load_tf_weights_in_funnel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_funnel import ( TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, TFFunnelPreTrainedModel, ) else: import sys lowercase : Any = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
20
0
'''simple docstring''' import os import tempfile import unittest import uuid from pathlib import Path from transformers.testing_utils import get_tests_dir, require_soundfile, require_torch, require_vision from transformers.tools.agent_types import AgentAudio, AgentImage, AgentText from transformers.utils import is_soundfile_availble, is_torch_available, is_vision_available if is_torch_available(): import torch if is_soundfile_availble(): import soundfile as sf if is_vision_available(): from PIL import Image def __snake_case ( UpperCAmelCase_ : str="" ): lowerCamelCase_ = tempfile.mkdtemp() return os.path.join(UpperCAmelCase_ , str(uuid.uuida() ) + suffix ) @require_soundfile @require_torch class snake_case ( unittest.TestCase ): """simple docstring""" def snake_case ( self ): """simple docstring""" lowerCamelCase_ = torch.rand(12 , dtype=torch.floataa ) - 0.5 lowerCamelCase_ = AgentAudio(UpperCamelCase ) lowerCamelCase_ = str(agent_type.to_string() ) # Ensure that the tensor and the agent_type's tensor are the same self.assertTrue(torch.allclose(UpperCamelCase , agent_type.to_raw() , atol=1e-4 ) ) del agent_type # Ensure the path remains even after the object deletion self.assertTrue(os.path.exists(UpperCamelCase ) ) # Ensure that the file contains the same value as the original tensor lowerCamelCase_ ,lowerCamelCase_ = sf.read(UpperCamelCase ) self.assertTrue(torch.allclose(UpperCamelCase , torch.tensor(UpperCamelCase ) , atol=1e-4 ) ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = torch.rand(12 , dtype=torch.floataa ) - 0.5 lowerCamelCase_ = get_new_path(suffix=".wav" ) sf.write(UpperCamelCase , UpperCamelCase , 1_6000 ) lowerCamelCase_ = AgentAudio(UpperCamelCase ) self.assertTrue(torch.allclose(UpperCamelCase , agent_type.to_raw() , atol=1e-4 ) ) self.assertEqual(agent_type.to_string() , UpperCamelCase ) @require_vision @require_torch class snake_case ( unittest.TestCase ): """simple docstring""" def snake_case ( self ): """simple docstring""" lowerCamelCase_ = torch.randint(0 , 256 , (64, 64, 3) ) lowerCamelCase_ = AgentImage(UpperCamelCase ) lowerCamelCase_ = str(agent_type.to_string() ) # Ensure that the tensor and the agent_type's tensor are the same self.assertTrue(torch.allclose(UpperCamelCase , agent_type._tensor , atol=1e-4 ) ) self.assertIsInstance(agent_type.to_raw() , Image.Image ) # Ensure the path remains even after the object deletion del agent_type self.assertTrue(os.path.exists(UpperCamelCase ) ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = Path(get_tests_dir("fixtures/tests_samples/COCO" ) ) / "000000039769.png" lowerCamelCase_ = Image.open(UpperCamelCase ) lowerCamelCase_ = AgentImage(UpperCamelCase ) self.assertTrue(path.samefile(agent_type.to_string() ) ) self.assertTrue(image == agent_type.to_raw() ) # Ensure the path remains even after the object deletion del agent_type self.assertTrue(os.path.exists(UpperCamelCase ) ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = Path(get_tests_dir("fixtures/tests_samples/COCO" ) ) / "000000039769.png" lowerCamelCase_ = Image.open(UpperCamelCase ) lowerCamelCase_ = AgentImage(UpperCamelCase ) self.assertFalse(path.samefile(agent_type.to_string() ) ) self.assertTrue(image == agent_type.to_raw() ) # Ensure the path remains even after the object deletion del agent_type self.assertTrue(os.path.exists(UpperCamelCase ) ) class snake_case ( unittest.TestCase ): """simple docstring""" def snake_case ( self ): """simple docstring""" lowerCamelCase_ = "Hey!" lowerCamelCase_ = AgentText(UpperCamelCase ) self.assertEqual(UpperCamelCase , agent_type.to_string() ) self.assertEqual(UpperCamelCase , agent_type.to_raw() ) self.assertEqual(UpperCamelCase , UpperCamelCase )
55
import os import zipfile import pytest from datasets.utils.extract import ( BzipaExtractor, Extractor, GzipExtractor, LzaExtractor, SevenZipExtractor, TarExtractor, XzExtractor, ZipExtractor, ZstdExtractor, ) from .utils import require_lza, require_pyazr, require_zstandard @pytest.mark.parametrize( """compression_format, is_archive""" , [ ("""7z""", True), ("""bz2""", False), ("""gzip""", False), ("""lz4""", False), ("""tar""", True), ("""xz""", False), ("""zip""", True), ("""zstd""", False), ] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) -> Any: lowercase : Dict = { """7z""": (seven_zip_file, SevenZipExtractor), """bz2""": (bza_file, BzipaExtractor), """gzip""": (gz_file, GzipExtractor), """lz4""": (lza_file, LzaExtractor), """tar""": (tar_file, TarExtractor), """xz""": (xz_file, XzExtractor), """zip""": (zip_file, ZipExtractor), """zstd""": (zstd_file, ZstdExtractor), } lowercase , lowercase : Optional[Any] = input_paths_and_base_extractors[compression_format] if input_path is None: lowercase : Dict = f"for '{compression_format}' compression_format, " if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(SCREAMING_SNAKE_CASE__ ) assert base_extractor.is_extractable(SCREAMING_SNAKE_CASE__ ) lowercase : Any = tmp_path / ("""extracted""" if is_archive else """extracted.txt""") base_extractor.extract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name lowercase : str = file_path.read_text(encoding="""utf-8""" ) else: lowercase : Optional[Any] = output_path.read_text(encoding="""utf-8""" ) lowercase : Tuple = text_file.read_text(encoding="""utf-8""" ) assert extracted_file_content == expected_file_content @pytest.mark.parametrize( """compression_format, is_archive""" , [ ("""7z""", True), ("""bz2""", False), ("""gzip""", False), ("""lz4""", False), ("""tar""", True), ("""xz""", False), ("""zip""", True), ("""zstd""", False), ] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) -> Dict: lowercase : str = { """7z""": seven_zip_file, """bz2""": bza_file, """gzip""": gz_file, """lz4""": lza_file, """tar""": tar_file, """xz""": xz_file, """zip""": zip_file, """zstd""": zstd_file, } lowercase : Optional[Any] = input_paths[compression_format] if input_path is None: lowercase : int = f"for '{compression_format}' compression_format, " if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = Extractor.infer_extractor_format(SCREAMING_SNAKE_CASE__ ) assert extractor_format is not None lowercase : Any = tmp_path / ("""extracted""" if is_archive else """extracted.txt""") Extractor.extract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name lowercase : Dict = file_path.read_text(encoding="""utf-8""" ) else: lowercase : int = output_path.read_text(encoding="""utf-8""" ) lowercase : Optional[Any] = text_file.read_text(encoding="""utf-8""" ) assert extracted_file_content == expected_file_content @pytest.fixture def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: import tarfile lowercase : Tuple = tmp_path / """data_dot_dot""" directory.mkdir() lowercase : str = directory / """tar_file_with_dot_dot.tar""" with tarfile.TarFile(SCREAMING_SNAKE_CASE__ , """w""" ) as f: f.add(SCREAMING_SNAKE_CASE__ , arcname=os.path.join("""..""" , text_file.name ) ) return path @pytest.fixture def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[str]: import tarfile lowercase : Tuple = tmp_path / """data_sym_link""" directory.mkdir() lowercase : int = directory / """tar_file_with_sym_link.tar""" os.symlink("""..""" , directory / """subdir""" , target_is_directory=SCREAMING_SNAKE_CASE__ ) with tarfile.TarFile(SCREAMING_SNAKE_CASE__ , """w""" ) as f: f.add(str(directory / """subdir""" ) , arcname="""subdir""" ) # str required by os.readlink on Windows and Python < 3.8 return path @pytest.mark.parametrize( """insecure_tar_file, error_log""" , [("""tar_file_with_dot_dot""", """illegal path"""), ("""tar_file_with_sym_link""", """Symlink""")] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: lowercase : List[Any] = { """tar_file_with_dot_dot""": tar_file_with_dot_dot, """tar_file_with_sym_link""": tar_file_with_sym_link, } lowercase : Optional[int] = insecure_tar_files[insecure_tar_file] lowercase : List[str] = tmp_path / """extracted""" TarExtractor.extract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) assert caplog.text for record in caplog.records: assert record.levelname == "ERROR" assert error_log in record.msg def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[int]: # We should have less false positives than zipfile.is_zipfile # We do that by checking only the magic number lowercase : Any = tmpdir / """not_a_zip_file""" # From: https://github.com/python/cpython/pull/5053 lowercase : str = ( B"""\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00""" B"""\x00\x02\x08\x06\x00\x00\x00\x99\x81\xb6'\x00\x00\x00\x15I""" B"""DATx\x01\x01\n\x00\xf5\xff\x00PK\x05\x06\x00PK\x06\x06\x07""" B"""\xac\x01N\xc6|a\r\x00\x00\x00\x00IEND\xaeB`\x82""" ) with not_a_zip_file.open("""wb""" ) as f: f.write(SCREAMING_SNAKE_CASE__ ) assert zipfile.is_zipfile(str(SCREAMING_SNAKE_CASE__ ) ) # is a false positive for `zipfile` assert not ZipExtractor.is_extractable(SCREAMING_SNAKE_CASE__ ) # but we're right
20
0
'''simple docstring''' a : Tuple = {'a': ['c', 'b'], 'b': ['d', 'e'], 'c': [], 'd': [], 'e': []} a : Optional[Any] = ['a', 'b', 'c', 'd', 'e'] def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase ) -> Any: '''simple docstring''' snake_case_ = start # add current to visited visited.append(__UpperCAmelCase ) snake_case_ = edges[current] for neighbor in neighbors: # if neighbor not in visited, visit if neighbor not in visited: snake_case_ = topological_sort(__UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase ) # if all neighbors visited add current to sort sort.append(__UpperCAmelCase ) # if all vertices haven't been visited select a new one to visit if len(__UpperCAmelCase ) != len(__UpperCAmelCase ): for vertice in vertices: if vertice not in visited: snake_case_ = topological_sort(__UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase ) # return sort return sort if __name__ == "__main__": a : int = topological_sort('a', [], []) print(sort)
56
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast from ...utils import logging lowercase : List[str] = logging.get_logger(__name__) lowercase : Optional[Any] = { """EleutherAI/gpt-neo-1.3B""": """https://huggingface.co/EleutherAI/gpt-neo-1.3B/resolve/main/config.json""", # See all GPTNeo models at https://huggingface.co/models?filter=gpt_neo } class __snake_case ( lowerCAmelCase ): _a : str= "gpt_neo" _a : Optional[int]= ["past_key_values"] _a : Dict= {"num_attention_heads": "num_heads", "num_hidden_layers": "num_layers"} def __init__( self ,snake_case=50257 ,snake_case=2048 ,snake_case=2048 ,snake_case=24 ,snake_case=[[["global", "local"], 12]] ,snake_case=16 ,snake_case=None ,snake_case=256 ,snake_case="gelu_new" ,snake_case=0.0 ,snake_case=0.0 ,snake_case=0.0 ,snake_case=0.1 ,snake_case=1e-5 ,snake_case=0.02 ,snake_case=True ,snake_case=50256 ,snake_case=50256 ,**snake_case ,): '''simple docstring''' lowercase : int = vocab_size lowercase : Union[str, Any] = max_position_embeddings lowercase : Dict = hidden_size lowercase : Union[str, Any] = num_layers lowercase : Union[str, Any] = num_heads lowercase : Optional[int] = intermediate_size lowercase : List[str] = window_size lowercase : Optional[int] = activation_function lowercase : List[str] = resid_dropout lowercase : int = embed_dropout lowercase : Optional[int] = attention_dropout lowercase : Tuple = classifier_dropout lowercase : Optional[int] = layer_norm_epsilon lowercase : Dict = initializer_range lowercase : List[str] = use_cache lowercase : Optional[int] = bos_token_id lowercase : int = eos_token_id lowercase : Union[str, Any] = attention_types lowercase : Dict = self.expand_attention_types_params(snake_case ) if len(self.attention_layers ) != self.num_layers: raise ValueError( """Configuration for convolutional module is incorrect. """ """It is required that `len(config.attention_layers)` == `config.num_layers` """ f"but is `len(config.attention_layers) = {len(self.attention_layers )}`, " f"`config.num_layers = {self.num_layers}`. " """`config.attention_layers` is prepared using `config.attention_types`. """ """Please verify the value of `config.attention_types` argument.""" ) super().__init__(bos_token_id=snake_case ,eos_token_id=snake_case ,**snake_case ) @staticmethod def _SCREAMING_SNAKE_CASE ( snake_case ): '''simple docstring''' lowercase : List[Any] = [] for item in attention_types: for _ in range(item[1] ): attentions.extend(item[0] ) return attentions def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: import torch lowercase : Tuple = input.size() lowercase : int = len(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = shape[dimension] lowercase : int = torch.arange(0 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = torch.div(sizedim - size , SCREAMING_SNAKE_CASE__ , rounding_mode="""floor""" ) + 1 lowercase : Dict = torch.arange(SCREAMING_SNAKE_CASE__ ) + low_indices[:min_length][:, None] lowercase : Union[str, Any] = [slice(SCREAMING_SNAKE_CASE__ )] * rank lowercase : Optional[Any] = indices lowercase : List[str] = input[s] lowercase : Optional[int] = list(range(0 , rank + 1 ) ) perm.append(perm.pop(dimension + 1 ) ) return sliced.permute(SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: import torch lowercase : Union[str, Any] = torch.arange(1 , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = torch.remainder(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = remainders == 0 lowercase : Optional[int] = candidates[divisor_indices] lowercase : List[Any] = torch.max(SCREAMING_SNAKE_CASE__ ) return largest_divisor, torch.div(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , rounding_mode="""floor""" ) class __snake_case ( lowerCAmelCase ): @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Dict = OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}} ) if self.use_past: self.fill_with_past_key_values_(snake_case ,direction="""inputs""" ) lowercase : Dict = {0: """batch""", 1: """past_sequence + sequence"""} else: lowercase : List[str] = {0: """batch""", 1: """sequence"""} return common_inputs @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return self._config.num_heads def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = -1 ,snake_case = -1 ,snake_case = False ,snake_case = None ,): '''simple docstring''' lowercase : Any = super(snake_case ,self ).generate_dummy_inputs( snake_case ,batch_size=snake_case ,seq_length=snake_case ,is_pair=snake_case ,framework=snake_case ) # We need to order the input in the way they appears in the forward() lowercase : List[str] = OrderedDict({"""input_ids""": common_inputs["""input_ids"""]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed.""" ) else: import torch lowercase , lowercase : List[Any] = common_inputs["""input_ids"""].shape # Not using the same length for past_key_values lowercase : Optional[int] = seqlen + 2 lowercase : int = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) lowercase : Optional[Any] = [ (torch.zeros(snake_case ), torch.zeros(snake_case )) for _ in range(self.num_layers ) ] lowercase : Optional[Any] = common_inputs["""attention_mask"""] if self.use_past: lowercase : Any = ordered_inputs["""attention_mask"""].dtype lowercase : Union[str, Any] = torch.cat( [ordered_inputs["""attention_mask"""], torch.ones(snake_case ,snake_case ,dtype=snake_case )] ,dim=1 ) return ordered_inputs @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return 13
20
0
"""simple docstring""" def _lowerCamelCase ( _UpperCamelCase ): '''simple docstring''' assert ( isinstance(_UpperCamelCase , _UpperCamelCase ) and number_of_steps > 0 ), f"number_of_steps needs to be positive integer, your input {number_of_steps}" if number_of_steps == 1: return 1 __lowerCAmelCase , __lowerCAmelCase = 1, 1 for _ in range(number_of_steps - 1 ): __lowerCAmelCase , __lowerCAmelCase = current + previous, current return current if __name__ == "__main__": import doctest doctest.testmod()
57
from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING lowercase : Any = logging.get_logger(__name__) @add_end_docstrings(lowerCAmelCase ) class __snake_case ( lowerCAmelCase ): def __init__( self ,*snake_case ,**snake_case ): '''simple docstring''' super().__init__(*snake_case ,**snake_case ) requires_backends(self ,"""vision""" ) self.check_model_type( TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING if self.framework == """tf""" else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING ) def _SCREAMING_SNAKE_CASE ( self ,snake_case=None ): '''simple docstring''' lowercase : List[Any] = {} if top_k is not None: lowercase : int = top_k return {}, {}, postprocess_params def __call__( self ,snake_case ,**snake_case ): '''simple docstring''' return super().__call__(snake_case ,**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Any = load_image(snake_case ) lowercase : List[Any] = self.image_processor(images=snake_case ,return_tensors=self.framework ) return model_inputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : int = self.model(**snake_case ) return model_outputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=5 ): '''simple docstring''' if top_k > self.model.config.num_labels: lowercase : Tuple = self.model.config.num_labels if self.framework == "pt": lowercase : str = model_outputs.logits.softmax(-1 )[0] lowercase , lowercase : Dict = probs.topk(snake_case ) elif self.framework == "tf": lowercase : Optional[int] = stable_softmax(model_outputs.logits ,axis=-1 )[0] lowercase : Union[str, Any] = tf.math.top_k(snake_case ,k=snake_case ) lowercase , lowercase : List[str] = topk.values.numpy(), topk.indices.numpy() else: raise ValueError(f"Unsupported framework: {self.framework}" ) lowercase : Tuple = scores.tolist() lowercase : Dict = ids.tolist() return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(snake_case ,snake_case )]
20
0
'''simple docstring''' import argparse import json from collections import OrderedDict from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import PoolFormerConfig, PoolFormerForImageClassification, PoolFormerImageProcessor from transformers.utils import logging logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) def lowerCamelCase ( __lowerCamelCase : str , __lowerCamelCase : Any , __lowerCamelCase : Optional[int] , __lowerCamelCase : Optional[Any] ) ->List[Any]: _SCREAMING_SNAKE_CASE = original_name.split(""".""" )[0] _SCREAMING_SNAKE_CASE = key.split(""".""" ) _SCREAMING_SNAKE_CASE = int(key_list[key_list.index(__lowerCamelCase ) - 2] ) _SCREAMING_SNAKE_CASE = int(key_list[key_list.index(__lowerCamelCase ) - 1] ) _SCREAMING_SNAKE_CASE = orig_block_num - offset _SCREAMING_SNAKE_CASE = key.replace(F'{orig_block_num}.{layer_num}.{original_name}' , F'block.{new_block_num}.{layer_num}.{new_name}' ) return key def lowerCamelCase ( __lowerCamelCase : Optional[Any] ) ->Any: _SCREAMING_SNAKE_CASE = OrderedDict() _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 0, 0 for key, value in state_dict.items(): if key.startswith("""network""" ): _SCREAMING_SNAKE_CASE = key.replace("""network""" , """poolformer.encoder""" ) if "proj" in key: # Works for the first embedding as well as the internal embedding layers if key.endswith("""bias""" ) and "patch_embed" not in key: patch_emb_offset += 1 _SCREAMING_SNAKE_CASE = key[: key.find("""proj""" )] _SCREAMING_SNAKE_CASE = key.replace(__lowerCamelCase , F'patch_embeddings.{total_embed_found}.' ) _SCREAMING_SNAKE_CASE = key.replace("""proj""" , """projection""" ) if key.endswith("""bias""" ): total_embed_found += 1 if "patch_embeddings" in key: _SCREAMING_SNAKE_CASE = """poolformer.encoder.""" + key if "mlp.fc1" in key: _SCREAMING_SNAKE_CASE = replace_key_with_offset(__lowerCamelCase , __lowerCamelCase , """mlp.fc1""" , """output.conv1""" ) if "mlp.fc2" in key: _SCREAMING_SNAKE_CASE = replace_key_with_offset(__lowerCamelCase , __lowerCamelCase , """mlp.fc2""" , """output.conv2""" ) if "norm1" in key: _SCREAMING_SNAKE_CASE = replace_key_with_offset(__lowerCamelCase , __lowerCamelCase , """norm1""" , """before_norm""" ) if "norm2" in key: _SCREAMING_SNAKE_CASE = replace_key_with_offset(__lowerCamelCase , __lowerCamelCase , """norm2""" , """after_norm""" ) if "layer_scale_1" in key: _SCREAMING_SNAKE_CASE = replace_key_with_offset(__lowerCamelCase , __lowerCamelCase , """layer_scale_1""" , """layer_scale_1""" ) if "layer_scale_2" in key: _SCREAMING_SNAKE_CASE = replace_key_with_offset(__lowerCamelCase , __lowerCamelCase , """layer_scale_2""" , """layer_scale_2""" ) if "head" in key: _SCREAMING_SNAKE_CASE = key.replace("""head""" , """classifier""" ) _SCREAMING_SNAKE_CASE = value return new_state_dict def lowerCamelCase ( ) ->int: _SCREAMING_SNAKE_CASE = """http://images.cocodataset.org/val2017/000000039769.jpg""" _SCREAMING_SNAKE_CASE = Image.open(requests.get(__lowerCamelCase , stream=__lowerCamelCase ).raw ) return image @torch.no_grad() def lowerCamelCase ( __lowerCamelCase : int , __lowerCamelCase : Tuple , __lowerCamelCase : int ) ->Optional[Any]: _SCREAMING_SNAKE_CASE = PoolFormerConfig() # set attributes based on model_name _SCREAMING_SNAKE_CASE = """huggingface/label-files""" _SCREAMING_SNAKE_CASE = model_name[-3:] _SCREAMING_SNAKE_CASE = 1000 _SCREAMING_SNAKE_CASE = """imagenet-1k-id2label.json""" _SCREAMING_SNAKE_CASE = (1, 1000) # set config attributes _SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(__lowerCamelCase , __lowerCamelCase , repo_type="""dataset""" ) , """r""" ) ) _SCREAMING_SNAKE_CASE = {int(__lowerCamelCase ): v for k, v in idalabel.items()} _SCREAMING_SNAKE_CASE = idalabel _SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} if size == "s12": _SCREAMING_SNAKE_CASE = [2, 2, 6, 2] _SCREAMING_SNAKE_CASE = [64, 128, 320, 512] _SCREAMING_SNAKE_CASE = 4.0 _SCREAMING_SNAKE_CASE = 0.9 elif size == "s24": _SCREAMING_SNAKE_CASE = [4, 4, 12, 4] _SCREAMING_SNAKE_CASE = [64, 128, 320, 512] _SCREAMING_SNAKE_CASE = 4.0 _SCREAMING_SNAKE_CASE = 0.9 elif size == "s36": _SCREAMING_SNAKE_CASE = [6, 6, 18, 6] _SCREAMING_SNAKE_CASE = [64, 128, 320, 512] _SCREAMING_SNAKE_CASE = 4.0 _SCREAMING_SNAKE_CASE = 1e-6 _SCREAMING_SNAKE_CASE = 0.9 elif size == "m36": _SCREAMING_SNAKE_CASE = [6, 6, 18, 6] _SCREAMING_SNAKE_CASE = [96, 192, 384, 768] _SCREAMING_SNAKE_CASE = 4.0 _SCREAMING_SNAKE_CASE = 1e-6 _SCREAMING_SNAKE_CASE = 0.95 elif size == "m48": _SCREAMING_SNAKE_CASE = [8, 8, 24, 8] _SCREAMING_SNAKE_CASE = [96, 192, 384, 768] _SCREAMING_SNAKE_CASE = 4.0 _SCREAMING_SNAKE_CASE = 1e-6 _SCREAMING_SNAKE_CASE = 0.95 else: raise ValueError(F'Size {size} not supported' ) # load image processor _SCREAMING_SNAKE_CASE = PoolFormerImageProcessor(crop_pct=__lowerCamelCase ) # Prepare image _SCREAMING_SNAKE_CASE = prepare_img() _SCREAMING_SNAKE_CASE = image_processor(images=__lowerCamelCase , return_tensors="""pt""" ).pixel_values logger.info(F'Converting model {model_name}...' ) # load original state dict _SCREAMING_SNAKE_CASE = torch.load(__lowerCamelCase , map_location=torch.device("""cpu""" ) ) # rename keys _SCREAMING_SNAKE_CASE = rename_keys(__lowerCamelCase ) # create HuggingFace model and load state dict _SCREAMING_SNAKE_CASE = PoolFormerForImageClassification(__lowerCamelCase ) model.load_state_dict(__lowerCamelCase ) model.eval() # Define image processor _SCREAMING_SNAKE_CASE = PoolFormerImageProcessor(crop_pct=__lowerCamelCase ) _SCREAMING_SNAKE_CASE = image_processor(images=prepare_img() , return_tensors="""pt""" ).pixel_values # forward pass _SCREAMING_SNAKE_CASE = model(__lowerCamelCase ) _SCREAMING_SNAKE_CASE = outputs.logits # define expected logit slices for different models if size == "s12": _SCREAMING_SNAKE_CASE = torch.tensor([-0.3045, -0.6758, -0.4869] ) elif size == "s24": _SCREAMING_SNAKE_CASE = torch.tensor([0.4402, -0.1374, -0.8045] ) elif size == "s36": _SCREAMING_SNAKE_CASE = torch.tensor([-0.6080, -0.5133, -0.5898] ) elif size == "m36": _SCREAMING_SNAKE_CASE = torch.tensor([0.3952, 0.2263, -1.2668] ) elif size == "m48": _SCREAMING_SNAKE_CASE = torch.tensor([0.1167, -0.0656, -0.3423] ) else: raise ValueError(F'Size {size} not supported' ) # verify logits assert logits.shape == expected_shape assert torch.allclose(logits[0, :3] , __lowerCamelCase , atol=1e-2 ) # finally, save model and image processor logger.info(F'Saving PyTorch model and image processor to {pytorch_dump_folder_path}...' ) Path(__lowerCamelCase ).mkdir(exist_ok=__lowerCamelCase ) model.save_pretrained(__lowerCamelCase ) print(F'Saving image processor to {pytorch_dump_folder_path}' ) image_processor.save_pretrained(__lowerCamelCase ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument( """--model_name""", default="""poolformer_s12""", type=str, help="""Name of the model you'd like to convert.""", ) parser.add_argument( """--checkpoint_path""", default=None, type=str, help="""Path to the original PyTorch checkpoint (.pth file).""" ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the folder to output PyTorch model.""" ) lowercase_ = parser.parse_args() convert_poolformer_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path)
58
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class __snake_case : def __init__( self ,snake_case ,): '''simple docstring''' lowercase : Any = parent lowercase : Tuple = 13 lowercase : str = 7 lowercase : Dict = True lowercase : Dict = True lowercase : str = True lowercase : List[str] = True lowercase : int = True lowercase : Union[str, Any] = False lowercase : Dict = False lowercase : List[Any] = False lowercase : List[Any] = 2 lowercase : Optional[Any] = 99 lowercase : int = 0 lowercase : Tuple = 32 lowercase : int = 2 lowercase : Tuple = 4 lowercase : List[Any] = 0.1 lowercase : Tuple = 0.1 lowercase : List[Any] = 512 lowercase : int = 16 lowercase : Dict = 2 lowercase : int = 0.02 lowercase : Union[str, Any] = 3 lowercase : Any = 4 lowercase : List[Any] = """last""" lowercase : Tuple = True lowercase : List[Any] = None lowercase : Any = 0 def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) lowercase : List[str] = random_attention_mask([self.batch_size, self.seq_length] ,dtype=tf.floataa ) lowercase : Tuple = None if self.use_input_lengths: lowercase : List[str] = ( ids_tensor([self.batch_size] ,vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length lowercase : Tuple = None if self.use_token_type_ids: lowercase : Optional[int] = ids_tensor([self.batch_size, self.seq_length] ,self.n_langs ) lowercase : List[str] = None lowercase : List[str] = None lowercase : Optional[Any] = None if self.use_labels: lowercase : List[str] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) lowercase : List[str] = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) lowercase : str = ids_tensor([self.batch_size] ,2 ,dtype=tf.floataa ) lowercase : Optional[Any] = ids_tensor([self.batch_size] ,self.num_choices ) lowercase : str = FlaubertConfig( vocab_size=self.vocab_size ,n_special=self.n_special ,emb_dim=self.hidden_size ,n_layers=self.num_hidden_layers ,n_heads=self.num_attention_heads ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,gelu_activation=self.gelu_activation ,sinusoidal_embeddings=self.sinusoidal_embeddings ,asm=self.asm ,causal=self.causal ,n_langs=self.n_langs ,max_position_embeddings=self.max_position_embeddings ,initializer_range=self.initializer_range ,summary_type=self.summary_type ,use_proj=self.use_proj ,bos_token_id=self.bos_token_id ,) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Tuple = TFFlaubertModel(config=snake_case ) lowercase : str = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} lowercase : Optional[Any] = model(snake_case ) lowercase : List[Any] = [input_ids, input_mask] lowercase : int = model(snake_case ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : List[Any] = TFFlaubertWithLMHeadModel(snake_case ) lowercase : Optional[Any] = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} lowercase : int = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Tuple = TFFlaubertForQuestionAnsweringSimple(snake_case ) lowercase : Union[str, Any] = {"""input_ids""": input_ids, """lengths""": input_lengths} lowercase : Tuple = model(snake_case ) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Union[str, Any] = TFFlaubertForSequenceClassification(snake_case ) lowercase : str = {"""input_ids""": input_ids, """lengths""": input_lengths} lowercase : str = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Any = self.num_labels lowercase : List[str] = TFFlaubertForTokenClassification(config=snake_case ) lowercase : Dict = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} lowercase : int = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Any = self.num_choices lowercase : Dict = TFFlaubertForMultipleChoice(config=snake_case ) lowercase : Any = tf.tile(tf.expand_dims(snake_case ,1 ) ,(1, self.num_choices, 1) ) lowercase : Optional[Any] = tf.tile(tf.expand_dims(snake_case ,1 ) ,(1, self.num_choices, 1) ) lowercase : Dict = tf.tile(tf.expand_dims(snake_case ,1 ) ,(1, self.num_choices, 1) ) lowercase : Union[str, Any] = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } lowercase : int = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_choices) ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Any = self.prepare_config_and_inputs() ( ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ) : int = config_and_inputs lowercase : List[str] = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """langs""": token_type_ids, """lengths""": input_lengths, } return config, inputs_dict @require_tf class __snake_case ( lowerCAmelCase , lowerCAmelCase , unittest.TestCase ): _a : Dict= ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) _a : Optional[Any]= ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable _a : Any= ( { "feature-extraction": TFFlaubertModel, "fill-mask": TFFlaubertWithLMHeadModel, "question-answering": TFFlaubertForQuestionAnsweringSimple, "text-classification": TFFlaubertForSequenceClassification, "token-classification": TFFlaubertForTokenClassification, "zero-shot": TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) _a : Tuple= False _a : int= False def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ): '''simple docstring''' if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("""Fast""" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : str = TFFlaubertModelTester(self ) lowercase : List[Any] = ConfigTester(self ,config_class=snake_case ,emb_dim=37 ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' self.config_tester.run_common_tests() def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*snake_case ) @slow def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase : Dict = TFFlaubertModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) @require_tf @require_sentencepiece @require_tokenizers class __snake_case ( unittest.TestCase ): @slow def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased""" ) lowercase : int = tf.convert_to_tensor( [[0, 158, 735, 2592, 1424, 6727, 82, 1]] ,dtype=tf.intaa ,) # "J'aime flaubert !" lowercase : Dict = model(snake_case )[0] lowercase : Union[str, Any] = tf.TensorShape((1, 8, 512) ) self.assertEqual(output.shape ,snake_case ) # compare the actual values for a slice. lowercase : Tuple = tf.convert_to_tensor( [ [ [-1.8_768_773, -1.566_555, 0.27_072_418], [-1.6_920_038, -0.5_873_505, 1.9_329_599], [-2.9_563_985, -1.6_993_835, 1.7_972_052], ] ] ,dtype=tf.floataa ,) self.assertTrue(np.allclose(output[:, :3, :3].numpy() ,expected_slice.numpy() ,atol=1e-4 ) )
20
0
import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( "kwargs, expected" , [ ({"num_shards": 0, "max_num_jobs": 1}, []), ({"num_shards": 10, "max_num_jobs": 1}, [range(10 )]), ({"num_shards": 10, "max_num_jobs": 10}, [range(__lowerCamelCase , i + 1 ) for i in range(10 )]), ({"num_shards": 1, "max_num_jobs": 10}, [range(1 )]), ({"num_shards": 10, "max_num_jobs": 3}, [range(0 , 4 ), range(4 , 7 ), range(7 , 10 )]), ({"num_shards": 3, "max_num_jobs": 10}, [range(0 , 1 ), range(1 , 2 ), range(2 , 3 )]), ] , ) def UpperCamelCase ( __lowerCamelCase : Optional[Any] , __lowerCamelCase : Any ): snake_case : Union[str, Any] = _distribute_shards(**__lowerCamelCase ) assert out == expected @pytest.mark.parametrize( "gen_kwargs, max_num_jobs, expected" , [ ({"foo": 0}, 10, [{"foo": 0}]), ({"shards": [0, 1, 2, 3]}, 1, [{"shards": [0, 1, 2, 3]}]), ({"shards": [0, 1, 2, 3]}, 4, [{"shards": [0]}, {"shards": [1]}, {"shards": [2]}, {"shards": [3]}]), ({"shards": [0, 1]}, 4, [{"shards": [0]}, {"shards": [1]}]), ({"shards": [0, 1, 2, 3]}, 2, [{"shards": [0, 1]}, {"shards": [2, 3]}]), ] , ) def UpperCamelCase ( __lowerCamelCase : Dict , __lowerCamelCase : str , __lowerCamelCase : Dict ): snake_case : str = _split_gen_kwargs(__lowerCamelCase , __lowerCamelCase ) assert out == expected @pytest.mark.parametrize( "gen_kwargs, expected" , [ ({"foo": 0}, 1), ({"shards": [0]}, 1), ({"shards": [0, 1, 2, 3]}, 4), ({"shards": [0, 1, 2, 3], "foo": 0}, 4), ({"shards": [0, 1, 2, 3], "other": (0, 1)}, 4), ({"shards": [0, 1, 2, 3], "shards2": [0, 1]}, RuntimeError), ] , ) def UpperCamelCase ( __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Optional[int] ): if expected is RuntimeError: with pytest.raises(__lowerCamelCase ): _number_of_shards_in_gen_kwargs(__lowerCamelCase ) else: snake_case : Optional[Any] = _number_of_shards_in_gen_kwargs(__lowerCamelCase ) assert out == expected
59
import json import os from dataclasses import dataclass from functools import partial from typing import Callable import flax.linen as nn import jax import jax.numpy as jnp import joblib import optax import wandb from flax import jax_utils, struct, traverse_util from flax.serialization import from_bytes, to_bytes from flax.training import train_state from flax.training.common_utils import shard from tqdm.auto import tqdm from transformers import BigBirdConfig, FlaxBigBirdForQuestionAnswering from transformers.models.big_bird.modeling_flax_big_bird import FlaxBigBirdForQuestionAnsweringModule class __snake_case ( lowerCAmelCase ): _a : BigBirdConfig _a : jnp.dtype= jnp.floataa _a : bool= True def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' super().setup() lowercase : List[str] = nn.Dense(5 ,dtype=self.dtype ) def __call__( self ,*snake_case ,**snake_case ): '''simple docstring''' lowercase : int = super().__call__(*snake_case ,**snake_case ) lowercase : Any = self.cls(outputs[2] ) return outputs[:2] + (cls_out,) class __snake_case ( lowerCAmelCase ): _a : List[Any]= FlaxBigBirdForNaturalQuestionsModule def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: def cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None ): lowercase : int = logits.shape[-1] lowercase : Dict = (labels[..., None] == jnp.arange(SCREAMING_SNAKE_CASE__ )[None]).astype("""f4""" ) lowercase : Any = jax.nn.log_softmax(SCREAMING_SNAKE_CASE__ , axis=-1 ) lowercase : Optional[Any] = -jnp.sum(labels * logits , axis=-1 ) if reduction is not None: lowercase : Any = reduction(SCREAMING_SNAKE_CASE__ ) return loss lowercase : Optional[Any] = partial(SCREAMING_SNAKE_CASE__ , reduction=jnp.mean ) lowercase : Optional[int] = cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Dict = cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : int = cross_entropy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return (start_loss + end_loss + pooled_loss) / 3 @dataclass class __snake_case : _a : str= "google/bigbird-roberta-base" _a : int= 3000 _a : int= 1_0500 _a : int= 128 _a : int= 3 _a : int= 1 _a : int= 5 # tx_args _a : float= 3E-5 _a : float= 0.0 _a : int= 2_0000 _a : float= 0.00_95 _a : str= "bigbird-roberta-natural-questions" _a : str= "training-expt" _a : str= "data/nq-training.jsonl" _a : str= "data/nq-validation.jsonl" def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' os.makedirs(self.base_dir ,exist_ok=snake_case ) lowercase : Optional[int] = os.path.join(self.base_dir ,self.save_dir ) lowercase : Optional[int] = self.batch_size_per_device * jax.device_count() @dataclass class __snake_case : _a : int _a : int= 4096 # no dynamic padding on TPUs def __call__( self ,snake_case ): '''simple docstring''' lowercase : int = self.collate_fn(snake_case ) lowercase : Union[str, Any] = jax.tree_util.tree_map(snake_case ,snake_case ) return batch def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase , lowercase : Union[str, Any] = self.fetch_inputs(features["""input_ids"""] ) lowercase : Tuple = { """input_ids""": jnp.array(snake_case ,dtype=jnp.intaa ), """attention_mask""": jnp.array(snake_case ,dtype=jnp.intaa ), """start_labels""": jnp.array(features["""start_token"""] ,dtype=jnp.intaa ), """end_labels""": jnp.array(features["""end_token"""] ,dtype=jnp.intaa ), """pooled_labels""": jnp.array(features["""category"""] ,dtype=jnp.intaa ), } return batch def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Tuple = [self._fetch_inputs(snake_case ) for ids in input_ids] return zip(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Union[str, Any] = [1 for _ in range(len(snake_case ) )] while len(snake_case ) < self.max_length: input_ids.append(self.pad_id ) attention_mask.append(0 ) return input_ids, attention_mask def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None ) -> Any: if seed is not None: lowercase : Optional[int] = dataset.shuffle(seed=SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) // batch_size ): lowercase : Optional[Any] = dataset[i * batch_size : (i + 1) * batch_size] yield dict(SCREAMING_SNAKE_CASE__ ) @partial(jax.pmap , axis_name="""batch""" ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) -> List[Any]: def loss_fn(SCREAMING_SNAKE_CASE__ ): lowercase : List[str] = model_inputs.pop("""start_labels""" ) lowercase : Optional[int] = model_inputs.pop("""end_labels""" ) lowercase : str = model_inputs.pop("""pooled_labels""" ) lowercase : Union[str, Any] = state.apply_fn(**SCREAMING_SNAKE_CASE__ , params=SCREAMING_SNAKE_CASE__ , dropout_rng=SCREAMING_SNAKE_CASE__ , train=SCREAMING_SNAKE_CASE__ ) lowercase , lowercase , lowercase : List[str] = outputs return state.loss_fn( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) lowercase , lowercase : int = jax.random.split(SCREAMING_SNAKE_CASE__ ) lowercase : Dict = jax.value_and_grad(SCREAMING_SNAKE_CASE__ ) lowercase , lowercase : Union[str, Any] = grad_fn(state.params ) lowercase : List[Any] = jax.lax.pmean({"""loss""": loss} , axis_name="""batch""" ) lowercase : List[Any] = jax.lax.pmean(SCREAMING_SNAKE_CASE__ , """batch""" ) lowercase : str = state.apply_gradients(grads=SCREAMING_SNAKE_CASE__ ) return state, metrics, new_drp_rng @partial(jax.pmap , axis_name="""batch""" ) def _snake_case( SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : int = model_inputs.pop("""start_labels""" ) lowercase : Dict = model_inputs.pop("""end_labels""" ) lowercase : Optional[Any] = model_inputs.pop("""pooled_labels""" ) lowercase : Optional[int] = state.apply_fn(**SCREAMING_SNAKE_CASE__ , params=state.params , train=SCREAMING_SNAKE_CASE__ ) lowercase , lowercase , lowercase : List[Any] = outputs lowercase : Dict = state.loss_fn(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : str = jax.lax.pmean({"""loss""": loss} , axis_name="""batch""" ) return metrics class __snake_case ( train_state.TrainState ): _a : Callable= struct.field(pytree_node=lowerCAmelCase ) @dataclass class __snake_case : _a : Args _a : Callable _a : Callable _a : Callable _a : Callable _a : wandb _a : Callable= None def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case=None ): '''simple docstring''' lowercase : Tuple = model.params lowercase : Any = TrainState.create( apply_fn=model.__call__ ,params=snake_case ,tx=snake_case ,loss_fn=snake_case ,) if ckpt_dir is not None: lowercase , lowercase , lowercase , lowercase , lowercase : Tuple = restore_checkpoint(snake_case ,snake_case ) lowercase : List[str] = { """lr""": args.lr, """init_lr""": args.init_lr, """warmup_steps""": args.warmup_steps, """num_train_steps""": num_train_steps, """weight_decay""": args.weight_decay, } lowercase , lowercase : Tuple = build_tx(**snake_case ) lowercase : str = train_state.TrainState( step=snake_case ,apply_fn=model.__call__ ,params=snake_case ,tx=snake_case ,opt_state=snake_case ,) lowercase : Any = args lowercase : Optional[Any] = data_collator lowercase : List[str] = lr lowercase : str = params lowercase : Tuple = jax_utils.replicate(snake_case ) return state def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ): '''simple docstring''' lowercase : Dict = self.args lowercase : Optional[Any] = len(snake_case ) // args.batch_size lowercase : int = jax.random.PRNGKey(0 ) lowercase : List[str] = jax.random.split(snake_case ,jax.device_count() ) for epoch in range(args.max_epochs ): lowercase : List[Any] = jnp.array(0 ,dtype=jnp.floataa ) lowercase : List[str] = get_batched_dataset(snake_case ,args.batch_size ,seed=snake_case ) lowercase : int = 0 for batch in tqdm(snake_case ,total=snake_case ,desc=f"Running EPOCH-{epoch}" ): lowercase : Dict = self.data_collator(snake_case ) lowercase , lowercase , lowercase : Optional[int] = self.train_step_fn(snake_case ,snake_case ,**snake_case ) running_loss += jax_utils.unreplicate(metrics["""loss"""] ) i += 1 if i % args.logging_steps == 0: lowercase : Optional[Any] = jax_utils.unreplicate(state.step ) lowercase : List[str] = running_loss.item() / i lowercase : List[str] = self.scheduler_fn(state_step - 1 ) lowercase : int = self.evaluate(snake_case ,snake_case ) lowercase : Tuple = { """step""": state_step.item(), """eval_loss""": eval_loss.item(), """tr_loss""": tr_loss, """lr""": lr.item(), } tqdm.write(str(snake_case ) ) self.logger.log(snake_case ,commit=snake_case ) if i % args.save_steps == 0: self.save_checkpoint(args.save_dir + f"-e{epoch}-s{i}" ,state=snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : List[str] = get_batched_dataset(snake_case ,self.args.batch_size ) lowercase : Any = len(snake_case ) // self.args.batch_size lowercase : List[Any] = jnp.array(0 ,dtype=jnp.floataa ) lowercase : Optional[int] = 0 for batch in tqdm(snake_case ,total=snake_case ,desc="""Evaluating ... """ ): lowercase : Tuple = self.data_collator(snake_case ) lowercase : Optional[int] = self.val_step_fn(snake_case ,**snake_case ) running_loss += jax_utils.unreplicate(metrics["""loss"""] ) i += 1 return running_loss / i def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : str = jax_utils.unreplicate(snake_case ) print(f"SAVING CHECKPOINT IN {save_dir}" ,end=""" ... """ ) self.model_save_fn(snake_case ,params=state.params ) with open(os.path.join(snake_case ,"""opt_state.msgpack""" ) ,"""wb""" ) as f: f.write(to_bytes(state.opt_state ) ) joblib.dump(self.args ,os.path.join(snake_case ,"""args.joblib""" ) ) joblib.dump(self.data_collator ,os.path.join(snake_case ,"""data_collator.joblib""" ) ) with open(os.path.join(snake_case ,"""training_state.json""" ) ,"""w""" ) as f: json.dump({"""step""": state.step.item()} ,snake_case ) print("""DONE""" ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: print(f"RESTORING CHECKPOINT FROM {save_dir}" , end=""" ... """ ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , """flax_model.msgpack""" ) , """rb""" ) as f: lowercase : str = from_bytes(state.params , f.read() ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , """opt_state.msgpack""" ) , """rb""" ) as f: lowercase : Optional[int] = from_bytes(state.opt_state , f.read() ) lowercase : Optional[Any] = joblib.load(os.path.join(SCREAMING_SNAKE_CASE__ , """args.joblib""" ) ) lowercase : int = joblib.load(os.path.join(SCREAMING_SNAKE_CASE__ , """data_collator.joblib""" ) ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , """training_state.json""" ) , """r""" ) as f: lowercase : Tuple = json.load(SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = training_state["""step"""] print("""DONE""" ) return params, opt_state, step, args, data_collator def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: lowercase : List[str] = num_train_steps - warmup_steps lowercase : Dict = optax.linear_schedule(init_value=SCREAMING_SNAKE_CASE__ , end_value=SCREAMING_SNAKE_CASE__ , transition_steps=SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = optax.linear_schedule(init_value=SCREAMING_SNAKE_CASE__ , end_value=1e-7 , transition_steps=SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = optax.join_schedules(schedules=[warmup_fn, decay_fn] , boundaries=[warmup_steps] ) return lr def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: def weight_decay_mask(SCREAMING_SNAKE_CASE__ ): lowercase : List[Any] = traverse_util.flatten_dict(SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = {k: (v[-1] != """bias""" and v[-2:] != ("""LayerNorm""", """scale""")) for k, v in params.items()} return traverse_util.unflatten_dict(SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = scheduler_fn(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = optax.adamw(learning_rate=SCREAMING_SNAKE_CASE__ , weight_decay=SCREAMING_SNAKE_CASE__ , mask=SCREAMING_SNAKE_CASE__ ) return tx, lr
20
0
"""simple docstring""" snake_case__ : str = [ 999, 800, 799, 600, 599, 500, 400, 399, 377, 355, 333, 311, 288, 266, 244, 222, 200, 199, 177, 155, 133, 111, 88, 66, 44, 22, 0, ] snake_case__ : Optional[Any] = [ 999, 976, 952, 928, 905, 882, 858, 857, 810, 762, 715, 714, 572, 429, 428, 286, 285, 238, 190, 143, 142, 118, 95, 71, 47, 24, 0, ] snake_case__ : Any = [ 999, 988, 977, 966, 955, 944, 933, 922, 911, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 350, 300, 299, 266, 233, 200, 199, 179, 159, 140, 120, 100, 99, 88, 77, 66, 55, 44, 33, 22, 11, 0, ] snake_case__ : Optional[Any] = [ 999, 995, 992, 989, 985, 981, 978, 975, 971, 967, 964, 961, 957, 956, 951, 947, 942, 937, 933, 928, 923, 919, 914, 913, 908, 903, 897, 892, 887, 881, 876, 871, 870, 864, 858, 852, 846, 840, 834, 828, 827, 820, 813, 806, 799, 792, 785, 784, 777, 770, 763, 756, 749, 742, 741, 733, 724, 716, 707, 699, 698, 688, 677, 666, 656, 655, 645, 634, 623, 613, 612, 598, 584, 570, 569, 555, 541, 527, 526, 505, 484, 483, 462, 440, 439, 396, 395, 352, 351, 308, 307, 264, 263, 220, 219, 176, 132, 88, 44, 0, ] snake_case__ : int = [ 999, 997, 995, 992, 990, 988, 986, 984, 981, 979, 977, 975, 972, 970, 968, 966, 964, 961, 959, 957, 956, 954, 951, 949, 946, 944, 941, 939, 936, 934, 931, 929, 926, 924, 921, 919, 916, 914, 913, 910, 907, 905, 902, 899, 896, 893, 891, 888, 885, 882, 879, 877, 874, 871, 870, 867, 864, 861, 858, 855, 852, 849, 846, 843, 840, 837, 834, 831, 828, 827, 824, 821, 817, 814, 811, 808, 804, 801, 798, 795, 791, 788, 785, 784, 780, 777, 774, 770, 766, 763, 760, 756, 752, 749, 746, 742, 741, 737, 733, 730, 726, 722, 718, 714, 710, 707, 703, 699, 698, 694, 690, 685, 681, 677, 673, 669, 664, 660, 656, 655, 650, 646, 641, 636, 632, 627, 622, 618, 613, 612, 607, 602, 596, 591, 586, 580, 575, 570, 569, 563, 557, 551, 545, 539, 533, 527, 526, 519, 512, 505, 498, 491, 484, 483, 474, 466, 457, 449, 440, 439, 428, 418, 407, 396, 395, 381, 366, 352, 351, 330, 308, 307, 286, 264, 263, 242, 220, 219, 176, 175, 132, 131, 88, 44, 0, ] snake_case__ : Union[str, Any] = [ 999, 991, 982, 974, 966, 958, 950, 941, 933, 925, 916, 908, 900, 899, 874, 850, 825, 800, 799, 700, 600, 500, 400, 300, 200, 100, 0, ] snake_case__ : List[Any] = [ 999, 992, 985, 978, 971, 964, 957, 949, 942, 935, 928, 921, 914, 907, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 300, 299, 200, 199, 100, 99, 0, ] snake_case__ : Optional[int] = [ 999, 996, 992, 989, 985, 982, 979, 975, 972, 968, 965, 961, 958, 955, 951, 948, 944, 941, 938, 934, 931, 927, 924, 920, 917, 914, 910, 907, 903, 900, 899, 891, 884, 876, 869, 861, 853, 846, 838, 830, 823, 815, 808, 800, 799, 788, 777, 766, 755, 744, 733, 722, 711, 700, 699, 688, 677, 666, 655, 644, 633, 622, 611, 600, 599, 585, 571, 557, 542, 528, 514, 500, 499, 485, 471, 457, 442, 428, 414, 400, 399, 379, 359, 340, 320, 300, 299, 279, 259, 240, 220, 200, 199, 166, 133, 100, 99, 66, 33, 0, ]
60
from math import sqrt def _snake_case( SCREAMING_SNAKE_CASE__ ) -> bool: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number >= 0 ), "'number' must been an int and positive" lowercase : Union[str, Any] = True # 0 and 1 are none primes. if number <= 1: lowercase : str = False for divisor in range(2 , int(round(sqrt(SCREAMING_SNAKE_CASE__ ) ) ) + 1 ): # if 'number' divisible by 'divisor' then sets 'status' # of false and break up the loop. if number % divisor == 0: lowercase : Any = False break # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'status' must been from type bool" return status def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n > 2), "'N' must been an int and > 2" # beginList: contains all natural numbers from 2 up to N lowercase : str = list(range(2 , n + 1 ) ) lowercase : Tuple = [] # this list will be returns. # actual sieve of erathostenes for i in range(len(SCREAMING_SNAKE_CASE__ ) ): for j in range(i + 1 , len(SCREAMING_SNAKE_CASE__ ) ): if (begin_list[i] != 0) and (begin_list[j] % begin_list[i] == 0): lowercase : Tuple = 0 # filters actual prime numbers. lowercase : int = [x for x in begin_list if x != 0] # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type list" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n > 2), "'N' must been an int and > 2" lowercase : Dict = [] # iterates over all numbers between 2 up to N+1 # if a number is prime then appends to list 'ans' for number in range(2 , n + 1 ): if is_prime(SCREAMING_SNAKE_CASE__ ): ans.append(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type list" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Tuple: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and number >= 0, "'number' must been an int and >= 0" lowercase : Tuple = [] # this list will be returns of the function. # potential prime number factors. lowercase : Optional[Any] = 2 lowercase : Any = number if number == 0 or number == 1: ans.append(SCREAMING_SNAKE_CASE__ ) # if 'number' not prime then builds the prime factorization of 'number' elif not is_prime(SCREAMING_SNAKE_CASE__ ): while quotient != 1: if is_prime(SCREAMING_SNAKE_CASE__ ) and (quotient % factor == 0): ans.append(SCREAMING_SNAKE_CASE__ ) quotient /= factor else: factor += 1 else: ans.append(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type list" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number >= 0 ), "'number' bust been an int and >= 0" lowercase : Tuple = 0 # prime factorization of 'number' lowercase : Optional[int] = prime_factorization(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = max(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type int" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> str: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number >= 0 ), "'number' bust been an int and >= 0" lowercase : Union[str, Any] = 0 # prime factorization of 'number' lowercase : Tuple = prime_factorization(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = min(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'ans' must been from type int" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'number' must been an int" assert isinstance(number % 2 == 0 , SCREAMING_SNAKE_CASE__ ), "compare bust been from type bool" return number % 2 == 0 def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), "'number' must been an int" assert isinstance(number % 2 != 0 , SCREAMING_SNAKE_CASE__ ), "compare bust been from type bool" return number % 2 != 0 def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (number > 2) and is_even(SCREAMING_SNAKE_CASE__ ) ), "'number' must been an int, even and > 2" lowercase : Union[str, Any] = [] # this list will returned # creates a list of prime numbers between 2 up to 'number' lowercase : str = get_prime_numbers(SCREAMING_SNAKE_CASE__ ) lowercase : Any = len(SCREAMING_SNAKE_CASE__ ) # run variable for while-loops. lowercase : Optional[Any] = 0 lowercase : List[Any] = None # exit variable. for break up the loops lowercase : Any = True while i < len_pn and loop: lowercase : str = i + 1 while j < len_pn and loop: if prime_numbers[i] + prime_numbers[j] == number: lowercase : Union[str, Any] = False ans.append(prime_numbers[i] ) ans.append(prime_numbers[j] ) j += 1 i += 1 # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (len(SCREAMING_SNAKE_CASE__ ) == 2) and (ans[0] + ans[1] == number) and is_prime(ans[0] ) and is_prime(ans[1] ) ), "'ans' must contains two primes. And sum of elements must been eq 'number'" return ans def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (numbera >= 0) and (numbera >= 0) ), "'number1' and 'number2' must been positive integer." lowercase : Union[str, Any] = 0 while numbera != 0: lowercase : Optional[int] = numbera % numbera lowercase : Optional[int] = numbera lowercase : Dict = rest # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( numbera >= 0 ), "'number' must been from type int and positive" return numbera def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (numbera >= 1) and (numbera >= 1) ), "'number1' and 'number2' must been positive integer." lowercase : Dict = 1 # actual answer that will be return. # for kgV (x,1) if numbera > 1 and numbera > 1: # builds the prime factorization of 'number1' and 'number2' lowercase : Optional[Any] = prime_factorization(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[int] = prime_factorization(SCREAMING_SNAKE_CASE__ ) elif numbera == 1 or numbera == 1: lowercase : Union[str, Any] = [] lowercase : List[str] = [] lowercase : Dict = max(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = 0 lowercase : Optional[Any] = 0 lowercase : List[str] = [] # captured numbers int both 'primeFac1' and 'primeFac2' # iterates through primeFac1 for n in prime_fac_a: if n not in done: if n in prime_fac_a: lowercase : Dict = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) for _ in range(max(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ): ans *= n else: lowercase : List[Any] = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) for _ in range(SCREAMING_SNAKE_CASE__ ): ans *= n done.append(SCREAMING_SNAKE_CASE__ ) # iterates through primeFac2 for n in prime_fac_a: if n not in done: lowercase : Optional[int] = prime_fac_a.count(SCREAMING_SNAKE_CASE__ ) for _ in range(SCREAMING_SNAKE_CASE__ ): ans *= n done.append(SCREAMING_SNAKE_CASE__ ) # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( ans >= 0 ), "'ans' must been from type int and positive" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 0), "'number' must been a positive int" lowercase : Dict = 0 lowercase : List[str] = 2 # this variable holds the answer while index < n: index += 1 ans += 1 # counts to the next number # if ans not prime then # runs to the next prime number. while not is_prime(SCREAMING_SNAKE_CASE__ ): ans += 1 # precondition assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and is_prime( SCREAMING_SNAKE_CASE__ ), "'ans' must been a prime number and from type int" return ans def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: assert ( is_prime(SCREAMING_SNAKE_CASE__ ) and is_prime(SCREAMING_SNAKE_CASE__ ) and (p_number_a < p_number_a) ), "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" lowercase : List[str] = p_number_a + 1 # jump to the next number lowercase : List[Any] = [] # this list will be returns. # if number is not prime then # fetch the next prime number. while not is_prime(SCREAMING_SNAKE_CASE__ ): number += 1 while number < p_number_a: ans.append(SCREAMING_SNAKE_CASE__ ) number += 1 # fetch the next prime number. while not is_prime(SCREAMING_SNAKE_CASE__ ): number += 1 # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ans[0] != p_number_a and ans[len(SCREAMING_SNAKE_CASE__ ) - 1] != p_number_a ), "'ans' must been a list without the arguments" # 'ans' contains not 'pNumber1' and 'pNumber2' ! return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 1), "'n' must been int and >= 1" lowercase : Optional[Any] = [] # will be returned. for divisor in range(1 , n + 1 ): if n % divisor == 0: ans.append(SCREAMING_SNAKE_CASE__ ) # precondition assert ans[0] == 1 and ans[len(SCREAMING_SNAKE_CASE__ ) - 1] == n, "Error in function getDivisiors(...)" return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and ( number > 1 ), "'number' must been an int and >= 1" lowercase : str = get_divisors(SCREAMING_SNAKE_CASE__ ) # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (divisors[0] == 1) and (divisors[len(SCREAMING_SNAKE_CASE__ ) - 1] == number) ), "Error in help-function getDivisiors(...)" # summed all divisors up to 'number' (exclusive), hence [:-1] return sum(divisors[:-1] ) == number def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (denominator != 0) ), "The arguments must been from type int and 'denominator' != 0" # build the greatest common divisor of numerator and denominator. lowercase : Tuple = gcd(abs(SCREAMING_SNAKE_CASE__ ) , abs(SCREAMING_SNAKE_CASE__ ) ) # precondition assert ( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (numerator % gcd_of_fraction == 0) and (denominator % gcd_of_fraction == 0) ), "Error in function gcd(...,...)" return (numerator // gcd_of_fraction, denominator // gcd_of_fraction) def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 0), "'n' must been a int and >= 0" lowercase : List[str] = 1 # this will be return. for factor in range(1 , n + 1 ): ans *= factor return ans def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and (n >= 0), "'n' must been an int and >= 0" lowercase : int = 0 lowercase : Union[str, Any] = 1 lowercase : int = 1 # this will be return for _ in range(n - 1 ): lowercase : Optional[int] = ans ans += fiba lowercase : Optional[int] = tmp return ans
20
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _a = logging.get_logger(__name__) _a = { 'abeja/gpt-neox-japanese-2.7b': 'https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json', } class A_ (lowercase__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[str] = """gpt_neox_japanese""" def __init__( self , lowercase_=3_2000 , lowercase_=2560 , lowercase_=32 , lowercase_=32 , lowercase_=4 , lowercase_="gelu" , lowercase_=1.00 , lowercase_=1_0000 , lowercase_=2048 , lowercase_=0.02 , lowercase_=1E-5 , lowercase_=True , lowercase_=3_1996 , lowercase_=3_1999 , lowercase_=0.1 , lowercase_=0.0 , **lowercase_ , ): """simple docstring""" super().__init__(bos_token_id=lowercase_ , eos_token_id=lowercase_ , **lowercase_ ) UpperCAmelCase_ : str = vocab_size UpperCAmelCase_ : Any = max_position_embeddings UpperCAmelCase_ : Optional[int] = hidden_size UpperCAmelCase_ : Tuple = num_hidden_layers UpperCAmelCase_ : Optional[int] = num_attention_heads UpperCAmelCase_ : Any = intermediate_multiple_size UpperCAmelCase_ : Optional[int] = hidden_act UpperCAmelCase_ : List[Any] = rotary_pct UpperCAmelCase_ : List[str] = rotary_emb_base UpperCAmelCase_ : str = initializer_range UpperCAmelCase_ : Optional[int] = layer_norm_eps UpperCAmelCase_ : Tuple = use_cache UpperCAmelCase_ : Optional[int] = attention_dropout UpperCAmelCase_ : Union[str, Any] = hidden_dropout
61
from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase : List[str] = logging.get_logger(__name__) lowercase : Any = { """uclanlp/visualbert-vqa""": """https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json""", """uclanlp/visualbert-vqa-pre""": """https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json""", """uclanlp/visualbert-vqa-coco-pre""": ( """https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json""" ), """uclanlp/visualbert-vcr""": """https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json""", """uclanlp/visualbert-vcr-pre""": """https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json""", """uclanlp/visualbert-vcr-coco-pre""": ( """https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json""" ), """uclanlp/visualbert-nlvr2""": """https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json""", """uclanlp/visualbert-nlvr2-pre""": """https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json""", """uclanlp/visualbert-nlvr2-coco-pre""": ( """https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json""" ) # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert } class __snake_case ( lowerCAmelCase ): _a : Union[str, Any]= "visual_bert" def __init__( self ,snake_case=30522 ,snake_case=768 ,snake_case=512 ,snake_case=12 ,snake_case=12 ,snake_case=3072 ,snake_case="gelu" ,snake_case=0.1 ,snake_case=0.1 ,snake_case=512 ,snake_case=2 ,snake_case=0.02 ,snake_case=1e-12 ,snake_case=False ,snake_case=True ,snake_case=1 ,snake_case=0 ,snake_case=2 ,**snake_case ,): '''simple docstring''' super().__init__(pad_token_id=snake_case ,bos_token_id=snake_case ,eos_token_id=snake_case ,**snake_case ) lowercase : Tuple = vocab_size lowercase : int = max_position_embeddings lowercase : Optional[Any] = hidden_size lowercase : int = visual_embedding_dim lowercase : Tuple = num_hidden_layers lowercase : str = num_attention_heads lowercase : Optional[Any] = intermediate_size lowercase : str = hidden_act lowercase : Tuple = hidden_dropout_prob lowercase : List[Any] = attention_probs_dropout_prob lowercase : Union[str, Any] = initializer_range lowercase : int = type_vocab_size lowercase : Union[str, Any] = layer_norm_eps lowercase : Union[str, Any] = bypass_transformer lowercase : int = special_visual_initialize
20
0
import math def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : float , SCREAMING_SNAKE_CASE__ : float ): if ( not isinstance(SCREAMING_SNAKE_CASE__ , (int, float) ) or power_factor < -1 or power_factor > 1 ): raise ValueError('power_factor must be a valid float value between -1 and 1.' ) return apparent_power * power_factor def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : float , SCREAMING_SNAKE_CASE__ : float ): if ( not isinstance(SCREAMING_SNAKE_CASE__ , (int, float) ) or power_factor < -1 or power_factor > 1 ): raise ValueError('power_factor must be a valid float value between -1 and 1.' ) return apparent_power * math.sqrt(1 - power_factor**2 ) if __name__ == "__main__": import doctest doctest.testmod()
62
import argparse import requests import torch from PIL import Image from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: if "cls_token" in name: lowercase : List[Any] = name.replace("""cls_token""" , """vit.embeddings.cls_token""" ) if "mask_token" in name: lowercase : Any = name.replace("""mask_token""" , """decoder.mask_token""" ) if "decoder_pos_embed" in name: lowercase : str = name.replace("""decoder_pos_embed""" , """decoder.decoder_pos_embed""" ) if "pos_embed" in name and "decoder" not in name: lowercase : List[str] = name.replace("""pos_embed""" , """vit.embeddings.position_embeddings""" ) if "patch_embed.proj" in name: lowercase : Tuple = name.replace("""patch_embed.proj""" , """vit.embeddings.patch_embeddings.projection""" ) if "patch_embed.norm" in name: lowercase : int = name.replace("""patch_embed.norm""" , """vit.embeddings.norm""" ) if "decoder_blocks" in name: lowercase : Tuple = name.replace("""decoder_blocks""" , """decoder.decoder_layers""" ) if "blocks" in name: lowercase : List[Any] = name.replace("""blocks""" , """vit.encoder.layer""" ) if "attn.proj" in name: lowercase : List[str] = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name: lowercase : Union[str, Any] = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: lowercase : Optional[Any] = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: lowercase : Union[str, Any] = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: lowercase : Dict = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: lowercase : Dict = name.replace("""mlp.fc2""" , """output.dense""" ) if "decoder_embed" in name: lowercase : List[str] = name.replace("""decoder_embed""" , """decoder.decoder_embed""" ) if "decoder_norm" in name: lowercase : Dict = name.replace("""decoder_norm""" , """decoder.decoder_norm""" ) if "decoder_pred" in name: lowercase : List[str] = name.replace("""decoder_pred""" , """decoder.decoder_pred""" ) if "norm.weight" in name and "decoder" not in name: lowercase : Tuple = name.replace("""norm.weight""" , """vit.layernorm.weight""" ) if "norm.bias" in name and "decoder" not in name: lowercase : int = name.replace("""norm.bias""" , """vit.layernorm.bias""" ) return name def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: for key in orig_state_dict.copy().keys(): lowercase : List[Any] = orig_state_dict.pop(SCREAMING_SNAKE_CASE__ ) if "qkv" in key: lowercase : int = key.split(""".""" ) lowercase : List[str] = int(key_split[1] ) if "decoder_blocks" in key: lowercase : Tuple = config.decoder_hidden_size lowercase : int = """decoder.decoder_layers.""" if "weight" in key: lowercase : List[Any] = val[:dim, :] lowercase : Tuple = val[dim : dim * 2, :] lowercase : List[Any] = val[-dim:, :] elif "bias" in key: lowercase : str = val[:dim] lowercase : Dict = val[dim : dim * 2] lowercase : Union[str, Any] = val[-dim:] else: lowercase : Tuple = config.hidden_size lowercase : Union[str, Any] = """vit.encoder.layer.""" if "weight" in key: lowercase : Tuple = val[:dim, :] lowercase : List[str] = val[dim : dim * 2, :] lowercase : Dict = val[-dim:, :] elif "bias" in key: lowercase : Any = val[:dim] lowercase : str = val[dim : dim * 2] lowercase : Union[str, Any] = val[-dim:] else: lowercase : Union[str, Any] = val return orig_state_dict def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> int: lowercase : int = ViTMAEConfig() if "large" in checkpoint_url: lowercase : Dict = 1_024 lowercase : str = 4_096 lowercase : Optional[Any] = 24 lowercase : Optional[Any] = 16 elif "huge" in checkpoint_url: lowercase : int = 14 lowercase : List[Any] = 1_280 lowercase : int = 5_120 lowercase : List[Any] = 32 lowercase : Any = 16 lowercase : List[str] = ViTMAEForPreTraining(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE__ , map_location="""cpu""" )["""model"""] lowercase : Tuple = ViTMAEImageProcessor(size=config.image_size ) lowercase : Optional[int] = convert_state_dict(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) model.load_state_dict(SCREAMING_SNAKE_CASE__ ) model.eval() lowercase : Union[str, Any] = """https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg""" lowercase : Union[str, Any] = Image.open(requests.get(SCREAMING_SNAKE_CASE__ , stream=SCREAMING_SNAKE_CASE__ ).raw ) lowercase : Optional[Any] = ViTMAEImageProcessor(size=config.image_size ) lowercase : List[Any] = image_processor(images=SCREAMING_SNAKE_CASE__ , return_tensors="""pt""" ) # forward pass torch.manual_seed(2 ) lowercase : int = model(**SCREAMING_SNAKE_CASE__ ) lowercase : str = outputs.logits if "large" in checkpoint_url: lowercase : List[Any] = torch.tensor( [[-0.7309, -0.7128, -1.0169], [-1.0161, -0.9058, -1.1878], [-1.0478, -0.9411, -1.1911]] ) elif "huge" in checkpoint_url: lowercase : Tuple = torch.tensor( [[-1.1599, -0.9199, -1.2221], [-1.1952, -0.9269, -1.2307], [-1.2143, -0.9337, -1.2262]] ) else: lowercase : List[str] = torch.tensor( [[-0.9192, -0.8481, -1.1259], [-1.1349, -1.0034, -1.2599], [-1.1757, -1.0429, -1.2726]] ) # verify logits assert torch.allclose(logits[0, :3, :3] , SCREAMING_SNAKE_CASE__ , atol=1e-4 ) print(f"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) print(f"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": lowercase : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--checkpoint_url""", default="""https://dl.fbaipublicfiles.com/mae/visualize/mae_visualize_vit_base.pth""", type=str, help="""URL of the checkpoint you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) lowercase : List[Any] = parser.parse_args() convert_vit_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
20
0
'''simple docstring''' def _lowerCamelCase ( lowercase : str ) -> list: if n_term == "": return [] _a = [] for temp in range(int(lowercase ) ): series.append(F'1/{temp + 1}' if series else "1" ) return series if __name__ == "__main__": lowerCAmelCase_ : Union[str, Any] = input('Enter the last number (nth term) of the Harmonic Series') print('Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n') print(harmonic_series(nth_term))
63
import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=0.999 , SCREAMING_SNAKE_CASE__="cosine" , ) -> List[Any]: if alpha_transform_type == "cosine": def alpha_bar_fn(SCREAMING_SNAKE_CASE__ ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(SCREAMING_SNAKE_CASE__ ): return math.exp(t * -12.0 ) else: raise ValueError(f"Unsupported alpha_tranform_type: {alpha_transform_type}" ) lowercase : int = [] for i in range(SCREAMING_SNAKE_CASE__ ): lowercase : Dict = i / num_diffusion_timesteps lowercase : Optional[int] = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(SCREAMING_SNAKE_CASE__ ) / alpha_bar_fn(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) ) return torch.tensor(SCREAMING_SNAKE_CASE__ , dtype=torch.floataa ) class __snake_case ( lowerCAmelCase , lowerCAmelCase ): _a : Tuple= [e.name for e in KarrasDiffusionSchedulers] _a : int= 2 @register_to_config def __init__( self ,snake_case = 1000 ,snake_case = 0.00_085 ,snake_case = 0.012 ,snake_case = "linear" ,snake_case = None ,snake_case = "epsilon" ,snake_case = False ,snake_case = False ,snake_case = 1.0 ,snake_case = "linspace" ,snake_case = 0 ,): '''simple docstring''' if trained_betas is not None: lowercase : List[str] = torch.tensor(snake_case ,dtype=torch.floataa ) elif beta_schedule == "linear": lowercase : Optional[Any] = torch.linspace(snake_case ,snake_case ,snake_case ,dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. lowercase : int = ( torch.linspace(beta_start**0.5 ,beta_end**0.5 ,snake_case ,dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule lowercase : Union[str, Any] = betas_for_alpha_bar(snake_case ,alpha_transform_type="""cosine""" ) elif beta_schedule == "exp": lowercase : int = betas_for_alpha_bar(snake_case ,alpha_transform_type="""exp""" ) else: raise NotImplementedError(f"{beta_schedule} does is not implemented for {self.__class__}" ) lowercase : Any = 1.0 - self.betas lowercase : Dict = torch.cumprod(self.alphas ,dim=0 ) # set all values self.set_timesteps(snake_case ,snake_case ,snake_case ) lowercase : Tuple = use_karras_sigmas def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ): '''simple docstring''' if schedule_timesteps is None: lowercase : Union[str, Any] = self.timesteps lowercase : Dict = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: lowercase : Dict = 1 if len(snake_case ) > 1 else 0 else: lowercase : Union[str, Any] = timestep.cpu().item() if torch.is_tensor(snake_case ) else timestep lowercase : str = self._index_counter[timestep_int] return indices[pos].item() @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Optional[Any] = self.index_for_timestep(snake_case ) lowercase : Dict = self.sigmas[step_index] lowercase : List[str] = sample / ((sigma**2 + 1) ** 0.5) return sample def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = None ,snake_case = None ,): '''simple docstring''' lowercase : Any = num_inference_steps lowercase : List[Any] = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": lowercase : Optional[int] = np.linspace(0 ,num_train_timesteps - 1 ,snake_case ,dtype=snake_case )[::-1].copy() elif self.config.timestep_spacing == "leading": lowercase : int = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowercase : List[str] = (np.arange(0 ,snake_case ) * step_ratio).round()[::-1].copy().astype(snake_case ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": lowercase : List[str] = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 lowercase : Optional[int] = (np.arange(snake_case ,0 ,-step_ratio )).round().copy().astype(snake_case ) timesteps -= 1 else: raise ValueError( f"{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'." ) lowercase : Optional[int] = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) lowercase : Dict = np.log(snake_case ) lowercase : Union[str, Any] = np.interp(snake_case ,np.arange(0 ,len(snake_case ) ) ,snake_case ) if self.config.use_karras_sigmas: lowercase : List[Any] = self._convert_to_karras(in_sigmas=snake_case ,num_inference_steps=self.num_inference_steps ) lowercase : Tuple = np.array([self._sigma_to_t(snake_case ,snake_case ) for sigma in sigmas] ) lowercase : Any = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) lowercase : List[Any] = torch.from_numpy(snake_case ).to(device=snake_case ) lowercase : List[Any] = torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2 ), sigmas[-1:]] ) lowercase : Dict = torch.from_numpy(snake_case ) lowercase : List[Any] = torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2 )] ) if str(snake_case ).startswith("""mps""" ): # mps does not support float64 lowercase : Any = timesteps.to(snake_case ,dtype=torch.floataa ) else: lowercase : str = timesteps.to(device=snake_case ) # empty dt and derivative lowercase : Union[str, Any] = None lowercase : Any = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter lowercase : str = defaultdict(snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : Optional[int] = np.log(snake_case ) # get distribution lowercase : Union[str, Any] = log_sigma - log_sigmas[:, np.newaxis] # get sigmas range lowercase : Optional[int] = np.cumsum((dists >= 0) ,axis=0 ).argmax(axis=0 ).clip(max=log_sigmas.shape[0] - 2 ) lowercase : Any = low_idx + 1 lowercase : str = log_sigmas[low_idx] lowercase : Dict = log_sigmas[high_idx] # interpolate sigmas lowercase : int = (low - log_sigma) / (low - high) lowercase : Dict = np.clip(snake_case ,0 ,1 ) # transform interpolation to time range lowercase : Optional[Any] = (1 - w) * low_idx + w * high_idx lowercase : Tuple = t.reshape(sigma.shape ) return t def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' lowercase : float = in_sigmas[-1].item() lowercase : float = in_sigmas[0].item() lowercase : Dict = 7.0 # 7.0 is the value used in the paper lowercase : Optional[int] = np.linspace(0 ,1 ,snake_case ) lowercase : int = sigma_min ** (1 / rho) lowercase : Any = sigma_max ** (1 / rho) lowercase : Tuple = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho return sigmas @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return self.dt is None def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case = True ,): '''simple docstring''' lowercase : Union[str, Any] = self.index_for_timestep(snake_case ) # advance index counter by 1 lowercase : Optional[int] = timestep.cpu().item() if torch.is_tensor(snake_case ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: lowercase : str = self.sigmas[step_index] lowercase : Optional[int] = self.sigmas[step_index + 1] else: # 2nd order / Heun's method lowercase : Dict = self.sigmas[step_index - 1] lowercase : Optional[Any] = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API lowercase : Union[str, Any] = 0 lowercase : Any = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": lowercase : Any = sigma_hat if self.state_in_first_order else sigma_next lowercase : int = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": lowercase : Union[str, Any] = sigma_hat if self.state_in_first_order else sigma_next lowercase : Optional[Any] = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": lowercase : Optional[Any] = model_output else: raise ValueError( f"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`" ) if self.config.clip_sample: lowercase : str = pred_original_sample.clamp( -self.config.clip_sample_range ,self.config.clip_sample_range ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order lowercase : Optional[int] = (sample - pred_original_sample) / sigma_hat # 3. delta timestep lowercase : Union[str, Any] = sigma_next - sigma_hat # store for 2nd order step lowercase : Optional[int] = derivative lowercase : Union[str, Any] = dt lowercase : Union[str, Any] = sample else: # 2. 2nd order / Heun's method lowercase : Tuple = (sample - pred_original_sample) / sigma_next lowercase : Dict = (self.prev_derivative + derivative) / 2 # 3. take prev timestep & sample lowercase : Tuple = self.dt lowercase : Optional[Any] = self.sample # free dt and derivative # Note, this puts the scheduler in "first order mode" lowercase : List[str] = None lowercase : Tuple = None lowercase : Dict = None lowercase : List[Any] = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Optional[int] = self.sigmas.to(device=original_samples.device ,dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(snake_case ): # mps does not support float64 lowercase : List[Any] = self.timesteps.to(original_samples.device ,dtype=torch.floataa ) lowercase : List[str] = timesteps.to(original_samples.device ,dtype=torch.floataa ) else: lowercase : List[str] = self.timesteps.to(original_samples.device ) lowercase : Tuple = timesteps.to(original_samples.device ) lowercase : Any = [self.index_for_timestep(snake_case ,snake_case ) for t in timesteps] lowercase : int = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): lowercase : Any = sigma.unsqueeze(-1 ) lowercase : Optional[int] = original_samples + noise * sigma return noisy_samples def __len__( self ): '''simple docstring''' return self.config.num_train_timesteps
20
0
"""simple docstring""" import argparse import dataclasses import json import logging import os import shutil from typing import List, Optional import datasets from accelerate import Accelerator from datasets import load_dataset from finetuning import finetune from tqdm.auto import tqdm import transformers from transformers import AutoConfig, set_seed from transformers.trainer_utils import IntervalStrategy A_ = logging.getLogger(__name__) A_ = '''pytorch_model.bin''' @dataclasses.dataclass class lowercase: '''simple docstring''' lowercase__ = dataclasses.field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models."} ) lowercase__ = dataclasses.field( default=__a , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co."} , ) @dataclasses.dataclass class lowercase: '''simple docstring''' lowercase__ = dataclasses.field(metadata={"help": "A csv or a json file containing the training data."} ) lowercase__ = dataclasses.field(metadata={"help": "A csv or a json file containing the data to predict on."} ) lowercase__ = dataclasses.field( default=__a , metadata={"help": "A csv or a json file containing the validation data."} ) lowercase__ = dataclasses.field( default=__a , metadata={"help": "The name of the task to train on."} , ) lowercase__ = dataclasses.field( default=__a , metadata={"help": "The list of labels for the task."} ) @dataclasses.dataclass class lowercase: '''simple docstring''' lowercase__ = dataclasses.field( metadata={"help": "The output directory where the model predictions and checkpoints will be written."} ) lowercase__ = dataclasses.field( default="accuracy" , metadata={"help": "The evaluation metric used for the task."} ) lowercase__ = dataclasses.field( default="no" , metadata={ "help": "The evaluation strategy to adopt during training. Possible values are: [\"no\", \"step\", \"epoch]" } , ) lowercase__ = dataclasses.field( default=10 , metadata={"help": "Number of evaluation calls with no improvement after which training will be stopped."} , ) lowercase__ = dataclasses.field( default=0.0 , metadata={ "help": "How much the specified evaluation metric must improve to satisfy early stopping conditions." } , ) lowercase__ = dataclasses.field( default=__a , metadata={"help": "Whether to filter the pseudo-labeled data based on the confidence score."} , ) lowercase__ = dataclasses.field( default=__a , metadata={"help": "Whether to filter the pseudo-labeled data based on the validation performance."} , ) lowercase__ = dataclasses.field( default=__a , metadata={"help": "Whether to fine-tune on labeled data after pseudo training."} , ) lowercase__ = dataclasses.field( default=0.0 , metadata={"help": "Confidence threshold for pseudo-labeled data filtering."} , ) lowercase__ = dataclasses.field( default=1_00 , metadata={"help": "Number of evaluation calls with no improvement after which training will be stopped."} , ) lowercase__ = dataclasses.field( default=__a , metadata={"help": "Random seed for initialization."} , ) def UpperCAmelCase__ (snake_case__ : Tuple , snake_case__ : Union[str, Any] , snake_case__ : str , snake_case__ : Dict , snake_case__ : List[str] , snake_case__ : Optional[int] ): """simple docstring""" _snake_case : int = datasets.concatenate_datasets([infer_input, infer_output] , axis=1 ) if args.do_filter_by_confidence: _snake_case : str = dataset.filter(lambda snake_case__ : example["probability"] > args.confidence_threshold ) if args.do_filter_by_val_performance: assert eval_result >= 0.0 and eval_result <= 1.0 _snake_case : Optional[Any] = int(eval_result * len(snake_case__ ) ) print(snake_case__ ) _snake_case : Union[str, Any] = dataset.sort("""probability""" , reverse=snake_case__ ) _snake_case : int = dataset.select(range(snake_case__ ) ) _snake_case : Dict = dataset.remove_columns(["""label""", """probability"""] ) _snake_case : int = dataset.rename_column("""prediction""" , """label""" ) _snake_case : Dict = dataset.map(lambda snake_case__ : {"label": idalabel[example["label"]]} ) _snake_case : Optional[int] = dataset.shuffle(seed=args.seed ) _snake_case : List[Any] = os.path.join(snake_case__ , F"train_pseudo.{args.data_file_extension}" ) if args.data_file_extension == "csv": dataset.to_csv(snake_case__ , index=snake_case__ ) else: dataset.to_json(snake_case__ ) def UpperCAmelCase__ (snake_case__ : List[Any] , snake_case__ : Dict , snake_case__ : int , snake_case__ : List[Any] , **snake_case__ : int ): """simple docstring""" _snake_case : Tuple = Accelerator() # Make one log on every process with the configuration for debugging. logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , level=logging.INFO , ) logger.info(accelerator.state ) # Setup logging, we only want one process per machine to log things on the # screen. accelerator.is_local_main_process is only True for one process per # machine. logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() _snake_case : List[str] = STModelArguments(model_name_or_path=snake_case__ ) _snake_case : Union[str, Any] = STDataArguments(train_file=snake_case__ , infer_file=snake_case__ ) _snake_case : List[Any] = STTrainingArguments(output_dir=snake_case__ ) _snake_case : int = argparse.Namespace() for arg_class in (model_args, data_args, training_args): for key, value in vars(snake_case__ ).items(): setattr(snake_case__ , snake_case__ , snake_case__ ) for key, value in kwargs.items(): if hasattr(snake_case__ , snake_case__ ): setattr(snake_case__ , snake_case__ , snake_case__ ) # Sanity checks _snake_case : str = {} _snake_case : int = None # You need to provide the training data and the data to predict on assert args.train_file is not None assert args.infer_file is not None _snake_case : Any = args.train_file _snake_case : List[str] = args.infer_file if args.evaluation_strategy != IntervalStrategy.NO.value: assert args.eval_file is not None _snake_case : Tuple = args.eval_file for key in data_files: _snake_case : Tuple = data_files[key].split(""".""" )[-1] assert extension in ["csv", "json"], F"`{key}_file` should be a csv or a json file." if args.data_file_extension is None: _snake_case : Tuple = extension else: assert extension == args.data_file_extension, F"`{key}_file` should be a {args.data_file_extension} file`." assert ( args.eval_metric in datasets.list_metrics() ), F"{args.eval_metric} not in the list of supported metrics {datasets.list_metrics()}." # If passed along, set the training seed now. if args.seed is not None: set_seed(args.seed ) logger.info("""Creating the initial data directory for self-training...""" ) _snake_case : Any = F"{args.output_dir}/self-train_iter-{{}}".format _snake_case : Optional[int] = data_dir_format(0 ) if accelerator.is_main_process: if args.output_dir is not None: os.makedirs(args.output_dir , exist_ok=snake_case__ ) os.makedirs(snake_case__ , exist_ok=snake_case__ ) accelerator.wait_for_everyone() _snake_case : str = None _snake_case : Union[str, Any] = None _snake_case : Optional[int] = 0 _snake_case : Optional[Any] = False # Show the progress bar _snake_case : Optional[Any] = tqdm(range(args.max_selftrain_iterations ) , disable=not accelerator.is_local_main_process ) # Self-train for iteration in range(0 , int(args.max_selftrain_iterations ) ): _snake_case : List[str] = data_dir_format(snake_case__ ) assert os.path.exists(snake_case__ ) # Stage 1: initial fine-tuning for iteration = 0 or pseudo-training for # iteration > 0 _snake_case : Optional[int] = os.path.join(snake_case__ , """stage-1""" ) _snake_case : Tuple = { """accelerator""": accelerator, """model_name_or_path""": args.model_name_or_path, """cache_dir""": args.cache_dir, """do_train""": True, """train_file""": data_files["""train"""] if iteration == 0 else data_files["""train_pseudo"""], """do_eval""": True if args.eval_file is not None else False, """eval_file""": data_files["""eval"""], """do_predict""": True, """infer_file""": data_files["""infer"""], """task_name""": args.task_name, """label_list""": args.label_list, """output_dir""": current_output_dir, """eval_metric""": args.eval_metric, """evaluation_strategy""": args.evaluation_strategy, """early_stopping_patience""": args.early_stopping_patience, """early_stopping_threshold""": args.early_stopping_threshold, """seed""": args.seed, } # Add additional training arguments for key, value in kwargs.items(): if key not in arguments_dict and not hasattr(snake_case__ , snake_case__ ): arguments_dict.update({key: value} ) _snake_case : List[str] = os.path.join(snake_case__ , """best-checkpoint""" , snake_case__ ) if os.path.exists(snake_case__ ): logger.info( """Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 1.""" , snake_case__ , snake_case__ , ) else: logger.info("""***** Running self-training: iteration: %d, stage: 1 *****""" , snake_case__ ) finetune(**snake_case__ ) accelerator.wait_for_everyone() assert os.path.exists(snake_case__ ) logger.info("""Self-training job completed: iteration: %d, stage: 1.""" , snake_case__ ) if iteration > 0 and args.finetune_on_labeled_data: # Stage 2 (optional): fine-tuning on the original labeled data _snake_case : Any = os.path.join(snake_case__ , """best-checkpoint""" ) _snake_case : List[str] = os.path.join(snake_case__ , """stage-2""" ) # Update arguments_dict _snake_case : Union[str, Any] = model_path _snake_case : Union[str, Any] = data_files["""train"""] _snake_case : Union[str, Any] = current_output_dir _snake_case : Dict = os.path.join(snake_case__ , """best-checkpoint""" , snake_case__ ) if os.path.exists(snake_case__ ): logger.info( """Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 2.""" , snake_case__ , snake_case__ , ) else: logger.info("""***** Running self-training: iteration: %d, stage: 2 *****""" , snake_case__ ) finetune(**snake_case__ ) accelerator.wait_for_everyone() assert os.path.exists(snake_case__ ) logger.info("""Self-training job completed: iteration: %d, stage: 2.""" , snake_case__ ) _snake_case : Any = iteration _snake_case : Any = data_dir_format(iteration + 1 ) _snake_case : Dict = AutoConfig.from_pretrained(os.path.join(snake_case__ , """best-checkpoint""" ) ) _snake_case : List[Any] = config.idalabel _snake_case : Optional[Any] = os.path.join(snake_case__ , """eval_results_best-checkpoint.json""" ) _snake_case : int = os.path.join(snake_case__ , """test_results_best-checkpoint.json""" ) assert os.path.exists(snake_case__ ) with open(snake_case__ , """r""" ) as f: _snake_case : Any = float(json.load(snake_case__ )[args.eval_metric] ) _snake_case : List[str] = os.path.join(snake_case__ , """infer_output_best-checkpoint.csv""" ) assert os.path.exists(snake_case__ ) # Loading the dataset from local csv or json files. _snake_case : List[str] = load_dataset(args.data_file_extension , data_files={"""data""": data_files["""infer"""]} )["""data"""] _snake_case : Optional[Any] = load_dataset("""csv""" , data_files={"""data""": infer_output_file} )["""data"""] if accelerator.is_main_process: os.makedirs(snake_case__ , exist_ok=snake_case__ ) shutil.copy(snake_case__ , os.path.join(snake_case__ , F"eval_results_iter-{iteration}.json" ) ) if os.path.exists(snake_case__ ): shutil.copy(snake_case__ , os.path.join(snake_case__ , F"test_results_iter-{iteration}.json" ) ) create_pseudo_labeled_data(snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) accelerator.wait_for_everyone() _snake_case : Any = os.path.join(snake_case__ , F"train_pseudo.{args.data_file_extension}" ) if args.evaluation_strategy != IntervalStrategy.NO.value: _snake_case : Union[str, Any] = eval_result if best_iteration is None: _snake_case : List[Any] = new_iteration _snake_case : List[Any] = new_eval_result else: if new_eval_result - best_eval_result > args.early_stopping_threshold: _snake_case : Dict = new_iteration _snake_case : List[str] = new_eval_result _snake_case : Dict = 0 else: if new_eval_result == best_eval_result: _snake_case : Union[str, Any] = new_iteration _snake_case : int = new_eval_result early_stopping_patience_counter += 1 if early_stopping_patience_counter >= args.early_stopping_patience: _snake_case : str = True progress_bar.update(1 ) if should_training_stop: break if best_iteration is not None: # Save the best iteration logger.info("""Best iteration: %d""" , snake_case__ ) logger.info("""Best evaluation result: %s = %f""" , args.eval_metric , snake_case__ ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(snake_case__ , F"eval_results_iter-{iteration}.json" ) , os.path.join(snake_case__ , """eval_results_best-iteration.json""" ) , ) else: # Assume that the last iteration is the best logger.info("""Best iteration: %d""" , args.max_selftrain_iterations - 1 ) logger.info("""Best evaluation result: %s = %f""" , args.eval_metric , snake_case__ ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(snake_case__ , F"eval_results_iter-{args.max_selftrain_iterations - 1}.json" ) , os.path.join(snake_case__ , """eval_results_best-iteration.json""" ) , )
64
from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch lowercase : List[str] = logging.get_logger(__name__) @add_end_docstrings( lowerCAmelCase , R"\n top_k (`int`, defaults to 5):\n The number of predictions to return.\n targets (`str` or `List[str]`, *optional*):\n When passed, the model will limit the scores to the passed targets instead of looking up in the whole\n vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting\n token will be used (with a warning, and that might be slower).\n\n " , ) class __snake_case ( lowerCAmelCase ): def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' if self.framework == "tf": lowercase : str = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": lowercase : Optional[int] = torch.nonzero(input_ids == self.tokenizer.mask_token_id ,as_tuple=snake_case ) else: raise ValueError("""Unsupported framework""" ) return masked_index def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Tuple = self.get_masked_index(snake_case ) lowercase : Dict = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( """fill-mask""" ,self.model.base_model_prefix ,f"No mask_token ({self.tokenizer.mask_token}) found on the input" ,) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' if isinstance(snake_case ,snake_case ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input["""input_ids"""][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ,**snake_case ): '''simple docstring''' if return_tensors is None: lowercase : int = self.framework lowercase : Optional[Any] = self.tokenizer(snake_case ,return_tensors=snake_case ) self.ensure_exactly_one_mask_token(snake_case ) return model_inputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Optional[int] = self.model(**snake_case ) lowercase : Tuple = model_inputs["""input_ids"""] return model_outputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=5 ,snake_case=None ): '''simple docstring''' if target_ids is not None and target_ids.shape[0] < top_k: lowercase : str = target_ids.shape[0] lowercase : Optional[Any] = model_outputs["""input_ids"""][0] lowercase : List[str] = model_outputs["""logits"""] if self.framework == "tf": lowercase : List[str] = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] lowercase : Tuple = outputs.numpy() lowercase : Tuple = outputs[0, masked_index, :] lowercase : Any = stable_softmax(snake_case ,axis=-1 ) if target_ids is not None: lowercase : Union[str, Any] = tf.gather_nd(tf.squeeze(snake_case ,0 ) ,target_ids.reshape(-1 ,1 ) ) lowercase : int = tf.expand_dims(snake_case ,0 ) lowercase : Tuple = tf.math.top_k(snake_case ,k=snake_case ) lowercase , lowercase : int = topk.values.numpy(), topk.indices.numpy() else: lowercase : Optional[Any] = torch.nonzero(input_ids == self.tokenizer.mask_token_id ,as_tuple=snake_case ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample lowercase : Union[str, Any] = outputs[0, masked_index, :] lowercase : Tuple = logits.softmax(dim=-1 ) if target_ids is not None: lowercase : List[str] = probs[..., target_ids] lowercase , lowercase : Union[str, Any] = probs.topk(snake_case ) lowercase : Any = [] lowercase : List[Any] = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() ,predictions.tolist() ) ): lowercase : Dict = [] for v, p in zip(_values ,_predictions ): # Copy is important since we're going to modify this array in place lowercase : Dict = input_ids.numpy().copy() if target_ids is not None: lowercase : Union[str, Any] = target_ids[p].tolist() lowercase : Tuple = p # Filter padding out: lowercase : List[str] = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back lowercase : Tuple = self.tokenizer.decode(snake_case ,skip_special_tokens=snake_case ) lowercase : Optional[Any] = {"""score""": v, """token""": p, """token_str""": self.tokenizer.decode([p] ), """sequence""": sequence} row.append(snake_case ) result.append(snake_case ) if single_mask: return result[0] return result def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ): '''simple docstring''' if isinstance(snake_case ,snake_case ): lowercase : List[Any] = [targets] try: lowercase : List[str] = self.tokenizer.get_vocab() except Exception: lowercase : Any = {} lowercase : Dict = [] for target in targets: lowercase : Dict = vocab.get(snake_case ,snake_case ) if id_ is None: lowercase : Optional[int] = self.tokenizer( snake_case ,add_special_tokens=snake_case ,return_attention_mask=snake_case ,return_token_type_ids=snake_case ,max_length=1 ,truncation=snake_case ,)["""input_ids"""] if len(snake_case ) == 0: logger.warning( f"The specified target token `{target}` does not exist in the model vocabulary. " """We cannot replace it with anything meaningful, ignoring it""" ) continue lowercase : Union[str, Any] = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( f"The specified target token `{target}` does not exist in the model vocabulary. " f"Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`." ) target_ids.append(id_ ) lowercase : Optional[Any] = list(set(snake_case ) ) if len(snake_case ) == 0: raise ValueError("""At least one target must be provided when passed.""" ) lowercase : Optional[Any] = np.array(snake_case ) return target_ids def _SCREAMING_SNAKE_CASE ( self ,snake_case=None ,snake_case=None ): '''simple docstring''' lowercase : Dict = {} if targets is not None: lowercase : str = self.get_target_ids(snake_case ,snake_case ) lowercase : List[Any] = target_ids if top_k is not None: lowercase : List[str] = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( """fill-mask""" ,self.model.base_model_prefix ,"""The tokenizer does not define a `mask_token`.""" ) return {}, {}, postprocess_params def __call__( self ,snake_case ,*snake_case ,**snake_case ): '''simple docstring''' lowercase : Tuple = super().__call__(snake_case ,**snake_case ) if isinstance(snake_case ,snake_case ) and len(snake_case ) == 1: return outputs[0] return outputs
20
0
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 lowerCAmelCase_ ( __A, __A, __A=8 ) -> int: '''simple docstring''' UpperCAmelCase__ = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 UpperCAmelCase__ = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class A ( UpperCAmelCase_ ): def __init__(self : int , __UpperCAmelCase : UNetaDConditionModel , __UpperCAmelCase : DDPMScheduler , __UpperCAmelCase : VQModel , ) -> List[str]: """simple docstring""" super().__init__() self.register_modules( unet=__UpperCAmelCase , scheduler=__UpperCAmelCase , movq=__UpperCAmelCase , ) UpperCAmelCase__ = 2 ** (len(self.movq.config.block_out_channels ) - 1) def lowercase_ (self : Any , __UpperCAmelCase : List[str] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : str , __UpperCAmelCase : Any , __UpperCAmelCase : List[str] , __UpperCAmelCase : Union[str, Any] ) -> List[Any]: """simple docstring""" if latents is None: UpperCAmelCase__ = 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}""" ) UpperCAmelCase__ = latents.to(__UpperCAmelCase ) UpperCAmelCase__ = latents * scheduler.init_noise_sigma return latents def lowercase_ (self : Optional[int] , __UpperCAmelCase : int=0 ) -> Tuple: """simple docstring""" if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("Please install accelerate via `pip install accelerate`" ) UpperCAmelCase__ = torch.device(f"""cuda:{gpu_id}""" ) UpperCAmelCase__ = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(__UpperCAmelCase , __UpperCAmelCase ) def lowercase_ (self : Tuple , __UpperCAmelCase : int=0 ) -> List[str]: """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." ) UpperCAmelCase__ = 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) UpperCAmelCase__ = None for cpu_offloaded_model in [self.unet, self.movq]: UpperCAmelCase__ , UpperCAmelCase__ = cpu_offload_with_hook(__UpperCAmelCase , __UpperCAmelCase , prev_module_hook=__UpperCAmelCase ) # We'll offload the last model manually. UpperCAmelCase__ = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def lowercase_ (self : Optional[Any] ) -> List[str]: """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 : int , __UpperCAmelCase : Union[torch.FloatTensor, List[torch.FloatTensor]] , __UpperCAmelCase : Union[torch.FloatTensor, List[torch.FloatTensor]] , __UpperCAmelCase : torch.FloatTensor , __UpperCAmelCase : int = 5_1_2 , __UpperCAmelCase : int = 5_1_2 , __UpperCAmelCase : int = 1_0_0 , __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 , ) -> Optional[Any]: """simple docstring""" UpperCAmelCase__ = self._execution_device UpperCAmelCase__ = guidance_scale > 1.0 if isinstance(__UpperCAmelCase , __UpperCAmelCase ): UpperCAmelCase__ = torch.cat(__UpperCAmelCase , dim=0 ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ): UpperCAmelCase__ = torch.cat(__UpperCAmelCase , dim=0 ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ): UpperCAmelCase__ = torch.cat(__UpperCAmelCase , dim=0 ) UpperCAmelCase__ = image_embeds.shape[0] * num_images_per_prompt if do_classifier_free_guidance: UpperCAmelCase__ = image_embeds.repeat_interleave(__UpperCAmelCase , dim=0 ) UpperCAmelCase__ = negative_image_embeds.repeat_interleave(__UpperCAmelCase , dim=0 ) UpperCAmelCase__ = hint.repeat_interleave(__UpperCAmelCase , dim=0 ) UpperCAmelCase__ = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=__UpperCAmelCase ) UpperCAmelCase__ = torch.cat([hint, hint] , dim=0 ).to(dtype=self.unet.dtype , device=__UpperCAmelCase ) self.scheduler.set_timesteps(__UpperCAmelCase , device=__UpperCAmelCase ) UpperCAmelCase__ = self.scheduler.timesteps UpperCAmelCase__ = self.movq.config.latent_channels UpperCAmelCase__ , UpperCAmelCase__ = downscale_height_and_width(__UpperCAmelCase , __UpperCAmelCase , self.movq_scale_factor ) # create initial latent UpperCAmelCase__ = 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 UpperCAmelCase__ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents UpperCAmelCase__ = {"image_embeds": image_embeds, "hint": hint} UpperCAmelCase__ = self.unet( sample=__UpperCAmelCase , timestep=__UpperCAmelCase , encoder_hidden_states=__UpperCAmelCase , added_cond_kwargs=__UpperCAmelCase , return_dict=__UpperCAmelCase , )[0] if do_classifier_free_guidance: UpperCAmelCase__ , UpperCAmelCase__ = noise_pred.split(latents.shape[1] , dim=1 ) UpperCAmelCase__ , UpperCAmelCase__ = noise_pred.chunk(2 ) UpperCAmelCase__ , UpperCAmelCase__ = variance_pred.chunk(2 ) UpperCAmelCase__ = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) UpperCAmelCase__ = 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"] ): UpperCAmelCase__ , UpperCAmelCase__ = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 UpperCAmelCase__ = self.scheduler.step( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , generator=__UpperCAmelCase , )[0] # post-processing UpperCAmelCase__ = 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"]: UpperCAmelCase__ = image * 0.5 + 0.5 UpperCAmelCase__ = image.clamp(0 , 1 ) UpperCAmelCase__ = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": UpperCAmelCase__ = self.numpy_to_pil(__UpperCAmelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=__UpperCAmelCase )
65
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class __snake_case ( unittest.TestCase ): def __init__( self ,snake_case ,snake_case=7 ,snake_case=3 ,snake_case=18 ,snake_case=30 ,snake_case=400 ,snake_case=True ,snake_case=None ,snake_case=True ,snake_case=None ,): '''simple docstring''' lowercase : Dict = size if size is not None else {"""shortest_edge""": 20} lowercase : Union[str, Any] = crop_size if crop_size is not None else {"""height""": 18, """width""": 18} lowercase : str = parent lowercase : int = batch_size lowercase : str = num_channels lowercase : int = image_size lowercase : List[str] = min_resolution lowercase : str = max_resolution lowercase : Dict = do_resize lowercase : Dict = size lowercase : Dict = do_center_crop lowercase : str = crop_size def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, } @require_torch @require_vision class __snake_case ( lowerCAmelCase , unittest.TestCase ): _a : Any= MobileNetVaImageProcessor if is_vision_available() else None def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = MobileNetVaImageProcessingTester(self ) @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[int] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case ,"""do_resize""" ) ) self.assertTrue(hasattr(snake_case ,"""size""" ) ) self.assertTrue(hasattr(snake_case ,"""do_center_crop""" ) ) self.assertTrue(hasattr(snake_case ,"""crop_size""" ) ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : int = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{"""shortest_edge""": 20} ) self.assertEqual(image_processor.crop_size ,{"""height""": 18, """width""": 18} ) lowercase : int = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ,crop_size=84 ) self.assertEqual(image_processor.size ,{"""shortest_edge""": 42} ) self.assertEqual(image_processor.crop_size ,{"""height""": 84, """width""": 84} ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' pass def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowercase : str = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case ,Image.Image ) # Test not batched input lowercase : Dict = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) # Test batched lowercase : Tuple = image_processing(snake_case ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowercase : Union[str, Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case ,numpify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case ,np.ndarray ) # Test not batched input lowercase : Optional[Any] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) # Test batched lowercase : List[str] = image_processing(snake_case ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowercase : Tuple = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case ,torchify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case ,torch.Tensor ) # Test not batched input lowercase : Optional[int] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) # Test batched lowercase : List[str] = image_processing(snake_case ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,)
20
0
"""simple docstring""" # NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from ...utils import deprecate from ..controlnet.pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline # noqa: F401 deprecate( "stable diffusion controlnet", "0.22.0", "Importing `FlaxStableDiffusionControlNetPipeline` from diffusers.pipelines.stable_diffusion.flax_pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import FlaxStableDiffusionControlNetPipeline` instead.", standard_warn=False, stacklevel=3, )
66
from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef import datasets lowercase : str = """\ @inproceedings{wang2019glue, title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding}, author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.}, note={In the Proceedings of ICLR.}, year={2019} } """ lowercase : Dict = """\ GLUE, the General Language Understanding Evaluation benchmark (https://gluebenchmark.com/) is a collection of resources for training, evaluating, and analyzing natural language understanding systems. """ lowercase : int = """ Compute GLUE evaluation metric associated to each GLUE dataset. Args: predictions: list of predictions to score. Each translation should be tokenized into a list of tokens. references: list of lists of references for each translation. Each reference should be tokenized into a list of tokens. Returns: depending on the GLUE subset, one or several of: \"accuracy\": Accuracy \"f1\": F1 score \"pearson\": Pearson Correlation \"spearmanr\": Spearman Correlation \"matthews_correlation\": Matthew Correlation Examples: >>> glue_metric = datasets.load_metric('glue', 'sst2') # 'sst2' or any of [\"mnli\", \"mnli_mismatched\", \"mnli_matched\", \"qnli\", \"rte\", \"wnli\", \"hans\"] >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0} >>> glue_metric = datasets.load_metric('glue', 'mrpc') # 'mrpc' or 'qqp' >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0, 'f1': 1.0} >>> glue_metric = datasets.load_metric('glue', 'stsb') >>> references = [0., 1., 2., 3., 4., 5.] >>> predictions = [0., 1., 2., 3., 4., 5.] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print({\"pearson\": round(results[\"pearson\"], 2), \"spearmanr\": round(results[\"spearmanr\"], 2)}) {'pearson': 1.0, 'spearmanr': 1.0} >>> glue_metric = datasets.load_metric('glue', 'cola') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'matthews_correlation': 1.0} """ def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[int]: return float((preds == labels).mean() ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: lowercase : Any = simple_accuracy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = float(fa_score(y_true=SCREAMING_SNAKE_CASE__ , y_pred=SCREAMING_SNAKE_CASE__ ) ) return { "accuracy": acc, "f1": fa, } def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: lowercase : Union[str, Any] = float(pearsonr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )[0] ) lowercase : Dict = float(spearmanr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )[0] ) return { "pearson": pearson_corr, "spearmanr": spearman_corr, } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __snake_case ( datasets.Metric ): def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self.config_name not in [ "sst2", "mnli", "mnli_mismatched", "mnli_matched", "cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans", ]: raise KeyError( """You should supply a configuration name selected in """ """[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", """ """\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]""" ) return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { """predictions""": datasets.Value("""int64""" if self.config_name != """stsb""" else """float32""" ), """references""": datasets.Value("""int64""" if self.config_name != """stsb""" else """float32""" ), } ) ,codebase_urls=[] ,reference_urls=[] ,format="""numpy""" ,) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' if self.config_name == "cola": return {"matthews_correlation": matthews_corrcoef(snake_case ,snake_case )} elif self.config_name == "stsb": return pearson_and_spearman(snake_case ,snake_case ) elif self.config_name in ["mrpc", "qqp"]: return acc_and_fa(snake_case ,snake_case ) elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]: return {"accuracy": simple_accuracy(snake_case ,snake_case )} else: raise KeyError( """You should supply a configuration name selected in """ """[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", """ """\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]""" )
20
0
'''simple docstring''' import unittest import numpy as np from transformers import RobertaPreLayerNormConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roberta_prelayernorm.modeling_flax_roberta_prelayernorm import ( FlaxRobertaPreLayerNormForCausalLM, FlaxRobertaPreLayerNormForMaskedLM, FlaxRobertaPreLayerNormForMultipleChoice, FlaxRobertaPreLayerNormForQuestionAnswering, FlaxRobertaPreLayerNormForSequenceClassification, FlaxRobertaPreLayerNormForTokenClassification, FlaxRobertaPreLayerNormModel, ) class a__ ( unittest.TestCase ): def __init__( self : Union[str, Any] , a : Optional[int] , a : str=13 , a : str=7 , a : Optional[Any]=True , a : List[Any]=True , a : str=True , a : str=True , a : Dict=99 , a : Dict=32 , a : Union[str, Any]=5 , a : Tuple=4 , a : List[str]=37 , a : Optional[int]="gelu" , a : Dict=0.1 , a : List[str]=0.1 , a : Any=5_12 , a : int=16 , a : List[Any]=2 , a : List[str]=0.02 , a : Any=4 , ): """simple docstring""" __lowerCamelCase = parent __lowerCamelCase = batch_size __lowerCamelCase = seq_length __lowerCamelCase = is_training __lowerCamelCase = use_attention_mask __lowerCamelCase = use_token_type_ids __lowerCamelCase = use_labels __lowerCamelCase = vocab_size __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_size __lowerCamelCase = hidden_act __lowerCamelCase = hidden_dropout_prob __lowerCamelCase = attention_probs_dropout_prob __lowerCamelCase = max_position_embeddings __lowerCamelCase = type_vocab_size __lowerCamelCase = type_sequence_label_size __lowerCamelCase = initializer_range __lowerCamelCase = num_choices def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCamelCase = None if self.use_attention_mask: __lowerCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) __lowerCamelCase = None if self.use_token_type_ids: __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __lowerCamelCase = RobertaPreLayerNormConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=a , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" __lowerCamelCase = self.prepare_config_and_inputs() __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = config_and_inputs __lowerCamelCase = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = self.prepare_config_and_inputs() __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = config_and_inputs __lowerCamelCase = True __lowerCamelCase = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax # Copied from tests.models.roberta.test_modelling_flax_roberta.FlaxRobertaPreLayerNormModelTest with ROBERTA->ROBERTA_PRELAYERNORM,Roberta->RobertaPreLayerNorm,roberta-base->andreasmadsen/efficient_mlm_m0.40 class a__ ( UpperCAmelCase__ , unittest.TestCase ): lowerCamelCase : Optional[Any] =True lowerCamelCase : Dict =( ( FlaxRobertaPreLayerNormModel, FlaxRobertaPreLayerNormForCausalLM, FlaxRobertaPreLayerNormForMaskedLM, FlaxRobertaPreLayerNormForSequenceClassification, FlaxRobertaPreLayerNormForTokenClassification, FlaxRobertaPreLayerNormForMultipleChoice, FlaxRobertaPreLayerNormForQuestionAnswering, ) if is_flax_available() else () ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = FlaxRobertaPreLayerNormModelTester(self ) @slow def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" for model_class_name in self.all_model_classes: __lowerCamelCase = model_class_name.from_pretrained('''andreasmadsen/efficient_mlm_m0.40''' , from_pt=a ) __lowerCamelCase = model(np.ones((1, 1) ) ) self.assertIsNotNone(a ) @require_flax class a__ ( unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = FlaxRobertaPreLayerNormForMaskedLM.from_pretrained('''andreasmadsen/efficient_mlm_m0.40''' , from_pt=a ) __lowerCamelCase = np.array([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]] , dtype=jnp.intaa ) __lowerCamelCase = model(a )[0] __lowerCamelCase = [1, 11, 5_02_65] self.assertEqual(list(output.shape ) , a ) # compare the actual values for a slice. __lowerCamelCase = np.array( [[[40.48_80, 18.01_99, -5.23_67], [-1.88_77, -4.08_85, 10.70_85], [-2.26_13, -5.61_10, 7.26_65]]] , dtype=np.floataa ) self.assertTrue(np.allclose(output[:, :3, :3] , a , atol=1e-4 ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = FlaxRobertaPreLayerNormModel.from_pretrained('''andreasmadsen/efficient_mlm_m0.40''' , from_pt=a ) __lowerCamelCase = np.array([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]] , dtype=jnp.intaa ) __lowerCamelCase = model(a )[0] # compare the actual values for a slice. __lowerCamelCase = np.array( [[[0.02_08, -0.03_56, 0.02_37], [-0.15_69, -0.04_11, -0.26_26], [0.18_79, 0.01_25, -0.00_89]]] , dtype=np.floataa ) self.assertTrue(np.allclose(output[:, :3, :3] , a , atol=1e-4 ) )
67
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 __snake_case ( unittest.TestCase ): _a : Optional[int]= MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ): '''simple docstring''' lowercase : Any = hf_hub_download( repo_id="""nateraw/video-demo""" ,filename="""archery.mp4""" ,repo_type="""dataset""" ) lowercase : List[str] = VideoClassificationPipeline(model=snake_case ,image_processor=snake_case ,top_k=2 ) lowercase : Dict = [ example_video_filepath, """https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4""", ] return video_classifier, examples def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' for example in examples: lowercase : int = video_classifier(snake_case ) self.assertEqual( snake_case ,[ {"""score""": ANY(snake_case ), """label""": ANY(snake_case )}, {"""score""": ANY(snake_case ), """label""": ANY(snake_case )}, ] ,) @require_torch def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Any = """hf-internal-testing/tiny-random-VideoMAEForVideoClassification""" lowercase : str = VideoMAEFeatureExtractor( size={"""shortest_edge""": 10} ,crop_size={"""height""": 10, """width""": 10} ) lowercase : List[Any] = pipeline( """video-classification""" ,model=snake_case ,feature_extractor=snake_case ,frame_sampling_rate=4 ) lowercase : Dict = hf_hub_download(repo_id="""nateraw/video-demo""" ,filename="""archery.mp4""" ,repo_type="""dataset""" ) lowercase : Any = video_classifier(snake_case ,top_k=2 ) self.assertEqual( nested_simplify(snake_case ,decimals=4 ) ,[{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}] ,) lowercase : str = video_classifier( [ video_file_path, video_file_path, ] ,top_k=2 ,) self.assertEqual( nested_simplify(snake_case ,decimals=4 ) ,[ [{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}], [{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}], ] ,) @require_tf def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' pass
20
0
import unittest from transformers import SPIECE_UNDERLINE, ReformerTokenizer, ReformerTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase__ = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece @require_tokenizers class a__ ( snake_case , unittest.TestCase ): """simple docstring""" __lowerCamelCase = ReformerTokenizer __lowerCamelCase = ReformerTokenizerFast __lowerCamelCase = True __lowerCamelCase = False __lowerCamelCase = True def UpperCamelCase ( self ) -> int: '''simple docstring''' super().setUp() A__ = ReformerTokenizer(lowercase , keep_accents=lowercase ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCamelCase ( self ) -> Tuple: '''simple docstring''' A__ = "<s>" A__ = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowercase ) , lowercase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowercase ) , lowercase ) def UpperCamelCase ( self ) -> Tuple: '''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] , "j" ) self.assertEqual(len(lowercase ) , 1000 ) def UpperCamelCase ( self ) -> int: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def UpperCamelCase ( self ) -> Any: '''simple docstring''' if not self.test_rust_tokenizer: return A__ = self.get_tokenizer() A__ = self.get_rust_tokenizer() A__ = "I was born in 92000, and this is falsé." A__ = tokenizer.tokenize(lowercase ) A__ = rust_tokenizer.tokenize(lowercase ) self.assertListEqual(lowercase , lowercase ) A__ = tokenizer.encode(lowercase , add_special_tokens=lowercase ) A__ = rust_tokenizer.encode(lowercase , add_special_tokens=lowercase ) self.assertListEqual(lowercase , lowercase ) A__ = self.get_rust_tokenizer() A__ = tokenizer.encode(lowercase ) A__ = rust_tokenizer.encode(lowercase ) self.assertListEqual(lowercase , lowercase ) def UpperCamelCase ( self , lowercase=15 ) -> 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(lowercase , **lowercase ) # Simple input A__ = "This is a simple input" A__ = ["This is a simple input 1", "This is a simple input 2"] A__ = ("This is a simple input", "This is a pair") A__ = [ ("This is a simple input 1", "This is a simple input 2"), ("This is a simple pair 1", "This is a simple pair 2"), ] # Simple input tests self.assertRaises(lowercase , tokenizer_r.encode , lowercase , max_length=lowercase , padding="max_length" ) # Simple input self.assertRaises(lowercase , tokenizer_r.encode_plus , lowercase , max_length=lowercase , padding="max_length" ) # Simple input self.assertRaises( lowercase , tokenizer_r.batch_encode_plus , lowercase , max_length=lowercase , padding="max_length" , ) # Pair input self.assertRaises(lowercase , tokenizer_r.encode , lowercase , max_length=lowercase , padding="max_length" ) # Pair input self.assertRaises(lowercase , tokenizer_r.encode_plus , lowercase , max_length=lowercase , padding="max_length" ) # Pair input self.assertRaises( lowercase , tokenizer_r.batch_encode_plus , lowercase , max_length=lowercase , padding="max_length" , ) def UpperCamelCase ( self ) -> Tuple: '''simple docstring''' pass def UpperCamelCase ( self ) -> Dict: '''simple docstring''' A__ = ReformerTokenizer(lowercase , keep_accents=lowercase ) A__ = tokenizer.tokenize("This is a test" ) self.assertListEqual(lowercase , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowercase ) , [285, 46, 10, 170, 382] , ) A__ = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( lowercase , [ 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(lowercase ) self.assertListEqual( lowercase , [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(lowercase ) self.assertListEqual( lowercase , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) @cached_property def UpperCamelCase ( self ) -> List[Any]: '''simple docstring''' return ReformerTokenizer.from_pretrained("google/reformer-crime-and-punishment" ) @slow def UpperCamelCase ( self ) -> Optional[Any]: '''simple docstring''' A__ = "Hello World!" A__ = [126, 32, 262, 152, 38, 72, 287] self.assertListEqual(lowercase , self.big_tokenizer.encode(lowercase ) ) @slow def UpperCamelCase ( self ) -> Union[str, Any]: '''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__ = [ 108, 265, 24, 111, 4, 258, 156, 35, 28, 275, 3, 259, 297, 260, 84, 4, 35, 110, 44, 8, 259, 91, 268, 21, 11, 209, 274, 109, 266, 277, 117, 86, 93, 315, 258, 278, 258, 277, 258, 0, 258, 288, 258, 319, 258, 0, 258, 0, 258, 0, 258, 0, 258, 287, 258, 315, 258, 289, 258, 278, 99, 269, 266, 262, 8, 259, 241, 4, 217, 230, 268, 266, 55, 168, 106, 75, 193, 266, 223, 27, 49, 26, 282, 25, 264, 299, 19, 26, 0, 258, 277, 117, 86, 93, 176, 183, 270, 11, 262, 42, 61, 265, ] self.assertListEqual(lowercase , self.big_tokenizer.encode(lowercase ) ) @require_torch @slow def UpperCamelCase ( self ) -> Optional[int]: '''simple docstring''' import torch from transformers import ReformerConfig, ReformerModel # Build sequence A__ = list(self.big_tokenizer.get_vocab().keys() )[:10] A__ = " ".join(lowercase ) A__ = self.big_tokenizer.encode_plus(lowercase , return_tensors="pt" ) A__ = self.big_tokenizer.batch_encode_plus([sequence, sequence] , return_tensors="pt" ) A__ = ReformerConfig() # The input gets padded during training so adjust the axial position encodings from the pretrained model value of (512, 1024) A__ = encoded_sequence["input_ids"].shape A__ = ReformerModel(lowercase ) # Reformer has config.vocab_size == tokenizer.vocab_size == len(tokenizer) - 1 = 320; len(tokenizer) is 321 (including a pad token with id 320) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**lowercase ) model(**lowercase ) @slow def UpperCamelCase ( self ) -> Union[str, Any]: '''simple docstring''' A__ = {"input_ids": [[108, 265, 24, 111, 4, 258, 156, 7, 51, 279, 58, 7, 76, 25, 69, 278], [140, 243, 264, 134, 17, 267, 77, 263, 22, 262, 297, 258, 304, 177, 279, 266, 14, 89, 13, 35, 261, 299, 272, 137, 275, 278]], "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]]} # noqa: E501 # fmt: on # This tokenizer does not know some characters like ")". # That is the reason why we use very simple texts here. # Also see https://github.com/huggingface/transformers/pull/11737#issuecomment-850769064 A__ = [ "This is a very simple sentence.", "The quick brown fox jumps over the lazy dog.", ] self.tokenizer_integration_test_util( expected_encoding=lowercase , model_name="google/reformer-crime-and-punishment" , revision="0e6c3decb8211d49bf881013425dc8b0448b3f5a" , padding=lowercase , sequences=lowercase , )
68
from __future__ import annotations from collections import namedtuple from dataclasses import dataclass @dataclass class __snake_case : _a : int _a : TreeNode | None= None _a : TreeNode | None= None lowercase : Dict = namedtuple("""CoinsDistribResult""", """moves excess""") def _snake_case( SCREAMING_SNAKE_CASE__ ) -> int: if root is None: return 0 # Validation def count_nodes(SCREAMING_SNAKE_CASE__ ) -> int: if node is None: return 0 return count_nodes(node.left ) + count_nodes(node.right ) + 1 def count_coins(SCREAMING_SNAKE_CASE__ ) -> int: if node is None: return 0 return count_coins(node.left ) + count_coins(node.right ) + node.data if count_nodes(SCREAMING_SNAKE_CASE__ ) != count_coins(SCREAMING_SNAKE_CASE__ ): raise ValueError("""The nodes number should be same as the number of coins""" ) # Main calculation def get_distrib(SCREAMING_SNAKE_CASE__ ) -> CoinsDistribResult: if node is None: return CoinsDistribResult(0 , 1 ) lowercase , lowercase : int = get_distrib(node.left ) lowercase , lowercase : List[Any] = get_distrib(node.right ) lowercase : Optional[Any] = 1 - left_distrib_excess lowercase : Union[str, Any] = 1 - right_distrib_excess lowercase : List[Any] = ( left_distrib_moves + right_distrib_moves + abs(SCREAMING_SNAKE_CASE__ ) + abs(SCREAMING_SNAKE_CASE__ ) ) lowercase : Any = node.data - coins_to_left - coins_to_right return CoinsDistribResult(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return get_distrib(SCREAMING_SNAKE_CASE__ )[0] if __name__ == "__main__": import doctest doctest.testmod()
20
0
"""simple docstring""" from dataclasses import dataclass from enum import Enum from typing import List, Optional, Union import numpy as np import PIL from PIL import Image from ...utils import BaseOutput, is_torch_available, is_transformers_available @dataclass class UpperCamelCase ( lowerCAmelCase__ ): SCREAMING_SNAKE_CASE_ = 42 SCREAMING_SNAKE_CASE_ = 42 if is_transformers_available() and is_torch_available(): from .pipeline_semantic_stable_diffusion import SemanticStableDiffusionPipeline
69
from collections import UserDict from typing import Union import numpy as np import requests from ..utils import ( add_end_docstrings, logging, ) from .audio_classification import ffmpeg_read from .base import PIPELINE_INIT_ARGS, Pipeline lowercase : Optional[Any] = logging.get_logger(__name__) @add_end_docstrings(lowerCAmelCase ) class __snake_case ( lowerCAmelCase ): def __init__( self ,**snake_case ): '''simple docstring''' super().__init__(**snake_case ) if self.framework != "pt": raise ValueError(f"The {self.__class__} is only available in PyTorch." ) # No specific FOR_XXX available yet def __call__( self ,snake_case ,**snake_case ): '''simple docstring''' return super().__call__(snake_case ,**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,**snake_case ): '''simple docstring''' lowercase : Union[str, Any] = {} if "candidate_labels" in kwargs: lowercase : List[str] = kwargs["""candidate_labels"""] if "hypothesis_template" in kwargs: lowercase : Dict = kwargs["""hypothesis_template"""] return preprocess_params, {}, {} def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ,snake_case="This is a sound of {}." ): '''simple docstring''' if isinstance(snake_case ,snake_case ): if audio.startswith("""http://""" ) or audio.startswith("""https://""" ): # We need to actually check for a real protocol, otherwise it's impossible to use a local file # like http_huggingface_co.png lowercase : Optional[Any] = requests.get(snake_case ).content else: with open(snake_case ,"""rb""" ) as f: lowercase : Union[str, Any] = f.read() if isinstance(snake_case ,snake_case ): lowercase : int = ffmpeg_read(snake_case ,self.feature_extractor.sampling_rate ) if not isinstance(snake_case ,np.ndarray ): raise ValueError("""We expect a numpy ndarray as input""" ) if len(audio.shape ) != 1: raise ValueError("""We expect a single channel audio input for ZeroShotAudioClassificationPipeline""" ) lowercase : Dict = self.feature_extractor( [audio] ,sampling_rate=self.feature_extractor.sampling_rate ,return_tensors="""pt""" ) lowercase : Tuple = candidate_labels lowercase : Tuple = [hypothesis_template.format(snake_case ) for x in candidate_labels] lowercase : Optional[Any] = self.tokenizer(snake_case ,return_tensors=self.framework ,padding=snake_case ) lowercase : Optional[Any] = [text_inputs] return inputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : List[str] = model_inputs.pop("""candidate_labels""" ) lowercase : Dict = model_inputs.pop("""text_inputs""" ) if isinstance(text_inputs[0] ,snake_case ): lowercase : List[Any] = text_inputs[0] else: # Batching case. lowercase : Dict = text_inputs[0][0] lowercase : Optional[Any] = self.model(**snake_case ,**snake_case ) lowercase : Any = { """candidate_labels""": candidate_labels, """logits""": outputs.logits_per_audio, } return model_outputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : List[Any] = model_outputs.pop("""candidate_labels""" ) lowercase : Any = model_outputs["""logits"""][0] if self.framework == "pt": lowercase : Any = logits.softmax(dim=0 ) lowercase : Tuple = probs.tolist() else: raise ValueError("""`tf` framework not supported.""" ) lowercase : Tuple = [ {"""score""": score, """label""": candidate_label} for score, candidate_label in sorted(zip(snake_case ,snake_case ) ,key=lambda snake_case : -x[0] ) ] return result
20
0
'''simple docstring''' from scipy.stats import pearsonr import datasets A__ : int =''' Pearson correlation coefficient and p-value for testing non-correlation. The Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. ''' A__ : List[str] =''' Args: predictions (`list` of `int`): Predicted class labels, as returned by a model. references (`list` of `int`): Ground truth labels. return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`. Returns: pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation. p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities. Examples: Example 1-A simple example using only predictions and references. >>> pearsonr_metric = datasets.load_metric("pearsonr") >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5]) >>> print(round(results[\'pearsonr\'], 2)) -0.74 Example 2-The same as Example 1, but that also returns the `p-value`. >>> pearsonr_metric = datasets.load_metric("pearsonr") >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True) >>> print(sorted(list(results.keys()))) [\'p-value\', \'pearsonr\'] >>> print(round(results[\'pearsonr\'], 2)) -0.74 >>> print(round(results[\'p-value\'], 2)) 0.15 ''' A__ : List[str] =''' @article{2020SciPy-NMeth, author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and Haberland, Matt and Reddy, Tyler and Cournapeau, David and Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and Bright, Jonathan and {van der Walt}, St{\'e}fan J. and Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and Kern, Robert and Larson, Eric and Carey, C J and Polat, Ilhan and Feng, Yu and Moore, Eric W. and {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and Harris, Charles R. and Archibald, Anne M. and Ribeiro, Antonio H. and Pedregosa, Fabian and {van Mulbregt}, Paul and {SciPy 1.0 Contributors}}, title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific Computing in Python}}, journal = {Nature Methods}, year = {2020}, volume = {17}, pages = {261--272}, adsurl = {https://rdcu.be/b08Wh}, doi = {10.1038/s41592-019-0686-2}, } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase ( datasets.Metric ): def lowercase__ ( self : Optional[int] ) -> List[str]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""float""" ), """references""": datasets.Value("""float""" ), } ) , reference_urls=["""https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html"""] , ) def lowercase__ ( self : List[str] , __snake_case : str , __snake_case : List[Any] , __snake_case : Any=False ) -> Optional[int]: if return_pvalue: _lowerCAmelCase = pearsonr(__snake_case , __snake_case ) return {"pearsonr": results[0], "p-value": results[1]} else: return {"pearsonr": float(pearsonr(__snake_case , __snake_case )[0] )}
70
import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _snake_case( *SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__=True , SCREAMING_SNAKE_CASE__=2 ) -> Optional[Any]: from .. import __version__ lowercase : int = take_from lowercase : Tuple = () if not isinstance(args[0] , SCREAMING_SNAKE_CASE__ ): lowercase : Dict = (args,) for attribute, version_name, message in args: if version.parse(version.parse(SCREAMING_SNAKE_CASE__ ).base_version ) >= version.parse(SCREAMING_SNAKE_CASE__ ): raise ValueError( f"The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers'" f" version {__version__} is >= {version_name}" ) lowercase : int = None if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(SCREAMING_SNAKE_CASE__ ),) lowercase : Union[str, Any] = f"The `{attribute}` argument is deprecated and will be removed in version {version_name}." elif hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): values += (getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ),) lowercase : int = f"The `{attribute}` attribute is deprecated and will be removed in version {version_name}." elif deprecated_kwargs is None: lowercase : Dict = f"`{attribute}` is deprecated and will be removed in version {version_name}." if warning is not None: lowercase : Dict = warning + """ """ if standard_warn else """""" warnings.warn(warning + message , SCREAMING_SNAKE_CASE__ , stacklevel=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and len(SCREAMING_SNAKE_CASE__ ) > 0: lowercase : str = inspect.getouterframes(inspect.currentframe() )[1] lowercase : List[str] = call_frame.filename lowercase : Tuple = call_frame.lineno lowercase : List[str] = call_frame.function lowercase , lowercase : Optional[Any] = next(iter(deprecated_kwargs.items() ) ) raise TypeError(f"{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`" ) if len(SCREAMING_SNAKE_CASE__ ) == 0: return elif len(SCREAMING_SNAKE_CASE__ ) == 1: return values[0] return values
20
0
import argparse import os import transformers from .convert_slow_tokenizer import SLOW_TO_FAST_CONVERTERS from .utils import logging logging.set_verbosity_info() A_ :Union[str, Any] = logging.get_logger(__name__) A_ :Dict = {name: getattr(transformers, name + '''Fast''') for name in SLOW_TO_FAST_CONVERTERS} def A ( a_ ,a_ ,a_ ,a_ ) -> Optional[Any]: if tokenizer_name is not None and tokenizer_name not in TOKENIZER_CLASSES: raise ValueError(F'Unrecognized tokenizer name, should be one of {list(TOKENIZER_CLASSES.keys() )}.' ) if tokenizer_name is None: __UpperCamelCase : Any =TOKENIZER_CLASSES else: __UpperCamelCase : Tuple ={tokenizer_name: getattr(a_ ,tokenizer_name + 'Fast' )} logger.info(F'Loading tokenizer classes: {tokenizer_names}' ) for tokenizer_name in tokenizer_names: __UpperCamelCase : Union[str, Any] =TOKENIZER_CLASSES[tokenizer_name] __UpperCamelCase : List[str] =True if checkpoint_name is None: __UpperCamelCase : Any =list(tokenizer_class.max_model_input_sizes.keys() ) else: __UpperCamelCase : Optional[Any] =[checkpoint_name] logger.info(F'For tokenizer {tokenizer_class.__class__.__name__} loading checkpoints: {checkpoint_names}' ) for checkpoint in checkpoint_names: logger.info(F'Loading {tokenizer_class.__class__.__name__} {checkpoint}' ) # Load tokenizer __UpperCamelCase : str =tokenizer_class.from_pretrained(a_ ,force_download=a_ ) # Save fast tokenizer logger.info(F'Save fast tokenizer to {dump_path} with prefix {checkpoint} add_prefix {add_prefix}' ) # For organization names we create sub-directories if "/" in checkpoint: __UpperCamelCase , __UpperCamelCase : Optional[int] =checkpoint.split('/' ) __UpperCamelCase : Any =os.path.join(a_ ,a_ ) elif add_prefix: __UpperCamelCase : str =checkpoint __UpperCamelCase : List[str] =dump_path else: __UpperCamelCase : List[Any] =None __UpperCamelCase : List[Any] =dump_path logger.info(F'=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}' ) if checkpoint in list(tokenizer.pretrained_vocab_files_map.values() )[0]: __UpperCamelCase : str =list(tokenizer.pretrained_vocab_files_map.values() )[0][checkpoint] __UpperCamelCase : Any =file_path.split(a_ )[-1][0] if next_char == "/": __UpperCamelCase : str =os.path.join(a_ ,a_ ) __UpperCamelCase : int =None logger.info(F'=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}' ) __UpperCamelCase : Tuple =tokenizer.save_pretrained( a_ ,legacy_format=a_ ,filename_prefix=a_ ) logger.info(F'=> File names {file_names}' ) for file_name in file_names: if not file_name.endswith('tokenizer.json' ): os.remove(a_ ) logger.info(F'=> removing {file_name}' ) if __name__ == "__main__": A_ :Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--dump_path''', default=None, type=str, required=True, help='''Path to output generated fast tokenizer files.''' ) parser.add_argument( '''--tokenizer_name''', default=None, type=str, help=( f"Optional tokenizer type selected in the list of {list(TOKENIZER_CLASSES.keys())}. If not given, will " '''download and convert all the checkpoints from AWS.''' ), ) parser.add_argument( '''--checkpoint_name''', default=None, type=str, help='''Optional checkpoint name. If not given, will download and convert the canonical checkpoints from AWS.''', ) parser.add_argument( '''--force_download''', action='''store_true''', help='''Re-download checkpoints.''', ) A_ :List[Any] = parser.parse_args() convert_slow_checkpoint_to_fast(args.tokenizer_name, args.checkpoint_name, args.dump_path, args.force_download)
71
def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: if index == r: for j in range(SCREAMING_SNAKE_CASE__ ): print(data[j] , end=""" """ ) print(""" """ ) return # When no more elements are there to put in data[] if i >= n: return # current is included, put next at next location lowercase : Tuple = arr[i] combination_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , index + 1 , SCREAMING_SNAKE_CASE__ , i + 1 ) # current is excluded, replace it with # next (Note that i+1 is passed, but # index is not changed) combination_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , i + 1 ) # The main function that prints all combinations # of size r in arr[] of size n. This function # mainly uses combinationUtil() def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: # A temporary array to store all combination one by one lowercase : Optional[int] = [0] * r # Print all combination using temporary array 'data[]' combination_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , 0 , SCREAMING_SNAKE_CASE__ , 0 ) if __name__ == "__main__": # Driver code to check the function above lowercase : int = [10, 20, 30, 40, 50] print_combination(arr, len(arr), 3) # This code is contributed by Ambuj sahu
20
0
"""simple docstring""" from maths.prime_check import is_prime def snake_case_ ( A_ : int ): '''simple docstring''' if not isinstance(A_, A_ ): _lowerCamelCase : Optional[int] = F'''Input value of [number={number}] must be an integer''' raise TypeError(A_ ) if is_prime(A_ ) and is_prime(number + 2 ): return number + 2 else: return -1 if __name__ == "__main__": import doctest doctest.testmod()
72
import contextlib import os import sqlitea import pytest from datasets import Dataset, Features, Value from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) 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 @require_sqlalchemy @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: lowercase : Union[str, Any] = tmp_path / """cache""" lowercase : Optional[Any] = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowercase : Any = SqlDatasetReader( """dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ , keep_in_memory=SCREAMING_SNAKE_CASE__ ).read() _check_sql_dataset(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @require_sqlalchemy @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 _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: lowercase : Union[str, Any] = tmp_path / """cache""" lowercase : int = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} lowercase : str = features.copy() if features else default_expected_features lowercase : Optional[Any] = ( Features({feature: Value(SCREAMING_SNAKE_CASE__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowercase : Optional[int] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , features=SCREAMING_SNAKE_CASE__ , cache_dir=SCREAMING_SNAKE_CASE__ ).read() _check_sql_dataset(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[Any]: with contextlib.closing(sqlitea.connect(SCREAMING_SNAKE_CASE__ ) ) as con: lowercase : Optional[int] = con.cursor() cur.execute("""SELECT * FROM dataset""" ) for row in cur: yield row @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> int: lowercase : Any = tmp_path / """cache""" lowercase : int = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : List[str] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=1 ).write() lowercase : List[str] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = iter_sql_file(SCREAMING_SNAKE_CASE__ ) for rowa, rowa in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): assert rowa == rowa @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: lowercase : Dict = tmp_path / """cache""" lowercase : List[str] = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : List[str] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=2 ).write() lowercase : Optional[int] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) for rowa, rowa in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): assert rowa == rowa @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : str = tmp_path / """cache""" lowercase : Optional[int] = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : Optional[Any] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() with pytest.raises(SCREAMING_SNAKE_CASE__ ): SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=0 ).write()
20
0
import io import os import unicodedata from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging a =logging.get_logger(__name__) a ="""▁""" a ={"""vocab_file""": """vocab.txt""", """sentencepiece_model_ckpt""": """sentencepiece.bpe.model"""} a ={ """sentencepiece_model_file""": """sentencepiece.bpe.model""", """vocab_file""": """vocab.txt""", } a ={ """vocab_file""": { """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""", """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""", }, """sentencepiece_model_file""": { """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""", """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""", }, } a ={ """ernie-m-base""": 514, """ernie-m-large""": 514, } a ={ """ernie-m-base""": {"""do_lower_case""": False}, """ernie-m-large""": {"""do_lower_case""": False}, } class A_ ( SCREAMING_SNAKE_CASE ): _UpperCAmelCase : List[str] = ["input_ids"] _UpperCAmelCase : Dict = VOCAB_FILES_NAMES _UpperCAmelCase : Any = PRETRAINED_INIT_CONFIGURATION _UpperCAmelCase : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCAmelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP _UpperCAmelCase : List[Any] = RESOURCE_FILES_NAMES def __init__( self : Dict ,SCREAMING_SNAKE_CASE__ : List[str] ,SCREAMING_SNAKE_CASE__ : Union[str, Any]=None ,SCREAMING_SNAKE_CASE__ : int=False ,SCREAMING_SNAKE_CASE__ : Union[str, Any]="utf8" ,SCREAMING_SNAKE_CASE__ : List[Any]="[UNK]" ,SCREAMING_SNAKE_CASE__ : Any="[SEP]" ,SCREAMING_SNAKE_CASE__ : Optional[Any]="[PAD]" ,SCREAMING_SNAKE_CASE__ : List[Any]="[CLS]" ,SCREAMING_SNAKE_CASE__ : Optional[Any]="[MASK]" ,SCREAMING_SNAKE_CASE__ : Optional[Dict[str, Any]] = None ,**SCREAMING_SNAKE_CASE__ : List[Any] ,): # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. __lowerCamelCase : Any = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=SCREAMING_SNAKE_CASE__ ,unk_token=SCREAMING_SNAKE_CASE__ ,sep_token=SCREAMING_SNAKE_CASE__ ,pad_token=SCREAMING_SNAKE_CASE__ ,cls_token=SCREAMING_SNAKE_CASE__ ,mask_token=SCREAMING_SNAKE_CASE__ ,vocab_file=SCREAMING_SNAKE_CASE__ ,encoding=SCREAMING_SNAKE_CASE__ ,sp_model_kwargs=self.sp_model_kwargs ,**SCREAMING_SNAKE_CASE__ ,) __lowerCamelCase : Tuple = do_lower_case __lowerCamelCase : Any = sentencepiece_model_ckpt __lowerCamelCase : Any = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(SCREAMING_SNAKE_CASE__) # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning if vocab_file is not None: __lowerCamelCase : Union[str, Any] = self.load_vocab(filepath=SCREAMING_SNAKE_CASE__) else: __lowerCamelCase : List[Any] = {self.sp_model.id_to_piece(SCREAMING_SNAKE_CASE__): id for id in range(self.sp_model.get_piece_size())} __lowerCamelCase : Any = {v: k for k, v in self.vocab.items()} def lowerCAmelCase ( self : Union[str, Any] ,SCREAMING_SNAKE_CASE__ : List[str]): if text is None: return None __lowerCamelCase : Optional[int] = self.tokenize(SCREAMING_SNAKE_CASE__) __lowerCamelCase , __lowerCamelCase : Optional[Any] = '', [] for i, ch in enumerate(SCREAMING_SNAKE_CASE__): if ch in self.SP_CHAR_MAPPING: __lowerCamelCase : Union[str, Any] = self.SP_CHAR_MAPPING.get(SCREAMING_SNAKE_CASE__) else: __lowerCamelCase : Optional[int] = unicodedata.normalize('NFKC' ,SCREAMING_SNAKE_CASE__) if self.is_whitespace(SCREAMING_SNAKE_CASE__): continue normalized_text += ch char_mapping.extend([i] * len(SCREAMING_SNAKE_CASE__)) __lowerCamelCase , __lowerCamelCase , __lowerCamelCase : int = normalized_text, [], 0 if self.do_lower_case: __lowerCamelCase : Optional[Any] = text.lower() for token in split_tokens: if token[:1] == "▁": __lowerCamelCase : Any = token[1:] __lowerCamelCase : Union[str, Any] = text[offset:].index(SCREAMING_SNAKE_CASE__) + offset __lowerCamelCase : Tuple = start + len(SCREAMING_SNAKE_CASE__) token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1)) __lowerCamelCase : List[Any] = end return token_mapping @property def lowerCAmelCase ( self : Optional[Any]): return len(self.vocab) def lowerCAmelCase ( self : Any): return dict(self.vocab ,**self.added_tokens_encoder) def __getstate__( self : int): __lowerCamelCase : List[str] = self.__dict__.copy() __lowerCamelCase : List[str] = None return state def __setstate__( self : Any ,SCREAMING_SNAKE_CASE__ : List[Any]): __lowerCamelCase : int = d # for backward compatibility if not hasattr(self ,'sp_model_kwargs'): __lowerCamelCase : int = {} __lowerCamelCase : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.sentencepiece_model_ckpt) def lowerCAmelCase ( self : Optional[int] ,SCREAMING_SNAKE_CASE__ : List[Any]): return "".join((self.SP_CHAR_MAPPING.get(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__) for c in text)) def lowerCAmelCase ( self : List[str] ,SCREAMING_SNAKE_CASE__ : Optional[int] ,SCREAMING_SNAKE_CASE__ : Optional[Any]=False ,SCREAMING_SNAKE_CASE__ : Dict=6_4 ,SCREAMING_SNAKE_CASE__ : Optional[Any]=0.1): if self.sp_model_kwargs.get('enable_sampling') is True: __lowerCamelCase : Optional[int] = True if self.sp_model_kwargs.get('alpha') is not None: __lowerCamelCase : Any = self.sp_model_kwargs.get('alpha') if self.sp_model_kwargs.get('nbest_size') is not None: __lowerCamelCase : Tuple = self.sp_model_kwargs.get('nbest_size') if not enable_sampling: __lowerCamelCase : Dict = self.sp_model.EncodeAsPieces(SCREAMING_SNAKE_CASE__) else: __lowerCamelCase : Optional[int] = self.sp_model.SampleEncodeAsPieces(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__) __lowerCamelCase : Optional[Any] = [] for pi, piece in enumerate(SCREAMING_SNAKE_CASE__): if piece == SPIECE_UNDERLINE: if not pieces[pi + 1].startswith(SCREAMING_SNAKE_CASE__) and pi != 0: new_pieces.append(SCREAMING_SNAKE_CASE__) continue else: continue __lowerCamelCase : List[Any] = 0 for i, chunk in enumerate(SCREAMING_SNAKE_CASE__): if chunk == SPIECE_UNDERLINE: continue if self.is_ch_char(SCREAMING_SNAKE_CASE__) or self.is_punct(SCREAMING_SNAKE_CASE__): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i]) new_pieces.append(SCREAMING_SNAKE_CASE__) __lowerCamelCase : Optional[int] = i + 1 elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i]) __lowerCamelCase : Any = i elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i]) __lowerCamelCase : Dict = i if len(SCREAMING_SNAKE_CASE__) > lst_i: new_pieces.append(piece[lst_i:]) return new_pieces def lowerCAmelCase ( self : Tuple ,SCREAMING_SNAKE_CASE__ : int): __lowerCamelCase : List[str] = ''.join(SCREAMING_SNAKE_CASE__).replace(SCREAMING_SNAKE_CASE__ ,' ').strip() return out_string def lowerCAmelCase ( self : int ,SCREAMING_SNAKE_CASE__ : int): __lowerCamelCase : str = self.convert_ids_to_tokens(SCREAMING_SNAKE_CASE__) __lowerCamelCase : str = ''.join(SCREAMING_SNAKE_CASE__).replace(SCREAMING_SNAKE_CASE__ ,' ').strip() return out_string def lowerCAmelCase ( self : Optional[Any] ,SCREAMING_SNAKE_CASE__ : List[Any]): return self.vocab.get(SCREAMING_SNAKE_CASE__ ,self.vocab.get(self.unk_token)) def lowerCAmelCase ( self : str ,SCREAMING_SNAKE_CASE__ : List[str]): return self.reverse_vocab.get(SCREAMING_SNAKE_CASE__ ,self.unk_token) def lowerCAmelCase ( self : Dict ,SCREAMING_SNAKE_CASE__ : Optional[Any] ,SCREAMING_SNAKE_CASE__ : Optional[Any]=None): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __lowerCamelCase : Dict = [self.cls_token_id] __lowerCamelCase : Dict = [self.sep_token_id] return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep def lowerCAmelCase ( self : List[str] ,SCREAMING_SNAKE_CASE__ : Optional[Any] ,SCREAMING_SNAKE_CASE__ : int=None): if offset_mapping_a is None: return [(0, 0)] + offset_mapping_a + [(0, 0)] return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)] def lowerCAmelCase ( self : Optional[Any] ,SCREAMING_SNAKE_CASE__ : Dict ,SCREAMING_SNAKE_CASE__ : Union[str, Any]=None ,SCREAMING_SNAKE_CASE__ : List[Any]=False): if already_has_special_tokens: if token_ids_a is not None: raise ValueError( 'You should not supply a second sequence if the provided sequence of ' 'ids is already formatted with special tokens for the model.') return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(SCREAMING_SNAKE_CASE__)) + [1, 1] + ([0] * len(SCREAMING_SNAKE_CASE__)) + [1] return [1] + ([0] * len(SCREAMING_SNAKE_CASE__)) + [1] def lowerCAmelCase ( self : Any ,SCREAMING_SNAKE_CASE__ : List[int] ,SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None): # called when `add_special_tokens` is True, so align with `build_inputs_with_special_tokens` method if token_ids_a is None: # [CLS] X [SEP] return (len(SCREAMING_SNAKE_CASE__) + 2) * [0] # [CLS] A [SEP] [SEP] B [SEP] return [0] * (len(SCREAMING_SNAKE_CASE__) + 1) + [1] * (len(SCREAMING_SNAKE_CASE__) + 3) def lowerCAmelCase ( self : Tuple ,SCREAMING_SNAKE_CASE__ : Optional[Any]): if "\u4e00" <= char <= "\u9fff": return True return False def lowerCAmelCase ( self : List[Any] ,SCREAMING_SNAKE_CASE__ : Optional[int]): if ("a" <= char <= "z") or ("A" <= char <= "Z"): return True return False def lowerCAmelCase ( self : Optional[Any] ,SCREAMING_SNAKE_CASE__ : Any): if char in ",;:.?!~,;:。?!《》【】": return True return False def lowerCAmelCase ( self : Any ,SCREAMING_SNAKE_CASE__ : str): if char == " " or char == "\t" or char == "\n" or char == "\r": return True if len(SCREAMING_SNAKE_CASE__) == 1: __lowerCamelCase : int = unicodedata.category(SCREAMING_SNAKE_CASE__) if cat == "Zs": return True return False def lowerCAmelCase ( self : str ,SCREAMING_SNAKE_CASE__ : Union[str, Any]): __lowerCamelCase : Any = {} with io.open(SCREAMING_SNAKE_CASE__ ,'r' ,encoding='utf-8') as f: for index, line in enumerate(SCREAMING_SNAKE_CASE__): __lowerCamelCase : int = line.rstrip('\n') __lowerCamelCase : str = int(SCREAMING_SNAKE_CASE__) return token_to_idx def lowerCAmelCase ( self : str ,SCREAMING_SNAKE_CASE__ : str ,SCREAMING_SNAKE_CASE__ : Optional[str] = None): __lowerCamelCase : List[Any] = 0 if os.path.isdir(SCREAMING_SNAKE_CASE__): __lowerCamelCase : Union[str, Any] = os.path.join( SCREAMING_SNAKE_CASE__ ,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']) else: __lowerCamelCase : str = (filename_prefix + '-' if filename_prefix else '') + save_directory with open(SCREAMING_SNAKE_CASE__ ,'w' ,encoding='utf-8') as writer: for token, token_index in sorted(self.vocab.items() ,key=lambda SCREAMING_SNAKE_CASE__: kv[1]): if index != token_index: logger.warning( F"Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive." ' Please check that the vocabulary is not corrupted!') __lowerCamelCase : Dict = token_index writer.write(token + '\n') index += 1 __lowerCamelCase : Any = os.path.join(SCREAMING_SNAKE_CASE__ ,'sentencepiece.bpe.model') with open(SCREAMING_SNAKE_CASE__ ,'wb') as fi: __lowerCamelCase : Dict = self.sp_model.serialized_model_proto() fi.write(SCREAMING_SNAKE_CASE__) return (vocab_file,)
73
import os import numpy import onnx def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: lowercase : int = a.name lowercase : Any = b.name lowercase : Optional[Any] = """""" lowercase : Dict = """""" lowercase : int = a == b lowercase : int = name_a lowercase : List[str] = name_b return res def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: for i, input_name in enumerate(node_proto.input ): if input_name == name: node_proto.input.insert(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) node_proto.input.pop(i + 1 ) if node_proto.op_type == "If": _graph_replace_input_with(node_proto.attribute[0].g , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) _graph_replace_input_with(node_proto.attribute[1].g , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if node_proto.op_type == "Loop": _graph_replace_input_with(node_proto.attribute[0].g , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: for n in graph_proto.node: _node_replace_input_with(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[Any]: lowercase : Any = list(model.graph.initializer ) lowercase : Dict = list(model_without_ext.graph.initializer ) for i, ref_i in ind_to_replace: assert inits_with_data[i].name == inits[i].name assert inits_with_data[ref_i].name == inits[ref_i].name assert i > ref_i lowercase : Union[str, Any] = inits[i].name lowercase : Dict = inits[ref_i].name model_without_ext.graph.initializer.remove(inits[i] ) # for n in model.graph.node: _graph_replace_input_with(model_without_ext.graph , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : Union[str, Any] = os.path.dirname(SCREAMING_SNAKE_CASE__ ) lowercase : Dict = os.path.basename(SCREAMING_SNAKE_CASE__ ) lowercase : str = onnx.load(os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) lowercase : List[str] = list(model.graph.initializer ) lowercase : Tuple = set() lowercase : int = {} lowercase : Optional[Any] = [] lowercase : Dict = 0 for i in range(len(SCREAMING_SNAKE_CASE__ ) ): if i in dup_set: continue for j in range(i + 1 , len(SCREAMING_SNAKE_CASE__ ) ): if j in dup_set: continue if _is_equal_tensor_proto(inits[i] , inits[j] ): dup_set.add(SCREAMING_SNAKE_CASE__ ) dup_set.add(SCREAMING_SNAKE_CASE__ ) lowercase : int = inits[j].data_type lowercase : Optional[int] = numpy.prod(inits[j].dims ) if dtype == 1: mem_size *= 4 elif dtype == 6: mem_size *= 4 elif dtype == 7 or dtype == 11: mem_size *= 8 else: print("""unexpected data type: """ , SCREAMING_SNAKE_CASE__ ) total_reduced_size += mem_size lowercase : Tuple = inits[i].name lowercase : int = inits[j].name if name_i in dup_map: dup_map[name_i].append(SCREAMING_SNAKE_CASE__ ) else: lowercase : List[str] = [name_j] ind_to_replace.append((j, i) ) print("""total reduced size: """ , total_reduced_size / 1_024 / 1_024 / 1_024 , """GB""" ) lowercase : str = sorted(SCREAMING_SNAKE_CASE__ ) _remove_dup_initializers_from_model(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = """optimized_""" + model_file_name lowercase : Dict = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) onnx.save(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return new_model
20
0
"""simple docstring""" from sklearn.metrics import recall_score import datasets _lowercase = ''' Recall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation: Recall = TP / (TP + FN) Where TP is the true positives and FN is the false negatives. ''' _lowercase = ''' Args: - **predictions** (`list` of `int`): The predicted labels. - **references** (`list` of `int`): The ground truth labels. - **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None. - **pos_label** (`int`): The class label to use as the \'positive class\' when calculating the recall. Defaults to `1`. - **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`. - `\'binary\'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary. - `\'micro\'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives. - `\'macro\'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - `\'weighted\'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall. - `\'samples\'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). - **sample_weight** (`list` of `float`): Sample weights Defaults to `None`. - **zero_division** (): Sets the value to return when there is a zero division. Defaults to . - `\'warn\'`: If there is a zero division, the return value is `0`, but warnings are also raised. - `0`: If there is a zero division, the return value is `0`. - `1`: If there is a zero division, the return value is `1`. Returns: - **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better. Examples: Example 1-A simple example with some errors >>> recall_metric = datasets.load_metric(\'recall\') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1]) >>> print(results) {\'recall\': 0.6666666666666666} Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`. >>> recall_metric = datasets.load_metric(\'recall\') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0) >>> print(results) {\'recall\': 0.5} Example 3-The same example as Example 1, but with `sample_weight` included. >>> recall_metric = datasets.load_metric(\'recall\') >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8] >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight) >>> print(results) {\'recall\': 0.55} Example 4-A multiclass example, using different averages. >>> recall_metric = datasets.load_metric(\'recall\') >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = recall_metric.compute(predictions=predictions, references=references, average=\'macro\') >>> print(results) {\'recall\': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average=\'micro\') >>> print(results) {\'recall\': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average=\'weighted\') >>> print(results) {\'recall\': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {\'recall\': array([1., 0., 0.])} ''' _lowercase = ''' @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCAmelCase_ ( datasets.Metric ): '''simple docstring''' def _SCREAMING_SNAKE_CASE ( self : Tuple ) -> Any: return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('int32' ) ), 'references': datasets.Sequence(datasets.Value('int32' ) ), } if self.config_name == 'multilabel' else { 'predictions': datasets.Value('int32' ), 'references': datasets.Value('int32' ), } ) ,reference_urls=['https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html'] ,) def _SCREAMING_SNAKE_CASE ( self : Optional[int] ,A_ : Tuple ,A_ : List[Any] ,A_ : Optional[int]=None ,A_ : Optional[int]=1 ,A_ : Tuple="binary" ,A_ : Union[str, Any]=None ,A_ : str="warn" ,) -> Dict: A = recall_score( A_ ,A_ ,labels=A_ ,pos_label=A_ ,average=A_ ,sample_weight=A_ ,zero_division=A_ ,) return {"recall": float(A_ ) if score.size == 1 else score}
74
import argparse import json from collections import OrderedDict import torch from huggingface_hub import cached_download, hf_hub_url from transformers import AutoImageProcessor, CvtConfig, CvtForImageClassification def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Tuple: lowercase : Union[str, Any] = [] embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.weight", f"stage{idx}.patch_embed.proj.weight", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.bias", f"stage{idx}.patch_embed.proj.bias", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.weight", f"stage{idx}.patch_embed.norm.weight", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.bias", f"stage{idx}.patch_embed.norm.bias", ) ) return embed def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: lowercase : Optional[Any] = [] attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.weight", f"stage{idx}.blocks.{cnt}.attn.proj_q.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.bias", f"stage{idx}.blocks.{cnt}.attn.proj_q.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.weight", f"stage{idx}.blocks.{cnt}.attn.proj_k.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.bias", f"stage{idx}.blocks.{cnt}.attn.proj_k.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.weight", f"stage{idx}.blocks.{cnt}.attn.proj_v.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.bias", f"stage{idx}.blocks.{cnt}.attn.proj_v.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.weight", f"stage{idx}.blocks.{cnt}.attn.proj.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.bias", f"stage{idx}.blocks.{cnt}.attn.proj.bias", ) ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.weight", f"stage{idx}.blocks.{cnt}.mlp.fc1.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.bias", f"stage{idx}.blocks.{cnt}.mlp.fc1.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.weight", f"stage{idx}.blocks.{cnt}.mlp.fc2.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.bias", f"stage{idx}.blocks.{cnt}.mlp.fc2.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.weight", f"stage{idx}.blocks.{cnt}.norm1.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.bias", f"stage{idx}.blocks.{cnt}.norm1.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.weight", f"stage{idx}.blocks.{cnt}.norm2.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.bias", f"stage{idx}.blocks.{cnt}.norm2.bias") ) return attention_weights def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: lowercase : Optional[Any] = [] token.append((f"cvt.encoder.stages.{idx}.cls_token", """stage2.cls_token""") ) return token def _snake_case( ) -> Dict: lowercase : Optional[Any] = [] head.append(("""layernorm.weight""", """norm.weight""") ) head.append(("""layernorm.bias""", """norm.bias""") ) head.append(("""classifier.weight""", """head.weight""") ) head.append(("""classifier.bias""", """head.bias""") ) return head def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: lowercase : Any = """imagenet-1k-id2label.json""" lowercase : List[str] = 1_000 lowercase : int = """huggingface/label-files""" lowercase : Union[str, Any] = num_labels lowercase : Optional[Any] = json.load(open(cached_download(hf_hub_url(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , repo_type="""dataset""" ) ) , """r""" ) ) lowercase : List[Any] = {int(SCREAMING_SNAKE_CASE__ ): v for k, v in idalabel.items()} lowercase : Dict = idalabel lowercase : List[str] = {v: k for k, v in idalabel.items()} lowercase : List[str] = CvtConfig(num_labels=SCREAMING_SNAKE_CASE__ , idalabel=SCREAMING_SNAKE_CASE__ , labelaid=SCREAMING_SNAKE_CASE__ ) # For depth size 13 (13 = 1+2+10) if cvt_model.rsplit("""/""" , 1 )[-1][4:6] == "13": lowercase : Tuple = [1, 2, 10] # For depth size 21 (21 = 1+4+16) elif cvt_model.rsplit("""/""" , 1 )[-1][4:6] == "21": lowercase : Dict = [1, 4, 16] # For wide cvt (similar to wide-resnet) depth size 24 (w24 = 2 + 2 20) else: lowercase : int = [2, 2, 20] lowercase : Optional[int] = [3, 12, 16] lowercase : str = [192, 768, 1_024] lowercase : Union[str, Any] = CvtForImageClassification(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = AutoImageProcessor.from_pretrained("""facebook/convnext-base-224-22k-1k""" ) lowercase : Optional[Any] = image_size lowercase : Union[str, Any] = torch.load(SCREAMING_SNAKE_CASE__ , map_location=torch.device("""cpu""" ) ) lowercase : Optional[Any] = OrderedDict() lowercase : Tuple = [] for idx in range(len(config.depth ) ): if config.cls_token[idx]: lowercase : Optional[Any] = list_of_state_dict + cls_token(SCREAMING_SNAKE_CASE__ ) lowercase : str = list_of_state_dict + embeddings(SCREAMING_SNAKE_CASE__ ) for cnt in range(config.depth[idx] ): lowercase : List[str] = list_of_state_dict + attention(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = list_of_state_dict + final() for gg in list_of_state_dict: print(SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) ): lowercase : Optional[Any] = original_weights[list_of_state_dict[i][1]] model.load_state_dict(SCREAMING_SNAKE_CASE__ ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) # Download the weights from zoo: https://1drv.ms/u/s!AhIXJn_J-blW9RzF3rMW7SsLHa8h?e=blQ0Al if __name__ == "__main__": lowercase : Tuple = argparse.ArgumentParser() parser.add_argument( """--cvt_model""", default="""cvt-w24""", type=str, help="""Name of the cvt model you'd like to convert.""", ) parser.add_argument( """--image_size""", default=384, type=int, help="""Input Image Size""", ) parser.add_argument( """--cvt_file_name""", default=R"""cvtmodels\CvT-w24-384x384-IN-22k.pth""", type=str, help="""Input Image Size""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) lowercase : Optional[int] = parser.parse_args() convert_cvt_checkpoint(args.cvt_model, args.image_size, args.cvt_file_name, args.pytorch_dump_folder_path)
20
0
'''simple docstring''' from typing import List import jiwer import jiwer.transforms as tr from packaging import version import datasets from datasets.config import PY_VERSION if PY_VERSION < version.parse("""3.8"""): import importlib_metadata else: import importlib.metadata as importlib_metadata a_ : Optional[Any] = """""" if version.parse(importlib_metadata.version("""jiwer""")) < version.parse("""2.3.0"""): class __UpperCamelCase ( tr.AbstractTransform ): def __init__( self, lowerCAmelCase = " " ): """simple docstring""" lowerCamelCase_ =sentence_delimiter def lowercase__ ( self, lowerCAmelCase ): """simple docstring""" return list(lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =[] for sent_idx, sentence in enumerate(lowerCAmelCase ): chars.extend(self.process_string(lowerCAmelCase ) ) if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(lowerCAmelCase ) - 1: chars.append(self.sentence_delimiter ) return chars a_ : Tuple = tr.Compose( [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)] ) else: a_ : Optional[Any] = tr.Compose( [ tr.RemoveMultipleSpaces(), tr.Strip(), tr.ReduceToSingleSentence(SENTENCE_DELIMITER), tr.ReduceToListOfListOfChars(), ] ) a_ : List[Any] = """\ @inproceedings{inproceedings, author = {Morris, Andrew and Maier, Viktoria and Green, Phil}, year = {2004}, month = {01}, pages = {}, title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.} } """ a_ : int = """\ Character error rate (CER) is a common metric of the performance of an automatic speech recognition system. CER is similar to Word Error Rate (WER), but operates on character instead of word. Please refer to docs of WER for further information. Character error rate can be computed as: CER = (S + D + I) / N = (S + D + I) / (S + D + C) where S is the number of substitutions, D is the number of deletions, I is the number of insertions, C is the number of correct characters, N is the number of characters in the reference (N=S+D+C). CER's output is not always a number between 0 and 1, in particular when there is a high number of insertions. This value is often associated to the percentage of characters that were incorrectly predicted. The lower the value, the better the performance of the ASR system with a CER of 0 being a perfect score. """ a_ : List[Any] = """ Computes CER score of transcribed segments against references. Args: references: list of references for each speech input. predictions: list of transcribtions to score. concatenate_texts: Whether or not to concatenate sentences before evaluation, set to True for more accurate result. Returns: (float): the character error rate Examples: >>> predictions = [\"this is the prediction\", \"there is an other sample\"] >>> references = [\"this is the reference\", \"there is another one\"] >>> cer = datasets.load_metric(\"cer\") >>> cer_score = cer.compute(predictions=predictions, references=references) >>> print(cer_score) 0.34146341463414637 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCamelCase ( datasets.Metric ): def lowercase__ ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features( { '''predictions''': datasets.Value('''string''', id='''sequence''' ), '''references''': datasets.Value('''string''', id='''sequence''' ), } ), codebase_urls=['''https://github.com/jitsi/jiwer/'''], reference_urls=[ '''https://en.wikipedia.org/wiki/Word_error_rate''', '''https://sites.google.com/site/textdigitisation/qualitymeasures/computingerrorrates''', ], ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase=False ): """simple docstring""" if concatenate_texts: return jiwer.compute_measures( lowerCAmelCase, lowerCAmelCase, truth_transform=lowerCAmelCase, hypothesis_transform=lowerCAmelCase, )["wer"] lowerCamelCase_ =0 lowerCamelCase_ =0 for prediction, reference in zip(lowerCAmelCase, lowerCAmelCase ): lowerCamelCase_ =jiwer.compute_measures( lowerCAmelCase, lowerCAmelCase, truth_transform=lowerCAmelCase, hypothesis_transform=lowerCAmelCase, ) incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"] total += measures["substitutions"] + measures["deletions"] + measures["hits"] return incorrect / total
75
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor from ..utils import is_datasets_available from .base import PipelineTool if is_datasets_available(): from datasets import load_dataset class __snake_case ( lowerCAmelCase ): _a : Union[str, Any]= "microsoft/speecht5_tts" _a : Tuple= ( "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the " "text to read (in English) and returns a waveform object containing the sound." ) _a : Dict= "text_reader" _a : Optional[Any]= SpeechTaProcessor _a : Tuple= SpeechTaForTextToSpeech _a : Optional[int]= SpeechTaHifiGan _a : Union[str, Any]= ["text"] _a : Optional[int]= ["audio"] def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' if self.post_processor is None: lowercase : Any = """microsoft/speecht5_hifigan""" super().setup() def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ): '''simple docstring''' lowercase : int = self.pre_processor(text=snake_case ,return_tensors="""pt""" ,truncation=snake_case ) if speaker_embeddings is None: if not is_datasets_available(): raise ImportError("""Datasets needs to be installed if not passing speaker embeddings.""" ) lowercase : Tuple = load_dataset("""Matthijs/cmu-arctic-xvectors""" ,split="""validation""" ) lowercase : List[str] = torch.tensor(embeddings_dataset[7305]["""xvector"""] ).unsqueeze(0 ) return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings} def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' with torch.no_grad(): return self.model.generate_speech(**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' with torch.no_grad(): return self.post_processor(snake_case ).cpu().detach()
20
0
from typing import Optional, Tuple import jax import jax.numpy as jnp from flax import linen as nn from flax.core.frozen_dict import FrozenDict from transformers import CLIPConfig, FlaxPreTrainedModel from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule def lowerCamelCase__ ( _a , _a , _a=1E-12): SCREAMING_SNAKE_CASE : Optional[Any] = jnp.divide(emb_a.T , jnp.clip(jnp.linalg.norm(_a , axis=1) , a_min=_a)).T SCREAMING_SNAKE_CASE : Any = jnp.divide(emb_a.T , jnp.clip(jnp.linalg.norm(_a , axis=1) , a_min=_a)).T return jnp.matmul(_a , norm_emb_a.T) class _UpperCamelCase ( nn.Module ): '''simple docstring''' lowerCamelCase__ =42 lowerCamelCase__ =jnp.floataa def __UpperCamelCase ( self : List[Any] ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE : Dict = FlaxCLIPVisionModule(self.config.vision_config ) SCREAMING_SNAKE_CASE : Any = nn.Dense(self.config.projection_dim , use_bias=a , dtype=self.dtype ) SCREAMING_SNAKE_CASE : str = self.param("concept_embeds" , jax.nn.initializers.ones , (17, self.config.projection_dim) ) SCREAMING_SNAKE_CASE : Optional[int] = self.param( "special_care_embeds" , jax.nn.initializers.ones , (3, self.config.projection_dim) ) SCREAMING_SNAKE_CASE : List[Any] = self.param("concept_embeds_weights" , jax.nn.initializers.ones , (17,) ) SCREAMING_SNAKE_CASE : Any = self.param("special_care_embeds_weights" , jax.nn.initializers.ones , (3,) ) def __call__( self : Optional[Any] , a : List[str] ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE : Union[str, Any] = self.vision_model(a )[1] SCREAMING_SNAKE_CASE : Optional[Any] = self.visual_projection(a ) SCREAMING_SNAKE_CASE : List[Any] = jax_cosine_distance(a , self.special_care_embeds ) SCREAMING_SNAKE_CASE : str = jax_cosine_distance(a , self.concept_embeds ) # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign image inputs SCREAMING_SNAKE_CASE : Optional[int] = 0.0 SCREAMING_SNAKE_CASE : List[str] = special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment SCREAMING_SNAKE_CASE : str = jnp.round(a , 3 ) SCREAMING_SNAKE_CASE : Optional[Any] = jnp.any(special_scores > 0 , axis=1 , keepdims=a ) # Use a lower threshold if an image has any special care concept SCREAMING_SNAKE_CASE : Union[str, Any] = is_special_care * 0.01 SCREAMING_SNAKE_CASE : Union[str, Any] = cos_dist - self.concept_embeds_weights[None, :] + special_adjustment SCREAMING_SNAKE_CASE : Dict = jnp.round(a , 3 ) SCREAMING_SNAKE_CASE : int = jnp.any(concept_scores > 0 , axis=1 ) return has_nsfw_concepts class _UpperCamelCase ( __A ): '''simple docstring''' lowerCamelCase__ =CLIPConfig lowerCamelCase__ ='clip_input' lowerCamelCase__ =FlaxStableDiffusionSafetyCheckerModule def __init__( self : str , a : CLIPConfig , a : Optional[Tuple] = None , a : int = 0 , a : jnp.dtype = jnp.floataa , a : bool = True , **a : str , ) -> int: """simple docstring""" if input_shape is None: SCREAMING_SNAKE_CASE : List[Any] = (1, 224, 224, 3) SCREAMING_SNAKE_CASE : Optional[int] = self.module_class(config=a , dtype=a , **a ) super().__init__(a , a , input_shape=a , seed=a , dtype=a , _do_init=_do_init ) def __UpperCamelCase ( self : str , a : jax.random.KeyArray , a : Tuple , a : FrozenDict = None ) -> FrozenDict: """simple docstring""" SCREAMING_SNAKE_CASE : Union[str, Any] = jax.random.normal(a , a ) SCREAMING_SNAKE_CASE ,SCREAMING_SNAKE_CASE : Tuple = jax.random.split(a ) SCREAMING_SNAKE_CASE : Union[str, Any] = {"params": params_rng, "dropout": dropout_rng} SCREAMING_SNAKE_CASE : int = self.module.init(a , a )["params"] return random_params def __call__( self : List[Any] , a : Optional[int] , a : dict = None , ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE : Any = jnp.transpose(a , (0, 2, 3, 1) ) return self.module.apply( {"params": params or self.params} , jnp.array(a , dtype=jnp.floataa ) , rngs={} , )
76
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase : str = { """configuration_funnel""": ["""FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FunnelConfig"""], """convert_funnel_original_tf_checkpoint_to_pytorch""": [], """tokenization_funnel""": ["""FunnelTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Tuple = ["""FunnelTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : List[str] = [ """FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""", """FunnelBaseModel""", """FunnelForMaskedLM""", """FunnelForMultipleChoice""", """FunnelForPreTraining""", """FunnelForQuestionAnswering""", """FunnelForSequenceClassification""", """FunnelForTokenClassification""", """FunnelModel""", """FunnelPreTrainedModel""", """load_tf_weights_in_funnel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Optional[Any] = [ """TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFFunnelBaseModel""", """TFFunnelForMaskedLM""", """TFFunnelForMultipleChoice""", """TFFunnelForPreTraining""", """TFFunnelForQuestionAnswering""", """TFFunnelForSequenceClassification""", """TFFunnelForTokenClassification""", """TFFunnelModel""", """TFFunnelPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig from .tokenization_funnel import FunnelTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_funnel_fast import FunnelTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_funnel import ( FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, FunnelBaseModel, FunnelForMaskedLM, FunnelForMultipleChoice, FunnelForPreTraining, FunnelForQuestionAnswering, FunnelForSequenceClassification, FunnelForTokenClassification, FunnelModel, FunnelPreTrainedModel, load_tf_weights_in_funnel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_funnel import ( TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, TFFunnelPreTrainedModel, ) else: import sys lowercase : Any = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
20
0
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _UpperCamelCase : str = logging.get_logger(__name__) _UpperCamelCase : Tuple = { "facebook/data2vec-vision-base-ft": ( "https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json" ), } class UpperCAmelCase_ ( _a): lowerCamelCase__ : int = "data2vec-vision" def __init__( self , a=7_6_8 , a=1_2 , a=1_2 , a=3_0_7_2 , a="gelu" , a=0.0 , a=0.0 , a=0.02 , a=1e-12 , a=2_2_4 , a=1_6 , a=3 , a=False , a=False , a=False , a=False , a=0.1 , a=0.1 , a=True , a=[3, 5, 7, 1_1] , a=[1, 2, 3, 6] , a=True , a=0.4 , a=2_5_6 , a=1 , a=False , a=2_5_5 , **a , ) -> Optional[int]: super().__init__(**a ) lowercase__ : Any = hidden_size lowercase__ : Tuple = num_hidden_layers lowercase__ : List[str] = num_attention_heads lowercase__ : Tuple = intermediate_size lowercase__ : Dict = hidden_act lowercase__ : Any = hidden_dropout_prob lowercase__ : Optional[Any] = attention_probs_dropout_prob lowercase__ : Tuple = initializer_range lowercase__ : Tuple = layer_norm_eps lowercase__ : Union[str, Any] = image_size lowercase__ : str = patch_size lowercase__ : Optional[int] = num_channels lowercase__ : Dict = use_mask_token lowercase__ : List[str] = use_absolute_position_embeddings lowercase__ : List[Any] = use_relative_position_bias lowercase__ : List[str] = use_shared_relative_position_bias lowercase__ : Tuple = layer_scale_init_value lowercase__ : int = drop_path_rate lowercase__ : List[str] = use_mean_pooling # decode head attributes (semantic segmentation) lowercase__ : Tuple = out_indices lowercase__ : Dict = pool_scales # auxiliary head attributes (semantic segmentation) lowercase__ : Optional[int] = use_auxiliary_head lowercase__ : int = auxiliary_loss_weight lowercase__ : List[str] = auxiliary_channels lowercase__ : str = auxiliary_num_convs lowercase__ : int = auxiliary_concat_input lowercase__ : Dict = semantic_loss_ignore_index class UpperCAmelCase_ ( _a): lowerCamelCase__ : Union[str, Any] = version.parse("1.11") @property def _UpperCAmelCase ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def _UpperCAmelCase ( self ) -> float: return 1e-4
77
import os import zipfile import pytest from datasets.utils.extract import ( BzipaExtractor, Extractor, GzipExtractor, LzaExtractor, SevenZipExtractor, TarExtractor, XzExtractor, ZipExtractor, ZstdExtractor, ) from .utils import require_lza, require_pyazr, require_zstandard @pytest.mark.parametrize( """compression_format, is_archive""" , [ ("""7z""", True), ("""bz2""", False), ("""gzip""", False), ("""lz4""", False), ("""tar""", True), ("""xz""", False), ("""zip""", True), ("""zstd""", False), ] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) -> Any: lowercase : Dict = { """7z""": (seven_zip_file, SevenZipExtractor), """bz2""": (bza_file, BzipaExtractor), """gzip""": (gz_file, GzipExtractor), """lz4""": (lza_file, LzaExtractor), """tar""": (tar_file, TarExtractor), """xz""": (xz_file, XzExtractor), """zip""": (zip_file, ZipExtractor), """zstd""": (zstd_file, ZstdExtractor), } lowercase , lowercase : Optional[Any] = input_paths_and_base_extractors[compression_format] if input_path is None: lowercase : Dict = f"for '{compression_format}' compression_format, " if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(SCREAMING_SNAKE_CASE__ ) assert base_extractor.is_extractable(SCREAMING_SNAKE_CASE__ ) lowercase : Any = tmp_path / ("""extracted""" if is_archive else """extracted.txt""") base_extractor.extract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name lowercase : str = file_path.read_text(encoding="""utf-8""" ) else: lowercase : Optional[Any] = output_path.read_text(encoding="""utf-8""" ) lowercase : Tuple = text_file.read_text(encoding="""utf-8""" ) assert extracted_file_content == expected_file_content @pytest.mark.parametrize( """compression_format, is_archive""" , [ ("""7z""", True), ("""bz2""", False), ("""gzip""", False), ("""lz4""", False), ("""tar""", True), ("""xz""", False), ("""zip""", True), ("""zstd""", False), ] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) -> Dict: lowercase : str = { """7z""": seven_zip_file, """bz2""": bza_file, """gzip""": gz_file, """lz4""": lza_file, """tar""": tar_file, """xz""": xz_file, """zip""": zip_file, """zstd""": zstd_file, } lowercase : Optional[Any] = input_paths[compression_format] if input_path is None: lowercase : int = f"for '{compression_format}' compression_format, " if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = Extractor.infer_extractor_format(SCREAMING_SNAKE_CASE__ ) assert extractor_format is not None lowercase : Any = tmp_path / ("""extracted""" if is_archive else """extracted.txt""") Extractor.extract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name lowercase : Dict = file_path.read_text(encoding="""utf-8""" ) else: lowercase : int = output_path.read_text(encoding="""utf-8""" ) lowercase : Optional[Any] = text_file.read_text(encoding="""utf-8""" ) assert extracted_file_content == expected_file_content @pytest.fixture def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: import tarfile lowercase : Tuple = tmp_path / """data_dot_dot""" directory.mkdir() lowercase : str = directory / """tar_file_with_dot_dot.tar""" with tarfile.TarFile(SCREAMING_SNAKE_CASE__ , """w""" ) as f: f.add(SCREAMING_SNAKE_CASE__ , arcname=os.path.join("""..""" , text_file.name ) ) return path @pytest.fixture def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[str]: import tarfile lowercase : Tuple = tmp_path / """data_sym_link""" directory.mkdir() lowercase : int = directory / """tar_file_with_sym_link.tar""" os.symlink("""..""" , directory / """subdir""" , target_is_directory=SCREAMING_SNAKE_CASE__ ) with tarfile.TarFile(SCREAMING_SNAKE_CASE__ , """w""" ) as f: f.add(str(directory / """subdir""" ) , arcname="""subdir""" ) # str required by os.readlink on Windows and Python < 3.8 return path @pytest.mark.parametrize( """insecure_tar_file, error_log""" , [("""tar_file_with_dot_dot""", """illegal path"""), ("""tar_file_with_sym_link""", """Symlink""")] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: lowercase : List[Any] = { """tar_file_with_dot_dot""": tar_file_with_dot_dot, """tar_file_with_sym_link""": tar_file_with_sym_link, } lowercase : Optional[int] = insecure_tar_files[insecure_tar_file] lowercase : List[str] = tmp_path / """extracted""" TarExtractor.extract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) assert caplog.text for record in caplog.records: assert record.levelname == "ERROR" assert error_log in record.msg def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[int]: # We should have less false positives than zipfile.is_zipfile # We do that by checking only the magic number lowercase : Any = tmpdir / """not_a_zip_file""" # From: https://github.com/python/cpython/pull/5053 lowercase : str = ( B"""\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00""" B"""\x00\x02\x08\x06\x00\x00\x00\x99\x81\xb6'\x00\x00\x00\x15I""" B"""DATx\x01\x01\n\x00\xf5\xff\x00PK\x05\x06\x00PK\x06\x06\x07""" B"""\xac\x01N\xc6|a\r\x00\x00\x00\x00IEND\xaeB`\x82""" ) with not_a_zip_file.open("""wb""" ) as f: f.write(SCREAMING_SNAKE_CASE__ ) assert zipfile.is_zipfile(str(SCREAMING_SNAKE_CASE__ ) ) # is a false positive for `zipfile` assert not ZipExtractor.is_extractable(SCREAMING_SNAKE_CASE__ ) # but we're right
20
0
"""simple docstring""" import logging import os import sys from dataclasses import dataclass, field from typing import Optional import evaluate import numpy as np import torch from datasets import load_dataset from PIL import Image from torchvision.transforms import ( CenterCrop, Compose, Normalize, RandomHorizontalFlip, RandomResizedCrop, Resize, ToTensor, ) import transformers from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, AutoConfig, AutoImageProcessor, AutoModelForImageClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version snake_case_ = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("""4.31.0""") require_version("""datasets>=1.8.0""", """To fix: pip install -r examples/pytorch/image-classification/requirements.txt""") snake_case_ = list(MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING.keys()) snake_case_ = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) def _lowerCAmelCase ( lowercase_ ): with open(lowercase_ , 'rb' ) as f: UpperCAmelCase = Image.open(lowercase_ ) return im.convert('RGB' ) @dataclass class A_ : """simple docstring""" __UpperCamelCase = field( default=SCREAMING_SNAKE_CASE_ , metadata={ """help""": """Name of a dataset from the hub (could be your own, possibly private dataset hosted on the hub).""" } , ) __UpperCamelCase = field( default=SCREAMING_SNAKE_CASE_ , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) __UpperCamelCase = field(default=SCREAMING_SNAKE_CASE_ , metadata={"""help""": """A folder containing the training data."""} ) __UpperCamelCase = field(default=SCREAMING_SNAKE_CASE_ , metadata={"""help""": """A folder containing the validation data."""} ) __UpperCamelCase = field( default=0.15 , metadata={"""help""": """Percent to split off of train for validation."""} ) __UpperCamelCase = field( default=SCREAMING_SNAKE_CASE_ , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) __UpperCamelCase = field( default=SCREAMING_SNAKE_CASE_ , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) def UpperCAmelCase__ ( self :Tuple ) -> str: if self.dataset_name is None and (self.train_dir is None and self.validation_dir is None): raise ValueError( 'You must specify either a dataset name from the hub or a train and/or validation directory.' ) @dataclass class A_ : """simple docstring""" __UpperCamelCase = field( default="""google/vit-base-patch16-224-in21k""" , metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} , ) __UpperCamelCase = field( default=SCREAMING_SNAKE_CASE_ , metadata={"""help""": """If training from scratch, pass a model type from the list: """ + """, """.join(SCREAMING_SNAKE_CASE_ )} , ) __UpperCamelCase = field( default=SCREAMING_SNAKE_CASE_ , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) __UpperCamelCase = field( default=SCREAMING_SNAKE_CASE_ , metadata={"""help""": """Where do you want to store the pretrained models downloaded from s3"""} ) __UpperCamelCase = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) __UpperCamelCase = field(default=SCREAMING_SNAKE_CASE_ , metadata={"""help""": """Name or path of preprocessor config."""} ) __UpperCamelCase = field( default=SCREAMING_SNAKE_CASE_ , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) __UpperCamelCase = field( default=SCREAMING_SNAKE_CASE_ , metadata={"""help""": """Will enable to load a pretrained model whose head dimensions are different."""} , ) def _lowerCAmelCase ( lowercase_ ): UpperCAmelCase = torch.stack([example['pixel_values'] for example in examples] ) UpperCAmelCase = torch.tensor([example['labels'] for example in examples] ) return {"pixel_values": pixel_values, "labels": labels} def _lowerCAmelCase ( ): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. UpperCAmelCase = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('.json' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('run_image_classification' , lowercase_ , lowercase_ ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCAmelCase = training_args.get_process_log_level() logger.setLevel(lowercase_ ) transformers.utils.logging.set_verbosity(lowercase_ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + F"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(F"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. UpperCAmelCase = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCAmelCase = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. """ 'Use --overwrite_output_dir to overcome.' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ 'the `--output_dir` or add `--overwrite_output_dir` to train from scratch.' ) # Set seed before initializing model. set_seed(training_args.seed ) # Initialize our dataset and prepare it for the 'image-classification' task. if data_args.dataset_name is not None: UpperCAmelCase = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir , task='image-classification' , use_auth_token=True if model_args.use_auth_token else None , ) else: UpperCAmelCase = {} if data_args.train_dir is not None: UpperCAmelCase = os.path.join(data_args.train_dir , '**' ) if data_args.validation_dir is not None: UpperCAmelCase = os.path.join(data_args.validation_dir , '**' ) UpperCAmelCase = load_dataset( 'imagefolder' , data_files=lowercase_ , cache_dir=model_args.cache_dir , task='image-classification' , ) # If we don't have a validation split, split off a percentage of train as validation. UpperCAmelCase = None if 'validation' in dataset.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , lowercase_ ) and data_args.train_val_split > 0.0: UpperCAmelCase = dataset['train'].train_test_split(data_args.train_val_split ) UpperCAmelCase = split['train'] UpperCAmelCase = split['test'] # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. UpperCAmelCase = dataset['train'].features['labels'].names UpperCAmelCase , UpperCAmelCase = {}, {} for i, label in enumerate(lowercase_ ): UpperCAmelCase = str(lowercase_ ) UpperCAmelCase = label # Load the accuracy metric from the datasets package UpperCAmelCase = evaluate.load('accuracy' ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(lowercase_ ): return metric.compute(predictions=np.argmax(p.predictions , axis=1 ) , references=p.label_ids ) UpperCAmelCase = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path , num_labels=len(lowercase_ ) , labelaid=lowercase_ , idalabel=lowercase_ , finetuning_task='image-classification' , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) UpperCAmelCase = AutoModelForImageClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path ) , config=lowercase_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ignore_mismatched_sizes=model_args.ignore_mismatched_sizes , ) UpperCAmelCase = AutoImageProcessor.from_pretrained( model_args.image_processor_name or model_args.model_name_or_path , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Define torchvision transforms to be applied to each image. if "shortest_edge" in image_processor.size: UpperCAmelCase = image_processor.size['shortest_edge'] else: UpperCAmelCase = (image_processor.size['height'], image_processor.size['width']) UpperCAmelCase = Normalize(mean=image_processor.image_mean , std=image_processor.image_std ) UpperCAmelCase = Compose( [ RandomResizedCrop(lowercase_ ), RandomHorizontalFlip(), ToTensor(), normalize, ] ) UpperCAmelCase = Compose( [ Resize(lowercase_ ), CenterCrop(lowercase_ ), ToTensor(), normalize, ] ) def train_transforms(lowercase_ ): UpperCAmelCase = [ _train_transforms(pil_img.convert('RGB' ) ) for pil_img in example_batch['image'] ] return example_batch def val_transforms(lowercase_ ): UpperCAmelCase = [_val_transforms(pil_img.convert('RGB' ) ) for pil_img in example_batch['image']] return example_batch if training_args.do_train: if "train" not in dataset: raise ValueError('--do_train requires a train dataset' ) if data_args.max_train_samples is not None: UpperCAmelCase = ( dataset['train'].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms dataset["train"].set_transform(lowercase_ ) if training_args.do_eval: if "validation" not in dataset: raise ValueError('--do_eval requires a validation dataset' ) if data_args.max_eval_samples is not None: UpperCAmelCase = ( dataset['validation'].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms dataset["validation"].set_transform(lowercase_ ) # Initalize our trainer UpperCAmelCase = Trainer( model=lowercase_ , args=lowercase_ , train_dataset=dataset['train'] if training_args.do_train else None , eval_dataset=dataset['validation'] if training_args.do_eval else None , compute_metrics=lowercase_ , tokenizer=lowercase_ , data_collator=lowercase_ , ) # Training if training_args.do_train: UpperCAmelCase = None if training_args.resume_from_checkpoint is not None: UpperCAmelCase = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCAmelCase = last_checkpoint UpperCAmelCase = trainer.train(resume_from_checkpoint=lowercase_ ) trainer.save_model() trainer.log_metrics('train' , train_result.metrics ) trainer.save_metrics('train' , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCAmelCase = trainer.evaluate() trainer.log_metrics('eval' , lowercase_ ) trainer.save_metrics('eval' , lowercase_ ) # Write model card and (optionally) push to hub UpperCAmelCase = { 'finetuned_from': model_args.model_name_or_path, 'tasks': 'image-classification', 'dataset': data_args.dataset_name, 'tags': ['image-classification', 'vision'], } if training_args.push_to_hub: trainer.push_to_hub(**lowercase_ ) else: trainer.create_model_card(**lowercase_ ) if __name__ == "__main__": main()
78
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast from ...utils import logging lowercase : List[str] = logging.get_logger(__name__) lowercase : Optional[Any] = { """EleutherAI/gpt-neo-1.3B""": """https://huggingface.co/EleutherAI/gpt-neo-1.3B/resolve/main/config.json""", # See all GPTNeo models at https://huggingface.co/models?filter=gpt_neo } class __snake_case ( lowerCAmelCase ): _a : str= "gpt_neo" _a : Optional[int]= ["past_key_values"] _a : Dict= {"num_attention_heads": "num_heads", "num_hidden_layers": "num_layers"} def __init__( self ,snake_case=50257 ,snake_case=2048 ,snake_case=2048 ,snake_case=24 ,snake_case=[[["global", "local"], 12]] ,snake_case=16 ,snake_case=None ,snake_case=256 ,snake_case="gelu_new" ,snake_case=0.0 ,snake_case=0.0 ,snake_case=0.0 ,snake_case=0.1 ,snake_case=1e-5 ,snake_case=0.02 ,snake_case=True ,snake_case=50256 ,snake_case=50256 ,**snake_case ,): '''simple docstring''' lowercase : int = vocab_size lowercase : Union[str, Any] = max_position_embeddings lowercase : Dict = hidden_size lowercase : Union[str, Any] = num_layers lowercase : Union[str, Any] = num_heads lowercase : Optional[int] = intermediate_size lowercase : List[str] = window_size lowercase : Optional[int] = activation_function lowercase : List[str] = resid_dropout lowercase : int = embed_dropout lowercase : Optional[int] = attention_dropout lowercase : Tuple = classifier_dropout lowercase : Optional[int] = layer_norm_epsilon lowercase : Dict = initializer_range lowercase : List[str] = use_cache lowercase : Optional[int] = bos_token_id lowercase : int = eos_token_id lowercase : Union[str, Any] = attention_types lowercase : Dict = self.expand_attention_types_params(snake_case ) if len(self.attention_layers ) != self.num_layers: raise ValueError( """Configuration for convolutional module is incorrect. """ """It is required that `len(config.attention_layers)` == `config.num_layers` """ f"but is `len(config.attention_layers) = {len(self.attention_layers )}`, " f"`config.num_layers = {self.num_layers}`. " """`config.attention_layers` is prepared using `config.attention_types`. """ """Please verify the value of `config.attention_types` argument.""" ) super().__init__(bos_token_id=snake_case ,eos_token_id=snake_case ,**snake_case ) @staticmethod def _SCREAMING_SNAKE_CASE ( snake_case ): '''simple docstring''' lowercase : List[Any] = [] for item in attention_types: for _ in range(item[1] ): attentions.extend(item[0] ) return attentions def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: import torch lowercase : Tuple = input.size() lowercase : int = len(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = shape[dimension] lowercase : int = torch.arange(0 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = torch.div(sizedim - size , SCREAMING_SNAKE_CASE__ , rounding_mode="""floor""" ) + 1 lowercase : Dict = torch.arange(SCREAMING_SNAKE_CASE__ ) + low_indices[:min_length][:, None] lowercase : Union[str, Any] = [slice(SCREAMING_SNAKE_CASE__ )] * rank lowercase : Optional[Any] = indices lowercase : List[str] = input[s] lowercase : Optional[int] = list(range(0 , rank + 1 ) ) perm.append(perm.pop(dimension + 1 ) ) return sliced.permute(SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: import torch lowercase : Union[str, Any] = torch.arange(1 , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = torch.remainder(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = remainders == 0 lowercase : Optional[int] = candidates[divisor_indices] lowercase : List[Any] = torch.max(SCREAMING_SNAKE_CASE__ ) return largest_divisor, torch.div(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , rounding_mode="""floor""" ) class __snake_case ( lowerCAmelCase ): @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Dict = OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}} ) if self.use_past: self.fill_with_past_key_values_(snake_case ,direction="""inputs""" ) lowercase : Dict = {0: """batch""", 1: """past_sequence + sequence"""} else: lowercase : List[str] = {0: """batch""", 1: """sequence"""} return common_inputs @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return self._config.num_heads def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = -1 ,snake_case = -1 ,snake_case = False ,snake_case = None ,): '''simple docstring''' lowercase : Any = super(snake_case ,self ).generate_dummy_inputs( snake_case ,batch_size=snake_case ,seq_length=snake_case ,is_pair=snake_case ,framework=snake_case ) # We need to order the input in the way they appears in the forward() lowercase : List[str] = OrderedDict({"""input_ids""": common_inputs["""input_ids"""]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed.""" ) else: import torch lowercase , lowercase : List[Any] = common_inputs["""input_ids"""].shape # Not using the same length for past_key_values lowercase : Optional[int] = seqlen + 2 lowercase : int = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) lowercase : Optional[Any] = [ (torch.zeros(snake_case ), torch.zeros(snake_case )) for _ in range(self.num_layers ) ] lowercase : Optional[Any] = common_inputs["""attention_mask"""] if self.use_past: lowercase : Any = ordered_inputs["""attention_mask"""].dtype lowercase : Union[str, Any] = torch.cat( [ordered_inputs["""attention_mask"""], torch.ones(snake_case ,snake_case ,dtype=snake_case )] ,dim=1 ) return ordered_inputs @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return 13
20
0
'''simple docstring''' import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class _UpperCAmelCase ( snake_case_ ): """simple docstring""" snake_case = '''Speech2TextFeatureExtractor''' snake_case = '''Speech2TextTokenizer''' def __init__( self : str , __UpperCAmelCase : Tuple , __UpperCAmelCase : Tuple ): '''simple docstring''' super().__init__(__UpperCAmelCase , __UpperCAmelCase ) _A = self.feature_extractor _A = False def __call__( self : Tuple , *__UpperCAmelCase : Union[str, Any] , **__UpperCAmelCase : Tuple ): '''simple docstring''' if self._in_target_context_manager: return self.current_processor(*__UpperCAmelCase , **__UpperCAmelCase ) if "raw_speech" in kwargs: warnings.warn("Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead." ) _A = kwargs.pop("raw_speech" ) else: _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 : List[Any] , *__UpperCAmelCase : Tuple , **__UpperCAmelCase : Optional[int] ): '''simple docstring''' return self.tokenizer.batch_decode(*__UpperCAmelCase , **__UpperCAmelCase ) def lowerCAmelCase ( self : Any , *__UpperCAmelCase : Any , **__UpperCAmelCase : Optional[int] ): '''simple docstring''' return self.tokenizer.decode(*__UpperCAmelCase , **__UpperCAmelCase ) @contextmanager def lowerCAmelCase ( self : str ): '''simple docstring''' warnings.warn( "`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your " "labels by using the argument `text` of the regular `__call__` method (either in the same call as " "your audio inputs, or in a separate call." ) _A = True _A = self.tokenizer yield _A = self.feature_extractor _A = False
79
from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING lowercase : Any = logging.get_logger(__name__) @add_end_docstrings(lowerCAmelCase ) class __snake_case ( lowerCAmelCase ): def __init__( self ,*snake_case ,**snake_case ): '''simple docstring''' super().__init__(*snake_case ,**snake_case ) requires_backends(self ,"""vision""" ) self.check_model_type( TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING if self.framework == """tf""" else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING ) def _SCREAMING_SNAKE_CASE ( self ,snake_case=None ): '''simple docstring''' lowercase : List[Any] = {} if top_k is not None: lowercase : int = top_k return {}, {}, postprocess_params def __call__( self ,snake_case ,**snake_case ): '''simple docstring''' return super().__call__(snake_case ,**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Any = load_image(snake_case ) lowercase : List[Any] = self.image_processor(images=snake_case ,return_tensors=self.framework ) return model_inputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : int = self.model(**snake_case ) return model_outputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=5 ): '''simple docstring''' if top_k > self.model.config.num_labels: lowercase : Tuple = self.model.config.num_labels if self.framework == "pt": lowercase : str = model_outputs.logits.softmax(-1 )[0] lowercase , lowercase : Dict = probs.topk(snake_case ) elif self.framework == "tf": lowercase : Optional[int] = stable_softmax(model_outputs.logits ,axis=-1 )[0] lowercase : Union[str, Any] = tf.math.top_k(snake_case ,k=snake_case ) lowercase , lowercase : List[str] = topk.values.numpy(), topk.indices.numpy() else: raise ValueError(f"Unsupported framework: {self.framework}" ) lowercase : Tuple = scores.tolist() lowercase : Dict = ids.tolist() return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(snake_case ,snake_case )]
20
0
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roformer import RoFormerTokenizer from .tokenization_utils import JiebaPreTokenizer a__ : int = logging.get_logger(__name__) a__ : List[str] = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} a__ : List[Any] = { 'vocab_file': { 'junnyu/roformer_chinese_small': 'https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt', 'junnyu/roformer_chinese_base': 'https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt', 'junnyu/roformer_chinese_char_small': ( 'https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt' ), 'junnyu/roformer_chinese_char_base': ( 'https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt' ), 'junnyu/roformer_small_discriminator': ( 'https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt' ), 'junnyu/roformer_small_generator': ( 'https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt' ), } } a__ : Optional[Any] = { 'junnyu/roformer_chinese_small': 1_5_3_6, 'junnyu/roformer_chinese_base': 1_5_3_6, 'junnyu/roformer_chinese_char_small': 5_1_2, 'junnyu/roformer_chinese_char_base': 5_1_2, 'junnyu/roformer_small_discriminator': 1_2_8, 'junnyu/roformer_small_generator': 1_2_8, } a__ : str = { 'junnyu/roformer_chinese_small': {'do_lower_case': True}, 'junnyu/roformer_chinese_base': {'do_lower_case': True}, 'junnyu/roformer_chinese_char_small': {'do_lower_case': True}, 'junnyu/roformer_chinese_char_base': {'do_lower_case': True}, 'junnyu/roformer_small_discriminator': {'do_lower_case': True}, 'junnyu/roformer_small_generator': {'do_lower_case': True}, } class lowercase_ ( a__ ): __UpperCAmelCase = VOCAB_FILES_NAMES __UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCAmelCase = PRETRAINED_INIT_CONFIGURATION __UpperCAmelCase = RoFormerTokenizer def __init__( self , a=None , a=None , a=True , a="[UNK]" , a="[SEP]" , a="[PAD]" , a="[CLS]" , a="[MASK]" , a=True , a=None , **a , ): super().__init__( a , tokenizer_file=a , do_lower_case=a , unk_token=a , sep_token=a , pad_token=a , cls_token=a , mask_token=a , tokenize_chinese_chars=a , strip_accents=a , **a , ) UpperCamelCase__ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( pre_tok_state.get("lowercase" , a ) != do_lower_case or pre_tok_state.get("strip_accents" , a ) != strip_accents ): UpperCamelCase__ = getattr(a , pre_tok_state.pop("type" ) ) UpperCamelCase__ = do_lower_case UpperCamelCase__ = strip_accents UpperCamelCase__ = pre_tok_class(**a ) UpperCamelCase__ = do_lower_case def __getstate__( self ): UpperCamelCase__ = self.__dict__.copy() UpperCamelCase__ = BertPreTokenizer() return state def __setstate__( self , a ): UpperCamelCase__ = d UpperCamelCase__ = self.__dict__["_tokenizer"].get_vocab() UpperCamelCase__ = PreTokenizer.custom(JiebaPreTokenizer(a ) ) def __a ( self , a , a=None ): UpperCamelCase__ = [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 __a ( self , a , a = None ): UpperCamelCase__ = [self.sep_token_id] UpperCamelCase__ = [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 __a ( self , a , a = None ): UpperCamelCase__ = self._tokenizer.model.save(a , name=a ) return tuple(a ) def __a ( self , a , a=None , a=None , a=False , **a , ): UpperCamelCase__ = BertPreTokenizer() return super().save_pretrained(a , a , a , a , **a )
80
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class __snake_case : def __init__( self ,snake_case ,): '''simple docstring''' lowercase : Any = parent lowercase : Tuple = 13 lowercase : str = 7 lowercase : Dict = True lowercase : Dict = True lowercase : str = True lowercase : List[str] = True lowercase : int = True lowercase : Union[str, Any] = False lowercase : Dict = False lowercase : List[Any] = False lowercase : List[Any] = 2 lowercase : Optional[Any] = 99 lowercase : int = 0 lowercase : Tuple = 32 lowercase : int = 2 lowercase : Tuple = 4 lowercase : List[Any] = 0.1 lowercase : Tuple = 0.1 lowercase : List[Any] = 512 lowercase : int = 16 lowercase : Dict = 2 lowercase : int = 0.02 lowercase : Union[str, Any] = 3 lowercase : Any = 4 lowercase : List[Any] = """last""" lowercase : Tuple = True lowercase : List[Any] = None lowercase : Any = 0 def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) lowercase : List[str] = random_attention_mask([self.batch_size, self.seq_length] ,dtype=tf.floataa ) lowercase : Tuple = None if self.use_input_lengths: lowercase : List[str] = ( ids_tensor([self.batch_size] ,vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length lowercase : Tuple = None if self.use_token_type_ids: lowercase : Optional[int] = ids_tensor([self.batch_size, self.seq_length] ,self.n_langs ) lowercase : List[str] = None lowercase : List[str] = None lowercase : Optional[Any] = None if self.use_labels: lowercase : List[str] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) lowercase : List[str] = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) lowercase : str = ids_tensor([self.batch_size] ,2 ,dtype=tf.floataa ) lowercase : Optional[Any] = ids_tensor([self.batch_size] ,self.num_choices ) lowercase : str = FlaubertConfig( vocab_size=self.vocab_size ,n_special=self.n_special ,emb_dim=self.hidden_size ,n_layers=self.num_hidden_layers ,n_heads=self.num_attention_heads ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,gelu_activation=self.gelu_activation ,sinusoidal_embeddings=self.sinusoidal_embeddings ,asm=self.asm ,causal=self.causal ,n_langs=self.n_langs ,max_position_embeddings=self.max_position_embeddings ,initializer_range=self.initializer_range ,summary_type=self.summary_type ,use_proj=self.use_proj ,bos_token_id=self.bos_token_id ,) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Tuple = TFFlaubertModel(config=snake_case ) lowercase : str = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} lowercase : Optional[Any] = model(snake_case ) lowercase : List[Any] = [input_ids, input_mask] lowercase : int = model(snake_case ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : List[Any] = TFFlaubertWithLMHeadModel(snake_case ) lowercase : Optional[Any] = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} lowercase : int = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Tuple = TFFlaubertForQuestionAnsweringSimple(snake_case ) lowercase : Union[str, Any] = {"""input_ids""": input_ids, """lengths""": input_lengths} lowercase : Tuple = model(snake_case ) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Union[str, Any] = TFFlaubertForSequenceClassification(snake_case ) lowercase : str = {"""input_ids""": input_ids, """lengths""": input_lengths} lowercase : str = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Any = self.num_labels lowercase : List[str] = TFFlaubertForTokenClassification(config=snake_case ) lowercase : Dict = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} lowercase : int = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ,): '''simple docstring''' lowercase : Any = self.num_choices lowercase : Dict = TFFlaubertForMultipleChoice(config=snake_case ) lowercase : Any = tf.tile(tf.expand_dims(snake_case ,1 ) ,(1, self.num_choices, 1) ) lowercase : Optional[Any] = tf.tile(tf.expand_dims(snake_case ,1 ) ,(1, self.num_choices, 1) ) lowercase : Dict = tf.tile(tf.expand_dims(snake_case ,1 ) ,(1, self.num_choices, 1) ) lowercase : Union[str, Any] = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } lowercase : int = model(snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_choices) ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Any = self.prepare_config_and_inputs() ( ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ) : int = config_and_inputs lowercase : List[str] = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """langs""": token_type_ids, """lengths""": input_lengths, } return config, inputs_dict @require_tf class __snake_case ( lowerCAmelCase , lowerCAmelCase , unittest.TestCase ): _a : Dict= ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) _a : Optional[Any]= ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable _a : Any= ( { "feature-extraction": TFFlaubertModel, "fill-mask": TFFlaubertWithLMHeadModel, "question-answering": TFFlaubertForQuestionAnsweringSimple, "text-classification": TFFlaubertForSequenceClassification, "token-classification": TFFlaubertForTokenClassification, "zero-shot": TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) _a : Tuple= False _a : int= False def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ,snake_case ,snake_case ): '''simple docstring''' if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("""Fast""" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : str = TFFlaubertModelTester(self ) lowercase : List[Any] = ConfigTester(self ,config_class=snake_case ,emb_dim=37 ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' self.config_tester.run_common_tests() def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*snake_case ) def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*snake_case ) @slow def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase : Dict = TFFlaubertModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) @require_tf @require_sentencepiece @require_tokenizers class __snake_case ( unittest.TestCase ): @slow def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : List[Any] = TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased""" ) lowercase : int = tf.convert_to_tensor( [[0, 158, 735, 2592, 1424, 6727, 82, 1]] ,dtype=tf.intaa ,) # "J'aime flaubert !" lowercase : Dict = model(snake_case )[0] lowercase : Union[str, Any] = tf.TensorShape((1, 8, 512) ) self.assertEqual(output.shape ,snake_case ) # compare the actual values for a slice. lowercase : Tuple = tf.convert_to_tensor( [ [ [-1.8_768_773, -1.566_555, 0.27_072_418], [-1.6_920_038, -0.5_873_505, 1.9_329_599], [-2.9_563_985, -1.6_993_835, 1.7_972_052], ] ] ,dtype=tf.floataa ,) self.assertTrue(np.allclose(output[:, :3, :3].numpy() ,expected_slice.numpy() ,atol=1e-4 ) )
20
0