code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
def __lowerCAmelCase ( _UpperCamelCase , _UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' if not (isinstance(_lowercase , _lowercase ) and isinstance(_lowercase , _lowercase )): raise ValueError("""longest_common_substring() takes two strings for inputs""" ) lowerCamelCase__: Optional[Any] = len(_lowercase ) lowerCamelCase__: Optional[Any] = len(_lowercase ) lowerCamelCase__: Dict = [[0] * (texta_length + 1) for _ in range(texta_length + 1 )] lowerCamelCase__: Dict = 0 lowerCamelCase__: Tuple = 0 for i in range(1 , texta_length + 1 ): for j in range(1 , texta_length + 1 ): if texta[i - 1] == texta[j - 1]: lowerCamelCase__: Dict = 1 + dp[i - 1][j - 1] if dp[i][j] > ans_length: lowerCamelCase__: Any = i lowerCamelCase__: Optional[Any] = dp[i][j] return texta[ans_index - ans_length : ans_index] if __name__ == "__main__": import doctest doctest.testmod()
306
import numpy as np import datasets __a = '\nCompute the Mahalanobis Distance\n\nMahalonobis distance is the distance between a point and a distribution.\nAnd not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance.\nIt was introduced by Prof. P. C. Mahalanobis in 1936\nand has been used in various statistical applications ever since\n[source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/]\n' __a = '\\n@article{de2000mahalanobis,\n title={The mahalanobis distance},\n author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L},\n journal={Chemometrics and intelligent laboratory systems},\n volume={50},\n number={1},\n pages={1--18},\n year={2000},\n publisher={Elsevier}\n}\n' __a = '\nArgs:\n X: List of datapoints to be compared with the `reference_distribution`.\n reference_distribution: List of datapoints from the reference distribution we want to compare to.\nReturns:\n mahalanobis: The Mahalonobis distance for each datapoint in `X`.\nExamples:\n\n >>> mahalanobis_metric = datasets.load_metric("mahalanobis")\n >>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]])\n >>> print(results)\n {\'mahalanobis\': array([0.5])}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __a( datasets.Metric ): """simple docstring""" def a__ ( self ) -> Union[str, Any]: return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { '''X''': datasets.Sequence(datasets.Value('''float''' ,id='''sequence''' ) ,id='''X''' ), } ) ,) def a__ ( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) -> Any: # convert to numpy arrays UpperCAmelCase_ : str = np.array(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = np.array(_SCREAMING_SNAKE_CASE ) # Assert that arrays are 2D if len(X.shape ) != 2: raise ValueError('''Expected `X` to be a 2D vector''' ) if len(reference_distribution.shape ) != 2: raise ValueError('''Expected `reference_distribution` to be a 2D vector''' ) if reference_distribution.shape[0] < 2: raise ValueError( '''Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension''' ) # Get mahalanobis distance for each prediction UpperCAmelCase_ : List[str] = X - np.mean(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = np.cov(reference_distribution.T ) try: UpperCAmelCase_ : Any = np.linalg.inv(_SCREAMING_SNAKE_CASE ) except np.linalg.LinAlgError: UpperCAmelCase_ : List[str] = np.linalg.pinv(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = np.dot(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = np.dot(_SCREAMING_SNAKE_CASE ,X_minus_mu.T ).diagonal() return {"mahalanobis": mahal_dist}
30
0
import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging _SCREAMING_SNAKE_CASE : Optional[Any] = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Tuple = { 'BAAI/AltCLIP': 'https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json', # See all AltCLIP models at https://huggingface.co/models?filter=altclip } class A ( _a ): '''simple docstring''' lowerCamelCase : Dict = """altclip_text_model""" def __init__( self : Optional[Any] , _UpperCamelCase : List[Any]=250_002 , _UpperCamelCase : str=1_024 , _UpperCamelCase : str=24 , _UpperCamelCase : Union[str, Any]=16 , _UpperCamelCase : Any=4_096 , _UpperCamelCase : int="gelu" , _UpperCamelCase : List[Any]=0.1 , _UpperCamelCase : Any=0.1 , _UpperCamelCase : Optional[Any]=514 , _UpperCamelCase : Optional[Any]=1 , _UpperCamelCase : str=0.0_2 , _UpperCamelCase : Optional[Any]=0.0_2 , _UpperCamelCase : Optional[Any]=1e-05 , _UpperCamelCase : Optional[Any]=1 , _UpperCamelCase : Optional[int]=0 , _UpperCamelCase : Optional[Any]=2 , _UpperCamelCase : List[str]="absolute" , _UpperCamelCase : str=True , _UpperCamelCase : Dict=768 , **_UpperCamelCase : Optional[Any] , ): super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE , bos_token_id=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE) _lowercase: Union[str, Any] = vocab_size _lowercase: Optional[Any] = hidden_size _lowercase: List[Any] = num_hidden_layers _lowercase: List[Any] = num_attention_heads _lowercase: Any = hidden_act _lowercase: Optional[Any] = intermediate_size _lowercase: Optional[Any] = hidden_dropout_prob _lowercase: Optional[Any] = attention_probs_dropout_prob _lowercase: Any = max_position_embeddings _lowercase: Any = type_vocab_size _lowercase: Dict = initializer_range _lowercase: Union[str, Any] = initializer_factor _lowercase: Tuple = layer_norm_eps _lowercase: Union[str, Any] = position_embedding_type _lowercase: List[Any] = use_cache _lowercase: str = project_dim class A ( _a ): '''simple docstring''' lowerCamelCase : List[str] = """altclip_vision_model""" def __init__( self : Optional[int] , _UpperCamelCase : str=768 , _UpperCamelCase : str=3_072 , _UpperCamelCase : Optional[Any]=512 , _UpperCamelCase : Union[str, Any]=12 , _UpperCamelCase : str=12 , _UpperCamelCase : int=3 , _UpperCamelCase : Union[str, Any]=224 , _UpperCamelCase : Union[str, Any]=32 , _UpperCamelCase : List[str]="quick_gelu" , _UpperCamelCase : List[Any]=1e-5 , _UpperCamelCase : Tuple=0.0 , _UpperCamelCase : Optional[int]=0.0_2 , _UpperCamelCase : str=1.0 , **_UpperCamelCase : Optional[int] , ): super().__init__(**_SCREAMING_SNAKE_CASE) _lowercase: Optional[Any] = hidden_size _lowercase: Dict = intermediate_size _lowercase: str = projection_dim _lowercase: Tuple = num_hidden_layers _lowercase: str = num_attention_heads _lowercase: Optional[Any] = num_channels _lowercase: Union[str, Any] = patch_size _lowercase: Union[str, Any] = image_size _lowercase: Any = initializer_range _lowercase: Union[str, Any] = initializer_factor _lowercase: Optional[Any] = attention_dropout _lowercase: Any = layer_norm_eps _lowercase: List[Any] = hidden_act @classmethod def UpperCAmelCase__ ( cls : Dict , _UpperCamelCase : Tuple , **_UpperCamelCase : Optional[Any]): cls._set_token_in_kwargs(_SCREAMING_SNAKE_CASE) _lowercase: Union[str, Any] = cls.get_config_dict(_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE) # get the vision config dict if we are loading from AltCLIPConfig if config_dict.get("model_type") == "altclip": _lowercase: List[Any] = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , "model_type") and config_dict["model_type"] != cls.model_type: logger.warning( f"You are using a model of type {config_dict['model_type']} to instantiate a model of type " f"{cls.model_type}. This is not supported for all configurations of models and can yield errors.") return cls.from_dict(_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE) class A ( _a ): '''simple docstring''' lowerCamelCase : Optional[Any] = """altclip""" lowerCamelCase : Optional[Any] = True def __init__( self : Optional[int] , _UpperCamelCase : int=None , _UpperCamelCase : int=None , _UpperCamelCase : int=768 , _UpperCamelCase : str=2.6_5_9_2 , **_UpperCamelCase : Tuple): # If `_config_dict` exist, we use them for the backward compatibility. # We pop out these 2 attributes before calling `super().__init__` to avoid them being saved (which causes a lot # of confusion!). _lowercase: Optional[int] = kwargs.pop("text_config_dict" , _SCREAMING_SNAKE_CASE) _lowercase: List[str] = kwargs.pop("vision_config_dict" , _SCREAMING_SNAKE_CASE) super().__init__(**_SCREAMING_SNAKE_CASE) # Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in # `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most # cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`. if text_config_dict is not None: if text_config is None: _lowercase: Optional[Any] = {} # This is the complete result when using `text_config_dict`. _lowercase: Optional[Any] = AltCLIPTextConfig(**_SCREAMING_SNAKE_CASE).to_dict() # Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different. for key, value in _text_config_dict.items(): if key in text_config and value != text_config[key] and key not in ["transformers_version"]: # If specified in `text_config_dict` if key in text_config_dict: _lowercase: Any = ( f"`{key}` is found in both `text_config_dict` and `text_config` but with different values. " f"The value `text_config_dict[\"{key}\"]` will be used instead." ) # If inferred from default argument values (just to be super careful) else: _lowercase: Optional[int] = ( f"`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The " f"value `text_config[\"{key}\"]` will be overriden." ) logger.warning(_SCREAMING_SNAKE_CASE) # Update all values in `text_config` with the ones in `_text_config_dict`. text_config.update(_text_config_dict) if vision_config_dict is not None: if vision_config is None: _lowercase: Dict = {} # This is the complete result when using `vision_config_dict`. _lowercase: Tuple = AltCLIPVisionConfig(**_SCREAMING_SNAKE_CASE).to_dict() # convert keys to string instead of integer if "id2label" in _vision_config_dict: _lowercase: str = { str(_SCREAMING_SNAKE_CASE): value for key, value in _vision_config_dict['''id2label'''].items() } # Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different. for key, value in _vision_config_dict.items(): if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]: # If specified in `vision_config_dict` if key in vision_config_dict: _lowercase: Optional[int] = ( f"`{key}` is found in both `vision_config_dict` and `vision_config` but with different " f"values. The value `vision_config_dict[\"{key}\"]` will be used instead." ) # If inferred from default argument values (just to be super careful) else: _lowercase: str = ( f"`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. " f"The value `vision_config[\"{key}\"]` will be overriden." ) logger.warning(_SCREAMING_SNAKE_CASE) # Update all values in `vision_config` with the ones in `_vision_config_dict`. vision_config.update(_vision_config_dict) if text_config is None: _lowercase: Union[str, Any] = {} logger.info("`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values.") if vision_config is None: _lowercase: List[Any] = {} logger.info("`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values.") _lowercase: List[Any] = AltCLIPTextConfig(**_SCREAMING_SNAKE_CASE) _lowercase: Optional[int] = AltCLIPVisionConfig(**_SCREAMING_SNAKE_CASE) _lowercase: Optional[int] = projection_dim _lowercase: List[str] = logit_scale_init_value _lowercase: List[str] = 1.0 @classmethod def UpperCAmelCase__ ( cls : Optional[int] , _UpperCamelCase : str , _UpperCamelCase : Optional[Any] , **_UpperCamelCase : Any): return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **_SCREAMING_SNAKE_CASE) def UpperCAmelCase__ ( self : Dict): _lowercase: Dict = copy.deepcopy(self.__dict__) _lowercase: Tuple = self.text_config.to_dict() _lowercase: Optional[int] = self.vision_config.to_dict() _lowercase: str = self.__class__.model_type return output
226
import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert import BertTokenizer __a = logging.get_logger(__name__) __a = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} __a = { 'vocab_file': { 'facebook/dpr-ctx_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt' ), 'facebook/dpr-ctx_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'facebook/dpr-ctx_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json' ), 'facebook/dpr-ctx_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json' ), }, } __a = { 'vocab_file': { 'facebook/dpr-question_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt' ), 'facebook/dpr-question_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'facebook/dpr-question_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json' ), 'facebook/dpr-question_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json' ), }, } __a = { 'vocab_file': { 'facebook/dpr-reader-single-nq-base': ( 'https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt' ), 'facebook/dpr-reader-multiset-base': ( 'https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'facebook/dpr-reader-single-nq-base': ( 'https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json' ), 'facebook/dpr-reader-multiset-base': ( 'https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json' ), }, } __a = { 'facebook/dpr-ctx_encoder-single-nq-base': 512, 'facebook/dpr-ctx_encoder-multiset-base': 512, } __a = { 'facebook/dpr-question_encoder-single-nq-base': 512, 'facebook/dpr-question_encoder-multiset-base': 512, } __a = { 'facebook/dpr-reader-single-nq-base': 512, 'facebook/dpr-reader-multiset-base': 512, } __a = { 'facebook/dpr-ctx_encoder-single-nq-base': {'do_lower_case': True}, 'facebook/dpr-ctx_encoder-multiset-base': {'do_lower_case': True}, } __a = { 'facebook/dpr-question_encoder-single-nq-base': {'do_lower_case': True}, 'facebook/dpr-question_encoder-multiset-base': {'do_lower_case': True}, } __a = { 'facebook/dpr-reader-single-nq-base': {'do_lower_case': True}, 'facebook/dpr-reader-multiset-base': {'do_lower_case': True}, } class __a( _a ): """simple docstring""" lowerCAmelCase = VOCAB_FILES_NAMES lowerCAmelCase = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION class __a( _a ): """simple docstring""" lowerCAmelCase = VOCAB_FILES_NAMES lowerCAmelCase = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION __a = collections.namedtuple( 'DPRSpanPrediction', ['span_score', 'relevance_score', 'doc_id', 'start_index', 'end_index', 'text'] ) __a = collections.namedtuple('DPRReaderOutput', ['start_logits', 'end_logits', 'relevance_logits']) __a = R'\n Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`.\n It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers),\n using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)`\n with the format:\n\n ```\n [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>\n ```\n\n Args:\n questions (`str` or `List[str]`):\n The questions to be encoded. You can specify one question for many passages. In this case, the question\n will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in\n `titles` or `texts`.\n titles (`str` or `List[str]`):\n The passages titles to be encoded. This can be a string or a list of strings if there are several passages.\n texts (`str` or `List[str]`):\n The passages texts to be encoded. This can be a string or a list of strings if there are several passages.\n padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):\n Activates and controls padding. Accepts the following values:\n\n - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence\n if provided).\n - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided.\n - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different\n lengths).\n truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):\n Activates and controls truncation. Accepts the following values:\n\n - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to\n the maximum acceptable input length for the model if that argument is not provided. This will truncate\n token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch\n of pairs) is provided.\n - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the first\n sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the\n second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths\n greater than the model maximum admissible input size).\n max_length (`int`, *optional*):\n Controls the maximum length to use by one of the truncation/padding parameters.\n\n If left unset or set to `None`, this will use the predefined model maximum length if a maximum length\n is required by one of the truncation/padding parameters. If the model has no specific maximum input\n length (like XLNet) truncation/padding to a maximum length will be deactivated.\n return_tensors (`str` or [`~utils.TensorType`], *optional*):\n If set, will return tensors instead of list of python integers. Acceptable values are:\n\n - `\'tf\'`: Return TensorFlow `tf.constant` objects.\n - `\'pt\'`: Return PyTorch `torch.Tensor` objects.\n - `\'np\'`: Return Numpy `np.ndarray` objects.\n return_attention_mask (`bool`, *optional*):\n Whether or not to return the attention mask. If not set, will return the attention mask according to the\n specific tokenizer\'s default, defined by the `return_outputs` attribute.\n\n [What are attention masks?](../glossary#attention-mask)\n\n Returns:\n `Dict[str, List[List[int]]]`: A dictionary with the following keys:\n\n - `input_ids`: List of token ids to be fed to a model.\n - `attention_mask`: List of indices specifying which tokens should be attended to by the model.\n ' @add_start_docstrings(_a ) class __a: """simple docstring""" def __call__( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = None ,**_SCREAMING_SNAKE_CASE ,) -> BatchEncoding: if titles is None and texts is None: return super().__call__( _SCREAMING_SNAKE_CASE ,padding=_SCREAMING_SNAKE_CASE ,truncation=_SCREAMING_SNAKE_CASE ,max_length=_SCREAMING_SNAKE_CASE ,return_tensors=_SCREAMING_SNAKE_CASE ,return_attention_mask=_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ,) elif titles is None or texts is None: UpperCAmelCase_ : List[str] = titles if texts is None else texts return super().__call__( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,padding=_SCREAMING_SNAKE_CASE ,truncation=_SCREAMING_SNAKE_CASE ,max_length=_SCREAMING_SNAKE_CASE ,return_tensors=_SCREAMING_SNAKE_CASE ,return_attention_mask=_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ,) UpperCAmelCase_ : List[Any] = titles if not isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) else [titles] UpperCAmelCase_ : List[str] = texts if not isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) else [texts] UpperCAmelCase_ : Any = len(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = questions if not isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) else [questions] * n_passages if len(_SCREAMING_SNAKE_CASE ) != len(_SCREAMING_SNAKE_CASE ): raise ValueError( f'''There should be as many titles than texts but got {len(_SCREAMING_SNAKE_CASE )} titles and {len(_SCREAMING_SNAKE_CASE )} texts.''' ) UpperCAmelCase_ : Tuple = super().__call__(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,padding=_SCREAMING_SNAKE_CASE ,truncation=_SCREAMING_SNAKE_CASE )['''input_ids'''] UpperCAmelCase_ : int = super().__call__(_SCREAMING_SNAKE_CASE ,add_special_tokens=_SCREAMING_SNAKE_CASE ,padding=_SCREAMING_SNAKE_CASE ,truncation=_SCREAMING_SNAKE_CASE )['''input_ids'''] UpperCAmelCase_ : Optional[int] = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) ] } if return_attention_mask is not False: UpperCAmelCase_ : List[str] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) UpperCAmelCase_ : Dict = attention_mask return self.pad(_SCREAMING_SNAKE_CASE ,padding=_SCREAMING_SNAKE_CASE ,max_length=_SCREAMING_SNAKE_CASE ,return_tensors=_SCREAMING_SNAKE_CASE ) def a__ ( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE = 16 ,_SCREAMING_SNAKE_CASE = 64 ,_SCREAMING_SNAKE_CASE = 4 ,) -> List[DPRSpanPrediction]: UpperCAmelCase_ : Tuple = reader_input['''input_ids'''] UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_ : Optional[Any] = reader_output[:3] UpperCAmelCase_ : Optional[Any] = len(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = sorted(range(_SCREAMING_SNAKE_CASE ) ,reverse=_SCREAMING_SNAKE_CASE ,key=relevance_logits.__getitem__ ) UpperCAmelCase_ : List[DPRReaderOutput] = [] for doc_id in sorted_docs: UpperCAmelCase_ : List[Any] = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence UpperCAmelCase_ : str = sequence_ids.index(self.sep_token_id ,2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: UpperCAmelCase_ : List[Any] = sequence_ids.index(self.pad_token_id ) else: UpperCAmelCase_ : int = len(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] ,end_logits=end_logits[doc_id][passage_offset:sequence_len] ,max_answer_length=_SCREAMING_SNAKE_CASE ,top_spans=_SCREAMING_SNAKE_CASE ,) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] ,relevance_score=relevance_logits[doc_id] ,doc_id=_SCREAMING_SNAKE_CASE ,start_index=_SCREAMING_SNAKE_CASE ,end_index=_SCREAMING_SNAKE_CASE ,text=self.decode(sequence_ids[start_index : end_index + 1] ) ,) ) if len(_SCREAMING_SNAKE_CASE ) >= num_spans: break return nbest_spans_predictions[:num_spans] def a__ ( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,) -> List[DPRSpanPrediction]: UpperCAmelCase_ : Tuple = [] for start_index, start_score in enumerate(_SCREAMING_SNAKE_CASE ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) UpperCAmelCase_ : int = sorted(_SCREAMING_SNAKE_CASE ,key=lambda _SCREAMING_SNAKE_CASE : x[1] ,reverse=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = [] for (start_index, end_index), score in scores: if start_index > end_index: raise ValueError(f'''Wrong span indices: [{start_index}:{end_index}]''' ) UpperCAmelCase_ : str = end_index - start_index + 1 if length > max_answer_length: raise ValueError(f'''Span is too long: {length} > {max_answer_length}''' ) if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(_SCREAMING_SNAKE_CASE ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_a ) class __a( _a , _a ): """simple docstring""" lowerCAmelCase = VOCAB_FILES_NAMES lowerCAmelCase = READER_PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase = READER_PRETRAINED_INIT_CONFIGURATION lowerCAmelCase = ['''input_ids''', '''attention_mask''']
30
0
import gc import random import unittest import numpy as np import torch from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEImgaImgPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import floats_tensor, load_image, load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class lowercase__( _a , unittest.TestCase ): """simple docstring""" a :List[str] = ShapEImgaImgPipeline a :Tuple = ['image'] a :Union[str, Any] = ['image'] a :List[str] = [ 'num_images_per_prompt', 'num_inference_steps', 'generator', 'latents', 'guidance_scale', 'frame_size', 'output_type', 'return_dict', ] a :Tuple = False @property def _lowercase ( self : List[str] ) -> Optional[int]: return 3_2 @property def _lowercase ( self : Dict ) -> str: return 3_2 @property def _lowercase ( self : Dict ) -> Any: return self.time_input_dim * 4 @property def _lowercase ( self : List[str] ) -> Tuple: return 8 @property def _lowercase ( self : Optional[int] ) -> Any: torch.manual_seed(0 ) lowercase_ = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , image_size=6_4 , projection_dim=self.text_embedder_hidden_size , intermediate_size=3_7 , num_attention_heads=4 , num_channels=3 , num_hidden_layers=5 , patch_size=1 , ) lowercase_ = CLIPVisionModel(_SCREAMING_SNAKE_CASE ) return model @property def _lowercase ( self : List[Any] ) -> Tuple: lowercase_ = CLIPImageProcessor( crop_size=2_2_4 , do_center_crop=_SCREAMING_SNAKE_CASE , do_normalize=_SCREAMING_SNAKE_CASE , do_resize=_SCREAMING_SNAKE_CASE , image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73] , image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11] , resample=3 , size=2_2_4 , ) return image_processor @property def _lowercase ( self : Any ) -> Optional[Any]: torch.manual_seed(0 ) lowercase_ = { '''num_attention_heads''': 2, '''attention_head_dim''': 1_6, '''embedding_dim''': self.time_input_dim, '''num_embeddings''': 3_2, '''embedding_proj_dim''': self.text_embedder_hidden_size, '''time_embed_dim''': self.time_embed_dim, '''num_layers''': 1, '''clip_embed_dim''': self.time_input_dim * 2, '''additional_embeddings''': 0, '''time_embed_act_fn''': '''gelu''', '''norm_in_type''': '''layer''', '''embedding_proj_norm_type''': '''layer''', '''encoder_hid_proj_type''': None, '''added_emb_type''': None, } lowercase_ = PriorTransformer(**_SCREAMING_SNAKE_CASE ) return model @property def _lowercase ( self : Any ) -> Optional[int]: torch.manual_seed(0 ) lowercase_ = { '''param_shapes''': ( (self.renderer_dim, 9_3), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), '''d_latent''': self.time_input_dim, '''d_hidden''': self.renderer_dim, '''n_output''': 1_2, '''background''': ( 0.1, 0.1, 0.1, ), } lowercase_ = ShapERenderer(**_SCREAMING_SNAKE_CASE ) return model def _lowercase ( self : Any ) -> Optional[Any]: lowercase_ = self.dummy_prior lowercase_ = self.dummy_image_encoder lowercase_ = self.dummy_image_processor lowercase_ = self.dummy_renderer lowercase_ = HeunDiscreteScheduler( beta_schedule='''exp''' , num_train_timesteps=1_0_2_4 , prediction_type='''sample''' , use_karras_sigmas=_SCREAMING_SNAKE_CASE , clip_sample=_SCREAMING_SNAKE_CASE , clip_sample_range=1.0 , ) lowercase_ = { '''prior''': prior, '''image_encoder''': image_encoder, '''image_processor''': image_processor, '''renderer''': renderer, '''scheduler''': scheduler, } return components def _lowercase ( self : List[Any] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : int=0 ) -> Any: lowercase_ = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(_SCREAMING_SNAKE_CASE ) ).to(_SCREAMING_SNAKE_CASE ) if str(_SCREAMING_SNAKE_CASE ).startswith('''mps''' ): lowercase_ = torch.manual_seed(_SCREAMING_SNAKE_CASE ) else: lowercase_ = torch.Generator(device=_SCREAMING_SNAKE_CASE ).manual_seed(_SCREAMING_SNAKE_CASE ) lowercase_ = { '''image''': input_image, '''generator''': generator, '''num_inference_steps''': 1, '''frame_size''': 3_2, '''output_type''': '''np''', } return inputs def _lowercase ( self : List[str] ) -> Tuple: lowercase_ = '''cpu''' lowercase_ = self.get_dummy_components() lowercase_ = self.pipeline_class(**_SCREAMING_SNAKE_CASE ) lowercase_ = pipe.to(_SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE ) lowercase_ = pipe(**self.get_dummy_inputs(_SCREAMING_SNAKE_CASE ) ) lowercase_ = output.images[0] lowercase_ = image[0, -3:, -3:, -1] assert image.shape == (2_0, 3_2, 3_2, 3) lowercase_ = np.array( [ 0.00_03_92_16, 0.00_03_92_16, 0.00_03_92_16, 0.00_03_92_16, 0.00_03_92_16, 0.00_03_92_16, 0.00_03_92_16, 0.00_03_92_16, 0.00_03_92_16, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def _lowercase ( self : Optional[Any] ) -> int: # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def _lowercase ( self : Optional[int] ) -> Optional[Any]: lowercase_ = torch_device == '''cpu''' lowercase_ = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=_SCREAMING_SNAKE_CASE , relax_max_difference=_SCREAMING_SNAKE_CASE , ) def _lowercase ( self : Dict ) -> str: lowercase_ = self.get_dummy_components() lowercase_ = self.pipeline_class(**_SCREAMING_SNAKE_CASE ) lowercase_ = pipe.to(_SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE ) lowercase_ = 1 lowercase_ = 2 lowercase_ = self.get_dummy_inputs(_SCREAMING_SNAKE_CASE ) for key in inputs.keys(): if key in self.batch_params: lowercase_ = batch_size * [inputs[key]] lowercase_ = pipe(**_SCREAMING_SNAKE_CASE , num_images_per_prompt=_SCREAMING_SNAKE_CASE )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class lowercase__( unittest.TestCase ): """simple docstring""" def _lowercase ( self : List[Any] ) -> int: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowercase ( self : Optional[int] ) -> str: lowercase_ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/corgi.png''' ) lowercase_ = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/test_shap_e_img2img_out.npy''' ) lowercase_ = ShapEImgaImgPipeline.from_pretrained('''openai/shap-e-img2img''' ) lowercase_ = pipe.to(_SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE ) lowercase_ = torch.Generator(device=_SCREAMING_SNAKE_CASE ).manual_seed(0 ) lowercase_ = pipe( _SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , guidance_scale=3.0 , num_inference_steps=6_4 , frame_size=6_4 , output_type='''np''' , ).images[0] assert images.shape == (2_0, 6_4, 6_4, 3) assert_mean_pixel_difference(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )
97
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) __a = { 'configuration_encodec': [ 'ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP', 'EncodecConfig', ], 'feature_extraction_encodec': ['EncodecFeatureExtractor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST', 'EncodecModel', 'EncodecPreTrainedModel', ] if TYPE_CHECKING: from .configuration_encodec import ( ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP, EncodecConfig, ) from .feature_extraction_encodec import EncodecFeatureExtractor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_encodec import ( ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST, EncodecModel, EncodecPreTrainedModel, ) else: import sys __a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
30
0
'''simple docstring''' from collections import OrderedDict from typing import Any, List, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import logging SCREAMING_SNAKE_CASE = logging.get_logger(__name__) SCREAMING_SNAKE_CASE = { """EleutherAI/gpt-j-6B""": """https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json""", # See all GPT-J models at https://huggingface.co/models?filter=gpt_j } class UpperCAmelCase_ ( _a ): '''simple docstring''' lowercase_ : Tuple = "gptj" lowercase_ : Optional[Any] = { "max_position_embeddings": "n_positions", "hidden_size": "n_embd", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : Union[str, Any] , snake_case__ : Optional[int]=5_04_00 , snake_case__ : Optional[int]=20_48 , snake_case__ : Union[str, Any]=40_96 , snake_case__ : List[Any]=28 , snake_case__ : List[Any]=16 , snake_case__ : Optional[Any]=64 , snake_case__ : Optional[int]=None , snake_case__ : Dict="gelu_new" , snake_case__ : Optional[Any]=0.0 , snake_case__ : Union[str, Any]=0.0 , snake_case__ : Union[str, Any]=0.0 , snake_case__ : Union[str, Any]=1e-5 , snake_case__ : Tuple=0.02 , snake_case__ : Any=True , snake_case__ : List[Any]=5_02_56 , snake_case__ : int=5_02_56 , snake_case__ : Any=False , **snake_case__ : Tuple , ): '''simple docstring''' UpperCAmelCase__ : List[str] = vocab_size UpperCAmelCase__ : str = n_positions UpperCAmelCase__ : Dict = n_embd UpperCAmelCase__ : Tuple = n_layer UpperCAmelCase__ : Any = n_head UpperCAmelCase__ : Dict = n_inner UpperCAmelCase__ : Dict = rotary_dim UpperCAmelCase__ : Any = activation_function UpperCAmelCase__ : List[Any] = resid_pdrop UpperCAmelCase__ : Tuple = embd_pdrop UpperCAmelCase__ : Optional[Any] = attn_pdrop UpperCAmelCase__ : Optional[int] = layer_norm_epsilon UpperCAmelCase__ : int = initializer_range UpperCAmelCase__ : Dict = use_cache UpperCAmelCase__ : Dict = bos_token_id UpperCAmelCase__ : Union[str, Any] = eos_token_id super().__init__( bos_token_id=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE , tie_word_embeddings=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) class UpperCAmelCase_ ( _a ): '''simple docstring''' def __init__( self : List[Any] , snake_case__ : Optional[int] , snake_case__ : Tuple = "default" , snake_case__ : int = None , snake_case__ : List[Any] = False , ): '''simple docstring''' super().__init__(_SCREAMING_SNAKE_CASE , task=_SCREAMING_SNAKE_CASE , patching_specs=_SCREAMING_SNAKE_CASE , use_past=_SCREAMING_SNAKE_CASE ) if not getattr(self._config , "pad_token_id" , _SCREAMING_SNAKE_CASE ): # TODO: how to do that better? UpperCAmelCase__ : Any = 0 @property def UpperCamelCase ( self : List[str] ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = OrderedDict({"input_ids": {0: "batch", 1: "sequence"}} ) if self.use_past: self.fill_with_past_key_values_(_SCREAMING_SNAKE_CASE , direction="inputs" ) UpperCAmelCase__ : List[Any] = {0: '''batch''', 1: '''past_sequence + sequence'''} else: UpperCAmelCase__ : int = {0: '''batch''', 1: '''sequence'''} return common_inputs @property def UpperCamelCase ( self : Any ): '''simple docstring''' return self._config.n_layer @property def UpperCamelCase ( self : str ): '''simple docstring''' return self._config.n_head def UpperCamelCase ( self : Tuple , snake_case__ : Tuple , snake_case__ : Dict = -1 , snake_case__ : int = -1 , snake_case__ : Union[str, Any] = False , snake_case__ : str = None , ): '''simple docstring''' UpperCAmelCase__ : Any = super(_SCREAMING_SNAKE_CASE , self ).generate_dummy_inputs( _SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE , seq_length=_SCREAMING_SNAKE_CASE , is_pair=_SCREAMING_SNAKE_CASE , framework=_SCREAMING_SNAKE_CASE ) # We need to order the input in the way they appears in the forward() UpperCAmelCase__ : Union[str, Any] = 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 UpperCAmelCase__ : Any = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values UpperCAmelCase__ : int = seqlen + 2 UpperCAmelCase__ : Optional[Any] = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) UpperCAmelCase__ : Optional[Any] = [ (torch.zeros(_SCREAMING_SNAKE_CASE ), torch.zeros(_SCREAMING_SNAKE_CASE )) for _ in range(self.num_layers ) ] UpperCAmelCase__ : List[Any] = common_inputs['''attention_mask'''] if self.use_past: UpperCAmelCase__ : List[str] = ordered_inputs['''attention_mask'''].dtype UpperCAmelCase__ : Tuple = torch.cat( [ordered_inputs["attention_mask"], torch.ones(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , dtype=_SCREAMING_SNAKE_CASE )] , dim=1 ) return ordered_inputs @property def UpperCamelCase ( self : Dict ): '''simple docstring''' return 13
199
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __a = logging.get_logger(__name__) __a = { 'facebook/wav2vec2-base-960h': 'https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json', # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2 } class __a( _a ): """simple docstring""" lowerCAmelCase = '''wav2vec2''' def __init__( self ,_SCREAMING_SNAKE_CASE=32 ,_SCREAMING_SNAKE_CASE=768 ,_SCREAMING_SNAKE_CASE=12 ,_SCREAMING_SNAKE_CASE=12 ,_SCREAMING_SNAKE_CASE=3_072 ,_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="sum" ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=256 ,_SCREAMING_SNAKE_CASE=(512, 512, 512, 512, 1_500) ,_SCREAMING_SNAKE_CASE=(5, 3, 3, 1, 1) ,_SCREAMING_SNAKE_CASE=(1, 2, 3, 1, 1) ,_SCREAMING_SNAKE_CASE=512 ,_SCREAMING_SNAKE_CASE=0 ,_SCREAMING_SNAKE_CASE=1 ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=3 ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE=3 ,_SCREAMING_SNAKE_CASE=None ,_SCREAMING_SNAKE_CASE=None ,**_SCREAMING_SNAKE_CASE ,) -> Optional[int]: super().__init__(**_SCREAMING_SNAKE_CASE ,pad_token_id=_SCREAMING_SNAKE_CASE ,bos_token_id=_SCREAMING_SNAKE_CASE ,eos_token_id=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = hidden_size UpperCAmelCase_ : Tuple = feat_extract_norm UpperCAmelCase_ : List[Any] = feat_extract_activation UpperCAmelCase_ : str = list(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Any = list(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = list(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = conv_bias UpperCAmelCase_ : str = num_conv_pos_embeddings UpperCAmelCase_ : Any = num_conv_pos_embedding_groups UpperCAmelCase_ : Tuple = len(self.conv_dim ) UpperCAmelCase_ : Union[str, Any] = num_hidden_layers UpperCAmelCase_ : Dict = intermediate_size UpperCAmelCase_ : Any = hidden_act UpperCAmelCase_ : Any = num_attention_heads UpperCAmelCase_ : str = hidden_dropout UpperCAmelCase_ : int = attention_dropout UpperCAmelCase_ : Tuple = activation_dropout UpperCAmelCase_ : List[str] = feat_proj_dropout UpperCAmelCase_ : int = final_dropout UpperCAmelCase_ : Union[str, Any] = layerdrop UpperCAmelCase_ : Optional[Any] = layer_norm_eps UpperCAmelCase_ : str = initializer_range UpperCAmelCase_ : List[str] = vocab_size UpperCAmelCase_ : Optional[int] = do_stable_layer_norm UpperCAmelCase_ : Optional[int] = use_weighted_layer_sum 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 UpperCAmelCase_ : Optional[int] = apply_spec_augment UpperCAmelCase_ : Tuple = mask_time_prob UpperCAmelCase_ : Optional[Any] = mask_time_length UpperCAmelCase_ : Union[str, Any] = mask_time_min_masks UpperCAmelCase_ : Optional[Any] = mask_feature_prob UpperCAmelCase_ : str = mask_feature_length UpperCAmelCase_ : Dict = mask_feature_min_masks # parameters for pretraining with codevector quantized representations UpperCAmelCase_ : Union[str, Any] = num_codevectors_per_group UpperCAmelCase_ : Any = num_codevector_groups UpperCAmelCase_ : Union[str, Any] = contrastive_logits_temperature UpperCAmelCase_ : List[str] = feat_quantizer_dropout UpperCAmelCase_ : Dict = num_negatives UpperCAmelCase_ : List[str] = codevector_dim UpperCAmelCase_ : List[str] = proj_codevector_dim UpperCAmelCase_ : str = diversity_loss_weight # ctc loss UpperCAmelCase_ : List[Any] = ctc_loss_reduction UpperCAmelCase_ : List[str] = ctc_zero_infinity # adapter UpperCAmelCase_ : Optional[Any] = add_adapter UpperCAmelCase_ : Any = adapter_kernel_size UpperCAmelCase_ : Optional[int] = adapter_stride UpperCAmelCase_ : List[Any] = num_adapter_layers UpperCAmelCase_ : Optional[Any] = output_hidden_size or hidden_size UpperCAmelCase_ : Optional[int] = adapter_attn_dim # SequenceClassification-specific parameter. Feel free to ignore for other classes. UpperCAmelCase_ : List[str] = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. UpperCAmelCase_ : List[str] = list(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = list(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = list(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Any = xvector_output_dim @property def a__ ( self ) -> Any: return functools.reduce(operator.mul ,self.conv_stride ,1 )
30
0
import warnings from ...utils import logging from .image_processing_flava import FlavaImageProcessor __lowerCAmelCase : int =logging.get_logger(__name__) class _lowercase ( _a ): '''simple docstring''' def __init__( self :Any , *lowerCAmelCase__ :int , **lowerCAmelCase__ :Dict ) -> None: warnings.warn( '''The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use FlavaImageProcessor instead.''' , _SCREAMING_SNAKE_CASE , ) super().__init__(*_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE )
696
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __a = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ['NllbTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ['NllbTokenizerFast'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb import NllbTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb_fast import NllbTokenizerFast else: import sys __a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
30
0
def UpperCAmelCase__ (UpperCamelCase_ ,UpperCamelCase_ ): """simple docstring""" while a != 0: snake_case = b % a, a return b def UpperCAmelCase__ (UpperCamelCase_ ,UpperCamelCase_ ): """simple docstring""" if gcd(_lowercase ,_lowercase ) != 1: snake_case = F'''mod inverse of {a!r} and {m!r} does not exist''' raise ValueError(_lowercase ) snake_case = 1, 0, a snake_case = 0, 1, m while va != 0: snake_case = ua // va snake_case = (ua - q * va), (ua - q * va), (ua - q * va), va, va, va return ua % m
550
import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList __a = ['\nclass', '\ndef', '\n#', '\n@', '\nprint', '\nif'] class __a( _a ): """simple docstring""" def __init__( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE=None ,_SCREAMING_SNAKE_CASE=1 ) -> Dict: UpperCAmelCase_ : List[Any] = tokenizer UpperCAmelCase_ : int = dataset UpperCAmelCase_ : Dict = len(_SCREAMING_SNAKE_CASE ) if n_tasks is None else n_tasks UpperCAmelCase_ : Optional[int] = n_copies def __iter__( self ) -> Any: UpperCAmelCase_ : List[Any] = [] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['''prompt'''].strip() ) UpperCAmelCase_ : Union[str, Any] = self.tokenizer(_SCREAMING_SNAKE_CASE ,padding=_SCREAMING_SNAKE_CASE ,return_tensors='''pt''' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class __a( _a ): """simple docstring""" def __init__( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) -> List[Any]: UpperCAmelCase_ : str = start_length UpperCAmelCase_ : Optional[int] = eof_strings UpperCAmelCase_ : str = tokenizer def __call__( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ) -> List[Any]: UpperCAmelCase_ : Optional[Any] = self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) UpperCAmelCase_ : Optional[int] = [] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(_SCREAMING_SNAKE_CASE ) def lowerCamelCase__ ( _lowercase ): '''simple docstring''' UpperCAmelCase_ : Tuple = re.split('''(%s)''' % '''|'''.join(_lowercase ) , _lowercase ) # last string should be "" return "".join(string_list[:-2] ) def lowerCamelCase__ ( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase=20 , **_lowercase ): '''simple docstring''' UpperCAmelCase_ : List[Any] = defaultdict(_lowercase ) # dict of list of generated tokens for step, batch in tqdm(enumerate(_lowercase ) ): with torch.no_grad(): UpperCAmelCase_ : Dict = batch['''ids'''].shape[-1] UpperCAmelCase_ : Optional[Any] = accelerator.unwrap_model(_lowercase ).generate( input_ids=batch['''ids'''][:, : batch['''input_len''']] , num_return_sequences=_lowercase , **_lowercase ) # each task is generated batch_size times UpperCAmelCase_ : Union[str, Any] = batch['''task_id'''].repeat(_lowercase ) UpperCAmelCase_ : Dict = accelerator.pad_across_processes( _lowercase , dim=1 , pad_index=tokenizer.pad_token_id ) UpperCAmelCase_, UpperCAmelCase_ : List[str] = accelerator.gather((generated_tokens, generated_tasks) ) UpperCAmelCase_ : Union[str, Any] = generated_tokens.cpu().numpy() UpperCAmelCase_ : Union[str, Any] = generated_tasks.cpu().numpy() for task, generated_tokens in zip(_lowercase , _lowercase ): gen_token_dict[task].append(_lowercase ) UpperCAmelCase_ : Union[str, Any] = [[] for _ in range(_lowercase )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: UpperCAmelCase_ : int = tokenizer.decode(_lowercase , skip_special_tokens=_lowercase , clean_up_tokenization_spaces=_lowercase ) code_gens[task].append(remove_last_block(_lowercase ) ) return code_gens def lowerCamelCase__ ( ): '''simple docstring''' UpperCAmelCase_ : Optional[Any] = HfArgumentParser(_lowercase ) UpperCAmelCase_ : int = parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric UpperCAmelCase_ : Optional[Any] = args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing UpperCAmelCase_ : List[Any] = '''false''' if args.num_workers is None: UpperCAmelCase_ : Optional[Any] = multiprocessing.cpu_count() # Use dataset load to feed to accelerate UpperCAmelCase_ : int = Accelerator() set_seed(args.seed , device_specific=_lowercase ) # Load model and tokenizer UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained(args.model_ckpt ) UpperCAmelCase_ : Any = tokenizer.eos_token UpperCAmelCase_ : List[str] = AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings UpperCAmelCase_ : str = { '''do_sample''': args.do_sample, '''temperature''': args.temperature, '''max_new_tokens''': args.max_new_tokens, '''top_p''': args.top_p, '''top_k''': args.top_k, '''stopping_criteria''': StoppingCriteriaList([EndOfFunctionCriteria(0 , _lowercase , _lowercase )] ), } # Load evaluation dataset and metric UpperCAmelCase_ : Tuple = load_dataset('''openai_humaneval''' ) UpperCAmelCase_ : Dict = load_metric('''code_eval''' ) UpperCAmelCase_ : Optional[int] = args.num_tasks if args.num_tasks is not None else len(human_eval['''test'''] ) UpperCAmelCase_ : str = args.n_samples // args.batch_size UpperCAmelCase_ : str = TokenizedDataset(_lowercase , human_eval['''test'''] , n_copies=_lowercase , n_tasks=_lowercase ) # do not confuse args.batch_size, which is actually the num_return_sequences UpperCAmelCase_ : Optional[Any] = DataLoader(_lowercase , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: UpperCAmelCase_ : Any = code_eval_metric.compute(references=[''''''] , predictions=[['''''']] ) except ValueError as exception: print( '''Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`''' ''' flag to enable code evaluation.''' ) raise exception UpperCAmelCase_, UpperCAmelCase_ : int = accelerator.prepare(_lowercase , _lowercase ) UpperCAmelCase_ : int = complete_code( _lowercase , _lowercase , _lowercase , _lowercase , n_tasks=_lowercase , batch_size=args.batch_size , **_lowercase , ) if accelerator.is_main_process: UpperCAmelCase_ : Any = [] for task in tqdm(range(_lowercase ) ): UpperCAmelCase_ : int = human_eval['''test'''][task]['''test'''] UpperCAmelCase_ : str = f'''check({human_eval["test"][task]["entry_point"]})''' references.append('''\n''' + test_func + '''\n''' + entry_point ) # Evaluate completions with "code_eval" metric UpperCAmelCase_, UpperCAmelCase_ : Any = code_eval_metric.compute( references=_lowercase , predictions=_lowercase , num_workers=args.num_workers ) print(f'''Results: {pass_at_k}''' ) # Save results to json file with open(args.output_file , '''w''' ) as fp: json.dump(_lowercase , _lowercase ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
30
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available A_ = { "configuration_bloom": ["BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP", "BloomConfig", "BloomOnnxConfig"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ = ["BloomTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ = [ "BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST", "BloomForCausalLM", "BloomModel", "BloomPreTrainedModel", "BloomForSequenceClassification", "BloomForTokenClassification", "BloomForQuestionAnswering", ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys A_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
270
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, TensorType __a = logging.get_logger(__name__) __a = { 'openai/imagegpt-small': '', 'openai/imagegpt-medium': '', 'openai/imagegpt-large': '', } class __a( _a ): """simple docstring""" lowerCAmelCase = '''imagegpt''' lowerCAmelCase = ['''past_key_values'''] lowerCAmelCase = { '''hidden_size''': '''n_embd''', '''max_position_embeddings''': '''n_positions''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self ,_SCREAMING_SNAKE_CASE=512 + 1 ,_SCREAMING_SNAKE_CASE=32 * 32 ,_SCREAMING_SNAKE_CASE=512 ,_SCREAMING_SNAKE_CASE=24 ,_SCREAMING_SNAKE_CASE=8 ,_SCREAMING_SNAKE_CASE=None ,_SCREAMING_SNAKE_CASE="quick_gelu" ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=1e-5 ,_SCREAMING_SNAKE_CASE=0.02 ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=False ,**_SCREAMING_SNAKE_CASE ,) -> Optional[int]: UpperCAmelCase_ : Optional[int] = vocab_size UpperCAmelCase_ : Union[str, Any] = n_positions UpperCAmelCase_ : Union[str, Any] = n_embd UpperCAmelCase_ : Any = n_layer UpperCAmelCase_ : Optional[Any] = n_head UpperCAmelCase_ : Union[str, Any] = n_inner UpperCAmelCase_ : List[Any] = activation_function UpperCAmelCase_ : List[str] = resid_pdrop UpperCAmelCase_ : str = embd_pdrop UpperCAmelCase_ : Optional[Any] = attn_pdrop UpperCAmelCase_ : Dict = layer_norm_epsilon UpperCAmelCase_ : Union[str, Any] = initializer_range UpperCAmelCase_ : Dict = scale_attn_weights UpperCAmelCase_ : Any = use_cache UpperCAmelCase_ : List[str] = scale_attn_by_inverse_layer_idx UpperCAmelCase_ : Tuple = reorder_and_upcast_attn UpperCAmelCase_ : int = tie_word_embeddings super().__init__(tie_word_embeddings=_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ) class __a( _a ): """simple docstring""" @property def a__ ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ] ) def a__ ( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE = 1 ,_SCREAMING_SNAKE_CASE = -1 ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = 3 ,_SCREAMING_SNAKE_CASE = 32 ,_SCREAMING_SNAKE_CASE = 32 ,) -> Mapping[str, Any]: UpperCAmelCase_ : Any = self._generate_dummy_images(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = dict(preprocessor(images=_SCREAMING_SNAKE_CASE ,return_tensors=_SCREAMING_SNAKE_CASE ) ) return inputs
30
0
'''simple docstring''' import math import qiskit def A__ ( A : Any = 1 , A : Optional[int] = 1 , A : Dict = 1): '''simple docstring''' if ( isinstance(_lowercase , _lowercase) or isinstance(_lowercase , _lowercase) or isinstance(_lowercase , _lowercase) ): raise TypeError("inputs must be integers.") if (input_a < 0) or (input_a < 0) or (carry_in < 0): raise ValueError("inputs must be positive.") if ( (math.floor(_lowercase) != input_a) or (math.floor(_lowercase) != input_a) or (math.floor(_lowercase) != carry_in) ): raise ValueError("inputs must be exact integers.") if (input_a > 2) or (input_a > 2) or (carry_in > 2): raise ValueError("inputs must be less or equal to 2.") # build registers UpperCamelCase : List[str] = qiskit.QuantumRegister(4 , "qr") UpperCamelCase : Tuple = qiskit.ClassicalRegister(2 , "cr") # list the entries UpperCamelCase : str = [input_a, input_a, carry_in] UpperCamelCase : str = qiskit.QuantumCircuit(_lowercase , _lowercase) for i in range(0 , 3): if entry[i] == 2: quantum_circuit.h(_lowercase) # for hadamard entries elif entry[i] == 1: quantum_circuit.x(_lowercase) # for 1 entries elif entry[i] == 0: quantum_circuit.i(_lowercase) # for 0 entries # build the circuit quantum_circuit.ccx(0 , 1 , 3) # ccx = toffoli gate quantum_circuit.cx(0 , 1) quantum_circuit.ccx(1 , 2 , 3) quantum_circuit.cx(1 , 2) quantum_circuit.cx(0 , 1) quantum_circuit.measure([2, 3] , _lowercase) # measure the last two qbits UpperCamelCase : Union[str, Any] = qiskit.Aer.get_backend("aer_simulator") UpperCamelCase : str = qiskit.execute(_lowercase , _lowercase , shots=10_00) return job.result().get_counts(_lowercase) if __name__ == "__main__": print(f"""Total sum count for state is: {quantum_full_adder(1, 1, 1)}""")
173
import argparse import json import os import re import torch from transformers import BloomConfig, BloomModel from transformers.file_utils import CONFIG_NAME, WEIGHTS_NAME from transformers.utils import logging logging.set_verbosity_info() __a = [ 'word_embeddings_layernorm.weight', 'word_embeddings_layernorm.bias', 'input_layernorm.weight', 'input_layernorm.bias', 'post_attention_layernorm.weight', 'post_attention_layernorm.bias', 'self_attention.dense.bias', 'mlp.dense_4h_to_h.bias', 'ln_f.weight', 'ln_f.bias', ] __a = [ 'mlp.dense_4h_to_h.weight', 'self_attention.dense.weight', ] def lowerCamelCase__ ( _lowercase , _lowercase ): '''simple docstring''' UpperCAmelCase_ : List[Any] = { '''word_embeddings.weight''': '''word_embeddings.weight''', '''word_embeddings.norm.weight''': '''word_embeddings_layernorm.weight''', '''word_embeddings.norm.bias''': '''word_embeddings_layernorm.bias''', '''weight''': '''ln_f.weight''', '''bias''': '''ln_f.bias''', } if key in layer_rename_map: return layer_rename_map[key] # Handle transformer blocks UpperCAmelCase_ : Union[str, Any] = int(re.match(r'''.*layer_(\d*).*''' , _lowercase )[1] ) layer_number -= 3 return f'''h.{layer_number}.''' + key def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if dtype == torch.bool: return 1 / 8 UpperCAmelCase_ : Any = re.search(r'''[^\d](\d+)$''' , str(_lowercase ) ) if bit_search is None: raise ValueError(f'''`dtype` is not a valid dtype: {dtype}.''' ) UpperCAmelCase_ : Optional[int] = int(bit_search.groups()[0] ) return bit_size // 8 def lowerCamelCase__ ( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ): '''simple docstring''' if bloom_config_file == "": UpperCAmelCase_ : Tuple = BloomConfig() else: UpperCAmelCase_ : Optional[int] = BloomConfig.from_json_file(_lowercase ) if shard_model: UpperCAmelCase_ : Any = os.listdir(_lowercase ) UpperCAmelCase_ : Union[str, Any] = sorted(filter(lambda _lowercase : s.startswith('''layer''' ) and "model_00" in s , _lowercase ) ) UpperCAmelCase_ : Any = {'''weight_map''': {}, '''metadata''': {}} UpperCAmelCase_ : List[str] = 0 UpperCAmelCase_ : Any = None UpperCAmelCase_ : Optional[int] = BloomConfig() for j, file in enumerate(_lowercase ): print('''Processing file: {}'''.format(_lowercase ) ) UpperCAmelCase_ : Optional[Any] = None for i in range(_lowercase ): # load all TP files UpperCAmelCase_ : Tuple = file.replace('''model_00''' , f'''model_0{i}''' ) UpperCAmelCase_ : Any = torch.load(os.path.join(_lowercase , _lowercase ) , map_location='''cpu''' ) # Rename keys in the transformers names UpperCAmelCase_ : Dict = list(temp.keys() ) for key in keys: UpperCAmelCase_ : Union[str, Any] = temp.pop(_lowercase ) if tensors is None: UpperCAmelCase_ : Union[str, Any] = temp else: for key in tensors.keys(): if any(key.endswith(_lowercase ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel UpperCAmelCase_ : int = 1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks UpperCAmelCase_ : Tuple = torch.cat([tensors[key], temp[key]] , dim=_lowercase ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(_lowercase ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): UpperCAmelCase_ : List[str] = tensors[key] / pretraining_tp torch.save( _lowercase , os.path.join( _lowercase , '''pytorch_model_{}-of-{}.bin'''.format(str(j + 1 ).zfill(5 ) , str(len(_lowercase ) ).zfill(5 ) ) , ) , ) for key in tensors.keys(): UpperCAmelCase_ : Union[str, Any] = tensors[key] total_size += value.numel() * get_dtype_size(value.dtype ) if key not in index_dict["weight_map"]: UpperCAmelCase_ : List[str] = '''pytorch_model_{}-of-{}.bin'''.format( str(j + 1 ).zfill(5 ) , str(len(_lowercase ) ).zfill(5 ) ) UpperCAmelCase_ : List[Any] = BloomConfig() UpperCAmelCase_ : Tuple = pytorch_dump_folder_path + '''/''' + CONFIG_NAME UpperCAmelCase_ : List[str] = total_size with open(_lowercase , '''w''' , encoding='''utf-8''' ) as f: f.write(config.to_json_string() ) with open(os.path.join(_lowercase , WEIGHTS_NAME + '''.index.json''' ) , '''w''' , encoding='''utf-8''' ) as f: UpperCAmelCase_ : Optional[Any] = json.dumps(_lowercase , indent=2 , sort_keys=_lowercase ) + '''\n''' f.write(_lowercase ) else: UpperCAmelCase_ : Any = BloomModel(_lowercase ) UpperCAmelCase_ : Tuple = os.listdir(_lowercase ) UpperCAmelCase_ : Union[str, Any] = sorted(filter(lambda _lowercase : s.startswith('''layer''' ) and "model_00" in s , _lowercase ) ) UpperCAmelCase_ : Any = None for i, file in enumerate(_lowercase ): UpperCAmelCase_ : Optional[Any] = None for i in range(_lowercase ): # load all TP files UpperCAmelCase_ : List[Any] = file.replace('''model_00''' , f'''model_0{i}''' ) UpperCAmelCase_ : Optional[int] = torch.load(os.path.join(_lowercase , _lowercase ) , map_location='''cpu''' ) # Rename keys in the transformers names UpperCAmelCase_ : str = list(temp.keys() ) for key in keys: UpperCAmelCase_ : Dict = temp.pop(_lowercase ) if tensors is None: UpperCAmelCase_ : int = temp else: for key in tensors.keys(): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) if any(key.endswith(_lowercase ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel UpperCAmelCase_ : Optional[int] = 1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks UpperCAmelCase_ : List[str] = torch.cat([tensors[key], temp[key]] , dim=_lowercase ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(_lowercase ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): UpperCAmelCase_ : Dict = tensors[key] / pretraining_tp UpperCAmelCase_ : Tuple = model.load_state_dict(_lowercase , strict=_lowercase ) assert not other_keys.unexpected_keys, f'''The keys {other_keys.unexpected_keys} are unexpected''' if missing_keys is None: UpperCAmelCase_ : Union[str, Any] = set(other_keys.missing_keys ) else: UpperCAmelCase_ : Dict = missing_keys.intersection(set(other_keys.missing_keys ) ) assert not missing_keys, f'''The keys {missing_keys} are missing''' # Save pytorch-model os.makedirs(_lowercase , exist_ok=_lowercase ) UpperCAmelCase_ : str = pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME UpperCAmelCase_ : Dict = pytorch_dump_folder_path + '''/''' + CONFIG_NAME print(f'''Save PyTorch model to {pytorch_weights_dump_path} with dtype {config.torch_dtype}''' ) if config.torch_dtype is not None: UpperCAmelCase_ : Optional[int] = model.to(config.torch_dtype ) torch.save(model.state_dict() , _lowercase ) print(f'''Save configuration file to {pytorch_config_dump_path}''' ) with open(_lowercase , '''w''' , encoding='''utf-8''' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": __a = argparse.ArgumentParser() # Required parameters parser.add_argument( '--bloom_checkpoint_path', default=None, type=str, required=True, help='Path to the Megatron-LM checkpoint path.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) parser.add_argument( '--bloom_config_file', default='', type=str, help=( 'An optional config json file corresponding to the pre-trained model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--shard_model', action='store_true', help='An optional setting to shard the output model \nThis enables sharding the converted checkpoint', ) parser.add_argument( '--pretraining_tp', default=4, type=int, help='Pretraining TP rank that has been used when training the model in Megatron-LM \n', ) __a = parser.parse_args() convert_bloom_checkpoint_to_pytorch( args.bloom_checkpoint_path, args.bloom_config_file, args.pytorch_dump_folder_path, args.shard_model, args.pretraining_tp, )
30
0
import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert import BertTokenizer SCREAMING_SNAKE_CASE: List[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE: Optional[int] = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} SCREAMING_SNAKE_CASE: str = { '''vocab_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } SCREAMING_SNAKE_CASE: Union[str, Any] = { '''vocab_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } SCREAMING_SNAKE_CASE: List[Any] = { '''vocab_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json''' ), }, } SCREAMING_SNAKE_CASE: Optional[Any] = { '''facebook/dpr-ctx_encoder-single-nq-base''': 5_1_2, '''facebook/dpr-ctx_encoder-multiset-base''': 5_1_2, } SCREAMING_SNAKE_CASE: str = { '''facebook/dpr-question_encoder-single-nq-base''': 5_1_2, '''facebook/dpr-question_encoder-multiset-base''': 5_1_2, } SCREAMING_SNAKE_CASE: Tuple = { '''facebook/dpr-reader-single-nq-base''': 5_1_2, '''facebook/dpr-reader-multiset-base''': 5_1_2, } SCREAMING_SNAKE_CASE: str = { '''facebook/dpr-ctx_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-ctx_encoder-multiset-base''': {'''do_lower_case''': True}, } SCREAMING_SNAKE_CASE: List[str] = { '''facebook/dpr-question_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-question_encoder-multiset-base''': {'''do_lower_case''': True}, } SCREAMING_SNAKE_CASE: str = { '''facebook/dpr-reader-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-reader-multiset-base''': {'''do_lower_case''': True}, } class lowercase_ (_a ): lowerCAmelCase__ =VOCAB_FILES_NAMES lowerCAmelCase__ =CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase__ =CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase__ =CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION class lowercase_ (_a ): lowerCAmelCase__ =VOCAB_FILES_NAMES lowerCAmelCase__ =QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase__ =QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase__ =QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION SCREAMING_SNAKE_CASE: Tuple = collections.namedtuple( '''DPRSpanPrediction''', ['''span_score''', '''relevance_score''', '''doc_id''', '''start_index''', '''end_index''', '''text'''] ) SCREAMING_SNAKE_CASE: Optional[Any] = collections.namedtuple('''DPRReaderOutput''', ['''start_logits''', '''end_logits''', '''relevance_logits''']) SCREAMING_SNAKE_CASE: str = r'''\n Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`.\n It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers),\n using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)`\n with the format:\n\n ```\n [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>\n ```\n\n Args:\n questions (`str` or `List[str]`):\n The questions to be encoded. You can specify one question for many passages. In this case, the question\n will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in\n `titles` or `texts`.\n titles (`str` or `List[str]`):\n The passages titles to be encoded. This can be a string or a list of strings if there are several passages.\n texts (`str` or `List[str]`):\n The passages texts to be encoded. This can be a string or a list of strings if there are several passages.\n padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):\n Activates and controls padding. Accepts the following values:\n\n - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence\n if provided).\n - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided.\n - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different\n lengths).\n truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):\n Activates and controls truncation. Accepts the following values:\n\n - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to\n the maximum acceptable input length for the model if that argument is not provided. This will truncate\n token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch\n of pairs) is provided.\n - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the first\n sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the\n second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths\n greater than the model maximum admissible input size).\n max_length (`int`, *optional*):\n Controls the maximum length to use by one of the truncation/padding parameters.\n\n If left unset or set to `None`, this will use the predefined model maximum length if a maximum length\n is required by one of the truncation/padding parameters. If the model has no specific maximum input\n length (like XLNet) truncation/padding to a maximum length will be deactivated.\n return_tensors (`str` or [`~utils.TensorType`], *optional*):\n If set, will return tensors instead of list of python integers. Acceptable values are:\n\n - `\'tf\'`: Return TensorFlow `tf.constant` objects.\n - `\'pt\'`: Return PyTorch `torch.Tensor` objects.\n - `\'np\'`: Return Numpy `np.ndarray` objects.\n return_attention_mask (`bool`, *optional*):\n Whether or not to return the attention mask. If not set, will return the attention mask according to the\n specific tokenizer\'s default, defined by the `return_outputs` attribute.\n\n [What are attention masks?](../glossary#attention-mask)\n\n Returns:\n `Dict[str, List[List[int]]]`: A dictionary with the following keys:\n\n - `input_ids`: List of token ids to be fed to a model.\n - `attention_mask`: List of indices specifying which tokens should be attended to by the model.\n ''' @add_start_docstrings(_a ) class lowercase_ : def __call__( self : str , snake_case__ : List[str] , snake_case__ : Tuple = None , snake_case__ : List[str] = None , snake_case__ : Optional[int] = False , snake_case__ : Union[str, Any] = False , snake_case__ : List[Any] = None , snake_case__ : List[Any] = None , snake_case__ : List[Any] = None , **snake_case__ : List[Any] , ): """simple docstring""" if titles is None and texts is None: return super().__call__( _SCREAMING_SNAKE_CASE , padding=_SCREAMING_SNAKE_CASE , truncation=_SCREAMING_SNAKE_CASE , max_length=_SCREAMING_SNAKE_CASE , return_tensors=_SCREAMING_SNAKE_CASE , return_attention_mask=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) elif titles is None or texts is None: SCREAMING_SNAKE_CASE_ = titles if texts is None else texts return super().__call__( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , padding=_SCREAMING_SNAKE_CASE , truncation=_SCREAMING_SNAKE_CASE , max_length=_SCREAMING_SNAKE_CASE , return_tensors=_SCREAMING_SNAKE_CASE , return_attention_mask=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) SCREAMING_SNAKE_CASE_ = titles if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else [titles] SCREAMING_SNAKE_CASE_ = texts if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else [texts] SCREAMING_SNAKE_CASE_ = len(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = questions if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else [questions] * n_passages if len(_SCREAMING_SNAKE_CASE ) != len(_SCREAMING_SNAKE_CASE ): raise ValueError( f'''There should be as many titles than texts but got {len(_SCREAMING_SNAKE_CASE )} titles and {len(_SCREAMING_SNAKE_CASE )} texts.''' ) SCREAMING_SNAKE_CASE_ = super().__call__(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , padding=_SCREAMING_SNAKE_CASE , truncation=_SCREAMING_SNAKE_CASE )['''input_ids'''] SCREAMING_SNAKE_CASE_ = super().__call__(_SCREAMING_SNAKE_CASE , add_special_tokens=_SCREAMING_SNAKE_CASE , padding=_SCREAMING_SNAKE_CASE , truncation=_SCREAMING_SNAKE_CASE )['''input_ids'''] SCREAMING_SNAKE_CASE_ = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ] } if return_attention_mask is not False: SCREAMING_SNAKE_CASE_ = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) SCREAMING_SNAKE_CASE_ = attention_mask return self.pad(_SCREAMING_SNAKE_CASE , padding=_SCREAMING_SNAKE_CASE , max_length=_SCREAMING_SNAKE_CASE , return_tensors=_SCREAMING_SNAKE_CASE ) def __a ( self : Optional[int] , snake_case__ : Tuple , snake_case__ : str , snake_case__ : Any = 16 , snake_case__ : Any = 64 , snake_case__ : Optional[int] = 4 , ): """simple docstring""" SCREAMING_SNAKE_CASE_ = reader_input['''input_ids'''] SCREAMING_SNAKE_CASE_ = reader_output[:3] SCREAMING_SNAKE_CASE_ = len(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = sorted(range(_SCREAMING_SNAKE_CASE ) , reverse=_SCREAMING_SNAKE_CASE , key=relevance_logits.__getitem__ ) SCREAMING_SNAKE_CASE_ = [] for doc_id in sorted_docs: SCREAMING_SNAKE_CASE_ = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence SCREAMING_SNAKE_CASE_ = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: SCREAMING_SNAKE_CASE_ = sequence_ids.index(self.pad_token_id ) else: SCREAMING_SNAKE_CASE_ = len(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=_SCREAMING_SNAKE_CASE , top_spans=_SCREAMING_SNAKE_CASE , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=_SCREAMING_SNAKE_CASE , start_index=_SCREAMING_SNAKE_CASE , end_index=_SCREAMING_SNAKE_CASE , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(_SCREAMING_SNAKE_CASE ) >= num_spans: break return nbest_spans_predictions[:num_spans] def __a ( self : Optional[int] , snake_case__ : int , snake_case__ : Union[str, Any] , snake_case__ : str , snake_case__ : List[Any] , ): """simple docstring""" SCREAMING_SNAKE_CASE_ = [] for start_index, start_score in enumerate(_SCREAMING_SNAKE_CASE ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) SCREAMING_SNAKE_CASE_ = sorted(_SCREAMING_SNAKE_CASE , key=lambda snake_case__ : x[1] , reverse=_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = [] for (start_index, end_index), score in scores: if start_index > end_index: raise ValueError(f'''Wrong span indices: [{start_index}:{end_index}]''' ) SCREAMING_SNAKE_CASE_ = end_index - start_index + 1 if length > max_answer_length: raise ValueError(f'''Span is too long: {length} > {max_answer_length}''' ) if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(_SCREAMING_SNAKE_CASE ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_a ) class lowercase_ (_a , _a ): lowerCAmelCase__ =VOCAB_FILES_NAMES lowerCAmelCase__ =READER_PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase__ =READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase__ =READER_PRETRAINED_INIT_CONFIGURATION lowerCAmelCase__ =["input_ids", "attention_mask"]
360
def lowerCamelCase__ ( ): '''simple docstring''' UpperCAmelCase_ : Dict = 0 for i in range(1 , 1001 ): total += i**i return str(_lowercase )[-10:] if __name__ == "__main__": print(solution())
30
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available SCREAMING_SNAKE_CASE_ = { '''configuration_mask2former''': [ '''MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Mask2FormerConfig''', ], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ = ['''Mask2FormerImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ = [ '''MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Mask2FormerForUniversalSegmentation''', '''Mask2FormerModel''', '''Mask2FormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_maskaformer import MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskaFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_maskaformer import MaskaFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_maskaformer import ( MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST, MaskaFormerForUniversalSegmentation, MaskaFormerModel, MaskaFormerPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
426
import os import sys import warnings from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen from ..table import array_cast from ..utils.file_utils import is_local_path from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: import PIL.Image from .features import FeatureType __a = None __a = '<' if sys.byteorder == 'little' else '>' # Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image __a = [ np.dtype('|b1'), np.dtype('|u1'), np.dtype('<u2'), np.dtype('>u2'), np.dtype('<i2'), np.dtype('>i2'), np.dtype('<u4'), np.dtype('>u4'), np.dtype('<i4'), np.dtype('>i4'), np.dtype('<f4'), np.dtype('>f4'), np.dtype('<f8'), np.dtype('>f8'), ] @dataclass class __a: """simple docstring""" lowerCAmelCase = True lowerCAmelCase = None # Automatically constructed lowerCAmelCase = "PIL.Image.Image" lowerCAmelCase = pa.struct({'''bytes''': pa.binary(), '''path''': pa.string()} ) lowerCAmelCase = field(default='''Image''' , init=_a , repr=_a ) def __call__( self ) -> Tuple: return self.pa_type def a__ ( self ,_SCREAMING_SNAKE_CASE ) -> dict: if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('''To support encoding images, please install \'Pillow\'.''' ) if isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : List[str] = np.array(_SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ): return {"path": value, "bytes": None} elif isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ): return {"path": None, "bytes": value} elif isinstance(_SCREAMING_SNAKE_CASE ,np.ndarray ): # convert the image array to PNG/TIFF bytes return encode_np_array(_SCREAMING_SNAKE_CASE ) elif isinstance(_SCREAMING_SNAKE_CASE ,PIL.Image.Image ): # convert the PIL image to bytes (default format is PNG/TIFF) return encode_pil_image(_SCREAMING_SNAKE_CASE ) elif value.get('''path''' ) is not None and os.path.isfile(value['''path'''] ): # we set "bytes": None to not duplicate the data if they're already available locally return {"bytes": None, "path": value.get('''path''' )} elif value.get('''bytes''' ) is not None or value.get('''path''' ) is not None: # store the image bytes, and path is used to infer the image format using the file extension return {"bytes": value.get('''bytes''' ), "path": value.get('''path''' )} else: raise ValueError( f'''An image sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.''' ) def a__ ( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE=None ) -> "PIL.Image.Image": if not self.decode: raise RuntimeError('''Decoding is disabled for this feature. Please use Image(decode=True) instead.''' ) if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('''To support decoding images, please install \'Pillow\'.''' ) if token_per_repo_id is None: UpperCAmelCase_ : Dict = {} UpperCAmelCase_, UpperCAmelCase_ : Union[str, Any] = value['''path'''], value['''bytes'''] if bytes_ is None: if path is None: raise ValueError(f'''An image should have one of \'path\' or \'bytes\' but both are None in {value}.''' ) else: if is_local_path(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Tuple = PIL.Image.open(_SCREAMING_SNAKE_CASE ) else: UpperCAmelCase_ : Dict = path.split('''::''' )[-1] try: UpperCAmelCase_ : Optional[int] = string_to_dict(_SCREAMING_SNAKE_CASE ,config.HUB_DATASETS_URL )['''repo_id'''] UpperCAmelCase_ : Tuple = token_per_repo_id.get(_SCREAMING_SNAKE_CASE ) except ValueError: UpperCAmelCase_ : Optional[Any] = None with xopen(_SCREAMING_SNAKE_CASE ,'''rb''' ,use_auth_token=_SCREAMING_SNAKE_CASE ) as f: UpperCAmelCase_ : List[str] = BytesIO(f.read() ) UpperCAmelCase_ : Optional[Any] = PIL.Image.open(bytes_ ) else: UpperCAmelCase_ : List[Any] = PIL.Image.open(BytesIO(bytes_ ) ) image.load() # to avoid "Too many open files" errors return image def a__ ( self ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value return ( self if self.decode else { "bytes": Value('''binary''' ), "path": Value('''string''' ), } ) def a__ ( self ,_SCREAMING_SNAKE_CASE ) -> pa.StructArray: if pa.types.is_string(storage.type ): UpperCAmelCase_ : Dict = pa.array([None] * len(_SCREAMING_SNAKE_CASE ) ,type=pa.binary() ) UpperCAmelCase_ : Dict = pa.StructArray.from_arrays([bytes_array, storage] ,['''bytes''', '''path'''] ,mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): UpperCAmelCase_ : List[str] = pa.array([None] * len(_SCREAMING_SNAKE_CASE ) ,type=pa.string() ) UpperCAmelCase_ : Tuple = pa.StructArray.from_arrays([storage, path_array] ,['''bytes''', '''path'''] ,mask=storage.is_null() ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index('''bytes''' ) >= 0: UpperCAmelCase_ : Dict = storage.field('''bytes''' ) else: UpperCAmelCase_ : Any = pa.array([None] * len(_SCREAMING_SNAKE_CASE ) ,type=pa.binary() ) if storage.type.get_field_index('''path''' ) >= 0: UpperCAmelCase_ : int = storage.field('''path''' ) else: UpperCAmelCase_ : List[str] = pa.array([None] * len(_SCREAMING_SNAKE_CASE ) ,type=pa.string() ) UpperCAmelCase_ : Optional[Any] = pa.StructArray.from_arrays([bytes_array, path_array] ,['''bytes''', '''path'''] ,mask=storage.is_null() ) elif pa.types.is_list(storage.type ): UpperCAmelCase_ : Optional[Any] = pa.array( [encode_np_array(np.array(_SCREAMING_SNAKE_CASE ) )['''bytes'''] if arr is not None else None for arr in storage.to_pylist()] ,type=pa.binary() ,) UpperCAmelCase_ : Any = pa.array([None] * len(_SCREAMING_SNAKE_CASE ) ,type=pa.string() ) UpperCAmelCase_ : Dict = pa.StructArray.from_arrays( [bytes_array, path_array] ,['''bytes''', '''path'''] ,mask=bytes_array.is_null() ) return array_cast(_SCREAMING_SNAKE_CASE ,self.pa_type ) def a__ ( self ,_SCREAMING_SNAKE_CASE ) -> pa.StructArray: @no_op_if_value_is_null def path_to_bytes(_SCREAMING_SNAKE_CASE ): with xopen(_SCREAMING_SNAKE_CASE ,'''rb''' ) as f: UpperCAmelCase_ : Any = f.read() return bytes_ UpperCAmelCase_ : Union[str, Any] = pa.array( [ (path_to_bytes(x['''path'''] ) if x['''bytes'''] is None else x['''bytes''']) if x is not None else None for x in storage.to_pylist() ] ,type=pa.binary() ,) UpperCAmelCase_ : List[str] = pa.array( [os.path.basename(_SCREAMING_SNAKE_CASE ) if path is not None else None for path in storage.field('''path''' ).to_pylist()] ,type=pa.string() ,) UpperCAmelCase_ : Union[str, Any] = pa.StructArray.from_arrays([bytes_array, path_array] ,['''bytes''', '''path'''] ,mask=bytes_array.is_null() ) return array_cast(_SCREAMING_SNAKE_CASE ,self.pa_type ) def lowerCamelCase__ ( ): '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('''To support encoding images, please install \'Pillow\'.''' ) global _IMAGE_COMPRESSION_FORMATS if _IMAGE_COMPRESSION_FORMATS is None: PIL.Image.init() UpperCAmelCase_ : Optional[int] = list(set(PIL.Image.OPEN.keys() ) & set(PIL.Image.SAVE.keys() ) ) return _IMAGE_COMPRESSION_FORMATS def lowerCamelCase__ ( _lowercase ): '''simple docstring''' UpperCAmelCase_ : Optional[int] = BytesIO() if image.format in list_image_compression_formats(): UpperCAmelCase_ : int = image.format else: UpperCAmelCase_ : List[Any] = '''PNG''' if image.mode in ['''1''', '''L''', '''LA''', '''RGB''', '''RGBA'''] else '''TIFF''' image.save(_lowercase , format=_lowercase ) return buffer.getvalue() def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if hasattr(_lowercase , '''filename''' ) and image.filename != "": return {"path": image.filename, "bytes": None} else: return {"path": None, "bytes": image_to_bytes(_lowercase )} def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('''To support encoding images, please install \'Pillow\'.''' ) UpperCAmelCase_ : Tuple = array.dtype UpperCAmelCase_ : List[str] = dtype.byteorder if dtype.byteorder != '''=''' else _NATIVE_BYTEORDER UpperCAmelCase_ : Dict = dtype.kind UpperCAmelCase_ : Union[str, Any] = dtype.itemsize UpperCAmelCase_ : Optional[Any] = None # Multi-channel array case (only np.dtype("|u1") is allowed) if array.shape[2:]: UpperCAmelCase_ : Tuple = np.dtype('''|u1''' ) if dtype_kind not in ["u", "i"]: raise TypeError( f'''Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays.''' ) if dtype is not dest_dtype: warnings.warn(f'''Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'''' ) # Exact match elif dtype in _VALID_IMAGE_ARRAY_DTPYES: UpperCAmelCase_ : Union[str, Any] = dtype else: # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually) while dtype_itemsize >= 1: UpperCAmelCase_ : Union[str, Any] = dtype_byteorder + dtype_kind + str(_lowercase ) UpperCAmelCase_ : str = np.dtype(_lowercase ) if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES: warnings.warn(f'''Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'''' ) break else: dtype_itemsize //= 2 if dest_dtype is None: raise TypeError( f'''Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}''' ) UpperCAmelCase_ : Any = PIL.Image.fromarray(array.astype(_lowercase ) ) return {"path": None, "bytes": image_to_bytes(_lowercase )} def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('''To support encoding images, please install \'Pillow\'.''' ) if objs: UpperCAmelCase_, UpperCAmelCase_ : Tuple = first_non_null_value(_lowercase ) if isinstance(_lowercase , _lowercase ): return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs] if isinstance(_lowercase , np.ndarray ): UpperCAmelCase_ : Any = no_op_if_value_is_null(_lowercase ) return [obj_to_image_dict_func(_lowercase ) for obj in objs] elif isinstance(_lowercase , PIL.Image.Image ): UpperCAmelCase_ : Union[str, Any] = no_op_if_value_is_null(_lowercase ) return [obj_to_image_dict_func(_lowercase ) for obj in objs] else: return objs else: return objs
30
0
'''simple docstring''' import math def lowerCamelCase (_SCREAMING_SNAKE_CASE : int ): assert isinstance(_lowercase , _lowercase ) and ( number >= 0 ), "'number' must been an int and positive" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or not number % 2: # Negatives, 0, 1 and all even numbers are not primes return False __a : str = range(3 , int(math.sqrt(_lowercase ) + 1 ) , 2 ) return not any(not number % i for i in odd_numbers ) def lowerCamelCase (_SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : Union[str, Any]=1 , **_SCREAMING_SNAKE_CASE : Optional[Any] ): __a : Any = factor * value __a : List[str] = value while not is_prime(_lowercase ): value += 1 if not ("desc" in kwargs and kwargs["desc"] is True) else -1 if value == first_value_val: return next_prime(value + 1 , **_lowercase ) return value
476
import unittest from transformers import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision, slow, torch_device if is_torch_available(): import torch from transformers import AutoModelForImageClassification if is_vision_available(): from transformers import AutoImageProcessor @require_torch @require_vision class __a( unittest.TestCase ): """simple docstring""" @slow def a__ ( self ) -> List[str]: UpperCAmelCase_ : Tuple = AutoImageProcessor.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip''' ) UpperCAmelCase_ : Union[str, Any] = AutoModelForImageClassification.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip''' ) model.to(_SCREAMING_SNAKE_CASE ) from datasets import load_dataset UpperCAmelCase_ : Optional[int] = load_dataset('''nielsr/rvlcdip-demo''' ) UpperCAmelCase_ : Optional[Any] = dataset['''train'''][0]['''image'''].convert('''RGB''' ) UpperCAmelCase_ : str = image_processor(_SCREAMING_SNAKE_CASE ,return_tensors='''pt''' ).to(_SCREAMING_SNAKE_CASE ) # forward pass with torch.no_grad(): UpperCAmelCase_ : Optional[int] = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = outputs.logits UpperCAmelCase_ : Tuple = torch.Size((1, 16) ) self.assertEqual(logits.shape ,_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = torch.tensor( [-0.41_58, -0.40_92, -0.43_47] ,device=_SCREAMING_SNAKE_CASE ,dtype=torch.float ,) self.assertTrue(torch.allclose(logits[0, :3] ,_SCREAMING_SNAKE_CASE ,atol=1e-4 ) )
30
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available _lowercase = { 'configuration_ctrl': ['CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'CTRLConfig'], 'tokenization_ctrl': ['CTRLTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowercase = [ 'CTRL_PRETRAINED_MODEL_ARCHIVE_LIST', 'CTRLForSequenceClassification', 'CTRLLMHeadModel', 'CTRLModel', 'CTRLPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowercase = [ 'TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFCTRLForSequenceClassification', 'TFCTRLLMHeadModel', 'TFCTRLModel', 'TFCTRLPreTrainedModel', ] if TYPE_CHECKING: from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig from .tokenization_ctrl import CTRLTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ctrl import ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, CTRLPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_ctrl import ( TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, TFCTRLForSequenceClassification, TFCTRLLMHeadModel, TFCTRLModel, TFCTRLPreTrainedModel, ) else: import sys _lowercase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
306
import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor __a = logging.get_logger(__name__) class __a( _a ): """simple docstring""" def __init__( self ,*_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ) -> None: warnings.warn( '''The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use CLIPImageProcessor instead.''' ,_SCREAMING_SNAKE_CASE ,) super().__init__(*_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE )
30
0
import argparse import shutil from pathlib import Path from tqdm import tqdm from transformers import AutoTokenizer def __lowerCAmelCase ( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__=1_0_2_4 ): _lowercase: List[str] = [], [] _lowercase: Optional[Any] = list(zip(_lowercase , _lowercase ) ) _lowercase: int = sorted_examples[0] def is_too_big(__magic_name__ ): return tok(_lowercase , return_tensors="pt" ).input_ids.shape[1] > max_tokens for src, tgt in tqdm(sorted_examples[1:] ): _lowercase: int = new_src + ''' ''' + src _lowercase: int = new_tgt + ''' ''' + tgt if is_too_big(_lowercase ) or is_too_big(_lowercase ): # cant fit, finalize example finished_src.append(_lowercase ) finished_tgt.append(_lowercase ) _lowercase: Optional[int] = src, tgt else: # can fit, keep adding _lowercase: str = cand_src, cand_tgt # cleanup if new_src: assert new_tgt finished_src.append(_lowercase ) finished_tgt.append(_lowercase ) return finished_src, finished_tgt def __lowerCAmelCase ( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ): _lowercase: Tuple = Path(_lowercase ) save_path.mkdir(exist_ok=_lowercase ) for split in ["train"]: _lowercase: Any = data_dir / f"{split}.source", data_dir / f"{split}.target" _lowercase: List[str] = [x.rstrip() for x in Path(_lowercase ).open().readlines()] _lowercase: List[str] = [x.rstrip() for x in Path(_lowercase ).open().readlines()] _lowercase: Optional[int] = pack_examples(_lowercase , _lowercase , _lowercase , _lowercase ) print(f"packed {split} split from {len(_lowercase )} examples -> {len(_lowercase )}." ) Path(save_path / f"{split}.source" ).open("w" ).write("\n".join(_lowercase ) ) Path(save_path / f"{split}.target" ).open("w" ).write("\n".join(_lowercase ) ) for split in ["val", "test"]: _lowercase: Optional[Any] = data_dir / f"{split}.source", data_dir / f"{split}.target" shutil.copyfile(_lowercase , save_path / f"{split}.source" ) shutil.copyfile(_lowercase , save_path / f"{split}.target" ) def __lowerCAmelCase ( ): _lowercase: int = argparse.ArgumentParser() parser.add_argument("--tok_name" , type=_lowercase , help="like facebook/bart-large-cnn,t5-base, etc." ) parser.add_argument("--max_seq_len" , type=_lowercase , default=1_2_8 ) parser.add_argument("--data_dir" , type=_lowercase ) parser.add_argument("--save_path" , type=_lowercase ) _lowercase: Dict = parser.parse_args() _lowercase: Optional[Any] = AutoTokenizer.from_pretrained(args.tok_name ) return pack_data_dir(_lowercase , Path(args.data_dir ) , args.max_seq_len , args.save_path ) if __name__ == "__main__": packer_cli()
226
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 PoolFormerImageProcessor class __a( unittest.TestCase ): """simple docstring""" def __init__( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE=7 ,_SCREAMING_SNAKE_CASE=3 ,_SCREAMING_SNAKE_CASE=30 ,_SCREAMING_SNAKE_CASE=400 ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=None ,_SCREAMING_SNAKE_CASE=0.9 ,_SCREAMING_SNAKE_CASE=None ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5] ,_SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5] ,) -> Optional[int]: UpperCAmelCase_ : int = size if size is not None else {'''shortest_edge''': 30} UpperCAmelCase_ : List[str] = crop_size if crop_size is not None else {'''height''': 30, '''width''': 30} UpperCAmelCase_ : Dict = parent UpperCAmelCase_ : int = batch_size UpperCAmelCase_ : int = num_channels UpperCAmelCase_ : Any = min_resolution UpperCAmelCase_ : Tuple = max_resolution UpperCAmelCase_ : Optional[int] = do_resize_and_center_crop UpperCAmelCase_ : Tuple = size UpperCAmelCase_ : List[str] = crop_pct UpperCAmelCase_ : List[str] = crop_size UpperCAmelCase_ : Any = do_normalize UpperCAmelCase_ : str = image_mean UpperCAmelCase_ : List[Any] = image_std def a__ ( self ) -> str: return { "size": self.size, "do_resize_and_center_crop": self.do_resize_and_center_crop, "crop_pct": self.crop_pct, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class __a( _a , unittest.TestCase ): """simple docstring""" lowerCAmelCase = PoolFormerImageProcessor if is_vision_available() else None def a__ ( self ) -> Dict: UpperCAmelCase_ : str = PoolFormerImageProcessingTester(self ) @property def a__ ( self ) -> Optional[int]: return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self ) -> Optional[int]: UpperCAmelCase_ : List[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''do_resize_and_center_crop''' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''size''' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''crop_pct''' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''do_normalize''' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''image_mean''' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''image_std''' ) ) def a__ ( self ) -> Union[str, Any]: UpperCAmelCase_ : Any = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{'''shortest_edge''': 30} ) self.assertEqual(image_processor.crop_size ,{'''height''': 30, '''width''': 30} ) UpperCAmelCase_ : Optional[Any] = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ,crop_size=84 ) self.assertEqual(image_processor.size ,{'''shortest_edge''': 42} ) self.assertEqual(image_processor.crop_size ,{'''height''': 84, '''width''': 84} ) def a__ ( self ) -> Optional[int]: pass def a__ ( self ) -> Dict: # Initialize image_processing UpperCAmelCase_ : str = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCAmelCase_ : int = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE ,Image.Image ) # Test not batched input UpperCAmelCase_ : 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 UpperCAmelCase_ : Union[str, Any] = image_processing(_SCREAMING_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 a__ ( self ) -> List[Any]: # Initialize image_processing UpperCAmelCase_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCAmelCase_ : List[str] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_SCREAMING_SNAKE_CASE ,numpify=_SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE ,np.ndarray ) # Test not batched input UpperCAmelCase_ : Tuple = 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 UpperCAmelCase_ : List[Any] = image_processing(_SCREAMING_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 a__ ( self ) -> Union[str, Any]: # Initialize image_processing UpperCAmelCase_ : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCAmelCase_ : Optional[Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_SCREAMING_SNAKE_CASE ,torchify=_SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE ,torch.Tensor ) # Test not batched input UpperCAmelCase_ : Tuple = 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 UpperCAmelCase_ : List[Any] = image_processing(_SCREAMING_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'''], ) ,)
30
0
# 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 typing import TYPE_CHECKING from ..models.auto import AutoModelForVisionaSeq from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class lowercase__( _a ): """simple docstring""" a :str = 'Salesforce/blip-image-captioning-base' a :List[Any] = ( 'This is a tool that generates a description of an image. It takes an input named `image` which should be the ' 'image to caption, and returns a text that contains the description in English.' ) a :List[str] = 'image_captioner' a :int = AutoModelForVisionaSeq a :List[str] = ['image'] a :List[str] = ['text'] def __init__( self : Optional[Any] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Dict ) -> str: requires_backends(self , ['''vision'''] ) super().__init__(*_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def _lowercase ( self : int , SCREAMING_SNAKE_CASE_ : List[str] ) -> str: return self.pre_processor(images=_SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) def _lowercase ( self : str , SCREAMING_SNAKE_CASE_ : int ) -> Any: return self.model.generate(**_SCREAMING_SNAKE_CASE ) def _lowercase ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[Any] ) -> Union[str, Any]: return self.pre_processor.batch_decode(_SCREAMING_SNAKE_CASE , skip_special_tokens=_SCREAMING_SNAKE_CASE )[0].strip()
97
import unittest import numpy as np def lowerCamelCase__ ( _lowercase , _lowercase , _lowercase , _lowercase = None , ): '''simple docstring''' UpperCAmelCase_ : Dict = np.shape(_lowercase ) UpperCAmelCase_ : Optional[Any] = np.shape(_lowercase ) UpperCAmelCase_ : Tuple = np.shape(_lowercase ) if shape_a[0] != shape_b[0]: UpperCAmelCase_ : Tuple = ( '''Expected the same number of rows for A and B. ''' f'''Instead found A of size {shape_a} and B of size {shape_b}''' ) raise ValueError(_lowercase ) if shape_b[1] != shape_c[1]: UpperCAmelCase_ : List[Any] = ( '''Expected the same number of columns for B and C. ''' f'''Instead found B of size {shape_b} and C of size {shape_c}''' ) raise ValueError(_lowercase ) UpperCAmelCase_ : Dict = pseudo_inv if a_inv is None: try: UpperCAmelCase_ : Any = np.linalg.inv(_lowercase ) except np.linalg.LinAlgError: raise ValueError( '''Input matrix A is not invertible. Cannot compute Schur complement.''' ) return mat_c - mat_b.T @ a_inv @ mat_b class __a( unittest.TestCase ): """simple docstring""" def a__ ( self ) -> None: UpperCAmelCase_ : str = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase_ : Any = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase_ : List[str] = np.array([[2, 1], [6, 3]] ) UpperCAmelCase_ : Tuple = schur_complement(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = np.block([[a, b], [b.T, c]] ) UpperCAmelCase_ : List[Any] = np.linalg.det(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = np.linalg.det(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Any = np.linalg.det(_SCREAMING_SNAKE_CASE ) self.assertAlmostEqual(_SCREAMING_SNAKE_CASE ,det_a * det_s ) def a__ ( self ) -> None: UpperCAmelCase_ : str = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase_ : Optional[int] = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase_ : Optional[int] = np.array([[2, 1], [6, 3]] ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): schur_complement(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) def a__ ( self ) -> None: UpperCAmelCase_ : Optional[int] = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase_ : Optional[Any] = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase_ : int = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): schur_complement(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
30
0
'''simple docstring''' from __future__ import annotations SCREAMING_SNAKE_CASE = """Muhammad Umer Farooq""" SCREAMING_SNAKE_CASE = """MIT""" SCREAMING_SNAKE_CASE = """1.0.0""" SCREAMING_SNAKE_CASE = """Muhammad Umer Farooq""" SCREAMING_SNAKE_CASE = """[email protected]""" SCREAMING_SNAKE_CASE = """Alpha""" import re from html.parser import HTMLParser from urllib import parse import requests class UpperCAmelCase_ ( _a ): '''simple docstring''' def __init__( self : str , snake_case__ : Tuple ): '''simple docstring''' super().__init__() UpperCAmelCase__ : list[str] = [] UpperCAmelCase__ : Optional[Any] = domain def UpperCamelCase ( self : List[Any] , snake_case__ : Optional[int] , snake_case__ : List[str] ): '''simple docstring''' if tag == "a": # Check the list of defined attributes. for name, value in attrs: # If href is defined, and not empty nor # print it. if name == "href" and value != "#" and value != "": # If not already in urls. if value not in self.urls: UpperCAmelCase__ : Any = parse.urljoin(self.domain , _SCREAMING_SNAKE_CASE ) self.urls.append(_SCREAMING_SNAKE_CASE ) def snake_case_ ( lowercase__ ): return ".".join(get_sub_domain_name(_lowercase ).split("." )[-2:] ) def snake_case_ ( lowercase__ ): return parse.urlparse(_lowercase ).netloc def snake_case_ ( lowercase__ = "https://github.com" ): UpperCAmelCase__ : Optional[Any] = get_domain_name(_lowercase ) # Initialize the parser UpperCAmelCase__ : Tuple = Parser(_lowercase ) try: # Open URL UpperCAmelCase__ : List[Any] = requests.get(_lowercase ) # pass the raw HTML to the parser to get links parser.feed(r.text ) # Get links and loop through UpperCAmelCase__ : Optional[Any] = set() for link in parser.urls: # open URL. # read = requests.get(link) try: UpperCAmelCase__ : Union[str, Any] = requests.get(_lowercase ) # Get the valid email. UpperCAmelCase__ : Any = re.findall("[a-zA-Z0-9]+@" + domain , read.text ) # If not in list then append it. for email in emails: valid_emails.add(_lowercase ) except ValueError: pass except ValueError: raise SystemExit(1 ) # Finally return a sorted list of email addresses with no duplicates. return sorted(_lowercase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE = emails_from_url("""https://github.com""") print(F'{len(emails)} emails found:') print("""\n""".join(sorted(emails)))
199
__a = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if not isinstance(_lowercase , _lowercase ): UpperCAmelCase_ : Union[str, Any] = f'''a bytes-like object is required, not \'{data.__class__.__name__}\'''' raise TypeError(_lowercase ) UpperCAmelCase_ : Any = ''''''.join(bin(_lowercase )[2:].zfill(8 ) for byte in data ) UpperCAmelCase_ : Any = len(_lowercase ) % 6 != 0 if padding_needed: # The padding that will be added later UpperCAmelCase_ : Union[str, Any] = B'''=''' * ((6 - len(_lowercase ) % 6) // 2) # Append binary_stream with arbitrary binary digits (0's by default) to make its # length a multiple of 6. binary_stream += "0" * (6 - len(_lowercase ) % 6) else: UpperCAmelCase_ : int = B'''''' # Encode every 6 binary digits to their corresponding Base64 character return ( "".join( B64_CHARSET[int(binary_stream[index : index + 6] , 2 )] for index in range(0 , len(_lowercase ) , 6 ) ).encode() + padding ) def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if not isinstance(_lowercase , _lowercase ) and not isinstance(_lowercase , _lowercase ): UpperCAmelCase_ : Tuple = ( '''argument should be a bytes-like object or ASCII string, ''' f'''not \'{encoded_data.__class__.__name__}\'''' ) raise TypeError(_lowercase ) # In case encoded_data is a bytes-like object, make sure it contains only # ASCII characters so we convert it to a string object if isinstance(_lowercase , _lowercase ): try: UpperCAmelCase_ : Any = encoded_data.decode('''utf-8''' ) except UnicodeDecodeError: raise ValueError('''base64 encoded data should only contain ASCII characters''' ) UpperCAmelCase_ : str = encoded_data.count('''=''' ) # Check if the encoded string contains non base64 characters if padding: assert all( char in B64_CHARSET for char in encoded_data[:-padding] ), "Invalid base64 character(s) found." else: assert all( char in B64_CHARSET for char in encoded_data ), "Invalid base64 character(s) found." # Check the padding assert len(_lowercase ) % 4 == 0 and padding < 3, "Incorrect padding" if padding: # Remove padding if there is one UpperCAmelCase_ : List[Any] = encoded_data[:-padding] UpperCAmelCase_ : List[Any] = ''''''.join( bin(B64_CHARSET.index(_lowercase ) )[2:].zfill(6 ) for char in encoded_data )[: -padding * 2] else: UpperCAmelCase_ : Tuple = ''''''.join( bin(B64_CHARSET.index(_lowercase ) )[2:].zfill(6 ) for char in encoded_data ) UpperCAmelCase_ : str = [ int(binary_stream[index : index + 8] , 2 ) for index in range(0 , len(_lowercase ) , 8 ) ] return bytes(_lowercase ) if __name__ == "__main__": import doctest doctest.testmod()
30
0
import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class _lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self :Optional[Any] , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :Dict=7 , lowerCAmelCase__ :Dict=3 , lowerCAmelCase__ :Dict=18 , lowerCAmelCase__ :Optional[Any]=30 , lowerCAmelCase__ :List[str]=400 , lowerCAmelCase__ :str=True , lowerCAmelCase__ :Union[str, Any]=None , lowerCAmelCase__ :List[str]=True , ) -> List[Any]: __SCREAMING_SNAKE_CASE : int = size if size is not None else {'''height''': 18, '''width''': 18} __SCREAMING_SNAKE_CASE : List[str] = parent __SCREAMING_SNAKE_CASE : Dict = batch_size __SCREAMING_SNAKE_CASE : int = num_channels __SCREAMING_SNAKE_CASE : int = image_size __SCREAMING_SNAKE_CASE : Dict = min_resolution __SCREAMING_SNAKE_CASE : Tuple = max_resolution __SCREAMING_SNAKE_CASE : Any = do_resize __SCREAMING_SNAKE_CASE : Tuple = size __SCREAMING_SNAKE_CASE : Any = apply_ocr def __magic_name__( self :Dict ) -> Dict: return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class _lowercase ( _a , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : int = LayoutLMvaImageProcessor if is_pytesseract_available() else None def __magic_name__( self :Tuple ) -> str: __SCREAMING_SNAKE_CASE : List[str] = LayoutLMvaImageProcessingTester(self ) @property def __magic_name__( self :Union[str, Any] ) -> Union[str, Any]: return self.image_processor_tester.prepare_image_processor_dict() def __magic_name__( self :str ) -> int: __SCREAMING_SNAKE_CASE : int = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , '''do_resize''' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , '''size''' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , '''apply_ocr''' ) ) def __magic_name__( self :Union[str, Any] ) -> List[str]: __SCREAMING_SNAKE_CASE : Dict = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) __SCREAMING_SNAKE_CASE : str = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def __magic_name__( self :Any ) -> Any: pass def __magic_name__( self :Tuple ) -> Tuple: # Initialize image_processing __SCREAMING_SNAKE_CASE : str = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __SCREAMING_SNAKE_CASE : List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input __SCREAMING_SNAKE_CASE : Optional[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ) self.assertEqual( encoding.pixel_values.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) self.assertIsInstance(encoding.words , _SCREAMING_SNAKE_CASE ) self.assertIsInstance(encoding.boxes , _SCREAMING_SNAKE_CASE ) # Test batched __SCREAMING_SNAKE_CASE : Union[str, Any] = image_processing(_SCREAMING_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.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def __magic_name__( self :str ) -> Dict: # Initialize image_processing __SCREAMING_SNAKE_CASE : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __SCREAMING_SNAKE_CASE : Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=_SCREAMING_SNAKE_CASE , numpify=_SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE , np.ndarray ) # Test not batched input __SCREAMING_SNAKE_CASE : 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.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __SCREAMING_SNAKE_CASE : str = image_processing(_SCREAMING_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.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def __magic_name__( self :Optional[int] ) -> List[Any]: # Initialize image_processing __SCREAMING_SNAKE_CASE : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __SCREAMING_SNAKE_CASE : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_SCREAMING_SNAKE_CASE , torchify=_SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE , torch.Tensor ) # Test not batched input __SCREAMING_SNAKE_CASE : 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.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __SCREAMING_SNAKE_CASE : Optional[int] = image_processing(_SCREAMING_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.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def __magic_name__( self :Optional[Any] ) -> int: # with apply_OCR = True __SCREAMING_SNAKE_CASE : Optional[Any] = LayoutLMvaImageProcessor() from datasets import load_dataset __SCREAMING_SNAKE_CASE : str = load_dataset('''hf-internal-testing/fixtures_docvqa''' , split='''test''' ) __SCREAMING_SNAKE_CASE : Tuple = Image.open(ds[0]['''file'''] ).convert('''RGB''' ) __SCREAMING_SNAKE_CASE : Tuple = image_processing(_SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) , len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 __SCREAMING_SNAKE_CASE : List[str] = [['''11:14''', '''to''', '''11:39''', '''a.m''', '''11:39''', '''to''', '''11:44''', '''a.m.''', '''11:44''', '''a.m.''', '''to''', '''12:25''', '''p.m.''', '''12:25''', '''to''', '''12:58''', '''p.m.''', '''12:58''', '''to''', '''4:00''', '''p.m.''', '''2:00''', '''to''', '''5:00''', '''p.m.''', '''Coffee''', '''Break''', '''Coffee''', '''will''', '''be''', '''served''', '''for''', '''men''', '''and''', '''women''', '''in''', '''the''', '''lobby''', '''adjacent''', '''to''', '''exhibit''', '''area.''', '''Please''', '''move''', '''into''', '''exhibit''', '''area.''', '''(Exhibits''', '''Open)''', '''TRRF''', '''GENERAL''', '''SESSION''', '''(PART''', '''|)''', '''Presiding:''', '''Lee''', '''A.''', '''Waller''', '''TRRF''', '''Vice''', '''President''', '''“Introductory''', '''Remarks”''', '''Lee''', '''A.''', '''Waller,''', '''TRRF''', '''Vice''', '''Presi-''', '''dent''', '''Individual''', '''Interviews''', '''with''', '''TRRF''', '''Public''', '''Board''', '''Members''', '''and''', '''Sci-''', '''entific''', '''Advisory''', '''Council''', '''Mem-''', '''bers''', '''Conducted''', '''by''', '''TRRF''', '''Treasurer''', '''Philip''', '''G.''', '''Kuehn''', '''to''', '''get''', '''answers''', '''which''', '''the''', '''public''', '''refrigerated''', '''warehousing''', '''industry''', '''is''', '''looking''', '''for.''', '''Plus''', '''questions''', '''from''', '''the''', '''floor.''', '''Dr.''', '''Emil''', '''M.''', '''Mrak,''', '''University''', '''of''', '''Cal-''', '''ifornia,''', '''Chairman,''', '''TRRF''', '''Board;''', '''Sam''', '''R.''', '''Cecil,''', '''University''', '''of''', '''Georgia''', '''College''', '''of''', '''Agriculture;''', '''Dr.''', '''Stanley''', '''Charm,''', '''Tufts''', '''University''', '''School''', '''of''', '''Medicine;''', '''Dr.''', '''Robert''', '''H.''', '''Cotton,''', '''ITT''', '''Continental''', '''Baking''', '''Company;''', '''Dr.''', '''Owen''', '''Fennema,''', '''University''', '''of''', '''Wis-''', '''consin;''', '''Dr.''', '''Robert''', '''E.''', '''Hardenburg,''', '''USDA.''', '''Questions''', '''and''', '''Answers''', '''Exhibits''', '''Open''', '''Capt.''', '''Jack''', '''Stoney''', '''Room''', '''TRRF''', '''Scientific''', '''Advisory''', '''Council''', '''Meeting''', '''Ballroom''', '''Foyer''']] # noqa: E231 __SCREAMING_SNAKE_CASE : Optional[int] = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words , _SCREAMING_SNAKE_CASE ) self.assertListEqual(encoding.boxes , _SCREAMING_SNAKE_CASE ) # with apply_OCR = False __SCREAMING_SNAKE_CASE : Union[str, Any] = LayoutLMvaImageProcessor(apply_ocr=_SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE : Tuple = image_processing(_SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) )
696
import os import shutil import sys import tempfile import unittest from pathlib import Path import pytest import transformers from transformers import ( BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoTokenizer, BertConfig, BertTokenizer, BertTokenizerFast, CTRLTokenizer, GPTaTokenizer, GPTaTokenizerFast, PreTrainedTokenizerFast, RobertaTokenizer, RobertaTokenizerFast, is_tokenizers_available, ) from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig from transformers.models.auto.tokenization_auto import ( TOKENIZER_MAPPING, get_tokenizer_config, tokenizer_class_from_name, ) from transformers.models.roberta.configuration_roberta import RobertaConfig from transformers.testing_utils import ( DUMMY_DIFF_TOKENIZER_IDENTIFIER, DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tokenizers, slow, ) sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 if is_tokenizers_available(): from test_module.custom_tokenization_fast import CustomTokenizerFast class __a( unittest.TestCase ): """simple docstring""" def a__ ( self ) -> Union[str, Any]: UpperCAmelCase_ : Tuple = 0 @slow def a__ ( self ) -> Any: for model_name in (x for x in BERT_PRETRAINED_CONFIG_ARCHIVE_MAP.keys() if "japanese" not in x): UpperCAmelCase_ : Optional[Any] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(BertTokenizer, BertTokenizerFast) ) self.assertGreater(len(_SCREAMING_SNAKE_CASE ) ,0 ) for model_name in GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP.keys(): UpperCAmelCase_ : Union[str, Any] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(GPTaTokenizer, GPTaTokenizerFast) ) self.assertGreater(len(_SCREAMING_SNAKE_CASE ) ,0 ) def a__ ( self ) -> Optional[Any]: UpperCAmelCase_ : List[str] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size ,12 ) def a__ ( self ) -> Tuple: UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(RobertaTokenizer, RobertaTokenizerFast) ) self.assertEqual(tokenizer.vocab_size ,20 ) def a__ ( self ) -> List[str]: UpperCAmelCase_ : int = AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) # Check that tokenizer_type ≠ model_type UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,config=_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size ,12 ) def a__ ( self ) -> Dict: with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''vocab.txt''' ) ) UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,tokenizer_type='''bert''' ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''merges.txt''' ) ) UpperCAmelCase_ : Optional[Any] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,tokenizer_type='''gpt2''' ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) @require_tokenizers def a__ ( self ) -> Optional[int]: with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''vocab.txt''' ) ) UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,tokenizer_type='''bert''' ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''merges.txt''' ) ) UpperCAmelCase_ : Union[str, Any] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,tokenizer_type='''gpt2''' ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) def a__ ( self ) -> int: with pytest.raises(_SCREAMING_SNAKE_CASE ): AutoTokenizer.from_pretrained('''./''' ,tokenizer_type='''xxx''' ) @require_tokenizers def a__ ( self ) -> Optional[Any]: for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: UpperCAmelCase_ : Any = tokenizer_class.from_pretrained('''wietsedv/bert-base-dutch-cased''' ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(BertTokenizer, BertTokenizerFast) ) if isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ): self.assertEqual(tokenizer.basic_tokenizer.do_lower_case ,_SCREAMING_SNAKE_CASE ) else: self.assertEqual(tokenizer.do_lower_case ,_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.model_max_length ,512 ) @require_tokenizers def a__ ( self ) -> List[Any]: for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: with self.assertRaisesRegex( _SCREAMING_SNAKE_CASE ,'''julien-c/herlolip-not-exists is not a local folder and is not a valid model identifier''' ,): UpperCAmelCase_ : int = tokenizer_class.from_pretrained('''julien-c/herlolip-not-exists''' ) def a__ ( self ) -> Optional[Any]: # tests: https://github.com/huggingface/transformers/pull/13251 # 1. models with `-`, e.g. xlm-roberta -> xlm_roberta # 2. models that don't remap 1-1 from model-name to model file, e.g., openai-gpt -> openai UpperCAmelCase_ : int = TOKENIZER_MAPPING.values() UpperCAmelCase_ : List[Any] = [] for slow_tok, fast_tok in tokenizers: if slow_tok is not None: tokenizer_names.append(slow_tok.__name__ ) if fast_tok is not None: tokenizer_names.append(fast_tok.__name__ ) for tokenizer_name in tokenizer_names: # must find the right class tokenizer_class_from_name(_SCREAMING_SNAKE_CASE ) @require_tokenizers def a__ ( self ) -> Tuple: self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' ,use_fast=_SCREAMING_SNAKE_CASE ) ,_SCREAMING_SNAKE_CASE ) self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' ) ,_SCREAMING_SNAKE_CASE ) @require_tokenizers def a__ ( self ) -> Optional[int]: UpperCAmelCase_ : str = AutoTokenizer.from_pretrained('''distilbert-base-uncased''' ,do_lower_case=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = '''Hello, world. How are you?''' UpperCAmelCase_ : List[Any] = tokenizer.tokenize(_SCREAMING_SNAKE_CASE ) self.assertEqual('''[UNK]''' ,tokens[0] ) UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained('''microsoft/mpnet-base''' ,do_lower_case=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = tokenizer.tokenize(_SCREAMING_SNAKE_CASE ) self.assertEqual('''[UNK]''' ,tokens[0] ) @require_tokenizers def a__ ( self ) -> Dict: UpperCAmelCase_ : List[Any] = AutoTokenizer.from_pretrained('''robot-test/dummy-tokenizer-fast-with-model-config''' ) self.assertEqual(type(_SCREAMING_SNAKE_CASE ) ,_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.model_max_length ,512 ) self.assertEqual(tokenizer.vocab_size ,30_000 ) self.assertEqual(tokenizer.unk_token ,'''[UNK]''' ) self.assertEqual(tokenizer.padding_side ,'''right''' ) self.assertEqual(tokenizer.truncation_side ,'''right''' ) def a__ ( self ) -> Dict: UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(BertTokenizer, BertTokenizerFast) ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,tokenizer.__class__ ) self.assertEqual(tokenizera.vocab_size ,12 ) def a__ ( self ) -> Optional[Any]: UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained('''ctrl''' ) # There is no fast CTRL so this always gives us a slow tokenizer. self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) def a__ ( self ) -> str: # Check we can load the tokenizer config of an online model. UpperCAmelCase_ : int = get_tokenizer_config('''bert-base-cased''' ) UpperCAmelCase_ : Optional[int] = config.pop('''_commit_hash''' ,_SCREAMING_SNAKE_CASE ) # If we ever update bert-base-cased tokenizer config, this dict here will need to be updated. self.assertEqual(_SCREAMING_SNAKE_CASE ,{'''do_lower_case''': False} ) # This model does not have a tokenizer_config so we get back an empty dict. UpperCAmelCase_ : Any = get_tokenizer_config(_SCREAMING_SNAKE_CASE ) self.assertDictEqual(_SCREAMING_SNAKE_CASE ,{} ) # A tokenizer saved with `save_pretrained` always creates a tokenizer config. UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = get_tokenizer_config(_SCREAMING_SNAKE_CASE ) # Check the class of the tokenizer was properly saved (note that it always saves the slow class). self.assertEqual(config['''tokenizer_class'''] ,'''BertTokenizer''' ) def a__ ( self ) -> str: try: AutoConfig.register('''custom''' ,_SCREAMING_SNAKE_CASE ) AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,slow_tokenizer_class=_SCREAMING_SNAKE_CASE ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_SCREAMING_SNAKE_CASE ): AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,slow_tokenizer_class=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = CustomTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] @require_tokenizers def a__ ( self ) -> int: try: AutoConfig.register('''custom''' ,_SCREAMING_SNAKE_CASE ) # Can register in two steps AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,slow_tokenizer_class=_SCREAMING_SNAKE_CASE ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] ,(CustomTokenizer, None) ) AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,fast_tokenizer_class=_SCREAMING_SNAKE_CASE ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] ,(CustomTokenizer, CustomTokenizerFast) ) del TOKENIZER_MAPPING._extra_content[CustomConfig] # Can register in one step AutoTokenizer.register( _SCREAMING_SNAKE_CASE ,slow_tokenizer_class=_SCREAMING_SNAKE_CASE ,fast_tokenizer_class=_SCREAMING_SNAKE_CASE ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] ,(CustomTokenizer, CustomTokenizerFast) ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_SCREAMING_SNAKE_CASE ): AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,fast_tokenizer_class=_SCREAMING_SNAKE_CASE ) # We pass through a bert tokenizer fast cause there is no converter slow to fast for our new toknizer # and that model does not have a tokenizer.json with tempfile.TemporaryDirectory() as tmp_dir: UpperCAmelCase_ : List[str] = BertTokenizerFast.from_pretrained(_SCREAMING_SNAKE_CASE ) bert_tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = CustomTokenizerFast.from_pretrained(_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def a__ ( self ) -> Optional[int]: # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Union[str, Any] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) # Test we can also load the slow version UpperCAmelCase_ : List[str] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,trust_remote_code=_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertEqual(reloaded_tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) else: self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ ,'''NewTokenizer''' ) @require_tokenizers def a__ ( self ) -> Optional[int]: class __a( _a ): """simple docstring""" lowerCAmelCase = False class __a( _a ): """simple docstring""" lowerCAmelCase = NewTokenizer lowerCAmelCase = False try: AutoConfig.register('''custom''' ,_SCREAMING_SNAKE_CASE ) AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,slow_tokenizer_class=_SCREAMING_SNAKE_CASE ) AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,fast_tokenizer_class=_SCREAMING_SNAKE_CASE ) # If remote code is not set, the default is to use local UpperCAmelCase_ : int = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote code is disabled, we load the local one. UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) UpperCAmelCase_ : List[str] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertTrue(tokenizer.special_attribute_present ) UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertTrue(tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def a__ ( self ) -> int: UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) # Test we can also load the slow version UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) else: self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) def a__ ( self ) -> Optional[Any]: with self.assertRaisesRegex( _SCREAMING_SNAKE_CASE ,'''bert-base is not a local folder and is not a valid model identifier''' ): UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained('''bert-base''' ) def a__ ( self ) -> List[Any]: with self.assertRaisesRegex( _SCREAMING_SNAKE_CASE ,R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): UpperCAmelCase_ : str = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,revision='''aaaaaa''' ) def a__ ( self ) -> Any: # Make sure we have cached the tokenizer. UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) with RequestCounter() as counter: UpperCAmelCase_ : List[str] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) self.assertEqual(counter.get_request_count ,0 ) self.assertEqual(counter.head_request_count ,1 ) self.assertEqual(counter.other_request_count ,0 )
30
0
from dataclasses import dataclass, field from typing import Tuple from ..utils import cached_property, is_tf_available, logging, requires_backends from .benchmark_args_utils import BenchmarkArguments if is_tf_available(): import tensorflow as tf _SCREAMING_SNAKE_CASE : List[str] = logging.get_logger(__name__) @dataclass class A__ ( _a ): """simple docstring""" __magic_name__ = [ 'no_inference', 'no_cuda', 'no_tpu', 'no_speed', 'no_memory', 'no_env_print', 'no_multi_process', ] def __init__( self , **__snake_case ): for deprecated_arg in self.deprecated_args: if deprecated_arg in kwargs: snake_case = deprecated_arg[3:] snake_case = not kwargs.pop(_SCREAMING_SNAKE_CASE ) logger.warning( F'''{deprecated_arg} is depreciated. Please use --no-{positive_arg} or''' F''' {positive_arg}={kwargs[positive_arg]}''' ) snake_case = kwargs.pop('''tpu_name''' , self.tpu_name ) snake_case = kwargs.pop('''device_idx''' , self.device_idx ) snake_case = kwargs.pop('''eager_mode''' , self.eager_mode ) snake_case = kwargs.pop('''use_xla''' , self.use_xla ) super().__init__(**_SCREAMING_SNAKE_CASE ) __magic_name__ = field( default=_a , metadata={'help': 'Name of TPU'} , ) __magic_name__ = field( default=0 , metadata={'help': 'CPU / GPU device index. Defaults to 0.'} , ) __magic_name__ = field(default=_a , metadata={'help': 'Benchmark models in eager model.'} ) __magic_name__ = field( default=_a , metadata={ 'help': 'Benchmark models using XLA JIT compilation. Note that `eager_model` has to be set to `False`.' } , ) @cached_property def a_ ( self ): requires_backends(self , ['''tf'''] ) snake_case = None if self.tpu: try: if self.tpu_name: snake_case = tf.distribute.cluster_resolver.TPUClusterResolver(self.tpu_name ) else: snake_case = tf.distribute.cluster_resolver.TPUClusterResolver() except ValueError: snake_case = None return tpu @cached_property def a_ ( self ): requires_backends(self , ['''tf'''] ) if self.is_tpu: tf.config.experimental_connect_to_cluster(self._setup_tpu ) tf.tpu.experimental.initialize_tpu_system(self._setup_tpu ) snake_case = tf.distribute.TPUStrategy(self._setup_tpu ) else: # currently no multi gpu is allowed if self.is_gpu: # TODO: Currently only single GPU is supported tf.config.set_visible_devices(self.gpu_list[self.device_idx] , '''GPU''' ) snake_case = tf.distribute.OneDeviceStrategy(device=F'''/gpu:{self.device_idx}''' ) else: tf.config.set_visible_devices([] , '''GPU''' ) # disable GPU snake_case = tf.distribute.OneDeviceStrategy(device=F'''/cpu:{self.device_idx}''' ) return strategy @property def a_ ( self ): requires_backends(self , ['''tf'''] ) return self._setup_tpu is not None @property def a_ ( self ): requires_backends(self , ['''tf'''] ) return self._setup_strategy @property def a_ ( self ): requires_backends(self , ['''tf'''] ) return tf.config.list_physical_devices('''GPU''' ) @property def a_ ( self ): requires_backends(self , ['''tf'''] ) if self.cuda: return len(self.gpu_list ) return 0 @property def a_ ( self ): return self.n_gpu > 0
550
from functools import reduce __a = ( '73167176531330624919225119674426574742355349194934' '96983520312774506326239578318016984801869478851843' '85861560789112949495459501737958331952853208805511' '12540698747158523863050715693290963295227443043557' '66896648950445244523161731856403098711121722383113' '62229893423380308135336276614282806444486645238749' '30358907296290491560440772390713810515859307960866' '70172427121883998797908792274921901699720888093776' '65727333001053367881220235421809751254540594752243' '52584907711670556013604839586446706324415722155397' '53697817977846174064955149290862569321978468622482' '83972241375657056057490261407972968652414535100474' '82166370484403199890008895243450658541227588666881' '16427171479924442928230863465674813919123162824586' '17866458359124566529476545682848912883142607690042' '24219022671055626321111109370544217506941658960408' '07198403850962455444362981230987879927244284909188' '84580156166097919133875499200524063689912560717606' '05886116467109405077541002256983155200055935729725' '71636269561882670428252483600823257530420752963450' ) def lowerCamelCase__ ( _lowercase = N ): '''simple docstring''' return max( # mypy cannot properly interpret reduce int(reduce(lambda _lowercase , _lowercase : str(int(_lowercase ) * int(_lowercase ) ) , n[i : i + 13] ) ) for i in range(len(_lowercase ) - 12 ) ) if __name__ == "__main__": print(F"""{solution() = }""")
30
0
'''simple docstring''' import warnings from ...utils import logging from .image_processing_deformable_detr import DeformableDetrImageProcessor A_ = logging.get_logger(__name__) class lowercase_ ( _a ): def __init__( self : Union[str, Any] , *__lowerCamelCase : Optional[Any] , **__lowerCamelCase : Union[str, Any] ): warnings.warn( 'The class DeformableDetrFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use DeformableDetrImageProcessor instead.' , _SCREAMING_SNAKE_CASE , ) super().__init__(*_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE )
270
from decimal import Decimal, getcontext from math import ceil, factorial def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if not isinstance(_lowercase , _lowercase ): raise TypeError('''Undefined for non-integers''' ) elif precision < 1: raise ValueError('''Undefined for non-natural numbers''' ) UpperCAmelCase_ : Tuple = precision UpperCAmelCase_ : Optional[Any] = ceil(precision / 14 ) UpperCAmelCase_ : int = 426880 * Decimal(10005 ).sqrt() UpperCAmelCase_ : Tuple = 1 UpperCAmelCase_ : List[Any] = 13591409 UpperCAmelCase_ : Optional[Any] = Decimal(_lowercase ) for k in range(1 , _lowercase ): UpperCAmelCase_ : List[str] = factorial(6 * k ) // (factorial(3 * k ) * factorial(_lowercase ) ** 3) linear_term += 545140134 exponential_term *= -262537412640768000 partial_sum += Decimal(multinomial_term * linear_term ) / exponential_term return str(constant_term / partial_sum )[:-1] if __name__ == "__main__": __a = 50 print(F"""The first {n} digits of pi is: {pi(n)}""")
30
0
'''simple docstring''' import json import os import tempfile import unittest import numpy as np from datasets import load_dataset from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ImageGPTImageProcessor class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __init__( self , lowerCamelCase , lowerCamelCase=7 , lowerCamelCase=3 , lowerCamelCase=18 , lowerCamelCase=30 , lowerCamelCase=4_00 , lowerCamelCase=True , lowerCamelCase=None , lowerCamelCase=True , ) -> Dict: '''simple docstring''' UpperCamelCase : Optional[int] = size if size is not None else {'''height''': 18, '''width''': 18} UpperCamelCase : List[str] = parent UpperCamelCase : int = batch_size UpperCamelCase : Dict = num_channels UpperCamelCase : Tuple = image_size UpperCamelCase : List[str] = min_resolution UpperCamelCase : Any = max_resolution UpperCamelCase : Optional[int] = do_resize UpperCamelCase : Any = size UpperCamelCase : str = do_normalize def SCREAMING_SNAKE_CASE__ ( self ) -> Optional[int]: '''simple docstring''' return { # here we create 2 clusters for the sake of simplicity "clusters": np.asarray( [ [0.8866443634033203, 0.6618829369544983, 0.3891746401786804], [-0.6042559146881104, -0.02295008860528469, 0.5423797369003296], ] ), "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, } @require_torch @require_vision class UpperCAmelCase_ ( _a , unittest.TestCase ): """simple docstring""" __SCREAMING_SNAKE_CASE = ImageGPTImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE__ ( self ) -> int: '''simple docstring''' UpperCamelCase : Dict = ImageGPTImageProcessingTester(self ) @property def SCREAMING_SNAKE_CASE__ ( self ) -> Optional[int]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE__ ( self ) -> Any: '''simple docstring''' UpperCamelCase : Tuple = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "clusters" ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "do_resize" ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "size" ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "do_normalize" ) ) def SCREAMING_SNAKE_CASE__ ( self ) -> List[Any]: '''simple docstring''' UpperCamelCase : Tuple = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"height": 18, "width": 18} ) UpperCamelCase : Any = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"height": 42, "width": 42} ) def SCREAMING_SNAKE_CASE__ ( self ) -> int: '''simple docstring''' UpperCamelCase : List[Any] = self.image_processing_class(**self.image_processor_dict ) UpperCamelCase : Tuple = json.loads(image_processor.to_json_string() ) for key, value in self.image_processor_dict.items(): if key == "clusters": self.assertTrue(np.array_equal(_SCREAMING_SNAKE_CASE , obj[key] ) ) else: self.assertEqual(obj[key] , _SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE__ ( self ) -> Dict: '''simple docstring''' UpperCamelCase : Dict = self.image_processing_class(**self.image_processor_dict ) with tempfile.TemporaryDirectory() as tmpdirname: UpperCamelCase : Any = os.path.join(_SCREAMING_SNAKE_CASE , "image_processor.json" ) image_processor_first.to_json_file(_SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = self.image_processing_class.from_json_file(_SCREAMING_SNAKE_CASE ).to_dict() UpperCamelCase : int = image_processor_first.to_dict() for key, value in image_processor_first.items(): if key == "clusters": self.assertTrue(np.array_equal(_SCREAMING_SNAKE_CASE , image_processor_second[key] ) ) else: self.assertEqual(image_processor_first[key] , _SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE__ ( self ) -> Any: '''simple docstring''' UpperCamelCase : str = self.image_processing_class(**self.image_processor_dict ) with tempfile.TemporaryDirectory() as tmpdirname: image_processor_first.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = self.image_processing_class.from_pretrained(_SCREAMING_SNAKE_CASE ).to_dict() UpperCamelCase : Dict = image_processor_first.to_dict() for key, value in image_processor_first.items(): if key == "clusters": self.assertTrue(np.array_equal(_SCREAMING_SNAKE_CASE , image_processor_second[key] ) ) else: self.assertEqual(image_processor_first[key] , _SCREAMING_SNAKE_CASE ) @unittest.skip("ImageGPT requires clusters at initialization" ) def SCREAMING_SNAKE_CASE__ ( self ) -> str: '''simple docstring''' pass def A__ ( ): '''simple docstring''' UpperCamelCase : int = load_dataset("hf-internal-testing/fixtures_image_utils" , split="test") UpperCamelCase : Optional[int] = Image.open(dataset[4]["file"]) UpperCamelCase : Any = Image.open(dataset[5]["file"]) UpperCamelCase : Union[str, Any] = [imagea, imagea] return images @require_vision @require_torch class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE__ ( self ) -> List[str]: '''simple docstring''' UpperCamelCase : int = ImageGPTImageProcessor.from_pretrained("openai/imagegpt-small" ) UpperCamelCase : Any = prepare_images() # test non-batched UpperCamelCase : Tuple = image_processing(images[0] , return_tensors="pt" ) self.assertIsInstance(encoding.input_ids , torch.LongTensor ) self.assertEqual(encoding.input_ids.shape , (1, 10_24) ) UpperCamelCase : str = [3_06, 1_91, 1_91] self.assertEqual(encoding.input_ids[0, :3].tolist() , _SCREAMING_SNAKE_CASE ) # test batched UpperCamelCase : List[str] = image_processing(_SCREAMING_SNAKE_CASE , return_tensors="pt" ) self.assertIsInstance(encoding.input_ids , torch.LongTensor ) self.assertEqual(encoding.input_ids.shape , (2, 10_24) ) UpperCamelCase : List[str] = [3_03, 13, 13] self.assertEqual(encoding.input_ids[1, -3:].tolist() , _SCREAMING_SNAKE_CASE )
173
from __future__ import annotations import math __a = '2020.9.26' __a = 'xcodz-dot, cclaus, dhruvmanila' def lowerCamelCase__ ( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ): '''simple docstring''' if not all(isinstance(_lowercase , (float, int) ) for val in locals().values() ): UpperCAmelCase_ : Optional[int] = f'''Input values must either be float or int: {list(locals().values() )}''' raise TypeError(_lowercase ) UpperCAmelCase_ : Tuple = ((x * distance) / (z + distance)) * scale UpperCAmelCase_ : str = ((y * distance) / (z + distance)) * scale return projected_x, projected_y def lowerCamelCase__ ( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ): '''simple docstring''' if not isinstance(_lowercase , _lowercase ): raise TypeError('''Axis must be a str''' ) UpperCAmelCase_ : Optional[Any] = locals() del input_variables["axis"] if not all(isinstance(_lowercase , (float, int) ) for val in input_variables.values() ): UpperCAmelCase_ : List[Any] = ( '''Input values except axis must either be float or int: ''' f'''{list(input_variables.values() )}''' ) raise TypeError(_lowercase ) UpperCAmelCase_ : Dict = (angle % 360) / 450 * 180 / math.pi if axis == "z": UpperCAmelCase_ : Optional[int] = x * math.cos(_lowercase ) - y * math.sin(_lowercase ) UpperCAmelCase_ : List[Any] = y * math.cos(_lowercase ) + x * math.sin(_lowercase ) UpperCAmelCase_ : Optional[int] = z elif axis == "x": UpperCAmelCase_ : Any = y * math.cos(_lowercase ) - z * math.sin(_lowercase ) UpperCAmelCase_ : int = z * math.cos(_lowercase ) + y * math.sin(_lowercase ) UpperCAmelCase_ : Dict = x elif axis == "y": UpperCAmelCase_ : Union[str, Any] = x * math.cos(_lowercase ) - z * math.sin(_lowercase ) UpperCAmelCase_ : Optional[int] = z * math.cos(_lowercase ) + x * math.sin(_lowercase ) UpperCAmelCase_ : Optional[int] = y else: raise ValueError('''not a valid axis, choose one of \'x\', \'y\', \'z\'''' ) return new_x, new_y, new_z if __name__ == "__main__": import doctest doctest.testmod() print(F"""{convert_to_ad(1.0, 2.0, 3.0, 10.0, 10.0) = }""") print(F"""{rotate(1.0, 2.0, 3.0, "y", 90.0) = }""")
30
0
# 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.auto import AutoModelForSeqaSeqLM, AutoTokenizer from .base import PipelineTool class lowercase_ (_a ): lowerCAmelCase__ ="philschmid/bart-large-cnn-samsum" lowerCAmelCase__ =( "This is a tool that summarizes an English text. It takes an input `text` containing the text to summarize, " "and returns a summary of the text." ) lowerCAmelCase__ ="summarizer" lowerCAmelCase__ =AutoTokenizer lowerCAmelCase__ =AutoModelForSeqaSeqLM lowerCAmelCase__ =["text"] lowerCAmelCase__ =["text"] def __a ( self : str , snake_case__ : str ): """simple docstring""" return self.pre_processor(_SCREAMING_SNAKE_CASE , return_tensors='pt' , truncation=_SCREAMING_SNAKE_CASE ) def __a ( self : str , snake_case__ : List[Any] ): """simple docstring""" return self.model.generate(**_SCREAMING_SNAKE_CASE )[0] def __a ( self : Dict , snake_case__ : str ): """simple docstring""" return self.pre_processor.decode(_SCREAMING_SNAKE_CASE , skip_special_tokens=_SCREAMING_SNAKE_CASE , clean_up_tokenization_spaces=_SCREAMING_SNAKE_CASE )
360
# Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position __a = '2.13.1' import platform import pyarrow from packaging import version if version.parse(platform.python_version()) < version.parse('3.7'): raise ImportWarning( 'To use `datasets`, Python>=3.7 is required, and the current version of Python doesn\'t match this condition.' ) if version.parse(pyarrow.__version__).major < 8: raise ImportWarning( 'To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn\'t match this condition.\n' 'If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`.' ) del platform del pyarrow del version from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip __a = concatenate_datasets __a = DownloadConfig __a = DownloadManager __a = DownloadMode __a = DownloadConfig __a = DownloadMode __a = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
30
0
"""simple docstring""" def A__ ( A__ , A__ = " " ) -> Union[str, Any]: '''simple docstring''' _UpperCAmelCase = [] _UpperCAmelCase = 0 for index, char in enumerate(_lowercase ): if char == separator: split_words.append(string[last_index:index] ) _UpperCAmelCase = index + 1 elif index + 1 == len(_lowercase ): split_words.append(string[last_index : index + 1] ) return split_words if __name__ == "__main__": from doctest import testmod testmod()
426
def lowerCamelCase__ ( _lowercase , _lowercase ): '''simple docstring''' while a != 0: UpperCAmelCase_, UpperCAmelCase_ : Optional[int] = b % a, a return b def lowerCamelCase__ ( _lowercase , _lowercase ): '''simple docstring''' if gcd(_lowercase , _lowercase ) != 1: UpperCAmelCase_ : int = f'''mod inverse of {a!r} and {m!r} does not exist''' raise ValueError(_lowercase ) UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_ : Union[str, Any] = 1, 0, a UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_ : Dict = 0, 1, m while va != 0: UpperCAmelCase_ : List[Any] = ua // va UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_ : Any = (ua - q * va), (ua - q * va), (ua - q * va), va, va, va return ua % m
30
0
'''simple docstring''' import argparse from copy import deepcopy import numpy as np from datasets import ClassLabel, DatasetDict, load_dataset from evaluate import load from transformers import ( AutoModelForSequenceClassification, AutoTokenizer, DataCollatorWithPadding, Trainer, TrainerCallback, TrainingArguments, set_seed, ) def lowerCamelCase (): __a : List[Any] = argparse.ArgumentParser() parser.add_argument('--model_ckpt' , type=_lowercase , default='microsoft/unixcoder-base-nine' ) parser.add_argument('--num_epochs' , type=_lowercase , default=5 ) parser.add_argument('--batch_size' , type=_lowercase , default=6 ) parser.add_argument('--gradient_accumulation_steps' , type=_lowercase , default=1 ) parser.add_argument('--freeze' , type=_lowercase , default=_lowercase ) parser.add_argument('--learning_rate' , type=_lowercase , default=5e-4 ) parser.add_argument('--seed' , type=_lowercase , default=0 ) parser.add_argument('--lr_scheduler_type' , type=_lowercase , default='cosine' ) parser.add_argument('--num_warmup_steps' , type=_lowercase , default=10 ) parser.add_argument('--weight_decay' , type=_lowercase , default=0.0_1 ) parser.add_argument('--output_dir' , type=_lowercase , default='./results' ) return parser.parse_args() __lowercase : Optional[Any] = load('accuracy') def lowerCamelCase (_SCREAMING_SNAKE_CASE : Optional[int] ): __a : List[str] = eval_pred __a : str = np.argmax(_lowercase , axis=1 ) return metric.compute(predictions=_lowercase , references=_lowercase ) class __UpperCamelCase ( _a ): def __init__( self , __a ): '''simple docstring''' super().__init__() __a : List[Any] = trainer def __UpperCAmelCase ( self , __a , __a , __a , **__a ): '''simple docstring''' if control.should_evaluate: __a : Union[str, Any] = deepcopy(_SCREAMING_SNAKE_CASE ) self._trainer.evaluate(eval_dataset=self._trainer.train_dataset , metric_key_prefix='train' ) return control_copy def lowerCamelCase (): __a : str = get_args() set_seed(args.seed ) __a : Optional[int] = load_dataset('codeparrot/codecomplex' , split='train' ) __a : Optional[int] = dataset.train_test_split(test_size=0.2 ) __a : Dict = train_test['''test'''].train_test_split(test_size=0.5 ) __a : Tuple = DatasetDict( { 'train': train_test['train'], 'test': test_validation['train'], 'valid': test_validation['test'], } ) print('Loading tokenizer and model' ) __a : int = AutoTokenizer.from_pretrained(args.model_ckpt ) __a : str = tokenizer.eos_token __a : List[Any] = AutoModelForSequenceClassification.from_pretrained(args.model_ckpt , num_labels=7 ) __a : str = model.config.eos_token_id if args.freeze: for param in model.roberta.parameters(): __a : Optional[int] = False __a : List[Any] = ClassLabel(num_classes=7 , names=list(set(train_test_validation['train']['complexity'] ) ) ) def tokenize(_SCREAMING_SNAKE_CASE : List[str] ): __a : Dict = tokenizer(example['src'] , truncation=_lowercase , max_length=1_024 ) __a : Tuple = labels.straint(example['complexity'] ) return { "input_ids": inputs["input_ids"], "attention_mask": inputs["attention_mask"], "label": label, } __a : Dict = train_test_validation.map( _lowercase , batched=_lowercase , remove_columns=train_test_validation['train'].column_names , ) __a : List[Any] = DataCollatorWithPadding(tokenizer=_lowercase ) __a : int = TrainingArguments( output_dir=args.output_dir , learning_rate=args.learning_rate , lr_scheduler_type=args.lr_scheduler_type , evaluation_strategy='epoch' , save_strategy='epoch' , logging_strategy='epoch' , per_device_train_batch_size=args.batch_size , per_device_eval_batch_size=args.batch_size , num_train_epochs=args.num_epochs , gradient_accumulation_steps=args.gradient_accumulation_steps , weight_decay=0.0_1 , metric_for_best_model='accuracy' , run_name='complexity-java' , report_to='wandb' , ) __a : int = Trainer( model=_lowercase , args=_lowercase , train_dataset=tokenized_datasets['train'] , eval_dataset=tokenized_datasets['valid'] , tokenizer=_lowercase , data_collator=_lowercase , compute_metrics=_lowercase , ) print('Training...' ) trainer.add_callback(CustomCallback(_lowercase ) ) trainer.train() if __name__ == "__main__": main()
476
# 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 numpy as np import torch from ..models.clipseg import CLIPSegForImageSegmentation from ..utils import is_vision_available, requires_backends from .base import PipelineTool if is_vision_available(): from PIL import Image class __a( _a ): """simple docstring""" lowerCAmelCase = ( '''This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image.''' '''It takes two arguments named `image` which should be the original image, and `label` which should be a text ''' '''describing the elements what should be identified in the segmentation mask. The tool returns the mask.''' ) lowerCAmelCase = '''CIDAS/clipseg-rd64-refined''' lowerCAmelCase = '''image_segmenter''' lowerCAmelCase = CLIPSegForImageSegmentation lowerCAmelCase = ['''image''', '''text'''] lowerCAmelCase = ['''image'''] def __init__( self ,*_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ) -> Dict: requires_backends(self ,['''vision'''] ) super().__init__(*_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ) def a__ ( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) -> Tuple: return self.pre_processor(text=[label] ,images=[image] ,padding=_SCREAMING_SNAKE_CASE ,return_tensors='''pt''' ) def a__ ( self ,_SCREAMING_SNAKE_CASE ) -> str: with torch.no_grad(): UpperCAmelCase_ : Dict = self.model(**_SCREAMING_SNAKE_CASE ).logits return logits def a__ ( self ,_SCREAMING_SNAKE_CASE ) -> Dict: UpperCAmelCase_ : Dict = outputs.cpu().detach().numpy() UpperCAmelCase_ : Any = 0 UpperCAmelCase_ : List[Any] = 1 return Image.fromarray((array * 255).astype(np.uinta ) )
30
0
from ...configuration_utils import PretrainedConfig from ...utils import logging _lowercase = logging.get_logger(__name__) _lowercase = { 'facebook/xglm-564M': 'https://huggingface.co/facebook/xglm-564M/resolve/main/config.json', # See all XGLM models at https://huggingface.co/models?filter=xglm } class lowerCamelCase__ ( _a ): __lowerCamelCase = """xglm""" __lowerCamelCase = ["""past_key_values"""] __lowerCamelCase = { """num_attention_heads""": """attention_heads""", """hidden_size""": """d_model""", """num_hidden_layers""": """num_layers""", } def __init__( self : Dict , __a : List[Any]=256008 , __a : Any=2048 , __a : Dict=1024 , __a : Tuple=4096 , __a : Union[str, Any]=24 , __a : List[Any]=16 , __a : List[str]="gelu" , __a : int=0.1 , __a : Tuple=0.1 , __a : str=0.0 , __a : str=0.0 , __a : Union[str, Any]=0.02 , __a : Any=True , __a : List[str]=True , __a : List[Any]=2 , __a : Optional[Any]=1 , __a : int=0 , __a : List[str]=2 , **__a : Dict , ): '''simple docstring''' lowerCamelCase__: Optional[int] = vocab_size lowerCamelCase__: List[Any] = max_position_embeddings lowerCamelCase__: Dict = d_model lowerCamelCase__: Union[str, Any] = ffn_dim lowerCamelCase__: Dict = num_layers lowerCamelCase__: Dict = attention_heads lowerCamelCase__: Any = activation_function lowerCamelCase__: Optional[int] = dropout lowerCamelCase__: Tuple = attention_dropout lowerCamelCase__: str = activation_dropout lowerCamelCase__: Union[str, Any] = layerdrop lowerCamelCase__: Tuple = init_std lowerCamelCase__: int = scale_embedding # scale factor will be sqrt(d_model) if True lowerCamelCase__: str = use_cache super().__init__( pad_token_id=_SCREAMING_SNAKE_CASE , bos_token_id=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE , decoder_start_token_id=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , )
306
import numpy as np import datasets __a = '\nCompute the Mahalanobis Distance\n\nMahalonobis distance is the distance between a point and a distribution.\nAnd not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance.\nIt was introduced by Prof. P. C. Mahalanobis in 1936\nand has been used in various statistical applications ever since\n[source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/]\n' __a = '\\n@article{de2000mahalanobis,\n title={The mahalanobis distance},\n author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L},\n journal={Chemometrics and intelligent laboratory systems},\n volume={50},\n number={1},\n pages={1--18},\n year={2000},\n publisher={Elsevier}\n}\n' __a = '\nArgs:\n X: List of datapoints to be compared with the `reference_distribution`.\n reference_distribution: List of datapoints from the reference distribution we want to compare to.\nReturns:\n mahalanobis: The Mahalonobis distance for each datapoint in `X`.\nExamples:\n\n >>> mahalanobis_metric = datasets.load_metric("mahalanobis")\n >>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]])\n >>> print(results)\n {\'mahalanobis\': array([0.5])}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __a( datasets.Metric ): """simple docstring""" def a__ ( self ) -> Union[str, Any]: return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { '''X''': datasets.Sequence(datasets.Value('''float''' ,id='''sequence''' ) ,id='''X''' ), } ) ,) def a__ ( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) -> Any: # convert to numpy arrays UpperCAmelCase_ : str = np.array(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = np.array(_SCREAMING_SNAKE_CASE ) # Assert that arrays are 2D if len(X.shape ) != 2: raise ValueError('''Expected `X` to be a 2D vector''' ) if len(reference_distribution.shape ) != 2: raise ValueError('''Expected `reference_distribution` to be a 2D vector''' ) if reference_distribution.shape[0] < 2: raise ValueError( '''Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension''' ) # Get mahalanobis distance for each prediction UpperCAmelCase_ : List[str] = X - np.mean(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = np.cov(reference_distribution.T ) try: UpperCAmelCase_ : Any = np.linalg.inv(_SCREAMING_SNAKE_CASE ) except np.linalg.LinAlgError: UpperCAmelCase_ : List[str] = np.linalg.pinv(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = np.dot(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = np.dot(_SCREAMING_SNAKE_CASE ,X_minus_mu.T ).diagonal() return {"mahalanobis": mahal_dist}
30
0
import argparse import logging import os import re import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, DataCollatorForLanguageModeling, PushToHubCallback, TFAutoModelForMaskedLM, create_optimizer, ) _SCREAMING_SNAKE_CASE : str = logging.getLogger(__name__) _SCREAMING_SNAKE_CASE : Union[str, Any] = tf.data.AUTOTUNE def __lowerCAmelCase ( ): _lowercase: int = argparse.ArgumentParser(description="Train a masked language model on TPU." ) parser.add_argument( "--pretrained_model_config" , type=_lowercase , default="roberta-base" , help="The model config to use. Note that we don\'t copy the model\'s weights, only the config!" , ) parser.add_argument( "--tokenizer" , type=_lowercase , default="unigram-tokenizer-wikitext" , help="The name of the tokenizer to load. We use the pretrained tokenizer to initialize the model\'s vocab size." , ) parser.add_argument( "--per_replica_batch_size" , type=_lowercase , default=8 , help="Batch size per TPU core." , ) parser.add_argument( "--no_tpu" , action="store_true" , help="If set, run on CPU and don\'t try to initialize a TPU. Useful for debugging on non-TPU instances." , ) parser.add_argument( "--tpu_name" , type=_lowercase , help="Name of TPU resource to initialize. Should be blank on Colab, and \'local\' on TPU VMs." , default="local" , ) parser.add_argument( "--tpu_zone" , type=_lowercase , help="Google cloud zone that TPU resource is located in. Only used for non-Colab TPU nodes." , ) parser.add_argument( "--gcp_project" , type=_lowercase , help="Google cloud project name. Only used for non-Colab TPU nodes." ) parser.add_argument( "--bfloat16" , action="store_true" , help="Use mixed-precision bfloat16 for training. This is the recommended lower-precision format for TPU." , ) parser.add_argument( "--train_dataset" , type=_lowercase , help="Path to training dataset to load. If the path begins with `gs://`" " then the dataset will be loaded from a Google Cloud Storage bucket." , ) parser.add_argument( "--shuffle_buffer_size" , type=_lowercase , default=2**1_8 , help="Size of the shuffle buffer (in samples)" , ) parser.add_argument( "--eval_dataset" , type=_lowercase , help="Path to evaluation dataset to load. If the path begins with `gs://`" " then the dataset will be loaded from a Google Cloud Storage bucket." , ) parser.add_argument( "--num_epochs" , type=_lowercase , default=1 , help="Number of epochs to train for." , ) parser.add_argument( "--learning_rate" , type=_lowercase , default=1e-4 , help="Learning rate to use for training." , ) parser.add_argument( "--weight_decay_rate" , type=_lowercase , default=1e-3 , help="Weight decay rate to use for training." , ) parser.add_argument( "--max_length" , type=_lowercase , default=5_1_2 , help="Maximum length of tokenized sequences. Should match the setting used in prepare_tfrecord_shards.py" , ) parser.add_argument( "--mlm_probability" , type=_lowercase , default=0.15 , help="Fraction of tokens to mask during training." , ) parser.add_argument("--output_dir" , type=_lowercase , required=_lowercase , help="Path to save model checkpoints to." ) parser.add_argument("--hub_model_id" , type=_lowercase , help="Model ID to upload to on the Hugging Face Hub." ) _lowercase: Optional[int] = parser.parse_args() return args def __lowerCAmelCase ( __magic_name__ ): try: if args.tpu_name: _lowercase: List[Any] = tf.distribute.cluster_resolver.TPUClusterResolver( args.tpu_name , zone=args.tpu_zone , project=args.gcp_project ) else: _lowercase: Optional[Any] = tf.distribute.cluster_resolver.TPUClusterResolver() except ValueError: raise RuntimeError( "Couldn\'t connect to TPU! Most likely you need to specify --tpu_name, --tpu_zone, or " "--gcp_project. When running on a TPU VM, use --tpu_name local." ) tf.config.experimental_connect_to_cluster(_lowercase ) tf.tpu.experimental.initialize_tpu_system(_lowercase ) return tpu def __lowerCAmelCase ( __magic_name__ ): _lowercase: List[str] = 0 for file in file_list: _lowercase: Optional[int] = file.split("/" )[-1] _lowercase: Tuple = re.search(R"-\d+-(\d+)\.tfrecord" , _lowercase ).group(1 ) _lowercase: Dict = int(_lowercase ) num_samples += sample_count return num_samples def __lowerCAmelCase ( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__=None ): _lowercase: Tuple = count_samples(_lowercase ) _lowercase: List[Any] = tf.data.Dataset.from_tensor_slices(_lowercase ) if shuffle: _lowercase: List[Any] = dataset.shuffle(len(_lowercase ) ) _lowercase: Any = tf.data.TFRecordDataset(_lowercase , num_parallel_reads=_lowercase ) # TF can't infer the total sample count because it doesn't read all the records yet, so we assert it here _lowercase: Any = dataset.apply(tf.data.experimental.assert_cardinality(_lowercase ) ) _lowercase: List[Any] = dataset.map(_lowercase , num_parallel_calls=_lowercase ) if shuffle: assert shuffle_buffer_size is not None _lowercase: Tuple = dataset.shuffle(args.shuffle_buffer_size ) _lowercase: Optional[Any] = dataset.batch(_lowercase , drop_remainder=_lowercase ) _lowercase: Optional[Any] = dataset.map(_lowercase , num_parallel_calls=_lowercase ) _lowercase: Tuple = dataset.prefetch(_lowercase ) return dataset def __lowerCAmelCase ( __magic_name__ ): if not args.no_tpu: _lowercase: List[Any] = initialize_tpu(_lowercase ) _lowercase: Optional[int] = tf.distribute.TPUStrategy(_lowercase ) else: _lowercase: Dict = tf.distribute.OneDeviceStrategy(device="/gpu:0" ) if args.bfloataa: tf.keras.mixed_precision.set_global_policy("mixed_bfloat16" ) _lowercase: str = AutoTokenizer.from_pretrained(args.tokenizer ) _lowercase: List[str] = AutoConfig.from_pretrained(args.pretrained_model_config ) _lowercase: int = tokenizer.vocab_size _lowercase: Union[str, Any] = tf.io.gfile.glob(os.path.join(args.train_dataset , "*.tfrecord" ) ) if not training_records: raise ValueError(f"No .tfrecord files found in {args.train_dataset}." ) _lowercase: Tuple = tf.io.gfile.glob(os.path.join(args.eval_dataset , "*.tfrecord" ) ) if not eval_records: raise ValueError(f"No .tfrecord files found in {args.eval_dataset}." ) _lowercase: Dict = count_samples(_lowercase ) _lowercase: Any = num_train_samples // (args.per_replica_batch_size * strategy.num_replicas_in_sync) _lowercase: Optional[int] = steps_per_epoch * args.num_epochs with strategy.scope(): _lowercase: Any = TFAutoModelForMaskedLM.from_config(_lowercase ) model(model.dummy_inputs ) # Pass some dummy inputs through the model to ensure all the weights are built _lowercase: Any = create_optimizer( num_train_steps=_lowercase , num_warmup_steps=total_train_steps // 2_0 , init_lr=args.learning_rate , weight_decay_rate=args.weight_decay_rate , ) # Transformers models compute the right loss for their task by default when labels are passed, and will # use this for training unless you specify your own loss function in compile(). model.compile(optimizer=_lowercase , metrics=["accuracy"] ) def decode_fn(__magic_name__ ): _lowercase: List[Any] = { '''input_ids''': tf.io.FixedLenFeature(dtype=tf.intaa , shape=(args.max_length,) ), '''attention_mask''': tf.io.FixedLenFeature(dtype=tf.intaa , shape=(args.max_length,) ), } return tf.io.parse_single_example(_lowercase , _lowercase ) # Many of the data collators in Transformers are TF-compilable when return_tensors == "tf", so we can # use their methods in our data pipeline. _lowercase: str = DataCollatorForLanguageModeling( tokenizer=_lowercase , mlm_probability=args.mlm_probability , mlm=_lowercase , return_tensors="tf" ) def mask_with_collator(__magic_name__ ): # TF really needs an isin() function _lowercase: Dict = ( ~tf.cast(batch["attention_mask"] , tf.bool ) | (batch['''input_ids'''] == tokenizer.cls_token_id) | (batch['''input_ids'''] == tokenizer.sep_token_id) ) _lowercase: List[Any] = data_collator.tf_mask_tokens( batch["input_ids"] , vocab_size=len(_lowercase ) , mask_token_id=tokenizer.mask_token_id , special_tokens_mask=_lowercase , ) return batch _lowercase: Any = args.per_replica_batch_size * strategy.num_replicas_in_sync _lowercase: Any = prepare_dataset( _lowercase , decode_fn=_lowercase , mask_fn=_lowercase , batch_size=_lowercase , shuffle=_lowercase , shuffle_buffer_size=args.shuffle_buffer_size , ) _lowercase: Dict = prepare_dataset( _lowercase , decode_fn=_lowercase , mask_fn=_lowercase , batch_size=_lowercase , shuffle=_lowercase , ) _lowercase: Optional[Any] = [] if args.hub_model_id: callbacks.append( PushToHubCallback(output_dir=args.output_dir , hub_model_id=args.hub_model_id , tokenizer=_lowercase ) ) model.fit( _lowercase , validation_data=_lowercase , epochs=args.num_epochs , callbacks=_lowercase , ) model.save_pretrained(args.output_dir ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE : List[str] = parse_args() main(args)
226
import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert import BertTokenizer __a = logging.get_logger(__name__) __a = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} __a = { 'vocab_file': { 'facebook/dpr-ctx_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt' ), 'facebook/dpr-ctx_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'facebook/dpr-ctx_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json' ), 'facebook/dpr-ctx_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json' ), }, } __a = { 'vocab_file': { 'facebook/dpr-question_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt' ), 'facebook/dpr-question_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'facebook/dpr-question_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json' ), 'facebook/dpr-question_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json' ), }, } __a = { 'vocab_file': { 'facebook/dpr-reader-single-nq-base': ( 'https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt' ), 'facebook/dpr-reader-multiset-base': ( 'https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'facebook/dpr-reader-single-nq-base': ( 'https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json' ), 'facebook/dpr-reader-multiset-base': ( 'https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json' ), }, } __a = { 'facebook/dpr-ctx_encoder-single-nq-base': 512, 'facebook/dpr-ctx_encoder-multiset-base': 512, } __a = { 'facebook/dpr-question_encoder-single-nq-base': 512, 'facebook/dpr-question_encoder-multiset-base': 512, } __a = { 'facebook/dpr-reader-single-nq-base': 512, 'facebook/dpr-reader-multiset-base': 512, } __a = { 'facebook/dpr-ctx_encoder-single-nq-base': {'do_lower_case': True}, 'facebook/dpr-ctx_encoder-multiset-base': {'do_lower_case': True}, } __a = { 'facebook/dpr-question_encoder-single-nq-base': {'do_lower_case': True}, 'facebook/dpr-question_encoder-multiset-base': {'do_lower_case': True}, } __a = { 'facebook/dpr-reader-single-nq-base': {'do_lower_case': True}, 'facebook/dpr-reader-multiset-base': {'do_lower_case': True}, } class __a( _a ): """simple docstring""" lowerCAmelCase = VOCAB_FILES_NAMES lowerCAmelCase = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION class __a( _a ): """simple docstring""" lowerCAmelCase = VOCAB_FILES_NAMES lowerCAmelCase = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION __a = collections.namedtuple( 'DPRSpanPrediction', ['span_score', 'relevance_score', 'doc_id', 'start_index', 'end_index', 'text'] ) __a = collections.namedtuple('DPRReaderOutput', ['start_logits', 'end_logits', 'relevance_logits']) __a = R'\n Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`.\n It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers),\n using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)`\n with the format:\n\n ```\n [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>\n ```\n\n Args:\n questions (`str` or `List[str]`):\n The questions to be encoded. You can specify one question for many passages. In this case, the question\n will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in\n `titles` or `texts`.\n titles (`str` or `List[str]`):\n The passages titles to be encoded. This can be a string or a list of strings if there are several passages.\n texts (`str` or `List[str]`):\n The passages texts to be encoded. This can be a string or a list of strings if there are several passages.\n padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):\n Activates and controls padding. Accepts the following values:\n\n - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence\n if provided).\n - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided.\n - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different\n lengths).\n truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):\n Activates and controls truncation. Accepts the following values:\n\n - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to\n the maximum acceptable input length for the model if that argument is not provided. This will truncate\n token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch\n of pairs) is provided.\n - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the first\n sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the\n second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths\n greater than the model maximum admissible input size).\n max_length (`int`, *optional*):\n Controls the maximum length to use by one of the truncation/padding parameters.\n\n If left unset or set to `None`, this will use the predefined model maximum length if a maximum length\n is required by one of the truncation/padding parameters. If the model has no specific maximum input\n length (like XLNet) truncation/padding to a maximum length will be deactivated.\n return_tensors (`str` or [`~utils.TensorType`], *optional*):\n If set, will return tensors instead of list of python integers. Acceptable values are:\n\n - `\'tf\'`: Return TensorFlow `tf.constant` objects.\n - `\'pt\'`: Return PyTorch `torch.Tensor` objects.\n - `\'np\'`: Return Numpy `np.ndarray` objects.\n return_attention_mask (`bool`, *optional*):\n Whether or not to return the attention mask. If not set, will return the attention mask according to the\n specific tokenizer\'s default, defined by the `return_outputs` attribute.\n\n [What are attention masks?](../glossary#attention-mask)\n\n Returns:\n `Dict[str, List[List[int]]]`: A dictionary with the following keys:\n\n - `input_ids`: List of token ids to be fed to a model.\n - `attention_mask`: List of indices specifying which tokens should be attended to by the model.\n ' @add_start_docstrings(_a ) class __a: """simple docstring""" def __call__( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = None ,**_SCREAMING_SNAKE_CASE ,) -> BatchEncoding: if titles is None and texts is None: return super().__call__( _SCREAMING_SNAKE_CASE ,padding=_SCREAMING_SNAKE_CASE ,truncation=_SCREAMING_SNAKE_CASE ,max_length=_SCREAMING_SNAKE_CASE ,return_tensors=_SCREAMING_SNAKE_CASE ,return_attention_mask=_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ,) elif titles is None or texts is None: UpperCAmelCase_ : List[str] = titles if texts is None else texts return super().__call__( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,padding=_SCREAMING_SNAKE_CASE ,truncation=_SCREAMING_SNAKE_CASE ,max_length=_SCREAMING_SNAKE_CASE ,return_tensors=_SCREAMING_SNAKE_CASE ,return_attention_mask=_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ,) UpperCAmelCase_ : List[Any] = titles if not isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) else [titles] UpperCAmelCase_ : List[str] = texts if not isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) else [texts] UpperCAmelCase_ : Any = len(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = questions if not isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) else [questions] * n_passages if len(_SCREAMING_SNAKE_CASE ) != len(_SCREAMING_SNAKE_CASE ): raise ValueError( f'''There should be as many titles than texts but got {len(_SCREAMING_SNAKE_CASE )} titles and {len(_SCREAMING_SNAKE_CASE )} texts.''' ) UpperCAmelCase_ : Tuple = super().__call__(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,padding=_SCREAMING_SNAKE_CASE ,truncation=_SCREAMING_SNAKE_CASE )['''input_ids'''] UpperCAmelCase_ : int = super().__call__(_SCREAMING_SNAKE_CASE ,add_special_tokens=_SCREAMING_SNAKE_CASE ,padding=_SCREAMING_SNAKE_CASE ,truncation=_SCREAMING_SNAKE_CASE )['''input_ids'''] UpperCAmelCase_ : Optional[int] = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) ] } if return_attention_mask is not False: UpperCAmelCase_ : List[str] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) UpperCAmelCase_ : Dict = attention_mask return self.pad(_SCREAMING_SNAKE_CASE ,padding=_SCREAMING_SNAKE_CASE ,max_length=_SCREAMING_SNAKE_CASE ,return_tensors=_SCREAMING_SNAKE_CASE ) def a__ ( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE = 16 ,_SCREAMING_SNAKE_CASE = 64 ,_SCREAMING_SNAKE_CASE = 4 ,) -> List[DPRSpanPrediction]: UpperCAmelCase_ : Tuple = reader_input['''input_ids'''] UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_ : Optional[Any] = reader_output[:3] UpperCAmelCase_ : Optional[Any] = len(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = sorted(range(_SCREAMING_SNAKE_CASE ) ,reverse=_SCREAMING_SNAKE_CASE ,key=relevance_logits.__getitem__ ) UpperCAmelCase_ : List[DPRReaderOutput] = [] for doc_id in sorted_docs: UpperCAmelCase_ : List[Any] = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence UpperCAmelCase_ : str = sequence_ids.index(self.sep_token_id ,2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: UpperCAmelCase_ : List[Any] = sequence_ids.index(self.pad_token_id ) else: UpperCAmelCase_ : int = len(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] ,end_logits=end_logits[doc_id][passage_offset:sequence_len] ,max_answer_length=_SCREAMING_SNAKE_CASE ,top_spans=_SCREAMING_SNAKE_CASE ,) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] ,relevance_score=relevance_logits[doc_id] ,doc_id=_SCREAMING_SNAKE_CASE ,start_index=_SCREAMING_SNAKE_CASE ,end_index=_SCREAMING_SNAKE_CASE ,text=self.decode(sequence_ids[start_index : end_index + 1] ) ,) ) if len(_SCREAMING_SNAKE_CASE ) >= num_spans: break return nbest_spans_predictions[:num_spans] def a__ ( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,) -> List[DPRSpanPrediction]: UpperCAmelCase_ : Tuple = [] for start_index, start_score in enumerate(_SCREAMING_SNAKE_CASE ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) UpperCAmelCase_ : int = sorted(_SCREAMING_SNAKE_CASE ,key=lambda _SCREAMING_SNAKE_CASE : x[1] ,reverse=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = [] for (start_index, end_index), score in scores: if start_index > end_index: raise ValueError(f'''Wrong span indices: [{start_index}:{end_index}]''' ) UpperCAmelCase_ : str = end_index - start_index + 1 if length > max_answer_length: raise ValueError(f'''Span is too long: {length} > {max_answer_length}''' ) if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(_SCREAMING_SNAKE_CASE ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_a ) class __a( _a , _a ): """simple docstring""" lowerCAmelCase = VOCAB_FILES_NAMES lowerCAmelCase = READER_PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase = READER_PRETRAINED_INIT_CONFIGURATION lowerCAmelCase = ['''input_ids''', '''attention_mask''']
30
0
import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor __a = logging.get_logger(__name__) class lowercase__( _a ): """simple docstring""" def __init__( self : List[str] , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : str ) -> None: warnings.warn( '''The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use CLIPImageProcessor instead.''' , _SCREAMING_SNAKE_CASE , ) super().__init__(*_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE )
97
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) __a = { 'configuration_encodec': [ 'ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP', 'EncodecConfig', ], 'feature_extraction_encodec': ['EncodecFeatureExtractor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST', 'EncodecModel', 'EncodecPreTrainedModel', ] if TYPE_CHECKING: from .configuration_encodec import ( ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP, EncodecConfig, ) from .feature_extraction_encodec import EncodecFeatureExtractor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_encodec import ( ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST, EncodecModel, EncodecPreTrainedModel, ) else: import sys __a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
30
0
'''simple docstring''' from __future__ import annotations def snake_case_ ( lowercase__ ): UpperCAmelCase__ : Optional[int] = 0.00 UpperCAmelCase__ : Tuple = 0 for resistor in resistors: if resistor <= 0: UpperCAmelCase__ : List[Any] = F"""Resistor at index {index} has a negative or zero value!""" raise ValueError(_lowercase ) first_sum += 1 / float(_lowercase ) index += 1 return 1 / first_sum def snake_case_ ( lowercase__ ): UpperCAmelCase__ : Tuple = 0.00 UpperCAmelCase__ : str = 0 for resistor in resistors: sum_r += resistor if resistor < 0: UpperCAmelCase__ : Tuple = F"""Resistor at index {index} has a negative value!""" raise ValueError(_lowercase ) index += 1 return sum_r if __name__ == "__main__": import doctest doctest.testmod()
199
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __a = logging.get_logger(__name__) __a = { 'facebook/wav2vec2-base-960h': 'https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json', # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2 } class __a( _a ): """simple docstring""" lowerCAmelCase = '''wav2vec2''' def __init__( self ,_SCREAMING_SNAKE_CASE=32 ,_SCREAMING_SNAKE_CASE=768 ,_SCREAMING_SNAKE_CASE=12 ,_SCREAMING_SNAKE_CASE=12 ,_SCREAMING_SNAKE_CASE=3_072 ,_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="sum" ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=256 ,_SCREAMING_SNAKE_CASE=(512, 512, 512, 512, 1_500) ,_SCREAMING_SNAKE_CASE=(5, 3, 3, 1, 1) ,_SCREAMING_SNAKE_CASE=(1, 2, 3, 1, 1) ,_SCREAMING_SNAKE_CASE=512 ,_SCREAMING_SNAKE_CASE=0 ,_SCREAMING_SNAKE_CASE=1 ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=3 ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE=3 ,_SCREAMING_SNAKE_CASE=None ,_SCREAMING_SNAKE_CASE=None ,**_SCREAMING_SNAKE_CASE ,) -> Optional[int]: super().__init__(**_SCREAMING_SNAKE_CASE ,pad_token_id=_SCREAMING_SNAKE_CASE ,bos_token_id=_SCREAMING_SNAKE_CASE ,eos_token_id=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = hidden_size UpperCAmelCase_ : Tuple = feat_extract_norm UpperCAmelCase_ : List[Any] = feat_extract_activation UpperCAmelCase_ : str = list(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Any = list(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = list(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = conv_bias UpperCAmelCase_ : str = num_conv_pos_embeddings UpperCAmelCase_ : Any = num_conv_pos_embedding_groups UpperCAmelCase_ : Tuple = len(self.conv_dim ) UpperCAmelCase_ : Union[str, Any] = num_hidden_layers UpperCAmelCase_ : Dict = intermediate_size UpperCAmelCase_ : Any = hidden_act UpperCAmelCase_ : Any = num_attention_heads UpperCAmelCase_ : str = hidden_dropout UpperCAmelCase_ : int = attention_dropout UpperCAmelCase_ : Tuple = activation_dropout UpperCAmelCase_ : List[str] = feat_proj_dropout UpperCAmelCase_ : int = final_dropout UpperCAmelCase_ : Union[str, Any] = layerdrop UpperCAmelCase_ : Optional[Any] = layer_norm_eps UpperCAmelCase_ : str = initializer_range UpperCAmelCase_ : List[str] = vocab_size UpperCAmelCase_ : Optional[int] = do_stable_layer_norm UpperCAmelCase_ : Optional[int] = use_weighted_layer_sum 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 UpperCAmelCase_ : Optional[int] = apply_spec_augment UpperCAmelCase_ : Tuple = mask_time_prob UpperCAmelCase_ : Optional[Any] = mask_time_length UpperCAmelCase_ : Union[str, Any] = mask_time_min_masks UpperCAmelCase_ : Optional[Any] = mask_feature_prob UpperCAmelCase_ : str = mask_feature_length UpperCAmelCase_ : Dict = mask_feature_min_masks # parameters for pretraining with codevector quantized representations UpperCAmelCase_ : Union[str, Any] = num_codevectors_per_group UpperCAmelCase_ : Any = num_codevector_groups UpperCAmelCase_ : Union[str, Any] = contrastive_logits_temperature UpperCAmelCase_ : List[str] = feat_quantizer_dropout UpperCAmelCase_ : Dict = num_negatives UpperCAmelCase_ : List[str] = codevector_dim UpperCAmelCase_ : List[str] = proj_codevector_dim UpperCAmelCase_ : str = diversity_loss_weight # ctc loss UpperCAmelCase_ : List[Any] = ctc_loss_reduction UpperCAmelCase_ : List[str] = ctc_zero_infinity # adapter UpperCAmelCase_ : Optional[Any] = add_adapter UpperCAmelCase_ : Any = adapter_kernel_size UpperCAmelCase_ : Optional[int] = adapter_stride UpperCAmelCase_ : List[Any] = num_adapter_layers UpperCAmelCase_ : Optional[Any] = output_hidden_size or hidden_size UpperCAmelCase_ : Optional[int] = adapter_attn_dim # SequenceClassification-specific parameter. Feel free to ignore for other classes. UpperCAmelCase_ : List[str] = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. UpperCAmelCase_ : List[str] = list(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = list(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = list(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Any = xvector_output_dim @property def a__ ( self ) -> Any: return functools.reduce(operator.mul ,self.conv_stride ,1 )
30
0
def _UpperCamelCase ( lowercase__ , lowercase__ ): if density <= 0: raise ValueError('''Impossible fluid density''' ) if bulk_modulus <= 0: raise ValueError('''Impossible bulk modulus''' ) return (bulk_modulus / density) ** 0.5 if __name__ == "__main__": import doctest doctest.testmod()
696
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __a = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ['NllbTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ['NllbTokenizerFast'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb import NllbTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb_fast import NllbTokenizerFast else: import sys __a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
30
0
import os import sys import unittest _SCREAMING_SNAKE_CASE : int = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_dummies # noqa: E402 from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init # noqa: E402 # Align TRANSFORMERS_PATH in check_dummies with the current path _SCREAMING_SNAKE_CASE : int = os.path.join(git_repo_path, "src", "transformers") _SCREAMING_SNAKE_CASE : Optional[int] = "\n{0} = None\n" _SCREAMING_SNAKE_CASE : Optional[Any] = "\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n" _SCREAMING_SNAKE_CASE : List[str] = "\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n" class A__ ( unittest.TestCase ): """simple docstring""" def a_ ( self ): snake_case = find_backend(''' _import_structure["models.albert"].append("AlbertTokenizerFast")''' ) self.assertIsNone(_SCREAMING_SNAKE_CASE ) snake_case = find_backend(''' if not is_tokenizers_available():''' ) self.assertEqual(_SCREAMING_SNAKE_CASE , '''tokenizers''' ) snake_case = find_backend(''' if not is_tensorflow_text_available():''' ) self.assertEqual(_SCREAMING_SNAKE_CASE , '''tensorflow_text''' ) snake_case = find_backend(''' if not (is_sentencepiece_available() and is_tokenizers_available()):''' ) self.assertEqual(_SCREAMING_SNAKE_CASE , '''sentencepiece_and_tokenizers''' ) snake_case = find_backend( ''' if not (is_sentencepiece_available() and is_tensorflow_text_available()):''' ) self.assertEqual(_SCREAMING_SNAKE_CASE , '''sentencepiece_and_tensorflow_text''' ) snake_case = find_backend( ''' if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):''' ) self.assertEqual(_SCREAMING_SNAKE_CASE , '''sentencepiece_and_tokenizers_and_vision''' ) def a_ ( self ): snake_case = read_init() # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects self.assertIn('''torch''' , _SCREAMING_SNAKE_CASE ) self.assertIn('''tensorflow_text''' , _SCREAMING_SNAKE_CASE ) self.assertIn('''sentencepiece_and_tokenizers''' , _SCREAMING_SNAKE_CASE ) # Likewise, we can't assert on the exact content of a key self.assertIn('''BertModel''' , objects['''torch'''] ) self.assertIn('''TFBertModel''' , objects['''tf'''] ) self.assertIn('''FlaxBertModel''' , objects['''flax'''] ) self.assertIn('''BertModel''' , objects['''torch'''] ) self.assertIn('''TFBertTokenizer''' , objects['''tensorflow_text'''] ) self.assertIn('''convert_slow_tokenizer''' , objects['''sentencepiece_and_tokenizers'''] ) def a_ ( self ): snake_case = create_dummy_object('''CONSTANT''' , '''\'torch\'''' ) self.assertEqual(_SCREAMING_SNAKE_CASE , '''\nCONSTANT = None\n''' ) snake_case = create_dummy_object('''function''' , '''\'torch\'''' ) self.assertEqual( _SCREAMING_SNAKE_CASE , '''\ndef function(*args, **kwargs):\n requires_backends(function, \'torch\')\n''' ) snake_case = ''' class FakeClass(metaclass=DummyObject): _backends = \'torch\' def __init__(self, *args, **kwargs): requires_backends(self, \'torch\') ''' snake_case = create_dummy_object('''FakeClass''' , '''\'torch\'''' ) self.assertEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def a_ ( self ): snake_case = '''# This file is autogenerated by the command `make fix-copies`, do not edit. from ..utils import DummyObject, requires_backends CONSTANT = None def function(*args, **kwargs): requires_backends(function, ["torch"]) class FakeClass(metaclass=DummyObject): _backends = ["torch"] def __init__(self, *args, **kwargs): requires_backends(self, ["torch"]) ''' snake_case = create_dummy_files({'''torch''': ['''CONSTANT''', '''function''', '''FakeClass''']} ) self.assertEqual(dummy_files['''torch'''] , _SCREAMING_SNAKE_CASE )
550
import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList __a = ['\nclass', '\ndef', '\n#', '\n@', '\nprint', '\nif'] class __a( _a ): """simple docstring""" def __init__( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE=None ,_SCREAMING_SNAKE_CASE=1 ) -> Dict: UpperCAmelCase_ : List[Any] = tokenizer UpperCAmelCase_ : int = dataset UpperCAmelCase_ : Dict = len(_SCREAMING_SNAKE_CASE ) if n_tasks is None else n_tasks UpperCAmelCase_ : Optional[int] = n_copies def __iter__( self ) -> Any: UpperCAmelCase_ : List[Any] = [] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['''prompt'''].strip() ) UpperCAmelCase_ : Union[str, Any] = self.tokenizer(_SCREAMING_SNAKE_CASE ,padding=_SCREAMING_SNAKE_CASE ,return_tensors='''pt''' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class __a( _a ): """simple docstring""" def __init__( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) -> List[Any]: UpperCAmelCase_ : str = start_length UpperCAmelCase_ : Optional[int] = eof_strings UpperCAmelCase_ : str = tokenizer def __call__( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ) -> List[Any]: UpperCAmelCase_ : Optional[Any] = self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) UpperCAmelCase_ : Optional[int] = [] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(_SCREAMING_SNAKE_CASE ) def lowerCamelCase__ ( _lowercase ): '''simple docstring''' UpperCAmelCase_ : Tuple = re.split('''(%s)''' % '''|'''.join(_lowercase ) , _lowercase ) # last string should be "" return "".join(string_list[:-2] ) def lowerCamelCase__ ( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase=20 , **_lowercase ): '''simple docstring''' UpperCAmelCase_ : List[Any] = defaultdict(_lowercase ) # dict of list of generated tokens for step, batch in tqdm(enumerate(_lowercase ) ): with torch.no_grad(): UpperCAmelCase_ : Dict = batch['''ids'''].shape[-1] UpperCAmelCase_ : Optional[Any] = accelerator.unwrap_model(_lowercase ).generate( input_ids=batch['''ids'''][:, : batch['''input_len''']] , num_return_sequences=_lowercase , **_lowercase ) # each task is generated batch_size times UpperCAmelCase_ : Union[str, Any] = batch['''task_id'''].repeat(_lowercase ) UpperCAmelCase_ : Dict = accelerator.pad_across_processes( _lowercase , dim=1 , pad_index=tokenizer.pad_token_id ) UpperCAmelCase_, UpperCAmelCase_ : List[str] = accelerator.gather((generated_tokens, generated_tasks) ) UpperCAmelCase_ : Union[str, Any] = generated_tokens.cpu().numpy() UpperCAmelCase_ : Union[str, Any] = generated_tasks.cpu().numpy() for task, generated_tokens in zip(_lowercase , _lowercase ): gen_token_dict[task].append(_lowercase ) UpperCAmelCase_ : Union[str, Any] = [[] for _ in range(_lowercase )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: UpperCAmelCase_ : int = tokenizer.decode(_lowercase , skip_special_tokens=_lowercase , clean_up_tokenization_spaces=_lowercase ) code_gens[task].append(remove_last_block(_lowercase ) ) return code_gens def lowerCamelCase__ ( ): '''simple docstring''' UpperCAmelCase_ : Optional[Any] = HfArgumentParser(_lowercase ) UpperCAmelCase_ : int = parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric UpperCAmelCase_ : Optional[Any] = args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing UpperCAmelCase_ : List[Any] = '''false''' if args.num_workers is None: UpperCAmelCase_ : Optional[Any] = multiprocessing.cpu_count() # Use dataset load to feed to accelerate UpperCAmelCase_ : int = Accelerator() set_seed(args.seed , device_specific=_lowercase ) # Load model and tokenizer UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained(args.model_ckpt ) UpperCAmelCase_ : Any = tokenizer.eos_token UpperCAmelCase_ : List[str] = AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings UpperCAmelCase_ : str = { '''do_sample''': args.do_sample, '''temperature''': args.temperature, '''max_new_tokens''': args.max_new_tokens, '''top_p''': args.top_p, '''top_k''': args.top_k, '''stopping_criteria''': StoppingCriteriaList([EndOfFunctionCriteria(0 , _lowercase , _lowercase )] ), } # Load evaluation dataset and metric UpperCAmelCase_ : Tuple = load_dataset('''openai_humaneval''' ) UpperCAmelCase_ : Dict = load_metric('''code_eval''' ) UpperCAmelCase_ : Optional[int] = args.num_tasks if args.num_tasks is not None else len(human_eval['''test'''] ) UpperCAmelCase_ : str = args.n_samples // args.batch_size UpperCAmelCase_ : str = TokenizedDataset(_lowercase , human_eval['''test'''] , n_copies=_lowercase , n_tasks=_lowercase ) # do not confuse args.batch_size, which is actually the num_return_sequences UpperCAmelCase_ : Optional[Any] = DataLoader(_lowercase , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: UpperCAmelCase_ : Any = code_eval_metric.compute(references=[''''''] , predictions=[['''''']] ) except ValueError as exception: print( '''Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`''' ''' flag to enable code evaluation.''' ) raise exception UpperCAmelCase_, UpperCAmelCase_ : int = accelerator.prepare(_lowercase , _lowercase ) UpperCAmelCase_ : int = complete_code( _lowercase , _lowercase , _lowercase , _lowercase , n_tasks=_lowercase , batch_size=args.batch_size , **_lowercase , ) if accelerator.is_main_process: UpperCAmelCase_ : Any = [] for task in tqdm(range(_lowercase ) ): UpperCAmelCase_ : int = human_eval['''test'''][task]['''test'''] UpperCAmelCase_ : str = f'''check({human_eval["test"][task]["entry_point"]})''' references.append('''\n''' + test_func + '''\n''' + entry_point ) # Evaluate completions with "code_eval" metric UpperCAmelCase_, UpperCAmelCase_ : Any = code_eval_metric.compute( references=_lowercase , predictions=_lowercase , num_workers=args.num_workers ) print(f'''Results: {pass_at_k}''' ) # Save results to json file with open(args.output_file , '''w''' ) as fp: json.dump(_lowercase , _lowercase ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
30
0
'''simple docstring''' 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 tensorflow as tf from transformers import AutoTokenizer, TFAutoModelForSeqaSeqLM @require_tf @require_sentencepiece @require_tokenizers class lowercase_ ( unittest.TestCase ): @slow def _lowerCAmelCase ( self : Tuple ): snake_case__ : Dict = TFAutoModelForSeqaSeqLM.from_pretrained('google/mt5-small' ) snake_case__ : List[Any] = AutoTokenizer.from_pretrained('google/mt5-small' ) snake_case__ : Optional[int] = tokenizer('Hello there' , return_tensors='tf' ).input_ids snake_case__ : Tuple = tokenizer('Hi I am' , return_tensors='tf' ).input_ids snake_case__ : Optional[Any] = model(_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ).loss snake_case__ : Optional[Any] = -tf.math.reduce_mean(_SCREAMING_SNAKE_CASE ).numpy() snake_case__ : Optional[int] = -2_1.2_2_8_1_6_8 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 2E-4 )
270
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, TensorType __a = logging.get_logger(__name__) __a = { 'openai/imagegpt-small': '', 'openai/imagegpt-medium': '', 'openai/imagegpt-large': '', } class __a( _a ): """simple docstring""" lowerCAmelCase = '''imagegpt''' lowerCAmelCase = ['''past_key_values'''] lowerCAmelCase = { '''hidden_size''': '''n_embd''', '''max_position_embeddings''': '''n_positions''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self ,_SCREAMING_SNAKE_CASE=512 + 1 ,_SCREAMING_SNAKE_CASE=32 * 32 ,_SCREAMING_SNAKE_CASE=512 ,_SCREAMING_SNAKE_CASE=24 ,_SCREAMING_SNAKE_CASE=8 ,_SCREAMING_SNAKE_CASE=None ,_SCREAMING_SNAKE_CASE="quick_gelu" ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=1e-5 ,_SCREAMING_SNAKE_CASE=0.02 ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=False ,**_SCREAMING_SNAKE_CASE ,) -> Optional[int]: UpperCAmelCase_ : Optional[int] = vocab_size UpperCAmelCase_ : Union[str, Any] = n_positions UpperCAmelCase_ : Union[str, Any] = n_embd UpperCAmelCase_ : Any = n_layer UpperCAmelCase_ : Optional[Any] = n_head UpperCAmelCase_ : Union[str, Any] = n_inner UpperCAmelCase_ : List[Any] = activation_function UpperCAmelCase_ : List[str] = resid_pdrop UpperCAmelCase_ : str = embd_pdrop UpperCAmelCase_ : Optional[Any] = attn_pdrop UpperCAmelCase_ : Dict = layer_norm_epsilon UpperCAmelCase_ : Union[str, Any] = initializer_range UpperCAmelCase_ : Dict = scale_attn_weights UpperCAmelCase_ : Any = use_cache UpperCAmelCase_ : List[str] = scale_attn_by_inverse_layer_idx UpperCAmelCase_ : Tuple = reorder_and_upcast_attn UpperCAmelCase_ : int = tie_word_embeddings super().__init__(tie_word_embeddings=_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ) class __a( _a ): """simple docstring""" @property def a__ ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ] ) def a__ ( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE = 1 ,_SCREAMING_SNAKE_CASE = -1 ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = 3 ,_SCREAMING_SNAKE_CASE = 32 ,_SCREAMING_SNAKE_CASE = 32 ,) -> Mapping[str, Any]: UpperCAmelCase_ : Any = self._generate_dummy_images(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = dict(preprocessor(images=_SCREAMING_SNAKE_CASE ,return_tensors=_SCREAMING_SNAKE_CASE ) ) return inputs
30
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase_ = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['NllbTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['NllbTokenizerFast'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb import NllbTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb_fast import NllbTokenizerFast else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
173
import argparse import json import os import re import torch from transformers import BloomConfig, BloomModel from transformers.file_utils import CONFIG_NAME, WEIGHTS_NAME from transformers.utils import logging logging.set_verbosity_info() __a = [ 'word_embeddings_layernorm.weight', 'word_embeddings_layernorm.bias', 'input_layernorm.weight', 'input_layernorm.bias', 'post_attention_layernorm.weight', 'post_attention_layernorm.bias', 'self_attention.dense.bias', 'mlp.dense_4h_to_h.bias', 'ln_f.weight', 'ln_f.bias', ] __a = [ 'mlp.dense_4h_to_h.weight', 'self_attention.dense.weight', ] def lowerCamelCase__ ( _lowercase , _lowercase ): '''simple docstring''' UpperCAmelCase_ : List[Any] = { '''word_embeddings.weight''': '''word_embeddings.weight''', '''word_embeddings.norm.weight''': '''word_embeddings_layernorm.weight''', '''word_embeddings.norm.bias''': '''word_embeddings_layernorm.bias''', '''weight''': '''ln_f.weight''', '''bias''': '''ln_f.bias''', } if key in layer_rename_map: return layer_rename_map[key] # Handle transformer blocks UpperCAmelCase_ : Union[str, Any] = int(re.match(r'''.*layer_(\d*).*''' , _lowercase )[1] ) layer_number -= 3 return f'''h.{layer_number}.''' + key def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if dtype == torch.bool: return 1 / 8 UpperCAmelCase_ : Any = re.search(r'''[^\d](\d+)$''' , str(_lowercase ) ) if bit_search is None: raise ValueError(f'''`dtype` is not a valid dtype: {dtype}.''' ) UpperCAmelCase_ : Optional[int] = int(bit_search.groups()[0] ) return bit_size // 8 def lowerCamelCase__ ( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ): '''simple docstring''' if bloom_config_file == "": UpperCAmelCase_ : Tuple = BloomConfig() else: UpperCAmelCase_ : Optional[int] = BloomConfig.from_json_file(_lowercase ) if shard_model: UpperCAmelCase_ : Any = os.listdir(_lowercase ) UpperCAmelCase_ : Union[str, Any] = sorted(filter(lambda _lowercase : s.startswith('''layer''' ) and "model_00" in s , _lowercase ) ) UpperCAmelCase_ : Any = {'''weight_map''': {}, '''metadata''': {}} UpperCAmelCase_ : List[str] = 0 UpperCAmelCase_ : Any = None UpperCAmelCase_ : Optional[int] = BloomConfig() for j, file in enumerate(_lowercase ): print('''Processing file: {}'''.format(_lowercase ) ) UpperCAmelCase_ : Optional[Any] = None for i in range(_lowercase ): # load all TP files UpperCAmelCase_ : Tuple = file.replace('''model_00''' , f'''model_0{i}''' ) UpperCAmelCase_ : Any = torch.load(os.path.join(_lowercase , _lowercase ) , map_location='''cpu''' ) # Rename keys in the transformers names UpperCAmelCase_ : Dict = list(temp.keys() ) for key in keys: UpperCAmelCase_ : Union[str, Any] = temp.pop(_lowercase ) if tensors is None: UpperCAmelCase_ : Union[str, Any] = temp else: for key in tensors.keys(): if any(key.endswith(_lowercase ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel UpperCAmelCase_ : int = 1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks UpperCAmelCase_ : Tuple = torch.cat([tensors[key], temp[key]] , dim=_lowercase ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(_lowercase ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): UpperCAmelCase_ : List[str] = tensors[key] / pretraining_tp torch.save( _lowercase , os.path.join( _lowercase , '''pytorch_model_{}-of-{}.bin'''.format(str(j + 1 ).zfill(5 ) , str(len(_lowercase ) ).zfill(5 ) ) , ) , ) for key in tensors.keys(): UpperCAmelCase_ : Union[str, Any] = tensors[key] total_size += value.numel() * get_dtype_size(value.dtype ) if key not in index_dict["weight_map"]: UpperCAmelCase_ : List[str] = '''pytorch_model_{}-of-{}.bin'''.format( str(j + 1 ).zfill(5 ) , str(len(_lowercase ) ).zfill(5 ) ) UpperCAmelCase_ : List[Any] = BloomConfig() UpperCAmelCase_ : Tuple = pytorch_dump_folder_path + '''/''' + CONFIG_NAME UpperCAmelCase_ : List[str] = total_size with open(_lowercase , '''w''' , encoding='''utf-8''' ) as f: f.write(config.to_json_string() ) with open(os.path.join(_lowercase , WEIGHTS_NAME + '''.index.json''' ) , '''w''' , encoding='''utf-8''' ) as f: UpperCAmelCase_ : Optional[Any] = json.dumps(_lowercase , indent=2 , sort_keys=_lowercase ) + '''\n''' f.write(_lowercase ) else: UpperCAmelCase_ : Any = BloomModel(_lowercase ) UpperCAmelCase_ : Tuple = os.listdir(_lowercase ) UpperCAmelCase_ : Union[str, Any] = sorted(filter(lambda _lowercase : s.startswith('''layer''' ) and "model_00" in s , _lowercase ) ) UpperCAmelCase_ : Any = None for i, file in enumerate(_lowercase ): UpperCAmelCase_ : Optional[Any] = None for i in range(_lowercase ): # load all TP files UpperCAmelCase_ : List[Any] = file.replace('''model_00''' , f'''model_0{i}''' ) UpperCAmelCase_ : Optional[int] = torch.load(os.path.join(_lowercase , _lowercase ) , map_location='''cpu''' ) # Rename keys in the transformers names UpperCAmelCase_ : str = list(temp.keys() ) for key in keys: UpperCAmelCase_ : Dict = temp.pop(_lowercase ) if tensors is None: UpperCAmelCase_ : int = temp else: for key in tensors.keys(): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) if any(key.endswith(_lowercase ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel UpperCAmelCase_ : Optional[int] = 1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks UpperCAmelCase_ : List[str] = torch.cat([tensors[key], temp[key]] , dim=_lowercase ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(_lowercase ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): UpperCAmelCase_ : Dict = tensors[key] / pretraining_tp UpperCAmelCase_ : Tuple = model.load_state_dict(_lowercase , strict=_lowercase ) assert not other_keys.unexpected_keys, f'''The keys {other_keys.unexpected_keys} are unexpected''' if missing_keys is None: UpperCAmelCase_ : Union[str, Any] = set(other_keys.missing_keys ) else: UpperCAmelCase_ : Dict = missing_keys.intersection(set(other_keys.missing_keys ) ) assert not missing_keys, f'''The keys {missing_keys} are missing''' # Save pytorch-model os.makedirs(_lowercase , exist_ok=_lowercase ) UpperCAmelCase_ : str = pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME UpperCAmelCase_ : Dict = pytorch_dump_folder_path + '''/''' + CONFIG_NAME print(f'''Save PyTorch model to {pytorch_weights_dump_path} with dtype {config.torch_dtype}''' ) if config.torch_dtype is not None: UpperCAmelCase_ : Optional[int] = model.to(config.torch_dtype ) torch.save(model.state_dict() , _lowercase ) print(f'''Save configuration file to {pytorch_config_dump_path}''' ) with open(_lowercase , '''w''' , encoding='''utf-8''' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": __a = argparse.ArgumentParser() # Required parameters parser.add_argument( '--bloom_checkpoint_path', default=None, type=str, required=True, help='Path to the Megatron-LM checkpoint path.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) parser.add_argument( '--bloom_config_file', default='', type=str, help=( 'An optional config json file corresponding to the pre-trained model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--shard_model', action='store_true', help='An optional setting to shard the output model \nThis enables sharding the converted checkpoint', ) parser.add_argument( '--pretraining_tp', default=4, type=int, help='Pretraining TP rank that has been used when training the model in Megatron-LM \n', ) __a = parser.parse_args() convert_bloom_checkpoint_to_pytorch( args.bloom_checkpoint_path, args.bloom_config_file, args.pytorch_dump_folder_path, args.shard_model, args.pretraining_tp, )
30
0
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionTextToImagePipeline from diffusers.utils.testing_utils import nightly, require_torch_gpu, torch_device SCREAMING_SNAKE_CASE: str = False class lowercase_ (unittest.TestCase ): pass @nightly @require_torch_gpu class lowercase_ (unittest.TestCase ): def __a ( self : Dict ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def __a ( self : Optional[int] ): """simple docstring""" SCREAMING_SNAKE_CASE_ = VersatileDiffusionTextToImagePipeline.from_pretrained('shi-labs/versatile-diffusion' ) # remove text_unet pipe.remove_unused_weights() pipe.to(_SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = '''A painting of a squirrel eating a burger ''' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = VersatileDiffusionTextToImagePipeline.from_pretrained(_SCREAMING_SNAKE_CASE ) pipe.to(_SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = generator.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' ).images assert np.abs(image - new_image ).sum() < 1e-5, "Models don't have the same forward pass" def __a ( self : Optional[int] ): """simple docstring""" SCREAMING_SNAKE_CASE_ = VersatileDiffusionTextToImagePipeline.from_pretrained( 'shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(_SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = '''A painting of a squirrel eating a burger ''' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' ).images SCREAMING_SNAKE_CASE_ = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) SCREAMING_SNAKE_CASE_ = np.array([0.33_67, 0.31_69, 0.26_56, 0.38_70, 0.47_90, 0.37_96, 0.40_09, 0.48_78, 0.47_78] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
360
def lowerCamelCase__ ( ): '''simple docstring''' UpperCAmelCase_ : Dict = 0 for i in range(1 , 1001 ): total += i**i return str(_lowercase )[-10:] if __name__ == "__main__": print(solution())
30
0
"""simple docstring""" def A__ ( A__ , A__ , A__ ) -> Tuple: '''simple docstring''' return not any( neighbour == 1 and colored_vertices[i] == color for i, neighbour in enumerate(_lowercase ) ) def A__ ( A__ , A__ , A__ , A__ ) -> Optional[Any]: '''simple docstring''' if index == len(_lowercase ): return True # Recursive Step for i in range(_lowercase ): if valid_coloring(graph[index] , _lowercase , _lowercase ): # Color current vertex _UpperCAmelCase = i # Validate coloring if util_color(_lowercase , _lowercase , _lowercase , index + 1 ): return True # Backtrack _UpperCAmelCase = -1 return False def A__ ( A__ , A__ ) -> str: '''simple docstring''' _UpperCAmelCase = [-1] * len(_lowercase ) if util_color(_lowercase , _lowercase , _lowercase , 0 ): return colored_vertices return []
426
import os import sys import warnings from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen from ..table import array_cast from ..utils.file_utils import is_local_path from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: import PIL.Image from .features import FeatureType __a = None __a = '<' if sys.byteorder == 'little' else '>' # Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image __a = [ np.dtype('|b1'), np.dtype('|u1'), np.dtype('<u2'), np.dtype('>u2'), np.dtype('<i2'), np.dtype('>i2'), np.dtype('<u4'), np.dtype('>u4'), np.dtype('<i4'), np.dtype('>i4'), np.dtype('<f4'), np.dtype('>f4'), np.dtype('<f8'), np.dtype('>f8'), ] @dataclass class __a: """simple docstring""" lowerCAmelCase = True lowerCAmelCase = None # Automatically constructed lowerCAmelCase = "PIL.Image.Image" lowerCAmelCase = pa.struct({'''bytes''': pa.binary(), '''path''': pa.string()} ) lowerCAmelCase = field(default='''Image''' , init=_a , repr=_a ) def __call__( self ) -> Tuple: return self.pa_type def a__ ( self ,_SCREAMING_SNAKE_CASE ) -> dict: if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('''To support encoding images, please install \'Pillow\'.''' ) if isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : List[str] = np.array(_SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ): return {"path": value, "bytes": None} elif isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ): return {"path": None, "bytes": value} elif isinstance(_SCREAMING_SNAKE_CASE ,np.ndarray ): # convert the image array to PNG/TIFF bytes return encode_np_array(_SCREAMING_SNAKE_CASE ) elif isinstance(_SCREAMING_SNAKE_CASE ,PIL.Image.Image ): # convert the PIL image to bytes (default format is PNG/TIFF) return encode_pil_image(_SCREAMING_SNAKE_CASE ) elif value.get('''path''' ) is not None and os.path.isfile(value['''path'''] ): # we set "bytes": None to not duplicate the data if they're already available locally return {"bytes": None, "path": value.get('''path''' )} elif value.get('''bytes''' ) is not None or value.get('''path''' ) is not None: # store the image bytes, and path is used to infer the image format using the file extension return {"bytes": value.get('''bytes''' ), "path": value.get('''path''' )} else: raise ValueError( f'''An image sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.''' ) def a__ ( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE=None ) -> "PIL.Image.Image": if not self.decode: raise RuntimeError('''Decoding is disabled for this feature. Please use Image(decode=True) instead.''' ) if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('''To support decoding images, please install \'Pillow\'.''' ) if token_per_repo_id is None: UpperCAmelCase_ : Dict = {} UpperCAmelCase_, UpperCAmelCase_ : Union[str, Any] = value['''path'''], value['''bytes'''] if bytes_ is None: if path is None: raise ValueError(f'''An image should have one of \'path\' or \'bytes\' but both are None in {value}.''' ) else: if is_local_path(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Tuple = PIL.Image.open(_SCREAMING_SNAKE_CASE ) else: UpperCAmelCase_ : Dict = path.split('''::''' )[-1] try: UpperCAmelCase_ : Optional[int] = string_to_dict(_SCREAMING_SNAKE_CASE ,config.HUB_DATASETS_URL )['''repo_id'''] UpperCAmelCase_ : Tuple = token_per_repo_id.get(_SCREAMING_SNAKE_CASE ) except ValueError: UpperCAmelCase_ : Optional[Any] = None with xopen(_SCREAMING_SNAKE_CASE ,'''rb''' ,use_auth_token=_SCREAMING_SNAKE_CASE ) as f: UpperCAmelCase_ : List[str] = BytesIO(f.read() ) UpperCAmelCase_ : Optional[Any] = PIL.Image.open(bytes_ ) else: UpperCAmelCase_ : List[Any] = PIL.Image.open(BytesIO(bytes_ ) ) image.load() # to avoid "Too many open files" errors return image def a__ ( self ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value return ( self if self.decode else { "bytes": Value('''binary''' ), "path": Value('''string''' ), } ) def a__ ( self ,_SCREAMING_SNAKE_CASE ) -> pa.StructArray: if pa.types.is_string(storage.type ): UpperCAmelCase_ : Dict = pa.array([None] * len(_SCREAMING_SNAKE_CASE ) ,type=pa.binary() ) UpperCAmelCase_ : Dict = pa.StructArray.from_arrays([bytes_array, storage] ,['''bytes''', '''path'''] ,mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): UpperCAmelCase_ : List[str] = pa.array([None] * len(_SCREAMING_SNAKE_CASE ) ,type=pa.string() ) UpperCAmelCase_ : Tuple = pa.StructArray.from_arrays([storage, path_array] ,['''bytes''', '''path'''] ,mask=storage.is_null() ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index('''bytes''' ) >= 0: UpperCAmelCase_ : Dict = storage.field('''bytes''' ) else: UpperCAmelCase_ : Any = pa.array([None] * len(_SCREAMING_SNAKE_CASE ) ,type=pa.binary() ) if storage.type.get_field_index('''path''' ) >= 0: UpperCAmelCase_ : int = storage.field('''path''' ) else: UpperCAmelCase_ : List[str] = pa.array([None] * len(_SCREAMING_SNAKE_CASE ) ,type=pa.string() ) UpperCAmelCase_ : Optional[Any] = pa.StructArray.from_arrays([bytes_array, path_array] ,['''bytes''', '''path'''] ,mask=storage.is_null() ) elif pa.types.is_list(storage.type ): UpperCAmelCase_ : Optional[Any] = pa.array( [encode_np_array(np.array(_SCREAMING_SNAKE_CASE ) )['''bytes'''] if arr is not None else None for arr in storage.to_pylist()] ,type=pa.binary() ,) UpperCAmelCase_ : Any = pa.array([None] * len(_SCREAMING_SNAKE_CASE ) ,type=pa.string() ) UpperCAmelCase_ : Dict = pa.StructArray.from_arrays( [bytes_array, path_array] ,['''bytes''', '''path'''] ,mask=bytes_array.is_null() ) return array_cast(_SCREAMING_SNAKE_CASE ,self.pa_type ) def a__ ( self ,_SCREAMING_SNAKE_CASE ) -> pa.StructArray: @no_op_if_value_is_null def path_to_bytes(_SCREAMING_SNAKE_CASE ): with xopen(_SCREAMING_SNAKE_CASE ,'''rb''' ) as f: UpperCAmelCase_ : Any = f.read() return bytes_ UpperCAmelCase_ : Union[str, Any] = pa.array( [ (path_to_bytes(x['''path'''] ) if x['''bytes'''] is None else x['''bytes''']) if x is not None else None for x in storage.to_pylist() ] ,type=pa.binary() ,) UpperCAmelCase_ : List[str] = pa.array( [os.path.basename(_SCREAMING_SNAKE_CASE ) if path is not None else None for path in storage.field('''path''' ).to_pylist()] ,type=pa.string() ,) UpperCAmelCase_ : Union[str, Any] = pa.StructArray.from_arrays([bytes_array, path_array] ,['''bytes''', '''path'''] ,mask=bytes_array.is_null() ) return array_cast(_SCREAMING_SNAKE_CASE ,self.pa_type ) def lowerCamelCase__ ( ): '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('''To support encoding images, please install \'Pillow\'.''' ) global _IMAGE_COMPRESSION_FORMATS if _IMAGE_COMPRESSION_FORMATS is None: PIL.Image.init() UpperCAmelCase_ : Optional[int] = list(set(PIL.Image.OPEN.keys() ) & set(PIL.Image.SAVE.keys() ) ) return _IMAGE_COMPRESSION_FORMATS def lowerCamelCase__ ( _lowercase ): '''simple docstring''' UpperCAmelCase_ : Optional[int] = BytesIO() if image.format in list_image_compression_formats(): UpperCAmelCase_ : int = image.format else: UpperCAmelCase_ : List[Any] = '''PNG''' if image.mode in ['''1''', '''L''', '''LA''', '''RGB''', '''RGBA'''] else '''TIFF''' image.save(_lowercase , format=_lowercase ) return buffer.getvalue() def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if hasattr(_lowercase , '''filename''' ) and image.filename != "": return {"path": image.filename, "bytes": None} else: return {"path": None, "bytes": image_to_bytes(_lowercase )} def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('''To support encoding images, please install \'Pillow\'.''' ) UpperCAmelCase_ : Tuple = array.dtype UpperCAmelCase_ : List[str] = dtype.byteorder if dtype.byteorder != '''=''' else _NATIVE_BYTEORDER UpperCAmelCase_ : Dict = dtype.kind UpperCAmelCase_ : Union[str, Any] = dtype.itemsize UpperCAmelCase_ : Optional[Any] = None # Multi-channel array case (only np.dtype("|u1") is allowed) if array.shape[2:]: UpperCAmelCase_ : Tuple = np.dtype('''|u1''' ) if dtype_kind not in ["u", "i"]: raise TypeError( f'''Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays.''' ) if dtype is not dest_dtype: warnings.warn(f'''Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'''' ) # Exact match elif dtype in _VALID_IMAGE_ARRAY_DTPYES: UpperCAmelCase_ : Union[str, Any] = dtype else: # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually) while dtype_itemsize >= 1: UpperCAmelCase_ : Union[str, Any] = dtype_byteorder + dtype_kind + str(_lowercase ) UpperCAmelCase_ : str = np.dtype(_lowercase ) if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES: warnings.warn(f'''Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'''' ) break else: dtype_itemsize //= 2 if dest_dtype is None: raise TypeError( f'''Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}''' ) UpperCAmelCase_ : Any = PIL.Image.fromarray(array.astype(_lowercase ) ) return {"path": None, "bytes": image_to_bytes(_lowercase )} def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('''To support encoding images, please install \'Pillow\'.''' ) if objs: UpperCAmelCase_, UpperCAmelCase_ : Tuple = first_non_null_value(_lowercase ) if isinstance(_lowercase , _lowercase ): return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs] if isinstance(_lowercase , np.ndarray ): UpperCAmelCase_ : Any = no_op_if_value_is_null(_lowercase ) return [obj_to_image_dict_func(_lowercase ) for obj in objs] elif isinstance(_lowercase , PIL.Image.Image ): UpperCAmelCase_ : Union[str, Any] = no_op_if_value_is_null(_lowercase ) return [obj_to_image_dict_func(_lowercase ) for obj in objs] else: return objs else: return objs
30
0
'''simple docstring''' import argparse from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import BigBirdPegasusConfig, BigBirdPegasusForConditionalGeneration __lowercase : Any = [ # tf -> hf ('/', '.'), ('layer_', 'layers.'), ('kernel', 'weight'), ('beta', 'bias'), ('gamma', 'weight'), ('pegasus', 'model'), ] __lowercase : Dict = [ ('.output.dense', '.fc2'), ('intermediate.LayerNorm', 'final_layer_norm'), ('intermediate.dense', 'fc1'), ] __lowercase : List[Any] = ( INIT_COMMON + [ ('attention.self.LayerNorm', 'self_attn_layer_norm'), ('attention.output.dense', 'self_attn.out_proj'), ('attention.self', 'self_attn'), ('attention.encdec.LayerNorm', 'encoder_attn_layer_norm'), ('attention.encdec_output.dense', 'encoder_attn.out_proj'), ('attention.encdec', 'encoder_attn'), ('key', 'k_proj'), ('value', 'v_proj'), ('query', 'q_proj'), ('decoder.LayerNorm', 'decoder.layernorm_embedding'), ] + END_COMMON ) __lowercase : Optional[Any] = ( INIT_COMMON + [ ('embeddings.word_embeddings', 'shared.weight'), ('embeddings.position_embeddings', 'embed_positions.weight'), ('attention.self.LayerNorm', 'self_attn_layer_norm'), ('attention.output.dense', 'self_attn.output'), ('attention.self', 'self_attn.self'), ('encoder.LayerNorm', 'encoder.layernorm_embedding'), ] + END_COMMON ) __lowercase : Union[str, Any] = [ 'encdec/key/bias', 'encdec/query/bias', 'encdec/value/bias', 'self/key/bias', 'self/query/bias', 'self/value/bias', 'encdec_output/dense/bias', 'attention/output/dense/bias', ] def lowerCamelCase (_SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : Any ): for tf_name, hf_name in patterns: __a : int = k.replace(_lowercase , _lowercase ) return k def lowerCamelCase (_SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : str ): __a : Union[str, Any] = BigBirdPegasusConfig(**_lowercase ) __a : Union[str, Any] = BigBirdPegasusForConditionalGeneration(_lowercase ) __a : Union[str, Any] = torch_model.state_dict() __a : Dict = {} # separating decoder weights __a : Any = {k: tf_weights[k] for k in tf_weights if k.startswith('pegasus/decoder' )} __a : Optional[Any] = {k: tf_weights[k] for k in tf_weights if not k.startswith('pegasus/decoder' )} for k, v in tqdm(decoder_weights.items() , 'tf -> hf conversion' ): __a : Optional[Any] = [k.endswith(_lowercase ) for ending in KEYS_TO_IGNORE] if any(_lowercase ): continue __a : Optional[int] = DECODER_PATTERNS __a : Union[str, Any] = rename_state_dict_key(_lowercase , _lowercase ) if new_k not in state_dict: raise ValueError(F"""could not find new key {new_k} in state dict. (converted from {k})""" ) if any(True if i in k else False for i in ['dense', 'query', 'key', 'value'] ): __a : int = v.T __a : List[Any] = torch.from_numpy(_lowercase ) assert v.shape == state_dict[new_k].shape, F"""{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}""" for k, v in tqdm(remaining_weights.items() , 'tf -> hf conversion' ): __a : Optional[int] = [k.endswith(_lowercase ) for ending in KEYS_TO_IGNORE] if any(_lowercase ): continue __a : str = REMAINING_PATTERNS __a : Union[str, Any] = rename_state_dict_key(_lowercase , _lowercase ) if new_k not in state_dict and k != "pegasus/embeddings/position_embeddings": raise ValueError(F"""could not find new key {new_k} in state dict. (converted from {k})""" ) if any(True if i in k else False for i in ['dense', 'query', 'key', 'value'] ): __a : int = v.T __a : Tuple = torch.from_numpy(_lowercase ) if k != "pegasus/embeddings/position_embeddings": assert v.shape == state_dict[new_k].shape, F"""{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}""" __a : Tuple = mapping['''model.embed_positions.weight'''] __a : str = mapping.pop('model.embed_positions.weight' ) __a : List[Any] = torch_model.load_state_dict(_lowercase , strict=_lowercase ) __a : str = [ k for k in missing if k not in [ '''final_logits_bias''', '''model.encoder.embed_tokens.weight''', '''model.decoder.embed_tokens.weight''', '''lm_head.weight''', ] ] assert unexpected_missing == [], F"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], F"""no matches found for the following tf keys {extra}""" return torch_model def lowerCamelCase (_SCREAMING_SNAKE_CASE : Dict ): __a : List[str] = tf.train.list_variables(_lowercase ) __a : List[Any] = {} __a : List[str] = ['''global_step'''] for name, shape in tqdm(_lowercase , desc='converting tf checkpoint to dict' ): __a : Any = any(pat in name for pat in ignore_name ) if skip_key: continue __a : Tuple = tf.train.load_variable(_lowercase , _lowercase ) __a : List[Any] = array return tf_weights def lowerCamelCase (_SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : int ): __a : Dict = get_tf_weights_as_numpy(_lowercase ) __a : Any = convert_bigbird_pegasus(_lowercase , _lowercase ) torch_model.save_pretrained(_lowercase ) if __name__ == "__main__": __lowercase : Dict = argparse.ArgumentParser() parser.add_argument('--tf_ckpt_path', type=str, help='passed to tf.train.list_variables') parser.add_argument('--save_dir', default=None, type=str, help='Path to the output PyTorch model.') __lowercase : Union[str, Any] = parser.parse_args() __lowercase : Tuple = {} convert_bigbird_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir, config_update=config_update)
476
import unittest from transformers import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision, slow, torch_device if is_torch_available(): import torch from transformers import AutoModelForImageClassification if is_vision_available(): from transformers import AutoImageProcessor @require_torch @require_vision class __a( unittest.TestCase ): """simple docstring""" @slow def a__ ( self ) -> List[str]: UpperCAmelCase_ : Tuple = AutoImageProcessor.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip''' ) UpperCAmelCase_ : Union[str, Any] = AutoModelForImageClassification.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip''' ) model.to(_SCREAMING_SNAKE_CASE ) from datasets import load_dataset UpperCAmelCase_ : Optional[int] = load_dataset('''nielsr/rvlcdip-demo''' ) UpperCAmelCase_ : Optional[Any] = dataset['''train'''][0]['''image'''].convert('''RGB''' ) UpperCAmelCase_ : str = image_processor(_SCREAMING_SNAKE_CASE ,return_tensors='''pt''' ).to(_SCREAMING_SNAKE_CASE ) # forward pass with torch.no_grad(): UpperCAmelCase_ : Optional[int] = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = outputs.logits UpperCAmelCase_ : Tuple = torch.Size((1, 16) ) self.assertEqual(logits.shape ,_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = torch.tensor( [-0.41_58, -0.40_92, -0.43_47] ,device=_SCREAMING_SNAKE_CASE ,dtype=torch.float ,) self.assertTrue(torch.allclose(logits[0, :3] ,_SCREAMING_SNAKE_CASE ,atol=1e-4 ) )
30
0
def __lowerCAmelCase ( _UpperCamelCase , _UpperCamelCase ) -> int: '''simple docstring''' return "\n".join( f"""{number} * {i} = {number * i}""" for i in range(1 , number_of_terms + 1 ) ) if __name__ == "__main__": print(multiplication_table(number=5, number_of_terms=10))
306
import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor __a = logging.get_logger(__name__) class __a( _a ): """simple docstring""" def __init__( self ,*_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ) -> None: warnings.warn( '''The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use CLIPImageProcessor instead.''' ,_SCREAMING_SNAKE_CASE ,) super().__init__(*_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE )
30
0
import argparse import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( CLIPTokenizer, CLIPTokenizerFast, VideoMAEImageProcessor, XCLIPConfig, XCLIPModel, XCLIPProcessor, XCLIPTextConfig, XCLIPVisionConfig, ) def __lowerCAmelCase ( __magic_name__ , __magic_name__ ): _lowercase: Union[str, Any] = XCLIPTextConfig() # derive patch size from model name _lowercase: List[str] = model_name.find("patch" ) _lowercase: Optional[int] = int(model_name[start_idx + len("patch" ) : start_idx + len("patch" ) + 2] ) _lowercase: List[str] = XCLIPVisionConfig(patch_size=_lowercase , num_frames=_lowercase ) if "large" in model_name: _lowercase: Optional[int] = 7_6_8 _lowercase: Dict = 3_0_7_2 _lowercase: Dict = 1_2 _lowercase: str = 1_0_2_4 _lowercase: Any = 4_0_9_6 _lowercase: Union[str, Any] = 1_6 _lowercase: Optional[Any] = 2_4 _lowercase: List[Any] = 7_6_8 _lowercase: Dict = 3_0_7_2 if model_name == "xclip-large-patch14-16-frames": _lowercase: int = 3_3_6 _lowercase: List[Any] = XCLIPConfig.from_text_vision_configs(_lowercase , _lowercase ) if "large" in model_name: _lowercase: str = 7_6_8 return config def __lowerCAmelCase ( __magic_name__ ): if name == "token_embedding.weight": _lowercase: Optional[int] = name.replace("token_embedding.weight" , "text_model.embeddings.token_embedding.weight" ) if name == "positional_embedding": _lowercase: Any = name.replace("positional_embedding" , "text_model.embeddings.position_embedding.weight" ) if "ln_1" in name: _lowercase: Any = name.replace("ln_1" , "layer_norm1" ) if "ln_2" in name: _lowercase: List[str] = name.replace("ln_2" , "layer_norm2" ) if "c_fc" in name: _lowercase: List[Any] = name.replace("c_fc" , "fc1" ) if "c_proj" in name: _lowercase: Any = name.replace("c_proj" , "fc2" ) if name.startswith("transformer.resblocks" ): _lowercase: int = name.replace("transformer.resblocks" , "text_model.encoder.layers" ) if "attn.out_proj" in name and "message" not in name: _lowercase: str = name.replace("attn.out_proj" , "self_attn.out_proj" ) if "ln_final" in name: _lowercase: List[str] = name.replace("ln_final" , "text_model.final_layer_norm" ) # visual encoder if name == "visual.class_embedding": _lowercase: List[str] = name.replace("visual.class_embedding" , "vision_model.embeddings.class_embedding" ) if name == "visual.positional_embedding": _lowercase: Optional[Any] = name.replace("visual.positional_embedding" , "vision_model.embeddings.position_embedding.weight" ) if name.startswith("visual.transformer.resblocks" ): _lowercase: Union[str, Any] = name.replace("visual.transformer.resblocks" , "vision_model.encoder.layers" ) if "visual.conv1" in name: _lowercase: Dict = name.replace("visual.conv1" , "vision_model.embeddings.patch_embedding" ) if "visual.ln_pre" in name: _lowercase: List[Any] = name.replace("visual.ln_pre" , "vision_model.pre_layernorm" ) if "visual.ln_post" in name: _lowercase: Optional[int] = name.replace("visual.ln_post" , "vision_model.post_layernorm" ) if "visual.proj" in name: _lowercase: str = name.replace("visual.proj" , "visual_projection.weight" ) if "text_projection" in name: _lowercase: Optional[int] = name.replace("text_projection" , "text_projection.weight" ) # things on top if "prompts_visual_proj" in name: _lowercase: List[str] = name.replace("prompts_visual_proj" , "prompts_visual_projection" ) if "prompts_visual_ln" in name: _lowercase: List[str] = name.replace("prompts_visual_ln" , "prompts_visual_layernorm" ) # mit if name == "mit.positional_embedding": _lowercase: int = name.replace("positional" , "position" ) if name.startswith("mit.resblocks" ): _lowercase: Optional[int] = name.replace("mit.resblocks" , "mit.encoder.layers" ) # prompts generator if name.startswith("prompts_generator.norm" ): _lowercase: Optional[int] = name.replace("prompts_generator.norm" , "prompts_generator.layernorm" ) return name def __lowerCAmelCase ( __magic_name__ , __magic_name__ ): for key in orig_state_dict.copy().keys(): _lowercase: Any = orig_state_dict.pop(_lowercase ) if "attn.in_proj" in key: _lowercase: Optional[int] = key.split("." ) if key.startswith("visual" ): _lowercase: List[Any] = key_split[3] _lowercase: Optional[int] = config.vision_config.hidden_size if "message_attn" in key: if "weight" in key: _lowercase: List[str] = val[ :dim, : ] _lowercase: Optional[int] = val[ dim : dim * 2, : ] _lowercase: Optional[int] = val[ -dim:, : ] else: _lowercase: List[Any] = val[ :dim ] _lowercase: Any = val[ dim : dim * 2 ] _lowercase: List[str] = val[ -dim: ] else: if "weight" in key: _lowercase: Optional[int] = val[ :dim, : ] _lowercase: List[Any] = val[ dim : dim * 2, : ] _lowercase: List[str] = val[ -dim:, : ] else: _lowercase: List[Any] = val[:dim] _lowercase: List[Any] = val[ dim : dim * 2 ] _lowercase: Optional[Any] = val[-dim:] elif key.startswith("mit" ): _lowercase: Optional[Any] = key_split[2] _lowercase: Tuple = config.vision_config.mit_hidden_size if "weight" in key: _lowercase: Optional[Any] = val[:dim, :] _lowercase: Optional[int] = val[dim : dim * 2, :] _lowercase: Optional[int] = val[-dim:, :] else: _lowercase: List[str] = val[:dim] _lowercase: List[Any] = val[dim : dim * 2] _lowercase: int = val[-dim:] else: _lowercase: List[str] = key_split[2] _lowercase: List[str] = config.text_config.hidden_size if "weight" in key: _lowercase: Tuple = val[:dim, :] _lowercase: Any = val[ dim : dim * 2, : ] _lowercase: str = val[-dim:, :] else: _lowercase: Optional[int] = val[:dim] _lowercase: Tuple = val[ dim : dim * 2 ] _lowercase: List[Any] = val[-dim:] else: _lowercase: int = rename_key(_lowercase ) if new_key_name in ["visual_projection.weight", "text_projection.weight"]: _lowercase: Dict = val.T _lowercase: Tuple = val return orig_state_dict def __lowerCAmelCase ( __magic_name__ ): if num_frames == 8: _lowercase: Tuple = '''eating_spaghetti_8_frames.npy''' elif num_frames == 1_6: _lowercase: Union[str, Any] = '''eating_spaghetti.npy''' elif num_frames == 3_2: _lowercase: Optional[Any] = '''eating_spaghetti_32_frames.npy''' _lowercase: List[Any] = hf_hub_download( repo_id="hf-internal-testing/spaghetti-video" , filename=_lowercase , repo_type="dataset" , ) _lowercase: Union[str, Any] = np.load(_lowercase ) return list(_lowercase ) def __lowerCAmelCase ( __magic_name__ , __magic_name__=None , __magic_name__=False ): _lowercase: Optional[Any] = { # fully supervised kinetics-400 checkpoints '''xclip-base-patch32''': '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_8.pth''', '''xclip-base-patch32-16-frames''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_16.pth''' ), '''xclip-base-patch16''': '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_8.pth''', '''xclip-base-patch16-16-frames''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_16.pth''' ), '''xclip-large-patch14''': '''https://drive.google.com/u/0/uc?id=1NUOImq0o5DlQTST17iIP3vG7DgmHQuCx&amp;export=download&amp;confirm=t&amp;uuid=b26caedc-88e2-473e-830a-9d158b653cdb''', '''xclip-large-patch14-16-frames''': '''https://drive.google.com/u/0/uc?id=1FOYgnJc097OJ4lGwtRCCydQyVPJEOH7d&amp;export=download&amp;confirm=t&amp;uuid=538fa810-e671-4050-b385-9a623f89804f''', # fully supervised kinetics-600 checkpoints '''xclip-base-patch16-kinetics-600''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_8.pth''' ), '''xclip-base-patch16-kinetics-600-16-frames''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_16.pth''' ), '''xclip-large-patch14-kinetics-600''': '''https://drive.google.com/u/0/uc?id=1FV8C1INuM91sLAN4ImjzePLIlpMSihwV&amp;export=download&amp;confirm=t&amp;uuid=141d4977-4a65-44ae-864f-4b0c19f838be''', # few shot '''xclip-base-patch16-hmdb-2-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_2.pth''' ), '''xclip-base-patch16-hmdb-4-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_4.pth''' ), '''xclip-base-patch16-hmdb-8-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_8.pth''' ), '''xclip-base-patch16-hmdb-16-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_16.pth''' ), '''xclip-base-patch16-ucf-2-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_2.pth''' ), '''xclip-base-patch16-ucf-4-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_4.pth''' ), '''xclip-base-patch16-ucf-8-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_8.pth''' ), '''xclip-base-patch16-ucf-16-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_16.pth''' ), # zero shot '''xclip-base-patch16-zero-shot''': '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/zero.pth''', } _lowercase: Optional[int] = model_to_url[model_name] _lowercase: Dict = 8 if "16-frames" in model_name: _lowercase: List[Any] = 1_6 elif "shot" in model_name: _lowercase: Tuple = 3_2 _lowercase: Any = get_xclip_config(_lowercase , _lowercase ) _lowercase: str = XCLIPModel(_lowercase ) model.eval() if "drive" in checkpoint_url: _lowercase: int = '''pytorch_model.bin''' gdown.cached_download(_lowercase , _lowercase , quiet=_lowercase ) _lowercase: Union[str, Any] = torch.load(_lowercase , map_location="cpu" )['''model'''] else: _lowercase: Optional[int] = torch.hub.load_state_dict_from_url(_lowercase )['''model'''] _lowercase: Dict = convert_state_dict(_lowercase , _lowercase ) _lowercase: int = XCLIPModel(_lowercase ) _lowercase: List[Any] = model.load_state_dict(_lowercase , strict=_lowercase ) assert missing_keys == ["text_model.embeddings.position_ids", "vision_model.embeddings.position_ids"] model.eval() _lowercase: Optional[Any] = 3_3_6 if model_name == '''xclip-large-patch14-16-frames''' else 2_2_4 _lowercase: List[str] = VideoMAEImageProcessor(size=_lowercase ) _lowercase: Tuple = CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch32" ) _lowercase: Tuple = CLIPTokenizerFast.from_pretrained("openai/clip-vit-base-patch32" ) _lowercase: List[Any] = XCLIPProcessor(image_processor=_lowercase , tokenizer=_lowercase ) _lowercase: Dict = prepare_video(_lowercase ) _lowercase: List[str] = processor( text=["playing sports", "eating spaghetti", "go shopping"] , videos=_lowercase , return_tensors="pt" , padding=_lowercase ) print("Shape of pixel values:" , inputs.pixel_values.shape ) with torch.no_grad(): _lowercase: str = model(**_lowercase ) # Verify outputs _lowercase: Dict = outputs.logits_per_video _lowercase: Union[str, Any] = logits_per_video.softmax(dim=1 ) print("Probs:" , _lowercase ) # kinetics-400 if model_name == "xclip-base-patch32": _lowercase: Tuple = torch.tensor([[0.0_019, 0.9_951, 0.0_030]] ) elif model_name == "xclip-base-patch32-16-frames": _lowercase: Any = torch.tensor([[7.0999e-04, 9.9883e-01, 4.5580e-04]] ) elif model_name == "xclip-base-patch16": _lowercase: Tuple = torch.tensor([[0.0_083, 0.9_681, 0.0_236]] ) elif model_name == "xclip-base-patch16-16-frames": _lowercase: Union[str, Any] = torch.tensor([[7.6937e-04, 9.9728e-01, 1.9473e-03]] ) elif model_name == "xclip-large-patch14": _lowercase: str = torch.tensor([[0.0_062, 0.9_864, 0.0_075]] ) elif model_name == "xclip-large-patch14-16-frames": _lowercase: Optional[int] = torch.tensor([[3.3877e-04, 9.9937e-01, 2.8888e-04]] ) # kinetics-600 elif model_name == "xclip-base-patch16-kinetics-600": _lowercase: Optional[int] = torch.tensor([[0.0_555, 0.8_914, 0.0_531]] ) elif model_name == "xclip-base-patch16-kinetics-600-16-frames": _lowercase: List[str] = torch.tensor([[3.8554e-04, 9.9929e-01, 3.2754e-04]] ) elif model_name == "xclip-large-patch14-kinetics-600": _lowercase: Optional[Any] = torch.tensor([[0.0_036, 0.9_920, 0.0_045]] ) # few shot elif model_name == "xclip-base-patch16-hmdb-2-shot": _lowercase: List[Any] = torch.tensor([[7.1890e-06, 9.9994e-01, 5.6559e-05]] ) elif model_name == "xclip-base-patch16-hmdb-4-shot": _lowercase: str = torch.tensor([[1.0320e-05, 9.9993e-01, 6.2435e-05]] ) elif model_name == "xclip-base-patch16-hmdb-8-shot": _lowercase: List[str] = torch.tensor([[4.1377e-06, 9.9990e-01, 9.8386e-05]] ) elif model_name == "xclip-base-patch16-hmdb-16-shot": _lowercase: Union[str, Any] = torch.tensor([[4.1347e-05, 9.9962e-01, 3.3411e-04]] ) elif model_name == "xclip-base-patch16-ucf-2-shot": _lowercase: Dict = torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]] ) elif model_name == "xclip-base-patch16-ucf-4-shot": _lowercase: Union[str, Any] = torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]] ) elif model_name == "xclip-base-patch16-ucf-8-shot": _lowercase: Optional[Any] = torch.tensor([[0.0_027, 0.9_904, 0.0_070]] ) elif model_name == "xclip-base-patch16-ucf-16-shot": _lowercase: Any = torch.tensor([[9.8219e-04, 9.9593e-01, 3.0863e-03]] ) # zero shot elif model_name == "xclip-base-patch16-zero-shot": _lowercase: List[str] = torch.tensor([[3.5082e-04, 9.9785e-01, 1.7966e-03]] ) else: raise ValueError(f"Model name {model_name} not supported" ) assert torch.allclose(_lowercase , _lowercase , atol=1e-3 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: print(f"Saving model {model_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(_lowercase ) if push_to_hub: print("Pushing model, processor and slow tokenizer files to the hub..." ) model.push_to_hub(_lowercase , organization="nielsr" ) processor.push_to_hub(_lowercase , organization="nielsr" ) slow_tokenizer.push_to_hub(_lowercase , organization="nielsr" ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='xclip-base-patch32', type=str, help='Name of the model.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) _SCREAMING_SNAKE_CASE : List[str] = parser.parse_args() convert_xclip_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
226
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 PoolFormerImageProcessor class __a( unittest.TestCase ): """simple docstring""" def __init__( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE=7 ,_SCREAMING_SNAKE_CASE=3 ,_SCREAMING_SNAKE_CASE=30 ,_SCREAMING_SNAKE_CASE=400 ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=None ,_SCREAMING_SNAKE_CASE=0.9 ,_SCREAMING_SNAKE_CASE=None ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5] ,_SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5] ,) -> Optional[int]: UpperCAmelCase_ : int = size if size is not None else {'''shortest_edge''': 30} UpperCAmelCase_ : List[str] = crop_size if crop_size is not None else {'''height''': 30, '''width''': 30} UpperCAmelCase_ : Dict = parent UpperCAmelCase_ : int = batch_size UpperCAmelCase_ : int = num_channels UpperCAmelCase_ : Any = min_resolution UpperCAmelCase_ : Tuple = max_resolution UpperCAmelCase_ : Optional[int] = do_resize_and_center_crop UpperCAmelCase_ : Tuple = size UpperCAmelCase_ : List[str] = crop_pct UpperCAmelCase_ : List[str] = crop_size UpperCAmelCase_ : Any = do_normalize UpperCAmelCase_ : str = image_mean UpperCAmelCase_ : List[Any] = image_std def a__ ( self ) -> str: return { "size": self.size, "do_resize_and_center_crop": self.do_resize_and_center_crop, "crop_pct": self.crop_pct, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class __a( _a , unittest.TestCase ): """simple docstring""" lowerCAmelCase = PoolFormerImageProcessor if is_vision_available() else None def a__ ( self ) -> Dict: UpperCAmelCase_ : str = PoolFormerImageProcessingTester(self ) @property def a__ ( self ) -> Optional[int]: return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self ) -> Optional[int]: UpperCAmelCase_ : List[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''do_resize_and_center_crop''' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''size''' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''crop_pct''' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''do_normalize''' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''image_mean''' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''image_std''' ) ) def a__ ( self ) -> Union[str, Any]: UpperCAmelCase_ : Any = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{'''shortest_edge''': 30} ) self.assertEqual(image_processor.crop_size ,{'''height''': 30, '''width''': 30} ) UpperCAmelCase_ : Optional[Any] = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ,crop_size=84 ) self.assertEqual(image_processor.size ,{'''shortest_edge''': 42} ) self.assertEqual(image_processor.crop_size ,{'''height''': 84, '''width''': 84} ) def a__ ( self ) -> Optional[int]: pass def a__ ( self ) -> Dict: # Initialize image_processing UpperCAmelCase_ : str = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCAmelCase_ : int = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE ,Image.Image ) # Test not batched input UpperCAmelCase_ : 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 UpperCAmelCase_ : Union[str, Any] = image_processing(_SCREAMING_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 a__ ( self ) -> List[Any]: # Initialize image_processing UpperCAmelCase_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCAmelCase_ : List[str] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_SCREAMING_SNAKE_CASE ,numpify=_SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE ,np.ndarray ) # Test not batched input UpperCAmelCase_ : Tuple = 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 UpperCAmelCase_ : List[Any] = image_processing(_SCREAMING_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 a__ ( self ) -> Union[str, Any]: # Initialize image_processing UpperCAmelCase_ : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCAmelCase_ : Optional[Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_SCREAMING_SNAKE_CASE ,torchify=_SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE ,torch.Tensor ) # Test not batched input UpperCAmelCase_ : Tuple = 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 UpperCAmelCase_ : List[Any] = image_processing(_SCREAMING_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'''], ) ,)
30
0
def a ( snake_case__: Tuple = 1_000 ): '''simple docstring''' return sum(2 * a * ((a - 1) // 2) for a in range(3 , n + 1 ) ) if __name__ == "__main__": print(solution())
97
import unittest import numpy as np def lowerCamelCase__ ( _lowercase , _lowercase , _lowercase , _lowercase = None , ): '''simple docstring''' UpperCAmelCase_ : Dict = np.shape(_lowercase ) UpperCAmelCase_ : Optional[Any] = np.shape(_lowercase ) UpperCAmelCase_ : Tuple = np.shape(_lowercase ) if shape_a[0] != shape_b[0]: UpperCAmelCase_ : Tuple = ( '''Expected the same number of rows for A and B. ''' f'''Instead found A of size {shape_a} and B of size {shape_b}''' ) raise ValueError(_lowercase ) if shape_b[1] != shape_c[1]: UpperCAmelCase_ : List[Any] = ( '''Expected the same number of columns for B and C. ''' f'''Instead found B of size {shape_b} and C of size {shape_c}''' ) raise ValueError(_lowercase ) UpperCAmelCase_ : Dict = pseudo_inv if a_inv is None: try: UpperCAmelCase_ : Any = np.linalg.inv(_lowercase ) except np.linalg.LinAlgError: raise ValueError( '''Input matrix A is not invertible. Cannot compute Schur complement.''' ) return mat_c - mat_b.T @ a_inv @ mat_b class __a( unittest.TestCase ): """simple docstring""" def a__ ( self ) -> None: UpperCAmelCase_ : str = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase_ : Any = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase_ : List[str] = np.array([[2, 1], [6, 3]] ) UpperCAmelCase_ : Tuple = schur_complement(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = np.block([[a, b], [b.T, c]] ) UpperCAmelCase_ : List[Any] = np.linalg.det(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = np.linalg.det(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Any = np.linalg.det(_SCREAMING_SNAKE_CASE ) self.assertAlmostEqual(_SCREAMING_SNAKE_CASE ,det_a * det_s ) def a__ ( self ) -> None: UpperCAmelCase_ : str = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase_ : Optional[int] = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase_ : Optional[int] = np.array([[2, 1], [6, 3]] ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): schur_complement(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) def a__ ( self ) -> None: UpperCAmelCase_ : Optional[int] = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase_ : Optional[Any] = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase_ : int = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): schur_complement(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
30
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) SCREAMING_SNAKE_CASE = { """configuration_rembert""": ["""REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """RemBertConfig""", """RemBertOnnxConfig"""] } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE = ["""RemBertTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE = ["""RemBertTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE = [ """REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """RemBertForCausalLM""", """RemBertForMaskedLM""", """RemBertForMultipleChoice""", """RemBertForQuestionAnswering""", """RemBertForSequenceClassification""", """RemBertForTokenClassification""", """RemBertLayer""", """RemBertModel""", """RemBertPreTrainedModel""", """load_tf_weights_in_rembert""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE = [ """TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFRemBertForCausalLM""", """TFRemBertForMaskedLM""", """TFRemBertForMultipleChoice""", """TFRemBertForQuestionAnswering""", """TFRemBertForSequenceClassification""", """TFRemBertForTokenClassification""", """TFRemBertLayer""", """TFRemBertModel""", """TFRemBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert import RemBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert_fast import RemBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rembert import ( REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, RemBertForCausalLM, RemBertForMaskedLM, RemBertForMultipleChoice, RemBertForQuestionAnswering, RemBertForSequenceClassification, RemBertForTokenClassification, RemBertLayer, RemBertModel, RemBertPreTrainedModel, load_tf_weights_in_rembert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rembert import ( TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFRemBertForCausalLM, TFRemBertForMaskedLM, TFRemBertForMultipleChoice, TFRemBertForQuestionAnswering, TFRemBertForSequenceClassification, TFRemBertForTokenClassification, TFRemBertLayer, TFRemBertModel, TFRemBertPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
199
__a = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if not isinstance(_lowercase , _lowercase ): UpperCAmelCase_ : Union[str, Any] = f'''a bytes-like object is required, not \'{data.__class__.__name__}\'''' raise TypeError(_lowercase ) UpperCAmelCase_ : Any = ''''''.join(bin(_lowercase )[2:].zfill(8 ) for byte in data ) UpperCAmelCase_ : Any = len(_lowercase ) % 6 != 0 if padding_needed: # The padding that will be added later UpperCAmelCase_ : Union[str, Any] = B'''=''' * ((6 - len(_lowercase ) % 6) // 2) # Append binary_stream with arbitrary binary digits (0's by default) to make its # length a multiple of 6. binary_stream += "0" * (6 - len(_lowercase ) % 6) else: UpperCAmelCase_ : int = B'''''' # Encode every 6 binary digits to their corresponding Base64 character return ( "".join( B64_CHARSET[int(binary_stream[index : index + 6] , 2 )] for index in range(0 , len(_lowercase ) , 6 ) ).encode() + padding ) def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if not isinstance(_lowercase , _lowercase ) and not isinstance(_lowercase , _lowercase ): UpperCAmelCase_ : Tuple = ( '''argument should be a bytes-like object or ASCII string, ''' f'''not \'{encoded_data.__class__.__name__}\'''' ) raise TypeError(_lowercase ) # In case encoded_data is a bytes-like object, make sure it contains only # ASCII characters so we convert it to a string object if isinstance(_lowercase , _lowercase ): try: UpperCAmelCase_ : Any = encoded_data.decode('''utf-8''' ) except UnicodeDecodeError: raise ValueError('''base64 encoded data should only contain ASCII characters''' ) UpperCAmelCase_ : str = encoded_data.count('''=''' ) # Check if the encoded string contains non base64 characters if padding: assert all( char in B64_CHARSET for char in encoded_data[:-padding] ), "Invalid base64 character(s) found." else: assert all( char in B64_CHARSET for char in encoded_data ), "Invalid base64 character(s) found." # Check the padding assert len(_lowercase ) % 4 == 0 and padding < 3, "Incorrect padding" if padding: # Remove padding if there is one UpperCAmelCase_ : List[Any] = encoded_data[:-padding] UpperCAmelCase_ : List[Any] = ''''''.join( bin(B64_CHARSET.index(_lowercase ) )[2:].zfill(6 ) for char in encoded_data )[: -padding * 2] else: UpperCAmelCase_ : Tuple = ''''''.join( bin(B64_CHARSET.index(_lowercase ) )[2:].zfill(6 ) for char in encoded_data ) UpperCAmelCase_ : str = [ int(binary_stream[index : index + 8] , 2 ) for index in range(0 , len(_lowercase ) , 8 ) ] return bytes(_lowercase ) if __name__ == "__main__": import doctest doctest.testmod()
30
0
__lowerCAmelCase : str =[0, 2, 4, 6, 8] __lowerCAmelCase : Tuple =[1, 3, 5, 7, 9] def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , lowercase__ ): if remaining_length == 0: if digits[0] == 0 or digits[-1] == 0: return 0 for i in range(length // 2 - 1 , -1 , -1 ): remainder += digits[i] + digits[length - i - 1] if remainder % 2 == 0: return 0 remainder //= 10 return 1 if remaining_length == 1: if remainder % 2 == 0: return 0 __SCREAMING_SNAKE_CASE : Any = 0 for digit in range(10 ): __SCREAMING_SNAKE_CASE : int = digit result += reversible_numbers( 0 , (remainder + 2 * digit) // 10 , _lowercase , _lowercase ) return result __SCREAMING_SNAKE_CASE : Optional[int] = 0 for digita in range(10 ): __SCREAMING_SNAKE_CASE : Tuple = digita if (remainder + digita) % 2 == 0: __SCREAMING_SNAKE_CASE : Union[str, Any] = ODD_DIGITS else: __SCREAMING_SNAKE_CASE : int = EVEN_DIGITS for digita in other_parity_digits: __SCREAMING_SNAKE_CASE : Any = digita result += reversible_numbers( remaining_length - 2 , (remainder + digita + digita) // 10 , _lowercase , _lowercase , ) return result def _UpperCamelCase ( lowercase__ = 9 ): __SCREAMING_SNAKE_CASE : List[Any] = 0 for length in range(1 , max_power + 1 ): result += reversible_numbers(_lowercase , 0 , [0] * length , _lowercase ) return result if __name__ == "__main__": print(f"""{solution() = }""")
696
import os import shutil import sys import tempfile import unittest from pathlib import Path import pytest import transformers from transformers import ( BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoTokenizer, BertConfig, BertTokenizer, BertTokenizerFast, CTRLTokenizer, GPTaTokenizer, GPTaTokenizerFast, PreTrainedTokenizerFast, RobertaTokenizer, RobertaTokenizerFast, is_tokenizers_available, ) from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig from transformers.models.auto.tokenization_auto import ( TOKENIZER_MAPPING, get_tokenizer_config, tokenizer_class_from_name, ) from transformers.models.roberta.configuration_roberta import RobertaConfig from transformers.testing_utils import ( DUMMY_DIFF_TOKENIZER_IDENTIFIER, DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tokenizers, slow, ) sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 if is_tokenizers_available(): from test_module.custom_tokenization_fast import CustomTokenizerFast class __a( unittest.TestCase ): """simple docstring""" def a__ ( self ) -> Union[str, Any]: UpperCAmelCase_ : Tuple = 0 @slow def a__ ( self ) -> Any: for model_name in (x for x in BERT_PRETRAINED_CONFIG_ARCHIVE_MAP.keys() if "japanese" not in x): UpperCAmelCase_ : Optional[Any] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(BertTokenizer, BertTokenizerFast) ) self.assertGreater(len(_SCREAMING_SNAKE_CASE ) ,0 ) for model_name in GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP.keys(): UpperCAmelCase_ : Union[str, Any] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(GPTaTokenizer, GPTaTokenizerFast) ) self.assertGreater(len(_SCREAMING_SNAKE_CASE ) ,0 ) def a__ ( self ) -> Optional[Any]: UpperCAmelCase_ : List[str] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size ,12 ) def a__ ( self ) -> Tuple: UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(RobertaTokenizer, RobertaTokenizerFast) ) self.assertEqual(tokenizer.vocab_size ,20 ) def a__ ( self ) -> List[str]: UpperCAmelCase_ : int = AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) # Check that tokenizer_type ≠ model_type UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,config=_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size ,12 ) def a__ ( self ) -> Dict: with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''vocab.txt''' ) ) UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,tokenizer_type='''bert''' ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''merges.txt''' ) ) UpperCAmelCase_ : Optional[Any] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,tokenizer_type='''gpt2''' ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) @require_tokenizers def a__ ( self ) -> Optional[int]: with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''vocab.txt''' ) ) UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,tokenizer_type='''bert''' ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''merges.txt''' ) ) UpperCAmelCase_ : Union[str, Any] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,tokenizer_type='''gpt2''' ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) def a__ ( self ) -> int: with pytest.raises(_SCREAMING_SNAKE_CASE ): AutoTokenizer.from_pretrained('''./''' ,tokenizer_type='''xxx''' ) @require_tokenizers def a__ ( self ) -> Optional[Any]: for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: UpperCAmelCase_ : Any = tokenizer_class.from_pretrained('''wietsedv/bert-base-dutch-cased''' ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(BertTokenizer, BertTokenizerFast) ) if isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ): self.assertEqual(tokenizer.basic_tokenizer.do_lower_case ,_SCREAMING_SNAKE_CASE ) else: self.assertEqual(tokenizer.do_lower_case ,_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.model_max_length ,512 ) @require_tokenizers def a__ ( self ) -> List[Any]: for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: with self.assertRaisesRegex( _SCREAMING_SNAKE_CASE ,'''julien-c/herlolip-not-exists is not a local folder and is not a valid model identifier''' ,): UpperCAmelCase_ : int = tokenizer_class.from_pretrained('''julien-c/herlolip-not-exists''' ) def a__ ( self ) -> Optional[Any]: # tests: https://github.com/huggingface/transformers/pull/13251 # 1. models with `-`, e.g. xlm-roberta -> xlm_roberta # 2. models that don't remap 1-1 from model-name to model file, e.g., openai-gpt -> openai UpperCAmelCase_ : int = TOKENIZER_MAPPING.values() UpperCAmelCase_ : List[Any] = [] for slow_tok, fast_tok in tokenizers: if slow_tok is not None: tokenizer_names.append(slow_tok.__name__ ) if fast_tok is not None: tokenizer_names.append(fast_tok.__name__ ) for tokenizer_name in tokenizer_names: # must find the right class tokenizer_class_from_name(_SCREAMING_SNAKE_CASE ) @require_tokenizers def a__ ( self ) -> Tuple: self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' ,use_fast=_SCREAMING_SNAKE_CASE ) ,_SCREAMING_SNAKE_CASE ) self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' ) ,_SCREAMING_SNAKE_CASE ) @require_tokenizers def a__ ( self ) -> Optional[int]: UpperCAmelCase_ : str = AutoTokenizer.from_pretrained('''distilbert-base-uncased''' ,do_lower_case=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = '''Hello, world. How are you?''' UpperCAmelCase_ : List[Any] = tokenizer.tokenize(_SCREAMING_SNAKE_CASE ) self.assertEqual('''[UNK]''' ,tokens[0] ) UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained('''microsoft/mpnet-base''' ,do_lower_case=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = tokenizer.tokenize(_SCREAMING_SNAKE_CASE ) self.assertEqual('''[UNK]''' ,tokens[0] ) @require_tokenizers def a__ ( self ) -> Dict: UpperCAmelCase_ : List[Any] = AutoTokenizer.from_pretrained('''robot-test/dummy-tokenizer-fast-with-model-config''' ) self.assertEqual(type(_SCREAMING_SNAKE_CASE ) ,_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.model_max_length ,512 ) self.assertEqual(tokenizer.vocab_size ,30_000 ) self.assertEqual(tokenizer.unk_token ,'''[UNK]''' ) self.assertEqual(tokenizer.padding_side ,'''right''' ) self.assertEqual(tokenizer.truncation_side ,'''right''' ) def a__ ( self ) -> Dict: UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(BertTokenizer, BertTokenizerFast) ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,tokenizer.__class__ ) self.assertEqual(tokenizera.vocab_size ,12 ) def a__ ( self ) -> Optional[Any]: UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained('''ctrl''' ) # There is no fast CTRL so this always gives us a slow tokenizer. self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) def a__ ( self ) -> str: # Check we can load the tokenizer config of an online model. UpperCAmelCase_ : int = get_tokenizer_config('''bert-base-cased''' ) UpperCAmelCase_ : Optional[int] = config.pop('''_commit_hash''' ,_SCREAMING_SNAKE_CASE ) # If we ever update bert-base-cased tokenizer config, this dict here will need to be updated. self.assertEqual(_SCREAMING_SNAKE_CASE ,{'''do_lower_case''': False} ) # This model does not have a tokenizer_config so we get back an empty dict. UpperCAmelCase_ : Any = get_tokenizer_config(_SCREAMING_SNAKE_CASE ) self.assertDictEqual(_SCREAMING_SNAKE_CASE ,{} ) # A tokenizer saved with `save_pretrained` always creates a tokenizer config. UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = get_tokenizer_config(_SCREAMING_SNAKE_CASE ) # Check the class of the tokenizer was properly saved (note that it always saves the slow class). self.assertEqual(config['''tokenizer_class'''] ,'''BertTokenizer''' ) def a__ ( self ) -> str: try: AutoConfig.register('''custom''' ,_SCREAMING_SNAKE_CASE ) AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,slow_tokenizer_class=_SCREAMING_SNAKE_CASE ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_SCREAMING_SNAKE_CASE ): AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,slow_tokenizer_class=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = CustomTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] @require_tokenizers def a__ ( self ) -> int: try: AutoConfig.register('''custom''' ,_SCREAMING_SNAKE_CASE ) # Can register in two steps AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,slow_tokenizer_class=_SCREAMING_SNAKE_CASE ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] ,(CustomTokenizer, None) ) AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,fast_tokenizer_class=_SCREAMING_SNAKE_CASE ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] ,(CustomTokenizer, CustomTokenizerFast) ) del TOKENIZER_MAPPING._extra_content[CustomConfig] # Can register in one step AutoTokenizer.register( _SCREAMING_SNAKE_CASE ,slow_tokenizer_class=_SCREAMING_SNAKE_CASE ,fast_tokenizer_class=_SCREAMING_SNAKE_CASE ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] ,(CustomTokenizer, CustomTokenizerFast) ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_SCREAMING_SNAKE_CASE ): AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,fast_tokenizer_class=_SCREAMING_SNAKE_CASE ) # We pass through a bert tokenizer fast cause there is no converter slow to fast for our new toknizer # and that model does not have a tokenizer.json with tempfile.TemporaryDirectory() as tmp_dir: UpperCAmelCase_ : List[str] = BertTokenizerFast.from_pretrained(_SCREAMING_SNAKE_CASE ) bert_tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = CustomTokenizerFast.from_pretrained(_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def a__ ( self ) -> Optional[int]: # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Union[str, Any] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) # Test we can also load the slow version UpperCAmelCase_ : List[str] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,trust_remote_code=_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertEqual(reloaded_tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) else: self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ ,'''NewTokenizer''' ) @require_tokenizers def a__ ( self ) -> Optional[int]: class __a( _a ): """simple docstring""" lowerCAmelCase = False class __a( _a ): """simple docstring""" lowerCAmelCase = NewTokenizer lowerCAmelCase = False try: AutoConfig.register('''custom''' ,_SCREAMING_SNAKE_CASE ) AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,slow_tokenizer_class=_SCREAMING_SNAKE_CASE ) AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,fast_tokenizer_class=_SCREAMING_SNAKE_CASE ) # If remote code is not set, the default is to use local UpperCAmelCase_ : int = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote code is disabled, we load the local one. UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) UpperCAmelCase_ : List[str] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertTrue(tokenizer.special_attribute_present ) UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertTrue(tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def a__ ( self ) -> int: UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) # Test we can also load the slow version UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) else: self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) def a__ ( self ) -> Optional[Any]: with self.assertRaisesRegex( _SCREAMING_SNAKE_CASE ,'''bert-base is not a local folder and is not a valid model identifier''' ): UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained('''bert-base''' ) def a__ ( self ) -> List[Any]: with self.assertRaisesRegex( _SCREAMING_SNAKE_CASE ,R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): UpperCAmelCase_ : str = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,revision='''aaaaaa''' ) def a__ ( self ) -> Any: # Make sure we have cached the tokenizer. UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) with RequestCounter() as counter: UpperCAmelCase_ : List[str] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) self.assertEqual(counter.get_request_count ,0 ) self.assertEqual(counter.head_request_count ,1 ) self.assertEqual(counter.other_request_count ,0 )
30
0
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices _SCREAMING_SNAKE_CASE : List[Any] = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Optional[Any] = { "google/bit-50": "https://huggingface.co/google/bit-50/resolve/main/config.json", } class A__ ( _a , _a ): """simple docstring""" __magic_name__ = 'bit' __magic_name__ = ['preactivation', 'bottleneck'] __magic_name__ = ['SAME', 'VALID'] def __init__( self , __snake_case=3 , __snake_case=6_4 , __snake_case=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , __snake_case=[3, 4, 6, 3] , __snake_case="preactivation" , __snake_case="relu" , __snake_case=None , __snake_case=3_2 , __snake_case=0.0 , __snake_case=False , __snake_case=3_2 , __snake_case=1 , __snake_case=None , __snake_case=None , **__snake_case , ): super().__init__(**_SCREAMING_SNAKE_CASE ) if layer_type not in self.layer_types: raise ValueError(F'''layer_type={layer_type} is not one of {",".join(self.layer_types )}''' ) if global_padding is not None: if global_padding.upper() in self.supported_padding: snake_case = global_padding.upper() else: raise ValueError(F'''Padding strategy {global_padding} not supported''' ) snake_case = num_channels snake_case = embedding_size snake_case = hidden_sizes snake_case = depths snake_case = layer_type snake_case = hidden_act snake_case = global_padding snake_case = num_groups snake_case = drop_path_rate snake_case = embedding_dynamic_padding snake_case = output_stride snake_case = width_factor snake_case = ['''stem'''] + [F'''stage{idx}''' for idx in range(1 , len(_SCREAMING_SNAKE_CASE ) + 1 )] snake_case = get_aligned_output_features_output_indices( out_features=_SCREAMING_SNAKE_CASE , out_indices=_SCREAMING_SNAKE_CASE , stage_names=self.stage_names )
550
from functools import reduce __a = ( '73167176531330624919225119674426574742355349194934' '96983520312774506326239578318016984801869478851843' '85861560789112949495459501737958331952853208805511' '12540698747158523863050715693290963295227443043557' '66896648950445244523161731856403098711121722383113' '62229893423380308135336276614282806444486645238749' '30358907296290491560440772390713810515859307960866' '70172427121883998797908792274921901699720888093776' '65727333001053367881220235421809751254540594752243' '52584907711670556013604839586446706324415722155397' '53697817977846174064955149290862569321978468622482' '83972241375657056057490261407972968652414535100474' '82166370484403199890008895243450658541227588666881' '16427171479924442928230863465674813919123162824586' '17866458359124566529476545682848912883142607690042' '24219022671055626321111109370544217506941658960408' '07198403850962455444362981230987879927244284909188' '84580156166097919133875499200524063689912560717606' '05886116467109405077541002256983155200055935729725' '71636269561882670428252483600823257530420752963450' ) def lowerCamelCase__ ( _lowercase = N ): '''simple docstring''' return max( # mypy cannot properly interpret reduce int(reduce(lambda _lowercase , _lowercase : str(int(_lowercase ) * int(_lowercase ) ) , n[i : i + 13] ) ) for i in range(len(_lowercase ) - 12 ) ) if __name__ == "__main__": print(F"""{solution() = }""")
30
0
'''simple docstring''' # A Bipartite Graph is a graph whose vertices can be divided into two independent sets, # U and V such that every edge (u, v) either connects a vertex from U to V or a vertex # from V to U. In other words, for every edge (u, v), either u belongs to U and v to V, # or u belongs to V and v to U. We can also say that there is no edge that connects # vertices of same set. def UpperCamelCase__ ( __SCREAMING_SNAKE_CASE ) -> Optional[int]: snake_case__ : Optional[int] = [False] * len(_lowercase ) snake_case__ : str = [-1] * len(_lowercase ) def dfs(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Dict = True snake_case__ : Tuple = c for u in graph[v]: if not visited[u]: dfs(_lowercase , 1 - c ) for i in range(len(_lowercase ) ): if not visited[i]: dfs(_lowercase , 0 ) for i in range(len(_lowercase ) ): for j in graph[i]: if color[i] == color[j]: return False return True # Adjacency list of graph A_ = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []} print(check_bipartite_dfs(graph))
270
from decimal import Decimal, getcontext from math import ceil, factorial def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if not isinstance(_lowercase , _lowercase ): raise TypeError('''Undefined for non-integers''' ) elif precision < 1: raise ValueError('''Undefined for non-natural numbers''' ) UpperCAmelCase_ : Tuple = precision UpperCAmelCase_ : Optional[Any] = ceil(precision / 14 ) UpperCAmelCase_ : int = 426880 * Decimal(10005 ).sqrt() UpperCAmelCase_ : Tuple = 1 UpperCAmelCase_ : List[Any] = 13591409 UpperCAmelCase_ : Optional[Any] = Decimal(_lowercase ) for k in range(1 , _lowercase ): UpperCAmelCase_ : List[str] = factorial(6 * k ) // (factorial(3 * k ) * factorial(_lowercase ) ** 3) linear_term += 545140134 exponential_term *= -262537412640768000 partial_sum += Decimal(multinomial_term * linear_term ) / exponential_term return str(constant_term / partial_sum )[:-1] if __name__ == "__main__": __a = 50 print(F"""The first {n} digits of pi is: {pi(n)}""")
30
0
def UpperCAmelCase_ ( __UpperCAmelCase : list , __UpperCAmelCase : int , __UpperCAmelCase : int = 0 , __UpperCAmelCase : int = 0 ) -> int: SCREAMING_SNAKE_CASE_ = right or len(__UpperCAmelCase ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(__UpperCAmelCase , __UpperCAmelCase , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
31
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : Optional[int] = {'configuration_mmbt': ['MMBTConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Any = ['MMBTForClassification', 'MMBTModel', 'ModalEmbeddings'] if TYPE_CHECKING: from .configuration_mmbt import MMBTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mmbt import MMBTForClassification, MMBTModel, ModalEmbeddings else: import sys lowerCamelCase__ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
31
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : Union[str, Any] = { 'sail/poolformer_s12': 'https://huggingface.co/sail/poolformer_s12/resolve/main/config.json', # See all PoolFormer models at https://huggingface.co/models?filter=poolformer } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "poolformer" def __init__( self : List[Any] , _lowerCAmelCase : Optional[int]=3 , _lowerCAmelCase : int=16 , _lowerCAmelCase : Union[str, Any]=16 , _lowerCAmelCase : Any=3 , _lowerCAmelCase : str=4.0 , _lowerCAmelCase : List[Any]=[2, 2, 6, 2] , _lowerCAmelCase : Optional[Any]=[64, 128, 320, 512] , _lowerCAmelCase : Optional[Any]=[7, 3, 3, 3] , _lowerCAmelCase : Union[str, Any]=[4, 2, 2, 2] , _lowerCAmelCase : Optional[int]=[2, 1, 1, 1] , _lowerCAmelCase : List[str]=4 , _lowerCAmelCase : Tuple=0.0 , _lowerCAmelCase : str="gelu" , _lowerCAmelCase : str=True , _lowerCAmelCase : Dict=1E-5 , _lowerCAmelCase : Optional[Any]=0.02 , **_lowerCAmelCase : Optional[Any] , ): SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = stride SCREAMING_SNAKE_CASE_ = padding SCREAMING_SNAKE_CASE_ = pool_size SCREAMING_SNAKE_CASE_ = hidden_sizes SCREAMING_SNAKE_CASE_ = mlp_ratio SCREAMING_SNAKE_CASE_ = depths SCREAMING_SNAKE_CASE_ = patch_sizes SCREAMING_SNAKE_CASE_ = strides SCREAMING_SNAKE_CASE_ = num_encoder_blocks SCREAMING_SNAKE_CASE_ = drop_path_rate SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = use_layer_scale SCREAMING_SNAKE_CASE_ = layer_scale_init_value SCREAMING_SNAKE_CASE_ = initializer_range super().__init__(**_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = version.parse("1.11" ) @property def lowerCAmelCase_ ( self : str ): return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def lowerCAmelCase_ ( self : List[Any] ): return 2E-3
31
import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class lowerCamelCase_ : '''simple docstring''' @property def lowerCAmelCase_ ( self : Optional[Any] ): return self.get_dummy_input() @property def lowerCAmelCase_ ( self : Union[str, Any] ): if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(F"'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'." ) def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : str=False , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : Dict=False , ): SCREAMING_SNAKE_CASE_ = 4 SCREAMING_SNAKE_CASE_ = 32 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = (batch_size, num_channels) + sizes SCREAMING_SNAKE_CASE_ = randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = {'hidden_states': hidden_states} if include_temb: SCREAMING_SNAKE_CASE_ = 128 SCREAMING_SNAKE_CASE_ = randn_tensor((batch_size, temb_channels) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) if include_res_hidden_states_tuple: SCREAMING_SNAKE_CASE_ = torch.manual_seed(1 ) SCREAMING_SNAKE_CASE_ = (randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ),) if include_encoder_hidden_states: SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, 32, 32) ).to(_lowerCAmelCase ) if include_skip_sample: SCREAMING_SNAKE_CASE_ = randn_tensor(((batch_size, 3) + sizes) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) return dummy_input def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = { 'in_channels': 32, 'out_channels': 32, 'temb_channels': 128, } if self.block_type == "up": SCREAMING_SNAKE_CASE_ = 32 if self.block_type == "mid": init_dict.pop('out_channels' ) SCREAMING_SNAKE_CASE_ = self.dummy_input return init_dict, inputs_dict def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) unet_block.to(_lowerCAmelCase ) unet_block.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_ = unet_block(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] self.assertEqual(output.shape , self.output_shape ) SCREAMING_SNAKE_CASE_ = output[0, -1, -3:, -3:] SCREAMING_SNAKE_CASE_ = torch.tensor(_lowerCAmelCase ).to(_lowerCAmelCase ) assert torch_all_close(output_slice.flatten() , _lowerCAmelCase , atol=5E-3 ) @unittest.skipIf(torch_device == 'mps' , 'Training is not supported in mps' ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.train() SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = randn_tensor(output.shape , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.functional.mse_loss(_lowerCAmelCase , _lowerCAmelCase ) loss.backward()
31
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowerCamelCase__ : Tuple = { 'configuration_pix2struct': [ 'PIX2STRUCT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Pix2StructConfig', 'Pix2StructTextConfig', 'Pix2StructVisionConfig', ], 'processing_pix2struct': ['Pix2StructProcessor'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Union[str, Any] = ['Pix2StructImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : int = [ 'PIX2STRUCT_PRETRAINED_MODEL_ARCHIVE_LIST', 'Pix2StructPreTrainedModel', 'Pix2StructForConditionalGeneration', 'Pix2StructVisionModel', 'Pix2StructTextModel', ] if TYPE_CHECKING: from .configuration_pixastruct import ( PIX2STRUCT_PRETRAINED_CONFIG_ARCHIVE_MAP, PixaStructConfig, PixaStructTextConfig, PixaStructVisionConfig, ) from .processing_pixastruct import PixaStructProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_pixastruct import PixaStructImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_pixastruct import ( PIX2STRUCT_PRETRAINED_MODEL_ARCHIVE_LIST, PixaStructForConditionalGeneration, PixaStructPreTrainedModel, PixaStructTextModel, PixaStructVisionModel, ) else: import sys lowerCamelCase__ : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
31
import operator as op def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> Any: SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = lambda __UpperCAmelCase , __UpperCAmelCase : int(x / y ) # noqa: E731 integer division operation SCREAMING_SNAKE_CASE_ = { '^': op.pow, '*': op.mul, '/': div, '+': op.add, '-': op.sub, } # operators & their respective operation # print table header print('Symbol'.center(8 ) , 'Action'.center(12 ) , 'Stack' , sep=' | ' ) print('-' * (30 + len(__UpperCAmelCase )) ) for x in post_fix: if x.isdigit(): # if x in digit stack.append(__UpperCAmelCase ) # append x to stack # output in tabular format print(x.rjust(8 ) , ('push(' + x + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) else: SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print(''.rjust(8 ) , ('pop(' + b + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print(''.rjust(8 ) , ('pop(' + a + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) stack.append( str(opr[x](int(__UpperCAmelCase ) , int(__UpperCAmelCase ) ) ) ) # evaluate the 2 values popped from stack & push result to stack # output in tabular format print( x.rjust(8 ) , ('push(' + a + x + b + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' , ) return int(stack[0] ) if __name__ == "__main__": lowerCamelCase__ : Tuple = input('\n\nEnter a Postfix Equation (space separated) = ').split(' ') print('\n\tResult = ', solve(Postfix))
31
1
import random from .binary_exp_mod import bin_exp_mod def UpperCAmelCase_ ( __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : List[Any]=10_00 ) -> Optional[int]: if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd SCREAMING_SNAKE_CASE_ = n - 1 SCREAMING_SNAKE_CASE_ = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) SCREAMING_SNAKE_CASE_ = 0 while count < prec: SCREAMING_SNAKE_CASE_ = random.randint(2 , n - 1 ) SCREAMING_SNAKE_CASE_ = bin_exp_mod(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) if b != 1: SCREAMING_SNAKE_CASE_ = True for _ in range(__UpperCAmelCase ): if b == n - 1: SCREAMING_SNAKE_CASE_ = False break SCREAMING_SNAKE_CASE_ = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": lowerCamelCase__ : str = abs(int(input('Enter bound : ').strip())) print('Here\'s the list of primes:') print(', '.join(str(i) for i in range(n + 1) if is_prime_big(i)))
31
def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: assert isinstance(__UpperCAmelCase , __UpperCAmelCase ), f"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE_ = f"The input value of [n={number}] has to be > 0" raise ValueError(__UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_ = sylvester(number - 1 ) SCREAMING_SNAKE_CASE_ = num - 1 SCREAMING_SNAKE_CASE_ = num return lower * upper + 1 if __name__ == "__main__": print(f'''The 8th number in Sylvester\'s sequence: {sylvester(8)}''')
31
1
import tempfile import unittest from pathlib import Path from shutil import copyfile from transformers import BatchEncoding, MarianTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow from transformers.utils import is_sentencepiece_available, is_tf_available, is_torch_available if is_sentencepiece_available(): from transformers.models.marian.tokenization_marian import VOCAB_FILES_NAMES, save_json from ...test_tokenization_common import TokenizerTesterMixin lowerCamelCase__ : int = get_tests_dir('fixtures/test_sentencepiece.model') lowerCamelCase__ : int = {'target_lang': 'fi', 'source_lang': 'en'} lowerCamelCase__ : List[Any] = '>>zh<<' lowerCamelCase__ : Optional[Any] = 'Helsinki-NLP/' if is_torch_available(): lowerCamelCase__ : List[str] = 'pt' elif is_tf_available(): lowerCamelCase__ : int = 'tf' else: lowerCamelCase__ : Tuple = 'jax' @require_sentencepiece class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = MarianTokenizer lowercase_ = False lowercase_ = True def lowerCAmelCase_ ( self : Dict ): super().setUp() SCREAMING_SNAKE_CASE_ = ['</s>', '<unk>', '▁This', '▁is', '▁a', '▁t', 'est', '\u0120', '<pad>'] SCREAMING_SNAKE_CASE_ = dict(zip(_lowerCAmelCase , range(len(_lowerCAmelCase ) ) ) ) SCREAMING_SNAKE_CASE_ = Path(self.tmpdirname ) save_json(_lowerCAmelCase , save_dir / VOCAB_FILES_NAMES['vocab'] ) save_json(_lowerCAmelCase , save_dir / VOCAB_FILES_NAMES['tokenizer_config_file'] ) if not (save_dir / VOCAB_FILES_NAMES["source_spm"]).exists(): copyfile(_lowerCAmelCase , save_dir / VOCAB_FILES_NAMES['source_spm'] ) copyfile(_lowerCAmelCase , save_dir / VOCAB_FILES_NAMES['target_spm'] ) SCREAMING_SNAKE_CASE_ = MarianTokenizer.from_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname ) def lowerCAmelCase_ ( self : List[Any] , **_lowerCAmelCase : int ): return MarianTokenizer.from_pretrained(self.tmpdirname , **_lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : int ): return ( "This is a test", "This is a test", ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = '</s>' SCREAMING_SNAKE_CASE_ = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_lowerCAmelCase ) , _lowerCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_lowerCAmelCase ) , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '</s>' ) self.assertEqual(vocab_keys[1] , '<unk>' ) self.assertEqual(vocab_keys[-1] , '<pad>' ) self.assertEqual(len(_lowerCAmelCase ) , 9 ) def lowerCAmelCase_ ( self : str ): self.assertEqual(self.get_tokenizer().vocab_size , 9 ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = MarianTokenizer.from_pretrained(F"{ORG_NAME}opus-mt-en-de" ) SCREAMING_SNAKE_CASE_ = en_de_tokenizer(['I am a small frog'] , return_tensors=_lowerCAmelCase ) self.assertIsInstance(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = [38, 121, 14, 697, 38_848, 0] self.assertListEqual(_lowerCAmelCase , batch.input_ids[0] ) SCREAMING_SNAKE_CASE_ = tempfile.mkdtemp() en_de_tokenizer.save_pretrained(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = [x.name for x in Path(_lowerCAmelCase ).glob('*' )] self.assertIn('source.spm' , _lowerCAmelCase ) MarianTokenizer.from_pretrained(_lowerCAmelCase ) def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = tok( ['I am a small frog' * 1_000, 'I am a small frog'] , padding=_lowerCAmelCase , truncation=_lowerCAmelCase , return_tensors=_lowerCAmelCase ) self.assertIsInstance(_lowerCAmelCase , _lowerCAmelCase ) self.assertEqual(batch.input_ids.shape , (2, 512) ) def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = tok(['I am a tiny frog', 'I am a small frog'] , padding=_lowerCAmelCase , return_tensors=_lowerCAmelCase ) self.assertIsInstance(_lowerCAmelCase , _lowerCAmelCase ) self.assertEqual(batch_smaller.input_ids.shape , (2, 10) ) @slow def lowerCAmelCase_ ( self : List[str] ): # fmt: off SCREAMING_SNAKE_CASE_ = {'input_ids': [[43_495, 462, 20, 42_164, 1_369, 52, 464, 132, 1_703, 492, 13, 7_491, 38_999, 6, 8, 464, 132, 1_703, 492, 13, 4_669, 37_867, 13, 7_525, 27, 1_593, 988, 13, 33_972, 7_029, 6, 20, 8_251, 383, 2, 270, 5_866, 3_788, 2, 2_353, 8_251, 12_338, 2, 13_958, 387, 2, 3_629, 6_953, 188, 2_900, 2, 13_958, 8_011, 11_501, 23, 8_460, 4_073, 34_009, 20, 435, 11_439, 27, 8, 8_460, 4_073, 6_004, 20, 9_988, 375, 27, 33, 266, 1_945, 1_076, 1_350, 37_867, 3_288, 5, 577, 1_076, 4_374, 8, 5_082, 5, 26_453, 257, 556, 403, 2, 242, 132, 383, 316, 492, 8, 10_767, 6, 316, 304, 4_239, 3, 0], [148, 15_722, 19, 1_839, 12, 1_350, 13, 22_327, 5_082, 5_418, 47_567, 35_938, 59, 318, 19_552, 108, 2_183, 54, 14_976, 4_835, 32, 547, 1_114, 8, 315, 2_417, 5, 92, 19_088, 3, 0, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100], [36, 6_395, 12_570, 39_147, 11_597, 6, 266, 4, 45_405, 7_296, 3, 0, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_lowerCAmelCase , model_name='Helsinki-NLP/opus-mt-en-de' , revision='1a8c2263da11e68e50938f97e10cd57820bd504c' , decode_kwargs={'use_source_tokenizer': True} , ) def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = MarianTokenizer.from_pretrained('hf-internal-testing/test-marian-two-vocabs' ) SCREAMING_SNAKE_CASE_ = 'Tämä on testi' SCREAMING_SNAKE_CASE_ = 'This is a test' SCREAMING_SNAKE_CASE_ = [76, 7, 2_047, 2] SCREAMING_SNAKE_CASE_ = [69, 12, 11, 940, 2] SCREAMING_SNAKE_CASE_ = tokenizer(_lowerCAmelCase ).input_ids self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer(text_target=_lowerCAmelCase ).input_ids self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.decode(_lowerCAmelCase , skip_special_tokens=_lowerCAmelCase ) self.assertEqual(_lowerCAmelCase , _lowerCAmelCase )
31
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCamelCase__ : List[Any] = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCamelCase__ : Union[str, Any] = TaTokenizerFast lowerCamelCase__ : Dict = {'configuration_mt5': ['MT5Config', 'MT5OnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = [ 'MT5EncoderModel', 'MT5ForConditionalGeneration', 'MT5ForQuestionAnswering', 'MT5Model', 'MT5PreTrainedModel', 'MT5Stack', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = ['TFMT5EncoderModel', 'TFMT5ForConditionalGeneration', 'TFMT5Model'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = ['FlaxMT5EncoderModel', 'FlaxMT5ForConditionalGeneration', 'FlaxMT5Model'] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCamelCase__ : int = _LazyModule( __name__, globals()['__file__'], _import_structure, extra_objects={'MT5Tokenizer': MTaTokenizer, 'MT5TokenizerFast': MTaTokenizerFast}, module_spec=__spec__, )
31
1
import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available 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 ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class lowerCamelCase_ : '''simple docstring''' def __init__( self : Any , _lowerCAmelCase : str , _lowerCAmelCase : int=13 , _lowerCAmelCase : Tuple=30 , _lowerCAmelCase : Any=2 , _lowerCAmelCase : Optional[Any]=3 , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Any=True , _lowerCAmelCase : List[Any]=32 , _lowerCAmelCase : Optional[Any]=5 , _lowerCAmelCase : List[Any]=4 , _lowerCAmelCase : int=37 , _lowerCAmelCase : Optional[Any]="gelu" , _lowerCAmelCase : Tuple=0.1 , _lowerCAmelCase : Union[str, Any]=0.1 , _lowerCAmelCase : Union[str, Any]=10 , _lowerCAmelCase : Dict=0.02 , _lowerCAmelCase : Optional[Any]=None , _lowerCAmelCase : Dict=2 , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_labels SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = type_sequence_label_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = scope SCREAMING_SNAKE_CASE_ = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) SCREAMING_SNAKE_CASE_ = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE_ = num_patches + 1 def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ = None if self.use_labels: SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ = self.get_config() return config, pixel_values, labels def lowerCAmelCase_ ( self : str ): return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_lowerCAmelCase , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : Any , _lowerCAmelCase : int , _lowerCAmelCase : str ): SCREAMING_SNAKE_CASE_ = ViTModel(config=_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[str] , _lowerCAmelCase : List[str] , _lowerCAmelCase : Optional[int] ): SCREAMING_SNAKE_CASE_ = ViTForMaskedImageModeling(config=_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = ViTForMaskedImageModeling(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Tuple , _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.type_sequence_label_size SCREAMING_SNAKE_CASE_ = ViTForImageClassification(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , labels=_lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = ViTForImageClassification(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ) = config_and_inputs SCREAMING_SNAKE_CASE_ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) lowercase_ = ( {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification} if is_torch_available() else {} ) lowercase_ = True lowercase_ = False lowercase_ = False lowercase_ = False def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = ViTModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=_lowerCAmelCase , has_text_modality=_lowerCAmelCase , hidden_size=37 ) def lowerCAmelCase_ ( self : int ): self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def lowerCAmelCase_ ( self : List[str] ): pass def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class(_lowerCAmelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE_ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_lowerCAmelCase , nn.Linear ) ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_ = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_lowerCAmelCase ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*_lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase ) @slow def lowerCAmelCase_ ( self : List[Any] ): for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ = ViTModel.from_pretrained(_lowerCAmelCase ) self.assertIsNotNone(_lowerCAmelCase ) def UpperCAmelCase_ ( ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' @cached_property def lowerCAmelCase_ ( self : Tuple ): return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224' ) if is_vision_available() else None @slow def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224' ).to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=_lowerCAmelCase , return_tensors='pt' ).to(_lowerCAmelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ) # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.tensor([-0.2744, 0.8215, -0.0836] ).to(_lowerCAmelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _lowerCAmelCase , atol=1E-4 ) ) @slow def lowerCAmelCase_ ( self : int ): # ViT models have an `interpolate_pos_encoding` argument in their forward method, # allowing to interpolate the pre-trained position embeddings in order to use # the model on higher resolutions. The DINO model by Facebook AI leverages this # to visualize self-attention on higher resolution images. SCREAMING_SNAKE_CASE_ = ViTModel.from_pretrained('facebook/dino-vits8' ).to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = ViTImageProcessor.from_pretrained('facebook/dino-vits8' , size=480 ) SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=_lowerCAmelCase , return_tensors='pt' ) SCREAMING_SNAKE_CASE_ = inputs.pixel_values.to(_lowerCAmelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , interpolate_pos_encoding=_lowerCAmelCase ) # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 3_601, 384) ) self.assertEqual(outputs.last_hidden_state.shape , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.tensor( [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]] ).to(_lowerCAmelCase ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , _lowerCAmelCase , atol=1E-4 ) ) @slow @require_accelerate @require_torch_gpu def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = ViTModel.from_pretrained('facebook/dino-vits8' , torch_dtype=torch.floataa , device_map='auto' ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=_lowerCAmelCase , return_tensors='pt' ) SCREAMING_SNAKE_CASE_ = inputs.pixel_values.to(_lowerCAmelCase ) # forward pass to make sure inference works in fp16 with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase )
31
import subprocess import sys from transformers import BertConfig, BertModel, BertTokenizer, pipeline from transformers.testing_utils import TestCasePlus, require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' @require_torch def lowerCAmelCase_ ( self : int ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Tuple ): # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : List[str] ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # next emulate no network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this. # env["TRANSFORMERS_OFFLINE"] = "0" # result = subprocess.run(cmd, env=env, check=False, capture_output=True) # self.assertEqual(result.returncode, 1, result.stderr) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n ' SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 1 , result.stderr ) self.assertIn( 'You cannot infer task automatically within `pipeline` when using offline mode' , result.stderr.decode().replace('\n' , '' ) , ) @require_torch def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import AutoModel\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() )
31
1
def UpperCAmelCase_ ( ) -> list[list[int]]: return [list(range(10_00 - i , -10_00 - i , -1 ) ) for i in range(10_00 )] lowerCamelCase__ : List[Any] = generate_large_matrix() lowerCamelCase__ : List[Any] = ( [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]], [[3, 2], [1, 0]], [[7, 7, 6]], [[7, 7, 6], [-1, -2, -3]], grid, ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> None: assert all(row == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for row in grid ) assert all(list(__UpperCAmelCase ) == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for col in zip(*__UpperCAmelCase ) ) def UpperCAmelCase_ ( __UpperCAmelCase : list[int] ) -> int: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = len(__UpperCAmelCase ) - 1 # Edge cases such as no values or all numbers are negative. if not array or array[0] < 0: return 0 while right + 1 > left: SCREAMING_SNAKE_CASE_ = (left + right) // 2 SCREAMING_SNAKE_CASE_ = array[mid] # Num must be negative and the index must be greater than or equal to 0. if num < 0 and array[mid - 1] >= 0: return mid if num >= 0: SCREAMING_SNAKE_CASE_ = mid + 1 else: SCREAMING_SNAKE_CASE_ = mid - 1 # No negative numbers so return the last index of the array + 1 which is the length. return len(__UpperCAmelCase ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = len(grid[0] ) for i in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = find_negative_index(grid[i][:bound] ) total += bound return (len(__UpperCAmelCase ) * len(grid[0] )) - total def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: return len([number for row in grid for number in row if number < 0] ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: SCREAMING_SNAKE_CASE_ = 0 for row in grid: for i, number in enumerate(__UpperCAmelCase ): if number < 0: total += len(__UpperCAmelCase ) - i break return total def UpperCAmelCase_ ( ) -> None: from timeit import timeit print('Running benchmarks' ) SCREAMING_SNAKE_CASE_ = ( 'from __main__ import count_negatives_binary_search, ' 'count_negatives_brute_force, count_negatives_brute_force_with_break, grid' ) for func in ( "count_negatives_binary_search", # took 0.7727 seconds "count_negatives_brute_force_with_break", # took 4.6505 seconds "count_negatives_brute_force", # took 12.8160 seconds ): SCREAMING_SNAKE_CASE_ = timeit(f"{func}(grid=grid)" , setup=__UpperCAmelCase , number=5_00 ) print(f"{func}() took {time:0.4f} seconds" ) if __name__ == "__main__": import doctest doctest.testmod() benchmark()
31
import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "M-CLIP" def __init__( self : Tuple , _lowerCAmelCase : List[str]=1_024 , _lowerCAmelCase : str=768 , **_lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = transformerDimSize SCREAMING_SNAKE_CASE_ = imageDimSize super().__init__(**_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = MCLIPConfig def __init__( self : Dict , _lowerCAmelCase : Union[str, Any] , *_lowerCAmelCase : str , **_lowerCAmelCase : str ): super().__init__(_lowerCAmelCase , *_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = XLMRobertaModel(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.Linear( in_features=config.transformerDimensions , out_features=config.numDims ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.transformer(input_ids=_lowerCAmelCase , attention_mask=_lowerCAmelCase )[0] SCREAMING_SNAKE_CASE_ = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(_lowerCAmelCase ), embs
31
1
import unittest from transformers import DebertaVaTokenizer, DebertaVaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin lowerCamelCase__ : int = get_tests_dir('fixtures/spiece.model') @require_sentencepiece @require_tokenizers class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = DebertaVaTokenizer lowercase_ = DebertaVaTokenizerFast lowercase_ = True lowercase_ = True def lowerCAmelCase_ ( self : int ): super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE_ = DebertaVaTokenizer(_lowerCAmelCase , unk_token='<unk>' ) tokenizer.save_pretrained(self.tmpdirname ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Dict ): SCREAMING_SNAKE_CASE_ = 'this is a test' SCREAMING_SNAKE_CASE_ = 'this is a test' return input_text, output_text def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = '<pad>' SCREAMING_SNAKE_CASE_ = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_lowerCAmelCase ) , _lowerCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_lowerCAmelCase ) , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<pad>' ) self.assertEqual(vocab_keys[1] , '<unk>' ) self.assertEqual(vocab_keys[-1] , '[PAD]' ) self.assertEqual(len(_lowerCAmelCase ) , 30_001 ) def lowerCAmelCase_ ( self : Any ): self.assertEqual(self.get_tokenizer().vocab_size , 30_000 ) def lowerCAmelCase_ ( self : Tuple ): # fmt: off SCREAMING_SNAKE_CASE_ = ' \tHeLLo!how \n Are yoU? ' SCREAMING_SNAKE_CASE_ = ['▁hello', '!', 'how', '▁are', '▁you', '?'] # fmt: on SCREAMING_SNAKE_CASE_ = DebertaVaTokenizer(_lowerCAmelCase , do_lower_case=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.convert_ids_to_tokens(tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = DebertaVaTokenizerFast(_lowerCAmelCase , do_lower_case=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) @unittest.skip('There is an inconsistency between slow and fast tokenizer due to a bug in the fast one.' ) def lowerCAmelCase_ ( self : Optional[Any] ): pass @unittest.skip('There is an inconsistency between slow and fast tokenizer due to a bug in the fast one.' ) def lowerCAmelCase_ ( self : Union[str, Any] ): pass def lowerCAmelCase_ ( self : List[str] ): # fmt: off SCREAMING_SNAKE_CASE_ = 'I was born in 92000, and this is falsé.' SCREAMING_SNAKE_CASE_ = ['▁', '<unk>', '▁was', '▁born', '▁in', '▁9', '2000', '▁', ',', '▁and', '▁this', '▁is', '▁fal', 's', '<unk>', '▁', '.', ] # fmt: on SCREAMING_SNAKE_CASE_ = DebertaVaTokenizer(_lowerCAmelCase , split_by_punct=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.convert_ids_to_tokens(tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = DebertaVaTokenizerFast(_lowerCAmelCase , split_by_punct=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict ): # fmt: off SCREAMING_SNAKE_CASE_ = 'I was born in 92000, and this is falsé.' SCREAMING_SNAKE_CASE_ = ['▁i', '▁was', '▁born', '▁in', '▁9', '2000', '▁', ',', '▁and', '▁this', '▁is', '▁fal', 's', '<unk>', '▁', '.', ] # fmt: on SCREAMING_SNAKE_CASE_ = DebertaVaTokenizer(_lowerCAmelCase , do_lower_case=_lowerCAmelCase , split_by_punct=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.convert_ids_to_tokens(tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = DebertaVaTokenizerFast(_lowerCAmelCase , do_lower_case=_lowerCAmelCase , split_by_punct=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : List[str] ): # fmt: off SCREAMING_SNAKE_CASE_ = 'I was born in 92000, and this is falsé.' SCREAMING_SNAKE_CASE_ = ['▁i', '▁was', '▁born', '▁in', '▁9', '2000', ',', '▁and', '▁this', '▁is', '▁fal', 's', '<unk>', '.', ] # fmt: on SCREAMING_SNAKE_CASE_ = DebertaVaTokenizer(_lowerCAmelCase , do_lower_case=_lowerCAmelCase , split_by_punct=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.convert_ids_to_tokens(tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = DebertaVaTokenizerFast(_lowerCAmelCase , do_lower_case=_lowerCAmelCase , split_by_punct=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] ): # fmt: off SCREAMING_SNAKE_CASE_ = 'I was born in 92000, and this is falsé.' SCREAMING_SNAKE_CASE_ = ['▁', '<unk>', '▁was', '▁born', '▁in', '▁9', '2000', '▁', ',', '▁and', '▁this', '▁is', '▁fal', 's', '<unk>', '▁', '.', ] # fmt: on SCREAMING_SNAKE_CASE_ = DebertaVaTokenizer(_lowerCAmelCase , do_lower_case=_lowerCAmelCase , split_by_punct=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.convert_ids_to_tokens(tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = DebertaVaTokenizerFast(_lowerCAmelCase , do_lower_case=_lowerCAmelCase , split_by_punct=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple ): # fmt: off SCREAMING_SNAKE_CASE_ = ' \tHeLLo!how \n Are yoU? ' SCREAMING_SNAKE_CASE_ = ['▁', '<unk>', 'e', '<unk>', 'o', '!', 'how', '▁', '<unk>', 're', '▁yo', '<unk>', '?'] # fmt: on SCREAMING_SNAKE_CASE_ = DebertaVaTokenizer(_lowerCAmelCase , do_lower_case=_lowerCAmelCase , split_by_punct=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.convert_ids_to_tokens(tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = DebertaVaTokenizerFast(_lowerCAmelCase , do_lower_case=_lowerCAmelCase , split_by_punct=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_ = 'I was born in 92000, and this is falsé.' SCREAMING_SNAKE_CASE_ = tokenizer.convert_ids_to_tokens(tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_ = tokenizer.encode(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.encode(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = 'This is a test' SCREAMING_SNAKE_CASE_ = [13, 1, 4_398, 25, 21, 1_289] SCREAMING_SNAKE_CASE_ = ['▁', 'T', 'his', '▁is', '▁a', '▁test'] SCREAMING_SNAKE_CASE_ = ['▁', '<unk>', 'his', '▁is', '▁a', '▁test'] SCREAMING_SNAKE_CASE_ = DebertaVaTokenizer(_lowerCAmelCase , keep_accents=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = DebertaVaTokenizerFast(_lowerCAmelCase , keep_accents=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.tokenize(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.convert_ids_to_tokens(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.tokenize(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.convert_ids_to_tokens(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) # fmt: off SCREAMING_SNAKE_CASE_ = 'I was born in 92000, and this is falsé.' SCREAMING_SNAKE_CASE_ = [13, 1, 23, 386, 19, 561, 3_050, 15, 17, 48, 25, 8_256, 18, 1, 9] SCREAMING_SNAKE_CASE_ = ['▁', 'I', '▁was', '▁born', '▁in', '▁9', '2000', ',', '▁and', '▁this', '▁is', '▁fal', 's', 'é', '.', ] SCREAMING_SNAKE_CASE_ = ['▁', '<unk>', '▁was', '▁born', '▁in', '▁9', '2000', ',', '▁and', '▁this', '▁is', '▁fal', 's', '<unk>', '.', ] # fmt: on SCREAMING_SNAKE_CASE_ = tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.tokenize(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.convert_ids_to_tokens(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.tokenize(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.convert_ids_to_tokens(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = DebertaVaTokenizer(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.encode('sequence builders' ) SCREAMING_SNAKE_CASE_ = tokenizer.encode('multi-sequence build' ) SCREAMING_SNAKE_CASE_ = tokenizer.build_inputs_with_special_tokens(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.build_inputs_with_special_tokens(_lowerCAmelCase , _lowerCAmelCase ) self.assertEqual([tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] , _lowerCAmelCase ) self.assertEqual( [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [tokenizer.sep_token_id] , _lowerCAmelCase , ) @slow def lowerCAmelCase_ ( self : str ): # fmt: off SCREAMING_SNAKE_CASE_ = {'input_ids': [[1, 39_867, 36, 19_390, 486, 27, 35_052, 81_436, 18, 60_685, 1_225, 7, 35_052, 81_436, 18, 9_367, 16_899, 18, 15_937, 53, 594, 773, 18, 16_287, 30_465, 36, 15_937, 6, 41_139, 38, 36_979, 60_763, 191, 6, 34_132, 99, 6, 50_538, 390, 43_230, 6, 34_132, 2_779, 20_850, 14, 699, 1_072, 1_194, 36, 382, 10_901, 53, 7, 699, 1_072, 2_084, 36, 20_422, 630, 53, 19, 105, 3_049, 1_896, 1_053, 16_899, 1_506, 11, 37_978, 4_243, 7, 1_237, 31_869, 200, 16_566, 654, 6, 35_052, 81_436, 7, 55_630, 13_593, 4, 2], [1, 26, 15_011, 13, 667, 8, 1_053, 18, 23_611, 1_237, 72_356, 12_820, 34, 104_134, 1_209, 35, 13_313, 6_627, 21, 202, 347, 7, 164, 2_399, 11, 46, 4_485, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 5, 1_232, 2_864, 15_785, 14_951, 105, 5, 8_581, 1_250, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '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]], '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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_lowerCAmelCase , model_name='microsoft/deberta-v2-xlarge' , revision='ad6e42c1532ddf3a15c39246b63f5559d558b670' , )
31
import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) 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 enable_full_determinism() class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = 3 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(_lowerCAmelCase ) return image @property def lowerCAmelCase_ ( self : Union[str, Any] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) return model @property def lowerCAmelCase_ ( self : Tuple ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , ) return model @property def lowerCAmelCase_ ( self : Optional[int] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5_006 , ) return RobertaSeriesModelWithTransformation(_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : List[Any] ): def extract(*_lowerCAmelCase : Optional[int] , **_lowerCAmelCase : str ): class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = torch.ones([0] ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int ): self.pixel_values.to(_lowerCAmelCase ) return self return Out() return extract def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = 'cpu' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = output.images SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , return_dict=_lowerCAmelCase , )[0] SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE_ = np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5E-3 @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) # put models in fp16 SCREAMING_SNAKE_CASE_ = unet.half() SCREAMING_SNAKE_CASE_ = vae.half() SCREAMING_SNAKE_CASE_ = bert.half() # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) # resize to resolution that is divisible by 8 but not 16 or 32 SCREAMING_SNAKE_CASE_ = init_image.resize((760, 504) ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] SCREAMING_SNAKE_CASE_ = image[255:258, 383:386, -1] assert image.shape == (504, 760, 3) SCREAMING_SNAKE_CASE_ = np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) SCREAMING_SNAKE_CASE_ = init_image.resize((768, 512) ) SCREAMING_SNAKE_CASE_ = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy' ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] assert image.shape == (512, 768, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1E-2
31
1
import contextlib import csv import json import os import sqlitea import tarfile import textwrap import zipfile import pyarrow as pa import pyarrow.parquet as pq import pytest import datasets import datasets.config @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( ) -> Any: SCREAMING_SNAKE_CASE_ = 10 SCREAMING_SNAKE_CASE_ = datasets.Features( { 'tokens': datasets.Sequence(datasets.Value('string' ) ), 'labels': datasets.Sequence(datasets.ClassLabel(names=['negative', 'positive'] ) ), 'answers': datasets.Sequence( { 'text': datasets.Value('string' ), 'answer_start': datasets.Value('int32' ), } ), 'id': datasets.Value('int64' ), } ) SCREAMING_SNAKE_CASE_ = datasets.Dataset.from_dict( { 'tokens': [['foo'] * 5] * n, 'labels': [[1] * 5] * n, 'answers': [{'answer_start': [97], 'text': ['1976']}] * 10, 'id': list(range(__UpperCAmelCase ) ), } , features=__UpperCAmelCase , ) return dataset @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : List[str] , __UpperCAmelCase : Dict ) -> Tuple: SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'file.arrow' ) dataset.map(cache_file_name=__UpperCAmelCase ) return filename # FILE_CONTENT + files lowerCamelCase__ : Optional[Any] = '\\n Text data.\n Second line of data.' @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Dict ) -> str: SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'file.txt' SCREAMING_SNAKE_CASE_ = FILE_CONTENT with open(__UpperCAmelCase , 'w' ) as f: f.write(__UpperCAmelCase ) return filename @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> Any: import bza SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'file.txt.bz2' SCREAMING_SNAKE_CASE_ = bytes(__UpperCAmelCase , 'utf-8' ) with bza.open(__UpperCAmelCase , 'wb' ) as f: f.write(__UpperCAmelCase ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> List[Any]: import gzip SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'file.txt.gz' ) SCREAMING_SNAKE_CASE_ = bytes(__UpperCAmelCase , 'utf-8' ) with gzip.open(__UpperCAmelCase , 'wb' ) as f: f.write(__UpperCAmelCase ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> Any: if datasets.config.LZ4_AVAILABLE: import lza.frame SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'file.txt.lz4' SCREAMING_SNAKE_CASE_ = bytes(__UpperCAmelCase , 'utf-8' ) with lza.frame.open(__UpperCAmelCase , 'wb' ) as f: f.write(__UpperCAmelCase ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : List[str] ) -> List[Any]: if datasets.config.PY7ZR_AVAILABLE: import pyazr SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'file.txt.7z' with pyazr.SevenZipFile(__UpperCAmelCase , 'w' ) as archive: archive.write(__UpperCAmelCase , arcname=os.path.basename(__UpperCAmelCase ) ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : List[Any] , __UpperCAmelCase : Union[str, Any] ) -> Optional[int]: import tarfile SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'file.txt.tar' with tarfile.TarFile(__UpperCAmelCase , 'w' ) as f: f.add(__UpperCAmelCase , arcname=os.path.basename(__UpperCAmelCase ) ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Union[str, Any] ) -> Optional[Any]: import lzma SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'file.txt.xz' SCREAMING_SNAKE_CASE_ = bytes(__UpperCAmelCase , 'utf-8' ) with lzma.open(__UpperCAmelCase , 'wb' ) as f: f.write(__UpperCAmelCase ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Optional[int] , __UpperCAmelCase : List[str] ) -> Union[str, Any]: import zipfile SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'file.txt.zip' with zipfile.ZipFile(__UpperCAmelCase , 'w' ) as f: f.write(__UpperCAmelCase , arcname=os.path.basename(__UpperCAmelCase ) ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : List[Any] ) -> str: if datasets.config.ZSTANDARD_AVAILABLE: import zstandard as zstd SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'file.txt.zst' SCREAMING_SNAKE_CASE_ = bytes(__UpperCAmelCase , 'utf-8' ) with zstd.open(__UpperCAmelCase , 'wb' ) as f: f.write(__UpperCAmelCase ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Union[str, Any] ) -> Any: SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'file.xml' SCREAMING_SNAKE_CASE_ = textwrap.dedent( '\\n <?xml version="1.0" encoding="UTF-8" ?>\n <tmx version="1.4">\n <header segtype="sentence" srclang="ca" />\n <body>\n <tu>\n <tuv xml:lang="ca"><seg>Contingut 1</seg></tuv>\n <tuv xml:lang="en"><seg>Content 1</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang="ca"><seg>Contingut 2</seg></tuv>\n <tuv xml:lang="en"><seg>Content 2</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang="ca"><seg>Contingut 3</seg></tuv>\n <tuv xml:lang="en"><seg>Content 3</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang="ca"><seg>Contingut 4</seg></tuv>\n <tuv xml:lang="en"><seg>Content 4</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang="ca"><seg>Contingut 5</seg></tuv>\n <tuv xml:lang="en"><seg>Content 5</seg></tuv>\n </tu>\n </body>\n </tmx>' ) with open(__UpperCAmelCase , 'w' ) as f: f.write(__UpperCAmelCase ) return filename lowerCamelCase__ : Dict = [ {'col_1': '0', 'col_2': 0, 'col_3': 0.0}, {'col_1': '1', 'col_2': 1, 'col_3': 1.0}, {'col_1': '2', 'col_2': 2, 'col_3': 2.0}, {'col_1': '3', 'col_2': 3, 'col_3': 3.0}, ] lowerCamelCase__ : Optional[Any] = [ {'col_1': '4', 'col_2': 4, 'col_3': 4.0}, {'col_1': '5', 'col_2': 5, 'col_3': 5.0}, ] lowerCamelCase__ : List[str] = { 'col_1': ['0', '1', '2', '3'], 'col_2': [0, 1, 2, 3], 'col_3': [0.0, 1.0, 2.0, 3.0], } lowerCamelCase__ : str = [ {'col_3': 0.0, 'col_1': '0', 'col_2': 0}, {'col_3': 1.0, 'col_1': '1', 'col_2': 1}, ] lowerCamelCase__ : Union[str, Any] = [ {'col_1': 's0', 'col_2': 0, 'col_3': 0.0}, {'col_1': 's1', 'col_2': 1, 'col_3': 1.0}, {'col_1': 's2', 'col_2': 2, 'col_3': 2.0}, {'col_1': 's3', 'col_2': 3, 'col_3': 3.0}, ] @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( ) -> Any: return DATA_DICT_OF_LISTS @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> List[str]: SCREAMING_SNAKE_CASE_ = datasets.Dataset.from_dict(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset.arrow' ) dataset.map(cache_file_name=__UpperCAmelCase ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Dict ) -> List[str]: SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset.sqlite' ) with contextlib.closing(sqlitea.connect(__UpperCAmelCase ) ) as con: SCREAMING_SNAKE_CASE_ = con.cursor() cur.execute('CREATE TABLE dataset(col_1 text, col_2 int, col_3 real)' ) for item in DATA: cur.execute('INSERT INTO dataset(col_1, col_2, col_3) VALUES (?, ?, ?)' , tuple(item.values() ) ) con.commit() return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Optional[Any] ) -> Tuple: SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset.csv' ) with open(__UpperCAmelCase , 'w' , newline='' ) as f: SCREAMING_SNAKE_CASE_ = csv.DictWriter(__UpperCAmelCase , fieldnames=['col_1', 'col_2', 'col_3'] ) writer.writeheader() for item in DATA: writer.writerow(__UpperCAmelCase ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Dict ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset2.csv' ) with open(__UpperCAmelCase , 'w' , newline='' ) as f: SCREAMING_SNAKE_CASE_ = csv.DictWriter(__UpperCAmelCase , fieldnames=['col_1', 'col_2', 'col_3'] ) writer.writeheader() for item in DATA: writer.writerow(__UpperCAmelCase ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : Any ) -> List[str]: import bza SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'dataset.csv.bz2' with open(__UpperCAmelCase , 'rb' ) as f: SCREAMING_SNAKE_CASE_ = f.read() # data = bytes(FILE_CONTENT, "utf-8") with bza.open(__UpperCAmelCase , 'wb' ) as f: f.write(__UpperCAmelCase ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : List[Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Tuple ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'dataset.csv.zip' with zipfile.ZipFile(__UpperCAmelCase , 'w' ) as f: f.write(__UpperCAmelCase , arcname=os.path.basename(__UpperCAmelCase ) ) f.write(__UpperCAmelCase , arcname=os.path.basename(__UpperCAmelCase ) ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : List[Any] , __UpperCAmelCase : Any , __UpperCAmelCase : Dict ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'dataset.csv.zip' with zipfile.ZipFile(__UpperCAmelCase , 'w' ) as f: f.write(__UpperCAmelCase , arcname=os.path.basename(csv_path.replace('.csv' , '.CSV' ) ) ) f.write(__UpperCAmelCase , arcname=os.path.basename(csva_path.replace('.csv' , '.CSV' ) ) ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Any , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : List[str] ) -> Any: SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'dataset_with_dir.csv.zip' with zipfile.ZipFile(__UpperCAmelCase , 'w' ) as f: f.write(__UpperCAmelCase , arcname=os.path.join('main_dir' , os.path.basename(__UpperCAmelCase ) ) ) f.write(__UpperCAmelCase , arcname=os.path.join('main_dir' , os.path.basename(__UpperCAmelCase ) ) ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset.parquet' ) SCREAMING_SNAKE_CASE_ = pa.schema( { 'col_1': pa.string(), 'col_2': pa.intaa(), 'col_3': pa.floataa(), } ) with open(__UpperCAmelCase , 'wb' ) as f: SCREAMING_SNAKE_CASE_ = pq.ParquetWriter(__UpperCAmelCase , schema=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = pa.Table.from_pydict({k: [DATA[i][k] for i in range(len(__UpperCAmelCase ) )] for k in DATA[0]} , schema=__UpperCAmelCase ) writer.write_table(__UpperCAmelCase ) writer.close() return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Any ) -> List[str]: SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset.json' ) SCREAMING_SNAKE_CASE_ = {'data': DATA} with open(__UpperCAmelCase , 'w' ) as f: json.dump(__UpperCAmelCase , __UpperCAmelCase ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Optional[int] ) -> Tuple: SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset.json' ) SCREAMING_SNAKE_CASE_ = {'data': DATA_DICT_OF_LISTS} with open(__UpperCAmelCase , 'w' ) as f: json.dump(__UpperCAmelCase , __UpperCAmelCase ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Optional[int] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset.jsonl' ) with open(__UpperCAmelCase , 'w' ) as f: for item in DATA: f.write(json.dumps(__UpperCAmelCase ) + '\n' ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Union[str, Any] ) -> int: SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset2.jsonl' ) with open(__UpperCAmelCase , 'w' ) as f: for item in DATA: f.write(json.dumps(__UpperCAmelCase ) + '\n' ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : List[str] ) -> Dict: SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset_312.jsonl' ) with open(__UpperCAmelCase , 'w' ) as f: for item in DATA_312: f.write(json.dumps(__UpperCAmelCase ) + '\n' ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Tuple ) -> int: SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset-str.jsonl' ) with open(__UpperCAmelCase , 'w' ) as f: for item in DATA_STR: f.write(json.dumps(__UpperCAmelCase ) + '\n' ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : Optional[int] ) -> Any: import gzip SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset.txt.gz' ) with open(__UpperCAmelCase , 'rb' ) as orig_file: with gzip.open(__UpperCAmelCase , 'wb' ) as zipped_file: zipped_file.writelines(__UpperCAmelCase ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : Optional[int] ) -> Any: import gzip SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset.jsonl.gz' ) with open(__UpperCAmelCase , 'rb' ) as orig_file: with gzip.open(__UpperCAmelCase , 'wb' ) as zipped_file: zipped_file.writelines(__UpperCAmelCase ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : List[str] , __UpperCAmelCase : List[str] ) -> Any: SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'dataset.jsonl.zip' with zipfile.ZipFile(__UpperCAmelCase , 'w' ) as f: f.write(__UpperCAmelCase , arcname=os.path.basename(__UpperCAmelCase ) ) f.write(__UpperCAmelCase , arcname=os.path.basename(__UpperCAmelCase ) ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Dict , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Tuple ) -> int: SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'dataset_nested.jsonl.zip' with zipfile.ZipFile(__UpperCAmelCase , 'w' ) as f: f.write(__UpperCAmelCase , arcname=os.path.join('nested' , os.path.basename(__UpperCAmelCase ) ) ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[int] ) -> Tuple: SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'dataset_with_dir.jsonl.zip' with zipfile.ZipFile(__UpperCAmelCase , 'w' ) as f: f.write(__UpperCAmelCase , arcname=os.path.join('main_dir' , os.path.basename(__UpperCAmelCase ) ) ) f.write(__UpperCAmelCase , arcname=os.path.join('main_dir' , os.path.basename(__UpperCAmelCase ) ) ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : str , __UpperCAmelCase : List[str] ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'dataset.jsonl.tar' with tarfile.TarFile(__UpperCAmelCase , 'w' ) as f: f.add(__UpperCAmelCase , arcname=os.path.basename(__UpperCAmelCase ) ) f.add(__UpperCAmelCase , arcname=os.path.basename(__UpperCAmelCase ) ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Any , __UpperCAmelCase : List[Any] , __UpperCAmelCase : List[Any] , __UpperCAmelCase : List[str] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'dataset_nested.jsonl.tar' with tarfile.TarFile(__UpperCAmelCase , 'w' ) as f: f.add(__UpperCAmelCase , arcname=os.path.join('nested' , os.path.basename(__UpperCAmelCase ) ) ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : List[str] ) -> Dict: SCREAMING_SNAKE_CASE_ = ['0', '1', '2', '3'] SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset.txt' ) with open(__UpperCAmelCase , 'w' ) as f: for item in data: f.write(item + '\n' ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Tuple ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = ['0', '1', '2', '3'] SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset2.txt' ) with open(__UpperCAmelCase , 'w' ) as f: for item in data: f.write(item + '\n' ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Optional[int] ) -> Any: SCREAMING_SNAKE_CASE_ = ['0', '1', '2', '3'] SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'dataset.abc' with open(__UpperCAmelCase , 'w' ) as f: for item in data: f.write(item + '\n' ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Dict , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : List[str] ) -> Dict: SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'dataset.text.zip' with zipfile.ZipFile(__UpperCAmelCase , 'w' ) as f: f.write(__UpperCAmelCase , arcname=os.path.basename(__UpperCAmelCase ) ) f.write(__UpperCAmelCase , arcname=os.path.basename(__UpperCAmelCase ) ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Any , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[int] ) -> Tuple: SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'dataset_with_dir.text.zip' with zipfile.ZipFile(__UpperCAmelCase , 'w' ) as f: f.write(__UpperCAmelCase , arcname=os.path.join('main_dir' , os.path.basename(__UpperCAmelCase ) ) ) f.write(__UpperCAmelCase , arcname=os.path.join('main_dir' , os.path.basename(__UpperCAmelCase ) ) ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : List[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[int] ) -> Tuple: SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'dataset.ext.zip' with zipfile.ZipFile(__UpperCAmelCase , 'w' ) as f: f.write(__UpperCAmelCase , arcname=os.path.basename('unsupported.ext' ) ) f.write(__UpperCAmelCase , arcname=os.path.basename('unsupported_2.ext' ) ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> Dict: SCREAMING_SNAKE_CASE_ = '\n'.join(['First', 'Second\u2029with Unicode new line', 'Third'] ) SCREAMING_SNAKE_CASE_ = str(tmp_path_factory.mktemp('data' ) / 'dataset_with_unicode_new_lines.txt' ) with open(__UpperCAmelCase , 'w' , encoding='utf-8' ) as f: f.write(__UpperCAmelCase ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( ) -> str: return os.path.join('tests' , 'features' , 'data' , 'test_image_rgb.jpg' ) @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( ) -> int: return os.path.join('tests' , 'features' , 'data' , 'test_audio_44100.wav' ) @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : Tuple , __UpperCAmelCase : List[str] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data' ) / 'dataset.img.zip' with zipfile.ZipFile(__UpperCAmelCase , 'w' ) as f: f.write(__UpperCAmelCase , arcname=os.path.basename(__UpperCAmelCase ) ) f.write(__UpperCAmelCase , arcname=os.path.basename(__UpperCAmelCase ).replace('.jpg' , '2.jpg' ) ) return path @pytest.fixture(scope='session' ) def UpperCAmelCase_ ( __UpperCAmelCase : List[str] ) -> Dict: SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp('data_dir' ) (data_dir / "subdir").mkdir() with open(data_dir / 'subdir' / 'train.txt' , 'w' ) as f: f.write('foo\n' * 10 ) with open(data_dir / 'subdir' / 'test.txt' , 'w' ) as f: f.write('bar\n' * 10 ) # hidden file with open(data_dir / 'subdir' / '.test.txt' , 'w' ) as f: f.write('bar\n' * 10 ) # hidden directory (data_dir / ".subdir").mkdir() with open(data_dir / '.subdir' / 'train.txt' , 'w' ) as f: f.write('foo\n' * 10 ) with open(data_dir / '.subdir' / 'test.txt' , 'w' ) as f: f.write('bar\n' * 10 ) return data_dir
31
from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import TensorType, logging if TYPE_CHECKING: from ...onnx.config import PatchingSpec from ...tokenization_utils_base import PreTrainedTokenizerBase lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Dict = { 'allenai/longformer-base-4096': 'https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json', 'allenai/longformer-large-4096': 'https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json', 'allenai/longformer-large-4096-finetuned-triviaqa': ( 'https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json' ), 'allenai/longformer-base-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json' ), 'allenai/longformer-large-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json' ), } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "longformer" def __init__( self : Union[str, Any] , _lowerCAmelCase : Union[List[int], int] = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 1 , _lowerCAmelCase : int = 0 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 30_522 , _lowerCAmelCase : int = 768 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 3_072 , _lowerCAmelCase : str = "gelu" , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : int = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : float = 0.02 , _lowerCAmelCase : float = 1E-12 , _lowerCAmelCase : bool = False , **_lowerCAmelCase : Union[str, Any] , ): super().__init__(pad_token_id=_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = attention_window SCREAMING_SNAKE_CASE_ = sep_token_id SCREAMING_SNAKE_CASE_ = bos_token_id SCREAMING_SNAKE_CASE_ = eos_token_id SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = type_vocab_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = onnx_export class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[Any] , _lowerCAmelCase : "PretrainedConfig" , _lowerCAmelCase : str = "default" , _lowerCAmelCase : "List[PatchingSpec]" = None ): super().__init__(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = True @property def lowerCAmelCase_ ( self : Any ): if self.task == "multiple-choice": SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'choice', 2: 'sequence'} else: SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('global_attention_mask', dynamic_axis), ] ) @property def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = super().outputs if self.task == "default": SCREAMING_SNAKE_CASE_ = {0: 'batch'} return outputs @property def lowerCAmelCase_ ( self : str ): return 1E-4 @property def lowerCAmelCase_ ( self : Optional[Any] ): # needs to be >= 14 to support tril operator return max(super().default_onnx_opset , 14 ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : "PreTrainedTokenizerBase" , _lowerCAmelCase : int = -1 , _lowerCAmelCase : int = -1 , _lowerCAmelCase : bool = False , _lowerCAmelCase : Optional[TensorType] = None , ): SCREAMING_SNAKE_CASE_ = super().generate_dummy_inputs( preprocessor=_lowerCAmelCase , batch_size=_lowerCAmelCase , seq_length=_lowerCAmelCase , is_pair=_lowerCAmelCase , framework=_lowerCAmelCase ) import torch # for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64) # makes the export fail randomly SCREAMING_SNAKE_CASE_ = torch.zeros_like(inputs['input_ids'] ) # make every second token global SCREAMING_SNAKE_CASE_ = 1 return inputs
31
1
def UpperCAmelCase_ ( __UpperCAmelCase : List[Any] , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Any ) -> Union[str, Any]: if index == r: for j in range(__UpperCAmelCase ): 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 SCREAMING_SNAKE_CASE_ = arr[i] combination_util(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , index + 1 , __UpperCAmelCase , i + 1 ) # current is excluded, replace it with # next (Note that i+1 is passed, but # index is not changed) combination_util(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , i + 1 ) # The main function that prints all combinations # of size r in arr[] of size n. This function # mainly uses combinationUtil() def UpperCAmelCase_ ( __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : List[str] ) -> Optional[int]: # A temporary array to store all combination one by one SCREAMING_SNAKE_CASE_ = [0] * r # Print all combination using temporary array 'data[]' combination_util(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , 0 , __UpperCAmelCase , 0 ) if __name__ == "__main__": # Driver code to check the function above lowerCamelCase__ : Tuple = [10, 20, 30, 40, 50] print_combination(arr, len(arr), 3) # This code is contributed by Ambuj sahu
31
import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : str , *_lowerCAmelCase : Tuple , **_lowerCAmelCase : int ): warnings.warn( 'The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use MobileViTImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
1
def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('Input value must be an \'int\' type' ) SCREAMING_SNAKE_CASE_ = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
31
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'microsoft/swinv2-tiny-patch4-window8-256': ( 'https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json' ), } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "swinv2" lowercase_ = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : Dict , _lowerCAmelCase : Optional[Any]=224 , _lowerCAmelCase : Optional[int]=4 , _lowerCAmelCase : Tuple=3 , _lowerCAmelCase : Tuple=96 , _lowerCAmelCase : Dict=[2, 2, 6, 2] , _lowerCAmelCase : Optional[Any]=[3, 6, 12, 24] , _lowerCAmelCase : str=7 , _lowerCAmelCase : List[Any]=4.0 , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : List[Any]="gelu" , _lowerCAmelCase : str=False , _lowerCAmelCase : str=0.02 , _lowerCAmelCase : List[Any]=1E-5 , _lowerCAmelCase : str=32 , **_lowerCAmelCase : List[Any] , ): super().__init__(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = embed_dim SCREAMING_SNAKE_CASE_ = depths SCREAMING_SNAKE_CASE_ = len(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = num_heads SCREAMING_SNAKE_CASE_ = window_size SCREAMING_SNAKE_CASE_ = mlp_ratio SCREAMING_SNAKE_CASE_ = qkv_bias SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = drop_path_rate SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = use_absolute_embeddings SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE_ = int(embed_dim * 2 ** (len(_lowerCAmelCase ) - 1) ) SCREAMING_SNAKE_CASE_ = (0, 0, 0, 0)
31
1
from __future__ import annotations import unittest from transformers import AutoTokenizer, MBartConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFMBartForConditionalGeneration, TFMBartModel @require_tf class lowerCamelCase_ : '''simple docstring''' lowercase_ = MBartConfig lowercase_ = {} lowercase_ = "gelu" def __init__( self : List[Any] , _lowerCAmelCase : List[str] , _lowerCAmelCase : List[Any]=13 , _lowerCAmelCase : List[str]=7 , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : Optional[Any]=False , _lowerCAmelCase : Dict=99 , _lowerCAmelCase : str=32 , _lowerCAmelCase : int=2 , _lowerCAmelCase : Any=4 , _lowerCAmelCase : List[Any]=37 , _lowerCAmelCase : List[str]=0.1 , _lowerCAmelCase : Union[str, Any]=0.1 , _lowerCAmelCase : int=20 , _lowerCAmelCase : Optional[Any]=2 , _lowerCAmelCase : Optional[Any]=1 , _lowerCAmelCase : Dict=0 , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = seq_length SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_labels SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = eos_token_id SCREAMING_SNAKE_CASE_ = pad_token_id SCREAMING_SNAKE_CASE_ = bos_token_id def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) SCREAMING_SNAKE_CASE_ = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) SCREAMING_SNAKE_CASE_ = tf.concat([input_ids, eos_tensor] , axis=1 ) SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE_ = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) SCREAMING_SNAKE_CASE_ = prepare_mbart_inputs_dict(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) return config, inputs_dict def lowerCAmelCase_ ( self : str , _lowerCAmelCase : Tuple , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = TFMBartModel(config=_lowerCAmelCase ).get_decoder() SCREAMING_SNAKE_CASE_ = inputs_dict['input_ids'] SCREAMING_SNAKE_CASE_ = input_ids[:1, :] SCREAMING_SNAKE_CASE_ = inputs_dict['attention_mask'][:1, :] SCREAMING_SNAKE_CASE_ = inputs_dict['head_mask'] SCREAMING_SNAKE_CASE_ = 1 # first forward pass SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , attention_mask=_lowerCAmelCase , head_mask=_lowerCAmelCase , use_cache=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = outputs.to_tuple() SCREAMING_SNAKE_CASE_ = past_key_values[1] def UpperCAmelCase_ ( __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Dict , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Union[str, Any]=None , __UpperCAmelCase : Optional[int]=None , __UpperCAmelCase : Any=None , __UpperCAmelCase : Optional[Any]=None , ) -> List[str]: if attention_mask is None: SCREAMING_SNAKE_CASE_ = tf.cast(tf.math.not_equal(__UpperCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: SCREAMING_SNAKE_CASE_ = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: SCREAMING_SNAKE_CASE_ = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: SCREAMING_SNAKE_CASE_ = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: SCREAMING_SNAKE_CASE_ = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = (TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else () lowercase_ = (TFMBartForConditionalGeneration,) if is_tf_available() else () lowercase_ = ( { "conversational": TFMBartForConditionalGeneration, "feature-extraction": TFMBartModel, "summarization": TFMBartForConditionalGeneration, "text2text-generation": TFMBartForConditionalGeneration, "translation": TFMBartForConditionalGeneration, } if is_tf_available() else {} ) lowercase_ = True lowercase_ = False lowercase_ = False def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : Dict , _lowerCAmelCase : Any , _lowerCAmelCase : List[str] , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : str ): if pipeline_test_casse_name != "FeatureExtractionPipelineTests": # Exception encountered when calling layer '...' return True return False def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = TFMBartModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[Any] ): self.config_tester.run_common_tests() def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_lowerCAmelCase ) @require_sentencepiece @require_tokenizers @require_tf class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' lowercase_ = [ " UN Chief Says There Is No Military Solution in Syria", ] lowercase_ = [ "Şeful ONU declară că nu există o soluţie militară în Siria", ] lowercase_ = "facebook/mbart-large-en-ro" @cached_property def lowerCAmelCase_ ( self : Tuple ): return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def lowerCAmelCase_ ( self : int , **_lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.translate_src_text(**_lowerCAmelCase ) self.assertListEqual(self.expected_text , _lowerCAmelCase ) def lowerCAmelCase_ ( self : str , **_lowerCAmelCase : Any ): SCREAMING_SNAKE_CASE_ = self.tokenizer(self.src_text , **_lowerCAmelCase , return_tensors='tf' ) SCREAMING_SNAKE_CASE_ = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 ) SCREAMING_SNAKE_CASE_ = self.tokenizer.batch_decode(_lowerCAmelCase , skip_special_tokens=_lowerCAmelCase ) return generated_words @slow def lowerCAmelCase_ ( self : Any ): self._assert_generated_batch_equal_expected()
31
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch lowerCamelCase__ : Dict = random.Random() def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : Tuple=1.0 , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Dict=None ) -> Tuple: if rng is None: SCREAMING_SNAKE_CASE_ = global_rng SCREAMING_SNAKE_CASE_ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any]=7 , _lowerCAmelCase : Union[str, Any]=400 , _lowerCAmelCase : Tuple=2_000 , _lowerCAmelCase : str=1 , _lowerCAmelCase : int=0.0 , _lowerCAmelCase : Optional[Any]=16_000 , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Any=80 , _lowerCAmelCase : Union[str, Any]=16 , _lowerCAmelCase : List[str]=64 , _lowerCAmelCase : List[Any]="hann_window" , _lowerCAmelCase : Any=80 , _lowerCAmelCase : List[Any]=7_600 , _lowerCAmelCase : List[Any]=1E-10 , _lowerCAmelCase : Optional[Any]=True , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = min_seq_length SCREAMING_SNAKE_CASE_ = max_seq_length SCREAMING_SNAKE_CASE_ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE_ = feature_size SCREAMING_SNAKE_CASE_ = padding_value SCREAMING_SNAKE_CASE_ = sampling_rate SCREAMING_SNAKE_CASE_ = do_normalize SCREAMING_SNAKE_CASE_ = num_mel_bins SCREAMING_SNAKE_CASE_ = hop_length SCREAMING_SNAKE_CASE_ = win_length SCREAMING_SNAKE_CASE_ = win_function SCREAMING_SNAKE_CASE_ = fmin SCREAMING_SNAKE_CASE_ = fmax SCREAMING_SNAKE_CASE_ = mel_floor SCREAMING_SNAKE_CASE_ = return_attention_mask def lowerCAmelCase_ ( self : Union[str, Any] ): return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : str=False ): def _flatten(_lowerCAmelCase : Dict ): return list(itertools.chain(*_lowerCAmelCase ) ) if equal_length: SCREAMING_SNAKE_CASE_ = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Union[str, Any]=False , _lowerCAmelCase : Optional[int]=False ): if equal_length: SCREAMING_SNAKE_CASE_ = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = SpeechTaFeatureExtractor def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractionTester(self ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : int ): self.assertTrue(np.all(np.mean(_lowerCAmelCase , axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_lowerCAmelCase , axis=0 ) - 1 ) < 1E-3 ) ) def lowerCAmelCase_ ( self : List[Any] ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE_ = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , padding=_lowerCAmelCase , max_length=_lowerCAmelCase , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self.assertTrue(input_values[0][800:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self.assertTrue(input_values[0][1_000:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = range(800 , 1_400 , 200 ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in lengths] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , max_length=_lowerCAmelCase , padding=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='max_length' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1_000) ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=2_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1_200) ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = np.random.rand(100 ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def lowerCAmelCase_ ( self : Tuple ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test feature size SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , padding=_lowerCAmelCase , return_tensors='np' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input SCREAMING_SNAKE_CASE_ = feature_extractor(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE_ = np.asarray(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_lowerCAmelCase ) == len(_lowerCAmelCase ) for x, y in zip(_lowerCAmelCase , processed_features[input_name] ) ) ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='np' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='pt' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' )[input_name] SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='pt' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = min(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad( _lowerCAmelCase , padding='max_length' , max_length=_lowerCAmelCase , truncation=_lowerCAmelCase , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Tuple ): from datasets import load_dataset SCREAMING_SNAKE_CASE_ = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE_ = ds.sort('id' ).select(range(_lowerCAmelCase ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] def lowerCAmelCase_ ( self : Any ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [2.3_804E-03, 2.0_752E-03, 1.9_836E-03, 2.1_057E-03, 1.6_174E-03, 3.0_518E-04, 9.1_553E-05, 3.3_569E-04, 9.7_656E-04, 1.8_311E-03, 2.0_142E-03, 2.1_057E-03, 1.7_395E-03, 4.5_776E-04, -3.9_673E-04, 4.5_776E-04, 1.0_071E-03, 9.1_553E-05, 4.8_828E-04, 1.1_597E-03, 7.3_242E-04, 9.4_604E-04, 1.8_005E-03, 1.8_311E-03, 8.8_501E-04, 4.2_725E-04, 4.8_828E-04, 7.3_242E-04, 1.0_986E-03, 2.1_057E-03] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 93_680) ) self.assertTrue(torch.allclose(input_values[0, :30] , _lowerCAmelCase , atol=1E-6 ) ) def lowerCAmelCase_ ( self : Optional[int] ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [-2.6870, -3.0104, -3.1356, -3.5352, -3.0044, -3.0353, -3.4719, -3.6777, -3.1520, -2.9435, -2.6553, -2.8795, -2.9944, -2.5921, -3.0279, -3.0386, -3.0864, -3.1291, -3.2353, -2.7444, -2.6831, -2.7287, -3.1761, -3.1571, -3.2726, -3.0582, -3.1007, -3.4533, -3.4695, -3.0998] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 366, 80) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] , _lowerCAmelCase , atol=1E-4 ) )
31
1
from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 lowerCamelCase__ : Tuple = { # 1536-bit 5: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 2048-bit 14: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AACAA68FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 3072-bit 15: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 4096-bit 16: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7' + '88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA' + '2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6' + '287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED' + '1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9' + '93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199' + 'FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 6144-bit 17: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08' + '8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B' + '302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9' + 'A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6' + '49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8' + 'FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C' + '180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718' + '3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D' + '04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D' + 'B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226' + '1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC' + 'E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26' + '99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB' + '04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2' + '233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127' + 'D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492' + '36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406' + 'AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918' + 'DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151' + '2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03' + 'F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F' + 'BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA' + 'CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B' + 'B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632' + '387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E' + '6DCC4024FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 8192-bit 18: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7' + '88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA' + '2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6' + '287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED' + '1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9' + '93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492' + '36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD' + 'F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831' + '179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B' + 'DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF' + '5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6' + 'D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3' + '23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA' + 'CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328' + '06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C' + 'DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE' + '12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4' + '38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300' + '741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568' + '3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9' + '22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B' + '4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A' + '062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36' + '4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1' + 'B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92' + '4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47' + '9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71' + '60C980DD98EDD3DFFFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, } class lowerCamelCase_ : '''simple docstring''' def __init__( self : str , _lowerCAmelCase : int = 14 ): if group not in primes: raise ValueError('Unsupported Group' ) SCREAMING_SNAKE_CASE_ = primes[group]['prime'] SCREAMING_SNAKE_CASE_ = primes[group]['generator'] SCREAMING_SNAKE_CASE_ = int(hexlify(urandom(32 ) ) , base=16 ) def lowerCAmelCase_ ( self : List[Any] ): return hex(self.__private_key )[2:] def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = pow(self.generator , self.__private_key , self.prime ) return hex(_lowerCAmelCase )[2:] def lowerCAmelCase_ ( self : int , _lowerCAmelCase : int ): # check if the other public key is valid based on NIST SP800-56 return ( 2 <= key <= self.prime - 2 and pow(_lowerCAmelCase , (self.prime - 1) // 2 , self.prime ) == 1 ) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : str ): SCREAMING_SNAKE_CASE_ = int(_lowerCAmelCase , base=16 ) if not self.is_valid_public_key(_lowerCAmelCase ): raise ValueError('Invalid public key' ) SCREAMING_SNAKE_CASE_ = pow(_lowerCAmelCase , self.__private_key , self.prime ) return shaaaa(str(_lowerCAmelCase ).encode() ).hexdigest() @staticmethod def lowerCAmelCase_ ( _lowerCAmelCase : int , _lowerCAmelCase : int ): # check if the other public key is valid based on NIST SP800-56 return ( 2 <= remote_public_key_str <= prime - 2 and pow(_lowerCAmelCase , (prime - 1) // 2 , _lowerCAmelCase ) == 1 ) @staticmethod def lowerCAmelCase_ ( _lowerCAmelCase : str , _lowerCAmelCase : str , _lowerCAmelCase : int = 14 ): SCREAMING_SNAKE_CASE_ = int(_lowerCAmelCase , base=16 ) SCREAMING_SNAKE_CASE_ = int(_lowerCAmelCase , base=16 ) SCREAMING_SNAKE_CASE_ = primes[group]['prime'] if not DiffieHellman.is_valid_public_key_static(_lowerCAmelCase , _lowerCAmelCase ): raise ValueError('Invalid public key' ) SCREAMING_SNAKE_CASE_ = pow(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) return shaaaa(str(_lowerCAmelCase ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
31
from __future__ import annotations from typing import TypedDict class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = 42 lowercase_ = 42 def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> list[str]: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) return [s[i:] + s[:i] for i in range(len(__UpperCAmelCase ) )] def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> BWTTransformDict: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) if not s: raise ValueError('The parameter s must not be empty.' ) SCREAMING_SNAKE_CASE_ = all_rotations(__UpperCAmelCase ) rotations.sort() # sort the list of rotations in alphabetically order # make a string composed of the last char of each rotation SCREAMING_SNAKE_CASE_ = { "bwt_string": "".join([word[-1] for word in rotations] ), "idx_original_string": rotations.index(__UpperCAmelCase ), } return response def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : int ) -> str: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter bwt_string type must be str.' ) if not bwt_string: raise ValueError('The parameter bwt_string must not be empty.' ) try: SCREAMING_SNAKE_CASE_ = int(__UpperCAmelCase ) except ValueError: raise TypeError( 'The parameter idx_original_string type must be int or passive' ' of cast to int.' ) if idx_original_string < 0: raise ValueError('The parameter idx_original_string must not be lower than 0.' ) if idx_original_string >= len(__UpperCAmelCase ): raise ValueError( 'The parameter idx_original_string must be lower than' ' len(bwt_string).' ) SCREAMING_SNAKE_CASE_ = [''] * len(__UpperCAmelCase ) for _ in range(len(__UpperCAmelCase ) ): for i in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = bwt_string[i] + ordered_rotations[i] ordered_rotations.sort() return ordered_rotations[idx_original_string] if __name__ == "__main__": lowerCamelCase__ : Optional[int] = 'Provide a string that I will generate its BWT transform: ' lowerCamelCase__ : List[str] = input(entry_msg).strip() lowerCamelCase__ : int = bwt_transform(s) print( f'''Burrows Wheeler transform for string \'{s}\' results ''' f'''in \'{result['bwt_string']}\'''' ) lowerCamelCase__ : Dict = reverse_bwt(result['bwt_string'], result['idx_original_string']) print( f'''Reversing Burrows Wheeler transform for entry \'{result['bwt_string']}\' ''' f'''we get original string \'{original_string}\'''' )
31
1
import time import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch, torch_device from ..test_modeling_common import ids_tensor if is_torch_available(): import torch from transformers.generation import ( MaxLengthCriteria, MaxNewTokensCriteria, MaxTimeCriteria, StoppingCriteriaList, validate_stopping_criteria, ) @require_torch class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : int , _lowerCAmelCase : List[str] ): SCREAMING_SNAKE_CASE_ = 3 SCREAMING_SNAKE_CASE_ = 250 SCREAMING_SNAKE_CASE_ = ids_tensor((batch_size, length) , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.ones((batch_size, length) , device=_lowerCAmelCase , dtype=torch.float ) / length return input_ids, scores def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self._get_tensors(5 ) SCREAMING_SNAKE_CASE_ = StoppingCriteriaList( [ MaxLengthCriteria(max_length=10 ), MaxTimeCriteria(max_time=0.1 ), ] ) self.assertFalse(criteria(_lowerCAmelCase , _lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self._get_tensors(9 ) self.assertFalse(criteria(_lowerCAmelCase , _lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self._get_tensors(10 ) self.assertTrue(criteria(_lowerCAmelCase , _lowerCAmelCase ) ) def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = MaxLengthCriteria(max_length=10 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self._get_tensors(5 ) self.assertFalse(criteria(_lowerCAmelCase , _lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self._get_tensors(9 ) self.assertFalse(criteria(_lowerCAmelCase , _lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self._get_tensors(10 ) self.assertTrue(criteria(_lowerCAmelCase , _lowerCAmelCase ) ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = MaxNewTokensCriteria(start_length=5 , max_new_tokens=5 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self._get_tensors(5 ) self.assertFalse(criteria(_lowerCAmelCase , _lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self._get_tensors(9 ) self.assertFalse(criteria(_lowerCAmelCase , _lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self._get_tensors(10 ) self.assertTrue(criteria(_lowerCAmelCase , _lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ = StoppingCriteriaList([criteria] ) self.assertEqual(criteria_list.max_length , 10 ) def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self._get_tensors(5 ) SCREAMING_SNAKE_CASE_ = MaxTimeCriteria(max_time=0.1 ) self.assertFalse(criteria(_lowerCAmelCase , _lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ = MaxTimeCriteria(max_time=0.1 , initial_timestamp=time.time() - 0.2 ) self.assertTrue(criteria(_lowerCAmelCase , _lowerCAmelCase ) ) def lowerCAmelCase_ ( self : str ): validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10 )] ) , 10 ) with self.assertWarns(_lowerCAmelCase ): validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10 )] ) , 11 ) SCREAMING_SNAKE_CASE_ = validate_stopping_criteria(StoppingCriteriaList() , 11 ) self.assertEqual(len(_lowerCAmelCase ) , 1 )
31
class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = {} def lowerCAmelCase_ ( self : List[str] ): print(self.vertex ) for i in self.vertex: print(_lowerCAmelCase , ' -> ' , ' -> '.join([str(_lowerCAmelCase ) for j in self.vertex[i]] ) ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int , _lowerCAmelCase : int ): # check if vertex is already present, if from_vertex in self.vertex: self.vertex[from_vertex].append(_lowerCAmelCase ) else: # else make a new vertex SCREAMING_SNAKE_CASE_ = [to_vertex] def lowerCAmelCase_ ( self : Optional[Any] ): # visited array for storing already visited nodes SCREAMING_SNAKE_CASE_ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : int , _lowerCAmelCase : list ): # mark start vertex as visited SCREAMING_SNAKE_CASE_ = True print(_lowerCAmelCase , end=' ' ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) if __name__ == "__main__": lowerCamelCase__ : List[Any] = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('DFS:') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
31
1
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : str = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/config.json', 'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json', 'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/config.json', 'funnel-transformer/medium-base': 'https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json', 'funnel-transformer/intermediate': ( 'https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json' ), 'funnel-transformer/intermediate-base': ( 'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json' ), 'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/config.json', 'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json', 'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json', 'funnel-transformer/xlarge-base': 'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json', } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "funnel" lowercase_ = { "hidden_size": "d_model", "num_attention_heads": "n_head", } def __init__( self : int , _lowerCAmelCase : Optional[int]=30_522 , _lowerCAmelCase : List[str]=[4, 4, 4] , _lowerCAmelCase : Tuple=None , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : int=768 , _lowerCAmelCase : Optional[Any]=12 , _lowerCAmelCase : Optional[Any]=64 , _lowerCAmelCase : Optional[Any]=3_072 , _lowerCAmelCase : List[str]="gelu_new" , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : int=0.1 , _lowerCAmelCase : Tuple=0.0 , _lowerCAmelCase : List[Any]=0.1 , _lowerCAmelCase : Dict=None , _lowerCAmelCase : str=1E-9 , _lowerCAmelCase : Any="mean" , _lowerCAmelCase : Union[str, Any]="relative_shift" , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Dict=True , _lowerCAmelCase : Tuple=True , **_lowerCAmelCase : Optional[Any] , ): SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = block_sizes SCREAMING_SNAKE_CASE_ = [1] * len(_lowerCAmelCase ) if block_repeats is None else block_repeats assert len(_lowerCAmelCase ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." SCREAMING_SNAKE_CASE_ = num_decoder_layers SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = n_head SCREAMING_SNAKE_CASE_ = d_head SCREAMING_SNAKE_CASE_ = d_inner SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = initializer_std SCREAMING_SNAKE_CASE_ = layer_norm_eps assert pooling_type in [ "mean", "max", ], F"Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported." SCREAMING_SNAKE_CASE_ = pooling_type assert attention_type in [ "relative_shift", "factorized", ], F"Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported." SCREAMING_SNAKE_CASE_ = attention_type SCREAMING_SNAKE_CASE_ = separate_cls SCREAMING_SNAKE_CASE_ = truncate_seq SCREAMING_SNAKE_CASE_ = pool_q_only super().__init__(**_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : Optional[int] ): return sum(self.block_sizes ) @num_hidden_layers.setter def lowerCAmelCase_ ( self : int , _lowerCAmelCase : List[Any] ): raise NotImplementedError( 'This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.' ) @property def lowerCAmelCase_ ( self : List[Any] ): return len(self.block_sizes ) @num_blocks.setter def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any] ): raise NotImplementedError('This model does not support the setting of `num_blocks`. Please set `block_sizes`.' )
31
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : str = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/config.json', 'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json', 'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/config.json', 'funnel-transformer/medium-base': 'https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json', 'funnel-transformer/intermediate': ( 'https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json' ), 'funnel-transformer/intermediate-base': ( 'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json' ), 'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/config.json', 'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json', 'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json', 'funnel-transformer/xlarge-base': 'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json', } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "funnel" lowercase_ = { "hidden_size": "d_model", "num_attention_heads": "n_head", } def __init__( self : int , _lowerCAmelCase : Optional[int]=30_522 , _lowerCAmelCase : List[str]=[4, 4, 4] , _lowerCAmelCase : Tuple=None , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : int=768 , _lowerCAmelCase : Optional[Any]=12 , _lowerCAmelCase : Optional[Any]=64 , _lowerCAmelCase : Optional[Any]=3_072 , _lowerCAmelCase : List[str]="gelu_new" , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : int=0.1 , _lowerCAmelCase : Tuple=0.0 , _lowerCAmelCase : List[Any]=0.1 , _lowerCAmelCase : Dict=None , _lowerCAmelCase : str=1E-9 , _lowerCAmelCase : Any="mean" , _lowerCAmelCase : Union[str, Any]="relative_shift" , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Dict=True , _lowerCAmelCase : Tuple=True , **_lowerCAmelCase : Optional[Any] , ): SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = block_sizes SCREAMING_SNAKE_CASE_ = [1] * len(_lowerCAmelCase ) if block_repeats is None else block_repeats assert len(_lowerCAmelCase ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." SCREAMING_SNAKE_CASE_ = num_decoder_layers SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = n_head SCREAMING_SNAKE_CASE_ = d_head SCREAMING_SNAKE_CASE_ = d_inner SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = initializer_std SCREAMING_SNAKE_CASE_ = layer_norm_eps assert pooling_type in [ "mean", "max", ], F"Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported." SCREAMING_SNAKE_CASE_ = pooling_type assert attention_type in [ "relative_shift", "factorized", ], F"Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported." SCREAMING_SNAKE_CASE_ = attention_type SCREAMING_SNAKE_CASE_ = separate_cls SCREAMING_SNAKE_CASE_ = truncate_seq SCREAMING_SNAKE_CASE_ = pool_q_only super().__init__(**_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : Optional[int] ): return sum(self.block_sizes ) @num_hidden_layers.setter def lowerCAmelCase_ ( self : int , _lowerCAmelCase : List[Any] ): raise NotImplementedError( 'This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.' ) @property def lowerCAmelCase_ ( self : List[Any] ): return len(self.block_sizes ) @num_blocks.setter def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any] ): raise NotImplementedError('This model does not support the setting of `num_blocks`. Please set `block_sizes`.' )
31
1
import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( 'The `image_to_image.py` script is outdated. Please use directly `from diffusers import' ' StableDiffusionImg2ImgPipeline` instead.' )
31
from __future__ import annotations from collections.abc import Iterator class lowerCamelCase_ : '''simple docstring''' def __init__( self : Union[str, Any] , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = value SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None class lowerCamelCase_ : '''simple docstring''' def __init__( self : int , _lowerCAmelCase : Node ): SCREAMING_SNAKE_CASE_ = tree def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Node | None ): if node is None: return 0 return node.value + ( self.depth_first_search(node.left ) + self.depth_first_search(node.right ) ) def __iter__( self : Dict ): yield self.depth_first_search(self.tree ) if __name__ == "__main__": import doctest doctest.testmod()
31
1
def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: assert isinstance(__UpperCAmelCase , __UpperCAmelCase ), f"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE_ = f"The input value of [n={number}] has to be > 0" raise ValueError(__UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_ = sylvester(number - 1 ) SCREAMING_SNAKE_CASE_ = num - 1 SCREAMING_SNAKE_CASE_ = num return lower * upper + 1 if __name__ == "__main__": print(f'''The 8th number in Sylvester\'s sequence: {sylvester(8)}''')
31
def UpperCAmelCase_ ( __UpperCAmelCase : list , __UpperCAmelCase : int , __UpperCAmelCase : int = 0 , __UpperCAmelCase : int = 0 ) -> int: SCREAMING_SNAKE_CASE_ = right or len(__UpperCAmelCase ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(__UpperCAmelCase , __UpperCAmelCase , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
31
1
from __future__ import annotations import requests lowerCamelCase__ : int = set( 'approved_at_utc approved_by author_flair_background_color\nauthor_flair_css_class author_flair_richtext author_flair_template_id author_fullname\nauthor_premium can_mod_post category clicked content_categories created_utc downs\nedited gilded gildings hidden hide_score is_created_from_ads_ui is_meta\nis_original_content is_reddit_media_domain is_video link_flair_css_class\nlink_flair_richtext link_flair_text link_flair_text_color media_embed mod_reason_title\nname permalink pwls quarantine saved score secure_media secure_media_embed selftext\nsubreddit subreddit_name_prefixed subreddit_type thumbnail title top_awarded_type\ntotal_awards_received ups upvote_ratio url user_reports'.split() ) def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : int = 1 , __UpperCAmelCase : str = "new" , __UpperCAmelCase : list | None = None ) -> dict: SCREAMING_SNAKE_CASE_ = wanted_data or [] if invalid_search_terms := ", ".join(sorted(set(__UpperCAmelCase ) - valid_terms ) ): SCREAMING_SNAKE_CASE_ = f"Invalid search term: {invalid_search_terms}" raise ValueError(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = requests.get( f"https://reddit.com/r/{subreddit}/{age}.json?limit={limit}" , headers={'User-agent': 'A random string'} , ) if response.status_code == 4_29: raise requests.HTTPError SCREAMING_SNAKE_CASE_ = response.json() if not wanted_data: return {id_: data["data"]["children"][id_] for id_ in range(__UpperCAmelCase )} SCREAMING_SNAKE_CASE_ = {} for id_ in range(__UpperCAmelCase ): SCREAMING_SNAKE_CASE_ = { item: data['data']['children'][id_]['data'][item] for item in wanted_data } return data_dict if __name__ == "__main__": # If you get Error 429, that means you are rate limited.Try after some time print(get_subreddit_data('learnpython', wanted_data=['title', 'url', 'selftext']))
31
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_fnet import FNetTokenizer else: lowerCamelCase__ : Optional[Any] = None lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : List[str] = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} lowerCamelCase__ : List[str] = { 'vocab_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/spiece.model', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/spiece.model', }, 'tokenizer_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json', }, } lowerCamelCase__ : Optional[Any] = { 'google/fnet-base': 512, 'google/fnet-large': 512, } lowerCamelCase__ : List[Any] = '▁' class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = ["input_ids", "token_type_ids"] lowercase_ = FNetTokenizer def __init__( self : List[Any] , _lowerCAmelCase : Dict=None , _lowerCAmelCase : Dict=None , _lowerCAmelCase : List[str]=False , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Tuple=True , _lowerCAmelCase : List[Any]="<unk>" , _lowerCAmelCase : Optional[Any]="[SEP]" , _lowerCAmelCase : Optional[Any]="<pad>" , _lowerCAmelCase : Optional[int]="[CLS]" , _lowerCAmelCase : Optional[Any]="[MASK]" , **_lowerCAmelCase : 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. SCREAMING_SNAKE_CASE_ = ( AddedToken(_lowerCAmelCase , lstrip=_lowerCAmelCase , rstrip=_lowerCAmelCase , normalized=_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ) else mask_token ) super().__init__( _lowerCAmelCase , tokenizer_file=_lowerCAmelCase , do_lower_case=_lowerCAmelCase , remove_space=_lowerCAmelCase , keep_accents=_lowerCAmelCase , unk_token=_lowerCAmelCase , sep_token=_lowerCAmelCase , pad_token=_lowerCAmelCase , cls_token=_lowerCAmelCase , mask_token=_lowerCAmelCase , **_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = do_lower_case SCREAMING_SNAKE_CASE_ = remove_space SCREAMING_SNAKE_CASE_ = keep_accents SCREAMING_SNAKE_CASE_ = vocab_file SCREAMING_SNAKE_CASE_ = False if not self.vocab_file else True def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None ): if not os.path.isdir(_lowerCAmelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE_ = os.path.join( _lowerCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_lowerCAmelCase ): copyfile(self.vocab_file , _lowerCAmelCase ) return (out_vocab_file,)
31
1
import html from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin from ...utils import is_bsa_available, logging, requires_backends if is_bsa_available(): import bsa from bsa import BeautifulSoup lowerCamelCase__ : List[str] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Any , **_lowerCAmelCase : List[str] ): requires_backends(self , ['bs4'] ) super().__init__(**_lowerCAmelCase ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = element if element.name else element.parent for parent in child.parents: # type: bs4.element.Tag SCREAMING_SNAKE_CASE_ = parent.find_all(child.name , recursive=_lowerCAmelCase ) xpath_tags.append(child.name ) xpath_subscripts.append( 0 if 1 == len(_lowerCAmelCase ) else next(i for i, s in enumerate(_lowerCAmelCase , 1 ) if s is child ) ) SCREAMING_SNAKE_CASE_ = parent xpath_tags.reverse() xpath_subscripts.reverse() return xpath_tags, xpath_subscripts def lowerCAmelCase_ ( self : int , _lowerCAmelCase : str ): SCREAMING_SNAKE_CASE_ = BeautifulSoup(_lowerCAmelCase , 'html.parser' ) SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = [] for element in html_code.descendants: if type(_lowerCAmelCase ) == bsa.element.NavigableString: if type(element.parent ) != bsa.element.Tag: continue SCREAMING_SNAKE_CASE_ = html.unescape(_lowerCAmelCase ).strip() if not text_in_this_tag: continue all_doc_strings.append(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.xpath_soup(_lowerCAmelCase ) stringaxtag_seq.append(_lowerCAmelCase ) stringaxsubs_seq.append(_lowerCAmelCase ) if len(_lowerCAmelCase ) != len(_lowerCAmelCase ): raise ValueError('Number of doc strings and xtags does not correspond' ) if len(_lowerCAmelCase ) != len(_lowerCAmelCase ): raise ValueError('Number of doc strings and xsubs does not correspond' ) return all_doc_strings, stringaxtag_seq, stringaxsubs_seq def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Dict ): SCREAMING_SNAKE_CASE_ = '' for tagname, subs in zip(_lowerCAmelCase , _lowerCAmelCase ): xpath += F"/{tagname}" if subs != 0: xpath += F"[{subs}]" return xpath def __call__( self : Optional[int] , _lowerCAmelCase : Dict ): SCREAMING_SNAKE_CASE_ = False # Check that strings has a valid type if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = True elif isinstance(_lowerCAmelCase , (list, tuple) ): if len(_lowerCAmelCase ) == 0 or isinstance(html_strings[0] , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = True if not valid_strings: raise ValueError( 'HTML strings must of type `str`, `List[str]` (batch of examples), ' F"but is of type {type(_lowerCAmelCase )}." ) SCREAMING_SNAKE_CASE_ = bool(isinstance(_lowerCAmelCase , (list, tuple) ) and (isinstance(html_strings[0] , _lowerCAmelCase )) ) if not is_batched: SCREAMING_SNAKE_CASE_ = [html_strings] # Get nodes + xpaths SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = [] for html_string in html_strings: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.get_three_from_single(_lowerCAmelCase ) nodes.append(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = [] for node, tag_list, sub_list in zip(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = self.construct_xpath(_lowerCAmelCase , _lowerCAmelCase ) xpath_strings.append(_lowerCAmelCase ) xpaths.append(_lowerCAmelCase ) # return as Dict SCREAMING_SNAKE_CASE_ = {'nodes': nodes, 'xpaths': xpaths} SCREAMING_SNAKE_CASE_ = BatchFeature(data=_lowerCAmelCase , tensor_type=_lowerCAmelCase ) return encoded_inputs
31
from __future__ import annotations from collections.abc import Generator def UpperCAmelCase_ ( ) -> Generator[int, None, None]: SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = 2 while True: SCREAMING_SNAKE_CASE_ = factor_map.pop(__UpperCAmelCase , __UpperCAmelCase ) if factor: SCREAMING_SNAKE_CASE_ = factor + prime while x in factor_map: x += factor SCREAMING_SNAKE_CASE_ = factor else: SCREAMING_SNAKE_CASE_ = prime yield prime prime += 1 def UpperCAmelCase_ ( __UpperCAmelCase : float = 1E10 ) -> int: SCREAMING_SNAKE_CASE_ = sieve() SCREAMING_SNAKE_CASE_ = 1 while True: SCREAMING_SNAKE_CASE_ = next(__UpperCAmelCase ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(__UpperCAmelCase ) n += 2 if __name__ == "__main__": print(solution())
31
1
import inspect from typing import Callable, List, Optional, Union import torch from transformers import ( CLIPImageProcessor, CLIPTextModel, CLIPTokenizer, WhisperForConditionalGeneration, WhisperProcessor, ) from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.utils import logging lowerCamelCase__ : int = logging.get_logger(__name__) # pylint: disable=invalid-name class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : int , _lowerCAmelCase : WhisperForConditionalGeneration , _lowerCAmelCase : WhisperProcessor , _lowerCAmelCase : AutoencoderKL , _lowerCAmelCase : CLIPTextModel , _lowerCAmelCase : CLIPTokenizer , _lowerCAmelCase : UNetaDConditionModel , _lowerCAmelCase : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , _lowerCAmelCase : StableDiffusionSafetyChecker , _lowerCAmelCase : CLIPImageProcessor , ): super().__init__() if safety_checker is None: logger.warning( F"You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure" ' that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered' ' results in services or applications open to the public. Both the diffusers team and Hugging Face' ' strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling' ' it only for use-cases that involve analyzing network behavior or auditing its results. For more' ' information, please have a look at https://github.com/huggingface/diffusers/pull/254 .' ) self.register_modules( speech_model=_lowerCAmelCase , speech_processor=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , feature_extractor=_lowerCAmelCase , ) def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : Optional[Union[str, int]] = "auto" ): if slice_size == "auto": SCREAMING_SNAKE_CASE_ = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(_lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple ): self.enable_attention_slicing(_lowerCAmelCase ) @torch.no_grad() def __call__( self : Dict , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Dict=16_000 , _lowerCAmelCase : int = 512 , _lowerCAmelCase : int = 512 , _lowerCAmelCase : int = 50 , _lowerCAmelCase : float = 7.5 , _lowerCAmelCase : Optional[Union[str, List[str]]] = None , _lowerCAmelCase : Optional[int] = 1 , _lowerCAmelCase : float = 0.0 , _lowerCAmelCase : Optional[torch.Generator] = None , _lowerCAmelCase : Optional[torch.FloatTensor] = None , _lowerCAmelCase : Optional[str] = "pil" , _lowerCAmelCase : bool = True , _lowerCAmelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , _lowerCAmelCase : int = 1 , **_lowerCAmelCase : List[str] , ): SCREAMING_SNAKE_CASE_ = self.speech_processor.feature_extractor( _lowerCAmelCase , return_tensors='pt' , sampling_rate=_lowerCAmelCase ).input_features.to(self.device ) SCREAMING_SNAKE_CASE_ = self.speech_model.generate(_lowerCAmelCase , max_length=480_000 ) SCREAMING_SNAKE_CASE_ = self.speech_processor.tokenizer.batch_decode(_lowerCAmelCase , skip_special_tokens=_lowerCAmelCase , normalize=_lowerCAmelCase )[ 0 ] if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = 1 elif isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = len(_lowerCAmelCase ) else: raise ValueError(F"`prompt` has to be of type `str` or `list` but is {type(_lowerCAmelCase )}" ) if height % 8 != 0 or width % 8 != 0: raise ValueError(F"`height` and `width` have to be divisible by 8 but are {height} and {width}." ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(_lowerCAmelCase , _lowerCAmelCase ) or callback_steps <= 0) ): raise ValueError( F"`callback_steps` has to be a positive integer but is {callback_steps} of type" F" {type(_lowerCAmelCase )}." ) # get prompt text embeddings SCREAMING_SNAKE_CASE_ = self.tokenizer( _lowerCAmelCase , padding='max_length' , max_length=self.tokenizer.model_max_length , return_tensors='pt' , ) SCREAMING_SNAKE_CASE_ = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: SCREAMING_SNAKE_CASE_ = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( 'The following part of your input was truncated because CLIP can only handle sequences up to' F" {self.tokenizer.model_max_length} tokens: {removed_text}" ) SCREAMING_SNAKE_CASE_ = text_input_ids[:, : self.tokenizer.model_max_length] SCREAMING_SNAKE_CASE_ = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = text_embeddings.shape SCREAMING_SNAKE_CASE_ = text_embeddings.repeat(1 , _lowerCAmelCase , 1 ) SCREAMING_SNAKE_CASE_ = text_embeddings.view(bs_embed * num_images_per_prompt , _lowerCAmelCase , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. SCREAMING_SNAKE_CASE_ = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: SCREAMING_SNAKE_CASE_ = 42 if negative_prompt is None: SCREAMING_SNAKE_CASE_ = [''] * batch_size elif type(_lowerCAmelCase ) is not type(_lowerCAmelCase ): raise TypeError( F"`negative_prompt` should be the same type to `prompt`, but got {type(_lowerCAmelCase )} !=" F" {type(_lowerCAmelCase )}." ) elif isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = [negative_prompt] elif batch_size != len(_lowerCAmelCase ): raise ValueError( F"`negative_prompt`: {negative_prompt} has batch size {len(_lowerCAmelCase )}, but `prompt`:" F" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches" ' the batch size of `prompt`.' ) else: SCREAMING_SNAKE_CASE_ = negative_prompt SCREAMING_SNAKE_CASE_ = text_input_ids.shape[-1] SCREAMING_SNAKE_CASE_ = self.tokenizer( _lowerCAmelCase , padding='max_length' , max_length=_lowerCAmelCase , truncation=_lowerCAmelCase , return_tensors='pt' , ) SCREAMING_SNAKE_CASE_ = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method SCREAMING_SNAKE_CASE_ = uncond_embeddings.shape[1] SCREAMING_SNAKE_CASE_ = uncond_embeddings.repeat(1 , _lowerCAmelCase , 1 ) SCREAMING_SNAKE_CASE_ = uncond_embeddings.view(batch_size * num_images_per_prompt , _lowerCAmelCase , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes SCREAMING_SNAKE_CASE_ = 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`. SCREAMING_SNAKE_CASE_ = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) SCREAMING_SNAKE_CASE_ = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps SCREAMING_SNAKE_CASE_ = torch.randn(_lowerCAmelCase , generator=_lowerCAmelCase , device='cpu' , dtype=_lowerCAmelCase ).to( self.device ) else: SCREAMING_SNAKE_CASE_ = torch.randn(_lowerCAmelCase , generator=_lowerCAmelCase , device=self.device , dtype=_lowerCAmelCase ) else: if latents.shape != latents_shape: raise ValueError(F"Unexpected latents shape, got {latents.shape}, expected {latents_shape}" ) SCREAMING_SNAKE_CASE_ = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(_lowerCAmelCase ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand SCREAMING_SNAKE_CASE_ = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler SCREAMING_SNAKE_CASE_ = 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] SCREAMING_SNAKE_CASE_ = 'eta' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) SCREAMING_SNAKE_CASE_ = {} if accepts_eta: SCREAMING_SNAKE_CASE_ = eta for i, t in enumerate(self.progress_bar(_lowerCAmelCase ) ): # expand the latents if we are doing classifier free guidance SCREAMING_SNAKE_CASE_ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents SCREAMING_SNAKE_CASE_ = self.scheduler.scale_model_input(_lowerCAmelCase , _lowerCAmelCase ) # predict the noise residual SCREAMING_SNAKE_CASE_ = self.unet(_lowerCAmelCase , _lowerCAmelCase , encoder_hidden_states=_lowerCAmelCase ).sample # perform guidance if do_classifier_free_guidance: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = noise_pred.chunk(2 ) SCREAMING_SNAKE_CASE_ = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 SCREAMING_SNAKE_CASE_ = self.scheduler.step(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , **_lowerCAmelCase ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 1 / 0.1_8215 * latents SCREAMING_SNAKE_CASE_ = self.vae.decode(_lowerCAmelCase ).sample SCREAMING_SNAKE_CASE_ = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 SCREAMING_SNAKE_CASE_ = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": SCREAMING_SNAKE_CASE_ = self.numpy_to_pil(_lowerCAmelCase ) if not return_dict: return image return StableDiffusionPipelineOutput(images=_lowerCAmelCase , nsfw_content_detected=_lowerCAmelCase )
31
import numpy as np import torch from torch.utils.data import DataLoader from accelerate.utils.dataclasses import DistributedType class lowerCamelCase_ : '''simple docstring''' def __init__( self : Any , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : Any=3 , _lowerCAmelCase : Tuple=64 , _lowerCAmelCase : List[str]=None ): SCREAMING_SNAKE_CASE_ = np.random.default_rng(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = length SCREAMING_SNAKE_CASE_ = rng.normal(size=(length,) ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = a * self.x + b + rng.normal(scale=0.1 , size=(length,) ).astype(np.floataa ) def __len__( self : Optional[int] ): return self.length def __getitem__( self : str , _lowerCAmelCase : Union[str, Any] ): return {"x": self.x[i], "y": self.y[i]} class lowerCamelCase_ ( torch.nn.Module ): '''simple docstring''' def __init__( self : Tuple , _lowerCAmelCase : Dict=0 , _lowerCAmelCase : List[str]=0 , _lowerCAmelCase : str=False ): super().__init__() SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) SCREAMING_SNAKE_CASE_ = True def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any]=None ): if self.first_batch: print(F"Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}" ) SCREAMING_SNAKE_CASE_ = False return x * self.a[0] + self.b[0] class lowerCamelCase_ ( torch.nn.Module ): '''simple docstring''' def __init__( self : Optional[int] , _lowerCAmelCase : Any=0 , _lowerCAmelCase : Any=0 , _lowerCAmelCase : Optional[Any]=False ): super().__init__() SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor(_lowerCAmelCase ).float() ) SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor(_lowerCAmelCase ).float() ) SCREAMING_SNAKE_CASE_ = True def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : Optional[int]=None ): if self.first_batch: print(F"Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}" ) SCREAMING_SNAKE_CASE_ = False return x * self.a + self.b def UpperCAmelCase_ ( __UpperCAmelCase : Dict , __UpperCAmelCase : int = 16 ) -> Union[str, Any]: from datasets import load_dataset from transformers import AutoTokenizer SCREAMING_SNAKE_CASE_ = AutoTokenizer.from_pretrained('bert-base-cased' ) SCREAMING_SNAKE_CASE_ = {'train': 'tests/test_samples/MRPC/train.csv', 'validation': 'tests/test_samples/MRPC/dev.csv'} SCREAMING_SNAKE_CASE_ = load_dataset('csv' , data_files=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = datasets['train'].unique('label' ) SCREAMING_SNAKE_CASE_ = {v: i for i, v in enumerate(__UpperCAmelCase )} def tokenize_function(__UpperCAmelCase : Optional[int] ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE_ = tokenizer( examples['sentence1'] , examples['sentence2'] , truncation=__UpperCAmelCase , max_length=__UpperCAmelCase , padding='max_length' ) if "label" in examples: SCREAMING_SNAKE_CASE_ = [label_to_id[l] for l in examples['label']] return outputs # Apply the method we just defined to all the examples in all the splits of the dataset SCREAMING_SNAKE_CASE_ = datasets.map( __UpperCAmelCase , batched=__UpperCAmelCase , remove_columns=['sentence1', 'sentence2', 'label'] , ) def collate_fn(__UpperCAmelCase : Dict ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(__UpperCAmelCase , padding='max_length' , max_length=1_28 , return_tensors='pt' ) return tokenizer.pad(__UpperCAmelCase , padding='longest' , return_tensors='pt' ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE_ = DataLoader(tokenized_datasets['train'] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=2 ) SCREAMING_SNAKE_CASE_ = DataLoader(tokenized_datasets['validation'] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=1 ) return train_dataloader, eval_dataloader
31
1
import os import tempfile from functools import partial from unittest import TestCase from unittest.mock import patch import datasets import datasets.config from .utils import require_beam class lowerCamelCase_ ( datasets.BeamBasedBuilder ): '''simple docstring''' def lowerCAmelCase_ ( self : Any ): return datasets.DatasetInfo( features=datasets.Features({'content': datasets.Value('string' )} ) , supervised_keys=_lowerCAmelCase , ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Optional[Any] ): return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'examples': get_test_dummy_examples()} )] def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[str] , _lowerCAmelCase : List[Any] ): import apache_beam as beam return pipeline | "Load Examples" >> beam.Create(_lowerCAmelCase ) class lowerCamelCase_ ( datasets.BeamBasedBuilder ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): return datasets.DatasetInfo( features=datasets.Features({'a': datasets.Sequence({'b': datasets.Value('string' )} )} ) , supervised_keys=_lowerCAmelCase , ) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Tuple ): return [ datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'examples': get_test_nested_examples()} ) ] def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Any , _lowerCAmelCase : Optional[Any] ): import apache_beam as beam return pipeline | "Load Examples" >> beam.Create(_lowerCAmelCase ) def UpperCAmelCase_ ( ) -> Optional[Any]: return [(i, {"content": content}) for i, content in enumerate(['foo', 'bar', 'foobar'] )] def UpperCAmelCase_ ( ) -> Union[str, Any]: return [(i, {"a": {"b": [content]}}) for i, content in enumerate(['foo', 'bar', 'foobar'] )] class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' @require_beam def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = len(get_test_dummy_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: SCREAMING_SNAKE_CASE_ = DummyBeamDataset(cache_dir=_lowerCAmelCase , beam_runner='DirectRunner' ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join(_lowerCAmelCase , builder.name , 'default' , '0.0.0' , F"{builder.name}-train.arrow" ) ) ) self.assertDictEqual(builder.info.features , datasets.Features({'content': datasets.Value('string' )} ) ) SCREAMING_SNAKE_CASE_ = builder.as_dataset() self.assertEqual(dset['train'].num_rows , _lowerCAmelCase ) self.assertEqual(dset['train'].info.splits['train'].num_examples , _lowerCAmelCase ) self.assertDictEqual(dset['train'][0] , get_test_dummy_examples()[0][1] ) self.assertDictEqual( dset['train'][expected_num_examples - 1] , get_test_dummy_examples()[expected_num_examples - 1][1] ) self.assertTrue( os.path.exists(os.path.join(_lowerCAmelCase , builder.name , 'default' , '0.0.0' , 'dataset_info.json' ) ) ) del dset @require_beam def lowerCAmelCase_ ( self : List[str] ): import apache_beam as beam SCREAMING_SNAKE_CASE_ = beam.io.parquetio.WriteToParquet SCREAMING_SNAKE_CASE_ = len(get_test_dummy_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: SCREAMING_SNAKE_CASE_ = DummyBeamDataset(cache_dir=_lowerCAmelCase , beam_runner='DirectRunner' ) with patch('apache_beam.io.parquetio.WriteToParquet' ) as write_parquet_mock: SCREAMING_SNAKE_CASE_ = partial(_lowerCAmelCase , num_shards=2 ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join( _lowerCAmelCase , builder.name , 'default' , '0.0.0' , F"{builder.name}-train-00000-of-00002.arrow" ) ) ) self.assertTrue( os.path.exists( os.path.join( _lowerCAmelCase , builder.name , 'default' , '0.0.0' , F"{builder.name}-train-00000-of-00002.arrow" ) ) ) self.assertDictEqual(builder.info.features , datasets.Features({'content': datasets.Value('string' )} ) ) SCREAMING_SNAKE_CASE_ = builder.as_dataset() self.assertEqual(dset['train'].num_rows , _lowerCAmelCase ) self.assertEqual(dset['train'].info.splits['train'].num_examples , _lowerCAmelCase ) # Order is not preserved when sharding, so we just check that all the elements are there self.assertListEqual(sorted(dset['train']['content'] ) , sorted(['foo', 'bar', 'foobar'] ) ) self.assertTrue( os.path.exists(os.path.join(_lowerCAmelCase , builder.name , 'default' , '0.0.0' , 'dataset_info.json' ) ) ) del dset @require_beam def lowerCAmelCase_ ( self : Optional[int] ): with tempfile.TemporaryDirectory() as tmp_cache_dir: SCREAMING_SNAKE_CASE_ = DummyBeamDataset(cache_dir=_lowerCAmelCase ) self.assertRaises(datasets.builder.MissingBeamOptions , builder.download_and_prepare ) @require_beam def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = len(get_test_nested_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: SCREAMING_SNAKE_CASE_ = NestedBeamDataset(cache_dir=_lowerCAmelCase , beam_runner='DirectRunner' ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join(_lowerCAmelCase , builder.name , 'default' , '0.0.0' , F"{builder.name}-train.arrow" ) ) ) self.assertDictEqual( builder.info.features , datasets.Features({'a': datasets.Sequence({'b': datasets.Value('string' )} )} ) ) SCREAMING_SNAKE_CASE_ = builder.as_dataset() self.assertEqual(dset['train'].num_rows , _lowerCAmelCase ) self.assertEqual(dset['train'].info.splits['train'].num_examples , _lowerCAmelCase ) self.assertDictEqual(dset['train'][0] , get_test_nested_examples()[0][1] ) self.assertDictEqual( dset['train'][expected_num_examples - 1] , get_test_nested_examples()[expected_num_examples - 1][1] ) self.assertTrue( os.path.exists(os.path.join(_lowerCAmelCase , builder.name , 'default' , '0.0.0' , 'dataset_info.json' ) ) ) del dset
31
import warnings from ...utils import logging from .image_processing_layoutlmva import LayoutLMvaImageProcessor lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Dict , *_lowerCAmelCase : Optional[Any] , **_lowerCAmelCase : Any ): warnings.warn( 'The class LayoutLMv2FeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use LayoutLMv2ImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
1
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : Optional[int] = {'configuration_focalnet': ['FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FocalNetConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : str = [ 'FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST', 'FocalNetForImageClassification', 'FocalNetForMaskedImageModeling', 'FocalNetBackbone', 'FocalNetModel', 'FocalNetPreTrainedModel', ] if TYPE_CHECKING: from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_focalnet import ( FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST, FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, FocalNetPreTrainedModel, ) else: import sys lowerCamelCase__ : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
31
def UpperCAmelCase_ ( ) -> list[list[int]]: return [list(range(10_00 - i , -10_00 - i , -1 ) ) for i in range(10_00 )] lowerCamelCase__ : List[Any] = generate_large_matrix() lowerCamelCase__ : List[Any] = ( [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]], [[3, 2], [1, 0]], [[7, 7, 6]], [[7, 7, 6], [-1, -2, -3]], grid, ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> None: assert all(row == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for row in grid ) assert all(list(__UpperCAmelCase ) == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for col in zip(*__UpperCAmelCase ) ) def UpperCAmelCase_ ( __UpperCAmelCase : list[int] ) -> int: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = len(__UpperCAmelCase ) - 1 # Edge cases such as no values or all numbers are negative. if not array or array[0] < 0: return 0 while right + 1 > left: SCREAMING_SNAKE_CASE_ = (left + right) // 2 SCREAMING_SNAKE_CASE_ = array[mid] # Num must be negative and the index must be greater than or equal to 0. if num < 0 and array[mid - 1] >= 0: return mid if num >= 0: SCREAMING_SNAKE_CASE_ = mid + 1 else: SCREAMING_SNAKE_CASE_ = mid - 1 # No negative numbers so return the last index of the array + 1 which is the length. return len(__UpperCAmelCase ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = len(grid[0] ) for i in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = find_negative_index(grid[i][:bound] ) total += bound return (len(__UpperCAmelCase ) * len(grid[0] )) - total def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: return len([number for row in grid for number in row if number < 0] ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: SCREAMING_SNAKE_CASE_ = 0 for row in grid: for i, number in enumerate(__UpperCAmelCase ): if number < 0: total += len(__UpperCAmelCase ) - i break return total def UpperCAmelCase_ ( ) -> None: from timeit import timeit print('Running benchmarks' ) SCREAMING_SNAKE_CASE_ = ( 'from __main__ import count_negatives_binary_search, ' 'count_negatives_brute_force, count_negatives_brute_force_with_break, grid' ) for func in ( "count_negatives_binary_search", # took 0.7727 seconds "count_negatives_brute_force_with_break", # took 4.6505 seconds "count_negatives_brute_force", # took 12.8160 seconds ): SCREAMING_SNAKE_CASE_ = timeit(f"{func}(grid=grid)" , setup=__UpperCAmelCase , number=5_00 ) print(f"{func}() took {time:0.4f} seconds" ) if __name__ == "__main__": import doctest doctest.testmod() benchmark()
31
1
import inspect import os import unittest import torch import accelerate from accelerate import debug_launcher from accelerate.test_utils import ( execute_subprocess_async, require_cpu, require_huggingface_suite, require_multi_gpu, require_single_gpu, ) from accelerate.utils import patch_environment @require_huggingface_suite class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = inspect.getfile(accelerate.test_utils ) SCREAMING_SNAKE_CASE_ = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['scripts', 'external_deps', 'test_metrics.py'] ) from accelerate.test_utils.scripts.external_deps import test_metrics # noqa: F401 SCREAMING_SNAKE_CASE_ = test_metrics @require_cpu def lowerCAmelCase_ ( self : Optional[Any] ): debug_launcher(self.test_metrics.main , num_processes=1 ) @require_cpu def lowerCAmelCase_ ( self : Optional[Any] ): debug_launcher(self.test_metrics.main ) @require_single_gpu def lowerCAmelCase_ ( self : Union[str, Any] ): self.test_metrics.main() @require_multi_gpu def lowerCAmelCase_ ( self : Tuple ): print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE_ = ['torchrun', F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_lowerCAmelCase , env=os.environ.copy() )
31
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : Optional[int] = {'configuration_mmbt': ['MMBTConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Any = ['MMBTForClassification', 'MMBTModel', 'ModalEmbeddings'] if TYPE_CHECKING: from .configuration_mmbt import MMBTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mmbt import MMBTForClassification, MMBTModel, ModalEmbeddings else: import sys lowerCamelCase__ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
31
1
import math def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> str: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = 0 while num > 0: SCREAMING_SNAKE_CASE_ = num % 8 SCREAMING_SNAKE_CASE_ = octal + (remainder * math.floor(math.pow(10 , __UpperCAmelCase ) )) counter += 1 SCREAMING_SNAKE_CASE_ = math.floor(num / 8 ) # basically /= 8 without remainder if any # This formatting removes trailing '.0' from `octal`. return f"0o{int(__UpperCAmelCase )}" def UpperCAmelCase_ ( ) -> None: print('\n2 in octal is:' ) print(decimal_to_octal(2 ) ) # = 2 print('\n8 in octal is:' ) print(decimal_to_octal(8 ) ) # = 10 print('\n65 in octal is:' ) print(decimal_to_octal(65 ) ) # = 101 print('\n216 in octal is:' ) print(decimal_to_octal(2_16 ) ) # = 330 print('\n512 in octal is:' ) print(decimal_to_octal(5_12 ) ) # = 1000 print('\n' ) if __name__ == "__main__": main()
31
import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class lowerCamelCase_ : '''simple docstring''' @property def lowerCAmelCase_ ( self : Optional[Any] ): return self.get_dummy_input() @property def lowerCAmelCase_ ( self : Union[str, Any] ): if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(F"'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'." ) def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : str=False , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : Dict=False , ): SCREAMING_SNAKE_CASE_ = 4 SCREAMING_SNAKE_CASE_ = 32 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = (batch_size, num_channels) + sizes SCREAMING_SNAKE_CASE_ = randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = {'hidden_states': hidden_states} if include_temb: SCREAMING_SNAKE_CASE_ = 128 SCREAMING_SNAKE_CASE_ = randn_tensor((batch_size, temb_channels) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) if include_res_hidden_states_tuple: SCREAMING_SNAKE_CASE_ = torch.manual_seed(1 ) SCREAMING_SNAKE_CASE_ = (randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ),) if include_encoder_hidden_states: SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, 32, 32) ).to(_lowerCAmelCase ) if include_skip_sample: SCREAMING_SNAKE_CASE_ = randn_tensor(((batch_size, 3) + sizes) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) return dummy_input def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = { 'in_channels': 32, 'out_channels': 32, 'temb_channels': 128, } if self.block_type == "up": SCREAMING_SNAKE_CASE_ = 32 if self.block_type == "mid": init_dict.pop('out_channels' ) SCREAMING_SNAKE_CASE_ = self.dummy_input return init_dict, inputs_dict def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) unet_block.to(_lowerCAmelCase ) unet_block.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_ = unet_block(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] self.assertEqual(output.shape , self.output_shape ) SCREAMING_SNAKE_CASE_ = output[0, -1, -3:, -3:] SCREAMING_SNAKE_CASE_ = torch.tensor(_lowerCAmelCase ).to(_lowerCAmelCase ) assert torch_all_close(output_slice.flatten() , _lowerCAmelCase , atol=5E-3 ) @unittest.skipIf(torch_device == 'mps' , 'Training is not supported in mps' ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.train() SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = randn_tensor(output.shape , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.functional.mse_loss(_lowerCAmelCase , _lowerCAmelCase ) loss.backward()
31
1
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_fnet import FNetTokenizer else: lowerCamelCase__ : Optional[Any] = None lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : List[str] = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} lowerCamelCase__ : List[str] = { 'vocab_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/spiece.model', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/spiece.model', }, 'tokenizer_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json', }, } lowerCamelCase__ : Optional[Any] = { 'google/fnet-base': 512, 'google/fnet-large': 512, } lowerCamelCase__ : List[Any] = '▁' class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = ["input_ids", "token_type_ids"] lowercase_ = FNetTokenizer def __init__( self : List[Any] , _lowerCAmelCase : Dict=None , _lowerCAmelCase : Dict=None , _lowerCAmelCase : List[str]=False , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Tuple=True , _lowerCAmelCase : List[Any]="<unk>" , _lowerCAmelCase : Optional[Any]="[SEP]" , _lowerCAmelCase : Optional[Any]="<pad>" , _lowerCAmelCase : Optional[int]="[CLS]" , _lowerCAmelCase : Optional[Any]="[MASK]" , **_lowerCAmelCase : 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. SCREAMING_SNAKE_CASE_ = ( AddedToken(_lowerCAmelCase , lstrip=_lowerCAmelCase , rstrip=_lowerCAmelCase , normalized=_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ) else mask_token ) super().__init__( _lowerCAmelCase , tokenizer_file=_lowerCAmelCase , do_lower_case=_lowerCAmelCase , remove_space=_lowerCAmelCase , keep_accents=_lowerCAmelCase , unk_token=_lowerCAmelCase , sep_token=_lowerCAmelCase , pad_token=_lowerCAmelCase , cls_token=_lowerCAmelCase , mask_token=_lowerCAmelCase , **_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = do_lower_case SCREAMING_SNAKE_CASE_ = remove_space SCREAMING_SNAKE_CASE_ = keep_accents SCREAMING_SNAKE_CASE_ = vocab_file SCREAMING_SNAKE_CASE_ = False if not self.vocab_file else True def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None ): if not os.path.isdir(_lowerCAmelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE_ = os.path.join( _lowerCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_lowerCAmelCase ): copyfile(self.vocab_file , _lowerCAmelCase ) return (out_vocab_file,)
31
import operator as op def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> Any: SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = lambda __UpperCAmelCase , __UpperCAmelCase : int(x / y ) # noqa: E731 integer division operation SCREAMING_SNAKE_CASE_ = { '^': op.pow, '*': op.mul, '/': div, '+': op.add, '-': op.sub, } # operators & their respective operation # print table header print('Symbol'.center(8 ) , 'Action'.center(12 ) , 'Stack' , sep=' | ' ) print('-' * (30 + len(__UpperCAmelCase )) ) for x in post_fix: if x.isdigit(): # if x in digit stack.append(__UpperCAmelCase ) # append x to stack # output in tabular format print(x.rjust(8 ) , ('push(' + x + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) else: SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print(''.rjust(8 ) , ('pop(' + b + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print(''.rjust(8 ) , ('pop(' + a + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) stack.append( str(opr[x](int(__UpperCAmelCase ) , int(__UpperCAmelCase ) ) ) ) # evaluate the 2 values popped from stack & push result to stack # output in tabular format print( x.rjust(8 ) , ('push(' + a + x + b + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' , ) return int(stack[0] ) if __name__ == "__main__": lowerCamelCase__ : Tuple = input('\n\nEnter a Postfix Equation (space separated) = ').split(' ') print('\n\tResult = ', solve(Postfix))
31
1
def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int ) -> int: return number | (1 << position) def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int ) -> int: return number & ~(1 << position) def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int ) -> int: return number ^ (1 << position) def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int ) -> bool: return ((number >> position) & 1) == 1 def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int ) -> int: return int((number & (1 << position)) != 0 ) if __name__ == "__main__": import doctest doctest.testmod()
31
def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: assert isinstance(__UpperCAmelCase , __UpperCAmelCase ), f"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE_ = f"The input value of [n={number}] has to be > 0" raise ValueError(__UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_ = sylvester(number - 1 ) SCREAMING_SNAKE_CASE_ = num - 1 SCREAMING_SNAKE_CASE_ = num return lower * upper + 1 if __name__ == "__main__": print(f'''The 8th number in Sylvester\'s sequence: {sylvester(8)}''')
31
1
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = ["image_processor", "tokenizer"] lowercase_ = "CLIPImageProcessor" lowercase_ = ("XLMRobertaTokenizer", "XLMRobertaTokenizerFast") def __init__( self : Tuple , _lowerCAmelCase : Dict=None , _lowerCAmelCase : List[Any]=None , **_lowerCAmelCase : Any ): SCREAMING_SNAKE_CASE_ = None if "feature_extractor" in kwargs: warnings.warn( 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`' ' instead.' , _lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = kwargs.pop('feature_extractor' ) SCREAMING_SNAKE_CASE_ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('You need to specify an `image_processor`.' ) if tokenizer is None: raise ValueError('You need to specify a `tokenizer`.' ) super().__init__(_lowerCAmelCase , _lowerCAmelCase ) def __call__( self : Tuple , _lowerCAmelCase : Optional[Any]=None , _lowerCAmelCase : Tuple=None , _lowerCAmelCase : Union[str, Any]=None , **_lowerCAmelCase : Optional[int] ): if text is None and images is None: raise ValueError('You have to specify either text or images. Both cannot be none.' ) if text is not None: SCREAMING_SNAKE_CASE_ = self.tokenizer(_lowerCAmelCase , return_tensors=_lowerCAmelCase , **_lowerCAmelCase ) if images is not None: SCREAMING_SNAKE_CASE_ = self.image_processor(_lowerCAmelCase , return_tensors=_lowerCAmelCase , **_lowerCAmelCase ) if text is not None and images is not None: SCREAMING_SNAKE_CASE_ = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**_lowerCAmelCase ) , tensor_type=_lowerCAmelCase ) def lowerCAmelCase_ ( self : str , *_lowerCAmelCase : str , **_lowerCAmelCase : Any ): return self.tokenizer.batch_decode(*_lowerCAmelCase , **_lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] , *_lowerCAmelCase : Union[str, Any] , **_lowerCAmelCase : str ): return self.tokenizer.decode(*_lowerCAmelCase , **_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.tokenizer.model_input_names SCREAMING_SNAKE_CASE_ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
31
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCamelCase__ : List[Any] = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCamelCase__ : Union[str, Any] = TaTokenizerFast lowerCamelCase__ : Dict = {'configuration_mt5': ['MT5Config', 'MT5OnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = [ 'MT5EncoderModel', 'MT5ForConditionalGeneration', 'MT5ForQuestionAnswering', 'MT5Model', 'MT5PreTrainedModel', 'MT5Stack', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = ['TFMT5EncoderModel', 'TFMT5ForConditionalGeneration', 'TFMT5Model'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = ['FlaxMT5EncoderModel', 'FlaxMT5ForConditionalGeneration', 'FlaxMT5Model'] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCamelCase__ : int = _LazyModule( __name__, globals()['__file__'], _import_structure, extra_objects={'MT5Tokenizer': MTaTokenizer, 'MT5TokenizerFast': MTaTokenizerFast}, module_spec=__spec__, )
31
1
import torch from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.bert.modeling_bert import ( BERT_INPUTS_DOCSTRING, BERT_START_DOCSTRING, BertEmbeddings, BertLayer, BertPooler, BertPreTrainedModel, ) def UpperCAmelCase_ ( __UpperCAmelCase : Optional[Any] ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = torch.exp(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.sum(__UpperCAmelCase , dim=1 ) # sum of exp(x_i) SCREAMING_SNAKE_CASE_ = torch.sum(x * exp_x , dim=1 ) # sum of x_i * exp(x_i) return torch.log(__UpperCAmelCase ) - B / A class lowerCamelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : Tuple , _lowerCAmelCase : List[Any] ): super().__init__() SCREAMING_SNAKE_CASE_ = config.output_attentions SCREAMING_SNAKE_CASE_ = config.output_hidden_states SCREAMING_SNAKE_CASE_ = nn.ModuleList([BertLayer(_lowerCAmelCase ) for _ in range(config.num_hidden_layers )] ) SCREAMING_SNAKE_CASE_ = nn.ModuleList([BertHighway(_lowerCAmelCase ) for _ in range(config.num_hidden_layers )] ) SCREAMING_SNAKE_CASE_ = [-1 for _ in range(config.num_hidden_layers )] def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : Optional[Any] ): if (type(_lowerCAmelCase ) is float) or (type(_lowerCAmelCase ) is int): for i in range(len(self.early_exit_entropy ) ): SCREAMING_SNAKE_CASE_ = x else: SCREAMING_SNAKE_CASE_ = x def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : List[Any] ): SCREAMING_SNAKE_CASE_ = pooler.state_dict() for highway in self.highway: for name, param in highway.pooler.state_dict().items(): param.copy_(loaded_model[name] ) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : int , _lowerCAmelCase : Union[str, Any]=None , _lowerCAmelCase : Tuple=None , _lowerCAmelCase : List[Any]=None , _lowerCAmelCase : Optional[Any]=None , ): SCREAMING_SNAKE_CASE_ = () SCREAMING_SNAKE_CASE_ = () SCREAMING_SNAKE_CASE_ = () for i, layer_module in enumerate(self.layer ): if self.output_hidden_states: SCREAMING_SNAKE_CASE_ = all_hidden_states + (hidden_states,) SCREAMING_SNAKE_CASE_ = layer_module( _lowerCAmelCase , _lowerCAmelCase , head_mask[i] , _lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = layer_outputs[0] if self.output_attentions: SCREAMING_SNAKE_CASE_ = all_attentions + (layer_outputs[1],) SCREAMING_SNAKE_CASE_ = (hidden_states,) if self.output_hidden_states: SCREAMING_SNAKE_CASE_ = current_outputs + (all_hidden_states,) if self.output_attentions: SCREAMING_SNAKE_CASE_ = current_outputs + (all_attentions,) SCREAMING_SNAKE_CASE_ = self.highway[i](_lowerCAmelCase ) # logits, pooled_output if not self.training: SCREAMING_SNAKE_CASE_ = highway_exit[0] SCREAMING_SNAKE_CASE_ = entropy(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = highway_exit + (highway_entropy,) # logits, hidden_states(?), entropy SCREAMING_SNAKE_CASE_ = all_highway_exits + (highway_exit,) if highway_entropy < self.early_exit_entropy[i]: SCREAMING_SNAKE_CASE_ = (highway_logits,) + current_outputs[1:] + (all_highway_exits,) raise HighwayException(_lowerCAmelCase , i + 1 ) else: SCREAMING_SNAKE_CASE_ = all_highway_exits + (highway_exit,) # Add last layer if self.output_hidden_states: SCREAMING_SNAKE_CASE_ = all_hidden_states + (hidden_states,) SCREAMING_SNAKE_CASE_ = (hidden_states,) if self.output_hidden_states: SCREAMING_SNAKE_CASE_ = outputs + (all_hidden_states,) if self.output_attentions: SCREAMING_SNAKE_CASE_ = outputs + (all_attentions,) SCREAMING_SNAKE_CASE_ = outputs + (all_highway_exits,) return outputs # last-layer hidden state, (all hidden states), (all attentions), all highway exits @add_start_docstrings( "The Bert Model transformer with early exiting (DeeBERT). " , _SCREAMING_SNAKE_CASE , ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : List[Any] , _lowerCAmelCase : Tuple ): super().__init__(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = config SCREAMING_SNAKE_CASE_ = BertEmbeddings(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = DeeBertEncoder(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = BertPooler(_lowerCAmelCase ) self.init_weights() def lowerCAmelCase_ ( self : int ): self.encoder.init_highway_pooler(self.pooler ) def lowerCAmelCase_ ( self : Optional[int] ): return self.embeddings.word_embeddings def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : List[Any] ): SCREAMING_SNAKE_CASE_ = value def lowerCAmelCase_ ( self : str , _lowerCAmelCase : Tuple ): for layer, heads in heads_to_prune.items(): self.encoder.layer[layer].attention.prune_heads(_lowerCAmelCase ) @add_start_docstrings_to_model_forward(_lowerCAmelCase ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : str=None , _lowerCAmelCase : int=None , _lowerCAmelCase : str=None , _lowerCAmelCase : str=None , _lowerCAmelCase : Tuple=None , _lowerCAmelCase : Dict=None , _lowerCAmelCase : int=None , _lowerCAmelCase : Any=None , ): if input_ids is not None and inputs_embeds is not None: raise ValueError('You cannot specify both input_ids and inputs_embeds at the same time' ) elif input_ids is not None: SCREAMING_SNAKE_CASE_ = input_ids.size() elif inputs_embeds is not None: SCREAMING_SNAKE_CASE_ = inputs_embeds.size()[:-1] else: raise ValueError('You have to specify either input_ids or inputs_embeds' ) SCREAMING_SNAKE_CASE_ = input_ids.device if input_ids is not None else inputs_embeds.device if attention_mask is None: SCREAMING_SNAKE_CASE_ = torch.ones(_lowerCAmelCase , device=_lowerCAmelCase ) if encoder_attention_mask is None: SCREAMING_SNAKE_CASE_ = torch.ones(_lowerCAmelCase , device=_lowerCAmelCase ) if token_type_ids is None: SCREAMING_SNAKE_CASE_ = torch.zeros(_lowerCAmelCase , dtype=torch.long , device=_lowerCAmelCase ) # We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length] # ourselves in which case we just need to make it broadcastable to all heads. SCREAMING_SNAKE_CASE_ = self.get_extended_attention_mask(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) # If a 2D ou 3D attention mask is provided for the cross-attention # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length] if encoder_attention_mask.dim() == 3: SCREAMING_SNAKE_CASE_ = encoder_attention_mask[:, None, :, :] if encoder_attention_mask.dim() == 2: SCREAMING_SNAKE_CASE_ = encoder_attention_mask[:, None, None, :] SCREAMING_SNAKE_CASE_ = encoder_extended_attention_mask.to( dtype=next(self.parameters() ).dtype ) # fp16 compatibility SCREAMING_SNAKE_CASE_ = (1.0 - encoder_extended_attention_mask) * -1_0000.0 # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape bsz x n_heads x N x N # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads] # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length] SCREAMING_SNAKE_CASE_ = self.get_head_mask(_lowerCAmelCase , self.config.num_hidden_layers ) SCREAMING_SNAKE_CASE_ = self.embeddings( input_ids=_lowerCAmelCase , position_ids=_lowerCAmelCase , token_type_ids=_lowerCAmelCase , inputs_embeds=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.encoder( _lowerCAmelCase , attention_mask=_lowerCAmelCase , head_mask=_lowerCAmelCase , encoder_hidden_states=_lowerCAmelCase , encoder_attention_mask=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = encoder_outputs[0] SCREAMING_SNAKE_CASE_ = self.pooler(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = ( sequence_output, pooled_output, ) + encoder_outputs[ 1: ] # add hidden_states and attentions if they are here return outputs # sequence_output, pooled_output, (hidden_states), (attentions), highway exits class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Dict , _lowerCAmelCase : str , _lowerCAmelCase : str ): SCREAMING_SNAKE_CASE_ = message SCREAMING_SNAKE_CASE_ = exit_layer # start from 1! class lowerCamelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : List[Any] , _lowerCAmelCase : int ): super().__init__() SCREAMING_SNAKE_CASE_ = BertPooler(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = nn.Dropout(config.hidden_dropout_prob ) SCREAMING_SNAKE_CASE_ = nn.Linear(config.hidden_size , config.num_labels ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Optional[Any] ): # Pooler SCREAMING_SNAKE_CASE_ = encoder_outputs[0] SCREAMING_SNAKE_CASE_ = self.pooler(_lowerCAmelCase ) # "return" pooler_output # BertModel SCREAMING_SNAKE_CASE_ = (pooler_input, pooler_output) + encoder_outputs[1:] # "return" bmodel_output # Dropout and classification SCREAMING_SNAKE_CASE_ = bmodel_output[1] SCREAMING_SNAKE_CASE_ = self.dropout(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.classifier(_lowerCAmelCase ) return logits, pooled_output @add_start_docstrings( "Bert Model (with early exiting - DeeBERT) with a classifier on top,\n also takes care of multi-layer training. " , _SCREAMING_SNAKE_CASE , ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Tuple , _lowerCAmelCase : List[str] ): super().__init__(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = config.num_labels SCREAMING_SNAKE_CASE_ = config.num_hidden_layers SCREAMING_SNAKE_CASE_ = DeeBertModel(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = nn.Dropout(config.hidden_dropout_prob ) SCREAMING_SNAKE_CASE_ = nn.Linear(config.hidden_size , self.config.num_labels ) self.init_weights() @add_start_docstrings_to_model_forward(_lowerCAmelCase ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : int=None , _lowerCAmelCase : List[str]=None , _lowerCAmelCase : Optional[Any]=None , _lowerCAmelCase : List[Any]=None , _lowerCAmelCase : List[str]=None , _lowerCAmelCase : int=None , _lowerCAmelCase : List[Any]=None , _lowerCAmelCase : List[str]=-1 , _lowerCAmelCase : List[str]=False , ): SCREAMING_SNAKE_CASE_ = self.num_layers try: SCREAMING_SNAKE_CASE_ = self.bert( _lowerCAmelCase , attention_mask=_lowerCAmelCase , token_type_ids=_lowerCAmelCase , position_ids=_lowerCAmelCase , head_mask=_lowerCAmelCase , inputs_embeds=_lowerCAmelCase , ) # sequence_output, pooled_output, (hidden_states), (attentions), highway exits SCREAMING_SNAKE_CASE_ = outputs[1] SCREAMING_SNAKE_CASE_ = self.dropout(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.classifier(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: SCREAMING_SNAKE_CASE_ = e.message SCREAMING_SNAKE_CASE_ = e.exit_layer SCREAMING_SNAKE_CASE_ = outputs[0] if not self.training: SCREAMING_SNAKE_CASE_ = entropy(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = [] if labels is not None: if self.num_labels == 1: # We are doing regression SCREAMING_SNAKE_CASE_ = MSELoss() SCREAMING_SNAKE_CASE_ = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: SCREAMING_SNAKE_CASE_ = CrossEntropyLoss() SCREAMING_SNAKE_CASE_ = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits SCREAMING_SNAKE_CASE_ = [] for highway_exit in outputs[-1]: SCREAMING_SNAKE_CASE_ = highway_exit[0] if not self.training: highway_logits_all.append(_lowerCAmelCase ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression SCREAMING_SNAKE_CASE_ = MSELoss() SCREAMING_SNAKE_CASE_ = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: SCREAMING_SNAKE_CASE_ = CrossEntropyLoss() SCREAMING_SNAKE_CASE_ = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(_lowerCAmelCase ) if train_highway: SCREAMING_SNAKE_CASE_ = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: SCREAMING_SNAKE_CASE_ = (loss,) + outputs if not self.training: SCREAMING_SNAKE_CASE_ = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: SCREAMING_SNAKE_CASE_ = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), (highway_exits)
31
import subprocess import sys from transformers import BertConfig, BertModel, BertTokenizer, pipeline from transformers.testing_utils import TestCasePlus, require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' @require_torch def lowerCAmelCase_ ( self : int ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Tuple ): # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : List[str] ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # next emulate no network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this. # env["TRANSFORMERS_OFFLINE"] = "0" # result = subprocess.run(cmd, env=env, check=False, capture_output=True) # self.assertEqual(result.returncode, 1, result.stderr) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n ' SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 1 , result.stderr ) self.assertIn( 'You cannot infer task automatically within `pipeline` when using offline mode' , result.stderr.decode().replace('\n' , '' ) , ) @require_torch def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import AutoModel\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() )
31
1
from __future__ import annotations import inspect import unittest from math import floor import numpy as np from transformers import CvtConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFCvtForImageClassification, TFCvtModel from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_lowerCAmelCase , 'embed_dim' ) ) self.parent.assertTrue(hasattr(_lowerCAmelCase , 'num_heads' ) ) class lowerCamelCase_ : '''simple docstring''' def __init__( self : Optional[int] , _lowerCAmelCase : int , _lowerCAmelCase : List[str]=13 , _lowerCAmelCase : str=64 , _lowerCAmelCase : Optional[Any]=3 , _lowerCAmelCase : List[str]=[16, 48, 96] , _lowerCAmelCase : List[Any]=[1, 3, 6] , _lowerCAmelCase : str=[1, 2, 10] , _lowerCAmelCase : Optional[int]=[7, 3, 3] , _lowerCAmelCase : Optional[int]=[4, 2, 2] , _lowerCAmelCase : List[Any]=[2, 1, 1] , _lowerCAmelCase : str=[2, 2, 2] , _lowerCAmelCase : List[str]=[False, False, True] , _lowerCAmelCase : Any=[0.0, 0.0, 0.0] , _lowerCAmelCase : int=0.02 , _lowerCAmelCase : Tuple=1E-12 , _lowerCAmelCase : List[Any]=True , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : Optional[int]=2 , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_sizes SCREAMING_SNAKE_CASE_ = patch_stride SCREAMING_SNAKE_CASE_ = patch_padding SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_labels SCREAMING_SNAKE_CASE_ = num_labels SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = embed_dim SCREAMING_SNAKE_CASE_ = num_heads SCREAMING_SNAKE_CASE_ = stride_kv SCREAMING_SNAKE_CASE_ = depth SCREAMING_SNAKE_CASE_ = cls_token SCREAMING_SNAKE_CASE_ = attention_drop_rate SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = layer_norm_eps def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ = None if self.use_labels: # create a random int32 tensor of given shape SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size] , self.num_labels ) SCREAMING_SNAKE_CASE_ = self.get_config() return config, pixel_values, labels def lowerCAmelCase_ ( self : int ): return CvtConfig( image_size=self.image_size , num_labels=self.num_labels , num_channels=self.num_channels , embed_dim=self.embed_dim , num_heads=self.num_heads , patch_sizes=self.patch_sizes , patch_padding=self.patch_padding , patch_stride=self.patch_stride , stride_kv=self.stride_kv , depth=self.depth , cls_token=self.cls_token , attention_drop_rate=self.attention_drop_rate , initializer_range=self.initializer_range , ) def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : List[str] , _lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ = TFCvtModel(config=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , training=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = (self.image_size, self.image_size) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = image_size[0], image_size[1] for i in range(len(self.depth ) ): SCREAMING_SNAKE_CASE_ = floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) SCREAMING_SNAKE_CASE_ = floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.embed_dim[-1], height, width) ) def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : str , _lowerCAmelCase : str , _lowerCAmelCase : Tuple ): SCREAMING_SNAKE_CASE_ = self.num_labels SCREAMING_SNAKE_CASE_ = TFCvtForImageClassification(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , labels=_lowerCAmelCase , training=_lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = config_and_inputs SCREAMING_SNAKE_CASE_ = {'pixel_values': pixel_values} return config, inputs_dict @require_tf class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else () lowercase_ = ( {"feature-extraction": TFCvtModel, "image-classification": TFCvtForImageClassification} if is_tf_available() else {} ) lowercase_ = False lowercase_ = False lowercase_ = False lowercase_ = False lowercase_ = False def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = TFCvtModelTester(self ) SCREAMING_SNAKE_CASE_ = TFCvtConfigTester(self , config_class=_lowerCAmelCase , has_text_modality=_lowerCAmelCase , hidden_size=37 ) def lowerCAmelCase_ ( self : Dict ): self.config_tester.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() @unittest.skip(reason='Cvt does not output attentions' ) def lowerCAmelCase_ ( self : Optional[int] ): pass @unittest.skip(reason='Cvt does not use inputs_embeds' ) def lowerCAmelCase_ ( self : Dict ): pass @unittest.skip(reason='Cvt does not support input and output embeddings' ) def lowerCAmelCase_ ( self : Optional[int] ): pass @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices('GPU' ) ) == 0 , reason='TF does not support backprop for grouped convolutions on CPU.' , ) def lowerCAmelCase_ ( self : Tuple ): super().test_dataset_conversion() @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices('GPU' ) ) == 0 , reason='TF does not support backprop for grouped convolutions on CPU.' , ) @slow def lowerCAmelCase_ ( self : Any ): super().test_keras_fit() @unittest.skip(reason='Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8' ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = tf.keras.mixed_precision.Policy('mixed_float16' ) tf.keras.mixed_precision.set_global_policy(_lowerCAmelCase ) super().test_keras_fit() tf.keras.mixed_precision.set_global_policy('float32' ) def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_ = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _lowerCAmelCase ) def lowerCAmelCase_ ( self : str ): def check_hidden_states_output(_lowerCAmelCase : Dict , _lowerCAmelCase : str , _lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ = model_class(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = model(**self._prepare_for_class(_lowerCAmelCase , _lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ = outputs.hidden_states SCREAMING_SNAKE_CASE_ = len(self.model_tester.depth ) self.assertEqual(len(_lowerCAmelCase ) , _lowerCAmelCase ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.embed_dim[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = True check_hidden_states_output(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE_ = True check_hidden_states_output(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_lowerCAmelCase ) def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase ) @slow def lowerCAmelCase_ ( self : List[Any] ): for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ = TFCvtModel.from_pretrained(_lowerCAmelCase ) self.assertIsNotNone(_lowerCAmelCase ) def UpperCAmelCase_ ( ) -> str: SCREAMING_SNAKE_CASE_ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' @cached_property def lowerCAmelCase_ ( self : int ): return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) @slow def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=_lowerCAmelCase , return_tensors='tf' ) # forward pass SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ) # verify the logits SCREAMING_SNAKE_CASE_ = tf.TensorShape((1, 1_000) ) self.assertEqual(outputs.logits.shape , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tf.constant([0.9285, 0.9015, -0.3150] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , _lowerCAmelCase , atol=1E-4 ) )
31
import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "M-CLIP" def __init__( self : Tuple , _lowerCAmelCase : List[str]=1_024 , _lowerCAmelCase : str=768 , **_lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = transformerDimSize SCREAMING_SNAKE_CASE_ = imageDimSize super().__init__(**_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = MCLIPConfig def __init__( self : Dict , _lowerCAmelCase : Union[str, Any] , *_lowerCAmelCase : str , **_lowerCAmelCase : str ): super().__init__(_lowerCAmelCase , *_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = XLMRobertaModel(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.Linear( in_features=config.transformerDimensions , out_features=config.numDims ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.transformer(input_ids=_lowerCAmelCase , attention_mask=_lowerCAmelCase )[0] SCREAMING_SNAKE_CASE_ = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(_lowerCAmelCase ), embs
31
1
import argparse import json import os from tensorflow.core.protobuf.saved_model_pba import SavedModel # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py lowerCamelCase__ : Dict = '.' # Internal TensorFlow ops that can be safely ignored (mostly specific to a saved model) lowerCamelCase__ : List[str] = [ 'Assert', 'AssignVariableOp', 'EmptyTensorList', 'MergeV2Checkpoints', 'ReadVariableOp', 'ResourceGather', 'RestoreV2', 'SaveV2', 'ShardedFilename', 'StatefulPartitionedCall', 'StaticRegexFullMatch', 'VarHandleOp', ] def UpperCAmelCase_ ( __UpperCAmelCase : Tuple , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Union[str, Any] ) -> str: SCREAMING_SNAKE_CASE_ = SavedModel() SCREAMING_SNAKE_CASE_ = [] with open(os.path.join(__UpperCAmelCase , 'utils' , 'tf_ops' , 'onnx.json' ) ) as f: SCREAMING_SNAKE_CASE_ = json.load(__UpperCAmelCase )['opsets'] for i in range(1 , opset + 1 ): onnx_ops.extend(onnx_opsets[str(__UpperCAmelCase )] ) with open(__UpperCAmelCase , 'rb' ) as f: saved_model.ParseFromString(f.read() ) SCREAMING_SNAKE_CASE_ = set() # Iterate over every metagraph in case there is more than one (a saved model can contain multiple graphs) for meta_graph in saved_model.meta_graphs: # Add operations in the graph definition model_op_names.update(node.op for node in meta_graph.graph_def.node ) # Go through the functions in the graph definition for func in meta_graph.graph_def.library.function: # Add operations in each function model_op_names.update(node.op for node in func.node_def ) # Convert to list, sorted if you want SCREAMING_SNAKE_CASE_ = sorted(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = [] for op in model_op_names: if op not in onnx_ops and op not in INTERNAL_OPS: incompatible_ops.append(__UpperCAmelCase ) if strict and len(__UpperCAmelCase ) > 0: raise Exception(f"Found the following incompatible ops for the opset {opset}:\n" + incompatible_ops ) elif len(__UpperCAmelCase ) > 0: print(f"Found the following incompatible ops for the opset {opset}:" ) print(*__UpperCAmelCase , sep='\n' ) else: print(f"The saved model {saved_model_path} can properly be converted with ONNX." ) if __name__ == "__main__": lowerCamelCase__ : List[Any] = argparse.ArgumentParser() parser.add_argument('--saved_model_path', help='Path of the saved model to check (the .pb file).') parser.add_argument( '--opset', default=12, type=int, help='The ONNX opset against which the model has to be tested.' ) parser.add_argument( '--framework', choices=['onnx'], default='onnx', help='Frameworks against which to test the saved model.' ) parser.add_argument( '--strict', action='store_true', help='Whether make the checking strict (raise errors) or not (raise warnings)' ) lowerCamelCase__ : str = parser.parse_args() if args.framework == "onnx": onnx_compliancy(args.saved_model_path, args.strict, args.opset)
31
import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) 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 enable_full_determinism() class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = 3 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(_lowerCAmelCase ) return image @property def lowerCAmelCase_ ( self : Union[str, Any] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) return model @property def lowerCAmelCase_ ( self : Tuple ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , ) return model @property def lowerCAmelCase_ ( self : Optional[int] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5_006 , ) return RobertaSeriesModelWithTransformation(_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : List[Any] ): def extract(*_lowerCAmelCase : Optional[int] , **_lowerCAmelCase : str ): class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = torch.ones([0] ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int ): self.pixel_values.to(_lowerCAmelCase ) return self return Out() return extract def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = 'cpu' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = output.images SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , return_dict=_lowerCAmelCase , )[0] SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE_ = np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5E-3 @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) # put models in fp16 SCREAMING_SNAKE_CASE_ = unet.half() SCREAMING_SNAKE_CASE_ = vae.half() SCREAMING_SNAKE_CASE_ = bert.half() # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) # resize to resolution that is divisible by 8 but not 16 or 32 SCREAMING_SNAKE_CASE_ = init_image.resize((760, 504) ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] SCREAMING_SNAKE_CASE_ = image[255:258, 383:386, -1] assert image.shape == (504, 760, 3) SCREAMING_SNAKE_CASE_ = np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) SCREAMING_SNAKE_CASE_ = init_image.resize((768, 512) ) SCREAMING_SNAKE_CASE_ = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy' ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] assert image.shape == (512, 768, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1E-2
31
1
import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : str , _lowerCAmelCase : str , _lowerCAmelCase : Optional[int]=7 , _lowerCAmelCase : Optional[Any]=3 , _lowerCAmelCase : List[Any]=18 , _lowerCAmelCase : List[str]=30 , _lowerCAmelCase : Tuple=400 , _lowerCAmelCase : int=True , _lowerCAmelCase : Optional[int]=None , _lowerCAmelCase : str=True , ): SCREAMING_SNAKE_CASE_ = size if size is not None else {'height': 18, 'width': 18} SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = min_resolution SCREAMING_SNAKE_CASE_ = max_resolution SCREAMING_SNAKE_CASE_ = do_resize SCREAMING_SNAKE_CASE_ = size SCREAMING_SNAKE_CASE_ = apply_ocr def lowerCAmelCase_ ( self : Optional[Any] ): return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = LayoutLMvaImageProcessor if is_pytesseract_available() else None def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = LayoutLMvaImageProcessingTester(self ) @property def lowerCAmelCase_ ( self : List[Any] ): return self.image_processor_tester.prepare_image_processor_dict() def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_lowerCAmelCase , 'do_resize' ) ) self.assertTrue(hasattr(_lowerCAmelCase , 'size' ) ) self.assertTrue(hasattr(_lowerCAmelCase , 'apply_ocr' ) ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 18, 'width': 18} ) SCREAMING_SNAKE_CASE_ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'height': 42, 'width': 42} ) def lowerCAmelCase_ ( self : Optional[Any] ): pass def lowerCAmelCase_ ( self : Any ): # Initialize image_processing SCREAMING_SNAKE_CASE_ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE_ = 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_ = image_processing(image_inputs[0] , return_tensors='pt' ) self.assertEqual( encoding.pixel_values.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) self.assertIsInstance(encoding.words , _lowerCAmelCase ) self.assertIsInstance(encoding.boxes , _lowerCAmelCase ) # Test batched SCREAMING_SNAKE_CASE_ = image_processing(_lowerCAmelCase , 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.size['height'], self.image_processor_tester.size['width'], ) , ) def lowerCAmelCase_ ( self : int ): # Initialize image_processing SCREAMING_SNAKE_CASE_ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE_ = 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_ = 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.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched SCREAMING_SNAKE_CASE_ = image_processing(_lowerCAmelCase , 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.size['height'], self.image_processor_tester.size['width'], ) , ) def lowerCAmelCase_ ( self : Optional[Any] ): # Initialize image_processing SCREAMING_SNAKE_CASE_ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE_ = 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_ = 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.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched SCREAMING_SNAKE_CASE_ = image_processing(_lowerCAmelCase , 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.size['height'], self.image_processor_tester.size['width'], ) , ) def lowerCAmelCase_ ( self : List[str] ): # with apply_OCR = True SCREAMING_SNAKE_CASE_ = LayoutLMvaImageProcessor() from datasets import load_dataset SCREAMING_SNAKE_CASE_ = load_dataset('hf-internal-testing/fixtures_docvqa' , split='test' ) SCREAMING_SNAKE_CASE_ = Image.open(ds[0]['file'] ).convert('RGB' ) SCREAMING_SNAKE_CASE_ = image_processing(_lowerCAmelCase , return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) , len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 SCREAMING_SNAKE_CASE_ = [['11:14', 'to', '11:39', 'a.m', '11:39', 'to', '11:44', 'a.m.', '11:44', 'a.m.', 'to', '12:25', 'p.m.', '12:25', 'to', '12:58', 'p.m.', '12:58', 'to', '4:00', 'p.m.', '2:00', 'to', '5:00', 'p.m.', 'Coffee', 'Break', 'Coffee', 'will', 'be', 'served', 'for', 'men', 'and', 'women', 'in', 'the', 'lobby', 'adjacent', 'to', 'exhibit', 'area.', 'Please', 'move', 'into', 'exhibit', 'area.', '(Exhibits', 'Open)', 'TRRF', 'GENERAL', 'SESSION', '(PART', '|)', 'Presiding:', 'Lee', 'A.', 'Waller', 'TRRF', 'Vice', 'President', '“Introductory', 'Remarks”', 'Lee', 'A.', 'Waller,', 'TRRF', 'Vice', 'Presi-', 'dent', 'Individual', 'Interviews', 'with', 'TRRF', 'Public', 'Board', 'Members', 'and', 'Sci-', 'entific', 'Advisory', 'Council', 'Mem-', 'bers', 'Conducted', 'by', 'TRRF', 'Treasurer', 'Philip', 'G.', 'Kuehn', 'to', 'get', 'answers', 'which', 'the', 'public', 'refrigerated', 'warehousing', 'industry', 'is', 'looking', 'for.', 'Plus', 'questions', 'from', 'the', 'floor.', 'Dr.', 'Emil', 'M.', 'Mrak,', 'University', 'of', 'Cal-', 'ifornia,', 'Chairman,', 'TRRF', 'Board;', 'Sam', 'R.', 'Cecil,', 'University', 'of', 'Georgia', 'College', 'of', 'Agriculture;', 'Dr.', 'Stanley', 'Charm,', 'Tufts', 'University', 'School', 'of', 'Medicine;', 'Dr.', 'Robert', 'H.', 'Cotton,', 'ITT', 'Continental', 'Baking', 'Company;', 'Dr.', 'Owen', 'Fennema,', 'University', 'of', 'Wis-', 'consin;', 'Dr.', 'Robert', 'E.', 'Hardenburg,', 'USDA.', 'Questions', 'and', 'Answers', 'Exhibits', 'Open', 'Capt.', 'Jack', 'Stoney', 'Room', 'TRRF', 'Scientific', 'Advisory', 'Council', 'Meeting', 'Ballroom', 'Foyer']] # noqa: E231 SCREAMING_SNAKE_CASE_ = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words , _lowerCAmelCase ) self.assertListEqual(encoding.boxes , _lowerCAmelCase ) # with apply_OCR = False SCREAMING_SNAKE_CASE_ = LayoutLMvaImageProcessor(apply_ocr=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = image_processing(_lowerCAmelCase , return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) )
31
from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import TensorType, logging if TYPE_CHECKING: from ...onnx.config import PatchingSpec from ...tokenization_utils_base import PreTrainedTokenizerBase lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Dict = { 'allenai/longformer-base-4096': 'https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json', 'allenai/longformer-large-4096': 'https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json', 'allenai/longformer-large-4096-finetuned-triviaqa': ( 'https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json' ), 'allenai/longformer-base-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json' ), 'allenai/longformer-large-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json' ), } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "longformer" def __init__( self : Union[str, Any] , _lowerCAmelCase : Union[List[int], int] = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 1 , _lowerCAmelCase : int = 0 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 30_522 , _lowerCAmelCase : int = 768 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 3_072 , _lowerCAmelCase : str = "gelu" , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : int = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : float = 0.02 , _lowerCAmelCase : float = 1E-12 , _lowerCAmelCase : bool = False , **_lowerCAmelCase : Union[str, Any] , ): super().__init__(pad_token_id=_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = attention_window SCREAMING_SNAKE_CASE_ = sep_token_id SCREAMING_SNAKE_CASE_ = bos_token_id SCREAMING_SNAKE_CASE_ = eos_token_id SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = type_vocab_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = onnx_export class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[Any] , _lowerCAmelCase : "PretrainedConfig" , _lowerCAmelCase : str = "default" , _lowerCAmelCase : "List[PatchingSpec]" = None ): super().__init__(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = True @property def lowerCAmelCase_ ( self : Any ): if self.task == "multiple-choice": SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'choice', 2: 'sequence'} else: SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('global_attention_mask', dynamic_axis), ] ) @property def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = super().outputs if self.task == "default": SCREAMING_SNAKE_CASE_ = {0: 'batch'} return outputs @property def lowerCAmelCase_ ( self : str ): return 1E-4 @property def lowerCAmelCase_ ( self : Optional[Any] ): # needs to be >= 14 to support tril operator return max(super().default_onnx_opset , 14 ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : "PreTrainedTokenizerBase" , _lowerCAmelCase : int = -1 , _lowerCAmelCase : int = -1 , _lowerCAmelCase : bool = False , _lowerCAmelCase : Optional[TensorType] = None , ): SCREAMING_SNAKE_CASE_ = super().generate_dummy_inputs( preprocessor=_lowerCAmelCase , batch_size=_lowerCAmelCase , seq_length=_lowerCAmelCase , is_pair=_lowerCAmelCase , framework=_lowerCAmelCase ) import torch # for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64) # makes the export fail randomly SCREAMING_SNAKE_CASE_ = torch.zeros_like(inputs['input_ids'] ) # make every second token global SCREAMING_SNAKE_CASE_ = 1 return inputs
31
1
import json import os from typing import Optional, Tuple import regex as re from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCamelCase__ : str = logging.get_logger(__name__) lowerCamelCase__ : List[str] = { 'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', } lowerCamelCase__ : Union[str, Any] = { 'vocab_file': {'ctrl': 'https://raw.githubusercontent.com/salesforce/ctrl/master/ctrl-vocab.json'}, 'merges_file': {'ctrl': 'https://raw.githubusercontent.com/salesforce/ctrl/master/ctrl-merges.txt'}, } lowerCamelCase__ : Dict = { 'ctrl': 256, } lowerCamelCase__ : Tuple = { 'Pregnancy': 168_629, 'Christianity': 7_675, 'Explain': 106_423, 'Fitness': 63_440, 'Saving': 63_163, 'Ask': 27_171, 'Ass': 95_985, 'Joke': 163_509, 'Questions': 45_622, 'Thoughts': 49_605, 'Retail': 52_342, 'Feminism': 164_338, 'Writing': 11_992, 'Atheism': 192_263, 'Netflix': 48_616, 'Computing': 39_639, 'Opinion': 43_213, 'Alone': 44_967, 'Funny': 58_917, 'Gaming': 40_358, 'Human': 4_088, 'India': 1_331, 'Joker': 77_138, 'Diet': 36_206, 'Legal': 11_859, 'Norman': 4_939, 'Tip': 72_689, 'Weight': 52_343, 'Movies': 46_273, 'Running': 23_425, 'Science': 2_090, 'Horror': 37_793, 'Confession': 60_572, 'Finance': 12_250, 'Politics': 16_360, 'Scary': 191_985, 'Support': 12_654, 'Technologies': 32_516, 'Teenage': 66_160, 'Event': 32_769, 'Learned': 67_460, 'Notion': 182_770, 'Wikipedia': 37_583, 'Books': 6_665, 'Extract': 76_050, 'Confessions': 102_701, 'Conspiracy': 75_932, 'Links': 63_674, 'Narcissus': 150_425, 'Relationship': 54_766, 'Relationships': 134_796, 'Reviews': 41_671, 'News': 4_256, 'Translation': 26_820, 'multilingual': 128_406, } def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> Tuple: SCREAMING_SNAKE_CASE_ = set() SCREAMING_SNAKE_CASE_ = word[0] for char in word[1:]: pairs.add((prev_char, char) ) SCREAMING_SNAKE_CASE_ = char SCREAMING_SNAKE_CASE_ = set(__UpperCAmelCase ) return pairs class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = CONTROL_CODES def __init__( self : str , _lowerCAmelCase : Any , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Optional[Any]="<unk>" , **_lowerCAmelCase : Union[str, Any] ): super().__init__(unk_token=_lowerCAmelCase , **_lowerCAmelCase ) with open(_lowerCAmelCase , encoding='utf-8' ) as vocab_handle: SCREAMING_SNAKE_CASE_ = json.load(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = {v: k for k, v in self.encoder.items()} with open(_lowerCAmelCase , encoding='utf-8' ) as merges_handle: SCREAMING_SNAKE_CASE_ = merges_handle.read().split('\n' )[1:-1] SCREAMING_SNAKE_CASE_ = [tuple(merge.split() ) for merge in merges] SCREAMING_SNAKE_CASE_ = dict(zip(_lowerCAmelCase , range(len(_lowerCAmelCase ) ) ) ) SCREAMING_SNAKE_CASE_ = {} @property def lowerCAmelCase_ ( self : str ): return len(self.encoder ) def lowerCAmelCase_ ( self : int ): return dict(self.encoder , **self.added_tokens_encoder ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : Optional[Any] ): if token in self.cache: return self.cache[token] SCREAMING_SNAKE_CASE_ = tuple(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tuple(list(word[:-1] ) + [word[-1] + '</w>'] ) SCREAMING_SNAKE_CASE_ = get_pairs(_lowerCAmelCase ) if not pairs: return token while True: SCREAMING_SNAKE_CASE_ = min(_lowerCAmelCase , key=lambda _lowerCAmelCase : self.bpe_ranks.get(_lowerCAmelCase , float('inf' ) ) ) if bigram not in self.bpe_ranks: break SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = bigram SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = 0 while i < len(_lowerCAmelCase ): try: SCREAMING_SNAKE_CASE_ = word.index(_lowerCAmelCase , _lowerCAmelCase ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) SCREAMING_SNAKE_CASE_ = j if word[i] == first and i < len(_lowerCAmelCase ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 SCREAMING_SNAKE_CASE_ = tuple(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = new_word if len(_lowerCAmelCase ) == 1: break else: SCREAMING_SNAKE_CASE_ = get_pairs(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = '@@ '.join(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = word[:-4] SCREAMING_SNAKE_CASE_ = word return word def lowerCAmelCase_ ( self : str , _lowerCAmelCase : str ): SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = re.findall(R'\S+\n?' , _lowerCAmelCase ) for token in words: split_tokens.extend(list(self.bpe(_lowerCAmelCase ).split(' ' ) ) ) return split_tokens def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : Optional[Any] ): return self.encoder.get(_lowerCAmelCase , self.encoder.get(self.unk_token ) ) def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : str ): return self.decoder.get(_lowerCAmelCase , self.unk_token ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = ' '.join(_lowerCAmelCase ).replace('@@ ' , '' ).strip() return out_string def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None ): if not os.path.isdir(_lowerCAmelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE_ = os.path.join( _lowerCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ = os.path.join( _lowerCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file'] ) with open(_lowerCAmelCase , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=_lowerCAmelCase , ensure_ascii=_lowerCAmelCase ) + '\n' ) SCREAMING_SNAKE_CASE_ = 0 with open(_lowerCAmelCase , 'w' , encoding='utf-8' ) as writer: writer.write('#version: 0.2\n' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda _lowerCAmelCase : kv[1] ): if index != token_index: logger.warning( F"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive." ' Please check that the tokenizer is not corrupted!' ) SCREAMING_SNAKE_CASE_ = token_index writer.write(' '.join(_lowerCAmelCase ) + '\n' ) index += 1 return vocab_file, merge_file # def decode(self, token_ids, skip_special_tokens=False, clean_up_tokenization_spaces=True): # filtered_tokens = ' '.join(self.convert_ids_to_tokens(token_ids, skip_special_tokens=skip_special_tokens)) # tokens_generated_so_far = re.sub('(@@ )', '', string=filtered_tokens) # tokens_generated_so_far = re.sub('(@@ ?$)', '', string=tokens_generated_so_far) # return ''.join(tokens_generated_so_far)
31
import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : str , *_lowerCAmelCase : Tuple , **_lowerCAmelCase : int ): warnings.warn( 'The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use MobileViTImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
1
import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def UpperCAmelCase_ ( __UpperCAmelCase : Any ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = tokenizer(example['content'] , truncation=__UpperCAmelCase )['input_ids'] SCREAMING_SNAKE_CASE_ = len(example['content'] ) / len(output['input_ids'] ) return output lowerCamelCase__ : List[str] = HfArgumentParser(PretokenizationArguments) lowerCamelCase__ : Any = parser.parse_args() if args.num_workers is None: lowerCamelCase__ : Optional[Any] = multiprocessing.cpu_count() lowerCamelCase__ : Optional[Any] = AutoTokenizer.from_pretrained(args.tokenizer_dir) lowerCamelCase__ : Dict = time.time() lowerCamelCase__ : Dict = load_dataset(args.dataset_name, split='train') print(f'''Dataset loaded in {time.time()-t_start:.2f}s''') lowerCamelCase__ : Optional[Any] = time.time() lowerCamelCase__ : Optional[int] = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ 'repo_name', 'path', 'copies', 'size', 'content', 'license', 'hash', 'line_mean', 'line_max', 'alpha_frac', 'autogenerated', ], ) print(f'''Dataset tokenized in {time.time()-t_start:.2f}s''') lowerCamelCase__ : Union[str, Any] = time.time() ds.push_to_hub(args.tokenized_data_repo) print(f'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
31
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'microsoft/swinv2-tiny-patch4-window8-256': ( 'https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json' ), } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "swinv2" lowercase_ = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : Dict , _lowerCAmelCase : Optional[Any]=224 , _lowerCAmelCase : Optional[int]=4 , _lowerCAmelCase : Tuple=3 , _lowerCAmelCase : Tuple=96 , _lowerCAmelCase : Dict=[2, 2, 6, 2] , _lowerCAmelCase : Optional[Any]=[3, 6, 12, 24] , _lowerCAmelCase : str=7 , _lowerCAmelCase : List[Any]=4.0 , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : List[Any]="gelu" , _lowerCAmelCase : str=False , _lowerCAmelCase : str=0.02 , _lowerCAmelCase : List[Any]=1E-5 , _lowerCAmelCase : str=32 , **_lowerCAmelCase : List[Any] , ): super().__init__(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = embed_dim SCREAMING_SNAKE_CASE_ = depths SCREAMING_SNAKE_CASE_ = len(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = num_heads SCREAMING_SNAKE_CASE_ = window_size SCREAMING_SNAKE_CASE_ = mlp_ratio SCREAMING_SNAKE_CASE_ = qkv_bias SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = drop_path_rate SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = use_absolute_embeddings SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE_ = int(embed_dim * 2 ** (len(_lowerCAmelCase ) - 1) ) SCREAMING_SNAKE_CASE_ = (0, 0, 0, 0)
31
1
import os import unittest from transformers.models.phobert.tokenization_phobert import VOCAB_FILES_NAMES, PhobertTokenizer from ...test_tokenization_common import TokenizerTesterMixin class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = PhobertTokenizer lowercase_ = False def lowerCAmelCase_ ( self : int ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt SCREAMING_SNAKE_CASE_ = ['T@@', 'i', 'I', 'R@@', 'r', 'e@@'] SCREAMING_SNAKE_CASE_ = dict(zip(_lowerCAmelCase , range(len(_lowerCAmelCase ) ) ) ) SCREAMING_SNAKE_CASE_ = ['#version: 0.2', 'l à</w>'] SCREAMING_SNAKE_CASE_ = {'unk_token': '<unk>'} SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: for token in vocab_tokens: fp.write(F"{token} {vocab_tokens[token]}\n" ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(_lowerCAmelCase ) ) def lowerCAmelCase_ ( self : Union[str, Any] , **_lowerCAmelCase : str ): kwargs.update(self.special_tokens_map ) return PhobertTokenizer.from_pretrained(self.tmpdirname , **_lowerCAmelCase ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Tuple ): SCREAMING_SNAKE_CASE_ = 'Tôi là VinAI Research' SCREAMING_SNAKE_CASE_ = 'T<unk> i <unk> <unk> <unk> <unk> <unk> <unk> I Re<unk> e<unk> <unk> <unk> <unk>' return input_text, output_text def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = PhobertTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) SCREAMING_SNAKE_CASE_ = 'Tôi là VinAI Research' SCREAMING_SNAKE_CASE_ = 'T@@ ô@@ i l@@ à V@@ i@@ n@@ A@@ I R@@ e@@ s@@ e@@ a@@ r@@ c@@ h'.split() SCREAMING_SNAKE_CASE_ = tokenizer.tokenize(_lowerCAmelCase ) print(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE_ = [4, 3, 5, 3, 3, 3, 3, 3, 3, 6, 7, 9, 3, 9, 3, 3, 3, 3, 3] self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCAmelCase ) , _lowerCAmelCase )
31
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch lowerCamelCase__ : Dict = random.Random() def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : Tuple=1.0 , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Dict=None ) -> Tuple: if rng is None: SCREAMING_SNAKE_CASE_ = global_rng SCREAMING_SNAKE_CASE_ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any]=7 , _lowerCAmelCase : Union[str, Any]=400 , _lowerCAmelCase : Tuple=2_000 , _lowerCAmelCase : str=1 , _lowerCAmelCase : int=0.0 , _lowerCAmelCase : Optional[Any]=16_000 , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Any=80 , _lowerCAmelCase : Union[str, Any]=16 , _lowerCAmelCase : List[str]=64 , _lowerCAmelCase : List[Any]="hann_window" , _lowerCAmelCase : Any=80 , _lowerCAmelCase : List[Any]=7_600 , _lowerCAmelCase : List[Any]=1E-10 , _lowerCAmelCase : Optional[Any]=True , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = min_seq_length SCREAMING_SNAKE_CASE_ = max_seq_length SCREAMING_SNAKE_CASE_ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE_ = feature_size SCREAMING_SNAKE_CASE_ = padding_value SCREAMING_SNAKE_CASE_ = sampling_rate SCREAMING_SNAKE_CASE_ = do_normalize SCREAMING_SNAKE_CASE_ = num_mel_bins SCREAMING_SNAKE_CASE_ = hop_length SCREAMING_SNAKE_CASE_ = win_length SCREAMING_SNAKE_CASE_ = win_function SCREAMING_SNAKE_CASE_ = fmin SCREAMING_SNAKE_CASE_ = fmax SCREAMING_SNAKE_CASE_ = mel_floor SCREAMING_SNAKE_CASE_ = return_attention_mask def lowerCAmelCase_ ( self : Union[str, Any] ): return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : str=False ): def _flatten(_lowerCAmelCase : Dict ): return list(itertools.chain(*_lowerCAmelCase ) ) if equal_length: SCREAMING_SNAKE_CASE_ = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Union[str, Any]=False , _lowerCAmelCase : Optional[int]=False ): if equal_length: SCREAMING_SNAKE_CASE_ = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = SpeechTaFeatureExtractor def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractionTester(self ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : int ): self.assertTrue(np.all(np.mean(_lowerCAmelCase , axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_lowerCAmelCase , axis=0 ) - 1 ) < 1E-3 ) ) def lowerCAmelCase_ ( self : List[Any] ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE_ = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , padding=_lowerCAmelCase , max_length=_lowerCAmelCase , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self.assertTrue(input_values[0][800:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self.assertTrue(input_values[0][1_000:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = range(800 , 1_400 , 200 ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in lengths] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , max_length=_lowerCAmelCase , padding=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='max_length' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1_000) ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=2_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1_200) ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = np.random.rand(100 ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def lowerCAmelCase_ ( self : Tuple ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test feature size SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , padding=_lowerCAmelCase , return_tensors='np' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input SCREAMING_SNAKE_CASE_ = feature_extractor(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE_ = np.asarray(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_lowerCAmelCase ) == len(_lowerCAmelCase ) for x, y in zip(_lowerCAmelCase , processed_features[input_name] ) ) ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='np' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='pt' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' )[input_name] SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='pt' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = min(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad( _lowerCAmelCase , padding='max_length' , max_length=_lowerCAmelCase , truncation=_lowerCAmelCase , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Tuple ): from datasets import load_dataset SCREAMING_SNAKE_CASE_ = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE_ = ds.sort('id' ).select(range(_lowerCAmelCase ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] def lowerCAmelCase_ ( self : Any ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [2.3_804E-03, 2.0_752E-03, 1.9_836E-03, 2.1_057E-03, 1.6_174E-03, 3.0_518E-04, 9.1_553E-05, 3.3_569E-04, 9.7_656E-04, 1.8_311E-03, 2.0_142E-03, 2.1_057E-03, 1.7_395E-03, 4.5_776E-04, -3.9_673E-04, 4.5_776E-04, 1.0_071E-03, 9.1_553E-05, 4.8_828E-04, 1.1_597E-03, 7.3_242E-04, 9.4_604E-04, 1.8_005E-03, 1.8_311E-03, 8.8_501E-04, 4.2_725E-04, 4.8_828E-04, 7.3_242E-04, 1.0_986E-03, 2.1_057E-03] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 93_680) ) self.assertTrue(torch.allclose(input_values[0, :30] , _lowerCAmelCase , atol=1E-6 ) ) def lowerCAmelCase_ ( self : Optional[int] ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [-2.6870, -3.0104, -3.1356, -3.5352, -3.0044, -3.0353, -3.4719, -3.6777, -3.1520, -2.9435, -2.6553, -2.8795, -2.9944, -2.5921, -3.0279, -3.0386, -3.0864, -3.1291, -3.2353, -2.7444, -2.6831, -2.7287, -3.1761, -3.1571, -3.2726, -3.0582, -3.1007, -3.4533, -3.4695, -3.0998] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 366, 80) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] , _lowerCAmelCase , atol=1E-4 ) )
31
1
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = CLIPTokenizer lowercase_ = CLIPTokenizerFast lowercase_ = True lowercase_ = {} lowercase_ = False def lowerCAmelCase_ ( self : Optional[Any] ): super().setUp() # fmt: off SCREAMING_SNAKE_CASE_ = ['l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>'] # fmt: on SCREAMING_SNAKE_CASE_ = dict(zip(_lowerCAmelCase , range(len(_lowerCAmelCase ) ) ) ) SCREAMING_SNAKE_CASE_ = ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>'] SCREAMING_SNAKE_CASE_ = {'unk_token': '<unk>'} SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ = 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(_lowerCAmelCase ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(_lowerCAmelCase ) ) def lowerCAmelCase_ ( self : str , **_lowerCAmelCase : Optional[Any] ): kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname , **_lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple , **_lowerCAmelCase : int ): kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **_lowerCAmelCase ) def lowerCAmelCase_ ( self : int , _lowerCAmelCase : List[Any] ): SCREAMING_SNAKE_CASE_ = 'lower newer' SCREAMING_SNAKE_CASE_ = 'lower newer' return input_text, output_text def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = CLIPTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) SCREAMING_SNAKE_CASE_ = 'lower newer' SCREAMING_SNAKE_CASE_ = ['lo', 'w', 'er</w>', 'n', 'e', 'w', 'er</w>'] SCREAMING_SNAKE_CASE_ = tokenizer.tokenize(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE_ = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCAmelCase ) , _lowerCAmelCase ) @require_ftfy def lowerCAmelCase_ ( self : Dict ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): SCREAMING_SNAKE_CASE_ = self.tokenizer_class.from_pretrained(_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.rust_tokenizer_class.from_pretrained(_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.' SCREAMING_SNAKE_CASE_ = tokenizer_s.tokenize(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer_r.tokenize(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways SCREAMING_SNAKE_CASE_ = 'xa\u0303y' + ' ' + 'x\xe3y' SCREAMING_SNAKE_CASE_ = tokenizer_s.tokenize(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer_r.tokenize(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) # Test that the tokenization is identical on unicode of space type SCREAMING_SNAKE_CASE_ = [ '\u0009', # (horizontal tab, '\t') '\u000B', # (vertical tab) '\u000C', # (form feed) '\u0020', # (space, ' ') '\u200E', # (left-to-right mark):w '\u200F', # (right-to-left mark) ] for unicode_seq in spaces_unicodes: SCREAMING_SNAKE_CASE_ = tokenizer_s.tokenize(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer_r.tokenize(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) # Test that the tokenization is identical on unicode of line break type SCREAMING_SNAKE_CASE_ = [ '\u000A', # (line feed, '\n') '\r\n', # (carriage return and line feed, '\r\n') '\u000D', # (carriage return, '\r') '\r', # (carriage return, '\r') '\u000D', # (carriage return, '\r') '\u2028', # (line separator) '\u2029', # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: SCREAMING_SNAKE_CASE_ = tokenizer_s.tokenize(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer_r.tokenize(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): SCREAMING_SNAKE_CASE_ = 'hello' # `hello` is a token in the vocabulary of `pretrained_name` SCREAMING_SNAKE_CASE_ = F"{text_of_1_token} {text_of_1_token}" SCREAMING_SNAKE_CASE_ = self.rust_tokenizer_class.from_pretrained( _lowerCAmelCase , use_fast=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = tokenizer_r(_lowerCAmelCase , return_offsets_mapping=_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_lowerCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_lowerCAmelCase ) + 1, len(_lowerCAmelCase ) + 1 + len(_lowerCAmelCase )) , ) SCREAMING_SNAKE_CASE_ = F" {text}" SCREAMING_SNAKE_CASE_ = self.rust_tokenizer_class.from_pretrained( _lowerCAmelCase , use_fast=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = tokenizer_r(_lowerCAmelCase , return_offsets_mapping=_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_lowerCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_lowerCAmelCase ) + 1, 1 + len(_lowerCAmelCase ) + 1 + len(_lowerCAmelCase )) , ) def lowerCAmelCase_ ( self : Optional[int] ): # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(_lowerCAmelCase ) as context: self.rust_tokenizer_class.from_pretrained('robot-test/old-clip-tokenizer' ) self.assertTrue( context.exception.args[0].startswith( 'The `backend_tokenizer` provided does not match the expected format.' ) ) @require_ftfy def lowerCAmelCase_ ( self : Dict ): super().test_tokenization_python_rust_equals() def lowerCAmelCase_ ( self : Any ): # CLIP always lower cases letters pass
31
from __future__ import annotations from typing import TypedDict class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = 42 lowercase_ = 42 def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> list[str]: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) return [s[i:] + s[:i] for i in range(len(__UpperCAmelCase ) )] def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> BWTTransformDict: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) if not s: raise ValueError('The parameter s must not be empty.' ) SCREAMING_SNAKE_CASE_ = all_rotations(__UpperCAmelCase ) rotations.sort() # sort the list of rotations in alphabetically order # make a string composed of the last char of each rotation SCREAMING_SNAKE_CASE_ = { "bwt_string": "".join([word[-1] for word in rotations] ), "idx_original_string": rotations.index(__UpperCAmelCase ), } return response def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : int ) -> str: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter bwt_string type must be str.' ) if not bwt_string: raise ValueError('The parameter bwt_string must not be empty.' ) try: SCREAMING_SNAKE_CASE_ = int(__UpperCAmelCase ) except ValueError: raise TypeError( 'The parameter idx_original_string type must be int or passive' ' of cast to int.' ) if idx_original_string < 0: raise ValueError('The parameter idx_original_string must not be lower than 0.' ) if idx_original_string >= len(__UpperCAmelCase ): raise ValueError( 'The parameter idx_original_string must be lower than' ' len(bwt_string).' ) SCREAMING_SNAKE_CASE_ = [''] * len(__UpperCAmelCase ) for _ in range(len(__UpperCAmelCase ) ): for i in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = bwt_string[i] + ordered_rotations[i] ordered_rotations.sort() return ordered_rotations[idx_original_string] if __name__ == "__main__": lowerCamelCase__ : Optional[int] = 'Provide a string that I will generate its BWT transform: ' lowerCamelCase__ : List[str] = input(entry_msg).strip() lowerCamelCase__ : int = bwt_transform(s) print( f'''Burrows Wheeler transform for string \'{s}\' results ''' f'''in \'{result['bwt_string']}\'''' ) lowerCamelCase__ : Dict = reverse_bwt(result['bwt_string'], result['idx_original_string']) print( f'''Reversing Burrows Wheeler transform for entry \'{result['bwt_string']}\' ''' f'''we get original string \'{original_string}\'''' )
31
1
import numpy as np def UpperCAmelCase_ ( __UpperCAmelCase : np.array ) -> np.array: return 1 / (1 + np.exp(-vector )) if __name__ == "__main__": import doctest doctest.testmod()
31
class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = {} def lowerCAmelCase_ ( self : List[str] ): print(self.vertex ) for i in self.vertex: print(_lowerCAmelCase , ' -> ' , ' -> '.join([str(_lowerCAmelCase ) for j in self.vertex[i]] ) ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int , _lowerCAmelCase : int ): # check if vertex is already present, if from_vertex in self.vertex: self.vertex[from_vertex].append(_lowerCAmelCase ) else: # else make a new vertex SCREAMING_SNAKE_CASE_ = [to_vertex] def lowerCAmelCase_ ( self : Optional[Any] ): # visited array for storing already visited nodes SCREAMING_SNAKE_CASE_ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : int , _lowerCAmelCase : list ): # mark start vertex as visited SCREAMING_SNAKE_CASE_ = True print(_lowerCAmelCase , end=' ' ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) if __name__ == "__main__": lowerCamelCase__ : List[Any] = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('DFS:') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
31
1
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation lowerCamelCase__ : str = logging.get_logger(__name__) lowerCamelCase__ : Tuple = {'tokenizer_file': 'tokenizer.json'} lowerCamelCase__ : Optional[int] = { 'tokenizer_file': { 'bigscience/tokenizer': 'https://huggingface.co/bigscience/tokenizer/blob/main/tokenizer.json', 'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/tokenizer.json', 'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/tokenizer.json', 'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/tokenizer.json', 'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/tokenizer.json', 'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/tokenizer.json', 'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/blob/main/tokenizer.json', }, } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = ["input_ids", "attention_mask"] lowercase_ = None def __init__( self : int , _lowerCAmelCase : Dict=None , _lowerCAmelCase : List[str]=None , _lowerCAmelCase : List[Any]=None , _lowerCAmelCase : Tuple="<unk>" , _lowerCAmelCase : Optional[Any]="<s>" , _lowerCAmelCase : List[str]="</s>" , _lowerCAmelCase : Optional[Any]="<pad>" , _lowerCAmelCase : str=False , _lowerCAmelCase : Any=False , **_lowerCAmelCase : int , ): super().__init__( _lowerCAmelCase , _lowerCAmelCase , tokenizer_file=_lowerCAmelCase , unk_token=_lowerCAmelCase , bos_token=_lowerCAmelCase , eos_token=_lowerCAmelCase , pad_token=_lowerCAmelCase , add_prefix_space=_lowerCAmelCase , clean_up_tokenization_spaces=_lowerCAmelCase , **_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('add_prefix_space' , _lowerCAmelCase ) != add_prefix_space: SCREAMING_SNAKE_CASE_ = getattr(_lowerCAmelCase , pre_tok_state.pop('type' ) ) SCREAMING_SNAKE_CASE_ = add_prefix_space SCREAMING_SNAKE_CASE_ = pre_tok_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = add_prefix_space def lowerCAmelCase_ ( self : Optional[Any] , *_lowerCAmelCase : List[str] , **_lowerCAmelCase : Optional[int] ): SCREAMING_SNAKE_CASE_ = kwargs.get('is_split_into_words' , _lowerCAmelCase ) if not (self.add_prefix_space or not is_split_into_words): raise Exception( F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with" ' pretokenized inputs.' ) return super()._batch_encode_plus(*_lowerCAmelCase , **_lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[Any] , *_lowerCAmelCase : List[Any] , **_lowerCAmelCase : List[str] ): SCREAMING_SNAKE_CASE_ = kwargs.get('is_split_into_words' , _lowerCAmelCase ) if not (self.add_prefix_space or not is_split_into_words): raise Exception( F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with" ' pretokenized inputs.' ) return super()._encode_plus(*_lowerCAmelCase , **_lowerCAmelCase ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None ): SCREAMING_SNAKE_CASE_ = self._tokenizer.model.save(_lowerCAmelCase , name=_lowerCAmelCase ) return tuple(_lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : "Conversation" ): SCREAMING_SNAKE_CASE_ = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) + [self.eos_token_id] ) if len(_lowerCAmelCase ) > self.model_max_length: SCREAMING_SNAKE_CASE_ = input_ids[-self.model_max_length :] return input_ids
31
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : str = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/config.json', 'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json', 'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/config.json', 'funnel-transformer/medium-base': 'https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json', 'funnel-transformer/intermediate': ( 'https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json' ), 'funnel-transformer/intermediate-base': ( 'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json' ), 'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/config.json', 'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json', 'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json', 'funnel-transformer/xlarge-base': 'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json', } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "funnel" lowercase_ = { "hidden_size": "d_model", "num_attention_heads": "n_head", } def __init__( self : int , _lowerCAmelCase : Optional[int]=30_522 , _lowerCAmelCase : List[str]=[4, 4, 4] , _lowerCAmelCase : Tuple=None , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : int=768 , _lowerCAmelCase : Optional[Any]=12 , _lowerCAmelCase : Optional[Any]=64 , _lowerCAmelCase : Optional[Any]=3_072 , _lowerCAmelCase : List[str]="gelu_new" , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : int=0.1 , _lowerCAmelCase : Tuple=0.0 , _lowerCAmelCase : List[Any]=0.1 , _lowerCAmelCase : Dict=None , _lowerCAmelCase : str=1E-9 , _lowerCAmelCase : Any="mean" , _lowerCAmelCase : Union[str, Any]="relative_shift" , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Dict=True , _lowerCAmelCase : Tuple=True , **_lowerCAmelCase : Optional[Any] , ): SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = block_sizes SCREAMING_SNAKE_CASE_ = [1] * len(_lowerCAmelCase ) if block_repeats is None else block_repeats assert len(_lowerCAmelCase ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." SCREAMING_SNAKE_CASE_ = num_decoder_layers SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = n_head SCREAMING_SNAKE_CASE_ = d_head SCREAMING_SNAKE_CASE_ = d_inner SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = initializer_std SCREAMING_SNAKE_CASE_ = layer_norm_eps assert pooling_type in [ "mean", "max", ], F"Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported." SCREAMING_SNAKE_CASE_ = pooling_type assert attention_type in [ "relative_shift", "factorized", ], F"Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported." SCREAMING_SNAKE_CASE_ = attention_type SCREAMING_SNAKE_CASE_ = separate_cls SCREAMING_SNAKE_CASE_ = truncate_seq SCREAMING_SNAKE_CASE_ = pool_q_only super().__init__(**_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : Optional[int] ): return sum(self.block_sizes ) @num_hidden_layers.setter def lowerCAmelCase_ ( self : int , _lowerCAmelCase : List[Any] ): raise NotImplementedError( 'This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.' ) @property def lowerCAmelCase_ ( self : List[Any] ): return len(self.block_sizes ) @num_blocks.setter def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any] ): raise NotImplementedError('This model does not support the setting of `num_blocks`. Please set `block_sizes`.' )
31
1
from functools import reduce lowerCamelCase__ : Any = ( '73167176531330624919225119674426574742355349194934' '96983520312774506326239578318016984801869478851843' '85861560789112949495459501737958331952853208805511' '12540698747158523863050715693290963295227443043557' '66896648950445244523161731856403098711121722383113' '62229893423380308135336276614282806444486645238749' '30358907296290491560440772390713810515859307960866' '70172427121883998797908792274921901699720888093776' '65727333001053367881220235421809751254540594752243' '52584907711670556013604839586446706324415722155397' '53697817977846174064955149290862569321978468622482' '83972241375657056057490261407972968652414535100474' '82166370484403199890008895243450658541227588666881' '16427171479924442928230863465674813919123162824586' '17866458359124566529476545682848912883142607690042' '24219022671055626321111109370544217506941658960408' '07198403850962455444362981230987879927244284909188' '84580156166097919133875499200524063689912560717606' '05886116467109405077541002256983155200055935729725' '71636269561882670428252483600823257530420752963450' ) def UpperCAmelCase_ ( __UpperCAmelCase : str = N ) -> int: return max( # mypy cannot properly interpret reduce int(reduce(lambda __UpperCAmelCase , __UpperCAmelCase : str(int(__UpperCAmelCase ) * int(__UpperCAmelCase ) ) , n[i : i + 13] ) ) for i in range(len(__UpperCAmelCase ) - 12 ) ) if __name__ == "__main__": print(f'''{solution() = }''')
31
from __future__ import annotations from collections.abc import Iterator class lowerCamelCase_ : '''simple docstring''' def __init__( self : Union[str, Any] , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = value SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None class lowerCamelCase_ : '''simple docstring''' def __init__( self : int , _lowerCAmelCase : Node ): SCREAMING_SNAKE_CASE_ = tree def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Node | None ): if node is None: return 0 return node.value + ( self.depth_first_search(node.left ) + self.depth_first_search(node.right ) ) def __iter__( self : Dict ): yield self.depth_first_search(self.tree ) if __name__ == "__main__": import doctest doctest.testmod()
31
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, is_vision_available, ) lowerCamelCase__ : List[Any] = {'configuration_vit': ['VIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ViTConfig', 'ViTOnnxConfig']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Optional[Any] = ['ViTFeatureExtractor'] lowerCamelCase__ : Dict = ['ViTImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Any = [ 'VIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'ViTForImageClassification', 'ViTForMaskedImageModeling', 'ViTModel', 'ViTPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Dict = [ 'TFViTForImageClassification', 'TFViTModel', 'TFViTPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : int = [ 'FlaxViTForImageClassification', 'FlaxViTModel', 'FlaxViTPreTrainedModel', ] if TYPE_CHECKING: from .configuration_vit import VIT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTConfig, ViTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_vit import ViTFeatureExtractor from .image_processing_vit import ViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit import ( VIT_PRETRAINED_MODEL_ARCHIVE_LIST, ViTForImageClassification, ViTForMaskedImageModeling, ViTModel, ViTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vit import TFViTForImageClassification, TFViTModel, TFViTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel, FlaxViTPreTrainedModel else: import sys lowerCamelCase__ : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
31
def UpperCAmelCase_ ( __UpperCAmelCase : list , __UpperCAmelCase : int , __UpperCAmelCase : int = 0 , __UpperCAmelCase : int = 0 ) -> int: SCREAMING_SNAKE_CASE_ = right or len(__UpperCAmelCase ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(__UpperCAmelCase , __UpperCAmelCase , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
31
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) lowerCamelCase__ : List[str] = { '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: lowerCamelCase__ : Optional[int] = ['LayoutLMv2TokenizerFast'] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : int = ['LayoutLMv2FeatureExtractor'] lowerCamelCase__ : List[Any] = ['LayoutLMv2ImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : List[str] = [ '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 lowerCamelCase__ : int = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
31
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_fnet import FNetTokenizer else: lowerCamelCase__ : Optional[Any] = None lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : List[str] = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} lowerCamelCase__ : List[str] = { 'vocab_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/spiece.model', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/spiece.model', }, 'tokenizer_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json', }, } lowerCamelCase__ : Optional[Any] = { 'google/fnet-base': 512, 'google/fnet-large': 512, } lowerCamelCase__ : List[Any] = '▁' class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = ["input_ids", "token_type_ids"] lowercase_ = FNetTokenizer def __init__( self : List[Any] , _lowerCAmelCase : Dict=None , _lowerCAmelCase : Dict=None , _lowerCAmelCase : List[str]=False , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Tuple=True , _lowerCAmelCase : List[Any]="<unk>" , _lowerCAmelCase : Optional[Any]="[SEP]" , _lowerCAmelCase : Optional[Any]="<pad>" , _lowerCAmelCase : Optional[int]="[CLS]" , _lowerCAmelCase : Optional[Any]="[MASK]" , **_lowerCAmelCase : 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. SCREAMING_SNAKE_CASE_ = ( AddedToken(_lowerCAmelCase , lstrip=_lowerCAmelCase , rstrip=_lowerCAmelCase , normalized=_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ) else mask_token ) super().__init__( _lowerCAmelCase , tokenizer_file=_lowerCAmelCase , do_lower_case=_lowerCAmelCase , remove_space=_lowerCAmelCase , keep_accents=_lowerCAmelCase , unk_token=_lowerCAmelCase , sep_token=_lowerCAmelCase , pad_token=_lowerCAmelCase , cls_token=_lowerCAmelCase , mask_token=_lowerCAmelCase , **_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = do_lower_case SCREAMING_SNAKE_CASE_ = remove_space SCREAMING_SNAKE_CASE_ = keep_accents SCREAMING_SNAKE_CASE_ = vocab_file SCREAMING_SNAKE_CASE_ = False if not self.vocab_file else True def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None ): if not os.path.isdir(_lowerCAmelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE_ = os.path.join( _lowerCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_lowerCAmelCase ): copyfile(self.vocab_file , _lowerCAmelCase ) return (out_vocab_file,)
31
1
lowerCamelCase__ : int = 8.314_4598 def UpperCAmelCase_ ( __UpperCAmelCase : float , __UpperCAmelCase : float ) -> float: if temperature < 0: raise Exception('Temperature cannot be less than 0 K' ) if molar_mass <= 0: raise Exception('Molar mass cannot be less than or equal to 0 kg/mol' ) else: return (3 * UNIVERSAL_GAS_CONSTANT * temperature / molar_mass) ** 0.5 if __name__ == "__main__": import doctest # run doctest doctest.testmod() # example lowerCamelCase__ : str = 300 lowerCamelCase__ : int = 28 lowerCamelCase__ : Optional[int] = rms_speed_of_molecule(temperature, molar_mass) print(f'''Vrms of Nitrogen gas at 300 K is {vrms} m/s''')
31
from __future__ import annotations from collections.abc import Generator def UpperCAmelCase_ ( ) -> Generator[int, None, None]: SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = 2 while True: SCREAMING_SNAKE_CASE_ = factor_map.pop(__UpperCAmelCase , __UpperCAmelCase ) if factor: SCREAMING_SNAKE_CASE_ = factor + prime while x in factor_map: x += factor SCREAMING_SNAKE_CASE_ = factor else: SCREAMING_SNAKE_CASE_ = prime yield prime prime += 1 def UpperCAmelCase_ ( __UpperCAmelCase : float = 1E10 ) -> int: SCREAMING_SNAKE_CASE_ = sieve() SCREAMING_SNAKE_CASE_ = 1 while True: SCREAMING_SNAKE_CASE_ = next(__UpperCAmelCase ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(__UpperCAmelCase ) n += 2 if __name__ == "__main__": print(solution())
31
1
import unittest from diffusers.models.unet_ad_blocks import * # noqa F403 from diffusers.utils import torch_device from .test_unet_blocks_common import UNetBlockTesterMixin class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = DownBlockaD # noqa F405 lowercase_ = "down" def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = [-0.0232, -0.9869, 0.8054, -0.0637, -0.1688, -1.4264, 0.4470, -1.3394, 0.0904] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = ResnetDownsampleBlockaD # noqa F405 lowercase_ = "down" def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = [0.0710, 0.2410, -0.7320, -1.0757, -1.1343, 0.3540, -0.0133, -0.2576, 0.0948] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = AttnDownBlockaD # noqa F405 lowercase_ = "down" def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = [0.0636, 0.8964, -0.6234, -1.0131, 0.0844, 0.4935, 0.3437, 0.0911, -0.2957] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = CrossAttnDownBlockaD # noqa F405 lowercase_ = "down" def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = super().prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = 32 return init_dict, inputs_dict def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = [0.2238, -0.7396, -0.2255, -0.3829, 0.1925, 1.1665, 0.0603, -0.7295, 0.1983] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = SimpleCrossAttnDownBlockaD # noqa F405 lowercase_ = "down" @property def lowerCAmelCase_ ( self : Optional[int] ): return super().get_dummy_input(include_encoder_hidden_states=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = super().prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = 32 return init_dict, inputs_dict @unittest.skipIf(torch_device == 'mps' , 'MPS result is not consistent' ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = [0.7921, -0.0992, -0.1962, -0.7695, -0.4242, 0.7804, 0.4737, 0.2765, 0.3338] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = SkipDownBlockaD # noqa F405 lowercase_ = "down" @property def lowerCAmelCase_ ( self : Optional[Any] ): return super().get_dummy_input(include_skip_sample=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = [-0.0845, -0.2087, -0.2465, 0.0971, 0.1900, -0.0484, 0.2664, 0.4179, 0.5069] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = AttnSkipDownBlockaD # noqa F405 lowercase_ = "down" @property def lowerCAmelCase_ ( self : int ): return super().get_dummy_input(include_skip_sample=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = [0.5539, 0.1609, 0.4924, 0.0537, -0.1995, 0.4050, 0.0979, -0.2721, -0.0642] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = DownEncoderBlockaD # noqa F405 lowercase_ = "down" @property def lowerCAmelCase_ ( self : List[str] ): return super().get_dummy_input(include_temb=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = { 'in_channels': 32, 'out_channels': 32, } SCREAMING_SNAKE_CASE_ = self.dummy_input return init_dict, inputs_dict def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = [1.1102, 0.5302, 0.4872, -0.0023, -0.8042, 0.0483, -0.3489, -0.5632, 0.7626] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = AttnDownEncoderBlockaD # noqa F405 lowercase_ = "down" @property def lowerCAmelCase_ ( self : Union[str, Any] ): return super().get_dummy_input(include_temb=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = { 'in_channels': 32, 'out_channels': 32, } SCREAMING_SNAKE_CASE_ = self.dummy_input return init_dict, inputs_dict def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = [0.8966, -0.1486, 0.8568, 0.8141, -0.9046, -0.1342, -0.0972, -0.7417, 0.1538] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = UNetMidBlockaD # noqa F405 lowercase_ = "mid" def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = { 'in_channels': 32, 'temb_channels': 128, } SCREAMING_SNAKE_CASE_ = self.dummy_input return init_dict, inputs_dict def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = [-0.1062, 1.7248, 0.3494, 1.4569, -0.0910, -1.2421, -0.9984, 0.6736, 1.0028] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = UNetMidBlockaDCrossAttn # noqa F405 lowercase_ = "mid" def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = super().prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = 32 return init_dict, inputs_dict def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = [0.0187, 2.4220, 0.4484, 1.1203, -0.6121, -1.5122, -0.8270, 0.7851, 1.8335] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = UNetMidBlockaDSimpleCrossAttn # noqa F405 lowercase_ = "mid" @property def lowerCAmelCase_ ( self : List[Any] ): return super().get_dummy_input(include_encoder_hidden_states=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = super().prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = 32 return init_dict, inputs_dict def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = [0.7143, 1.9974, 0.5448, 1.3977, 0.1282, -1.1237, -1.4238, 0.5530, 0.8880] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = UpBlockaD # noqa F405 lowercase_ = "up" @property def lowerCAmelCase_ ( self : List[Any] ): return super().get_dummy_input(include_res_hidden_states_tuple=_lowerCAmelCase ) def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = [-0.2041, -0.4165, -0.3022, 0.0041, -0.6628, -0.7053, 0.1928, -0.0325, 0.0523] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = ResnetUpsampleBlockaD # noqa F405 lowercase_ = "up" @property def lowerCAmelCase_ ( self : Union[str, Any] ): return super().get_dummy_input(include_res_hidden_states_tuple=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = [0.2287, 0.3549, -0.1346, 0.4797, -0.1715, -0.9649, 0.7305, -0.5864, -0.6244] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = CrossAttnUpBlockaD # noqa F405 lowercase_ = "up" @property def lowerCAmelCase_ ( self : int ): return super().get_dummy_input(include_res_hidden_states_tuple=_lowerCAmelCase ) def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = super().prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = 32 return init_dict, inputs_dict def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = [-0.1403, -0.3515, -0.0420, -0.1425, 0.3167, 0.5094, -0.2181, 0.5931, 0.5582] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = SimpleCrossAttnUpBlockaD # noqa F405 lowercase_ = "up" @property def lowerCAmelCase_ ( self : Tuple ): return super().get_dummy_input(include_res_hidden_states_tuple=_lowerCAmelCase , include_encoder_hidden_states=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = super().prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = 32 return init_dict, inputs_dict def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = [0.2645, 0.1480, 0.0909, 0.8044, -0.9758, -0.9083, 0.0994, -1.1453, -0.7402] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = AttnUpBlockaD # noqa F405 lowercase_ = "up" @property def lowerCAmelCase_ ( self : Optional[Any] ): return super().get_dummy_input(include_res_hidden_states_tuple=_lowerCAmelCase ) @unittest.skipIf(torch_device == 'mps' , 'MPS result is not consistent' ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = [0.0979, 0.1326, 0.0021, 0.0659, 0.2249, 0.0059, 0.1132, 0.5952, 0.1033] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = SkipUpBlockaD # noqa F405 lowercase_ = "up" @property def lowerCAmelCase_ ( self : Union[str, Any] ): return super().get_dummy_input(include_res_hidden_states_tuple=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = [-0.0893, -0.1234, -0.1506, -0.0332, 0.0123, -0.0211, 0.0566, 0.0143, 0.0362] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = AttnSkipUpBlockaD # noqa F405 lowercase_ = "up" @property def lowerCAmelCase_ ( self : Dict ): return super().get_dummy_input(include_res_hidden_states_tuple=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = [0.0361, 0.0617, 0.2787, -0.0350, 0.0342, 0.3421, -0.0843, 0.0913, 0.3015] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = UpDecoderBlockaD # noqa F405 lowercase_ = "up" @property def lowerCAmelCase_ ( self : List[str] ): return super().get_dummy_input(include_temb=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = {'in_channels': 32, 'out_channels': 32} SCREAMING_SNAKE_CASE_ = self.dummy_input return init_dict, inputs_dict def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = [0.4404, 0.1998, -0.9886, -0.3320, -0.3128, -0.7034, -0.6955, -0.2338, -0.3137] super().test_output(_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = AttnUpDecoderBlockaD # noqa F405 lowercase_ = "up" @property def lowerCAmelCase_ ( self : Optional[int] ): return super().get_dummy_input(include_temb=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = {'in_channels': 32, 'out_channels': 32} SCREAMING_SNAKE_CASE_ = self.dummy_input return init_dict, inputs_dict def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = [0.6738, 0.4491, 0.1055, 1.0710, 0.7316, 0.3339, 0.3352, 0.1023, 0.3568] super().test_output(_lowerCAmelCase )
31
import numpy as np import torch from torch.utils.data import DataLoader from accelerate.utils.dataclasses import DistributedType class lowerCamelCase_ : '''simple docstring''' def __init__( self : Any , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : Any=3 , _lowerCAmelCase : Tuple=64 , _lowerCAmelCase : List[str]=None ): SCREAMING_SNAKE_CASE_ = np.random.default_rng(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = length SCREAMING_SNAKE_CASE_ = rng.normal(size=(length,) ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = a * self.x + b + rng.normal(scale=0.1 , size=(length,) ).astype(np.floataa ) def __len__( self : Optional[int] ): return self.length def __getitem__( self : str , _lowerCAmelCase : Union[str, Any] ): return {"x": self.x[i], "y": self.y[i]} class lowerCamelCase_ ( torch.nn.Module ): '''simple docstring''' def __init__( self : Tuple , _lowerCAmelCase : Dict=0 , _lowerCAmelCase : List[str]=0 , _lowerCAmelCase : str=False ): super().__init__() SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) SCREAMING_SNAKE_CASE_ = True def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any]=None ): if self.first_batch: print(F"Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}" ) SCREAMING_SNAKE_CASE_ = False return x * self.a[0] + self.b[0] class lowerCamelCase_ ( torch.nn.Module ): '''simple docstring''' def __init__( self : Optional[int] , _lowerCAmelCase : Any=0 , _lowerCAmelCase : Any=0 , _lowerCAmelCase : Optional[Any]=False ): super().__init__() SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor(_lowerCAmelCase ).float() ) SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor(_lowerCAmelCase ).float() ) SCREAMING_SNAKE_CASE_ = True def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : Optional[int]=None ): if self.first_batch: print(F"Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}" ) SCREAMING_SNAKE_CASE_ = False return x * self.a + self.b def UpperCAmelCase_ ( __UpperCAmelCase : Dict , __UpperCAmelCase : int = 16 ) -> Union[str, Any]: from datasets import load_dataset from transformers import AutoTokenizer SCREAMING_SNAKE_CASE_ = AutoTokenizer.from_pretrained('bert-base-cased' ) SCREAMING_SNAKE_CASE_ = {'train': 'tests/test_samples/MRPC/train.csv', 'validation': 'tests/test_samples/MRPC/dev.csv'} SCREAMING_SNAKE_CASE_ = load_dataset('csv' , data_files=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = datasets['train'].unique('label' ) SCREAMING_SNAKE_CASE_ = {v: i for i, v in enumerate(__UpperCAmelCase )} def tokenize_function(__UpperCAmelCase : Optional[int] ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE_ = tokenizer( examples['sentence1'] , examples['sentence2'] , truncation=__UpperCAmelCase , max_length=__UpperCAmelCase , padding='max_length' ) if "label" in examples: SCREAMING_SNAKE_CASE_ = [label_to_id[l] for l in examples['label']] return outputs # Apply the method we just defined to all the examples in all the splits of the dataset SCREAMING_SNAKE_CASE_ = datasets.map( __UpperCAmelCase , batched=__UpperCAmelCase , remove_columns=['sentence1', 'sentence2', 'label'] , ) def collate_fn(__UpperCAmelCase : Dict ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(__UpperCAmelCase , padding='max_length' , max_length=1_28 , return_tensors='pt' ) return tokenizer.pad(__UpperCAmelCase , padding='longest' , return_tensors='pt' ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE_ = DataLoader(tokenized_datasets['train'] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=2 ) SCREAMING_SNAKE_CASE_ = DataLoader(tokenized_datasets['validation'] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=1 ) return train_dataloader, eval_dataloader
31
1
def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : int ) -> str: SCREAMING_SNAKE_CASE_ = [[] for _ in range(__UpperCAmelCase )] SCREAMING_SNAKE_CASE_ = key - 1 if key <= 0: raise ValueError('Height of grid can\'t be 0 or negative' ) if key == 1 or len(__UpperCAmelCase ) <= key: return input_string for position, character in enumerate(__UpperCAmelCase ): SCREAMING_SNAKE_CASE_ = position % (lowest * 2) # puts it in bounds SCREAMING_SNAKE_CASE_ = min(__UpperCAmelCase , lowest * 2 - num ) # creates zigzag pattern temp_grid[num].append(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = [''.join(__UpperCAmelCase ) for row in temp_grid] SCREAMING_SNAKE_CASE_ = ''.join(__UpperCAmelCase ) return output_string def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : int ) -> str: SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = key - 1 if key <= 0: raise ValueError('Height of grid can\'t be 0 or negative' ) if key == 1: return input_string SCREAMING_SNAKE_CASE_ = [[] for _ in range(__UpperCAmelCase )] # generates template for position in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = position % (lowest * 2) # puts it in bounds SCREAMING_SNAKE_CASE_ = min(__UpperCAmelCase , lowest * 2 - num ) # creates zigzag pattern temp_grid[num].append('*' ) SCREAMING_SNAKE_CASE_ = 0 for row in temp_grid: # fills in the characters SCREAMING_SNAKE_CASE_ = input_string[counter : counter + len(__UpperCAmelCase )] grid.append(list(__UpperCAmelCase ) ) counter += len(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = '' # reads as zigzag for position in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = position % (lowest * 2) # puts it in bounds SCREAMING_SNAKE_CASE_ = min(__UpperCAmelCase , lowest * 2 - num ) # creates zigzag pattern output_string += grid[num][0] grid[num].pop(0 ) return output_string def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> dict[int, str]: SCREAMING_SNAKE_CASE_ = {} for key_guess in range(1 , len(__UpperCAmelCase ) ): # tries every key SCREAMING_SNAKE_CASE_ = decrypt(__UpperCAmelCase , __UpperCAmelCase ) return results if __name__ == "__main__": import doctest doctest.testmod()
31
import warnings from ...utils import logging from .image_processing_layoutlmva import LayoutLMvaImageProcessor lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Dict , *_lowerCAmelCase : Optional[Any] , **_lowerCAmelCase : Any ): warnings.warn( 'The class LayoutLMv2FeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use LayoutLMv2ImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
1
import os import tempfile import unittest from pathlib import Path from transformers import AutoConfig, is_tf_available from transformers.testing_utils import require_tf if is_tf_available(): import tensorflow as tf from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments @require_tf class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Union[str, Any] ): for model_result in results.values(): for batch_size, sequence_length in zip(model_result['bs'] , model_result['ss'] ): SCREAMING_SNAKE_CASE_ = model_result['result'][batch_size][sequence_length] self.assertIsNotNone(_lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = 'sshleifer/tiny-gpt2' SCREAMING_SNAKE_CASE_ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_lowerCAmelCase , inference=_lowerCAmelCase , sequence_lengths=[8] , batch_sizes=[1] , eager_mode=_lowerCAmelCase , multi_process=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = TensorFlowBenchmark(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = 'sgugger/tiny-distilbert-classification' SCREAMING_SNAKE_CASE_ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_lowerCAmelCase , inference=_lowerCAmelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_lowerCAmelCase , only_pretrain_model=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = TensorFlowBenchmark(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = 'sshleifer/tiny-gpt2' SCREAMING_SNAKE_CASE_ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_lowerCAmelCase , inference=_lowerCAmelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = TensorFlowBenchmark(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = 'sshleifer/tiny-gpt2' SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_lowerCAmelCase , inference=_lowerCAmelCase , sequence_lengths=[8] , batch_sizes=[1] , eager_mode=_lowerCAmelCase , multi_process=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = TensorFlowBenchmark(_lowerCAmelCase , [config] ) SCREAMING_SNAKE_CASE_ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = 'sshleifer/tiny-gpt2' SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_lowerCAmelCase , inference=_lowerCAmelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = TensorFlowBenchmark(_lowerCAmelCase , [config] ) SCREAMING_SNAKE_CASE_ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = 'sshleifer/tiny-gpt2' SCREAMING_SNAKE_CASE_ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_lowerCAmelCase , inference=_lowerCAmelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = TensorFlowBenchmark(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = 'sshleifer/tiny-gpt2' SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_lowerCAmelCase , inference=_lowerCAmelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = TensorFlowBenchmark(_lowerCAmelCase , [config] ) SCREAMING_SNAKE_CASE_ = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = 'patrickvonplaten/t5-tiny-random' SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_lowerCAmelCase , inference=_lowerCAmelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = TensorFlowBenchmark(_lowerCAmelCase , configs=[config] ) SCREAMING_SNAKE_CASE_ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) @unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices('GPU' ) ) == 0 , 'Cannot do xla on CPU.' ) def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = 'sshleifer/tiny-gpt2' SCREAMING_SNAKE_CASE_ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_lowerCAmelCase , inference=_lowerCAmelCase , sequence_lengths=[8] , batch_sizes=[1] , use_xla=_lowerCAmelCase , multi_process=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = TensorFlowBenchmark(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = 'sshleifer/tiny-gpt2' with tempfile.TemporaryDirectory() as tmp_dir: SCREAMING_SNAKE_CASE_ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , inference=_lowerCAmelCase , save_to_csv=_lowerCAmelCase , sequence_lengths=[8] , batch_sizes=[1] , inference_time_csv_file=os.path.join(_lowerCAmelCase , 'inf_time.csv' ) , inference_memory_csv_file=os.path.join(_lowerCAmelCase , 'inf_mem.csv' ) , env_info_csv_file=os.path.join(_lowerCAmelCase , 'env.csv' ) , multi_process=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = TensorFlowBenchmark(_lowerCAmelCase ) benchmark.run() self.assertTrue(Path(os.path.join(_lowerCAmelCase , 'inf_time.csv' ) ).exists() ) self.assertTrue(Path(os.path.join(_lowerCAmelCase , 'inf_mem.csv' ) ).exists() ) self.assertTrue(Path(os.path.join(_lowerCAmelCase , 'env.csv' ) ).exists() ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = 'sshleifer/tiny-gpt2' def _check_summary_is_not_empty(_lowerCAmelCase : Dict ): self.assertTrue(hasattr(_lowerCAmelCase , 'sequential' ) ) self.assertTrue(hasattr(_lowerCAmelCase , 'cumulative' ) ) self.assertTrue(hasattr(_lowerCAmelCase , 'current' ) ) self.assertTrue(hasattr(_lowerCAmelCase , 'total' ) ) with tempfile.TemporaryDirectory() as tmp_dir: SCREAMING_SNAKE_CASE_ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , inference=_lowerCAmelCase , sequence_lengths=[8] , batch_sizes=[1] , log_filename=os.path.join(_lowerCAmelCase , 'log.txt' ) , log_print=_lowerCAmelCase , trace_memory_line_by_line=_lowerCAmelCase , eager_mode=_lowerCAmelCase , multi_process=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = TensorFlowBenchmark(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = benchmark.run() _check_summary_is_not_empty(result.inference_summary ) self.assertTrue(Path(os.path.join(_lowerCAmelCase , 'log.txt' ) ).exists() )
31
def UpperCAmelCase_ ( ) -> list[list[int]]: return [list(range(10_00 - i , -10_00 - i , -1 ) ) for i in range(10_00 )] lowerCamelCase__ : List[Any] = generate_large_matrix() lowerCamelCase__ : List[Any] = ( [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]], [[3, 2], [1, 0]], [[7, 7, 6]], [[7, 7, 6], [-1, -2, -3]], grid, ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> None: assert all(row == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for row in grid ) assert all(list(__UpperCAmelCase ) == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for col in zip(*__UpperCAmelCase ) ) def UpperCAmelCase_ ( __UpperCAmelCase : list[int] ) -> int: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = len(__UpperCAmelCase ) - 1 # Edge cases such as no values or all numbers are negative. if not array or array[0] < 0: return 0 while right + 1 > left: SCREAMING_SNAKE_CASE_ = (left + right) // 2 SCREAMING_SNAKE_CASE_ = array[mid] # Num must be negative and the index must be greater than or equal to 0. if num < 0 and array[mid - 1] >= 0: return mid if num >= 0: SCREAMING_SNAKE_CASE_ = mid + 1 else: SCREAMING_SNAKE_CASE_ = mid - 1 # No negative numbers so return the last index of the array + 1 which is the length. return len(__UpperCAmelCase ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = len(grid[0] ) for i in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = find_negative_index(grid[i][:bound] ) total += bound return (len(__UpperCAmelCase ) * len(grid[0] )) - total def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: return len([number for row in grid for number in row if number < 0] ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: SCREAMING_SNAKE_CASE_ = 0 for row in grid: for i, number in enumerate(__UpperCAmelCase ): if number < 0: total += len(__UpperCAmelCase ) - i break return total def UpperCAmelCase_ ( ) -> None: from timeit import timeit print('Running benchmarks' ) SCREAMING_SNAKE_CASE_ = ( 'from __main__ import count_negatives_binary_search, ' 'count_negatives_brute_force, count_negatives_brute_force_with_break, grid' ) for func in ( "count_negatives_binary_search", # took 0.7727 seconds "count_negatives_brute_force_with_break", # took 4.6505 seconds "count_negatives_brute_force", # took 12.8160 seconds ): SCREAMING_SNAKE_CASE_ = timeit(f"{func}(grid=grid)" , setup=__UpperCAmelCase , number=5_00 ) print(f"{func}() took {time:0.4f} seconds" ) if __name__ == "__main__": import doctest doctest.testmod() benchmark()
31
1
import os import time import numpy as np import onnxruntime as ort lowerCamelCase__ : int = '1' lowerCamelCase__ : Optional[int] = '0' lowerCamelCase__ : Optional[Any] = '1' lowerCamelCase__ : int = ort.SessionOptions() lowerCamelCase__ : List[Any] = ort.GraphOptimizationLevel.ORT_DISABLE_ALL print('Create inference session...') lowerCamelCase__ : List[str] = ['TensorrtExecutionProvider', 'CUDAExecutionProvider'] lowerCamelCase__ : List[str] = ort.InferenceSession('model.onnx', sess_options=sess_opt, providers=execution_provider) lowerCamelCase__ : Union[str, Any] = ort.RunOptions() lowerCamelCase__ : int = 128 lowerCamelCase__ : Dict = 1 lowerCamelCase__ : Tuple = np.ones((batch, sequence), dtype=np.intaa) lowerCamelCase__ : Union[str, Any] = np.ones((batch, sequence), dtype=np.intaa) lowerCamelCase__ : Any = np.ones((batch, sequence), dtype=np.intaa) print('Warm up phase...') sess.run( None, { sess.get_inputs()[0].name: input_ids, sess.get_inputs()[1].name: attention_mask, sess.get_inputs()[2].name: token_type_ids, }, run_options=run_opt, ) print('Start inference...') lowerCamelCase__ : str = time.time() lowerCamelCase__ : int = 2_000 lowerCamelCase__ : Any = {} for iter in range(max_iters): lowerCamelCase__ : str = sess.run( None, { sess.get_inputs()[0].name: input_ids, sess.get_inputs()[1].name: attention_mask, sess.get_inputs()[2].name: token_type_ids, }, run_options=run_opt, ) print('Average Inference Time = {:.3f} ms'.format((time.time() - start_time) * 1_000 / max_iters))
31
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : Optional[int] = {'configuration_mmbt': ['MMBTConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Any = ['MMBTForClassification', 'MMBTModel', 'ModalEmbeddings'] if TYPE_CHECKING: from .configuration_mmbt import MMBTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mmbt import MMBTForClassification, MMBTModel, ModalEmbeddings else: import sys lowerCamelCase__ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
31
1
from collections import OrderedDict from typing import Any, List, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import logging lowerCamelCase__ : Optional[Any] = logging.get_logger(__name__) lowerCamelCase__ : Optional[int] = { 'EleutherAI/gpt-j-6B': 'https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json', # See all GPT-J models at https://huggingface.co/models?filter=gpt_j } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "gptj" lowercase_ = { "max_position_embeddings": "n_positions", "hidden_size": "n_embd", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : str , _lowerCAmelCase : str=50_400 , _lowerCAmelCase : int=2_048 , _lowerCAmelCase : Union[str, Any]=4_096 , _lowerCAmelCase : Tuple=28 , _lowerCAmelCase : List[str]=16 , _lowerCAmelCase : Dict=64 , _lowerCAmelCase : Tuple=None , _lowerCAmelCase : str="gelu_new" , _lowerCAmelCase : List[str]=0.0 , _lowerCAmelCase : Any=0.0 , _lowerCAmelCase : Optional[Any]=0.0 , _lowerCAmelCase : str=1E-5 , _lowerCAmelCase : Union[str, Any]=0.02 , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Any=50_256 , _lowerCAmelCase : Optional[int]=50_256 , _lowerCAmelCase : Tuple=False , **_lowerCAmelCase : Tuple , ): SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = n_positions SCREAMING_SNAKE_CASE_ = n_embd SCREAMING_SNAKE_CASE_ = n_layer SCREAMING_SNAKE_CASE_ = n_head SCREAMING_SNAKE_CASE_ = n_inner SCREAMING_SNAKE_CASE_ = rotary_dim SCREAMING_SNAKE_CASE_ = activation_function SCREAMING_SNAKE_CASE_ = resid_pdrop SCREAMING_SNAKE_CASE_ = embd_pdrop SCREAMING_SNAKE_CASE_ = attn_pdrop SCREAMING_SNAKE_CASE_ = layer_norm_epsilon SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = use_cache SCREAMING_SNAKE_CASE_ = bos_token_id SCREAMING_SNAKE_CASE_ = eos_token_id super().__init__( bos_token_id=_lowerCAmelCase , eos_token_id=_lowerCAmelCase , tie_word_embeddings=_lowerCAmelCase , **_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Union[str, Any] , _lowerCAmelCase : PretrainedConfig , _lowerCAmelCase : str = "default" , _lowerCAmelCase : List[PatchingSpec] = None , _lowerCAmelCase : bool = False , ): super().__init__(_lowerCAmelCase , task=_lowerCAmelCase , patching_specs=_lowerCAmelCase , use_past=_lowerCAmelCase ) if not getattr(self._config , 'pad_token_id' , _lowerCAmelCase ): # TODO: how to do that better? SCREAMING_SNAKE_CASE_ = 0 @property def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}} ) if self.use_past: self.fill_with_past_key_values_(_lowerCAmelCase , direction='inputs' ) SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'past_sequence + sequence'} else: SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'sequence'} return common_inputs @property def lowerCAmelCase_ ( self : Dict ): return self._config.n_layer @property def lowerCAmelCase_ ( self : Optional[Any] ): return self._config.n_head def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : PreTrainedTokenizer , _lowerCAmelCase : int = -1 , _lowerCAmelCase : int = -1 , _lowerCAmelCase : bool = False , _lowerCAmelCase : Optional[TensorType] = None , ): SCREAMING_SNAKE_CASE_ = super(_lowerCAmelCase , self ).generate_dummy_inputs( _lowerCAmelCase , batch_size=_lowerCAmelCase , seq_length=_lowerCAmelCase , is_pair=_lowerCAmelCase , framework=_lowerCAmelCase ) # We need to order the input in the way they appears in the forward() SCREAMING_SNAKE_CASE_ = 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 SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = common_inputs['input_ids'].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE_ = seqlen + 2 SCREAMING_SNAKE_CASE_ = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) SCREAMING_SNAKE_CASE_ = [ (torch.zeros(_lowerCAmelCase ), torch.zeros(_lowerCAmelCase )) for _ in range(self.num_layers ) ] SCREAMING_SNAKE_CASE_ = common_inputs['attention_mask'] if self.use_past: SCREAMING_SNAKE_CASE_ = ordered_inputs['attention_mask'].dtype SCREAMING_SNAKE_CASE_ = torch.cat( [ordered_inputs['attention_mask'], torch.ones(_lowerCAmelCase , _lowerCAmelCase , dtype=_lowerCAmelCase )] , dim=1 ) return ordered_inputs @property def lowerCAmelCase_ ( self : int ): return 13
31
import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class lowerCamelCase_ : '''simple docstring''' @property def lowerCAmelCase_ ( self : Optional[Any] ): return self.get_dummy_input() @property def lowerCAmelCase_ ( self : Union[str, Any] ): if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(F"'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'." ) def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : str=False , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : Dict=False , ): SCREAMING_SNAKE_CASE_ = 4 SCREAMING_SNAKE_CASE_ = 32 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = (batch_size, num_channels) + sizes SCREAMING_SNAKE_CASE_ = randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = {'hidden_states': hidden_states} if include_temb: SCREAMING_SNAKE_CASE_ = 128 SCREAMING_SNAKE_CASE_ = randn_tensor((batch_size, temb_channels) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) if include_res_hidden_states_tuple: SCREAMING_SNAKE_CASE_ = torch.manual_seed(1 ) SCREAMING_SNAKE_CASE_ = (randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ),) if include_encoder_hidden_states: SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, 32, 32) ).to(_lowerCAmelCase ) if include_skip_sample: SCREAMING_SNAKE_CASE_ = randn_tensor(((batch_size, 3) + sizes) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) return dummy_input def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = { 'in_channels': 32, 'out_channels': 32, 'temb_channels': 128, } if self.block_type == "up": SCREAMING_SNAKE_CASE_ = 32 if self.block_type == "mid": init_dict.pop('out_channels' ) SCREAMING_SNAKE_CASE_ = self.dummy_input return init_dict, inputs_dict def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) unet_block.to(_lowerCAmelCase ) unet_block.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_ = unet_block(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] self.assertEqual(output.shape , self.output_shape ) SCREAMING_SNAKE_CASE_ = output[0, -1, -3:, -3:] SCREAMING_SNAKE_CASE_ = torch.tensor(_lowerCAmelCase ).to(_lowerCAmelCase ) assert torch_all_close(output_slice.flatten() , _lowerCAmelCase , atol=5E-3 ) @unittest.skipIf(torch_device == 'mps' , 'Training is not supported in mps' ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.train() SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = randn_tensor(output.shape , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.functional.mse_loss(_lowerCAmelCase , _lowerCAmelCase ) loss.backward()
31
1
from collections.abc import Iterator, MutableMapping from dataclasses import dataclass from typing import Generic, TypeVar lowerCamelCase__ : Any = TypeVar('KEY') lowerCamelCase__ : str = TypeVar('VAL') @dataclass(frozen=_SCREAMING_SNAKE_CASE , slots=_SCREAMING_SNAKE_CASE ) class lowerCamelCase_ ( Generic[KEY, VAL] ): '''simple docstring''' lowercase_ = 42 lowercase_ = 42 class lowerCamelCase_ ( _Item ): '''simple docstring''' def __init__( self : Optional[int] ): super().__init__(_lowerCAmelCase , _lowerCAmelCase ) def __bool__( self : Tuple ): return False lowerCamelCase__ : str = _DeletedItem() class lowerCamelCase_ ( MutableMapping[KEY, VAL] ): '''simple docstring''' def __init__( self : Dict , _lowerCAmelCase : int = 8 , _lowerCAmelCase : float = 0.75 ): SCREAMING_SNAKE_CASE_ = initial_block_size SCREAMING_SNAKE_CASE_ = [None] * initial_block_size assert 0.0 < capacity_factor < 1.0 SCREAMING_SNAKE_CASE_ = capacity_factor SCREAMING_SNAKE_CASE_ = 0 def lowerCAmelCase_ ( self : int , _lowerCAmelCase : KEY ): return hash(_lowerCAmelCase ) % len(self._buckets ) def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : int ): return (ind + 1) % len(self._buckets ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : int , _lowerCAmelCase : KEY , _lowerCAmelCase : VAL ): SCREAMING_SNAKE_CASE_ = self._buckets[ind] if not stored: SCREAMING_SNAKE_CASE_ = _Item(_lowerCAmelCase , _lowerCAmelCase ) self._len += 1 return True elif stored.key == key: SCREAMING_SNAKE_CASE_ = _Item(_lowerCAmelCase , _lowerCAmelCase ) return True else: return False def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = len(self._buckets ) * self._capacity_factor return len(self ) >= int(_lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple ): if len(self._buckets ) <= self._initial_block_size: return False SCREAMING_SNAKE_CASE_ = len(self._buckets ) * self._capacity_factor / 2 return len(self ) < limit def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = self._buckets SCREAMING_SNAKE_CASE_ = [None] * new_size SCREAMING_SNAKE_CASE_ = 0 for item in old_buckets: if item: self._add_item(item.key , item.val ) def lowerCAmelCase_ ( self : List[Any] ): self._resize(len(self._buckets ) * 2 ) def lowerCAmelCase_ ( self : int ): self._resize(len(self._buckets ) // 2 ) def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : KEY ): SCREAMING_SNAKE_CASE_ = self._get_bucket_index(_lowerCAmelCase ) for _ in range(len(self._buckets ) ): yield ind SCREAMING_SNAKE_CASE_ = self._get_next_ind(_lowerCAmelCase ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : KEY , _lowerCAmelCase : VAL ): for ind in self._iterate_buckets(_lowerCAmelCase ): if self._try_set(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ): break def __setitem__( self : Optional[Any] , _lowerCAmelCase : KEY , _lowerCAmelCase : VAL ): if self._is_full(): self._size_up() self._add_item(_lowerCAmelCase , _lowerCAmelCase ) def __delitem__( self : Any , _lowerCAmelCase : KEY ): for ind in self._iterate_buckets(_lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = self._buckets[ind] if item is None: raise KeyError(_lowerCAmelCase ) if item is _deleted: continue if item.key == key: SCREAMING_SNAKE_CASE_ = _deleted self._len -= 1 break if self._is_sparse(): self._size_down() def __getitem__( self : Dict , _lowerCAmelCase : KEY ): for ind in self._iterate_buckets(_lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = self._buckets[ind] if item is None: break if item is _deleted: continue if item.key == key: return item.val raise KeyError(_lowerCAmelCase ) def __len__( self : Dict ): return self._len def __iter__( self : Tuple ): yield from (item.key for item in self._buckets if item) def __repr__( self : Dict ): SCREAMING_SNAKE_CASE_ = ' ,'.join( F"{item.key}: {item.val}" for item in self._buckets if item ) return F"HashMap({val_string})"
31
import operator as op def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> Any: SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = lambda __UpperCAmelCase , __UpperCAmelCase : int(x / y ) # noqa: E731 integer division operation SCREAMING_SNAKE_CASE_ = { '^': op.pow, '*': op.mul, '/': div, '+': op.add, '-': op.sub, } # operators & their respective operation # print table header print('Symbol'.center(8 ) , 'Action'.center(12 ) , 'Stack' , sep=' | ' ) print('-' * (30 + len(__UpperCAmelCase )) ) for x in post_fix: if x.isdigit(): # if x in digit stack.append(__UpperCAmelCase ) # append x to stack # output in tabular format print(x.rjust(8 ) , ('push(' + x + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) else: SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print(''.rjust(8 ) , ('pop(' + b + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print(''.rjust(8 ) , ('pop(' + a + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) stack.append( str(opr[x](int(__UpperCAmelCase ) , int(__UpperCAmelCase ) ) ) ) # evaluate the 2 values popped from stack & push result to stack # output in tabular format print( x.rjust(8 ) , ('push(' + a + x + b + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' , ) return int(stack[0] ) if __name__ == "__main__": lowerCamelCase__ : Tuple = input('\n\nEnter a Postfix Equation (space separated) = ').split(' ') print('\n\tResult = ', solve(Postfix))
31
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : List[str] = { 'shi-labs/nat-mini-in1k-224': 'https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json', # See all Nat models at https://huggingface.co/models?filter=nat } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "nat" lowercase_ = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : int , _lowerCAmelCase : Optional[Any]=4 , _lowerCAmelCase : str=3 , _lowerCAmelCase : Any=64 , _lowerCAmelCase : Union[str, Any]=[3, 4, 6, 5] , _lowerCAmelCase : List[str]=[2, 4, 8, 16] , _lowerCAmelCase : List[Any]=7 , _lowerCAmelCase : Any=3.0 , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : Dict=0.0 , _lowerCAmelCase : str=0.0 , _lowerCAmelCase : int=0.1 , _lowerCAmelCase : List[str]="gelu" , _lowerCAmelCase : str=0.02 , _lowerCAmelCase : Optional[int]=1E-5 , _lowerCAmelCase : Tuple=0.0 , _lowerCAmelCase : Tuple=None , _lowerCAmelCase : Optional[Any]=None , **_lowerCAmelCase : Optional[int] , ): super().__init__(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = embed_dim SCREAMING_SNAKE_CASE_ = depths SCREAMING_SNAKE_CASE_ = len(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = num_heads SCREAMING_SNAKE_CASE_ = kernel_size SCREAMING_SNAKE_CASE_ = mlp_ratio SCREAMING_SNAKE_CASE_ = qkv_bias SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = drop_path_rate SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = initializer_range # we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE_ = int(embed_dim * 2 ** (len(_lowerCAmelCase ) - 1) ) SCREAMING_SNAKE_CASE_ = layer_scale_init_value SCREAMING_SNAKE_CASE_ = ['stem'] + [F"stage{idx}" for idx in range(1 , len(_lowerCAmelCase ) + 1 )] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = get_aligned_output_features_output_indices( out_features=_lowerCAmelCase , out_indices=_lowerCAmelCase , stage_names=self.stage_names )
31
def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: assert isinstance(__UpperCAmelCase , __UpperCAmelCase ), f"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE_ = f"The input value of [n={number}] has to be > 0" raise ValueError(__UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_ = sylvester(number - 1 ) SCREAMING_SNAKE_CASE_ = num - 1 SCREAMING_SNAKE_CASE_ = num return lower * upper + 1 if __name__ == "__main__": print(f'''The 8th number in Sylvester\'s sequence: {sylvester(8)}''')
31
1
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch lowerCamelCase__ : Dict = random.Random() def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : Tuple=1.0 , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Dict=None ) -> Tuple: if rng is None: SCREAMING_SNAKE_CASE_ = global_rng SCREAMING_SNAKE_CASE_ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any]=7 , _lowerCAmelCase : Union[str, Any]=400 , _lowerCAmelCase : Tuple=2_000 , _lowerCAmelCase : str=1 , _lowerCAmelCase : int=0.0 , _lowerCAmelCase : Optional[Any]=16_000 , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Any=80 , _lowerCAmelCase : Union[str, Any]=16 , _lowerCAmelCase : List[str]=64 , _lowerCAmelCase : List[Any]="hann_window" , _lowerCAmelCase : Any=80 , _lowerCAmelCase : List[Any]=7_600 , _lowerCAmelCase : List[Any]=1E-10 , _lowerCAmelCase : Optional[Any]=True , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = min_seq_length SCREAMING_SNAKE_CASE_ = max_seq_length SCREAMING_SNAKE_CASE_ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE_ = feature_size SCREAMING_SNAKE_CASE_ = padding_value SCREAMING_SNAKE_CASE_ = sampling_rate SCREAMING_SNAKE_CASE_ = do_normalize SCREAMING_SNAKE_CASE_ = num_mel_bins SCREAMING_SNAKE_CASE_ = hop_length SCREAMING_SNAKE_CASE_ = win_length SCREAMING_SNAKE_CASE_ = win_function SCREAMING_SNAKE_CASE_ = fmin SCREAMING_SNAKE_CASE_ = fmax SCREAMING_SNAKE_CASE_ = mel_floor SCREAMING_SNAKE_CASE_ = return_attention_mask def lowerCAmelCase_ ( self : Union[str, Any] ): return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : str=False ): def _flatten(_lowerCAmelCase : Dict ): return list(itertools.chain(*_lowerCAmelCase ) ) if equal_length: SCREAMING_SNAKE_CASE_ = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Union[str, Any]=False , _lowerCAmelCase : Optional[int]=False ): if equal_length: SCREAMING_SNAKE_CASE_ = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = SpeechTaFeatureExtractor def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractionTester(self ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : int ): self.assertTrue(np.all(np.mean(_lowerCAmelCase , axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_lowerCAmelCase , axis=0 ) - 1 ) < 1E-3 ) ) def lowerCAmelCase_ ( self : List[Any] ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE_ = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , padding=_lowerCAmelCase , max_length=_lowerCAmelCase , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self.assertTrue(input_values[0][800:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self.assertTrue(input_values[0][1_000:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = range(800 , 1_400 , 200 ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in lengths] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , max_length=_lowerCAmelCase , padding=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='max_length' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1_000) ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=2_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1_200) ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = np.random.rand(100 ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def lowerCAmelCase_ ( self : Tuple ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test feature size SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , padding=_lowerCAmelCase , return_tensors='np' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input SCREAMING_SNAKE_CASE_ = feature_extractor(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE_ = np.asarray(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_lowerCAmelCase ) == len(_lowerCAmelCase ) for x, y in zip(_lowerCAmelCase , processed_features[input_name] ) ) ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='np' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='pt' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' )[input_name] SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='pt' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = min(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad( _lowerCAmelCase , padding='max_length' , max_length=_lowerCAmelCase , truncation=_lowerCAmelCase , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Tuple ): from datasets import load_dataset SCREAMING_SNAKE_CASE_ = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE_ = ds.sort('id' ).select(range(_lowerCAmelCase ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] def lowerCAmelCase_ ( self : Any ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [2.3_804E-03, 2.0_752E-03, 1.9_836E-03, 2.1_057E-03, 1.6_174E-03, 3.0_518E-04, 9.1_553E-05, 3.3_569E-04, 9.7_656E-04, 1.8_311E-03, 2.0_142E-03, 2.1_057E-03, 1.7_395E-03, 4.5_776E-04, -3.9_673E-04, 4.5_776E-04, 1.0_071E-03, 9.1_553E-05, 4.8_828E-04, 1.1_597E-03, 7.3_242E-04, 9.4_604E-04, 1.8_005E-03, 1.8_311E-03, 8.8_501E-04, 4.2_725E-04, 4.8_828E-04, 7.3_242E-04, 1.0_986E-03, 2.1_057E-03] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 93_680) ) self.assertTrue(torch.allclose(input_values[0, :30] , _lowerCAmelCase , atol=1E-6 ) ) def lowerCAmelCase_ ( self : Optional[int] ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [-2.6870, -3.0104, -3.1356, -3.5352, -3.0044, -3.0353, -3.4719, -3.6777, -3.1520, -2.9435, -2.6553, -2.8795, -2.9944, -2.5921, -3.0279, -3.0386, -3.0864, -3.1291, -3.2353, -2.7444, -2.6831, -2.7287, -3.1761, -3.1571, -3.2726, -3.0582, -3.1007, -3.4533, -3.4695, -3.0998] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 366, 80) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] , _lowerCAmelCase , atol=1E-4 ) )
31
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCamelCase__ : List[Any] = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCamelCase__ : Union[str, Any] = TaTokenizerFast lowerCamelCase__ : Dict = {'configuration_mt5': ['MT5Config', 'MT5OnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = [ 'MT5EncoderModel', 'MT5ForConditionalGeneration', 'MT5ForQuestionAnswering', 'MT5Model', 'MT5PreTrainedModel', 'MT5Stack', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = ['TFMT5EncoderModel', 'TFMT5ForConditionalGeneration', 'TFMT5Model'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = ['FlaxMT5EncoderModel', 'FlaxMT5ForConditionalGeneration', 'FlaxMT5Model'] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCamelCase__ : int = _LazyModule( __name__, globals()['__file__'], _import_structure, extra_objects={'MT5Tokenizer': MTaTokenizer, 'MT5TokenizerFast': MTaTokenizerFast}, module_spec=__spec__, )
31
1
def UpperCAmelCase_ ( __UpperCAmelCase : List[Any] ) -> Tuple: SCREAMING_SNAKE_CASE_ = len(__UpperCAmelCase ) for i in range(length - 1 ): SCREAMING_SNAKE_CASE_ = i for k in range(i + 1 , __UpperCAmelCase ): if collection[k] < collection[least]: SCREAMING_SNAKE_CASE_ = k if least != i: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = (collection[i], collection[least]) return collection if __name__ == "__main__": lowerCamelCase__ : str = input('Enter numbers separated by a comma:\n').strip() lowerCamelCase__ : List[Any] = [int(item) for item in user_input.split(',')] print(selection_sort(unsorted))
31
import subprocess import sys from transformers import BertConfig, BertModel, BertTokenizer, pipeline from transformers.testing_utils import TestCasePlus, require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' @require_torch def lowerCAmelCase_ ( self : int ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Tuple ): # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : List[str] ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # next emulate no network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this. # env["TRANSFORMERS_OFFLINE"] = "0" # result = subprocess.run(cmd, env=env, check=False, capture_output=True) # self.assertEqual(result.returncode, 1, result.stderr) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n ' SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 1 , result.stderr ) self.assertIn( 'You cannot infer task automatically within `pipeline` when using offline mode' , result.stderr.decode().replace('\n' , '' ) , ) @require_torch def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import AutoModel\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() )
31
1
import subprocess import sys from transformers import BertConfig, BertModel, BertTokenizer, pipeline from transformers.testing_utils import TestCasePlus, require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' @require_torch def lowerCAmelCase_ ( self : int ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Tuple ): # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : List[str] ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # next emulate no network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this. # env["TRANSFORMERS_OFFLINE"] = "0" # result = subprocess.run(cmd, env=env, check=False, capture_output=True) # self.assertEqual(result.returncode, 1, result.stderr) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n ' SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 1 , result.stderr ) self.assertIn( 'You cannot infer task automatically within `pipeline` when using offline mode' , result.stderr.decode().replace('\n' , '' ) , ) @require_torch def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import AutoModel\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() )
31
import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "M-CLIP" def __init__( self : Tuple , _lowerCAmelCase : List[str]=1_024 , _lowerCAmelCase : str=768 , **_lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = transformerDimSize SCREAMING_SNAKE_CASE_ = imageDimSize super().__init__(**_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = MCLIPConfig def __init__( self : Dict , _lowerCAmelCase : Union[str, Any] , *_lowerCAmelCase : str , **_lowerCAmelCase : str ): super().__init__(_lowerCAmelCase , *_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = XLMRobertaModel(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.Linear( in_features=config.transformerDimensions , out_features=config.numDims ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.transformer(input_ids=_lowerCAmelCase , attention_mask=_lowerCAmelCase )[0] SCREAMING_SNAKE_CASE_ = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(_lowerCAmelCase ), embs
31
1
def UpperCAmelCase_ ( __UpperCAmelCase : list , __UpperCAmelCase : list ) -> float: _validate_point(__UpperCAmelCase ) _validate_point(__UpperCAmelCase ) if len(__UpperCAmelCase ) != len(__UpperCAmelCase ): raise ValueError('Both points must be in the same n-dimensional space' ) return float(sum(abs(a - b ) for a, b in zip(__UpperCAmelCase , __UpperCAmelCase ) ) ) def UpperCAmelCase_ ( __UpperCAmelCase : list[float] ) -> None: if point: if isinstance(__UpperCAmelCase , __UpperCAmelCase ): for item in point: if not isinstance(__UpperCAmelCase , (int, float) ): SCREAMING_SNAKE_CASE_ = ( 'Expected a list of numbers as input, found ' f"{type(__UpperCAmelCase ).__name__}" ) raise TypeError(__UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_ = f"Expected a list of numbers as input, found {type(__UpperCAmelCase ).__name__}" raise TypeError(__UpperCAmelCase ) else: raise ValueError('Missing an input' ) def UpperCAmelCase_ ( __UpperCAmelCase : list , __UpperCAmelCase : list ) -> float: _validate_point(__UpperCAmelCase ) _validate_point(__UpperCAmelCase ) if len(__UpperCAmelCase ) != len(__UpperCAmelCase ): raise ValueError('Both points must be in the same n-dimensional space' ) return float(sum(abs(x - y ) for x, y in zip(__UpperCAmelCase , __UpperCAmelCase ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
31
import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) 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 enable_full_determinism() class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = 3 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(_lowerCAmelCase ) return image @property def lowerCAmelCase_ ( self : Union[str, Any] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) return model @property def lowerCAmelCase_ ( self : Tuple ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , ) return model @property def lowerCAmelCase_ ( self : Optional[int] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5_006 , ) return RobertaSeriesModelWithTransformation(_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : List[Any] ): def extract(*_lowerCAmelCase : Optional[int] , **_lowerCAmelCase : str ): class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = torch.ones([0] ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int ): self.pixel_values.to(_lowerCAmelCase ) return self return Out() return extract def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = 'cpu' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = output.images SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , return_dict=_lowerCAmelCase , )[0] SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE_ = np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5E-3 @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) # put models in fp16 SCREAMING_SNAKE_CASE_ = unet.half() SCREAMING_SNAKE_CASE_ = vae.half() SCREAMING_SNAKE_CASE_ = bert.half() # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) # resize to resolution that is divisible by 8 but not 16 or 32 SCREAMING_SNAKE_CASE_ = init_image.resize((760, 504) ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] SCREAMING_SNAKE_CASE_ = image[255:258, 383:386, -1] assert image.shape == (504, 760, 3) SCREAMING_SNAKE_CASE_ = np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) SCREAMING_SNAKE_CASE_ = init_image.resize((768, 512) ) SCREAMING_SNAKE_CASE_ = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy' ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] assert image.shape == (512, 768, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1E-2
31
1
from __future__ import annotations from math import ceil, floor, sqrt def UpperCAmelCase_ ( __UpperCAmelCase : int = 2_00_00_00 ) -> int: SCREAMING_SNAKE_CASE_ = [0] SCREAMING_SNAKE_CASE_ = 42 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 SCREAMING_SNAKE_CASE_ = 0 # the area corresponding to the grid that gives the product closest to target SCREAMING_SNAKE_CASE_ = 0 # an estimate of b, using the quadratic formula SCREAMING_SNAKE_CASE_ = 42 # the largest integer less than b_estimate SCREAMING_SNAKE_CASE_ = 42 # the largest integer less than b_estimate SCREAMING_SNAKE_CASE_ = 42 # the triangle number corresponding to b_floor SCREAMING_SNAKE_CASE_ = 42 # the triangle number corresponding to b_ceil SCREAMING_SNAKE_CASE_ = 42 for idx_a, triangle_a in enumerate(triangle_numbers[1:] , 1 ): SCREAMING_SNAKE_CASE_ = (-1 + sqrt(1 + 8 * target / triangle_a )) / 2 SCREAMING_SNAKE_CASE_ = floor(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = ceil(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = triangle_numbers[b_floor] SCREAMING_SNAKE_CASE_ = triangle_numbers[b_ceil] if abs(target - triangle_b_first_guess * triangle_a ) < abs( target - best_product ): SCREAMING_SNAKE_CASE_ = triangle_b_first_guess * triangle_a SCREAMING_SNAKE_CASE_ = idx_a * b_floor if abs(target - triangle_b_second_guess * triangle_a ) < abs( target - best_product ): SCREAMING_SNAKE_CASE_ = triangle_b_second_guess * triangle_a SCREAMING_SNAKE_CASE_ = idx_a * b_ceil return area if __name__ == "__main__": print(f'''{solution() = }''')
31
from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import TensorType, logging if TYPE_CHECKING: from ...onnx.config import PatchingSpec from ...tokenization_utils_base import PreTrainedTokenizerBase lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Dict = { 'allenai/longformer-base-4096': 'https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json', 'allenai/longformer-large-4096': 'https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json', 'allenai/longformer-large-4096-finetuned-triviaqa': ( 'https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json' ), 'allenai/longformer-base-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json' ), 'allenai/longformer-large-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json' ), } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "longformer" def __init__( self : Union[str, Any] , _lowerCAmelCase : Union[List[int], int] = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 1 , _lowerCAmelCase : int = 0 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 30_522 , _lowerCAmelCase : int = 768 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 3_072 , _lowerCAmelCase : str = "gelu" , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : int = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : float = 0.02 , _lowerCAmelCase : float = 1E-12 , _lowerCAmelCase : bool = False , **_lowerCAmelCase : Union[str, Any] , ): super().__init__(pad_token_id=_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = attention_window SCREAMING_SNAKE_CASE_ = sep_token_id SCREAMING_SNAKE_CASE_ = bos_token_id SCREAMING_SNAKE_CASE_ = eos_token_id SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = type_vocab_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = onnx_export class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[Any] , _lowerCAmelCase : "PretrainedConfig" , _lowerCAmelCase : str = "default" , _lowerCAmelCase : "List[PatchingSpec]" = None ): super().__init__(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = True @property def lowerCAmelCase_ ( self : Any ): if self.task == "multiple-choice": SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'choice', 2: 'sequence'} else: SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('global_attention_mask', dynamic_axis), ] ) @property def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = super().outputs if self.task == "default": SCREAMING_SNAKE_CASE_ = {0: 'batch'} return outputs @property def lowerCAmelCase_ ( self : str ): return 1E-4 @property def lowerCAmelCase_ ( self : Optional[Any] ): # needs to be >= 14 to support tril operator return max(super().default_onnx_opset , 14 ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : "PreTrainedTokenizerBase" , _lowerCAmelCase : int = -1 , _lowerCAmelCase : int = -1 , _lowerCAmelCase : bool = False , _lowerCAmelCase : Optional[TensorType] = None , ): SCREAMING_SNAKE_CASE_ = super().generate_dummy_inputs( preprocessor=_lowerCAmelCase , batch_size=_lowerCAmelCase , seq_length=_lowerCAmelCase , is_pair=_lowerCAmelCase , framework=_lowerCAmelCase ) import torch # for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64) # makes the export fail randomly SCREAMING_SNAKE_CASE_ = torch.zeros_like(inputs['input_ids'] ) # make every second token global SCREAMING_SNAKE_CASE_ = 1 return inputs
31
1
def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : float , __UpperCAmelCase : float ) -> float: return round(float(moles / volume ) * nfactor ) def UpperCAmelCase_ ( __UpperCAmelCase : float , __UpperCAmelCase : float , __UpperCAmelCase : float ) -> float: return round(float((moles * 0.0_8_2_1 * temperature) / (volume) ) ) def UpperCAmelCase_ ( __UpperCAmelCase : float , __UpperCAmelCase : float , __UpperCAmelCase : float ) -> float: return round(float((moles * 0.0_8_2_1 * temperature) / (pressure) ) ) def UpperCAmelCase_ ( __UpperCAmelCase : float , __UpperCAmelCase : float , __UpperCAmelCase : float ) -> float: return round(float((pressure * volume) / (0.0_8_2_1 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
31
import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : str , *_lowerCAmelCase : Tuple , **_lowerCAmelCase : int ): warnings.warn( 'The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use MobileViTImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
1
from pathlib import Path import fire def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : str , __UpperCAmelCase : int ) -> List[Any]: SCREAMING_SNAKE_CASE_ = Path(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = Path(__UpperCAmelCase ) dest_dir.mkdir(exist_ok=__UpperCAmelCase ) for path in src_dir.iterdir(): SCREAMING_SNAKE_CASE_ = [x.rstrip() for x in list(path.open().readlines() )][:n] SCREAMING_SNAKE_CASE_ = dest_dir.joinpath(path.name ) print(__UpperCAmelCase ) dest_path.open('w' ).write('\n'.join(__UpperCAmelCase ) ) if __name__ == "__main__": fire.Fire(minify)
31
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'microsoft/swinv2-tiny-patch4-window8-256': ( 'https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json' ), } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "swinv2" lowercase_ = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : Dict , _lowerCAmelCase : Optional[Any]=224 , _lowerCAmelCase : Optional[int]=4 , _lowerCAmelCase : Tuple=3 , _lowerCAmelCase : Tuple=96 , _lowerCAmelCase : Dict=[2, 2, 6, 2] , _lowerCAmelCase : Optional[Any]=[3, 6, 12, 24] , _lowerCAmelCase : str=7 , _lowerCAmelCase : List[Any]=4.0 , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : List[Any]="gelu" , _lowerCAmelCase : str=False , _lowerCAmelCase : str=0.02 , _lowerCAmelCase : List[Any]=1E-5 , _lowerCAmelCase : str=32 , **_lowerCAmelCase : List[Any] , ): super().__init__(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = embed_dim SCREAMING_SNAKE_CASE_ = depths SCREAMING_SNAKE_CASE_ = len(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = num_heads SCREAMING_SNAKE_CASE_ = window_size SCREAMING_SNAKE_CASE_ = mlp_ratio SCREAMING_SNAKE_CASE_ = qkv_bias SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = drop_path_rate SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = use_absolute_embeddings SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE_ = int(embed_dim * 2 ** (len(_lowerCAmelCase ) - 1) ) SCREAMING_SNAKE_CASE_ = (0, 0, 0, 0)
31
1
from __future__ import annotations class lowerCamelCase_ : '''simple docstring''' def __init__( self : List[Any] , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = data SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None def UpperCAmelCase_ ( __UpperCAmelCase : Node | None ) -> None: # In Order traversal of the tree if tree: display(tree.left ) print(tree.data ) display(tree.right ) def UpperCAmelCase_ ( __UpperCAmelCase : Node | None ) -> int: return 1 + max(depth_of_tree(tree.left ) , depth_of_tree(tree.right ) ) if tree else 0 def UpperCAmelCase_ ( __UpperCAmelCase : Node ) -> bool: if not tree: return True if tree.left and tree.right: return is_full_binary_tree(tree.left ) and is_full_binary_tree(tree.right ) else: return not tree.left and not tree.right def UpperCAmelCase_ ( ) -> None: # Main function for testing. SCREAMING_SNAKE_CASE_ = Node(1 ) SCREAMING_SNAKE_CASE_ = Node(2 ) SCREAMING_SNAKE_CASE_ = Node(3 ) SCREAMING_SNAKE_CASE_ = Node(4 ) SCREAMING_SNAKE_CASE_ = Node(5 ) SCREAMING_SNAKE_CASE_ = Node(6 ) SCREAMING_SNAKE_CASE_ = Node(7 ) SCREAMING_SNAKE_CASE_ = Node(8 ) SCREAMING_SNAKE_CASE_ = Node(9 ) print(is_full_binary_tree(__UpperCAmelCase ) ) print(depth_of_tree(__UpperCAmelCase ) ) print('Tree is: ' ) display(__UpperCAmelCase ) if __name__ == "__main__": main()
31
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch lowerCamelCase__ : Dict = random.Random() def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : Tuple=1.0 , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Dict=None ) -> Tuple: if rng is None: SCREAMING_SNAKE_CASE_ = global_rng SCREAMING_SNAKE_CASE_ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any]=7 , _lowerCAmelCase : Union[str, Any]=400 , _lowerCAmelCase : Tuple=2_000 , _lowerCAmelCase : str=1 , _lowerCAmelCase : int=0.0 , _lowerCAmelCase : Optional[Any]=16_000 , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Any=80 , _lowerCAmelCase : Union[str, Any]=16 , _lowerCAmelCase : List[str]=64 , _lowerCAmelCase : List[Any]="hann_window" , _lowerCAmelCase : Any=80 , _lowerCAmelCase : List[Any]=7_600 , _lowerCAmelCase : List[Any]=1E-10 , _lowerCAmelCase : Optional[Any]=True , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = min_seq_length SCREAMING_SNAKE_CASE_ = max_seq_length SCREAMING_SNAKE_CASE_ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE_ = feature_size SCREAMING_SNAKE_CASE_ = padding_value SCREAMING_SNAKE_CASE_ = sampling_rate SCREAMING_SNAKE_CASE_ = do_normalize SCREAMING_SNAKE_CASE_ = num_mel_bins SCREAMING_SNAKE_CASE_ = hop_length SCREAMING_SNAKE_CASE_ = win_length SCREAMING_SNAKE_CASE_ = win_function SCREAMING_SNAKE_CASE_ = fmin SCREAMING_SNAKE_CASE_ = fmax SCREAMING_SNAKE_CASE_ = mel_floor SCREAMING_SNAKE_CASE_ = return_attention_mask def lowerCAmelCase_ ( self : Union[str, Any] ): return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : str=False ): def _flatten(_lowerCAmelCase : Dict ): return list(itertools.chain(*_lowerCAmelCase ) ) if equal_length: SCREAMING_SNAKE_CASE_ = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Union[str, Any]=False , _lowerCAmelCase : Optional[int]=False ): if equal_length: SCREAMING_SNAKE_CASE_ = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = SpeechTaFeatureExtractor def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractionTester(self ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : int ): self.assertTrue(np.all(np.mean(_lowerCAmelCase , axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_lowerCAmelCase , axis=0 ) - 1 ) < 1E-3 ) ) def lowerCAmelCase_ ( self : List[Any] ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE_ = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , padding=_lowerCAmelCase , max_length=_lowerCAmelCase , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self.assertTrue(input_values[0][800:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self.assertTrue(input_values[0][1_000:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = range(800 , 1_400 , 200 ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in lengths] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , max_length=_lowerCAmelCase , padding=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='max_length' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1_000) ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=2_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1_200) ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = np.random.rand(100 ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def lowerCAmelCase_ ( self : Tuple ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test feature size SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , padding=_lowerCAmelCase , return_tensors='np' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input SCREAMING_SNAKE_CASE_ = feature_extractor(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE_ = np.asarray(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_lowerCAmelCase ) == len(_lowerCAmelCase ) for x, y in zip(_lowerCAmelCase , processed_features[input_name] ) ) ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='np' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='pt' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' )[input_name] SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='pt' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = min(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad( _lowerCAmelCase , padding='max_length' , max_length=_lowerCAmelCase , truncation=_lowerCAmelCase , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Tuple ): from datasets import load_dataset SCREAMING_SNAKE_CASE_ = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE_ = ds.sort('id' ).select(range(_lowerCAmelCase ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] def lowerCAmelCase_ ( self : Any ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [2.3_804E-03, 2.0_752E-03, 1.9_836E-03, 2.1_057E-03, 1.6_174E-03, 3.0_518E-04, 9.1_553E-05, 3.3_569E-04, 9.7_656E-04, 1.8_311E-03, 2.0_142E-03, 2.1_057E-03, 1.7_395E-03, 4.5_776E-04, -3.9_673E-04, 4.5_776E-04, 1.0_071E-03, 9.1_553E-05, 4.8_828E-04, 1.1_597E-03, 7.3_242E-04, 9.4_604E-04, 1.8_005E-03, 1.8_311E-03, 8.8_501E-04, 4.2_725E-04, 4.8_828E-04, 7.3_242E-04, 1.0_986E-03, 2.1_057E-03] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 93_680) ) self.assertTrue(torch.allclose(input_values[0, :30] , _lowerCAmelCase , atol=1E-6 ) ) def lowerCAmelCase_ ( self : Optional[int] ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [-2.6870, -3.0104, -3.1356, -3.5352, -3.0044, -3.0353, -3.4719, -3.6777, -3.1520, -2.9435, -2.6553, -2.8795, -2.9944, -2.5921, -3.0279, -3.0386, -3.0864, -3.1291, -3.2353, -2.7444, -2.6831, -2.7287, -3.1761, -3.1571, -3.2726, -3.0582, -3.1007, -3.4533, -3.4695, -3.0998] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 366, 80) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] , _lowerCAmelCase , atol=1E-4 ) )
31
1
lowerCamelCase__ : str = 8.31_4462 # Unit - J mol-1 K-1 def UpperCAmelCase_ ( __UpperCAmelCase : float , __UpperCAmelCase : float , __UpperCAmelCase : float ) -> 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 UpperCAmelCase_ ( __UpperCAmelCase : float , __UpperCAmelCase : float , __UpperCAmelCase : float ) -> 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()
31
from __future__ import annotations from typing import TypedDict class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = 42 lowercase_ = 42 def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> list[str]: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) return [s[i:] + s[:i] for i in range(len(__UpperCAmelCase ) )] def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> BWTTransformDict: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) if not s: raise ValueError('The parameter s must not be empty.' ) SCREAMING_SNAKE_CASE_ = all_rotations(__UpperCAmelCase ) rotations.sort() # sort the list of rotations in alphabetically order # make a string composed of the last char of each rotation SCREAMING_SNAKE_CASE_ = { "bwt_string": "".join([word[-1] for word in rotations] ), "idx_original_string": rotations.index(__UpperCAmelCase ), } return response def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : int ) -> str: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter bwt_string type must be str.' ) if not bwt_string: raise ValueError('The parameter bwt_string must not be empty.' ) try: SCREAMING_SNAKE_CASE_ = int(__UpperCAmelCase ) except ValueError: raise TypeError( 'The parameter idx_original_string type must be int or passive' ' of cast to int.' ) if idx_original_string < 0: raise ValueError('The parameter idx_original_string must not be lower than 0.' ) if idx_original_string >= len(__UpperCAmelCase ): raise ValueError( 'The parameter idx_original_string must be lower than' ' len(bwt_string).' ) SCREAMING_SNAKE_CASE_ = [''] * len(__UpperCAmelCase ) for _ in range(len(__UpperCAmelCase ) ): for i in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = bwt_string[i] + ordered_rotations[i] ordered_rotations.sort() return ordered_rotations[idx_original_string] if __name__ == "__main__": lowerCamelCase__ : Optional[int] = 'Provide a string that I will generate its BWT transform: ' lowerCamelCase__ : List[str] = input(entry_msg).strip() lowerCamelCase__ : int = bwt_transform(s) print( f'''Burrows Wheeler transform for string \'{s}\' results ''' f'''in \'{result['bwt_string']}\'''' ) lowerCamelCase__ : Dict = reverse_bwt(result['bwt_string'], result['idx_original_string']) print( f'''Reversing Burrows Wheeler transform for entry \'{result['bwt_string']}\' ''' f'''we get original string \'{original_string}\'''' )
31
1
from __future__ import annotations def UpperCAmelCase_ ( __UpperCAmelCase : list ) -> list: if len(__UpperCAmelCase ) == 0: return [] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = min(__UpperCAmelCase ), max(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = int(max_value - min_value ) + 1 SCREAMING_SNAKE_CASE_ = [[] for _ in range(__UpperCAmelCase )] for i in my_list: buckets[int(i - min_value )].append(__UpperCAmelCase ) return [v for bucket in buckets for v in sorted(__UpperCAmelCase )] if __name__ == "__main__": from doctest import testmod testmod() assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]
31
class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = {} def lowerCAmelCase_ ( self : List[str] ): print(self.vertex ) for i in self.vertex: print(_lowerCAmelCase , ' -> ' , ' -> '.join([str(_lowerCAmelCase ) for j in self.vertex[i]] ) ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int , _lowerCAmelCase : int ): # check if vertex is already present, if from_vertex in self.vertex: self.vertex[from_vertex].append(_lowerCAmelCase ) else: # else make a new vertex SCREAMING_SNAKE_CASE_ = [to_vertex] def lowerCAmelCase_ ( self : Optional[Any] ): # visited array for storing already visited nodes SCREAMING_SNAKE_CASE_ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : int , _lowerCAmelCase : list ): # mark start vertex as visited SCREAMING_SNAKE_CASE_ = True print(_lowerCAmelCase , end=' ' ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) if __name__ == "__main__": lowerCamelCase__ : List[Any] = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('DFS:') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
31
1
def UpperCAmelCase_ ( __UpperCAmelCase : int = 10 , __UpperCAmelCase : int = 10_00 , __UpperCAmelCase : bool = True ) -> int: assert ( isinstance(__UpperCAmelCase , __UpperCAmelCase ) and isinstance(__UpperCAmelCase , __UpperCAmelCase ) and isinstance(__UpperCAmelCase , __UpperCAmelCase ) ), "Invalid type of value(s) specified to function!" if min_val > max_val: raise ValueError('Invalid value for min_val or max_val (min_value < max_value)' ) return min_val if option else max_val def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int ) -> int: return int((number_a + number_a) / 2 ) def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int , __UpperCAmelCase : int ) -> None: assert ( isinstance(__UpperCAmelCase , __UpperCAmelCase ) and isinstance(__UpperCAmelCase , __UpperCAmelCase ) and isinstance(__UpperCAmelCase , __UpperCAmelCase ) ), 'argument values must be type of "int"' if lower > higher: raise ValueError('argument value for lower and higher must be(lower > higher)' ) if not lower < to_guess < higher: raise ValueError( 'guess value must be within the range of lower and higher value' ) def answer(__UpperCAmelCase : int ) -> str: if number > to_guess: return "high" elif number < to_guess: return "low" else: return "same" print('started...' ) SCREAMING_SNAKE_CASE_ = lower SCREAMING_SNAKE_CASE_ = higher SCREAMING_SNAKE_CASE_ = [] while True: SCREAMING_SNAKE_CASE_ = get_avg(__UpperCAmelCase , __UpperCAmelCase ) last_numbers.append(__UpperCAmelCase ) if answer(__UpperCAmelCase ) == "low": SCREAMING_SNAKE_CASE_ = number elif answer(__UpperCAmelCase ) == "high": SCREAMING_SNAKE_CASE_ = number else: break print(f"guess the number : {last_numbers[-1]}" ) print(f"details : {last_numbers!s}" ) def UpperCAmelCase_ ( ) -> None: SCREAMING_SNAKE_CASE_ = int(input('Enter lower value : ' ).strip() ) SCREAMING_SNAKE_CASE_ = int(input('Enter high value : ' ).strip() ) SCREAMING_SNAKE_CASE_ = int(input('Enter value to guess : ' ).strip() ) guess_the_number(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) if __name__ == "__main__": main()
31
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : str = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/config.json', 'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json', 'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/config.json', 'funnel-transformer/medium-base': 'https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json', 'funnel-transformer/intermediate': ( 'https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json' ), 'funnel-transformer/intermediate-base': ( 'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json' ), 'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/config.json', 'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json', 'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json', 'funnel-transformer/xlarge-base': 'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json', } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "funnel" lowercase_ = { "hidden_size": "d_model", "num_attention_heads": "n_head", } def __init__( self : int , _lowerCAmelCase : Optional[int]=30_522 , _lowerCAmelCase : List[str]=[4, 4, 4] , _lowerCAmelCase : Tuple=None , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : int=768 , _lowerCAmelCase : Optional[Any]=12 , _lowerCAmelCase : Optional[Any]=64 , _lowerCAmelCase : Optional[Any]=3_072 , _lowerCAmelCase : List[str]="gelu_new" , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : int=0.1 , _lowerCAmelCase : Tuple=0.0 , _lowerCAmelCase : List[Any]=0.1 , _lowerCAmelCase : Dict=None , _lowerCAmelCase : str=1E-9 , _lowerCAmelCase : Any="mean" , _lowerCAmelCase : Union[str, Any]="relative_shift" , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Dict=True , _lowerCAmelCase : Tuple=True , **_lowerCAmelCase : Optional[Any] , ): SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = block_sizes SCREAMING_SNAKE_CASE_ = [1] * len(_lowerCAmelCase ) if block_repeats is None else block_repeats assert len(_lowerCAmelCase ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." SCREAMING_SNAKE_CASE_ = num_decoder_layers SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = n_head SCREAMING_SNAKE_CASE_ = d_head SCREAMING_SNAKE_CASE_ = d_inner SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = initializer_std SCREAMING_SNAKE_CASE_ = layer_norm_eps assert pooling_type in [ "mean", "max", ], F"Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported." SCREAMING_SNAKE_CASE_ = pooling_type assert attention_type in [ "relative_shift", "factorized", ], F"Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported." SCREAMING_SNAKE_CASE_ = attention_type SCREAMING_SNAKE_CASE_ = separate_cls SCREAMING_SNAKE_CASE_ = truncate_seq SCREAMING_SNAKE_CASE_ = pool_q_only super().__init__(**_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : Optional[int] ): return sum(self.block_sizes ) @num_hidden_layers.setter def lowerCAmelCase_ ( self : int , _lowerCAmelCase : List[Any] ): raise NotImplementedError( 'This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.' ) @property def lowerCAmelCase_ ( self : List[Any] ): return len(self.block_sizes ) @num_blocks.setter def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any] ): raise NotImplementedError('This model does not support the setting of `num_blocks`. Please set `block_sizes`.' )
31
1
import os def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> List[str]: SCREAMING_SNAKE_CASE_ = len(grid[0] ) SCREAMING_SNAKE_CASE_ = len(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = 0 # Check vertically, horizontally, diagonally at the same time (only works # for nxn grid) for i in range(__UpperCAmelCase ): for j in range(n_rows - 3 ): SCREAMING_SNAKE_CASE_ = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i] SCREAMING_SNAKE_CASE_ = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3] # Left-to-right diagonal (\) product if i < n_columns - 3: SCREAMING_SNAKE_CASE_ = ( grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2] * grid[i + 3][j + 3] ) # Right-to-left diagonal(/) product if i > 2: SCREAMING_SNAKE_CASE_ = ( grid[i][j] * grid[i - 1][j + 1] * grid[i - 2][j + 2] * grid[i - 3][j + 3] ) SCREAMING_SNAKE_CASE_ = max( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) if max_product > largest: SCREAMING_SNAKE_CASE_ = max_product return largest def UpperCAmelCase_ ( ) -> Any: SCREAMING_SNAKE_CASE_ = [] with open(os.path.dirname(__UpperCAmelCase ) + '/grid.txt' ) as file: for line in file: grid.append(line.strip('\n' ).split(' ' ) ) SCREAMING_SNAKE_CASE_ = [[int(__UpperCAmelCase ) for i in grid[j]] for j in range(len(__UpperCAmelCase ) )] return largest_product(__UpperCAmelCase ) if __name__ == "__main__": print(solution())
31
from __future__ import annotations from collections.abc import Iterator class lowerCamelCase_ : '''simple docstring''' def __init__( self : Union[str, Any] , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = value SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None class lowerCamelCase_ : '''simple docstring''' def __init__( self : int , _lowerCAmelCase : Node ): SCREAMING_SNAKE_CASE_ = tree def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Node | None ): if node is None: return 0 return node.value + ( self.depth_first_search(node.left ) + self.depth_first_search(node.right ) ) def __iter__( self : Dict ): yield self.depth_first_search(self.tree ) if __name__ == "__main__": import doctest doctest.testmod()
31
1