code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
'''simple docstring''' 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 a = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast a = TaTokenizerFast a = {"configuration_mt5": ["MT5Config", "MT5OnnxConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = [ "MT5EncoderModel", "MT5ForConditionalGeneration", "MT5ForQuestionAnswering", "MT5Model", "MT5PreTrainedModel", "MT5Stack", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = ["TFMT5EncoderModel", "TFMT5ForConditionalGeneration", "TFMT5Model"] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = ["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 a = _LazyModule( __name__, globals()["__file__"], _import_structure, extra_objects={"MT5Tokenizer": MTaTokenizer, "MT5TokenizerFast": MTaTokenizerFast}, module_spec=__spec__, )
13
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a = {"configuration_sew": ["SEW_PRETRAINED_CONFIG_ARCHIVE_MAP", "SEWConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = [ "SEW_PRETRAINED_MODEL_ARCHIVE_LIST", "SEWForCTC", "SEWForSequenceClassification", "SEWModel", "SEWPreTrainedModel", ] if TYPE_CHECKING: from .configuration_sew import SEW_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_sew import ( SEW_PRETRAINED_MODEL_ARCHIVE_LIST, SEWForCTC, SEWForSequenceClassification, SEWModel, SEWPreTrainedModel, ) else: import sys a = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
13
1
'''simple docstring''' import darl # noqa import gym import tqdm from diffusers.experimental import ValueGuidedRLPipeline a = { "n_samples": 64, "horizon": 32, "num_inference_steps": 20, "n_guide_steps": 2, # can set to 0 for faster sampling, does not use value network "scale_grad_by_std": True, "scale": 0.1, "eta": 0.0, "t_grad_cutoff": 2, "device": "cpu", } if __name__ == "__main__": a = "hopper-medium-v2" a = gym.make(env_name) a = ValueGuidedRLPipeline.from_pretrained( "bglick13/hopper-medium-v2-value-function-hor32", env=env, ) env.seed(0) a = env.reset() a = 0 a = 0 a = 1000 a = [obs.copy()] try: for t in tqdm.tqdm(range(T)): # call the policy a = pipeline(obs, planning_horizon=32) # execute action in environment a , a , a , a = env.step(denorm_actions) a = env.get_normalized_score(total_reward) # update return total_reward += reward total_score += score print( F'''Step: {t}, Reward: {reward}, Total Reward: {total_reward}, Score: {score}, Total Score:''' F''' {total_score}''' ) # save observations for rendering rollout.append(next_observation.copy()) a = next_observation except KeyboardInterrupt: pass print(F'''Total reward: {total_reward}''')
13
'''simple docstring''' import requests from bsa import BeautifulSoup def __magic_name__ ( __UpperCAmelCase = "AAPL" ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""https://in.finance.yahoo.com/quote/{symbol}?s={symbol}""" __SCREAMING_SNAKE_CASE = BeautifulSoup(requests.get(__UpperCAmelCase ).text , """html.parser""" ) __SCREAMING_SNAKE_CASE = """My(6px) Pos(r) smartphone_Mt(6px)""" return soup.find("""div""" , class_=class_ ).find("""span""" ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(F'''Current {symbol:<4} stock price is {stock_price(symbol):>8}''')
13
1
'''simple docstring''' from typing import List, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging a = logging.get_logger(__name__) a = { "huggingface/time-series-transformer-tourism-monthly": ( "https://huggingface.co/huggingface/time-series-transformer-tourism-monthly/resolve/main/config.json" ), # See all TimeSeriesTransformer models at https://huggingface.co/models?filter=time_series_transformer } class __a ( _snake_case ): __UpperCamelCase : List[Any] = 'time_series_transformer' __UpperCamelCase : Dict = { 'hidden_size': 'd_model', 'num_attention_heads': 'encoder_attention_heads', 'num_hidden_layers': 'encoder_layers', } def __init__( self : List[str] ,lowerCamelCase : Optional[int] = None ,lowerCamelCase : Optional[int] = None ,lowerCamelCase : str = "student_t" ,lowerCamelCase : str = "nll" ,lowerCamelCase : int = 1 ,lowerCamelCase : List[int] = [1, 2, 3, 4, 5, 6, 7] ,lowerCamelCase : Optional[Union[str, bool]] = "mean" ,lowerCamelCase : int = 0 ,lowerCamelCase : int = 0 ,lowerCamelCase : int = 0 ,lowerCamelCase : int = 0 ,lowerCamelCase : Optional[List[int]] = None ,lowerCamelCase : Optional[List[int]] = None ,lowerCamelCase : int = 32 ,lowerCamelCase : int = 32 ,lowerCamelCase : int = 2 ,lowerCamelCase : int = 2 ,lowerCamelCase : int = 2 ,lowerCamelCase : int = 2 ,lowerCamelCase : bool = True ,lowerCamelCase : str = "gelu" ,lowerCamelCase : int = 64 ,lowerCamelCase : float = 0.1 ,lowerCamelCase : float = 0.1 ,lowerCamelCase : float = 0.1 ,lowerCamelCase : float = 0.1 ,lowerCamelCase : float = 0.1 ,lowerCamelCase : int = 100 ,lowerCamelCase : float = 0.02 ,lowerCamelCase : Optional[Any]=True ,**lowerCamelCase : Dict ,): '''simple docstring''' __SCREAMING_SNAKE_CASE = prediction_length __SCREAMING_SNAKE_CASE = context_length or prediction_length __SCREAMING_SNAKE_CASE = distribution_output __SCREAMING_SNAKE_CASE = loss __SCREAMING_SNAKE_CASE = input_size __SCREAMING_SNAKE_CASE = num_time_features __SCREAMING_SNAKE_CASE = lags_sequence __SCREAMING_SNAKE_CASE = scaling __SCREAMING_SNAKE_CASE = num_dynamic_real_features __SCREAMING_SNAKE_CASE = num_static_real_features __SCREAMING_SNAKE_CASE = num_static_categorical_features if cardinality and num_static_categorical_features > 0: if len(lowerCamelCase ) != num_static_categorical_features: raise ValueError( """The cardinality should be a list of the same length as `num_static_categorical_features`""" ) __SCREAMING_SNAKE_CASE = cardinality else: __SCREAMING_SNAKE_CASE = [0] if embedding_dimension and num_static_categorical_features > 0: if len(lowerCamelCase ) != num_static_categorical_features: raise ValueError( """The embedding dimension should be a list of the same length as `num_static_categorical_features`""" ) __SCREAMING_SNAKE_CASE = embedding_dimension else: __SCREAMING_SNAKE_CASE = [min(50 ,(cat + 1) // 2 ) for cat in self.cardinality] __SCREAMING_SNAKE_CASE = num_parallel_samples # Transformer architecture configuration __SCREAMING_SNAKE_CASE = input_size * len(lowerCamelCase ) + self._number_of_features __SCREAMING_SNAKE_CASE = d_model __SCREAMING_SNAKE_CASE = encoder_attention_heads __SCREAMING_SNAKE_CASE = decoder_attention_heads __SCREAMING_SNAKE_CASE = encoder_ffn_dim __SCREAMING_SNAKE_CASE = decoder_ffn_dim __SCREAMING_SNAKE_CASE = encoder_layers __SCREAMING_SNAKE_CASE = decoder_layers __SCREAMING_SNAKE_CASE = dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = encoder_layerdrop __SCREAMING_SNAKE_CASE = decoder_layerdrop __SCREAMING_SNAKE_CASE = activation_function __SCREAMING_SNAKE_CASE = init_std __SCREAMING_SNAKE_CASE = use_cache super().__init__(is_encoder_decoder=lowerCamelCase ,**lowerCamelCase ) @property def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' return ( sum(self.embedding_dimension ) + self.num_dynamic_real_features + self.num_time_features + self.num_static_real_features + self.input_size * 2 # the log1p(abs(loc)) and log(scale) features )
13
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase ) -> bool: '''simple docstring''' if num < 0: return False __SCREAMING_SNAKE_CASE = num __SCREAMING_SNAKE_CASE = 0 while num > 0: __SCREAMING_SNAKE_CASE = rev_num * 10 + (num % 10) num //= 10 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
13
1
'''simple docstring''' import math def __magic_name__ ( __UpperCAmelCase ) -> bool: '''simple docstring''' return math.sqrt(__UpperCAmelCase ) * math.sqrt(__UpperCAmelCase ) == num def __magic_name__ ( __UpperCAmelCase ) -> bool: '''simple docstring''' __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = n while left <= right: __SCREAMING_SNAKE_CASE = (left + right) // 2 if mid**2 == n: return True elif mid**2 > n: __SCREAMING_SNAKE_CASE = mid - 1 else: __SCREAMING_SNAKE_CASE = mid + 1 return False if __name__ == "__main__": import doctest doctest.testmod()
13
'''simple docstring''' from __future__ import annotations from collections.abc import Callable a = list[list[float | int]] def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Matrix: '''simple docstring''' __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [[0 for _ in range(size + 1 )] for _ in range(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 for row in range(__UpperCAmelCase ): for col in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = matrix[row][col] __SCREAMING_SNAKE_CASE = vector[row][0] __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 while row < size and col < size: # pivoting __SCREAMING_SNAKE_CASE = max((abs(augmented[rowa][col] ), rowa) for rowa in range(__UpperCAmelCase , __UpperCAmelCase ) )[ 1 ] if augmented[pivot_row][col] == 0: col += 1 continue else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = augmented[pivot_row], augmented[row] for rowa in range(row + 1 , __UpperCAmelCase ): __SCREAMING_SNAKE_CASE = augmented[rowa][col] / augmented[row][col] __SCREAMING_SNAKE_CASE = 0 for cola in range(col + 1 , size + 1 ): augmented[rowa][cola] -= augmented[row][cola] * ratio row += 1 col += 1 # back substitution for col in range(1 , __UpperCAmelCase ): for row in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = augmented[row][col] / augmented[col][col] for cola in range(__UpperCAmelCase , size + 1 ): augmented[row][cola] -= augmented[col][cola] * ratio # round to get rid of numbers like 2.000000000000004 return [ [round(augmented[row][size] / augmented[row][row] , 10 )] for row in range(__UpperCAmelCase ) ] def __magic_name__ ( __UpperCAmelCase ) -> Callable[[int], int]: '''simple docstring''' __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [[0 for _ in range(__UpperCAmelCase )] for _ in range(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = [[0] for _ in range(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 for x_val, y_val in enumerate(__UpperCAmelCase ): for col in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = (x_val + 1) ** (size - col - 1) __SCREAMING_SNAKE_CASE = y_val __SCREAMING_SNAKE_CASE = solve(__UpperCAmelCase , __UpperCAmelCase ) def interpolated_func(__UpperCAmelCase ) -> int: return sum( round(coeffs[x_val][0] ) * (var ** (size - x_val - 1)) for x_val in range(__UpperCAmelCase ) ) return interpolated_func def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' return ( 1 - variable + variable**2 - variable**3 + variable**4 - variable**5 + variable**6 - variable**7 + variable**8 - variable**9 + variable**10 ) def __magic_name__ ( __UpperCAmelCase = question_function , __UpperCAmelCase = 10 ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = [func(__UpperCAmelCase ) for x_val in range(1 , order + 1 )] __SCREAMING_SNAKE_CASE = [ interpolate(data_points[:max_coeff] ) for max_coeff in range(1 , order + 1 ) ] __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 for poly in polynomials: __SCREAMING_SNAKE_CASE = 1 while func(__UpperCAmelCase ) == poly(__UpperCAmelCase ): x_val += 1 ret += poly(__UpperCAmelCase ) return ret if __name__ == "__main__": print(F'''{solution() = }''')
13
1
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_distilbert import DistilBertTokenizer a = logging.get_logger(__name__) a = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} a = { "vocab_file": { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt" ), "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt", "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt" ), }, "tokenizer_file": { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json" ), "distilbert-base-german-cased": ( "https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json" ), "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json" ), }, } a = { "distilbert-base-uncased": 512, "distilbert-base-uncased-distilled-squad": 512, "distilbert-base-cased": 512, "distilbert-base-cased-distilled-squad": 512, "distilbert-base-german-cased": 512, "distilbert-base-multilingual-cased": 512, } a = { "distilbert-base-uncased": {"do_lower_case": True}, "distilbert-base-uncased-distilled-squad": {"do_lower_case": True}, "distilbert-base-cased": {"do_lower_case": False}, "distilbert-base-cased-distilled-squad": {"do_lower_case": False}, "distilbert-base-german-cased": {"do_lower_case": False}, "distilbert-base-multilingual-cased": {"do_lower_case": False}, } class __a ( _snake_case ): __UpperCamelCase : int = VOCAB_FILES_NAMES __UpperCamelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : List[Any] = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase : str = ['input_ids', 'attention_mask'] __UpperCamelCase : str = DistilBertTokenizer def __init__( self : Union[str, Any] ,lowerCamelCase : Optional[int]=None ,lowerCamelCase : List[str]=None ,lowerCamelCase : int=True ,lowerCamelCase : Any="[UNK]" ,lowerCamelCase : Tuple="[SEP]" ,lowerCamelCase : Dict="[PAD]" ,lowerCamelCase : Optional[Any]="[CLS]" ,lowerCamelCase : int="[MASK]" ,lowerCamelCase : Union[str, Any]=True ,lowerCamelCase : Tuple=None ,**lowerCamelCase : Any ,): '''simple docstring''' super().__init__( lowerCamelCase ,tokenizer_file=lowerCamelCase ,do_lower_case=lowerCamelCase ,unk_token=lowerCamelCase ,sep_token=lowerCamelCase ,pad_token=lowerCamelCase ,cls_token=lowerCamelCase ,mask_token=lowerCamelCase ,tokenize_chinese_chars=lowerCamelCase ,strip_accents=lowerCamelCase ,**lowerCamelCase ,) __SCREAMING_SNAKE_CASE = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("""lowercase""" ,lowerCamelCase ) != do_lower_case or normalizer_state.get("""strip_accents""" ,lowerCamelCase ) != strip_accents or normalizer_state.get("""handle_chinese_chars""" ,lowerCamelCase ) != tokenize_chinese_chars ): __SCREAMING_SNAKE_CASE = getattr(lowerCamelCase ,normalizer_state.pop("""type""" ) ) __SCREAMING_SNAKE_CASE = do_lower_case __SCREAMING_SNAKE_CASE = strip_accents __SCREAMING_SNAKE_CASE = tokenize_chinese_chars __SCREAMING_SNAKE_CASE = normalizer_class(**lowerCamelCase ) __SCREAMING_SNAKE_CASE = do_lower_case def UpperCAmelCase__ ( self : Union[str, Any] ,lowerCamelCase : List[str] ,lowerCamelCase : Dict=None ): '''simple docstring''' __SCREAMING_SNAKE_CASE = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : List[int] ,lowerCamelCase : Optional[List[int]] = None ): '''simple docstring''' __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 UpperCAmelCase__ ( self : Optional[Any] ,lowerCamelCase : str ,lowerCamelCase : Optional[str] = None ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self._tokenizer.model.save(lowerCamelCase ,name=lowerCamelCase ) return tuple(lowerCamelCase )
13
'''simple docstring''' from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax a = logging.get_logger(__name__) @add_end_docstrings(_snake_case ) class __a ( _snake_case ): def __init__( self : Union[str, Any] ,**lowerCamelCase : str ): '''simple docstring''' super().__init__(**lowerCamelCase ) requires_backends(self ,"""vision""" ) self.check_model_type( TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if self.framework == """tf""" else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING ) def __call__( self : Dict ,lowerCamelCase : Union[str, List[str], "Image", List["Image"]] ,**lowerCamelCase : Optional[Any] ): '''simple docstring''' return super().__call__(lowerCamelCase ,**lowerCamelCase ) def UpperCAmelCase__ ( self : Optional[Any] ,**lowerCamelCase : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = {} if "candidate_labels" in kwargs: __SCREAMING_SNAKE_CASE = kwargs["""candidate_labels"""] if "hypothesis_template" in kwargs: __SCREAMING_SNAKE_CASE = kwargs["""hypothesis_template"""] return preprocess_params, {}, {} def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : Union[str, Any]=None ,lowerCamelCase : Union[str, Any]="This is a photo of {}." ): '''simple docstring''' __SCREAMING_SNAKE_CASE = load_image(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.image_processor(images=[image] ,return_tensors=self.framework ) __SCREAMING_SNAKE_CASE = candidate_labels __SCREAMING_SNAKE_CASE = [hypothesis_template.format(lowerCamelCase ) for x in candidate_labels] __SCREAMING_SNAKE_CASE = self.tokenizer(lowerCamelCase ,return_tensors=self.framework ,padding=lowerCamelCase ) __SCREAMING_SNAKE_CASE = [text_inputs] return inputs def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = model_inputs.pop("""candidate_labels""" ) __SCREAMING_SNAKE_CASE = model_inputs.pop("""text_inputs""" ) if isinstance(text_inputs[0] ,lowerCamelCase ): __SCREAMING_SNAKE_CASE = text_inputs[0] else: # Batching case. __SCREAMING_SNAKE_CASE = text_inputs[0][0] __SCREAMING_SNAKE_CASE = self.model(**lowerCamelCase ,**lowerCamelCase ) __SCREAMING_SNAKE_CASE = { """candidate_labels""": candidate_labels, """logits""": outputs.logits_per_image, } return model_outputs def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = model_outputs.pop("""candidate_labels""" ) __SCREAMING_SNAKE_CASE = model_outputs["""logits"""][0] if self.framework == "pt": __SCREAMING_SNAKE_CASE = logits.softmax(dim=-1 ).squeeze(-1 ) __SCREAMING_SNAKE_CASE = probs.tolist() if not isinstance(lowerCamelCase ,lowerCamelCase ): __SCREAMING_SNAKE_CASE = [scores] elif self.framework == "tf": __SCREAMING_SNAKE_CASE = stable_softmax(lowerCamelCase ,axis=-1 ) __SCREAMING_SNAKE_CASE = probs.numpy().tolist() else: raise ValueError(f"""Unsupported framework: {self.framework}""" ) __SCREAMING_SNAKE_CASE = [ {"""score""": score, """label""": candidate_label} for score, candidate_label in sorted(zip(lowerCamelCase ,lowerCamelCase ) ,key=lambda lowerCamelCase : -x[0] ) ] return result
13
1
'''simple docstring''' from typing import List, Optional import numpy as np from ...processing_utils import ProcessorMixin from ...utils import to_numpy class __a ( _snake_case ): __UpperCamelCase : Optional[Any] = 'EncodecFeatureExtractor' __UpperCamelCase : Dict = ('T5Tokenizer', 'T5TokenizerFast') def __init__( self : Any ,lowerCamelCase : Optional[int] ,lowerCamelCase : List[str] ): '''simple docstring''' super().__init__(lowerCamelCase ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.feature_extractor __SCREAMING_SNAKE_CASE = False def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : Dict=None ,lowerCamelCase : int=None ,lowerCamelCase : str=True ): '''simple docstring''' return self.tokenizer.get_decoder_prompt_ids(task=lowerCamelCase ,language=lowerCamelCase ,no_timestamps=lowerCamelCase ) def __call__( self : Any ,*lowerCamelCase : Tuple ,**lowerCamelCase : List[str] ): '''simple docstring''' if self._in_target_context_manager: return self.current_processor(*lowerCamelCase ,**lowerCamelCase ) __SCREAMING_SNAKE_CASE = kwargs.pop("""audio""" ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = kwargs.pop("""sampling_rate""" ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = kwargs.pop("""text""" ,lowerCamelCase ) if len(lowerCamelCase ) > 0: __SCREAMING_SNAKE_CASE = args[0] __SCREAMING_SNAKE_CASE = args[1:] if audio is None and text is None: raise ValueError("""You need to specify either an `audio` or `text` input to process.""" ) if text is not None: __SCREAMING_SNAKE_CASE = self.tokenizer(lowerCamelCase ,**lowerCamelCase ) if audio is not None: __SCREAMING_SNAKE_CASE = self.feature_extractor(lowerCamelCase ,*lowerCamelCase ,sampling_rate=lowerCamelCase ,**lowerCamelCase ) if audio is None: return inputs elif text is None: return audio_inputs else: __SCREAMING_SNAKE_CASE = audio_inputs["""input_values"""] if "padding_mask" in audio_inputs: __SCREAMING_SNAKE_CASE = audio_inputs["""padding_mask"""] return inputs def UpperCAmelCase__ ( self : Optional[int] ,*lowerCamelCase : str ,**lowerCamelCase : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = kwargs.pop("""audio""" ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = kwargs.pop("""padding_mask""" ,lowerCamelCase ) if len(lowerCamelCase ) > 0: __SCREAMING_SNAKE_CASE = args[0] __SCREAMING_SNAKE_CASE = args[1:] if audio_values is not None: return self._decode_audio(lowerCamelCase ,padding_mask=lowerCamelCase ) else: return self.tokenizer.batch_decode(*lowerCamelCase ,**lowerCamelCase ) def UpperCAmelCase__ ( self : Any ,*lowerCamelCase : Any ,**lowerCamelCase : Dict ): '''simple docstring''' return self.tokenizer.decode(*lowerCamelCase ,**lowerCamelCase ) def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : List[str] ,lowerCamelCase : Optional = None ): '''simple docstring''' __SCREAMING_SNAKE_CASE = to_numpy(lowerCamelCase ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = audio_values.shape if padding_mask is None: return list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = to_numpy(lowerCamelCase ) # match the sequence length of the padding mask to the generated audio arrays by padding with the **non-padding** # token (so that the generated audio values are **not** treated as padded tokens) __SCREAMING_SNAKE_CASE = seq_len - padding_mask.shape[-1] __SCREAMING_SNAKE_CASE = 1 - self.feature_extractor.padding_value __SCREAMING_SNAKE_CASE = np.pad(lowerCamelCase ,((0, 0), (0, difference)) ,"""constant""" ,constant_values=lowerCamelCase ) __SCREAMING_SNAKE_CASE = audio_values.tolist() for i in range(lowerCamelCase ): __SCREAMING_SNAKE_CASE = np.asarray(audio_values[i] )[ padding_mask[i][None, :] != self.feature_extractor.padding_value ] __SCREAMING_SNAKE_CASE = sliced_audio.reshape(lowerCamelCase ,-1 ) return audio_values
13
'''simple docstring''' from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers a = [ "python", "tqdm", "regex", "requests", "packaging", "filelock", "numpy", "tokenizers", "huggingface-hub", "safetensors", "accelerate", "pyyaml", ] for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed elif pkg == "accelerate": # must be loaded here, or else tqdm check may fail from .utils import is_accelerate_available # Maybe switch to is_torch_available in the future here so that Accelerate is hard dep of # Transformers with PyTorch if not is_accelerate_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(F'''can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py''') def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase=None ) -> Optional[Any]: '''simple docstring''' require_version(deps[pkg] , __UpperCAmelCase )
13
1
'''simple docstring''' from random import shuffle import tensorflow as tf from numpy import array def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = int(__UpperCAmelCase ) assert noofclusters < len(__UpperCAmelCase ) # Find out the dimensionality __SCREAMING_SNAKE_CASE = len(vectors[0] ) # Will help select random centroids from among the available vectors __SCREAMING_SNAKE_CASE = list(range(len(__UpperCAmelCase ) ) ) shuffle(__UpperCAmelCase ) # GRAPH OF COMPUTATION # We initialize a new graph and set it as the default during each run # of this algorithm. This ensures that as this function is called # multiple times, the default graph doesn't keep getting crowded with # unused ops and Variables from previous function calls. __SCREAMING_SNAKE_CASE = tf.Graph() with graph.as_default(): # SESSION OF COMPUTATION __SCREAMING_SNAKE_CASE = tf.Session() ##CONSTRUCTING THE ELEMENTS OF COMPUTATION ##First lets ensure we have a Variable vector for each centroid, ##initialized to one of the vectors from the available data points __SCREAMING_SNAKE_CASE = [ tf.Variable(vectors[vector_indices[i]] ) for i in range(__UpperCAmelCase ) ] ##These nodes will assign the centroid Variables the appropriate ##values __SCREAMING_SNAKE_CASE = tf.placeholder("""float64""" , [dim] ) __SCREAMING_SNAKE_CASE = [] for centroid in centroids: cent_assigns.append(tf.assign(__UpperCAmelCase , __UpperCAmelCase ) ) ##Variables for cluster assignments of individual vectors(initialized ##to 0 at first) __SCREAMING_SNAKE_CASE = [tf.Variable(0 ) for i in range(len(__UpperCAmelCase ) )] ##These nodes will assign an assignment Variable the appropriate ##value __SCREAMING_SNAKE_CASE = tf.placeholder("""int32""" ) __SCREAMING_SNAKE_CASE = [] for assignment in assignments: cluster_assigns.append(tf.assign(__UpperCAmelCase , __UpperCAmelCase ) ) ##Now lets construct the node that will compute the mean # The placeholder for the input __SCREAMING_SNAKE_CASE = tf.placeholder("""float""" , [None, dim] ) # The Node/op takes the input and computes a mean along the 0th # dimension, i.e. the list of input vectors __SCREAMING_SNAKE_CASE = tf.reduce_mean(__UpperCAmelCase , 0 ) ##Node for computing Euclidean distances # Placeholders for input __SCREAMING_SNAKE_CASE = tf.placeholder("""float""" , [dim] ) __SCREAMING_SNAKE_CASE = tf.placeholder("""float""" , [dim] ) __SCREAMING_SNAKE_CASE = tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(__UpperCAmelCase , __UpperCAmelCase ) , 2 ) ) ) ##This node will figure out which cluster to assign a vector to, ##based on Euclidean distances of the vector from the centroids. # Placeholder for input __SCREAMING_SNAKE_CASE = tf.placeholder("""float""" , [noofclusters] ) __SCREAMING_SNAKE_CASE = tf.argmin(__UpperCAmelCase , 0 ) ##INITIALIZING STATE VARIABLES ##This will help initialization of all Variables defined with respect ##to the graph. The Variable-initializer should be defined after ##all the Variables have been constructed, so that each of them ##will be included in the initialization. __SCREAMING_SNAKE_CASE = tf.initialize_all_variables() # Initialize all variables sess.run(__UpperCAmelCase ) ##CLUSTERING ITERATIONS # Now perform the Expectation-Maximization steps of K-Means clustering # iterations. To keep things simple, we will only do a set number of # iterations, instead of using a Stopping Criterion. __SCREAMING_SNAKE_CASE = 100 for _ in range(__UpperCAmelCase ): ##EXPECTATION STEP ##Based on the centroid locations till last iteration, compute ##the _expected_ centroid assignments. # Iterate over each vector for vector_n in range(len(__UpperCAmelCase ) ): __SCREAMING_SNAKE_CASE = vectors[vector_n] # Compute Euclidean distance between this vector and each # centroid. Remember that this list cannot be named #'centroid_distances', since that is the input to the # cluster assignment node. __SCREAMING_SNAKE_CASE = [ sess.run(__UpperCAmelCase , feed_dict={va: vect, va: sess.run(__UpperCAmelCase )} ) for centroid in centroids ] # Now use the cluster assignment node, with the distances # as the input __SCREAMING_SNAKE_CASE = sess.run( __UpperCAmelCase , feed_dict={centroid_distances: distances} ) # Now assign the value to the appropriate state variable sess.run( cluster_assigns[vector_n] , feed_dict={assignment_value: assignment} ) ##MAXIMIZATION STEP # Based on the expected state computed from the Expectation Step, # compute the locations of the centroids so as to maximize the # overall objective of minimizing within-cluster Sum-of-Squares for cluster_n in range(__UpperCAmelCase ): # Collect all the vectors assigned to this cluster __SCREAMING_SNAKE_CASE = [ vectors[i] for i in range(len(__UpperCAmelCase ) ) if sess.run(assignments[i] ) == cluster_n ] # Compute new centroid location __SCREAMING_SNAKE_CASE = sess.run( __UpperCAmelCase , feed_dict={mean_input: array(__UpperCAmelCase )} ) # Assign value to appropriate variable sess.run( cent_assigns[cluster_n] , feed_dict={centroid_value: new_location} ) # Return centroids and assignments __SCREAMING_SNAKE_CASE = sess.run(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = sess.run(__UpperCAmelCase ) return centroids, assignments
13
'''simple docstring''' import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import numpy as np import pandas as pd from datasets import load_dataset import transformers from transformers import ( AutoConfig, BartForSequenceClassification, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, TapexTokenizer, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.17.0.dev0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/text-classification/requirements.txt") a = logging.getLogger(__name__) @dataclass class __a : __UpperCamelCase : Optional[str] = field( default='tab_fact', metadata={'help': 'The name of the dataset to use (via the datasets library).'} ) __UpperCamelCase : Optional[str] = field( default='tab_fact', metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'}, ) __UpperCamelCase : int = field( default=1024, metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) }, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={'help': 'Overwrite the cached preprocessed datasets or not.'} ) __UpperCamelCase : bool = field( default=_snake_case, metadata={ 'help': ( 'Whether to pad all samples to `max_seq_length`. ' 'If False, will pad the samples dynamically when batching to the maximum length in the batch.' ) }, ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) }, ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) }, ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of prediction examples to this ' 'value if set.' ) }, ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'A csv or a json file containing the training data.'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'A csv or a json file containing the validation data.'} ) __UpperCamelCase : Optional[str] = field(default=_snake_case, metadata={'help': 'A csv or a json file containing the test data.'} ) def UpperCAmelCase__ ( self : int ): '''simple docstring''' if self.dataset_name is not None: pass elif self.train_file is None or self.validation_file is None: raise ValueError("""Need either a GLUE task, a training/validation file or a dataset name.""" ) else: __SCREAMING_SNAKE_CASE = self.train_file.split(""".""" )[-1] assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file." __SCREAMING_SNAKE_CASE = self.validation_file.split(""".""" )[-1] assert ( validation_extension == train_extension ), "`validation_file` should have the same extension (csv or json) as `train_file`." @dataclass class __a : __UpperCamelCase : str = field( default=_snake_case, metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'}, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={'help': 'Whether to use one of the fast tokenizer (backed by the tokenizers library) or not.'}, ) __UpperCamelCase : str = field( default='main', metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'}, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) }, ) def __magic_name__ ( ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = parser.parse_args_into_dataclasses() # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) __SCREAMING_SNAKE_CASE = training_args.get_process_log_level() logger.setLevel(__UpperCAmelCase ) datasets.utils.logging.set_verbosity(__UpperCAmelCase ) transformers.utils.logging.set_verbosity(__UpperCAmelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + f"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(f"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. __SCREAMING_SNAKE_CASE = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: __SCREAMING_SNAKE_CASE = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. """ """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON training and evaluation files (see below) # or specify a GLUE benchmark task (the dataset will be downloaded automatically from the datasets Hub). # # For JSON files, this script will use the `question` column for the input question and `table` column for the corresponding table. # # If the CSVs/JSONs contain only one non-label column, the script does single sentence classification on this # single column. You can easily tweak this behavior (see below) # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. __SCREAMING_SNAKE_CASE = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) else: # Loading a dataset from your local files. # CSV/JSON training and evaluation files are needed. __SCREAMING_SNAKE_CASE = {"""train""": data_args.train_file, """validation""": data_args.validation_file} # Get the test dataset: you can provide your own CSV/JSON test file (see below) # when you use `do_predict` without specifying a GLUE benchmark task. if training_args.do_predict: if data_args.test_file is not None: __SCREAMING_SNAKE_CASE = data_args.train_file.split(""".""" )[-1] __SCREAMING_SNAKE_CASE = data_args.test_file.split(""".""" )[-1] assert ( test_extension == train_extension ), "`test_file` should have the same extension (csv or json) as `train_file`." __SCREAMING_SNAKE_CASE = data_args.test_file else: raise ValueError("""Need either a GLUE task or a test file for `do_predict`.""" ) for key in data_files.keys(): logger.info(f"""load a local file for {key}: {data_files[key]}""" ) if data_args.train_file.endswith(""".csv""" ): # Loading a dataset from local csv files __SCREAMING_SNAKE_CASE = load_dataset("""csv""" , data_files=__UpperCAmelCase , cache_dir=model_args.cache_dir ) else: # Loading a dataset from local json files __SCREAMING_SNAKE_CASE = load_dataset("""json""" , data_files=__UpperCAmelCase , cache_dir=model_args.cache_dir ) # See more about loading any type of standard or custom dataset at # https://huggingface.co/docs/datasets/loading_datasets.html. # Labels __SCREAMING_SNAKE_CASE = raw_datasets["""train"""].features["""label"""].names __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) # Load pretrained model and tokenizer # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. __SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=__UpperCAmelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # load tapex tokenizer __SCREAMING_SNAKE_CASE = TapexTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , add_prefix_space=__UpperCAmelCase , ) __SCREAMING_SNAKE_CASE = BartForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=__UpperCAmelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Padding strategy if data_args.pad_to_max_length: __SCREAMING_SNAKE_CASE = """max_length""" else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch __SCREAMING_SNAKE_CASE = False # Some models have set the order of the labels to use, so let's make sure we do use it. __SCREAMING_SNAKE_CASE = {"""Refused""": 0, """Entailed""": 1} __SCREAMING_SNAKE_CASE = {0: """Refused""", 1: """Entailed"""} if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( f"""The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the""" f"""model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}.""" ) __SCREAMING_SNAKE_CASE = min(data_args.max_seq_length , tokenizer.model_max_length ) def preprocess_tabfact_function(__UpperCAmelCase ): # Tokenize the texts def _convert_table_text_to_pandas(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = [_table_row.split("""#""" ) for _table_row in _table_text.strip("""\n""" ).split("""\n""" )] __SCREAMING_SNAKE_CASE = pd.DataFrame.from_records(_table_content[1:] , columns=_table_content[0] ) return _table_pd __SCREAMING_SNAKE_CASE = examples["""statement"""] __SCREAMING_SNAKE_CASE = list(map(_convert_table_text_to_pandas , examples["""table_text"""] ) ) __SCREAMING_SNAKE_CASE = tokenizer(__UpperCAmelCase , __UpperCAmelCase , padding=__UpperCAmelCase , max_length=__UpperCAmelCase , truncation=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = examples["""label"""] return result with training_args.main_process_first(desc="""dataset map pre-processing""" ): __SCREAMING_SNAKE_CASE = raw_datasets.map( __UpperCAmelCase , batched=__UpperCAmelCase , load_from_cache_file=not data_args.overwrite_cache , desc="""Running tokenizer on dataset""" , ) if training_args.do_train: if "train" not in raw_datasets: raise ValueError("""--do_train requires a train dataset""" ) __SCREAMING_SNAKE_CASE = raw_datasets["""train"""] if data_args.max_train_samples is not None: __SCREAMING_SNAKE_CASE = train_dataset.select(range(data_args.max_train_samples ) ) if training_args.do_eval: if "validation" not in raw_datasets and "validation_matched" not in raw_datasets: raise ValueError("""--do_eval requires a validation dataset""" ) __SCREAMING_SNAKE_CASE = raw_datasets["""validation"""] if data_args.max_eval_samples is not None: __SCREAMING_SNAKE_CASE = eval_dataset.select(range(data_args.max_eval_samples ) ) if training_args.do_predict or data_args.test_file is not None: if "test" not in raw_datasets and "test_matched" not in raw_datasets: raise ValueError("""--do_predict requires a test dataset""" ) __SCREAMING_SNAKE_CASE = raw_datasets["""test"""] if data_args.max_predict_samples is not None: __SCREAMING_SNAKE_CASE = predict_dataset.select(range(data_args.max_predict_samples ) ) # Log a few random samples from the training set: if training_args.do_train: for index in random.sample(range(len(__UpperCAmelCase ) ) , 3 ): logger.info(f"""Sample {index} of the training set: {train_dataset[index]}.""" ) # You can define your custom compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = p.predictions[0] if isinstance(p.predictions , __UpperCAmelCase ) else p.predictions __SCREAMING_SNAKE_CASE = np.argmax(__UpperCAmelCase , axis=1 ) return {"accuracy": (preds == p.label_ids).astype(np.floataa ).mean().item()} # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: __SCREAMING_SNAKE_CASE = default_data_collator elif training_args.fpaa: __SCREAMING_SNAKE_CASE = DataCollatorWithPadding(__UpperCAmelCase , pad_to_multiple_of=8 ) else: __SCREAMING_SNAKE_CASE = None # Initialize our Trainer __SCREAMING_SNAKE_CASE = Trainer( model=__UpperCAmelCase , args=__UpperCAmelCase , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=__UpperCAmelCase , tokenizer=__UpperCAmelCase , data_collator=__UpperCAmelCase , ) # Training if training_args.do_train: __SCREAMING_SNAKE_CASE = None if training_args.resume_from_checkpoint is not None: __SCREAMING_SNAKE_CASE = training_args.resume_from_checkpoint elif last_checkpoint is not None: __SCREAMING_SNAKE_CASE = last_checkpoint __SCREAMING_SNAKE_CASE = trainer.train(resume_from_checkpoint=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = train_result.metrics __SCREAMING_SNAKE_CASE = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(__UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = min(__UpperCAmelCase , len(__UpperCAmelCase ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics("""train""" , __UpperCAmelCase ) trainer.save_metrics("""train""" , __UpperCAmelCase ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info("""*** Evaluate ***""" ) __SCREAMING_SNAKE_CASE = trainer.evaluate(eval_dataset=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = min(__UpperCAmelCase , len(__UpperCAmelCase ) ) trainer.log_metrics("""eval""" , __UpperCAmelCase ) trainer.save_metrics("""eval""" , __UpperCAmelCase ) if training_args.do_predict: logger.info("""*** Predict ***""" ) # Removing the `label` columns because it contains -1 and Trainer won't like that. __SCREAMING_SNAKE_CASE = predict_dataset.remove_columns("""label""" ) __SCREAMING_SNAKE_CASE = trainer.predict(__UpperCAmelCase , metric_key_prefix="""predict""" ).predictions __SCREAMING_SNAKE_CASE = np.argmax(__UpperCAmelCase , axis=1 ) __SCREAMING_SNAKE_CASE = os.path.join(training_args.output_dir , """predict_results_tabfact.txt""" ) if trainer.is_world_process_zero(): with open(__UpperCAmelCase , """w""" ) as writer: logger.info("""***** Predict Results *****""" ) writer.write("""index\tprediction\n""" ) for index, item in enumerate(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = label_list[item] writer.write(f"""{index}\t{item}\n""" ) __SCREAMING_SNAKE_CASE = {"""finetuned_from""": model_args.model_name_or_path, """tasks""": """text-classification"""} if training_args.push_to_hub: trainer.push_to_hub(**__UpperCAmelCase ) else: trainer.create_model_card(**__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase ) -> Any: '''simple docstring''' main() if __name__ == "__main__": main()
13
1
'''simple docstring''' import collections import importlib.util import os import re from pathlib import Path a = "src/transformers" # Matches is_xxx_available() a = re.compile(r"is\_([a-z_]*)_available()") # Catches a one-line _import_struct = {xxx} a = re.compile(r"^_import_structure\s+=\s+\{([^\}]+)\}") # Catches a line with a key-values pattern: "bla": ["foo", "bar"] a = re.compile(r"\s+\"\S*\":\s+\[([^\]]*)\]") # Catches a line if not is_foo_available a = re.compile(r"^\s*if\s+not\s+is\_[a-z_]*\_available\(\)") # Catches a line _import_struct["bla"].append("foo") a = re.compile(r"^\s*_import_structure\[\"\S*\"\]\.append\(\"(\S*)\"\)") # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] a = re.compile(r"^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]") # Catches a line with an object between quotes and a comma: "MyModel", a = re.compile("^\s+\"([^\"]+)\",") # Catches a line with objects between brackets only: ["foo", "bar"], a = re.compile("^\s+\[([^\]]+)\]") # Catches a line with from foo import bar, bla, boo a = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") # Catches a line with try: a = re.compile(r"^\s*try:") # Catches a line with else: a = re.compile(r"^\s*else:") def __magic_name__ ( __UpperCAmelCase ) -> Tuple: '''simple docstring''' if _re_test_backend.search(__UpperCAmelCase ) is None: return None __SCREAMING_SNAKE_CASE = [b[0] for b in _re_backend.findall(__UpperCAmelCase )] backends.sort() return "_and_".join(__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase ) -> Optional[int]: '''simple docstring''' with open(__UpperCAmelCase , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: __SCREAMING_SNAKE_CASE = f.readlines() __SCREAMING_SNAKE_CASE = 0 while line_index < len(__UpperCAmelCase ) and not lines[line_index].startswith("""_import_structure = {""" ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(__UpperCAmelCase ): return None # First grab the objects without a specific backend in _import_structure __SCREAMING_SNAKE_CASE = [] while not lines[line_index].startswith("""if TYPE_CHECKING""" ) and find_backend(lines[line_index] ) is None: __SCREAMING_SNAKE_CASE = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = _re_one_line_import_struct.search(__UpperCAmelCase ).groups()[0] __SCREAMING_SNAKE_CASE = re.findall("""\[([^\]]+)\]""" , __UpperCAmelCase ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(""", """ )] ) line_index += 1 continue __SCREAMING_SNAKE_CASE = _re_import_struct_key_value.search(__UpperCAmelCase ) if single_line_import_search is not None: __SCREAMING_SNAKE_CASE = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(""", """ ) if len(__UpperCAmelCase ) > 0] objects.extend(__UpperCAmelCase ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) line_index += 1 __SCREAMING_SNAKE_CASE = {"""none""": objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith("""if TYPE_CHECKING""" ): # If the line is an if not is_backend_available, we grab all objects associated. __SCREAMING_SNAKE_CASE = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: __SCREAMING_SNAKE_CASE = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 __SCREAMING_SNAKE_CASE = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 4 ): __SCREAMING_SNAKE_CASE = lines[line_index] if _re_import_struct_add_one.search(__UpperCAmelCase ) is not None: objects.append(_re_import_struct_add_one.search(__UpperCAmelCase ).groups()[0] ) elif _re_import_struct_add_many.search(__UpperCAmelCase ) is not None: __SCREAMING_SNAKE_CASE = _re_import_struct_add_many.search(__UpperCAmelCase ).groups()[0].split(""", """ ) __SCREAMING_SNAKE_CASE = [obj[1:-1] for obj in imports if len(__UpperCAmelCase ) > 0] objects.extend(__UpperCAmelCase ) elif _re_between_brackets.search(__UpperCAmelCase ) is not None: __SCREAMING_SNAKE_CASE = _re_between_brackets.search(__UpperCAmelCase ).groups()[0].split(""", """ ) __SCREAMING_SNAKE_CASE = [obj[1:-1] for obj in imports if len(__UpperCAmelCase ) > 0] objects.extend(__UpperCAmelCase ) elif _re_quote_object.search(__UpperCAmelCase ) is not None: objects.append(_re_quote_object.search(__UpperCAmelCase ).groups()[0] ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) elif line.startswith(""" """ * 12 + """\"""" ): objects.append(line[13:-3] ) line_index += 1 __SCREAMING_SNAKE_CASE = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend __SCREAMING_SNAKE_CASE = [] while ( line_index < len(__UpperCAmelCase ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith("""else""" ) ): __SCREAMING_SNAKE_CASE = lines[line_index] __SCREAMING_SNAKE_CASE = _re_import.search(__UpperCAmelCase ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 8 ): objects.append(line[8:-2] ) line_index += 1 __SCREAMING_SNAKE_CASE = {"""none""": objects} # Let's continue with backend-specific objects while line_index < len(__UpperCAmelCase ): # If the line is an if is_backend_available, we grab all objects associated. __SCREAMING_SNAKE_CASE = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: __SCREAMING_SNAKE_CASE = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 __SCREAMING_SNAKE_CASE = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 8 ): __SCREAMING_SNAKE_CASE = lines[line_index] __SCREAMING_SNAKE_CASE = _re_import.search(__UpperCAmelCase ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 12 ): objects.append(line[12:-2] ) line_index += 1 __SCREAMING_SNAKE_CASE = objects else: line_index += 1 return import_dict_objects, type_hint_objects def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Tuple: '''simple docstring''' def find_duplicates(__UpperCAmelCase ): return [k for k, v in collections.Counter(__UpperCAmelCase ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] __SCREAMING_SNAKE_CASE = [] for key in import_dict_objects.keys(): __SCREAMING_SNAKE_CASE = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(f"""Duplicate _import_structure definitions for: {duplicate_imports}""" ) __SCREAMING_SNAKE_CASE = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(f"""Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}""" ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): __SCREAMING_SNAKE_CASE = """base imports""" if key == """none""" else f"""{key} backend""" errors.append(f"""Differences for {name}:""" ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(f""" {a} in TYPE_HINT but not in _import_structure.""" ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(f""" {a} in _import_structure but not in TYPE_HINT.""" ) return errors def __magic_name__ ( ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] for root, _, files in os.walk(__UpperCAmelCase ): if "__init__.py" in files: __SCREAMING_SNAKE_CASE = os.path.join(__UpperCAmelCase , """__init__.py""" ) __SCREAMING_SNAKE_CASE = parse_init(__UpperCAmelCase ) if objects is not None: __SCREAMING_SNAKE_CASE = analyze_results(*__UpperCAmelCase ) if len(__UpperCAmelCase ) > 0: __SCREAMING_SNAKE_CASE = f"""Problem in {fname}, both halves do not define the same objects.\n{errors[0]}""" failures.append("""\n""".join(__UpperCAmelCase ) ) if len(__UpperCAmelCase ) > 0: raise ValueError("""\n\n""".join(__UpperCAmelCase ) ) def __magic_name__ ( ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] for path, directories, files in os.walk(__UpperCAmelCase ): for folder in directories: # Ignore private modules if folder.startswith("""_""" ): directories.remove(__UpperCAmelCase ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(__UpperCAmelCase ) / folder).glob("""*.py""" ) ) ) == 0: continue __SCREAMING_SNAKE_CASE = str((Path(__UpperCAmelCase ) / folder).relative_to(__UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = short_path.replace(os.path.sep , """.""" ) submodules.append(__UpperCAmelCase ) for fname in files: if fname == "__init__.py": continue __SCREAMING_SNAKE_CASE = str((Path(__UpperCAmelCase ) / fname).relative_to(__UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = short_path.replace(""".py""" , """""" ).replace(os.path.sep , """.""" ) if len(submodule.split(""".""" ) ) == 1: submodules.append(__UpperCAmelCase ) return submodules a = [ "convert_pytorch_checkpoint_to_tf2", "modeling_flax_pytorch_utils", ] def __magic_name__ ( ) -> Tuple: '''simple docstring''' __SCREAMING_SNAKE_CASE = importlib.util.spec_from_file_location( """transformers""" , os.path.join(__UpperCAmelCase , """__init__.py""" ) , submodule_search_locations=[PATH_TO_TRANSFORMERS] , ) __SCREAMING_SNAKE_CASE = spec.loader.load_module() __SCREAMING_SNAKE_CASE = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in transformers._import_structure.keys() ] if len(__UpperCAmelCase ) > 0: __SCREAMING_SNAKE_CASE = """\n""".join(f"""- {module}""" for module in module_not_registered ) raise ValueError( """The following submodules are not properly registered in the main init of Transformers:\n""" f"""{list_of_modules}\n""" """Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value.""" ) if __name__ == "__main__": check_all_inits() check_submodules()
13
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_flax_available, is_torch_available, is_transformers_available, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .multicontrolnet import MultiControlNetModel from .pipeline_controlnet import StableDiffusionControlNetPipeline from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline if is_transformers_available() and is_flax_available(): from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline
13
1
'''simple docstring''' import warnings from ...utils import logging from .image_processing_poolformer import PoolFormerImageProcessor a = logging.get_logger(__name__) class __a ( _snake_case ): def __init__( self : Optional[Any] ,*lowerCamelCase : List[str] ,**lowerCamelCase : List[str] ): '''simple docstring''' warnings.warn( """The class PoolFormerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use PoolFormerImageProcessor instead.""" ,lowerCamelCase ,) super().__init__(*lowerCamelCase ,**lowerCamelCase )
13
'''simple docstring''' import requests from bsa import BeautifulSoup def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = BeautifulSoup(requests.get(__UpperCAmelCase , params=__UpperCAmelCase ).content , """html.parser""" ) __SCREAMING_SNAKE_CASE = soup.find("""div""" , attrs={"""class""": """gs_ri"""} ) __SCREAMING_SNAKE_CASE = div.find("""div""" , attrs={"""class""": """gs_fl"""} ).find_all("""a""" ) return anchors[2].get_text() if __name__ == "__main__": a = { "title": ( "Precisely geometry controlled microsupercapacitors for ultrahigh areal " "capacitance, volumetric capacitance, and energy density" ), "journal": "Chem. Mater.", "volume": 30, "pages": "3979-3990", "year": 2018, "hl": "en", } print(get_citation("https://scholar.google.com/scholar_lookup", params=params))
13
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a = { "configuration_clipseg": [ "CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP", "CLIPSegConfig", "CLIPSegTextConfig", "CLIPSegVisionConfig", ], "processing_clipseg": ["CLIPSegProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = [ "CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST", "CLIPSegModel", "CLIPSegPreTrainedModel", "CLIPSegTextModel", "CLIPSegVisionModel", "CLIPSegForImageSegmentation", ] if TYPE_CHECKING: from .configuration_clipseg import ( CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPSegConfig, CLIPSegTextConfig, CLIPSegVisionConfig, ) from .processing_clipseg import CLIPSegProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clipseg import ( CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPSegForImageSegmentation, CLIPSegModel, CLIPSegPreTrainedModel, CLIPSegTextModel, CLIPSegVisionModel, ) else: import sys a = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
13
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a = logging.get_logger(__name__) a = { "camembert-base": "https://huggingface.co/camembert-base/resolve/main/config.json", "umberto-commoncrawl-cased-v1": ( "https://huggingface.co/Musixmatch/umberto-commoncrawl-cased-v1/resolve/main/config.json" ), "umberto-wikipedia-uncased-v1": ( "https://huggingface.co/Musixmatch/umberto-wikipedia-uncased-v1/resolve/main/config.json" ), } class __a ( _snake_case ): __UpperCamelCase : Tuple = 'camembert' def __init__( self : int ,lowerCamelCase : List[Any]=3_0522 ,lowerCamelCase : List[Any]=768 ,lowerCamelCase : str=12 ,lowerCamelCase : List[str]=12 ,lowerCamelCase : Optional[Any]=3072 ,lowerCamelCase : Tuple="gelu" ,lowerCamelCase : List[str]=0.1 ,lowerCamelCase : Tuple=0.1 ,lowerCamelCase : Union[str, Any]=512 ,lowerCamelCase : Dict=2 ,lowerCamelCase : Tuple=0.02 ,lowerCamelCase : List[Any]=1E-1_2 ,lowerCamelCase : Union[str, Any]=1 ,lowerCamelCase : Optional[Any]=0 ,lowerCamelCase : List[Any]=2 ,lowerCamelCase : List[str]="absolute" ,lowerCamelCase : int=True ,lowerCamelCase : Any=None ,**lowerCamelCase : Optional[Any] ,): '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase ,bos_token_id=lowerCamelCase ,eos_token_id=lowerCamelCase ,**lowerCamelCase ) __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 = position_embedding_type __SCREAMING_SNAKE_CASE = use_cache __SCREAMING_SNAKE_CASE = classifier_dropout class __a ( _snake_case ): @property def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' 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), ] )
13
1
'''simple docstring''' import logging from transformers.configuration_utils import PretrainedConfig a = logging.getLogger(__name__) class __a ( _snake_case ): __UpperCamelCase : List[Any] = 'masked_bert' def __init__( self : Optional[int] ,lowerCamelCase : Dict=3_0522 ,lowerCamelCase : List[Any]=768 ,lowerCamelCase : Optional[int]=12 ,lowerCamelCase : Tuple=12 ,lowerCamelCase : Tuple=3072 ,lowerCamelCase : Dict="gelu" ,lowerCamelCase : int=0.1 ,lowerCamelCase : List[str]=0.1 ,lowerCamelCase : Optional[Any]=512 ,lowerCamelCase : Optional[Any]=2 ,lowerCamelCase : Tuple=0.02 ,lowerCamelCase : Optional[int]=1E-1_2 ,lowerCamelCase : Optional[int]=0 ,lowerCamelCase : List[Any]="topK" ,lowerCamelCase : Union[str, Any]="constant" ,lowerCamelCase : List[Any]=0.0 ,**lowerCamelCase : Tuple ,): '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase ,**lowerCamelCase ) __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 = pruning_method __SCREAMING_SNAKE_CASE = mask_init __SCREAMING_SNAKE_CASE = mask_scale
13
'''simple docstring''' import inspect import unittest import numpy as np from transformers import ViTConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor if is_flax_available(): import jax from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel class __a ( unittest.TestCase ): def __init__( self : Optional[int] ,lowerCamelCase : str ,lowerCamelCase : List[str]=13 ,lowerCamelCase : Optional[Any]=30 ,lowerCamelCase : Dict=2 ,lowerCamelCase : List[Any]=3 ,lowerCamelCase : List[str]=True ,lowerCamelCase : str=True ,lowerCamelCase : Optional[int]=32 ,lowerCamelCase : Dict=5 ,lowerCamelCase : Optional[int]=4 ,lowerCamelCase : List[Any]=37 ,lowerCamelCase : Union[str, Any]="gelu" ,lowerCamelCase : List[Any]=0.1 ,lowerCamelCase : Any=0.1 ,lowerCamelCase : str=10 ,lowerCamelCase : Dict=0.02 ,): '''simple docstring''' __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 # 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 UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __SCREAMING_SNAKE_CASE = 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 ,) return config, pixel_values def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : int ,lowerCamelCase : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = FlaxViTModel(config=lowerCamelCase ) __SCREAMING_SNAKE_CASE = model(lowerCamelCase ) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) __SCREAMING_SNAKE_CASE = (self.image_size, self.image_size) __SCREAMING_SNAKE_CASE = (self.patch_size, self.patch_size) __SCREAMING_SNAKE_CASE = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, num_patches + 1, self.hidden_size) ) def UpperCAmelCase__ ( self : Union[str, Any] ,lowerCamelCase : Optional[int] ,lowerCamelCase : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.type_sequence_label_size __SCREAMING_SNAKE_CASE = FlaxViTForImageClassification(config=lowerCamelCase ) __SCREAMING_SNAKE_CASE = model(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 = FlaxViTForImageClassification(lowerCamelCase ) __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) __SCREAMING_SNAKE_CASE = model(lowerCamelCase ) def UpperCAmelCase__ ( self : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ) = config_and_inputs __SCREAMING_SNAKE_CASE = {"""pixel_values""": pixel_values} return config, inputs_dict @require_flax class __a ( _snake_case, unittest.TestCase ): __UpperCamelCase : Any = (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else () def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = FlaxViTModelTester(self ) __SCREAMING_SNAKE_CASE = ConfigTester(self ,config_class=lowerCamelCase ,has_text_modality=lowerCamelCase ,hidden_size=37 ) def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __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 UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __SCREAMING_SNAKE_CASE = self._prepare_for_class(lowerCamelCase ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = model_class(lowerCamelCase ) @jax.jit def model_jitted(lowerCamelCase : int ,**lowerCamelCase : Union[str, Any] ): return model(pixel_values=lowerCamelCase ,**lowerCamelCase ) with self.subTest("""JIT Enabled""" ): __SCREAMING_SNAKE_CASE = model_jitted(**lowerCamelCase ).to_tuple() with self.subTest("""JIT Disabled""" ): with jax.disable_jit(): __SCREAMING_SNAKE_CASE = model_jitted(**lowerCamelCase ).to_tuple() self.assertEqual(len(lowerCamelCase ) ,len(lowerCamelCase ) ) for jitted_output, output in zip(lowerCamelCase ,lowerCamelCase ): self.assertEqual(jitted_output.shape ,output.shape ) @slow def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' for model_class_name in self.all_model_classes: __SCREAMING_SNAKE_CASE = model_class_name.from_pretrained("""google/vit-base-patch16-224""" ) __SCREAMING_SNAKE_CASE = model(np.ones((1, 3, 224, 224) ) ) self.assertIsNotNone(lowerCamelCase )
13
1
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase ) -> str: '''simple docstring''' if isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError("""'float' object cannot be interpreted as an integer""" ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError("""'str' object cannot be interpreted as an integer""" ) if num == 0: return "0b0" __SCREAMING_SNAKE_CASE = False if num < 0: __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = -num __SCREAMING_SNAKE_CASE = [] while num > 0: binary.insert(0 , num % 2 ) num >>= 1 if negative: return "-0b" + "".join(str(__UpperCAmelCase ) for e in binary ) return "0b" + "".join(str(__UpperCAmelCase ) for e in binary ) if __name__ == "__main__": import doctest doctest.testmod()
13
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging a = logging.get_logger(__name__) a = { "asapp/sew-tiny-100k": "https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json", # See all SEW models at https://huggingface.co/models?filter=sew } class __a ( _snake_case ): __UpperCamelCase : Tuple = 'sew' def __init__( self : str ,lowerCamelCase : Any=32 ,lowerCamelCase : str=768 ,lowerCamelCase : str=12 ,lowerCamelCase : Union[str, Any]=12 ,lowerCamelCase : Union[str, Any]=3072 ,lowerCamelCase : int=2 ,lowerCamelCase : Union[str, Any]="gelu" ,lowerCamelCase : Tuple=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : Any=0.0 ,lowerCamelCase : Optional[Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : Optional[Any]=0.02 ,lowerCamelCase : List[str]=1E-5 ,lowerCamelCase : Tuple="group" ,lowerCamelCase : Optional[Any]="gelu" ,lowerCamelCase : List[str]=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) ,lowerCamelCase : Any=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) ,lowerCamelCase : Dict=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) ,lowerCamelCase : Optional[int]=False ,lowerCamelCase : Dict=128 ,lowerCamelCase : Union[str, Any]=16 ,lowerCamelCase : List[Any]=True ,lowerCamelCase : List[Any]=0.05 ,lowerCamelCase : Optional[int]=10 ,lowerCamelCase : Any=2 ,lowerCamelCase : Any=0.0 ,lowerCamelCase : Tuple=10 ,lowerCamelCase : str=0 ,lowerCamelCase : Tuple="mean" ,lowerCamelCase : int=False ,lowerCamelCase : Dict=False ,lowerCamelCase : Optional[int]=256 ,lowerCamelCase : str=0 ,lowerCamelCase : Tuple=1 ,lowerCamelCase : Tuple=2 ,**lowerCamelCase : Union[str, Any] ,): '''simple docstring''' super().__init__(**lowerCamelCase ,pad_token_id=lowerCamelCase ,bos_token_id=lowerCamelCase ,eos_token_id=lowerCamelCase ) __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = feat_extract_norm __SCREAMING_SNAKE_CASE = feat_extract_activation __SCREAMING_SNAKE_CASE = list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = conv_bias __SCREAMING_SNAKE_CASE = num_conv_pos_embeddings __SCREAMING_SNAKE_CASE = num_conv_pos_embedding_groups __SCREAMING_SNAKE_CASE = len(self.conv_dim ) __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = squeeze_factor __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = hidden_dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = feat_proj_dropout __SCREAMING_SNAKE_CASE = final_dropout __SCREAMING_SNAKE_CASE = layerdrop __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect.""" """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,""" f"""but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)""" f"""= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __SCREAMING_SNAKE_CASE = apply_spec_augment __SCREAMING_SNAKE_CASE = mask_time_prob __SCREAMING_SNAKE_CASE = mask_time_length __SCREAMING_SNAKE_CASE = mask_time_min_masks __SCREAMING_SNAKE_CASE = mask_feature_prob __SCREAMING_SNAKE_CASE = mask_feature_length __SCREAMING_SNAKE_CASE = mask_feature_min_masks # ctc loss __SCREAMING_SNAKE_CASE = ctc_loss_reduction __SCREAMING_SNAKE_CASE = ctc_zero_infinity # sequence classification __SCREAMING_SNAKE_CASE = use_weighted_layer_sum __SCREAMING_SNAKE_CASE = classifier_proj_size @property def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' return functools.reduce(operator.mul ,self.conv_stride ,1 )
13
1
'''simple docstring''' 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 ): def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' 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 UpperCAmelCase__ ( self : Optional[int] ,lowerCamelCase : str ,lowerCamelCase : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = np.array(lowerCamelCase ) __SCREAMING_SNAKE_CASE = np.array(lowerCamelCase ) # 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 __SCREAMING_SNAKE_CASE = X - np.mean(lowerCamelCase ) __SCREAMING_SNAKE_CASE = np.cov(reference_distribution.T ) try: __SCREAMING_SNAKE_CASE = np.linalg.inv(lowerCamelCase ) except np.linalg.LinAlgError: __SCREAMING_SNAKE_CASE = np.linalg.pinv(lowerCamelCase ) __SCREAMING_SNAKE_CASE = np.dot(lowerCamelCase ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = np.dot(lowerCamelCase ,X_minus_mu.T ).diagonal() return {"mahalanobis": mahal_dist}
13
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase = 1 , __UpperCAmelCase = 1000 ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 0 for divide_by_number in range(__UpperCAmelCase , digit + 1 ): __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = numerator for _ in range(1 , digit + 1 ): if now_divide in has_been_divided: if longest_list_length < len(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = divide_by_number else: has_been_divided.append(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = now_divide * 10 % divide_by_number return the_digit # Tests if __name__ == "__main__": import doctest doctest.testmod()
13
1
'''simple docstring''' from __future__ import annotations def __magic_name__ ( __UpperCAmelCase ) -> bool: '''simple docstring''' __SCREAMING_SNAKE_CASE = str(__UpperCAmelCase ) return len(__UpperCAmelCase ) == 9 and set(__UpperCAmelCase ) == set("""123456789""" ) def __magic_name__ ( ) -> int | None: '''simple docstring''' for base_num in range(9999 , 4999 , -1 ): __SCREAMING_SNAKE_CASE = 100002 * base_num if is_9_pandigital(__UpperCAmelCase ): return candidate for base_num in range(333 , 99 , -1 ): __SCREAMING_SNAKE_CASE = 1002003 * base_num if is_9_pandigital(__UpperCAmelCase ): return candidate return None if __name__ == "__main__": print(F'''{solution() = }''')
13
'''simple docstring''' 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 if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor class __a ( unittest.TestCase ): def __init__( self : List[Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : List[str]=7 ,lowerCamelCase : List[str]=3 ,lowerCamelCase : List[str]=18 ,lowerCamelCase : Any=30 ,lowerCamelCase : Optional[Any]=400 ,lowerCamelCase : Optional[Any]=True ,lowerCamelCase : Optional[Any]=None ,lowerCamelCase : Optional[int]=True ,lowerCamelCase : int=None ,lowerCamelCase : str=True ,lowerCamelCase : Dict=[0.48_145_466, 0.4_578_275, 0.40_821_073] ,lowerCamelCase : List[str]=[0.26_862_954, 0.26_130_258, 0.27_577_711] ,lowerCamelCase : Tuple=True ,): '''simple docstring''' __SCREAMING_SNAKE_CASE = size if size is not None else {"""height""": 224, """width""": 224} __SCREAMING_SNAKE_CASE = crop_size if crop_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 = do_center_crop __SCREAMING_SNAKE_CASE = crop_size __SCREAMING_SNAKE_CASE = do_normalize __SCREAMING_SNAKE_CASE = image_mean __SCREAMING_SNAKE_CASE = image_std __SCREAMING_SNAKE_CASE = do_convert_rgb def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_convert_rgb": self.do_convert_rgb, } def UpperCAmelCase__ ( self : int ,lowerCamelCase : Union[str, Any]=False ,lowerCamelCase : str=False ,lowerCamelCase : str=False ): '''simple docstring''' assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time" if equal_resolution: __SCREAMING_SNAKE_CASE = [] for i in range(self.batch_size ): image_inputs.append( np.random.randint( 255 ,size=(self.num_channels, self.max_resolution, self.max_resolution) ,dtype=np.uinta ) ) else: __SCREAMING_SNAKE_CASE = [] for i in range(self.batch_size ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = np.random.choice(np.arange(self.min_resolution ,self.max_resolution ) ,2 ) image_inputs.append(np.random.randint(255 ,size=(self.num_channels, width, height) ,dtype=np.uinta ) ) if not numpify and not torchify: # PIL expects the channel dimension as last dimension __SCREAMING_SNAKE_CASE = [Image.fromarray(np.moveaxis(lowerCamelCase ,0 ,-1 ) ) for x in image_inputs] if torchify: __SCREAMING_SNAKE_CASE = [torch.from_numpy(lowerCamelCase ) for x in image_inputs] return image_inputs @require_torch @require_vision class __a ( _snake_case, unittest.TestCase ): __UpperCamelCase : int = ChineseCLIPImageProcessor if is_vision_available() else None def UpperCAmelCase__ ( self : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = ChineseCLIPImageProcessingTester(self ,do_center_crop=lowerCamelCase ) @property def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __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 ,"""do_center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_normalize""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_mean""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_std""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_convert_rgb""" ) ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{"""height""": 224, """width""": 224} ) self.assertEqual(image_processor.crop_size ,{"""height""": 18, """width""": 18} ) __SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ,crop_size=84 ) self.assertEqual(image_processor.size ,{"""shortest_edge""": 42} ) self.assertEqual(image_processor.crop_size ,{"""height""": 84, """width""": 84} ) def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' pass def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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""" ).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 __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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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.crop_size["""height"""], self.image_processor_tester.crop_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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def UpperCAmelCase__ ( self : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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.crop_size["""height"""], self.image_processor_tester.crop_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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) @require_torch @require_vision class __a ( _snake_case, unittest.TestCase ): __UpperCamelCase : Optional[int] = ChineseCLIPImageProcessor if is_vision_available() else None def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = ChineseCLIPImageProcessingTester(self ,num_channels=4 ,do_center_crop=lowerCamelCase ) __SCREAMING_SNAKE_CASE = 3 @property def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase__ ( self : int ): '''simple docstring''' __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 ,"""do_center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_normalize""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_mean""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_std""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_convert_rgb""" ) ) def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' pass def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_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.expected_encoded_image_num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,)
13
1
'''simple docstring''' from sklearn.metrics import matthews_corrcoef import datasets a = "\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction. The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n" a = "\nArgs:\n predictions (list of int): Predicted labels, as returned by a model.\n references (list of int): Ground truth labels.\n sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n Example 1, a basic example with only predictions and references as inputs:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3])\n >>> print(round(results['matthews_correlation'], 2))\n 0.54\n\n Example 2, the same example as above, but also including sample weights:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 3, 1, 1, 1, 2])\n >>> print(round(results['matthews_correlation'], 2))\n 0.1\n\n Example 3, the same example as above, but with sample weights that cause a negative correlation:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 1, 0, 0, 0, 1])\n >>> print(round(results['matthews_correlation'], 2))\n -0.25\n" a = "\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class __a ( datasets.Metric ): def UpperCAmelCase__ ( self : str ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) ,reference_urls=[ """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html""" ] ,) def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : Any ,lowerCamelCase : str ,lowerCamelCase : Tuple=None ): '''simple docstring''' return { "matthews_correlation": float(matthews_corrcoef(lowerCamelCase ,lowerCamelCase ,sample_weight=lowerCamelCase ) ), }
13
'''simple docstring''' import timeit import numpy as np import datasets from datasets.arrow_writer import ArrowWriter from datasets.features.features import _ArrayXD def __magic_name__ ( __UpperCAmelCase ) -> Tuple: '''simple docstring''' def wrapper(*__UpperCAmelCase , **__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = timeit.default_timer() __SCREAMING_SNAKE_CASE = func(*__UpperCAmelCase , **__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = timeit.default_timer() - starttime return delta __SCREAMING_SNAKE_CASE = func.__name__ return wrapper def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase=100 , __UpperCAmelCase=None ) -> Optional[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = seq_shapes or {} for i in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = {} for col_id, (k, v) in enumerate(features.items() ): if isinstance(__UpperCAmelCase , _ArrayXD ): __SCREAMING_SNAKE_CASE = np.random.rand(*v.shape ).astype(v.dtype ) elif isinstance(__UpperCAmelCase , datasets.Value ): if v.dtype == "string": __SCREAMING_SNAKE_CASE = """The small grey turtle was surprisingly fast when challenged.""" else: __SCREAMING_SNAKE_CASE = np.random.randint(10 , size=1 ).astype(v.dtype ).item() elif isinstance(__UpperCAmelCase , datasets.Sequence ): while isinstance(__UpperCAmelCase , datasets.Sequence ): __SCREAMING_SNAKE_CASE = v.feature __SCREAMING_SNAKE_CASE = seq_shapes[k] __SCREAMING_SNAKE_CASE = np.random.rand(*__UpperCAmelCase ).astype(v.dtype ) __SCREAMING_SNAKE_CASE = data dummy_data.append((i, example) ) return dummy_data def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=100 , __UpperCAmelCase=None ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = generate_examples(__UpperCAmelCase , num_examples=__UpperCAmelCase , seq_shapes=__UpperCAmelCase ) with ArrowWriter(features=__UpperCAmelCase , path=__UpperCAmelCase ) as writer: for key, record in dummy_data: __SCREAMING_SNAKE_CASE = features.encode_example(__UpperCAmelCase ) writer.write(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = writer.finalize() if not num_final_examples == num_examples: raise ValueError( f"""Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}.""" ) __SCREAMING_SNAKE_CASE = datasets.Dataset.from_file(filename=__UpperCAmelCase , info=datasets.DatasetInfo(features=__UpperCAmelCase ) ) return dataset
13
1
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_convbert import ConvBertTokenizer a = logging.get_logger(__name__) a = {"vocab_file": "vocab.txt"} a = { "vocab_file": { "YituTech/conv-bert-base": "https://huggingface.co/YituTech/conv-bert-base/resolve/main/vocab.txt", "YituTech/conv-bert-medium-small": ( "https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/vocab.txt" ), "YituTech/conv-bert-small": "https://huggingface.co/YituTech/conv-bert-small/resolve/main/vocab.txt", } } a = { "YituTech/conv-bert-base": 512, "YituTech/conv-bert-medium-small": 512, "YituTech/conv-bert-small": 512, } a = { "YituTech/conv-bert-base": {"do_lower_case": True}, "YituTech/conv-bert-medium-small": {"do_lower_case": True}, "YituTech/conv-bert-small": {"do_lower_case": True}, } class __a ( _snake_case ): __UpperCamelCase : List[Any] = VOCAB_FILES_NAMES __UpperCamelCase : str = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Dict = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : List[Any] = ConvBertTokenizer def __init__( self : List[Any] ,lowerCamelCase : Dict=None ,lowerCamelCase : str=None ,lowerCamelCase : str=True ,lowerCamelCase : Any="[UNK]" ,lowerCamelCase : List[str]="[SEP]" ,lowerCamelCase : Union[str, Any]="[PAD]" ,lowerCamelCase : List[str]="[CLS]" ,lowerCamelCase : Union[str, Any]="[MASK]" ,lowerCamelCase : Optional[Any]=True ,lowerCamelCase : Dict=None ,**lowerCamelCase : Union[str, Any] ,): '''simple docstring''' super().__init__( lowerCamelCase ,tokenizer_file=lowerCamelCase ,do_lower_case=lowerCamelCase ,unk_token=lowerCamelCase ,sep_token=lowerCamelCase ,pad_token=lowerCamelCase ,cls_token=lowerCamelCase ,mask_token=lowerCamelCase ,tokenize_chinese_chars=lowerCamelCase ,strip_accents=lowerCamelCase ,**lowerCamelCase ,) __SCREAMING_SNAKE_CASE = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("""lowercase""" ,lowerCamelCase ) != do_lower_case or normalizer_state.get("""strip_accents""" ,lowerCamelCase ) != strip_accents or normalizer_state.get("""handle_chinese_chars""" ,lowerCamelCase ) != tokenize_chinese_chars ): __SCREAMING_SNAKE_CASE = getattr(lowerCamelCase ,normalizer_state.pop("""type""" ) ) __SCREAMING_SNAKE_CASE = do_lower_case __SCREAMING_SNAKE_CASE = strip_accents __SCREAMING_SNAKE_CASE = tokenize_chinese_chars __SCREAMING_SNAKE_CASE = normalizer_class(**lowerCamelCase ) __SCREAMING_SNAKE_CASE = do_lower_case def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : List[str] ,lowerCamelCase : List[Any]=None ): '''simple docstring''' __SCREAMING_SNAKE_CASE = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def UpperCAmelCase__ ( self : Union[str, Any] ,lowerCamelCase : List[int] ,lowerCamelCase : Optional[List[int]] = None ): '''simple docstring''' __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 UpperCAmelCase__ ( self : Any ,lowerCamelCase : str ,lowerCamelCase : Optional[str] = None ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self._tokenizer.model.save(lowerCamelCase ,name=lowerCamelCase ) return tuple(lowerCamelCase )
13
'''simple docstring''' import time from contextlib import contextmanager from pathlib import Path import pytest import requests from huggingface_hub.hf_api import HfApi, HfFolder a = "__DUMMY_TRANSFORMERS_USER__" a = "Dummy User" a = "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" a = "https://hub-ci.huggingface.co" a = CI_HUB_ENDPOINT + "/datasets/{repo_id}/resolve/{revision}/{path}" a = CI_HUB_ENDPOINT + "/{repo_id}/resolve/{revision}/{filename}" a = Path("~/.huggingface/hub_ci_token").expanduser() @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' monkeypatch.setattr( """huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE""" , __UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' monkeypatch.setattr("""datasets.config.HF_ENDPOINT""" , __UpperCAmelCase ) monkeypatch.setattr("""datasets.config.HUB_DATASETS_URL""" , __UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' monkeypatch.setattr("""huggingface_hub.hf_api.HfFolder.path_token""" , __UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' HfFolder.save_token(__UpperCAmelCase ) yield HfFolder.delete_token() @pytest.fixture(scope="""session""" ) def __magic_name__ ( ) -> Optional[Any]: '''simple docstring''' return HfApi(endpoint=__UpperCAmelCase ) @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = HfFolder.get_token() HfFolder.save_token(__UpperCAmelCase ) yield CI_HUB_USER_TOKEN if previous_token is not None: HfFolder.save_token(__UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> Dict: '''simple docstring''' def _cleanup_repo(__UpperCAmelCase ): hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) return _cleanup_repo @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' @contextmanager def _temporary_repo(__UpperCAmelCase ): try: yield repo_id finally: cleanup_repo(__UpperCAmelCase ) return _temporary_repo @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""repo_txt_data-{int(time.time() * 1_0e3 )}""" __SCREAMING_SNAKE_CASE = f"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" , private=__UpperCAmelCase ) hf_api.upload_file( token=__UpperCAmelCase , path_or_fileobj=str(__UpperCAmelCase ) , path_in_repo="""data/text_data.txt""" , repo_id=__UpperCAmelCase , repo_type="""dataset""" , ) yield repo_id try: hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' return hf_private_dataset_repo_txt_data_ @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""repo_zipped_txt_data-{int(time.time() * 1_0e3 )}""" __SCREAMING_SNAKE_CASE = f"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" , private=__UpperCAmelCase ) hf_api.upload_file( token=__UpperCAmelCase , path_or_fileobj=str(__UpperCAmelCase ) , path_in_repo="""data.zip""" , repo_id=__UpperCAmelCase , repo_type="""dataset""" , ) yield repo_id try: hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Tuple: '''simple docstring''' return hf_private_dataset_repo_zipped_txt_data_ @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""repo_zipped_img_data-{int(time.time() * 1_0e3 )}""" __SCREAMING_SNAKE_CASE = f"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" , private=__UpperCAmelCase ) hf_api.upload_file( token=__UpperCAmelCase , path_or_fileobj=str(__UpperCAmelCase ) , path_in_repo="""data.zip""" , repo_id=__UpperCAmelCase , repo_type="""dataset""" , ) yield repo_id try: hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' return hf_private_dataset_repo_zipped_img_data_
13
1
'''simple docstring''' import glob import os import random from string import ascii_lowercase, digits import cva import numpy as np # Parrameters a = (720, 1280) # Height, Width a = (0.4, 0.6) # if height or width lower than this scale, drop it. a = 1 / 100 a = "" a = "" a = "" a = 250 def __magic_name__ ( ) -> None: '''simple docstring''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = get_dataset(__UpperCAmelCase , __UpperCAmelCase ) for index in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = random.sample(range(len(__UpperCAmelCase ) ) , 4 ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = update_image_and_anno( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , filter_scale=__UpperCAmelCase , ) # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' __SCREAMING_SNAKE_CASE = random_chars(32 ) __SCREAMING_SNAKE_CASE = path.split(os.sep )[-1].rsplit(""".""" , 1 )[0] __SCREAMING_SNAKE_CASE = f"""{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}""" cva.imwrite(f"""{file_root}.jpg""" , __UpperCAmelCase , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(f"""Succeeded {index+1}/{NUMBER_IMAGES} with {file_name}""" ) __SCREAMING_SNAKE_CASE = [] for anno in new_annos: __SCREAMING_SNAKE_CASE = anno[3] - anno[1] __SCREAMING_SNAKE_CASE = anno[4] - anno[2] __SCREAMING_SNAKE_CASE = anno[1] + width / 2 __SCREAMING_SNAKE_CASE = anno[2] + height / 2 __SCREAMING_SNAKE_CASE = f"""{anno[0]} {x_center} {y_center} {width} {height}""" annos_list.append(__UpperCAmelCase ) with open(f"""{file_root}.txt""" , """w""" ) as outfile: outfile.write("""\n""".join(line for line in annos_list ) ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> tuple[list, list]: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = [] for label_file in glob.glob(os.path.join(__UpperCAmelCase , """*.txt""" ) ): __SCREAMING_SNAKE_CASE = label_file.split(os.sep )[-1].rsplit(""".""" , 1 )[0] with open(__UpperCAmelCase ) as in_file: __SCREAMING_SNAKE_CASE = in_file.readlines() __SCREAMING_SNAKE_CASE = os.path.join(__UpperCAmelCase , f"""{label_name}.jpg""" ) __SCREAMING_SNAKE_CASE = [] for obj_list in obj_lists: __SCREAMING_SNAKE_CASE = obj_list.rstrip("""\n""" ).split(""" """ ) __SCREAMING_SNAKE_CASE = float(obj[1] ) - float(obj[3] ) / 2 __SCREAMING_SNAKE_CASE = float(obj[2] ) - float(obj[4] ) / 2 __SCREAMING_SNAKE_CASE = float(obj[1] ) + float(obj[3] ) / 2 __SCREAMING_SNAKE_CASE = float(obj[2] ) + float(obj[4] ) / 2 boxes.append([int(obj[0] ), xmin, ymin, xmax, ymax] ) if not boxes: continue img_paths.append(__UpperCAmelCase ) labels.append(__UpperCAmelCase ) return img_paths, labels def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0.0 , ) -> tuple[list, list, str]: '''simple docstring''' __SCREAMING_SNAKE_CASE = np.zeros([output_size[0], output_size[1], 3] , dtype=np.uinta ) __SCREAMING_SNAKE_CASE = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) __SCREAMING_SNAKE_CASE = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) __SCREAMING_SNAKE_CASE = int(scale_x * output_size[1] ) __SCREAMING_SNAKE_CASE = int(scale_y * output_size[0] ) __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = [] for i, index in enumerate(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = all_img_list[index] path_list.append(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = all_annos[index] __SCREAMING_SNAKE_CASE = cva.imread(__UpperCAmelCase ) if i == 0: # top-left __SCREAMING_SNAKE_CASE = cva.resize(__UpperCAmelCase , (divid_point_x, divid_point_y) ) __SCREAMING_SNAKE_CASE = img for bbox in img_annos: __SCREAMING_SNAKE_CASE = bbox[1] * scale_x __SCREAMING_SNAKE_CASE = bbox[2] * scale_y __SCREAMING_SNAKE_CASE = bbox[3] * scale_x __SCREAMING_SNAKE_CASE = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) elif i == 1: # top-right __SCREAMING_SNAKE_CASE = cva.resize(__UpperCAmelCase , (output_size[1] - divid_point_x, divid_point_y) ) __SCREAMING_SNAKE_CASE = img for bbox in img_annos: __SCREAMING_SNAKE_CASE = scale_x + bbox[1] * (1 - scale_x) __SCREAMING_SNAKE_CASE = bbox[2] * scale_y __SCREAMING_SNAKE_CASE = scale_x + bbox[3] * (1 - scale_x) __SCREAMING_SNAKE_CASE = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) elif i == 2: # bottom-left __SCREAMING_SNAKE_CASE = cva.resize(__UpperCAmelCase , (divid_point_x, output_size[0] - divid_point_y) ) __SCREAMING_SNAKE_CASE = img for bbox in img_annos: __SCREAMING_SNAKE_CASE = bbox[1] * scale_x __SCREAMING_SNAKE_CASE = scale_y + bbox[2] * (1 - scale_y) __SCREAMING_SNAKE_CASE = bbox[3] * scale_x __SCREAMING_SNAKE_CASE = scale_y + bbox[4] * (1 - scale_y) new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) else: # bottom-right __SCREAMING_SNAKE_CASE = cva.resize( __UpperCAmelCase , (output_size[1] - divid_point_x, output_size[0] - divid_point_y) ) __SCREAMING_SNAKE_CASE = img for bbox in img_annos: __SCREAMING_SNAKE_CASE = scale_x + bbox[1] * (1 - scale_x) __SCREAMING_SNAKE_CASE = scale_y + bbox[2] * (1 - scale_y) __SCREAMING_SNAKE_CASE = scale_x + bbox[3] * (1 - scale_x) __SCREAMING_SNAKE_CASE = scale_y + bbox[4] * (1 - scale_y) new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) # Remove bounding box small than scale of filter if filter_scale > 0: __SCREAMING_SNAKE_CASE = [ anno for anno in new_anno if filter_scale < (anno[3] - anno[1]) and filter_scale < (anno[4] - anno[2]) ] return output_img, new_anno, path_list[0] def __magic_name__ ( __UpperCAmelCase ) -> str: '''simple docstring''' assert number_char > 1, "The number of character should greater than 1" __SCREAMING_SNAKE_CASE = ascii_lowercase + digits return "".join(random.choice(__UpperCAmelCase ) for _ in range(__UpperCAmelCase ) ) if __name__ == "__main__": main() print("DONE ✅")
13
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING a = logging.get_logger(__name__) a = { "ut/deta": "https://huggingface.co/ut/deta/resolve/main/config.json", } class __a ( _snake_case ): __UpperCamelCase : Dict = 'deta' __UpperCamelCase : List[str] = { 'hidden_size': 'd_model', 'num_attention_heads': 'encoder_attention_heads', } def __init__( self : Tuple ,lowerCamelCase : List[Any]=None ,lowerCamelCase : Any=900 ,lowerCamelCase : int=2048 ,lowerCamelCase : Any=6 ,lowerCamelCase : Optional[Any]=2048 ,lowerCamelCase : str=8 ,lowerCamelCase : Union[str, Any]=6 ,lowerCamelCase : List[str]=1024 ,lowerCamelCase : int=8 ,lowerCamelCase : Any=0.0 ,lowerCamelCase : Any=True ,lowerCamelCase : Optional[int]="relu" ,lowerCamelCase : int=256 ,lowerCamelCase : Tuple=0.1 ,lowerCamelCase : Optional[Any]=0.0 ,lowerCamelCase : Tuple=0.0 ,lowerCamelCase : List[str]=0.02 ,lowerCamelCase : Any=1.0 ,lowerCamelCase : Optional[int]=True ,lowerCamelCase : int=False ,lowerCamelCase : Optional[Any]="sine" ,lowerCamelCase : Dict=5 ,lowerCamelCase : List[Any]=4 ,lowerCamelCase : Optional[Any]=4 ,lowerCamelCase : Any=True ,lowerCamelCase : int=300 ,lowerCamelCase : Any=True ,lowerCamelCase : Tuple=True ,lowerCamelCase : int=1 ,lowerCamelCase : Tuple=5 ,lowerCamelCase : Union[str, Any]=2 ,lowerCamelCase : Tuple=1 ,lowerCamelCase : int=1 ,lowerCamelCase : str=5 ,lowerCamelCase : Optional[Any]=2 ,lowerCamelCase : List[Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.25 ,**lowerCamelCase : int ,): '''simple docstring''' if backbone_config is None: logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.""" ) __SCREAMING_SNAKE_CASE = CONFIG_MAPPING["""resnet"""](out_features=["""stage2""", """stage3""", """stage4"""] ) else: if isinstance(lowerCamelCase ,lowerCamelCase ): __SCREAMING_SNAKE_CASE = backbone_config.pop("""model_type""" ) __SCREAMING_SNAKE_CASE = CONFIG_MAPPING[backbone_model_type] __SCREAMING_SNAKE_CASE = config_class.from_dict(lowerCamelCase ) __SCREAMING_SNAKE_CASE = backbone_config __SCREAMING_SNAKE_CASE = num_queries __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = d_model __SCREAMING_SNAKE_CASE = encoder_ffn_dim __SCREAMING_SNAKE_CASE = encoder_layers __SCREAMING_SNAKE_CASE = encoder_attention_heads __SCREAMING_SNAKE_CASE = decoder_ffn_dim __SCREAMING_SNAKE_CASE = decoder_layers __SCREAMING_SNAKE_CASE = decoder_attention_heads __SCREAMING_SNAKE_CASE = dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = activation_function __SCREAMING_SNAKE_CASE = init_std __SCREAMING_SNAKE_CASE = init_xavier_std __SCREAMING_SNAKE_CASE = encoder_layerdrop __SCREAMING_SNAKE_CASE = auxiliary_loss __SCREAMING_SNAKE_CASE = position_embedding_type # deformable attributes __SCREAMING_SNAKE_CASE = num_feature_levels __SCREAMING_SNAKE_CASE = encoder_n_points __SCREAMING_SNAKE_CASE = decoder_n_points __SCREAMING_SNAKE_CASE = two_stage __SCREAMING_SNAKE_CASE = two_stage_num_proposals __SCREAMING_SNAKE_CASE = with_box_refine __SCREAMING_SNAKE_CASE = assign_first_stage if two_stage is True and with_box_refine is False: raise ValueError("""If two_stage is True, with_box_refine must be True.""" ) # Hungarian matcher __SCREAMING_SNAKE_CASE = class_cost __SCREAMING_SNAKE_CASE = bbox_cost __SCREAMING_SNAKE_CASE = giou_cost # Loss coefficients __SCREAMING_SNAKE_CASE = mask_loss_coefficient __SCREAMING_SNAKE_CASE = dice_loss_coefficient __SCREAMING_SNAKE_CASE = bbox_loss_coefficient __SCREAMING_SNAKE_CASE = giou_loss_coefficient __SCREAMING_SNAKE_CASE = eos_coefficient __SCREAMING_SNAKE_CASE = focal_alpha super().__init__(is_encoder_decoder=lowerCamelCase ,**lowerCamelCase ) @property def UpperCAmelCase__ ( self : Any ): '''simple docstring''' return self.encoder_attention_heads @property def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' return self.d_model def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) __SCREAMING_SNAKE_CASE = self.backbone_config.to_dict() __SCREAMING_SNAKE_CASE = self.__class__.model_type return output
13
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a = logging.get_logger(__name__) a = { "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 __a ( _snake_case ): __UpperCamelCase : int = 'poolformer' def __init__( self : Any ,lowerCamelCase : str=3 ,lowerCamelCase : str=16 ,lowerCamelCase : Dict=16 ,lowerCamelCase : int=3 ,lowerCamelCase : List[str]=4.0 ,lowerCamelCase : List[Any]=[2, 2, 6, 2] ,lowerCamelCase : Union[str, Any]=[64, 128, 320, 512] ,lowerCamelCase : int=[7, 3, 3, 3] ,lowerCamelCase : List[str]=[4, 2, 2, 2] ,lowerCamelCase : Any=[2, 1, 1, 1] ,lowerCamelCase : List[str]=4 ,lowerCamelCase : Tuple=0.0 ,lowerCamelCase : List[Any]="gelu" ,lowerCamelCase : List[str]=True ,lowerCamelCase : Dict=1E-5 ,lowerCamelCase : Dict=0.02 ,**lowerCamelCase : Tuple ,): '''simple docstring''' __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 __a ( _snake_case ): __UpperCamelCase : List[str] = version.parse('1.11' ) @property def UpperCAmelCase__ ( self : int ): '''simple docstring''' return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' return 2E-3
13
'''simple docstring''' import flax.linen as nn import jax import jax.numpy as jnp class __a ( nn.Module ): __UpperCamelCase : int __UpperCamelCase : jnp.dtype = jnp.floataa def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = nn.Conv( self.out_channels ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) def __call__( self : List[Any] ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = hidden_states.shape __SCREAMING_SNAKE_CASE = jax.image.resize( lowerCamelCase ,shape=(batch, height * 2, width * 2, channels) ,method="""nearest""" ,) __SCREAMING_SNAKE_CASE = self.conv(lowerCamelCase ) return hidden_states class __a ( nn.Module ): __UpperCamelCase : int __UpperCamelCase : jnp.dtype = jnp.floataa def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = nn.Conv( self.out_channels ,kernel_size=(3, 3) ,strides=(2, 2) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) def __call__( self : List[str] ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.conv(lowerCamelCase ) return hidden_states class __a ( nn.Module ): __UpperCamelCase : int __UpperCamelCase : int = None __UpperCamelCase : float = 0.0 __UpperCamelCase : bool = None __UpperCamelCase : jnp.dtype = jnp.floataa def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.in_channels if self.out_channels is None else self.out_channels __SCREAMING_SNAKE_CASE = nn.GroupNorm(num_groups=32 ,epsilon=1E-5 ) __SCREAMING_SNAKE_CASE = nn.Conv( lowerCamelCase ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) __SCREAMING_SNAKE_CASE = nn.Dense(lowerCamelCase ,dtype=self.dtype ) __SCREAMING_SNAKE_CASE = nn.GroupNorm(num_groups=32 ,epsilon=1E-5 ) __SCREAMING_SNAKE_CASE = nn.Dropout(self.dropout_prob ) __SCREAMING_SNAKE_CASE = nn.Conv( lowerCamelCase ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) __SCREAMING_SNAKE_CASE = self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut __SCREAMING_SNAKE_CASE = None if use_nin_shortcut: __SCREAMING_SNAKE_CASE = nn.Conv( lowerCamelCase ,kernel_size=(1, 1) ,strides=(1, 1) ,padding="""VALID""" ,dtype=self.dtype ,) def __call__( self : List[str] ,lowerCamelCase : Optional[int] ,lowerCamelCase : Tuple ,lowerCamelCase : Union[str, Any]=True ): '''simple docstring''' __SCREAMING_SNAKE_CASE = hidden_states __SCREAMING_SNAKE_CASE = self.norma(lowerCamelCase ) __SCREAMING_SNAKE_CASE = nn.swish(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.conva(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.time_emb_proj(nn.swish(lowerCamelCase ) ) __SCREAMING_SNAKE_CASE = jnp.expand_dims(jnp.expand_dims(lowerCamelCase ,1 ) ,1 ) __SCREAMING_SNAKE_CASE = hidden_states + temb __SCREAMING_SNAKE_CASE = self.norma(lowerCamelCase ) __SCREAMING_SNAKE_CASE = nn.swish(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.dropout(lowerCamelCase ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.conva(lowerCamelCase ) if self.conv_shortcut is not None: __SCREAMING_SNAKE_CASE = self.conv_shortcut(lowerCamelCase ) return hidden_states + residual
13
1
'''simple docstring''' import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, PLBartTokenizer, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin a = get_tests_dir("fixtures/test_sentencepiece.model") if is_torch_available(): from transformers.models.plbart.modeling_plbart import shift_tokens_right a = 50003 a = 50002 @require_sentencepiece @require_tokenizers class __a ( _snake_case, unittest.TestCase ): __UpperCamelCase : Dict = PLBartTokenizer __UpperCamelCase : Union[str, Any] = None __UpperCamelCase : Union[str, Any] = False def UpperCAmelCase__ ( self : str ): '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing __SCREAMING_SNAKE_CASE = PLBartTokenizer(lowerCamelCase ,language_codes="""base""" ,keep_accents=lowerCamelCase ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = PLBartTokenizer(lowerCamelCase ,language_codes="""base""" ,keep_accents=lowerCamelCase ) __SCREAMING_SNAKE_CASE = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(lowerCamelCase ,["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCamelCase ) ,[value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] ,) __SCREAMING_SNAKE_CASE = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( lowerCamelCase ,[ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """.""", ] ,) __SCREAMING_SNAKE_CASE = tokenizer.convert_tokens_to_ids(lowerCamelCase ) self.assertListEqual( lowerCamelCase ,[ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] ,) __SCREAMING_SNAKE_CASE = tokenizer.convert_ids_to_tokens(lowerCamelCase ) self.assertListEqual( lowerCamelCase ,[ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] ,) __SCREAMING_SNAKE_CASE = tokenizer.vocab_size __SCREAMING_SNAKE_CASE = [tokenizer.convert_ids_to_tokens(lowerCamelCase ) for x in range(end - 4 ,lowerCamelCase )] self.assertListEqual(lowerCamelCase ,["""__java__""", """__python__""", """__en_XX__""", """<mask>"""] ) __SCREAMING_SNAKE_CASE = """java.lang.Exception, python.lang.Exception, javascript, php, ruby, go""" __SCREAMING_SNAKE_CASE = tokenizer(lowerCamelCase ).input_ids self.assertEqual( tokenizer.decode(lowerCamelCase ,skip_special_tokens=lowerCamelCase ,clean_up_tokenization_spaces=lowerCamelCase ) ,lowerCamelCase ,) def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = PLBartTokenizer(lowerCamelCase ,language_codes="""multi""" ,keep_accents=lowerCamelCase ) __SCREAMING_SNAKE_CASE = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(lowerCamelCase ,["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCamelCase ) ,[value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] ,) __SCREAMING_SNAKE_CASE = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( lowerCamelCase ,[ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """.""", ] ,) __SCREAMING_SNAKE_CASE = tokenizer.convert_tokens_to_ids(lowerCamelCase ) self.assertListEqual( lowerCamelCase ,[ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] ,) __SCREAMING_SNAKE_CASE = tokenizer.convert_ids_to_tokens(lowerCamelCase ) self.assertListEqual( lowerCamelCase ,[ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] ,) __SCREAMING_SNAKE_CASE = tokenizer.vocab_size __SCREAMING_SNAKE_CASE = [tokenizer.convert_ids_to_tokens(lowerCamelCase ) for x in range(end - 7 ,lowerCamelCase )] self.assertListEqual( lowerCamelCase ,["""__java__""", """__python__""", """__en_XX__""", """__javascript__""", """__php__""", """__ruby__""", """__go__"""] ) __SCREAMING_SNAKE_CASE = """java.lang.Exception, python.lang.Exception, javascript, php, ruby, go""" __SCREAMING_SNAKE_CASE = tokenizer(lowerCamelCase ).input_ids self.assertEqual( tokenizer.decode(lowerCamelCase ,skip_special_tokens=lowerCamelCase ,clean_up_tokenization_spaces=lowerCamelCase ) ,lowerCamelCase ,) @require_torch @require_sentencepiece @require_tokenizers class __a ( unittest.TestCase ): __UpperCamelCase : Optional[int] = 'uclanlp/plbart-python-en_XX' __UpperCamelCase : Dict = [ 'def maximum(a,b,c):NEW_LINE_INDENTreturn max([a,b,c])', 'def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])', ] __UpperCamelCase : Tuple = [ 'Returns the maximum value of a b c.', 'Sums the values of a b c.', ] __UpperCamelCase : str = [ 134, 5452, 3_3460, 3_3441, 3_3463, 3_3465, 3_3463, 3_3449, 988, 20, 3_3456, 19, 3_3456, 771, 39, 4258, 889, 3318, 3_3441, 3_3463, 3_3465, 3_3463, 3_3449, 2471, 2, PYTHON_CODE, ] @classmethod def UpperCAmelCase__ ( cls : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = PLBartTokenizer.from_pretrained( cls.checkpoint_name ,language_codes="""base""" ,src_lang="""python""" ,tgt_lang="""en_XX""" ) __SCREAMING_SNAKE_CASE = 1 return cls def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""__java__"""] ,5_0001 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""__python__"""] ,5_0002 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""__en_XX__"""] ,5_0003 ) def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens ,lowerCamelCase ) def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' self.assertIn(lowerCamelCase ,self.tokenizer.all_special_ids ) __SCREAMING_SNAKE_CASE = [EN_CODE, 9037, 3_3442, 57, 752, 153, 14, 56, 18, 9, 2] __SCREAMING_SNAKE_CASE = self.tokenizer.decode(lowerCamelCase ,skip_special_tokens=lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.tokenizer.decode(generated_ids[1:] ,skip_special_tokens=lowerCamelCase ) self.assertEqual(lowerCamelCase ,lowerCamelCase ) self.assertNotIn(self.tokenizer.eos_token ,lowerCamelCase ) def UpperCAmelCase__ ( self : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = ["""def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])""" * 20] self.assertIsInstance(src_text[0] ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = 10 __SCREAMING_SNAKE_CASE = self.tokenizer(lowerCamelCase ,max_length=lowerCamelCase ,truncation=lowerCamelCase ).input_ids[0] self.assertEqual(ids[-2] ,2 ) self.assertEqual(ids[-1] ,lowerCamelCase ) self.assertEqual(len(lowerCamelCase ) ,lowerCamelCase ) def UpperCAmelCase__ ( self : Any ): '''simple docstring''' self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["""<mask>""", """__java__"""] ) ,[5_0004, 5_0001] ) def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = tempfile.mkdtemp() __SCREAMING_SNAKE_CASE = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(lowerCamelCase ) __SCREAMING_SNAKE_CASE = PLBartTokenizer.from_pretrained(lowerCamelCase ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids ,lowerCamelCase ) @require_torch def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.tokenizer(self.src_text ,text_target=self.tgt_text ,padding=lowerCamelCase ,return_tensors="""pt""" ) __SCREAMING_SNAKE_CASE = shift_tokens_right(batch["""labels"""] ,self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 self.assertEqual(batch.input_ids[1][-2:].tolist() ,[2, PYTHON_CODE] ) self.assertEqual(batch.decoder_input_ids[1][0] ,lowerCamelCase ) self.assertEqual(batch.decoder_input_ids[1][-1] ,2 ) self.assertEqual(batch.labels[1][-2:].tolist() ,[2, EN_CODE] ) @require_torch def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.tokenizer( self.src_text ,text_target=self.tgt_text ,padding=lowerCamelCase ,truncation=lowerCamelCase ,max_length=len(self.expected_src_tokens ) ,return_tensors="""pt""" ,) __SCREAMING_SNAKE_CASE = shift_tokens_right(batch["""labels"""] ,self.tokenizer.pad_token_id ) self.assertIsInstance(lowerCamelCase ,lowerCamelCase ) self.assertEqual((2, 26) ,batch.input_ids.shape ) self.assertEqual((2, 26) ,batch.attention_mask.shape ) __SCREAMING_SNAKE_CASE = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens ,lowerCamelCase ) self.assertEqual(2 ,batch.decoder_input_ids[0, -1] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens ,[] ) self.assertEqual(self.tokenizer.suffix_tokens ,[self.tokenizer.eos_token_id, PYTHON_CODE] ) def UpperCAmelCase__ ( self : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.tokenizer(self.src_text ,padding=lowerCamelCase ,truncation=lowerCamelCase ,max_length=3 ,return_tensors="""pt""" ) __SCREAMING_SNAKE_CASE = self.tokenizer( text_target=self.tgt_text ,padding=lowerCamelCase ,truncation=lowerCamelCase ,max_length=10 ,return_tensors="""pt""" ) __SCREAMING_SNAKE_CASE = targets["""input_ids"""] __SCREAMING_SNAKE_CASE = shift_tokens_right(lowerCamelCase ,self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] ,3 ) self.assertEqual(batch.decoder_input_ids.shape[1] ,10 ) @require_torch def UpperCAmelCase__ ( self : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.tokenizer._build_translation_inputs( """A test""" ,return_tensors="""pt""" ,src_lang="""en_XX""" ,tgt_lang="""java""" ) self.assertEqual( nested_simplify(lowerCamelCase ) ,{ # A, test, EOS, en_XX """input_ids""": [[150, 242, 2, 5_0003]], """attention_mask""": [[1, 1, 1, 1]], # java """forced_bos_token_id""": 5_0001, } ,)
13
'''simple docstring''' import sys from collections import defaultdict class __a : def __init__( self : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = [] def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : List[Any] ): '''simple docstring''' return self.node_position[vertex] def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : str ,lowerCamelCase : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = pos def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : Optional[int] ,lowerCamelCase : Union[str, Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : Any ): '''simple docstring''' if start > size // 2 - 1: return else: if 2 * start + 2 >= size: __SCREAMING_SNAKE_CASE = 2 * start + 1 else: if heap[2 * start + 1] < heap[2 * start + 2]: __SCREAMING_SNAKE_CASE = 2 * start + 1 else: __SCREAMING_SNAKE_CASE = 2 * start + 2 if heap[smallest_child] < heap[start]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = heap[smallest_child], positions[smallest_child] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = ( heap[start], positions[start], ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = temp, tempa __SCREAMING_SNAKE_CASE = self.get_position(positions[smallest_child] ) self.set_position( positions[smallest_child] ,self.get_position(positions[start] ) ) self.set_position(positions[start] ,lowerCamelCase ) self.top_to_bottom(lowerCamelCase ,lowerCamelCase ,lowerCamelCase ,lowerCamelCase ) def UpperCAmelCase__ ( self : Any ,lowerCamelCase : int ,lowerCamelCase : List[str] ,lowerCamelCase : Optional[Any] ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = position[index] while index != 0: __SCREAMING_SNAKE_CASE = int((index - 2) / 2 ) if index % 2 == 0 else int((index - 1) / 2 ) if val < heap[parent]: __SCREAMING_SNAKE_CASE = heap[parent] __SCREAMING_SNAKE_CASE = position[parent] self.set_position(position[parent] ,lowerCamelCase ) else: __SCREAMING_SNAKE_CASE = val __SCREAMING_SNAKE_CASE = temp self.set_position(lowerCamelCase ,lowerCamelCase ) break __SCREAMING_SNAKE_CASE = parent else: __SCREAMING_SNAKE_CASE = val __SCREAMING_SNAKE_CASE = temp self.set_position(lowerCamelCase ,0 ) def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : List[Any] ,lowerCamelCase : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = len(lowerCamelCase ) // 2 - 1 for i in range(lowerCamelCase ,-1 ,-1 ): self.top_to_bottom(lowerCamelCase ,lowerCamelCase ,len(lowerCamelCase ) ,lowerCamelCase ) def UpperCAmelCase__ ( self : int ,lowerCamelCase : Optional[int] ,lowerCamelCase : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = positions[0] __SCREAMING_SNAKE_CASE = sys.maxsize self.top_to_bottom(lowerCamelCase ,0 ,len(lowerCamelCase ) ,lowerCamelCase ) return temp def __magic_name__ ( __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = Heap() __SCREAMING_SNAKE_CASE = [0] * len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [-1] * len(__UpperCAmelCase ) # Neighboring Tree Vertex of selected vertex # Minimum Distance of explored vertex with neighboring vertex of partial tree # formed in graph __SCREAMING_SNAKE_CASE = [] # Heap of Distance of vertices from their neighboring vertex __SCREAMING_SNAKE_CASE = [] for vertex in range(len(__UpperCAmelCase ) ): distance_tv.append(sys.maxsize ) positions.append(__UpperCAmelCase ) heap.node_position.append(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = sys.maxsize for neighbor, distance in adjacency_list[0]: __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = distance heap.heapify(__UpperCAmelCase , __UpperCAmelCase ) for _ in range(1 , len(__UpperCAmelCase ) ): __SCREAMING_SNAKE_CASE = heap.delete_minimum(__UpperCAmelCase , __UpperCAmelCase ) if visited[vertex] == 0: tree_edges.append((nbr_tv[vertex], vertex) ) __SCREAMING_SNAKE_CASE = 1 for neighbor, distance in adjacency_list[vertex]: if ( visited[neighbor] == 0 and distance < distance_tv[heap.get_position(__UpperCAmelCase )] ): __SCREAMING_SNAKE_CASE = distance heap.bottom_to_top( __UpperCAmelCase , heap.get_position(__UpperCAmelCase ) , __UpperCAmelCase , __UpperCAmelCase ) __SCREAMING_SNAKE_CASE = vertex return tree_edges if __name__ == "__main__": # pragma: no cover # < --------- Prims Algorithm --------- > a = int(input("Enter number of edges: ").strip()) a = defaultdict(list) for _ in range(edges_number): a = [int(x) for x in input().strip().split()] adjacency_list[edge[0]].append([edge[1], edge[2]]) adjacency_list[edge[1]].append([edge[0], edge[2]]) print(prisms_algorithm(adjacency_list))
13
1
'''simple docstring''' import argparse import logging import os from datetime import datetime import numpy as np import torch from torch import nn from torch.utils.data import DataLoader, RandomSampler, TensorDataset from tqdm import tqdm from transformers import GPTaLMHeadModel a = logging.getLogger(__name__) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' if os.path.exists(__UpperCAmelCase ): if os.path.exists(os.path.join(__UpperCAmelCase , """config.json""" ) ) and os.path.isfile( os.path.join(__UpperCAmelCase , """config.json""" ) ): os.remove(os.path.join(__UpperCAmelCase , """config.json""" ) ) if os.path.exists(os.path.join(__UpperCAmelCase , """pytorch_model.bin""" ) ) and os.path.isfile( os.path.join(__UpperCAmelCase , """pytorch_model.bin""" ) ): os.remove(os.path.join(__UpperCAmelCase , """pytorch_model.bin""" ) ) else: os.makedirs(__UpperCAmelCase ) model.save_pretrained(__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase=False ) -> List[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = 2 if unlogit: __SCREAMING_SNAKE_CASE = torch.pow(__UpperCAmelCase , __UpperCAmelCase ) __SCREAMING_SNAKE_CASE = p * torch.log(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = 0 return -plogp.sum(dim=-1 ) def __magic_name__ ( __UpperCAmelCase ) -> Optional[int]: '''simple docstring''' logger.info("""lv, h >\t""" + """\t""".join(f"""{x + 1}""" for x in range(len(__UpperCAmelCase ) ) ) ) for row in range(len(__UpperCAmelCase ) ): if tensor.dtype != torch.long: logger.info(f"""layer {row + 1}:\t""" + """\t""".join(f"""{x:.5f}""" for x in tensor[row].cpu().data ) ) else: logger.info(f"""layer {row + 1}:\t""" + """\t""".join(f"""{x:d}""" for x in tensor[row].cpu().data ) ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=None , __UpperCAmelCase=False ) -> List[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = model.config.num_hidden_layers, model.config.num_attention_heads __SCREAMING_SNAKE_CASE = torch.zeros(__UpperCAmelCase , __UpperCAmelCase ).to(args.device ) __SCREAMING_SNAKE_CASE = torch.zeros(__UpperCAmelCase , __UpperCAmelCase ).to(args.device ) if head_mask is None: __SCREAMING_SNAKE_CASE = torch.ones(__UpperCAmelCase , __UpperCAmelCase ).to(args.device ) head_mask.requires_grad_(requires_grad=__UpperCAmelCase ) # If actually pruned attention multi-head, set head mask to None to avoid shape mismatch if actually_pruned: __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = 0.0 __SCREAMING_SNAKE_CASE = 0.0 for step, inputs in enumerate(tqdm(__UpperCAmelCase , desc="""Iteration""" , disable=args.local_rank not in [-1, 0] ) ): __SCREAMING_SNAKE_CASE = tuple(t.to(args.device ) for t in inputs ) ((__SCREAMING_SNAKE_CASE) , ) = inputs # Do a forward pass (not with torch.no_grad() since we need gradients for importance score - see below) __SCREAMING_SNAKE_CASE = model(__UpperCAmelCase , labels=__UpperCAmelCase , head_mask=__UpperCAmelCase ) # (loss), lm_logits, presents, (all hidden_states), (attentions) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = ( outputs[0], outputs[1], outputs[-1], ) # Loss and logits are the first, attention the last loss.backward() # Backpropagate to populate the gradients in the head mask total_loss += loss.detach().cpu().numpy() if compute_entropy: for layer, attn in enumerate(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = entropy(attn.detach() , __UpperCAmelCase ) attn_entropy[layer] += masked_entropy.sum(-1 ).sum(0 ).sum(0 ).detach() if compute_importance: head_importance += head_mask.grad.abs().detach() tot_tokens += torch.ones_like(__UpperCAmelCase ).float().detach().sum().data # Normalize attn_entropy /= tot_tokens head_importance /= tot_tokens # Layerwise importance normalization if not args.dont_normalize_importance_by_layer: __SCREAMING_SNAKE_CASE = 2 __SCREAMING_SNAKE_CASE = torch.pow(torch.pow(__UpperCAmelCase , __UpperCAmelCase ).sum(-1 ) , 1 / exponent ) head_importance /= norm_by_layer.unsqueeze(-1 ) + 1e-2_0 if not args.dont_normalize_global_importance: __SCREAMING_SNAKE_CASE = (head_importance - head_importance.min()) / (head_importance.max() - head_importance.min()) # Print matrices if compute_entropy: logger.info("""Attention entropies""" ) print_ad_tensor(__UpperCAmelCase ) if compute_importance: logger.info("""Head importance scores""" ) print_ad_tensor(__UpperCAmelCase ) logger.info("""Head ranked by importance scores""" ) __SCREAMING_SNAKE_CASE = torch.zeros(head_importance.numel() , dtype=torch.long , device=args.device ) __SCREAMING_SNAKE_CASE = torch.arange( head_importance.numel() , device=args.device ) __SCREAMING_SNAKE_CASE = head_ranks.view_as(__UpperCAmelCase ) print_ad_tensor(__UpperCAmelCase ) return attn_entropy, head_importance, total_loss def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = compute_heads_importance(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , compute_entropy=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = 1 / loss # instead of downsteam score use the LM loss logger.info("""Pruning: original score: %f, threshold: %f""" , __UpperCAmelCase , original_score * args.masking_threshold ) __SCREAMING_SNAKE_CASE = torch.ones_like(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = max(1 , int(new_head_mask.numel() * args.masking_amount ) ) __SCREAMING_SNAKE_CASE = original_score while current_score >= original_score * args.masking_threshold: __SCREAMING_SNAKE_CASE = new_head_mask.clone().detach() # save current head mask # heads from least important to most - keep only not-masked heads __SCREAMING_SNAKE_CASE = float("""Inf""" ) __SCREAMING_SNAKE_CASE = head_importance.view(-1 ).sort()[1] if len(__UpperCAmelCase ) <= num_to_mask: print("""BREAK BY num_to_mask""" ) break # mask heads __SCREAMING_SNAKE_CASE = current_heads_to_mask[:num_to_mask] logger.info("""Heads to mask: %s""" , str(current_heads_to_mask.tolist() ) ) __SCREAMING_SNAKE_CASE = new_head_mask.view(-1 ) __SCREAMING_SNAKE_CASE = 0.0 __SCREAMING_SNAKE_CASE = new_head_mask.view_as(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = new_head_mask.clone().detach() print_ad_tensor(__UpperCAmelCase ) # Compute metric and head importance again __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = compute_heads_importance( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , compute_entropy=__UpperCAmelCase , head_mask=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = 1 / loss logger.info( """Masking: current score: %f, remaining heads %d (%.1f percents)""" , __UpperCAmelCase , new_head_mask.sum() , new_head_mask.sum() / new_head_mask.numel() * 100 , ) logger.info("""Final head mask""" ) print_ad_tensor(__UpperCAmelCase ) np.save(os.path.join(args.output_dir , """head_mask.npy""" ) , head_mask.detach().cpu().numpy() ) return head_mask def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> List[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = datetime.now() __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = compute_heads_importance( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , compute_entropy=__UpperCAmelCase , compute_importance=__UpperCAmelCase , head_mask=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = 1 / loss __SCREAMING_SNAKE_CASE = datetime.now() - before_time __SCREAMING_SNAKE_CASE = sum(p.numel() for p in model.parameters() ) __SCREAMING_SNAKE_CASE = { layer: (1 - head_mask[layer].long()).nonzero().squeeze().tolist() for layer in range(len(__UpperCAmelCase ) ) } for k, v in heads_to_prune.items(): if isinstance(__UpperCAmelCase , __UpperCAmelCase ): __SCREAMING_SNAKE_CASE = [ v, ] assert sum(len(__UpperCAmelCase ) for h in heads_to_prune.values() ) == (1 - head_mask.long()).sum().item() model.prune_heads(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = sum(p.numel() for p in model.parameters() ) __SCREAMING_SNAKE_CASE = datetime.now() __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = compute_heads_importance( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , compute_entropy=__UpperCAmelCase , compute_importance=__UpperCAmelCase , head_mask=__UpperCAmelCase , actually_pruned=__UpperCAmelCase , ) __SCREAMING_SNAKE_CASE = 1 / loss __SCREAMING_SNAKE_CASE = datetime.now() - before_time logger.info( """Pruning: original num of params: %.2e, after pruning %.2e (%.1f percents)""" , __UpperCAmelCase , __UpperCAmelCase , pruned_num_params / original_num_params * 100 , ) logger.info("""Pruning: score with masking: %f score with pruning: %f""" , __UpperCAmelCase , __UpperCAmelCase ) logger.info("""Pruning: speed ratio (original timing / new timing): %f percents""" , original_time / new_time * 100 ) save_model(__UpperCAmelCase , args.output_dir ) def __magic_name__ ( ) -> Any: '''simple docstring''' __SCREAMING_SNAKE_CASE = argparse.ArgumentParser() # Required parameters parser.add_argument( """--data_dir""" , default=__UpperCAmelCase , type=__UpperCAmelCase , required=__UpperCAmelCase , help="""The input data dir. Should contain the .tsv files (or other data files) for the task.""" , ) parser.add_argument( """--model_name_or_path""" , default=__UpperCAmelCase , type=__UpperCAmelCase , required=__UpperCAmelCase , help="""Path to pretrained model or model identifier from huggingface.co/models""" , ) parser.add_argument( """--output_dir""" , default=__UpperCAmelCase , type=__UpperCAmelCase , required=__UpperCAmelCase , help="""The output directory where the model predictions and checkpoints will be written.""" , ) # Other parameters parser.add_argument( """--config_name""" , default="""""" , type=__UpperCAmelCase , help="""Pretrained config name or path if not the same as model_name_or_path""" , ) parser.add_argument( """--tokenizer_name""" , default="""""" , type=__UpperCAmelCase , help="""Pretrained tokenizer name or path if not the same as model_name_or_path""" , ) parser.add_argument( """--cache_dir""" , default=__UpperCAmelCase , type=__UpperCAmelCase , help="""Where do you want to store the pre-trained models downloaded from s3""" , ) parser.add_argument( """--data_subset""" , type=__UpperCAmelCase , default=-1 , help="""If > 0: limit the data to a subset of data_subset instances.""" ) parser.add_argument( """--overwrite_output_dir""" , action="""store_true""" , help="""Whether to overwrite data in output directory""" ) parser.add_argument( """--overwrite_cache""" , action="""store_true""" , help="""Overwrite the cached training and evaluation sets""" ) parser.add_argument( """--dont_normalize_importance_by_layer""" , action="""store_true""" , help="""Don't normalize importance score by layers""" ) parser.add_argument( """--dont_normalize_global_importance""" , action="""store_true""" , help="""Don't normalize all importance scores between 0 and 1""" , ) parser.add_argument( """--try_masking""" , action="""store_true""" , help="""Whether to try to mask head until a threshold of accuracy.""" ) parser.add_argument( """--masking_threshold""" , default=0.9 , type=__UpperCAmelCase , help="""masking threshold in term of metrics (stop masking when metric < threshold * original metric value).""" , ) parser.add_argument( """--masking_amount""" , default=0.1 , type=__UpperCAmelCase , help="""Amount to heads to masking at each masking step.""" ) parser.add_argument("""--metric_name""" , default="""acc""" , type=__UpperCAmelCase , help="""Metric to use for head masking.""" ) parser.add_argument( """--max_seq_length""" , default=128 , type=__UpperCAmelCase , help=( """The maximum total input sequence length after WordPiece tokenization. \n""" """Sequences longer than this will be truncated, sequences shorter padded.""" ) , ) parser.add_argument("""--batch_size""" , default=1 , type=__UpperCAmelCase , help="""Batch size.""" ) parser.add_argument("""--seed""" , type=__UpperCAmelCase , default=42 ) parser.add_argument("""--local_rank""" , type=__UpperCAmelCase , default=-1 , help="""local_rank for distributed training on gpus""" ) parser.add_argument("""--no_cuda""" , action="""store_true""" , help="""Whether not to use CUDA when available""" ) parser.add_argument("""--server_ip""" , type=__UpperCAmelCase , default="""""" , help="""Can be used for distant debugging.""" ) parser.add_argument("""--server_port""" , type=__UpperCAmelCase , default="""""" , help="""Can be used for distant debugging.""" ) __SCREAMING_SNAKE_CASE = parser.parse_args() if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("""Waiting for debugger attach""" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__UpperCAmelCase ) ptvsd.wait_for_attach() # Setup devices and distributed training if args.local_rank == -1 or args.no_cuda: __SCREAMING_SNAKE_CASE = torch.device("""cuda""" if torch.cuda.is_available() and not args.no_cuda else """cpu""" ) __SCREAMING_SNAKE_CASE = 0 if args.no_cuda else torch.cuda.device_count() else: torch.cuda.set_device(args.local_rank ) __SCREAMING_SNAKE_CASE = torch.device("""cuda""" , args.local_rank ) __SCREAMING_SNAKE_CASE = 1 torch.distributed.init_process_group(backend="""nccl""" ) # Initializes the distributed backend # Setup logging logging.basicConfig(level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN ) logger.info("""device: {} n_gpu: {}, distributed: {}""".format(args.device , args.n_gpu , bool(args.local_rank != -1 ) ) ) __SCREAMING_SNAKE_CASE = GPTaLMHeadModel.from_pretrained(args.model_name_or_path ) # Distributed and parallel training model.to(args.device ) if args.local_rank != -1: __SCREAMING_SNAKE_CASE = nn.parallel.DistributedDataParallel( __UpperCAmelCase , device_ids=[args.local_rank] , output_device=args.local_rank , find_unused_parameters=__UpperCAmelCase ) elif args.n_gpu > 1: __SCREAMING_SNAKE_CASE = nn.DataParallel(__UpperCAmelCase ) # Print/save training arguments os.makedirs(args.output_dir , exist_ok=__UpperCAmelCase ) torch.save(__UpperCAmelCase , os.path.join(args.output_dir , """run_args.bin""" ) ) logger.info("""Training/evaluation parameters %s""" , __UpperCAmelCase ) # Prepare dataset __SCREAMING_SNAKE_CASE = np.concatenate( [ np.loadtxt(args.data_dir , dtype=np.intaa ), ] ) __SCREAMING_SNAKE_CASE = (torch.from_numpy(__UpperCAmelCase ),) __SCREAMING_SNAKE_CASE = TensorDataset(*__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = RandomSampler(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = DataLoader(__UpperCAmelCase , sampler=__UpperCAmelCase , batch_size=args.batch_size ) # Compute head entropy and importance score compute_heads_importance(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) # Try head masking (set heads to zero until the score goes under a threshole) # and head pruning (remove masked heads and see the effect on the network) if args.try_masking and args.masking_threshold > 0.0 and args.masking_threshold < 1.0: __SCREAMING_SNAKE_CASE = mask_heads(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) prune_heads(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) if __name__ == "__main__": main()
13
'''simple docstring''' import os import string import sys a = 1 << 8 a = { "tab": ord("\t"), "newline": ord("\r"), "esc": 27, "up": 65 + ARROW_KEY_FLAG, "down": 66 + ARROW_KEY_FLAG, "right": 67 + ARROW_KEY_FLAG, "left": 68 + ARROW_KEY_FLAG, "mod_int": 91, "undefined": sys.maxsize, "interrupt": 3, "insert": 50, "delete": 51, "pg_up": 53, "pg_down": 54, } a = KEYMAP["up"] a = KEYMAP["left"] if sys.platform == "win32": a = [] a = { b"\xe0H": KEYMAP["up"] - ARROW_KEY_FLAG, b"\x00H": KEYMAP["up"] - ARROW_KEY_FLAG, b"\xe0P": KEYMAP["down"] - ARROW_KEY_FLAG, b"\x00P": KEYMAP["down"] - ARROW_KEY_FLAG, b"\xe0M": KEYMAP["right"] - ARROW_KEY_FLAG, b"\x00M": KEYMAP["right"] - ARROW_KEY_FLAG, b"\xe0K": KEYMAP["left"] - ARROW_KEY_FLAG, b"\x00K": KEYMAP["left"] - ARROW_KEY_FLAG, } for i in range(10): a = ord(str(i)) def __magic_name__ ( ) -> Union[str, Any]: '''simple docstring''' if os.name == "nt": import msvcrt __SCREAMING_SNAKE_CASE = """mbcs""" # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(__UpperCAmelCase ) == 0: # Read the keystroke __SCREAMING_SNAKE_CASE = msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): __SCREAMING_SNAKE_CASE = ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: __SCREAMING_SNAKE_CASE = chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP["""mod_int"""] ) ) WIN_CH_BUFFER.append(__UpperCAmelCase ) if ord(__UpperCAmelCase ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(126 ) ) __SCREAMING_SNAKE_CASE = chr(KEYMAP["""esc"""] ) except KeyError: __SCREAMING_SNAKE_CASE = cha[1] else: __SCREAMING_SNAKE_CASE = ch.decode(__UpperCAmelCase ) else: __SCREAMING_SNAKE_CASE = WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty __SCREAMING_SNAKE_CASE = sys.stdin.fileno() __SCREAMING_SNAKE_CASE = termios.tcgetattr(__UpperCAmelCase ) try: tty.setraw(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = sys.stdin.read(1 ) finally: termios.tcsetattr(__UpperCAmelCase , termios.TCSADRAIN , __UpperCAmelCase ) return ch def __magic_name__ ( ) -> List[str]: '''simple docstring''' __SCREAMING_SNAKE_CASE = get_raw_chars() if ord(__UpperCAmelCase ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(__UpperCAmelCase ) == KEYMAP["esc"]: __SCREAMING_SNAKE_CASE = get_raw_chars() if ord(__UpperCAmelCase ) == KEYMAP["mod_int"]: __SCREAMING_SNAKE_CASE = get_raw_chars() if ord(__UpperCAmelCase ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(__UpperCAmelCase ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(__UpperCAmelCase ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
13
1
'''simple docstring''' a = [ [0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], [0, 4, 0, 0, 14, 0], [0, 0, 9, 0, 0, 20], [0, 0, 0, 7, 0, 4], [0, 0, 0, 0, 0, 0], ] def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Tuple: '''simple docstring''' __SCREAMING_SNAKE_CASE = [False] * len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [s] __SCREAMING_SNAKE_CASE = True while queue: __SCREAMING_SNAKE_CASE = queue.pop(0 ) for ind in range(len(graph[u] ) ): if visited[ind] is False and graph[u][ind] > 0: queue.append(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = u return visited[t] def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> List[str]: '''simple docstring''' __SCREAMING_SNAKE_CASE = [-1] * (len(__UpperCAmelCase )) __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = [i[:] for i in graph] # Record original cut, copy. while bfs(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ): __SCREAMING_SNAKE_CASE = float("""Inf""" ) __SCREAMING_SNAKE_CASE = sink while s != source: # Find the minimum value in select path __SCREAMING_SNAKE_CASE = min(__UpperCAmelCase , graph[parent[s]][s] ) __SCREAMING_SNAKE_CASE = parent[s] max_flow += path_flow __SCREAMING_SNAKE_CASE = sink while v != source: __SCREAMING_SNAKE_CASE = parent[v] graph[u][v] -= path_flow graph[v][u] += path_flow __SCREAMING_SNAKE_CASE = parent[v] for i in range(len(__UpperCAmelCase ) ): for j in range(len(graph[0] ) ): if graph[i][j] == 0 and temp[i][j] > 0: res.append((i, j) ) return res if __name__ == "__main__": print(mincut(test_graph, source=0, sink=5))
13
'''simple docstring''' from __future__ import annotations import bisect def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' if hi < 0: __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) while lo < hi: __SCREAMING_SNAKE_CASE = lo + (hi - lo) // 2 if sorted_collection[mid] < item: __SCREAMING_SNAKE_CASE = mid + 1 else: __SCREAMING_SNAKE_CASE = mid return lo def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' if hi < 0: __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) while lo < hi: __SCREAMING_SNAKE_CASE = lo + (hi - lo) // 2 if sorted_collection[mid] <= item: __SCREAMING_SNAKE_CASE = mid + 1 else: __SCREAMING_SNAKE_CASE = mid return lo def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> None: '''simple docstring''' sorted_collection.insert(bisect_left(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> None: '''simple docstring''' sorted_collection.insert(bisect_right(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> int | None: '''simple docstring''' __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) - 1 while left <= right: __SCREAMING_SNAKE_CASE = left + (right - left) // 2 __SCREAMING_SNAKE_CASE = sorted_collection[midpoint] if current_item == item: return midpoint elif item < current_item: __SCREAMING_SNAKE_CASE = midpoint - 1 else: __SCREAMING_SNAKE_CASE = midpoint + 1 return None def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> int | None: '''simple docstring''' __SCREAMING_SNAKE_CASE = bisect.bisect_left(__UpperCAmelCase , __UpperCAmelCase ) if index != len(__UpperCAmelCase ) and sorted_collection[index] == item: return index return None def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> int | None: '''simple docstring''' if right < left: return None __SCREAMING_SNAKE_CASE = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: return binary_search_by_recursion(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , midpoint - 1 ) else: return binary_search_by_recursion(__UpperCAmelCase , __UpperCAmelCase , midpoint + 1 , __UpperCAmelCase ) if __name__ == "__main__": a = input("Enter numbers separated by comma:\n").strip() a = sorted(int(item) for item in user_input.split(",")) a = int(input("Enter a single number to be found in the list:\n")) a = binary_search(collection, target) if result is None: print(F'''{target} was not found in {collection}.''') else: print(F'''{target} was found at position {result} in {collection}.''')
13
1
'''simple docstring''' from __future__ import annotations def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> list[str]: '''simple docstring''' if nth_term == "": return [""] __SCREAMING_SNAKE_CASE = int(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = int(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [] for temp in range(int(__UpperCAmelCase ) ): series.append(f"""1 / {pow(temp + 1 , int(__UpperCAmelCase ) )}""" if series else """1""" ) return series if __name__ == "__main__": import doctest doctest.testmod() a = int(input("Enter the last number (nth term) of the P-Series")) a = int(input("Enter the power for P-Series")) print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p") print(p_series(nth_term, power))
13
'''simple docstring''' import math from enum import Enum from typing import Optional, Union from torch.optim import Optimizer from torch.optim.lr_scheduler import LambdaLR from .utils import logging a = logging.get_logger(__name__) class __a ( _snake_case ): __UpperCamelCase : int = 'linear' __UpperCamelCase : Tuple = 'cosine' __UpperCamelCase : Tuple = 'cosine_with_restarts' __UpperCamelCase : List[Any] = 'polynomial' __UpperCamelCase : Optional[Any] = 'constant' __UpperCamelCase : Optional[int] = 'constant_with_warmup' __UpperCamelCase : List[Any] = 'piecewise_constant' def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' return LambdaLR(__UpperCAmelCase , lambda __UpperCAmelCase : 1 , last_epoch=__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = -1 ) -> List[Any]: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1.0 , __UpperCAmelCase ) ) return 1.0 return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , last_epoch=__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = step_rules.split(""",""" ) for rule_str in rule_list[:-1]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = rule_str.split(""":""" ) __SCREAMING_SNAKE_CASE = int(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = float(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = value __SCREAMING_SNAKE_CASE = float(rule_list[-1] ) def create_rules_function(__UpperCAmelCase , __UpperCAmelCase ): def rule_func(__UpperCAmelCase ) -> float: __SCREAMING_SNAKE_CASE = sorted(rules_dict.keys() ) for i, sorted_step in enumerate(__UpperCAmelCase ): if steps < sorted_step: return rules_dict[sorted_steps[i]] return last_lr_multiple return rule_func __SCREAMING_SNAKE_CASE = create_rules_function(__UpperCAmelCase , __UpperCAmelCase ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , last_epoch=__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=-1 ) -> int: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) return max( 0.0 , float(num_training_steps - current_step ) / float(max(1 , num_training_steps - num_warmup_steps ) ) ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0.5 , __UpperCAmelCase = -1 ) -> Dict: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = float(current_step - num_warmup_steps ) / float(max(1 , num_training_steps - num_warmup_steps ) ) return max(0.0 , 0.5 * (1.0 + math.cos(math.pi * float(__UpperCAmelCase ) * 2.0 * progress )) ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 1 , __UpperCAmelCase = -1 ) -> Tuple: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = float(current_step - num_warmup_steps ) / float(max(1 , num_training_steps - num_warmup_steps ) ) if progress >= 1.0: return 0.0 return max(0.0 , 0.5 * (1.0 + math.cos(math.pi * ((float(__UpperCAmelCase ) * progress) % 1.0) )) ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=1e-7 , __UpperCAmelCase=1.0 , __UpperCAmelCase=-1 ) -> Tuple: '''simple docstring''' __SCREAMING_SNAKE_CASE = optimizer.defaults["""lr"""] if not (lr_init > lr_end): raise ValueError(f"""lr_end ({lr_end}) must be be smaller than initial lr ({lr_init})""" ) def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) elif current_step > num_training_steps: return lr_end / lr_init # as LambdaLR multiplies by lr_init else: __SCREAMING_SNAKE_CASE = lr_init - lr_end __SCREAMING_SNAKE_CASE = num_training_steps - num_warmup_steps __SCREAMING_SNAKE_CASE = 1 - (current_step - num_warmup_steps) / decay_steps __SCREAMING_SNAKE_CASE = lr_range * pct_remaining**power + lr_end return decay / lr_init # as LambdaLR multiplies by lr_init return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) a = { SchedulerType.LINEAR: get_linear_schedule_with_warmup, SchedulerType.COSINE: get_cosine_schedule_with_warmup, SchedulerType.COSINE_WITH_RESTARTS: get_cosine_with_hard_restarts_schedule_with_warmup, SchedulerType.POLYNOMIAL: get_polynomial_decay_schedule_with_warmup, SchedulerType.CONSTANT: get_constant_schedule, SchedulerType.CONSTANT_WITH_WARMUP: get_constant_schedule_with_warmup, SchedulerType.PIECEWISE_CONSTANT: get_piecewise_constant_schedule, } def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = 1 , __UpperCAmelCase = 1.0 , __UpperCAmelCase = -1 , ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = SchedulerType(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = TYPE_TO_SCHEDULER_FUNCTION[name] if name == SchedulerType.CONSTANT: return schedule_func(__UpperCAmelCase , last_epoch=__UpperCAmelCase ) if name == SchedulerType.PIECEWISE_CONSTANT: return schedule_func(__UpperCAmelCase , step_rules=__UpperCAmelCase , last_epoch=__UpperCAmelCase ) # All other schedulers require `num_warmup_steps` if num_warmup_steps is None: raise ValueError(f"""{name} requires `num_warmup_steps`, please provide that argument.""" ) if name == SchedulerType.CONSTANT_WITH_WARMUP: return schedule_func(__UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , last_epoch=__UpperCAmelCase ) # All other schedulers require `num_training_steps` if num_training_steps is None: raise ValueError(f"""{name} requires `num_training_steps`, please provide that argument.""" ) if name == SchedulerType.COSINE_WITH_RESTARTS: return schedule_func( __UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , num_training_steps=__UpperCAmelCase , num_cycles=__UpperCAmelCase , last_epoch=__UpperCAmelCase , ) if name == SchedulerType.POLYNOMIAL: return schedule_func( __UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , num_training_steps=__UpperCAmelCase , power=__UpperCAmelCase , last_epoch=__UpperCAmelCase , ) return schedule_func( __UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , num_training_steps=__UpperCAmelCase , last_epoch=__UpperCAmelCase )
13
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging a = logging.get_logger(__name__) a = { "naver-clova-ix/donut-base": "https://huggingface.co/naver-clova-ix/donut-base/resolve/main/config.json", # See all Donut models at https://huggingface.co/models?filter=donut-swin } class __a ( _snake_case ): __UpperCamelCase : Union[str, Any] = 'donut-swin' __UpperCamelCase : Dict = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self : List[str] ,lowerCamelCase : List[Any]=224 ,lowerCamelCase : List[str]=4 ,lowerCamelCase : str=3 ,lowerCamelCase : Tuple=96 ,lowerCamelCase : Dict=[2, 2, 6, 2] ,lowerCamelCase : str=[3, 6, 12, 24] ,lowerCamelCase : List[str]=7 ,lowerCamelCase : Dict=4.0 ,lowerCamelCase : str=True ,lowerCamelCase : Union[str, Any]=0.0 ,lowerCamelCase : Union[str, Any]=0.0 ,lowerCamelCase : str=0.1 ,lowerCamelCase : Dict="gelu" ,lowerCamelCase : Union[str, Any]=False ,lowerCamelCase : Dict=0.02 ,lowerCamelCase : List[Any]=1E-5 ,**lowerCamelCase : Any ,): '''simple docstring''' 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 # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model __SCREAMING_SNAKE_CASE = int(embed_dim * 2 ** (len(lowerCamelCase ) - 1) )
13
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a = {"configuration_sew": ["SEW_PRETRAINED_CONFIG_ARCHIVE_MAP", "SEWConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = [ "SEW_PRETRAINED_MODEL_ARCHIVE_LIST", "SEWForCTC", "SEWForSequenceClassification", "SEWModel", "SEWPreTrainedModel", ] if TYPE_CHECKING: from .configuration_sew import SEW_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_sew import ( SEW_PRETRAINED_MODEL_ARCHIVE_LIST, SEWForCTC, SEWForSequenceClassification, SEWModel, SEWPreTrainedModel, ) else: import sys a = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
13
1
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase = 1000 ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = 3 __SCREAMING_SNAKE_CASE = 0 while a < n: if a % 3 == 0 or a % 5 == 0: result += a elif a % 15 == 0: result -= a a += 1 return result if __name__ == "__main__": print(F'''{solution() = }''')
13
'''simple docstring''' import requests from bsa import BeautifulSoup def __magic_name__ ( __UpperCAmelCase = "AAPL" ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""https://in.finance.yahoo.com/quote/{symbol}?s={symbol}""" __SCREAMING_SNAKE_CASE = BeautifulSoup(requests.get(__UpperCAmelCase ).text , """html.parser""" ) __SCREAMING_SNAKE_CASE = """My(6px) Pos(r) smartphone_Mt(6px)""" return soup.find("""div""" , class_=class_ ).find("""span""" ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(F'''Current {symbol:<4} stock price is {stock_price(symbol):>8}''')
13
1
'''simple docstring''' from __future__ import annotations from collections.abc import Iterator class __a : def __init__( self : Optional[int] ,lowerCamelCase : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = value __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None class __a : def __init__( self : int ,lowerCamelCase : Node ): '''simple docstring''' __SCREAMING_SNAKE_CASE = tree def UpperCAmelCase__ ( self : Optional[int] ,lowerCamelCase : Node | None ): '''simple docstring''' if node is None: return 0 return node.value + ( self.depth_first_search(node.left ) + self.depth_first_search(node.right ) ) def __iter__( self : List[str] ): '''simple docstring''' yield self.depth_first_search(self.tree ) if __name__ == "__main__": import doctest doctest.testmod()
13
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase ) -> bool: '''simple docstring''' if num < 0: return False __SCREAMING_SNAKE_CASE = num __SCREAMING_SNAKE_CASE = 0 while num > 0: __SCREAMING_SNAKE_CASE = rev_num * 10 + (num % 10) num //= 10 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
13
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a = logging.get_logger(__name__) a = { "xlm-roberta-base": "https://huggingface.co/xlm-roberta-base/resolve/main/config.json", "xlm-roberta-large": "https://huggingface.co/xlm-roberta-large/resolve/main/config.json", "xlm-roberta-large-finetuned-conll02-dutch": ( "https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json" ), "xlm-roberta-large-finetuned-conll02-spanish": ( "https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json" ), "xlm-roberta-large-finetuned-conll03-english": ( "https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json" ), "xlm-roberta-large-finetuned-conll03-german": ( "https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json" ), } class __a ( _snake_case ): __UpperCamelCase : List[str] = 'xlm-roberta' def __init__( self : Union[str, Any] ,lowerCamelCase : Dict=3_0522 ,lowerCamelCase : Optional[int]=768 ,lowerCamelCase : Tuple=12 ,lowerCamelCase : List[Any]=12 ,lowerCamelCase : List[Any]=3072 ,lowerCamelCase : str="gelu" ,lowerCamelCase : Optional[int]=0.1 ,lowerCamelCase : List[Any]=0.1 ,lowerCamelCase : int=512 ,lowerCamelCase : Optional[int]=2 ,lowerCamelCase : str=0.02 ,lowerCamelCase : int=1E-1_2 ,lowerCamelCase : int=1 ,lowerCamelCase : Union[str, Any]=0 ,lowerCamelCase : Dict=2 ,lowerCamelCase : Optional[int]="absolute" ,lowerCamelCase : Tuple=True ,lowerCamelCase : List[Any]=None ,**lowerCamelCase : List[str] ,): '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase ,bos_token_id=lowerCamelCase ,eos_token_id=lowerCamelCase ,**lowerCamelCase ) __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 = position_embedding_type __SCREAMING_SNAKE_CASE = use_cache __SCREAMING_SNAKE_CASE = classifier_dropout class __a ( _snake_case ): @property def UpperCAmelCase__ ( self : int ): '''simple docstring''' 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), ] )
13
'''simple docstring''' from __future__ import annotations from collections.abc import Callable a = list[list[float | int]] def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Matrix: '''simple docstring''' __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [[0 for _ in range(size + 1 )] for _ in range(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 for row in range(__UpperCAmelCase ): for col in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = matrix[row][col] __SCREAMING_SNAKE_CASE = vector[row][0] __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 while row < size and col < size: # pivoting __SCREAMING_SNAKE_CASE = max((abs(augmented[rowa][col] ), rowa) for rowa in range(__UpperCAmelCase , __UpperCAmelCase ) )[ 1 ] if augmented[pivot_row][col] == 0: col += 1 continue else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = augmented[pivot_row], augmented[row] for rowa in range(row + 1 , __UpperCAmelCase ): __SCREAMING_SNAKE_CASE = augmented[rowa][col] / augmented[row][col] __SCREAMING_SNAKE_CASE = 0 for cola in range(col + 1 , size + 1 ): augmented[rowa][cola] -= augmented[row][cola] * ratio row += 1 col += 1 # back substitution for col in range(1 , __UpperCAmelCase ): for row in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = augmented[row][col] / augmented[col][col] for cola in range(__UpperCAmelCase , size + 1 ): augmented[row][cola] -= augmented[col][cola] * ratio # round to get rid of numbers like 2.000000000000004 return [ [round(augmented[row][size] / augmented[row][row] , 10 )] for row in range(__UpperCAmelCase ) ] def __magic_name__ ( __UpperCAmelCase ) -> Callable[[int], int]: '''simple docstring''' __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [[0 for _ in range(__UpperCAmelCase )] for _ in range(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = [[0] for _ in range(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 for x_val, y_val in enumerate(__UpperCAmelCase ): for col in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = (x_val + 1) ** (size - col - 1) __SCREAMING_SNAKE_CASE = y_val __SCREAMING_SNAKE_CASE = solve(__UpperCAmelCase , __UpperCAmelCase ) def interpolated_func(__UpperCAmelCase ) -> int: return sum( round(coeffs[x_val][0] ) * (var ** (size - x_val - 1)) for x_val in range(__UpperCAmelCase ) ) return interpolated_func def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' return ( 1 - variable + variable**2 - variable**3 + variable**4 - variable**5 + variable**6 - variable**7 + variable**8 - variable**9 + variable**10 ) def __magic_name__ ( __UpperCAmelCase = question_function , __UpperCAmelCase = 10 ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = [func(__UpperCAmelCase ) for x_val in range(1 , order + 1 )] __SCREAMING_SNAKE_CASE = [ interpolate(data_points[:max_coeff] ) for max_coeff in range(1 , order + 1 ) ] __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 for poly in polynomials: __SCREAMING_SNAKE_CASE = 1 while func(__UpperCAmelCase ) == poly(__UpperCAmelCase ): x_val += 1 ret += poly(__UpperCAmelCase ) return ret if __name__ == "__main__": print(F'''{solution() = }''')
13
1
'''simple docstring''' from __future__ import annotations def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , ) -> None: '''simple docstring''' __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) # If row is equal to the size of the board it means there are a queen in each row in # the current board (possible_board) if row == n: # We convert the variable possible_board that looks like this: [1, 3, 0, 2] to # this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . '] boards.append([""". """ * i + """Q """ + """. """ * (n - 1 - i) for i in possible_board] ) return # We iterate each column in the row to find all possible results in each row for col in range(__UpperCAmelCase ): # We apply that we learned previously. First we check that in the current board # (possible_board) there are not other same value because if there is it means # that there are a collision in vertical. Then we apply the two formulas we # learned before: # # 45º: y - x = b or 45: row - col = b # 135º: y + x = b or row + col = b. # # And we verify if the results of this two formulas not exist in their variables # respectively. (diagonal_right_collisions, diagonal_left_collisions) # # If any or these are True it means there is a collision so we continue to the # next value in the for loop. if ( col in possible_board or row - col in diagonal_right_collisions or row + col in diagonal_left_collisions ): continue # If it is False we call dfs function again and we update the inputs depth_first_search( [*possible_board, col] , [*diagonal_right_collisions, row - col] , [*diagonal_left_collisions, row + col] , __UpperCAmelCase , __UpperCAmelCase , ) def __magic_name__ ( __UpperCAmelCase ) -> None: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] depth_first_search([] , [] , [] , __UpperCAmelCase , __UpperCAmelCase ) # Print all the boards for board in boards: for column in board: print(__UpperCAmelCase ) print("""""" ) print(len(__UpperCAmelCase ) , """solutions were found.""" ) if __name__ == "__main__": import doctest doctest.testmod() n_queens_solution(4)
13
'''simple docstring''' from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax a = logging.get_logger(__name__) @add_end_docstrings(_snake_case ) class __a ( _snake_case ): def __init__( self : Union[str, Any] ,**lowerCamelCase : str ): '''simple docstring''' super().__init__(**lowerCamelCase ) requires_backends(self ,"""vision""" ) self.check_model_type( TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if self.framework == """tf""" else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING ) def __call__( self : Dict ,lowerCamelCase : Union[str, List[str], "Image", List["Image"]] ,**lowerCamelCase : Optional[Any] ): '''simple docstring''' return super().__call__(lowerCamelCase ,**lowerCamelCase ) def UpperCAmelCase__ ( self : Optional[Any] ,**lowerCamelCase : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = {} if "candidate_labels" in kwargs: __SCREAMING_SNAKE_CASE = kwargs["""candidate_labels"""] if "hypothesis_template" in kwargs: __SCREAMING_SNAKE_CASE = kwargs["""hypothesis_template"""] return preprocess_params, {}, {} def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : Union[str, Any]=None ,lowerCamelCase : Union[str, Any]="This is a photo of {}." ): '''simple docstring''' __SCREAMING_SNAKE_CASE = load_image(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.image_processor(images=[image] ,return_tensors=self.framework ) __SCREAMING_SNAKE_CASE = candidate_labels __SCREAMING_SNAKE_CASE = [hypothesis_template.format(lowerCamelCase ) for x in candidate_labels] __SCREAMING_SNAKE_CASE = self.tokenizer(lowerCamelCase ,return_tensors=self.framework ,padding=lowerCamelCase ) __SCREAMING_SNAKE_CASE = [text_inputs] return inputs def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = model_inputs.pop("""candidate_labels""" ) __SCREAMING_SNAKE_CASE = model_inputs.pop("""text_inputs""" ) if isinstance(text_inputs[0] ,lowerCamelCase ): __SCREAMING_SNAKE_CASE = text_inputs[0] else: # Batching case. __SCREAMING_SNAKE_CASE = text_inputs[0][0] __SCREAMING_SNAKE_CASE = self.model(**lowerCamelCase ,**lowerCamelCase ) __SCREAMING_SNAKE_CASE = { """candidate_labels""": candidate_labels, """logits""": outputs.logits_per_image, } return model_outputs def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = model_outputs.pop("""candidate_labels""" ) __SCREAMING_SNAKE_CASE = model_outputs["""logits"""][0] if self.framework == "pt": __SCREAMING_SNAKE_CASE = logits.softmax(dim=-1 ).squeeze(-1 ) __SCREAMING_SNAKE_CASE = probs.tolist() if not isinstance(lowerCamelCase ,lowerCamelCase ): __SCREAMING_SNAKE_CASE = [scores] elif self.framework == "tf": __SCREAMING_SNAKE_CASE = stable_softmax(lowerCamelCase ,axis=-1 ) __SCREAMING_SNAKE_CASE = probs.numpy().tolist() else: raise ValueError(f"""Unsupported framework: {self.framework}""" ) __SCREAMING_SNAKE_CASE = [ {"""score""": score, """label""": candidate_label} for score, candidate_label in sorted(zip(lowerCamelCase ,lowerCamelCase ) ,key=lambda lowerCamelCase : -x[0] ) ] return result
13
1
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer from transformers.testing_utils import require_tokenizers, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor @require_tokenizers @require_vision class __a ( unittest.TestCase ): def UpperCAmelCase__ ( self : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = tempfile.mkdtemp() # fmt: off __SCREAMING_SNAKE_CASE = ["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""] # fmt: on __SCREAMING_SNAKE_CASE = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file ,"""w""" ,encoding="""utf-8""" ) as vocab_writer: vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens] ) ) __SCREAMING_SNAKE_CASE = { """do_resize""": True, """size""": {"""height""": 18, """width""": 18}, """do_normalize""": True, """image_mean""": [0.5, 0.5, 0.5], """image_std""": [0.5, 0.5, 0.5], } __SCREAMING_SNAKE_CASE = os.path.join(self.tmpdirname ,lowerCamelCase ) with open(self.image_processor_file ,"""w""" ,encoding="""utf-8""" ) as fp: json.dump(lowerCamelCase ,lowerCamelCase ) def UpperCAmelCase__ ( self : str ,**lowerCamelCase : Any ): '''simple docstring''' return BertTokenizer.from_pretrained(self.tmpdirname ,**lowerCamelCase ) def UpperCAmelCase__ ( self : Tuple ,**lowerCamelCase : List[Any] ): '''simple docstring''' return ViTImageProcessor.from_pretrained(self.tmpdirname ,**lowerCamelCase ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' shutil.rmtree(self.tmpdirname ) def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = [np.random.randint(255 ,size=(3, 30, 400) ,dtype=np.uinta )] __SCREAMING_SNAKE_CASE = [Image.fromarray(np.moveaxis(lowerCamelCase ,0 ,-1 ) ) for x in image_inputs] return image_inputs def UpperCAmelCase__ ( self : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = self.get_image_processor() __SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor(tokenizer=lowerCamelCase ,image_processor=lowerCamelCase ) processor.save_pretrained(self.tmpdirname ) __SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() ,tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer ,(BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() ,image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor ,lowerCamelCase ) def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor( tokenizer=self.get_tokenizer() ,image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) __SCREAMING_SNAKE_CASE = self.get_tokenizer(bos_token="""(BOS)""" ,eos_token="""(EOS)""" ) __SCREAMING_SNAKE_CASE = self.get_image_processor(do_normalize=lowerCamelCase ,padding_value=1.0 ) __SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor.from_pretrained( self.tmpdirname ,bos_token="""(BOS)""" ,eos_token="""(EOS)""" ,do_normalize=lowerCamelCase ,padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() ,tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer ,(BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() ,image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor ,lowerCamelCase ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.get_image_processor() __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor(tokenizer=lowerCamelCase ,image_processor=lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.prepare_image_inputs() __SCREAMING_SNAKE_CASE = image_processor(lowerCamelCase ,return_tensors="""np""" ) __SCREAMING_SNAKE_CASE = processor(images=lowerCamelCase ,return_tensors="""np""" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() ,input_processor[key].sum() ,delta=1E-2 ) def UpperCAmelCase__ ( self : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.get_image_processor() __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor(tokenizer=lowerCamelCase ,image_processor=lowerCamelCase ) __SCREAMING_SNAKE_CASE = """lower newer""" __SCREAMING_SNAKE_CASE = processor(text=lowerCamelCase ) __SCREAMING_SNAKE_CASE = tokenizer(lowerCamelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] ,encoded_processor[key] ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.get_image_processor() __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor(tokenizer=lowerCamelCase ,image_processor=lowerCamelCase ) __SCREAMING_SNAKE_CASE = """lower newer""" __SCREAMING_SNAKE_CASE = self.prepare_image_inputs() __SCREAMING_SNAKE_CASE = processor(text=lowerCamelCase ,images=lowerCamelCase ) self.assertListEqual(list(inputs.keys() ) ,["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with self.assertRaises(lowerCamelCase ): processor() def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.get_image_processor() __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor(tokenizer=lowerCamelCase ,image_processor=lowerCamelCase ) __SCREAMING_SNAKE_CASE = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] __SCREAMING_SNAKE_CASE = processor.batch_decode(lowerCamelCase ) __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(lowerCamelCase ) self.assertListEqual(lowerCamelCase ,lowerCamelCase ) def UpperCAmelCase__ ( self : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.get_image_processor() __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor(tokenizer=lowerCamelCase ,image_processor=lowerCamelCase ) __SCREAMING_SNAKE_CASE = """lower newer""" __SCREAMING_SNAKE_CASE = self.prepare_image_inputs() __SCREAMING_SNAKE_CASE = processor(text=lowerCamelCase ,images=lowerCamelCase ) self.assertListEqual(list(inputs.keys() ) ,processor.model_input_names )
13
'''simple docstring''' from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers a = [ "python", "tqdm", "regex", "requests", "packaging", "filelock", "numpy", "tokenizers", "huggingface-hub", "safetensors", "accelerate", "pyyaml", ] for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed elif pkg == "accelerate": # must be loaded here, or else tqdm check may fail from .utils import is_accelerate_available # Maybe switch to is_torch_available in the future here so that Accelerate is hard dep of # Transformers with PyTorch if not is_accelerate_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(F'''can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py''') def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase=None ) -> Optional[Any]: '''simple docstring''' require_version(deps[pkg] , __UpperCAmelCase )
13
1
'''simple docstring''' import argparse import copy def __magic_name__ ( __UpperCAmelCase ) -> Optional[int]: '''simple docstring''' __SCREAMING_SNAKE_CASE = {} with open(__UpperCAmelCase ) as f: for line in f: if line.split()[0] not in dict_of_neighbours: __SCREAMING_SNAKE_CASE = [] _list.append([line.split()[1], line.split()[2]] ) __SCREAMING_SNAKE_CASE = _list else: dict_of_neighbours[line.split()[0]].append( [line.split()[1], line.split()[2]] ) if line.split()[1] not in dict_of_neighbours: __SCREAMING_SNAKE_CASE = [] _list.append([line.split()[0], line.split()[2]] ) __SCREAMING_SNAKE_CASE = _list else: dict_of_neighbours[line.split()[1]].append( [line.split()[0], line.split()[2]] ) return dict_of_neighbours def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' with open(__UpperCAmelCase ) as f: __SCREAMING_SNAKE_CASE = f.read(1 ) __SCREAMING_SNAKE_CASE = start_node __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = start_node __SCREAMING_SNAKE_CASE = 0 while visiting not in first_solution: __SCREAMING_SNAKE_CASE = 10000 for k in dict_of_neighbours[visiting]: if int(k[1] ) < int(__UpperCAmelCase ) and k[0] not in first_solution: __SCREAMING_SNAKE_CASE = k[1] __SCREAMING_SNAKE_CASE = k[0] first_solution.append(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = distance_of_first_solution + int(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = best_node first_solution.append(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = 0 for k in dict_of_neighbours[first_solution[-2]]: if k[0] == start_node: break position += 1 __SCREAMING_SNAKE_CASE = ( distance_of_first_solution + int(dict_of_neighbours[first_solution[-2]][position][1] ) - 10000 ) return first_solution, distance_of_first_solution def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Tuple: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] for n in solution[1:-1]: __SCREAMING_SNAKE_CASE = solution.index(__UpperCAmelCase ) for kn in solution[1:-1]: __SCREAMING_SNAKE_CASE = solution.index(__UpperCAmelCase ) if n == kn: continue __SCREAMING_SNAKE_CASE = copy.deepcopy(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = kn __SCREAMING_SNAKE_CASE = n __SCREAMING_SNAKE_CASE = 0 for k in _tmp[:-1]: __SCREAMING_SNAKE_CASE = _tmp[_tmp.index(__UpperCAmelCase ) + 1] for i in dict_of_neighbours[k]: if i[0] == next_node: __SCREAMING_SNAKE_CASE = distance + int(i[1] ) _tmp.append(__UpperCAmelCase ) if _tmp not in neighborhood_of_solution: neighborhood_of_solution.append(_tmp ) __SCREAMING_SNAKE_CASE = len(neighborhood_of_solution[0] ) - 1 neighborhood_of_solution.sort(key=lambda __UpperCAmelCase : x[index_of_last_item_in_the_list] ) return neighborhood_of_solution def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Optional[int]: '''simple docstring''' __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = first_solution __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = distance_of_first_solution __SCREAMING_SNAKE_CASE = solution while count <= iters: __SCREAMING_SNAKE_CASE = find_neighborhood(__UpperCAmelCase , __UpperCAmelCase ) __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = neighborhood[index_of_best_solution] __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) - 1 __SCREAMING_SNAKE_CASE = False while not found: __SCREAMING_SNAKE_CASE = 0 while i < len(__UpperCAmelCase ): if best_solution[i] != solution[i]: __SCREAMING_SNAKE_CASE = best_solution[i] __SCREAMING_SNAKE_CASE = solution[i] break __SCREAMING_SNAKE_CASE = i + 1 if [first_exchange_node, second_exchange_node] not in tabu_list and [ second_exchange_node, first_exchange_node, ] not in tabu_list: tabu_list.append([first_exchange_node, second_exchange_node] ) __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = best_solution[:-1] __SCREAMING_SNAKE_CASE = neighborhood[index_of_best_solution][best_cost_index] if cost < best_cost: __SCREAMING_SNAKE_CASE = cost __SCREAMING_SNAKE_CASE = solution else: __SCREAMING_SNAKE_CASE = index_of_best_solution + 1 __SCREAMING_SNAKE_CASE = neighborhood[index_of_best_solution] if len(__UpperCAmelCase ) >= size: tabu_list.pop(0 ) __SCREAMING_SNAKE_CASE = count + 1 return best_solution_ever, best_cost def __magic_name__ ( __UpperCAmelCase=None ) -> Optional[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = generate_neighbours(args.File ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = generate_first_solution( args.File , __UpperCAmelCase ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = tabu_search( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , args.Iterations , args.Size , ) print(f"""Best solution: {best_sol}, with total distance: {best_cost}.""" ) if __name__ == "__main__": a = argparse.ArgumentParser(description="Tabu Search") parser.add_argument( "-f", "--File", type=str, help="Path to the file containing the data", required=True, ) parser.add_argument( "-i", "--Iterations", type=int, help="How many iterations the algorithm should perform", required=True, ) parser.add_argument( "-s", "--Size", type=int, help="Size of the tabu list", required=True ) # Pass the arguments to main method main(parser.parse_args())
13
'''simple docstring''' import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import numpy as np import pandas as pd from datasets import load_dataset import transformers from transformers import ( AutoConfig, BartForSequenceClassification, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, TapexTokenizer, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.17.0.dev0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/text-classification/requirements.txt") a = logging.getLogger(__name__) @dataclass class __a : __UpperCamelCase : Optional[str] = field( default='tab_fact', metadata={'help': 'The name of the dataset to use (via the datasets library).'} ) __UpperCamelCase : Optional[str] = field( default='tab_fact', metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'}, ) __UpperCamelCase : int = field( default=1024, metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) }, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={'help': 'Overwrite the cached preprocessed datasets or not.'} ) __UpperCamelCase : bool = field( default=_snake_case, metadata={ 'help': ( 'Whether to pad all samples to `max_seq_length`. ' 'If False, will pad the samples dynamically when batching to the maximum length in the batch.' ) }, ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) }, ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) }, ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of prediction examples to this ' 'value if set.' ) }, ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'A csv or a json file containing the training data.'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'A csv or a json file containing the validation data.'} ) __UpperCamelCase : Optional[str] = field(default=_snake_case, metadata={'help': 'A csv or a json file containing the test data.'} ) def UpperCAmelCase__ ( self : int ): '''simple docstring''' if self.dataset_name is not None: pass elif self.train_file is None or self.validation_file is None: raise ValueError("""Need either a GLUE task, a training/validation file or a dataset name.""" ) else: __SCREAMING_SNAKE_CASE = self.train_file.split(""".""" )[-1] assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file." __SCREAMING_SNAKE_CASE = self.validation_file.split(""".""" )[-1] assert ( validation_extension == train_extension ), "`validation_file` should have the same extension (csv or json) as `train_file`." @dataclass class __a : __UpperCamelCase : str = field( default=_snake_case, metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'}, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={'help': 'Whether to use one of the fast tokenizer (backed by the tokenizers library) or not.'}, ) __UpperCamelCase : str = field( default='main', metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'}, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) }, ) def __magic_name__ ( ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = parser.parse_args_into_dataclasses() # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) __SCREAMING_SNAKE_CASE = training_args.get_process_log_level() logger.setLevel(__UpperCAmelCase ) datasets.utils.logging.set_verbosity(__UpperCAmelCase ) transformers.utils.logging.set_verbosity(__UpperCAmelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + f"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(f"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. __SCREAMING_SNAKE_CASE = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: __SCREAMING_SNAKE_CASE = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. """ """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON training and evaluation files (see below) # or specify a GLUE benchmark task (the dataset will be downloaded automatically from the datasets Hub). # # For JSON files, this script will use the `question` column for the input question and `table` column for the corresponding table. # # If the CSVs/JSONs contain only one non-label column, the script does single sentence classification on this # single column. You can easily tweak this behavior (see below) # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. __SCREAMING_SNAKE_CASE = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) else: # Loading a dataset from your local files. # CSV/JSON training and evaluation files are needed. __SCREAMING_SNAKE_CASE = {"""train""": data_args.train_file, """validation""": data_args.validation_file} # Get the test dataset: you can provide your own CSV/JSON test file (see below) # when you use `do_predict` without specifying a GLUE benchmark task. if training_args.do_predict: if data_args.test_file is not None: __SCREAMING_SNAKE_CASE = data_args.train_file.split(""".""" )[-1] __SCREAMING_SNAKE_CASE = data_args.test_file.split(""".""" )[-1] assert ( test_extension == train_extension ), "`test_file` should have the same extension (csv or json) as `train_file`." __SCREAMING_SNAKE_CASE = data_args.test_file else: raise ValueError("""Need either a GLUE task or a test file for `do_predict`.""" ) for key in data_files.keys(): logger.info(f"""load a local file for {key}: {data_files[key]}""" ) if data_args.train_file.endswith(""".csv""" ): # Loading a dataset from local csv files __SCREAMING_SNAKE_CASE = load_dataset("""csv""" , data_files=__UpperCAmelCase , cache_dir=model_args.cache_dir ) else: # Loading a dataset from local json files __SCREAMING_SNAKE_CASE = load_dataset("""json""" , data_files=__UpperCAmelCase , cache_dir=model_args.cache_dir ) # See more about loading any type of standard or custom dataset at # https://huggingface.co/docs/datasets/loading_datasets.html. # Labels __SCREAMING_SNAKE_CASE = raw_datasets["""train"""].features["""label"""].names __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) # Load pretrained model and tokenizer # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. __SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=__UpperCAmelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # load tapex tokenizer __SCREAMING_SNAKE_CASE = TapexTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , add_prefix_space=__UpperCAmelCase , ) __SCREAMING_SNAKE_CASE = BartForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=__UpperCAmelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Padding strategy if data_args.pad_to_max_length: __SCREAMING_SNAKE_CASE = """max_length""" else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch __SCREAMING_SNAKE_CASE = False # Some models have set the order of the labels to use, so let's make sure we do use it. __SCREAMING_SNAKE_CASE = {"""Refused""": 0, """Entailed""": 1} __SCREAMING_SNAKE_CASE = {0: """Refused""", 1: """Entailed"""} if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( f"""The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the""" f"""model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}.""" ) __SCREAMING_SNAKE_CASE = min(data_args.max_seq_length , tokenizer.model_max_length ) def preprocess_tabfact_function(__UpperCAmelCase ): # Tokenize the texts def _convert_table_text_to_pandas(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = [_table_row.split("""#""" ) for _table_row in _table_text.strip("""\n""" ).split("""\n""" )] __SCREAMING_SNAKE_CASE = pd.DataFrame.from_records(_table_content[1:] , columns=_table_content[0] ) return _table_pd __SCREAMING_SNAKE_CASE = examples["""statement"""] __SCREAMING_SNAKE_CASE = list(map(_convert_table_text_to_pandas , examples["""table_text"""] ) ) __SCREAMING_SNAKE_CASE = tokenizer(__UpperCAmelCase , __UpperCAmelCase , padding=__UpperCAmelCase , max_length=__UpperCAmelCase , truncation=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = examples["""label"""] return result with training_args.main_process_first(desc="""dataset map pre-processing""" ): __SCREAMING_SNAKE_CASE = raw_datasets.map( __UpperCAmelCase , batched=__UpperCAmelCase , load_from_cache_file=not data_args.overwrite_cache , desc="""Running tokenizer on dataset""" , ) if training_args.do_train: if "train" not in raw_datasets: raise ValueError("""--do_train requires a train dataset""" ) __SCREAMING_SNAKE_CASE = raw_datasets["""train"""] if data_args.max_train_samples is not None: __SCREAMING_SNAKE_CASE = train_dataset.select(range(data_args.max_train_samples ) ) if training_args.do_eval: if "validation" not in raw_datasets and "validation_matched" not in raw_datasets: raise ValueError("""--do_eval requires a validation dataset""" ) __SCREAMING_SNAKE_CASE = raw_datasets["""validation"""] if data_args.max_eval_samples is not None: __SCREAMING_SNAKE_CASE = eval_dataset.select(range(data_args.max_eval_samples ) ) if training_args.do_predict or data_args.test_file is not None: if "test" not in raw_datasets and "test_matched" not in raw_datasets: raise ValueError("""--do_predict requires a test dataset""" ) __SCREAMING_SNAKE_CASE = raw_datasets["""test"""] if data_args.max_predict_samples is not None: __SCREAMING_SNAKE_CASE = predict_dataset.select(range(data_args.max_predict_samples ) ) # Log a few random samples from the training set: if training_args.do_train: for index in random.sample(range(len(__UpperCAmelCase ) ) , 3 ): logger.info(f"""Sample {index} of the training set: {train_dataset[index]}.""" ) # You can define your custom compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = p.predictions[0] if isinstance(p.predictions , __UpperCAmelCase ) else p.predictions __SCREAMING_SNAKE_CASE = np.argmax(__UpperCAmelCase , axis=1 ) return {"accuracy": (preds == p.label_ids).astype(np.floataa ).mean().item()} # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: __SCREAMING_SNAKE_CASE = default_data_collator elif training_args.fpaa: __SCREAMING_SNAKE_CASE = DataCollatorWithPadding(__UpperCAmelCase , pad_to_multiple_of=8 ) else: __SCREAMING_SNAKE_CASE = None # Initialize our Trainer __SCREAMING_SNAKE_CASE = Trainer( model=__UpperCAmelCase , args=__UpperCAmelCase , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=__UpperCAmelCase , tokenizer=__UpperCAmelCase , data_collator=__UpperCAmelCase , ) # Training if training_args.do_train: __SCREAMING_SNAKE_CASE = None if training_args.resume_from_checkpoint is not None: __SCREAMING_SNAKE_CASE = training_args.resume_from_checkpoint elif last_checkpoint is not None: __SCREAMING_SNAKE_CASE = last_checkpoint __SCREAMING_SNAKE_CASE = trainer.train(resume_from_checkpoint=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = train_result.metrics __SCREAMING_SNAKE_CASE = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(__UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = min(__UpperCAmelCase , len(__UpperCAmelCase ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics("""train""" , __UpperCAmelCase ) trainer.save_metrics("""train""" , __UpperCAmelCase ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info("""*** Evaluate ***""" ) __SCREAMING_SNAKE_CASE = trainer.evaluate(eval_dataset=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = min(__UpperCAmelCase , len(__UpperCAmelCase ) ) trainer.log_metrics("""eval""" , __UpperCAmelCase ) trainer.save_metrics("""eval""" , __UpperCAmelCase ) if training_args.do_predict: logger.info("""*** Predict ***""" ) # Removing the `label` columns because it contains -1 and Trainer won't like that. __SCREAMING_SNAKE_CASE = predict_dataset.remove_columns("""label""" ) __SCREAMING_SNAKE_CASE = trainer.predict(__UpperCAmelCase , metric_key_prefix="""predict""" ).predictions __SCREAMING_SNAKE_CASE = np.argmax(__UpperCAmelCase , axis=1 ) __SCREAMING_SNAKE_CASE = os.path.join(training_args.output_dir , """predict_results_tabfact.txt""" ) if trainer.is_world_process_zero(): with open(__UpperCAmelCase , """w""" ) as writer: logger.info("""***** Predict Results *****""" ) writer.write("""index\tprediction\n""" ) for index, item in enumerate(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = label_list[item] writer.write(f"""{index}\t{item}\n""" ) __SCREAMING_SNAKE_CASE = {"""finetuned_from""": model_args.model_name_or_path, """tasks""": """text-classification"""} if training_args.push_to_hub: trainer.push_to_hub(**__UpperCAmelCase ) else: trainer.create_model_card(**__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase ) -> Any: '''simple docstring''' main() if __name__ == "__main__": main()
13
1
'''simple docstring''' import copy import os from collections import OrderedDict from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Union if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a = logging.get_logger(__name__) a = { "google/owlvit-base-patch32": "https://huggingface.co/google/owlvit-base-patch32/resolve/main/config.json", "google/owlvit-base-patch16": "https://huggingface.co/google/owlvit-base-patch16/resolve/main/config.json", "google/owlvit-large-patch14": "https://huggingface.co/google/owlvit-large-patch14/resolve/main/config.json", } class __a ( _snake_case ): __UpperCamelCase : Optional[Any] = 'owlvit_text_model' def __init__( self : Dict ,lowerCamelCase : Optional[int]=4_9408 ,lowerCamelCase : List[Any]=512 ,lowerCamelCase : Dict=2048 ,lowerCamelCase : str=12 ,lowerCamelCase : Tuple=8 ,lowerCamelCase : int=16 ,lowerCamelCase : Optional[int]="quick_gelu" ,lowerCamelCase : int=1E-5 ,lowerCamelCase : str=0.0 ,lowerCamelCase : Tuple=0.02 ,lowerCamelCase : str=1.0 ,lowerCamelCase : Union[str, Any]=0 ,lowerCamelCase : Optional[int]=4_9406 ,lowerCamelCase : Dict=4_9407 ,**lowerCamelCase : Tuple ,): '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase ,bos_token_id=lowerCamelCase ,eos_token_id=lowerCamelCase ,**lowerCamelCase ) __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = initializer_factor @classmethod def UpperCAmelCase__ ( cls : List[Any] ,lowerCamelCase : Union[str, os.PathLike] ,**lowerCamelCase : Optional[int] ): '''simple docstring''' cls._set_token_in_kwargs(lowerCamelCase ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = cls.get_config_dict(lowerCamelCase ,**lowerCamelCase ) # get the text config dict if we are loading from OwlViTConfig if config_dict.get("""model_type""" ) == "owlvit": __SCREAMING_SNAKE_CASE = config_dict["""text_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(lowerCamelCase ,**lowerCamelCase ) class __a ( _snake_case ): __UpperCamelCase : int = 'owlvit_vision_model' def __init__( self : Optional[Any] ,lowerCamelCase : List[str]=768 ,lowerCamelCase : Any=3072 ,lowerCamelCase : Dict=12 ,lowerCamelCase : str=12 ,lowerCamelCase : Union[str, Any]=3 ,lowerCamelCase : Dict=768 ,lowerCamelCase : List[Any]=32 ,lowerCamelCase : int="quick_gelu" ,lowerCamelCase : int=1E-5 ,lowerCamelCase : str=0.0 ,lowerCamelCase : str=0.02 ,lowerCamelCase : Union[str, Any]=1.0 ,**lowerCamelCase : Any ,): '''simple docstring''' super().__init__(**lowerCamelCase ) __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = num_channels __SCREAMING_SNAKE_CASE = image_size __SCREAMING_SNAKE_CASE = patch_size __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = initializer_factor @classmethod def UpperCAmelCase__ ( cls : str ,lowerCamelCase : Union[str, os.PathLike] ,**lowerCamelCase : List[Any] ): '''simple docstring''' cls._set_token_in_kwargs(lowerCamelCase ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = cls.get_config_dict(lowerCamelCase ,**lowerCamelCase ) # get the vision config dict if we are loading from OwlViTConfig if config_dict.get("""model_type""" ) == "owlvit": __SCREAMING_SNAKE_CASE = 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(lowerCamelCase ,**lowerCamelCase ) class __a ( _snake_case ): __UpperCamelCase : Dict = 'owlvit' __UpperCamelCase : Tuple = True def __init__( self : Optional[int] ,lowerCamelCase : Tuple=None ,lowerCamelCase : Optional[Any]=None ,lowerCamelCase : int=512 ,lowerCamelCase : Union[str, Any]=2.6_592 ,lowerCamelCase : List[Any]=True ,**lowerCamelCase : List[str] ,): '''simple docstring''' super().__init__(**lowerCamelCase ) if text_config is None: __SCREAMING_SNAKE_CASE = {} logger.info("""text_config is None. Initializing the OwlViTTextConfig with default values.""" ) if vision_config is None: __SCREAMING_SNAKE_CASE = {} logger.info("""vision_config is None. initializing the OwlViTVisionConfig with default values.""" ) __SCREAMING_SNAKE_CASE = OwlViTTextConfig(**lowerCamelCase ) __SCREAMING_SNAKE_CASE = OwlViTVisionConfig(**lowerCamelCase ) __SCREAMING_SNAKE_CASE = projection_dim __SCREAMING_SNAKE_CASE = logit_scale_init_value __SCREAMING_SNAKE_CASE = return_dict __SCREAMING_SNAKE_CASE = 1.0 @classmethod def UpperCAmelCase__ ( cls : Any ,lowerCamelCase : Union[str, os.PathLike] ,**lowerCamelCase : Optional[Any] ): '''simple docstring''' cls._set_token_in_kwargs(lowerCamelCase ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = cls.get_config_dict(lowerCamelCase ,**lowerCamelCase ) 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(lowerCamelCase ,**lowerCamelCase ) @classmethod def UpperCAmelCase__ ( cls : int ,lowerCamelCase : Dict ,lowerCamelCase : Dict ,**lowerCamelCase : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = text_config __SCREAMING_SNAKE_CASE = vision_config return cls.from_dict(lowerCamelCase ,**lowerCamelCase ) def UpperCAmelCase__ ( self : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) __SCREAMING_SNAKE_CASE = self.text_config.to_dict() __SCREAMING_SNAKE_CASE = self.vision_config.to_dict() __SCREAMING_SNAKE_CASE = self.__class__.model_type return output class __a ( _snake_case ): @property def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ] ) @property def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' return OrderedDict( [ ("""logits_per_image""", {0: """batch"""}), ("""logits_per_text""", {0: """batch"""}), ("""text_embeds""", {0: """batch"""}), ("""image_embeds""", {0: """batch"""}), ] ) @property def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' return 1E-4 def UpperCAmelCase__ ( self : int ,lowerCamelCase : "ProcessorMixin" ,lowerCamelCase : int = -1 ,lowerCamelCase : int = -1 ,lowerCamelCase : Optional["TensorType"] = None ,): '''simple docstring''' __SCREAMING_SNAKE_CASE = super().generate_dummy_inputs( processor.tokenizer ,batch_size=lowerCamelCase ,seq_length=lowerCamelCase ,framework=lowerCamelCase ) __SCREAMING_SNAKE_CASE = super().generate_dummy_inputs( processor.image_processor ,batch_size=lowerCamelCase ,framework=lowerCamelCase ) return {**text_input_dict, **image_input_dict} @property def UpperCAmelCase__ ( self : str ): '''simple docstring''' return 14
13
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_flax_available, is_torch_available, is_transformers_available, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .multicontrolnet import MultiControlNetModel from .pipeline_controlnet import StableDiffusionControlNetPipeline from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline if is_transformers_available() and is_flax_available(): from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline
13
1
'''simple docstring''' import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging a = logging.get_logger(__name__) class __a ( _snake_case ): def __init__( self : Optional[Any] ,lowerCamelCase : Union[List[ControlNetModel], Tuple[ControlNetModel]] ): '''simple docstring''' super().__init__() __SCREAMING_SNAKE_CASE = nn.ModuleList(lowerCamelCase ) def UpperCAmelCase__ ( self : Optional[int] ,lowerCamelCase : torch.FloatTensor ,lowerCamelCase : Union[torch.Tensor, float, int] ,lowerCamelCase : torch.Tensor ,lowerCamelCase : List[torch.tensor] ,lowerCamelCase : List[float] ,lowerCamelCase : Optional[torch.Tensor] = None ,lowerCamelCase : Optional[torch.Tensor] = None ,lowerCamelCase : Optional[torch.Tensor] = None ,lowerCamelCase : Optional[Dict[str, Any]] = None ,lowerCamelCase : bool = False ,lowerCamelCase : bool = True ,): '''simple docstring''' for i, (image, scale, controlnet) in enumerate(zip(lowerCamelCase ,lowerCamelCase ,self.nets ) ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = controlnet( lowerCamelCase ,lowerCamelCase ,lowerCamelCase ,lowerCamelCase ,lowerCamelCase ,lowerCamelCase ,lowerCamelCase ,lowerCamelCase ,lowerCamelCase ,lowerCamelCase ,lowerCamelCase ,) # merge samples if i == 0: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = down_samples, mid_sample else: __SCREAMING_SNAKE_CASE = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(lowerCamelCase ,lowerCamelCase ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def UpperCAmelCase__ ( self : List[str] ,lowerCamelCase : Union[str, os.PathLike] ,lowerCamelCase : bool = True ,lowerCamelCase : Callable = None ,lowerCamelCase : bool = False ,lowerCamelCase : Optional[str] = None ,): '''simple docstring''' __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = save_directory for controlnet in self.nets: controlnet.save_pretrained( lowerCamelCase ,is_main_process=lowerCamelCase ,save_function=lowerCamelCase ,safe_serialization=lowerCamelCase ,variant=lowerCamelCase ,) idx += 1 __SCREAMING_SNAKE_CASE = model_path_to_save + f"""_{idx}""" @classmethod def UpperCAmelCase__ ( cls : List[Any] ,lowerCamelCase : Optional[Union[str, os.PathLike]] ,**lowerCamelCase : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... __SCREAMING_SNAKE_CASE = pretrained_model_path while os.path.isdir(lowerCamelCase ): __SCREAMING_SNAKE_CASE = ControlNetModel.from_pretrained(lowerCamelCase ,**lowerCamelCase ) controlnets.append(lowerCamelCase ) idx += 1 __SCREAMING_SNAKE_CASE = pretrained_model_path + f"""_{idx}""" logger.info(f"""{len(lowerCamelCase )} controlnets loaded from {pretrained_model_path}.""" ) if len(lowerCamelCase ) == 0: raise ValueError( f"""No ControlNets found under {os.path.dirname(lowerCamelCase )}. Expected at least {pretrained_model_path + '_0'}.""" ) return cls(lowerCamelCase )
13
'''simple docstring''' import requests from bsa import BeautifulSoup def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = BeautifulSoup(requests.get(__UpperCAmelCase , params=__UpperCAmelCase ).content , """html.parser""" ) __SCREAMING_SNAKE_CASE = soup.find("""div""" , attrs={"""class""": """gs_ri"""} ) __SCREAMING_SNAKE_CASE = div.find("""div""" , attrs={"""class""": """gs_fl"""} ).find_all("""a""" ) return anchors[2].get_text() if __name__ == "__main__": a = { "title": ( "Precisely geometry controlled microsupercapacitors for ultrahigh areal " "capacitance, volumetric capacitance, and energy density" ), "journal": "Chem. Mater.", "volume": 30, "pages": "3979-3990", "year": 2018, "hl": "en", } print(get_citation("https://scholar.google.com/scholar_lookup", params=params))
13
1
'''simple docstring''' from arguments import InitializationArguments from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer, HfArgumentParser # Configuration a = HfArgumentParser(InitializationArguments) a = parser.parse_args() # Load codeparrot tokenizer trained for Python code tokenization a = AutoTokenizer.from_pretrained(args.tokenizer_name) # Config: "scale_attn_by_layer_idx" and "reorder_and_upcast_attn" are Mistral stability tweaks a = { "vocab_size": len(tokenizer), "scale_attn_by_inverse_layer_idx": True, "reorder_and_upcast_attn": True, } # Load model config (GPT-2 large in this case) a = AutoConfig.from_pretrained(args.config_name, **config_kwargs) # Initialize new model with config a = AutoModelForCausalLM.from_config(config) # Save model to the hub model.save_pretrained(args.model_name, push_to_hub=args.push_to_hub)
13
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a = logging.get_logger(__name__) a = { "camembert-base": "https://huggingface.co/camembert-base/resolve/main/config.json", "umberto-commoncrawl-cased-v1": ( "https://huggingface.co/Musixmatch/umberto-commoncrawl-cased-v1/resolve/main/config.json" ), "umberto-wikipedia-uncased-v1": ( "https://huggingface.co/Musixmatch/umberto-wikipedia-uncased-v1/resolve/main/config.json" ), } class __a ( _snake_case ): __UpperCamelCase : Tuple = 'camembert' def __init__( self : int ,lowerCamelCase : List[Any]=3_0522 ,lowerCamelCase : List[Any]=768 ,lowerCamelCase : str=12 ,lowerCamelCase : List[str]=12 ,lowerCamelCase : Optional[Any]=3072 ,lowerCamelCase : Tuple="gelu" ,lowerCamelCase : List[str]=0.1 ,lowerCamelCase : Tuple=0.1 ,lowerCamelCase : Union[str, Any]=512 ,lowerCamelCase : Dict=2 ,lowerCamelCase : Tuple=0.02 ,lowerCamelCase : List[Any]=1E-1_2 ,lowerCamelCase : Union[str, Any]=1 ,lowerCamelCase : Optional[Any]=0 ,lowerCamelCase : List[Any]=2 ,lowerCamelCase : List[str]="absolute" ,lowerCamelCase : int=True ,lowerCamelCase : Any=None ,**lowerCamelCase : Optional[Any] ,): '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase ,bos_token_id=lowerCamelCase ,eos_token_id=lowerCamelCase ,**lowerCamelCase ) __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 = position_embedding_type __SCREAMING_SNAKE_CASE = use_cache __SCREAMING_SNAKE_CASE = classifier_dropout class __a ( _snake_case ): @property def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' 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), ] )
13
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a = { "configuration_blip_2": [ "BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP", "Blip2Config", "Blip2QFormerConfig", "Blip2VisionConfig", ], "processing_blip_2": ["Blip2Processor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = [ "BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST", "Blip2Model", "Blip2QFormerModel", "Blip2PreTrainedModel", "Blip2ForConditionalGeneration", "Blip2VisionModel", ] if TYPE_CHECKING: from .configuration_blip_a import ( BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipaConfig, BlipaQFormerConfig, BlipaVisionConfig, ) from .processing_blip_a import BlipaProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip_a import ( BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST, BlipaForConditionalGeneration, BlipaModel, BlipaPreTrainedModel, BlipaQFormerModel, BlipaVisionModel, ) else: import sys a = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
13
'''simple docstring''' import inspect import unittest import numpy as np from transformers import ViTConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor if is_flax_available(): import jax from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel class __a ( unittest.TestCase ): def __init__( self : Optional[int] ,lowerCamelCase : str ,lowerCamelCase : List[str]=13 ,lowerCamelCase : Optional[Any]=30 ,lowerCamelCase : Dict=2 ,lowerCamelCase : List[Any]=3 ,lowerCamelCase : List[str]=True ,lowerCamelCase : str=True ,lowerCamelCase : Optional[int]=32 ,lowerCamelCase : Dict=5 ,lowerCamelCase : Optional[int]=4 ,lowerCamelCase : List[Any]=37 ,lowerCamelCase : Union[str, Any]="gelu" ,lowerCamelCase : List[Any]=0.1 ,lowerCamelCase : Any=0.1 ,lowerCamelCase : str=10 ,lowerCamelCase : Dict=0.02 ,): '''simple docstring''' __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 # 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 UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __SCREAMING_SNAKE_CASE = 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 ,) return config, pixel_values def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : int ,lowerCamelCase : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = FlaxViTModel(config=lowerCamelCase ) __SCREAMING_SNAKE_CASE = model(lowerCamelCase ) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) __SCREAMING_SNAKE_CASE = (self.image_size, self.image_size) __SCREAMING_SNAKE_CASE = (self.patch_size, self.patch_size) __SCREAMING_SNAKE_CASE = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, num_patches + 1, self.hidden_size) ) def UpperCAmelCase__ ( self : Union[str, Any] ,lowerCamelCase : Optional[int] ,lowerCamelCase : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.type_sequence_label_size __SCREAMING_SNAKE_CASE = FlaxViTForImageClassification(config=lowerCamelCase ) __SCREAMING_SNAKE_CASE = model(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 = FlaxViTForImageClassification(lowerCamelCase ) __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) __SCREAMING_SNAKE_CASE = model(lowerCamelCase ) def UpperCAmelCase__ ( self : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ) = config_and_inputs __SCREAMING_SNAKE_CASE = {"""pixel_values""": pixel_values} return config, inputs_dict @require_flax class __a ( _snake_case, unittest.TestCase ): __UpperCamelCase : Any = (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else () def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = FlaxViTModelTester(self ) __SCREAMING_SNAKE_CASE = ConfigTester(self ,config_class=lowerCamelCase ,has_text_modality=lowerCamelCase ,hidden_size=37 ) def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __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 UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __SCREAMING_SNAKE_CASE = self._prepare_for_class(lowerCamelCase ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = model_class(lowerCamelCase ) @jax.jit def model_jitted(lowerCamelCase : int ,**lowerCamelCase : Union[str, Any] ): return model(pixel_values=lowerCamelCase ,**lowerCamelCase ) with self.subTest("""JIT Enabled""" ): __SCREAMING_SNAKE_CASE = model_jitted(**lowerCamelCase ).to_tuple() with self.subTest("""JIT Disabled""" ): with jax.disable_jit(): __SCREAMING_SNAKE_CASE = model_jitted(**lowerCamelCase ).to_tuple() self.assertEqual(len(lowerCamelCase ) ,len(lowerCamelCase ) ) for jitted_output, output in zip(lowerCamelCase ,lowerCamelCase ): self.assertEqual(jitted_output.shape ,output.shape ) @slow def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' for model_class_name in self.all_model_classes: __SCREAMING_SNAKE_CASE = model_class_name.from_pretrained("""google/vit-base-patch16-224""" ) __SCREAMING_SNAKE_CASE = model(np.ones((1, 3, 224, 224) ) ) self.assertIsNotNone(lowerCamelCase )
13
1
'''simple docstring''' import timeit import numpy as np import datasets from datasets.arrow_writer import ArrowWriter from datasets.features.features import _ArrayXD def __magic_name__ ( __UpperCAmelCase ) -> Tuple: '''simple docstring''' def wrapper(*__UpperCAmelCase , **__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = timeit.default_timer() __SCREAMING_SNAKE_CASE = func(*__UpperCAmelCase , **__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = timeit.default_timer() - starttime return delta __SCREAMING_SNAKE_CASE = func.__name__ return wrapper def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase=100 , __UpperCAmelCase=None ) -> Optional[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = seq_shapes or {} for i in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = {} for col_id, (k, v) in enumerate(features.items() ): if isinstance(__UpperCAmelCase , _ArrayXD ): __SCREAMING_SNAKE_CASE = np.random.rand(*v.shape ).astype(v.dtype ) elif isinstance(__UpperCAmelCase , datasets.Value ): if v.dtype == "string": __SCREAMING_SNAKE_CASE = """The small grey turtle was surprisingly fast when challenged.""" else: __SCREAMING_SNAKE_CASE = np.random.randint(10 , size=1 ).astype(v.dtype ).item() elif isinstance(__UpperCAmelCase , datasets.Sequence ): while isinstance(__UpperCAmelCase , datasets.Sequence ): __SCREAMING_SNAKE_CASE = v.feature __SCREAMING_SNAKE_CASE = seq_shapes[k] __SCREAMING_SNAKE_CASE = np.random.rand(*__UpperCAmelCase ).astype(v.dtype ) __SCREAMING_SNAKE_CASE = data dummy_data.append((i, example) ) return dummy_data def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=100 , __UpperCAmelCase=None ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = generate_examples(__UpperCAmelCase , num_examples=__UpperCAmelCase , seq_shapes=__UpperCAmelCase ) with ArrowWriter(features=__UpperCAmelCase , path=__UpperCAmelCase ) as writer: for key, record in dummy_data: __SCREAMING_SNAKE_CASE = features.encode_example(__UpperCAmelCase ) writer.write(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = writer.finalize() if not num_final_examples == num_examples: raise ValueError( f"""Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}.""" ) __SCREAMING_SNAKE_CASE = datasets.Dataset.from_file(filename=__UpperCAmelCase , info=datasets.DatasetInfo(features=__UpperCAmelCase ) ) return dataset
13
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging a = logging.get_logger(__name__) a = { "asapp/sew-tiny-100k": "https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json", # See all SEW models at https://huggingface.co/models?filter=sew } class __a ( _snake_case ): __UpperCamelCase : Tuple = 'sew' def __init__( self : str ,lowerCamelCase : Any=32 ,lowerCamelCase : str=768 ,lowerCamelCase : str=12 ,lowerCamelCase : Union[str, Any]=12 ,lowerCamelCase : Union[str, Any]=3072 ,lowerCamelCase : int=2 ,lowerCamelCase : Union[str, Any]="gelu" ,lowerCamelCase : Tuple=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : Any=0.0 ,lowerCamelCase : Optional[Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : Optional[Any]=0.02 ,lowerCamelCase : List[str]=1E-5 ,lowerCamelCase : Tuple="group" ,lowerCamelCase : Optional[Any]="gelu" ,lowerCamelCase : List[str]=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) ,lowerCamelCase : Any=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) ,lowerCamelCase : Dict=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) ,lowerCamelCase : Optional[int]=False ,lowerCamelCase : Dict=128 ,lowerCamelCase : Union[str, Any]=16 ,lowerCamelCase : List[Any]=True ,lowerCamelCase : List[Any]=0.05 ,lowerCamelCase : Optional[int]=10 ,lowerCamelCase : Any=2 ,lowerCamelCase : Any=0.0 ,lowerCamelCase : Tuple=10 ,lowerCamelCase : str=0 ,lowerCamelCase : Tuple="mean" ,lowerCamelCase : int=False ,lowerCamelCase : Dict=False ,lowerCamelCase : Optional[int]=256 ,lowerCamelCase : str=0 ,lowerCamelCase : Tuple=1 ,lowerCamelCase : Tuple=2 ,**lowerCamelCase : Union[str, Any] ,): '''simple docstring''' super().__init__(**lowerCamelCase ,pad_token_id=lowerCamelCase ,bos_token_id=lowerCamelCase ,eos_token_id=lowerCamelCase ) __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = feat_extract_norm __SCREAMING_SNAKE_CASE = feat_extract_activation __SCREAMING_SNAKE_CASE = list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = conv_bias __SCREAMING_SNAKE_CASE = num_conv_pos_embeddings __SCREAMING_SNAKE_CASE = num_conv_pos_embedding_groups __SCREAMING_SNAKE_CASE = len(self.conv_dim ) __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = squeeze_factor __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = hidden_dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = feat_proj_dropout __SCREAMING_SNAKE_CASE = final_dropout __SCREAMING_SNAKE_CASE = layerdrop __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect.""" """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,""" f"""but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)""" f"""= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __SCREAMING_SNAKE_CASE = apply_spec_augment __SCREAMING_SNAKE_CASE = mask_time_prob __SCREAMING_SNAKE_CASE = mask_time_length __SCREAMING_SNAKE_CASE = mask_time_min_masks __SCREAMING_SNAKE_CASE = mask_feature_prob __SCREAMING_SNAKE_CASE = mask_feature_length __SCREAMING_SNAKE_CASE = mask_feature_min_masks # ctc loss __SCREAMING_SNAKE_CASE = ctc_loss_reduction __SCREAMING_SNAKE_CASE = ctc_zero_infinity # sequence classification __SCREAMING_SNAKE_CASE = use_weighted_layer_sum __SCREAMING_SNAKE_CASE = classifier_proj_size @property def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' return functools.reduce(operator.mul ,self.conv_stride ,1 )
13
1
'''simple docstring''' from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo a = "\\n@misc{wu2016googles,\n title={Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation},\n author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey\n and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin\n Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto\n Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and\n Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes\n and Jeffrey Dean},\n year={2016},\n eprint={1609.08144},\n archivePrefix={arXiv},\n primaryClass={cs.CL}\n}\n" a = "\\nThe BLEU score has some undesirable properties when used for single\nsentences, as it was designed to be a corpus measure. We therefore\nuse a slightly different score for our RL experiments which we call\nthe 'GLEU score'. For the GLEU score, we record all sub-sequences of\n1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then\ncompute a recall, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the target (ground truth) sequence,\nand a precision, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the generated output sequence. Then\nGLEU score is simply the minimum of recall and precision. This GLEU\nscore's range is always between 0 (no matches) and 1 (all match) and\nit is symmetrical when switching output and target. According to\nour experiments, GLEU score correlates quite well with the BLEU\nmetric on a corpus level but does not have its drawbacks for our per\nsentence reward objective.\n" a = "\\nComputes corpus-level Google BLEU (GLEU) score of translated segments against one or more references.\nInstead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching\ntokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values.\n\nArgs:\n predictions (list of str): list of translations to score.\n Each translation should be tokenized into a list of tokens.\n references (list of list of str): list of lists of references for each translation.\n Each reference should be tokenized into a list of tokens.\n min_len (int): The minimum order of n-gram this function should extract. Defaults to 1.\n max_len (int): The maximum order of n-gram this function should extract. Defaults to 4.\n\nReturns:\n 'google_bleu': google_bleu score\n\nExamples:\n Example 1:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.44\n\n Example 2:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.61\n\n Example 3:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.53\n\n Example 4:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.4\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class __a ( datasets.Metric ): def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""string""" ,id="""token""" ) ,id="""sequence""" ), """references""": datasets.Sequence( datasets.Sequence(datasets.Value("""string""" ,id="""token""" ) ,id="""sequence""" ) ,id="""references""" ), } ) ,) def UpperCAmelCase__ ( self : Optional[int] ,lowerCamelCase : List[List[List[str]]] ,lowerCamelCase : List[List[str]] ,lowerCamelCase : int = 1 ,lowerCamelCase : int = 4 ,): '''simple docstring''' return { "google_bleu": gleu_score.corpus_gleu( list_of_references=lowerCamelCase ,hypotheses=lowerCamelCase ,min_len=lowerCamelCase ,max_len=lowerCamelCase ) }
13
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase = 1 , __UpperCAmelCase = 1000 ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 0 for divide_by_number in range(__UpperCAmelCase , digit + 1 ): __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = numerator for _ in range(1 , digit + 1 ): if now_divide in has_been_divided: if longest_list_length < len(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = divide_by_number else: has_been_divided.append(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = now_divide * 10 % divide_by_number return the_digit # Tests if __name__ == "__main__": import doctest doctest.testmod()
13
1
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase = 100 ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = n * (n + 1) * (2 * n + 1) / 6 __SCREAMING_SNAKE_CASE = (n * (n + 1) / 2) ** 2 return int(square_of_sum - sum_of_squares ) if __name__ == "__main__": print(F'''{solution() = }''')
13
'''simple docstring''' 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 if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor class __a ( unittest.TestCase ): def __init__( self : List[Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : List[str]=7 ,lowerCamelCase : List[str]=3 ,lowerCamelCase : List[str]=18 ,lowerCamelCase : Any=30 ,lowerCamelCase : Optional[Any]=400 ,lowerCamelCase : Optional[Any]=True ,lowerCamelCase : Optional[Any]=None ,lowerCamelCase : Optional[int]=True ,lowerCamelCase : int=None ,lowerCamelCase : str=True ,lowerCamelCase : Dict=[0.48_145_466, 0.4_578_275, 0.40_821_073] ,lowerCamelCase : List[str]=[0.26_862_954, 0.26_130_258, 0.27_577_711] ,lowerCamelCase : Tuple=True ,): '''simple docstring''' __SCREAMING_SNAKE_CASE = size if size is not None else {"""height""": 224, """width""": 224} __SCREAMING_SNAKE_CASE = crop_size if crop_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 = do_center_crop __SCREAMING_SNAKE_CASE = crop_size __SCREAMING_SNAKE_CASE = do_normalize __SCREAMING_SNAKE_CASE = image_mean __SCREAMING_SNAKE_CASE = image_std __SCREAMING_SNAKE_CASE = do_convert_rgb def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_convert_rgb": self.do_convert_rgb, } def UpperCAmelCase__ ( self : int ,lowerCamelCase : Union[str, Any]=False ,lowerCamelCase : str=False ,lowerCamelCase : str=False ): '''simple docstring''' assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time" if equal_resolution: __SCREAMING_SNAKE_CASE = [] for i in range(self.batch_size ): image_inputs.append( np.random.randint( 255 ,size=(self.num_channels, self.max_resolution, self.max_resolution) ,dtype=np.uinta ) ) else: __SCREAMING_SNAKE_CASE = [] for i in range(self.batch_size ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = np.random.choice(np.arange(self.min_resolution ,self.max_resolution ) ,2 ) image_inputs.append(np.random.randint(255 ,size=(self.num_channels, width, height) ,dtype=np.uinta ) ) if not numpify and not torchify: # PIL expects the channel dimension as last dimension __SCREAMING_SNAKE_CASE = [Image.fromarray(np.moveaxis(lowerCamelCase ,0 ,-1 ) ) for x in image_inputs] if torchify: __SCREAMING_SNAKE_CASE = [torch.from_numpy(lowerCamelCase ) for x in image_inputs] return image_inputs @require_torch @require_vision class __a ( _snake_case, unittest.TestCase ): __UpperCamelCase : int = ChineseCLIPImageProcessor if is_vision_available() else None def UpperCAmelCase__ ( self : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = ChineseCLIPImageProcessingTester(self ,do_center_crop=lowerCamelCase ) @property def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __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 ,"""do_center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_normalize""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_mean""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_std""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_convert_rgb""" ) ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{"""height""": 224, """width""": 224} ) self.assertEqual(image_processor.crop_size ,{"""height""": 18, """width""": 18} ) __SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ,crop_size=84 ) self.assertEqual(image_processor.size ,{"""shortest_edge""": 42} ) self.assertEqual(image_processor.crop_size ,{"""height""": 84, """width""": 84} ) def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' pass def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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""" ).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 __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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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.crop_size["""height"""], self.image_processor_tester.crop_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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def UpperCAmelCase__ ( self : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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.crop_size["""height"""], self.image_processor_tester.crop_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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) @require_torch @require_vision class __a ( _snake_case, unittest.TestCase ): __UpperCamelCase : Optional[int] = ChineseCLIPImageProcessor if is_vision_available() else None def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = ChineseCLIPImageProcessingTester(self ,num_channels=4 ,do_center_crop=lowerCamelCase ) __SCREAMING_SNAKE_CASE = 3 @property def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase__ ( self : int ): '''simple docstring''' __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 ,"""do_center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_normalize""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_mean""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_std""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_convert_rgb""" ) ) def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' pass def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_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.expected_encoded_image_num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,)
13
1
'''simple docstring''' from statistics import mean import numpy as np def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> list: '''simple docstring''' __SCREAMING_SNAKE_CASE = 0 # Number of processes finished __SCREAMING_SNAKE_CASE = 0 # Displays the finished process. # If it is 0, the performance is completed if it is 1, before the performance. __SCREAMING_SNAKE_CASE = [0] * no_of_process # List to include calculation results __SCREAMING_SNAKE_CASE = [0] * no_of_process # Sort by arrival time. __SCREAMING_SNAKE_CASE = [burst_time[i] for i in np.argsort(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = [process_name[i] for i in np.argsort(__UpperCAmelCase )] arrival_time.sort() while no_of_process > finished_process_count: __SCREAMING_SNAKE_CASE = 0 while finished_process[i] == 1: i += 1 if current_time < arrival_time[i]: __SCREAMING_SNAKE_CASE = arrival_time[i] __SCREAMING_SNAKE_CASE = 0 # Index showing the location of the process being performed __SCREAMING_SNAKE_CASE = 0 # Saves the current response ratio. __SCREAMING_SNAKE_CASE = 0 for i in range(0 , __UpperCAmelCase ): if finished_process[i] == 0 and arrival_time[i] <= current_time: __SCREAMING_SNAKE_CASE = (burst_time[i] + (current_time - arrival_time[i])) / burst_time[ i ] if response_ratio < temp: __SCREAMING_SNAKE_CASE = temp __SCREAMING_SNAKE_CASE = i # Calculate the turn around time __SCREAMING_SNAKE_CASE = current_time + burst_time[loc] - arrival_time[loc] current_time += burst_time[loc] # Indicates that the process has been performed. __SCREAMING_SNAKE_CASE = 1 # Increase finished_process_count by 1 finished_process_count += 1 return turn_around_time def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> list: '''simple docstring''' __SCREAMING_SNAKE_CASE = [0] * no_of_process for i in range(0 , __UpperCAmelCase ): __SCREAMING_SNAKE_CASE = turn_around_time[i] - burst_time[i] return waiting_time if __name__ == "__main__": a = 5 a = ["A", "B", "C", "D", "E"] a = [1, 2, 3, 4, 5] a = [1, 2, 3, 4, 5] a = calculate_turn_around_time( process_name, arrival_time, burst_time, no_of_process ) a = calculate_waiting_time( process_name, turn_around_time, burst_time, no_of_process ) print("Process name \tArrival time \tBurst time \tTurn around time \tWaiting time") for i in range(0, no_of_process): print( F'''{process_name[i]}\t\t{arrival_time[i]}\t\t{burst_time[i]}\t\t''' F'''{turn_around_time[i]}\t\t\t{waiting_time[i]}''' ) print(F'''average waiting time : {mean(waiting_time):.5f}''') print(F'''average turn around time : {mean(turn_around_time):.5f}''')
13
'''simple docstring''' import timeit import numpy as np import datasets from datasets.arrow_writer import ArrowWriter from datasets.features.features import _ArrayXD def __magic_name__ ( __UpperCAmelCase ) -> Tuple: '''simple docstring''' def wrapper(*__UpperCAmelCase , **__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = timeit.default_timer() __SCREAMING_SNAKE_CASE = func(*__UpperCAmelCase , **__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = timeit.default_timer() - starttime return delta __SCREAMING_SNAKE_CASE = func.__name__ return wrapper def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase=100 , __UpperCAmelCase=None ) -> Optional[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = seq_shapes or {} for i in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = {} for col_id, (k, v) in enumerate(features.items() ): if isinstance(__UpperCAmelCase , _ArrayXD ): __SCREAMING_SNAKE_CASE = np.random.rand(*v.shape ).astype(v.dtype ) elif isinstance(__UpperCAmelCase , datasets.Value ): if v.dtype == "string": __SCREAMING_SNAKE_CASE = """The small grey turtle was surprisingly fast when challenged.""" else: __SCREAMING_SNAKE_CASE = np.random.randint(10 , size=1 ).astype(v.dtype ).item() elif isinstance(__UpperCAmelCase , datasets.Sequence ): while isinstance(__UpperCAmelCase , datasets.Sequence ): __SCREAMING_SNAKE_CASE = v.feature __SCREAMING_SNAKE_CASE = seq_shapes[k] __SCREAMING_SNAKE_CASE = np.random.rand(*__UpperCAmelCase ).astype(v.dtype ) __SCREAMING_SNAKE_CASE = data dummy_data.append((i, example) ) return dummy_data def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=100 , __UpperCAmelCase=None ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = generate_examples(__UpperCAmelCase , num_examples=__UpperCAmelCase , seq_shapes=__UpperCAmelCase ) with ArrowWriter(features=__UpperCAmelCase , path=__UpperCAmelCase ) as writer: for key, record in dummy_data: __SCREAMING_SNAKE_CASE = features.encode_example(__UpperCAmelCase ) writer.write(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = writer.finalize() if not num_final_examples == num_examples: raise ValueError( f"""Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}.""" ) __SCREAMING_SNAKE_CASE = datasets.Dataset.from_file(filename=__UpperCAmelCase , info=datasets.DatasetInfo(features=__UpperCAmelCase ) ) return dataset
13
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) a = { "configuration_vision_encoder_decoder": ["VisionEncoderDecoderConfig", "VisionEncoderDecoderOnnxConfig"] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = ["VisionEncoderDecoderModel"] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = ["TFVisionEncoderDecoderModel"] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = ["FlaxVisionEncoderDecoderModel"] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys a = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
13
'''simple docstring''' import time from contextlib import contextmanager from pathlib import Path import pytest import requests from huggingface_hub.hf_api import HfApi, HfFolder a = "__DUMMY_TRANSFORMERS_USER__" a = "Dummy User" a = "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" a = "https://hub-ci.huggingface.co" a = CI_HUB_ENDPOINT + "/datasets/{repo_id}/resolve/{revision}/{path}" a = CI_HUB_ENDPOINT + "/{repo_id}/resolve/{revision}/{filename}" a = Path("~/.huggingface/hub_ci_token").expanduser() @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' monkeypatch.setattr( """huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE""" , __UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' monkeypatch.setattr("""datasets.config.HF_ENDPOINT""" , __UpperCAmelCase ) monkeypatch.setattr("""datasets.config.HUB_DATASETS_URL""" , __UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' monkeypatch.setattr("""huggingface_hub.hf_api.HfFolder.path_token""" , __UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' HfFolder.save_token(__UpperCAmelCase ) yield HfFolder.delete_token() @pytest.fixture(scope="""session""" ) def __magic_name__ ( ) -> Optional[Any]: '''simple docstring''' return HfApi(endpoint=__UpperCAmelCase ) @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = HfFolder.get_token() HfFolder.save_token(__UpperCAmelCase ) yield CI_HUB_USER_TOKEN if previous_token is not None: HfFolder.save_token(__UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> Dict: '''simple docstring''' def _cleanup_repo(__UpperCAmelCase ): hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) return _cleanup_repo @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' @contextmanager def _temporary_repo(__UpperCAmelCase ): try: yield repo_id finally: cleanup_repo(__UpperCAmelCase ) return _temporary_repo @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""repo_txt_data-{int(time.time() * 1_0e3 )}""" __SCREAMING_SNAKE_CASE = f"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" , private=__UpperCAmelCase ) hf_api.upload_file( token=__UpperCAmelCase , path_or_fileobj=str(__UpperCAmelCase ) , path_in_repo="""data/text_data.txt""" , repo_id=__UpperCAmelCase , repo_type="""dataset""" , ) yield repo_id try: hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' return hf_private_dataset_repo_txt_data_ @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""repo_zipped_txt_data-{int(time.time() * 1_0e3 )}""" __SCREAMING_SNAKE_CASE = f"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" , private=__UpperCAmelCase ) hf_api.upload_file( token=__UpperCAmelCase , path_or_fileobj=str(__UpperCAmelCase ) , path_in_repo="""data.zip""" , repo_id=__UpperCAmelCase , repo_type="""dataset""" , ) yield repo_id try: hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Tuple: '''simple docstring''' return hf_private_dataset_repo_zipped_txt_data_ @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""repo_zipped_img_data-{int(time.time() * 1_0e3 )}""" __SCREAMING_SNAKE_CASE = f"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" , private=__UpperCAmelCase ) hf_api.upload_file( token=__UpperCAmelCase , path_or_fileobj=str(__UpperCAmelCase ) , path_in_repo="""data.zip""" , repo_id=__UpperCAmelCase , repo_type="""dataset""" , ) yield repo_id try: hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' return hf_private_dataset_repo_zipped_img_data_
13
1
'''simple docstring''' from __future__ import annotations import os from collections.abc import Mapping a = tuple[int, int] class __a : def __init__( self : List[Any] ,lowerCamelCase : set[int] ,lowerCamelCase : Mapping[EdgeT, int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = vertices __SCREAMING_SNAKE_CASE = { (min(lowerCamelCase ), max(lowerCamelCase )): weight for edge, weight in edges.items() } def UpperCAmelCase__ ( self : Optional[Any] ,lowerCamelCase : EdgeT ,lowerCamelCase : int ): '''simple docstring''' self.vertices.add(edge[0] ) self.vertices.add(edge[1] ) __SCREAMING_SNAKE_CASE = weight def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = Graph({min(self.vertices )} ,{} ) __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 while len(subgraph.vertices ) < len(self.vertices ): __SCREAMING_SNAKE_CASE = max(self.edges.values() ) + 1 for edge, weight in self.edges.items(): if (edge[0] in subgraph.vertices) ^ (edge[1] in subgraph.vertices): if weight < min_weight: __SCREAMING_SNAKE_CASE = edge __SCREAMING_SNAKE_CASE = weight subgraph.add_edge(lowerCamelCase ,lowerCamelCase ) return subgraph def __magic_name__ ( __UpperCAmelCase = "p107_network.txt" ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = os.path.abspath(os.path.dirname(__UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = os.path.join(__UpperCAmelCase , __UpperCAmelCase ) __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 with open(__UpperCAmelCase ) as f: __SCREAMING_SNAKE_CASE = f.read().strip().split("""\n""" ) __SCREAMING_SNAKE_CASE = [line.split(""",""" ) for line in data] for edgea in range(1 , len(__UpperCAmelCase ) ): for edgea in range(__UpperCAmelCase ): if adjaceny_matrix[edgea][edgea] != "-": __SCREAMING_SNAKE_CASE = int(adjaceny_matrix[edgea][edgea] ) __SCREAMING_SNAKE_CASE = Graph(set(range(len(__UpperCAmelCase ) ) ) , __UpperCAmelCase ) __SCREAMING_SNAKE_CASE = graph.prims_algorithm() __SCREAMING_SNAKE_CASE = sum(graph.edges.values() ) __SCREAMING_SNAKE_CASE = sum(subgraph.edges.values() ) return initial_total - optimal_total if __name__ == "__main__": print(F'''{solution() = }''')
13
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING a = logging.get_logger(__name__) a = { "ut/deta": "https://huggingface.co/ut/deta/resolve/main/config.json", } class __a ( _snake_case ): __UpperCamelCase : Dict = 'deta' __UpperCamelCase : List[str] = { 'hidden_size': 'd_model', 'num_attention_heads': 'encoder_attention_heads', } def __init__( self : Tuple ,lowerCamelCase : List[Any]=None ,lowerCamelCase : Any=900 ,lowerCamelCase : int=2048 ,lowerCamelCase : Any=6 ,lowerCamelCase : Optional[Any]=2048 ,lowerCamelCase : str=8 ,lowerCamelCase : Union[str, Any]=6 ,lowerCamelCase : List[str]=1024 ,lowerCamelCase : int=8 ,lowerCamelCase : Any=0.0 ,lowerCamelCase : Any=True ,lowerCamelCase : Optional[int]="relu" ,lowerCamelCase : int=256 ,lowerCamelCase : Tuple=0.1 ,lowerCamelCase : Optional[Any]=0.0 ,lowerCamelCase : Tuple=0.0 ,lowerCamelCase : List[str]=0.02 ,lowerCamelCase : Any=1.0 ,lowerCamelCase : Optional[int]=True ,lowerCamelCase : int=False ,lowerCamelCase : Optional[Any]="sine" ,lowerCamelCase : Dict=5 ,lowerCamelCase : List[Any]=4 ,lowerCamelCase : Optional[Any]=4 ,lowerCamelCase : Any=True ,lowerCamelCase : int=300 ,lowerCamelCase : Any=True ,lowerCamelCase : Tuple=True ,lowerCamelCase : int=1 ,lowerCamelCase : Tuple=5 ,lowerCamelCase : Union[str, Any]=2 ,lowerCamelCase : Tuple=1 ,lowerCamelCase : int=1 ,lowerCamelCase : str=5 ,lowerCamelCase : Optional[Any]=2 ,lowerCamelCase : List[Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.25 ,**lowerCamelCase : int ,): '''simple docstring''' if backbone_config is None: logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.""" ) __SCREAMING_SNAKE_CASE = CONFIG_MAPPING["""resnet"""](out_features=["""stage2""", """stage3""", """stage4"""] ) else: if isinstance(lowerCamelCase ,lowerCamelCase ): __SCREAMING_SNAKE_CASE = backbone_config.pop("""model_type""" ) __SCREAMING_SNAKE_CASE = CONFIG_MAPPING[backbone_model_type] __SCREAMING_SNAKE_CASE = config_class.from_dict(lowerCamelCase ) __SCREAMING_SNAKE_CASE = backbone_config __SCREAMING_SNAKE_CASE = num_queries __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = d_model __SCREAMING_SNAKE_CASE = encoder_ffn_dim __SCREAMING_SNAKE_CASE = encoder_layers __SCREAMING_SNAKE_CASE = encoder_attention_heads __SCREAMING_SNAKE_CASE = decoder_ffn_dim __SCREAMING_SNAKE_CASE = decoder_layers __SCREAMING_SNAKE_CASE = decoder_attention_heads __SCREAMING_SNAKE_CASE = dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = activation_function __SCREAMING_SNAKE_CASE = init_std __SCREAMING_SNAKE_CASE = init_xavier_std __SCREAMING_SNAKE_CASE = encoder_layerdrop __SCREAMING_SNAKE_CASE = auxiliary_loss __SCREAMING_SNAKE_CASE = position_embedding_type # deformable attributes __SCREAMING_SNAKE_CASE = num_feature_levels __SCREAMING_SNAKE_CASE = encoder_n_points __SCREAMING_SNAKE_CASE = decoder_n_points __SCREAMING_SNAKE_CASE = two_stage __SCREAMING_SNAKE_CASE = two_stage_num_proposals __SCREAMING_SNAKE_CASE = with_box_refine __SCREAMING_SNAKE_CASE = assign_first_stage if two_stage is True and with_box_refine is False: raise ValueError("""If two_stage is True, with_box_refine must be True.""" ) # Hungarian matcher __SCREAMING_SNAKE_CASE = class_cost __SCREAMING_SNAKE_CASE = bbox_cost __SCREAMING_SNAKE_CASE = giou_cost # Loss coefficients __SCREAMING_SNAKE_CASE = mask_loss_coefficient __SCREAMING_SNAKE_CASE = dice_loss_coefficient __SCREAMING_SNAKE_CASE = bbox_loss_coefficient __SCREAMING_SNAKE_CASE = giou_loss_coefficient __SCREAMING_SNAKE_CASE = eos_coefficient __SCREAMING_SNAKE_CASE = focal_alpha super().__init__(is_encoder_decoder=lowerCamelCase ,**lowerCamelCase ) @property def UpperCAmelCase__ ( self : Any ): '''simple docstring''' return self.encoder_attention_heads @property def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' return self.d_model def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) __SCREAMING_SNAKE_CASE = self.backbone_config.to_dict() __SCREAMING_SNAKE_CASE = self.__class__.model_type return output
13
1
import multiprocessing import os from typing import BinaryIO, Optional, Union import fsspec from .. import Dataset, Features, NamedSplit, config from ..formatting import query_table from ..packaged_modules.json.json import Json from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class __a ( _lowerCamelCase ): def __init__( self : Dict ,lowerCamelCase : Union[str, Any] ,lowerCamelCase : Tuple = None ,lowerCamelCase : Dict = None ,lowerCamelCase : Any = None ,lowerCamelCase : Tuple = False ,lowerCamelCase : Any = False ,lowerCamelCase : List[str] = None ,lowerCamelCase : List[str] = None ,**lowerCamelCase : Any ,): '''simple docstring''' super().__init__( A__ ,split=A__ ,features=A__ ,cache_dir=A__ ,keep_in_memory=A__ ,streaming=A__ ,num_proc=A__ ,**A__ ,) __SCREAMING_SNAKE_CASE = field __SCREAMING_SNAKE_CASE = path_or_paths if isinstance(A__ ,A__ ) else {self.split: path_or_paths} __SCREAMING_SNAKE_CASE = Json( cache_dir=A__ ,data_files=A__ ,features=A__ ,field=A__ ,**A__ ,) def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' if self.streaming: __SCREAMING_SNAKE_CASE = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None self.builder.download_and_prepare( download_config=A__ ,download_mode=A__ ,verification_mode=A__ ,base_path=A__ ,num_proc=self.num_proc ,) __SCREAMING_SNAKE_CASE = self.builder.as_dataset( split=self.split ,verification_mode=A__ ,in_memory=self.keep_in_memory ) return dataset class __a : def __init__( self : str ,lowerCamelCase : Optional[int] ,lowerCamelCase : int ,lowerCamelCase : Tuple = None ,lowerCamelCase : Dict = None ,**lowerCamelCase : str ,): '''simple docstring''' if num_proc is not None and num_proc <= 0: raise ValueError(f"""num_proc {num_proc} must be an integer > 0.""" ) __SCREAMING_SNAKE_CASE = dataset __SCREAMING_SNAKE_CASE = path_or_buf __SCREAMING_SNAKE_CASE = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE __SCREAMING_SNAKE_CASE = num_proc __SCREAMING_SNAKE_CASE = """utf-8""" __SCREAMING_SNAKE_CASE = to_json_kwargs def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.to_json_kwargs.pop("""path_or_buf""" ,A__ ) __SCREAMING_SNAKE_CASE = self.to_json_kwargs.pop("""orient""" ,"""records""" ) __SCREAMING_SNAKE_CASE = self.to_json_kwargs.pop("""lines""" ,True if orient == """records""" else False ) __SCREAMING_SNAKE_CASE = self.to_json_kwargs.pop("""index""" ,False if orient in ["""split""", """table"""] else True ) __SCREAMING_SNAKE_CASE = self.to_json_kwargs.pop("""compression""" ,A__ ) if compression not in [None, "infer", "gzip", "bz2", "xz"]: raise NotImplementedError(f"""`datasets` currently does not support {compression} compression""" ) if isinstance(self.path_or_buf ,(str, bytes, os.PathLike) ): with fsspec.open(self.path_or_buf ,"""wb""" ,compression=A__ ) as buffer: __SCREAMING_SNAKE_CASE = self._write(file_obj=A__ ,orient=A__ ,lines=A__ ,index=A__ ,**self.to_json_kwargs ) else: if compression: raise NotImplementedError( f"""The compression parameter is not supported when writing to a buffer, but compression={compression}""" """ was passed. Please provide a local path instead.""" ) __SCREAMING_SNAKE_CASE = self._write( file_obj=self.path_or_buf ,orient=A__ ,lines=A__ ,index=A__ ,**self.to_json_kwargs ) return written def UpperCAmelCase__ ( self : Optional[int] ,lowerCamelCase : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = args __SCREAMING_SNAKE_CASE = query_table( table=self.dataset.data ,key=slice(A__ ,offset + self.batch_size ) ,indices=self.dataset._indices ,) __SCREAMING_SNAKE_CASE = batch.to_pandas().to_json( path_or_buf=A__ ,orient=A__ ,lines=A__ ,index=A__ ,**A__ ) if not json_str.endswith("""\n""" ): json_str += "\n" return json_str.encode(self.encoding ) def UpperCAmelCase__ ( self : Optional[int] ,lowerCamelCase : int ,lowerCamelCase : Optional[Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : Any ,**lowerCamelCase : Optional[Any] ,): '''simple docstring''' __SCREAMING_SNAKE_CASE = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 ,len(self.dataset ) ,self.batch_size ) ,unit="""ba""" ,disable=not logging.is_progress_bar_enabled() ,desc="""Creating json from Arrow format""" ,): __SCREAMING_SNAKE_CASE = self._batch_json((offset, orient, lines, index, to_json_kwargs) ) written += file_obj.write(A__ ) else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for json_str in logging.tqdm( pool.imap( self._batch_json ,[(offset, orient, lines, index, to_json_kwargs) for offset in range(0 ,A__ ,A__ )] ,) ,total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size ,unit="""ba""" ,disable=not logging.is_progress_bar_enabled() ,desc="""Creating json from Arrow format""" ,): written += file_obj.write(A__ ) return written
700
'''simple docstring''' import flax.linen as nn import jax import jax.numpy as jnp class __a ( nn.Module ): __UpperCamelCase : int __UpperCamelCase : jnp.dtype = jnp.floataa def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = nn.Conv( self.out_channels ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) def __call__( self : List[Any] ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = hidden_states.shape __SCREAMING_SNAKE_CASE = jax.image.resize( lowerCamelCase ,shape=(batch, height * 2, width * 2, channels) ,method="""nearest""" ,) __SCREAMING_SNAKE_CASE = self.conv(lowerCamelCase ) return hidden_states class __a ( nn.Module ): __UpperCamelCase : int __UpperCamelCase : jnp.dtype = jnp.floataa def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = nn.Conv( self.out_channels ,kernel_size=(3, 3) ,strides=(2, 2) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) def __call__( self : List[str] ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.conv(lowerCamelCase ) return hidden_states class __a ( nn.Module ): __UpperCamelCase : int __UpperCamelCase : int = None __UpperCamelCase : float = 0.0 __UpperCamelCase : bool = None __UpperCamelCase : jnp.dtype = jnp.floataa def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.in_channels if self.out_channels is None else self.out_channels __SCREAMING_SNAKE_CASE = nn.GroupNorm(num_groups=32 ,epsilon=1E-5 ) __SCREAMING_SNAKE_CASE = nn.Conv( lowerCamelCase ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) __SCREAMING_SNAKE_CASE = nn.Dense(lowerCamelCase ,dtype=self.dtype ) __SCREAMING_SNAKE_CASE = nn.GroupNorm(num_groups=32 ,epsilon=1E-5 ) __SCREAMING_SNAKE_CASE = nn.Dropout(self.dropout_prob ) __SCREAMING_SNAKE_CASE = nn.Conv( lowerCamelCase ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) __SCREAMING_SNAKE_CASE = self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut __SCREAMING_SNAKE_CASE = None if use_nin_shortcut: __SCREAMING_SNAKE_CASE = nn.Conv( lowerCamelCase ,kernel_size=(1, 1) ,strides=(1, 1) ,padding="""VALID""" ,dtype=self.dtype ,) def __call__( self : List[str] ,lowerCamelCase : Optional[int] ,lowerCamelCase : Tuple ,lowerCamelCase : Union[str, Any]=True ): '''simple docstring''' __SCREAMING_SNAKE_CASE = hidden_states __SCREAMING_SNAKE_CASE = self.norma(lowerCamelCase ) __SCREAMING_SNAKE_CASE = nn.swish(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.conva(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.time_emb_proj(nn.swish(lowerCamelCase ) ) __SCREAMING_SNAKE_CASE = jnp.expand_dims(jnp.expand_dims(lowerCamelCase ,1 ) ,1 ) __SCREAMING_SNAKE_CASE = hidden_states + temb __SCREAMING_SNAKE_CASE = self.norma(lowerCamelCase ) __SCREAMING_SNAKE_CASE = nn.swish(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.dropout(lowerCamelCase ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.conva(lowerCamelCase ) if self.conv_shortcut is not None: __SCREAMING_SNAKE_CASE = self.conv_shortcut(lowerCamelCase ) return hidden_states + residual
13
0
'''simple docstring''' import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipaConfig, BlipaForConditionalGeneration, BlipaProcessor, BlipaVisionConfig, BlipImageProcessor, OPTConfig, TaConfig, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def __magic_name__ ( ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = """https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png""" __SCREAMING_SNAKE_CASE = Image.open(requests.get(__A , stream=__A ).raw ).convert("""RGB""" ) return image def __magic_name__ ( __UpperCAmelCase ) -> Any: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] # fmt: off # vision encoder rename_keys.append(("""visual_encoder.cls_token""", """vision_model.embeddings.class_embedding""") ) rename_keys.append(("""visual_encoder.pos_embed""", """vision_model.embeddings.position_embedding""") ) rename_keys.append(("""visual_encoder.patch_embed.proj.weight""", """vision_model.embeddings.patch_embedding.weight""") ) rename_keys.append(("""visual_encoder.patch_embed.proj.bias""", """vision_model.embeddings.patch_embedding.bias""") ) rename_keys.append(("""ln_vision.weight""", """vision_model.post_layernorm.weight""") ) rename_keys.append(("""ln_vision.bias""", """vision_model.post_layernorm.bias""") ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((f"""visual_encoder.blocks.{i}.norm1.weight""", f"""vision_model.encoder.layers.{i}.layer_norm1.weight""") ) rename_keys.append((f"""visual_encoder.blocks.{i}.norm1.bias""", f"""vision_model.encoder.layers.{i}.layer_norm1.bias""") ) rename_keys.append((f"""visual_encoder.blocks.{i}.norm2.weight""", f"""vision_model.encoder.layers.{i}.layer_norm2.weight""") ) rename_keys.append((f"""visual_encoder.blocks.{i}.norm2.bias""", f"""vision_model.encoder.layers.{i}.layer_norm2.bias""") ) rename_keys.append((f"""visual_encoder.blocks.{i}.attn.qkv.weight""", f"""vision_model.encoder.layers.{i}.self_attn.qkv.weight""") ) rename_keys.append((f"""visual_encoder.blocks.{i}.attn.proj.weight""", f"""vision_model.encoder.layers.{i}.self_attn.projection.weight""",) ) rename_keys.append((f"""visual_encoder.blocks.{i}.attn.proj.bias""", f"""vision_model.encoder.layers.{i}.self_attn.projection.bias""") ) rename_keys.append((f"""visual_encoder.blocks.{i}.mlp.fc1.weight""", f"""vision_model.encoder.layers.{i}.mlp.fc1.weight""") ) rename_keys.append((f"""visual_encoder.blocks.{i}.mlp.fc1.bias""", f"""vision_model.encoder.layers.{i}.mlp.fc1.bias""") ) rename_keys.append((f"""visual_encoder.blocks.{i}.mlp.fc2.weight""", f"""vision_model.encoder.layers.{i}.mlp.fc2.weight""") ) rename_keys.append((f"""visual_encoder.blocks.{i}.mlp.fc2.bias""", f"""vision_model.encoder.layers.{i}.mlp.fc2.bias""") ) # QFormer rename_keys.append(("""Qformer.bert.embeddings.LayerNorm.weight""", """qformer.layernorm.weight""") ) rename_keys.append(("""Qformer.bert.embeddings.LayerNorm.bias""", """qformer.layernorm.bias""") ) # fmt: on return rename_keys def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = dct.pop(__A ) __SCREAMING_SNAKE_CASE = val def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases __SCREAMING_SNAKE_CASE = state_dict.pop(f"""visual_encoder.blocks.{i}.attn.q_bias""" ) __SCREAMING_SNAKE_CASE = state_dict.pop(f"""visual_encoder.blocks.{i}.attn.v_bias""" ) # next, set bias in the state dict __SCREAMING_SNAKE_CASE = torch.cat((q_bias, torch.zeros_like(__A , requires_grad=__A ), v_bias) ) __SCREAMING_SNAKE_CASE = qkv_bias def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Any: '''simple docstring''' __SCREAMING_SNAKE_CASE = 364 if """coco""" in model_name else 224 __SCREAMING_SNAKE_CASE = BlipaVisionConfig(image_size=__A ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: __SCREAMING_SNAKE_CASE = OPTConfig.from_pretrained("""facebook/opt-2.7b""" , eos_token_id=__A ).to_dict() elif "opt-6.7b" in model_name: __SCREAMING_SNAKE_CASE = OPTConfig.from_pretrained("""facebook/opt-6.7b""" , eos_token_id=__A ).to_dict() elif "t5-xl" in model_name: __SCREAMING_SNAKE_CASE = TaConfig.from_pretrained("""google/flan-t5-xl""" , dense_act_fn="""gelu""" , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: __SCREAMING_SNAKE_CASE = TaConfig.from_pretrained("""google/flan-t5-xxl""" , dense_act_fn="""gelu""" , bos_token_id=1 ).to_dict() __SCREAMING_SNAKE_CASE = BlipaConfig(vision_config=__A , text_config=__A ) return config, image_size @torch.no_grad() def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase=None , __UpperCAmelCase=False ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = ( AutoTokenizer.from_pretrained("""facebook/opt-2.7b""" ) if """opt""" in model_name else AutoTokenizer.from_pretrained("""google/flan-t5-xl""" ) ) __SCREAMING_SNAKE_CASE = tokenizer("""\n""" , add_special_tokens=__A ).input_ids[0] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = get_blipa_config(__A , eos_token_id=__A ) __SCREAMING_SNAKE_CASE = BlipaForConditionalGeneration(__A ).eval() __SCREAMING_SNAKE_CASE = { """blip2-opt-2.7b""": ("""blip2_opt""", """pretrain_opt2.7b"""), """blip2-opt-6.7b""": ("""blip2_opt""", """pretrain_opt6.7b"""), """blip2-opt-2.7b-coco""": ("""blip2_opt""", """caption_coco_opt2.7b"""), """blip2-opt-6.7b-coco""": ("""blip2_opt""", """caption_coco_opt6.7b"""), """blip2-flan-t5-xl""": ("""blip2_t5""", """pretrain_flant5xl"""), """blip2-flan-t5-xl-coco""": ("""blip2_t5""", """caption_coco_flant5xl"""), """blip2-flan-t5-xxl""": ("""blip2_t5""", """pretrain_flant5xxl"""), } __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = model_name_to_original[model_name] # load original model print("""Loading original model...""" ) __SCREAMING_SNAKE_CASE = """cuda""" if torch.cuda.is_available() else """cpu""" __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = load_model_and_preprocess( name=__A , model_type=__A , is_eval=__A , device=__A ) original_model.eval() print("""Done!""" ) # update state dict keys __SCREAMING_SNAKE_CASE = original_model.state_dict() __SCREAMING_SNAKE_CASE = create_rename_keys(__A ) for src, dest in rename_keys: rename_key(__A , __A , __A ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): __SCREAMING_SNAKE_CASE = state_dict.pop(__A ) if key.startswith("""Qformer.bert""" ): __SCREAMING_SNAKE_CASE = key.replace("""Qformer.bert""" , """qformer""" ) if "attention.self" in key: __SCREAMING_SNAKE_CASE = key.replace("""self""" , """attention""" ) if "opt_proj" in key: __SCREAMING_SNAKE_CASE = key.replace("""opt_proj""" , """language_projection""" ) if "t5_proj" in key: __SCREAMING_SNAKE_CASE = key.replace("""t5_proj""" , """language_projection""" ) if key.startswith("""opt""" ): __SCREAMING_SNAKE_CASE = key.replace("""opt""" , """language""" ) if key.startswith("""t5""" ): __SCREAMING_SNAKE_CASE = key.replace("""t5""" , """language""" ) __SCREAMING_SNAKE_CASE = val # read in qv biases read_in_q_v_bias(__A , __A ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = hf_model.load_state_dict(__A , strict=__A ) assert len(__A ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] __SCREAMING_SNAKE_CASE = load_demo_image() __SCREAMING_SNAKE_CASE = vis_processors["""eval"""](__A ).unsqueeze(0 ).to(__A ) __SCREAMING_SNAKE_CASE = tokenizer(["""\n"""] , return_tensors="""pt""" ).input_ids.to(__A ) # create processor __SCREAMING_SNAKE_CASE = BlipImageProcessor( size={"""height""": image_size, """width""": image_size} , image_mean=__A , image_std=__A ) __SCREAMING_SNAKE_CASE = BlipaProcessor(image_processor=__A , tokenizer=__A ) __SCREAMING_SNAKE_CASE = processor(images=__A , return_tensors="""pt""" ).pixel_values.to(__A ) # make sure processor creates exact same pixel values assert torch.allclose(__A , __A ) original_model.to(__A ) hf_model.to(__A ) with torch.no_grad(): if "opt" in model_name: __SCREAMING_SNAKE_CASE = original_model({"""image""": original_pixel_values, """text_input""": [""""""]} ).logits __SCREAMING_SNAKE_CASE = hf_model(__A , __A ).logits else: __SCREAMING_SNAKE_CASE = original_model( {"""image""": original_pixel_values, """text_input""": ["""\n"""], """text_output""": ["""\n"""]} ).logits __SCREAMING_SNAKE_CASE = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -100 ) __SCREAMING_SNAKE_CASE = hf_model(__A , __A , labels=__A ).logits assert original_logits.shape == logits.shape print("""First values of original logits:""" , original_logits[0, :3, :3] ) print("""First values of HF logits:""" , logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": __SCREAMING_SNAKE_CASE = torch.tensor( [[-4_1.5_8_5_0, -4.4_4_4_0, -8.9_9_2_2], [-4_7.4_3_2_2, -5.9_1_4_3, -1.7_3_4_0]] , device=__A ) assert torch.allclose(logits[0, :3, :3] , __A , atol=1e-4 ) elif model_name == "blip2-flan-t5-xl-coco": __SCREAMING_SNAKE_CASE = torch.tensor( [[-5_7.0_1_0_9, -9.8_9_6_7, -1_2.6_2_8_0], [-6_8.6_5_7_8, -1_2.7_1_9_1, -1_0.5_0_6_5]] , device=__A ) else: # cast to same type __SCREAMING_SNAKE_CASE = logits.dtype assert torch.allclose(original_logits.to(__A ) , __A , atol=1e-2 ) print("""Looks ok!""" ) print("""Generating a caption...""" ) __SCREAMING_SNAKE_CASE = """""" __SCREAMING_SNAKE_CASE = tokenizer(__A , return_tensors="""pt""" ).input_ids.to(__A ) __SCREAMING_SNAKE_CASE = original_model.generate({"""image""": original_pixel_values} ) __SCREAMING_SNAKE_CASE = hf_model.generate( __A , __A , do_sample=__A , num_beams=5 , max_length=30 , min_length=1 , top_p=0.9 , repetition_penalty=1.0 , length_penalty=1.0 , temperature=1 , ) print("""Original generation:""" , __A ) __SCREAMING_SNAKE_CASE = input_ids.shape[1] __SCREAMING_SNAKE_CASE = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=__A ) __SCREAMING_SNAKE_CASE = [text.strip() for text in output_text] print("""HF generation:""" , __A ) if pytorch_dump_folder_path is not None: processor.save_pretrained(__A ) hf_model.save_pretrained(__A ) if push_to_hub: processor.push_to_hub(f"""nielsr/{model_name}""" ) hf_model.push_to_hub(f"""nielsr/{model_name}""" ) if __name__ == "__main__": a = argparse.ArgumentParser() a = [ "blip2-opt-2.7b", "blip2-opt-6.7b", "blip2-opt-2.7b-coco", "blip2-opt-6.7b-coco", "blip2-flan-t5-xl", "blip2-flan-t5-xl-coco", "blip2-flan-t5-xxl", ] parser.add_argument( "--model_name", default="blip2-opt-2.7b", choices=choices, type=str, help="Path to hf config.json of model to convert", ) parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument( "--push_to_hub", action="store_true", help="Whether to push the model and processor to the hub after converting", ) a = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
701
'''simple docstring''' import sys from collections import defaultdict class __a : def __init__( self : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = [] def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : List[Any] ): '''simple docstring''' return self.node_position[vertex] def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : str ,lowerCamelCase : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = pos def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : Optional[int] ,lowerCamelCase : Union[str, Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : Any ): '''simple docstring''' if start > size // 2 - 1: return else: if 2 * start + 2 >= size: __SCREAMING_SNAKE_CASE = 2 * start + 1 else: if heap[2 * start + 1] < heap[2 * start + 2]: __SCREAMING_SNAKE_CASE = 2 * start + 1 else: __SCREAMING_SNAKE_CASE = 2 * start + 2 if heap[smallest_child] < heap[start]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = heap[smallest_child], positions[smallest_child] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = ( heap[start], positions[start], ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = temp, tempa __SCREAMING_SNAKE_CASE = self.get_position(positions[smallest_child] ) self.set_position( positions[smallest_child] ,self.get_position(positions[start] ) ) self.set_position(positions[start] ,lowerCamelCase ) self.top_to_bottom(lowerCamelCase ,lowerCamelCase ,lowerCamelCase ,lowerCamelCase ) def UpperCAmelCase__ ( self : Any ,lowerCamelCase : int ,lowerCamelCase : List[str] ,lowerCamelCase : Optional[Any] ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = position[index] while index != 0: __SCREAMING_SNAKE_CASE = int((index - 2) / 2 ) if index % 2 == 0 else int((index - 1) / 2 ) if val < heap[parent]: __SCREAMING_SNAKE_CASE = heap[parent] __SCREAMING_SNAKE_CASE = position[parent] self.set_position(position[parent] ,lowerCamelCase ) else: __SCREAMING_SNAKE_CASE = val __SCREAMING_SNAKE_CASE = temp self.set_position(lowerCamelCase ,lowerCamelCase ) break __SCREAMING_SNAKE_CASE = parent else: __SCREAMING_SNAKE_CASE = val __SCREAMING_SNAKE_CASE = temp self.set_position(lowerCamelCase ,0 ) def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : List[Any] ,lowerCamelCase : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = len(lowerCamelCase ) // 2 - 1 for i in range(lowerCamelCase ,-1 ,-1 ): self.top_to_bottom(lowerCamelCase ,lowerCamelCase ,len(lowerCamelCase ) ,lowerCamelCase ) def UpperCAmelCase__ ( self : int ,lowerCamelCase : Optional[int] ,lowerCamelCase : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = positions[0] __SCREAMING_SNAKE_CASE = sys.maxsize self.top_to_bottom(lowerCamelCase ,0 ,len(lowerCamelCase ) ,lowerCamelCase ) return temp def __magic_name__ ( __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = Heap() __SCREAMING_SNAKE_CASE = [0] * len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [-1] * len(__UpperCAmelCase ) # Neighboring Tree Vertex of selected vertex # Minimum Distance of explored vertex with neighboring vertex of partial tree # formed in graph __SCREAMING_SNAKE_CASE = [] # Heap of Distance of vertices from their neighboring vertex __SCREAMING_SNAKE_CASE = [] for vertex in range(len(__UpperCAmelCase ) ): distance_tv.append(sys.maxsize ) positions.append(__UpperCAmelCase ) heap.node_position.append(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = sys.maxsize for neighbor, distance in adjacency_list[0]: __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = distance heap.heapify(__UpperCAmelCase , __UpperCAmelCase ) for _ in range(1 , len(__UpperCAmelCase ) ): __SCREAMING_SNAKE_CASE = heap.delete_minimum(__UpperCAmelCase , __UpperCAmelCase ) if visited[vertex] == 0: tree_edges.append((nbr_tv[vertex], vertex) ) __SCREAMING_SNAKE_CASE = 1 for neighbor, distance in adjacency_list[vertex]: if ( visited[neighbor] == 0 and distance < distance_tv[heap.get_position(__UpperCAmelCase )] ): __SCREAMING_SNAKE_CASE = distance heap.bottom_to_top( __UpperCAmelCase , heap.get_position(__UpperCAmelCase ) , __UpperCAmelCase , __UpperCAmelCase ) __SCREAMING_SNAKE_CASE = vertex return tree_edges if __name__ == "__main__": # pragma: no cover # < --------- Prims Algorithm --------- > a = int(input("Enter number of edges: ").strip()) a = defaultdict(list) for _ in range(edges_number): a = [int(x) for x in input().strip().split()] adjacency_list[edge[0]].append([edge[1], edge[2]]) adjacency_list[edge[1]].append([edge[0], edge[2]]) print(prisms_algorithm(adjacency_list))
13
0
'''simple docstring''' from .integrations import ( is_optuna_available, is_ray_available, is_sigopt_available, is_wandb_available, run_hp_search_optuna, run_hp_search_ray, run_hp_search_sigopt, run_hp_search_wandb, ) from .trainer_utils import ( HPSearchBackend, default_hp_space_optuna, default_hp_space_ray, default_hp_space_sigopt, default_hp_space_wandb, ) from .utils import logging a = logging.get_logger(__name__) class __a : __UpperCamelCase : str __UpperCamelCase : str = None @staticmethod def UpperCAmelCase__ ( ): '''simple docstring''' raise NotImplementedError def UpperCAmelCase__ ( self : str ,lowerCamelCase : Any ,lowerCamelCase : int ,lowerCamelCase : str ,**lowerCamelCase : Union[str, Any] ): '''simple docstring''' raise NotImplementedError def UpperCAmelCase__ ( self : Optional[Any] ,lowerCamelCase : List[Any] ): '''simple docstring''' raise NotImplementedError def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' if not self.is_available(): raise RuntimeError( f"""You picked the {self.name} backend, but it is not installed. Run {self.pip_install()}.""" ) @classmethod def UpperCAmelCase__ ( cls : Union[str, Any] ): '''simple docstring''' return f"""`pip install {cls.pip_package or cls.name}`""" class __a ( snake_case__ ): __UpperCamelCase : List[str] = """optuna""" @staticmethod def UpperCAmelCase__ ( ): '''simple docstring''' return is_optuna_available() def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : str ,lowerCamelCase : int ,lowerCamelCase : str ,**lowerCamelCase : str ): '''simple docstring''' return run_hp_search_optuna(UpperCAmelCase_ ,UpperCAmelCase_ ,UpperCAmelCase_ ,**UpperCAmelCase_ ) def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : int ): '''simple docstring''' return default_hp_space_optuna(UpperCAmelCase_ ) class __a ( snake_case__ ): __UpperCamelCase : Any = """ray""" __UpperCamelCase : Tuple = """'ray[tune]'""" @staticmethod def UpperCAmelCase__ ( ): '''simple docstring''' return is_ray_available() def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : Union[str, Any] ,lowerCamelCase : int ,lowerCamelCase : str ,**lowerCamelCase : List[str] ): '''simple docstring''' return run_hp_search_ray(UpperCAmelCase_ ,UpperCAmelCase_ ,UpperCAmelCase_ ,**UpperCAmelCase_ ) def UpperCAmelCase__ ( self : str ,lowerCamelCase : str ): '''simple docstring''' return default_hp_space_ray(UpperCAmelCase_ ) class __a ( snake_case__ ): __UpperCamelCase : Union[str, Any] = """sigopt""" @staticmethod def UpperCAmelCase__ ( ): '''simple docstring''' return is_sigopt_available() def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : str ,lowerCamelCase : int ,lowerCamelCase : str ,**lowerCamelCase : str ): '''simple docstring''' return run_hp_search_sigopt(UpperCAmelCase_ ,UpperCAmelCase_ ,UpperCAmelCase_ ,**UpperCAmelCase_ ) def UpperCAmelCase__ ( self : Union[str, Any] ,lowerCamelCase : Dict ): '''simple docstring''' return default_hp_space_sigopt(UpperCAmelCase_ ) class __a ( snake_case__ ): __UpperCamelCase : Optional[int] = """wandb""" @staticmethod def UpperCAmelCase__ ( ): '''simple docstring''' return is_wandb_available() def UpperCAmelCase__ ( self : int ,lowerCamelCase : Optional[Any] ,lowerCamelCase : int ,lowerCamelCase : str ,**lowerCamelCase : Optional[int] ): '''simple docstring''' return run_hp_search_wandb(UpperCAmelCase_ ,UpperCAmelCase_ ,UpperCAmelCase_ ,**UpperCAmelCase_ ) def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : Optional[int] ): '''simple docstring''' return default_hp_space_wandb(UpperCAmelCase_ ) a = { HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend] } def __magic_name__ ( ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()] if len(_SCREAMING_SNAKE_CASE ) > 0: __SCREAMING_SNAKE_CASE = available_backends[0].name if len(_SCREAMING_SNAKE_CASE ) > 1: logger.info( f"""{len(_SCREAMING_SNAKE_CASE )} hyperparameter search backends available. Using {name} as the default.""" ) return name raise RuntimeError( """No hyperparameter search backend available.\n""" + """\n""".join( f""" - To install {backend.name} run {backend.pip_install()}""" for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() ) )
702
'''simple docstring''' import os import string import sys a = 1 << 8 a = { "tab": ord("\t"), "newline": ord("\r"), "esc": 27, "up": 65 + ARROW_KEY_FLAG, "down": 66 + ARROW_KEY_FLAG, "right": 67 + ARROW_KEY_FLAG, "left": 68 + ARROW_KEY_FLAG, "mod_int": 91, "undefined": sys.maxsize, "interrupt": 3, "insert": 50, "delete": 51, "pg_up": 53, "pg_down": 54, } a = KEYMAP["up"] a = KEYMAP["left"] if sys.platform == "win32": a = [] a = { b"\xe0H": KEYMAP["up"] - ARROW_KEY_FLAG, b"\x00H": KEYMAP["up"] - ARROW_KEY_FLAG, b"\xe0P": KEYMAP["down"] - ARROW_KEY_FLAG, b"\x00P": KEYMAP["down"] - ARROW_KEY_FLAG, b"\xe0M": KEYMAP["right"] - ARROW_KEY_FLAG, b"\x00M": KEYMAP["right"] - ARROW_KEY_FLAG, b"\xe0K": KEYMAP["left"] - ARROW_KEY_FLAG, b"\x00K": KEYMAP["left"] - ARROW_KEY_FLAG, } for i in range(10): a = ord(str(i)) def __magic_name__ ( ) -> Union[str, Any]: '''simple docstring''' if os.name == "nt": import msvcrt __SCREAMING_SNAKE_CASE = """mbcs""" # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(__UpperCAmelCase ) == 0: # Read the keystroke __SCREAMING_SNAKE_CASE = msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): __SCREAMING_SNAKE_CASE = ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: __SCREAMING_SNAKE_CASE = chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP["""mod_int"""] ) ) WIN_CH_BUFFER.append(__UpperCAmelCase ) if ord(__UpperCAmelCase ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(126 ) ) __SCREAMING_SNAKE_CASE = chr(KEYMAP["""esc"""] ) except KeyError: __SCREAMING_SNAKE_CASE = cha[1] else: __SCREAMING_SNAKE_CASE = ch.decode(__UpperCAmelCase ) else: __SCREAMING_SNAKE_CASE = WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty __SCREAMING_SNAKE_CASE = sys.stdin.fileno() __SCREAMING_SNAKE_CASE = termios.tcgetattr(__UpperCAmelCase ) try: tty.setraw(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = sys.stdin.read(1 ) finally: termios.tcsetattr(__UpperCAmelCase , termios.TCSADRAIN , __UpperCAmelCase ) return ch def __magic_name__ ( ) -> List[str]: '''simple docstring''' __SCREAMING_SNAKE_CASE = get_raw_chars() if ord(__UpperCAmelCase ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(__UpperCAmelCase ) == KEYMAP["esc"]: __SCREAMING_SNAKE_CASE = get_raw_chars() if ord(__UpperCAmelCase ) == KEYMAP["mod_int"]: __SCREAMING_SNAKE_CASE = get_raw_chars() if ord(__UpperCAmelCase ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(__UpperCAmelCase ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(__UpperCAmelCase ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
13
0
'''simple docstring''' import copy import inspect import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import TimesformerConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_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 ( MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, TimesformerForVideoClassification, TimesformerModel, ) from transformers.models.timesformer.modeling_timesformer import TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from transformers import VideoMAEImageProcessor class __a : def __init__( self : Any ,lowerCamelCase : int ,lowerCamelCase : Any=13 ,lowerCamelCase : Any=10 ,lowerCamelCase : Optional[int]=3 ,lowerCamelCase : str=2 ,lowerCamelCase : List[str]=2 ,lowerCamelCase : Any=True ,lowerCamelCase : str=True ,lowerCamelCase : Optional[int]=32 ,lowerCamelCase : Optional[Any]=5 ,lowerCamelCase : List[str]=4 ,lowerCamelCase : Union[str, Any]=37 ,lowerCamelCase : Union[str, Any]="gelu" ,lowerCamelCase : int=0.1 ,lowerCamelCase : Dict=0.1 ,lowerCamelCase : List[str]=10 ,lowerCamelCase : List[Any]=0.02 ,lowerCamelCase : List[str]="divided_space_time" ,lowerCamelCase : Optional[int]=None ,): '''simple docstring''' __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = image_size __SCREAMING_SNAKE_CASE = num_channels __SCREAMING_SNAKE_CASE = patch_size __SCREAMING_SNAKE_CASE = num_frames __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 = attention_type __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = scope __SCREAMING_SNAKE_CASE = num_labels # in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token __SCREAMING_SNAKE_CASE = (image_size // patch_size) ** 2 __SCREAMING_SNAKE_CASE = (num_frames) * self.num_patches_per_frame + 1 def UpperCAmelCase__ ( self : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = floats_tensor( [self.batch_size, self.num_frames, 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.num_labels ) __SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TimesformerConfig( image_size=self.image_size ,patch_size=self.patch_size ,num_channels=self.num_channels ,num_frames=self.num_frames ,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 ,initializer_range=self.initializer_range ,attention_type=self.attention_type ,) __SCREAMING_SNAKE_CASE = self.num_labels return config def UpperCAmelCase__ ( self : Optional[int] ,lowerCamelCase : Optional[Any] ,lowerCamelCase : Any ,lowerCamelCase : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TimesformerModel(config=A_ ) model.to(A_ ) model.eval() __SCREAMING_SNAKE_CASE = model(A_ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase__ ( self : List[str] ,lowerCamelCase : Optional[int] ,lowerCamelCase : List[str] ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TimesformerForVideoClassification(A_ ) model.to(A_ ) model.eval() __SCREAMING_SNAKE_CASE = model(A_ ) # verify the logits shape __SCREAMING_SNAKE_CASE = torch.Size((self.batch_size, self.num_labels) ) self.parent.assertEqual(result.logits.shape ,A_ ) def UpperCAmelCase__ ( self : str ): '''simple docstring''' __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 __a ( _SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE, unittest.TestCase ): __UpperCamelCase : Optional[int] = (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else () __UpperCamelCase : Dict = ( {"feature-extraction": TimesformerModel, "video-classification": TimesformerForVideoClassification} if is_torch_available() else {} ) __UpperCamelCase : Optional[Any] = False __UpperCamelCase : int = False __UpperCamelCase : Any = False __UpperCamelCase : Any = False def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TimesformerModelTester(self ) __SCREAMING_SNAKE_CASE = ConfigTester( self ,config_class=A_ ,has_text_modality=A_ ,hidden_size=37 ) def UpperCAmelCase__ ( self : List[str] ,lowerCamelCase : Optional[int] ,lowerCamelCase : Optional[Any] ,lowerCamelCase : str=False ): '''simple docstring''' __SCREAMING_SNAKE_CASE = copy.deepcopy(A_ ) if return_labels: if model_class in get_values(A_ ): __SCREAMING_SNAKE_CASE = torch.zeros( self.model_tester.batch_size ,dtype=torch.long ,device=A_ ) return inputs_dict def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="""TimeSformer does not use inputs_embeds""" ) def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' pass def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __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(A_ ) self.assertIsInstance(model.get_input_embeddings() ,(nn.Module) ) __SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(A_ ,nn.Linear ) ) def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __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(A_ ) __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] ,A_ ) def UpperCAmelCase__ ( self : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A_ ) def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_video_classification(*A_ ) @slow def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __SCREAMING_SNAKE_CASE = TimesformerModel.from_pretrained(A_ ) self.assertIsNotNone(A_ ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' if not self.has_attentions: pass else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() __SCREAMING_SNAKE_CASE = True for model_class in self.all_model_classes: __SCREAMING_SNAKE_CASE = self.model_tester.seq_length __SCREAMING_SNAKE_CASE = self.model_tester.num_frames __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = model_class(A_ ) model.to(A_ ) model.eval() with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(A_ ,A_ ) ) __SCREAMING_SNAKE_CASE = outputs.attentions self.assertEqual(len(A_ ) ,self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = model_class(A_ ) model.to(A_ ) model.eval() with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(A_ ,A_ ) ) __SCREAMING_SNAKE_CASE = outputs.attentions self.assertEqual(len(A_ ) ,self.model_tester.num_hidden_layers ) # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1) self.assertListEqual( list(attentions[0].shape[-3:] ) ,[self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1] ,) __SCREAMING_SNAKE_CASE = len(A_ ) # Check attention is always last and order is fine __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = model_class(A_ ) model.to(A_ ) model.eval() with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(A_ ,A_ ) ) self.assertEqual(out_len + 1 ,len(A_ ) ) __SCREAMING_SNAKE_CASE = outputs.attentions self.assertEqual(len(A_ ) ,self.model_tester.num_hidden_layers ) # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1) self.assertListEqual( list(self_attentions[0].shape[-3:] ) ,[self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1] ,) def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' def check_hidden_states_output(lowerCamelCase : Tuple ,lowerCamelCase : List[Any] ,lowerCamelCase : Union[str, Any] ): __SCREAMING_SNAKE_CASE = model_class(A_ ) model.to(A_ ) model.eval() with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(A_ ,A_ ) ) __SCREAMING_SNAKE_CASE = outputs.hidden_states __SCREAMING_SNAKE_CASE = self.model_tester.num_hidden_layers + 1 self.assertEqual(len(A_ ) ,A_ ) __SCREAMING_SNAKE_CASE = self.model_tester.seq_length self.assertListEqual( list(hidden_states[0].shape[-2:] ) ,[seq_length, self.model_tester.hidden_size] ,) __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(A_ ,A_ ,A_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __SCREAMING_SNAKE_CASE = True check_hidden_states_output(A_ ,A_ ,A_ ) def __magic_name__ ( ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = hf_hub_download( repo_id="""hf-internal-testing/spaghetti-video""" , filename="""eating_spaghetti.npy""" , repo_type="""dataset""" ) __SCREAMING_SNAKE_CASE = np.load(__UpperCAmelCase ) return list(__UpperCAmelCase ) @require_torch @require_vision class __a ( unittest.TestCase ): @cached_property def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' return ( VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] ,image_std=[0.5, 0.5, 0.5] ) if is_vision_available() else None ) @slow def UpperCAmelCase__ ( self : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TimesformerForVideoClassification.from_pretrained("""facebook/timesformer-base-finetuned-k400""" ).to( A_ ) __SCREAMING_SNAKE_CASE = self.default_image_processor __SCREAMING_SNAKE_CASE = prepare_video() __SCREAMING_SNAKE_CASE = image_processor(video[:8] ,return_tensors="""pt""" ).to(A_ ) # forward pass with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(**A_ ) # verify the logits __SCREAMING_SNAKE_CASE = torch.Size((1, 400) ) self.assertEqual(outputs.logits.shape ,A_ ) __SCREAMING_SNAKE_CASE = torch.tensor([-0.3_016, -0.7_713, -0.4_205] ).to(A_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] ,A_ ,atol=1E-4 ) )
703
'''simple docstring''' from __future__ import annotations import bisect def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' if hi < 0: __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) while lo < hi: __SCREAMING_SNAKE_CASE = lo + (hi - lo) // 2 if sorted_collection[mid] < item: __SCREAMING_SNAKE_CASE = mid + 1 else: __SCREAMING_SNAKE_CASE = mid return lo def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' if hi < 0: __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) while lo < hi: __SCREAMING_SNAKE_CASE = lo + (hi - lo) // 2 if sorted_collection[mid] <= item: __SCREAMING_SNAKE_CASE = mid + 1 else: __SCREAMING_SNAKE_CASE = mid return lo def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> None: '''simple docstring''' sorted_collection.insert(bisect_left(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> None: '''simple docstring''' sorted_collection.insert(bisect_right(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> int | None: '''simple docstring''' __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) - 1 while left <= right: __SCREAMING_SNAKE_CASE = left + (right - left) // 2 __SCREAMING_SNAKE_CASE = sorted_collection[midpoint] if current_item == item: return midpoint elif item < current_item: __SCREAMING_SNAKE_CASE = midpoint - 1 else: __SCREAMING_SNAKE_CASE = midpoint + 1 return None def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> int | None: '''simple docstring''' __SCREAMING_SNAKE_CASE = bisect.bisect_left(__UpperCAmelCase , __UpperCAmelCase ) if index != len(__UpperCAmelCase ) and sorted_collection[index] == item: return index return None def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> int | None: '''simple docstring''' if right < left: return None __SCREAMING_SNAKE_CASE = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: return binary_search_by_recursion(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , midpoint - 1 ) else: return binary_search_by_recursion(__UpperCAmelCase , __UpperCAmelCase , midpoint + 1 , __UpperCAmelCase ) if __name__ == "__main__": a = input("Enter numbers separated by comma:\n").strip() a = sorted(int(item) for item in user_input.split(",")) a = int(input("Enter a single number to be found in the list:\n")) a = binary_search(collection, target) if result is None: print(F'''{target} was not found in {collection}.''') else: print(F'''{target} was found at position {result} in {collection}.''')
13
0
'''simple docstring''' import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.text import TextDatasetReader from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' assert isinstance(_lowercase , _lowercase ) assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Tuple: '''simple docstring''' __SCREAMING_SNAKE_CASE = tmp_path / 'cache' __SCREAMING_SNAKE_CASE = {'text': 'string'} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): __SCREAMING_SNAKE_CASE = TextDatasetReader(_lowercase , cache_dir=_lowercase , keep_in_memory=_lowercase ).read() _check_text_dataset(_lowercase , _lowercase ) @pytest.mark.parametrize( """features""" , [ None, {"""text""": """string"""}, {"""text""": """int32"""}, {"""text""": """float32"""}, ] , ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Tuple: '''simple docstring''' __SCREAMING_SNAKE_CASE = tmp_path / 'cache' __SCREAMING_SNAKE_CASE = {'text': 'string'} __SCREAMING_SNAKE_CASE = features.copy() if features else default_expected_features __SCREAMING_SNAKE_CASE = ( Features({feature: Value(_lowercase ) for feature, dtype in features.items()} ) if features is not None else None ) __SCREAMING_SNAKE_CASE = TextDatasetReader(_lowercase , features=_lowercase , cache_dir=_lowercase ).read() _check_text_dataset(_lowercase , _lowercase ) @pytest.mark.parametrize("""split""" , [None, NamedSplit("""train""" ), """train""", """test"""] ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = tmp_path / 'cache' __SCREAMING_SNAKE_CASE = {'text': 'string'} __SCREAMING_SNAKE_CASE = TextDatasetReader(_lowercase , cache_dir=_lowercase , split=_lowercase ).read() _check_text_dataset(_lowercase , _lowercase ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("""path_type""" , [str, list] ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> int: '''simple docstring''' if issubclass(_lowercase , _lowercase ): __SCREAMING_SNAKE_CASE = text_path elif issubclass(_lowercase , _lowercase ): __SCREAMING_SNAKE_CASE = [text_path] __SCREAMING_SNAKE_CASE = tmp_path / 'cache' __SCREAMING_SNAKE_CASE = {'text': 'string'} __SCREAMING_SNAKE_CASE = TextDatasetReader(_lowercase , cache_dir=_lowercase ).read() _check_text_dataset(_lowercase , _lowercase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=("train",) ) -> Optional[Any]: '''simple docstring''' assert isinstance(_lowercase , _lowercase ) for split in splits: __SCREAMING_SNAKE_CASE = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = tmp_path / 'cache' __SCREAMING_SNAKE_CASE = {'text': 'string'} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): __SCREAMING_SNAKE_CASE = TextDatasetReader({"""train""": text_path} , cache_dir=_lowercase , keep_in_memory=_lowercase ).read() _check_text_datasetdict(_lowercase , _lowercase ) @pytest.mark.parametrize( """features""" , [ None, {"""text""": """string"""}, {"""text""": """int32"""}, {"""text""": """float32"""}, ] , ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> List[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = tmp_path / 'cache' # CSV file loses col_1 string dtype information: default now is "int64" instead of "string" __SCREAMING_SNAKE_CASE = {'text': 'string'} __SCREAMING_SNAKE_CASE = features.copy() if features else default_expected_features __SCREAMING_SNAKE_CASE = ( Features({feature: Value(_lowercase ) for feature, dtype in features.items()} ) if features is not None else None ) __SCREAMING_SNAKE_CASE = TextDatasetReader({"""train""": text_path} , features=_lowercase , cache_dir=_lowercase ).read() _check_text_datasetdict(_lowercase , _lowercase ) @pytest.mark.parametrize("""split""" , [None, NamedSplit("""train""" ), """train""", """test"""] ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> int: '''simple docstring''' if split: __SCREAMING_SNAKE_CASE = {split: text_path} else: __SCREAMING_SNAKE_CASE = 'train' __SCREAMING_SNAKE_CASE = {'train': text_path, 'test': text_path} __SCREAMING_SNAKE_CASE = tmp_path / 'cache' __SCREAMING_SNAKE_CASE = {'text': 'string'} __SCREAMING_SNAKE_CASE = TextDatasetReader(_lowercase , cache_dir=_lowercase ).read() _check_text_datasetdict(_lowercase , _lowercase , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() )
704
'''simple docstring''' import math from enum import Enum from typing import Optional, Union from torch.optim import Optimizer from torch.optim.lr_scheduler import LambdaLR from .utils import logging a = logging.get_logger(__name__) class __a ( _snake_case ): __UpperCamelCase : int = 'linear' __UpperCamelCase : Tuple = 'cosine' __UpperCamelCase : Tuple = 'cosine_with_restarts' __UpperCamelCase : List[Any] = 'polynomial' __UpperCamelCase : Optional[Any] = 'constant' __UpperCamelCase : Optional[int] = 'constant_with_warmup' __UpperCamelCase : List[Any] = 'piecewise_constant' def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' return LambdaLR(__UpperCAmelCase , lambda __UpperCAmelCase : 1 , last_epoch=__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = -1 ) -> List[Any]: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1.0 , __UpperCAmelCase ) ) return 1.0 return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , last_epoch=__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = step_rules.split(""",""" ) for rule_str in rule_list[:-1]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = rule_str.split(""":""" ) __SCREAMING_SNAKE_CASE = int(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = float(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = value __SCREAMING_SNAKE_CASE = float(rule_list[-1] ) def create_rules_function(__UpperCAmelCase , __UpperCAmelCase ): def rule_func(__UpperCAmelCase ) -> float: __SCREAMING_SNAKE_CASE = sorted(rules_dict.keys() ) for i, sorted_step in enumerate(__UpperCAmelCase ): if steps < sorted_step: return rules_dict[sorted_steps[i]] return last_lr_multiple return rule_func __SCREAMING_SNAKE_CASE = create_rules_function(__UpperCAmelCase , __UpperCAmelCase ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , last_epoch=__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=-1 ) -> int: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) return max( 0.0 , float(num_training_steps - current_step ) / float(max(1 , num_training_steps - num_warmup_steps ) ) ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0.5 , __UpperCAmelCase = -1 ) -> Dict: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = float(current_step - num_warmup_steps ) / float(max(1 , num_training_steps - num_warmup_steps ) ) return max(0.0 , 0.5 * (1.0 + math.cos(math.pi * float(__UpperCAmelCase ) * 2.0 * progress )) ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 1 , __UpperCAmelCase = -1 ) -> Tuple: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = float(current_step - num_warmup_steps ) / float(max(1 , num_training_steps - num_warmup_steps ) ) if progress >= 1.0: return 0.0 return max(0.0 , 0.5 * (1.0 + math.cos(math.pi * ((float(__UpperCAmelCase ) * progress) % 1.0) )) ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=1e-7 , __UpperCAmelCase=1.0 , __UpperCAmelCase=-1 ) -> Tuple: '''simple docstring''' __SCREAMING_SNAKE_CASE = optimizer.defaults["""lr"""] if not (lr_init > lr_end): raise ValueError(f"""lr_end ({lr_end}) must be be smaller than initial lr ({lr_init})""" ) def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) elif current_step > num_training_steps: return lr_end / lr_init # as LambdaLR multiplies by lr_init else: __SCREAMING_SNAKE_CASE = lr_init - lr_end __SCREAMING_SNAKE_CASE = num_training_steps - num_warmup_steps __SCREAMING_SNAKE_CASE = 1 - (current_step - num_warmup_steps) / decay_steps __SCREAMING_SNAKE_CASE = lr_range * pct_remaining**power + lr_end return decay / lr_init # as LambdaLR multiplies by lr_init return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) a = { SchedulerType.LINEAR: get_linear_schedule_with_warmup, SchedulerType.COSINE: get_cosine_schedule_with_warmup, SchedulerType.COSINE_WITH_RESTARTS: get_cosine_with_hard_restarts_schedule_with_warmup, SchedulerType.POLYNOMIAL: get_polynomial_decay_schedule_with_warmup, SchedulerType.CONSTANT: get_constant_schedule, SchedulerType.CONSTANT_WITH_WARMUP: get_constant_schedule_with_warmup, SchedulerType.PIECEWISE_CONSTANT: get_piecewise_constant_schedule, } def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = 1 , __UpperCAmelCase = 1.0 , __UpperCAmelCase = -1 , ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = SchedulerType(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = TYPE_TO_SCHEDULER_FUNCTION[name] if name == SchedulerType.CONSTANT: return schedule_func(__UpperCAmelCase , last_epoch=__UpperCAmelCase ) if name == SchedulerType.PIECEWISE_CONSTANT: return schedule_func(__UpperCAmelCase , step_rules=__UpperCAmelCase , last_epoch=__UpperCAmelCase ) # All other schedulers require `num_warmup_steps` if num_warmup_steps is None: raise ValueError(f"""{name} requires `num_warmup_steps`, please provide that argument.""" ) if name == SchedulerType.CONSTANT_WITH_WARMUP: return schedule_func(__UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , last_epoch=__UpperCAmelCase ) # All other schedulers require `num_training_steps` if num_training_steps is None: raise ValueError(f"""{name} requires `num_training_steps`, please provide that argument.""" ) if name == SchedulerType.COSINE_WITH_RESTARTS: return schedule_func( __UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , num_training_steps=__UpperCAmelCase , num_cycles=__UpperCAmelCase , last_epoch=__UpperCAmelCase , ) if name == SchedulerType.POLYNOMIAL: return schedule_func( __UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , num_training_steps=__UpperCAmelCase , power=__UpperCAmelCase , last_epoch=__UpperCAmelCase , ) return schedule_func( __UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , num_training_steps=__UpperCAmelCase , last_epoch=__UpperCAmelCase )
13
0
'''simple docstring''' 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 __magic_name__ ( __UpperCAmelCase = N ) -> Optional[Any]: '''simple docstring''' return max( # mypy cannot properly interpret reduce int(reduce(lambda __UpperCAmelCase , __UpperCAmelCase : str(int(_A ) * int(_A ) ) , n[i : i + 13] ) ) for i in range(len(_A ) - 12 ) ) if __name__ == "__main__": print(F'''{solution() = }''')
705
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a = {"configuration_sew": ["SEW_PRETRAINED_CONFIG_ARCHIVE_MAP", "SEWConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = [ "SEW_PRETRAINED_MODEL_ARCHIVE_LIST", "SEWForCTC", "SEWForSequenceClassification", "SEWModel", "SEWPreTrainedModel", ] if TYPE_CHECKING: from .configuration_sew import SEW_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_sew import ( SEW_PRETRAINED_MODEL_ARCHIVE_LIST, SEWForCTC, SEWForSequenceClassification, SEWModel, SEWPreTrainedModel, ) else: import sys a = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
13
0
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> list[str]: '''simple docstring''' return [sentence[i : i + ngram_size] for i in range(len(lowerCamelCase_ ) - ngram_size + 1 )] if __name__ == "__main__": from doctest import testmod testmod()
706
'''simple docstring''' import requests from bsa import BeautifulSoup def __magic_name__ ( __UpperCAmelCase = "AAPL" ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""https://in.finance.yahoo.com/quote/{symbol}?s={symbol}""" __SCREAMING_SNAKE_CASE = BeautifulSoup(requests.get(__UpperCAmelCase ).text , """html.parser""" ) __SCREAMING_SNAKE_CASE = """My(6px) Pos(r) smartphone_Mt(6px)""" return soup.find("""div""" , class_=class_ ).find("""span""" ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(F'''Current {symbol:<4} stock price is {stock_price(symbol):>8}''')
13
0
'''simple docstring''' from collections import Counter from pathlib import Path from typing import Optional, Tuple import yaml class __a ( yaml.SafeLoader ): def UpperCAmelCase__ ( self : Optional[int] ,lowerCamelCase : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = [self.constructed_objects[key_node] for key_node, _ in node.value] __SCREAMING_SNAKE_CASE = [tuple(_lowercase ) if isinstance(_lowercase ,_lowercase ) else key for key in keys] __SCREAMING_SNAKE_CASE = Counter(_lowercase ) __SCREAMING_SNAKE_CASE = [key for key in counter if counter[key] > 1] if duplicate_keys: raise TypeError(f"""Got duplicate yaml keys: {duplicate_keys}""" ) def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : int ,lowerCamelCase : List[str]=False ): '''simple docstring''' __SCREAMING_SNAKE_CASE = super().construct_mapping(_lowercase ,deep=_lowercase ) self._check_no_duplicates_on_constructed_node(_lowercase ) return mapping def __magic_name__ ( __UpperCAmelCase ) -> Tuple[Optional[str], str]: '''simple docstring''' __SCREAMING_SNAKE_CASE = list(readme_content.splitlines() ) if full_content and full_content[0] == "---" and "---" in full_content[1:]: __SCREAMING_SNAKE_CASE = full_content[1:].index("""---""" ) + 1 __SCREAMING_SNAKE_CASE = '\n'.join(full_content[1:sep_idx] ) return yamlblock, "\n".join(full_content[sep_idx + 1 :] ) return None, "\n".join(__UpperCAmelCase ) class __a ( UpperCAmelCase_ ): __UpperCamelCase : Union[str, Any] = {'train_eval_index'} # train-eval-index in the YAML metadata @classmethod def UpperCAmelCase__ ( cls : Optional[Any] ,lowerCamelCase : Optional[int] ): '''simple docstring''' with open(_lowercase ,encoding="""utf-8""" ) as readme_file: __SCREAMING_SNAKE_CASE = _split_yaml_from_readme(readme_file.read() ) if yaml_string is not None: return cls.from_yaml_string(_lowercase ) else: return cls() def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : List[Any] ): '''simple docstring''' if path.exists(): with open(_lowercase ,encoding="""utf-8""" ) as readme_file: __SCREAMING_SNAKE_CASE = readme_file.read() else: __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = self._to_readme(_lowercase ) with open(_lowercase ,"""w""" ,encoding="""utf-8""" ) as readme_file: readme_file.write(_lowercase ) def UpperCAmelCase__ ( self : Union[str, Any] ,lowerCamelCase : Tuple = None ): '''simple docstring''' if readme_content is not None: __SCREAMING_SNAKE_CASE = _split_yaml_from_readme(_lowercase ) __SCREAMING_SNAKE_CASE = '---\n' + self.to_yaml_string() + '---\n' + content else: __SCREAMING_SNAKE_CASE = '---\n' + self.to_yaml_string() + '---\n' return full_content @classmethod def UpperCAmelCase__ ( cls : Optional[int] ,lowerCamelCase : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = yaml.load(_lowercase ,Loader=_NoDuplicateSafeLoader ) or {} # Convert the YAML keys to DatasetMetadata fields __SCREAMING_SNAKE_CASE = { (key.replace("""-""" ,"""_""" ) if key.replace("""-""" ,"""_""" ) in cls._FIELDS_WITH_DASHES else key): value for key, value in metadata_dict.items() } return cls(**_lowercase ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' return yaml.safe_dump( { (key.replace("""_""" ,"""-""" ) if key in self._FIELDS_WITH_DASHES else key): value for key, value in self.items() } ,sort_keys=_lowercase ,allow_unicode=_lowercase ,encoding="""utf-8""" ,).decode("""utf-8""" ) a = { "image-classification": [], "translation": [], "image-segmentation": [], "fill-mask": [], "automatic-speech-recognition": [], "token-classification": [], "sentence-similarity": [], "audio-classification": [], "question-answering": [], "summarization": [], "zero-shot-classification": [], "table-to-text": [], "feature-extraction": [], "other": [], "multiple-choice": [], "text-classification": [], "text-to-image": [], "text2text-generation": [], "zero-shot-image-classification": [], "tabular-classification": [], "tabular-regression": [], "image-to-image": [], "tabular-to-text": [], "unconditional-image-generation": [], "text-retrieval": [], "text-to-speech": [], "object-detection": [], "audio-to-audio": [], "text-generation": [], "conversational": [], "table-question-answering": [], "visual-question-answering": [], "image-to-text": [], "reinforcement-learning": [], "voice-activity-detection": [], "time-series-forecasting": [], "document-question-answering": [], } if __name__ == "__main__": from argparse import ArgumentParser a = ArgumentParser(usage="Validate the yaml metadata block of a README.md file.") ap.add_argument("readme_filepath") a = ap.parse_args() a = Path(args.readme_filepath) a = DatasetMetadata.from_readme(readme_filepath) print(dataset_metadata) dataset_metadata.to_readme(readme_filepath)
707
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase ) -> bool: '''simple docstring''' if num < 0: return False __SCREAMING_SNAKE_CASE = num __SCREAMING_SNAKE_CASE = 0 while num > 0: __SCREAMING_SNAKE_CASE = rev_num * 10 + (num % 10) num //= 10 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
13
0
'''simple docstring''' from __future__ import annotations import queue class __a : def __init__( self : Optional[int] ,lowerCamelCase : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = data __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None def __magic_name__ ( ) -> TreeNode: '''simple docstring''' print("""\n********Press N to stop entering at any point of time********\n""" ) __SCREAMING_SNAKE_CASE = input("""Enter the value of the root node: """ ).strip().lower() __SCREAMING_SNAKE_CASE = queue.Queue() __SCREAMING_SNAKE_CASE = TreeNode(int(__A ) ) q.put(__A ) while not q.empty(): __SCREAMING_SNAKE_CASE = q.get() __SCREAMING_SNAKE_CASE = f"""Enter the left node of {node_found.data}: """ __SCREAMING_SNAKE_CASE = input(__A ).strip().lower() or '''n''' if check == "n": return tree_node __SCREAMING_SNAKE_CASE = TreeNode(int(__A ) ) __SCREAMING_SNAKE_CASE = left_node q.put(__A ) __SCREAMING_SNAKE_CASE = f"""Enter the right node of {node_found.data}: """ __SCREAMING_SNAKE_CASE = input(__A ).strip().lower() or '''n''' if check == "n": return tree_node __SCREAMING_SNAKE_CASE = TreeNode(int(__A ) ) __SCREAMING_SNAKE_CASE = right_node q.put(__A ) raise def __magic_name__ ( __UpperCAmelCase ) -> None: '''simple docstring''' if not isinstance(__A , __A ) or not node: return print(node.data , end=""",""" ) pre_order(node.left ) pre_order(node.right ) def __magic_name__ ( __UpperCAmelCase ) -> None: '''simple docstring''' if not isinstance(__A , __A ) or not node: return in_order(node.left ) print(node.data , end=""",""" ) in_order(node.right ) def __magic_name__ ( __UpperCAmelCase ) -> None: '''simple docstring''' if not isinstance(__A , __A ) or not node: return post_order(node.left ) post_order(node.right ) print(node.data , end=""",""" ) def __magic_name__ ( __UpperCAmelCase ) -> None: '''simple docstring''' if not isinstance(__A , __A ) or not node: return __SCREAMING_SNAKE_CASE = queue.Queue() q.put(__A ) while not q.empty(): __SCREAMING_SNAKE_CASE = q.get() print(node_dequeued.data , end=""",""" ) if node_dequeued.left: q.put(node_dequeued.left ) if node_dequeued.right: q.put(node_dequeued.right ) def __magic_name__ ( __UpperCAmelCase ) -> None: '''simple docstring''' if not isinstance(__A , __A ) or not node: return __SCREAMING_SNAKE_CASE = queue.Queue() q.put(__A ) while not q.empty(): __SCREAMING_SNAKE_CASE = [] while not q.empty(): __SCREAMING_SNAKE_CASE = q.get() print(node_dequeued.data , end=""",""" ) if node_dequeued.left: list_.append(node_dequeued.left ) if node_dequeued.right: list_.append(node_dequeued.right ) print() for node in list_: q.put(__A ) def __magic_name__ ( __UpperCAmelCase ) -> None: '''simple docstring''' if not isinstance(__A , __A ) or not node: return __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = node while n or stack: while n: # start from root node, find its left child print(n.data , end=""",""" ) stack.append(__A ) __SCREAMING_SNAKE_CASE = n.left # end of while means current node doesn't have left child __SCREAMING_SNAKE_CASE = stack.pop() # start to traverse its right child __SCREAMING_SNAKE_CASE = n.right def __magic_name__ ( __UpperCAmelCase ) -> None: '''simple docstring''' if not isinstance(__A , __A ) or not node: return __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = node while n or stack: while n: stack.append(__A ) __SCREAMING_SNAKE_CASE = n.left __SCREAMING_SNAKE_CASE = stack.pop() print(n.data , end=""",""" ) __SCREAMING_SNAKE_CASE = n.right def __magic_name__ ( __UpperCAmelCase ) -> None: '''simple docstring''' if not isinstance(__A , __A ) or not node: return __SCREAMING_SNAKE_CASE = [], [] __SCREAMING_SNAKE_CASE = node stacka.append(__A ) while stacka: # to find the reversed order of post order, store it in stack2 __SCREAMING_SNAKE_CASE = stacka.pop() if n.left: stacka.append(n.left ) if n.right: stacka.append(n.right ) stacka.append(__A ) while stacka: # pop up from stack2 will be the post order print(stacka.pop().data , end=""",""" ) def __magic_name__ ( __UpperCAmelCase = "" , __UpperCAmelCase=50 , __UpperCAmelCase="*" ) -> str: '''simple docstring''' if not s: return "\n" + width * char __SCREAMING_SNAKE_CASE = divmod(width - len(__A ) - 2 , 2 ) return f"""{left * char} {s} {(left + extra) * char}""" if __name__ == "__main__": import doctest doctest.testmod() print(prompt("Binary Tree Traversals")) a = build_tree() print(prompt("Pre Order Traversal")) pre_order(node) print(prompt() + "\n") print(prompt("In Order Traversal")) in_order(node) print(prompt() + "\n") print(prompt("Post Order Traversal")) post_order(node) print(prompt() + "\n") print(prompt("Level Order Traversal")) level_order(node) print(prompt() + "\n") print(prompt("Actual Level Order Traversal")) level_order_actual(node) print("*" * 50 + "\n") print(prompt("Pre Order Traversal - Iteration Version")) pre_order_iter(node) print(prompt() + "\n") print(prompt("In Order Traversal - Iteration Version")) in_order_iter(node) print(prompt() + "\n") print(prompt("Post Order Traversal - Iteration Version")) post_order_iter(node) print(prompt())
708
'''simple docstring''' from __future__ import annotations from collections.abc import Callable a = list[list[float | int]] def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Matrix: '''simple docstring''' __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [[0 for _ in range(size + 1 )] for _ in range(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 for row in range(__UpperCAmelCase ): for col in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = matrix[row][col] __SCREAMING_SNAKE_CASE = vector[row][0] __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 while row < size and col < size: # pivoting __SCREAMING_SNAKE_CASE = max((abs(augmented[rowa][col] ), rowa) for rowa in range(__UpperCAmelCase , __UpperCAmelCase ) )[ 1 ] if augmented[pivot_row][col] == 0: col += 1 continue else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = augmented[pivot_row], augmented[row] for rowa in range(row + 1 , __UpperCAmelCase ): __SCREAMING_SNAKE_CASE = augmented[rowa][col] / augmented[row][col] __SCREAMING_SNAKE_CASE = 0 for cola in range(col + 1 , size + 1 ): augmented[rowa][cola] -= augmented[row][cola] * ratio row += 1 col += 1 # back substitution for col in range(1 , __UpperCAmelCase ): for row in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = augmented[row][col] / augmented[col][col] for cola in range(__UpperCAmelCase , size + 1 ): augmented[row][cola] -= augmented[col][cola] * ratio # round to get rid of numbers like 2.000000000000004 return [ [round(augmented[row][size] / augmented[row][row] , 10 )] for row in range(__UpperCAmelCase ) ] def __magic_name__ ( __UpperCAmelCase ) -> Callable[[int], int]: '''simple docstring''' __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [[0 for _ in range(__UpperCAmelCase )] for _ in range(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = [[0] for _ in range(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 for x_val, y_val in enumerate(__UpperCAmelCase ): for col in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = (x_val + 1) ** (size - col - 1) __SCREAMING_SNAKE_CASE = y_val __SCREAMING_SNAKE_CASE = solve(__UpperCAmelCase , __UpperCAmelCase ) def interpolated_func(__UpperCAmelCase ) -> int: return sum( round(coeffs[x_val][0] ) * (var ** (size - x_val - 1)) for x_val in range(__UpperCAmelCase ) ) return interpolated_func def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' return ( 1 - variable + variable**2 - variable**3 + variable**4 - variable**5 + variable**6 - variable**7 + variable**8 - variable**9 + variable**10 ) def __magic_name__ ( __UpperCAmelCase = question_function , __UpperCAmelCase = 10 ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = [func(__UpperCAmelCase ) for x_val in range(1 , order + 1 )] __SCREAMING_SNAKE_CASE = [ interpolate(data_points[:max_coeff] ) for max_coeff in range(1 , order + 1 ) ] __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 for poly in polynomials: __SCREAMING_SNAKE_CASE = 1 while func(__UpperCAmelCase ) == poly(__UpperCAmelCase ): x_val += 1 ret += poly(__UpperCAmelCase ) return ret if __name__ == "__main__": print(F'''{solution() = }''')
13
0
'''simple docstring''' from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available from ...utils import OptionalDependencyNotAvailable a = {'configuration_dpt': ['DPT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DPTConfig']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = ['DPTFeatureExtractor'] a = ['DPTImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = [ 'DPT_PRETRAINED_MODEL_ARCHIVE_LIST', 'DPTForDepthEstimation', 'DPTForSemanticSegmentation', 'DPTModel', 'DPTPreTrainedModel', ] if TYPE_CHECKING: from .configuration_dpt import DPT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPTConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_dpt import DPTFeatureExtractor from .image_processing_dpt import DPTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dpt import ( DPT_PRETRAINED_MODEL_ARCHIVE_LIST, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel, DPTPreTrainedModel, ) else: import sys a = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
709
'''simple docstring''' from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax a = logging.get_logger(__name__) @add_end_docstrings(_snake_case ) class __a ( _snake_case ): def __init__( self : Union[str, Any] ,**lowerCamelCase : str ): '''simple docstring''' super().__init__(**lowerCamelCase ) requires_backends(self ,"""vision""" ) self.check_model_type( TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if self.framework == """tf""" else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING ) def __call__( self : Dict ,lowerCamelCase : Union[str, List[str], "Image", List["Image"]] ,**lowerCamelCase : Optional[Any] ): '''simple docstring''' return super().__call__(lowerCamelCase ,**lowerCamelCase ) def UpperCAmelCase__ ( self : Optional[Any] ,**lowerCamelCase : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = {} if "candidate_labels" in kwargs: __SCREAMING_SNAKE_CASE = kwargs["""candidate_labels"""] if "hypothesis_template" in kwargs: __SCREAMING_SNAKE_CASE = kwargs["""hypothesis_template"""] return preprocess_params, {}, {} def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : Union[str, Any]=None ,lowerCamelCase : Union[str, Any]="This is a photo of {}." ): '''simple docstring''' __SCREAMING_SNAKE_CASE = load_image(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.image_processor(images=[image] ,return_tensors=self.framework ) __SCREAMING_SNAKE_CASE = candidate_labels __SCREAMING_SNAKE_CASE = [hypothesis_template.format(lowerCamelCase ) for x in candidate_labels] __SCREAMING_SNAKE_CASE = self.tokenizer(lowerCamelCase ,return_tensors=self.framework ,padding=lowerCamelCase ) __SCREAMING_SNAKE_CASE = [text_inputs] return inputs def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = model_inputs.pop("""candidate_labels""" ) __SCREAMING_SNAKE_CASE = model_inputs.pop("""text_inputs""" ) if isinstance(text_inputs[0] ,lowerCamelCase ): __SCREAMING_SNAKE_CASE = text_inputs[0] else: # Batching case. __SCREAMING_SNAKE_CASE = text_inputs[0][0] __SCREAMING_SNAKE_CASE = self.model(**lowerCamelCase ,**lowerCamelCase ) __SCREAMING_SNAKE_CASE = { """candidate_labels""": candidate_labels, """logits""": outputs.logits_per_image, } return model_outputs def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = model_outputs.pop("""candidate_labels""" ) __SCREAMING_SNAKE_CASE = model_outputs["""logits"""][0] if self.framework == "pt": __SCREAMING_SNAKE_CASE = logits.softmax(dim=-1 ).squeeze(-1 ) __SCREAMING_SNAKE_CASE = probs.tolist() if not isinstance(lowerCamelCase ,lowerCamelCase ): __SCREAMING_SNAKE_CASE = [scores] elif self.framework == "tf": __SCREAMING_SNAKE_CASE = stable_softmax(lowerCamelCase ,axis=-1 ) __SCREAMING_SNAKE_CASE = probs.numpy().tolist() else: raise ValueError(f"""Unsupported framework: {self.framework}""" ) __SCREAMING_SNAKE_CASE = [ {"""score""": score, """label""": candidate_label} for score, candidate_label in sorted(zip(lowerCamelCase ,lowerCamelCase ) ,key=lambda lowerCamelCase : -x[0] ) ] return result
13
0
'''simple docstring''' import inspect import unittest import numpy as np from tests.test_modeling_common import floats_tensor from transformers import MaskaFormerConfig, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaskaFormerForUniversalSegmentation, MaskaFormerModel if is_vision_available(): from transformers import MaskaFormerImageProcessor if is_vision_available(): from PIL import Image class __a : def __init__( self : List[str] ,lowerCamelCase : List[str] ,lowerCamelCase : Optional[int]=2 ,lowerCamelCase : List[str]=True ,lowerCamelCase : List[Any]=False ,lowerCamelCase : List[Any]=10 ,lowerCamelCase : str=3 ,lowerCamelCase : Optional[Any]=32 * 8 ,lowerCamelCase : Optional[int]=32 * 8 ,lowerCamelCase : Any=4 ,lowerCamelCase : Any=64 ,): '''simple docstring''' __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = is_training __SCREAMING_SNAKE_CASE = use_auxiliary_loss __SCREAMING_SNAKE_CASE = num_queries __SCREAMING_SNAKE_CASE = num_channels __SCREAMING_SNAKE_CASE = min_size __SCREAMING_SNAKE_CASE = max_size __SCREAMING_SNAKE_CASE = num_labels __SCREAMING_SNAKE_CASE = hidden_dim __SCREAMING_SNAKE_CASE = hidden_dim def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to( UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = torch.ones([self.batch_size, self.min_size, self.max_size] ,device=UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = ( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] ,device=UpperCamelCase_ ) > 0.5 ).float() __SCREAMING_SNAKE_CASE = (torch.rand((self.batch_size, self.num_labels) ,device=UpperCamelCase_ ) > 0.5).long() __SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = MaskaFormerConfig( hidden_size=self.hidden_dim ,) __SCREAMING_SNAKE_CASE = self.num_queries __SCREAMING_SNAKE_CASE = self.num_labels __SCREAMING_SNAKE_CASE = [1, 1, 1, 1] __SCREAMING_SNAKE_CASE = self.num_channels __SCREAMING_SNAKE_CASE = 64 __SCREAMING_SNAKE_CASE = 128 __SCREAMING_SNAKE_CASE = self.hidden_dim __SCREAMING_SNAKE_CASE = self.hidden_dim __SCREAMING_SNAKE_CASE = self.hidden_dim return config def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values, "pixel_mask": pixel_mask} return config, inputs_dict def UpperCAmelCase__ ( self : str ,lowerCamelCase : List[str] ,lowerCamelCase : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = output.encoder_hidden_states __SCREAMING_SNAKE_CASE = output.pixel_decoder_hidden_states __SCREAMING_SNAKE_CASE = output.transformer_decoder_hidden_states self.parent.assertTrue(len(UpperCamelCase_ ) ,len(config.backbone_config.depths ) ) self.parent.assertTrue(len(UpperCamelCase_ ) ,len(config.backbone_config.depths ) ) self.parent.assertTrue(len(UpperCamelCase_ ) ,config.decoder_layers ) def UpperCAmelCase__ ( self : int ,lowerCamelCase : Tuple ,lowerCamelCase : List[Any] ,lowerCamelCase : Dict ,lowerCamelCase : Union[str, Any]=False ): '''simple docstring''' with torch.no_grad(): __SCREAMING_SNAKE_CASE = MaskaFormerModel(config=UpperCamelCase_ ) model.to(UpperCamelCase_ ) model.eval() __SCREAMING_SNAKE_CASE = model(pixel_values=UpperCamelCase_ ,pixel_mask=UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = model(UpperCamelCase_ ,output_hidden_states=UpperCamelCase_ ) self.parent.assertEqual( output.transformer_decoder_last_hidden_state.shape ,(self.batch_size, self.num_queries, self.hidden_dim) ,) # let's ensure the other two hidden state exists self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(output.encoder_last_hidden_state is not None ) if output_hidden_states: self.check_output_hidden_state(UpperCamelCase_ ,UpperCamelCase_ ) def UpperCAmelCase__ ( self : Any ,lowerCamelCase : Dict ,lowerCamelCase : Optional[Any] ,lowerCamelCase : Tuple ,lowerCamelCase : Tuple ,lowerCamelCase : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = MaskaFormerForUniversalSegmentation(config=UpperCamelCase_ ) model.to(UpperCamelCase_ ) model.eval() def comm_check_on_output(lowerCamelCase : List[Any] ): # let's still check that all the required stuff is there self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.encoder_last_hidden_state is not None ) # okay, now we need to check the logits shape # due to the encoder compression, masks have a //4 spatial size self.parent.assertEqual( result.masks_queries_logits.shape ,(self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4) ,) # + 1 for null class self.parent.assertEqual( result.class_queries_logits.shape ,(self.batch_size, self.num_queries, self.num_labels + 1) ) with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(pixel_values=UpperCamelCase_ ,pixel_mask=UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = model(UpperCamelCase_ ) comm_check_on_output(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = model( pixel_values=UpperCamelCase_ ,pixel_mask=UpperCamelCase_ ,mask_labels=UpperCamelCase_ ,class_labels=UpperCamelCase_ ) comm_check_on_output(UpperCamelCase_ ) self.parent.assertTrue(result.loss is not None ) self.parent.assertEqual(result.loss.shape ,torch.Size([1] ) ) @require_torch class __a ( _snake_case, _snake_case, unittest.TestCase ): __UpperCamelCase : List[Any] = (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else () __UpperCamelCase : Tuple = {"feature-extraction": MaskaFormerModel} if is_torch_available() else {} __UpperCamelCase : List[str] = False __UpperCamelCase : Optional[int] = False __UpperCamelCase : Tuple = False __UpperCamelCase : Optional[Any] = False def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = MaskaFormerModelTester(self ) __SCREAMING_SNAKE_CASE = ConfigTester(self ,config_class=UpperCamelCase_ ,has_text_modality=UpperCamelCase_ ) def UpperCAmelCase__ ( self : int ): '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(UpperCamelCase_ ,**UpperCamelCase_ ,output_hidden_states=UpperCamelCase_ ) def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*UpperCamelCase_ ) @unittest.skip(reason="""Mask2Former does not use inputs_embeds""" ) def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' pass @unittest.skip(reason="""Mask2Former does not have a get_input_embeddings method""" ) def UpperCAmelCase__ ( self : str ): '''simple docstring''' pass @unittest.skip(reason="""Mask2Former is not a generative model""" ) def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' pass @unittest.skip(reason="""Mask2Former does not use token embeddings""" ) def UpperCAmelCase__ ( self : Any ): '''simple docstring''' pass @require_torch_multi_gpu @unittest.skip( reason="""Mask2Former has some layers using `add_module` which doesn't work well with `nn.DataParallel`""" ) def UpperCAmelCase__ ( self : Any ): '''simple docstring''' pass @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' pass def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __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(UpperCamelCase_ ) __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] ,UpperCamelCase_ ) @slow def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' for model_name in ["facebook/mask2former-swin-small-coco-instance"]: __SCREAMING_SNAKE_CASE = MaskaFormerModel.from_pretrained(UpperCamelCase_ ) self.assertIsNotNone(UpperCamelCase_ ) def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = (self.model_tester.min_size,) * 2 __SCREAMING_SNAKE_CASE = { "pixel_values": torch.randn((2, 3, *size) ,device=UpperCamelCase_ ), "mask_labels": torch.randn((2, 10, *size) ,device=UpperCamelCase_ ), "class_labels": torch.zeros(2 ,10 ,device=UpperCamelCase_ ).long(), } __SCREAMING_SNAKE_CASE = self.model_tester.get_config() __SCREAMING_SNAKE_CASE = MaskaFormerForUniversalSegmentation(UpperCamelCase_ ).to(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = model(**UpperCamelCase_ ) self.assertTrue(outputs.loss is not None ) def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(UpperCamelCase_ ,**UpperCamelCase_ ,output_hidden_states=UpperCamelCase_ ) def UpperCAmelCase__ ( self : int ): '''simple docstring''' __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(UpperCamelCase_ ).to(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = model(**UpperCamelCase_ ,output_attentions=UpperCamelCase_ ) self.assertTrue(outputs.attentions is not None ) def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' if not self.model_tester.is_training: return __SCREAMING_SNAKE_CASE = self.all_model_classes[1] __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE = model_class(UpperCamelCase_ ) model.to(UpperCamelCase_ ) model.train() __SCREAMING_SNAKE_CASE = model(UpperCamelCase_ ,mask_labels=UpperCamelCase_ ,class_labels=UpperCamelCase_ ).loss loss.backward() def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.all_model_classes[1] __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = model_class(UpperCamelCase_ ).to(UpperCamelCase_ ) model.train() __SCREAMING_SNAKE_CASE = model(UpperCamelCase_ ,mask_labels=UpperCamelCase_ ,class_labels=UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() __SCREAMING_SNAKE_CASE = outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() __SCREAMING_SNAKE_CASE = outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() __SCREAMING_SNAKE_CASE = outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=UpperCamelCase_ ) self.assertIsNotNone(encoder_hidden_states.grad ) self.assertIsNotNone(pixel_decoder_hidden_states.grad ) self.assertIsNotNone(transformer_decoder_hidden_states.grad ) self.assertIsNotNone(attentions.grad ) a = 1E-4 def __magic_name__ ( ) -> List[str]: '''simple docstring''' __SCREAMING_SNAKE_CASE = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_vision @slow class __a ( unittest.TestCase ): @cached_property def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' return "facebook/mask2former-swin-small-coco-instance" @cached_property def UpperCAmelCase__ ( self : Any ): '''simple docstring''' return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints ) if is_vision_available() else None def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = MaskaFormerModel.from_pretrained(self.model_checkpoints ).to(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = self.default_image_processor __SCREAMING_SNAKE_CASE = prepare_img() __SCREAMING_SNAKE_CASE = image_processor(UpperCamelCase_ ,return_tensors="""pt""" ).to(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = inputs["pixel_values"].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(UpperCamelCase_ ,(1, 3, 384, 384) ) with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(**UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = torch.tensor( [[-0.2_790, -1.0_717, -1.1_668], [-0.5_128, -0.3_128, -0.4_987], [-0.5_832, 0.1_971, -0.0_197]] ).to(UpperCamelCase_ ) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3] ,UpperCamelCase_ ,atol=UpperCamelCase_ ) ) __SCREAMING_SNAKE_CASE = torch.tensor( [[0.8_973, 1.1_847, 1.1_776], [1.1_934, 1.5_040, 1.5_128], [1.1_153, 1.4_486, 1.4_951]] ).to(UpperCamelCase_ ) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] ,UpperCamelCase_ ,atol=UpperCamelCase_ ) ) __SCREAMING_SNAKE_CASE = torch.tensor( [[2.1_152, 1.7_000, -0.8_603], [1.5_808, 1.8_004, -0.9_353], [1.6_043, 1.7_495, -0.5_999]] ).to(UpperCamelCase_ ) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3] ,UpperCamelCase_ ,atol=UpperCamelCase_ ) ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(UpperCamelCase_ ).eval() __SCREAMING_SNAKE_CASE = self.default_image_processor __SCREAMING_SNAKE_CASE = prepare_img() __SCREAMING_SNAKE_CASE = image_processor(UpperCamelCase_ ,return_tensors="""pt""" ).to(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = inputs["pixel_values"].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(UpperCamelCase_ ,(1, 3, 384, 384) ) with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(**UpperCamelCase_ ) # masks_queries_logits __SCREAMING_SNAKE_CASE = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape ,(1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) ) __SCREAMING_SNAKE_CASE = [ [-8.7_839, -9.0_056, -8.8_121], [-7.4_104, -7.0_313, -6.5_401], [-6.6_105, -6.3_427, -6.4_675], ] __SCREAMING_SNAKE_CASE = torch.tensor(UpperCamelCase_ ).to(UpperCamelCase_ ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] ,UpperCamelCase_ ,atol=UpperCamelCase_ ) ) # class_queries_logits __SCREAMING_SNAKE_CASE = outputs.class_queries_logits self.assertEqual(class_queries_logits.shape ,(1, model.config.num_queries, model.config.num_labels + 1) ) __SCREAMING_SNAKE_CASE = torch.tensor( [ [1.8_324, -8.0_835, -4.1_922], [0.8_450, -9.0_050, -3.6_053], [0.3_045, -7.7_293, -3.0_275], ] ).to(UpperCamelCase_ ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] ,UpperCamelCase_ ,atol=UpperCamelCase_ ) ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(UpperCamelCase_ ).eval() __SCREAMING_SNAKE_CASE = self.default_image_processor __SCREAMING_SNAKE_CASE = image_processor( [np.zeros((3, 800, 1333) ), np.zeros((3, 800, 1333) )] ,segmentation_maps=[np.zeros((384, 384) ).astype(np.floataa ), np.zeros((384, 384) ).astype(np.floataa )] ,return_tensors="""pt""" ,) __SCREAMING_SNAKE_CASE = inputs["pixel_values"].to(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = [el.to(UpperCamelCase_ ) for el in inputs["mask_labels"]] __SCREAMING_SNAKE_CASE = [el.to(UpperCamelCase_ ) for el in inputs["class_labels"]] with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(**UpperCamelCase_ ) self.assertTrue(outputs.loss is not None )
710
'''simple docstring''' from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers a = [ "python", "tqdm", "regex", "requests", "packaging", "filelock", "numpy", "tokenizers", "huggingface-hub", "safetensors", "accelerate", "pyyaml", ] for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed elif pkg == "accelerate": # must be loaded here, or else tqdm check may fail from .utils import is_accelerate_available # Maybe switch to is_torch_available in the future here so that Accelerate is hard dep of # Transformers with PyTorch if not is_accelerate_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(F'''can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py''') def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase=None ) -> Optional[Any]: '''simple docstring''' require_version(deps[pkg] , __UpperCAmelCase )
13
0
'''simple docstring''' import importlib.metadata import operator import re import sys from typing import Optional from packaging import version a = { '<': operator.lt, '<=': operator.le, '==': operator.eq, '!=': operator.ne, '>=': operator.ge, '>': operator.gt, } def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> int: '''simple docstring''' if got_ver is None or want_ver is None: raise ValueError( f"""Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider""" f""" reinstalling {pkg}.""" ) if not ops[op](version.parse(_lowerCAmelCase ) , version.parse(_lowerCAmelCase ) ): raise ImportError( f"""{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}""" ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase = None ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""\n{hint}""" if hint is not None else "" # non-versioned check if re.match(R"""^[\w_\-\d]+$""" , _lowerCAmelCase ): __SCREAMING_SNAKE_CASE = requirement, None, None else: __SCREAMING_SNAKE_CASE = re.findall(R"""^([^!=<>\s]+)([\s!=<>]{1,2}.+)""" , _lowerCAmelCase ) if not match: raise ValueError( """requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but""" f""" got {requirement}""" ) __SCREAMING_SNAKE_CASE = match[0] __SCREAMING_SNAKE_CASE = want_full.split(""",""" ) # there could be multiple requirements __SCREAMING_SNAKE_CASE = {} for w in want_range: __SCREAMING_SNAKE_CASE = re.findall(R"""^([\s!=<>]{1,2})(.+)""" , _lowerCAmelCase ) if not match: raise ValueError( """requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,""" f""" but got {requirement}""" ) __SCREAMING_SNAKE_CASE = match[0] __SCREAMING_SNAKE_CASE = want_ver if op not in ops: raise ValueError(f"""{requirement}: need one of {list(ops.keys() )}, but got {op}""" ) # special case if pkg == "python": __SCREAMING_SNAKE_CASE = ".".join([str(_lowerCAmelCase ) for x in sys.version_info[:3]] ) for op, want_ver in wanted.items(): _compare_versions(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) return # check if any version is installed try: __SCREAMING_SNAKE_CASE = importlib.metadata.version(_lowerCAmelCase ) except importlib.metadata.PackageNotFoundError: raise importlib.metadata.PackageNotFoundError( f"""The \'{requirement}\' distribution was not found and is required by this application. {hint}""" ) # check that the right version is installed if version number or a range was provided if want_ver is not None: for op, want_ver in wanted.items(): _compare_versions(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) def __magic_name__ ( __UpperCAmelCase ) -> Optional[int]: '''simple docstring''' __SCREAMING_SNAKE_CASE = "Try: pip install transformers -U or pip install -e '.[dev]' if you're working with git main" return require_version(_lowerCAmelCase , _lowerCAmelCase )
711
'''simple docstring''' import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import numpy as np import pandas as pd from datasets import load_dataset import transformers from transformers import ( AutoConfig, BartForSequenceClassification, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, TapexTokenizer, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.17.0.dev0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/text-classification/requirements.txt") a = logging.getLogger(__name__) @dataclass class __a : __UpperCamelCase : Optional[str] = field( default='tab_fact', metadata={'help': 'The name of the dataset to use (via the datasets library).'} ) __UpperCamelCase : Optional[str] = field( default='tab_fact', metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'}, ) __UpperCamelCase : int = field( default=1024, metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) }, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={'help': 'Overwrite the cached preprocessed datasets or not.'} ) __UpperCamelCase : bool = field( default=_snake_case, metadata={ 'help': ( 'Whether to pad all samples to `max_seq_length`. ' 'If False, will pad the samples dynamically when batching to the maximum length in the batch.' ) }, ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) }, ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) }, ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of prediction examples to this ' 'value if set.' ) }, ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'A csv or a json file containing the training data.'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'A csv or a json file containing the validation data.'} ) __UpperCamelCase : Optional[str] = field(default=_snake_case, metadata={'help': 'A csv or a json file containing the test data.'} ) def UpperCAmelCase__ ( self : int ): '''simple docstring''' if self.dataset_name is not None: pass elif self.train_file is None or self.validation_file is None: raise ValueError("""Need either a GLUE task, a training/validation file or a dataset name.""" ) else: __SCREAMING_SNAKE_CASE = self.train_file.split(""".""" )[-1] assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file." __SCREAMING_SNAKE_CASE = self.validation_file.split(""".""" )[-1] assert ( validation_extension == train_extension ), "`validation_file` should have the same extension (csv or json) as `train_file`." @dataclass class __a : __UpperCamelCase : str = field( default=_snake_case, metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'}, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={'help': 'Whether to use one of the fast tokenizer (backed by the tokenizers library) or not.'}, ) __UpperCamelCase : str = field( default='main', metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'}, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) }, ) def __magic_name__ ( ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = parser.parse_args_into_dataclasses() # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) __SCREAMING_SNAKE_CASE = training_args.get_process_log_level() logger.setLevel(__UpperCAmelCase ) datasets.utils.logging.set_verbosity(__UpperCAmelCase ) transformers.utils.logging.set_verbosity(__UpperCAmelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + f"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(f"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. __SCREAMING_SNAKE_CASE = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: __SCREAMING_SNAKE_CASE = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. """ """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON training and evaluation files (see below) # or specify a GLUE benchmark task (the dataset will be downloaded automatically from the datasets Hub). # # For JSON files, this script will use the `question` column for the input question and `table` column for the corresponding table. # # If the CSVs/JSONs contain only one non-label column, the script does single sentence classification on this # single column. You can easily tweak this behavior (see below) # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. __SCREAMING_SNAKE_CASE = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) else: # Loading a dataset from your local files. # CSV/JSON training and evaluation files are needed. __SCREAMING_SNAKE_CASE = {"""train""": data_args.train_file, """validation""": data_args.validation_file} # Get the test dataset: you can provide your own CSV/JSON test file (see below) # when you use `do_predict` without specifying a GLUE benchmark task. if training_args.do_predict: if data_args.test_file is not None: __SCREAMING_SNAKE_CASE = data_args.train_file.split(""".""" )[-1] __SCREAMING_SNAKE_CASE = data_args.test_file.split(""".""" )[-1] assert ( test_extension == train_extension ), "`test_file` should have the same extension (csv or json) as `train_file`." __SCREAMING_SNAKE_CASE = data_args.test_file else: raise ValueError("""Need either a GLUE task or a test file for `do_predict`.""" ) for key in data_files.keys(): logger.info(f"""load a local file for {key}: {data_files[key]}""" ) if data_args.train_file.endswith(""".csv""" ): # Loading a dataset from local csv files __SCREAMING_SNAKE_CASE = load_dataset("""csv""" , data_files=__UpperCAmelCase , cache_dir=model_args.cache_dir ) else: # Loading a dataset from local json files __SCREAMING_SNAKE_CASE = load_dataset("""json""" , data_files=__UpperCAmelCase , cache_dir=model_args.cache_dir ) # See more about loading any type of standard or custom dataset at # https://huggingface.co/docs/datasets/loading_datasets.html. # Labels __SCREAMING_SNAKE_CASE = raw_datasets["""train"""].features["""label"""].names __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) # Load pretrained model and tokenizer # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. __SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=__UpperCAmelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # load tapex tokenizer __SCREAMING_SNAKE_CASE = TapexTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , add_prefix_space=__UpperCAmelCase , ) __SCREAMING_SNAKE_CASE = BartForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=__UpperCAmelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Padding strategy if data_args.pad_to_max_length: __SCREAMING_SNAKE_CASE = """max_length""" else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch __SCREAMING_SNAKE_CASE = False # Some models have set the order of the labels to use, so let's make sure we do use it. __SCREAMING_SNAKE_CASE = {"""Refused""": 0, """Entailed""": 1} __SCREAMING_SNAKE_CASE = {0: """Refused""", 1: """Entailed"""} if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( f"""The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the""" f"""model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}.""" ) __SCREAMING_SNAKE_CASE = min(data_args.max_seq_length , tokenizer.model_max_length ) def preprocess_tabfact_function(__UpperCAmelCase ): # Tokenize the texts def _convert_table_text_to_pandas(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = [_table_row.split("""#""" ) for _table_row in _table_text.strip("""\n""" ).split("""\n""" )] __SCREAMING_SNAKE_CASE = pd.DataFrame.from_records(_table_content[1:] , columns=_table_content[0] ) return _table_pd __SCREAMING_SNAKE_CASE = examples["""statement"""] __SCREAMING_SNAKE_CASE = list(map(_convert_table_text_to_pandas , examples["""table_text"""] ) ) __SCREAMING_SNAKE_CASE = tokenizer(__UpperCAmelCase , __UpperCAmelCase , padding=__UpperCAmelCase , max_length=__UpperCAmelCase , truncation=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = examples["""label"""] return result with training_args.main_process_first(desc="""dataset map pre-processing""" ): __SCREAMING_SNAKE_CASE = raw_datasets.map( __UpperCAmelCase , batched=__UpperCAmelCase , load_from_cache_file=not data_args.overwrite_cache , desc="""Running tokenizer on dataset""" , ) if training_args.do_train: if "train" not in raw_datasets: raise ValueError("""--do_train requires a train dataset""" ) __SCREAMING_SNAKE_CASE = raw_datasets["""train"""] if data_args.max_train_samples is not None: __SCREAMING_SNAKE_CASE = train_dataset.select(range(data_args.max_train_samples ) ) if training_args.do_eval: if "validation" not in raw_datasets and "validation_matched" not in raw_datasets: raise ValueError("""--do_eval requires a validation dataset""" ) __SCREAMING_SNAKE_CASE = raw_datasets["""validation"""] if data_args.max_eval_samples is not None: __SCREAMING_SNAKE_CASE = eval_dataset.select(range(data_args.max_eval_samples ) ) if training_args.do_predict or data_args.test_file is not None: if "test" not in raw_datasets and "test_matched" not in raw_datasets: raise ValueError("""--do_predict requires a test dataset""" ) __SCREAMING_SNAKE_CASE = raw_datasets["""test"""] if data_args.max_predict_samples is not None: __SCREAMING_SNAKE_CASE = predict_dataset.select(range(data_args.max_predict_samples ) ) # Log a few random samples from the training set: if training_args.do_train: for index in random.sample(range(len(__UpperCAmelCase ) ) , 3 ): logger.info(f"""Sample {index} of the training set: {train_dataset[index]}.""" ) # You can define your custom compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = p.predictions[0] if isinstance(p.predictions , __UpperCAmelCase ) else p.predictions __SCREAMING_SNAKE_CASE = np.argmax(__UpperCAmelCase , axis=1 ) return {"accuracy": (preds == p.label_ids).astype(np.floataa ).mean().item()} # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: __SCREAMING_SNAKE_CASE = default_data_collator elif training_args.fpaa: __SCREAMING_SNAKE_CASE = DataCollatorWithPadding(__UpperCAmelCase , pad_to_multiple_of=8 ) else: __SCREAMING_SNAKE_CASE = None # Initialize our Trainer __SCREAMING_SNAKE_CASE = Trainer( model=__UpperCAmelCase , args=__UpperCAmelCase , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=__UpperCAmelCase , tokenizer=__UpperCAmelCase , data_collator=__UpperCAmelCase , ) # Training if training_args.do_train: __SCREAMING_SNAKE_CASE = None if training_args.resume_from_checkpoint is not None: __SCREAMING_SNAKE_CASE = training_args.resume_from_checkpoint elif last_checkpoint is not None: __SCREAMING_SNAKE_CASE = last_checkpoint __SCREAMING_SNAKE_CASE = trainer.train(resume_from_checkpoint=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = train_result.metrics __SCREAMING_SNAKE_CASE = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(__UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = min(__UpperCAmelCase , len(__UpperCAmelCase ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics("""train""" , __UpperCAmelCase ) trainer.save_metrics("""train""" , __UpperCAmelCase ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info("""*** Evaluate ***""" ) __SCREAMING_SNAKE_CASE = trainer.evaluate(eval_dataset=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = min(__UpperCAmelCase , len(__UpperCAmelCase ) ) trainer.log_metrics("""eval""" , __UpperCAmelCase ) trainer.save_metrics("""eval""" , __UpperCAmelCase ) if training_args.do_predict: logger.info("""*** Predict ***""" ) # Removing the `label` columns because it contains -1 and Trainer won't like that. __SCREAMING_SNAKE_CASE = predict_dataset.remove_columns("""label""" ) __SCREAMING_SNAKE_CASE = trainer.predict(__UpperCAmelCase , metric_key_prefix="""predict""" ).predictions __SCREAMING_SNAKE_CASE = np.argmax(__UpperCAmelCase , axis=1 ) __SCREAMING_SNAKE_CASE = os.path.join(training_args.output_dir , """predict_results_tabfact.txt""" ) if trainer.is_world_process_zero(): with open(__UpperCAmelCase , """w""" ) as writer: logger.info("""***** Predict Results *****""" ) writer.write("""index\tprediction\n""" ) for index, item in enumerate(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = label_list[item] writer.write(f"""{index}\t{item}\n""" ) __SCREAMING_SNAKE_CASE = {"""finetuned_from""": model_args.model_name_or_path, """tasks""": """text-classification"""} if training_args.push_to_hub: trainer.push_to_hub(**__UpperCAmelCase ) else: trainer.create_model_card(**__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase ) -> Any: '''simple docstring''' main() if __name__ == "__main__": main()
13
0
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> bool: '''simple docstring''' __SCREAMING_SNAKE_CASE = len(__lowerCAmelCase ) + 1 __SCREAMING_SNAKE_CASE = len(__lowerCAmelCase ) + 1 # dp is a 2d matrix where dp[i][j] denotes whether prefix string of # length i of input_string matches with prefix string of length j of # given pattern. # "dp" stands for dynamic programming. __SCREAMING_SNAKE_CASE = [[0 for i in range(__lowerCAmelCase )] for j in range(__lowerCAmelCase )] # since string of zero length match pattern of zero length __SCREAMING_SNAKE_CASE = 1 # since pattern of zero length will never match with string of non-zero length for i in range(1 , __lowerCAmelCase ): __SCREAMING_SNAKE_CASE = 0 # since string of zero length will match with pattern where there # is at least one * alternatively for j in range(1 , __lowerCAmelCase ): __SCREAMING_SNAKE_CASE = dp[0][j - 2] if pattern[j - 1] == """*""" else 0 # now using bottom-up approach to find for all remaining lengths for i in range(1 , __lowerCAmelCase ): for j in range(1 , __lowerCAmelCase ): if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".": __SCREAMING_SNAKE_CASE = dp[i - 1][j - 1] elif pattern[j - 1] == "*": if dp[i][j - 2] == 1: __SCREAMING_SNAKE_CASE = 1 elif pattern[j - 2] in (input_string[i - 1], "."): __SCREAMING_SNAKE_CASE = dp[i - 1][j] else: __SCREAMING_SNAKE_CASE = 0 else: __SCREAMING_SNAKE_CASE = 0 return bool(dp[-1][-1] ) if __name__ == "__main__": import doctest doctest.testmod() # inputing the strings # input_string = input("input a string :") # pattern = input("input a pattern :") a = "aab" a = "c*a*b" # using function to check whether given string matches the given pattern if match_pattern(input_string, pattern): print(F'''{input_string} matches the given pattern {pattern}''') else: print(F'''{input_string} does not match with the given pattern {pattern}''')
712
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_flax_available, is_torch_available, is_transformers_available, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .multicontrolnet import MultiControlNetModel from .pipeline_controlnet import StableDiffusionControlNetPipeline from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline if is_transformers_available() and is_flax_available(): from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline
13
0
'''simple docstring''' import argparse import json import logging import os import sys from unittest.mock import patch from transformers.testing_utils import TestCasePlus, get_gpu_count, slow a = [ os.path.join(os.path.dirname(__file__), dirname) for dirname in [ "text-classification", "language-modeling", "summarization", "token-classification", "question-answering", ] ] sys.path.extend(SRC_DIRS) if SRC_DIRS is not None: import run_clm_flax import run_flax_glue import run_flax_ner import run_mlm_flax import run_qa import run_summarization_flax import run_ta_mlm_flax logging.basicConfig(level=logging.DEBUG) a = logging.getLogger() def __magic_name__ ( ) -> Union[str, Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument("""-f""" ) __SCREAMING_SNAKE_CASE = parser.parse_args() return args.f def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase="eval" ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = os.path.join(lowerCAmelCase__ , f"""{split}_results.json""" ) if os.path.exists(lowerCAmelCase__ ): with open(lowerCAmelCase__ , """r""" ) as f: return json.load(lowerCAmelCase__ ) raise ValueError(f"""can't find {path}""" ) a = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class __a ( _snake_case ): def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() __SCREAMING_SNAKE_CASE = f""" run_glue.py --model_name_or_path distilbert-base-uncased --output_dir {tmp_dir} --train_file ./tests/fixtures/tests_samples/MRPC/train.csv --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --learning_rate=1e-4 --eval_steps=2 --warmup_steps=2 --seed=42 --max_seq_length=128 """.split() with patch.object(_UpperCAmelCase ,"""argv""" ,_UpperCAmelCase ): run_flax_glue.main() __SCREAMING_SNAKE_CASE = get_results(_UpperCAmelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.75 ) @slow def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() __SCREAMING_SNAKE_CASE = f""" run_clm_flax.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --do_train --do_eval --block_size 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --num_train_epochs 2 --logging_steps 2 --eval_steps 2 --output_dir {tmp_dir} --overwrite_output_dir """.split() with patch.object(_UpperCAmelCase ,"""argv""" ,_UpperCAmelCase ): run_clm_flax.main() __SCREAMING_SNAKE_CASE = get_results(_UpperCAmelCase ) self.assertLess(result["""eval_perplexity"""] ,100 ) @slow def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() __SCREAMING_SNAKE_CASE = f""" run_summarization.py --model_name_or_path t5-small --train_file tests/fixtures/tests_samples/xsum/sample.json --validation_file tests/fixtures/tests_samples/xsum/sample.json --test_file tests/fixtures/tests_samples/xsum/sample.json --output_dir {tmp_dir} --overwrite_output_dir --num_train_epochs=3 --warmup_steps=8 --do_train --do_eval --do_predict --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --predict_with_generate """.split() with patch.object(_UpperCAmelCase ,"""argv""" ,_UpperCAmelCase ): run_summarization_flax.main() __SCREAMING_SNAKE_CASE = get_results(_UpperCAmelCase ,split="""test""" ) self.assertGreaterEqual(result["""test_rouge1"""] ,10 ) self.assertGreaterEqual(result["""test_rouge2"""] ,2 ) self.assertGreaterEqual(result["""test_rougeL"""] ,7 ) self.assertGreaterEqual(result["""test_rougeLsum"""] ,7 ) @slow def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() __SCREAMING_SNAKE_CASE = f""" run_mlm.py --model_name_or_path distilroberta-base --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --output_dir {tmp_dir} --overwrite_output_dir --max_seq_length 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --logging_steps 2 --eval_steps 2 --do_train --do_eval --num_train_epochs=1 """.split() with patch.object(_UpperCAmelCase ,"""argv""" ,_UpperCAmelCase ): run_mlm_flax.main() __SCREAMING_SNAKE_CASE = get_results(_UpperCAmelCase ) self.assertLess(result["""eval_perplexity"""] ,42 ) @slow def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() __SCREAMING_SNAKE_CASE = f""" run_t5_mlm_flax.py --model_name_or_path t5-small --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --do_train --do_eval --max_seq_length 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --num_train_epochs 2 --logging_steps 2 --eval_steps 2 --output_dir {tmp_dir} --overwrite_output_dir """.split() with patch.object(_UpperCAmelCase ,"""argv""" ,_UpperCAmelCase ): run_ta_mlm_flax.main() __SCREAMING_SNAKE_CASE = get_results(_UpperCAmelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.42 ) @slow def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = 7 if get_gpu_count() > 1 else 2 __SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() __SCREAMING_SNAKE_CASE = f""" run_flax_ner.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/conll/sample.json --validation_file tests/fixtures/tests_samples/conll/sample.json --output_dir {tmp_dir} --overwrite_output_dir --do_train --do_eval --warmup_steps=2 --learning_rate=2e-4 --logging_steps 2 --eval_steps 2 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 """.split() with patch.object(_UpperCAmelCase ,"""argv""" ,_UpperCAmelCase ): run_flax_ner.main() __SCREAMING_SNAKE_CASE = get_results(_UpperCAmelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.75 ) self.assertGreaterEqual(result["""eval_f1"""] ,0.3 ) @slow def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() __SCREAMING_SNAKE_CASE = f""" run_qa.py --model_name_or_path bert-base-uncased --version_2_with_negative --train_file tests/fixtures/tests_samples/SQUAD/sample.json --validation_file tests/fixtures/tests_samples/SQUAD/sample.json --output_dir {tmp_dir} --overwrite_output_dir --num_train_epochs=3 --warmup_steps=2 --do_train --do_eval --logging_steps 2 --eval_steps 2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 """.split() with patch.object(_UpperCAmelCase ,"""argv""" ,_UpperCAmelCase ): run_qa.main() __SCREAMING_SNAKE_CASE = get_results(_UpperCAmelCase ) self.assertGreaterEqual(result["""eval_f1"""] ,30 ) self.assertGreaterEqual(result["""eval_exact"""] ,30 )
713
'''simple docstring''' import requests from bsa import BeautifulSoup def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = BeautifulSoup(requests.get(__UpperCAmelCase , params=__UpperCAmelCase ).content , """html.parser""" ) __SCREAMING_SNAKE_CASE = soup.find("""div""" , attrs={"""class""": """gs_ri"""} ) __SCREAMING_SNAKE_CASE = div.find("""div""" , attrs={"""class""": """gs_fl"""} ).find_all("""a""" ) return anchors[2].get_text() if __name__ == "__main__": a = { "title": ( "Precisely geometry controlled microsupercapacitors for ultrahigh areal " "capacitance, volumetric capacitance, and energy density" ), "journal": "Chem. Mater.", "volume": 30, "pages": "3979-3990", "year": 2018, "hl": "en", } print(get_citation("https://scholar.google.com/scholar_lookup", params=params))
13
0
from maths.is_square_free import is_square_free from maths.prime_factors import prime_factors def __magic_name__ ( __UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = prime_factors(__A ) if is_square_free(__A ): return -1 if len(__A ) % 2 else 1 return 0 if __name__ == "__main__": import doctest doctest.testmod()
714
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a = logging.get_logger(__name__) a = { "camembert-base": "https://huggingface.co/camembert-base/resolve/main/config.json", "umberto-commoncrawl-cased-v1": ( "https://huggingface.co/Musixmatch/umberto-commoncrawl-cased-v1/resolve/main/config.json" ), "umberto-wikipedia-uncased-v1": ( "https://huggingface.co/Musixmatch/umberto-wikipedia-uncased-v1/resolve/main/config.json" ), } class __a ( _snake_case ): __UpperCamelCase : Tuple = 'camembert' def __init__( self : int ,lowerCamelCase : List[Any]=3_0522 ,lowerCamelCase : List[Any]=768 ,lowerCamelCase : str=12 ,lowerCamelCase : List[str]=12 ,lowerCamelCase : Optional[Any]=3072 ,lowerCamelCase : Tuple="gelu" ,lowerCamelCase : List[str]=0.1 ,lowerCamelCase : Tuple=0.1 ,lowerCamelCase : Union[str, Any]=512 ,lowerCamelCase : Dict=2 ,lowerCamelCase : Tuple=0.02 ,lowerCamelCase : List[Any]=1E-1_2 ,lowerCamelCase : Union[str, Any]=1 ,lowerCamelCase : Optional[Any]=0 ,lowerCamelCase : List[Any]=2 ,lowerCamelCase : List[str]="absolute" ,lowerCamelCase : int=True ,lowerCamelCase : Any=None ,**lowerCamelCase : Optional[Any] ,): '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase ,bos_token_id=lowerCamelCase ,eos_token_id=lowerCamelCase ,**lowerCamelCase ) __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 = position_embedding_type __SCREAMING_SNAKE_CASE = use_cache __SCREAMING_SNAKE_CASE = classifier_dropout class __a ( _snake_case ): @property def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' 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), ] )
13
0
import unittest from transformers import ( MODEL_FOR_OBJECT_DETECTION_MAPPING, AutoFeatureExtractor, AutoModelForObjectDetection, ObjectDetectionPipeline, is_vision_available, pipeline, ) from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_pytesseract, require_tf, require_timm, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __a : @staticmethod def UpperCAmelCase__ ( *lowerCamelCase : int ,**lowerCamelCase : List[str] ): '''simple docstring''' pass @is_pipeline_test @require_vision @require_timm @require_torch class __a ( unittest.TestCase ): __UpperCamelCase : Tuple = MODEL_FOR_OBJECT_DETECTION_MAPPING def UpperCAmelCase__ ( self : Optional[int] ,lowerCamelCase : Optional[Any] ,lowerCamelCase : Optional[Any] ,lowerCamelCase : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = ObjectDetectionPipeline(model=_A ,image_processor=_A ) return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"] def UpperCAmelCase__ ( self : List[str] ,lowerCamelCase : List[Any] ,lowerCamelCase : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = object_detector("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ,threshold=0.0 ) self.assertGreater(len(_A ) ,0 ) for detected_object in outputs: self.assertEqual( _A ,{ """score""": ANY(_A ), """label""": ANY(_A ), """box""": {"""xmin""": ANY(_A ), """ymin""": ANY(_A ), """xmax""": ANY(_A ), """ymax""": ANY(_A )}, } ,) import datasets __SCREAMING_SNAKE_CASE = datasets.load_dataset("""hf-internal-testing/fixtures_image_utils""" ,"""image""" ,split="""test""" ) __SCREAMING_SNAKE_CASE = [ Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ), 'http://images.cocodataset.org/val2017/000000039769.jpg', # RGBA dataset[0]['file'], # LA dataset[1]['file'], # L dataset[2]['file'], ] __SCREAMING_SNAKE_CASE = object_detector(_A ,threshold=0.0 ) self.assertEqual(len(_A ) ,len(_A ) ) for outputs in batch_outputs: self.assertGreater(len(_A ) ,0 ) for detected_object in outputs: self.assertEqual( _A ,{ """score""": ANY(_A ), """label""": ANY(_A ), """box""": {"""xmin""": ANY(_A ), """ymin""": ANY(_A ), """xmax""": ANY(_A ), """ymax""": ANY(_A )}, } ,) @require_tf @unittest.skip("""Object detection not implemented in TF""" ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' pass @require_torch def UpperCAmelCase__ ( self : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = 'hf-internal-testing/tiny-detr-mobilenetsv3' __SCREAMING_SNAKE_CASE = AutoModelForObjectDetection.from_pretrained(_A ) __SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained(_A ) __SCREAMING_SNAKE_CASE = ObjectDetectionPipeline(model=_A ,feature_extractor=_A ) __SCREAMING_SNAKE_CASE = object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg""" ,threshold=0.0 ) self.assertEqual( nested_simplify(_A ,decimals=4 ) ,[ {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}}, {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}}, ] ,) __SCREAMING_SNAKE_CASE = object_detector( [ """http://images.cocodataset.org/val2017/000000039769.jpg""", """http://images.cocodataset.org/val2017/000000039769.jpg""", ] ,threshold=0.0 ,) self.assertEqual( nested_simplify(_A ,decimals=4 ) ,[ [ {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}}, {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}}, ], [ {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}}, {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}}, ], ] ,) @require_torch @slow def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = 'facebook/detr-resnet-50' __SCREAMING_SNAKE_CASE = AutoModelForObjectDetection.from_pretrained(_A ) __SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained(_A ) __SCREAMING_SNAKE_CASE = ObjectDetectionPipeline(model=_A ,feature_extractor=_A ) __SCREAMING_SNAKE_CASE = object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg""" ) self.assertEqual( nested_simplify(_A ,decimals=4 ) ,[ {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}}, {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}}, {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}}, {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}}, {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}}, ] ,) __SCREAMING_SNAKE_CASE = object_detector( [ """http://images.cocodataset.org/val2017/000000039769.jpg""", """http://images.cocodataset.org/val2017/000000039769.jpg""", ] ) self.assertEqual( nested_simplify(_A ,decimals=4 ) ,[ [ {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}}, {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}}, {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}}, {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}}, {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}}, ], [ {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}}, {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}}, {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}}, {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}}, {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}}, ], ] ,) @require_torch @slow def UpperCAmelCase__ ( self : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = 'facebook/detr-resnet-50' __SCREAMING_SNAKE_CASE = pipeline("""object-detection""" ,model=_A ) __SCREAMING_SNAKE_CASE = object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg""" ) self.assertEqual( nested_simplify(_A ,decimals=4 ) ,[ {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}}, {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}}, {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}}, {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}}, {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}}, ] ,) __SCREAMING_SNAKE_CASE = object_detector( [ """http://images.cocodataset.org/val2017/000000039769.jpg""", """http://images.cocodataset.org/val2017/000000039769.jpg""", ] ) self.assertEqual( nested_simplify(_A ,decimals=4 ) ,[ [ {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}}, {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}}, {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}}, {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}}, {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}}, ], [ {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}}, {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}}, {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}}, {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}}, {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}}, ], ] ,) @require_torch @slow def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = 0.9_985 __SCREAMING_SNAKE_CASE = 'facebook/detr-resnet-50' __SCREAMING_SNAKE_CASE = pipeline("""object-detection""" ,model=_A ) __SCREAMING_SNAKE_CASE = object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg""" ,threshold=_A ) self.assertEqual( nested_simplify(_A ,decimals=4 ) ,[ {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}}, {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}}, ] ,) @require_torch @require_pytesseract @slow def UpperCAmelCase__ ( self : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = 'Narsil/layoutlmv3-finetuned-funsd' __SCREAMING_SNAKE_CASE = 0.9_993 __SCREAMING_SNAKE_CASE = pipeline("""object-detection""" ,model=_A ,threshold=_A ) __SCREAMING_SNAKE_CASE = object_detector( """https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png""" ) self.assertEqual( nested_simplify(_A ,decimals=4 ) ,[ {"""score""": 0.9_993, """label""": """I-ANSWER""", """box""": {"""xmin""": 294, """ymin""": 254, """xmax""": 343, """ymax""": 264}}, {"""score""": 0.9_993, """label""": """I-ANSWER""", """box""": {"""xmin""": 294, """ymin""": 254, """xmax""": 343, """ymax""": 264}}, ] ,)
715
'''simple docstring''' import inspect import unittest import numpy as np from transformers import ViTConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor if is_flax_available(): import jax from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel class __a ( unittest.TestCase ): def __init__( self : Optional[int] ,lowerCamelCase : str ,lowerCamelCase : List[str]=13 ,lowerCamelCase : Optional[Any]=30 ,lowerCamelCase : Dict=2 ,lowerCamelCase : List[Any]=3 ,lowerCamelCase : List[str]=True ,lowerCamelCase : str=True ,lowerCamelCase : Optional[int]=32 ,lowerCamelCase : Dict=5 ,lowerCamelCase : Optional[int]=4 ,lowerCamelCase : List[Any]=37 ,lowerCamelCase : Union[str, Any]="gelu" ,lowerCamelCase : List[Any]=0.1 ,lowerCamelCase : Any=0.1 ,lowerCamelCase : str=10 ,lowerCamelCase : Dict=0.02 ,): '''simple docstring''' __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 # 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 UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __SCREAMING_SNAKE_CASE = 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 ,) return config, pixel_values def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : int ,lowerCamelCase : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = FlaxViTModel(config=lowerCamelCase ) __SCREAMING_SNAKE_CASE = model(lowerCamelCase ) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) __SCREAMING_SNAKE_CASE = (self.image_size, self.image_size) __SCREAMING_SNAKE_CASE = (self.patch_size, self.patch_size) __SCREAMING_SNAKE_CASE = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, num_patches + 1, self.hidden_size) ) def UpperCAmelCase__ ( self : Union[str, Any] ,lowerCamelCase : Optional[int] ,lowerCamelCase : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.type_sequence_label_size __SCREAMING_SNAKE_CASE = FlaxViTForImageClassification(config=lowerCamelCase ) __SCREAMING_SNAKE_CASE = model(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 = FlaxViTForImageClassification(lowerCamelCase ) __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) __SCREAMING_SNAKE_CASE = model(lowerCamelCase ) def UpperCAmelCase__ ( self : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ) = config_and_inputs __SCREAMING_SNAKE_CASE = {"""pixel_values""": pixel_values} return config, inputs_dict @require_flax class __a ( _snake_case, unittest.TestCase ): __UpperCamelCase : Any = (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else () def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = FlaxViTModelTester(self ) __SCREAMING_SNAKE_CASE = ConfigTester(self ,config_class=lowerCamelCase ,has_text_modality=lowerCamelCase ,hidden_size=37 ) def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __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 UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __SCREAMING_SNAKE_CASE = self._prepare_for_class(lowerCamelCase ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = model_class(lowerCamelCase ) @jax.jit def model_jitted(lowerCamelCase : int ,**lowerCamelCase : Union[str, Any] ): return model(pixel_values=lowerCamelCase ,**lowerCamelCase ) with self.subTest("""JIT Enabled""" ): __SCREAMING_SNAKE_CASE = model_jitted(**lowerCamelCase ).to_tuple() with self.subTest("""JIT Disabled""" ): with jax.disable_jit(): __SCREAMING_SNAKE_CASE = model_jitted(**lowerCamelCase ).to_tuple() self.assertEqual(len(lowerCamelCase ) ,len(lowerCamelCase ) ) for jitted_output, output in zip(lowerCamelCase ,lowerCamelCase ): self.assertEqual(jitted_output.shape ,output.shape ) @slow def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' for model_class_name in self.all_model_classes: __SCREAMING_SNAKE_CASE = model_class_name.from_pretrained("""google/vit-base-patch16-224""" ) __SCREAMING_SNAKE_CASE = model(np.ones((1, 3, 224, 224) ) ) self.assertIsNotNone(lowerCamelCase )
13
0
'''simple docstring''' from __future__ import annotations from random import choice def __magic_name__ ( __UpperCAmelCase ) -> Optional[int]: '''simple docstring''' return choice(_lowerCamelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Any: '''simple docstring''' __SCREAMING_SNAKE_CASE : List[str] = random_pivot(_lowerCamelCase ) # partition based on pivot # linear time __SCREAMING_SNAKE_CASE : Union[str, Any] = [e for e in lst if e < pivot] __SCREAMING_SNAKE_CASE : int = [e for e in lst if e > pivot] # if we get lucky, pivot might be the element we want. # we can easily see this: # small (elements smaller than k) # + pivot (kth element) # + big (elements larger than k) if len(_lowerCamelCase ) == k - 1: return pivot # pivot is in elements bigger than k elif len(_lowerCamelCase ) < k - 1: return kth_number(_lowerCamelCase , k - len(_lowerCamelCase ) - 1 ) # pivot is in elements smaller than k else: return kth_number(_lowerCamelCase , _lowerCamelCase ) if __name__ == "__main__": import doctest doctest.testmod()
716
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging a = logging.get_logger(__name__) a = { "asapp/sew-tiny-100k": "https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json", # See all SEW models at https://huggingface.co/models?filter=sew } class __a ( _snake_case ): __UpperCamelCase : Tuple = 'sew' def __init__( self : str ,lowerCamelCase : Any=32 ,lowerCamelCase : str=768 ,lowerCamelCase : str=12 ,lowerCamelCase : Union[str, Any]=12 ,lowerCamelCase : Union[str, Any]=3072 ,lowerCamelCase : int=2 ,lowerCamelCase : Union[str, Any]="gelu" ,lowerCamelCase : Tuple=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : Any=0.0 ,lowerCamelCase : Optional[Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : Optional[Any]=0.02 ,lowerCamelCase : List[str]=1E-5 ,lowerCamelCase : Tuple="group" ,lowerCamelCase : Optional[Any]="gelu" ,lowerCamelCase : List[str]=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) ,lowerCamelCase : Any=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) ,lowerCamelCase : Dict=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) ,lowerCamelCase : Optional[int]=False ,lowerCamelCase : Dict=128 ,lowerCamelCase : Union[str, Any]=16 ,lowerCamelCase : List[Any]=True ,lowerCamelCase : List[Any]=0.05 ,lowerCamelCase : Optional[int]=10 ,lowerCamelCase : Any=2 ,lowerCamelCase : Any=0.0 ,lowerCamelCase : Tuple=10 ,lowerCamelCase : str=0 ,lowerCamelCase : Tuple="mean" ,lowerCamelCase : int=False ,lowerCamelCase : Dict=False ,lowerCamelCase : Optional[int]=256 ,lowerCamelCase : str=0 ,lowerCamelCase : Tuple=1 ,lowerCamelCase : Tuple=2 ,**lowerCamelCase : Union[str, Any] ,): '''simple docstring''' super().__init__(**lowerCamelCase ,pad_token_id=lowerCamelCase ,bos_token_id=lowerCamelCase ,eos_token_id=lowerCamelCase ) __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = feat_extract_norm __SCREAMING_SNAKE_CASE = feat_extract_activation __SCREAMING_SNAKE_CASE = list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = conv_bias __SCREAMING_SNAKE_CASE = num_conv_pos_embeddings __SCREAMING_SNAKE_CASE = num_conv_pos_embedding_groups __SCREAMING_SNAKE_CASE = len(self.conv_dim ) __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = squeeze_factor __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = hidden_dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = feat_proj_dropout __SCREAMING_SNAKE_CASE = final_dropout __SCREAMING_SNAKE_CASE = layerdrop __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect.""" """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,""" f"""but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)""" f"""= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __SCREAMING_SNAKE_CASE = apply_spec_augment __SCREAMING_SNAKE_CASE = mask_time_prob __SCREAMING_SNAKE_CASE = mask_time_length __SCREAMING_SNAKE_CASE = mask_time_min_masks __SCREAMING_SNAKE_CASE = mask_feature_prob __SCREAMING_SNAKE_CASE = mask_feature_length __SCREAMING_SNAKE_CASE = mask_feature_min_masks # ctc loss __SCREAMING_SNAKE_CASE = ctc_loss_reduction __SCREAMING_SNAKE_CASE = ctc_zero_infinity # sequence classification __SCREAMING_SNAKE_CASE = use_weighted_layer_sum __SCREAMING_SNAKE_CASE = classifier_proj_size @property def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' return functools.reduce(operator.mul ,self.conv_stride ,1 )
13
0
'''simple docstring''' from __future__ import annotations from bisect import bisect_left from functools import total_ordering from heapq import merge @total_ordering class __a ( _UpperCAmelCase ): def __lt__( self : Optional[int] ,lowerCamelCase : List[str] ): '''simple docstring''' return self[-1] < other[-1] def __eq__( self : List[Any] ,lowerCamelCase : int ): '''simple docstring''' return self[-1] == other[-1] def __magic_name__ ( __UpperCAmelCase ) -> list: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] # sort into stacks for element in collection: __SCREAMING_SNAKE_CASE = Stack([element] ) __SCREAMING_SNAKE_CASE = bisect_left(a_ , a_ ) if i != len(a_ ): stacks[i].append(a_ ) else: stacks.append(a_ ) # use a heap-based merge to merge stack efficiently __SCREAMING_SNAKE_CASE = merge(*(reversed(a_ ) for stack in stacks) ) return collection if __name__ == "__main__": a = input("Enter numbers separated by a comma:\n").strip() a = [int(item) for item in user_input.split(",")] print(patience_sort(unsorted))
717
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase = 1 , __UpperCAmelCase = 1000 ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 0 for divide_by_number in range(__UpperCAmelCase , digit + 1 ): __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = numerator for _ in range(1 , digit + 1 ): if now_divide in has_been_divided: if longest_list_length < len(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = divide_by_number else: has_been_divided.append(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = now_divide * 10 % divide_by_number return the_digit # Tests if __name__ == "__main__": import doctest doctest.testmod()
13
0
'''simple docstring''' import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __a ( _UpperCAmelCase, _UpperCAmelCase, _UpperCAmelCase, unittest.TestCase ): __UpperCamelCase : Dict = StableDiffusionInpaintPipeline __UpperCamelCase : Any = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS __UpperCamelCase : List[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __UpperCamelCase : Optional[Any] = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess __UpperCamelCase : List[str] = frozenset([] ) def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = UNetaDConditionModel( block_out_channels=(32, 64) ,layers_per_block=2 ,sample_size=32 ,in_channels=9 ,out_channels=4 ,down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") ,up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") ,cross_attention_dim=32 ,attention_head_dim=(2, 4) ,use_linear_projection=__UpperCamelCase ,) __SCREAMING_SNAKE_CASE = PNDMScheduler(skip_prk_steps=__UpperCamelCase ) 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 ,sample_size=128 ,) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = CLIPTextConfig( bos_token_id=0 ,eos_token_id=2 ,hidden_size=32 ,intermediate_size=37 ,layer_norm_eps=1E-0_5 ,num_attention_heads=4 ,num_hidden_layers=5 ,pad_token_id=1 ,vocab_size=1000 ,hidden_act="""gelu""" ,projection_dim=512 ,) __SCREAMING_SNAKE_CASE = CLIPTextModel(__UpperCamelCase ) __SCREAMING_SNAKE_CASE = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) __SCREAMING_SNAKE_CASE = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : List[Any] ,lowerCamelCase : Optional[int]=0 ): '''simple docstring''' __SCREAMING_SNAKE_CASE = floats_tensor((1, 3, 32, 32) ,rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) __SCREAMING_SNAKE_CASE = image.cpu().permute(0 ,2 ,3 ,1 )[0] __SCREAMING_SNAKE_CASE = Image.fromarray(np.uinta(__UpperCamelCase ) ).convert("""RGB""" ).resize((64, 64) ) __SCREAMING_SNAKE_CASE = Image.fromarray(np.uinta(image + 4 ) ).convert("""RGB""" ).resize((64, 64) ) if str(__UpperCamelCase ).startswith("""mps""" ): __SCREAMING_SNAKE_CASE = torch.manual_seed(__UpperCamelCase ) else: __SCREAMING_SNAKE_CASE = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) __SCREAMING_SNAKE_CASE = { """prompt""": """A painting of a squirrel eating a burger""", """image""": init_image, """mask_image""": mask_image, """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 6.0, """output_type""": """numpy""", } return inputs def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = """cpu""" # ensure determinism for the device-dependent torch.Generator __SCREAMING_SNAKE_CASE = self.get_dummy_components() __SCREAMING_SNAKE_CASE = StableDiffusionInpaintPipeline(**__UpperCamelCase ) __SCREAMING_SNAKE_CASE = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) __SCREAMING_SNAKE_CASE = self.get_dummy_inputs(__UpperCamelCase ) __SCREAMING_SNAKE_CASE = sd_pipe(**__UpperCamelCase ).images __SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __SCREAMING_SNAKE_CASE = np.array([0.4_727, 0.5_735, 0.3_941, 0.5_446, 0.5_926, 0.4_394, 0.5_062, 0.4_654, 0.4_476] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCAmelCase__ ( self : str ): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __a ( unittest.TestCase ): def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) __SCREAMING_SNAKE_CASE = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) __SCREAMING_SNAKE_CASE = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint""" """/yellow_cat_sitting_on_a_park_bench.npy""" ) __SCREAMING_SNAKE_CASE = """stabilityai/stable-diffusion-2-inpainting""" __SCREAMING_SNAKE_CASE = StableDiffusionInpaintPipeline.from_pretrained(__UpperCamelCase ,safety_checker=__UpperCamelCase ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() __SCREAMING_SNAKE_CASE = """Face of a yellow cat, high resolution, sitting on a park bench""" __SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = pipe( prompt=__UpperCamelCase ,image=__UpperCamelCase ,mask_image=__UpperCamelCase ,generator=__UpperCamelCase ,output_type="""np""" ,) __SCREAMING_SNAKE_CASE = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 9E-3 def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) __SCREAMING_SNAKE_CASE = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) __SCREAMING_SNAKE_CASE = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint""" """/yellow_cat_sitting_on_a_park_bench_fp16.npy""" ) __SCREAMING_SNAKE_CASE = """stabilityai/stable-diffusion-2-inpainting""" __SCREAMING_SNAKE_CASE = StableDiffusionInpaintPipeline.from_pretrained( __UpperCamelCase ,torch_dtype=torch.floataa ,safety_checker=__UpperCamelCase ,) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() __SCREAMING_SNAKE_CASE = """Face of a yellow cat, high resolution, sitting on a park bench""" __SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = pipe( prompt=__UpperCamelCase ,image=__UpperCamelCase ,mask_image=__UpperCamelCase ,generator=__UpperCamelCase ,output_type="""np""" ,) __SCREAMING_SNAKE_CASE = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 5E-1 def UpperCAmelCase__ ( self : str ): '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __SCREAMING_SNAKE_CASE = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) __SCREAMING_SNAKE_CASE = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) __SCREAMING_SNAKE_CASE = """stabilityai/stable-diffusion-2-inpainting""" __SCREAMING_SNAKE_CASE = PNDMScheduler.from_pretrained(__UpperCamelCase ,subfolder="""scheduler""" ) __SCREAMING_SNAKE_CASE = StableDiffusionInpaintPipeline.from_pretrained( __UpperCamelCase ,safety_checker=__UpperCamelCase ,scheduler=__UpperCamelCase ,torch_dtype=torch.floataa ,) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() __SCREAMING_SNAKE_CASE = """Face of a yellow cat, high resolution, sitting on a park bench""" __SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = pipe( prompt=__UpperCamelCase ,image=__UpperCamelCase ,mask_image=__UpperCamelCase ,generator=__UpperCamelCase ,num_inference_steps=2 ,output_type="""np""" ,) __SCREAMING_SNAKE_CASE = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 10**9
718
'''simple docstring''' 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 if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor class __a ( unittest.TestCase ): def __init__( self : List[Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : List[str]=7 ,lowerCamelCase : List[str]=3 ,lowerCamelCase : List[str]=18 ,lowerCamelCase : Any=30 ,lowerCamelCase : Optional[Any]=400 ,lowerCamelCase : Optional[Any]=True ,lowerCamelCase : Optional[Any]=None ,lowerCamelCase : Optional[int]=True ,lowerCamelCase : int=None ,lowerCamelCase : str=True ,lowerCamelCase : Dict=[0.48_145_466, 0.4_578_275, 0.40_821_073] ,lowerCamelCase : List[str]=[0.26_862_954, 0.26_130_258, 0.27_577_711] ,lowerCamelCase : Tuple=True ,): '''simple docstring''' __SCREAMING_SNAKE_CASE = size if size is not None else {"""height""": 224, """width""": 224} __SCREAMING_SNAKE_CASE = crop_size if crop_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 = do_center_crop __SCREAMING_SNAKE_CASE = crop_size __SCREAMING_SNAKE_CASE = do_normalize __SCREAMING_SNAKE_CASE = image_mean __SCREAMING_SNAKE_CASE = image_std __SCREAMING_SNAKE_CASE = do_convert_rgb def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_convert_rgb": self.do_convert_rgb, } def UpperCAmelCase__ ( self : int ,lowerCamelCase : Union[str, Any]=False ,lowerCamelCase : str=False ,lowerCamelCase : str=False ): '''simple docstring''' assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time" if equal_resolution: __SCREAMING_SNAKE_CASE = [] for i in range(self.batch_size ): image_inputs.append( np.random.randint( 255 ,size=(self.num_channels, self.max_resolution, self.max_resolution) ,dtype=np.uinta ) ) else: __SCREAMING_SNAKE_CASE = [] for i in range(self.batch_size ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = np.random.choice(np.arange(self.min_resolution ,self.max_resolution ) ,2 ) image_inputs.append(np.random.randint(255 ,size=(self.num_channels, width, height) ,dtype=np.uinta ) ) if not numpify and not torchify: # PIL expects the channel dimension as last dimension __SCREAMING_SNAKE_CASE = [Image.fromarray(np.moveaxis(lowerCamelCase ,0 ,-1 ) ) for x in image_inputs] if torchify: __SCREAMING_SNAKE_CASE = [torch.from_numpy(lowerCamelCase ) for x in image_inputs] return image_inputs @require_torch @require_vision class __a ( _snake_case, unittest.TestCase ): __UpperCamelCase : int = ChineseCLIPImageProcessor if is_vision_available() else None def UpperCAmelCase__ ( self : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = ChineseCLIPImageProcessingTester(self ,do_center_crop=lowerCamelCase ) @property def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __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 ,"""do_center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_normalize""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_mean""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_std""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_convert_rgb""" ) ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{"""height""": 224, """width""": 224} ) self.assertEqual(image_processor.crop_size ,{"""height""": 18, """width""": 18} ) __SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ,crop_size=84 ) self.assertEqual(image_processor.size ,{"""shortest_edge""": 42} ) self.assertEqual(image_processor.crop_size ,{"""height""": 84, """width""": 84} ) def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' pass def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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""" ).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 __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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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.crop_size["""height"""], self.image_processor_tester.crop_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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def UpperCAmelCase__ ( self : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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.crop_size["""height"""], self.image_processor_tester.crop_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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) @require_torch @require_vision class __a ( _snake_case, unittest.TestCase ): __UpperCamelCase : Optional[int] = ChineseCLIPImageProcessor if is_vision_available() else None def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = ChineseCLIPImageProcessingTester(self ,num_channels=4 ,do_center_crop=lowerCamelCase ) __SCREAMING_SNAKE_CASE = 3 @property def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase__ ( self : int ): '''simple docstring''' __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 ,"""do_center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_normalize""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_mean""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_std""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_convert_rgb""" ) ) def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' pass def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_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.expected_encoded_image_num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,)
13
0
'''simple docstring''' import argparse import json from pathlib import Path import torch import torchaudio from datasets import load_dataset from huggingface_hub import hf_hub_download from transformers import ASTConfig, ASTFeatureExtractor, ASTForAudioClassification from transformers.utils import logging logging.set_verbosity_info() a = logging.get_logger(__name__) def __magic_name__ ( __UpperCAmelCase ) -> Any: '''simple docstring''' __SCREAMING_SNAKE_CASE = ASTConfig() if "10-10" in model_name: pass elif "speech-commands" in model_name: __SCREAMING_SNAKE_CASE = 128 elif "12-12" in model_name: __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 12 elif "14-14" in model_name: __SCREAMING_SNAKE_CASE = 14 __SCREAMING_SNAKE_CASE = 14 elif "16-16" in model_name: __SCREAMING_SNAKE_CASE = 16 __SCREAMING_SNAKE_CASE = 16 else: raise ValueError("""Model not supported""" ) __SCREAMING_SNAKE_CASE = """huggingface/label-files""" if "speech-commands" in model_name: __SCREAMING_SNAKE_CASE = 35 __SCREAMING_SNAKE_CASE = """speech-commands-v2-id2label.json""" else: __SCREAMING_SNAKE_CASE = 527 __SCREAMING_SNAKE_CASE = """audioset-id2label.json""" __SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(__UpperCAmelCase , __UpperCAmelCase , repo_type="""dataset""" ) , """r""" ) ) __SCREAMING_SNAKE_CASE = {int(__UpperCAmelCase ): v for k, v in idalabel.items()} __SCREAMING_SNAKE_CASE = idalabel __SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} return config def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' if "module.v" in name: __SCREAMING_SNAKE_CASE = name.replace("""module.v""" , """audio_spectrogram_transformer""" ) if "cls_token" in name: __SCREAMING_SNAKE_CASE = name.replace("""cls_token""" , """embeddings.cls_token""" ) if "dist_token" in name: __SCREAMING_SNAKE_CASE = name.replace("""dist_token""" , """embeddings.distillation_token""" ) if "pos_embed" in name: __SCREAMING_SNAKE_CASE = name.replace("""pos_embed""" , """embeddings.position_embeddings""" ) if "patch_embed.proj" in name: __SCREAMING_SNAKE_CASE = name.replace("""patch_embed.proj""" , """embeddings.patch_embeddings.projection""" ) # transformer blocks if "blocks" in name: __SCREAMING_SNAKE_CASE = name.replace("""blocks""" , """encoder.layer""" ) if "attn.proj" in name: __SCREAMING_SNAKE_CASE = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name: __SCREAMING_SNAKE_CASE = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: __SCREAMING_SNAKE_CASE = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: __SCREAMING_SNAKE_CASE = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: __SCREAMING_SNAKE_CASE = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: __SCREAMING_SNAKE_CASE = name.replace("""mlp.fc2""" , """output.dense""" ) # final layernorm if "audio_spectrogram_transformer.norm" in name: __SCREAMING_SNAKE_CASE = name.replace("""audio_spectrogram_transformer.norm""" , """audio_spectrogram_transformer.layernorm""" ) # classifier head if "module.mlp_head.0" in name: __SCREAMING_SNAKE_CASE = name.replace("""module.mlp_head.0""" , """classifier.layernorm""" ) if "module.mlp_head.1" in name: __SCREAMING_SNAKE_CASE = name.replace("""module.mlp_head.1""" , """classifier.dense""" ) return name def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' for key in orig_state_dict.copy().keys(): __SCREAMING_SNAKE_CASE = orig_state_dict.pop(__UpperCAmelCase ) if "qkv" in key: __SCREAMING_SNAKE_CASE = key.split(""".""" ) __SCREAMING_SNAKE_CASE = int(key_split[3] ) __SCREAMING_SNAKE_CASE = config.hidden_size if "weight" in key: __SCREAMING_SNAKE_CASE = val[:dim, :] __SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] __SCREAMING_SNAKE_CASE = val[-dim:, :] else: __SCREAMING_SNAKE_CASE = val[:dim] __SCREAMING_SNAKE_CASE = val[dim : dim * 2] __SCREAMING_SNAKE_CASE = val[-dim:] else: __SCREAMING_SNAKE_CASE = val return orig_state_dict def __magic_name__ ( __UpperCAmelCase ) -> List[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = [ """module.v.head.weight""", """module.v.head.bias""", """module.v.head_dist.weight""", """module.v.head_dist.bias""", ] for k in ignore_keys: state_dict.pop(__UpperCAmelCase , __UpperCAmelCase ) @torch.no_grad() def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=False ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = get_audio_spectrogram_transformer_config(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = { """ast-finetuned-audioset-10-10-0.4593""": ( """https://www.dropbox.com/s/ca0b1v2nlxzyeb4/audioset_10_10_0.4593.pth?dl=1""" ), """ast-finetuned-audioset-10-10-0.450""": ( """https://www.dropbox.com/s/1tv0hovue1bxupk/audioset_10_10_0.4495.pth?dl=1""" ), """ast-finetuned-audioset-10-10-0.448""": ( """https://www.dropbox.com/s/6u5sikl4b9wo4u5/audioset_10_10_0.4483.pth?dl=1""" ), """ast-finetuned-audioset-10-10-0.448-v2""": ( """https://www.dropbox.com/s/kt6i0v9fvfm1mbq/audioset_10_10_0.4475.pth?dl=1""" ), """ast-finetuned-audioset-12-12-0.447""": ( """https://www.dropbox.com/s/snfhx3tizr4nuc8/audioset_12_12_0.4467.pth?dl=1""" ), """ast-finetuned-audioset-14-14-0.443""": ( """https://www.dropbox.com/s/z18s6pemtnxm4k7/audioset_14_14_0.4431.pth?dl=1""" ), """ast-finetuned-audioset-16-16-0.442""": ( """https://www.dropbox.com/s/mdsa4t1xmcimia6/audioset_16_16_0.4422.pth?dl=1""" ), """ast-finetuned-speech-commands-v2""": ( """https://www.dropbox.com/s/q0tbqpwv44pquwy/speechcommands_10_10_0.9812.pth?dl=1""" ), } # load original state_dict __SCREAMING_SNAKE_CASE = model_name_to_url[model_name] __SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(__UpperCAmelCase , map_location="""cpu""" ) # remove some keys remove_keys(__UpperCAmelCase ) # rename some keys __SCREAMING_SNAKE_CASE = convert_state_dict(__UpperCAmelCase , __UpperCAmelCase ) # load 🤗 model __SCREAMING_SNAKE_CASE = ASTForAudioClassification(__UpperCAmelCase ) model.eval() model.load_state_dict(__UpperCAmelCase ) # verify outputs on dummy input # source: https://github.com/YuanGongND/ast/blob/79e873b8a54d0a3b330dd522584ff2b9926cd581/src/run.py#L62 __SCREAMING_SNAKE_CASE = -4.2_6_7_7_3_9_3 if """speech-commands""" not in model_name else -6.8_4_5_9_7_8 __SCREAMING_SNAKE_CASE = 4.5_6_8_9_9_7_4 if """speech-commands""" not in model_name else 5.5_6_5_4_5_2_6 __SCREAMING_SNAKE_CASE = 1024 if """speech-commands""" not in model_name else 128 __SCREAMING_SNAKE_CASE = ASTFeatureExtractor(mean=__UpperCAmelCase , std=__UpperCAmelCase , max_length=__UpperCAmelCase ) if "speech-commands" in model_name: __SCREAMING_SNAKE_CASE = load_dataset("""speech_commands""" , """v0.02""" , split="""validation""" ) __SCREAMING_SNAKE_CASE = dataset[0]["""audio"""]["""array"""] else: __SCREAMING_SNAKE_CASE = hf_hub_download( repo_id="""nielsr/audio-spectogram-transformer-checkpoint""" , filename="""sample_audio.flac""" , repo_type="""dataset""" , ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = torchaudio.load(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = waveform.squeeze().numpy() __SCREAMING_SNAKE_CASE = feature_extractor(__UpperCAmelCase , sampling_rate=16000 , return_tensors="""pt""" ) # forward pass __SCREAMING_SNAKE_CASE = model(**__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = outputs.logits if model_name == "ast-finetuned-audioset-10-10-0.4593": __SCREAMING_SNAKE_CASE = torch.tensor([-0.8_7_6_0, -7.0_0_4_2, -8.6_6_0_2] ) elif model_name == "ast-finetuned-audioset-10-10-0.450": __SCREAMING_SNAKE_CASE = torch.tensor([-1.1_9_8_6, -7.0_9_0_3, -8.2_7_1_8] ) elif model_name == "ast-finetuned-audioset-10-10-0.448": __SCREAMING_SNAKE_CASE = torch.tensor([-2.6_1_2_8, -8.0_0_8_0, -9.4_3_4_4] ) elif model_name == "ast-finetuned-audioset-10-10-0.448-v2": __SCREAMING_SNAKE_CASE = torch.tensor([-1.5_0_8_0, -7.4_5_3_4, -8.8_9_1_7] ) elif model_name == "ast-finetuned-audioset-12-12-0.447": __SCREAMING_SNAKE_CASE = torch.tensor([-0.5_0_5_0, -6.5_8_3_3, -8.0_8_4_3] ) elif model_name == "ast-finetuned-audioset-14-14-0.443": __SCREAMING_SNAKE_CASE = torch.tensor([-0.3_8_2_6, -7.0_3_3_6, -8.2_4_1_3] ) elif model_name == "ast-finetuned-audioset-16-16-0.442": __SCREAMING_SNAKE_CASE = torch.tensor([-1.2_1_1_3, -6.9_1_0_1, -8.3_4_7_0] ) elif model_name == "ast-finetuned-speech-commands-v2": __SCREAMING_SNAKE_CASE = torch.tensor([6.1_5_8_9, -8.0_5_6_6, -8.7_9_8_4] ) else: raise ValueError("""Unknown model name""" ) if not torch.allclose(logits[0, :3] , __UpperCAmelCase , atol=1e-4 ): raise ValueError("""Logits don\'t match""" ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(__UpperCAmelCase ).mkdir(exist_ok=__UpperCAmelCase ) print(f"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(__UpperCAmelCase ) print(f"""Saving feature extractor to {pytorch_dump_folder_path}""" ) feature_extractor.save_pretrained(__UpperCAmelCase ) if push_to_hub: print("""Pushing model and feature extractor to the hub...""" ) model.push_to_hub(f"""MIT/{model_name}""" ) feature_extractor.push_to_hub(f"""MIT/{model_name}""" ) if __name__ == "__main__": a = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="ast-finetuned-audioset-10-10-0.4593", type=str, help="Name of the Audio Spectrogram Transformer model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) a = parser.parse_args() convert_audio_spectrogram_transformer_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
719
'''simple docstring''' import timeit import numpy as np import datasets from datasets.arrow_writer import ArrowWriter from datasets.features.features import _ArrayXD def __magic_name__ ( __UpperCAmelCase ) -> Tuple: '''simple docstring''' def wrapper(*__UpperCAmelCase , **__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = timeit.default_timer() __SCREAMING_SNAKE_CASE = func(*__UpperCAmelCase , **__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = timeit.default_timer() - starttime return delta __SCREAMING_SNAKE_CASE = func.__name__ return wrapper def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase=100 , __UpperCAmelCase=None ) -> Optional[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = seq_shapes or {} for i in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = {} for col_id, (k, v) in enumerate(features.items() ): if isinstance(__UpperCAmelCase , _ArrayXD ): __SCREAMING_SNAKE_CASE = np.random.rand(*v.shape ).astype(v.dtype ) elif isinstance(__UpperCAmelCase , datasets.Value ): if v.dtype == "string": __SCREAMING_SNAKE_CASE = """The small grey turtle was surprisingly fast when challenged.""" else: __SCREAMING_SNAKE_CASE = np.random.randint(10 , size=1 ).astype(v.dtype ).item() elif isinstance(__UpperCAmelCase , datasets.Sequence ): while isinstance(__UpperCAmelCase , datasets.Sequence ): __SCREAMING_SNAKE_CASE = v.feature __SCREAMING_SNAKE_CASE = seq_shapes[k] __SCREAMING_SNAKE_CASE = np.random.rand(*__UpperCAmelCase ).astype(v.dtype ) __SCREAMING_SNAKE_CASE = data dummy_data.append((i, example) ) return dummy_data def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=100 , __UpperCAmelCase=None ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = generate_examples(__UpperCAmelCase , num_examples=__UpperCAmelCase , seq_shapes=__UpperCAmelCase ) with ArrowWriter(features=__UpperCAmelCase , path=__UpperCAmelCase ) as writer: for key, record in dummy_data: __SCREAMING_SNAKE_CASE = features.encode_example(__UpperCAmelCase ) writer.write(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = writer.finalize() if not num_final_examples == num_examples: raise ValueError( f"""Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}.""" ) __SCREAMING_SNAKE_CASE = datasets.Dataset.from_file(filename=__UpperCAmelCase , info=datasets.DatasetInfo(features=__UpperCAmelCase ) ) return dataset
13
0
'''simple docstring''' from __future__ import annotations def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase = None , __UpperCAmelCase = None ) -> List[Any]: '''simple docstring''' if start is None: __SCREAMING_SNAKE_CASE = 0 if end is None: __SCREAMING_SNAKE_CASE = len(__UpperCamelCase ) - 1 if start >= end: return __SCREAMING_SNAKE_CASE = (start + end) // 2 slowsort(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) slowsort(__UpperCamelCase , mid + 1 , __UpperCamelCase ) if sequence[end] < sequence[mid]: __SCREAMING_SNAKE_CASE = sequence[mid], sequence[end] slowsort(__UpperCamelCase , __UpperCamelCase , end - 1 ) if __name__ == "__main__": from doctest import testmod testmod()
720
'''simple docstring''' import time from contextlib import contextmanager from pathlib import Path import pytest import requests from huggingface_hub.hf_api import HfApi, HfFolder a = "__DUMMY_TRANSFORMERS_USER__" a = "Dummy User" a = "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" a = "https://hub-ci.huggingface.co" a = CI_HUB_ENDPOINT + "/datasets/{repo_id}/resolve/{revision}/{path}" a = CI_HUB_ENDPOINT + "/{repo_id}/resolve/{revision}/{filename}" a = Path("~/.huggingface/hub_ci_token").expanduser() @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' monkeypatch.setattr( """huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE""" , __UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' monkeypatch.setattr("""datasets.config.HF_ENDPOINT""" , __UpperCAmelCase ) monkeypatch.setattr("""datasets.config.HUB_DATASETS_URL""" , __UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' monkeypatch.setattr("""huggingface_hub.hf_api.HfFolder.path_token""" , __UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' HfFolder.save_token(__UpperCAmelCase ) yield HfFolder.delete_token() @pytest.fixture(scope="""session""" ) def __magic_name__ ( ) -> Optional[Any]: '''simple docstring''' return HfApi(endpoint=__UpperCAmelCase ) @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = HfFolder.get_token() HfFolder.save_token(__UpperCAmelCase ) yield CI_HUB_USER_TOKEN if previous_token is not None: HfFolder.save_token(__UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> Dict: '''simple docstring''' def _cleanup_repo(__UpperCAmelCase ): hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) return _cleanup_repo @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' @contextmanager def _temporary_repo(__UpperCAmelCase ): try: yield repo_id finally: cleanup_repo(__UpperCAmelCase ) return _temporary_repo @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""repo_txt_data-{int(time.time() * 1_0e3 )}""" __SCREAMING_SNAKE_CASE = f"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" , private=__UpperCAmelCase ) hf_api.upload_file( token=__UpperCAmelCase , path_or_fileobj=str(__UpperCAmelCase ) , path_in_repo="""data/text_data.txt""" , repo_id=__UpperCAmelCase , repo_type="""dataset""" , ) yield repo_id try: hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' return hf_private_dataset_repo_txt_data_ @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""repo_zipped_txt_data-{int(time.time() * 1_0e3 )}""" __SCREAMING_SNAKE_CASE = f"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" , private=__UpperCAmelCase ) hf_api.upload_file( token=__UpperCAmelCase , path_or_fileobj=str(__UpperCAmelCase ) , path_in_repo="""data.zip""" , repo_id=__UpperCAmelCase , repo_type="""dataset""" , ) yield repo_id try: hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Tuple: '''simple docstring''' return hf_private_dataset_repo_zipped_txt_data_ @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""repo_zipped_img_data-{int(time.time() * 1_0e3 )}""" __SCREAMING_SNAKE_CASE = f"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" , private=__UpperCAmelCase ) hf_api.upload_file( token=__UpperCAmelCase , path_or_fileobj=str(__UpperCAmelCase ) , path_in_repo="""data.zip""" , repo_id=__UpperCAmelCase , repo_type="""dataset""" , ) yield repo_id try: hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' return hf_private_dataset_repo_zipped_img_data_
13
0
'''simple docstring''' from json import JSONDecodeError # Workaround for requests.exceptions.JSONDecodeError import requests def __magic_name__ ( __UpperCAmelCase = "isbn/0140328726" ) -> Any: '''simple docstring''' __SCREAMING_SNAKE_CASE = olid.strip().strip("""/""" ) # Remove leading/trailing whitespace & slashes if new_olid.count("""/""" ) != 1: __SCREAMING_SNAKE_CASE = f"""{olid} is not a valid Open Library olid""" raise ValueError(snake_case__ ) return requests.get(f"""https://openlibrary.org/{new_olid}.json""" ).json() def __magic_name__ ( __UpperCAmelCase ) -> Any: '''simple docstring''' __SCREAMING_SNAKE_CASE = { """title""": """Title""", """publish_date""": """Publish date""", """authors""": """Authors""", """number_of_pages""": """Number of pages:""", """first_sentence""": """First sentence""", """isbn_10""": """ISBN (10)""", """isbn_13""": """ISBN (13)""", } __SCREAMING_SNAKE_CASE = {better_key: ol_book_data[key] for key, better_key in desired_keys.items()} __SCREAMING_SNAKE_CASE = [ get_openlibrary_data(author["""key"""] )["""name"""] for author in data["""Authors"""] ] __SCREAMING_SNAKE_CASE = data["""First sentence"""]["""value"""] for key, value in data.items(): if isinstance(snake_case__ , snake_case__ ): __SCREAMING_SNAKE_CASE = """, """.join(snake_case__ ) return data if __name__ == "__main__": import doctest doctest.testmod() while True: a = input("\nEnter the ISBN code to search (or \'quit\' to stop): ").strip() if isbn.lower() in ("", "q", "quit", "exit", "stop"): break if len(isbn) not in (10, 13) or not isbn.isdigit(): print(F'''Sorry, {isbn} is not a valid ISBN. Please, input a valid ISBN.''') continue print(F'''\nSearching Open Library for ISBN: {isbn}...\n''') try: a = summarize_book(get_openlibrary_data(F'''isbn/{isbn}''')) print("\n".join(F'''{key}: {value}''' for key, value in book_summary.items())) except JSONDecodeError: # Workaround for requests.exceptions.RequestException: print(F'''Sorry, there are no results for ISBN: {isbn}.''')
721
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING a = logging.get_logger(__name__) a = { "ut/deta": "https://huggingface.co/ut/deta/resolve/main/config.json", } class __a ( _snake_case ): __UpperCamelCase : Dict = 'deta' __UpperCamelCase : List[str] = { 'hidden_size': 'd_model', 'num_attention_heads': 'encoder_attention_heads', } def __init__( self : Tuple ,lowerCamelCase : List[Any]=None ,lowerCamelCase : Any=900 ,lowerCamelCase : int=2048 ,lowerCamelCase : Any=6 ,lowerCamelCase : Optional[Any]=2048 ,lowerCamelCase : str=8 ,lowerCamelCase : Union[str, Any]=6 ,lowerCamelCase : List[str]=1024 ,lowerCamelCase : int=8 ,lowerCamelCase : Any=0.0 ,lowerCamelCase : Any=True ,lowerCamelCase : Optional[int]="relu" ,lowerCamelCase : int=256 ,lowerCamelCase : Tuple=0.1 ,lowerCamelCase : Optional[Any]=0.0 ,lowerCamelCase : Tuple=0.0 ,lowerCamelCase : List[str]=0.02 ,lowerCamelCase : Any=1.0 ,lowerCamelCase : Optional[int]=True ,lowerCamelCase : int=False ,lowerCamelCase : Optional[Any]="sine" ,lowerCamelCase : Dict=5 ,lowerCamelCase : List[Any]=4 ,lowerCamelCase : Optional[Any]=4 ,lowerCamelCase : Any=True ,lowerCamelCase : int=300 ,lowerCamelCase : Any=True ,lowerCamelCase : Tuple=True ,lowerCamelCase : int=1 ,lowerCamelCase : Tuple=5 ,lowerCamelCase : Union[str, Any]=2 ,lowerCamelCase : Tuple=1 ,lowerCamelCase : int=1 ,lowerCamelCase : str=5 ,lowerCamelCase : Optional[Any]=2 ,lowerCamelCase : List[Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.25 ,**lowerCamelCase : int ,): '''simple docstring''' if backbone_config is None: logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.""" ) __SCREAMING_SNAKE_CASE = CONFIG_MAPPING["""resnet"""](out_features=["""stage2""", """stage3""", """stage4"""] ) else: if isinstance(lowerCamelCase ,lowerCamelCase ): __SCREAMING_SNAKE_CASE = backbone_config.pop("""model_type""" ) __SCREAMING_SNAKE_CASE = CONFIG_MAPPING[backbone_model_type] __SCREAMING_SNAKE_CASE = config_class.from_dict(lowerCamelCase ) __SCREAMING_SNAKE_CASE = backbone_config __SCREAMING_SNAKE_CASE = num_queries __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = d_model __SCREAMING_SNAKE_CASE = encoder_ffn_dim __SCREAMING_SNAKE_CASE = encoder_layers __SCREAMING_SNAKE_CASE = encoder_attention_heads __SCREAMING_SNAKE_CASE = decoder_ffn_dim __SCREAMING_SNAKE_CASE = decoder_layers __SCREAMING_SNAKE_CASE = decoder_attention_heads __SCREAMING_SNAKE_CASE = dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = activation_function __SCREAMING_SNAKE_CASE = init_std __SCREAMING_SNAKE_CASE = init_xavier_std __SCREAMING_SNAKE_CASE = encoder_layerdrop __SCREAMING_SNAKE_CASE = auxiliary_loss __SCREAMING_SNAKE_CASE = position_embedding_type # deformable attributes __SCREAMING_SNAKE_CASE = num_feature_levels __SCREAMING_SNAKE_CASE = encoder_n_points __SCREAMING_SNAKE_CASE = decoder_n_points __SCREAMING_SNAKE_CASE = two_stage __SCREAMING_SNAKE_CASE = two_stage_num_proposals __SCREAMING_SNAKE_CASE = with_box_refine __SCREAMING_SNAKE_CASE = assign_first_stage if two_stage is True and with_box_refine is False: raise ValueError("""If two_stage is True, with_box_refine must be True.""" ) # Hungarian matcher __SCREAMING_SNAKE_CASE = class_cost __SCREAMING_SNAKE_CASE = bbox_cost __SCREAMING_SNAKE_CASE = giou_cost # Loss coefficients __SCREAMING_SNAKE_CASE = mask_loss_coefficient __SCREAMING_SNAKE_CASE = dice_loss_coefficient __SCREAMING_SNAKE_CASE = bbox_loss_coefficient __SCREAMING_SNAKE_CASE = giou_loss_coefficient __SCREAMING_SNAKE_CASE = eos_coefficient __SCREAMING_SNAKE_CASE = focal_alpha super().__init__(is_encoder_decoder=lowerCamelCase ,**lowerCamelCase ) @property def UpperCAmelCase__ ( self : Any ): '''simple docstring''' return self.encoder_attention_heads @property def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' return self.d_model def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) __SCREAMING_SNAKE_CASE = self.backbone_config.to_dict() __SCREAMING_SNAKE_CASE = self.__class__.model_type return output
13
0
import heapq as hq import math from collections.abc import Iterator class __a : def __init__( self : Tuple ,lowerCamelCase : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = str(id_ ) __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = {} # {vertex:distance} def __lt__( self : Dict ,lowerCamelCase : List[str] ): '''simple docstring''' return self.key < other.key def __repr__( self : List[str] ): '''simple docstring''' return self.id def UpperCAmelCase__ ( self : List[str] ,lowerCamelCase : Tuple ): '''simple docstring''' self.neighbors.append(lowerCamelCase ) def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : Optional[int] ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = weight def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Optional[int]: '''simple docstring''' graph[a - 1].add_neighbor(graph[b - 1] ) graph[b - 1].add_neighbor(graph[a - 1] ) # add the edges: graph[a - 1].add_edge(graph[b - 1] , __SCREAMING_SNAKE_CASE ) graph[b - 1].add_edge(graph[a - 1] , __SCREAMING_SNAKE_CASE ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> list: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] for u in graph: __SCREAMING_SNAKE_CASE = math.inf __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = graph[:] while q: __SCREAMING_SNAKE_CASE = min(__SCREAMING_SNAKE_CASE ) q.remove(__SCREAMING_SNAKE_CASE ) for v in u.neighbors: if (v in q) and (u.edges[v.id] < v.key): __SCREAMING_SNAKE_CASE = u __SCREAMING_SNAKE_CASE = u.edges[v.id] for i in range(1 , len(__SCREAMING_SNAKE_CASE ) ): a.append((int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) ) return a def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Iterator[tuple]: '''simple docstring''' for u in graph: __SCREAMING_SNAKE_CASE = math.inf __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = list(__SCREAMING_SNAKE_CASE ) hq.heapify(__SCREAMING_SNAKE_CASE ) while h: __SCREAMING_SNAKE_CASE = hq.heappop(__SCREAMING_SNAKE_CASE ) for v in u.neighbors: if (v in h) and (u.edges[v.id] < v.key): __SCREAMING_SNAKE_CASE = u __SCREAMING_SNAKE_CASE = u.edges[v.id] hq.heapify(__SCREAMING_SNAKE_CASE ) for i in range(1 , len(__SCREAMING_SNAKE_CASE ) ): yield (int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) def __magic_name__ ( ) -> None: '''simple docstring''' pass if __name__ == "__main__": import doctest doctest.testmod()
700
'''simple docstring''' import flax.linen as nn import jax import jax.numpy as jnp class __a ( nn.Module ): __UpperCamelCase : int __UpperCamelCase : jnp.dtype = jnp.floataa def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = nn.Conv( self.out_channels ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) def __call__( self : List[Any] ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = hidden_states.shape __SCREAMING_SNAKE_CASE = jax.image.resize( lowerCamelCase ,shape=(batch, height * 2, width * 2, channels) ,method="""nearest""" ,) __SCREAMING_SNAKE_CASE = self.conv(lowerCamelCase ) return hidden_states class __a ( nn.Module ): __UpperCamelCase : int __UpperCamelCase : jnp.dtype = jnp.floataa def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = nn.Conv( self.out_channels ,kernel_size=(3, 3) ,strides=(2, 2) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) def __call__( self : List[str] ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.conv(lowerCamelCase ) return hidden_states class __a ( nn.Module ): __UpperCamelCase : int __UpperCamelCase : int = None __UpperCamelCase : float = 0.0 __UpperCamelCase : bool = None __UpperCamelCase : jnp.dtype = jnp.floataa def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.in_channels if self.out_channels is None else self.out_channels __SCREAMING_SNAKE_CASE = nn.GroupNorm(num_groups=32 ,epsilon=1E-5 ) __SCREAMING_SNAKE_CASE = nn.Conv( lowerCamelCase ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) __SCREAMING_SNAKE_CASE = nn.Dense(lowerCamelCase ,dtype=self.dtype ) __SCREAMING_SNAKE_CASE = nn.GroupNorm(num_groups=32 ,epsilon=1E-5 ) __SCREAMING_SNAKE_CASE = nn.Dropout(self.dropout_prob ) __SCREAMING_SNAKE_CASE = nn.Conv( lowerCamelCase ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) __SCREAMING_SNAKE_CASE = self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut __SCREAMING_SNAKE_CASE = None if use_nin_shortcut: __SCREAMING_SNAKE_CASE = nn.Conv( lowerCamelCase ,kernel_size=(1, 1) ,strides=(1, 1) ,padding="""VALID""" ,dtype=self.dtype ,) def __call__( self : List[str] ,lowerCamelCase : Optional[int] ,lowerCamelCase : Tuple ,lowerCamelCase : Union[str, Any]=True ): '''simple docstring''' __SCREAMING_SNAKE_CASE = hidden_states __SCREAMING_SNAKE_CASE = self.norma(lowerCamelCase ) __SCREAMING_SNAKE_CASE = nn.swish(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.conva(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.time_emb_proj(nn.swish(lowerCamelCase ) ) __SCREAMING_SNAKE_CASE = jnp.expand_dims(jnp.expand_dims(lowerCamelCase ,1 ) ,1 ) __SCREAMING_SNAKE_CASE = hidden_states + temb __SCREAMING_SNAKE_CASE = self.norma(lowerCamelCase ) __SCREAMING_SNAKE_CASE = nn.swish(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.dropout(lowerCamelCase ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.conva(lowerCamelCase ) if self.conv_shortcut is not None: __SCREAMING_SNAKE_CASE = self.conv_shortcut(lowerCamelCase ) return hidden_states + residual
13
0
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.activations import gelu_new, gelu_python, get_activation @require_torch class __a( unittest.TestCase ): def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100] ) __SCREAMING_SNAKE_CASE = get_activation("""gelu""" ) self.assertTrue(torch.allclose(gelu_python(lowerCamelCase ) ,torch_builtin(lowerCamelCase ) ) ) self.assertFalse(torch.allclose(gelu_python(lowerCamelCase ) ,gelu_new(lowerCamelCase ) ) ) def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100] ) __SCREAMING_SNAKE_CASE = get_activation("""gelu""" ) __SCREAMING_SNAKE_CASE = get_activation("""gelu_10""" ) __SCREAMING_SNAKE_CASE = torch_builtin(lowerCamelCase ) __SCREAMING_SNAKE_CASE = geluaa(lowerCamelCase ) __SCREAMING_SNAKE_CASE = torch.where(y_gelu_aa < 10.0 ,1 ,0 ) self.assertTrue(torch.max(lowerCamelCase ).item() == 10.0 ) self.assertTrue(torch.allclose(y_gelu * clipped_mask ,y_gelu_aa * clipped_mask ) ) def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' get_activation("""gelu""" ) get_activation("""gelu_10""" ) get_activation("""gelu_fast""" ) get_activation("""gelu_new""" ) get_activation("""gelu_python""" ) get_activation("""gelu_pytorch_tanh""" ) get_activation("""linear""" ) get_activation("""mish""" ) get_activation("""quick_gelu""" ) get_activation("""relu""" ) get_activation("""sigmoid""" ) get_activation("""silu""" ) get_activation("""swish""" ) get_activation("""tanh""" ) with self.assertRaises(lowerCamelCase ): get_activation("""bogus""" ) with self.assertRaises(lowerCamelCase ): get_activation(lowerCamelCase ) def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = get_activation("""gelu""" ) __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = get_activation("""gelu""" ) self.assertEqual(acta.a ,1 ) with self.assertRaises(lowerCamelCase ): __SCREAMING_SNAKE_CASE = acta.a
701
'''simple docstring''' import sys from collections import defaultdict class __a : def __init__( self : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = [] def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : List[Any] ): '''simple docstring''' return self.node_position[vertex] def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : str ,lowerCamelCase : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = pos def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : Optional[int] ,lowerCamelCase : Union[str, Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : Any ): '''simple docstring''' if start > size // 2 - 1: return else: if 2 * start + 2 >= size: __SCREAMING_SNAKE_CASE = 2 * start + 1 else: if heap[2 * start + 1] < heap[2 * start + 2]: __SCREAMING_SNAKE_CASE = 2 * start + 1 else: __SCREAMING_SNAKE_CASE = 2 * start + 2 if heap[smallest_child] < heap[start]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = heap[smallest_child], positions[smallest_child] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = ( heap[start], positions[start], ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = temp, tempa __SCREAMING_SNAKE_CASE = self.get_position(positions[smallest_child] ) self.set_position( positions[smallest_child] ,self.get_position(positions[start] ) ) self.set_position(positions[start] ,lowerCamelCase ) self.top_to_bottom(lowerCamelCase ,lowerCamelCase ,lowerCamelCase ,lowerCamelCase ) def UpperCAmelCase__ ( self : Any ,lowerCamelCase : int ,lowerCamelCase : List[str] ,lowerCamelCase : Optional[Any] ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = position[index] while index != 0: __SCREAMING_SNAKE_CASE = int((index - 2) / 2 ) if index % 2 == 0 else int((index - 1) / 2 ) if val < heap[parent]: __SCREAMING_SNAKE_CASE = heap[parent] __SCREAMING_SNAKE_CASE = position[parent] self.set_position(position[parent] ,lowerCamelCase ) else: __SCREAMING_SNAKE_CASE = val __SCREAMING_SNAKE_CASE = temp self.set_position(lowerCamelCase ,lowerCamelCase ) break __SCREAMING_SNAKE_CASE = parent else: __SCREAMING_SNAKE_CASE = val __SCREAMING_SNAKE_CASE = temp self.set_position(lowerCamelCase ,0 ) def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : List[Any] ,lowerCamelCase : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = len(lowerCamelCase ) // 2 - 1 for i in range(lowerCamelCase ,-1 ,-1 ): self.top_to_bottom(lowerCamelCase ,lowerCamelCase ,len(lowerCamelCase ) ,lowerCamelCase ) def UpperCAmelCase__ ( self : int ,lowerCamelCase : Optional[int] ,lowerCamelCase : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = positions[0] __SCREAMING_SNAKE_CASE = sys.maxsize self.top_to_bottom(lowerCamelCase ,0 ,len(lowerCamelCase ) ,lowerCamelCase ) return temp def __magic_name__ ( __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = Heap() __SCREAMING_SNAKE_CASE = [0] * len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [-1] * len(__UpperCAmelCase ) # Neighboring Tree Vertex of selected vertex # Minimum Distance of explored vertex with neighboring vertex of partial tree # formed in graph __SCREAMING_SNAKE_CASE = [] # Heap of Distance of vertices from their neighboring vertex __SCREAMING_SNAKE_CASE = [] for vertex in range(len(__UpperCAmelCase ) ): distance_tv.append(sys.maxsize ) positions.append(__UpperCAmelCase ) heap.node_position.append(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = sys.maxsize for neighbor, distance in adjacency_list[0]: __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = distance heap.heapify(__UpperCAmelCase , __UpperCAmelCase ) for _ in range(1 , len(__UpperCAmelCase ) ): __SCREAMING_SNAKE_CASE = heap.delete_minimum(__UpperCAmelCase , __UpperCAmelCase ) if visited[vertex] == 0: tree_edges.append((nbr_tv[vertex], vertex) ) __SCREAMING_SNAKE_CASE = 1 for neighbor, distance in adjacency_list[vertex]: if ( visited[neighbor] == 0 and distance < distance_tv[heap.get_position(__UpperCAmelCase )] ): __SCREAMING_SNAKE_CASE = distance heap.bottom_to_top( __UpperCAmelCase , heap.get_position(__UpperCAmelCase ) , __UpperCAmelCase , __UpperCAmelCase ) __SCREAMING_SNAKE_CASE = vertex return tree_edges if __name__ == "__main__": # pragma: no cover # < --------- Prims Algorithm --------- > a = int(input("Enter number of edges: ").strip()) a = defaultdict(list) for _ in range(edges_number): a = [int(x) for x in input().strip().split()] adjacency_list[edge[0]].append([edge[1], edge[2]]) adjacency_list[edge[1]].append([edge[0], edge[2]]) print(prisms_algorithm(adjacency_list))
13
0
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Value from .base import TaskTemplate @dataclass(frozen=UpperCamelCase_ ) class __a ( UpperCamelCase_ ): __UpperCamelCase : List[Any] = field(default='text-classification', metadata={'include_in_asdict_even_if_is_default': True} ) __UpperCamelCase : Optional[int] = Features({'text': Value('string' )} ) __UpperCamelCase : str = Features({'labels': ClassLabel} ) __UpperCamelCase : Optional[Any] = 'text' __UpperCamelCase : Any = 'labels' def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : List[Any] ): '''simple docstring''' if self.label_column not in features: raise ValueError(f"""Column {self.label_column} is not present in features.""" ) if not isinstance(features[self.label_column] ,__A ): raise ValueError(f"""Column {self.label_column} is not a ClassLabel.""" ) __SCREAMING_SNAKE_CASE = copy.deepcopy(self ) __SCREAMING_SNAKE_CASE = self.label_schema.copy() __SCREAMING_SNAKE_CASE = features[self.label_column] __SCREAMING_SNAKE_CASE = label_schema return task_template @property def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' return { self.text_column: "text", self.label_column: "labels", }
702
'''simple docstring''' import os import string import sys a = 1 << 8 a = { "tab": ord("\t"), "newline": ord("\r"), "esc": 27, "up": 65 + ARROW_KEY_FLAG, "down": 66 + ARROW_KEY_FLAG, "right": 67 + ARROW_KEY_FLAG, "left": 68 + ARROW_KEY_FLAG, "mod_int": 91, "undefined": sys.maxsize, "interrupt": 3, "insert": 50, "delete": 51, "pg_up": 53, "pg_down": 54, } a = KEYMAP["up"] a = KEYMAP["left"] if sys.platform == "win32": a = [] a = { b"\xe0H": KEYMAP["up"] - ARROW_KEY_FLAG, b"\x00H": KEYMAP["up"] - ARROW_KEY_FLAG, b"\xe0P": KEYMAP["down"] - ARROW_KEY_FLAG, b"\x00P": KEYMAP["down"] - ARROW_KEY_FLAG, b"\xe0M": KEYMAP["right"] - ARROW_KEY_FLAG, b"\x00M": KEYMAP["right"] - ARROW_KEY_FLAG, b"\xe0K": KEYMAP["left"] - ARROW_KEY_FLAG, b"\x00K": KEYMAP["left"] - ARROW_KEY_FLAG, } for i in range(10): a = ord(str(i)) def __magic_name__ ( ) -> Union[str, Any]: '''simple docstring''' if os.name == "nt": import msvcrt __SCREAMING_SNAKE_CASE = """mbcs""" # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(__UpperCAmelCase ) == 0: # Read the keystroke __SCREAMING_SNAKE_CASE = msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): __SCREAMING_SNAKE_CASE = ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: __SCREAMING_SNAKE_CASE = chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP["""mod_int"""] ) ) WIN_CH_BUFFER.append(__UpperCAmelCase ) if ord(__UpperCAmelCase ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(126 ) ) __SCREAMING_SNAKE_CASE = chr(KEYMAP["""esc"""] ) except KeyError: __SCREAMING_SNAKE_CASE = cha[1] else: __SCREAMING_SNAKE_CASE = ch.decode(__UpperCAmelCase ) else: __SCREAMING_SNAKE_CASE = WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty __SCREAMING_SNAKE_CASE = sys.stdin.fileno() __SCREAMING_SNAKE_CASE = termios.tcgetattr(__UpperCAmelCase ) try: tty.setraw(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = sys.stdin.read(1 ) finally: termios.tcsetattr(__UpperCAmelCase , termios.TCSADRAIN , __UpperCAmelCase ) return ch def __magic_name__ ( ) -> List[str]: '''simple docstring''' __SCREAMING_SNAKE_CASE = get_raw_chars() if ord(__UpperCAmelCase ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(__UpperCAmelCase ) == KEYMAP["esc"]: __SCREAMING_SNAKE_CASE = get_raw_chars() if ord(__UpperCAmelCase ) == KEYMAP["mod_int"]: __SCREAMING_SNAKE_CASE = get_raw_chars() if ord(__UpperCAmelCase ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(__UpperCAmelCase ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(__UpperCAmelCase ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
13
0
'''simple docstring''' from random import randint from tempfile import TemporaryFile import numpy as np def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Optional[int]: '''simple docstring''' __SCREAMING_SNAKE_CASE = 0 if start < end: __SCREAMING_SNAKE_CASE = randint(_lowerCamelCase , _lowerCamelCase ) __SCREAMING_SNAKE_CASE = a[end] __SCREAMING_SNAKE_CASE = a[pivot] __SCREAMING_SNAKE_CASE = temp __SCREAMING_SNAKE_CASE = _in_place_partition(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) count += _in_place_quick_sort(_lowerCamelCase , _lowerCamelCase , p - 1 ) count += _in_place_quick_sort(_lowerCamelCase , p + 1 , _lowerCamelCase ) return count def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> List[str]: '''simple docstring''' __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = randint(_lowerCamelCase , _lowerCamelCase ) __SCREAMING_SNAKE_CASE = a[end] __SCREAMING_SNAKE_CASE = a[pivot] __SCREAMING_SNAKE_CASE = temp __SCREAMING_SNAKE_CASE = start - 1 for index in range(_lowerCamelCase , _lowerCamelCase ): count += 1 if a[index] < a[end]: # check if current val is less than pivot value __SCREAMING_SNAKE_CASE = new_pivot_index + 1 __SCREAMING_SNAKE_CASE = a[new_pivot_index] __SCREAMING_SNAKE_CASE = a[index] __SCREAMING_SNAKE_CASE = temp __SCREAMING_SNAKE_CASE = a[new_pivot_index + 1] __SCREAMING_SNAKE_CASE = a[end] __SCREAMING_SNAKE_CASE = temp return new_pivot_index + 1, count a = TemporaryFile() a = 100 # 1000 elements are to be sorted a = 0, 1 # mean and standard deviation a = np.random.normal(mu, sigma, p) np.save(outfile, X) print("The array is") print(X) outfile.seek(0) # using the same array a = np.load(outfile) a = len(M) - 1 a = _in_place_quick_sort(M, 0, r) print( "No of Comparisons for 100 elements selected from a standard normal distribution" "is :" ) print(z)
703
'''simple docstring''' from __future__ import annotations import bisect def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' if hi < 0: __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) while lo < hi: __SCREAMING_SNAKE_CASE = lo + (hi - lo) // 2 if sorted_collection[mid] < item: __SCREAMING_SNAKE_CASE = mid + 1 else: __SCREAMING_SNAKE_CASE = mid return lo def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' if hi < 0: __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) while lo < hi: __SCREAMING_SNAKE_CASE = lo + (hi - lo) // 2 if sorted_collection[mid] <= item: __SCREAMING_SNAKE_CASE = mid + 1 else: __SCREAMING_SNAKE_CASE = mid return lo def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> None: '''simple docstring''' sorted_collection.insert(bisect_left(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> None: '''simple docstring''' sorted_collection.insert(bisect_right(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> int | None: '''simple docstring''' __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) - 1 while left <= right: __SCREAMING_SNAKE_CASE = left + (right - left) // 2 __SCREAMING_SNAKE_CASE = sorted_collection[midpoint] if current_item == item: return midpoint elif item < current_item: __SCREAMING_SNAKE_CASE = midpoint - 1 else: __SCREAMING_SNAKE_CASE = midpoint + 1 return None def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> int | None: '''simple docstring''' __SCREAMING_SNAKE_CASE = bisect.bisect_left(__UpperCAmelCase , __UpperCAmelCase ) if index != len(__UpperCAmelCase ) and sorted_collection[index] == item: return index return None def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> int | None: '''simple docstring''' if right < left: return None __SCREAMING_SNAKE_CASE = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: return binary_search_by_recursion(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , midpoint - 1 ) else: return binary_search_by_recursion(__UpperCAmelCase , __UpperCAmelCase , midpoint + 1 , __UpperCAmelCase ) if __name__ == "__main__": a = input("Enter numbers separated by comma:\n").strip() a = sorted(int(item) for item in user_input.split(",")) a = int(input("Enter a single number to be found in the list:\n")) a = binary_search(collection, target) if result is None: print(F'''{target} was not found in {collection}.''') else: print(F'''{target} was found at position {result} in {collection}.''')
13
0
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_big_bird import BigBirdTokenizer else: a = None a = logging.get_logger(__name__) a = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"} a = { "vocab_file": { "google/bigbird-roberta-base": "https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model", "google/bigbird-roberta-large": ( "https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model" ), "google/bigbird-base-trivia-itc": ( "https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model" ), }, "tokenizer_file": { "google/bigbird-roberta-base": ( "https://huggingface.co/google/bigbird-roberta-base/resolve/main/tokenizer.json" ), "google/bigbird-roberta-large": ( "https://huggingface.co/google/bigbird-roberta-large/resolve/main/tokenizer.json" ), "google/bigbird-base-trivia-itc": ( "https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/tokenizer.json" ), }, } a = { "google/bigbird-roberta-base": 4096, "google/bigbird-roberta-large": 4096, "google/bigbird-base-trivia-itc": 4096, } a = "▁" class __a ( __SCREAMING_SNAKE_CASE ): __UpperCamelCase : Optional[int] = VOCAB_FILES_NAMES __UpperCamelCase : Tuple = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : List[Any] = BigBirdTokenizer __UpperCamelCase : Tuple = ['input_ids', 'attention_mask'] __UpperCamelCase : List[Any] = [] def __init__( self : str ,lowerCamelCase : Any=None ,lowerCamelCase : Optional[Any]=None ,lowerCamelCase : str="<unk>" ,lowerCamelCase : Union[str, Any]="<s>" ,lowerCamelCase : Optional[Any]="</s>" ,lowerCamelCase : Any="<pad>" ,lowerCamelCase : str="[SEP]" ,lowerCamelCase : Dict="[MASK]" ,lowerCamelCase : int="[CLS]" ,**lowerCamelCase : List[Any] ,): '''simple docstring''' __SCREAMING_SNAKE_CASE = AddedToken(_a ,lstrip=_a ,rstrip=_a ) if isinstance(_a ,_a ) else bos_token __SCREAMING_SNAKE_CASE = AddedToken(_a ,lstrip=_a ,rstrip=_a ) if isinstance(_a ,_a ) else eos_token __SCREAMING_SNAKE_CASE = AddedToken(_a ,lstrip=_a ,rstrip=_a ) if isinstance(_a ,_a ) else unk_token __SCREAMING_SNAKE_CASE = AddedToken(_a ,lstrip=_a ,rstrip=_a ) if isinstance(_a ,_a ) else pad_token __SCREAMING_SNAKE_CASE = AddedToken(_a ,lstrip=_a ,rstrip=_a ) if isinstance(_a ,_a ) else cls_token __SCREAMING_SNAKE_CASE = AddedToken(_a ,lstrip=_a ,rstrip=_a ) if isinstance(_a ,_a ) else sep_token # Mask token behave like a normal word, i.e. include the space before it __SCREAMING_SNAKE_CASE = AddedToken(_a ,lstrip=_a ,rstrip=_a ) if isinstance(_a ,_a ) else mask_token super().__init__( _a ,tokenizer_file=_a ,bos_token=_a ,eos_token=_a ,unk_token=_a ,sep_token=_a ,pad_token=_a ,cls_token=_a ,mask_token=_a ,**_a ,) __SCREAMING_SNAKE_CASE = vocab_file __SCREAMING_SNAKE_CASE = False if not self.vocab_file else True def UpperCAmelCase__ ( self : Any ,lowerCamelCase : List[int] ,lowerCamelCase : Optional[List[int]] = None ): '''simple docstring''' __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 UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : List[int] ,lowerCamelCase : Optional[List[int]] = None ,lowerCamelCase : bool = False ): '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( """You should not supply a second sequence if the provided sequence of """ """ids is already formatted with special tokens for the model.""" ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is None: return [1] + ([0] * len(_a )) + [1] return [1] + ([0] * len(_a )) + [1] + ([0] * len(_a )) + [1] def UpperCAmelCase__ ( self : str ,lowerCamelCase : List[int] ,lowerCamelCase : Optional[List[int]] = None ): '''simple docstring''' __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 UpperCAmelCase__ ( self : Union[str, Any] ,lowerCamelCase : str ,lowerCamelCase : Optional[str] = None ): '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(_a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return __SCREAMING_SNAKE_CASE = os.path.join( _a ,(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ): copyfile(self.vocab_file ,_a ) return (out_vocab_file,)
704
'''simple docstring''' import math from enum import Enum from typing import Optional, Union from torch.optim import Optimizer from torch.optim.lr_scheduler import LambdaLR from .utils import logging a = logging.get_logger(__name__) class __a ( _snake_case ): __UpperCamelCase : int = 'linear' __UpperCamelCase : Tuple = 'cosine' __UpperCamelCase : Tuple = 'cosine_with_restarts' __UpperCamelCase : List[Any] = 'polynomial' __UpperCamelCase : Optional[Any] = 'constant' __UpperCamelCase : Optional[int] = 'constant_with_warmup' __UpperCamelCase : List[Any] = 'piecewise_constant' def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' return LambdaLR(__UpperCAmelCase , lambda __UpperCAmelCase : 1 , last_epoch=__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = -1 ) -> List[Any]: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1.0 , __UpperCAmelCase ) ) return 1.0 return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , last_epoch=__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = step_rules.split(""",""" ) for rule_str in rule_list[:-1]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = rule_str.split(""":""" ) __SCREAMING_SNAKE_CASE = int(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = float(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = value __SCREAMING_SNAKE_CASE = float(rule_list[-1] ) def create_rules_function(__UpperCAmelCase , __UpperCAmelCase ): def rule_func(__UpperCAmelCase ) -> float: __SCREAMING_SNAKE_CASE = sorted(rules_dict.keys() ) for i, sorted_step in enumerate(__UpperCAmelCase ): if steps < sorted_step: return rules_dict[sorted_steps[i]] return last_lr_multiple return rule_func __SCREAMING_SNAKE_CASE = create_rules_function(__UpperCAmelCase , __UpperCAmelCase ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , last_epoch=__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=-1 ) -> int: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) return max( 0.0 , float(num_training_steps - current_step ) / float(max(1 , num_training_steps - num_warmup_steps ) ) ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0.5 , __UpperCAmelCase = -1 ) -> Dict: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = float(current_step - num_warmup_steps ) / float(max(1 , num_training_steps - num_warmup_steps ) ) return max(0.0 , 0.5 * (1.0 + math.cos(math.pi * float(__UpperCAmelCase ) * 2.0 * progress )) ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 1 , __UpperCAmelCase = -1 ) -> Tuple: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = float(current_step - num_warmup_steps ) / float(max(1 , num_training_steps - num_warmup_steps ) ) if progress >= 1.0: return 0.0 return max(0.0 , 0.5 * (1.0 + math.cos(math.pi * ((float(__UpperCAmelCase ) * progress) % 1.0) )) ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=1e-7 , __UpperCAmelCase=1.0 , __UpperCAmelCase=-1 ) -> Tuple: '''simple docstring''' __SCREAMING_SNAKE_CASE = optimizer.defaults["""lr"""] if not (lr_init > lr_end): raise ValueError(f"""lr_end ({lr_end}) must be be smaller than initial lr ({lr_init})""" ) def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) elif current_step > num_training_steps: return lr_end / lr_init # as LambdaLR multiplies by lr_init else: __SCREAMING_SNAKE_CASE = lr_init - lr_end __SCREAMING_SNAKE_CASE = num_training_steps - num_warmup_steps __SCREAMING_SNAKE_CASE = 1 - (current_step - num_warmup_steps) / decay_steps __SCREAMING_SNAKE_CASE = lr_range * pct_remaining**power + lr_end return decay / lr_init # as LambdaLR multiplies by lr_init return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) a = { SchedulerType.LINEAR: get_linear_schedule_with_warmup, SchedulerType.COSINE: get_cosine_schedule_with_warmup, SchedulerType.COSINE_WITH_RESTARTS: get_cosine_with_hard_restarts_schedule_with_warmup, SchedulerType.POLYNOMIAL: get_polynomial_decay_schedule_with_warmup, SchedulerType.CONSTANT: get_constant_schedule, SchedulerType.CONSTANT_WITH_WARMUP: get_constant_schedule_with_warmup, SchedulerType.PIECEWISE_CONSTANT: get_piecewise_constant_schedule, } def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = 1 , __UpperCAmelCase = 1.0 , __UpperCAmelCase = -1 , ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = SchedulerType(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = TYPE_TO_SCHEDULER_FUNCTION[name] if name == SchedulerType.CONSTANT: return schedule_func(__UpperCAmelCase , last_epoch=__UpperCAmelCase ) if name == SchedulerType.PIECEWISE_CONSTANT: return schedule_func(__UpperCAmelCase , step_rules=__UpperCAmelCase , last_epoch=__UpperCAmelCase ) # All other schedulers require `num_warmup_steps` if num_warmup_steps is None: raise ValueError(f"""{name} requires `num_warmup_steps`, please provide that argument.""" ) if name == SchedulerType.CONSTANT_WITH_WARMUP: return schedule_func(__UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , last_epoch=__UpperCAmelCase ) # All other schedulers require `num_training_steps` if num_training_steps is None: raise ValueError(f"""{name} requires `num_training_steps`, please provide that argument.""" ) if name == SchedulerType.COSINE_WITH_RESTARTS: return schedule_func( __UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , num_training_steps=__UpperCAmelCase , num_cycles=__UpperCAmelCase , last_epoch=__UpperCAmelCase , ) if name == SchedulerType.POLYNOMIAL: return schedule_func( __UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , num_training_steps=__UpperCAmelCase , power=__UpperCAmelCase , last_epoch=__UpperCAmelCase , ) return schedule_func( __UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , num_training_steps=__UpperCAmelCase , last_epoch=__UpperCAmelCase )
13
0
'''simple docstring''' import inspect import unittest class __a ( unittest.TestCase ): '''simple docstring''' def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' try: import diffusers # noqa: F401 except ImportError: assert False def UpperCAmelCase__ ( self : int ): '''simple docstring''' import diffusers from diffusers.dependency_versions_table import deps __SCREAMING_SNAKE_CASE = inspect.getmembers(a_ ,inspect.isclass ) for cls_name, cls_module in all_classes: if "dummy_" in cls_module.__module__: for backend in cls_module._backends: if backend == "k_diffusion": __SCREAMING_SNAKE_CASE = """k-diffusion""" elif backend == "invisible_watermark": __SCREAMING_SNAKE_CASE = """invisible-watermark""" assert backend in deps, f"""{backend} is not in the deps table!"""
705
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a = {"configuration_sew": ["SEW_PRETRAINED_CONFIG_ARCHIVE_MAP", "SEWConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = [ "SEW_PRETRAINED_MODEL_ARCHIVE_LIST", "SEWForCTC", "SEWForSequenceClassification", "SEWModel", "SEWPreTrainedModel", ] if TYPE_CHECKING: from .configuration_sew import SEW_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_sew import ( SEW_PRETRAINED_MODEL_ARCHIVE_LIST, SEWForCTC, SEWForSequenceClassification, SEWModel, SEWPreTrainedModel, ) else: import sys a = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
13
0
'''simple docstring''' a = "Alexander Joslin" import operator as op from .stack import Stack def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = {"""*""": op.mul, """/""": op.truediv, """+""": op.add, """-""": op.sub} __SCREAMING_SNAKE_CASE = Stack() __SCREAMING_SNAKE_CASE = Stack() for i in equation: if i.isdigit(): # RULE 1 operand_stack.push(int(__snake_case ) ) elif i in operators: # RULE 2 operator_stack.push(__snake_case ) elif i == ")": # RULE 4 __SCREAMING_SNAKE_CASE = operator_stack.peek() operator_stack.pop() __SCREAMING_SNAKE_CASE = operand_stack.peek() operand_stack.pop() __SCREAMING_SNAKE_CASE = operand_stack.peek() operand_stack.pop() __SCREAMING_SNAKE_CASE = operators[opr](__snake_case , __snake_case ) operand_stack.push(__snake_case ) # RULE 5 return operand_stack.peek() if __name__ == "__main__": a = "(5 + ((4 * 2) * (2 + 3)))" # answer = 45 print(F'''{equation} = {dijkstras_two_stack_algorithm(equation)}''')
706
'''simple docstring''' import requests from bsa import BeautifulSoup def __magic_name__ ( __UpperCAmelCase = "AAPL" ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""https://in.finance.yahoo.com/quote/{symbol}?s={symbol}""" __SCREAMING_SNAKE_CASE = BeautifulSoup(requests.get(__UpperCAmelCase ).text , """html.parser""" ) __SCREAMING_SNAKE_CASE = """My(6px) Pos(r) smartphone_Mt(6px)""" return soup.find("""div""" , class_=class_ ).find("""span""" ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(F'''Current {symbol:<4} stock price is {stock_price(symbol):>8}''')
13
0
'''simple docstring''' import argparse import torch from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_from_original_stable_diffusion_ckpt if __name__ == "__main__": a = argparse.ArgumentParser() parser.add_argument( "--checkpoint_path", default=None, type=str, required=True, help="Path to the checkpoint to convert." ) # !wget https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml parser.add_argument( "--original_config_file", default=None, type=str, help="The YAML config file corresponding to the original architecture.", ) parser.add_argument( "--num_in_channels", default=None, type=int, help="The number of input channels. If `None` number of input channels will be automatically inferred.", ) parser.add_argument( "--scheduler_type", default="pndm", type=str, help="Type of scheduler to use. Should be one of ['pndm', 'lms', 'ddim', 'euler', 'euler-ancestral', 'dpm']", ) parser.add_argument( "--pipeline_type", default=None, type=str, help=( "The pipeline type. One of 'FrozenOpenCLIPEmbedder', 'FrozenCLIPEmbedder', 'PaintByExample'" ". If `None` pipeline will be automatically inferred." ), ) parser.add_argument( "--image_size", default=None, type=int, help=( "The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2" " Base. Use 768 for Stable Diffusion v2." ), ) parser.add_argument( "--prediction_type", default=None, type=str, help=( "The prediction type that the model was trained on. Use 'epsilon' for Stable Diffusion v1.X and Stable" " Diffusion v2 Base. Use 'v_prediction' for Stable Diffusion v2." ), ) parser.add_argument( "--extract_ema", action="store_true", help=( "Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights" " or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield" " higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning." ), ) parser.add_argument( "--upcast_attention", action="store_true", help=( "Whether the attention computation should always be upcasted. This is necessary when running stable" " diffusion 2.1." ), ) parser.add_argument( "--from_safetensors", action="store_true", help="If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.", ) parser.add_argument( "--to_safetensors", action="store_true", help="Whether to store pipeline in safetensors format or not.", ) parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the output model.") parser.add_argument("--device", type=str, help="Device to use (e.g. cpu, cuda:0, cuda:1, etc.)") parser.add_argument( "--stable_unclip", type=str, default=None, required=False, help="Set if this is a stable unCLIP model. One of 'txt2img' or 'img2img'.", ) parser.add_argument( "--stable_unclip_prior", type=str, default=None, required=False, help="Set if this is a stable unCLIP txt2img model. Selects which prior to use. If `--stable_unclip` is set to `txt2img`, the karlo prior (https://huggingface.co/kakaobrain/karlo-v1-alpha/tree/main/prior) is selected by default.", ) parser.add_argument( "--clip_stats_path", type=str, help="Path to the clip stats file. Only required if the stable unclip model's config specifies `model.params.noise_aug_config.params.clip_stats_path`.", required=False, ) parser.add_argument( "--controlnet", action="store_true", default=None, help="Set flag if this is a controlnet checkpoint." ) parser.add_argument("--half", action="store_true", help="Save weights in half precision.") parser.add_argument( "--vae_path", type=str, default=None, required=False, help="Set to a path, hub id to an already converted vae to not convert it again.", ) a = parser.parse_args() a = download_from_original_stable_diffusion_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, prediction_type=args.prediction_type, model_type=args.pipeline_type, extract_ema=args.extract_ema, scheduler_type=args.scheduler_type, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, stable_unclip=args.stable_unclip, stable_unclip_prior=args.stable_unclip_prior, clip_stats_path=args.clip_stats_path, controlnet=args.controlnet, vae_path=args.vae_path, ) if args.half: pipe.to(torch_dtype=torch.floataa) if args.controlnet: # only save the controlnet model pipe.controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors) else: pipe.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
707
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase ) -> bool: '''simple docstring''' if num < 0: return False __SCREAMING_SNAKE_CASE = num __SCREAMING_SNAKE_CASE = 0 while num > 0: __SCREAMING_SNAKE_CASE = rev_num * 10 + (num % 10) num //= 10 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
13
0
'''simple docstring''' import baseaa def __magic_name__ ( __UpperCAmelCase ) -> bytes: '''simple docstring''' return baseaa.aaaencode(string.encode("""utf-8""" ) ) def __magic_name__ ( __UpperCAmelCase ) -> str: '''simple docstring''' return baseaa.aaadecode(_lowercase ).decode("""utf-8""" ) if __name__ == "__main__": import doctest doctest.testmod()
708
'''simple docstring''' from __future__ import annotations from collections.abc import Callable a = list[list[float | int]] def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Matrix: '''simple docstring''' __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [[0 for _ in range(size + 1 )] for _ in range(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 for row in range(__UpperCAmelCase ): for col in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = matrix[row][col] __SCREAMING_SNAKE_CASE = vector[row][0] __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 while row < size and col < size: # pivoting __SCREAMING_SNAKE_CASE = max((abs(augmented[rowa][col] ), rowa) for rowa in range(__UpperCAmelCase , __UpperCAmelCase ) )[ 1 ] if augmented[pivot_row][col] == 0: col += 1 continue else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = augmented[pivot_row], augmented[row] for rowa in range(row + 1 , __UpperCAmelCase ): __SCREAMING_SNAKE_CASE = augmented[rowa][col] / augmented[row][col] __SCREAMING_SNAKE_CASE = 0 for cola in range(col + 1 , size + 1 ): augmented[rowa][cola] -= augmented[row][cola] * ratio row += 1 col += 1 # back substitution for col in range(1 , __UpperCAmelCase ): for row in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = augmented[row][col] / augmented[col][col] for cola in range(__UpperCAmelCase , size + 1 ): augmented[row][cola] -= augmented[col][cola] * ratio # round to get rid of numbers like 2.000000000000004 return [ [round(augmented[row][size] / augmented[row][row] , 10 )] for row in range(__UpperCAmelCase ) ] def __magic_name__ ( __UpperCAmelCase ) -> Callable[[int], int]: '''simple docstring''' __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [[0 for _ in range(__UpperCAmelCase )] for _ in range(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = [[0] for _ in range(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 for x_val, y_val in enumerate(__UpperCAmelCase ): for col in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = (x_val + 1) ** (size - col - 1) __SCREAMING_SNAKE_CASE = y_val __SCREAMING_SNAKE_CASE = solve(__UpperCAmelCase , __UpperCAmelCase ) def interpolated_func(__UpperCAmelCase ) -> int: return sum( round(coeffs[x_val][0] ) * (var ** (size - x_val - 1)) for x_val in range(__UpperCAmelCase ) ) return interpolated_func def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' return ( 1 - variable + variable**2 - variable**3 + variable**4 - variable**5 + variable**6 - variable**7 + variable**8 - variable**9 + variable**10 ) def __magic_name__ ( __UpperCAmelCase = question_function , __UpperCAmelCase = 10 ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = [func(__UpperCAmelCase ) for x_val in range(1 , order + 1 )] __SCREAMING_SNAKE_CASE = [ interpolate(data_points[:max_coeff] ) for max_coeff in range(1 , order + 1 ) ] __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 for poly in polynomials: __SCREAMING_SNAKE_CASE = 1 while func(__UpperCAmelCase ) == poly(__UpperCAmelCase ): x_val += 1 ret += poly(__UpperCAmelCase ) return ret if __name__ == "__main__": print(F'''{solution() = }''')
13
0
'''simple docstring''' from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer a = logging.get_logger(__name__) a = { '''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_config_file''': '''tokenizer_config.json''', } a = { '''vocab_file''': { '''facebook/blenderbot_small-90M''': '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json''' }, '''merges_file''': { '''facebook/blenderbot_small-90M''': '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt''' }, '''tokenizer_config_file''': { '''facebook/blenderbot_small-90M''': ( '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json''' ) }, } a = { '''facebook/blenderbot_small-90M''': 512, } class __a ( UpperCAmelCase__ ): __UpperCamelCase : Union[str, Any] = VOCAB_FILES_NAMES __UpperCamelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : Optional[Any] = BlenderbotSmallTokenizer def __init__( self : List[Any] ,lowerCamelCase : Union[str, Any]=None ,lowerCamelCase : Tuple=None ,lowerCamelCase : Any="<|endoftext|>" ,lowerCamelCase : Tuple="<|endoftext|>" ,lowerCamelCase : str="<|endoftext|>" ,lowerCamelCase : Any=False ,lowerCamelCase : Union[str, Any]=True ,**lowerCamelCase : int ,): '''simple docstring''' super().__init__( ByteLevelBPETokenizer( vocab=__lowerCAmelCase ,merges=__lowerCAmelCase ,add_prefix_space=__lowerCAmelCase ,trim_offsets=__lowerCAmelCase ,) ,bos_token=__lowerCAmelCase ,eos_token=__lowerCAmelCase ,unk_token=__lowerCAmelCase ,**__lowerCAmelCase ,) __SCREAMING_SNAKE_CASE = add_prefix_space def UpperCAmelCase__ ( self : str ,lowerCamelCase : List[Any] ,lowerCamelCase : List[str]=None ): '''simple docstring''' __SCREAMING_SNAKE_CASE = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def UpperCAmelCase__ ( self : int ,lowerCamelCase : List[int] ,lowerCamelCase : Optional[List[int]] = None ): '''simple docstring''' __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 + sep + token_ids_a + sep ) * [0]
709
'''simple docstring''' from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax a = logging.get_logger(__name__) @add_end_docstrings(_snake_case ) class __a ( _snake_case ): def __init__( self : Union[str, Any] ,**lowerCamelCase : str ): '''simple docstring''' super().__init__(**lowerCamelCase ) requires_backends(self ,"""vision""" ) self.check_model_type( TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if self.framework == """tf""" else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING ) def __call__( self : Dict ,lowerCamelCase : Union[str, List[str], "Image", List["Image"]] ,**lowerCamelCase : Optional[Any] ): '''simple docstring''' return super().__call__(lowerCamelCase ,**lowerCamelCase ) def UpperCAmelCase__ ( self : Optional[Any] ,**lowerCamelCase : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = {} if "candidate_labels" in kwargs: __SCREAMING_SNAKE_CASE = kwargs["""candidate_labels"""] if "hypothesis_template" in kwargs: __SCREAMING_SNAKE_CASE = kwargs["""hypothesis_template"""] return preprocess_params, {}, {} def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : Union[str, Any]=None ,lowerCamelCase : Union[str, Any]="This is a photo of {}." ): '''simple docstring''' __SCREAMING_SNAKE_CASE = load_image(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.image_processor(images=[image] ,return_tensors=self.framework ) __SCREAMING_SNAKE_CASE = candidate_labels __SCREAMING_SNAKE_CASE = [hypothesis_template.format(lowerCamelCase ) for x in candidate_labels] __SCREAMING_SNAKE_CASE = self.tokenizer(lowerCamelCase ,return_tensors=self.framework ,padding=lowerCamelCase ) __SCREAMING_SNAKE_CASE = [text_inputs] return inputs def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = model_inputs.pop("""candidate_labels""" ) __SCREAMING_SNAKE_CASE = model_inputs.pop("""text_inputs""" ) if isinstance(text_inputs[0] ,lowerCamelCase ): __SCREAMING_SNAKE_CASE = text_inputs[0] else: # Batching case. __SCREAMING_SNAKE_CASE = text_inputs[0][0] __SCREAMING_SNAKE_CASE = self.model(**lowerCamelCase ,**lowerCamelCase ) __SCREAMING_SNAKE_CASE = { """candidate_labels""": candidate_labels, """logits""": outputs.logits_per_image, } return model_outputs def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = model_outputs.pop("""candidate_labels""" ) __SCREAMING_SNAKE_CASE = model_outputs["""logits"""][0] if self.framework == "pt": __SCREAMING_SNAKE_CASE = logits.softmax(dim=-1 ).squeeze(-1 ) __SCREAMING_SNAKE_CASE = probs.tolist() if not isinstance(lowerCamelCase ,lowerCamelCase ): __SCREAMING_SNAKE_CASE = [scores] elif self.framework == "tf": __SCREAMING_SNAKE_CASE = stable_softmax(lowerCamelCase ,axis=-1 ) __SCREAMING_SNAKE_CASE = probs.numpy().tolist() else: raise ValueError(f"""Unsupported framework: {self.framework}""" ) __SCREAMING_SNAKE_CASE = [ {"""score""": score, """label""": candidate_label} for score, candidate_label in sorted(zip(lowerCamelCase ,lowerCamelCase ) ,key=lambda lowerCamelCase : -x[0] ) ] return result
13
0
'''simple docstring''' from math import isqrt def __magic_name__ ( __UpperCAmelCase ) -> List[str]: '''simple docstring''' return all(number % divisor != 0 for divisor in range(2 , isqrt(_snake_case ) + 1 ) ) def __magic_name__ ( __UpperCAmelCase = 10**6 ) -> Any: '''simple docstring''' __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 7 while prime_candidate < max_prime: primes_count += is_prime(_snake_case ) cube_index += 1 prime_candidate += 6 * cube_index return primes_count if __name__ == "__main__": print(F'''{solution() = }''')
710
'''simple docstring''' from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers a = [ "python", "tqdm", "regex", "requests", "packaging", "filelock", "numpy", "tokenizers", "huggingface-hub", "safetensors", "accelerate", "pyyaml", ] for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed elif pkg == "accelerate": # must be loaded here, or else tqdm check may fail from .utils import is_accelerate_available # Maybe switch to is_torch_available in the future here so that Accelerate is hard dep of # Transformers with PyTorch if not is_accelerate_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(F'''can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py''') def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase=None ) -> Optional[Any]: '''simple docstring''' require_version(deps[pkg] , __UpperCAmelCase )
13
0
'''simple docstring''' import hashlib import unittest from typing import Dict import numpy as np from transformers import ( MODEL_FOR_MASK_GENERATION_MAPPING, TF_MODEL_FOR_MASK_GENERATION_MAPPING, is_vision_available, pipeline, ) from transformers.pipelines import MaskGenerationPipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) if is_vision_available(): from PIL import Image else: class __a : @staticmethod def UpperCAmelCase__ ( *lowerCamelCase : Optional[int] ,**lowerCamelCase : Dict ): '''simple docstring''' pass def __magic_name__ ( __UpperCAmelCase ) -> List[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = hashlib.mda(image.tobytes() ) return m.hexdigest()[:10] def __magic_name__ ( __UpperCAmelCase ) -> Any: '''simple docstring''' __SCREAMING_SNAKE_CASE = np.array(_lowercase ) __SCREAMING_SNAKE_CASE = npimg.shape return {"hash": hashimage(_lowercase ), "shape": shape} @is_pipeline_test @require_vision @require_torch class __a ( unittest.TestCase ): __UpperCamelCase : int = dict( (list(MODEL_FOR_MASK_GENERATION_MAPPING.items() ) if MODEL_FOR_MASK_GENERATION_MAPPING else []) ) __UpperCamelCase : List[str] = dict( (list(TF_MODEL_FOR_MASK_GENERATION_MAPPING.items() ) if TF_MODEL_FOR_MASK_GENERATION_MAPPING else []) ) def UpperCAmelCase__ ( self : List[str] ,lowerCamelCase : Union[str, Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = MaskGenerationPipeline(model=UpperCamelCase__ ,image_processor=UpperCamelCase__ ) return image_segmenter, [ "./tests/fixtures/tests_samples/COCO/000000039769.png", "./tests/fixtures/tests_samples/COCO/000000039769.png", ] def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : Any ,lowerCamelCase : Optional[Any] ): '''simple docstring''' pass @require_tf @unittest.skip("""Image segmentation not implemented in TF""" ) def UpperCAmelCase__ ( self : int ): '''simple docstring''' pass @slow @require_torch def UpperCAmelCase__ ( self : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = pipeline("""mask-generation""" ,model="""facebook/sam-vit-huge""" ) __SCREAMING_SNAKE_CASE = image_segmenter("""http://images.cocodataset.org/val2017/000000039769.jpg""" ,points_per_batch=256 ) # Shortening by hashing __SCREAMING_SNAKE_CASE = [] for i, o in enumerate(outputs["""masks"""] ): new_outupt += [{"mask": mask_to_test_readable(UpperCamelCase__ ), "scores": outputs["scores"][i]}] # fmt: off self.assertEqual( nested_simplify(UpperCamelCase__ ,decimals=4 ) ,[ {"""mask""": {"""hash""": """115ad19f5f""", """shape""": (480, 640)}, """scores""": 1.0_444}, {"""mask""": {"""hash""": """6affa964c6""", """shape""": (480, 640)}, """scores""": 1.021}, {"""mask""": {"""hash""": """dfe28a0388""", """shape""": (480, 640)}, """scores""": 1.0_167}, {"""mask""": {"""hash""": """c0a5f4a318""", """shape""": (480, 640)}, """scores""": 1.0_132}, {"""mask""": {"""hash""": """fe8065c197""", """shape""": (480, 640)}, """scores""": 1.0_053}, {"""mask""": {"""hash""": """e2d0b7a0b7""", """shape""": (480, 640)}, """scores""": 0.9_967}, {"""mask""": {"""hash""": """453c7844bd""", """shape""": (480, 640)}, """scores""": 0.993}, {"""mask""": {"""hash""": """3d44f2926d""", """shape""": (480, 640)}, """scores""": 0.9_909}, {"""mask""": {"""hash""": """64033ddc3f""", """shape""": (480, 640)}, """scores""": 0.9_879}, {"""mask""": {"""hash""": """801064ff79""", """shape""": (480, 640)}, """scores""": 0.9_834}, {"""mask""": {"""hash""": """6172f276ef""", """shape""": (480, 640)}, """scores""": 0.9_716}, {"""mask""": {"""hash""": """b49e60e084""", """shape""": (480, 640)}, """scores""": 0.9_612}, {"""mask""": {"""hash""": """a811e775fd""", """shape""": (480, 640)}, """scores""": 0.9_599}, {"""mask""": {"""hash""": """a6a8ebcf4b""", """shape""": (480, 640)}, """scores""": 0.9_552}, {"""mask""": {"""hash""": """9d8257e080""", """shape""": (480, 640)}, """scores""": 0.9_532}, {"""mask""": {"""hash""": """32de6454a8""", """shape""": (480, 640)}, """scores""": 0.9_516}, {"""mask""": {"""hash""": """af3d4af2c8""", """shape""": (480, 640)}, """scores""": 0.9_499}, {"""mask""": {"""hash""": """3c6db475fb""", """shape""": (480, 640)}, """scores""": 0.9_483}, {"""mask""": {"""hash""": """c290813fb9""", """shape""": (480, 640)}, """scores""": 0.9_464}, {"""mask""": {"""hash""": """b6f0b8f606""", """shape""": (480, 640)}, """scores""": 0.943}, {"""mask""": {"""hash""": """92ce16bfdf""", """shape""": (480, 640)}, """scores""": 0.943}, {"""mask""": {"""hash""": """c749b25868""", """shape""": (480, 640)}, """scores""": 0.9_408}, {"""mask""": {"""hash""": """efb6cab859""", """shape""": (480, 640)}, """scores""": 0.9_335}, {"""mask""": {"""hash""": """1ff2eafb30""", """shape""": (480, 640)}, """scores""": 0.9_326}, {"""mask""": {"""hash""": """788b798e24""", """shape""": (480, 640)}, """scores""": 0.9_262}, {"""mask""": {"""hash""": """abea804f0e""", """shape""": (480, 640)}, """scores""": 0.8_999}, {"""mask""": {"""hash""": """7b9e8ddb73""", """shape""": (480, 640)}, """scores""": 0.8_986}, {"""mask""": {"""hash""": """cd24047c8a""", """shape""": (480, 640)}, """scores""": 0.8_984}, {"""mask""": {"""hash""": """6943e6bcbd""", """shape""": (480, 640)}, """scores""": 0.8_873}, {"""mask""": {"""hash""": """b5f47c9191""", """shape""": (480, 640)}, """scores""": 0.8_871} ] ,) # fmt: on @require_torch @slow def UpperCAmelCase__ ( self : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = '''facebook/sam-vit-huge''' __SCREAMING_SNAKE_CASE = pipeline("""mask-generation""" ,model=UpperCamelCase__ ) __SCREAMING_SNAKE_CASE = image_segmenter( """http://images.cocodataset.org/val2017/000000039769.jpg""" ,pred_iou_thresh=1 ,points_per_batch=256 ) # Shortening by hashing __SCREAMING_SNAKE_CASE = [] for i, o in enumerate(outputs["""masks"""] ): new_outupt += [{"mask": mask_to_test_readable(UpperCamelCase__ ), "scores": outputs["scores"][i]}] self.assertEqual( nested_simplify(UpperCamelCase__ ,decimals=4 ) ,[ {"""mask""": {"""hash""": """115ad19f5f""", """shape""": (480, 640)}, """scores""": 1.0_444}, {"""mask""": {"""hash""": """6affa964c6""", """shape""": (480, 640)}, """scores""": 1.0_210}, {"""mask""": {"""hash""": """dfe28a0388""", """shape""": (480, 640)}, """scores""": 1.0_167}, {"""mask""": {"""hash""": """c0a5f4a318""", """shape""": (480, 640)}, """scores""": 1.0_132}, {"""mask""": {"""hash""": """fe8065c197""", """shape""": (480, 640)}, """scores""": 1.0_053}, ] ,)
711
'''simple docstring''' import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import numpy as np import pandas as pd from datasets import load_dataset import transformers from transformers import ( AutoConfig, BartForSequenceClassification, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, TapexTokenizer, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.17.0.dev0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/text-classification/requirements.txt") a = logging.getLogger(__name__) @dataclass class __a : __UpperCamelCase : Optional[str] = field( default='tab_fact', metadata={'help': 'The name of the dataset to use (via the datasets library).'} ) __UpperCamelCase : Optional[str] = field( default='tab_fact', metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'}, ) __UpperCamelCase : int = field( default=1024, metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) }, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={'help': 'Overwrite the cached preprocessed datasets or not.'} ) __UpperCamelCase : bool = field( default=_snake_case, metadata={ 'help': ( 'Whether to pad all samples to `max_seq_length`. ' 'If False, will pad the samples dynamically when batching to the maximum length in the batch.' ) }, ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) }, ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) }, ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of prediction examples to this ' 'value if set.' ) }, ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'A csv or a json file containing the training data.'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'A csv or a json file containing the validation data.'} ) __UpperCamelCase : Optional[str] = field(default=_snake_case, metadata={'help': 'A csv or a json file containing the test data.'} ) def UpperCAmelCase__ ( self : int ): '''simple docstring''' if self.dataset_name is not None: pass elif self.train_file is None or self.validation_file is None: raise ValueError("""Need either a GLUE task, a training/validation file or a dataset name.""" ) else: __SCREAMING_SNAKE_CASE = self.train_file.split(""".""" )[-1] assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file." __SCREAMING_SNAKE_CASE = self.validation_file.split(""".""" )[-1] assert ( validation_extension == train_extension ), "`validation_file` should have the same extension (csv or json) as `train_file`." @dataclass class __a : __UpperCamelCase : str = field( default=_snake_case, metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'}, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={'help': 'Whether to use one of the fast tokenizer (backed by the tokenizers library) or not.'}, ) __UpperCamelCase : str = field( default='main', metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'}, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) }, ) def __magic_name__ ( ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = parser.parse_args_into_dataclasses() # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) __SCREAMING_SNAKE_CASE = training_args.get_process_log_level() logger.setLevel(__UpperCAmelCase ) datasets.utils.logging.set_verbosity(__UpperCAmelCase ) transformers.utils.logging.set_verbosity(__UpperCAmelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + f"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(f"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. __SCREAMING_SNAKE_CASE = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: __SCREAMING_SNAKE_CASE = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. """ """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON training and evaluation files (see below) # or specify a GLUE benchmark task (the dataset will be downloaded automatically from the datasets Hub). # # For JSON files, this script will use the `question` column for the input question and `table` column for the corresponding table. # # If the CSVs/JSONs contain only one non-label column, the script does single sentence classification on this # single column. You can easily tweak this behavior (see below) # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. __SCREAMING_SNAKE_CASE = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) else: # Loading a dataset from your local files. # CSV/JSON training and evaluation files are needed. __SCREAMING_SNAKE_CASE = {"""train""": data_args.train_file, """validation""": data_args.validation_file} # Get the test dataset: you can provide your own CSV/JSON test file (see below) # when you use `do_predict` without specifying a GLUE benchmark task. if training_args.do_predict: if data_args.test_file is not None: __SCREAMING_SNAKE_CASE = data_args.train_file.split(""".""" )[-1] __SCREAMING_SNAKE_CASE = data_args.test_file.split(""".""" )[-1] assert ( test_extension == train_extension ), "`test_file` should have the same extension (csv or json) as `train_file`." __SCREAMING_SNAKE_CASE = data_args.test_file else: raise ValueError("""Need either a GLUE task or a test file for `do_predict`.""" ) for key in data_files.keys(): logger.info(f"""load a local file for {key}: {data_files[key]}""" ) if data_args.train_file.endswith(""".csv""" ): # Loading a dataset from local csv files __SCREAMING_SNAKE_CASE = load_dataset("""csv""" , data_files=__UpperCAmelCase , cache_dir=model_args.cache_dir ) else: # Loading a dataset from local json files __SCREAMING_SNAKE_CASE = load_dataset("""json""" , data_files=__UpperCAmelCase , cache_dir=model_args.cache_dir ) # See more about loading any type of standard or custom dataset at # https://huggingface.co/docs/datasets/loading_datasets.html. # Labels __SCREAMING_SNAKE_CASE = raw_datasets["""train"""].features["""label"""].names __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) # Load pretrained model and tokenizer # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. __SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=__UpperCAmelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # load tapex tokenizer __SCREAMING_SNAKE_CASE = TapexTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , add_prefix_space=__UpperCAmelCase , ) __SCREAMING_SNAKE_CASE = BartForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=__UpperCAmelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Padding strategy if data_args.pad_to_max_length: __SCREAMING_SNAKE_CASE = """max_length""" else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch __SCREAMING_SNAKE_CASE = False # Some models have set the order of the labels to use, so let's make sure we do use it. __SCREAMING_SNAKE_CASE = {"""Refused""": 0, """Entailed""": 1} __SCREAMING_SNAKE_CASE = {0: """Refused""", 1: """Entailed"""} if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( f"""The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the""" f"""model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}.""" ) __SCREAMING_SNAKE_CASE = min(data_args.max_seq_length , tokenizer.model_max_length ) def preprocess_tabfact_function(__UpperCAmelCase ): # Tokenize the texts def _convert_table_text_to_pandas(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = [_table_row.split("""#""" ) for _table_row in _table_text.strip("""\n""" ).split("""\n""" )] __SCREAMING_SNAKE_CASE = pd.DataFrame.from_records(_table_content[1:] , columns=_table_content[0] ) return _table_pd __SCREAMING_SNAKE_CASE = examples["""statement"""] __SCREAMING_SNAKE_CASE = list(map(_convert_table_text_to_pandas , examples["""table_text"""] ) ) __SCREAMING_SNAKE_CASE = tokenizer(__UpperCAmelCase , __UpperCAmelCase , padding=__UpperCAmelCase , max_length=__UpperCAmelCase , truncation=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = examples["""label"""] return result with training_args.main_process_first(desc="""dataset map pre-processing""" ): __SCREAMING_SNAKE_CASE = raw_datasets.map( __UpperCAmelCase , batched=__UpperCAmelCase , load_from_cache_file=not data_args.overwrite_cache , desc="""Running tokenizer on dataset""" , ) if training_args.do_train: if "train" not in raw_datasets: raise ValueError("""--do_train requires a train dataset""" ) __SCREAMING_SNAKE_CASE = raw_datasets["""train"""] if data_args.max_train_samples is not None: __SCREAMING_SNAKE_CASE = train_dataset.select(range(data_args.max_train_samples ) ) if training_args.do_eval: if "validation" not in raw_datasets and "validation_matched" not in raw_datasets: raise ValueError("""--do_eval requires a validation dataset""" ) __SCREAMING_SNAKE_CASE = raw_datasets["""validation"""] if data_args.max_eval_samples is not None: __SCREAMING_SNAKE_CASE = eval_dataset.select(range(data_args.max_eval_samples ) ) if training_args.do_predict or data_args.test_file is not None: if "test" not in raw_datasets and "test_matched" not in raw_datasets: raise ValueError("""--do_predict requires a test dataset""" ) __SCREAMING_SNAKE_CASE = raw_datasets["""test"""] if data_args.max_predict_samples is not None: __SCREAMING_SNAKE_CASE = predict_dataset.select(range(data_args.max_predict_samples ) ) # Log a few random samples from the training set: if training_args.do_train: for index in random.sample(range(len(__UpperCAmelCase ) ) , 3 ): logger.info(f"""Sample {index} of the training set: {train_dataset[index]}.""" ) # You can define your custom compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = p.predictions[0] if isinstance(p.predictions , __UpperCAmelCase ) else p.predictions __SCREAMING_SNAKE_CASE = np.argmax(__UpperCAmelCase , axis=1 ) return {"accuracy": (preds == p.label_ids).astype(np.floataa ).mean().item()} # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: __SCREAMING_SNAKE_CASE = default_data_collator elif training_args.fpaa: __SCREAMING_SNAKE_CASE = DataCollatorWithPadding(__UpperCAmelCase , pad_to_multiple_of=8 ) else: __SCREAMING_SNAKE_CASE = None # Initialize our Trainer __SCREAMING_SNAKE_CASE = Trainer( model=__UpperCAmelCase , args=__UpperCAmelCase , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=__UpperCAmelCase , tokenizer=__UpperCAmelCase , data_collator=__UpperCAmelCase , ) # Training if training_args.do_train: __SCREAMING_SNAKE_CASE = None if training_args.resume_from_checkpoint is not None: __SCREAMING_SNAKE_CASE = training_args.resume_from_checkpoint elif last_checkpoint is not None: __SCREAMING_SNAKE_CASE = last_checkpoint __SCREAMING_SNAKE_CASE = trainer.train(resume_from_checkpoint=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = train_result.metrics __SCREAMING_SNAKE_CASE = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(__UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = min(__UpperCAmelCase , len(__UpperCAmelCase ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics("""train""" , __UpperCAmelCase ) trainer.save_metrics("""train""" , __UpperCAmelCase ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info("""*** Evaluate ***""" ) __SCREAMING_SNAKE_CASE = trainer.evaluate(eval_dataset=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = min(__UpperCAmelCase , len(__UpperCAmelCase ) ) trainer.log_metrics("""eval""" , __UpperCAmelCase ) trainer.save_metrics("""eval""" , __UpperCAmelCase ) if training_args.do_predict: logger.info("""*** Predict ***""" ) # Removing the `label` columns because it contains -1 and Trainer won't like that. __SCREAMING_SNAKE_CASE = predict_dataset.remove_columns("""label""" ) __SCREAMING_SNAKE_CASE = trainer.predict(__UpperCAmelCase , metric_key_prefix="""predict""" ).predictions __SCREAMING_SNAKE_CASE = np.argmax(__UpperCAmelCase , axis=1 ) __SCREAMING_SNAKE_CASE = os.path.join(training_args.output_dir , """predict_results_tabfact.txt""" ) if trainer.is_world_process_zero(): with open(__UpperCAmelCase , """w""" ) as writer: logger.info("""***** Predict Results *****""" ) writer.write("""index\tprediction\n""" ) for index, item in enumerate(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = label_list[item] writer.write(f"""{index}\t{item}\n""" ) __SCREAMING_SNAKE_CASE = {"""finetuned_from""": model_args.model_name_or_path, """tasks""": """text-classification"""} if training_args.push_to_hub: trainer.push_to_hub(**__UpperCAmelCase ) else: trainer.create_model_card(**__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase ) -> Any: '''simple docstring''' main() if __name__ == "__main__": main()
13
0
'''simple docstring''' from __future__ import annotations import inspect import unittest import numpy as np from transformers import DeiTConfig 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 ( TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, ) from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DeiTImageProcessor class __a : def __init__( self : Union[str, Any] ,lowerCamelCase : Dict ,lowerCamelCase : Optional[int]=13 ,lowerCamelCase : List[Any]=30 ,lowerCamelCase : Tuple=2 ,lowerCamelCase : Tuple=3 ,lowerCamelCase : str=True ,lowerCamelCase : str=True ,lowerCamelCase : Tuple=32 ,lowerCamelCase : Dict=2 ,lowerCamelCase : Dict=4 ,lowerCamelCase : Tuple=37 ,lowerCamelCase : Dict="gelu" ,lowerCamelCase : List[Any]=0.1 ,lowerCamelCase : Optional[Any]=0.1 ,lowerCamelCase : List[Any]=10 ,lowerCamelCase : str=0.02 ,lowerCamelCase : List[Any]=3 ,lowerCamelCase : List[str]=None ,lowerCamelCase : Tuple=2 ,): '''simple docstring''' __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 DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens) __SCREAMING_SNAKE_CASE = (image_size // patch_size) ** 2 __SCREAMING_SNAKE_CASE = num_patches + 2 def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __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 UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' return DeiTConfig( 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=_SCREAMING_SNAKE_CASE ,initializer_range=self.initializer_range ,encoder_stride=self.encoder_stride ,) def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : str ,lowerCamelCase : str ,lowerCamelCase : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TFDeiTModel(config=_SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase__ ( self : Any ,lowerCamelCase : Any ,lowerCamelCase : Dict ,lowerCamelCase : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TFDeiTForMaskedImageModeling(config=_SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = model(_SCREAMING_SNAKE_CASE ) 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 = TFDeiTForMaskedImageModeling(_SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) __SCREAMING_SNAKE_CASE = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.reconstruction.shape ,(self.batch_size, 1, self.image_size, self.image_size) ) def UpperCAmelCase__ ( self : Optional[Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : Any ,lowerCamelCase : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.type_sequence_label_size __SCREAMING_SNAKE_CASE = TFDeiTForImageClassification(_SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = model(_SCREAMING_SNAKE_CASE ,labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) # test greyscale images __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = TFDeiTForImageClassification(_SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) __SCREAMING_SNAKE_CASE = model(_SCREAMING_SNAKE_CASE ,labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' __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 __a ( _snake_case, _snake_case, unittest.TestCase ): __UpperCamelCase : Union[str, Any] = ( ( TFDeiTModel, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, ) if is_tf_available() else () ) __UpperCamelCase : Optional[int] = ( { 'feature-extraction': TFDeiTModel, 'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher), } if is_tf_available() else {} ) __UpperCamelCase : Union[str, Any] = False __UpperCamelCase : List[str] = False __UpperCamelCase : str = False __UpperCamelCase : Tuple = False def UpperCAmelCase__ ( self : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TFDeiTModelTester(self ) __SCREAMING_SNAKE_CASE = ConfigTester(self ,config_class=_SCREAMING_SNAKE_CASE ,has_text_modality=_SCREAMING_SNAKE_CASE ,hidden_size=37 ) def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="""DeiT does not use inputs_embeds""" ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' pass def UpperCAmelCase__ ( self : int ): '''simple docstring''' __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(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(model.get_input_embeddings() ,(tf.keras.layers.Layer) ) __SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE ,tf.keras.layers.Dense ) ) def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __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(_SCREAMING_SNAKE_CASE ) __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] ,_SCREAMING_SNAKE_CASE ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*_SCREAMING_SNAKE_CASE ) def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE ) def UpperCAmelCase__ ( self : Optional[Any] ,lowerCamelCase : List[str] ,lowerCamelCase : int ,lowerCamelCase : Dict=False ): '''simple docstring''' __SCREAMING_SNAKE_CASE = super()._prepare_for_class(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,return_labels=_SCREAMING_SNAKE_CASE ) if return_labels: if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call ).parameters: del inputs_dict["labels"] return inputs_dict @slow def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __SCREAMING_SNAKE_CASE = TFDeiTModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def __magic_name__ ( ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf @require_vision class __a ( unittest.TestCase ): @cached_property def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' return ( DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224""" ) if is_vision_available() else None ) @slow def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TFDeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224""" ) __SCREAMING_SNAKE_CASE = self.default_image_processor __SCREAMING_SNAKE_CASE = prepare_img() __SCREAMING_SNAKE_CASE = image_processor(images=_SCREAMING_SNAKE_CASE ,return_tensors="""tf""" ) # forward pass __SCREAMING_SNAKE_CASE = model(**_SCREAMING_SNAKE_CASE ) # verify the logits __SCREAMING_SNAKE_CASE = tf.TensorShape((1, 1000) ) self.assertEqual(outputs.logits.shape ,_SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = tf.constant([-1.0_266, 0.1_912, -1.2_861] ) self.assertTrue(np.allclose(outputs.logits[0, :3] ,_SCREAMING_SNAKE_CASE ,atol=1E-4 ) )
712
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_flax_available, is_torch_available, is_transformers_available, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .multicontrolnet import MultiControlNetModel from .pipeline_controlnet import StableDiffusionControlNetPipeline from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline if is_transformers_available() and is_flax_available(): from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline
13
0
'''simple docstring''' import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class __a ( UpperCAmelCase__, UpperCAmelCase__ ): @register_to_config def __init__( self : Dict ,*, lowerCamelCase : int = 4 ,lowerCamelCase : int = 768 ,lowerCamelCase : int ,lowerCamelCase : Optional[int] ,): '''simple docstring''' super().__init__() __SCREAMING_SNAKE_CASE = nn.Parameter(torch.zeros(lowerCamelCase ) ) # parameters for additional clip time embeddings __SCREAMING_SNAKE_CASE = nn.Linear(lowerCamelCase ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = nn.Linear(lowerCamelCase ,lowerCamelCase ) # parameters for encoder hidden states __SCREAMING_SNAKE_CASE = clip_extra_context_tokens __SCREAMING_SNAKE_CASE = nn.Linear( lowerCamelCase ,self.clip_extra_context_tokens * cross_attention_dim ) __SCREAMING_SNAKE_CASE = nn.Linear(lowerCamelCase ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = nn.LayerNorm(lowerCamelCase ) def UpperCAmelCase__ ( self : Union[str, Any] ,*, lowerCamelCase : Union[str, Any] ,lowerCamelCase : List[str] ,lowerCamelCase : Dict ,lowerCamelCase : Dict ): '''simple docstring''' if do_classifier_free_guidance: # Add the classifier free guidance embeddings to the image embeddings __SCREAMING_SNAKE_CASE = image_embeddings.shape[0] __SCREAMING_SNAKE_CASE = self.learned_classifier_free_guidance_embeddings.unsqueeze(0 ) __SCREAMING_SNAKE_CASE = classifier_free_guidance_embeddings.expand( lowerCamelCase ,-1 ) __SCREAMING_SNAKE_CASE = torch.cat([classifier_free_guidance_embeddings, image_embeddings] ,dim=0 ) # The image embeddings batch size and the text embeddings batch size are equal assert image_embeddings.shape[0] == prompt_embeds.shape[0] __SCREAMING_SNAKE_CASE = prompt_embeds.shape[0] # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and # adding CLIP embeddings to the existing timestep embedding, ... __SCREAMING_SNAKE_CASE = self.embedding_proj(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.clip_image_embeddings_project_to_time_embeddings(lowerCamelCase ) __SCREAMING_SNAKE_CASE = time_projected_image_embeddings + time_projected_prompt_embeds # ... and by projecting CLIP embeddings into four # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder" __SCREAMING_SNAKE_CASE = self.clip_extra_context_tokens_proj(lowerCamelCase ) __SCREAMING_SNAKE_CASE = clip_extra_context_tokens.reshape(lowerCamelCase ,-1 ,self.clip_extra_context_tokens ) __SCREAMING_SNAKE_CASE = clip_extra_context_tokens.permute(0 ,2 ,1 ) __SCREAMING_SNAKE_CASE = self.encoder_hidden_states_proj(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.text_encoder_hidden_states_norm(lowerCamelCase ) __SCREAMING_SNAKE_CASE = torch.cat([clip_extra_context_tokens, text_encoder_hidden_states] ,dim=1 ) return text_encoder_hidden_states, additive_clip_time_embeddings
713
'''simple docstring''' import requests from bsa import BeautifulSoup def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = BeautifulSoup(requests.get(__UpperCAmelCase , params=__UpperCAmelCase ).content , """html.parser""" ) __SCREAMING_SNAKE_CASE = soup.find("""div""" , attrs={"""class""": """gs_ri"""} ) __SCREAMING_SNAKE_CASE = div.find("""div""" , attrs={"""class""": """gs_fl"""} ).find_all("""a""" ) return anchors[2].get_text() if __name__ == "__main__": a = { "title": ( "Precisely geometry controlled microsupercapacitors for ultrahigh areal " "capacitance, volumetric capacitance, and energy density" ), "journal": "Chem. Mater.", "volume": 30, "pages": "3979-3990", "year": 2018, "hl": "en", } print(get_citation("https://scholar.google.com/scholar_lookup", params=params))
13
0
from collections import defaultdict from graphs.minimum_spanning_tree_prims import prisms_algorithm as mst def __magic_name__ ( ) -> List[str]: '''simple docstring''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 9, 14 # noqa: F841 __SCREAMING_SNAKE_CASE = [ [0, 1, 4], [0, 7, 8], [1, 2, 8], [7, 8, 7], [7, 6, 1], [2, 8, 2], [8, 6, 6], [2, 3, 7], [2, 5, 4], [6, 5, 2], [3, 5, 14], [3, 4, 9], [5, 4, 10], [1, 7, 11], ] __SCREAMING_SNAKE_CASE = defaultdict(SCREAMING_SNAKE_CASE_ ) for nodea, nodea, cost in edges: adjancency[nodea].append([nodea, cost] ) adjancency[nodea].append([nodea, cost] ) __SCREAMING_SNAKE_CASE = mst(SCREAMING_SNAKE_CASE_ ) __SCREAMING_SNAKE_CASE = [ [7, 6, 1], [2, 8, 2], [6, 5, 2], [0, 1, 4], [2, 5, 4], [2, 3, 7], [0, 7, 8], [3, 4, 9], ] for answer in expected: __SCREAMING_SNAKE_CASE = tuple(answer[:2] ) __SCREAMING_SNAKE_CASE = tuple(edge[::-1] ) assert edge in result or reverse in result
714
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a = logging.get_logger(__name__) a = { "camembert-base": "https://huggingface.co/camembert-base/resolve/main/config.json", "umberto-commoncrawl-cased-v1": ( "https://huggingface.co/Musixmatch/umberto-commoncrawl-cased-v1/resolve/main/config.json" ), "umberto-wikipedia-uncased-v1": ( "https://huggingface.co/Musixmatch/umberto-wikipedia-uncased-v1/resolve/main/config.json" ), } class __a ( _snake_case ): __UpperCamelCase : Tuple = 'camembert' def __init__( self : int ,lowerCamelCase : List[Any]=3_0522 ,lowerCamelCase : List[Any]=768 ,lowerCamelCase : str=12 ,lowerCamelCase : List[str]=12 ,lowerCamelCase : Optional[Any]=3072 ,lowerCamelCase : Tuple="gelu" ,lowerCamelCase : List[str]=0.1 ,lowerCamelCase : Tuple=0.1 ,lowerCamelCase : Union[str, Any]=512 ,lowerCamelCase : Dict=2 ,lowerCamelCase : Tuple=0.02 ,lowerCamelCase : List[Any]=1E-1_2 ,lowerCamelCase : Union[str, Any]=1 ,lowerCamelCase : Optional[Any]=0 ,lowerCamelCase : List[Any]=2 ,lowerCamelCase : List[str]="absolute" ,lowerCamelCase : int=True ,lowerCamelCase : Any=None ,**lowerCamelCase : Optional[Any] ,): '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase ,bos_token_id=lowerCamelCase ,eos_token_id=lowerCamelCase ,**lowerCamelCase ) __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 = position_embedding_type __SCREAMING_SNAKE_CASE = use_cache __SCREAMING_SNAKE_CASE = classifier_dropout class __a ( _snake_case ): @property def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' 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), ] )
13
0
from __future__ import annotations import requests a = 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 __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase = 1 , __UpperCAmelCase = "new" , __UpperCAmelCase = None ) -> dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = wanted_data or [] if invalid_search_terms := ", ".join(sorted(set(_lowerCAmelCase ) - valid_terms ) ): __SCREAMING_SNAKE_CASE = f"""Invalid search term: {invalid_search_terms}""" raise ValueError(_lowerCAmelCase ) __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 == 429: raise requests.HTTPError __SCREAMING_SNAKE_CASE = response.json() if not wanted_data: return {id_: data["data"]["children"][id_] for id_ in range(_lowerCAmelCase )} __SCREAMING_SNAKE_CASE = {} for id_ in range(_lowerCAmelCase ): __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"]))
715
'''simple docstring''' import inspect import unittest import numpy as np from transformers import ViTConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor if is_flax_available(): import jax from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel class __a ( unittest.TestCase ): def __init__( self : Optional[int] ,lowerCamelCase : str ,lowerCamelCase : List[str]=13 ,lowerCamelCase : Optional[Any]=30 ,lowerCamelCase : Dict=2 ,lowerCamelCase : List[Any]=3 ,lowerCamelCase : List[str]=True ,lowerCamelCase : str=True ,lowerCamelCase : Optional[int]=32 ,lowerCamelCase : Dict=5 ,lowerCamelCase : Optional[int]=4 ,lowerCamelCase : List[Any]=37 ,lowerCamelCase : Union[str, Any]="gelu" ,lowerCamelCase : List[Any]=0.1 ,lowerCamelCase : Any=0.1 ,lowerCamelCase : str=10 ,lowerCamelCase : Dict=0.02 ,): '''simple docstring''' __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 # 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 UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __SCREAMING_SNAKE_CASE = 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 ,) return config, pixel_values def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : int ,lowerCamelCase : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = FlaxViTModel(config=lowerCamelCase ) __SCREAMING_SNAKE_CASE = model(lowerCamelCase ) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) __SCREAMING_SNAKE_CASE = (self.image_size, self.image_size) __SCREAMING_SNAKE_CASE = (self.patch_size, self.patch_size) __SCREAMING_SNAKE_CASE = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, num_patches + 1, self.hidden_size) ) def UpperCAmelCase__ ( self : Union[str, Any] ,lowerCamelCase : Optional[int] ,lowerCamelCase : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.type_sequence_label_size __SCREAMING_SNAKE_CASE = FlaxViTForImageClassification(config=lowerCamelCase ) __SCREAMING_SNAKE_CASE = model(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 = FlaxViTForImageClassification(lowerCamelCase ) __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) __SCREAMING_SNAKE_CASE = model(lowerCamelCase ) def UpperCAmelCase__ ( self : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ) = config_and_inputs __SCREAMING_SNAKE_CASE = {"""pixel_values""": pixel_values} return config, inputs_dict @require_flax class __a ( _snake_case, unittest.TestCase ): __UpperCamelCase : Any = (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else () def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = FlaxViTModelTester(self ) __SCREAMING_SNAKE_CASE = ConfigTester(self ,config_class=lowerCamelCase ,has_text_modality=lowerCamelCase ,hidden_size=37 ) def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __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 UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __SCREAMING_SNAKE_CASE = self._prepare_for_class(lowerCamelCase ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = model_class(lowerCamelCase ) @jax.jit def model_jitted(lowerCamelCase : int ,**lowerCamelCase : Union[str, Any] ): return model(pixel_values=lowerCamelCase ,**lowerCamelCase ) with self.subTest("""JIT Enabled""" ): __SCREAMING_SNAKE_CASE = model_jitted(**lowerCamelCase ).to_tuple() with self.subTest("""JIT Disabled""" ): with jax.disable_jit(): __SCREAMING_SNAKE_CASE = model_jitted(**lowerCamelCase ).to_tuple() self.assertEqual(len(lowerCamelCase ) ,len(lowerCamelCase ) ) for jitted_output, output in zip(lowerCamelCase ,lowerCamelCase ): self.assertEqual(jitted_output.shape ,output.shape ) @slow def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' for model_class_name in self.all_model_classes: __SCREAMING_SNAKE_CASE = model_class_name.from_pretrained("""google/vit-base-patch16-224""" ) __SCREAMING_SNAKE_CASE = model(np.ones((1, 3, 224, 224) ) ) self.assertIsNotNone(lowerCamelCase )
13
0
'''simple docstring''' from typing import TYPE_CHECKING import torch from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class __a ( UpperCamelCase__ ): __UpperCamelCase : int = """dandelin/vilt-b32-finetuned-vqa""" __UpperCamelCase : Tuple = ( """This is a tool that answers a question about an image. It takes an input named `image` which should be the """ """image containing the information, as well as a `question` which should be the question in English. It """ """returns a text that is the answer to the question.""" ) __UpperCamelCase : Optional[Any] = """image_qa""" __UpperCamelCase : Tuple = AutoProcessor __UpperCamelCase : Optional[Any] = AutoModelForVisualQuestionAnswering __UpperCamelCase : Union[str, Any] = ["""image""", """text"""] __UpperCamelCase : Union[str, Any] = ["""text"""] def __init__( self : List[Any] ,*lowerCamelCase : int ,**lowerCamelCase : Any ): '''simple docstring''' requires_backends(self ,["""vision"""] ) super().__init__(*lowerCamelCase ,**lowerCamelCase ) def UpperCAmelCase__ ( self : str ,lowerCamelCase : List[Any] ,lowerCamelCase : Tuple ): '''simple docstring''' return self.pre_processor(lowerCamelCase ,lowerCamelCase ,return_tensors="""pt""" ) def UpperCAmelCase__ ( self : str ,lowerCamelCase : Optional[int] ): '''simple docstring''' with torch.no_grad(): return self.model(**lowerCamelCase ).logits def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE : Union[str, Any] = outputs.argmax(-1 ).item() return self.model.config.idalabel[idx]
716
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging a = logging.get_logger(__name__) a = { "asapp/sew-tiny-100k": "https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json", # See all SEW models at https://huggingface.co/models?filter=sew } class __a ( _snake_case ): __UpperCamelCase : Tuple = 'sew' def __init__( self : str ,lowerCamelCase : Any=32 ,lowerCamelCase : str=768 ,lowerCamelCase : str=12 ,lowerCamelCase : Union[str, Any]=12 ,lowerCamelCase : Union[str, Any]=3072 ,lowerCamelCase : int=2 ,lowerCamelCase : Union[str, Any]="gelu" ,lowerCamelCase : Tuple=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : Any=0.0 ,lowerCamelCase : Optional[Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : Optional[Any]=0.02 ,lowerCamelCase : List[str]=1E-5 ,lowerCamelCase : Tuple="group" ,lowerCamelCase : Optional[Any]="gelu" ,lowerCamelCase : List[str]=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) ,lowerCamelCase : Any=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) ,lowerCamelCase : Dict=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) ,lowerCamelCase : Optional[int]=False ,lowerCamelCase : Dict=128 ,lowerCamelCase : Union[str, Any]=16 ,lowerCamelCase : List[Any]=True ,lowerCamelCase : List[Any]=0.05 ,lowerCamelCase : Optional[int]=10 ,lowerCamelCase : Any=2 ,lowerCamelCase : Any=0.0 ,lowerCamelCase : Tuple=10 ,lowerCamelCase : str=0 ,lowerCamelCase : Tuple="mean" ,lowerCamelCase : int=False ,lowerCamelCase : Dict=False ,lowerCamelCase : Optional[int]=256 ,lowerCamelCase : str=0 ,lowerCamelCase : Tuple=1 ,lowerCamelCase : Tuple=2 ,**lowerCamelCase : Union[str, Any] ,): '''simple docstring''' super().__init__(**lowerCamelCase ,pad_token_id=lowerCamelCase ,bos_token_id=lowerCamelCase ,eos_token_id=lowerCamelCase ) __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = feat_extract_norm __SCREAMING_SNAKE_CASE = feat_extract_activation __SCREAMING_SNAKE_CASE = list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = conv_bias __SCREAMING_SNAKE_CASE = num_conv_pos_embeddings __SCREAMING_SNAKE_CASE = num_conv_pos_embedding_groups __SCREAMING_SNAKE_CASE = len(self.conv_dim ) __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = squeeze_factor __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = hidden_dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = feat_proj_dropout __SCREAMING_SNAKE_CASE = final_dropout __SCREAMING_SNAKE_CASE = layerdrop __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect.""" """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,""" f"""but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)""" f"""= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __SCREAMING_SNAKE_CASE = apply_spec_augment __SCREAMING_SNAKE_CASE = mask_time_prob __SCREAMING_SNAKE_CASE = mask_time_length __SCREAMING_SNAKE_CASE = mask_time_min_masks __SCREAMING_SNAKE_CASE = mask_feature_prob __SCREAMING_SNAKE_CASE = mask_feature_length __SCREAMING_SNAKE_CASE = mask_feature_min_masks # ctc loss __SCREAMING_SNAKE_CASE = ctc_loss_reduction __SCREAMING_SNAKE_CASE = ctc_zero_infinity # sequence classification __SCREAMING_SNAKE_CASE = use_weighted_layer_sum __SCREAMING_SNAKE_CASE = classifier_proj_size @property def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' return functools.reduce(operator.mul ,self.conv_stride ,1 )
13
0
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging a = logging.get_logger(__name__) a = { "asapp/sew-d-tiny-100k": "https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json", # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __a ( UpperCAmelCase__ ): __UpperCamelCase : List[str] = "sew-d" def __init__( self : Union[str, Any] ,lowerCamelCase : Optional[int]=32 ,lowerCamelCase : Any=768 ,lowerCamelCase : int=12 ,lowerCamelCase : str=12 ,lowerCamelCase : List[str]=3072 ,lowerCamelCase : Any=2 ,lowerCamelCase : List[str]=512 ,lowerCamelCase : List[str]=256 ,lowerCamelCase : List[str]=True ,lowerCamelCase : int=True ,lowerCamelCase : Tuple=("p2c", "c2p") ,lowerCamelCase : List[Any]="layer_norm" ,lowerCamelCase : Optional[int]="gelu_python" ,lowerCamelCase : List[Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : List[Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.0 ,lowerCamelCase : Tuple=0.1 ,lowerCamelCase : Tuple=0.02 ,lowerCamelCase : Any=1E-7 ,lowerCamelCase : Dict=1E-5 ,lowerCamelCase : List[Any]="group" ,lowerCamelCase : Tuple="gelu" ,lowerCamelCase : List[Any]=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) ,lowerCamelCase : List[Any]=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) ,lowerCamelCase : Tuple=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) ,lowerCamelCase : Tuple=False ,lowerCamelCase : Tuple=128 ,lowerCamelCase : Dict=16 ,lowerCamelCase : int=True ,lowerCamelCase : List[Any]=0.05 ,lowerCamelCase : Union[str, Any]=10 ,lowerCamelCase : Optional[int]=2 ,lowerCamelCase : Tuple=0.0 ,lowerCamelCase : Any=10 ,lowerCamelCase : Optional[int]=0 ,lowerCamelCase : Dict="mean" ,lowerCamelCase : List[Any]=False ,lowerCamelCase : int=False ,lowerCamelCase : Tuple=256 ,lowerCamelCase : Dict=0 ,lowerCamelCase : List[str]=1 ,lowerCamelCase : List[str]=2 ,**lowerCamelCase : Union[str, Any] ,): '''simple docstring''' super().__init__(**lowerCamelCase__ ,pad_token_id=lowerCamelCase__ ,bos_token_id=lowerCamelCase__ ,eos_token_id=lowerCamelCase__ ) __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = feat_extract_norm __SCREAMING_SNAKE_CASE = feat_extract_activation __SCREAMING_SNAKE_CASE = list(lowerCamelCase__ ) __SCREAMING_SNAKE_CASE = list(lowerCamelCase__ ) __SCREAMING_SNAKE_CASE = list(lowerCamelCase__ ) __SCREAMING_SNAKE_CASE = conv_bias __SCREAMING_SNAKE_CASE = num_conv_pos_embeddings __SCREAMING_SNAKE_CASE = num_conv_pos_embedding_groups __SCREAMING_SNAKE_CASE = len(self.conv_dim ) __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = squeeze_factor __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = position_buckets __SCREAMING_SNAKE_CASE = share_att_key __SCREAMING_SNAKE_CASE = relative_attention __SCREAMING_SNAKE_CASE = norm_rel_ebd __SCREAMING_SNAKE_CASE = list(lowerCamelCase__ ) __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = hidden_dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = feat_proj_dropout __SCREAMING_SNAKE_CASE = final_dropout __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = feature_layer_norm_eps __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect.""" """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,""" f"""but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)""" f"""= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __SCREAMING_SNAKE_CASE = apply_spec_augment __SCREAMING_SNAKE_CASE = mask_time_prob __SCREAMING_SNAKE_CASE = mask_time_length __SCREAMING_SNAKE_CASE = mask_time_min_masks __SCREAMING_SNAKE_CASE = mask_feature_prob __SCREAMING_SNAKE_CASE = mask_feature_length __SCREAMING_SNAKE_CASE = mask_feature_min_masks # ctc loss __SCREAMING_SNAKE_CASE = ctc_loss_reduction __SCREAMING_SNAKE_CASE = ctc_zero_infinity # sequence classification __SCREAMING_SNAKE_CASE = use_weighted_layer_sum __SCREAMING_SNAKE_CASE = classifier_proj_size @property def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' return functools.reduce(operator.mul ,self.conv_stride ,1 )
717
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase = 1 , __UpperCAmelCase = 1000 ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 0 for divide_by_number in range(__UpperCAmelCase , digit + 1 ): __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = numerator for _ in range(1 , digit + 1 ): if now_divide in has_been_divided: if longest_list_length < len(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = divide_by_number else: has_been_divided.append(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = now_divide * 10 % divide_by_number return the_digit # Tests if __name__ == "__main__": import doctest doctest.testmod()
13
0
'''simple docstring''' import json import os import tempfile import datasets from utils import generate_example_dataset, get_duration a = 50000 a = 5000 a , a = os.path.split(__file__) a = os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json")) @get_duration def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> str: '''simple docstring''' for i in range(lowercase__ ): __SCREAMING_SNAKE_CASE = dataset[i] @get_duration def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> List[Any]: '''simple docstring''' for i in range(0 , len(lowercase__ ) , lowercase__ ): __SCREAMING_SNAKE_CASE = dataset[i : i + batch_size] @get_duration def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Any: '''simple docstring''' with dataset.formatted_as(type=lowercase__ ): for i in range(lowercase__ ): __SCREAMING_SNAKE_CASE = dataset[i] @get_duration def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' with dataset.formatted_as(type=lowercase__ ): for i in range(0 , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE = dataset[i : i + batch_size] def __magic_name__ ( ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = {"""num examples""": SPEED_TEST_N_EXAMPLES} __SCREAMING_SNAKE_CASE = [ (read, {"""length""": SMALL_TEST}), (read, {"""length""": SPEED_TEST_N_EXAMPLES}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 10}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 100}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 1000}), (read_formatted, {"""type""": """numpy""", """length""": SMALL_TEST}), (read_formatted, {"""type""": """pandas""", """length""": SMALL_TEST}), (read_formatted, {"""type""": """torch""", """length""": SMALL_TEST}), (read_formatted, {"""type""": """tensorflow""", """length""": SMALL_TEST}), (read_formatted_batch, {"""type""": """numpy""", """length""": SMALL_TEST, """batch_size""": 10}), (read_formatted_batch, {"""type""": """numpy""", """length""": SMALL_TEST, """batch_size""": 1000}), ] __SCREAMING_SNAKE_CASE = [ (read, {"""length""": SMALL_TEST}), (read, {"""length""": SPEED_TEST_N_EXAMPLES}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 10}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 100}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 1000}), (read_formatted, {"""type""": """numpy""", """length""": SMALL_TEST}), (read_formatted_batch, {"""type""": """numpy""", """length""": SMALL_TEST, """batch_size""": 10}), (read_formatted_batch, {"""type""": """numpy""", """length""": SMALL_TEST, """batch_size""": 1000}), ] with tempfile.TemporaryDirectory() as tmp_dir: print("""generating dataset""" ) __SCREAMING_SNAKE_CASE = datasets.Features( {"""list""": datasets.Sequence(datasets.Value("""float32""" ) ), """numbers""": datasets.Value("""float32""" )} ) __SCREAMING_SNAKE_CASE = generate_example_dataset( os.path.join(lowercase__ , """dataset.arrow""" ) , lowercase__ , num_examples=lowercase__ , seq_shapes={"""list""": (100,)} , ) print("""first set of iterations""" ) for func, kwargs in functions: print(func.__name__ , str(lowercase__ ) ) __SCREAMING_SNAKE_CASE = func(lowercase__ , **lowercase__ ) print("""shuffling dataset""" ) __SCREAMING_SNAKE_CASE = dataset.shuffle() print("""Second set of iterations (after shuffling""" ) for func, kwargs in functions_shuffled: print("""shuffled """ , func.__name__ , str(lowercase__ ) ) __SCREAMING_SNAKE_CASE = func( lowercase__ , **lowercase__ ) with open(lowercase__ , """wb""" ) as f: f.write(json.dumps(lowercase__ ).encode("""utf-8""" ) ) if __name__ == "__main__": # useful to run the profiler benchmark_iterating()
718
'''simple docstring''' 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 if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor class __a ( unittest.TestCase ): def __init__( self : List[Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : List[str]=7 ,lowerCamelCase : List[str]=3 ,lowerCamelCase : List[str]=18 ,lowerCamelCase : Any=30 ,lowerCamelCase : Optional[Any]=400 ,lowerCamelCase : Optional[Any]=True ,lowerCamelCase : Optional[Any]=None ,lowerCamelCase : Optional[int]=True ,lowerCamelCase : int=None ,lowerCamelCase : str=True ,lowerCamelCase : Dict=[0.48_145_466, 0.4_578_275, 0.40_821_073] ,lowerCamelCase : List[str]=[0.26_862_954, 0.26_130_258, 0.27_577_711] ,lowerCamelCase : Tuple=True ,): '''simple docstring''' __SCREAMING_SNAKE_CASE = size if size is not None else {"""height""": 224, """width""": 224} __SCREAMING_SNAKE_CASE = crop_size if crop_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 = do_center_crop __SCREAMING_SNAKE_CASE = crop_size __SCREAMING_SNAKE_CASE = do_normalize __SCREAMING_SNAKE_CASE = image_mean __SCREAMING_SNAKE_CASE = image_std __SCREAMING_SNAKE_CASE = do_convert_rgb def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_convert_rgb": self.do_convert_rgb, } def UpperCAmelCase__ ( self : int ,lowerCamelCase : Union[str, Any]=False ,lowerCamelCase : str=False ,lowerCamelCase : str=False ): '''simple docstring''' assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time" if equal_resolution: __SCREAMING_SNAKE_CASE = [] for i in range(self.batch_size ): image_inputs.append( np.random.randint( 255 ,size=(self.num_channels, self.max_resolution, self.max_resolution) ,dtype=np.uinta ) ) else: __SCREAMING_SNAKE_CASE = [] for i in range(self.batch_size ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = np.random.choice(np.arange(self.min_resolution ,self.max_resolution ) ,2 ) image_inputs.append(np.random.randint(255 ,size=(self.num_channels, width, height) ,dtype=np.uinta ) ) if not numpify and not torchify: # PIL expects the channel dimension as last dimension __SCREAMING_SNAKE_CASE = [Image.fromarray(np.moveaxis(lowerCamelCase ,0 ,-1 ) ) for x in image_inputs] if torchify: __SCREAMING_SNAKE_CASE = [torch.from_numpy(lowerCamelCase ) for x in image_inputs] return image_inputs @require_torch @require_vision class __a ( _snake_case, unittest.TestCase ): __UpperCamelCase : int = ChineseCLIPImageProcessor if is_vision_available() else None def UpperCAmelCase__ ( self : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = ChineseCLIPImageProcessingTester(self ,do_center_crop=lowerCamelCase ) @property def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __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 ,"""do_center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_normalize""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_mean""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_std""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_convert_rgb""" ) ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{"""height""": 224, """width""": 224} ) self.assertEqual(image_processor.crop_size ,{"""height""": 18, """width""": 18} ) __SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ,crop_size=84 ) self.assertEqual(image_processor.size ,{"""shortest_edge""": 42} ) self.assertEqual(image_processor.crop_size ,{"""height""": 84, """width""": 84} ) def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' pass def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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""" ).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 __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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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.crop_size["""height"""], self.image_processor_tester.crop_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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) def UpperCAmelCase__ ( self : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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.crop_size["""height"""], self.image_processor_tester.crop_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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,) @require_torch @require_vision class __a ( _snake_case, unittest.TestCase ): __UpperCamelCase : Optional[int] = ChineseCLIPImageProcessor if is_vision_available() else None def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = ChineseCLIPImageProcessingTester(self ,num_channels=4 ,do_center_crop=lowerCamelCase ) __SCREAMING_SNAKE_CASE = 3 @property def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase__ ( self : int ): '''simple docstring''' __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 ,"""do_center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""center_crop""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_normalize""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_mean""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""image_std""" ) ) self.assertTrue(hasattr(lowerCamelCase ,"""do_convert_rgb""" ) ) def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' pass def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_inputs(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""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_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.expected_encoded_image_num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) ,)
13
0
'''simple docstring''' import os import string import sys a = 1 << 8 a = { 'tab': ord("\t"), 'newline': ord("\r"), 'esc': 27, 'up': 65 + ARROW_KEY_FLAG, 'down': 66 + ARROW_KEY_FLAG, 'right': 67 + ARROW_KEY_FLAG, 'left': 68 + ARROW_KEY_FLAG, 'mod_int': 91, 'undefined': sys.maxsize, 'interrupt': 3, 'insert': 50, 'delete': 51, 'pg_up': 53, 'pg_down': 54, } a = KEYMAP['up'] a = KEYMAP['left'] if sys.platform == "win32": a = [] a = { b'\xe0H': KEYMAP['up'] - ARROW_KEY_FLAG, b'\x00H': KEYMAP['up'] - ARROW_KEY_FLAG, b'\xe0P': KEYMAP['down'] - ARROW_KEY_FLAG, b'\x00P': KEYMAP['down'] - ARROW_KEY_FLAG, b'\xe0M': KEYMAP['right'] - ARROW_KEY_FLAG, b'\x00M': KEYMAP['right'] - ARROW_KEY_FLAG, b'\xe0K': KEYMAP['left'] - ARROW_KEY_FLAG, b'\x00K': KEYMAP['left'] - ARROW_KEY_FLAG, } for i in range(10): a = ord(str(i)) def __magic_name__ ( ) -> int: '''simple docstring''' if os.name == "nt": import msvcrt __SCREAMING_SNAKE_CASE = """mbcs""" # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(UpperCamelCase__ ) == 0: # Read the keystroke __SCREAMING_SNAKE_CASE = msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): __SCREAMING_SNAKE_CASE = ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: __SCREAMING_SNAKE_CASE = chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP["""mod_int"""] ) ) WIN_CH_BUFFER.append(UpperCamelCase__ ) if ord(UpperCamelCase__ ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(126 ) ) __SCREAMING_SNAKE_CASE = chr(KEYMAP["""esc"""] ) except KeyError: __SCREAMING_SNAKE_CASE = cha[1] else: __SCREAMING_SNAKE_CASE = ch.decode(UpperCamelCase__ ) else: __SCREAMING_SNAKE_CASE = WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty __SCREAMING_SNAKE_CASE = sys.stdin.fileno() __SCREAMING_SNAKE_CASE = termios.tcgetattr(UpperCamelCase__ ) try: tty.setraw(UpperCamelCase__ ) __SCREAMING_SNAKE_CASE = sys.stdin.read(1 ) finally: termios.tcsetattr(UpperCamelCase__ , termios.TCSADRAIN , UpperCamelCase__ ) return ch def __magic_name__ ( ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = get_raw_chars() if ord(UpperCamelCase__ ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(UpperCamelCase__ ) == KEYMAP["esc"]: __SCREAMING_SNAKE_CASE = get_raw_chars() if ord(UpperCamelCase__ ) == KEYMAP["mod_int"]: __SCREAMING_SNAKE_CASE = get_raw_chars() if ord(UpperCamelCase__ ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(UpperCamelCase__ ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(UpperCamelCase__ ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
719
'''simple docstring''' import timeit import numpy as np import datasets from datasets.arrow_writer import ArrowWriter from datasets.features.features import _ArrayXD def __magic_name__ ( __UpperCAmelCase ) -> Tuple: '''simple docstring''' def wrapper(*__UpperCAmelCase , **__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = timeit.default_timer() __SCREAMING_SNAKE_CASE = func(*__UpperCAmelCase , **__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = timeit.default_timer() - starttime return delta __SCREAMING_SNAKE_CASE = func.__name__ return wrapper def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase=100 , __UpperCAmelCase=None ) -> Optional[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = seq_shapes or {} for i in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = {} for col_id, (k, v) in enumerate(features.items() ): if isinstance(__UpperCAmelCase , _ArrayXD ): __SCREAMING_SNAKE_CASE = np.random.rand(*v.shape ).astype(v.dtype ) elif isinstance(__UpperCAmelCase , datasets.Value ): if v.dtype == "string": __SCREAMING_SNAKE_CASE = """The small grey turtle was surprisingly fast when challenged.""" else: __SCREAMING_SNAKE_CASE = np.random.randint(10 , size=1 ).astype(v.dtype ).item() elif isinstance(__UpperCAmelCase , datasets.Sequence ): while isinstance(__UpperCAmelCase , datasets.Sequence ): __SCREAMING_SNAKE_CASE = v.feature __SCREAMING_SNAKE_CASE = seq_shapes[k] __SCREAMING_SNAKE_CASE = np.random.rand(*__UpperCAmelCase ).astype(v.dtype ) __SCREAMING_SNAKE_CASE = data dummy_data.append((i, example) ) return dummy_data def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=100 , __UpperCAmelCase=None ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = generate_examples(__UpperCAmelCase , num_examples=__UpperCAmelCase , seq_shapes=__UpperCAmelCase ) with ArrowWriter(features=__UpperCAmelCase , path=__UpperCAmelCase ) as writer: for key, record in dummy_data: __SCREAMING_SNAKE_CASE = features.encode_example(__UpperCAmelCase ) writer.write(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = writer.finalize() if not num_final_examples == num_examples: raise ValueError( f"""Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}.""" ) __SCREAMING_SNAKE_CASE = datasets.Dataset.from_file(filename=__UpperCAmelCase , info=datasets.DatasetInfo(features=__UpperCAmelCase ) ) return dataset
13
0
'''simple docstring''' import collections import os import re from pathlib import Path a = "src/transformers" # Matches is_xxx_available() a = re.compile(r"is\_([a-z_]*)_available()") # Catches a one-line _import_struct = {xxx} a = re.compile(r"^_import_structure\s+=\s+\{([^\}]+)\}") # Catches a line with a key-values pattern: "bla": ["foo", "bar"] a = re.compile(r"\s+\"\S*\":\s+\[([^\]]*)\]") # Catches a line if not is_foo_available a = re.compile(r"^\s*if\s+not\s+is\_[a-z_]*\_available\(\)") # Catches a line _import_struct["bla"].append("foo") a = re.compile(r"^\s*_import_structure\[\"\S*\"\]\.append\(\"(\S*)\"\)") # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] a = re.compile(r"^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]") # Catches a line with an object between quotes and a comma: "MyModel", a = re.compile(r"^\s+\"([^\"]+)\",") # Catches a line with objects between brackets only: ["foo", "bar"], a = re.compile(r"^\s+\[([^\]]+)\]") # Catches a line with from foo import bar, bla, boo a = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") # Catches a line with try: a = re.compile(r"^\s*try:") # Catches a line with else: a = re.compile(r"^\s*else:") def __magic_name__ ( __UpperCAmelCase ) -> Any: '''simple docstring''' if _re_test_backend.search(_UpperCamelCase ) is None: return None __SCREAMING_SNAKE_CASE = [b[0] for b in _re_backend.findall(_UpperCamelCase )] backends.sort() return "_and_".join(_UpperCamelCase ) def __magic_name__ ( __UpperCAmelCase ) -> Tuple: '''simple docstring''' with open(_UpperCamelCase , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: __SCREAMING_SNAKE_CASE = f.readlines() __SCREAMING_SNAKE_CASE = 0 while line_index < len(_UpperCamelCase ) and not lines[line_index].startswith("""_import_structure = {""" ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(_UpperCamelCase ): return None # First grab the objects without a specific backend in _import_structure __SCREAMING_SNAKE_CASE = [] while not lines[line_index].startswith("""if TYPE_CHECKING""" ) and find_backend(lines[line_index] ) is None: __SCREAMING_SNAKE_CASE = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(_UpperCamelCase ): __SCREAMING_SNAKE_CASE = _re_one_line_import_struct.search(_UpperCamelCase ).groups()[0] __SCREAMING_SNAKE_CASE = re.findall(R"""\[([^\]]+)\]""" , _UpperCamelCase ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(""", """ )] ) line_index += 1 continue __SCREAMING_SNAKE_CASE = _re_import_struct_key_value.search(_UpperCamelCase ) if single_line_import_search is not None: __SCREAMING_SNAKE_CASE = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(""", """ ) if len(_UpperCamelCase ) > 0] objects.extend(_UpperCamelCase ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) line_index += 1 __SCREAMING_SNAKE_CASE = {"""none""": objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith("""if TYPE_CHECKING""" ): # If the line is an if not is_backend_available, we grab all objects associated. __SCREAMING_SNAKE_CASE = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: __SCREAMING_SNAKE_CASE = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 __SCREAMING_SNAKE_CASE = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 4 ): __SCREAMING_SNAKE_CASE = lines[line_index] if _re_import_struct_add_one.search(_UpperCamelCase ) is not None: objects.append(_re_import_struct_add_one.search(_UpperCamelCase ).groups()[0] ) elif _re_import_struct_add_many.search(_UpperCamelCase ) is not None: __SCREAMING_SNAKE_CASE = _re_import_struct_add_many.search(_UpperCamelCase ).groups()[0].split(""", """ ) __SCREAMING_SNAKE_CASE = [obj[1:-1] for obj in imports if len(_UpperCamelCase ) > 0] objects.extend(_UpperCamelCase ) elif _re_between_brackets.search(_UpperCamelCase ) is not None: __SCREAMING_SNAKE_CASE = _re_between_brackets.search(_UpperCamelCase ).groups()[0].split(""", """ ) __SCREAMING_SNAKE_CASE = [obj[1:-1] for obj in imports if len(_UpperCamelCase ) > 0] objects.extend(_UpperCamelCase ) elif _re_quote_object.search(_UpperCamelCase ) is not None: objects.append(_re_quote_object.search(_UpperCamelCase ).groups()[0] ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) elif line.startswith(""" """ * 12 + """\"""" ): objects.append(line[13:-3] ) line_index += 1 __SCREAMING_SNAKE_CASE = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend __SCREAMING_SNAKE_CASE = [] while ( line_index < len(_UpperCamelCase ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith("""else""" ) ): __SCREAMING_SNAKE_CASE = lines[line_index] __SCREAMING_SNAKE_CASE = _re_import.search(_UpperCamelCase ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 8 ): objects.append(line[8:-2] ) line_index += 1 __SCREAMING_SNAKE_CASE = {"""none""": objects} # Let's continue with backend-specific objects while line_index < len(_UpperCamelCase ): # If the line is an if is_backend_available, we grab all objects associated. __SCREAMING_SNAKE_CASE = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: __SCREAMING_SNAKE_CASE = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 __SCREAMING_SNAKE_CASE = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 8 ): __SCREAMING_SNAKE_CASE = lines[line_index] __SCREAMING_SNAKE_CASE = _re_import.search(_UpperCamelCase ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 12 ): objects.append(line[12:-2] ) line_index += 1 __SCREAMING_SNAKE_CASE = objects else: line_index += 1 return import_dict_objects, type_hint_objects def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' def find_duplicates(__UpperCAmelCase ): return [k for k, v in collections.Counter(_UpperCamelCase ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] __SCREAMING_SNAKE_CASE = [] for key in import_dict_objects.keys(): __SCREAMING_SNAKE_CASE = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(f"""Duplicate _import_structure definitions for: {duplicate_imports}""" ) __SCREAMING_SNAKE_CASE = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(f"""Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}""" ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): __SCREAMING_SNAKE_CASE = """base imports""" if key == """none""" else f"""{key} backend""" errors.append(f"""Differences for {name}:""" ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(f""" {a} in TYPE_HINT but not in _import_structure.""" ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(f""" {a} in _import_structure but not in TYPE_HINT.""" ) return errors def __magic_name__ ( ) -> List[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] for root, _, files in os.walk(_UpperCamelCase ): if "__init__.py" in files: __SCREAMING_SNAKE_CASE = os.path.join(_UpperCamelCase , """__init__.py""" ) __SCREAMING_SNAKE_CASE = parse_init(_UpperCamelCase ) if objects is not None: __SCREAMING_SNAKE_CASE = analyze_results(*_UpperCamelCase ) if len(_UpperCamelCase ) > 0: __SCREAMING_SNAKE_CASE = f"""Problem in {fname}, both halves do not define the same objects.\n{errors[0]}""" failures.append("""\n""".join(_UpperCamelCase ) ) if len(_UpperCamelCase ) > 0: raise ValueError("""\n\n""".join(_UpperCamelCase ) ) def __magic_name__ ( ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] for path, directories, files in os.walk(_UpperCamelCase ): for folder in directories: # Ignore private modules if folder.startswith("""_""" ): directories.remove(_UpperCamelCase ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(_UpperCamelCase ) / folder).glob("""*.py""" ) ) ) == 0: continue __SCREAMING_SNAKE_CASE = str((Path(_UpperCamelCase ) / folder).relative_to(_UpperCamelCase ) ) __SCREAMING_SNAKE_CASE = short_path.replace(os.path.sep , """.""" ) submodules.append(_UpperCamelCase ) for fname in files: if fname == "__init__.py": continue __SCREAMING_SNAKE_CASE = str((Path(_UpperCamelCase ) / fname).relative_to(_UpperCamelCase ) ) __SCREAMING_SNAKE_CASE = short_path.replace(""".py""" , """""" ).replace(os.path.sep , """.""" ) if len(submodule.split(""".""" ) ) == 1: submodules.append(_UpperCamelCase ) return submodules a = [ "convert_pytorch_checkpoint_to_tf2", "modeling_flax_pytorch_utils", "models.esm.openfold_utils", ] def __magic_name__ ( ) -> Optional[Any]: '''simple docstring''' from transformers.utils import direct_transformers_import __SCREAMING_SNAKE_CASE = direct_transformers_import(_UpperCamelCase ) __SCREAMING_SNAKE_CASE = set(transformers._import_structure.keys() ) # This contains all the base keys of the _import_structure object defined in the init, but if the user is missing # some optional dependencies, they may not have all of them. Thus we read the init to read all additions and # (potentiall re-) add them. with open(os.path.join(_UpperCamelCase , """__init__.py""" ) , """r""" ) as f: __SCREAMING_SNAKE_CASE = f.read() import_structure_keys.update(set(re.findall(R"""import_structure\[\"([^\"]*)\"\]""" , _UpperCamelCase ) ) ) __SCREAMING_SNAKE_CASE = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in import_structure_keys ] if len(_UpperCamelCase ) > 0: __SCREAMING_SNAKE_CASE = """\n""".join(f"""- {module}""" for module in module_not_registered ) raise ValueError( """The following submodules are not properly registed in the main init of Transformers:\n""" f"""{list_of_modules}\n""" """Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value.""" ) if __name__ == "__main__": check_all_inits() check_submodules()
720
'''simple docstring''' import time from contextlib import contextmanager from pathlib import Path import pytest import requests from huggingface_hub.hf_api import HfApi, HfFolder a = "__DUMMY_TRANSFORMERS_USER__" a = "Dummy User" a = "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" a = "https://hub-ci.huggingface.co" a = CI_HUB_ENDPOINT + "/datasets/{repo_id}/resolve/{revision}/{path}" a = CI_HUB_ENDPOINT + "/{repo_id}/resolve/{revision}/{filename}" a = Path("~/.huggingface/hub_ci_token").expanduser() @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' monkeypatch.setattr( """huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE""" , __UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' monkeypatch.setattr("""datasets.config.HF_ENDPOINT""" , __UpperCAmelCase ) monkeypatch.setattr("""datasets.config.HUB_DATASETS_URL""" , __UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' monkeypatch.setattr("""huggingface_hub.hf_api.HfFolder.path_token""" , __UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' HfFolder.save_token(__UpperCAmelCase ) yield HfFolder.delete_token() @pytest.fixture(scope="""session""" ) def __magic_name__ ( ) -> Optional[Any]: '''simple docstring''' return HfApi(endpoint=__UpperCAmelCase ) @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = HfFolder.get_token() HfFolder.save_token(__UpperCAmelCase ) yield CI_HUB_USER_TOKEN if previous_token is not None: HfFolder.save_token(__UpperCAmelCase ) @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> Dict: '''simple docstring''' def _cleanup_repo(__UpperCAmelCase ): hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) return _cleanup_repo @pytest.fixture def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' @contextmanager def _temporary_repo(__UpperCAmelCase ): try: yield repo_id finally: cleanup_repo(__UpperCAmelCase ) return _temporary_repo @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""repo_txt_data-{int(time.time() * 1_0e3 )}""" __SCREAMING_SNAKE_CASE = f"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" , private=__UpperCAmelCase ) hf_api.upload_file( token=__UpperCAmelCase , path_or_fileobj=str(__UpperCAmelCase ) , path_in_repo="""data/text_data.txt""" , repo_id=__UpperCAmelCase , repo_type="""dataset""" , ) yield repo_id try: hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' return hf_private_dataset_repo_txt_data_ @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""repo_zipped_txt_data-{int(time.time() * 1_0e3 )}""" __SCREAMING_SNAKE_CASE = f"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" , private=__UpperCAmelCase ) hf_api.upload_file( token=__UpperCAmelCase , path_or_fileobj=str(__UpperCAmelCase ) , path_in_repo="""data.zip""" , repo_id=__UpperCAmelCase , repo_type="""dataset""" , ) yield repo_id try: hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Tuple: '''simple docstring''' return hf_private_dataset_repo_zipped_txt_data_ @pytest.fixture(scope="""session""" ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""repo_zipped_img_data-{int(time.time() * 1_0e3 )}""" __SCREAMING_SNAKE_CASE = f"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" , private=__UpperCAmelCase ) hf_api.upload_file( token=__UpperCAmelCase , path_or_fileobj=str(__UpperCAmelCase ) , path_in_repo="""data.zip""" , repo_id=__UpperCAmelCase , repo_type="""dataset""" , ) yield repo_id try: hf_api.delete_repo(__UpperCAmelCase , token=__UpperCAmelCase , repo_type="""dataset""" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' return hf_private_dataset_repo_zipped_img_data_
13
0
'''simple docstring''' import hashlib import unittest from transformers import MODEL_FOR_DEPTH_ESTIMATION_MAPPING, is_torch_available, is_vision_available from transformers.pipelines import DepthEstimationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_timm, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_torch_available(): import torch if is_vision_available(): from PIL import Image else: class __a : @staticmethod def UpperCAmelCase__ ( *lowerCamelCase : List[str] ,**lowerCamelCase : Optional[Any] ): '''simple docstring''' pass def __magic_name__ ( __UpperCAmelCase ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = hashlib.mda(image.tobytes() ) return m.hexdigest() @is_pipeline_test @require_vision @require_timm @require_torch class __a ( unittest.TestCase ): __UpperCamelCase : Dict = MODEL_FOR_DEPTH_ESTIMATION_MAPPING def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : str ,lowerCamelCase : Tuple ,lowerCamelCase : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = DepthEstimationPipeline(model=A__ ,image_processor=A__ ) return depth_estimator, [ "./tests/fixtures/tests_samples/COCO/000000039769.png", "./tests/fixtures/tests_samples/COCO/000000039769.png", ] def UpperCAmelCase__ ( self : Union[str, Any] ,lowerCamelCase : str ,lowerCamelCase : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = depth_estimator("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) self.assertEqual({"""predicted_depth""": ANY(torch.Tensor ), """depth""": ANY(Image.Image )} ,A__ ) import datasets __SCREAMING_SNAKE_CASE = datasets.load_dataset("""hf-internal-testing/fixtures_image_utils""" ,"""image""" ,split="""test""" ) __SCREAMING_SNAKE_CASE = depth_estimator( [ Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ), """http://images.cocodataset.org/val2017/000000039769.jpg""", # RGBA dataset[0]["""file"""], # LA dataset[1]["""file"""], # L dataset[2]["""file"""], ] ) self.assertEqual( [ {"""predicted_depth""": ANY(torch.Tensor ), """depth""": ANY(Image.Image )}, {"""predicted_depth""": ANY(torch.Tensor ), """depth""": ANY(Image.Image )}, {"""predicted_depth""": ANY(torch.Tensor ), """depth""": ANY(Image.Image )}, {"""predicted_depth""": ANY(torch.Tensor ), """depth""": ANY(Image.Image )}, {"""predicted_depth""": ANY(torch.Tensor ), """depth""": ANY(Image.Image )}, ] ,A__ ,) @require_tf @unittest.skip("""Depth estimation is not implemented in TF""" ) def UpperCAmelCase__ ( self : int ): '''simple docstring''' pass @slow @require_torch def UpperCAmelCase__ ( self : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = """Intel/dpt-large""" __SCREAMING_SNAKE_CASE = pipeline("""depth-estimation""" ,model=A__ ) __SCREAMING_SNAKE_CASE = depth_estimator("""http://images.cocodataset.org/val2017/000000039769.jpg""" ) __SCREAMING_SNAKE_CASE = hashimage(outputs["""depth"""] ) # This seems flaky. # self.assertEqual(outputs["depth"], "1a39394e282e9f3b0741a90b9f108977") self.assertEqual(nested_simplify(outputs["""predicted_depth"""].max().item() ) ,29.304 ) self.assertEqual(nested_simplify(outputs["""predicted_depth"""].min().item() ) ,2.662 ) @require_torch def UpperCAmelCase__ ( self : int ): '''simple docstring''' self.skipTest("""There is not hf-internal-testing tiny model for either GLPN nor DPT""" )
721
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING a = logging.get_logger(__name__) a = { "ut/deta": "https://huggingface.co/ut/deta/resolve/main/config.json", } class __a ( _snake_case ): __UpperCamelCase : Dict = 'deta' __UpperCamelCase : List[str] = { 'hidden_size': 'd_model', 'num_attention_heads': 'encoder_attention_heads', } def __init__( self : Tuple ,lowerCamelCase : List[Any]=None ,lowerCamelCase : Any=900 ,lowerCamelCase : int=2048 ,lowerCamelCase : Any=6 ,lowerCamelCase : Optional[Any]=2048 ,lowerCamelCase : str=8 ,lowerCamelCase : Union[str, Any]=6 ,lowerCamelCase : List[str]=1024 ,lowerCamelCase : int=8 ,lowerCamelCase : Any=0.0 ,lowerCamelCase : Any=True ,lowerCamelCase : Optional[int]="relu" ,lowerCamelCase : int=256 ,lowerCamelCase : Tuple=0.1 ,lowerCamelCase : Optional[Any]=0.0 ,lowerCamelCase : Tuple=0.0 ,lowerCamelCase : List[str]=0.02 ,lowerCamelCase : Any=1.0 ,lowerCamelCase : Optional[int]=True ,lowerCamelCase : int=False ,lowerCamelCase : Optional[Any]="sine" ,lowerCamelCase : Dict=5 ,lowerCamelCase : List[Any]=4 ,lowerCamelCase : Optional[Any]=4 ,lowerCamelCase : Any=True ,lowerCamelCase : int=300 ,lowerCamelCase : Any=True ,lowerCamelCase : Tuple=True ,lowerCamelCase : int=1 ,lowerCamelCase : Tuple=5 ,lowerCamelCase : Union[str, Any]=2 ,lowerCamelCase : Tuple=1 ,lowerCamelCase : int=1 ,lowerCamelCase : str=5 ,lowerCamelCase : Optional[Any]=2 ,lowerCamelCase : List[Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.25 ,**lowerCamelCase : int ,): '''simple docstring''' if backbone_config is None: logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.""" ) __SCREAMING_SNAKE_CASE = CONFIG_MAPPING["""resnet"""](out_features=["""stage2""", """stage3""", """stage4"""] ) else: if isinstance(lowerCamelCase ,lowerCamelCase ): __SCREAMING_SNAKE_CASE = backbone_config.pop("""model_type""" ) __SCREAMING_SNAKE_CASE = CONFIG_MAPPING[backbone_model_type] __SCREAMING_SNAKE_CASE = config_class.from_dict(lowerCamelCase ) __SCREAMING_SNAKE_CASE = backbone_config __SCREAMING_SNAKE_CASE = num_queries __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = d_model __SCREAMING_SNAKE_CASE = encoder_ffn_dim __SCREAMING_SNAKE_CASE = encoder_layers __SCREAMING_SNAKE_CASE = encoder_attention_heads __SCREAMING_SNAKE_CASE = decoder_ffn_dim __SCREAMING_SNAKE_CASE = decoder_layers __SCREAMING_SNAKE_CASE = decoder_attention_heads __SCREAMING_SNAKE_CASE = dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = activation_function __SCREAMING_SNAKE_CASE = init_std __SCREAMING_SNAKE_CASE = init_xavier_std __SCREAMING_SNAKE_CASE = encoder_layerdrop __SCREAMING_SNAKE_CASE = auxiliary_loss __SCREAMING_SNAKE_CASE = position_embedding_type # deformable attributes __SCREAMING_SNAKE_CASE = num_feature_levels __SCREAMING_SNAKE_CASE = encoder_n_points __SCREAMING_SNAKE_CASE = decoder_n_points __SCREAMING_SNAKE_CASE = two_stage __SCREAMING_SNAKE_CASE = two_stage_num_proposals __SCREAMING_SNAKE_CASE = with_box_refine __SCREAMING_SNAKE_CASE = assign_first_stage if two_stage is True and with_box_refine is False: raise ValueError("""If two_stage is True, with_box_refine must be True.""" ) # Hungarian matcher __SCREAMING_SNAKE_CASE = class_cost __SCREAMING_SNAKE_CASE = bbox_cost __SCREAMING_SNAKE_CASE = giou_cost # Loss coefficients __SCREAMING_SNAKE_CASE = mask_loss_coefficient __SCREAMING_SNAKE_CASE = dice_loss_coefficient __SCREAMING_SNAKE_CASE = bbox_loss_coefficient __SCREAMING_SNAKE_CASE = giou_loss_coefficient __SCREAMING_SNAKE_CASE = eos_coefficient __SCREAMING_SNAKE_CASE = focal_alpha super().__init__(is_encoder_decoder=lowerCamelCase ,**lowerCamelCase ) @property def UpperCAmelCase__ ( self : Any ): '''simple docstring''' return self.encoder_attention_heads @property def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' return self.d_model def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) __SCREAMING_SNAKE_CASE = self.backbone_config.to_dict() __SCREAMING_SNAKE_CASE = self.__class__.model_type return output
13
0
from dataclasses import dataclass, field from typing import Optional @dataclass class __a : __UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot', metadata={'help': 'Model name or path of model to be trained.'} ) __UpperCamelCase : Optional[str] = field( default='./', metadata={'help': 'Save dir where model repo is cloned and models updates are saved to.'} ) __UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot-clean-train', metadata={'help': 'Name or path of training dataset.'} ) __UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot-clean-valid', metadata={'help': 'Name or path of validation dataset.'} ) __UpperCamelCase : Optional[int] = field(default=2, metadata={'help': 'Batch size for training.'} ) __UpperCamelCase : Optional[int] = field(default=2, metadata={'help': 'Batch size for evaluation.'} ) __UpperCamelCase : Optional[float] = field(default=0.1, metadata={'help': 'Value of weight decay.'} ) __UpperCamelCase : Optional[int] = field( default=1_0000, metadata={'help': 'Size of buffer used to shuffle streaming dataset.'} ) __UpperCamelCase : Optional[float] = field(default=2E-4, metadata={'help': 'Learning rate fo training.'} ) __UpperCamelCase : Optional[str] = field(default='cosine', metadata={'help': 'Learning rate.'} ) __UpperCamelCase : Optional[int] = field( default=750, metadata={'help': 'Number of warmup steps in the learning rate schedule.'} ) __UpperCamelCase : Optional[int] = field( default=16, metadata={'help': 'Number of gradient accumulation steps.'} ) __UpperCamelCase : Optional[bool] = field( default=_snake_case, metadata={'help': 'Use gradient checkpointing to reduce memory footprint.'} ) __UpperCamelCase : Optional[int] = field(default=5_0000, metadata={'help': 'Maximum number of training steps.'} ) __UpperCamelCase : Optional[int] = field( default=-1, metadata={'help': 'Maximum number of evaluation steps. If -1 the full dataset is evaluated.'} ) __UpperCamelCase : Optional[int] = field(default=1024, metadata={'help': 'Sequence lengths used for training.'} ) __UpperCamelCase : Optional[int] = field(default=1, metadata={'help': 'Training seed.'} ) __UpperCamelCase : Optional[int] = field( default=1024, metadata={'help': 'Interval to save checkpoints. Measured as number of forward passes not training steps.'}, ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'States path if the training should continue from a checkpoint folder.'} ) __UpperCamelCase : Optional[bool] = field(default=_snake_case, metadata={'help': 'If True the data is pretokenized.'} ) @dataclass class __a : __UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot', metadata={'help': 'Model name or path of model to be evaluated.'} ) __UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot-clean-valid', metadata={'help': 'Name or path of validation dataset.'} ) __UpperCamelCase : Optional[int] = field(default=2, metadata={'help': 'Batch size used for evaluation.'} ) __UpperCamelCase : Optional[int] = field( default=-1, metadata={'help': 'Maximum number of evaluation steps. If -1 the full dataset is evaluated.'} ) __UpperCamelCase : Optional[int] = field(default=1024, metadata={'help': 'Length of sequences to be evaluated.'} ) __UpperCamelCase : Optional[int] = field(default=1, metadata={'help': 'Random seed used for evaluation.'} ) @dataclass class __a : __UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot', metadata={'help': 'Model name or path of model to be evaluated.'} ) __UpperCamelCase : Optional[int] = field(default=_snake_case, metadata={'help': 'Number of workers used for code evaluation.'} ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={'help': 'The number of human-eval tasks to run. If not included all tasks are evaluated.'}, ) __UpperCamelCase : Optional[bool] = field( default=_snake_case, metadata={'help': 'Sample from the language model\'s output distribution.'} ) __UpperCamelCase : Optional[float] = field(default=0.2, metadata={'help': 'Sampling temperature used for generation.'} ) __UpperCamelCase : Optional[int] = field(default=256, metadata={'help': 'Maximum number of newly generated tokens.'} ) __UpperCamelCase : Optional[int] = field(default=0, metadata={'help': 'Top-k parameter used for generation.'} ) __UpperCamelCase : Optional[float] = field(default=0.95, metadata={'help': 'Top-p parameter used for nucleus sampling.'} ) __UpperCamelCase : Optional[int] = field(default=10, metadata={'help': 'Number of generations to run in parallel.'} ) __UpperCamelCase : Optional[int] = field( default=200, metadata={'help': 'Number of completions to generate for each sample.'} ) __UpperCamelCase : Optional[int] = field(default=1, metadata={'help': 'Random seed used for evaluation.'} ) __UpperCamelCase : Optional[str] = field( default='eval_results.json', metadata={'help': 'Random seed used for evaluation.'} ) __UpperCamelCase : Optional[str] = field( default='0', metadata={'help': 'Allow `code_eval` to execute Python code on machine'} ) __UpperCamelCase : Optional[int] = field( default=-1, metadata={ 'help': ( 'Determine which device to run the `text-generation` Pipeline on. -1 is CPU and any zero or positive' ' number corresponds to which GPU device id to run on.' ) }, ) @dataclass class __a : __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': 'The number of CPU cores to use for parallel preprocessing. Default uses the maximum available.' }, ) __UpperCamelCase : Optional[str] = field( default='transformersbook/codeparrot', metadata={'help': 'Folder or name of dataset to process.'} ) __UpperCamelCase : Optional[str] = field( default='codeparrot-clean', metadata={'help': 'Folder to save processed processed dataset.'} ) __UpperCamelCase : Optional[int] = field( default=10_0000, metadata={'help': 'Number of files to save per JSON output file.'} ) __UpperCamelCase : Optional[str] = field(default='content', metadata={'help': 'Column containing text data to process.'} ) __UpperCamelCase : Optional[float] = field( default=1000, metadata={'help': 'Maximum line length in file, otherwise file is filtered.'} ) __UpperCamelCase : Optional[float] = field( default=100, metadata={'help': 'Maximum mean line length in file, otherwise file is filtered.'} ) __UpperCamelCase : Optional[float] = field( default=0.25, metadata={'help': 'Maximum fraction of non-alphanumeric characters, otherwise file is filtered.'} ) __UpperCamelCase : Optional[float] = field( default=1.5, metadata={'help': 'Minimum character token ratio for the file, otherwise file is filtered.'} ) __UpperCamelCase : Optional[float] = field( default=0.7, metadata={'help': 'Probability for filtering config, test and uncommon files.'} ) __UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot', metadata={'help': 'Name or path to the tokenizer.'}, ) __UpperCamelCase : Optional[bool] = field( default=_snake_case, metadata={'help': 'If True, near-duplicate samples are removed.'} ) __UpperCamelCase : Optional[float] = field( default=0.85, metadata={'help': 'Jaccard threshold for near-duplicate samples.'} ) @dataclass class __a : __UpperCamelCase : Optional[str] = field( default='gpt2', metadata={'help': 'Base tokenizer to build new tokenizer from.'} ) __UpperCamelCase : Optional[str] = field( default='transformersbook/codeparrot-train', metadata={'help': 'Dataset to train tokenizer on.'} ) __UpperCamelCase : Optional[str] = field(default='content', metadata={'help': 'Column containing text data to process.'} ) __UpperCamelCase : Optional[int] = field(default=20_0000, metadata={'help': 'Number of examples to train tokenizer on.'} ) __UpperCamelCase : Optional[int] = field( default=3_2768, metadata={'help': 'Number of examples to train the tokenizer on.'} ) __UpperCamelCase : Optional[str] = field(default='codeparrot', metadata={'help': 'Name of new tokenizer.'} ) __UpperCamelCase : Optional[bool] = field(default=_snake_case, metadata={'help': 'Push saved tokenizer to the hub.'} ) @dataclass class __a : __UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot', metadata={'help': 'Name or path to the tokenizer.'} ) __UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot-clean-train', metadata={'help': 'Name or path to the dataset to pretokenize.'} ) __UpperCamelCase : Optional[str] = field( default='tokenized-codeparrot-train', metadata={'help': 'Repo name of the pretokenized data.'} ) __UpperCamelCase : Optional[int] = field(default=_snake_case, metadata={'help': 'Number of workers used for code evaluation.'} ) @dataclass class __a : __UpperCamelCase : Optional[str] = field( default='gpt2-large', metadata={'help': 'Configuration to use for model initialization.'} ) __UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot', metadata={'help': 'Tokenizer attached to model.'} ) __UpperCamelCase : Optional[str] = field(default='codeparrot', metadata={'help': 'Name of the created model.'} ) __UpperCamelCase : Optional[bool] = field(default=_snake_case, metadata={'help': 'Push saved tokenizer to the hub.'} )
700
'''simple docstring''' import flax.linen as nn import jax import jax.numpy as jnp class __a ( nn.Module ): __UpperCamelCase : int __UpperCamelCase : jnp.dtype = jnp.floataa def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = nn.Conv( self.out_channels ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) def __call__( self : List[Any] ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = hidden_states.shape __SCREAMING_SNAKE_CASE = jax.image.resize( lowerCamelCase ,shape=(batch, height * 2, width * 2, channels) ,method="""nearest""" ,) __SCREAMING_SNAKE_CASE = self.conv(lowerCamelCase ) return hidden_states class __a ( nn.Module ): __UpperCamelCase : int __UpperCamelCase : jnp.dtype = jnp.floataa def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = nn.Conv( self.out_channels ,kernel_size=(3, 3) ,strides=(2, 2) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) def __call__( self : List[str] ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.conv(lowerCamelCase ) return hidden_states class __a ( nn.Module ): __UpperCamelCase : int __UpperCamelCase : int = None __UpperCamelCase : float = 0.0 __UpperCamelCase : bool = None __UpperCamelCase : jnp.dtype = jnp.floataa def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.in_channels if self.out_channels is None else self.out_channels __SCREAMING_SNAKE_CASE = nn.GroupNorm(num_groups=32 ,epsilon=1E-5 ) __SCREAMING_SNAKE_CASE = nn.Conv( lowerCamelCase ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) __SCREAMING_SNAKE_CASE = nn.Dense(lowerCamelCase ,dtype=self.dtype ) __SCREAMING_SNAKE_CASE = nn.GroupNorm(num_groups=32 ,epsilon=1E-5 ) __SCREAMING_SNAKE_CASE = nn.Dropout(self.dropout_prob ) __SCREAMING_SNAKE_CASE = nn.Conv( lowerCamelCase ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) __SCREAMING_SNAKE_CASE = self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut __SCREAMING_SNAKE_CASE = None if use_nin_shortcut: __SCREAMING_SNAKE_CASE = nn.Conv( lowerCamelCase ,kernel_size=(1, 1) ,strides=(1, 1) ,padding="""VALID""" ,dtype=self.dtype ,) def __call__( self : List[str] ,lowerCamelCase : Optional[int] ,lowerCamelCase : Tuple ,lowerCamelCase : Union[str, Any]=True ): '''simple docstring''' __SCREAMING_SNAKE_CASE = hidden_states __SCREAMING_SNAKE_CASE = self.norma(lowerCamelCase ) __SCREAMING_SNAKE_CASE = nn.swish(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.conva(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.time_emb_proj(nn.swish(lowerCamelCase ) ) __SCREAMING_SNAKE_CASE = jnp.expand_dims(jnp.expand_dims(lowerCamelCase ,1 ) ,1 ) __SCREAMING_SNAKE_CASE = hidden_states + temb __SCREAMING_SNAKE_CASE = self.norma(lowerCamelCase ) __SCREAMING_SNAKE_CASE = nn.swish(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.dropout(lowerCamelCase ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.conva(lowerCamelCase ) if self.conv_shortcut is not None: __SCREAMING_SNAKE_CASE = self.conv_shortcut(lowerCamelCase ) return hidden_states + residual
13
0
'''simple docstring''' import inspect import unittest from huggingface_hub import hf_hub_download from transformers import ASTConfig from transformers.testing_utils import require_torch, require_torchaudio, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_torchaudio_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 ASTForAudioClassification, ASTModel from transformers.models.audio_spectrogram_transformer.modeling_audio_spectrogram_transformer import ( AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) if is_torchaudio_available(): import torchaudio from transformers import ASTFeatureExtractor class __a: def __init__( self : Optional[int] ,lowerCamelCase : Union[str, Any] ,lowerCamelCase : List[str]=13 ,lowerCamelCase : List[Any]=2 ,lowerCamelCase : Optional[Any]=24 ,lowerCamelCase : Any=16 ,lowerCamelCase : int=True ,lowerCamelCase : List[Any]=True ,lowerCamelCase : Union[str, Any]=32 ,lowerCamelCase : int=5 ,lowerCamelCase : Union[str, Any]=4 ,lowerCamelCase : Optional[int]=37 ,lowerCamelCase : Any="gelu" ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : int=0.1 ,lowerCamelCase : Union[str, Any]=10 ,lowerCamelCase : List[str]=0.02 ,lowerCamelCase : Union[str, Any]=None ,lowerCamelCase : int=2 ,lowerCamelCase : Optional[Any]=2 ,): '''simple docstring''' __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = patch_size __SCREAMING_SNAKE_CASE = max_length __SCREAMING_SNAKE_CASE = num_mel_bins __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 = frequency_stride __SCREAMING_SNAKE_CASE = time_stride # in AST, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens) __SCREAMING_SNAKE_CASE = (self.num_mel_bins - self.patch_size) // self.frequency_stride + 1 __SCREAMING_SNAKE_CASE = (self.max_length - self.patch_size) // self.time_stride + 1 __SCREAMING_SNAKE_CASE = frequency_out_dimension * time_out_dimension __SCREAMING_SNAKE_CASE = num_patches + 2 def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.max_length, self.num_mel_bins] ) __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, input_values, labels def UpperCAmelCase__ ( self : Any ): '''simple docstring''' return ASTConfig( patch_size=self.patch_size ,max_length=self.max_length ,num_mel_bins=self.num_mel_bins ,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=__UpperCamelCase ,initializer_range=self.initializer_range ,frequency_stride=self.frequency_stride ,time_stride=self.time_stride ,) def UpperCAmelCase__ ( self : Optional[int] ,lowerCamelCase : Optional[Any] ,lowerCamelCase : Optional[Any] ,lowerCamelCase : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = ASTModel(config=__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() __SCREAMING_SNAKE_CASE = model(__UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase__ ( self : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ) = config_and_inputs __SCREAMING_SNAKE_CASE = {"""input_values""": input_values} return config, inputs_dict @require_torch class __a( __snake_case, __snake_case, unittest.TestCase ): __UpperCamelCase : Optional[Any] = ( ( ASTModel, ASTForAudioClassification, ) if is_torch_available() else () ) __UpperCamelCase : List[Any] = ( {'audio-classification': ASTForAudioClassification, 'feature-extraction': ASTModel} if is_torch_available() else {} ) __UpperCamelCase : Optional[int] = False __UpperCamelCase : List[Any] = False __UpperCamelCase : List[str] = False __UpperCamelCase : List[Any] = False def UpperCAmelCase__ ( self : int ,lowerCamelCase : List[Any] ,lowerCamelCase : Dict ,lowerCamelCase : Dict ,lowerCamelCase : List[str] ,lowerCamelCase : int ): '''simple docstring''' if pipeline_test_casse_name == "AudioClassificationPipelineTests": return True return False def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = ASTModelTester(self ) __SCREAMING_SNAKE_CASE = ConfigTester(self ,config_class=__UpperCamelCase ,has_text_modality=__UpperCamelCase ,hidden_size=37 ) def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="""AST does not use inputs_embeds""" ) def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' pass def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __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(__UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() ,(nn.Module) ) __SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__UpperCamelCase ,nn.Linear ) ) def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __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(__UpperCamelCase ) __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 = ["""input_values"""] self.assertListEqual(arg_names[:1] ,__UpperCamelCase ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__UpperCamelCase ) @slow def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __SCREAMING_SNAKE_CASE = ASTModel.from_pretrained(__UpperCamelCase ) self.assertIsNotNone(__UpperCamelCase ) def __magic_name__ ( ) -> Union[str, Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = hf_hub_download( repo_id="""nielsr/audio-spectogram-transformer-checkpoint""" , filename="""sample_audio.flac""" , repo_type="""dataset""" ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = torchaudio.load(lowercase__ ) return audio, sampling_rate @require_torch @require_torchaudio class __a( unittest.TestCase ): @cached_property def UpperCAmelCase__ ( self : int ): '''simple docstring''' return ( ASTFeatureExtractor.from_pretrained("""MIT/ast-finetuned-audioset-10-10-0.4593""" ) if is_torchaudio_available() else None ) @slow def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.default_feature_extractor __SCREAMING_SNAKE_CASE = ASTForAudioClassification.from_pretrained("""MIT/ast-finetuned-audioset-10-10-0.4593""" ).to(__UpperCamelCase ) __SCREAMING_SNAKE_CASE = self.default_feature_extractor __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = prepare_audio() __SCREAMING_SNAKE_CASE = audio.squeeze().numpy() __SCREAMING_SNAKE_CASE = feature_extractor(__UpperCamelCase ,sampling_rate=__UpperCamelCase ,return_tensors="""pt""" ).to(__UpperCamelCase ) # forward pass with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(**__UpperCamelCase ) # verify the logits __SCREAMING_SNAKE_CASE = torch.Size((1, 527) ) self.assertEqual(outputs.logits.shape ,__UpperCamelCase ) __SCREAMING_SNAKE_CASE = torch.tensor([-0.8_760, -7.0_042, -8.6_602] ).to(__UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] ,__UpperCamelCase ,atol=1E-4 ) )
701
'''simple docstring''' import sys from collections import defaultdict class __a : def __init__( self : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = [] def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : List[Any] ): '''simple docstring''' return self.node_position[vertex] def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : str ,lowerCamelCase : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = pos def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : Optional[int] ,lowerCamelCase : Union[str, Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : Any ): '''simple docstring''' if start > size // 2 - 1: return else: if 2 * start + 2 >= size: __SCREAMING_SNAKE_CASE = 2 * start + 1 else: if heap[2 * start + 1] < heap[2 * start + 2]: __SCREAMING_SNAKE_CASE = 2 * start + 1 else: __SCREAMING_SNAKE_CASE = 2 * start + 2 if heap[smallest_child] < heap[start]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = heap[smallest_child], positions[smallest_child] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = ( heap[start], positions[start], ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = temp, tempa __SCREAMING_SNAKE_CASE = self.get_position(positions[smallest_child] ) self.set_position( positions[smallest_child] ,self.get_position(positions[start] ) ) self.set_position(positions[start] ,lowerCamelCase ) self.top_to_bottom(lowerCamelCase ,lowerCamelCase ,lowerCamelCase ,lowerCamelCase ) def UpperCAmelCase__ ( self : Any ,lowerCamelCase : int ,lowerCamelCase : List[str] ,lowerCamelCase : Optional[Any] ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = position[index] while index != 0: __SCREAMING_SNAKE_CASE = int((index - 2) / 2 ) if index % 2 == 0 else int((index - 1) / 2 ) if val < heap[parent]: __SCREAMING_SNAKE_CASE = heap[parent] __SCREAMING_SNAKE_CASE = position[parent] self.set_position(position[parent] ,lowerCamelCase ) else: __SCREAMING_SNAKE_CASE = val __SCREAMING_SNAKE_CASE = temp self.set_position(lowerCamelCase ,lowerCamelCase ) break __SCREAMING_SNAKE_CASE = parent else: __SCREAMING_SNAKE_CASE = val __SCREAMING_SNAKE_CASE = temp self.set_position(lowerCamelCase ,0 ) def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : List[Any] ,lowerCamelCase : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = len(lowerCamelCase ) // 2 - 1 for i in range(lowerCamelCase ,-1 ,-1 ): self.top_to_bottom(lowerCamelCase ,lowerCamelCase ,len(lowerCamelCase ) ,lowerCamelCase ) def UpperCAmelCase__ ( self : int ,lowerCamelCase : Optional[int] ,lowerCamelCase : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = positions[0] __SCREAMING_SNAKE_CASE = sys.maxsize self.top_to_bottom(lowerCamelCase ,0 ,len(lowerCamelCase ) ,lowerCamelCase ) return temp def __magic_name__ ( __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = Heap() __SCREAMING_SNAKE_CASE = [0] * len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [-1] * len(__UpperCAmelCase ) # Neighboring Tree Vertex of selected vertex # Minimum Distance of explored vertex with neighboring vertex of partial tree # formed in graph __SCREAMING_SNAKE_CASE = [] # Heap of Distance of vertices from their neighboring vertex __SCREAMING_SNAKE_CASE = [] for vertex in range(len(__UpperCAmelCase ) ): distance_tv.append(sys.maxsize ) positions.append(__UpperCAmelCase ) heap.node_position.append(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = sys.maxsize for neighbor, distance in adjacency_list[0]: __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = distance heap.heapify(__UpperCAmelCase , __UpperCAmelCase ) for _ in range(1 , len(__UpperCAmelCase ) ): __SCREAMING_SNAKE_CASE = heap.delete_minimum(__UpperCAmelCase , __UpperCAmelCase ) if visited[vertex] == 0: tree_edges.append((nbr_tv[vertex], vertex) ) __SCREAMING_SNAKE_CASE = 1 for neighbor, distance in adjacency_list[vertex]: if ( visited[neighbor] == 0 and distance < distance_tv[heap.get_position(__UpperCAmelCase )] ): __SCREAMING_SNAKE_CASE = distance heap.bottom_to_top( __UpperCAmelCase , heap.get_position(__UpperCAmelCase ) , __UpperCAmelCase , __UpperCAmelCase ) __SCREAMING_SNAKE_CASE = vertex return tree_edges if __name__ == "__main__": # pragma: no cover # < --------- Prims Algorithm --------- > a = int(input("Enter number of edges: ").strip()) a = defaultdict(list) for _ in range(edges_number): a = [int(x) for x in input().strip().split()] adjacency_list[edge[0]].append([edge[1], edge[2]]) adjacency_list[edge[1]].append([edge[0], edge[2]]) print(prisms_algorithm(adjacency_list))
13
0
'''simple docstring''' import torch def __magic_name__ ( ) -> Tuple: '''simple docstring''' if torch.cuda.is_available(): __SCREAMING_SNAKE_CASE = torch.cuda.device_count() else: __SCREAMING_SNAKE_CASE = 0 print(f"""Successfully ran on {num_gpus} GPUs""" ) if __name__ == "__main__": main()
702
'''simple docstring''' import os import string import sys a = 1 << 8 a = { "tab": ord("\t"), "newline": ord("\r"), "esc": 27, "up": 65 + ARROW_KEY_FLAG, "down": 66 + ARROW_KEY_FLAG, "right": 67 + ARROW_KEY_FLAG, "left": 68 + ARROW_KEY_FLAG, "mod_int": 91, "undefined": sys.maxsize, "interrupt": 3, "insert": 50, "delete": 51, "pg_up": 53, "pg_down": 54, } a = KEYMAP["up"] a = KEYMAP["left"] if sys.platform == "win32": a = [] a = { b"\xe0H": KEYMAP["up"] - ARROW_KEY_FLAG, b"\x00H": KEYMAP["up"] - ARROW_KEY_FLAG, b"\xe0P": KEYMAP["down"] - ARROW_KEY_FLAG, b"\x00P": KEYMAP["down"] - ARROW_KEY_FLAG, b"\xe0M": KEYMAP["right"] - ARROW_KEY_FLAG, b"\x00M": KEYMAP["right"] - ARROW_KEY_FLAG, b"\xe0K": KEYMAP["left"] - ARROW_KEY_FLAG, b"\x00K": KEYMAP["left"] - ARROW_KEY_FLAG, } for i in range(10): a = ord(str(i)) def __magic_name__ ( ) -> Union[str, Any]: '''simple docstring''' if os.name == "nt": import msvcrt __SCREAMING_SNAKE_CASE = """mbcs""" # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(__UpperCAmelCase ) == 0: # Read the keystroke __SCREAMING_SNAKE_CASE = msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): __SCREAMING_SNAKE_CASE = ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: __SCREAMING_SNAKE_CASE = chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP["""mod_int"""] ) ) WIN_CH_BUFFER.append(__UpperCAmelCase ) if ord(__UpperCAmelCase ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(126 ) ) __SCREAMING_SNAKE_CASE = chr(KEYMAP["""esc"""] ) except KeyError: __SCREAMING_SNAKE_CASE = cha[1] else: __SCREAMING_SNAKE_CASE = ch.decode(__UpperCAmelCase ) else: __SCREAMING_SNAKE_CASE = WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty __SCREAMING_SNAKE_CASE = sys.stdin.fileno() __SCREAMING_SNAKE_CASE = termios.tcgetattr(__UpperCAmelCase ) try: tty.setraw(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = sys.stdin.read(1 ) finally: termios.tcsetattr(__UpperCAmelCase , termios.TCSADRAIN , __UpperCAmelCase ) return ch def __magic_name__ ( ) -> List[str]: '''simple docstring''' __SCREAMING_SNAKE_CASE = get_raw_chars() if ord(__UpperCAmelCase ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(__UpperCAmelCase ) == KEYMAP["esc"]: __SCREAMING_SNAKE_CASE = get_raw_chars() if ord(__UpperCAmelCase ) == KEYMAP["mod_int"]: __SCREAMING_SNAKE_CASE = get_raw_chars() if ord(__UpperCAmelCase ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(__UpperCAmelCase ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(__UpperCAmelCase ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
13
0
'''simple docstring''' from PIL import Image def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> List[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = (259 * (level + 255)) / (255 * (259 - level)) def contrast(__UpperCAmelCase ) -> int: return int(128 + factor * (c - 128) ) return img.point(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": # Load image with Image.open("image_data/lena.jpg") as img: # Change contrast to 170 a = change_contrast(img, 170) cont_img.save("image_data/lena_high_contrast.png", format="png")
703
'''simple docstring''' from __future__ import annotations import bisect def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' if hi < 0: __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) while lo < hi: __SCREAMING_SNAKE_CASE = lo + (hi - lo) // 2 if sorted_collection[mid] < item: __SCREAMING_SNAKE_CASE = mid + 1 else: __SCREAMING_SNAKE_CASE = mid return lo def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' if hi < 0: __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) while lo < hi: __SCREAMING_SNAKE_CASE = lo + (hi - lo) // 2 if sorted_collection[mid] <= item: __SCREAMING_SNAKE_CASE = mid + 1 else: __SCREAMING_SNAKE_CASE = mid return lo def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> None: '''simple docstring''' sorted_collection.insert(bisect_left(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> None: '''simple docstring''' sorted_collection.insert(bisect_right(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> int | None: '''simple docstring''' __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) - 1 while left <= right: __SCREAMING_SNAKE_CASE = left + (right - left) // 2 __SCREAMING_SNAKE_CASE = sorted_collection[midpoint] if current_item == item: return midpoint elif item < current_item: __SCREAMING_SNAKE_CASE = midpoint - 1 else: __SCREAMING_SNAKE_CASE = midpoint + 1 return None def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> int | None: '''simple docstring''' __SCREAMING_SNAKE_CASE = bisect.bisect_left(__UpperCAmelCase , __UpperCAmelCase ) if index != len(__UpperCAmelCase ) and sorted_collection[index] == item: return index return None def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> int | None: '''simple docstring''' if right < left: return None __SCREAMING_SNAKE_CASE = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: return binary_search_by_recursion(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , midpoint - 1 ) else: return binary_search_by_recursion(__UpperCAmelCase , __UpperCAmelCase , midpoint + 1 , __UpperCAmelCase ) if __name__ == "__main__": a = input("Enter numbers separated by comma:\n").strip() a = sorted(int(item) for item in user_input.split(",")) a = int(input("Enter a single number to be found in the list:\n")) a = binary_search(collection, target) if result is None: print(F'''{target} was not found in {collection}.''') else: print(F'''{target} was found at position {result} in {collection}.''')
13
0
'''simple docstring''' import argparse import logging from collections import namedtuple import torch from model_bertabs import BertAbsSummarizer from models.model_builder import AbsSummarizer # The authors' implementation from transformers import BertTokenizer logging.basicConfig(level=logging.INFO) a = logging.getLogger(__name__) a = "Hello world! cécé herlolip" a = namedtuple( "BertAbsConfig", [ "temp_dir", "large", "use_bert_emb", "finetune_bert", "encoder", "share_emb", "max_pos", "enc_layers", "enc_hidden_size", "enc_heads", "enc_ff_size", "enc_dropout", "dec_layers", "dec_hidden_size", "dec_heads", "dec_ff_size", "dec_dropout", ], ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = BertAbsConfig( temp_dir=""".""" , finetune_bert=a_ , large=a_ , share_emb=a_ , use_bert_emb=a_ , encoder="""bert""" , max_pos=512 , enc_layers=6 , enc_hidden_size=512 , enc_heads=8 , enc_ff_size=512 , enc_dropout=0.2 , dec_layers=6 , dec_hidden_size=768 , dec_heads=8 , dec_ff_size=2048 , dec_dropout=0.2 , ) __SCREAMING_SNAKE_CASE = torch.load(a_ , lambda __UpperCAmelCase , __UpperCAmelCase : storage ) __SCREAMING_SNAKE_CASE = AbsSummarizer(a_ , torch.device("""cpu""" ) , a_ ) original.eval() __SCREAMING_SNAKE_CASE = BertAbsSummarizer(a_ , torch.device("""cpu""" ) ) new_model.eval() # ------------------- # Convert the weights # ------------------- logging.info("""convert the model""" ) new_model.bert.load_state_dict(original.bert.state_dict() ) new_model.decoder.load_state_dict(original.decoder.state_dict() ) new_model.generator.load_state_dict(original.generator.state_dict() ) # ---------------------------------- # Make sure the outpus are identical # ---------------------------------- logging.info("""Make sure that the models\' outputs are identical""" ) __SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("""bert-base-uncased""" ) # prepare the model inputs __SCREAMING_SNAKE_CASE = tokenizer.encode("""This is sample éàalj\'-.""" ) encoder_input_ids.extend([tokenizer.pad_token_id] * (512 - len(a_ )) ) __SCREAMING_SNAKE_CASE = torch.tensor(a_ ).unsqueeze(0 ) __SCREAMING_SNAKE_CASE = tokenizer.encode("""This is sample 3 éàalj\'-.""" ) decoder_input_ids.extend([tokenizer.pad_token_id] * (512 - len(a_ )) ) __SCREAMING_SNAKE_CASE = torch.tensor(a_ ).unsqueeze(0 ) # failsafe to make sure the weights reset does not affect the # loaded weights. assert torch.max(torch.abs(original.generator[0].weight - new_model.generator[0].weight ) ) == 0 # forward pass __SCREAMING_SNAKE_CASE = encoder_input_ids __SCREAMING_SNAKE_CASE = decoder_input_ids __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None # The original model does not apply the geneator layer immediatly but rather in # the beam search (where it combines softmax + linear layer). Since we already # apply the softmax in our generation process we only apply the linear layer here. # We make sure that the outputs of the full stack are identical __SCREAMING_SNAKE_CASE = original(a_ , a_ , a_ , a_ , a_ , a_ , a_ )[0] __SCREAMING_SNAKE_CASE = original.generator(a_ ) __SCREAMING_SNAKE_CASE = new_model( a_ , a_ , a_ , a_ , a_ )[0] __SCREAMING_SNAKE_CASE = new_model.generator(a_ ) __SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_model - output_original_model ) ).item() print("""Maximum absolute difference beween weights: {:.2f}""".format(a_ ) ) __SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_generator - output_original_generator ) ).item() print("""Maximum absolute difference beween weights: {:.2f}""".format(a_ ) ) __SCREAMING_SNAKE_CASE = torch.allclose(a_ , a_ , atol=1e-3 ) if are_identical: logging.info("""all weights are equal up to 1e-3""" ) else: raise ValueError("""the weights are different. The new model is likely different from the original one.""" ) # The model has been saved with torch.save(model) and this is bound to the exact # directory structure. We save the state_dict instead. logging.info("""saving the model\'s state dictionary""" ) torch.save( new_model.state_dict() , """./bertabs-finetuned-cnndm-extractive-abstractive-summarization/pytorch_model.bin""" ) if __name__ == "__main__": a = argparse.ArgumentParser() parser.add_argument( "--bertabs_checkpoint_path", default=None, type=str, required=True, help="Path the official PyTorch dump.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model.", ) a = parser.parse_args() convert_bertabs_checkpoints( args.bertabs_checkpoint_path, args.pytorch_dump_folder_path, )
704
'''simple docstring''' import math from enum import Enum from typing import Optional, Union from torch.optim import Optimizer from torch.optim.lr_scheduler import LambdaLR from .utils import logging a = logging.get_logger(__name__) class __a ( _snake_case ): __UpperCamelCase : int = 'linear' __UpperCamelCase : Tuple = 'cosine' __UpperCamelCase : Tuple = 'cosine_with_restarts' __UpperCamelCase : List[Any] = 'polynomial' __UpperCamelCase : Optional[Any] = 'constant' __UpperCamelCase : Optional[int] = 'constant_with_warmup' __UpperCamelCase : List[Any] = 'piecewise_constant' def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' return LambdaLR(__UpperCAmelCase , lambda __UpperCAmelCase : 1 , last_epoch=__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = -1 ) -> List[Any]: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1.0 , __UpperCAmelCase ) ) return 1.0 return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , last_epoch=__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = -1 ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = step_rules.split(""",""" ) for rule_str in rule_list[:-1]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = rule_str.split(""":""" ) __SCREAMING_SNAKE_CASE = int(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = float(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = value __SCREAMING_SNAKE_CASE = float(rule_list[-1] ) def create_rules_function(__UpperCAmelCase , __UpperCAmelCase ): def rule_func(__UpperCAmelCase ) -> float: __SCREAMING_SNAKE_CASE = sorted(rules_dict.keys() ) for i, sorted_step in enumerate(__UpperCAmelCase ): if steps < sorted_step: return rules_dict[sorted_steps[i]] return last_lr_multiple return rule_func __SCREAMING_SNAKE_CASE = create_rules_function(__UpperCAmelCase , __UpperCAmelCase ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , last_epoch=__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=-1 ) -> int: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) return max( 0.0 , float(num_training_steps - current_step ) / float(max(1 , num_training_steps - num_warmup_steps ) ) ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0.5 , __UpperCAmelCase = -1 ) -> Dict: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = float(current_step - num_warmup_steps ) / float(max(1 , num_training_steps - num_warmup_steps ) ) return max(0.0 , 0.5 * (1.0 + math.cos(math.pi * float(__UpperCAmelCase ) * 2.0 * progress )) ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 1 , __UpperCAmelCase = -1 ) -> Tuple: '''simple docstring''' def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = float(current_step - num_warmup_steps ) / float(max(1 , num_training_steps - num_warmup_steps ) ) if progress >= 1.0: return 0.0 return max(0.0 , 0.5 * (1.0 + math.cos(math.pi * ((float(__UpperCAmelCase ) * progress) % 1.0) )) ) return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=1e-7 , __UpperCAmelCase=1.0 , __UpperCAmelCase=-1 ) -> Tuple: '''simple docstring''' __SCREAMING_SNAKE_CASE = optimizer.defaults["""lr"""] if not (lr_init > lr_end): raise ValueError(f"""lr_end ({lr_end}) must be be smaller than initial lr ({lr_init})""" ) def lr_lambda(__UpperCAmelCase ): if current_step < num_warmup_steps: return float(__UpperCAmelCase ) / float(max(1 , __UpperCAmelCase ) ) elif current_step > num_training_steps: return lr_end / lr_init # as LambdaLR multiplies by lr_init else: __SCREAMING_SNAKE_CASE = lr_init - lr_end __SCREAMING_SNAKE_CASE = num_training_steps - num_warmup_steps __SCREAMING_SNAKE_CASE = 1 - (current_step - num_warmup_steps) / decay_steps __SCREAMING_SNAKE_CASE = lr_range * pct_remaining**power + lr_end return decay / lr_init # as LambdaLR multiplies by lr_init return LambdaLR(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) a = { SchedulerType.LINEAR: get_linear_schedule_with_warmup, SchedulerType.COSINE: get_cosine_schedule_with_warmup, SchedulerType.COSINE_WITH_RESTARTS: get_cosine_with_hard_restarts_schedule_with_warmup, SchedulerType.POLYNOMIAL: get_polynomial_decay_schedule_with_warmup, SchedulerType.CONSTANT: get_constant_schedule, SchedulerType.CONSTANT_WITH_WARMUP: get_constant_schedule_with_warmup, SchedulerType.PIECEWISE_CONSTANT: get_piecewise_constant_schedule, } def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = 1 , __UpperCAmelCase = 1.0 , __UpperCAmelCase = -1 , ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = SchedulerType(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = TYPE_TO_SCHEDULER_FUNCTION[name] if name == SchedulerType.CONSTANT: return schedule_func(__UpperCAmelCase , last_epoch=__UpperCAmelCase ) if name == SchedulerType.PIECEWISE_CONSTANT: return schedule_func(__UpperCAmelCase , step_rules=__UpperCAmelCase , last_epoch=__UpperCAmelCase ) # All other schedulers require `num_warmup_steps` if num_warmup_steps is None: raise ValueError(f"""{name} requires `num_warmup_steps`, please provide that argument.""" ) if name == SchedulerType.CONSTANT_WITH_WARMUP: return schedule_func(__UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , last_epoch=__UpperCAmelCase ) # All other schedulers require `num_training_steps` if num_training_steps is None: raise ValueError(f"""{name} requires `num_training_steps`, please provide that argument.""" ) if name == SchedulerType.COSINE_WITH_RESTARTS: return schedule_func( __UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , num_training_steps=__UpperCAmelCase , num_cycles=__UpperCAmelCase , last_epoch=__UpperCAmelCase , ) if name == SchedulerType.POLYNOMIAL: return schedule_func( __UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , num_training_steps=__UpperCAmelCase , power=__UpperCAmelCase , last_epoch=__UpperCAmelCase , ) return schedule_func( __UpperCAmelCase , num_warmup_steps=__UpperCAmelCase , num_training_steps=__UpperCAmelCase , last_epoch=__UpperCAmelCase )
13
0
'''simple docstring''' from collections import OrderedDict from ...utils import logging from .auto_factory import _BaseAutoModelClass, _LazyAutoMapping, auto_class_update from .configuration_auto import CONFIG_MAPPING_NAMES a = logging.get_logger(__name__) a = OrderedDict( [ # Base model mapping ("albert", "FlaxAlbertModel"), ("bart", "FlaxBartModel"), ("beit", "FlaxBeitModel"), ("bert", "FlaxBertModel"), ("big_bird", "FlaxBigBirdModel"), ("blenderbot", "FlaxBlenderbotModel"), ("blenderbot-small", "FlaxBlenderbotSmallModel"), ("clip", "FlaxCLIPModel"), ("distilbert", "FlaxDistilBertModel"), ("electra", "FlaxElectraModel"), ("gpt-sw3", "FlaxGPT2Model"), ("gpt2", "FlaxGPT2Model"), ("gpt_neo", "FlaxGPTNeoModel"), ("gptj", "FlaxGPTJModel"), ("longt5", "FlaxLongT5Model"), ("marian", "FlaxMarianModel"), ("mbart", "FlaxMBartModel"), ("mt5", "FlaxMT5Model"), ("opt", "FlaxOPTModel"), ("pegasus", "FlaxPegasusModel"), ("regnet", "FlaxRegNetModel"), ("resnet", "FlaxResNetModel"), ("roberta", "FlaxRobertaModel"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormModel"), ("roformer", "FlaxRoFormerModel"), ("t5", "FlaxT5Model"), ("vision-text-dual-encoder", "FlaxVisionTextDualEncoderModel"), ("vit", "FlaxViTModel"), ("wav2vec2", "FlaxWav2Vec2Model"), ("whisper", "FlaxWhisperModel"), ("xglm", "FlaxXGLMModel"), ("xlm-roberta", "FlaxXLMRobertaModel"), ] ) a = OrderedDict( [ # Model for pre-training mapping ("albert", "FlaxAlbertForPreTraining"), ("bart", "FlaxBartForConditionalGeneration"), ("bert", "FlaxBertForPreTraining"), ("big_bird", "FlaxBigBirdForPreTraining"), ("electra", "FlaxElectraForPreTraining"), ("longt5", "FlaxLongT5ForConditionalGeneration"), ("mbart", "FlaxMBartForConditionalGeneration"), ("mt5", "FlaxMT5ForConditionalGeneration"), ("roberta", "FlaxRobertaForMaskedLM"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormForMaskedLM"), ("roformer", "FlaxRoFormerForMaskedLM"), ("t5", "FlaxT5ForConditionalGeneration"), ("wav2vec2", "FlaxWav2Vec2ForPreTraining"), ("whisper", "FlaxWhisperForConditionalGeneration"), ("xlm-roberta", "FlaxXLMRobertaForMaskedLM"), ] ) a = OrderedDict( [ # Model for Masked LM mapping ("albert", "FlaxAlbertForMaskedLM"), ("bart", "FlaxBartForConditionalGeneration"), ("bert", "FlaxBertForMaskedLM"), ("big_bird", "FlaxBigBirdForMaskedLM"), ("distilbert", "FlaxDistilBertForMaskedLM"), ("electra", "FlaxElectraForMaskedLM"), ("mbart", "FlaxMBartForConditionalGeneration"), ("roberta", "FlaxRobertaForMaskedLM"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormForMaskedLM"), ("roformer", "FlaxRoFormerForMaskedLM"), ("xlm-roberta", "FlaxXLMRobertaForMaskedLM"), ] ) a = OrderedDict( [ # Model for Seq2Seq Causal LM mapping ("bart", "FlaxBartForConditionalGeneration"), ("blenderbot", "FlaxBlenderbotForConditionalGeneration"), ("blenderbot-small", "FlaxBlenderbotSmallForConditionalGeneration"), ("encoder-decoder", "FlaxEncoderDecoderModel"), ("longt5", "FlaxLongT5ForConditionalGeneration"), ("marian", "FlaxMarianMTModel"), ("mbart", "FlaxMBartForConditionalGeneration"), ("mt5", "FlaxMT5ForConditionalGeneration"), ("pegasus", "FlaxPegasusForConditionalGeneration"), ("t5", "FlaxT5ForConditionalGeneration"), ] ) a = OrderedDict( [ # Model for Image-classsification ("beit", "FlaxBeitForImageClassification"), ("regnet", "FlaxRegNetForImageClassification"), ("resnet", "FlaxResNetForImageClassification"), ("vit", "FlaxViTForImageClassification"), ] ) a = OrderedDict( [ ("vision-encoder-decoder", "FlaxVisionEncoderDecoderModel"), ] ) a = OrderedDict( [ # Model for Causal LM mapping ("bart", "FlaxBartForCausalLM"), ("bert", "FlaxBertForCausalLM"), ("big_bird", "FlaxBigBirdForCausalLM"), ("electra", "FlaxElectraForCausalLM"), ("gpt-sw3", "FlaxGPT2LMHeadModel"), ("gpt2", "FlaxGPT2LMHeadModel"), ("gpt_neo", "FlaxGPTNeoForCausalLM"), ("gptj", "FlaxGPTJForCausalLM"), ("opt", "FlaxOPTForCausalLM"), ("roberta", "FlaxRobertaForCausalLM"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormForCausalLM"), ("xglm", "FlaxXGLMForCausalLM"), ("xlm-roberta", "FlaxXLMRobertaForCausalLM"), ] ) a = OrderedDict( [ # Model for Sequence Classification mapping ("albert", "FlaxAlbertForSequenceClassification"), ("bart", "FlaxBartForSequenceClassification"), ("bert", "FlaxBertForSequenceClassification"), ("big_bird", "FlaxBigBirdForSequenceClassification"), ("distilbert", "FlaxDistilBertForSequenceClassification"), ("electra", "FlaxElectraForSequenceClassification"), ("mbart", "FlaxMBartForSequenceClassification"), ("roberta", "FlaxRobertaForSequenceClassification"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormForSequenceClassification"), ("roformer", "FlaxRoFormerForSequenceClassification"), ("xlm-roberta", "FlaxXLMRobertaForSequenceClassification"), ] ) a = OrderedDict( [ # Model for Question Answering mapping ("albert", "FlaxAlbertForQuestionAnswering"), ("bart", "FlaxBartForQuestionAnswering"), ("bert", "FlaxBertForQuestionAnswering"), ("big_bird", "FlaxBigBirdForQuestionAnswering"), ("distilbert", "FlaxDistilBertForQuestionAnswering"), ("electra", "FlaxElectraForQuestionAnswering"), ("mbart", "FlaxMBartForQuestionAnswering"), ("roberta", "FlaxRobertaForQuestionAnswering"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormForQuestionAnswering"), ("roformer", "FlaxRoFormerForQuestionAnswering"), ("xlm-roberta", "FlaxXLMRobertaForQuestionAnswering"), ] ) a = OrderedDict( [ # Model for Token Classification mapping ("albert", "FlaxAlbertForTokenClassification"), ("bert", "FlaxBertForTokenClassification"), ("big_bird", "FlaxBigBirdForTokenClassification"), ("distilbert", "FlaxDistilBertForTokenClassification"), ("electra", "FlaxElectraForTokenClassification"), ("roberta", "FlaxRobertaForTokenClassification"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormForTokenClassification"), ("roformer", "FlaxRoFormerForTokenClassification"), ("xlm-roberta", "FlaxXLMRobertaForTokenClassification"), ] ) a = OrderedDict( [ # Model for Multiple Choice mapping ("albert", "FlaxAlbertForMultipleChoice"), ("bert", "FlaxBertForMultipleChoice"), ("big_bird", "FlaxBigBirdForMultipleChoice"), ("distilbert", "FlaxDistilBertForMultipleChoice"), ("electra", "FlaxElectraForMultipleChoice"), ("roberta", "FlaxRobertaForMultipleChoice"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormForMultipleChoice"), ("roformer", "FlaxRoFormerForMultipleChoice"), ("xlm-roberta", "FlaxXLMRobertaForMultipleChoice"), ] ) a = OrderedDict( [ ("bert", "FlaxBertForNextSentencePrediction"), ] ) a = OrderedDict( [ ("speech-encoder-decoder", "FlaxSpeechEncoderDecoderModel"), ("whisper", "FlaxWhisperForConditionalGeneration"), ] ) a = OrderedDict( [ ("whisper", "FlaxWhisperForAudioClassification"), ] ) a = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_MAPPING_NAMES) a = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_PRETRAINING_MAPPING_NAMES) a = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MASKED_LM_MAPPING_NAMES) a = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES ) a = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES ) a = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES) a = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_CAUSAL_LM_MAPPING_NAMES) a = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES ) a = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES ) a = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES ) a = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES ) a = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES ) a = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES ) a = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES ) class __a ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase : Tuple = FLAX_MODEL_MAPPING a = auto_class_update(FlaxAutoModel) class __a ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase : Dict = FLAX_MODEL_FOR_PRETRAINING_MAPPING a = auto_class_update(FlaxAutoModelForPreTraining, head_doc="pretraining") class __a ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase : List[Any] = FLAX_MODEL_FOR_CAUSAL_LM_MAPPING a = auto_class_update(FlaxAutoModelForCausalLM, head_doc="causal language modeling") class __a ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase : str = FLAX_MODEL_FOR_MASKED_LM_MAPPING a = auto_class_update(FlaxAutoModelForMaskedLM, head_doc="masked language modeling") class __a ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase : Tuple = FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING a = auto_class_update( FlaxAutoModelForSeqaSeqLM, head_doc="sequence-to-sequence language modeling", checkpoint_for_example="t5-base" ) class __a ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase : Tuple = FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING a = auto_class_update( FlaxAutoModelForSequenceClassification, head_doc="sequence classification" ) class __a ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase : List[Any] = FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING a = auto_class_update(FlaxAutoModelForQuestionAnswering, head_doc="question answering") class __a ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase : Dict = FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING a = auto_class_update( FlaxAutoModelForTokenClassification, head_doc="token classification" ) class __a ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase : List[Any] = FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING a = auto_class_update(FlaxAutoModelForMultipleChoice, head_doc="multiple choice") class __a ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase : Optional[Any] = FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING a = auto_class_update( FlaxAutoModelForNextSentencePrediction, head_doc="next sentence prediction" ) class __a ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase : Any = FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING a = auto_class_update( FlaxAutoModelForImageClassification, head_doc="image classification" ) class __a ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase : Tuple = FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING a = auto_class_update(FlaxAutoModelForVisionaSeq, head_doc="vision-to-text modeling") class __a ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase : str = FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING a = auto_class_update( FlaxAutoModelForSpeechSeqaSeq, head_doc="sequence-to-sequence speech-to-text modeling" )
705
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a = {"configuration_sew": ["SEW_PRETRAINED_CONFIG_ARCHIVE_MAP", "SEWConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = [ "SEW_PRETRAINED_MODEL_ARCHIVE_LIST", "SEWForCTC", "SEWForSequenceClassification", "SEWModel", "SEWPreTrainedModel", ] if TYPE_CHECKING: from .configuration_sew import SEW_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_sew import ( SEW_PRETRAINED_MODEL_ARCHIVE_LIST, SEWForCTC, SEWForSequenceClassification, SEWModel, SEWPreTrainedModel, ) else: import sys a = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
13
0
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class __a ( unittest.TestCase ): @slow def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = XLMRobertaModel.from_pretrained("""xlm-roberta-base""" ) __SCREAMING_SNAKE_CASE = torch.tensor([[0, 581, 1_0269, 83, 9_9942, 136, 6_0742, 23, 70, 8_0583, 1_8276, 2]] ) # The dog is cute and lives in the garden house __SCREAMING_SNAKE_CASE = torch.Size((1, 12, 768) ) # batch_size, sequence_length, embedding_vector_dim __SCREAMING_SNAKE_CASE = torch.tensor( [[-0.0_101, 0.1_218, -0.0_803, 0.0_801, 0.1_327, 0.0_776, -0.1_215, 0.2_383, 0.3_338, 0.3_106, 0.0_300, 0.0_252]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(__a )["last_hidden_state"].detach() self.assertEqual(output.shape ,__a ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] ,__a ,atol=1E-3 ) ) @slow def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = XLMRobertaModel.from_pretrained("""xlm-roberta-large""" ) __SCREAMING_SNAKE_CASE = torch.tensor([[0, 581, 1_0269, 83, 9_9942, 136, 6_0742, 23, 70, 8_0583, 1_8276, 2]] ) # The dog is cute and lives in the garden house __SCREAMING_SNAKE_CASE = torch.Size((1, 12, 1024) ) # batch_size, sequence_length, embedding_vector_dim __SCREAMING_SNAKE_CASE = torch.tensor( [[-0.0_699, -0.0_318, 0.0_705, -0.1_241, 0.0_999, -0.0_520, 0.1_004, -0.1_838, -0.4_704, 0.1_437, 0.0_821, 0.0_126]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(__a )["last_hidden_state"].detach() self.assertEqual(output.shape ,__a ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] ,__a ,atol=1E-3 ) )
706
'''simple docstring''' import requests from bsa import BeautifulSoup def __magic_name__ ( __UpperCAmelCase = "AAPL" ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""https://in.finance.yahoo.com/quote/{symbol}?s={symbol}""" __SCREAMING_SNAKE_CASE = BeautifulSoup(requests.get(__UpperCAmelCase ).text , """html.parser""" ) __SCREAMING_SNAKE_CASE = """My(6px) Pos(r) smartphone_Mt(6px)""" return soup.find("""div""" , class_=class_ ).find("""span""" ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(F'''Current {symbol:<4} stock price is {stock_price(symbol):>8}''')
13
0
'''simple docstring''' from __future__ import annotations a = { "A": ["B", "C", "E"], "B": ["A", "D", "E"], "C": ["A", "F", "G"], "D": ["B"], "E": ["A", "B", "D"], "F": ["C"], "G": ["C"], } class __a : def __init__( self : List[Any] ,lowerCamelCase : dict[str, list[str]] ,lowerCamelCase : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = graph # mapping node to its parent in resulting breadth first tree __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = source_vertex def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = {self.source_vertex} __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = [self.source_vertex] # first in first out queue while queue: __SCREAMING_SNAKE_CASE = queue.pop(0 ) for adjacent_vertex in self.graph[vertex]: if adjacent_vertex not in visited: visited.add(_snake_case ) __SCREAMING_SNAKE_CASE = vertex queue.append(_snake_case ) def UpperCAmelCase__ ( self : str ,lowerCamelCase : str ): '''simple docstring''' if target_vertex == self.source_vertex: return self.source_vertex __SCREAMING_SNAKE_CASE = self.parent.get(_snake_case ) if target_vertex_parent is None: __SCREAMING_SNAKE_CASE = ( f"""No path from vertex: {self.source_vertex} to vertex: {target_vertex}""" ) raise ValueError(_snake_case ) return self.shortest_path(_snake_case ) + f"""->{target_vertex}""" if __name__ == "__main__": a = Graph(graph, "G") g.breath_first_search() print(g.shortest_path("D")) print(g.shortest_path("G")) print(g.shortest_path("Foo"))
707
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase ) -> bool: '''simple docstring''' if num < 0: return False __SCREAMING_SNAKE_CASE = num __SCREAMING_SNAKE_CASE = 0 while num > 0: __SCREAMING_SNAKE_CASE = rev_num * 10 + (num % 10) num //= 10 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
13
0
'''simple docstring''' import argparse from collections import defaultdict def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Optional[int]: '''simple docstring''' __SCREAMING_SNAKE_CASE = f"""{file}_{class_name}_{test_name}""" done_test[_id] += 1 with open(__UpperCAmelCase , """r""" ) as f: __SCREAMING_SNAKE_CASE = f.readlines() __SCREAMING_SNAKE_CASE = f"""class {class_name}(""" __SCREAMING_SNAKE_CASE = f"""{4 * ' '}def {test_name}(""" __SCREAMING_SNAKE_CASE = f"""{8 * ' '}{correct_line.split()[0]}""" __SCREAMING_SNAKE_CASE = f"""{16 * ' '}{correct_line.split()[0]}""" __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = [] for line in lines: if line.startswith(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = True elif in_class and line.startswith(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = True elif in_class and in_func and (line.startswith(__UpperCAmelCase ) or line.startswith(__UpperCAmelCase )): __SCREAMING_SNAKE_CASE = len(line.split(correct_line.split()[0] )[0] ) count += 1 if count == done_test[_id]: __SCREAMING_SNAKE_CASE = True if in_class and in_func and in_line: if ")" not in line: continue else: __SCREAMING_SNAKE_CASE = True if in_class and in_func and in_line and insert_line: new_lines.append(f"""{spaces * ' '}{correct_line}""" ) __SCREAMING_SNAKE_CASE = __SCREAMING_SNAKE_CASE = __SCREAMING_SNAKE_CASE = __SCREAMING_SNAKE_CASE = False else: new_lines.append(__UpperCAmelCase ) with open(__UpperCAmelCase , """w""" ) as f: for line in new_lines: f.write(__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase=None ) -> Tuple: '''simple docstring''' if fail is not None: with open(__UpperCAmelCase , """r""" ) as f: __SCREAMING_SNAKE_CASE = {l.strip() for l in f.readlines()} else: __SCREAMING_SNAKE_CASE = None with open(__UpperCAmelCase , """r""" ) as f: __SCREAMING_SNAKE_CASE = f.readlines() __SCREAMING_SNAKE_CASE = defaultdict(__UpperCAmelCase ) for line in correct_lines: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = line.split(""";""" ) if test_failures is None or "::".join([file, class_name, test_name] ) in test_failures: overwrite_file(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) if __name__ == "__main__": a = argparse.ArgumentParser() parser.add_argument("--correct_filename", help="filename of tests with expected result") parser.add_argument("--fail_filename", help="filename of test failures", type=str, default=None) a = parser.parse_args() main(args.correct_filename, args.fail_filename)
708
'''simple docstring''' from __future__ import annotations from collections.abc import Callable a = list[list[float | int]] def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Matrix: '''simple docstring''' __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [[0 for _ in range(size + 1 )] for _ in range(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 for row in range(__UpperCAmelCase ): for col in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = matrix[row][col] __SCREAMING_SNAKE_CASE = vector[row][0] __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 while row < size and col < size: # pivoting __SCREAMING_SNAKE_CASE = max((abs(augmented[rowa][col] ), rowa) for rowa in range(__UpperCAmelCase , __UpperCAmelCase ) )[ 1 ] if augmented[pivot_row][col] == 0: col += 1 continue else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = augmented[pivot_row], augmented[row] for rowa in range(row + 1 , __UpperCAmelCase ): __SCREAMING_SNAKE_CASE = augmented[rowa][col] / augmented[row][col] __SCREAMING_SNAKE_CASE = 0 for cola in range(col + 1 , size + 1 ): augmented[rowa][cola] -= augmented[row][cola] * ratio row += 1 col += 1 # back substitution for col in range(1 , __UpperCAmelCase ): for row in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = augmented[row][col] / augmented[col][col] for cola in range(__UpperCAmelCase , size + 1 ): augmented[row][cola] -= augmented[col][cola] * ratio # round to get rid of numbers like 2.000000000000004 return [ [round(augmented[row][size] / augmented[row][row] , 10 )] for row in range(__UpperCAmelCase ) ] def __magic_name__ ( __UpperCAmelCase ) -> Callable[[int], int]: '''simple docstring''' __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = [[0 for _ in range(__UpperCAmelCase )] for _ in range(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = [[0] for _ in range(__UpperCAmelCase )] __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 for x_val, y_val in enumerate(__UpperCAmelCase ): for col in range(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = (x_val + 1) ** (size - col - 1) __SCREAMING_SNAKE_CASE = y_val __SCREAMING_SNAKE_CASE = solve(__UpperCAmelCase , __UpperCAmelCase ) def interpolated_func(__UpperCAmelCase ) -> int: return sum( round(coeffs[x_val][0] ) * (var ** (size - x_val - 1)) for x_val in range(__UpperCAmelCase ) ) return interpolated_func def __magic_name__ ( __UpperCAmelCase ) -> int: '''simple docstring''' return ( 1 - variable + variable**2 - variable**3 + variable**4 - variable**5 + variable**6 - variable**7 + variable**8 - variable**9 + variable**10 ) def __magic_name__ ( __UpperCAmelCase = question_function , __UpperCAmelCase = 10 ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = [func(__UpperCAmelCase ) for x_val in range(1 , order + 1 )] __SCREAMING_SNAKE_CASE = [ interpolate(data_points[:max_coeff] ) for max_coeff in range(1 , order + 1 ) ] __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = 42 for poly in polynomials: __SCREAMING_SNAKE_CASE = 1 while func(__UpperCAmelCase ) == poly(__UpperCAmelCase ): x_val += 1 ret += poly(__UpperCAmelCase ) return ret if __name__ == "__main__": print(F'''{solution() = }''')
13
0
'''simple docstring''' import tempfile import unittest import numpy as np from diffusers import ( DDIMScheduler, DPMSolverMultistepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, LMSDiscreteScheduler, OnnxStableDiffusionPipeline, PNDMScheduler, ) from diffusers.utils.testing_utils import is_onnx_available, nightly, require_onnxruntime, require_torch_gpu from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __a ( UpperCamelCase__, unittest.TestCase ): __UpperCamelCase : Optional[Any] = 'hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline' def UpperCAmelCase__ ( self : Any ,lowerCamelCase : str=0 ): '''simple docstring''' __SCREAMING_SNAKE_CASE = np.random.RandomState(_a ) __SCREAMING_SNAKE_CASE = { """prompt""": """A painting of a squirrel eating a burger""", """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 7.5, """output_type""": """numpy""", } return inputs def UpperCAmelCase__ ( self : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,provider="""CPUExecutionProvider""" ) pipe.set_progress_bar_config(disable=_a ) __SCREAMING_SNAKE_CASE = self.get_dummy_inputs() __SCREAMING_SNAKE_CASE = pipe(**_a ).images __SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) __SCREAMING_SNAKE_CASE = np.array([0.65_072, 0.58_492, 0.48_219, 0.55_521, 0.53_180, 0.55_939, 0.50_697, 0.39_800, 0.46_455] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,provider="""CPUExecutionProvider""" ) __SCREAMING_SNAKE_CASE = PNDMScheduler.from_config(pipe.scheduler.config ,skip_prk_steps=_a ) pipe.set_progress_bar_config(disable=_a ) __SCREAMING_SNAKE_CASE = self.get_dummy_inputs() __SCREAMING_SNAKE_CASE = pipe(**_a ).images __SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) __SCREAMING_SNAKE_CASE = np.array([0.65_863, 0.59_425, 0.49_326, 0.56_313, 0.53_875, 0.56_627, 0.51_065, 0.39_777, 0.46_330] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCAmelCase__ ( self : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,provider="""CPUExecutionProvider""" ) __SCREAMING_SNAKE_CASE = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=_a ) __SCREAMING_SNAKE_CASE = self.get_dummy_inputs() __SCREAMING_SNAKE_CASE = pipe(**_a ).images __SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) __SCREAMING_SNAKE_CASE = np.array([0.53_755, 0.60_786, 0.47_402, 0.49_488, 0.51_869, 0.49_819, 0.47_985, 0.38_957, 0.44_279] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,provider="""CPUExecutionProvider""" ) __SCREAMING_SNAKE_CASE = EulerDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=_a ) __SCREAMING_SNAKE_CASE = self.get_dummy_inputs() __SCREAMING_SNAKE_CASE = pipe(**_a ).images __SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) __SCREAMING_SNAKE_CASE = np.array([0.53_755, 0.60_786, 0.47_402, 0.49_488, 0.51_869, 0.49_819, 0.47_985, 0.38_957, 0.44_279] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,provider="""CPUExecutionProvider""" ) __SCREAMING_SNAKE_CASE = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=_a ) __SCREAMING_SNAKE_CASE = self.get_dummy_inputs() __SCREAMING_SNAKE_CASE = pipe(**_a ).images __SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) __SCREAMING_SNAKE_CASE = np.array([0.53_817, 0.60_812, 0.47_384, 0.49_530, 0.51_894, 0.49_814, 0.47_984, 0.38_958, 0.44_271] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,provider="""CPUExecutionProvider""" ) __SCREAMING_SNAKE_CASE = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=_a ) __SCREAMING_SNAKE_CASE = self.get_dummy_inputs() __SCREAMING_SNAKE_CASE = pipe(**_a ).images __SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) __SCREAMING_SNAKE_CASE = np.array([0.53_895, 0.60_808, 0.47_933, 0.49_608, 0.51_886, 0.49_950, 0.48_053, 0.38_957, 0.44_200] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,provider="""CPUExecutionProvider""" ) pipe.set_progress_bar_config(disable=_a ) __SCREAMING_SNAKE_CASE = self.get_dummy_inputs() __SCREAMING_SNAKE_CASE = 3 * [inputs["""prompt"""]] # forward __SCREAMING_SNAKE_CASE = pipe(**_a ) __SCREAMING_SNAKE_CASE = output.images[0, -3:, -3:, -1] __SCREAMING_SNAKE_CASE = self.get_dummy_inputs() __SCREAMING_SNAKE_CASE = 3 * [inputs.pop("""prompt""" )] __SCREAMING_SNAKE_CASE = pipe.tokenizer( _a ,padding="""max_length""" ,max_length=pipe.tokenizer.model_max_length ,truncation=_a ,return_tensors="""np""" ,) __SCREAMING_SNAKE_CASE = text_inputs["""input_ids"""] __SCREAMING_SNAKE_CASE = pipe.text_encoder(input_ids=text_inputs.astype(np.intaa ) )[0] __SCREAMING_SNAKE_CASE = prompt_embeds # forward __SCREAMING_SNAKE_CASE = pipe(**_a ) __SCREAMING_SNAKE_CASE = output.images[0, -3:, -3:, -1] assert np.abs(image_slice_a.flatten() - image_slice_a.flatten() ).max() < 1E-4 def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,provider="""CPUExecutionProvider""" ) pipe.set_progress_bar_config(disable=_a ) __SCREAMING_SNAKE_CASE = self.get_dummy_inputs() __SCREAMING_SNAKE_CASE = 3 * ["""this is a negative prompt"""] __SCREAMING_SNAKE_CASE = negative_prompt __SCREAMING_SNAKE_CASE = 3 * [inputs["""prompt"""]] # forward __SCREAMING_SNAKE_CASE = pipe(**_a ) __SCREAMING_SNAKE_CASE = output.images[0, -3:, -3:, -1] __SCREAMING_SNAKE_CASE = self.get_dummy_inputs() __SCREAMING_SNAKE_CASE = 3 * [inputs.pop("""prompt""" )] __SCREAMING_SNAKE_CASE = [] for p in [prompt, negative_prompt]: __SCREAMING_SNAKE_CASE = pipe.tokenizer( _a ,padding="""max_length""" ,max_length=pipe.tokenizer.model_max_length ,truncation=_a ,return_tensors="""np""" ,) __SCREAMING_SNAKE_CASE = text_inputs["""input_ids"""] embeds.append(pipe.text_encoder(input_ids=text_inputs.astype(np.intaa ) )[0] ) __SCREAMING_SNAKE_CASE = embeds # forward __SCREAMING_SNAKE_CASE = pipe(**_a ) __SCREAMING_SNAKE_CASE = output.images[0, -3:, -3:, -1] assert np.abs(image_slice_a.flatten() - image_slice_a.flatten() ).max() < 1E-4 @nightly @require_onnxruntime @require_torch_gpu class __a ( unittest.TestCase ): @property def UpperCAmelCase__ ( self : Any ): '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = ort.SessionOptions() __SCREAMING_SNAKE_CASE = False return options def UpperCAmelCase__ ( self : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = OnnxStableDiffusionPipeline.from_pretrained( """CompVis/stable-diffusion-v1-4""" ,revision="""onnx""" ,safety_checker=_a ,feature_extractor=_a ,provider=self.gpu_provider ,sess_options=self.gpu_options ,) sd_pipe.set_progress_bar_config(disable=_a ) __SCREAMING_SNAKE_CASE = """A painting of a squirrel eating a burger""" np.random.seed(0 ) __SCREAMING_SNAKE_CASE = sd_pipe([prompt] ,guidance_scale=6.0 ,num_inference_steps=10 ,output_type="""np""" ) __SCREAMING_SNAKE_CASE = output.images __SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) __SCREAMING_SNAKE_CASE = np.array([0.0_452, 0.0_390, 0.0_087, 0.0_350, 0.0_617, 0.0_364, 0.0_544, 0.0_523, 0.0_720] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = DDIMScheduler.from_pretrained( """runwayml/stable-diffusion-v1-5""" ,subfolder="""scheduler""" ,revision="""onnx""" ) __SCREAMING_SNAKE_CASE = OnnxStableDiffusionPipeline.from_pretrained( """runwayml/stable-diffusion-v1-5""" ,revision="""onnx""" ,scheduler=_a ,safety_checker=_a ,feature_extractor=_a ,provider=self.gpu_provider ,sess_options=self.gpu_options ,) sd_pipe.set_progress_bar_config(disable=_a ) __SCREAMING_SNAKE_CASE = """open neural network exchange""" __SCREAMING_SNAKE_CASE = np.random.RandomState(0 ) __SCREAMING_SNAKE_CASE = sd_pipe([prompt] ,guidance_scale=7.5 ,num_inference_steps=10 ,generator=_a ,output_type="""np""" ) __SCREAMING_SNAKE_CASE = output.images __SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) __SCREAMING_SNAKE_CASE = np.array([0.2_867, 0.1_974, 0.1_481, 0.7_294, 0.7_251, 0.6_667, 0.4_194, 0.5_642, 0.6_486] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = LMSDiscreteScheduler.from_pretrained( """runwayml/stable-diffusion-v1-5""" ,subfolder="""scheduler""" ,revision="""onnx""" ) __SCREAMING_SNAKE_CASE = OnnxStableDiffusionPipeline.from_pretrained( """runwayml/stable-diffusion-v1-5""" ,revision="""onnx""" ,scheduler=_a ,safety_checker=_a ,feature_extractor=_a ,provider=self.gpu_provider ,sess_options=self.gpu_options ,) sd_pipe.set_progress_bar_config(disable=_a ) __SCREAMING_SNAKE_CASE = """open neural network exchange""" __SCREAMING_SNAKE_CASE = np.random.RandomState(0 ) __SCREAMING_SNAKE_CASE = sd_pipe([prompt] ,guidance_scale=7.5 ,num_inference_steps=10 ,generator=_a ,output_type="""np""" ) __SCREAMING_SNAKE_CASE = output.images __SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) __SCREAMING_SNAKE_CASE = np.array([0.2_306, 0.1_959, 0.1_593, 0.6_549, 0.6_394, 0.5_408, 0.5_065, 0.6_010, 0.6_161] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = 0 def test_callback_fn(lowerCamelCase : Dict ,lowerCamelCase : Union[str, Any] ,lowerCamelCase : Any ) -> None: __SCREAMING_SNAKE_CASE = True nonlocal number_of_steps number_of_steps += 1 if step == 0: assert latents.shape == (1, 4, 64, 64) __SCREAMING_SNAKE_CASE = latents[0, -3:, -3:, -1] __SCREAMING_SNAKE_CASE = np.array( [-0.6_772, -0.3_835, -1.2_456, 0.1_905, -1.0_974, 0.6_967, -1.9_353, 0.0_178, 1.0_167] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 1E-3 elif step == 5: assert latents.shape == (1, 4, 64, 64) __SCREAMING_SNAKE_CASE = latents[0, -3:, -3:, -1] __SCREAMING_SNAKE_CASE = np.array( [-0.3_351, 0.2_241, -0.1_837, -0.2_325, -0.6_577, 0.3_393, -0.0_241, 0.5_899, 1.3_875] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 1E-3 __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = OnnxStableDiffusionPipeline.from_pretrained( """runwayml/stable-diffusion-v1-5""" ,revision="""onnx""" ,safety_checker=_a ,feature_extractor=_a ,provider=self.gpu_provider ,sess_options=self.gpu_options ,) pipe.set_progress_bar_config(disable=_a ) __SCREAMING_SNAKE_CASE = """Andromeda galaxy in a bottle""" __SCREAMING_SNAKE_CASE = np.random.RandomState(0 ) pipe( prompt=_a ,num_inference_steps=5 ,guidance_scale=7.5 ,generator=_a ,callback=_a ,callback_steps=1 ,) assert test_callback_fn.has_been_called assert number_of_steps == 6 def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = OnnxStableDiffusionPipeline.from_pretrained( """runwayml/stable-diffusion-v1-5""" ,revision="""onnx""" ,safety_checker=_a ,feature_extractor=_a ,provider=self.gpu_provider ,sess_options=self.gpu_options ,) assert isinstance(_a ,_a ) assert pipe.safety_checker is None __SCREAMING_SNAKE_CASE = pipe("""example prompt""" ,num_inference_steps=2 ).images[0] assert image is not None # check that there's no error when saving a pipeline with one of the models being None with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(_a ) __SCREAMING_SNAKE_CASE = OnnxStableDiffusionPipeline.from_pretrained(_a ) # sanity check that the pipeline still works assert pipe.safety_checker is None __SCREAMING_SNAKE_CASE = pipe("""example prompt""" ,num_inference_steps=2 ).images[0] assert image is not None
709
'''simple docstring''' from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax a = logging.get_logger(__name__) @add_end_docstrings(_snake_case ) class __a ( _snake_case ): def __init__( self : Union[str, Any] ,**lowerCamelCase : str ): '''simple docstring''' super().__init__(**lowerCamelCase ) requires_backends(self ,"""vision""" ) self.check_model_type( TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if self.framework == """tf""" else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING ) def __call__( self : Dict ,lowerCamelCase : Union[str, List[str], "Image", List["Image"]] ,**lowerCamelCase : Optional[Any] ): '''simple docstring''' return super().__call__(lowerCamelCase ,**lowerCamelCase ) def UpperCAmelCase__ ( self : Optional[Any] ,**lowerCamelCase : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = {} if "candidate_labels" in kwargs: __SCREAMING_SNAKE_CASE = kwargs["""candidate_labels"""] if "hypothesis_template" in kwargs: __SCREAMING_SNAKE_CASE = kwargs["""hypothesis_template"""] return preprocess_params, {}, {} def UpperCAmelCase__ ( self : List[Any] ,lowerCamelCase : List[Any] ,lowerCamelCase : Union[str, Any]=None ,lowerCamelCase : Union[str, Any]="This is a photo of {}." ): '''simple docstring''' __SCREAMING_SNAKE_CASE = load_image(lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.image_processor(images=[image] ,return_tensors=self.framework ) __SCREAMING_SNAKE_CASE = candidate_labels __SCREAMING_SNAKE_CASE = [hypothesis_template.format(lowerCamelCase ) for x in candidate_labels] __SCREAMING_SNAKE_CASE = self.tokenizer(lowerCamelCase ,return_tensors=self.framework ,padding=lowerCamelCase ) __SCREAMING_SNAKE_CASE = [text_inputs] return inputs def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = model_inputs.pop("""candidate_labels""" ) __SCREAMING_SNAKE_CASE = model_inputs.pop("""text_inputs""" ) if isinstance(text_inputs[0] ,lowerCamelCase ): __SCREAMING_SNAKE_CASE = text_inputs[0] else: # Batching case. __SCREAMING_SNAKE_CASE = text_inputs[0][0] __SCREAMING_SNAKE_CASE = self.model(**lowerCamelCase ,**lowerCamelCase ) __SCREAMING_SNAKE_CASE = { """candidate_labels""": candidate_labels, """logits""": outputs.logits_per_image, } return model_outputs def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = model_outputs.pop("""candidate_labels""" ) __SCREAMING_SNAKE_CASE = model_outputs["""logits"""][0] if self.framework == "pt": __SCREAMING_SNAKE_CASE = logits.softmax(dim=-1 ).squeeze(-1 ) __SCREAMING_SNAKE_CASE = probs.tolist() if not isinstance(lowerCamelCase ,lowerCamelCase ): __SCREAMING_SNAKE_CASE = [scores] elif self.framework == "tf": __SCREAMING_SNAKE_CASE = stable_softmax(lowerCamelCase ,axis=-1 ) __SCREAMING_SNAKE_CASE = probs.numpy().tolist() else: raise ValueError(f"""Unsupported framework: {self.framework}""" ) __SCREAMING_SNAKE_CASE = [ {"""score""": score, """label""": candidate_label} for score, candidate_label in sorted(zip(lowerCamelCase ,lowerCamelCase ) ,key=lambda lowerCamelCase : -x[0] ) ] return result
13
0
'''simple docstring''' from __future__ import annotations import unittest from transformers import MobileBertConfig, is_tf_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_MODEL_FOR_PRETRAINING_MAPPING, TFMobileBertForMaskedLM, TFMobileBertForMultipleChoice, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertModel, ) @require_tf class __a ( SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, unittest.TestCase ): __UpperCamelCase : Optional[Any] = ( ( TFMobileBertModel, TFMobileBertForMaskedLM, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertForMultipleChoice, ) if is_tf_available() else () ) __UpperCamelCase : List[str] = ( { 'feature-extraction': TFMobileBertModel, 'fill-mask': TFMobileBertForMaskedLM, 'question-answering': TFMobileBertForQuestionAnswering, 'text-classification': TFMobileBertForSequenceClassification, 'token-classification': TFMobileBertForTokenClassification, 'zero-shot': TFMobileBertForSequenceClassification, } if is_tf_available() else {} ) __UpperCamelCase : int = False __UpperCamelCase : List[Any] = False def UpperCAmelCase__ ( self : Any ,lowerCamelCase : List[Any] ,lowerCamelCase : int ,lowerCamelCase : List[Any]=False ): '''simple docstring''' __SCREAMING_SNAKE_CASE = super()._prepare_for_class(UpperCamelCase__ ,UpperCamelCase__ ,return_labels=UpperCamelCase__ ) if return_labels: if model_class in get_values(UpperCamelCase__ ): __SCREAMING_SNAKE_CASE = tf.zeros(self.model_tester.batch_size ,dtype=tf.intaa ) return inputs_dict class __a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : List[Any] ,lowerCamelCase : int ,lowerCamelCase : Dict=13 ,lowerCamelCase : Optional[Any]=7 ,lowerCamelCase : Optional[Any]=True ,lowerCamelCase : Optional[int]=True ,lowerCamelCase : Tuple=True ,lowerCamelCase : List[Any]=True ,lowerCamelCase : Optional[Any]=99 ,lowerCamelCase : Optional[int]=32 ,lowerCamelCase : Any=32 ,lowerCamelCase : Dict=2 ,lowerCamelCase : Any=4 ,lowerCamelCase : str=37 ,lowerCamelCase : Optional[Any]="gelu" ,lowerCamelCase : Dict=0.1 ,lowerCamelCase : str=0.1 ,lowerCamelCase : str=512 ,lowerCamelCase : Optional[int]=16 ,lowerCamelCase : List[str]=2 ,lowerCamelCase : Any=0.02 ,lowerCamelCase : Optional[int]=3 ,lowerCamelCase : Union[str, Any]=4 ,lowerCamelCase : Optional[Any]=None ,): '''simple docstring''' __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = seq_length __SCREAMING_SNAKE_CASE = is_training __SCREAMING_SNAKE_CASE = use_input_mask __SCREAMING_SNAKE_CASE = use_token_type_ids __SCREAMING_SNAKE_CASE = use_labels __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = hidden_dropout_prob __SCREAMING_SNAKE_CASE = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = type_vocab_size __SCREAMING_SNAKE_CASE = type_sequence_label_size __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = num_labels __SCREAMING_SNAKE_CASE = num_choices __SCREAMING_SNAKE_CASE = scope __SCREAMING_SNAKE_CASE = embedding_size def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) __SCREAMING_SNAKE_CASE = None if self.use_input_mask: __SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.seq_length] ) __SCREAMING_SNAKE_CASE = None if self.use_token_type_ids: __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None if self.use_labels: __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] ,self.num_choices ) __SCREAMING_SNAKE_CASE = MobileBertConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,type_vocab_size=self.type_vocab_size ,initializer_range=self.initializer_range ,embedding_size=self.embedding_size ,) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : Union[str, Any] ,lowerCamelCase : Any ,lowerCamelCase : Optional[int] ,lowerCamelCase : int ,lowerCamelCase : Optional[Any] ,lowerCamelCase : Any ,lowerCamelCase : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TFMobileBertModel(config=UpperCamelCase__ ) __SCREAMING_SNAKE_CASE = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} __SCREAMING_SNAKE_CASE = model(UpperCamelCase__ ) __SCREAMING_SNAKE_CASE = [input_ids, input_mask] __SCREAMING_SNAKE_CASE = model(UpperCamelCase__ ) __SCREAMING_SNAKE_CASE = model(UpperCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape ,(self.batch_size, self.hidden_size) ) def UpperCAmelCase__ ( self : List[str] ,lowerCamelCase : List[str] ,lowerCamelCase : str ,lowerCamelCase : Optional[Any] ,lowerCamelCase : Any ,lowerCamelCase : int ,lowerCamelCase : Tuple ,lowerCamelCase : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TFMobileBertForMaskedLM(config=UpperCamelCase__ ) __SCREAMING_SNAKE_CASE = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} __SCREAMING_SNAKE_CASE = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def UpperCAmelCase__ ( self : str ,lowerCamelCase : Tuple ,lowerCamelCase : str ,lowerCamelCase : Union[str, Any] ,lowerCamelCase : int ,lowerCamelCase : List[str] ,lowerCamelCase : Any ,lowerCamelCase : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TFMobileBertForNextSentencePrediction(config=UpperCamelCase__ ) __SCREAMING_SNAKE_CASE = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} __SCREAMING_SNAKE_CASE = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, 2) ) def UpperCAmelCase__ ( self : int ,lowerCamelCase : Any ,lowerCamelCase : List[str] ,lowerCamelCase : Union[str, Any] ,lowerCamelCase : List[str] ,lowerCamelCase : Optional[Any] ,lowerCamelCase : Optional[Any] ,lowerCamelCase : Tuple ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TFMobileBertForPreTraining(config=UpperCamelCase__ ) __SCREAMING_SNAKE_CASE = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} __SCREAMING_SNAKE_CASE = model(UpperCamelCase__ ) self.parent.assertEqual( result.prediction_logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.seq_relationship_logits.shape ,(self.batch_size, 2) ) def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : Optional[Any] ,lowerCamelCase : Dict ,lowerCamelCase : List[str] ,lowerCamelCase : int ,lowerCamelCase : Any ,lowerCamelCase : List[Any] ,lowerCamelCase : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.num_labels __SCREAMING_SNAKE_CASE = TFMobileBertForSequenceClassification(config=UpperCamelCase__ ) __SCREAMING_SNAKE_CASE = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} __SCREAMING_SNAKE_CASE = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def UpperCAmelCase__ ( self : Optional[Any] ,lowerCamelCase : Optional[Any] ,lowerCamelCase : int ,lowerCamelCase : List[Any] ,lowerCamelCase : Dict ,lowerCamelCase : List[Any] ,lowerCamelCase : Optional[int] ,lowerCamelCase : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.num_choices __SCREAMING_SNAKE_CASE = TFMobileBertForMultipleChoice(config=UpperCamelCase__ ) __SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(UpperCamelCase__ ,1 ) ,(1, self.num_choices, 1) ) __SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(UpperCamelCase__ ,1 ) ,(1, self.num_choices, 1) ) __SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(UpperCamelCase__ ,1 ) ,(1, self.num_choices, 1) ) __SCREAMING_SNAKE_CASE = { '''input_ids''': multiple_choice_inputs_ids, '''attention_mask''': multiple_choice_input_mask, '''token_type_ids''': multiple_choice_token_type_ids, } __SCREAMING_SNAKE_CASE = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_choices) ) def UpperCAmelCase__ ( self : int ,lowerCamelCase : Union[str, Any] ,lowerCamelCase : Union[str, Any] ,lowerCamelCase : List[str] ,lowerCamelCase : List[Any] ,lowerCamelCase : Any ,lowerCamelCase : Optional[int] ,lowerCamelCase : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.num_labels __SCREAMING_SNAKE_CASE = TFMobileBertForTokenClassification(config=UpperCamelCase__ ) __SCREAMING_SNAKE_CASE = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} __SCREAMING_SNAKE_CASE = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) ) def UpperCAmelCase__ ( self : int ,lowerCamelCase : List[Any] ,lowerCamelCase : Optional[int] ,lowerCamelCase : int ,lowerCamelCase : int ,lowerCamelCase : Optional[Any] ,lowerCamelCase : List[str] ,lowerCamelCase : Any ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TFMobileBertForQuestionAnswering(config=UpperCamelCase__ ) __SCREAMING_SNAKE_CASE = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} __SCREAMING_SNAKE_CASE = model(UpperCamelCase__ ) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def UpperCAmelCase__ ( self : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( __SCREAMING_SNAKE_CASE ) = config_and_inputs __SCREAMING_SNAKE_CASE = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TFMobileBertModelTest.TFMobileBertModelTester(self ) __SCREAMING_SNAKE_CASE = ConfigTester(self ,config_class=UpperCamelCase__ ,hidden_size=37 ) def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_model(*UpperCamelCase__ ) def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_masked_lm(*UpperCamelCase__ ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_multiple_choice(*UpperCamelCase__ ) def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*UpperCamelCase__ ) def UpperCAmelCase__ ( self : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_pretraining(*UpperCamelCase__ ) def UpperCAmelCase__ ( self : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_question_answering(*UpperCamelCase__ ) def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_sequence_classification(*UpperCamelCase__ ) def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_token_classification(*UpperCamelCase__ ) @slow def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' for model_name in ["google/mobilebert-uncased"]: __SCREAMING_SNAKE_CASE = TFMobileBertModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) @require_tf class __a ( unittest.TestCase ): @slow def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = TFMobileBertForPreTraining.from_pretrained("""google/mobilebert-uncased""" ) __SCREAMING_SNAKE_CASE = tf.constant([[0, 1, 2, 3, 4, 5]] ) __SCREAMING_SNAKE_CASE = model(UpperCamelCase__ )[0] __SCREAMING_SNAKE_CASE = [1, 6, 3_0522] self.assertEqual(output.shape ,UpperCamelCase__ ) __SCREAMING_SNAKE_CASE = tf.constant( [ [ [-4.5_919_547, -9.248_295, -9.645_256], [-6.7_306_175, -6.440_284, -6.6_052_837], [-7.2_743_506, -6.7_847_915, -6.024_673], ] ] ) tf.debugging.assert_near(output[:, :3, :3] ,UpperCamelCase__ ,atol=1E-4 )
710
'''simple docstring''' from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers a = [ "python", "tqdm", "regex", "requests", "packaging", "filelock", "numpy", "tokenizers", "huggingface-hub", "safetensors", "accelerate", "pyyaml", ] for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed elif pkg == "accelerate": # must be loaded here, or else tqdm check may fail from .utils import is_accelerate_available # Maybe switch to is_torch_available in the future here so that Accelerate is hard dep of # Transformers with PyTorch if not is_accelerate_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(F'''can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py''') def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase=None ) -> Optional[Any]: '''simple docstring''' require_version(deps[pkg] , __UpperCAmelCase )
13
0
'''simple docstring''' from typing import Any class __a : def __init__( self : Union[str, Any] ,lowerCamelCase : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = data __SCREAMING_SNAKE_CASE = None class __a : def __init__( self : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = None def UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.head while temp is not None: print(temp.data ,end=""" """ ) __SCREAMING_SNAKE_CASE = temp.next print() def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = Node(_lowerCamelCase ) __SCREAMING_SNAKE_CASE = self.head __SCREAMING_SNAKE_CASE = new_node def UpperCAmelCase__ ( self : Optional[int] ,lowerCamelCase : Any ,lowerCamelCase : Optional[Any] ): '''simple docstring''' if node_data_a == node_data_a: return else: __SCREAMING_SNAKE_CASE = self.head while node_a is not None and node_a.data != node_data_a: __SCREAMING_SNAKE_CASE = node_a.next __SCREAMING_SNAKE_CASE = self.head while node_a is not None and node_a.data != node_data_a: __SCREAMING_SNAKE_CASE = node_a.next if node_a is None or node_a is None: return __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = node_a.data, node_a.data if __name__ == "__main__": a = LinkedList() for i in range(5, 0, -1): ll.push(i) ll.print_list() ll.swap_nodes(1, 4) print("After swapping") ll.print_list()
711
'''simple docstring''' import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import numpy as np import pandas as pd from datasets import load_dataset import transformers from transformers import ( AutoConfig, BartForSequenceClassification, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, TapexTokenizer, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.17.0.dev0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/text-classification/requirements.txt") a = logging.getLogger(__name__) @dataclass class __a : __UpperCamelCase : Optional[str] = field( default='tab_fact', metadata={'help': 'The name of the dataset to use (via the datasets library).'} ) __UpperCamelCase : Optional[str] = field( default='tab_fact', metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'}, ) __UpperCamelCase : int = field( default=1024, metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) }, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={'help': 'Overwrite the cached preprocessed datasets or not.'} ) __UpperCamelCase : bool = field( default=_snake_case, metadata={ 'help': ( 'Whether to pad all samples to `max_seq_length`. ' 'If False, will pad the samples dynamically when batching to the maximum length in the batch.' ) }, ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) }, ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) }, ) __UpperCamelCase : Optional[int] = field( default=_snake_case, metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of prediction examples to this ' 'value if set.' ) }, ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'A csv or a json file containing the training data.'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'A csv or a json file containing the validation data.'} ) __UpperCamelCase : Optional[str] = field(default=_snake_case, metadata={'help': 'A csv or a json file containing the test data.'} ) def UpperCAmelCase__ ( self : int ): '''simple docstring''' if self.dataset_name is not None: pass elif self.train_file is None or self.validation_file is None: raise ValueError("""Need either a GLUE task, a training/validation file or a dataset name.""" ) else: __SCREAMING_SNAKE_CASE = self.train_file.split(""".""" )[-1] assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file." __SCREAMING_SNAKE_CASE = self.validation_file.split(""".""" )[-1] assert ( validation_extension == train_extension ), "`validation_file` should have the same extension (csv or json) as `train_file`." @dataclass class __a : __UpperCamelCase : str = field( default=_snake_case, metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) __UpperCamelCase : Optional[str] = field( default=_snake_case, metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'}, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={'help': 'Whether to use one of the fast tokenizer (backed by the tokenizers library) or not.'}, ) __UpperCamelCase : str = field( default='main', metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'}, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) }, ) def __magic_name__ ( ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = parser.parse_args_into_dataclasses() # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) __SCREAMING_SNAKE_CASE = training_args.get_process_log_level() logger.setLevel(__UpperCAmelCase ) datasets.utils.logging.set_verbosity(__UpperCAmelCase ) transformers.utils.logging.set_verbosity(__UpperCAmelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + f"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(f"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. __SCREAMING_SNAKE_CASE = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: __SCREAMING_SNAKE_CASE = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. """ """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON training and evaluation files (see below) # or specify a GLUE benchmark task (the dataset will be downloaded automatically from the datasets Hub). # # For JSON files, this script will use the `question` column for the input question and `table` column for the corresponding table. # # If the CSVs/JSONs contain only one non-label column, the script does single sentence classification on this # single column. You can easily tweak this behavior (see below) # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. __SCREAMING_SNAKE_CASE = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) else: # Loading a dataset from your local files. # CSV/JSON training and evaluation files are needed. __SCREAMING_SNAKE_CASE = {"""train""": data_args.train_file, """validation""": data_args.validation_file} # Get the test dataset: you can provide your own CSV/JSON test file (see below) # when you use `do_predict` without specifying a GLUE benchmark task. if training_args.do_predict: if data_args.test_file is not None: __SCREAMING_SNAKE_CASE = data_args.train_file.split(""".""" )[-1] __SCREAMING_SNAKE_CASE = data_args.test_file.split(""".""" )[-1] assert ( test_extension == train_extension ), "`test_file` should have the same extension (csv or json) as `train_file`." __SCREAMING_SNAKE_CASE = data_args.test_file else: raise ValueError("""Need either a GLUE task or a test file for `do_predict`.""" ) for key in data_files.keys(): logger.info(f"""load a local file for {key}: {data_files[key]}""" ) if data_args.train_file.endswith(""".csv""" ): # Loading a dataset from local csv files __SCREAMING_SNAKE_CASE = load_dataset("""csv""" , data_files=__UpperCAmelCase , cache_dir=model_args.cache_dir ) else: # Loading a dataset from local json files __SCREAMING_SNAKE_CASE = load_dataset("""json""" , data_files=__UpperCAmelCase , cache_dir=model_args.cache_dir ) # See more about loading any type of standard or custom dataset at # https://huggingface.co/docs/datasets/loading_datasets.html. # Labels __SCREAMING_SNAKE_CASE = raw_datasets["""train"""].features["""label"""].names __SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) # Load pretrained model and tokenizer # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. __SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=__UpperCAmelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # load tapex tokenizer __SCREAMING_SNAKE_CASE = TapexTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , add_prefix_space=__UpperCAmelCase , ) __SCREAMING_SNAKE_CASE = BartForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=__UpperCAmelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Padding strategy if data_args.pad_to_max_length: __SCREAMING_SNAKE_CASE = """max_length""" else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch __SCREAMING_SNAKE_CASE = False # Some models have set the order of the labels to use, so let's make sure we do use it. __SCREAMING_SNAKE_CASE = {"""Refused""": 0, """Entailed""": 1} __SCREAMING_SNAKE_CASE = {0: """Refused""", 1: """Entailed"""} if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( f"""The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the""" f"""model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}.""" ) __SCREAMING_SNAKE_CASE = min(data_args.max_seq_length , tokenizer.model_max_length ) def preprocess_tabfact_function(__UpperCAmelCase ): # Tokenize the texts def _convert_table_text_to_pandas(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = [_table_row.split("""#""" ) for _table_row in _table_text.strip("""\n""" ).split("""\n""" )] __SCREAMING_SNAKE_CASE = pd.DataFrame.from_records(_table_content[1:] , columns=_table_content[0] ) return _table_pd __SCREAMING_SNAKE_CASE = examples["""statement"""] __SCREAMING_SNAKE_CASE = list(map(_convert_table_text_to_pandas , examples["""table_text"""] ) ) __SCREAMING_SNAKE_CASE = tokenizer(__UpperCAmelCase , __UpperCAmelCase , padding=__UpperCAmelCase , max_length=__UpperCAmelCase , truncation=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = examples["""label"""] return result with training_args.main_process_first(desc="""dataset map pre-processing""" ): __SCREAMING_SNAKE_CASE = raw_datasets.map( __UpperCAmelCase , batched=__UpperCAmelCase , load_from_cache_file=not data_args.overwrite_cache , desc="""Running tokenizer on dataset""" , ) if training_args.do_train: if "train" not in raw_datasets: raise ValueError("""--do_train requires a train dataset""" ) __SCREAMING_SNAKE_CASE = raw_datasets["""train"""] if data_args.max_train_samples is not None: __SCREAMING_SNAKE_CASE = train_dataset.select(range(data_args.max_train_samples ) ) if training_args.do_eval: if "validation" not in raw_datasets and "validation_matched" not in raw_datasets: raise ValueError("""--do_eval requires a validation dataset""" ) __SCREAMING_SNAKE_CASE = raw_datasets["""validation"""] if data_args.max_eval_samples is not None: __SCREAMING_SNAKE_CASE = eval_dataset.select(range(data_args.max_eval_samples ) ) if training_args.do_predict or data_args.test_file is not None: if "test" not in raw_datasets and "test_matched" not in raw_datasets: raise ValueError("""--do_predict requires a test dataset""" ) __SCREAMING_SNAKE_CASE = raw_datasets["""test"""] if data_args.max_predict_samples is not None: __SCREAMING_SNAKE_CASE = predict_dataset.select(range(data_args.max_predict_samples ) ) # Log a few random samples from the training set: if training_args.do_train: for index in random.sample(range(len(__UpperCAmelCase ) ) , 3 ): logger.info(f"""Sample {index} of the training set: {train_dataset[index]}.""" ) # You can define your custom compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = p.predictions[0] if isinstance(p.predictions , __UpperCAmelCase ) else p.predictions __SCREAMING_SNAKE_CASE = np.argmax(__UpperCAmelCase , axis=1 ) return {"accuracy": (preds == p.label_ids).astype(np.floataa ).mean().item()} # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: __SCREAMING_SNAKE_CASE = default_data_collator elif training_args.fpaa: __SCREAMING_SNAKE_CASE = DataCollatorWithPadding(__UpperCAmelCase , pad_to_multiple_of=8 ) else: __SCREAMING_SNAKE_CASE = None # Initialize our Trainer __SCREAMING_SNAKE_CASE = Trainer( model=__UpperCAmelCase , args=__UpperCAmelCase , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=__UpperCAmelCase , tokenizer=__UpperCAmelCase , data_collator=__UpperCAmelCase , ) # Training if training_args.do_train: __SCREAMING_SNAKE_CASE = None if training_args.resume_from_checkpoint is not None: __SCREAMING_SNAKE_CASE = training_args.resume_from_checkpoint elif last_checkpoint is not None: __SCREAMING_SNAKE_CASE = last_checkpoint __SCREAMING_SNAKE_CASE = trainer.train(resume_from_checkpoint=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = train_result.metrics __SCREAMING_SNAKE_CASE = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(__UpperCAmelCase ) ) __SCREAMING_SNAKE_CASE = min(__UpperCAmelCase , len(__UpperCAmelCase ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics("""train""" , __UpperCAmelCase ) trainer.save_metrics("""train""" , __UpperCAmelCase ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info("""*** Evaluate ***""" ) __SCREAMING_SNAKE_CASE = trainer.evaluate(eval_dataset=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(__UpperCAmelCase ) __SCREAMING_SNAKE_CASE = min(__UpperCAmelCase , len(__UpperCAmelCase ) ) trainer.log_metrics("""eval""" , __UpperCAmelCase ) trainer.save_metrics("""eval""" , __UpperCAmelCase ) if training_args.do_predict: logger.info("""*** Predict ***""" ) # Removing the `label` columns because it contains -1 and Trainer won't like that. __SCREAMING_SNAKE_CASE = predict_dataset.remove_columns("""label""" ) __SCREAMING_SNAKE_CASE = trainer.predict(__UpperCAmelCase , metric_key_prefix="""predict""" ).predictions __SCREAMING_SNAKE_CASE = np.argmax(__UpperCAmelCase , axis=1 ) __SCREAMING_SNAKE_CASE = os.path.join(training_args.output_dir , """predict_results_tabfact.txt""" ) if trainer.is_world_process_zero(): with open(__UpperCAmelCase , """w""" ) as writer: logger.info("""***** Predict Results *****""" ) writer.write("""index\tprediction\n""" ) for index, item in enumerate(__UpperCAmelCase ): __SCREAMING_SNAKE_CASE = label_list[item] writer.write(f"""{index}\t{item}\n""" ) __SCREAMING_SNAKE_CASE = {"""finetuned_from""": model_args.model_name_or_path, """tasks""": """text-classification"""} if training_args.push_to_hub: trainer.push_to_hub(**__UpperCAmelCase ) else: trainer.create_model_card(**__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase ) -> Any: '''simple docstring''' main() if __name__ == "__main__": main()
13
0
'''simple docstring''' import argparse import json from typing import List from ltp import LTP from transformers import BertTokenizer def __magic_name__ ( __UpperCAmelCase ) -> Optional[int]: '''simple docstring''' if ( (cp >= 0X4E00 and cp <= 0X9FFF) or (cp >= 0X3400 and cp <= 0X4DBF) # or (cp >= 0X2_0000 and cp <= 0X2_A6DF) # or (cp >= 0X2_A700 and cp <= 0X2_B73F) # or (cp >= 0X2_B740 and cp <= 0X2_B81F) # or (cp >= 0X2_B820 and cp <= 0X2_CEAF) # or (cp >= 0XF900 and cp <= 0XFAFF) or (cp >= 0X2_F800 and cp <= 0X2_FA1F) # ): # return True return False def __magic_name__ ( __UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' for char in word: __SCREAMING_SNAKE_CASE = ord(__snake_case ) if not _is_chinese_char(__snake_case ): return 0 return 1 def __magic_name__ ( __UpperCAmelCase ) -> Any: '''simple docstring''' __SCREAMING_SNAKE_CASE = set() for token in tokens: __SCREAMING_SNAKE_CASE = len(__snake_case ) > 1 and is_chinese(__snake_case ) if chinese_word: word_set.add(__snake_case ) __SCREAMING_SNAKE_CASE = list(__snake_case ) return word_list def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Dict: '''simple docstring''' if not chinese_word_set: return bert_tokens __SCREAMING_SNAKE_CASE = max([len(__snake_case ) for w in chinese_word_set] ) __SCREAMING_SNAKE_CASE = bert_tokens __SCREAMING_SNAKE_CASE = 0, len(__snake_case ) while start < end: __SCREAMING_SNAKE_CASE = True if is_chinese(bert_word[start] ): __SCREAMING_SNAKE_CASE = min(end - start , __snake_case ) for i in range(__snake_case , 1 , -1 ): __SCREAMING_SNAKE_CASE = "".join(bert_word[start : start + i] ) if whole_word in chinese_word_set: for j in range(start + 1 , start + i ): __SCREAMING_SNAKE_CASE = "##" + bert_word[j] __SCREAMING_SNAKE_CASE = start + i __SCREAMING_SNAKE_CASE = False break if single_word: start += 1 return bert_word def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' __SCREAMING_SNAKE_CASE = [] for i in range(0 , len(__snake_case ) , 100 ): __SCREAMING_SNAKE_CASE = ltp_tokenizer.seg(lines[i : i + 100] )[0] __SCREAMING_SNAKE_CASE = [get_chinese_word(__snake_case ) for r in res] ltp_res.extend(__snake_case ) assert len(__snake_case ) == len(__snake_case ) __SCREAMING_SNAKE_CASE = [] for i in range(0 , len(__snake_case ) , 100 ): __SCREAMING_SNAKE_CASE = bert_tokenizer(lines[i : i + 100] , add_special_tokens=__snake_case , truncation=__snake_case , max_length=512 ) bert_res.extend(res["""input_ids"""] ) assert len(__snake_case ) == len(__snake_case ) __SCREAMING_SNAKE_CASE = [] for input_ids, chinese_word in zip(__snake_case , __snake_case ): __SCREAMING_SNAKE_CASE = [] for id in input_ids: __SCREAMING_SNAKE_CASE = bert_tokenizer._convert_id_to_token(__snake_case ) input_tokens.append(__snake_case ) __SCREAMING_SNAKE_CASE = add_sub_symbol(__snake_case , __snake_case ) __SCREAMING_SNAKE_CASE = [] # We only save pos of chinese subwords start with ##, which mean is part of a whole word. for i, token in enumerate(__snake_case ): if token[:2] == "##": __SCREAMING_SNAKE_CASE = token[2:] # save chinese tokens' pos if len(__snake_case ) == 1 and _is_chinese_char(ord(__snake_case ) ): ref_id.append(__snake_case ) ref_ids.append(__snake_case ) assert len(__snake_case ) == len(__snake_case ) return ref_ids def __magic_name__ ( __UpperCAmelCase ) -> List[str]: '''simple docstring''' with open(args.file_name , """r""" , encoding="""utf-8""" ) as f: __SCREAMING_SNAKE_CASE = f.readlines() __SCREAMING_SNAKE_CASE = [line.strip() for line in data if len(__snake_case ) > 0 and not line.isspace()] # avoid delimiter like '\u2029' __SCREAMING_SNAKE_CASE = LTP(args.ltp ) # faster in GPU device __SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained(args.bert ) __SCREAMING_SNAKE_CASE = prepare_ref(__snake_case , __snake_case , __snake_case ) with open(args.save_path , """w""" , encoding="""utf-8""" ) as f: __SCREAMING_SNAKE_CASE = [json.dumps(__snake_case ) + "\n" for ref in ref_ids] f.writelines(__snake_case ) if __name__ == "__main__": a = argparse.ArgumentParser(description="prepare_chinese_ref") parser.add_argument( "--file_name", type=str, default="./resources/chinese-demo.txt", help="file need process, same as training data in lm", ) parser.add_argument( "--ltp", type=str, default="./resources/ltp", help="resources for LTP tokenizer, usually a path" ) parser.add_argument("--bert", type=str, default="./resources/robert", help="resources for Bert tokenizer") parser.add_argument("--save_path", type=str, default="./resources/ref.txt", help="path to save res") a = parser.parse_args() main(args)
712
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_flax_available, is_torch_available, is_transformers_available, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .multicontrolnet import MultiControlNetModel from .pipeline_controlnet import StableDiffusionControlNetPipeline from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline if is_transformers_available() and is_flax_available(): from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline
13
0
'''simple docstring''' import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __a : @staticmethod def UpperCAmelCase__ ( *lowerCamelCase : int ,**lowerCamelCase : List[Any] ): '''simple docstring''' pass @is_pipeline_test @require_vision class __a ( unittest.TestCase ): @require_torch def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = pipeline( model="""hf-internal-testing/tiny-random-clip-zero-shot-image-classification""" ,) __SCREAMING_SNAKE_CASE = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) __SCREAMING_SNAKE_CASE = image_classifier(__A ,candidate_labels=["""a""", """b""", """c"""] ) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(__A ) ,[ [{"""score""": 0.333, """label""": """a"""}, {"""score""": 0.333, """label""": """b"""}, {"""score""": 0.333, """label""": """c"""}], [{"""score""": 0.333, """label""": """a"""}, {"""score""": 0.333, """label""": """c"""}, {"""score""": 0.333, """label""": """b"""}], ] ,) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5 ,candidate_labels=["""A""", """B""", """C"""] ,batch_size=2 ) self.assertEqual( nested_simplify(__A ) ,[ [ {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, ], [ {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, ], [ {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, ], [ {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, ], [ {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, ], ] ,) @require_tf def UpperCAmelCase__ ( self : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = pipeline( model="""hf-internal-testing/tiny-random-clip-zero-shot-image-classification""" ,framework="""tf""" ) __SCREAMING_SNAKE_CASE = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) __SCREAMING_SNAKE_CASE = image_classifier(__A ,candidate_labels=["""a""", """b""", """c"""] ) self.assertEqual( nested_simplify(__A ) ,[{"""score""": 0.333, """label""": """a"""}, {"""score""": 0.333, """label""": """b"""}, {"""score""": 0.333, """label""": """c"""}] ,) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5 ,candidate_labels=["""A""", """B""", """C"""] ,batch_size=2 ) self.assertEqual( nested_simplify(__A ) ,[ [ {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, ], [ {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, ], [ {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, ], [ {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, ], [ {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, {"""score""": 0.333, """label""": ANY(__A )}, ], ] ,) @slow @require_torch def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = pipeline( task="""zero-shot-image-classification""" ,model="""openai/clip-vit-base-patch32""" ,) # This is an image of 2 cats with remotes and no planes __SCREAMING_SNAKE_CASE = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) __SCREAMING_SNAKE_CASE = image_classifier(__A ,candidate_labels=["""cat""", """plane""", """remote"""] ) self.assertEqual( nested_simplify(__A ) ,[ {"""score""": 0.511, """label""": """remote"""}, {"""score""": 0.485, """label""": """cat"""}, {"""score""": 0.004, """label""": """plane"""}, ] ,) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5 ,candidate_labels=["""cat""", """plane""", """remote"""] ,batch_size=2 ) self.assertEqual( nested_simplify(__A ) ,[ [ {"""score""": 0.511, """label""": """remote"""}, {"""score""": 0.485, """label""": """cat"""}, {"""score""": 0.004, """label""": """plane"""}, ], ] * 5 ,) @slow @require_tf def UpperCAmelCase__ ( self : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE = pipeline( task="""zero-shot-image-classification""" ,model="""openai/clip-vit-base-patch32""" ,framework="""tf""" ) # This is an image of 2 cats with remotes and no planes __SCREAMING_SNAKE_CASE = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) __SCREAMING_SNAKE_CASE = image_classifier(__A ,candidate_labels=["""cat""", """plane""", """remote"""] ) self.assertEqual( nested_simplify(__A ) ,[ {"""score""": 0.511, """label""": """remote"""}, {"""score""": 0.485, """label""": """cat"""}, {"""score""": 0.004, """label""": """plane"""}, ] ,) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5 ,candidate_labels=["""cat""", """plane""", """remote"""] ,batch_size=2 ) self.assertEqual( nested_simplify(__A ) ,[ [ {"""score""": 0.511, """label""": """remote"""}, {"""score""": 0.485, """label""": """cat"""}, {"""score""": 0.004, """label""": """plane"""}, ], ] * 5 ,)
713
'''simple docstring''' import requests from bsa import BeautifulSoup def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> str: '''simple docstring''' __SCREAMING_SNAKE_CASE = BeautifulSoup(requests.get(__UpperCAmelCase , params=__UpperCAmelCase ).content , """html.parser""" ) __SCREAMING_SNAKE_CASE = soup.find("""div""" , attrs={"""class""": """gs_ri"""} ) __SCREAMING_SNAKE_CASE = div.find("""div""" , attrs={"""class""": """gs_fl"""} ).find_all("""a""" ) return anchors[2].get_text() if __name__ == "__main__": a = { "title": ( "Precisely geometry controlled microsupercapacitors for ultrahigh areal " "capacitance, volumetric capacitance, and energy density" ), "journal": "Chem. Mater.", "volume": 30, "pages": "3979-3990", "year": 2018, "hl": "en", } print(get_citation("https://scholar.google.com/scholar_lookup", params=params))
13
0
def __magic_name__ ( __UpperCAmelCase = 100 ) -> Any: '''simple docstring''' __SCREAMING_SNAKE_CASE = (n * (n + 1) // 2) ** 2 __SCREAMING_SNAKE_CASE = n * (n + 1) * (2 * n + 1) // 6 return sum_cubes - sum_squares if __name__ == "__main__": print(F'''{solution() = }''')
714
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a = logging.get_logger(__name__) a = { "camembert-base": "https://huggingface.co/camembert-base/resolve/main/config.json", "umberto-commoncrawl-cased-v1": ( "https://huggingface.co/Musixmatch/umberto-commoncrawl-cased-v1/resolve/main/config.json" ), "umberto-wikipedia-uncased-v1": ( "https://huggingface.co/Musixmatch/umberto-wikipedia-uncased-v1/resolve/main/config.json" ), } class __a ( _snake_case ): __UpperCamelCase : Tuple = 'camembert' def __init__( self : int ,lowerCamelCase : List[Any]=3_0522 ,lowerCamelCase : List[Any]=768 ,lowerCamelCase : str=12 ,lowerCamelCase : List[str]=12 ,lowerCamelCase : Optional[Any]=3072 ,lowerCamelCase : Tuple="gelu" ,lowerCamelCase : List[str]=0.1 ,lowerCamelCase : Tuple=0.1 ,lowerCamelCase : Union[str, Any]=512 ,lowerCamelCase : Dict=2 ,lowerCamelCase : Tuple=0.02 ,lowerCamelCase : List[Any]=1E-1_2 ,lowerCamelCase : Union[str, Any]=1 ,lowerCamelCase : Optional[Any]=0 ,lowerCamelCase : List[Any]=2 ,lowerCamelCase : List[str]="absolute" ,lowerCamelCase : int=True ,lowerCamelCase : Any=None ,**lowerCamelCase : Optional[Any] ,): '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase ,bos_token_id=lowerCamelCase ,eos_token_id=lowerCamelCase ,**lowerCamelCase ) __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 = position_embedding_type __SCREAMING_SNAKE_CASE = use_cache __SCREAMING_SNAKE_CASE = classifier_dropout class __a ( _snake_case ): @property def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' 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), ] )
13
0
import argparse import torch from transformers import YosoConfig, YosoForMaskedLM def __magic_name__ ( __UpperCAmelCase ) -> List[str]: '''simple docstring''' if "model" in orig_key: __SCREAMING_SNAKE_CASE = orig_key.replace("""model.""" , """""" ) if "norm1" in orig_key: __SCREAMING_SNAKE_CASE = orig_key.replace("""norm1""" , """attention.output.LayerNorm""" ) if "norm2" in orig_key: __SCREAMING_SNAKE_CASE = orig_key.replace("""norm2""" , """output.LayerNorm""" ) if "norm" in orig_key: __SCREAMING_SNAKE_CASE = orig_key.replace("""norm""" , """LayerNorm""" ) if "transformer" in orig_key: __SCREAMING_SNAKE_CASE = orig_key.split(""".""" )[0].split("""_""" )[-1] __SCREAMING_SNAKE_CASE = orig_key.replace(f"""transformer_{layer_num}""" , f"""encoder.layer.{layer_num}""" ) if "mha.attn" in orig_key: __SCREAMING_SNAKE_CASE = orig_key.replace("""mha.attn""" , """attention.self""" ) if "mha" in orig_key: __SCREAMING_SNAKE_CASE = orig_key.replace("""mha""" , """attention""" ) if "W_q" in orig_key: __SCREAMING_SNAKE_CASE = orig_key.replace("""W_q""" , """self.query""" ) if "W_k" in orig_key: __SCREAMING_SNAKE_CASE = orig_key.replace("""W_k""" , """self.key""" ) if "W_v" in orig_key: __SCREAMING_SNAKE_CASE = orig_key.replace("""W_v""" , """self.value""" ) if "ff1" in orig_key: __SCREAMING_SNAKE_CASE = orig_key.replace("""ff1""" , """intermediate.dense""" ) if "ff2" in orig_key: __SCREAMING_SNAKE_CASE = orig_key.replace("""ff2""" , """output.dense""" ) if "ff" in orig_key: __SCREAMING_SNAKE_CASE = orig_key.replace("""ff""" , """output.dense""" ) if "mlm_class" in orig_key: __SCREAMING_SNAKE_CASE = orig_key.replace("""mlm.mlm_class""" , """cls.predictions.decoder""" ) if "mlm" in orig_key: __SCREAMING_SNAKE_CASE = orig_key.replace("""mlm""" , """cls.predictions.transform""" ) if "cls" not in orig_key: __SCREAMING_SNAKE_CASE = """yoso.""" + orig_key return orig_key def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' for key in orig_state_dict.copy().keys(): __SCREAMING_SNAKE_CASE = orig_state_dict.pop(_UpperCAmelCase ) if ("pooler" in key) or ("sen_class" in key): continue else: __SCREAMING_SNAKE_CASE = val __SCREAMING_SNAKE_CASE = orig_state_dict["""cls.predictions.decoder.bias"""] __SCREAMING_SNAKE_CASE = torch.arange(_UpperCAmelCase ).expand((1, -1) ) + 2 return orig_state_dict def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> int: '''simple docstring''' __SCREAMING_SNAKE_CASE = torch.load(_UpperCAmelCase , map_location="""cpu""" )["""model_state_dict"""] __SCREAMING_SNAKE_CASE = YosoConfig.from_json_file(_UpperCAmelCase ) __SCREAMING_SNAKE_CASE = YosoForMaskedLM(_UpperCAmelCase ) __SCREAMING_SNAKE_CASE = convert_checkpoint_helper(config.max_position_embeddings , _UpperCAmelCase ) print(model.load_state_dict(_UpperCAmelCase ) ) model.eval() model.save_pretrained(_UpperCAmelCase ) print(f"""Checkpoint successfuly converted. Model saved at {pytorch_dump_path}""" ) if __name__ == "__main__": a = argparse.ArgumentParser() # Required parameters parser.add_argument( "--pytorch_model_path", default=None, type=str, required=True, help="Path to YOSO pytorch checkpoint." ) parser.add_argument( "--config_file", default=None, type=str, required=True, help="The json file for YOSO model config.", ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) a = parser.parse_args() convert_yoso_checkpoint(args.pytorch_model_path, args.config_file, args.pytorch_dump_path)
715
'''simple docstring''' import inspect import unittest import numpy as np from transformers import ViTConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor if is_flax_available(): import jax from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel class __a ( unittest.TestCase ): def __init__( self : Optional[int] ,lowerCamelCase : str ,lowerCamelCase : List[str]=13 ,lowerCamelCase : Optional[Any]=30 ,lowerCamelCase : Dict=2 ,lowerCamelCase : List[Any]=3 ,lowerCamelCase : List[str]=True ,lowerCamelCase : str=True ,lowerCamelCase : Optional[int]=32 ,lowerCamelCase : Dict=5 ,lowerCamelCase : Optional[int]=4 ,lowerCamelCase : List[Any]=37 ,lowerCamelCase : Union[str, Any]="gelu" ,lowerCamelCase : List[Any]=0.1 ,lowerCamelCase : Any=0.1 ,lowerCamelCase : str=10 ,lowerCamelCase : Dict=0.02 ,): '''simple docstring''' __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 # 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 UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __SCREAMING_SNAKE_CASE = 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 ,) return config, pixel_values def UpperCAmelCase__ ( self : Tuple ,lowerCamelCase : int ,lowerCamelCase : Optional[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = FlaxViTModel(config=lowerCamelCase ) __SCREAMING_SNAKE_CASE = model(lowerCamelCase ) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) __SCREAMING_SNAKE_CASE = (self.image_size, self.image_size) __SCREAMING_SNAKE_CASE = (self.patch_size, self.patch_size) __SCREAMING_SNAKE_CASE = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, num_patches + 1, self.hidden_size) ) def UpperCAmelCase__ ( self : Union[str, Any] ,lowerCamelCase : Optional[int] ,lowerCamelCase : Dict ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.type_sequence_label_size __SCREAMING_SNAKE_CASE = FlaxViTForImageClassification(config=lowerCamelCase ) __SCREAMING_SNAKE_CASE = model(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 = FlaxViTForImageClassification(lowerCamelCase ) __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) __SCREAMING_SNAKE_CASE = model(lowerCamelCase ) def UpperCAmelCase__ ( self : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ) = config_and_inputs __SCREAMING_SNAKE_CASE = {"""pixel_values""": pixel_values} return config, inputs_dict @require_flax class __a ( _snake_case, unittest.TestCase ): __UpperCamelCase : Any = (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else () def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = FlaxViTModelTester(self ) __SCREAMING_SNAKE_CASE = ConfigTester(self ,config_class=lowerCamelCase ,has_text_modality=lowerCamelCase ,hidden_size=37 ) def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self : List[str] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' __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 UpperCAmelCase__ ( self : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __SCREAMING_SNAKE_CASE = self._prepare_for_class(lowerCamelCase ,lowerCamelCase ) __SCREAMING_SNAKE_CASE = model_class(lowerCamelCase ) @jax.jit def model_jitted(lowerCamelCase : int ,**lowerCamelCase : Union[str, Any] ): return model(pixel_values=lowerCamelCase ,**lowerCamelCase ) with self.subTest("""JIT Enabled""" ): __SCREAMING_SNAKE_CASE = model_jitted(**lowerCamelCase ).to_tuple() with self.subTest("""JIT Disabled""" ): with jax.disable_jit(): __SCREAMING_SNAKE_CASE = model_jitted(**lowerCamelCase ).to_tuple() self.assertEqual(len(lowerCamelCase ) ,len(lowerCamelCase ) ) for jitted_output, output in zip(lowerCamelCase ,lowerCamelCase ): self.assertEqual(jitted_output.shape ,output.shape ) @slow def UpperCAmelCase__ ( self : Optional[Any] ): '''simple docstring''' for model_class_name in self.all_model_classes: __SCREAMING_SNAKE_CASE = model_class_name.from_pretrained("""google/vit-base-patch16-224""" ) __SCREAMING_SNAKE_CASE = model(np.ones((1, 3, 224, 224) ) ) self.assertIsNotNone(lowerCamelCase )
13
0
'''simple docstring''' import numpy as np import torch from torch.utils.data import DataLoader from accelerate.utils.dataclasses import DistributedType class __a : def __init__( self : Optional[Any] ,lowerCamelCase : List[str]=2 ,lowerCamelCase : Tuple=3 ,lowerCamelCase : Union[str, Any]=64 ,lowerCamelCase : Any=None ): '''simple docstring''' __SCREAMING_SNAKE_CASE : List[Any] = np.random.default_rng(__a ) __SCREAMING_SNAKE_CASE : Dict = length __SCREAMING_SNAKE_CASE : str = rng.normal(size=(length,) ).astype(np.floataa ) __SCREAMING_SNAKE_CASE : int = a * self.x + b + rng.normal(scale=0.1 ,size=(length,) ).astype(np.floataa ) def __len__( self : List[str] ): '''simple docstring''' return self.length def __getitem__( self : Optional[int] ,lowerCamelCase : int ): '''simple docstring''' return {"x": self.x[i], "y": self.y[i]} class __a ( torch.nn.Module ): def __init__( self : List[Any] ,lowerCamelCase : Union[str, Any]=0 ,lowerCamelCase : Optional[Any]=0 ,lowerCamelCase : Dict=False ): '''simple docstring''' super().__init__() __SCREAMING_SNAKE_CASE : Optional[Any] = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) __SCREAMING_SNAKE_CASE : List[Any] = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) __SCREAMING_SNAKE_CASE : str = True def UpperCAmelCase__ ( self : Dict ,lowerCamelCase : Optional[int]=None ): '''simple docstring''' if self.first_batch: print(f"""Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}""" ) __SCREAMING_SNAKE_CASE : Optional[Any] = False return x * self.a[0] + self.b[0] class __a ( torch.nn.Module ): def __init__( self : Optional[Any] ,lowerCamelCase : str=0 ,lowerCamelCase : Optional[int]=0 ,lowerCamelCase : Any=False ): '''simple docstring''' super().__init__() __SCREAMING_SNAKE_CASE : int = torch.nn.Parameter(torch.tensor(__a ).float() ) __SCREAMING_SNAKE_CASE : Tuple = torch.nn.Parameter(torch.tensor(__a ).float() ) __SCREAMING_SNAKE_CASE : List[str] = True def UpperCAmelCase__ ( self : Optional[int] ,lowerCamelCase : Optional[Any]=None ): '''simple docstring''' if self.first_batch: print(f"""Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}""" ) __SCREAMING_SNAKE_CASE : Dict = False return x * self.a + self.b def __magic_name__ ( __UpperCAmelCase , __UpperCAmelCase = 16 ) -> int: '''simple docstring''' from datasets import load_dataset from transformers import AutoTokenizer __SCREAMING_SNAKE_CASE : List[str] = AutoTokenizer.from_pretrained("""bert-base-cased""" ) __SCREAMING_SNAKE_CASE : Optional[Any] = {'train': 'tests/test_samples/MRPC/train.csv', 'validation': 'tests/test_samples/MRPC/dev.csv'} __SCREAMING_SNAKE_CASE : Tuple = load_dataset("""csv""" , data_files=__UpperCAmelCase ) __SCREAMING_SNAKE_CASE : Optional[int] = datasets['train'].unique("""label""" ) __SCREAMING_SNAKE_CASE : Optional[Any] = {v: i for i, v in enumerate(__UpperCAmelCase )} def tokenize_function(__UpperCAmelCase ): # max_length=None => use the model max length (it's actually the default) __SCREAMING_SNAKE_CASE : Tuple = tokenizer( examples["""sentence1"""] , examples["""sentence2"""] , truncation=__UpperCAmelCase , max_length=__UpperCAmelCase , padding="""max_length""" ) if "label" in examples: __SCREAMING_SNAKE_CASE : Tuple = [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 : List[str] = datasets.map( __UpperCAmelCase , batched=__UpperCAmelCase , remove_columns=["""sentence1""", """sentence2""", """label"""] , ) def collate_fn(__UpperCAmelCase ): # 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=128 , return_tensors="""pt""" ) return tokenizer.pad(__UpperCAmelCase , padding="""longest""" , return_tensors="""pt""" ) # Instantiate dataloaders. __SCREAMING_SNAKE_CASE : Tuple = DataLoader(tokenized_datasets["""train"""] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=2 ) __SCREAMING_SNAKE_CASE : Union[str, Any] = DataLoader(tokenized_datasets["""validation"""] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=1 ) return train_dataloader, eval_dataloader
716
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging a = logging.get_logger(__name__) a = { "asapp/sew-tiny-100k": "https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json", # See all SEW models at https://huggingface.co/models?filter=sew } class __a ( _snake_case ): __UpperCamelCase : Tuple = 'sew' def __init__( self : str ,lowerCamelCase : Any=32 ,lowerCamelCase : str=768 ,lowerCamelCase : str=12 ,lowerCamelCase : Union[str, Any]=12 ,lowerCamelCase : Union[str, Any]=3072 ,lowerCamelCase : int=2 ,lowerCamelCase : Union[str, Any]="gelu" ,lowerCamelCase : Tuple=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : Any=0.0 ,lowerCamelCase : Optional[Any]=0.1 ,lowerCamelCase : Union[str, Any]=0.1 ,lowerCamelCase : Optional[Any]=0.02 ,lowerCamelCase : List[str]=1E-5 ,lowerCamelCase : Tuple="group" ,lowerCamelCase : Optional[Any]="gelu" ,lowerCamelCase : List[str]=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) ,lowerCamelCase : Any=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) ,lowerCamelCase : Dict=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) ,lowerCamelCase : Optional[int]=False ,lowerCamelCase : Dict=128 ,lowerCamelCase : Union[str, Any]=16 ,lowerCamelCase : List[Any]=True ,lowerCamelCase : List[Any]=0.05 ,lowerCamelCase : Optional[int]=10 ,lowerCamelCase : Any=2 ,lowerCamelCase : Any=0.0 ,lowerCamelCase : Tuple=10 ,lowerCamelCase : str=0 ,lowerCamelCase : Tuple="mean" ,lowerCamelCase : int=False ,lowerCamelCase : Dict=False ,lowerCamelCase : Optional[int]=256 ,lowerCamelCase : str=0 ,lowerCamelCase : Tuple=1 ,lowerCamelCase : Tuple=2 ,**lowerCamelCase : Union[str, Any] ,): '''simple docstring''' super().__init__(**lowerCamelCase ,pad_token_id=lowerCamelCase ,bos_token_id=lowerCamelCase ,eos_token_id=lowerCamelCase ) __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = feat_extract_norm __SCREAMING_SNAKE_CASE = feat_extract_activation __SCREAMING_SNAKE_CASE = list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = list(lowerCamelCase ) __SCREAMING_SNAKE_CASE = conv_bias __SCREAMING_SNAKE_CASE = num_conv_pos_embeddings __SCREAMING_SNAKE_CASE = num_conv_pos_embedding_groups __SCREAMING_SNAKE_CASE = len(self.conv_dim ) __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = squeeze_factor __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = hidden_dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = feat_proj_dropout __SCREAMING_SNAKE_CASE = final_dropout __SCREAMING_SNAKE_CASE = layerdrop __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect.""" """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,""" f"""but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)""" f"""= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __SCREAMING_SNAKE_CASE = apply_spec_augment __SCREAMING_SNAKE_CASE = mask_time_prob __SCREAMING_SNAKE_CASE = mask_time_length __SCREAMING_SNAKE_CASE = mask_time_min_masks __SCREAMING_SNAKE_CASE = mask_feature_prob __SCREAMING_SNAKE_CASE = mask_feature_length __SCREAMING_SNAKE_CASE = mask_feature_min_masks # ctc loss __SCREAMING_SNAKE_CASE = ctc_loss_reduction __SCREAMING_SNAKE_CASE = ctc_zero_infinity # sequence classification __SCREAMING_SNAKE_CASE = use_weighted_layer_sum __SCREAMING_SNAKE_CASE = classifier_proj_size @property def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' return functools.reduce(operator.mul ,self.conv_stride ,1 )
13
0